poly_int: MEM_REF offsets
[official-gcc.git] / gcc / expr.c
blob4e7349e649488dcf3f763e230996663afd0d0b21
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2017 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 "expmed.h"
33 #include "optabs.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-ssa-live.h"
58 #include "tree-outof-ssa.h"
59 #include "tree-ssa-address.h"
60 #include "builtins.h"
61 #include "tree-chkp.h"
62 #include "rtl-chkp.h"
63 #include "ccmp.h"
66 /* If this is nonzero, we do not bother generating VOLATILE
67 around volatile memory references, and we are willing to
68 output indirect addresses. If cse is to follow, we reject
69 indirect addresses so a useful potential cse is generated;
70 if it is used only once, instruction combination will produce
71 the same indirect address eventually. */
72 int cse_not_expected;
74 static bool block_move_libcall_safe_for_call_parm (void);
75 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
76 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT);
78 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
79 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
80 static rtx_insn *compress_float_constant (rtx, rtx);
81 static rtx get_subtarget (rtx);
82 static void store_constructor (tree, rtx, int, poly_int64, bool);
83 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
84 machine_mode, tree, alias_set_type, bool, bool);
86 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
88 static int is_aligning_offset (const_tree, const_tree);
89 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
90 static rtx do_store_flag (sepops, rtx, machine_mode);
91 #ifdef PUSH_ROUNDING
92 static void emit_single_push_insn (machine_mode, rtx, tree);
93 #endif
94 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
95 profile_probability);
96 static rtx const_vector_from_tree (tree);
97 static rtx const_scalar_mask_from_tree (scalar_int_mode, tree);
98 static tree tree_expr_size (const_tree);
99 static HOST_WIDE_INT int_expr_size (tree);
100 static void convert_mode_scalar (rtx, rtx, int);
103 /* This is run to set up which modes can be used
104 directly in memory and to initialize the block move optab. It is run
105 at the beginning of compilation and when the target is reinitialized. */
107 void
108 init_expr_target (void)
110 rtx pat;
111 int num_clobbers;
112 rtx mem, mem1;
113 rtx reg;
115 /* Try indexing by frame ptr and try by stack ptr.
116 It is known that on the Convex the stack ptr isn't a valid index.
117 With luck, one or the other is valid on any machine. */
118 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
119 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
121 /* A scratch register we can modify in-place below to avoid
122 useless RTL allocations. */
123 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
125 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
126 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
127 PATTERN (insn) = pat;
129 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
130 mode = (machine_mode) ((int) mode + 1))
132 int regno;
134 direct_load[(int) mode] = direct_store[(int) mode] = 0;
135 PUT_MODE (mem, mode);
136 PUT_MODE (mem1, mode);
138 /* See if there is some register that can be used in this mode and
139 directly loaded or stored from memory. */
141 if (mode != VOIDmode && mode != BLKmode)
142 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
143 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
144 regno++)
146 if (!targetm.hard_regno_mode_ok (regno, mode))
147 continue;
149 set_mode_and_regno (reg, mode, regno);
151 SET_SRC (pat) = mem;
152 SET_DEST (pat) = reg;
153 if (recog (pat, insn, &num_clobbers) >= 0)
154 direct_load[(int) mode] = 1;
156 SET_SRC (pat) = mem1;
157 SET_DEST (pat) = reg;
158 if (recog (pat, insn, &num_clobbers) >= 0)
159 direct_load[(int) mode] = 1;
161 SET_SRC (pat) = reg;
162 SET_DEST (pat) = mem;
163 if (recog (pat, insn, &num_clobbers) >= 0)
164 direct_store[(int) mode] = 1;
166 SET_SRC (pat) = reg;
167 SET_DEST (pat) = mem1;
168 if (recog (pat, insn, &num_clobbers) >= 0)
169 direct_store[(int) mode] = 1;
173 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
175 opt_scalar_float_mode mode_iter;
176 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
178 scalar_float_mode mode = mode_iter.require ();
179 scalar_float_mode srcmode;
180 FOR_EACH_MODE_UNTIL (srcmode, mode)
182 enum insn_code ic;
184 ic = can_extend_p (mode, srcmode, 0);
185 if (ic == CODE_FOR_nothing)
186 continue;
188 PUT_MODE (mem, srcmode);
190 if (insn_operand_matches (ic, 1, mem))
191 float_extend_from_mem[mode][srcmode] = true;
196 /* This is run at the start of compiling a function. */
198 void
199 init_expr (void)
201 memset (&crtl->expr, 0, sizeof (crtl->expr));
204 /* Copy data from FROM to TO, where the machine modes are not the same.
205 Both modes may be integer, or both may be floating, or both may be
206 fixed-point.
207 UNSIGNEDP should be nonzero if FROM is an unsigned type.
208 This causes zero-extension instead of sign-extension. */
210 void
211 convert_move (rtx to, rtx from, int unsignedp)
213 machine_mode to_mode = GET_MODE (to);
214 machine_mode from_mode = GET_MODE (from);
216 gcc_assert (to_mode != BLKmode);
217 gcc_assert (from_mode != BLKmode);
219 /* If the source and destination are already the same, then there's
220 nothing to do. */
221 if (to == from)
222 return;
224 /* If FROM is a SUBREG that indicates that we have already done at least
225 the required extension, strip it. We don't handle such SUBREGs as
226 TO here. */
228 scalar_int_mode to_int_mode;
229 if (GET_CODE (from) == SUBREG
230 && SUBREG_PROMOTED_VAR_P (from)
231 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
232 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
233 >= GET_MODE_PRECISION (to_int_mode))
234 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
235 from = gen_lowpart (to_int_mode, from), from_mode = to_int_mode;
237 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
239 if (to_mode == from_mode
240 || (from_mode == VOIDmode && CONSTANT_P (from)))
242 emit_move_insn (to, from);
243 return;
246 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
248 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
250 if (VECTOR_MODE_P (to_mode))
251 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
252 else
253 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
255 emit_move_insn (to, from);
256 return;
259 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
261 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
262 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
263 return;
266 convert_mode_scalar (to, from, unsignedp);
269 /* Like convert_move, but deals only with scalar modes. */
271 static void
272 convert_mode_scalar (rtx to, rtx from, int unsignedp)
274 /* Both modes should be scalar types. */
275 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
276 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
277 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
278 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
279 enum insn_code code;
280 rtx libcall;
282 gcc_assert (to_real == from_real);
284 /* rtx code for making an equivalent value. */
285 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
286 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
288 if (to_real)
290 rtx value;
291 rtx_insn *insns;
292 convert_optab tab;
294 gcc_assert ((GET_MODE_PRECISION (from_mode)
295 != GET_MODE_PRECISION (to_mode))
296 || (DECIMAL_FLOAT_MODE_P (from_mode)
297 != DECIMAL_FLOAT_MODE_P (to_mode)));
299 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
300 /* Conversion between decimal float and binary float, same size. */
301 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
302 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
303 tab = sext_optab;
304 else
305 tab = trunc_optab;
307 /* Try converting directly if the insn is supported. */
309 code = convert_optab_handler (tab, to_mode, from_mode);
310 if (code != CODE_FOR_nothing)
312 emit_unop_insn (code, to, from,
313 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
314 return;
317 /* Otherwise use a libcall. */
318 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
320 /* Is this conversion implemented yet? */
321 gcc_assert (libcall);
323 start_sequence ();
324 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
325 from, from_mode);
326 insns = get_insns ();
327 end_sequence ();
328 emit_libcall_block (insns, to, value,
329 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
330 from)
331 : gen_rtx_FLOAT_EXTEND (to_mode, from));
332 return;
335 /* Handle pointer conversion. */ /* SPEE 900220. */
336 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
338 convert_optab ctab;
340 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
341 ctab = trunc_optab;
342 else if (unsignedp)
343 ctab = zext_optab;
344 else
345 ctab = sext_optab;
347 if (convert_optab_handler (ctab, to_mode, from_mode)
348 != CODE_FOR_nothing)
350 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
351 to, from, UNKNOWN);
352 return;
356 /* Targets are expected to provide conversion insns between PxImode and
357 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
358 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
360 scalar_int_mode full_mode
361 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
363 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
364 != CODE_FOR_nothing);
366 if (full_mode != from_mode)
367 from = convert_to_mode (full_mode, from, unsignedp);
368 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
369 to, from, UNKNOWN);
370 return;
372 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
374 rtx new_from;
375 scalar_int_mode full_mode
376 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
377 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
378 enum insn_code icode;
380 icode = convert_optab_handler (ctab, full_mode, from_mode);
381 gcc_assert (icode != CODE_FOR_nothing);
383 if (to_mode == full_mode)
385 emit_unop_insn (icode, to, from, UNKNOWN);
386 return;
389 new_from = gen_reg_rtx (full_mode);
390 emit_unop_insn (icode, new_from, from, UNKNOWN);
392 /* else proceed to integer conversions below. */
393 from_mode = full_mode;
394 from = new_from;
397 /* Make sure both are fixed-point modes or both are not. */
398 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
399 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
400 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
402 /* If we widen from_mode to to_mode and they are in the same class,
403 we won't saturate the result.
404 Otherwise, always saturate the result to play safe. */
405 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
406 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
407 expand_fixed_convert (to, from, 0, 0);
408 else
409 expand_fixed_convert (to, from, 0, 1);
410 return;
413 /* Now both modes are integers. */
415 /* Handle expanding beyond a word. */
416 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
417 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
419 rtx_insn *insns;
420 rtx lowpart;
421 rtx fill_value;
422 rtx lowfrom;
423 int i;
424 scalar_mode lowpart_mode;
425 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
427 /* Try converting directly if the insn is supported. */
428 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
429 != CODE_FOR_nothing)
431 /* If FROM is a SUBREG, put it into a register. Do this
432 so that we always generate the same set of insns for
433 better cse'ing; if an intermediate assignment occurred,
434 we won't be doing the operation directly on the SUBREG. */
435 if (optimize > 0 && GET_CODE (from) == SUBREG)
436 from = force_reg (from_mode, from);
437 emit_unop_insn (code, to, from, equiv_code);
438 return;
440 /* Next, try converting via full word. */
441 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
442 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
443 != CODE_FOR_nothing))
445 rtx word_to = gen_reg_rtx (word_mode);
446 if (REG_P (to))
448 if (reg_overlap_mentioned_p (to, from))
449 from = force_reg (from_mode, from);
450 emit_clobber (to);
452 convert_move (word_to, from, unsignedp);
453 emit_unop_insn (code, to, word_to, equiv_code);
454 return;
457 /* No special multiword conversion insn; do it by hand. */
458 start_sequence ();
460 /* Since we will turn this into a no conflict block, we must ensure
461 the source does not overlap the target so force it into an isolated
462 register when maybe so. Likewise for any MEM input, since the
463 conversion sequence might require several references to it and we
464 must ensure we're getting the same value every time. */
466 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
467 from = force_reg (from_mode, from);
469 /* Get a copy of FROM widened to a word, if necessary. */
470 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
471 lowpart_mode = word_mode;
472 else
473 lowpart_mode = from_mode;
475 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
477 lowpart = gen_lowpart (lowpart_mode, to);
478 emit_move_insn (lowpart, lowfrom);
480 /* Compute the value to put in each remaining word. */
481 if (unsignedp)
482 fill_value = const0_rtx;
483 else
484 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
485 LT, lowfrom, const0_rtx,
486 lowpart_mode, 0, -1);
488 /* Fill the remaining words. */
489 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
491 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
492 rtx subword = operand_subword (to, index, 1, to_mode);
494 gcc_assert (subword);
496 if (fill_value != subword)
497 emit_move_insn (subword, fill_value);
500 insns = get_insns ();
501 end_sequence ();
503 emit_insn (insns);
504 return;
507 /* Truncating multi-word to a word or less. */
508 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
509 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
511 if (!((MEM_P (from)
512 && ! MEM_VOLATILE_P (from)
513 && direct_load[(int) to_mode]
514 && ! mode_dependent_address_p (XEXP (from, 0),
515 MEM_ADDR_SPACE (from)))
516 || REG_P (from)
517 || GET_CODE (from) == SUBREG))
518 from = force_reg (from_mode, from);
519 convert_move (to, gen_lowpart (word_mode, from), 0);
520 return;
523 /* Now follow all the conversions between integers
524 no more than a word long. */
526 /* For truncation, usually we can just refer to FROM in a narrower mode. */
527 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
528 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
530 if (!((MEM_P (from)
531 && ! MEM_VOLATILE_P (from)
532 && direct_load[(int) to_mode]
533 && ! mode_dependent_address_p (XEXP (from, 0),
534 MEM_ADDR_SPACE (from)))
535 || REG_P (from)
536 || GET_CODE (from) == SUBREG))
537 from = force_reg (from_mode, from);
538 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
539 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
540 from = copy_to_reg (from);
541 emit_move_insn (to, gen_lowpart (to_mode, from));
542 return;
545 /* Handle extension. */
546 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
548 /* Convert directly if that works. */
549 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
550 != CODE_FOR_nothing)
552 emit_unop_insn (code, to, from, equiv_code);
553 return;
555 else
557 scalar_mode intermediate;
558 rtx tmp;
559 int shift_amount;
561 /* Search for a mode to convert via. */
562 opt_scalar_mode intermediate_iter;
563 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
565 scalar_mode intermediate = intermediate_iter.require ();
566 if (((can_extend_p (to_mode, intermediate, unsignedp)
567 != CODE_FOR_nothing)
568 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
569 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
570 intermediate)))
571 && (can_extend_p (intermediate, from_mode, unsignedp)
572 != CODE_FOR_nothing))
574 convert_move (to, convert_to_mode (intermediate, from,
575 unsignedp), unsignedp);
576 return;
580 /* No suitable intermediate mode.
581 Generate what we need with shifts. */
582 shift_amount = (GET_MODE_PRECISION (to_mode)
583 - GET_MODE_PRECISION (from_mode));
584 from = gen_lowpart (to_mode, force_reg (from_mode, from));
585 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
586 to, unsignedp);
587 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
588 to, unsignedp);
589 if (tmp != to)
590 emit_move_insn (to, tmp);
591 return;
595 /* Support special truncate insns for certain modes. */
596 if (convert_optab_handler (trunc_optab, to_mode,
597 from_mode) != CODE_FOR_nothing)
599 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
600 to, from, UNKNOWN);
601 return;
604 /* Handle truncation of volatile memrefs, and so on;
605 the things that couldn't be truncated directly,
606 and for which there was no special instruction.
608 ??? Code above formerly short-circuited this, for most integer
609 mode pairs, with a force_reg in from_mode followed by a recursive
610 call to this routine. Appears always to have been wrong. */
611 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
613 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
614 emit_move_insn (to, temp);
615 return;
618 /* Mode combination is not recognized. */
619 gcc_unreachable ();
622 /* Return an rtx for a value that would result
623 from converting X to mode MODE.
624 Both X and MODE may be floating, or both integer.
625 UNSIGNEDP is nonzero if X is an unsigned value.
626 This can be done by referring to a part of X in place
627 or by copying to a new temporary with conversion. */
630 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
632 return convert_modes (mode, VOIDmode, x, unsignedp);
635 /* Return an rtx for a value that would result
636 from converting X from mode OLDMODE to mode MODE.
637 Both modes may be floating, or both integer.
638 UNSIGNEDP is nonzero if X is an unsigned value.
640 This can be done by referring to a part of X in place
641 or by copying to a new temporary with conversion.
643 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
646 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
648 rtx temp;
649 scalar_int_mode int_mode;
651 /* If FROM is a SUBREG that indicates that we have already done at least
652 the required extension, strip it. */
654 if (GET_CODE (x) == SUBREG
655 && SUBREG_PROMOTED_VAR_P (x)
656 && is_a <scalar_int_mode> (mode, &int_mode)
657 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
658 >= GET_MODE_PRECISION (int_mode))
659 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
660 x = gen_lowpart (int_mode, SUBREG_REG (x));
662 if (GET_MODE (x) != VOIDmode)
663 oldmode = GET_MODE (x);
665 if (mode == oldmode)
666 return x;
668 if (CONST_SCALAR_INT_P (x)
669 && is_int_mode (mode, &int_mode))
671 /* If the caller did not tell us the old mode, then there is not
672 much to do with respect to canonicalization. We have to
673 assume that all the bits are significant. */
674 if (GET_MODE_CLASS (oldmode) != MODE_INT)
675 oldmode = MAX_MODE_INT;
676 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
677 GET_MODE_PRECISION (int_mode),
678 unsignedp ? UNSIGNED : SIGNED);
679 return immed_wide_int_const (w, int_mode);
682 /* We can do this with a gen_lowpart if both desired and current modes
683 are integer, and this is either a constant integer, a register, or a
684 non-volatile MEM. */
685 scalar_int_mode int_oldmode;
686 if (is_int_mode (mode, &int_mode)
687 && is_int_mode (oldmode, &int_oldmode)
688 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
689 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
690 || CONST_POLY_INT_P (x)
691 || (REG_P (x)
692 && (!HARD_REGISTER_P (x)
693 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
694 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
695 return gen_lowpart (int_mode, x);
697 /* Converting from integer constant into mode is always equivalent to an
698 subreg operation. */
699 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
701 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
702 return simplify_gen_subreg (mode, x, oldmode, 0);
705 temp = gen_reg_rtx (mode);
706 convert_move (temp, x, unsignedp);
707 return temp;
710 /* Return the largest alignment we can use for doing a move (or store)
711 of MAX_PIECES. ALIGN is the largest alignment we could use. */
713 static unsigned int
714 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
716 scalar_int_mode tmode
717 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 1).require ();
719 if (align >= GET_MODE_ALIGNMENT (tmode))
720 align = GET_MODE_ALIGNMENT (tmode);
721 else
723 scalar_int_mode xmode = NARROWEST_INT_MODE;
724 opt_scalar_int_mode mode_iter;
725 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
727 tmode = mode_iter.require ();
728 if (GET_MODE_SIZE (tmode) > max_pieces
729 || targetm.slow_unaligned_access (tmode, align))
730 break;
731 xmode = tmode;
734 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
737 return align;
740 /* Return the widest integer mode that is narrower than SIZE bytes. */
742 static scalar_int_mode
743 widest_int_mode_for_size (unsigned int size)
745 scalar_int_mode result = NARROWEST_INT_MODE;
747 gcc_checking_assert (size > 1);
749 opt_scalar_int_mode tmode;
750 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
751 if (GET_MODE_SIZE (tmode.require ()) < size)
752 result = tmode.require ();
754 return result;
757 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
758 and should be performed piecewise. */
760 static bool
761 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
762 enum by_pieces_operation op)
764 return targetm.use_by_pieces_infrastructure_p (len, align, op,
765 optimize_insn_for_speed_p ());
768 /* Determine whether the LEN bytes can be moved by using several move
769 instructions. Return nonzero if a call to move_by_pieces should
770 succeed. */
772 bool
773 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
775 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
778 /* Return number of insns required to perform operation OP by pieces
779 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
781 unsigned HOST_WIDE_INT
782 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
783 unsigned int max_size, by_pieces_operation op)
785 unsigned HOST_WIDE_INT n_insns = 0;
787 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
789 while (max_size > 1 && l > 0)
791 scalar_int_mode mode = widest_int_mode_for_size (max_size);
792 enum insn_code icode;
794 unsigned int modesize = GET_MODE_SIZE (mode);
796 icode = optab_handler (mov_optab, mode);
797 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
799 unsigned HOST_WIDE_INT n_pieces = l / modesize;
800 l %= modesize;
801 switch (op)
803 default:
804 n_insns += n_pieces;
805 break;
807 case COMPARE_BY_PIECES:
808 int batch = targetm.compare_by_pieces_branch_ratio (mode);
809 int batch_ops = 4 * batch - 1;
810 unsigned HOST_WIDE_INT full = n_pieces / batch;
811 n_insns += full * batch_ops;
812 if (n_pieces % batch != 0)
813 n_insns++;
814 break;
818 max_size = modesize;
821 gcc_assert (!l);
822 return n_insns;
825 /* Used when performing piecewise block operations, holds information
826 about one of the memory objects involved. The member functions
827 can be used to generate code for loading from the object and
828 updating the address when iterating. */
830 class pieces_addr
832 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
833 stack pushes. */
834 rtx m_obj;
835 /* The address of the object. Can differ from that seen in the
836 MEM rtx if we copied the address to a register. */
837 rtx m_addr;
838 /* Nonzero if the address on the object has an autoincrement already,
839 signifies whether that was an increment or decrement. */
840 signed char m_addr_inc;
841 /* Nonzero if we intend to use autoinc without the address already
842 having autoinc form. We will insert add insns around each memory
843 reference, expecting later passes to form autoinc addressing modes.
844 The only supported options are predecrement and postincrement. */
845 signed char m_explicit_inc;
846 /* True if we have either of the two possible cases of using
847 autoincrement. */
848 bool m_auto;
849 /* True if this is an address to be used for load operations rather
850 than stores. */
851 bool m_is_load;
853 /* Optionally, a function to obtain constants for any given offset into
854 the objects, and data associated with it. */
855 by_pieces_constfn m_constfn;
856 void *m_cfndata;
857 public:
858 pieces_addr (rtx, bool, by_pieces_constfn, void *);
859 rtx adjust (scalar_int_mode, HOST_WIDE_INT);
860 void increment_address (HOST_WIDE_INT);
861 void maybe_predec (HOST_WIDE_INT);
862 void maybe_postinc (HOST_WIDE_INT);
863 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
864 int get_addr_inc ()
866 return m_addr_inc;
870 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
871 true if the operation to be performed on this object is a load
872 rather than a store. For stores, OBJ can be NULL, in which case we
873 assume the operation is a stack push. For loads, the optional
874 CONSTFN and its associated CFNDATA can be used in place of the
875 memory load. */
877 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
878 void *cfndata)
879 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
881 m_addr_inc = 0;
882 m_auto = false;
883 if (obj)
885 rtx addr = XEXP (obj, 0);
886 rtx_code code = GET_CODE (addr);
887 m_addr = addr;
888 bool dec = code == PRE_DEC || code == POST_DEC;
889 bool inc = code == PRE_INC || code == POST_INC;
890 m_auto = inc || dec;
891 if (m_auto)
892 m_addr_inc = dec ? -1 : 1;
894 /* While we have always looked for these codes here, the code
895 implementing the memory operation has never handled them.
896 Support could be added later if necessary or beneficial. */
897 gcc_assert (code != PRE_INC && code != POST_DEC);
899 else
901 m_addr = NULL_RTX;
902 if (!is_load)
904 m_auto = true;
905 if (STACK_GROWS_DOWNWARD)
906 m_addr_inc = -1;
907 else
908 m_addr_inc = 1;
910 else
911 gcc_assert (constfn != NULL);
913 m_explicit_inc = 0;
914 if (constfn)
915 gcc_assert (is_load);
918 /* Decide whether to use autoinc for an address involved in a memory op.
919 MODE is the mode of the accesses, REVERSE is true if we've decided to
920 perform the operation starting from the end, and LEN is the length of
921 the operation. Don't override an earlier decision to set m_auto. */
923 void
924 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
925 HOST_WIDE_INT len)
927 if (m_auto || m_obj == NULL_RTX)
928 return;
930 bool use_predec = (m_is_load
931 ? USE_LOAD_PRE_DECREMENT (mode)
932 : USE_STORE_PRE_DECREMENT (mode));
933 bool use_postinc = (m_is_load
934 ? USE_LOAD_POST_INCREMENT (mode)
935 : USE_STORE_POST_INCREMENT (mode));
936 machine_mode addr_mode = get_address_mode (m_obj);
938 if (use_predec && reverse)
940 m_addr = copy_to_mode_reg (addr_mode,
941 plus_constant (addr_mode,
942 m_addr, len));
943 m_auto = true;
944 m_explicit_inc = -1;
946 else if (use_postinc && !reverse)
948 m_addr = copy_to_mode_reg (addr_mode, m_addr);
949 m_auto = true;
950 m_explicit_inc = 1;
952 else if (CONSTANT_P (m_addr))
953 m_addr = copy_to_mode_reg (addr_mode, m_addr);
956 /* Adjust the address to refer to the data at OFFSET in MODE. If we
957 are using autoincrement for this address, we don't add the offset,
958 but we still modify the MEM's properties. */
961 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset)
963 if (m_constfn)
964 return m_constfn (m_cfndata, offset, mode);
965 if (m_obj == NULL_RTX)
966 return NULL_RTX;
967 if (m_auto)
968 return adjust_automodify_address (m_obj, mode, m_addr, offset);
969 else
970 return adjust_address (m_obj, mode, offset);
973 /* Emit an add instruction to increment the address by SIZE. */
975 void
976 pieces_addr::increment_address (HOST_WIDE_INT size)
978 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
979 emit_insn (gen_add2_insn (m_addr, amount));
982 /* If we are supposed to decrement the address after each access, emit code
983 to do so now. Increment by SIZE (which has should have the correct sign
984 already). */
986 void
987 pieces_addr::maybe_predec (HOST_WIDE_INT size)
989 if (m_explicit_inc >= 0)
990 return;
991 gcc_assert (HAVE_PRE_DECREMENT);
992 increment_address (size);
995 /* If we are supposed to decrement the address after each access, emit code
996 to do so now. Increment by SIZE. */
998 void
999 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1001 if (m_explicit_inc <= 0)
1002 return;
1003 gcc_assert (HAVE_POST_INCREMENT);
1004 increment_address (size);
1007 /* This structure is used by do_op_by_pieces to describe the operation
1008 to be performed. */
1010 class op_by_pieces_d
1012 protected:
1013 pieces_addr m_to, m_from;
1014 unsigned HOST_WIDE_INT m_len;
1015 HOST_WIDE_INT m_offset;
1016 unsigned int m_align;
1017 unsigned int m_max_size;
1018 bool m_reverse;
1020 /* Virtual functions, overriden by derived classes for the specific
1021 operation. */
1022 virtual void generate (rtx, rtx, machine_mode) = 0;
1023 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1024 virtual void finish_mode (machine_mode)
1028 public:
1029 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1030 unsigned HOST_WIDE_INT, unsigned int);
1031 void run ();
1034 /* The constructor for an op_by_pieces_d structure. We require two
1035 objects named TO and FROM, which are identified as loads or stores
1036 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1037 and its associated FROM_CFN_DATA can be used to replace loads with
1038 constant values. LEN describes the length of the operation. */
1040 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1041 rtx from, bool from_load,
1042 by_pieces_constfn from_cfn,
1043 void *from_cfn_data,
1044 unsigned HOST_WIDE_INT len,
1045 unsigned int align)
1046 : m_to (to, to_load, NULL, NULL),
1047 m_from (from, from_load, from_cfn, from_cfn_data),
1048 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1050 int toi = m_to.get_addr_inc ();
1051 int fromi = m_from.get_addr_inc ();
1052 if (toi >= 0 && fromi >= 0)
1053 m_reverse = false;
1054 else if (toi <= 0 && fromi <= 0)
1055 m_reverse = true;
1056 else
1057 gcc_unreachable ();
1059 m_offset = m_reverse ? len : 0;
1060 align = MIN (to ? MEM_ALIGN (to) : align,
1061 from ? MEM_ALIGN (from) : align);
1063 /* If copying requires more than two move insns,
1064 copy addresses to registers (to make displacements shorter)
1065 and use post-increment if available. */
1066 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1068 /* Find the mode of the largest comparison. */
1069 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1071 m_from.decide_autoinc (mode, m_reverse, len);
1072 m_to.decide_autoinc (mode, m_reverse, len);
1075 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1076 m_align = align;
1079 /* This function contains the main loop used for expanding a block
1080 operation. First move what we can in the largest integer mode,
1081 then go to successively smaller modes. For every access, call
1082 GENFUN with the two operands and the EXTRA_DATA. */
1084 void
1085 op_by_pieces_d::run ()
1087 while (m_max_size > 1 && m_len > 0)
1089 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1091 if (prepare_mode (mode, m_align))
1093 unsigned int size = GET_MODE_SIZE (mode);
1094 rtx to1 = NULL_RTX, from1;
1096 while (m_len >= size)
1098 if (m_reverse)
1099 m_offset -= size;
1101 to1 = m_to.adjust (mode, m_offset);
1102 from1 = m_from.adjust (mode, m_offset);
1104 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1105 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1107 generate (to1, from1, mode);
1109 m_to.maybe_postinc (size);
1110 m_from.maybe_postinc (size);
1112 if (!m_reverse)
1113 m_offset += size;
1115 m_len -= size;
1118 finish_mode (mode);
1121 m_max_size = GET_MODE_SIZE (mode);
1124 /* The code above should have handled everything. */
1125 gcc_assert (!m_len);
1128 /* Derived class from op_by_pieces_d, providing support for block move
1129 operations. */
1131 class move_by_pieces_d : public op_by_pieces_d
1133 insn_gen_fn m_gen_fun;
1134 void generate (rtx, rtx, machine_mode);
1135 bool prepare_mode (machine_mode, unsigned int);
1137 public:
1138 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1139 unsigned int align)
1140 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1143 rtx finish_endp (int);
1146 /* Return true if MODE can be used for a set of copies, given an
1147 alignment ALIGN. Prepare whatever data is necessary for later
1148 calls to generate. */
1150 bool
1151 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1153 insn_code icode = optab_handler (mov_optab, mode);
1154 m_gen_fun = GEN_FCN (icode);
1155 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1158 /* A callback used when iterating for a compare_by_pieces_operation.
1159 OP0 and OP1 are the values that have been loaded and should be
1160 compared in MODE. If OP0 is NULL, this means we should generate a
1161 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1162 gen function that should be used to generate the mode. */
1164 void
1165 move_by_pieces_d::generate (rtx op0, rtx op1,
1166 machine_mode mode ATTRIBUTE_UNUSED)
1168 #ifdef PUSH_ROUNDING
1169 if (op0 == NULL_RTX)
1171 emit_single_push_insn (mode, op1, NULL);
1172 return;
1174 #endif
1175 emit_insn (m_gen_fun (op0, op1));
1178 /* Perform the final adjustment at the end of a string to obtain the
1179 correct return value for the block operation. If ENDP is 1 return
1180 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1181 end minus one byte ala stpcpy. */
1184 move_by_pieces_d::finish_endp (int endp)
1186 gcc_assert (!m_reverse);
1187 if (endp == 2)
1189 m_to.maybe_postinc (-1);
1190 --m_offset;
1192 return m_to.adjust (QImode, m_offset);
1195 /* Generate several move instructions to copy LEN bytes from block FROM to
1196 block TO. (These are MEM rtx's with BLKmode).
1198 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1199 used to push FROM to the stack.
1201 ALIGN is maximum stack alignment we can assume.
1203 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1204 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1205 stpcpy. */
1208 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1209 unsigned int align, int endp)
1211 #ifndef PUSH_ROUNDING
1212 if (to == NULL)
1213 gcc_unreachable ();
1214 #endif
1216 move_by_pieces_d data (to, from, len, align);
1218 data.run ();
1220 if (endp)
1221 return data.finish_endp (endp);
1222 else
1223 return to;
1226 /* Derived class from op_by_pieces_d, providing support for block move
1227 operations. */
1229 class store_by_pieces_d : public op_by_pieces_d
1231 insn_gen_fn m_gen_fun;
1232 void generate (rtx, rtx, machine_mode);
1233 bool prepare_mode (machine_mode, unsigned int);
1235 public:
1236 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1237 unsigned HOST_WIDE_INT len, unsigned int align)
1238 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1241 rtx finish_endp (int);
1244 /* Return true if MODE can be used for a set of stores, given an
1245 alignment ALIGN. Prepare whatever data is necessary for later
1246 calls to generate. */
1248 bool
1249 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1251 insn_code icode = optab_handler (mov_optab, mode);
1252 m_gen_fun = GEN_FCN (icode);
1253 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1256 /* A callback used when iterating for a store_by_pieces_operation.
1257 OP0 and OP1 are the values that have been loaded and should be
1258 compared in MODE. If OP0 is NULL, this means we should generate a
1259 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1260 gen function that should be used to generate the mode. */
1262 void
1263 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1265 emit_insn (m_gen_fun (op0, op1));
1268 /* Perform the final adjustment at the end of a string to obtain the
1269 correct return value for the block operation. If ENDP is 1 return
1270 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1271 end minus one byte ala stpcpy. */
1274 store_by_pieces_d::finish_endp (int endp)
1276 gcc_assert (!m_reverse);
1277 if (endp == 2)
1279 m_to.maybe_postinc (-1);
1280 --m_offset;
1282 return m_to.adjust (QImode, m_offset);
1285 /* Determine whether the LEN bytes generated by CONSTFUN can be
1286 stored to memory using several move instructions. CONSTFUNDATA is
1287 a pointer which will be passed as argument in every CONSTFUN call.
1288 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1289 a memset operation and false if it's a copy of a constant string.
1290 Return nonzero if a call to store_by_pieces should succeed. */
1293 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1294 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1295 void *constfundata, unsigned int align, bool memsetp)
1297 unsigned HOST_WIDE_INT l;
1298 unsigned int max_size;
1299 HOST_WIDE_INT offset = 0;
1300 enum insn_code icode;
1301 int reverse;
1302 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1303 rtx cst ATTRIBUTE_UNUSED;
1305 if (len == 0)
1306 return 1;
1308 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1309 memsetp
1310 ? SET_BY_PIECES
1311 : STORE_BY_PIECES,
1312 optimize_insn_for_speed_p ()))
1313 return 0;
1315 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1317 /* We would first store what we can in the largest integer mode, then go to
1318 successively smaller modes. */
1320 for (reverse = 0;
1321 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1322 reverse++)
1324 l = len;
1325 max_size = STORE_MAX_PIECES + 1;
1326 while (max_size > 1 && l > 0)
1328 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1330 icode = optab_handler (mov_optab, mode);
1331 if (icode != CODE_FOR_nothing
1332 && align >= GET_MODE_ALIGNMENT (mode))
1334 unsigned int size = GET_MODE_SIZE (mode);
1336 while (l >= size)
1338 if (reverse)
1339 offset -= size;
1341 cst = (*constfun) (constfundata, offset, mode);
1342 if (!targetm.legitimate_constant_p (mode, cst))
1343 return 0;
1345 if (!reverse)
1346 offset += size;
1348 l -= size;
1352 max_size = GET_MODE_SIZE (mode);
1355 /* The code above should have handled everything. */
1356 gcc_assert (!l);
1359 return 1;
1362 /* Generate several move instructions to store LEN bytes generated by
1363 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1364 pointer which will be passed as argument in every CONSTFUN call.
1365 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1366 a memset operation and false if it's a copy of a constant string.
1367 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1368 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1369 stpcpy. */
1372 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1373 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1374 void *constfundata, unsigned int align, bool memsetp, int endp)
1376 if (len == 0)
1378 gcc_assert (endp != 2);
1379 return to;
1382 gcc_assert (targetm.use_by_pieces_infrastructure_p
1383 (len, align,
1384 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1385 optimize_insn_for_speed_p ()));
1387 store_by_pieces_d data (to, constfun, constfundata, len, align);
1388 data.run ();
1390 if (endp)
1391 return data.finish_endp (endp);
1392 else
1393 return to;
1396 /* Callback routine for clear_by_pieces.
1397 Return const0_rtx unconditionally. */
1399 static rtx
1400 clear_by_pieces_1 (void *, HOST_WIDE_INT, scalar_int_mode)
1402 return const0_rtx;
1405 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1406 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1408 static void
1409 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1411 if (len == 0)
1412 return;
1414 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1415 data.run ();
1418 /* Context used by compare_by_pieces_genfn. It stores the fail label
1419 to jump to in case of miscomparison, and for branch ratios greater than 1,
1420 it stores an accumulator and the current and maximum counts before
1421 emitting another branch. */
1423 class compare_by_pieces_d : public op_by_pieces_d
1425 rtx_code_label *m_fail_label;
1426 rtx m_accumulator;
1427 int m_count, m_batch;
1429 void generate (rtx, rtx, machine_mode);
1430 bool prepare_mode (machine_mode, unsigned int);
1431 void finish_mode (machine_mode);
1432 public:
1433 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1434 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1435 rtx_code_label *fail_label)
1436 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1438 m_fail_label = fail_label;
1442 /* A callback used when iterating for a compare_by_pieces_operation.
1443 OP0 and OP1 are the values that have been loaded and should be
1444 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1445 context structure. */
1447 void
1448 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1450 if (m_batch > 1)
1452 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1453 true, OPTAB_LIB_WIDEN);
1454 if (m_count != 0)
1455 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1456 true, OPTAB_LIB_WIDEN);
1457 m_accumulator = temp;
1459 if (++m_count < m_batch)
1460 return;
1462 m_count = 0;
1463 op0 = m_accumulator;
1464 op1 = const0_rtx;
1465 m_accumulator = NULL_RTX;
1467 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1468 m_fail_label, profile_probability::uninitialized ());
1471 /* Return true if MODE can be used for a set of moves and comparisons,
1472 given an alignment ALIGN. Prepare whatever data is necessary for
1473 later calls to generate. */
1475 bool
1476 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1478 insn_code icode = optab_handler (mov_optab, mode);
1479 if (icode == CODE_FOR_nothing
1480 || align < GET_MODE_ALIGNMENT (mode)
1481 || !can_compare_p (EQ, mode, ccp_jump))
1482 return false;
1483 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1484 if (m_batch < 0)
1485 return false;
1486 m_accumulator = NULL_RTX;
1487 m_count = 0;
1488 return true;
1491 /* Called after expanding a series of comparisons in MODE. If we have
1492 accumulated results for which we haven't emitted a branch yet, do
1493 so now. */
1495 void
1496 compare_by_pieces_d::finish_mode (machine_mode mode)
1498 if (m_accumulator != NULL_RTX)
1499 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1500 NULL_RTX, NULL, m_fail_label,
1501 profile_probability::uninitialized ());
1504 /* Generate several move instructions to compare LEN bytes from blocks
1505 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1507 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1508 used to push FROM to the stack.
1510 ALIGN is maximum stack alignment we can assume.
1512 Optionally, the caller can pass a constfn and associated data in A1_CFN
1513 and A1_CFN_DATA. describing that the second operand being compared is a
1514 known constant and how to obtain its data. */
1516 static rtx
1517 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1518 rtx target, unsigned int align,
1519 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1521 rtx_code_label *fail_label = gen_label_rtx ();
1522 rtx_code_label *end_label = gen_label_rtx ();
1524 if (target == NULL_RTX
1525 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1526 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1528 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1529 fail_label);
1531 data.run ();
1533 emit_move_insn (target, const0_rtx);
1534 emit_jump (end_label);
1535 emit_barrier ();
1536 emit_label (fail_label);
1537 emit_move_insn (target, const1_rtx);
1538 emit_label (end_label);
1540 return target;
1543 /* Emit code to move a block Y to a block X. This may be done with
1544 string-move instructions, with multiple scalar move instructions,
1545 or with a library call.
1547 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1548 SIZE is an rtx that says how long they are.
1549 ALIGN is the maximum alignment we can assume they have.
1550 METHOD describes what kind of copy this is, and what mechanisms may be used.
1551 MIN_SIZE is the minimal size of block to move
1552 MAX_SIZE is the maximal size of block to move, if it can not be represented
1553 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1555 Return the address of the new block, if memcpy is called and returns it,
1556 0 otherwise. */
1559 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1560 unsigned int expected_align, HOST_WIDE_INT expected_size,
1561 unsigned HOST_WIDE_INT min_size,
1562 unsigned HOST_WIDE_INT max_size,
1563 unsigned HOST_WIDE_INT probable_max_size)
1565 bool may_use_call;
1566 rtx retval = 0;
1567 unsigned int align;
1569 gcc_assert (size);
1570 if (CONST_INT_P (size) && INTVAL (size) == 0)
1571 return 0;
1573 switch (method)
1575 case BLOCK_OP_NORMAL:
1576 case BLOCK_OP_TAILCALL:
1577 may_use_call = true;
1578 break;
1580 case BLOCK_OP_CALL_PARM:
1581 may_use_call = block_move_libcall_safe_for_call_parm ();
1583 /* Make inhibit_defer_pop nonzero around the library call
1584 to force it to pop the arguments right away. */
1585 NO_DEFER_POP;
1586 break;
1588 case BLOCK_OP_NO_LIBCALL:
1589 may_use_call = false;
1590 break;
1592 default:
1593 gcc_unreachable ();
1596 gcc_assert (MEM_P (x) && MEM_P (y));
1597 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1598 gcc_assert (align >= BITS_PER_UNIT);
1600 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1601 block copy is more efficient for other large modes, e.g. DCmode. */
1602 x = adjust_address (x, BLKmode, 0);
1603 y = adjust_address (y, BLKmode, 0);
1605 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1606 can be incorrect is coming from __builtin_memcpy. */
1607 if (CONST_INT_P (size))
1609 x = shallow_copy_rtx (x);
1610 y = shallow_copy_rtx (y);
1611 set_mem_size (x, INTVAL (size));
1612 set_mem_size (y, INTVAL (size));
1615 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1616 move_by_pieces (x, y, INTVAL (size), align, 0);
1617 else if (emit_block_move_via_movmem (x, y, size, align,
1618 expected_align, expected_size,
1619 min_size, max_size, probable_max_size))
1621 else if (may_use_call
1622 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1623 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1625 /* Since x and y are passed to a libcall, mark the corresponding
1626 tree EXPR as addressable. */
1627 tree y_expr = MEM_EXPR (y);
1628 tree x_expr = MEM_EXPR (x);
1629 if (y_expr)
1630 mark_addressable (y_expr);
1631 if (x_expr)
1632 mark_addressable (x_expr);
1633 retval = emit_block_copy_via_libcall (x, y, size,
1634 method == BLOCK_OP_TAILCALL);
1637 else
1638 emit_block_move_via_loop (x, y, size, align);
1640 if (method == BLOCK_OP_CALL_PARM)
1641 OK_DEFER_POP;
1643 return retval;
1647 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1649 unsigned HOST_WIDE_INT max, min = 0;
1650 if (GET_CODE (size) == CONST_INT)
1651 min = max = UINTVAL (size);
1652 else
1653 max = GET_MODE_MASK (GET_MODE (size));
1654 return emit_block_move_hints (x, y, size, method, 0, -1,
1655 min, max, max);
1658 /* A subroutine of emit_block_move. Returns true if calling the
1659 block move libcall will not clobber any parameters which may have
1660 already been placed on the stack. */
1662 static bool
1663 block_move_libcall_safe_for_call_parm (void)
1665 #if defined (REG_PARM_STACK_SPACE)
1666 tree fn;
1667 #endif
1669 /* If arguments are pushed on the stack, then they're safe. */
1670 if (PUSH_ARGS)
1671 return true;
1673 /* If registers go on the stack anyway, any argument is sure to clobber
1674 an outgoing argument. */
1675 #if defined (REG_PARM_STACK_SPACE)
1676 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1677 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1678 depend on its argument. */
1679 (void) fn;
1680 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1681 && REG_PARM_STACK_SPACE (fn) != 0)
1682 return false;
1683 #endif
1685 /* If any argument goes in memory, then it might clobber an outgoing
1686 argument. */
1688 CUMULATIVE_ARGS args_so_far_v;
1689 cumulative_args_t args_so_far;
1690 tree fn, arg;
1692 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1693 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1694 args_so_far = pack_cumulative_args (&args_so_far_v);
1696 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1697 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1699 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1700 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1701 NULL_TREE, true);
1702 if (!tmp || !REG_P (tmp))
1703 return false;
1704 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1705 return false;
1706 targetm.calls.function_arg_advance (args_so_far, mode,
1707 NULL_TREE, true);
1710 return true;
1713 /* A subroutine of emit_block_move. Expand a movmem pattern;
1714 return true if successful. */
1716 static bool
1717 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1718 unsigned int expected_align, HOST_WIDE_INT expected_size,
1719 unsigned HOST_WIDE_INT min_size,
1720 unsigned HOST_WIDE_INT max_size,
1721 unsigned HOST_WIDE_INT probable_max_size)
1723 int save_volatile_ok = volatile_ok;
1725 if (expected_align < align)
1726 expected_align = align;
1727 if (expected_size != -1)
1729 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1730 expected_size = probable_max_size;
1731 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1732 expected_size = min_size;
1735 /* Since this is a move insn, we don't care about volatility. */
1736 volatile_ok = 1;
1738 /* Try the most limited insn first, because there's no point
1739 including more than one in the machine description unless
1740 the more limited one has some advantage. */
1742 opt_scalar_int_mode mode_iter;
1743 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1745 scalar_int_mode mode = mode_iter.require ();
1746 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1748 if (code != CODE_FOR_nothing
1749 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1750 here because if SIZE is less than the mode mask, as it is
1751 returned by the macro, it will definitely be less than the
1752 actual mode mask. Since SIZE is within the Pmode address
1753 space, we limit MODE to Pmode. */
1754 && ((CONST_INT_P (size)
1755 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1756 <= (GET_MODE_MASK (mode) >> 1)))
1757 || max_size <= (GET_MODE_MASK (mode) >> 1)
1758 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1760 struct expand_operand ops[9];
1761 unsigned int nops;
1763 /* ??? When called via emit_block_move_for_call, it'd be
1764 nice if there were some way to inform the backend, so
1765 that it doesn't fail the expansion because it thinks
1766 emitting the libcall would be more efficient. */
1767 nops = insn_data[(int) code].n_generator_args;
1768 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1770 create_fixed_operand (&ops[0], x);
1771 create_fixed_operand (&ops[1], y);
1772 /* The check above guarantees that this size conversion is valid. */
1773 create_convert_operand_to (&ops[2], size, mode, true);
1774 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1775 if (nops >= 6)
1777 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1778 create_integer_operand (&ops[5], expected_size);
1780 if (nops >= 8)
1782 create_integer_operand (&ops[6], min_size);
1783 /* If we can not represent the maximal size,
1784 make parameter NULL. */
1785 if ((HOST_WIDE_INT) max_size != -1)
1786 create_integer_operand (&ops[7], max_size);
1787 else
1788 create_fixed_operand (&ops[7], NULL);
1790 if (nops == 9)
1792 /* If we can not represent the maximal size,
1793 make parameter NULL. */
1794 if ((HOST_WIDE_INT) probable_max_size != -1)
1795 create_integer_operand (&ops[8], probable_max_size);
1796 else
1797 create_fixed_operand (&ops[8], NULL);
1799 if (maybe_expand_insn (code, nops, ops))
1801 volatile_ok = save_volatile_ok;
1802 return true;
1807 volatile_ok = save_volatile_ok;
1808 return false;
1811 /* A subroutine of emit_block_move. Copy the data via an explicit
1812 loop. This is used only when libcalls are forbidden. */
1813 /* ??? It'd be nice to copy in hunks larger than QImode. */
1815 static void
1816 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1817 unsigned int align ATTRIBUTE_UNUSED)
1819 rtx_code_label *cmp_label, *top_label;
1820 rtx iter, x_addr, y_addr, tmp;
1821 machine_mode x_addr_mode = get_address_mode (x);
1822 machine_mode y_addr_mode = get_address_mode (y);
1823 machine_mode iter_mode;
1825 iter_mode = GET_MODE (size);
1826 if (iter_mode == VOIDmode)
1827 iter_mode = word_mode;
1829 top_label = gen_label_rtx ();
1830 cmp_label = gen_label_rtx ();
1831 iter = gen_reg_rtx (iter_mode);
1833 emit_move_insn (iter, const0_rtx);
1835 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1836 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1837 do_pending_stack_adjust ();
1839 emit_jump (cmp_label);
1840 emit_label (top_label);
1842 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1843 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1845 if (x_addr_mode != y_addr_mode)
1846 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1847 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1849 x = change_address (x, QImode, x_addr);
1850 y = change_address (y, QImode, y_addr);
1852 emit_move_insn (x, y);
1854 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1855 true, OPTAB_LIB_WIDEN);
1856 if (tmp != iter)
1857 emit_move_insn (iter, tmp);
1859 emit_label (cmp_label);
1861 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1862 true, top_label,
1863 profile_probability::guessed_always ()
1864 .apply_scale (9, 10));
1867 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1868 TAILCALL is true if this is a tail call. */
1871 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1872 rtx size, bool tailcall)
1874 rtx dst_addr, src_addr;
1875 tree call_expr, dst_tree, src_tree, size_tree;
1876 machine_mode size_mode;
1878 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1879 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1880 dst_tree = make_tree (ptr_type_node, dst_addr);
1882 src_addr = copy_addr_to_reg (XEXP (src, 0));
1883 src_addr = convert_memory_address (ptr_mode, src_addr);
1884 src_tree = make_tree (ptr_type_node, src_addr);
1886 size_mode = TYPE_MODE (sizetype);
1887 size = convert_to_mode (size_mode, size, 1);
1888 size = copy_to_mode_reg (size_mode, size);
1889 size_tree = make_tree (sizetype, size);
1891 /* It is incorrect to use the libcall calling conventions for calls to
1892 memcpy/memmove/memcmp because they can be provided by the user. */
1893 tree fn = builtin_decl_implicit (fncode);
1894 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1895 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1897 return expand_call (call_expr, NULL_RTX, false);
1900 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1901 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1902 otherwise return null. */
1905 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1906 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1907 HOST_WIDE_INT align)
1909 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1911 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1912 target = NULL_RTX;
1914 struct expand_operand ops[5];
1915 create_output_operand (&ops[0], target, insn_mode);
1916 create_fixed_operand (&ops[1], arg1_rtx);
1917 create_fixed_operand (&ops[2], arg2_rtx);
1918 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1919 TYPE_UNSIGNED (arg3_type));
1920 create_integer_operand (&ops[4], align);
1921 if (maybe_expand_insn (icode, 5, ops))
1922 return ops[0].value;
1923 return NULL_RTX;
1926 /* Expand a block compare between X and Y with length LEN using the
1927 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1928 of the expression that was used to calculate the length. ALIGN
1929 gives the known minimum common alignment. */
1931 static rtx
1932 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1933 unsigned align)
1935 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1936 implementing memcmp because it will stop if it encounters two
1937 zero bytes. */
1938 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1940 if (icode == CODE_FOR_nothing)
1941 return NULL_RTX;
1943 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1946 /* Emit code to compare a block Y to a block X. This may be done with
1947 string-compare instructions, with multiple scalar instructions,
1948 or with a library call.
1950 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1951 they are. LEN_TYPE is the type of the expression that was used to
1952 calculate it.
1954 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1955 value of a normal memcmp call, instead we can just compare for equality.
1956 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1957 returning NULL_RTX.
1959 Optionally, the caller can pass a constfn and associated data in Y_CFN
1960 and Y_CFN_DATA. describing that the second operand being compared is a
1961 known constant and how to obtain its data.
1962 Return the result of the comparison, or NULL_RTX if we failed to
1963 perform the operation. */
1966 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1967 bool equality_only, by_pieces_constfn y_cfn,
1968 void *y_cfndata)
1970 rtx result = 0;
1972 if (CONST_INT_P (len) && INTVAL (len) == 0)
1973 return const0_rtx;
1975 gcc_assert (MEM_P (x) && MEM_P (y));
1976 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1977 gcc_assert (align >= BITS_PER_UNIT);
1979 x = adjust_address (x, BLKmode, 0);
1980 y = adjust_address (y, BLKmode, 0);
1982 if (equality_only
1983 && CONST_INT_P (len)
1984 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1985 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1986 y_cfn, y_cfndata);
1987 else
1988 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1990 return result;
1993 /* Copy all or part of a value X into registers starting at REGNO.
1994 The number of registers to be filled is NREGS. */
1996 void
1997 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1999 if (nregs == 0)
2000 return;
2002 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2003 x = validize_mem (force_const_mem (mode, x));
2005 /* See if the machine can do this with a load multiple insn. */
2006 if (targetm.have_load_multiple ())
2008 rtx_insn *last = get_last_insn ();
2009 rtx first = gen_rtx_REG (word_mode, regno);
2010 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2011 GEN_INT (nregs)))
2013 emit_insn (pat);
2014 return;
2016 else
2017 delete_insns_since (last);
2020 for (int i = 0; i < nregs; i++)
2021 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2022 operand_subword_force (x, i, mode));
2025 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2026 The number of registers to be filled is NREGS. */
2028 void
2029 move_block_from_reg (int regno, rtx x, int nregs)
2031 if (nregs == 0)
2032 return;
2034 /* See if the machine can do this with a store multiple insn. */
2035 if (targetm.have_store_multiple ())
2037 rtx_insn *last = get_last_insn ();
2038 rtx first = gen_rtx_REG (word_mode, regno);
2039 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2040 GEN_INT (nregs)))
2042 emit_insn (pat);
2043 return;
2045 else
2046 delete_insns_since (last);
2049 for (int i = 0; i < nregs; i++)
2051 rtx tem = operand_subword (x, i, 1, BLKmode);
2053 gcc_assert (tem);
2055 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2059 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2060 ORIG, where ORIG is a non-consecutive group of registers represented by
2061 a PARALLEL. The clone is identical to the original except in that the
2062 original set of registers is replaced by a new set of pseudo registers.
2063 The new set has the same modes as the original set. */
2066 gen_group_rtx (rtx orig)
2068 int i, length;
2069 rtx *tmps;
2071 gcc_assert (GET_CODE (orig) == PARALLEL);
2073 length = XVECLEN (orig, 0);
2074 tmps = XALLOCAVEC (rtx, length);
2076 /* Skip a NULL entry in first slot. */
2077 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2079 if (i)
2080 tmps[0] = 0;
2082 for (; i < length; i++)
2084 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2085 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2087 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2090 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2093 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2094 except that values are placed in TMPS[i], and must later be moved
2095 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2097 static void
2098 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2099 poly_int64 ssize)
2101 rtx src;
2102 int start, i;
2103 machine_mode m = GET_MODE (orig_src);
2105 gcc_assert (GET_CODE (dst) == PARALLEL);
2107 if (m != VOIDmode
2108 && !SCALAR_INT_MODE_P (m)
2109 && !MEM_P (orig_src)
2110 && GET_CODE (orig_src) != CONCAT)
2112 scalar_int_mode imode;
2113 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2115 src = gen_reg_rtx (imode);
2116 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2118 else
2120 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2121 emit_move_insn (src, orig_src);
2123 emit_group_load_1 (tmps, dst, src, type, ssize);
2124 return;
2127 /* Check for a NULL entry, used to indicate that the parameter goes
2128 both on the stack and in registers. */
2129 if (XEXP (XVECEXP (dst, 0, 0), 0))
2130 start = 0;
2131 else
2132 start = 1;
2134 /* Process the pieces. */
2135 for (i = start; i < XVECLEN (dst, 0); i++)
2137 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2138 poly_int64 bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2139 poly_int64 bytelen = GET_MODE_SIZE (mode);
2140 poly_int64 shift = 0;
2142 /* Handle trailing fragments that run over the size of the struct.
2143 It's the target's responsibility to make sure that the fragment
2144 cannot be strictly smaller in some cases and strictly larger
2145 in others. */
2146 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2147 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2149 /* Arrange to shift the fragment to where it belongs.
2150 extract_bit_field loads to the lsb of the reg. */
2151 if (
2152 #ifdef BLOCK_REG_PADDING
2153 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2154 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2155 #else
2156 BYTES_BIG_ENDIAN
2157 #endif
2159 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2160 bytelen = ssize - bytepos;
2161 gcc_assert (maybe_gt (bytelen, 0));
2164 /* If we won't be loading directly from memory, protect the real source
2165 from strange tricks we might play; but make sure that the source can
2166 be loaded directly into the destination. */
2167 src = orig_src;
2168 if (!MEM_P (orig_src)
2169 && (!CONSTANT_P (orig_src)
2170 || (GET_MODE (orig_src) != mode
2171 && GET_MODE (orig_src) != VOIDmode)))
2173 if (GET_MODE (orig_src) == VOIDmode)
2174 src = gen_reg_rtx (mode);
2175 else
2176 src = gen_reg_rtx (GET_MODE (orig_src));
2178 emit_move_insn (src, orig_src);
2181 /* Optimize the access just a bit. */
2182 if (MEM_P (src)
2183 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2184 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2185 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2186 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2188 tmps[i] = gen_reg_rtx (mode);
2189 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2191 else if (COMPLEX_MODE_P (mode)
2192 && GET_MODE (src) == mode
2193 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2194 /* Let emit_move_complex do the bulk of the work. */
2195 tmps[i] = src;
2196 else if (GET_CODE (src) == CONCAT)
2198 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2199 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2200 unsigned int elt;
2201 poly_int64 subpos;
2203 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2204 && known_le (subpos + bytelen, slen0))
2206 /* The following assumes that the concatenated objects all
2207 have the same size. In this case, a simple calculation
2208 can be used to determine the object and the bit field
2209 to be extracted. */
2210 tmps[i] = XEXP (src, elt);
2211 if (maybe_ne (subpos, 0)
2212 || maybe_ne (subpos + bytelen, slen0)
2213 || (!CONSTANT_P (tmps[i])
2214 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2215 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2216 subpos * BITS_PER_UNIT,
2217 1, NULL_RTX, mode, mode, false,
2218 NULL);
2220 else
2222 rtx mem;
2224 gcc_assert (known_eq (bytepos, 0));
2225 mem = assign_stack_temp (GET_MODE (src), slen);
2226 emit_move_insn (mem, src);
2227 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2228 0, 1, NULL_RTX, mode, mode, false,
2229 NULL);
2232 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2233 SIMD register, which is currently broken. While we get GCC
2234 to emit proper RTL for these cases, let's dump to memory. */
2235 else if (VECTOR_MODE_P (GET_MODE (dst))
2236 && REG_P (src))
2238 int slen = GET_MODE_SIZE (GET_MODE (src));
2239 rtx mem;
2241 mem = assign_stack_temp (GET_MODE (src), slen);
2242 emit_move_insn (mem, src);
2243 tmps[i] = adjust_address (mem, mode, bytepos);
2245 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2246 && XVECLEN (dst, 0) > 1)
2247 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2248 else if (CONSTANT_P (src))
2250 if (known_eq (bytelen, ssize))
2251 tmps[i] = src;
2252 else
2254 rtx first, second;
2256 /* TODO: const_wide_int can have sizes other than this... */
2257 gcc_assert (known_eq (2 * bytelen, ssize));
2258 split_double (src, &first, &second);
2259 if (i)
2260 tmps[i] = second;
2261 else
2262 tmps[i] = first;
2265 else if (REG_P (src) && GET_MODE (src) == mode)
2266 tmps[i] = src;
2267 else
2268 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2269 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2270 mode, mode, false, NULL);
2272 if (maybe_ne (shift, 0))
2273 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2274 shift, tmps[i], 0);
2278 /* Emit code to move a block SRC of type TYPE to a block DST,
2279 where DST is non-consecutive registers represented by a PARALLEL.
2280 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2281 if not known. */
2283 void
2284 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2286 rtx *tmps;
2287 int i;
2289 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2290 emit_group_load_1 (tmps, dst, src, type, ssize);
2292 /* Copy the extracted pieces into the proper (probable) hard regs. */
2293 for (i = 0; i < XVECLEN (dst, 0); i++)
2295 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2296 if (d == NULL)
2297 continue;
2298 emit_move_insn (d, tmps[i]);
2302 /* Similar, but load SRC into new pseudos in a format that looks like
2303 PARALLEL. This can later be fed to emit_group_move to get things
2304 in the right place. */
2307 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2309 rtvec vec;
2310 int i;
2312 vec = rtvec_alloc (XVECLEN (parallel, 0));
2313 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2315 /* Convert the vector to look just like the original PARALLEL, except
2316 with the computed values. */
2317 for (i = 0; i < XVECLEN (parallel, 0); i++)
2319 rtx e = XVECEXP (parallel, 0, i);
2320 rtx d = XEXP (e, 0);
2322 if (d)
2324 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2325 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2327 RTVEC_ELT (vec, i) = e;
2330 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2333 /* Emit code to move a block SRC to block DST, where SRC and DST are
2334 non-consecutive groups of registers, each represented by a PARALLEL. */
2336 void
2337 emit_group_move (rtx dst, rtx src)
2339 int i;
2341 gcc_assert (GET_CODE (src) == PARALLEL
2342 && GET_CODE (dst) == PARALLEL
2343 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2345 /* Skip first entry if NULL. */
2346 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2347 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2348 XEXP (XVECEXP (src, 0, i), 0));
2351 /* Move a group of registers represented by a PARALLEL into pseudos. */
2354 emit_group_move_into_temps (rtx src)
2356 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2357 int i;
2359 for (i = 0; i < XVECLEN (src, 0); i++)
2361 rtx e = XVECEXP (src, 0, i);
2362 rtx d = XEXP (e, 0);
2364 if (d)
2365 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2366 RTVEC_ELT (vec, i) = e;
2369 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2372 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2373 where SRC is non-consecutive registers represented by a PARALLEL.
2374 SSIZE represents the total size of block ORIG_DST, or -1 if not
2375 known. */
2377 void
2378 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2379 poly_int64 ssize)
2381 rtx *tmps, dst;
2382 int start, finish, i;
2383 machine_mode m = GET_MODE (orig_dst);
2385 gcc_assert (GET_CODE (src) == PARALLEL);
2387 if (!SCALAR_INT_MODE_P (m)
2388 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2390 scalar_int_mode imode;
2391 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2393 dst = gen_reg_rtx (imode);
2394 emit_group_store (dst, src, type, ssize);
2395 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2397 else
2399 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2400 emit_group_store (dst, src, type, ssize);
2402 emit_move_insn (orig_dst, dst);
2403 return;
2406 /* Check for a NULL entry, used to indicate that the parameter goes
2407 both on the stack and in registers. */
2408 if (XEXP (XVECEXP (src, 0, 0), 0))
2409 start = 0;
2410 else
2411 start = 1;
2412 finish = XVECLEN (src, 0);
2414 tmps = XALLOCAVEC (rtx, finish);
2416 /* Copy the (probable) hard regs into pseudos. */
2417 for (i = start; i < finish; i++)
2419 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2420 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2422 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2423 emit_move_insn (tmps[i], reg);
2425 else
2426 tmps[i] = reg;
2429 /* If we won't be storing directly into memory, protect the real destination
2430 from strange tricks we might play. */
2431 dst = orig_dst;
2432 if (GET_CODE (dst) == PARALLEL)
2434 rtx temp;
2436 /* We can get a PARALLEL dst if there is a conditional expression in
2437 a return statement. In that case, the dst and src are the same,
2438 so no action is necessary. */
2439 if (rtx_equal_p (dst, src))
2440 return;
2442 /* It is unclear if we can ever reach here, but we may as well handle
2443 it. Allocate a temporary, and split this into a store/load to/from
2444 the temporary. */
2445 temp = assign_stack_temp (GET_MODE (dst), ssize);
2446 emit_group_store (temp, src, type, ssize);
2447 emit_group_load (dst, temp, type, ssize);
2448 return;
2450 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2452 machine_mode outer = GET_MODE (dst);
2453 machine_mode inner;
2454 poly_int64 bytepos;
2455 bool done = false;
2456 rtx temp;
2458 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2459 dst = gen_reg_rtx (outer);
2461 /* Make life a bit easier for combine. */
2462 /* If the first element of the vector is the low part
2463 of the destination mode, use a paradoxical subreg to
2464 initialize the destination. */
2465 if (start < finish)
2467 inner = GET_MODE (tmps[start]);
2468 bytepos = subreg_lowpart_offset (inner, outer);
2469 if (known_eq (INTVAL (XEXP (XVECEXP (src, 0, start), 1)), bytepos))
2471 temp = simplify_gen_subreg (outer, tmps[start],
2472 inner, 0);
2473 if (temp)
2475 emit_move_insn (dst, temp);
2476 done = true;
2477 start++;
2482 /* If the first element wasn't the low part, try the last. */
2483 if (!done
2484 && start < finish - 1)
2486 inner = GET_MODE (tmps[finish - 1]);
2487 bytepos = subreg_lowpart_offset (inner, outer);
2488 if (known_eq (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)),
2489 bytepos))
2491 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2492 inner, 0);
2493 if (temp)
2495 emit_move_insn (dst, temp);
2496 done = true;
2497 finish--;
2502 /* Otherwise, simply initialize the result to zero. */
2503 if (!done)
2504 emit_move_insn (dst, CONST0_RTX (outer));
2507 /* Process the pieces. */
2508 for (i = start; i < finish; i++)
2510 poly_int64 bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2511 machine_mode mode = GET_MODE (tmps[i]);
2512 poly_int64 bytelen = GET_MODE_SIZE (mode);
2513 poly_uint64 adj_bytelen;
2514 rtx dest = dst;
2516 /* Handle trailing fragments that run over the size of the struct.
2517 It's the target's responsibility to make sure that the fragment
2518 cannot be strictly smaller in some cases and strictly larger
2519 in others. */
2520 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2521 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2522 adj_bytelen = ssize - bytepos;
2523 else
2524 adj_bytelen = bytelen;
2526 if (GET_CODE (dst) == CONCAT)
2528 if (known_le (bytepos + adj_bytelen,
2529 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2530 dest = XEXP (dst, 0);
2531 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2533 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2534 dest = XEXP (dst, 1);
2536 else
2538 machine_mode dest_mode = GET_MODE (dest);
2539 machine_mode tmp_mode = GET_MODE (tmps[i]);
2541 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2543 if (GET_MODE_ALIGNMENT (dest_mode)
2544 >= GET_MODE_ALIGNMENT (tmp_mode))
2546 dest = assign_stack_temp (dest_mode,
2547 GET_MODE_SIZE (dest_mode));
2548 emit_move_insn (adjust_address (dest,
2549 tmp_mode,
2550 bytepos),
2551 tmps[i]);
2552 dst = dest;
2554 else
2556 dest = assign_stack_temp (tmp_mode,
2557 GET_MODE_SIZE (tmp_mode));
2558 emit_move_insn (dest, tmps[i]);
2559 dst = adjust_address (dest, dest_mode, bytepos);
2561 break;
2565 /* Handle trailing fragments that run over the size of the struct. */
2566 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2568 /* store_bit_field always takes its value from the lsb.
2569 Move the fragment to the lsb if it's not already there. */
2570 if (
2571 #ifdef BLOCK_REG_PADDING
2572 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2573 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2574 #else
2575 BYTES_BIG_ENDIAN
2576 #endif
2579 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2580 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2581 shift, tmps[i], 0);
2584 /* Make sure not to write past the end of the struct. */
2585 store_bit_field (dest,
2586 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2587 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2588 VOIDmode, tmps[i], false);
2591 /* Optimize the access just a bit. */
2592 else if (MEM_P (dest)
2593 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2594 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2595 && multiple_p (bytepos * BITS_PER_UNIT,
2596 GET_MODE_ALIGNMENT (mode))
2597 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2598 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2600 else
2601 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2602 0, 0, mode, tmps[i], false);
2605 /* Copy from the pseudo into the (probable) hard reg. */
2606 if (orig_dst != dst)
2607 emit_move_insn (orig_dst, dst);
2610 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2611 of the value stored in X. */
2614 maybe_emit_group_store (rtx x, tree type)
2616 machine_mode mode = TYPE_MODE (type);
2617 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2618 if (GET_CODE (x) == PARALLEL)
2620 rtx result = gen_reg_rtx (mode);
2621 emit_group_store (result, x, type, int_size_in_bytes (type));
2622 return result;
2624 return x;
2627 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2629 This is used on targets that return BLKmode values in registers. */
2631 static void
2632 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2634 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2635 rtx src = NULL, dst = NULL;
2636 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2637 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2638 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2639 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2640 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2641 fixed_size_mode copy_mode;
2643 /* BLKmode registers created in the back-end shouldn't have survived. */
2644 gcc_assert (mode != BLKmode);
2646 /* If the structure doesn't take up a whole number of words, see whether
2647 SRCREG is padded on the left or on the right. If it's on the left,
2648 set PADDING_CORRECTION to the number of bits to skip.
2650 In most ABIs, the structure will be returned at the least end of
2651 the register, which translates to right padding on little-endian
2652 targets and left padding on big-endian targets. The opposite
2653 holds if the structure is returned at the most significant
2654 end of the register. */
2655 if (bytes % UNITS_PER_WORD != 0
2656 && (targetm.calls.return_in_msb (type)
2657 ? !BYTES_BIG_ENDIAN
2658 : BYTES_BIG_ENDIAN))
2659 padding_correction
2660 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2662 /* We can use a single move if we have an exact mode for the size. */
2663 else if (MEM_P (target)
2664 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2665 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2666 && bytes == GET_MODE_SIZE (mode))
2668 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2669 return;
2672 /* And if we additionally have the same mode for a register. */
2673 else if (REG_P (target)
2674 && GET_MODE (target) == mode
2675 && bytes == GET_MODE_SIZE (mode))
2677 emit_move_insn (target, srcreg);
2678 return;
2681 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2682 into a new pseudo which is a full word. */
2683 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2685 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2686 mode = word_mode;
2689 /* Copy the structure BITSIZE bits at a time. If the target lives in
2690 memory, take care of not reading/writing past its end by selecting
2691 a copy mode suited to BITSIZE. This should always be possible given
2692 how it is computed.
2694 If the target lives in register, make sure not to select a copy mode
2695 larger than the mode of the register.
2697 We could probably emit more efficient code for machines which do not use
2698 strict alignment, but it doesn't seem worth the effort at the current
2699 time. */
2701 copy_mode = word_mode;
2702 if (MEM_P (target))
2704 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2705 if (mem_mode.exists ())
2706 copy_mode = mem_mode.require ();
2708 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2709 copy_mode = tmode;
2711 for (bitpos = 0, xbitpos = padding_correction;
2712 bitpos < bytes * BITS_PER_UNIT;
2713 bitpos += bitsize, xbitpos += bitsize)
2715 /* We need a new source operand each time xbitpos is on a
2716 word boundary and when xbitpos == padding_correction
2717 (the first time through). */
2718 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2719 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2721 /* We need a new destination operand each time bitpos is on
2722 a word boundary. */
2723 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2724 dst = target;
2725 else if (bitpos % BITS_PER_WORD == 0)
2726 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2728 /* Use xbitpos for the source extraction (right justified) and
2729 bitpos for the destination store (left justified). */
2730 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2731 extract_bit_field (src, bitsize,
2732 xbitpos % BITS_PER_WORD, 1,
2733 NULL_RTX, copy_mode, copy_mode,
2734 false, NULL),
2735 false);
2739 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
2740 register if it contains any data, otherwise return null.
2742 This is used on targets that return BLKmode values in registers. */
2745 copy_blkmode_to_reg (machine_mode mode_in, tree src)
2747 int i, n_regs;
2748 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2749 unsigned int bitsize;
2750 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2751 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2752 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
2753 fixed_size_mode dst_mode;
2755 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2757 x = expand_normal (src);
2759 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
2760 if (bytes == 0)
2761 return NULL_RTX;
2763 /* If the structure doesn't take up a whole number of words, see
2764 whether the register value should be padded on the left or on
2765 the right. Set PADDING_CORRECTION to the number of padding
2766 bits needed on the left side.
2768 In most ABIs, the structure will be returned at the least end of
2769 the register, which translates to right padding on little-endian
2770 targets and left padding on big-endian targets. The opposite
2771 holds if the structure is returned at the most significant
2772 end of the register. */
2773 if (bytes % UNITS_PER_WORD != 0
2774 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2775 ? !BYTES_BIG_ENDIAN
2776 : BYTES_BIG_ENDIAN))
2777 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2778 * BITS_PER_UNIT));
2780 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2781 dst_words = XALLOCAVEC (rtx, n_regs);
2782 bitsize = BITS_PER_WORD;
2783 if (targetm.slow_unaligned_access (word_mode, TYPE_ALIGN (TREE_TYPE (src))))
2784 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2786 /* Copy the structure BITSIZE bits at a time. */
2787 for (bitpos = 0, xbitpos = padding_correction;
2788 bitpos < bytes * BITS_PER_UNIT;
2789 bitpos += bitsize, xbitpos += bitsize)
2791 /* We need a new destination pseudo each time xbitpos is
2792 on a word boundary and when xbitpos == padding_correction
2793 (the first time through). */
2794 if (xbitpos % BITS_PER_WORD == 0
2795 || xbitpos == padding_correction)
2797 /* Generate an appropriate register. */
2798 dst_word = gen_reg_rtx (word_mode);
2799 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2801 /* Clear the destination before we move anything into it. */
2802 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2805 /* We need a new source operand each time bitpos is on a word
2806 boundary. */
2807 if (bitpos % BITS_PER_WORD == 0)
2808 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2810 /* Use bitpos for the source extraction (left justified) and
2811 xbitpos for the destination store (right justified). */
2812 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2813 0, 0, word_mode,
2814 extract_bit_field (src_word, bitsize,
2815 bitpos % BITS_PER_WORD, 1,
2816 NULL_RTX, word_mode, word_mode,
2817 false, NULL),
2818 false);
2821 if (mode == BLKmode)
2823 /* Find the smallest integer mode large enough to hold the
2824 entire structure. */
2825 opt_scalar_int_mode mode_iter;
2826 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2827 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2828 break;
2830 /* A suitable mode should have been found. */
2831 mode = mode_iter.require ();
2834 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2835 dst_mode = word_mode;
2836 else
2837 dst_mode = mode;
2838 dst = gen_reg_rtx (dst_mode);
2840 for (i = 0; i < n_regs; i++)
2841 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2843 if (mode != dst_mode)
2844 dst = gen_lowpart (mode, dst);
2846 return dst;
2849 /* Add a USE expression for REG to the (possibly empty) list pointed
2850 to by CALL_FUSAGE. REG must denote a hard register. */
2852 void
2853 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2855 gcc_assert (REG_P (reg));
2857 if (!HARD_REGISTER_P (reg))
2858 return;
2860 *call_fusage
2861 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2864 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2865 to by CALL_FUSAGE. REG must denote a hard register. */
2867 void
2868 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2870 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2872 *call_fusage
2873 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2876 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2877 starting at REGNO. All of these registers must be hard registers. */
2879 void
2880 use_regs (rtx *call_fusage, int regno, int nregs)
2882 int i;
2884 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2886 for (i = 0; i < nregs; i++)
2887 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2890 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2891 PARALLEL REGS. This is for calls that pass values in multiple
2892 non-contiguous locations. The Irix 6 ABI has examples of this. */
2894 void
2895 use_group_regs (rtx *call_fusage, rtx regs)
2897 int i;
2899 for (i = 0; i < XVECLEN (regs, 0); i++)
2901 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2903 /* A NULL entry means the parameter goes both on the stack and in
2904 registers. This can also be a MEM for targets that pass values
2905 partially on the stack and partially in registers. */
2906 if (reg != 0 && REG_P (reg))
2907 use_reg (call_fusage, reg);
2911 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2912 assigment and the code of the expresion on the RHS is CODE. Return
2913 NULL otherwise. */
2915 static gimple *
2916 get_def_for_expr (tree name, enum tree_code code)
2918 gimple *def_stmt;
2920 if (TREE_CODE (name) != SSA_NAME)
2921 return NULL;
2923 def_stmt = get_gimple_for_ssa_name (name);
2924 if (!def_stmt
2925 || gimple_assign_rhs_code (def_stmt) != code)
2926 return NULL;
2928 return def_stmt;
2931 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2932 assigment and the class of the expresion on the RHS is CLASS. Return
2933 NULL otherwise. */
2935 static gimple *
2936 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2938 gimple *def_stmt;
2940 if (TREE_CODE (name) != SSA_NAME)
2941 return NULL;
2943 def_stmt = get_gimple_for_ssa_name (name);
2944 if (!def_stmt
2945 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2946 return NULL;
2948 return def_stmt;
2951 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2952 its length in bytes. */
2955 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2956 unsigned int expected_align, HOST_WIDE_INT expected_size,
2957 unsigned HOST_WIDE_INT min_size,
2958 unsigned HOST_WIDE_INT max_size,
2959 unsigned HOST_WIDE_INT probable_max_size)
2961 machine_mode mode = GET_MODE (object);
2962 unsigned int align;
2964 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2966 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2967 just move a zero. Otherwise, do this a piece at a time. */
2968 if (mode != BLKmode
2969 && CONST_INT_P (size)
2970 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2972 rtx zero = CONST0_RTX (mode);
2973 if (zero != NULL)
2975 emit_move_insn (object, zero);
2976 return NULL;
2979 if (COMPLEX_MODE_P (mode))
2981 zero = CONST0_RTX (GET_MODE_INNER (mode));
2982 if (zero != NULL)
2984 write_complex_part (object, zero, 0);
2985 write_complex_part (object, zero, 1);
2986 return NULL;
2991 if (size == const0_rtx)
2992 return NULL;
2994 align = MEM_ALIGN (object);
2996 if (CONST_INT_P (size)
2997 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2998 CLEAR_BY_PIECES,
2999 optimize_insn_for_speed_p ()))
3000 clear_by_pieces (object, INTVAL (size), align);
3001 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3002 expected_align, expected_size,
3003 min_size, max_size, probable_max_size))
3005 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3006 return set_storage_via_libcall (object, size, const0_rtx,
3007 method == BLOCK_OP_TAILCALL);
3008 else
3009 gcc_unreachable ();
3011 return NULL;
3015 clear_storage (rtx object, rtx size, enum block_op_methods method)
3017 unsigned HOST_WIDE_INT max, min = 0;
3018 if (GET_CODE (size) == CONST_INT)
3019 min = max = UINTVAL (size);
3020 else
3021 max = GET_MODE_MASK (GET_MODE (size));
3022 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
3026 /* A subroutine of clear_storage. Expand a call to memset.
3027 Return the return value of memset, 0 otherwise. */
3030 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3032 tree call_expr, fn, object_tree, size_tree, val_tree;
3033 machine_mode size_mode;
3035 object = copy_addr_to_reg (XEXP (object, 0));
3036 object_tree = make_tree (ptr_type_node, object);
3038 if (!CONST_INT_P (val))
3039 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3040 val_tree = make_tree (integer_type_node, val);
3042 size_mode = TYPE_MODE (sizetype);
3043 size = convert_to_mode (size_mode, size, 1);
3044 size = copy_to_mode_reg (size_mode, size);
3045 size_tree = make_tree (sizetype, size);
3047 /* It is incorrect to use the libcall calling conventions for calls to
3048 memset because it can be provided by the user. */
3049 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3050 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3051 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3053 return expand_call (call_expr, NULL_RTX, false);
3056 /* Expand a setmem pattern; return true if successful. */
3058 bool
3059 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3060 unsigned int expected_align, HOST_WIDE_INT expected_size,
3061 unsigned HOST_WIDE_INT min_size,
3062 unsigned HOST_WIDE_INT max_size,
3063 unsigned HOST_WIDE_INT probable_max_size)
3065 /* Try the most limited insn first, because there's no point
3066 including more than one in the machine description unless
3067 the more limited one has some advantage. */
3069 if (expected_align < align)
3070 expected_align = align;
3071 if (expected_size != -1)
3073 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3074 expected_size = max_size;
3075 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3076 expected_size = min_size;
3079 opt_scalar_int_mode mode_iter;
3080 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3082 scalar_int_mode mode = mode_iter.require ();
3083 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3085 if (code != CODE_FOR_nothing
3086 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3087 here because if SIZE is less than the mode mask, as it is
3088 returned by the macro, it will definitely be less than the
3089 actual mode mask. Since SIZE is within the Pmode address
3090 space, we limit MODE to Pmode. */
3091 && ((CONST_INT_P (size)
3092 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3093 <= (GET_MODE_MASK (mode) >> 1)))
3094 || max_size <= (GET_MODE_MASK (mode) >> 1)
3095 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3097 struct expand_operand ops[9];
3098 unsigned int nops;
3100 nops = insn_data[(int) code].n_generator_args;
3101 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3103 create_fixed_operand (&ops[0], object);
3104 /* The check above guarantees that this size conversion is valid. */
3105 create_convert_operand_to (&ops[1], size, mode, true);
3106 create_convert_operand_from (&ops[2], val, byte_mode, true);
3107 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3108 if (nops >= 6)
3110 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3111 create_integer_operand (&ops[5], expected_size);
3113 if (nops >= 8)
3115 create_integer_operand (&ops[6], min_size);
3116 /* If we can not represent the maximal size,
3117 make parameter NULL. */
3118 if ((HOST_WIDE_INT) max_size != -1)
3119 create_integer_operand (&ops[7], max_size);
3120 else
3121 create_fixed_operand (&ops[7], NULL);
3123 if (nops == 9)
3125 /* If we can not represent the maximal size,
3126 make parameter NULL. */
3127 if ((HOST_WIDE_INT) probable_max_size != -1)
3128 create_integer_operand (&ops[8], probable_max_size);
3129 else
3130 create_fixed_operand (&ops[8], NULL);
3132 if (maybe_expand_insn (code, nops, ops))
3133 return true;
3137 return false;
3141 /* Write to one of the components of the complex value CPLX. Write VAL to
3142 the real part if IMAG_P is false, and the imaginary part if its true. */
3144 void
3145 write_complex_part (rtx cplx, rtx val, bool imag_p)
3147 machine_mode cmode;
3148 scalar_mode imode;
3149 unsigned ibitsize;
3151 if (GET_CODE (cplx) == CONCAT)
3153 emit_move_insn (XEXP (cplx, imag_p), val);
3154 return;
3157 cmode = GET_MODE (cplx);
3158 imode = GET_MODE_INNER (cmode);
3159 ibitsize = GET_MODE_BITSIZE (imode);
3161 /* For MEMs simplify_gen_subreg may generate an invalid new address
3162 because, e.g., the original address is considered mode-dependent
3163 by the target, which restricts simplify_subreg from invoking
3164 adjust_address_nv. Instead of preparing fallback support for an
3165 invalid address, we call adjust_address_nv directly. */
3166 if (MEM_P (cplx))
3168 emit_move_insn (adjust_address_nv (cplx, imode,
3169 imag_p ? GET_MODE_SIZE (imode) : 0),
3170 val);
3171 return;
3174 /* If the sub-object is at least word sized, then we know that subregging
3175 will work. This special case is important, since store_bit_field
3176 wants to operate on integer modes, and there's rarely an OImode to
3177 correspond to TCmode. */
3178 if (ibitsize >= BITS_PER_WORD
3179 /* For hard regs we have exact predicates. Assume we can split
3180 the original object if it spans an even number of hard regs.
3181 This special case is important for SCmode on 64-bit platforms
3182 where the natural size of floating-point regs is 32-bit. */
3183 || (REG_P (cplx)
3184 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3185 && REG_NREGS (cplx) % 2 == 0))
3187 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3188 imag_p ? GET_MODE_SIZE (imode) : 0);
3189 if (part)
3191 emit_move_insn (part, val);
3192 return;
3194 else
3195 /* simplify_gen_subreg may fail for sub-word MEMs. */
3196 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3199 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3200 false);
3203 /* Extract one of the components of the complex value CPLX. Extract the
3204 real part if IMAG_P is false, and the imaginary part if it's true. */
3207 read_complex_part (rtx cplx, bool imag_p)
3209 machine_mode cmode;
3210 scalar_mode imode;
3211 unsigned ibitsize;
3213 if (GET_CODE (cplx) == CONCAT)
3214 return XEXP (cplx, imag_p);
3216 cmode = GET_MODE (cplx);
3217 imode = GET_MODE_INNER (cmode);
3218 ibitsize = GET_MODE_BITSIZE (imode);
3220 /* Special case reads from complex constants that got spilled to memory. */
3221 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3223 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3224 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3226 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3227 if (CONSTANT_CLASS_P (part))
3228 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3232 /* For MEMs simplify_gen_subreg may generate an invalid new address
3233 because, e.g., the original address is considered mode-dependent
3234 by the target, which restricts simplify_subreg from invoking
3235 adjust_address_nv. Instead of preparing fallback support for an
3236 invalid address, we call adjust_address_nv directly. */
3237 if (MEM_P (cplx))
3238 return adjust_address_nv (cplx, imode,
3239 imag_p ? GET_MODE_SIZE (imode) : 0);
3241 /* If the sub-object is at least word sized, then we know that subregging
3242 will work. This special case is important, since extract_bit_field
3243 wants to operate on integer modes, and there's rarely an OImode to
3244 correspond to TCmode. */
3245 if (ibitsize >= BITS_PER_WORD
3246 /* For hard regs we have exact predicates. Assume we can split
3247 the original object if it spans an even number of hard regs.
3248 This special case is important for SCmode on 64-bit platforms
3249 where the natural size of floating-point regs is 32-bit. */
3250 || (REG_P (cplx)
3251 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3252 && REG_NREGS (cplx) % 2 == 0))
3254 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3255 imag_p ? GET_MODE_SIZE (imode) : 0);
3256 if (ret)
3257 return ret;
3258 else
3259 /* simplify_gen_subreg may fail for sub-word MEMs. */
3260 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3263 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3264 true, NULL_RTX, imode, imode, false, NULL);
3267 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3268 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3269 represented in NEW_MODE. If FORCE is true, this will never happen, as
3270 we'll force-create a SUBREG if needed. */
3272 static rtx
3273 emit_move_change_mode (machine_mode new_mode,
3274 machine_mode old_mode, rtx x, bool force)
3276 rtx ret;
3278 if (push_operand (x, GET_MODE (x)))
3280 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3281 MEM_COPY_ATTRIBUTES (ret, x);
3283 else if (MEM_P (x))
3285 /* We don't have to worry about changing the address since the
3286 size in bytes is supposed to be the same. */
3287 if (reload_in_progress)
3289 /* Copy the MEM to change the mode and move any
3290 substitutions from the old MEM to the new one. */
3291 ret = adjust_address_nv (x, new_mode, 0);
3292 copy_replacements (x, ret);
3294 else
3295 ret = adjust_address (x, new_mode, 0);
3297 else
3299 /* Note that we do want simplify_subreg's behavior of validating
3300 that the new mode is ok for a hard register. If we were to use
3301 simplify_gen_subreg, we would create the subreg, but would
3302 probably run into the target not being able to implement it. */
3303 /* Except, of course, when FORCE is true, when this is exactly what
3304 we want. Which is needed for CCmodes on some targets. */
3305 if (force)
3306 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3307 else
3308 ret = simplify_subreg (new_mode, x, old_mode, 0);
3311 return ret;
3314 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3315 an integer mode of the same size as MODE. Returns the instruction
3316 emitted, or NULL if such a move could not be generated. */
3318 static rtx_insn *
3319 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3321 scalar_int_mode imode;
3322 enum insn_code code;
3324 /* There must exist a mode of the exact size we require. */
3325 if (!int_mode_for_mode (mode).exists (&imode))
3326 return NULL;
3328 /* The target must support moves in this mode. */
3329 code = optab_handler (mov_optab, imode);
3330 if (code == CODE_FOR_nothing)
3331 return NULL;
3333 x = emit_move_change_mode (imode, mode, x, force);
3334 if (x == NULL_RTX)
3335 return NULL;
3336 y = emit_move_change_mode (imode, mode, y, force);
3337 if (y == NULL_RTX)
3338 return NULL;
3339 return emit_insn (GEN_FCN (code) (x, y));
3342 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3343 Return an equivalent MEM that does not use an auto-increment. */
3346 emit_move_resolve_push (machine_mode mode, rtx x)
3348 enum rtx_code code = GET_CODE (XEXP (x, 0));
3349 HOST_WIDE_INT adjust;
3350 rtx temp;
3352 adjust = GET_MODE_SIZE (mode);
3353 #ifdef PUSH_ROUNDING
3354 adjust = PUSH_ROUNDING (adjust);
3355 #endif
3356 if (code == PRE_DEC || code == POST_DEC)
3357 adjust = -adjust;
3358 else if (code == PRE_MODIFY || code == POST_MODIFY)
3360 rtx expr = XEXP (XEXP (x, 0), 1);
3361 HOST_WIDE_INT val;
3363 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3364 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3365 val = INTVAL (XEXP (expr, 1));
3366 if (GET_CODE (expr) == MINUS)
3367 val = -val;
3368 gcc_assert (adjust == val || adjust == -val);
3369 adjust = val;
3372 /* Do not use anti_adjust_stack, since we don't want to update
3373 stack_pointer_delta. */
3374 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3375 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3376 0, OPTAB_LIB_WIDEN);
3377 if (temp != stack_pointer_rtx)
3378 emit_move_insn (stack_pointer_rtx, temp);
3380 switch (code)
3382 case PRE_INC:
3383 case PRE_DEC:
3384 case PRE_MODIFY:
3385 temp = stack_pointer_rtx;
3386 break;
3387 case POST_INC:
3388 case POST_DEC:
3389 case POST_MODIFY:
3390 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3391 break;
3392 default:
3393 gcc_unreachable ();
3396 return replace_equiv_address (x, temp);
3399 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3400 X is known to satisfy push_operand, and MODE is known to be complex.
3401 Returns the last instruction emitted. */
3403 rtx_insn *
3404 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3406 scalar_mode submode = GET_MODE_INNER (mode);
3407 bool imag_first;
3409 #ifdef PUSH_ROUNDING
3410 unsigned int submodesize = GET_MODE_SIZE (submode);
3412 /* In case we output to the stack, but the size is smaller than the
3413 machine can push exactly, we need to use move instructions. */
3414 if (PUSH_ROUNDING (submodesize) != submodesize)
3416 x = emit_move_resolve_push (mode, x);
3417 return emit_move_insn (x, y);
3419 #endif
3421 /* Note that the real part always precedes the imag part in memory
3422 regardless of machine's endianness. */
3423 switch (GET_CODE (XEXP (x, 0)))
3425 case PRE_DEC:
3426 case POST_DEC:
3427 imag_first = true;
3428 break;
3429 case PRE_INC:
3430 case POST_INC:
3431 imag_first = false;
3432 break;
3433 default:
3434 gcc_unreachable ();
3437 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3438 read_complex_part (y, imag_first));
3439 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3440 read_complex_part (y, !imag_first));
3443 /* A subroutine of emit_move_complex. Perform the move from Y to X
3444 via two moves of the parts. Returns the last instruction emitted. */
3446 rtx_insn *
3447 emit_move_complex_parts (rtx x, rtx y)
3449 /* Show the output dies here. This is necessary for SUBREGs
3450 of pseudos since we cannot track their lifetimes correctly;
3451 hard regs shouldn't appear here except as return values. */
3452 if (!reload_completed && !reload_in_progress
3453 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3454 emit_clobber (x);
3456 write_complex_part (x, read_complex_part (y, false), false);
3457 write_complex_part (x, read_complex_part (y, true), true);
3459 return get_last_insn ();
3462 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3463 MODE is known to be complex. Returns the last instruction emitted. */
3465 static rtx_insn *
3466 emit_move_complex (machine_mode mode, rtx x, rtx y)
3468 bool try_int;
3470 /* Need to take special care for pushes, to maintain proper ordering
3471 of the data, and possibly extra padding. */
3472 if (push_operand (x, mode))
3473 return emit_move_complex_push (mode, x, y);
3475 /* See if we can coerce the target into moving both values at once, except
3476 for floating point where we favor moving as parts if this is easy. */
3477 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3478 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3479 && !(REG_P (x)
3480 && HARD_REGISTER_P (x)
3481 && REG_NREGS (x) == 1)
3482 && !(REG_P (y)
3483 && HARD_REGISTER_P (y)
3484 && REG_NREGS (y) == 1))
3485 try_int = false;
3486 /* Not possible if the values are inherently not adjacent. */
3487 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3488 try_int = false;
3489 /* Is possible if both are registers (or subregs of registers). */
3490 else if (register_operand (x, mode) && register_operand (y, mode))
3491 try_int = true;
3492 /* If one of the operands is a memory, and alignment constraints
3493 are friendly enough, we may be able to do combined memory operations.
3494 We do not attempt this if Y is a constant because that combination is
3495 usually better with the by-parts thing below. */
3496 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3497 && (!STRICT_ALIGNMENT
3498 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3499 try_int = true;
3500 else
3501 try_int = false;
3503 if (try_int)
3505 rtx_insn *ret;
3507 /* For memory to memory moves, optimal behavior can be had with the
3508 existing block move logic. */
3509 if (MEM_P (x) && MEM_P (y))
3511 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3512 BLOCK_OP_NO_LIBCALL);
3513 return get_last_insn ();
3516 ret = emit_move_via_integer (mode, x, y, true);
3517 if (ret)
3518 return ret;
3521 return emit_move_complex_parts (x, y);
3524 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3525 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3527 static rtx_insn *
3528 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3530 rtx_insn *ret;
3532 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3533 if (mode != CCmode)
3535 enum insn_code code = optab_handler (mov_optab, CCmode);
3536 if (code != CODE_FOR_nothing)
3538 x = emit_move_change_mode (CCmode, mode, x, true);
3539 y = emit_move_change_mode (CCmode, mode, y, true);
3540 return emit_insn (GEN_FCN (code) (x, y));
3544 /* Otherwise, find the MODE_INT mode of the same width. */
3545 ret = emit_move_via_integer (mode, x, y, false);
3546 gcc_assert (ret != NULL);
3547 return ret;
3550 /* Return true if word I of OP lies entirely in the
3551 undefined bits of a paradoxical subreg. */
3553 static bool
3554 undefined_operand_subword_p (const_rtx op, int i)
3556 if (GET_CODE (op) != SUBREG)
3557 return false;
3558 machine_mode innermostmode = GET_MODE (SUBREG_REG (op));
3559 poly_int64 offset = i * UNITS_PER_WORD + subreg_memory_offset (op);
3560 return (known_ge (offset, GET_MODE_SIZE (innermostmode))
3561 || known_le (offset, -UNITS_PER_WORD));
3564 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3565 MODE is any multi-word or full-word mode that lacks a move_insn
3566 pattern. Note that you will get better code if you define such
3567 patterns, even if they must turn into multiple assembler instructions. */
3569 static rtx_insn *
3570 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3572 rtx_insn *last_insn = 0;
3573 rtx_insn *seq;
3574 rtx inner;
3575 bool need_clobber;
3576 int i;
3578 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3580 /* If X is a push on the stack, do the push now and replace
3581 X with a reference to the stack pointer. */
3582 if (push_operand (x, mode))
3583 x = emit_move_resolve_push (mode, x);
3585 /* If we are in reload, see if either operand is a MEM whose address
3586 is scheduled for replacement. */
3587 if (reload_in_progress && MEM_P (x)
3588 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3589 x = replace_equiv_address_nv (x, inner);
3590 if (reload_in_progress && MEM_P (y)
3591 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3592 y = replace_equiv_address_nv (y, inner);
3594 start_sequence ();
3596 need_clobber = false;
3597 for (i = 0;
3598 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3599 i++)
3601 rtx xpart = operand_subword (x, i, 1, mode);
3602 rtx ypart;
3604 /* Do not generate code for a move if it would come entirely
3605 from the undefined bits of a paradoxical subreg. */
3606 if (undefined_operand_subword_p (y, i))
3607 continue;
3609 ypart = operand_subword (y, i, 1, mode);
3611 /* If we can't get a part of Y, put Y into memory if it is a
3612 constant. Otherwise, force it into a register. Then we must
3613 be able to get a part of Y. */
3614 if (ypart == 0 && CONSTANT_P (y))
3616 y = use_anchored_address (force_const_mem (mode, y));
3617 ypart = operand_subword (y, i, 1, mode);
3619 else if (ypart == 0)
3620 ypart = operand_subword_force (y, i, mode);
3622 gcc_assert (xpart && ypart);
3624 need_clobber |= (GET_CODE (xpart) == SUBREG);
3626 last_insn = emit_move_insn (xpart, ypart);
3629 seq = get_insns ();
3630 end_sequence ();
3632 /* Show the output dies here. This is necessary for SUBREGs
3633 of pseudos since we cannot track their lifetimes correctly;
3634 hard regs shouldn't appear here except as return values.
3635 We never want to emit such a clobber after reload. */
3636 if (x != y
3637 && ! (reload_in_progress || reload_completed)
3638 && need_clobber != 0)
3639 emit_clobber (x);
3641 emit_insn (seq);
3643 return last_insn;
3646 /* Low level part of emit_move_insn.
3647 Called just like emit_move_insn, but assumes X and Y
3648 are basically valid. */
3650 rtx_insn *
3651 emit_move_insn_1 (rtx x, rtx y)
3653 machine_mode mode = GET_MODE (x);
3654 enum insn_code code;
3656 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3658 code = optab_handler (mov_optab, mode);
3659 if (code != CODE_FOR_nothing)
3660 return emit_insn (GEN_FCN (code) (x, y));
3662 /* Expand complex moves by moving real part and imag part. */
3663 if (COMPLEX_MODE_P (mode))
3664 return emit_move_complex (mode, x, y);
3666 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3667 || ALL_FIXED_POINT_MODE_P (mode))
3669 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3671 /* If we can't find an integer mode, use multi words. */
3672 if (result)
3673 return result;
3674 else
3675 return emit_move_multi_word (mode, x, y);
3678 if (GET_MODE_CLASS (mode) == MODE_CC)
3679 return emit_move_ccmode (mode, x, y);
3681 /* Try using a move pattern for the corresponding integer mode. This is
3682 only safe when simplify_subreg can convert MODE constants into integer
3683 constants. At present, it can only do this reliably if the value
3684 fits within a HOST_WIDE_INT. */
3685 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3687 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3689 if (ret)
3691 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3692 return ret;
3696 return emit_move_multi_word (mode, x, y);
3699 /* Generate code to copy Y into X.
3700 Both Y and X must have the same mode, except that
3701 Y can be a constant with VOIDmode.
3702 This mode cannot be BLKmode; use emit_block_move for that.
3704 Return the last instruction emitted. */
3706 rtx_insn *
3707 emit_move_insn (rtx x, rtx y)
3709 machine_mode mode = GET_MODE (x);
3710 rtx y_cst = NULL_RTX;
3711 rtx_insn *last_insn;
3712 rtx set;
3714 gcc_assert (mode != BLKmode
3715 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3717 if (CONSTANT_P (y))
3719 if (optimize
3720 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3721 && (last_insn = compress_float_constant (x, y)))
3722 return last_insn;
3724 y_cst = y;
3726 if (!targetm.legitimate_constant_p (mode, y))
3728 y = force_const_mem (mode, y);
3730 /* If the target's cannot_force_const_mem prevented the spill,
3731 assume that the target's move expanders will also take care
3732 of the non-legitimate constant. */
3733 if (!y)
3734 y = y_cst;
3735 else
3736 y = use_anchored_address (y);
3740 /* If X or Y are memory references, verify that their addresses are valid
3741 for the machine. */
3742 if (MEM_P (x)
3743 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3744 MEM_ADDR_SPACE (x))
3745 && ! push_operand (x, GET_MODE (x))))
3746 x = validize_mem (x);
3748 if (MEM_P (y)
3749 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3750 MEM_ADDR_SPACE (y)))
3751 y = validize_mem (y);
3753 gcc_assert (mode != BLKmode);
3755 last_insn = emit_move_insn_1 (x, y);
3757 if (y_cst && REG_P (x)
3758 && (set = single_set (last_insn)) != NULL_RTX
3759 && SET_DEST (set) == x
3760 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3761 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3763 return last_insn;
3766 /* Generate the body of an instruction to copy Y into X.
3767 It may be a list of insns, if one insn isn't enough. */
3769 rtx_insn *
3770 gen_move_insn (rtx x, rtx y)
3772 rtx_insn *seq;
3774 start_sequence ();
3775 emit_move_insn_1 (x, y);
3776 seq = get_insns ();
3777 end_sequence ();
3778 return seq;
3781 /* If Y is representable exactly in a narrower mode, and the target can
3782 perform the extension directly from constant or memory, then emit the
3783 move as an extension. */
3785 static rtx_insn *
3786 compress_float_constant (rtx x, rtx y)
3788 machine_mode dstmode = GET_MODE (x);
3789 machine_mode orig_srcmode = GET_MODE (y);
3790 machine_mode srcmode;
3791 const REAL_VALUE_TYPE *r;
3792 int oldcost, newcost;
3793 bool speed = optimize_insn_for_speed_p ();
3795 r = CONST_DOUBLE_REAL_VALUE (y);
3797 if (targetm.legitimate_constant_p (dstmode, y))
3798 oldcost = set_src_cost (y, orig_srcmode, speed);
3799 else
3800 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3802 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3804 enum insn_code ic;
3805 rtx trunc_y;
3806 rtx_insn *last_insn;
3808 /* Skip if the target can't extend this way. */
3809 ic = can_extend_p (dstmode, srcmode, 0);
3810 if (ic == CODE_FOR_nothing)
3811 continue;
3813 /* Skip if the narrowed value isn't exact. */
3814 if (! exact_real_truncate (srcmode, r))
3815 continue;
3817 trunc_y = const_double_from_real_value (*r, srcmode);
3819 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3821 /* Skip if the target needs extra instructions to perform
3822 the extension. */
3823 if (!insn_operand_matches (ic, 1, trunc_y))
3824 continue;
3825 /* This is valid, but may not be cheaper than the original. */
3826 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3827 dstmode, speed);
3828 if (oldcost < newcost)
3829 continue;
3831 else if (float_extend_from_mem[dstmode][srcmode])
3833 trunc_y = force_const_mem (srcmode, trunc_y);
3834 /* This is valid, but may not be cheaper than the original. */
3835 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3836 dstmode, speed);
3837 if (oldcost < newcost)
3838 continue;
3839 trunc_y = validize_mem (trunc_y);
3841 else
3842 continue;
3844 /* For CSE's benefit, force the compressed constant pool entry
3845 into a new pseudo. This constant may be used in different modes,
3846 and if not, combine will put things back together for us. */
3847 trunc_y = force_reg (srcmode, trunc_y);
3849 /* If x is a hard register, perform the extension into a pseudo,
3850 so that e.g. stack realignment code is aware of it. */
3851 rtx target = x;
3852 if (REG_P (x) && HARD_REGISTER_P (x))
3853 target = gen_reg_rtx (dstmode);
3855 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3856 last_insn = get_last_insn ();
3858 if (REG_P (target))
3859 set_unique_reg_note (last_insn, REG_EQUAL, y);
3861 if (target != x)
3862 return emit_move_insn (x, target);
3863 return last_insn;
3866 return NULL;
3869 /* Pushing data onto the stack. */
3871 /* Push a block of length SIZE (perhaps variable)
3872 and return an rtx to address the beginning of the block.
3873 The value may be virtual_outgoing_args_rtx.
3875 EXTRA is the number of bytes of padding to push in addition to SIZE.
3876 BELOW nonzero means this padding comes at low addresses;
3877 otherwise, the padding comes at high addresses. */
3880 push_block (rtx size, poly_int64 extra, int below)
3882 rtx temp;
3884 size = convert_modes (Pmode, ptr_mode, size, 1);
3885 if (CONSTANT_P (size))
3886 anti_adjust_stack (plus_constant (Pmode, size, extra));
3887 else if (REG_P (size) && known_eq (extra, 0))
3888 anti_adjust_stack (size);
3889 else
3891 temp = copy_to_mode_reg (Pmode, size);
3892 if (maybe_ne (extra, 0))
3893 temp = expand_binop (Pmode, add_optab, temp,
3894 gen_int_mode (extra, Pmode),
3895 temp, 0, OPTAB_LIB_WIDEN);
3896 anti_adjust_stack (temp);
3899 if (STACK_GROWS_DOWNWARD)
3901 temp = virtual_outgoing_args_rtx;
3902 if (maybe_ne (extra, 0) && below)
3903 temp = plus_constant (Pmode, temp, extra);
3905 else
3907 if (CONST_INT_P (size))
3908 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3909 -INTVAL (size) - (below ? 0 : extra));
3910 else if (maybe_ne (extra, 0) && !below)
3911 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3912 negate_rtx (Pmode, plus_constant (Pmode, size,
3913 extra)));
3914 else
3915 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3916 negate_rtx (Pmode, size));
3919 return memory_address (NARROWEST_INT_MODE, temp);
3922 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3924 static rtx
3925 mem_autoinc_base (rtx mem)
3927 if (MEM_P (mem))
3929 rtx addr = XEXP (mem, 0);
3930 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3931 return XEXP (addr, 0);
3933 return NULL;
3936 /* A utility routine used here, in reload, and in try_split. The insns
3937 after PREV up to and including LAST are known to adjust the stack,
3938 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3939 placing notes as appropriate. PREV may be NULL, indicating the
3940 entire insn sequence prior to LAST should be scanned.
3942 The set of allowed stack pointer modifications is small:
3943 (1) One or more auto-inc style memory references (aka pushes),
3944 (2) One or more addition/subtraction with the SP as destination,
3945 (3) A single move insn with the SP as destination,
3946 (4) A call_pop insn,
3947 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3949 Insns in the sequence that do not modify the SP are ignored,
3950 except for noreturn calls.
3952 The return value is the amount of adjustment that can be trivially
3953 verified, via immediate operand or auto-inc. If the adjustment
3954 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
3956 poly_int64
3957 find_args_size_adjust (rtx_insn *insn)
3959 rtx dest, set, pat;
3960 int i;
3962 pat = PATTERN (insn);
3963 set = NULL;
3965 /* Look for a call_pop pattern. */
3966 if (CALL_P (insn))
3968 /* We have to allow non-call_pop patterns for the case
3969 of emit_single_push_insn of a TLS address. */
3970 if (GET_CODE (pat) != PARALLEL)
3971 return 0;
3973 /* All call_pop have a stack pointer adjust in the parallel.
3974 The call itself is always first, and the stack adjust is
3975 usually last, so search from the end. */
3976 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3978 set = XVECEXP (pat, 0, i);
3979 if (GET_CODE (set) != SET)
3980 continue;
3981 dest = SET_DEST (set);
3982 if (dest == stack_pointer_rtx)
3983 break;
3985 /* We'd better have found the stack pointer adjust. */
3986 if (i == 0)
3987 return 0;
3988 /* Fall through to process the extracted SET and DEST
3989 as if it was a standalone insn. */
3991 else if (GET_CODE (pat) == SET)
3992 set = pat;
3993 else if ((set = single_set (insn)) != NULL)
3995 else if (GET_CODE (pat) == PARALLEL)
3997 /* ??? Some older ports use a parallel with a stack adjust
3998 and a store for a PUSH_ROUNDING pattern, rather than a
3999 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4000 /* ??? See h8300 and m68k, pushqi1. */
4001 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4003 set = XVECEXP (pat, 0, i);
4004 if (GET_CODE (set) != SET)
4005 continue;
4006 dest = SET_DEST (set);
4007 if (dest == stack_pointer_rtx)
4008 break;
4010 /* We do not expect an auto-inc of the sp in the parallel. */
4011 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4012 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4013 != stack_pointer_rtx);
4015 if (i < 0)
4016 return 0;
4018 else
4019 return 0;
4021 dest = SET_DEST (set);
4023 /* Look for direct modifications of the stack pointer. */
4024 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4026 /* Look for a trivial adjustment, otherwise assume nothing. */
4027 /* Note that the SPU restore_stack_block pattern refers to
4028 the stack pointer in V4SImode. Consider that non-trivial. */
4029 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4030 && GET_CODE (SET_SRC (set)) == PLUS
4031 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4032 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4033 return INTVAL (XEXP (SET_SRC (set), 1));
4034 /* ??? Reload can generate no-op moves, which will be cleaned
4035 up later. Recognize it and continue searching. */
4036 else if (rtx_equal_p (dest, SET_SRC (set)))
4037 return 0;
4038 else
4039 return HOST_WIDE_INT_MIN;
4041 else
4043 rtx mem, addr;
4045 /* Otherwise only think about autoinc patterns. */
4046 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4048 mem = dest;
4049 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4050 != stack_pointer_rtx);
4052 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4053 mem = SET_SRC (set);
4054 else
4055 return 0;
4057 addr = XEXP (mem, 0);
4058 switch (GET_CODE (addr))
4060 case PRE_INC:
4061 case POST_INC:
4062 return GET_MODE_SIZE (GET_MODE (mem));
4063 case PRE_DEC:
4064 case POST_DEC:
4065 return -GET_MODE_SIZE (GET_MODE (mem));
4066 case PRE_MODIFY:
4067 case POST_MODIFY:
4068 addr = XEXP (addr, 1);
4069 gcc_assert (GET_CODE (addr) == PLUS);
4070 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4071 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4072 return INTVAL (XEXP (addr, 1));
4073 default:
4074 gcc_unreachable ();
4079 poly_int64
4080 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4081 poly_int64 end_args_size)
4083 poly_int64 args_size = end_args_size;
4084 bool saw_unknown = false;
4085 rtx_insn *insn;
4087 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4089 if (!NONDEBUG_INSN_P (insn))
4090 continue;
4092 poly_int64 this_delta = find_args_size_adjust (insn);
4093 if (known_eq (this_delta, 0))
4095 if (!CALL_P (insn)
4096 || ACCUMULATE_OUTGOING_ARGS
4097 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4098 continue;
4101 gcc_assert (!saw_unknown);
4102 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4103 saw_unknown = true;
4105 add_args_size_note (insn, args_size);
4106 if (STACK_GROWS_DOWNWARD)
4107 this_delta = -poly_uint64 (this_delta);
4109 if (saw_unknown)
4110 args_size = HOST_WIDE_INT_MIN;
4111 else
4112 args_size -= this_delta;
4115 return args_size;
4118 #ifdef PUSH_ROUNDING
4119 /* Emit single push insn. */
4121 static void
4122 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4124 rtx dest_addr;
4125 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4126 rtx dest;
4127 enum insn_code icode;
4129 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4130 /* If there is push pattern, use it. Otherwise try old way of throwing
4131 MEM representing push operation to move expander. */
4132 icode = optab_handler (push_optab, mode);
4133 if (icode != CODE_FOR_nothing)
4135 struct expand_operand ops[1];
4137 create_input_operand (&ops[0], x, mode);
4138 if (maybe_expand_insn (icode, 1, ops))
4139 return;
4141 if (GET_MODE_SIZE (mode) == rounded_size)
4142 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4143 /* If we are to pad downward, adjust the stack pointer first and
4144 then store X into the stack location using an offset. This is
4145 because emit_move_insn does not know how to pad; it does not have
4146 access to type. */
4147 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4149 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4150 HOST_WIDE_INT offset;
4152 emit_move_insn (stack_pointer_rtx,
4153 expand_binop (Pmode,
4154 STACK_GROWS_DOWNWARD ? sub_optab
4155 : add_optab,
4156 stack_pointer_rtx,
4157 gen_int_mode (rounded_size, Pmode),
4158 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4160 offset = (HOST_WIDE_INT) padding_size;
4161 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4162 /* We have already decremented the stack pointer, so get the
4163 previous value. */
4164 offset += (HOST_WIDE_INT) rounded_size;
4166 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4167 /* We have already incremented the stack pointer, so get the
4168 previous value. */
4169 offset -= (HOST_WIDE_INT) rounded_size;
4171 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4172 gen_int_mode (offset, Pmode));
4174 else
4176 if (STACK_GROWS_DOWNWARD)
4177 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4178 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4179 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4180 Pmode));
4181 else
4182 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4183 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4184 gen_int_mode (rounded_size, Pmode));
4186 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4189 dest = gen_rtx_MEM (mode, dest_addr);
4191 if (type != 0)
4193 set_mem_attributes (dest, type, 1);
4195 if (cfun->tail_call_marked)
4196 /* Function incoming arguments may overlap with sibling call
4197 outgoing arguments and we cannot allow reordering of reads
4198 from function arguments with stores to outgoing arguments
4199 of sibling calls. */
4200 set_mem_alias_set (dest, 0);
4202 emit_move_insn (dest, x);
4205 /* Emit and annotate a single push insn. */
4207 static void
4208 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4210 poly_int64 delta, old_delta = stack_pointer_delta;
4211 rtx_insn *prev = get_last_insn ();
4212 rtx_insn *last;
4214 emit_single_push_insn_1 (mode, x, type);
4216 last = get_last_insn ();
4218 /* Notice the common case where we emitted exactly one insn. */
4219 if (PREV_INSN (last) == prev)
4221 add_args_size_note (last, stack_pointer_delta);
4222 return;
4225 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4226 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4227 || known_eq (delta, old_delta));
4229 #endif
4231 /* If reading SIZE bytes from X will end up reading from
4232 Y return the number of bytes that overlap. Return -1
4233 if there is no overlap or -2 if we can't determine
4234 (for example when X and Y have different base registers). */
4236 static int
4237 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4239 rtx tmp = plus_constant (Pmode, x, size);
4240 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4242 if (!CONST_INT_P (sub))
4243 return -2;
4245 HOST_WIDE_INT val = INTVAL (sub);
4247 return IN_RANGE (val, 1, size) ? val : -1;
4250 /* Generate code to push X onto the stack, assuming it has mode MODE and
4251 type TYPE.
4252 MODE is redundant except when X is a CONST_INT (since they don't
4253 carry mode info).
4254 SIZE is an rtx for the size of data to be copied (in bytes),
4255 needed only if X is BLKmode.
4256 Return true if successful. May return false if asked to push a
4257 partial argument during a sibcall optimization (as specified by
4258 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4259 to not overlap.
4261 ALIGN (in bits) is maximum alignment we can assume.
4263 If PARTIAL and REG are both nonzero, then copy that many of the first
4264 bytes of X into registers starting with REG, and push the rest of X.
4265 The amount of space pushed is decreased by PARTIAL bytes.
4266 REG must be a hard register in this case.
4267 If REG is zero but PARTIAL is not, take any all others actions for an
4268 argument partially in registers, but do not actually load any
4269 registers.
4271 EXTRA is the amount in bytes of extra space to leave next to this arg.
4272 This is ignored if an argument block has already been allocated.
4274 On a machine that lacks real push insns, ARGS_ADDR is the address of
4275 the bottom of the argument block for this call. We use indexing off there
4276 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4277 argument block has not been preallocated.
4279 ARGS_SO_FAR is the size of args previously pushed for this call.
4281 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4282 for arguments passed in registers. If nonzero, it will be the number
4283 of bytes required. */
4285 bool
4286 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4287 unsigned int align, int partial, rtx reg, poly_int64 extra,
4288 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4289 rtx alignment_pad, bool sibcall_p)
4291 rtx xinner;
4292 pad_direction stack_direction
4293 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4295 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4296 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4297 Default is below for small data on big-endian machines; else above. */
4298 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4300 /* Invert direction if stack is post-decrement.
4301 FIXME: why? */
4302 if (STACK_PUSH_CODE == POST_DEC)
4303 if (where_pad != PAD_NONE)
4304 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4306 xinner = x;
4308 int nregs = partial / UNITS_PER_WORD;
4309 rtx *tmp_regs = NULL;
4310 int overlapping = 0;
4312 if (mode == BLKmode
4313 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4315 /* Copy a block into the stack, entirely or partially. */
4317 rtx temp;
4318 int used;
4319 int offset;
4320 int skip;
4322 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4323 used = partial - offset;
4325 if (mode != BLKmode)
4327 /* A value is to be stored in an insufficiently aligned
4328 stack slot; copy via a suitably aligned slot if
4329 necessary. */
4330 size = GEN_INT (GET_MODE_SIZE (mode));
4331 if (!MEM_P (xinner))
4333 temp = assign_temp (type, 1, 1);
4334 emit_move_insn (temp, xinner);
4335 xinner = temp;
4339 gcc_assert (size);
4341 /* USED is now the # of bytes we need not copy to the stack
4342 because registers will take care of them. */
4344 if (partial != 0)
4345 xinner = adjust_address (xinner, BLKmode, used);
4347 /* If the partial register-part of the arg counts in its stack size,
4348 skip the part of stack space corresponding to the registers.
4349 Otherwise, start copying to the beginning of the stack space,
4350 by setting SKIP to 0. */
4351 skip = (reg_parm_stack_space == 0) ? 0 : used;
4353 #ifdef PUSH_ROUNDING
4354 /* Do it with several push insns if that doesn't take lots of insns
4355 and if there is no difficulty with push insns that skip bytes
4356 on the stack for alignment purposes. */
4357 if (args_addr == 0
4358 && PUSH_ARGS
4359 && CONST_INT_P (size)
4360 && skip == 0
4361 && MEM_ALIGN (xinner) >= align
4362 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4363 /* Here we avoid the case of a structure whose weak alignment
4364 forces many pushes of a small amount of data,
4365 and such small pushes do rounding that causes trouble. */
4366 && ((!targetm.slow_unaligned_access (word_mode, align))
4367 || align >= BIGGEST_ALIGNMENT
4368 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4369 == (align / BITS_PER_UNIT)))
4370 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4372 /* Push padding now if padding above and stack grows down,
4373 or if padding below and stack grows up.
4374 But if space already allocated, this has already been done. */
4375 if (maybe_ne (extra, 0)
4376 && args_addr == 0
4377 && where_pad != PAD_NONE
4378 && where_pad != stack_direction)
4379 anti_adjust_stack (gen_int_mode (extra, Pmode));
4381 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4383 else
4384 #endif /* PUSH_ROUNDING */
4386 rtx target;
4388 /* Otherwise make space on the stack and copy the data
4389 to the address of that space. */
4391 /* Deduct words put into registers from the size we must copy. */
4392 if (partial != 0)
4394 if (CONST_INT_P (size))
4395 size = GEN_INT (INTVAL (size) - used);
4396 else
4397 size = expand_binop (GET_MODE (size), sub_optab, size,
4398 gen_int_mode (used, GET_MODE (size)),
4399 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4402 /* Get the address of the stack space.
4403 In this case, we do not deal with EXTRA separately.
4404 A single stack adjust will do. */
4405 if (! args_addr)
4407 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4408 extra = 0;
4410 else if (CONST_INT_P (args_so_far))
4411 temp = memory_address (BLKmode,
4412 plus_constant (Pmode, args_addr,
4413 skip + INTVAL (args_so_far)));
4414 else
4415 temp = memory_address (BLKmode,
4416 plus_constant (Pmode,
4417 gen_rtx_PLUS (Pmode,
4418 args_addr,
4419 args_so_far),
4420 skip));
4422 if (!ACCUMULATE_OUTGOING_ARGS)
4424 /* If the source is referenced relative to the stack pointer,
4425 copy it to another register to stabilize it. We do not need
4426 to do this if we know that we won't be changing sp. */
4428 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4429 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4430 temp = copy_to_reg (temp);
4433 target = gen_rtx_MEM (BLKmode, temp);
4435 /* We do *not* set_mem_attributes here, because incoming arguments
4436 may overlap with sibling call outgoing arguments and we cannot
4437 allow reordering of reads from function arguments with stores
4438 to outgoing arguments of sibling calls. We do, however, want
4439 to record the alignment of the stack slot. */
4440 /* ALIGN may well be better aligned than TYPE, e.g. due to
4441 PARM_BOUNDARY. Assume the caller isn't lying. */
4442 set_mem_align (target, align);
4444 /* If part should go in registers and pushing to that part would
4445 overwrite some of the values that need to go into regs, load the
4446 overlapping values into temporary pseudos to be moved into the hard
4447 regs at the end after the stack pushing has completed.
4448 We cannot load them directly into the hard regs here because
4449 they can be clobbered by the block move expansions.
4450 See PR 65358. */
4452 if (partial > 0 && reg != 0 && mode == BLKmode
4453 && GET_CODE (reg) != PARALLEL)
4455 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4456 if (overlapping > 0)
4458 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4459 overlapping /= UNITS_PER_WORD;
4461 tmp_regs = XALLOCAVEC (rtx, overlapping);
4463 for (int i = 0; i < overlapping; i++)
4464 tmp_regs[i] = gen_reg_rtx (word_mode);
4466 for (int i = 0; i < overlapping; i++)
4467 emit_move_insn (tmp_regs[i],
4468 operand_subword_force (target, i, mode));
4470 else if (overlapping == -1)
4471 overlapping = 0;
4472 /* Could not determine whether there is overlap.
4473 Fail the sibcall. */
4474 else
4476 overlapping = 0;
4477 if (sibcall_p)
4478 return false;
4481 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4484 else if (partial > 0)
4486 /* Scalar partly in registers. */
4488 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4489 int i;
4490 int not_stack;
4491 /* # bytes of start of argument
4492 that we must make space for but need not store. */
4493 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4494 int args_offset = INTVAL (args_so_far);
4495 int skip;
4497 /* Push padding now if padding above and stack grows down,
4498 or if padding below and stack grows up.
4499 But if space already allocated, this has already been done. */
4500 if (maybe_ne (extra, 0)
4501 && args_addr == 0
4502 && where_pad != PAD_NONE
4503 && where_pad != stack_direction)
4504 anti_adjust_stack (gen_int_mode (extra, Pmode));
4506 /* If we make space by pushing it, we might as well push
4507 the real data. Otherwise, we can leave OFFSET nonzero
4508 and leave the space uninitialized. */
4509 if (args_addr == 0)
4510 offset = 0;
4512 /* Now NOT_STACK gets the number of words that we don't need to
4513 allocate on the stack. Convert OFFSET to words too. */
4514 not_stack = (partial - offset) / UNITS_PER_WORD;
4515 offset /= UNITS_PER_WORD;
4517 /* If the partial register-part of the arg counts in its stack size,
4518 skip the part of stack space corresponding to the registers.
4519 Otherwise, start copying to the beginning of the stack space,
4520 by setting SKIP to 0. */
4521 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4523 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4524 x = validize_mem (force_const_mem (mode, x));
4526 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4527 SUBREGs of such registers are not allowed. */
4528 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4529 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4530 x = copy_to_reg (x);
4532 /* Loop over all the words allocated on the stack for this arg. */
4533 /* We can do it by words, because any scalar bigger than a word
4534 has a size a multiple of a word. */
4535 for (i = size - 1; i >= not_stack; i--)
4536 if (i >= not_stack + offset)
4537 if (!emit_push_insn (operand_subword_force (x, i, mode),
4538 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4539 0, args_addr,
4540 GEN_INT (args_offset + ((i - not_stack + skip)
4541 * UNITS_PER_WORD)),
4542 reg_parm_stack_space, alignment_pad, sibcall_p))
4543 return false;
4545 else
4547 rtx addr;
4548 rtx dest;
4550 /* Push padding now if padding above and stack grows down,
4551 or if padding below and stack grows up.
4552 But if space already allocated, this has already been done. */
4553 if (maybe_ne (extra, 0)
4554 && args_addr == 0
4555 && where_pad != PAD_NONE
4556 && where_pad != stack_direction)
4557 anti_adjust_stack (gen_int_mode (extra, Pmode));
4559 #ifdef PUSH_ROUNDING
4560 if (args_addr == 0 && PUSH_ARGS)
4561 emit_single_push_insn (mode, x, type);
4562 else
4563 #endif
4565 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4566 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4568 /* We do *not* set_mem_attributes here, because incoming arguments
4569 may overlap with sibling call outgoing arguments and we cannot
4570 allow reordering of reads from function arguments with stores
4571 to outgoing arguments of sibling calls. We do, however, want
4572 to record the alignment of the stack slot. */
4573 /* ALIGN may well be better aligned than TYPE, e.g. due to
4574 PARM_BOUNDARY. Assume the caller isn't lying. */
4575 set_mem_align (dest, align);
4577 emit_move_insn (dest, x);
4581 /* Move the partial arguments into the registers and any overlapping
4582 values that we moved into the pseudos in tmp_regs. */
4583 if (partial > 0 && reg != 0)
4585 /* Handle calls that pass values in multiple non-contiguous locations.
4586 The Irix 6 ABI has examples of this. */
4587 if (GET_CODE (reg) == PARALLEL)
4588 emit_group_load (reg, x, type, -1);
4589 else
4591 gcc_assert (partial % UNITS_PER_WORD == 0);
4592 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4594 for (int i = 0; i < overlapping; i++)
4595 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4596 + nregs - overlapping + i),
4597 tmp_regs[i]);
4602 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4603 anti_adjust_stack (gen_int_mode (extra, Pmode));
4605 if (alignment_pad && args_addr == 0)
4606 anti_adjust_stack (alignment_pad);
4608 return true;
4611 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4612 operations. */
4614 static rtx
4615 get_subtarget (rtx x)
4617 return (optimize
4618 || x == 0
4619 /* Only registers can be subtargets. */
4620 || !REG_P (x)
4621 /* Don't use hard regs to avoid extending their life. */
4622 || REGNO (x) < FIRST_PSEUDO_REGISTER
4623 ? 0 : x);
4626 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4627 FIELD is a bitfield. Returns true if the optimization was successful,
4628 and there's nothing else to do. */
4630 static bool
4631 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
4632 poly_uint64 pbitpos,
4633 poly_uint64 pbitregion_start,
4634 poly_uint64 pbitregion_end,
4635 machine_mode mode1, rtx str_rtx,
4636 tree to, tree src, bool reverse)
4638 machine_mode str_mode = GET_MODE (str_rtx);
4639 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4640 tree op0, op1;
4641 rtx value, result;
4642 optab binop;
4643 gimple *srcstmt;
4644 enum tree_code code;
4646 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
4647 if (mode1 != VOIDmode
4648 || !pbitsize.is_constant (&bitsize)
4649 || !pbitpos.is_constant (&bitpos)
4650 || !pbitregion_start.is_constant (&bitregion_start)
4651 || !pbitregion_end.is_constant (&bitregion_end)
4652 || bitsize >= BITS_PER_WORD
4653 || str_bitsize > BITS_PER_WORD
4654 || TREE_SIDE_EFFECTS (to)
4655 || TREE_THIS_VOLATILE (to))
4656 return false;
4658 STRIP_NOPS (src);
4659 if (TREE_CODE (src) != SSA_NAME)
4660 return false;
4661 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4662 return false;
4664 srcstmt = get_gimple_for_ssa_name (src);
4665 if (!srcstmt
4666 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4667 return false;
4669 code = gimple_assign_rhs_code (srcstmt);
4671 op0 = gimple_assign_rhs1 (srcstmt);
4673 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4674 to find its initialization. Hopefully the initialization will
4675 be from a bitfield load. */
4676 if (TREE_CODE (op0) == SSA_NAME)
4678 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4680 /* We want to eventually have OP0 be the same as TO, which
4681 should be a bitfield. */
4682 if (!op0stmt
4683 || !is_gimple_assign (op0stmt)
4684 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4685 return false;
4686 op0 = gimple_assign_rhs1 (op0stmt);
4689 op1 = gimple_assign_rhs2 (srcstmt);
4691 if (!operand_equal_p (to, op0, 0))
4692 return false;
4694 if (MEM_P (str_rtx))
4696 unsigned HOST_WIDE_INT offset1;
4698 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4699 str_bitsize = BITS_PER_WORD;
4701 scalar_int_mode best_mode;
4702 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4703 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4704 return false;
4705 str_mode = best_mode;
4706 str_bitsize = GET_MODE_BITSIZE (best_mode);
4708 offset1 = bitpos;
4709 bitpos %= str_bitsize;
4710 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4711 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4713 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4714 return false;
4715 else
4716 gcc_assert (!reverse);
4718 /* If the bit field covers the whole REG/MEM, store_field
4719 will likely generate better code. */
4720 if (bitsize >= str_bitsize)
4721 return false;
4723 /* We can't handle fields split across multiple entities. */
4724 if (bitpos + bitsize > str_bitsize)
4725 return false;
4727 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4728 bitpos = str_bitsize - bitpos - bitsize;
4730 switch (code)
4732 case PLUS_EXPR:
4733 case MINUS_EXPR:
4734 /* For now, just optimize the case of the topmost bitfield
4735 where we don't need to do any masking and also
4736 1 bit bitfields where xor can be used.
4737 We might win by one instruction for the other bitfields
4738 too if insv/extv instructions aren't used, so that
4739 can be added later. */
4740 if ((reverse || bitpos + bitsize != str_bitsize)
4741 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4742 break;
4744 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4745 value = convert_modes (str_mode,
4746 TYPE_MODE (TREE_TYPE (op1)), value,
4747 TYPE_UNSIGNED (TREE_TYPE (op1)));
4749 /* We may be accessing data outside the field, which means
4750 we can alias adjacent data. */
4751 if (MEM_P (str_rtx))
4753 str_rtx = shallow_copy_rtx (str_rtx);
4754 set_mem_alias_set (str_rtx, 0);
4755 set_mem_expr (str_rtx, 0);
4758 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4760 value = expand_and (str_mode, value, const1_rtx, NULL);
4761 binop = xor_optab;
4763 else
4764 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4766 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4767 if (reverse)
4768 value = flip_storage_order (str_mode, value);
4769 result = expand_binop (str_mode, binop, str_rtx,
4770 value, str_rtx, 1, OPTAB_WIDEN);
4771 if (result != str_rtx)
4772 emit_move_insn (str_rtx, result);
4773 return true;
4775 case BIT_IOR_EXPR:
4776 case BIT_XOR_EXPR:
4777 if (TREE_CODE (op1) != INTEGER_CST)
4778 break;
4779 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4780 value = convert_modes (str_mode,
4781 TYPE_MODE (TREE_TYPE (op1)), value,
4782 TYPE_UNSIGNED (TREE_TYPE (op1)));
4784 /* We may be accessing data outside the field, which means
4785 we can alias adjacent data. */
4786 if (MEM_P (str_rtx))
4788 str_rtx = shallow_copy_rtx (str_rtx);
4789 set_mem_alias_set (str_rtx, 0);
4790 set_mem_expr (str_rtx, 0);
4793 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4794 if (bitpos + bitsize != str_bitsize)
4796 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4797 str_mode);
4798 value = expand_and (str_mode, value, mask, NULL_RTX);
4800 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4801 if (reverse)
4802 value = flip_storage_order (str_mode, value);
4803 result = expand_binop (str_mode, binop, str_rtx,
4804 value, str_rtx, 1, OPTAB_WIDEN);
4805 if (result != str_rtx)
4806 emit_move_insn (str_rtx, result);
4807 return true;
4809 default:
4810 break;
4813 return false;
4816 /* In the C++ memory model, consecutive bit fields in a structure are
4817 considered one memory location.
4819 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4820 returns the bit range of consecutive bits in which this COMPONENT_REF
4821 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4822 and *OFFSET may be adjusted in the process.
4824 If the access does not need to be restricted, 0 is returned in both
4825 *BITSTART and *BITEND. */
4827 void
4828 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
4829 poly_int64_pod *bitpos, tree *offset)
4831 poly_int64 bitoffset;
4832 tree field, repr;
4834 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4836 field = TREE_OPERAND (exp, 1);
4837 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4838 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4839 need to limit the range we can access. */
4840 if (!repr)
4842 *bitstart = *bitend = 0;
4843 return;
4846 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4847 part of a larger bit field, then the representative does not serve any
4848 useful purpose. This can occur in Ada. */
4849 if (handled_component_p (TREE_OPERAND (exp, 0)))
4851 machine_mode rmode;
4852 poly_int64 rbitsize, rbitpos;
4853 tree roffset;
4854 int unsignedp, reversep, volatilep = 0;
4855 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4856 &roffset, &rmode, &unsignedp, &reversep,
4857 &volatilep);
4858 if (!multiple_p (rbitpos, BITS_PER_UNIT))
4860 *bitstart = *bitend = 0;
4861 return;
4865 /* Compute the adjustment to bitpos from the offset of the field
4866 relative to the representative. DECL_FIELD_OFFSET of field and
4867 repr are the same by construction if they are not constants,
4868 see finish_bitfield_layout. */
4869 poly_uint64 field_offset, repr_offset;
4870 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
4871 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
4872 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
4873 else
4874 bitoffset = 0;
4875 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4876 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4878 /* If the adjustment is larger than bitpos, we would have a negative bit
4879 position for the lower bound and this may wreak havoc later. Adjust
4880 offset and bitpos to make the lower bound non-negative in that case. */
4881 if (maybe_gt (bitoffset, *bitpos))
4883 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
4884 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
4886 *bitpos += adjust_bits;
4887 if (*offset == NULL_TREE)
4888 *offset = size_int (-adjust_bytes);
4889 else
4890 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
4891 *bitstart = 0;
4893 else
4894 *bitstart = *bitpos - bitoffset;
4896 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4899 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4900 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4901 DECL_RTL was not set yet, return NORTL. */
4903 static inline bool
4904 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4906 if (TREE_CODE (addr) != ADDR_EXPR)
4907 return false;
4909 tree base = TREE_OPERAND (addr, 0);
4911 if (!DECL_P (base)
4912 || TREE_ADDRESSABLE (base)
4913 || DECL_MODE (base) == BLKmode)
4914 return false;
4916 if (!DECL_RTL_SET_P (base))
4917 return nortl;
4919 return (!MEM_P (DECL_RTL (base)));
4922 /* Returns true if the MEM_REF REF refers to an object that does not
4923 reside in memory and has non-BLKmode. */
4925 static inline bool
4926 mem_ref_refers_to_non_mem_p (tree ref)
4928 tree base = TREE_OPERAND (ref, 0);
4929 return addr_expr_of_non_mem_decl_p_1 (base, false);
4932 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4933 is true, try generating a nontemporal store. */
4935 void
4936 expand_assignment (tree to, tree from, bool nontemporal)
4938 rtx to_rtx = 0;
4939 rtx result;
4940 machine_mode mode;
4941 unsigned int align;
4942 enum insn_code icode;
4944 /* Don't crash if the lhs of the assignment was erroneous. */
4945 if (TREE_CODE (to) == ERROR_MARK)
4947 expand_normal (from);
4948 return;
4951 /* Optimize away no-op moves without side-effects. */
4952 if (operand_equal_p (to, from, 0))
4953 return;
4955 /* Handle misaligned stores. */
4956 mode = TYPE_MODE (TREE_TYPE (to));
4957 if ((TREE_CODE (to) == MEM_REF
4958 || TREE_CODE (to) == TARGET_MEM_REF)
4959 && mode != BLKmode
4960 && !mem_ref_refers_to_non_mem_p (to)
4961 && ((align = get_object_alignment (to))
4962 < GET_MODE_ALIGNMENT (mode))
4963 && (((icode = optab_handler (movmisalign_optab, mode))
4964 != CODE_FOR_nothing)
4965 || targetm.slow_unaligned_access (mode, align)))
4967 rtx reg, mem;
4969 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4970 reg = force_not_mem (reg);
4971 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4972 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4973 reg = flip_storage_order (mode, reg);
4975 if (icode != CODE_FOR_nothing)
4977 struct expand_operand ops[2];
4979 create_fixed_operand (&ops[0], mem);
4980 create_input_operand (&ops[1], reg, mode);
4981 /* The movmisalign<mode> pattern cannot fail, else the assignment
4982 would silently be omitted. */
4983 expand_insn (icode, 2, ops);
4985 else
4986 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4987 false);
4988 return;
4991 /* Assignment of a structure component needs special treatment
4992 if the structure component's rtx is not simply a MEM.
4993 Assignment of an array element at a constant index, and assignment of
4994 an array element in an unaligned packed structure field, has the same
4995 problem. Same for (partially) storing into a non-memory object. */
4996 if (handled_component_p (to)
4997 || (TREE_CODE (to) == MEM_REF
4998 && (REF_REVERSE_STORAGE_ORDER (to)
4999 || mem_ref_refers_to_non_mem_p (to)))
5000 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5002 machine_mode mode1;
5003 poly_int64 bitsize, bitpos;
5004 poly_uint64 bitregion_start = 0;
5005 poly_uint64 bitregion_end = 0;
5006 tree offset;
5007 int unsignedp, reversep, volatilep = 0;
5008 tree tem;
5010 push_temp_slots ();
5011 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5012 &unsignedp, &reversep, &volatilep);
5014 /* Make sure bitpos is not negative, it can wreak havoc later. */
5015 if (maybe_lt (bitpos, 0))
5017 gcc_assert (offset == NULL_TREE);
5018 offset = size_int (bits_to_bytes_round_down (bitpos));
5019 bitpos = num_trailing_bits (bitpos);
5022 if (TREE_CODE (to) == COMPONENT_REF
5023 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5024 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5025 /* The C++ memory model naturally applies to byte-aligned fields.
5026 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5027 BITSIZE are not byte-aligned, there is no need to limit the range
5028 we can access. This can occur with packed structures in Ada. */
5029 else if (maybe_gt (bitsize, 0)
5030 && multiple_p (bitsize, BITS_PER_UNIT)
5031 && multiple_p (bitpos, BITS_PER_UNIT))
5033 bitregion_start = bitpos;
5034 bitregion_end = bitpos + bitsize - 1;
5037 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5039 /* If the field has a mode, we want to access it in the
5040 field's mode, not the computed mode.
5041 If a MEM has VOIDmode (external with incomplete type),
5042 use BLKmode for it instead. */
5043 if (MEM_P (to_rtx))
5045 if (mode1 != VOIDmode)
5046 to_rtx = adjust_address (to_rtx, mode1, 0);
5047 else if (GET_MODE (to_rtx) == VOIDmode)
5048 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5051 if (offset != 0)
5053 machine_mode address_mode;
5054 rtx offset_rtx;
5056 if (!MEM_P (to_rtx))
5058 /* We can get constant negative offsets into arrays with broken
5059 user code. Translate this to a trap instead of ICEing. */
5060 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5061 expand_builtin_trap ();
5062 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5065 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5066 address_mode = get_address_mode (to_rtx);
5067 if (GET_MODE (offset_rtx) != address_mode)
5069 /* We cannot be sure that the RTL in offset_rtx is valid outside
5070 of a memory address context, so force it into a register
5071 before attempting to convert it to the desired mode. */
5072 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5073 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5076 /* If we have an expression in OFFSET_RTX and a non-zero
5077 byte offset in BITPOS, adding the byte offset before the
5078 OFFSET_RTX results in better intermediate code, which makes
5079 later rtl optimization passes perform better.
5081 We prefer intermediate code like this:
5083 r124:DI=r123:DI+0x18
5084 [r124:DI]=r121:DI
5086 ... instead of ...
5088 r124:DI=r123:DI+0x10
5089 [r124:DI+0x8]=r121:DI
5091 This is only done for aligned data values, as these can
5092 be expected to result in single move instructions. */
5093 poly_int64 bytepos;
5094 if (mode1 != VOIDmode
5095 && maybe_ne (bitpos, 0)
5096 && maybe_gt (bitsize, 0)
5097 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5098 && multiple_p (bitpos, bitsize)
5099 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5100 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5102 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5103 bitregion_start = 0;
5104 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5105 bitregion_end -= bitpos;
5106 bitpos = 0;
5109 to_rtx = offset_address (to_rtx, offset_rtx,
5110 highest_pow2_factor_for_target (to,
5111 offset));
5114 /* No action is needed if the target is not a memory and the field
5115 lies completely outside that target. This can occur if the source
5116 code contains an out-of-bounds access to a small array. */
5117 if (!MEM_P (to_rtx)
5118 && GET_MODE (to_rtx) != BLKmode
5119 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5121 expand_normal (from);
5122 result = NULL;
5124 /* Handle expand_expr of a complex value returning a CONCAT. */
5125 else if (GET_CODE (to_rtx) == CONCAT)
5127 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5128 if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE (to_rtx)
5129 && COMPLEX_MODE_P (GET_MODE (to_rtx))
5130 && known_eq (bitpos, 0)
5131 && known_eq (bitsize, mode_bitsize))
5132 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5133 else if (known_eq (bitsize, mode_bitsize / 2)
5134 && (known_eq (bitpos, 0)
5135 || known_eq (bitpos, mode_bitsize / 2)))
5136 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5137 false, nontemporal, reversep);
5138 else if (known_le (bitpos + bitsize, mode_bitsize / 2))
5139 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5140 bitregion_start, bitregion_end,
5141 mode1, from, get_alias_set (to),
5142 nontemporal, reversep);
5143 else if (known_ge (bitpos, mode_bitsize / 2))
5144 result = store_field (XEXP (to_rtx, 1), bitsize,
5145 bitpos - mode_bitsize / 2,
5146 bitregion_start, bitregion_end,
5147 mode1, from, get_alias_set (to),
5148 nontemporal, reversep);
5149 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5151 result = expand_normal (from);
5152 if (GET_CODE (result) == CONCAT)
5154 machine_mode to_mode = GET_MODE_INNER (GET_MODE (to_rtx));
5155 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5156 rtx from_real
5157 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5158 from_mode, 0);
5159 rtx from_imag
5160 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5161 from_mode, 1);
5162 emit_move_insn (XEXP (to_rtx, 0), from_real);
5163 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5165 else
5167 rtx from_rtx
5168 = simplify_gen_subreg (GET_MODE (to_rtx), result,
5169 TYPE_MODE (TREE_TYPE (from)), 0);
5170 emit_move_insn (XEXP (to_rtx, 0),
5171 read_complex_part (from_rtx, false));
5172 emit_move_insn (XEXP (to_rtx, 1),
5173 read_complex_part (from_rtx, true));
5176 else
5178 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5179 GET_MODE_SIZE (GET_MODE (to_rtx)));
5180 write_complex_part (temp, XEXP (to_rtx, 0), false);
5181 write_complex_part (temp, XEXP (to_rtx, 1), true);
5182 result = store_field (temp, bitsize, bitpos,
5183 bitregion_start, bitregion_end,
5184 mode1, from, get_alias_set (to),
5185 nontemporal, reversep);
5186 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5187 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5190 else
5192 if (MEM_P (to_rtx))
5194 /* If the field is at offset zero, we could have been given the
5195 DECL_RTX of the parent struct. Don't munge it. */
5196 to_rtx = shallow_copy_rtx (to_rtx);
5197 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5198 if (volatilep)
5199 MEM_VOLATILE_P (to_rtx) = 1;
5202 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5203 bitregion_start, bitregion_end,
5204 mode1, to_rtx, to, from,
5205 reversep))
5206 result = NULL;
5207 else
5208 result = store_field (to_rtx, bitsize, bitpos,
5209 bitregion_start, bitregion_end,
5210 mode1, from, get_alias_set (to),
5211 nontemporal, reversep);
5214 if (result)
5215 preserve_temp_slots (result);
5216 pop_temp_slots ();
5217 return;
5220 /* If the rhs is a function call and its value is not an aggregate,
5221 call the function before we start to compute the lhs.
5222 This is needed for correct code for cases such as
5223 val = setjmp (buf) on machines where reference to val
5224 requires loading up part of an address in a separate insn.
5226 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5227 since it might be a promoted variable where the zero- or sign- extension
5228 needs to be done. Handling this in the normal way is safe because no
5229 computation is done before the call. The same is true for SSA names. */
5230 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5231 && COMPLETE_TYPE_P (TREE_TYPE (from))
5232 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5233 && ! (((VAR_P (to)
5234 || TREE_CODE (to) == PARM_DECL
5235 || TREE_CODE (to) == RESULT_DECL)
5236 && REG_P (DECL_RTL (to)))
5237 || TREE_CODE (to) == SSA_NAME))
5239 rtx value;
5240 rtx bounds;
5242 push_temp_slots ();
5243 value = expand_normal (from);
5245 /* Split value and bounds to store them separately. */
5246 chkp_split_slot (value, &value, &bounds);
5248 if (to_rtx == 0)
5249 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5251 /* Handle calls that return values in multiple non-contiguous locations.
5252 The Irix 6 ABI has examples of this. */
5253 if (GET_CODE (to_rtx) == PARALLEL)
5255 if (GET_CODE (value) == PARALLEL)
5256 emit_group_move (to_rtx, value);
5257 else
5258 emit_group_load (to_rtx, value, TREE_TYPE (from),
5259 int_size_in_bytes (TREE_TYPE (from)));
5261 else if (GET_CODE (value) == PARALLEL)
5262 emit_group_store (to_rtx, value, TREE_TYPE (from),
5263 int_size_in_bytes (TREE_TYPE (from)));
5264 else if (GET_MODE (to_rtx) == BLKmode)
5266 /* Handle calls that return BLKmode values in registers. */
5267 if (REG_P (value))
5268 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5269 else
5270 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5272 else
5274 if (POINTER_TYPE_P (TREE_TYPE (to)))
5275 value = convert_memory_address_addr_space
5276 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5277 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5279 emit_move_insn (to_rtx, value);
5282 /* Store bounds if required. */
5283 if (bounds
5284 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5286 gcc_assert (MEM_P (to_rtx));
5287 chkp_emit_bounds_store (bounds, value, to_rtx);
5290 preserve_temp_slots (to_rtx);
5291 pop_temp_slots ();
5292 return;
5295 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5296 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5298 /* Don't move directly into a return register. */
5299 if (TREE_CODE (to) == RESULT_DECL
5300 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5302 rtx temp;
5304 push_temp_slots ();
5306 /* If the source is itself a return value, it still is in a pseudo at
5307 this point so we can move it back to the return register directly. */
5308 if (REG_P (to_rtx)
5309 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5310 && TREE_CODE (from) != CALL_EXPR)
5311 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5312 else
5313 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5315 /* Handle calls that return values in multiple non-contiguous locations.
5316 The Irix 6 ABI has examples of this. */
5317 if (GET_CODE (to_rtx) == PARALLEL)
5319 if (GET_CODE (temp) == PARALLEL)
5320 emit_group_move (to_rtx, temp);
5321 else
5322 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5323 int_size_in_bytes (TREE_TYPE (from)));
5325 else if (temp)
5326 emit_move_insn (to_rtx, temp);
5328 preserve_temp_slots (to_rtx);
5329 pop_temp_slots ();
5330 return;
5333 /* In case we are returning the contents of an object which overlaps
5334 the place the value is being stored, use a safe function when copying
5335 a value through a pointer into a structure value return block. */
5336 if (TREE_CODE (to) == RESULT_DECL
5337 && TREE_CODE (from) == INDIRECT_REF
5338 && ADDR_SPACE_GENERIC_P
5339 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5340 && refs_may_alias_p (to, from)
5341 && cfun->returns_struct
5342 && !cfun->returns_pcc_struct)
5344 rtx from_rtx, size;
5346 push_temp_slots ();
5347 size = expr_size (from);
5348 from_rtx = expand_normal (from);
5350 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5352 preserve_temp_slots (to_rtx);
5353 pop_temp_slots ();
5354 return;
5357 /* Compute FROM and store the value in the rtx we got. */
5359 push_temp_slots ();
5360 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5361 preserve_temp_slots (result);
5362 pop_temp_slots ();
5363 return;
5366 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5367 succeeded, false otherwise. */
5369 bool
5370 emit_storent_insn (rtx to, rtx from)
5372 struct expand_operand ops[2];
5373 machine_mode mode = GET_MODE (to);
5374 enum insn_code code = optab_handler (storent_optab, mode);
5376 if (code == CODE_FOR_nothing)
5377 return false;
5379 create_fixed_operand (&ops[0], to);
5380 create_input_operand (&ops[1], from, mode);
5381 return maybe_expand_insn (code, 2, ops);
5384 /* Generate code for computing expression EXP,
5385 and storing the value into TARGET.
5387 If the mode is BLKmode then we may return TARGET itself.
5388 It turns out that in BLKmode it doesn't cause a problem.
5389 because C has no operators that could combine two different
5390 assignments into the same BLKmode object with different values
5391 with no sequence point. Will other languages need this to
5392 be more thorough?
5394 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5395 stack, and block moves may need to be treated specially.
5397 If NONTEMPORAL is true, try using a nontemporal store instruction.
5399 If REVERSE is true, the store is to be done in reverse order.
5401 If BTARGET is not NULL then computed bounds of EXP are
5402 associated with BTARGET. */
5405 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5406 bool nontemporal, bool reverse, tree btarget)
5408 rtx temp;
5409 rtx alt_rtl = NULL_RTX;
5410 location_t loc = curr_insn_location ();
5412 if (VOID_TYPE_P (TREE_TYPE (exp)))
5414 /* C++ can generate ?: expressions with a throw expression in one
5415 branch and an rvalue in the other. Here, we resolve attempts to
5416 store the throw expression's nonexistent result. */
5417 gcc_assert (!call_param_p);
5418 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5419 return NULL_RTX;
5421 if (TREE_CODE (exp) == COMPOUND_EXPR)
5423 /* Perform first part of compound expression, then assign from second
5424 part. */
5425 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5426 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5427 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5428 call_param_p, nontemporal, reverse,
5429 btarget);
5431 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5433 /* For conditional expression, get safe form of the target. Then
5434 test the condition, doing the appropriate assignment on either
5435 side. This avoids the creation of unnecessary temporaries.
5436 For non-BLKmode, it is more efficient not to do this. */
5438 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5440 do_pending_stack_adjust ();
5441 NO_DEFER_POP;
5442 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5443 profile_probability::uninitialized ());
5444 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5445 nontemporal, reverse, btarget);
5446 emit_jump_insn (targetm.gen_jump (lab2));
5447 emit_barrier ();
5448 emit_label (lab1);
5449 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5450 nontemporal, reverse, btarget);
5451 emit_label (lab2);
5452 OK_DEFER_POP;
5454 return NULL_RTX;
5456 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5457 /* If this is a scalar in a register that is stored in a wider mode
5458 than the declared mode, compute the result into its declared mode
5459 and then convert to the wider mode. Our value is the computed
5460 expression. */
5462 rtx inner_target = 0;
5463 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5464 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5466 /* We can do the conversion inside EXP, which will often result
5467 in some optimizations. Do the conversion in two steps: first
5468 change the signedness, if needed, then the extend. But don't
5469 do this if the type of EXP is a subtype of something else
5470 since then the conversion might involve more than just
5471 converting modes. */
5472 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5473 && TREE_TYPE (TREE_TYPE (exp)) == 0
5474 && GET_MODE_PRECISION (outer_mode)
5475 == TYPE_PRECISION (TREE_TYPE (exp)))
5477 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5478 TYPE_UNSIGNED (TREE_TYPE (exp))))
5480 /* Some types, e.g. Fortran's logical*4, won't have a signed
5481 version, so use the mode instead. */
5482 tree ntype
5483 = (signed_or_unsigned_type_for
5484 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5485 if (ntype == NULL)
5486 ntype = lang_hooks.types.type_for_mode
5487 (TYPE_MODE (TREE_TYPE (exp)),
5488 SUBREG_PROMOTED_SIGN (target));
5490 exp = fold_convert_loc (loc, ntype, exp);
5493 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5494 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5495 exp);
5497 inner_target = SUBREG_REG (target);
5500 temp = expand_expr (exp, inner_target, VOIDmode,
5501 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5503 /* Handle bounds returned by call. */
5504 if (TREE_CODE (exp) == CALL_EXPR)
5506 rtx bounds;
5507 chkp_split_slot (temp, &temp, &bounds);
5508 if (bounds && btarget)
5510 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5511 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5512 chkp_set_rtl_bounds (btarget, tmp);
5516 /* If TEMP is a VOIDmode constant, use convert_modes to make
5517 sure that we properly convert it. */
5518 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5520 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5521 temp, SUBREG_PROMOTED_SIGN (target));
5522 temp = convert_modes (inner_mode, outer_mode, temp,
5523 SUBREG_PROMOTED_SIGN (target));
5526 convert_move (SUBREG_REG (target), temp,
5527 SUBREG_PROMOTED_SIGN (target));
5529 return NULL_RTX;
5531 else if ((TREE_CODE (exp) == STRING_CST
5532 || (TREE_CODE (exp) == MEM_REF
5533 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5534 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5535 == STRING_CST
5536 && integer_zerop (TREE_OPERAND (exp, 1))))
5537 && !nontemporal && !call_param_p
5538 && MEM_P (target))
5540 /* Optimize initialization of an array with a STRING_CST. */
5541 HOST_WIDE_INT exp_len, str_copy_len;
5542 rtx dest_mem;
5543 tree str = TREE_CODE (exp) == STRING_CST
5544 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5546 exp_len = int_expr_size (exp);
5547 if (exp_len <= 0)
5548 goto normal_expr;
5550 if (TREE_STRING_LENGTH (str) <= 0)
5551 goto normal_expr;
5553 str_copy_len = strlen (TREE_STRING_POINTER (str));
5554 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5555 goto normal_expr;
5557 str_copy_len = TREE_STRING_LENGTH (str);
5558 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5559 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5561 str_copy_len += STORE_MAX_PIECES - 1;
5562 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5564 str_copy_len = MIN (str_copy_len, exp_len);
5565 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5566 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5567 MEM_ALIGN (target), false))
5568 goto normal_expr;
5570 dest_mem = target;
5572 dest_mem = store_by_pieces (dest_mem,
5573 str_copy_len, builtin_strncpy_read_str,
5574 CONST_CAST (char *,
5575 TREE_STRING_POINTER (str)),
5576 MEM_ALIGN (target), false,
5577 exp_len > str_copy_len ? 1 : 0);
5578 if (exp_len > str_copy_len)
5579 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5580 GEN_INT (exp_len - str_copy_len),
5581 BLOCK_OP_NORMAL);
5582 return NULL_RTX;
5584 else
5586 rtx tmp_target;
5588 normal_expr:
5589 /* If we want to use a nontemporal or a reverse order store, force the
5590 value into a register first. */
5591 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5592 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5593 (call_param_p
5594 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5595 &alt_rtl, false);
5597 /* Handle bounds returned by call. */
5598 if (TREE_CODE (exp) == CALL_EXPR)
5600 rtx bounds;
5601 chkp_split_slot (temp, &temp, &bounds);
5602 if (bounds && btarget)
5604 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5605 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5606 chkp_set_rtl_bounds (btarget, tmp);
5611 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5612 the same as that of TARGET, adjust the constant. This is needed, for
5613 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5614 only a word-sized value. */
5615 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5616 && TREE_CODE (exp) != ERROR_MARK
5617 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5618 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5619 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5621 /* If value was not generated in the target, store it there.
5622 Convert the value to TARGET's type first if necessary and emit the
5623 pending incrementations that have been queued when expanding EXP.
5624 Note that we cannot emit the whole queue blindly because this will
5625 effectively disable the POST_INC optimization later.
5627 If TEMP and TARGET compare equal according to rtx_equal_p, but
5628 one or both of them are volatile memory refs, we have to distinguish
5629 two cases:
5630 - expand_expr has used TARGET. In this case, we must not generate
5631 another copy. This can be detected by TARGET being equal according
5632 to == .
5633 - expand_expr has not used TARGET - that means that the source just
5634 happens to have the same RTX form. Since temp will have been created
5635 by expand_expr, it will compare unequal according to == .
5636 We must generate a copy in this case, to reach the correct number
5637 of volatile memory references. */
5639 if ((! rtx_equal_p (temp, target)
5640 || (temp != target && (side_effects_p (temp)
5641 || side_effects_p (target))))
5642 && TREE_CODE (exp) != ERROR_MARK
5643 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5644 but TARGET is not valid memory reference, TEMP will differ
5645 from TARGET although it is really the same location. */
5646 && !(alt_rtl
5647 && rtx_equal_p (alt_rtl, target)
5648 && !side_effects_p (alt_rtl)
5649 && !side_effects_p (target))
5650 /* If there's nothing to copy, don't bother. Don't call
5651 expr_size unless necessary, because some front-ends (C++)
5652 expr_size-hook must not be given objects that are not
5653 supposed to be bit-copied or bit-initialized. */
5654 && expr_size (exp) != const0_rtx)
5656 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5658 if (GET_MODE (target) == BLKmode)
5660 /* Handle calls that return BLKmode values in registers. */
5661 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5662 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5663 else
5664 store_bit_field (target,
5665 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5666 0, 0, 0, GET_MODE (temp), temp, reverse);
5668 else
5669 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5672 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5674 /* Handle copying a string constant into an array. The string
5675 constant may be shorter than the array. So copy just the string's
5676 actual length, and clear the rest. First get the size of the data
5677 type of the string, which is actually the size of the target. */
5678 rtx size = expr_size (exp);
5680 if (CONST_INT_P (size)
5681 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5682 emit_block_move (target, temp, size,
5683 (call_param_p
5684 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5685 else
5687 machine_mode pointer_mode
5688 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5689 machine_mode address_mode = get_address_mode (target);
5691 /* Compute the size of the data to copy from the string. */
5692 tree copy_size
5693 = size_binop_loc (loc, MIN_EXPR,
5694 make_tree (sizetype, size),
5695 size_int (TREE_STRING_LENGTH (exp)));
5696 rtx copy_size_rtx
5697 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5698 (call_param_p
5699 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5700 rtx_code_label *label = 0;
5702 /* Copy that much. */
5703 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5704 TYPE_UNSIGNED (sizetype));
5705 emit_block_move (target, temp, copy_size_rtx,
5706 (call_param_p
5707 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5709 /* Figure out how much is left in TARGET that we have to clear.
5710 Do all calculations in pointer_mode. */
5711 if (CONST_INT_P (copy_size_rtx))
5713 size = plus_constant (address_mode, size,
5714 -INTVAL (copy_size_rtx));
5715 target = adjust_address (target, BLKmode,
5716 INTVAL (copy_size_rtx));
5718 else
5720 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5721 copy_size_rtx, NULL_RTX, 0,
5722 OPTAB_LIB_WIDEN);
5724 if (GET_MODE (copy_size_rtx) != address_mode)
5725 copy_size_rtx = convert_to_mode (address_mode,
5726 copy_size_rtx,
5727 TYPE_UNSIGNED (sizetype));
5729 target = offset_address (target, copy_size_rtx,
5730 highest_pow2_factor (copy_size));
5731 label = gen_label_rtx ();
5732 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5733 GET_MODE (size), 0, label);
5736 if (size != const0_rtx)
5737 clear_storage (target, size, BLOCK_OP_NORMAL);
5739 if (label)
5740 emit_label (label);
5743 /* Handle calls that return values in multiple non-contiguous locations.
5744 The Irix 6 ABI has examples of this. */
5745 else if (GET_CODE (target) == PARALLEL)
5747 if (GET_CODE (temp) == PARALLEL)
5748 emit_group_move (target, temp);
5749 else
5750 emit_group_load (target, temp, TREE_TYPE (exp),
5751 int_size_in_bytes (TREE_TYPE (exp)));
5753 else if (GET_CODE (temp) == PARALLEL)
5754 emit_group_store (target, temp, TREE_TYPE (exp),
5755 int_size_in_bytes (TREE_TYPE (exp)));
5756 else if (GET_MODE (temp) == BLKmode)
5757 emit_block_move (target, temp, expr_size (exp),
5758 (call_param_p
5759 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5760 /* If we emit a nontemporal store, there is nothing else to do. */
5761 else if (nontemporal && emit_storent_insn (target, temp))
5763 else
5765 if (reverse)
5766 temp = flip_storage_order (GET_MODE (target), temp);
5767 temp = force_operand (temp, target);
5768 if (temp != target)
5769 emit_move_insn (target, temp);
5773 return NULL_RTX;
5776 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5778 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5779 bool reverse)
5781 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5782 reverse, NULL);
5785 /* Return true if field F of structure TYPE is a flexible array. */
5787 static bool
5788 flexible_array_member_p (const_tree f, const_tree type)
5790 const_tree tf;
5792 tf = TREE_TYPE (f);
5793 return (DECL_CHAIN (f) == NULL
5794 && TREE_CODE (tf) == ARRAY_TYPE
5795 && TYPE_DOMAIN (tf)
5796 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5797 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5798 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5799 && int_size_in_bytes (type) >= 0);
5802 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5803 must have in order for it to completely initialize a value of type TYPE.
5804 Return -1 if the number isn't known.
5806 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5808 static HOST_WIDE_INT
5809 count_type_elements (const_tree type, bool for_ctor_p)
5811 switch (TREE_CODE (type))
5813 case ARRAY_TYPE:
5815 tree nelts;
5817 nelts = array_type_nelts (type);
5818 if (nelts && tree_fits_uhwi_p (nelts))
5820 unsigned HOST_WIDE_INT n;
5822 n = tree_to_uhwi (nelts) + 1;
5823 if (n == 0 || for_ctor_p)
5824 return n;
5825 else
5826 return n * count_type_elements (TREE_TYPE (type), false);
5828 return for_ctor_p ? -1 : 1;
5831 case RECORD_TYPE:
5833 unsigned HOST_WIDE_INT n;
5834 tree f;
5836 n = 0;
5837 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5838 if (TREE_CODE (f) == FIELD_DECL)
5840 if (!for_ctor_p)
5841 n += count_type_elements (TREE_TYPE (f), false);
5842 else if (!flexible_array_member_p (f, type))
5843 /* Don't count flexible arrays, which are not supposed
5844 to be initialized. */
5845 n += 1;
5848 return n;
5851 case UNION_TYPE:
5852 case QUAL_UNION_TYPE:
5854 tree f;
5855 HOST_WIDE_INT n, m;
5857 gcc_assert (!for_ctor_p);
5858 /* Estimate the number of scalars in each field and pick the
5859 maximum. Other estimates would do instead; the idea is simply
5860 to make sure that the estimate is not sensitive to the ordering
5861 of the fields. */
5862 n = 1;
5863 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5864 if (TREE_CODE (f) == FIELD_DECL)
5866 m = count_type_elements (TREE_TYPE (f), false);
5867 /* If the field doesn't span the whole union, add an extra
5868 scalar for the rest. */
5869 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5870 TYPE_SIZE (type)) != 1)
5871 m++;
5872 if (n < m)
5873 n = m;
5875 return n;
5878 case COMPLEX_TYPE:
5879 return 2;
5881 case VECTOR_TYPE:
5882 return TYPE_VECTOR_SUBPARTS (type);
5884 case INTEGER_TYPE:
5885 case REAL_TYPE:
5886 case FIXED_POINT_TYPE:
5887 case ENUMERAL_TYPE:
5888 case BOOLEAN_TYPE:
5889 case POINTER_TYPE:
5890 case OFFSET_TYPE:
5891 case REFERENCE_TYPE:
5892 case NULLPTR_TYPE:
5893 return 1;
5895 case ERROR_MARK:
5896 return 0;
5898 case VOID_TYPE:
5899 case METHOD_TYPE:
5900 case FUNCTION_TYPE:
5901 case LANG_TYPE:
5902 default:
5903 gcc_unreachable ();
5907 /* Helper for categorize_ctor_elements. Identical interface. */
5909 static bool
5910 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5911 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5913 unsigned HOST_WIDE_INT idx;
5914 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5915 tree value, purpose, elt_type;
5917 /* Whether CTOR is a valid constant initializer, in accordance with what
5918 initializer_constant_valid_p does. If inferred from the constructor
5919 elements, true until proven otherwise. */
5920 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5921 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5923 nz_elts = 0;
5924 init_elts = 0;
5925 num_fields = 0;
5926 elt_type = NULL_TREE;
5928 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5930 HOST_WIDE_INT mult = 1;
5932 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5934 tree lo_index = TREE_OPERAND (purpose, 0);
5935 tree hi_index = TREE_OPERAND (purpose, 1);
5937 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5938 mult = (tree_to_uhwi (hi_index)
5939 - tree_to_uhwi (lo_index) + 1);
5941 num_fields += mult;
5942 elt_type = TREE_TYPE (value);
5944 switch (TREE_CODE (value))
5946 case CONSTRUCTOR:
5948 HOST_WIDE_INT nz = 0, ic = 0;
5950 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5951 p_complete);
5953 nz_elts += mult * nz;
5954 init_elts += mult * ic;
5956 if (const_from_elts_p && const_p)
5957 const_p = const_elt_p;
5959 break;
5961 case INTEGER_CST:
5962 case REAL_CST:
5963 case FIXED_CST:
5964 if (!initializer_zerop (value))
5965 nz_elts += mult;
5966 init_elts += mult;
5967 break;
5969 case STRING_CST:
5970 nz_elts += mult * TREE_STRING_LENGTH (value);
5971 init_elts += mult * TREE_STRING_LENGTH (value);
5972 break;
5974 case COMPLEX_CST:
5975 if (!initializer_zerop (TREE_REALPART (value)))
5976 nz_elts += mult;
5977 if (!initializer_zerop (TREE_IMAGPART (value)))
5978 nz_elts += mult;
5979 init_elts += mult;
5980 break;
5982 case VECTOR_CST:
5984 unsigned i;
5985 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5987 tree v = VECTOR_CST_ELT (value, i);
5988 if (!initializer_zerop (v))
5989 nz_elts += mult;
5990 init_elts += mult;
5993 break;
5995 default:
5997 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5998 nz_elts += mult * tc;
5999 init_elts += mult * tc;
6001 if (const_from_elts_p && const_p)
6002 const_p
6003 = initializer_constant_valid_p (value,
6004 elt_type,
6005 TYPE_REVERSE_STORAGE_ORDER
6006 (TREE_TYPE (ctor)))
6007 != NULL_TREE;
6009 break;
6013 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6014 num_fields, elt_type))
6015 *p_complete = false;
6017 *p_nz_elts += nz_elts;
6018 *p_init_elts += init_elts;
6020 return const_p;
6023 /* Examine CTOR to discover:
6024 * how many scalar fields are set to nonzero values,
6025 and place it in *P_NZ_ELTS;
6026 * how many scalar fields in total are in CTOR,
6027 and place it in *P_ELT_COUNT.
6028 * whether the constructor is complete -- in the sense that every
6029 meaningful byte is explicitly given a value --
6030 and place it in *P_COMPLETE.
6032 Return whether or not CTOR is a valid static constant initializer, the same
6033 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6035 bool
6036 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6037 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6039 *p_nz_elts = 0;
6040 *p_init_elts = 0;
6041 *p_complete = true;
6043 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6046 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6047 of which had type LAST_TYPE. Each element was itself a complete
6048 initializer, in the sense that every meaningful byte was explicitly
6049 given a value. Return true if the same is true for the constructor
6050 as a whole. */
6052 bool
6053 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6054 const_tree last_type)
6056 if (TREE_CODE (type) == UNION_TYPE
6057 || TREE_CODE (type) == QUAL_UNION_TYPE)
6059 if (num_elts == 0)
6060 return false;
6062 gcc_assert (num_elts == 1 && last_type);
6064 /* ??? We could look at each element of the union, and find the
6065 largest element. Which would avoid comparing the size of the
6066 initialized element against any tail padding in the union.
6067 Doesn't seem worth the effort... */
6068 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6071 return count_type_elements (type, true) == num_elts;
6074 /* Return 1 if EXP contains mostly (3/4) zeros. */
6076 static int
6077 mostly_zeros_p (const_tree exp)
6079 if (TREE_CODE (exp) == CONSTRUCTOR)
6081 HOST_WIDE_INT nz_elts, init_elts;
6082 bool complete_p;
6084 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6085 return !complete_p || nz_elts < init_elts / 4;
6088 return initializer_zerop (exp);
6091 /* Return 1 if EXP contains all zeros. */
6093 static int
6094 all_zeros_p (const_tree exp)
6096 if (TREE_CODE (exp) == CONSTRUCTOR)
6098 HOST_WIDE_INT nz_elts, init_elts;
6099 bool complete_p;
6101 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6102 return nz_elts == 0;
6105 return initializer_zerop (exp);
6108 /* Helper function for store_constructor.
6109 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6110 CLEARED is as for store_constructor.
6111 ALIAS_SET is the alias set to use for any stores.
6112 If REVERSE is true, the store is to be done in reverse order.
6114 This provides a recursive shortcut back to store_constructor when it isn't
6115 necessary to go through store_field. This is so that we can pass through
6116 the cleared field to let store_constructor know that we may not have to
6117 clear a substructure if the outer structure has already been cleared. */
6119 static void
6120 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6121 poly_uint64 bitregion_start,
6122 poly_uint64 bitregion_end,
6123 machine_mode mode,
6124 tree exp, int cleared,
6125 alias_set_type alias_set, bool reverse)
6127 poly_int64 bytepos;
6128 poly_uint64 bytesize;
6129 if (TREE_CODE (exp) == CONSTRUCTOR
6130 /* We can only call store_constructor recursively if the size and
6131 bit position are on a byte boundary. */
6132 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6133 && maybe_ne (bitsize, 0U)
6134 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6135 /* If we have a nonzero bitpos for a register target, then we just
6136 let store_field do the bitfield handling. This is unlikely to
6137 generate unnecessary clear instructions anyways. */
6138 && (known_eq (bitpos, 0) || MEM_P (target)))
6140 if (MEM_P (target))
6142 machine_mode target_mode = GET_MODE (target);
6143 if (target_mode != BLKmode
6144 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6145 target_mode = BLKmode;
6146 target = adjust_address (target, target_mode, bytepos);
6150 /* Update the alias set, if required. */
6151 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6152 && MEM_ALIAS_SET (target) != 0)
6154 target = copy_rtx (target);
6155 set_mem_alias_set (target, alias_set);
6158 store_constructor (exp, target, cleared, bytesize, reverse);
6160 else
6161 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6162 exp, alias_set, false, reverse);
6166 /* Returns the number of FIELD_DECLs in TYPE. */
6168 static int
6169 fields_length (const_tree type)
6171 tree t = TYPE_FIELDS (type);
6172 int count = 0;
6174 for (; t; t = DECL_CHAIN (t))
6175 if (TREE_CODE (t) == FIELD_DECL)
6176 ++count;
6178 return count;
6182 /* Store the value of constructor EXP into the rtx TARGET.
6183 TARGET is either a REG or a MEM; we know it cannot conflict, since
6184 safe_from_p has been called.
6185 CLEARED is true if TARGET is known to have been zero'd.
6186 SIZE is the number of bytes of TARGET we are allowed to modify: this
6187 may not be the same as the size of EXP if we are assigning to a field
6188 which has been packed to exclude padding bits.
6189 If REVERSE is true, the store is to be done in reverse order. */
6191 static void
6192 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6193 bool reverse)
6195 tree type = TREE_TYPE (exp);
6196 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6197 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6199 switch (TREE_CODE (type))
6201 case RECORD_TYPE:
6202 case UNION_TYPE:
6203 case QUAL_UNION_TYPE:
6205 unsigned HOST_WIDE_INT idx;
6206 tree field, value;
6208 /* The storage order is specified for every aggregate type. */
6209 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6211 /* If size is zero or the target is already cleared, do nothing. */
6212 if (known_eq (size, 0) || cleared)
6213 cleared = 1;
6214 /* We either clear the aggregate or indicate the value is dead. */
6215 else if ((TREE_CODE (type) == UNION_TYPE
6216 || TREE_CODE (type) == QUAL_UNION_TYPE)
6217 && ! CONSTRUCTOR_ELTS (exp))
6218 /* If the constructor is empty, clear the union. */
6220 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6221 cleared = 1;
6224 /* If we are building a static constructor into a register,
6225 set the initial value as zero so we can fold the value into
6226 a constant. But if more than one register is involved,
6227 this probably loses. */
6228 else if (REG_P (target) && TREE_STATIC (exp)
6229 && (GET_MODE_SIZE (GET_MODE (target))
6230 <= REGMODE_NATURAL_SIZE (GET_MODE (target))))
6232 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6233 cleared = 1;
6236 /* If the constructor has fewer fields than the structure or
6237 if we are initializing the structure to mostly zeros, clear
6238 the whole structure first. Don't do this if TARGET is a
6239 register whose mode size isn't equal to SIZE since
6240 clear_storage can't handle this case. */
6241 else if (known_size_p (size)
6242 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6243 || mostly_zeros_p (exp))
6244 && (!REG_P (target)
6245 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6247 clear_storage (target, gen_int_mode (size, Pmode),
6248 BLOCK_OP_NORMAL);
6249 cleared = 1;
6252 if (REG_P (target) && !cleared)
6253 emit_clobber (target);
6255 /* Store each element of the constructor into the
6256 corresponding field of TARGET. */
6257 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6259 machine_mode mode;
6260 HOST_WIDE_INT bitsize;
6261 HOST_WIDE_INT bitpos = 0;
6262 tree offset;
6263 rtx to_rtx = target;
6265 /* Just ignore missing fields. We cleared the whole
6266 structure, above, if any fields are missing. */
6267 if (field == 0)
6268 continue;
6270 if (cleared && initializer_zerop (value))
6271 continue;
6273 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6274 bitsize = tree_to_uhwi (DECL_SIZE (field));
6275 else
6276 gcc_unreachable ();
6278 mode = DECL_MODE (field);
6279 if (DECL_BIT_FIELD (field))
6280 mode = VOIDmode;
6282 offset = DECL_FIELD_OFFSET (field);
6283 if (tree_fits_shwi_p (offset)
6284 && tree_fits_shwi_p (bit_position (field)))
6286 bitpos = int_bit_position (field);
6287 offset = NULL_TREE;
6289 else
6290 gcc_unreachable ();
6292 /* If this initializes a field that is smaller than a
6293 word, at the start of a word, try to widen it to a full
6294 word. This special case allows us to output C++ member
6295 function initializations in a form that the optimizers
6296 can understand. */
6297 if (WORD_REGISTER_OPERATIONS
6298 && REG_P (target)
6299 && bitsize < BITS_PER_WORD
6300 && bitpos % BITS_PER_WORD == 0
6301 && GET_MODE_CLASS (mode) == MODE_INT
6302 && TREE_CODE (value) == INTEGER_CST
6303 && exp_size >= 0
6304 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6306 tree type = TREE_TYPE (value);
6308 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6310 type = lang_hooks.types.type_for_mode
6311 (word_mode, TYPE_UNSIGNED (type));
6312 value = fold_convert (type, value);
6313 /* Make sure the bits beyond the original bitsize are zero
6314 so that we can correctly avoid extra zeroing stores in
6315 later constructor elements. */
6316 tree bitsize_mask
6317 = wide_int_to_tree (type, wi::mask (bitsize, false,
6318 BITS_PER_WORD));
6319 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6322 if (BYTES_BIG_ENDIAN)
6323 value
6324 = fold_build2 (LSHIFT_EXPR, type, value,
6325 build_int_cst (type,
6326 BITS_PER_WORD - bitsize));
6327 bitsize = BITS_PER_WORD;
6328 mode = word_mode;
6331 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6332 && DECL_NONADDRESSABLE_P (field))
6334 to_rtx = copy_rtx (to_rtx);
6335 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6338 store_constructor_field (to_rtx, bitsize, bitpos,
6339 0, bitregion_end, mode,
6340 value, cleared,
6341 get_alias_set (TREE_TYPE (field)),
6342 reverse);
6344 break;
6346 case ARRAY_TYPE:
6348 tree value, index;
6349 unsigned HOST_WIDE_INT i;
6350 int need_to_clear;
6351 tree domain;
6352 tree elttype = TREE_TYPE (type);
6353 int const_bounds_p;
6354 HOST_WIDE_INT minelt = 0;
6355 HOST_WIDE_INT maxelt = 0;
6357 /* The storage order is specified for every aggregate type. */
6358 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6360 domain = TYPE_DOMAIN (type);
6361 const_bounds_p = (TYPE_MIN_VALUE (domain)
6362 && TYPE_MAX_VALUE (domain)
6363 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6364 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6366 /* If we have constant bounds for the range of the type, get them. */
6367 if (const_bounds_p)
6369 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6370 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6373 /* If the constructor has fewer elements than the array, clear
6374 the whole array first. Similarly if this is static
6375 constructor of a non-BLKmode object. */
6376 if (cleared)
6377 need_to_clear = 0;
6378 else if (REG_P (target) && TREE_STATIC (exp))
6379 need_to_clear = 1;
6380 else
6382 unsigned HOST_WIDE_INT idx;
6383 tree index, value;
6384 HOST_WIDE_INT count = 0, zero_count = 0;
6385 need_to_clear = ! const_bounds_p;
6387 /* This loop is a more accurate version of the loop in
6388 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6389 is also needed to check for missing elements. */
6390 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6392 HOST_WIDE_INT this_node_count;
6394 if (need_to_clear)
6395 break;
6397 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6399 tree lo_index = TREE_OPERAND (index, 0);
6400 tree hi_index = TREE_OPERAND (index, 1);
6402 if (! tree_fits_uhwi_p (lo_index)
6403 || ! tree_fits_uhwi_p (hi_index))
6405 need_to_clear = 1;
6406 break;
6409 this_node_count = (tree_to_uhwi (hi_index)
6410 - tree_to_uhwi (lo_index) + 1);
6412 else
6413 this_node_count = 1;
6415 count += this_node_count;
6416 if (mostly_zeros_p (value))
6417 zero_count += this_node_count;
6420 /* Clear the entire array first if there are any missing
6421 elements, or if the incidence of zero elements is >=
6422 75%. */
6423 if (! need_to_clear
6424 && (count < maxelt - minelt + 1
6425 || 4 * zero_count >= 3 * count))
6426 need_to_clear = 1;
6429 if (need_to_clear && maybe_gt (size, 0))
6431 if (REG_P (target))
6432 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6433 else
6434 clear_storage (target, gen_int_mode (size, Pmode),
6435 BLOCK_OP_NORMAL);
6436 cleared = 1;
6439 if (!cleared && REG_P (target))
6440 /* Inform later passes that the old value is dead. */
6441 emit_clobber (target);
6443 /* Store each element of the constructor into the
6444 corresponding element of TARGET, determined by counting the
6445 elements. */
6446 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6448 machine_mode mode;
6449 poly_int64 bitsize;
6450 HOST_WIDE_INT bitpos;
6451 rtx xtarget = target;
6453 if (cleared && initializer_zerop (value))
6454 continue;
6456 mode = TYPE_MODE (elttype);
6457 if (mode == BLKmode)
6458 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6459 ? tree_to_uhwi (TYPE_SIZE (elttype))
6460 : -1);
6461 else
6462 bitsize = GET_MODE_BITSIZE (mode);
6464 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6466 tree lo_index = TREE_OPERAND (index, 0);
6467 tree hi_index = TREE_OPERAND (index, 1);
6468 rtx index_r, pos_rtx;
6469 HOST_WIDE_INT lo, hi, count;
6470 tree position;
6472 /* If the range is constant and "small", unroll the loop. */
6473 if (const_bounds_p
6474 && tree_fits_shwi_p (lo_index)
6475 && tree_fits_shwi_p (hi_index)
6476 && (lo = tree_to_shwi (lo_index),
6477 hi = tree_to_shwi (hi_index),
6478 count = hi - lo + 1,
6479 (!MEM_P (target)
6480 || count <= 2
6481 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6482 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6483 <= 40 * 8)))))
6485 lo -= minelt; hi -= minelt;
6486 for (; lo <= hi; lo++)
6488 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6490 if (MEM_P (target)
6491 && !MEM_KEEP_ALIAS_SET_P (target)
6492 && TREE_CODE (type) == ARRAY_TYPE
6493 && TYPE_NONALIASED_COMPONENT (type))
6495 target = copy_rtx (target);
6496 MEM_KEEP_ALIAS_SET_P (target) = 1;
6499 store_constructor_field
6500 (target, bitsize, bitpos, 0, bitregion_end,
6501 mode, value, cleared,
6502 get_alias_set (elttype), reverse);
6505 else
6507 rtx_code_label *loop_start = gen_label_rtx ();
6508 rtx_code_label *loop_end = gen_label_rtx ();
6509 tree exit_cond;
6511 expand_normal (hi_index);
6513 index = build_decl (EXPR_LOCATION (exp),
6514 VAR_DECL, NULL_TREE, domain);
6515 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6516 SET_DECL_RTL (index, index_r);
6517 store_expr (lo_index, index_r, 0, false, reverse);
6519 /* Build the head of the loop. */
6520 do_pending_stack_adjust ();
6521 emit_label (loop_start);
6523 /* Assign value to element index. */
6524 position =
6525 fold_convert (ssizetype,
6526 fold_build2 (MINUS_EXPR,
6527 TREE_TYPE (index),
6528 index,
6529 TYPE_MIN_VALUE (domain)));
6531 position =
6532 size_binop (MULT_EXPR, position,
6533 fold_convert (ssizetype,
6534 TYPE_SIZE_UNIT (elttype)));
6536 pos_rtx = expand_normal (position);
6537 xtarget = offset_address (target, pos_rtx,
6538 highest_pow2_factor (position));
6539 xtarget = adjust_address (xtarget, mode, 0);
6540 if (TREE_CODE (value) == CONSTRUCTOR)
6541 store_constructor (value, xtarget, cleared,
6542 exact_div (bitsize, BITS_PER_UNIT),
6543 reverse);
6544 else
6545 store_expr (value, xtarget, 0, false, reverse);
6547 /* Generate a conditional jump to exit the loop. */
6548 exit_cond = build2 (LT_EXPR, integer_type_node,
6549 index, hi_index);
6550 jumpif (exit_cond, loop_end,
6551 profile_probability::uninitialized ());
6553 /* Update the loop counter, and jump to the head of
6554 the loop. */
6555 expand_assignment (index,
6556 build2 (PLUS_EXPR, TREE_TYPE (index),
6557 index, integer_one_node),
6558 false);
6560 emit_jump (loop_start);
6562 /* Build the end of the loop. */
6563 emit_label (loop_end);
6566 else if ((index != 0 && ! tree_fits_shwi_p (index))
6567 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6569 tree position;
6571 if (index == 0)
6572 index = ssize_int (1);
6574 if (minelt)
6575 index = fold_convert (ssizetype,
6576 fold_build2 (MINUS_EXPR,
6577 TREE_TYPE (index),
6578 index,
6579 TYPE_MIN_VALUE (domain)));
6581 position =
6582 size_binop (MULT_EXPR, index,
6583 fold_convert (ssizetype,
6584 TYPE_SIZE_UNIT (elttype)));
6585 xtarget = offset_address (target,
6586 expand_normal (position),
6587 highest_pow2_factor (position));
6588 xtarget = adjust_address (xtarget, mode, 0);
6589 store_expr (value, xtarget, 0, false, reverse);
6591 else
6593 if (index != 0)
6594 bitpos = ((tree_to_shwi (index) - minelt)
6595 * tree_to_uhwi (TYPE_SIZE (elttype)));
6596 else
6597 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6599 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6600 && TREE_CODE (type) == ARRAY_TYPE
6601 && TYPE_NONALIASED_COMPONENT (type))
6603 target = copy_rtx (target);
6604 MEM_KEEP_ALIAS_SET_P (target) = 1;
6606 store_constructor_field (target, bitsize, bitpos, 0,
6607 bitregion_end, mode, value,
6608 cleared, get_alias_set (elttype),
6609 reverse);
6612 break;
6615 case VECTOR_TYPE:
6617 unsigned HOST_WIDE_INT idx;
6618 constructor_elt *ce;
6619 int i;
6620 int need_to_clear;
6621 insn_code icode = CODE_FOR_nothing;
6622 tree elt;
6623 tree elttype = TREE_TYPE (type);
6624 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6625 machine_mode eltmode = TYPE_MODE (elttype);
6626 HOST_WIDE_INT bitsize;
6627 HOST_WIDE_INT bitpos;
6628 rtvec vector = NULL;
6629 unsigned n_elts;
6630 alias_set_type alias;
6631 bool vec_vec_init_p = false;
6632 machine_mode mode = GET_MODE (target);
6634 gcc_assert (eltmode != BLKmode);
6636 /* Try using vec_duplicate_optab for uniform vectors. */
6637 if (!TREE_SIDE_EFFECTS (exp)
6638 && VECTOR_MODE_P (mode)
6639 && eltmode == GET_MODE_INNER (mode)
6640 && ((icode = optab_handler (vec_duplicate_optab, mode))
6641 != CODE_FOR_nothing)
6642 && (elt = uniform_vector_p (exp)))
6644 struct expand_operand ops[2];
6645 create_output_operand (&ops[0], target, mode);
6646 create_input_operand (&ops[1], expand_normal (elt), eltmode);
6647 expand_insn (icode, 2, ops);
6648 if (!rtx_equal_p (target, ops[0].value))
6649 emit_move_insn (target, ops[0].value);
6650 break;
6653 n_elts = TYPE_VECTOR_SUBPARTS (type);
6654 if (REG_P (target) && VECTOR_MODE_P (mode))
6656 machine_mode emode = eltmode;
6658 if (CONSTRUCTOR_NELTS (exp)
6659 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6660 == VECTOR_TYPE))
6662 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6663 gcc_assert (CONSTRUCTOR_NELTS (exp) * TYPE_VECTOR_SUBPARTS (etype)
6664 == n_elts);
6665 emode = TYPE_MODE (etype);
6667 icode = convert_optab_handler (vec_init_optab, mode, emode);
6668 if (icode != CODE_FOR_nothing)
6670 unsigned int i, n = n_elts;
6672 if (emode != eltmode)
6674 n = CONSTRUCTOR_NELTS (exp);
6675 vec_vec_init_p = true;
6677 vector = rtvec_alloc (n);
6678 for (i = 0; i < n; i++)
6679 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6683 /* If the constructor has fewer elements than the vector,
6684 clear the whole array first. Similarly if this is static
6685 constructor of a non-BLKmode object. */
6686 if (cleared)
6687 need_to_clear = 0;
6688 else if (REG_P (target) && TREE_STATIC (exp))
6689 need_to_clear = 1;
6690 else
6692 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6693 tree value;
6695 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6697 tree sz = TYPE_SIZE (TREE_TYPE (value));
6698 int n_elts_here
6699 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6700 TYPE_SIZE (elttype)));
6702 count += n_elts_here;
6703 if (mostly_zeros_p (value))
6704 zero_count += n_elts_here;
6707 /* Clear the entire vector first if there are any missing elements,
6708 or if the incidence of zero elements is >= 75%. */
6709 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6712 if (need_to_clear && maybe_gt (size, 0) && !vector)
6714 if (REG_P (target))
6715 emit_move_insn (target, CONST0_RTX (mode));
6716 else
6717 clear_storage (target, gen_int_mode (size, Pmode),
6718 BLOCK_OP_NORMAL);
6719 cleared = 1;
6722 /* Inform later passes that the old value is dead. */
6723 if (!cleared && !vector && REG_P (target))
6724 emit_move_insn (target, CONST0_RTX (mode));
6726 if (MEM_P (target))
6727 alias = MEM_ALIAS_SET (target);
6728 else
6729 alias = get_alias_set (elttype);
6731 /* Store each element of the constructor into the corresponding
6732 element of TARGET, determined by counting the elements. */
6733 for (idx = 0, i = 0;
6734 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6735 idx++, i += bitsize / elt_size)
6737 HOST_WIDE_INT eltpos;
6738 tree value = ce->value;
6740 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6741 if (cleared && initializer_zerop (value))
6742 continue;
6744 if (ce->index)
6745 eltpos = tree_to_uhwi (ce->index);
6746 else
6747 eltpos = i;
6749 if (vector)
6751 if (vec_vec_init_p)
6753 gcc_assert (ce->index == NULL_TREE);
6754 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6755 eltpos = idx;
6757 else
6758 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6759 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6761 else
6763 machine_mode value_mode
6764 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6765 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6766 bitpos = eltpos * elt_size;
6767 store_constructor_field (target, bitsize, bitpos, 0,
6768 bitregion_end, value_mode,
6769 value, cleared, alias, reverse);
6773 if (vector)
6774 emit_insn (GEN_FCN (icode) (target,
6775 gen_rtx_PARALLEL (mode, vector)));
6776 break;
6779 default:
6780 gcc_unreachable ();
6784 /* Store the value of EXP (an expression tree)
6785 into a subfield of TARGET which has mode MODE and occupies
6786 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6787 If MODE is VOIDmode, it means that we are storing into a bit-field.
6789 BITREGION_START is bitpos of the first bitfield in this region.
6790 BITREGION_END is the bitpos of the ending bitfield in this region.
6791 These two fields are 0, if the C++ memory model does not apply,
6792 or we are not interested in keeping track of bitfield regions.
6794 Always return const0_rtx unless we have something particular to
6795 return.
6797 ALIAS_SET is the alias set for the destination. This value will
6798 (in general) be different from that for TARGET, since TARGET is a
6799 reference to the containing structure.
6801 If NONTEMPORAL is true, try generating a nontemporal store.
6803 If REVERSE is true, the store is to be done in reverse order. */
6805 static rtx
6806 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
6807 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
6808 machine_mode mode, tree exp,
6809 alias_set_type alias_set, bool nontemporal, bool reverse)
6811 if (TREE_CODE (exp) == ERROR_MARK)
6812 return const0_rtx;
6814 /* If we have nothing to store, do nothing unless the expression has
6815 side-effects. Don't do that for zero sized addressable lhs of
6816 calls. */
6817 if (known_eq (bitsize, 0)
6818 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6819 || TREE_CODE (exp) != CALL_EXPR))
6820 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6822 if (GET_CODE (target) == CONCAT)
6824 /* We're storing into a struct containing a single __complex. */
6826 gcc_assert (known_eq (bitpos, 0));
6827 return store_expr (exp, target, 0, nontemporal, reverse);
6830 /* If the structure is in a register or if the component
6831 is a bit field, we cannot use addressing to access it.
6832 Use bit-field techniques or SUBREG to store in it. */
6834 poly_int64 decl_bitsize;
6835 if (mode == VOIDmode
6836 || (mode != BLKmode && ! direct_store[(int) mode]
6837 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6838 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6839 || REG_P (target)
6840 || GET_CODE (target) == SUBREG
6841 /* If the field isn't aligned enough to store as an ordinary memref,
6842 store it as a bit field. */
6843 || (mode != BLKmode
6844 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6845 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
6846 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
6847 || !multiple_p (bitpos, BITS_PER_UNIT)))
6848 || (known_size_p (bitsize)
6849 && mode != BLKmode
6850 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
6851 /* If the RHS and field are a constant size and the size of the
6852 RHS isn't the same size as the bitfield, we must use bitfield
6853 operations. */
6854 || (known_size_p (bitsize)
6855 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
6856 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
6857 bitsize)
6858 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6859 we will handle specially below. */
6860 && !(TREE_CODE (exp) == CONSTRUCTOR
6861 && multiple_p (bitsize, BITS_PER_UNIT))
6862 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6863 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6864 includes some extra padding. store_expr / expand_expr will in
6865 that case call get_inner_reference that will have the bitsize
6866 we check here and thus the block move will not clobber the
6867 padding that shouldn't be clobbered. In the future we could
6868 replace the TREE_ADDRESSABLE check with a check that
6869 get_base_address needs to live in memory. */
6870 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6871 || TREE_CODE (exp) != COMPONENT_REF
6872 || !multiple_p (bitsize, BITS_PER_UNIT)
6873 || !multiple_p (bitpos, BITS_PER_UNIT)
6874 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
6875 &decl_bitsize)
6876 || maybe_ne (decl_bitsize, bitsize)))
6877 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6878 decl we must use bitfield operations. */
6879 || (known_size_p (bitsize)
6880 && TREE_CODE (exp) == MEM_REF
6881 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6882 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6883 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6884 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6886 rtx temp;
6887 gimple *nop_def;
6889 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6890 implies a mask operation. If the precision is the same size as
6891 the field we're storing into, that mask is redundant. This is
6892 particularly common with bit field assignments generated by the
6893 C front end. */
6894 nop_def = get_def_for_expr (exp, NOP_EXPR);
6895 if (nop_def)
6897 tree type = TREE_TYPE (exp);
6898 if (INTEGRAL_TYPE_P (type)
6899 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6900 && known_eq (bitsize, TYPE_PRECISION (type)))
6902 tree op = gimple_assign_rhs1 (nop_def);
6903 type = TREE_TYPE (op);
6904 if (INTEGRAL_TYPE_P (type)
6905 && known_ge (TYPE_PRECISION (type), bitsize))
6906 exp = op;
6910 temp = expand_normal (exp);
6912 /* We don't support variable-sized BLKmode bitfields, since our
6913 handling of BLKmode is bound up with the ability to break
6914 things into words. */
6915 gcc_assert (mode != BLKmode || bitsize.is_constant ());
6917 /* Handle calls that return values in multiple non-contiguous locations.
6918 The Irix 6 ABI has examples of this. */
6919 if (GET_CODE (temp) == PARALLEL)
6921 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6922 scalar_int_mode temp_mode
6923 = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6924 rtx temp_target = gen_reg_rtx (temp_mode);
6925 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6926 temp = temp_target;
6929 /* Handle calls that return BLKmode values in registers. */
6930 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6932 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6933 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6934 temp = temp_target;
6937 /* If the value has aggregate type and an integral mode then, if BITSIZE
6938 is narrower than this mode and this is for big-endian data, we first
6939 need to put the value into the low-order bits for store_bit_field,
6940 except when MODE is BLKmode and BITSIZE larger than the word size
6941 (see the handling of fields larger than a word in store_bit_field).
6942 Moreover, the field may be not aligned on a byte boundary; in this
6943 case, if it has reverse storage order, it needs to be accessed as a
6944 scalar field with reverse storage order and we must first put the
6945 value into target order. */
6946 scalar_int_mode temp_mode;
6947 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6948 && is_int_mode (GET_MODE (temp), &temp_mode))
6950 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
6952 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6954 if (reverse)
6955 temp = flip_storage_order (temp_mode, temp);
6957 gcc_checking_assert (known_le (bitsize, size));
6958 if (maybe_lt (bitsize, size)
6959 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6960 /* Use of to_constant for BLKmode was checked above. */
6961 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
6962 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
6963 size - bitsize, NULL_RTX, 1);
6966 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6967 if (mode != VOIDmode && mode != BLKmode
6968 && mode != TYPE_MODE (TREE_TYPE (exp)))
6969 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6971 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6972 and BITPOS must be aligned on a byte boundary. If so, we simply do
6973 a block copy. Likewise for a BLKmode-like TARGET. */
6974 if (GET_MODE (temp) == BLKmode
6975 && (GET_MODE (target) == BLKmode
6976 || (MEM_P (target)
6977 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6978 && multiple_p (bitpos, BITS_PER_UNIT)
6979 && multiple_p (bitsize, BITS_PER_UNIT))))
6981 gcc_assert (MEM_P (target) && MEM_P (temp));
6982 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
6983 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
6985 target = adjust_address (target, VOIDmode, bytepos);
6986 emit_block_move (target, temp,
6987 gen_int_mode (bytesize, Pmode),
6988 BLOCK_OP_NORMAL);
6990 return const0_rtx;
6993 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6994 word size, we need to load the value (see again store_bit_field). */
6995 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
6997 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
6998 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6999 temp_mode, false, NULL);
7002 /* Store the value in the bitfield. */
7003 store_bit_field (target, bitsize, bitpos,
7004 bitregion_start, bitregion_end,
7005 mode, temp, reverse);
7007 return const0_rtx;
7009 else
7011 /* Now build a reference to just the desired component. */
7012 rtx to_rtx = adjust_address (target, mode,
7013 exact_div (bitpos, BITS_PER_UNIT));
7015 if (to_rtx == target)
7016 to_rtx = copy_rtx (to_rtx);
7018 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7019 set_mem_alias_set (to_rtx, alias_set);
7021 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7022 into a target smaller than its type; handle that case now. */
7023 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7025 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7026 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7027 return to_rtx;
7030 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7034 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7035 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7036 codes and find the ultimate containing object, which we return.
7038 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7039 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7040 storage order of the field.
7041 If the position of the field is variable, we store a tree
7042 giving the variable offset (in units) in *POFFSET.
7043 This offset is in addition to the bit position.
7044 If the position is not variable, we store 0 in *POFFSET.
7046 If any of the extraction expressions is volatile,
7047 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7049 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7050 Otherwise, it is a mode that can be used to access the field.
7052 If the field describes a variable-sized object, *PMODE is set to
7053 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7054 this case, but the address of the object can be found. */
7056 tree
7057 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7058 poly_int64_pod *pbitpos, tree *poffset,
7059 machine_mode *pmode, int *punsignedp,
7060 int *preversep, int *pvolatilep)
7062 tree size_tree = 0;
7063 machine_mode mode = VOIDmode;
7064 bool blkmode_bitfield = false;
7065 tree offset = size_zero_node;
7066 poly_offset_int bit_offset = 0;
7068 /* First get the mode, signedness, storage order and size. We do this from
7069 just the outermost expression. */
7070 *pbitsize = -1;
7071 if (TREE_CODE (exp) == COMPONENT_REF)
7073 tree field = TREE_OPERAND (exp, 1);
7074 size_tree = DECL_SIZE (field);
7075 if (flag_strict_volatile_bitfields > 0
7076 && TREE_THIS_VOLATILE (exp)
7077 && DECL_BIT_FIELD_TYPE (field)
7078 && DECL_MODE (field) != BLKmode)
7079 /* Volatile bitfields should be accessed in the mode of the
7080 field's type, not the mode computed based on the bit
7081 size. */
7082 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7083 else if (!DECL_BIT_FIELD (field))
7085 mode = DECL_MODE (field);
7086 /* For vector fields re-check the target flags, as DECL_MODE
7087 could have been set with different target flags than
7088 the current function has. */
7089 if (mode == BLKmode
7090 && VECTOR_TYPE_P (TREE_TYPE (field))
7091 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7092 mode = TYPE_MODE (TREE_TYPE (field));
7094 else if (DECL_MODE (field) == BLKmode)
7095 blkmode_bitfield = true;
7097 *punsignedp = DECL_UNSIGNED (field);
7099 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7101 size_tree = TREE_OPERAND (exp, 1);
7102 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7103 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7105 /* For vector types, with the correct size of access, use the mode of
7106 inner type. */
7107 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7108 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7109 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7110 mode = TYPE_MODE (TREE_TYPE (exp));
7112 else
7114 mode = TYPE_MODE (TREE_TYPE (exp));
7115 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7117 if (mode == BLKmode)
7118 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7119 else
7120 *pbitsize = GET_MODE_BITSIZE (mode);
7123 if (size_tree != 0)
7125 if (! tree_fits_uhwi_p (size_tree))
7126 mode = BLKmode, *pbitsize = -1;
7127 else
7128 *pbitsize = tree_to_uhwi (size_tree);
7131 *preversep = reverse_storage_order_for_component_p (exp);
7133 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7134 and find the ultimate containing object. */
7135 while (1)
7137 switch (TREE_CODE (exp))
7139 case BIT_FIELD_REF:
7140 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7141 break;
7143 case COMPONENT_REF:
7145 tree field = TREE_OPERAND (exp, 1);
7146 tree this_offset = component_ref_field_offset (exp);
7148 /* If this field hasn't been filled in yet, don't go past it.
7149 This should only happen when folding expressions made during
7150 type construction. */
7151 if (this_offset == 0)
7152 break;
7154 offset = size_binop (PLUS_EXPR, offset, this_offset);
7155 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7157 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7159 break;
7161 case ARRAY_REF:
7162 case ARRAY_RANGE_REF:
7164 tree index = TREE_OPERAND (exp, 1);
7165 tree low_bound = array_ref_low_bound (exp);
7166 tree unit_size = array_ref_element_size (exp);
7168 /* We assume all arrays have sizes that are a multiple of a byte.
7169 First subtract the lower bound, if any, in the type of the
7170 index, then convert to sizetype and multiply by the size of
7171 the array element. */
7172 if (! integer_zerop (low_bound))
7173 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7174 index, low_bound);
7176 offset = size_binop (PLUS_EXPR, offset,
7177 size_binop (MULT_EXPR,
7178 fold_convert (sizetype, index),
7179 unit_size));
7181 break;
7183 case REALPART_EXPR:
7184 break;
7186 case IMAGPART_EXPR:
7187 bit_offset += *pbitsize;
7188 break;
7190 case VIEW_CONVERT_EXPR:
7191 break;
7193 case MEM_REF:
7194 /* Hand back the decl for MEM[&decl, off]. */
7195 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7197 tree off = TREE_OPERAND (exp, 1);
7198 if (!integer_zerop (off))
7200 poly_offset_int boff = mem_ref_offset (exp);
7201 boff <<= LOG2_BITS_PER_UNIT;
7202 bit_offset += boff;
7204 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7206 goto done;
7208 default:
7209 goto done;
7212 /* If any reference in the chain is volatile, the effect is volatile. */
7213 if (TREE_THIS_VOLATILE (exp))
7214 *pvolatilep = 1;
7216 exp = TREE_OPERAND (exp, 0);
7218 done:
7220 /* If OFFSET is constant, see if we can return the whole thing as a
7221 constant bit position. Make sure to handle overflow during
7222 this conversion. */
7223 if (poly_int_tree_p (offset))
7225 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7226 TYPE_PRECISION (sizetype));
7227 tem <<= LOG2_BITS_PER_UNIT;
7228 tem += bit_offset;
7229 if (tem.to_shwi (pbitpos))
7230 *poffset = offset = NULL_TREE;
7233 /* Otherwise, split it up. */
7234 if (offset)
7236 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7237 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7239 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7240 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7241 offset = size_binop (PLUS_EXPR, offset,
7242 build_int_cst (sizetype, bytes.force_shwi ()));
7245 *poffset = offset;
7248 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7249 if (mode == VOIDmode
7250 && blkmode_bitfield
7251 && multiple_p (*pbitpos, BITS_PER_UNIT)
7252 && multiple_p (*pbitsize, BITS_PER_UNIT))
7253 *pmode = BLKmode;
7254 else
7255 *pmode = mode;
7257 return exp;
7260 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7262 static unsigned HOST_WIDE_INT
7263 target_align (const_tree target)
7265 /* We might have a chain of nested references with intermediate misaligning
7266 bitfields components, so need to recurse to find out. */
7268 unsigned HOST_WIDE_INT this_align, outer_align;
7270 switch (TREE_CODE (target))
7272 case BIT_FIELD_REF:
7273 return 1;
7275 case COMPONENT_REF:
7276 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7277 outer_align = target_align (TREE_OPERAND (target, 0));
7278 return MIN (this_align, outer_align);
7280 case ARRAY_REF:
7281 case ARRAY_RANGE_REF:
7282 this_align = TYPE_ALIGN (TREE_TYPE (target));
7283 outer_align = target_align (TREE_OPERAND (target, 0));
7284 return MIN (this_align, outer_align);
7286 CASE_CONVERT:
7287 case NON_LVALUE_EXPR:
7288 case VIEW_CONVERT_EXPR:
7289 this_align = TYPE_ALIGN (TREE_TYPE (target));
7290 outer_align = target_align (TREE_OPERAND (target, 0));
7291 return MAX (this_align, outer_align);
7293 default:
7294 return TYPE_ALIGN (TREE_TYPE (target));
7299 /* Given an rtx VALUE that may contain additions and multiplications, return
7300 an equivalent value that just refers to a register, memory, or constant.
7301 This is done by generating instructions to perform the arithmetic and
7302 returning a pseudo-register containing the value.
7304 The returned value may be a REG, SUBREG, MEM or constant. */
7307 force_operand (rtx value, rtx target)
7309 rtx op1, op2;
7310 /* Use subtarget as the target for operand 0 of a binary operation. */
7311 rtx subtarget = get_subtarget (target);
7312 enum rtx_code code = GET_CODE (value);
7314 /* Check for subreg applied to an expression produced by loop optimizer. */
7315 if (code == SUBREG
7316 && !REG_P (SUBREG_REG (value))
7317 && !MEM_P (SUBREG_REG (value)))
7319 value
7320 = simplify_gen_subreg (GET_MODE (value),
7321 force_reg (GET_MODE (SUBREG_REG (value)),
7322 force_operand (SUBREG_REG (value),
7323 NULL_RTX)),
7324 GET_MODE (SUBREG_REG (value)),
7325 SUBREG_BYTE (value));
7326 code = GET_CODE (value);
7329 /* Check for a PIC address load. */
7330 if ((code == PLUS || code == MINUS)
7331 && XEXP (value, 0) == pic_offset_table_rtx
7332 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7333 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7334 || GET_CODE (XEXP (value, 1)) == CONST))
7336 if (!subtarget)
7337 subtarget = gen_reg_rtx (GET_MODE (value));
7338 emit_move_insn (subtarget, value);
7339 return subtarget;
7342 if (ARITHMETIC_P (value))
7344 op2 = XEXP (value, 1);
7345 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7346 subtarget = 0;
7347 if (code == MINUS && CONST_INT_P (op2))
7349 code = PLUS;
7350 op2 = negate_rtx (GET_MODE (value), op2);
7353 /* Check for an addition with OP2 a constant integer and our first
7354 operand a PLUS of a virtual register and something else. In that
7355 case, we want to emit the sum of the virtual register and the
7356 constant first and then add the other value. This allows virtual
7357 register instantiation to simply modify the constant rather than
7358 creating another one around this addition. */
7359 if (code == PLUS && CONST_INT_P (op2)
7360 && GET_CODE (XEXP (value, 0)) == PLUS
7361 && REG_P (XEXP (XEXP (value, 0), 0))
7362 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7363 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7365 rtx temp = expand_simple_binop (GET_MODE (value), code,
7366 XEXP (XEXP (value, 0), 0), op2,
7367 subtarget, 0, OPTAB_LIB_WIDEN);
7368 return expand_simple_binop (GET_MODE (value), code, temp,
7369 force_operand (XEXP (XEXP (value,
7370 0), 1), 0),
7371 target, 0, OPTAB_LIB_WIDEN);
7374 op1 = force_operand (XEXP (value, 0), subtarget);
7375 op2 = force_operand (op2, NULL_RTX);
7376 switch (code)
7378 case MULT:
7379 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7380 case DIV:
7381 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7382 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7383 target, 1, OPTAB_LIB_WIDEN);
7384 else
7385 return expand_divmod (0,
7386 FLOAT_MODE_P (GET_MODE (value))
7387 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7388 GET_MODE (value), op1, op2, target, 0);
7389 case MOD:
7390 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7391 target, 0);
7392 case UDIV:
7393 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7394 target, 1);
7395 case UMOD:
7396 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7397 target, 1);
7398 case ASHIFTRT:
7399 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7400 target, 0, OPTAB_LIB_WIDEN);
7401 default:
7402 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7403 target, 1, OPTAB_LIB_WIDEN);
7406 if (UNARY_P (value))
7408 if (!target)
7409 target = gen_reg_rtx (GET_MODE (value));
7410 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7411 switch (code)
7413 case ZERO_EXTEND:
7414 case SIGN_EXTEND:
7415 case TRUNCATE:
7416 case FLOAT_EXTEND:
7417 case FLOAT_TRUNCATE:
7418 convert_move (target, op1, code == ZERO_EXTEND);
7419 return target;
7421 case FIX:
7422 case UNSIGNED_FIX:
7423 expand_fix (target, op1, code == UNSIGNED_FIX);
7424 return target;
7426 case FLOAT:
7427 case UNSIGNED_FLOAT:
7428 expand_float (target, op1, code == UNSIGNED_FLOAT);
7429 return target;
7431 default:
7432 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7436 #ifdef INSN_SCHEDULING
7437 /* On machines that have insn scheduling, we want all memory reference to be
7438 explicit, so we need to deal with such paradoxical SUBREGs. */
7439 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7440 value
7441 = simplify_gen_subreg (GET_MODE (value),
7442 force_reg (GET_MODE (SUBREG_REG (value)),
7443 force_operand (SUBREG_REG (value),
7444 NULL_RTX)),
7445 GET_MODE (SUBREG_REG (value)),
7446 SUBREG_BYTE (value));
7447 #endif
7449 return value;
7452 /* Subroutine of expand_expr: return nonzero iff there is no way that
7453 EXP can reference X, which is being modified. TOP_P is nonzero if this
7454 call is going to be used to determine whether we need a temporary
7455 for EXP, as opposed to a recursive call to this function.
7457 It is always safe for this routine to return zero since it merely
7458 searches for optimization opportunities. */
7461 safe_from_p (const_rtx x, tree exp, int top_p)
7463 rtx exp_rtl = 0;
7464 int i, nops;
7466 if (x == 0
7467 /* If EXP has varying size, we MUST use a target since we currently
7468 have no way of allocating temporaries of variable size
7469 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7470 So we assume here that something at a higher level has prevented a
7471 clash. This is somewhat bogus, but the best we can do. Only
7472 do this when X is BLKmode and when we are at the top level. */
7473 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7474 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7475 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7476 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7477 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7478 != INTEGER_CST)
7479 && GET_MODE (x) == BLKmode)
7480 /* If X is in the outgoing argument area, it is always safe. */
7481 || (MEM_P (x)
7482 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7483 || (GET_CODE (XEXP (x, 0)) == PLUS
7484 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7485 return 1;
7487 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7488 find the underlying pseudo. */
7489 if (GET_CODE (x) == SUBREG)
7491 x = SUBREG_REG (x);
7492 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7493 return 0;
7496 /* Now look at our tree code and possibly recurse. */
7497 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7499 case tcc_declaration:
7500 exp_rtl = DECL_RTL_IF_SET (exp);
7501 break;
7503 case tcc_constant:
7504 return 1;
7506 case tcc_exceptional:
7507 if (TREE_CODE (exp) == TREE_LIST)
7509 while (1)
7511 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7512 return 0;
7513 exp = TREE_CHAIN (exp);
7514 if (!exp)
7515 return 1;
7516 if (TREE_CODE (exp) != TREE_LIST)
7517 return safe_from_p (x, exp, 0);
7520 else if (TREE_CODE (exp) == CONSTRUCTOR)
7522 constructor_elt *ce;
7523 unsigned HOST_WIDE_INT idx;
7525 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7526 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7527 || !safe_from_p (x, ce->value, 0))
7528 return 0;
7529 return 1;
7531 else if (TREE_CODE (exp) == ERROR_MARK)
7532 return 1; /* An already-visited SAVE_EXPR? */
7533 else
7534 return 0;
7536 case tcc_statement:
7537 /* The only case we look at here is the DECL_INITIAL inside a
7538 DECL_EXPR. */
7539 return (TREE_CODE (exp) != DECL_EXPR
7540 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7541 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7542 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7544 case tcc_binary:
7545 case tcc_comparison:
7546 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7547 return 0;
7548 /* Fall through. */
7550 case tcc_unary:
7551 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7553 case tcc_expression:
7554 case tcc_reference:
7555 case tcc_vl_exp:
7556 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7557 the expression. If it is set, we conflict iff we are that rtx or
7558 both are in memory. Otherwise, we check all operands of the
7559 expression recursively. */
7561 switch (TREE_CODE (exp))
7563 case ADDR_EXPR:
7564 /* If the operand is static or we are static, we can't conflict.
7565 Likewise if we don't conflict with the operand at all. */
7566 if (staticp (TREE_OPERAND (exp, 0))
7567 || TREE_STATIC (exp)
7568 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7569 return 1;
7571 /* Otherwise, the only way this can conflict is if we are taking
7572 the address of a DECL a that address if part of X, which is
7573 very rare. */
7574 exp = TREE_OPERAND (exp, 0);
7575 if (DECL_P (exp))
7577 if (!DECL_RTL_SET_P (exp)
7578 || !MEM_P (DECL_RTL (exp)))
7579 return 0;
7580 else
7581 exp_rtl = XEXP (DECL_RTL (exp), 0);
7583 break;
7585 case MEM_REF:
7586 if (MEM_P (x)
7587 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7588 get_alias_set (exp)))
7589 return 0;
7590 break;
7592 case CALL_EXPR:
7593 /* Assume that the call will clobber all hard registers and
7594 all of memory. */
7595 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7596 || MEM_P (x))
7597 return 0;
7598 break;
7600 case WITH_CLEANUP_EXPR:
7601 case CLEANUP_POINT_EXPR:
7602 /* Lowered by gimplify.c. */
7603 gcc_unreachable ();
7605 case SAVE_EXPR:
7606 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7608 default:
7609 break;
7612 /* If we have an rtx, we do not need to scan our operands. */
7613 if (exp_rtl)
7614 break;
7616 nops = TREE_OPERAND_LENGTH (exp);
7617 for (i = 0; i < nops; i++)
7618 if (TREE_OPERAND (exp, i) != 0
7619 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7620 return 0;
7622 break;
7624 case tcc_type:
7625 /* Should never get a type here. */
7626 gcc_unreachable ();
7629 /* If we have an rtl, find any enclosed object. Then see if we conflict
7630 with it. */
7631 if (exp_rtl)
7633 if (GET_CODE (exp_rtl) == SUBREG)
7635 exp_rtl = SUBREG_REG (exp_rtl);
7636 if (REG_P (exp_rtl)
7637 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7638 return 0;
7641 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7642 are memory and they conflict. */
7643 return ! (rtx_equal_p (x, exp_rtl)
7644 || (MEM_P (x) && MEM_P (exp_rtl)
7645 && true_dependence (exp_rtl, VOIDmode, x)));
7648 /* If we reach here, it is safe. */
7649 return 1;
7653 /* Return the highest power of two that EXP is known to be a multiple of.
7654 This is used in updating alignment of MEMs in array references. */
7656 unsigned HOST_WIDE_INT
7657 highest_pow2_factor (const_tree exp)
7659 unsigned HOST_WIDE_INT ret;
7660 int trailing_zeros = tree_ctz (exp);
7661 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7662 return BIGGEST_ALIGNMENT;
7663 ret = HOST_WIDE_INT_1U << trailing_zeros;
7664 if (ret > BIGGEST_ALIGNMENT)
7665 return BIGGEST_ALIGNMENT;
7666 return ret;
7669 /* Similar, except that the alignment requirements of TARGET are
7670 taken into account. Assume it is at least as aligned as its
7671 type, unless it is a COMPONENT_REF in which case the layout of
7672 the structure gives the alignment. */
7674 static unsigned HOST_WIDE_INT
7675 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7677 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7678 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7680 return MAX (factor, talign);
7683 /* Convert the tree comparison code TCODE to the rtl one where the
7684 signedness is UNSIGNEDP. */
7686 static enum rtx_code
7687 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7689 enum rtx_code code;
7690 switch (tcode)
7692 case EQ_EXPR:
7693 code = EQ;
7694 break;
7695 case NE_EXPR:
7696 code = NE;
7697 break;
7698 case LT_EXPR:
7699 code = unsignedp ? LTU : LT;
7700 break;
7701 case LE_EXPR:
7702 code = unsignedp ? LEU : LE;
7703 break;
7704 case GT_EXPR:
7705 code = unsignedp ? GTU : GT;
7706 break;
7707 case GE_EXPR:
7708 code = unsignedp ? GEU : GE;
7709 break;
7710 case UNORDERED_EXPR:
7711 code = UNORDERED;
7712 break;
7713 case ORDERED_EXPR:
7714 code = ORDERED;
7715 break;
7716 case UNLT_EXPR:
7717 code = UNLT;
7718 break;
7719 case UNLE_EXPR:
7720 code = UNLE;
7721 break;
7722 case UNGT_EXPR:
7723 code = UNGT;
7724 break;
7725 case UNGE_EXPR:
7726 code = UNGE;
7727 break;
7728 case UNEQ_EXPR:
7729 code = UNEQ;
7730 break;
7731 case LTGT_EXPR:
7732 code = LTGT;
7733 break;
7735 default:
7736 gcc_unreachable ();
7738 return code;
7741 /* Subroutine of expand_expr. Expand the two operands of a binary
7742 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7743 The value may be stored in TARGET if TARGET is nonzero. The
7744 MODIFIER argument is as documented by expand_expr. */
7746 void
7747 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7748 enum expand_modifier modifier)
7750 if (! safe_from_p (target, exp1, 1))
7751 target = 0;
7752 if (operand_equal_p (exp0, exp1, 0))
7754 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7755 *op1 = copy_rtx (*op0);
7757 else
7759 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7760 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7765 /* Return a MEM that contains constant EXP. DEFER is as for
7766 output_constant_def and MODIFIER is as for expand_expr. */
7768 static rtx
7769 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7771 rtx mem;
7773 mem = output_constant_def (exp, defer);
7774 if (modifier != EXPAND_INITIALIZER)
7775 mem = use_anchored_address (mem);
7776 return mem;
7779 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7780 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7782 static rtx
7783 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
7784 enum expand_modifier modifier, addr_space_t as)
7786 rtx result, subtarget;
7787 tree inner, offset;
7788 poly_int64 bitsize, bitpos;
7789 int unsignedp, reversep, volatilep = 0;
7790 machine_mode mode1;
7792 /* If we are taking the address of a constant and are at the top level,
7793 we have to use output_constant_def since we can't call force_const_mem
7794 at top level. */
7795 /* ??? This should be considered a front-end bug. We should not be
7796 generating ADDR_EXPR of something that isn't an LVALUE. The only
7797 exception here is STRING_CST. */
7798 if (CONSTANT_CLASS_P (exp))
7800 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7801 if (modifier < EXPAND_SUM)
7802 result = force_operand (result, target);
7803 return result;
7806 /* Everything must be something allowed by is_gimple_addressable. */
7807 switch (TREE_CODE (exp))
7809 case INDIRECT_REF:
7810 /* This case will happen via recursion for &a->b. */
7811 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7813 case MEM_REF:
7815 tree tem = TREE_OPERAND (exp, 0);
7816 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7817 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7818 return expand_expr (tem, target, tmode, modifier);
7821 case CONST_DECL:
7822 /* Expand the initializer like constants above. */
7823 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7824 0, modifier), 0);
7825 if (modifier < EXPAND_SUM)
7826 result = force_operand (result, target);
7827 return result;
7829 case REALPART_EXPR:
7830 /* The real part of the complex number is always first, therefore
7831 the address is the same as the address of the parent object. */
7832 offset = 0;
7833 bitpos = 0;
7834 inner = TREE_OPERAND (exp, 0);
7835 break;
7837 case IMAGPART_EXPR:
7838 /* The imaginary part of the complex number is always second.
7839 The expression is therefore always offset by the size of the
7840 scalar type. */
7841 offset = 0;
7842 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
7843 inner = TREE_OPERAND (exp, 0);
7844 break;
7846 case COMPOUND_LITERAL_EXPR:
7847 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7848 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7849 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7850 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7851 the initializers aren't gimplified. */
7852 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7853 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7854 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7855 target, tmode, modifier, as);
7856 /* FALLTHRU */
7857 default:
7858 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7859 expand_expr, as that can have various side effects; LABEL_DECLs for
7860 example, may not have their DECL_RTL set yet. Expand the rtl of
7861 CONSTRUCTORs too, which should yield a memory reference for the
7862 constructor's contents. Assume language specific tree nodes can
7863 be expanded in some interesting way. */
7864 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7865 if (DECL_P (exp)
7866 || TREE_CODE (exp) == CONSTRUCTOR
7867 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7869 result = expand_expr (exp, target, tmode,
7870 modifier == EXPAND_INITIALIZER
7871 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7873 /* If the DECL isn't in memory, then the DECL wasn't properly
7874 marked TREE_ADDRESSABLE, which will be either a front-end
7875 or a tree optimizer bug. */
7877 gcc_assert (MEM_P (result));
7878 result = XEXP (result, 0);
7880 /* ??? Is this needed anymore? */
7881 if (DECL_P (exp))
7882 TREE_USED (exp) = 1;
7884 if (modifier != EXPAND_INITIALIZER
7885 && modifier != EXPAND_CONST_ADDRESS
7886 && modifier != EXPAND_SUM)
7887 result = force_operand (result, target);
7888 return result;
7891 /* Pass FALSE as the last argument to get_inner_reference although
7892 we are expanding to RTL. The rationale is that we know how to
7893 handle "aligning nodes" here: we can just bypass them because
7894 they won't change the final object whose address will be returned
7895 (they actually exist only for that purpose). */
7896 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7897 &unsignedp, &reversep, &volatilep);
7898 break;
7901 /* We must have made progress. */
7902 gcc_assert (inner != exp);
7904 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
7905 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7906 inner alignment, force the inner to be sufficiently aligned. */
7907 if (CONSTANT_CLASS_P (inner)
7908 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7910 inner = copy_node (inner);
7911 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7912 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7913 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7915 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7917 if (offset)
7919 rtx tmp;
7921 if (modifier != EXPAND_NORMAL)
7922 result = force_operand (result, NULL);
7923 tmp = expand_expr (offset, NULL_RTX, tmode,
7924 modifier == EXPAND_INITIALIZER
7925 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7927 /* expand_expr is allowed to return an object in a mode other
7928 than TMODE. If it did, we need to convert. */
7929 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7930 tmp = convert_modes (tmode, GET_MODE (tmp),
7931 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7932 result = convert_memory_address_addr_space (tmode, result, as);
7933 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7935 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7936 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7937 else
7939 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
7940 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7941 1, OPTAB_LIB_WIDEN);
7945 if (maybe_ne (bitpos, 0))
7947 /* Someone beforehand should have rejected taking the address
7948 of an object that isn't byte-aligned. */
7949 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7950 result = convert_memory_address_addr_space (tmode, result, as);
7951 result = plus_constant (tmode, result, bytepos);
7952 if (modifier < EXPAND_SUM)
7953 result = force_operand (result, target);
7956 return result;
7959 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7960 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7962 static rtx
7963 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7964 enum expand_modifier modifier)
7966 addr_space_t as = ADDR_SPACE_GENERIC;
7967 scalar_int_mode address_mode = Pmode;
7968 scalar_int_mode pointer_mode = ptr_mode;
7969 machine_mode rmode;
7970 rtx result;
7972 /* Target mode of VOIDmode says "whatever's natural". */
7973 if (tmode == VOIDmode)
7974 tmode = TYPE_MODE (TREE_TYPE (exp));
7976 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7978 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7979 address_mode = targetm.addr_space.address_mode (as);
7980 pointer_mode = targetm.addr_space.pointer_mode (as);
7983 /* We can get called with some Weird Things if the user does silliness
7984 like "(short) &a". In that case, convert_memory_address won't do
7985 the right thing, so ignore the given target mode. */
7986 scalar_int_mode new_tmode = (tmode == pointer_mode
7987 ? pointer_mode
7988 : address_mode);
7990 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7991 new_tmode, modifier, as);
7993 /* Despite expand_expr claims concerning ignoring TMODE when not
7994 strictly convenient, stuff breaks if we don't honor it. Note
7995 that combined with the above, we only do this for pointer modes. */
7996 rmode = GET_MODE (result);
7997 if (rmode == VOIDmode)
7998 rmode = new_tmode;
7999 if (rmode != new_tmode)
8000 result = convert_memory_address_addr_space (new_tmode, result, as);
8002 return result;
8005 /* Generate code for computing CONSTRUCTOR EXP.
8006 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8007 is TRUE, instead of creating a temporary variable in memory
8008 NULL is returned and the caller needs to handle it differently. */
8010 static rtx
8011 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8012 bool avoid_temp_mem)
8014 tree type = TREE_TYPE (exp);
8015 machine_mode mode = TYPE_MODE (type);
8017 /* Try to avoid creating a temporary at all. This is possible
8018 if all of the initializer is zero.
8019 FIXME: try to handle all [0..255] initializers we can handle
8020 with memset. */
8021 if (TREE_STATIC (exp)
8022 && !TREE_ADDRESSABLE (exp)
8023 && target != 0 && mode == BLKmode
8024 && all_zeros_p (exp))
8026 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8027 return target;
8030 /* All elts simple constants => refer to a constant in memory. But
8031 if this is a non-BLKmode mode, let it store a field at a time
8032 since that should make a CONST_INT, CONST_WIDE_INT or
8033 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8034 use, it is best to store directly into the target unless the type
8035 is large enough that memcpy will be used. If we are making an
8036 initializer and all operands are constant, put it in memory as
8037 well.
8039 FIXME: Avoid trying to fill vector constructors piece-meal.
8040 Output them with output_constant_def below unless we're sure
8041 they're zeros. This should go away when vector initializers
8042 are treated like VECTOR_CST instead of arrays. */
8043 if ((TREE_STATIC (exp)
8044 && ((mode == BLKmode
8045 && ! (target != 0 && safe_from_p (target, exp, 1)))
8046 || TREE_ADDRESSABLE (exp)
8047 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8048 && (! can_move_by_pieces
8049 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8050 TYPE_ALIGN (type)))
8051 && ! mostly_zeros_p (exp))))
8052 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8053 && TREE_CONSTANT (exp)))
8055 rtx constructor;
8057 if (avoid_temp_mem)
8058 return NULL_RTX;
8060 constructor = expand_expr_constant (exp, 1, modifier);
8062 if (modifier != EXPAND_CONST_ADDRESS
8063 && modifier != EXPAND_INITIALIZER
8064 && modifier != EXPAND_SUM)
8065 constructor = validize_mem (constructor);
8067 return constructor;
8070 /* Handle calls that pass values in multiple non-contiguous
8071 locations. The Irix 6 ABI has examples of this. */
8072 if (target == 0 || ! safe_from_p (target, exp, 1)
8073 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8075 if (avoid_temp_mem)
8076 return NULL_RTX;
8078 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8081 store_constructor (exp, target, 0, int_expr_size (exp), false);
8082 return target;
8086 /* expand_expr: generate code for computing expression EXP.
8087 An rtx for the computed value is returned. The value is never null.
8088 In the case of a void EXP, const0_rtx is returned.
8090 The value may be stored in TARGET if TARGET is nonzero.
8091 TARGET is just a suggestion; callers must assume that
8092 the rtx returned may not be the same as TARGET.
8094 If TARGET is CONST0_RTX, it means that the value will be ignored.
8096 If TMODE is not VOIDmode, it suggests generating the
8097 result in mode TMODE. But this is done only when convenient.
8098 Otherwise, TMODE is ignored and the value generated in its natural mode.
8099 TMODE is just a suggestion; callers must assume that
8100 the rtx returned may not have mode TMODE.
8102 Note that TARGET may have neither TMODE nor MODE. In that case, it
8103 probably will not be used.
8105 If MODIFIER is EXPAND_SUM then when EXP is an addition
8106 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8107 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8108 products as above, or REG or MEM, or constant.
8109 Ordinarily in such cases we would output mul or add instructions
8110 and then return a pseudo reg containing the sum.
8112 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8113 it also marks a label as absolutely required (it can't be dead).
8114 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8115 This is used for outputting expressions used in initializers.
8117 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8118 with a constant address even if that address is not normally legitimate.
8119 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8121 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8122 a call parameter. Such targets require special care as we haven't yet
8123 marked TARGET so that it's safe from being trashed by libcalls. We
8124 don't want to use TARGET for anything but the final result;
8125 Intermediate values must go elsewhere. Additionally, calls to
8126 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8128 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8129 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8130 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8131 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8132 recursively.
8134 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8135 In this case, we don't adjust a returned MEM rtx that wouldn't be
8136 sufficiently aligned for its mode; instead, it's up to the caller
8137 to deal with it afterwards. This is used to make sure that unaligned
8138 base objects for which out-of-bounds accesses are supported, for
8139 example record types with trailing arrays, aren't realigned behind
8140 the back of the caller.
8141 The normal operating mode is to pass FALSE for this parameter. */
8144 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8145 enum expand_modifier modifier, rtx *alt_rtl,
8146 bool inner_reference_p)
8148 rtx ret;
8150 /* Handle ERROR_MARK before anybody tries to access its type. */
8151 if (TREE_CODE (exp) == ERROR_MARK
8152 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8154 ret = CONST0_RTX (tmode);
8155 return ret ? ret : const0_rtx;
8158 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8159 inner_reference_p);
8160 return ret;
8163 /* Try to expand the conditional expression which is represented by
8164 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8165 return the rtl reg which represents the result. Otherwise return
8166 NULL_RTX. */
8168 static rtx
8169 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8170 tree treeop1 ATTRIBUTE_UNUSED,
8171 tree treeop2 ATTRIBUTE_UNUSED)
8173 rtx insn;
8174 rtx op00, op01, op1, op2;
8175 enum rtx_code comparison_code;
8176 machine_mode comparison_mode;
8177 gimple *srcstmt;
8178 rtx temp;
8179 tree type = TREE_TYPE (treeop1);
8180 int unsignedp = TYPE_UNSIGNED (type);
8181 machine_mode mode = TYPE_MODE (type);
8182 machine_mode orig_mode = mode;
8183 static bool expanding_cond_expr_using_cmove = false;
8185 /* Conditional move expansion can end up TERing two operands which,
8186 when recursively hitting conditional expressions can result in
8187 exponential behavior if the cmove expansion ultimatively fails.
8188 It's hardly profitable to TER a cmove into a cmove so avoid doing
8189 that by failing early if we end up recursing. */
8190 if (expanding_cond_expr_using_cmove)
8191 return NULL_RTX;
8193 /* If we cannot do a conditional move on the mode, try doing it
8194 with the promoted mode. */
8195 if (!can_conditionally_move_p (mode))
8197 mode = promote_mode (type, mode, &unsignedp);
8198 if (!can_conditionally_move_p (mode))
8199 return NULL_RTX;
8200 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8202 else
8203 temp = assign_temp (type, 0, 1);
8205 expanding_cond_expr_using_cmove = true;
8206 start_sequence ();
8207 expand_operands (treeop1, treeop2,
8208 temp, &op1, &op2, EXPAND_NORMAL);
8210 if (TREE_CODE (treeop0) == SSA_NAME
8211 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8213 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8214 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8215 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8216 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8217 comparison_mode = TYPE_MODE (type);
8218 unsignedp = TYPE_UNSIGNED (type);
8219 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8221 else if (COMPARISON_CLASS_P (treeop0))
8223 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8224 enum tree_code cmpcode = TREE_CODE (treeop0);
8225 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8226 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8227 unsignedp = TYPE_UNSIGNED (type);
8228 comparison_mode = TYPE_MODE (type);
8229 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8231 else
8233 op00 = expand_normal (treeop0);
8234 op01 = const0_rtx;
8235 comparison_code = NE;
8236 comparison_mode = GET_MODE (op00);
8237 if (comparison_mode == VOIDmode)
8238 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8240 expanding_cond_expr_using_cmove = false;
8242 if (GET_MODE (op1) != mode)
8243 op1 = gen_lowpart (mode, op1);
8245 if (GET_MODE (op2) != mode)
8246 op2 = gen_lowpart (mode, op2);
8248 /* Try to emit the conditional move. */
8249 insn = emit_conditional_move (temp, comparison_code,
8250 op00, op01, comparison_mode,
8251 op1, op2, mode,
8252 unsignedp);
8254 /* If we could do the conditional move, emit the sequence,
8255 and return. */
8256 if (insn)
8258 rtx_insn *seq = get_insns ();
8259 end_sequence ();
8260 emit_insn (seq);
8261 return convert_modes (orig_mode, mode, temp, 0);
8264 /* Otherwise discard the sequence and fall back to code with
8265 branches. */
8266 end_sequence ();
8267 return NULL_RTX;
8271 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8272 enum expand_modifier modifier)
8274 rtx op0, op1, op2, temp;
8275 rtx_code_label *lab;
8276 tree type;
8277 int unsignedp;
8278 machine_mode mode;
8279 scalar_int_mode int_mode;
8280 enum tree_code code = ops->code;
8281 optab this_optab;
8282 rtx subtarget, original_target;
8283 int ignore;
8284 bool reduce_bit_field;
8285 location_t loc = ops->location;
8286 tree treeop0, treeop1, treeop2;
8287 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8288 ? reduce_to_bit_field_precision ((expr), \
8289 target, \
8290 type) \
8291 : (expr))
8293 type = ops->type;
8294 mode = TYPE_MODE (type);
8295 unsignedp = TYPE_UNSIGNED (type);
8297 treeop0 = ops->op0;
8298 treeop1 = ops->op1;
8299 treeop2 = ops->op2;
8301 /* We should be called only on simple (binary or unary) expressions,
8302 exactly those that are valid in gimple expressions that aren't
8303 GIMPLE_SINGLE_RHS (or invalid). */
8304 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8305 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8306 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8308 ignore = (target == const0_rtx
8309 || ((CONVERT_EXPR_CODE_P (code)
8310 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8311 && TREE_CODE (type) == VOID_TYPE));
8313 /* We should be called only if we need the result. */
8314 gcc_assert (!ignore);
8316 /* An operation in what may be a bit-field type needs the
8317 result to be reduced to the precision of the bit-field type,
8318 which is narrower than that of the type's mode. */
8319 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8320 && !type_has_mode_precision_p (type));
8322 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8323 target = 0;
8325 /* Use subtarget as the target for operand 0 of a binary operation. */
8326 subtarget = get_subtarget (target);
8327 original_target = target;
8329 switch (code)
8331 case NON_LVALUE_EXPR:
8332 case PAREN_EXPR:
8333 CASE_CONVERT:
8334 if (treeop0 == error_mark_node)
8335 return const0_rtx;
8337 if (TREE_CODE (type) == UNION_TYPE)
8339 tree valtype = TREE_TYPE (treeop0);
8341 /* If both input and output are BLKmode, this conversion isn't doing
8342 anything except possibly changing memory attribute. */
8343 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8345 rtx result = expand_expr (treeop0, target, tmode,
8346 modifier);
8348 result = copy_rtx (result);
8349 set_mem_attributes (result, type, 0);
8350 return result;
8353 if (target == 0)
8355 if (TYPE_MODE (type) != BLKmode)
8356 target = gen_reg_rtx (TYPE_MODE (type));
8357 else
8358 target = assign_temp (type, 1, 1);
8361 if (MEM_P (target))
8362 /* Store data into beginning of memory target. */
8363 store_expr (treeop0,
8364 adjust_address (target, TYPE_MODE (valtype), 0),
8365 modifier == EXPAND_STACK_PARM,
8366 false, TYPE_REVERSE_STORAGE_ORDER (type));
8368 else
8370 gcc_assert (REG_P (target)
8371 && !TYPE_REVERSE_STORAGE_ORDER (type));
8373 /* Store this field into a union of the proper type. */
8374 store_field (target,
8375 MIN ((int_size_in_bytes (TREE_TYPE
8376 (treeop0))
8377 * BITS_PER_UNIT),
8378 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8379 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8380 false, false);
8383 /* Return the entire union. */
8384 return target;
8387 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8389 op0 = expand_expr (treeop0, target, VOIDmode,
8390 modifier);
8392 /* If the signedness of the conversion differs and OP0 is
8393 a promoted SUBREG, clear that indication since we now
8394 have to do the proper extension. */
8395 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8396 && GET_CODE (op0) == SUBREG)
8397 SUBREG_PROMOTED_VAR_P (op0) = 0;
8399 return REDUCE_BIT_FIELD (op0);
8402 op0 = expand_expr (treeop0, NULL_RTX, mode,
8403 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8404 if (GET_MODE (op0) == mode)
8407 /* If OP0 is a constant, just convert it into the proper mode. */
8408 else if (CONSTANT_P (op0))
8410 tree inner_type = TREE_TYPE (treeop0);
8411 machine_mode inner_mode = GET_MODE (op0);
8413 if (inner_mode == VOIDmode)
8414 inner_mode = TYPE_MODE (inner_type);
8416 if (modifier == EXPAND_INITIALIZER)
8417 op0 = lowpart_subreg (mode, op0, inner_mode);
8418 else
8419 op0= convert_modes (mode, inner_mode, op0,
8420 TYPE_UNSIGNED (inner_type));
8423 else if (modifier == EXPAND_INITIALIZER)
8424 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8425 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8427 else if (target == 0)
8428 op0 = convert_to_mode (mode, op0,
8429 TYPE_UNSIGNED (TREE_TYPE
8430 (treeop0)));
8431 else
8433 convert_move (target, op0,
8434 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8435 op0 = target;
8438 return REDUCE_BIT_FIELD (op0);
8440 case ADDR_SPACE_CONVERT_EXPR:
8442 tree treeop0_type = TREE_TYPE (treeop0);
8444 gcc_assert (POINTER_TYPE_P (type));
8445 gcc_assert (POINTER_TYPE_P (treeop0_type));
8447 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8448 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8450 /* Conversions between pointers to the same address space should
8451 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8452 gcc_assert (as_to != as_from);
8454 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8456 /* Ask target code to handle conversion between pointers
8457 to overlapping address spaces. */
8458 if (targetm.addr_space.subset_p (as_to, as_from)
8459 || targetm.addr_space.subset_p (as_from, as_to))
8461 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8463 else
8465 /* For disjoint address spaces, converting anything but a null
8466 pointer invokes undefined behavior. We truncate or extend the
8467 value as if we'd converted via integers, which handles 0 as
8468 required, and all others as the programmer likely expects. */
8469 #ifndef POINTERS_EXTEND_UNSIGNED
8470 const int POINTERS_EXTEND_UNSIGNED = 1;
8471 #endif
8472 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8473 op0, POINTERS_EXTEND_UNSIGNED);
8475 gcc_assert (op0);
8476 return op0;
8479 case POINTER_PLUS_EXPR:
8480 /* Even though the sizetype mode and the pointer's mode can be different
8481 expand is able to handle this correctly and get the correct result out
8482 of the PLUS_EXPR code. */
8483 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8484 if sizetype precision is smaller than pointer precision. */
8485 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8486 treeop1 = fold_convert_loc (loc, type,
8487 fold_convert_loc (loc, ssizetype,
8488 treeop1));
8489 /* If sizetype precision is larger than pointer precision, truncate the
8490 offset to have matching modes. */
8491 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8492 treeop1 = fold_convert_loc (loc, type, treeop1);
8493 /* FALLTHRU */
8495 case PLUS_EXPR:
8496 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8497 something else, make sure we add the register to the constant and
8498 then to the other thing. This case can occur during strength
8499 reduction and doing it this way will produce better code if the
8500 frame pointer or argument pointer is eliminated.
8502 fold-const.c will ensure that the constant is always in the inner
8503 PLUS_EXPR, so the only case we need to do anything about is if
8504 sp, ap, or fp is our second argument, in which case we must swap
8505 the innermost first argument and our second argument. */
8507 if (TREE_CODE (treeop0) == PLUS_EXPR
8508 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8509 && VAR_P (treeop1)
8510 && (DECL_RTL (treeop1) == frame_pointer_rtx
8511 || DECL_RTL (treeop1) == stack_pointer_rtx
8512 || DECL_RTL (treeop1) == arg_pointer_rtx))
8514 gcc_unreachable ();
8517 /* If the result is to be ptr_mode and we are adding an integer to
8518 something, we might be forming a constant. So try to use
8519 plus_constant. If it produces a sum and we can't accept it,
8520 use force_operand. This allows P = &ARR[const] to generate
8521 efficient code on machines where a SYMBOL_REF is not a valid
8522 address.
8524 If this is an EXPAND_SUM call, always return the sum. */
8525 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8526 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8528 if (modifier == EXPAND_STACK_PARM)
8529 target = 0;
8530 if (TREE_CODE (treeop0) == INTEGER_CST
8531 && HWI_COMPUTABLE_MODE_P (mode)
8532 && TREE_CONSTANT (treeop1))
8534 rtx constant_part;
8535 HOST_WIDE_INT wc;
8536 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8538 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8539 EXPAND_SUM);
8540 /* Use wi::shwi to ensure that the constant is
8541 truncated according to the mode of OP1, then sign extended
8542 to a HOST_WIDE_INT. Using the constant directly can result
8543 in non-canonical RTL in a 64x32 cross compile. */
8544 wc = TREE_INT_CST_LOW (treeop0);
8545 constant_part =
8546 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8547 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8548 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8549 op1 = force_operand (op1, target);
8550 return REDUCE_BIT_FIELD (op1);
8553 else if (TREE_CODE (treeop1) == INTEGER_CST
8554 && HWI_COMPUTABLE_MODE_P (mode)
8555 && TREE_CONSTANT (treeop0))
8557 rtx constant_part;
8558 HOST_WIDE_INT wc;
8559 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8561 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8562 (modifier == EXPAND_INITIALIZER
8563 ? EXPAND_INITIALIZER : EXPAND_SUM));
8564 if (! CONSTANT_P (op0))
8566 op1 = expand_expr (treeop1, NULL_RTX,
8567 VOIDmode, modifier);
8568 /* Return a PLUS if modifier says it's OK. */
8569 if (modifier == EXPAND_SUM
8570 || modifier == EXPAND_INITIALIZER)
8571 return simplify_gen_binary (PLUS, mode, op0, op1);
8572 goto binop2;
8574 /* Use wi::shwi to ensure that the constant is
8575 truncated according to the mode of OP1, then sign extended
8576 to a HOST_WIDE_INT. Using the constant directly can result
8577 in non-canonical RTL in a 64x32 cross compile. */
8578 wc = TREE_INT_CST_LOW (treeop1);
8579 constant_part
8580 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8581 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8582 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8583 op0 = force_operand (op0, target);
8584 return REDUCE_BIT_FIELD (op0);
8588 /* Use TER to expand pointer addition of a negated value
8589 as pointer subtraction. */
8590 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8591 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8592 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8593 && TREE_CODE (treeop1) == SSA_NAME
8594 && TYPE_MODE (TREE_TYPE (treeop0))
8595 == TYPE_MODE (TREE_TYPE (treeop1)))
8597 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8598 if (def)
8600 treeop1 = gimple_assign_rhs1 (def);
8601 code = MINUS_EXPR;
8602 goto do_minus;
8606 /* No sense saving up arithmetic to be done
8607 if it's all in the wrong mode to form part of an address.
8608 And force_operand won't know whether to sign-extend or
8609 zero-extend. */
8610 if (modifier != EXPAND_INITIALIZER
8611 && (modifier != EXPAND_SUM || mode != ptr_mode))
8613 expand_operands (treeop0, treeop1,
8614 subtarget, &op0, &op1, modifier);
8615 if (op0 == const0_rtx)
8616 return op1;
8617 if (op1 == const0_rtx)
8618 return op0;
8619 goto binop2;
8622 expand_operands (treeop0, treeop1,
8623 subtarget, &op0, &op1, modifier);
8624 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8626 case MINUS_EXPR:
8627 case POINTER_DIFF_EXPR:
8628 do_minus:
8629 /* For initializers, we are allowed to return a MINUS of two
8630 symbolic constants. Here we handle all cases when both operands
8631 are constant. */
8632 /* Handle difference of two symbolic constants,
8633 for the sake of an initializer. */
8634 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8635 && really_constant_p (treeop0)
8636 && really_constant_p (treeop1))
8638 expand_operands (treeop0, treeop1,
8639 NULL_RTX, &op0, &op1, modifier);
8640 return simplify_gen_binary (MINUS, mode, op0, op1);
8643 /* No sense saving up arithmetic to be done
8644 if it's all in the wrong mode to form part of an address.
8645 And force_operand won't know whether to sign-extend or
8646 zero-extend. */
8647 if (modifier != EXPAND_INITIALIZER
8648 && (modifier != EXPAND_SUM || mode != ptr_mode))
8649 goto binop;
8651 expand_operands (treeop0, treeop1,
8652 subtarget, &op0, &op1, modifier);
8654 /* Convert A - const to A + (-const). */
8655 if (CONST_INT_P (op1))
8657 op1 = negate_rtx (mode, op1);
8658 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8661 goto binop2;
8663 case WIDEN_MULT_PLUS_EXPR:
8664 case WIDEN_MULT_MINUS_EXPR:
8665 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8666 op2 = expand_normal (treeop2);
8667 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8668 target, unsignedp);
8669 return target;
8671 case WIDEN_MULT_EXPR:
8672 /* If first operand is constant, swap them.
8673 Thus the following special case checks need only
8674 check the second operand. */
8675 if (TREE_CODE (treeop0) == INTEGER_CST)
8676 std::swap (treeop0, treeop1);
8678 /* First, check if we have a multiplication of one signed and one
8679 unsigned operand. */
8680 if (TREE_CODE (treeop1) != INTEGER_CST
8681 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8682 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8684 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8685 this_optab = usmul_widen_optab;
8686 if (find_widening_optab_handler (this_optab, mode, innermode)
8687 != CODE_FOR_nothing)
8689 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8690 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8691 EXPAND_NORMAL);
8692 else
8693 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8694 EXPAND_NORMAL);
8695 /* op0 and op1 might still be constant, despite the above
8696 != INTEGER_CST check. Handle it. */
8697 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8699 op0 = convert_modes (innermode, mode, op0, true);
8700 op1 = convert_modes (innermode, mode, op1, false);
8701 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8702 target, unsignedp));
8704 goto binop3;
8707 /* Check for a multiplication with matching signedness. */
8708 else if ((TREE_CODE (treeop1) == INTEGER_CST
8709 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8710 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8711 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8713 tree op0type = TREE_TYPE (treeop0);
8714 machine_mode innermode = TYPE_MODE (op0type);
8715 bool zextend_p = TYPE_UNSIGNED (op0type);
8716 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8717 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8719 if (TREE_CODE (treeop0) != INTEGER_CST)
8721 if (find_widening_optab_handler (this_optab, mode, innermode)
8722 != CODE_FOR_nothing)
8724 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8725 EXPAND_NORMAL);
8726 /* op0 and op1 might still be constant, despite the above
8727 != INTEGER_CST check. Handle it. */
8728 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8730 widen_mult_const:
8731 op0 = convert_modes (innermode, mode, op0, zextend_p);
8733 = convert_modes (innermode, mode, op1,
8734 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8735 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8736 target,
8737 unsignedp));
8739 temp = expand_widening_mult (mode, op0, op1, target,
8740 unsignedp, this_optab);
8741 return REDUCE_BIT_FIELD (temp);
8743 if (find_widening_optab_handler (other_optab, mode, innermode)
8744 != CODE_FOR_nothing
8745 && innermode == word_mode)
8747 rtx htem, hipart;
8748 op0 = expand_normal (treeop0);
8749 if (TREE_CODE (treeop1) == INTEGER_CST)
8750 op1 = convert_modes (word_mode, mode,
8751 expand_normal (treeop1),
8752 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8753 else
8754 op1 = expand_normal (treeop1);
8755 /* op0 and op1 might still be constant, despite the above
8756 != INTEGER_CST check. Handle it. */
8757 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8758 goto widen_mult_const;
8759 temp = expand_binop (mode, other_optab, op0, op1, target,
8760 unsignedp, OPTAB_LIB_WIDEN);
8761 hipart = gen_highpart (word_mode, temp);
8762 htem = expand_mult_highpart_adjust (word_mode, hipart,
8763 op0, op1, hipart,
8764 zextend_p);
8765 if (htem != hipart)
8766 emit_move_insn (hipart, htem);
8767 return REDUCE_BIT_FIELD (temp);
8771 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8772 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8773 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8774 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8776 case FMA_EXPR:
8778 optab opt = fma_optab;
8779 gimple *def0, *def2;
8781 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8782 call. */
8783 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8785 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8786 tree call_expr;
8788 gcc_assert (fn != NULL_TREE);
8789 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8790 return expand_builtin (call_expr, target, subtarget, mode, false);
8793 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8794 /* The multiplication is commutative - look at its 2nd operand
8795 if the first isn't fed by a negate. */
8796 if (!def0)
8798 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8799 /* Swap operands if the 2nd operand is fed by a negate. */
8800 if (def0)
8801 std::swap (treeop0, treeop1);
8803 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8805 op0 = op2 = NULL;
8807 if (def0 && def2
8808 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8810 opt = fnms_optab;
8811 op0 = expand_normal (gimple_assign_rhs1 (def0));
8812 op2 = expand_normal (gimple_assign_rhs1 (def2));
8814 else if (def0
8815 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8817 opt = fnma_optab;
8818 op0 = expand_normal (gimple_assign_rhs1 (def0));
8820 else if (def2
8821 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8823 opt = fms_optab;
8824 op2 = expand_normal (gimple_assign_rhs1 (def2));
8827 if (op0 == NULL)
8828 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8829 if (op2 == NULL)
8830 op2 = expand_normal (treeop2);
8831 op1 = expand_normal (treeop1);
8833 return expand_ternary_op (TYPE_MODE (type), opt,
8834 op0, op1, op2, target, 0);
8837 case MULT_EXPR:
8838 /* If this is a fixed-point operation, then we cannot use the code
8839 below because "expand_mult" doesn't support sat/no-sat fixed-point
8840 multiplications. */
8841 if (ALL_FIXED_POINT_MODE_P (mode))
8842 goto binop;
8844 /* If first operand is constant, swap them.
8845 Thus the following special case checks need only
8846 check the second operand. */
8847 if (TREE_CODE (treeop0) == INTEGER_CST)
8848 std::swap (treeop0, treeop1);
8850 /* Attempt to return something suitable for generating an
8851 indexed address, for machines that support that. */
8853 if (modifier == EXPAND_SUM && mode == ptr_mode
8854 && tree_fits_shwi_p (treeop1))
8856 tree exp1 = treeop1;
8858 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8859 EXPAND_SUM);
8861 if (!REG_P (op0))
8862 op0 = force_operand (op0, NULL_RTX);
8863 if (!REG_P (op0))
8864 op0 = copy_to_mode_reg (mode, op0);
8866 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8867 gen_int_mode (tree_to_shwi (exp1),
8868 TYPE_MODE (TREE_TYPE (exp1)))));
8871 if (modifier == EXPAND_STACK_PARM)
8872 target = 0;
8874 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8875 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8877 case TRUNC_MOD_EXPR:
8878 case FLOOR_MOD_EXPR:
8879 case CEIL_MOD_EXPR:
8880 case ROUND_MOD_EXPR:
8882 case TRUNC_DIV_EXPR:
8883 case FLOOR_DIV_EXPR:
8884 case CEIL_DIV_EXPR:
8885 case ROUND_DIV_EXPR:
8886 case EXACT_DIV_EXPR:
8888 /* If this is a fixed-point operation, then we cannot use the code
8889 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8890 divisions. */
8891 if (ALL_FIXED_POINT_MODE_P (mode))
8892 goto binop;
8894 if (modifier == EXPAND_STACK_PARM)
8895 target = 0;
8896 /* Possible optimization: compute the dividend with EXPAND_SUM
8897 then if the divisor is constant can optimize the case
8898 where some terms of the dividend have coeffs divisible by it. */
8899 expand_operands (treeop0, treeop1,
8900 subtarget, &op0, &op1, EXPAND_NORMAL);
8901 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8902 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8903 if (SCALAR_INT_MODE_P (mode)
8904 && optimize >= 2
8905 && get_range_pos_neg (treeop0) == 1
8906 && get_range_pos_neg (treeop1) == 1)
8908 /* If both arguments are known to be positive when interpreted
8909 as signed, we can expand it as both signed and unsigned
8910 division or modulo. Choose the cheaper sequence in that case. */
8911 bool speed_p = optimize_insn_for_speed_p ();
8912 do_pending_stack_adjust ();
8913 start_sequence ();
8914 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8915 rtx_insn *uns_insns = get_insns ();
8916 end_sequence ();
8917 start_sequence ();
8918 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8919 rtx_insn *sgn_insns = get_insns ();
8920 end_sequence ();
8921 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8922 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8924 /* If costs are the same then use as tie breaker the other
8925 other factor. */
8926 if (uns_cost == sgn_cost)
8928 uns_cost = seq_cost (uns_insns, !speed_p);
8929 sgn_cost = seq_cost (sgn_insns, !speed_p);
8932 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8934 emit_insn (uns_insns);
8935 return uns_ret;
8937 emit_insn (sgn_insns);
8938 return sgn_ret;
8940 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8942 case RDIV_EXPR:
8943 goto binop;
8945 case MULT_HIGHPART_EXPR:
8946 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8947 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8948 gcc_assert (temp);
8949 return temp;
8951 case FIXED_CONVERT_EXPR:
8952 op0 = expand_normal (treeop0);
8953 if (target == 0 || modifier == EXPAND_STACK_PARM)
8954 target = gen_reg_rtx (mode);
8956 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8957 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8958 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8959 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8960 else
8961 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8962 return target;
8964 case FIX_TRUNC_EXPR:
8965 op0 = expand_normal (treeop0);
8966 if (target == 0 || modifier == EXPAND_STACK_PARM)
8967 target = gen_reg_rtx (mode);
8968 expand_fix (target, op0, unsignedp);
8969 return target;
8971 case FLOAT_EXPR:
8972 op0 = expand_normal (treeop0);
8973 if (target == 0 || modifier == EXPAND_STACK_PARM)
8974 target = gen_reg_rtx (mode);
8975 /* expand_float can't figure out what to do if FROM has VOIDmode.
8976 So give it the correct mode. With -O, cse will optimize this. */
8977 if (GET_MODE (op0) == VOIDmode)
8978 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8979 op0);
8980 expand_float (target, op0,
8981 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8982 return target;
8984 case NEGATE_EXPR:
8985 op0 = expand_expr (treeop0, subtarget,
8986 VOIDmode, EXPAND_NORMAL);
8987 if (modifier == EXPAND_STACK_PARM)
8988 target = 0;
8989 temp = expand_unop (mode,
8990 optab_for_tree_code (NEGATE_EXPR, type,
8991 optab_default),
8992 op0, target, 0);
8993 gcc_assert (temp);
8994 return REDUCE_BIT_FIELD (temp);
8996 case ABS_EXPR:
8997 op0 = expand_expr (treeop0, subtarget,
8998 VOIDmode, EXPAND_NORMAL);
8999 if (modifier == EXPAND_STACK_PARM)
9000 target = 0;
9002 /* ABS_EXPR is not valid for complex arguments. */
9003 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9004 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9006 /* Unsigned abs is simply the operand. Testing here means we don't
9007 risk generating incorrect code below. */
9008 if (TYPE_UNSIGNED (type))
9009 return op0;
9011 return expand_abs (mode, op0, target, unsignedp,
9012 safe_from_p (target, treeop0, 1));
9014 case MAX_EXPR:
9015 case MIN_EXPR:
9016 target = original_target;
9017 if (target == 0
9018 || modifier == EXPAND_STACK_PARM
9019 || (MEM_P (target) && MEM_VOLATILE_P (target))
9020 || GET_MODE (target) != mode
9021 || (REG_P (target)
9022 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9023 target = gen_reg_rtx (mode);
9024 expand_operands (treeop0, treeop1,
9025 target, &op0, &op1, EXPAND_NORMAL);
9027 /* First try to do it with a special MIN or MAX instruction.
9028 If that does not win, use a conditional jump to select the proper
9029 value. */
9030 this_optab = optab_for_tree_code (code, type, optab_default);
9031 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9032 OPTAB_WIDEN);
9033 if (temp != 0)
9034 return temp;
9036 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
9037 and similarly for MAX <x, y>. */
9038 if (VECTOR_TYPE_P (type))
9040 tree t0 = make_tree (type, op0);
9041 tree t1 = make_tree (type, op1);
9042 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
9043 type, t0, t1);
9044 return expand_vec_cond_expr (type, comparison, t0, t1,
9045 original_target);
9048 /* At this point, a MEM target is no longer useful; we will get better
9049 code without it. */
9051 if (! REG_P (target))
9052 target = gen_reg_rtx (mode);
9054 /* If op1 was placed in target, swap op0 and op1. */
9055 if (target != op0 && target == op1)
9056 std::swap (op0, op1);
9058 /* We generate better code and avoid problems with op1 mentioning
9059 target by forcing op1 into a pseudo if it isn't a constant. */
9060 if (! CONSTANT_P (op1))
9061 op1 = force_reg (mode, op1);
9064 enum rtx_code comparison_code;
9065 rtx cmpop1 = op1;
9067 if (code == MAX_EXPR)
9068 comparison_code = unsignedp ? GEU : GE;
9069 else
9070 comparison_code = unsignedp ? LEU : LE;
9072 /* Canonicalize to comparisons against 0. */
9073 if (op1 == const1_rtx)
9075 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9076 or (a != 0 ? a : 1) for unsigned.
9077 For MIN we are safe converting (a <= 1 ? a : 1)
9078 into (a <= 0 ? a : 1) */
9079 cmpop1 = const0_rtx;
9080 if (code == MAX_EXPR)
9081 comparison_code = unsignedp ? NE : GT;
9083 if (op1 == constm1_rtx && !unsignedp)
9085 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9086 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9087 cmpop1 = const0_rtx;
9088 if (code == MIN_EXPR)
9089 comparison_code = LT;
9092 /* Use a conditional move if possible. */
9093 if (can_conditionally_move_p (mode))
9095 rtx insn;
9097 start_sequence ();
9099 /* Try to emit the conditional move. */
9100 insn = emit_conditional_move (target, comparison_code,
9101 op0, cmpop1, mode,
9102 op0, op1, mode,
9103 unsignedp);
9105 /* If we could do the conditional move, emit the sequence,
9106 and return. */
9107 if (insn)
9109 rtx_insn *seq = get_insns ();
9110 end_sequence ();
9111 emit_insn (seq);
9112 return target;
9115 /* Otherwise discard the sequence and fall back to code with
9116 branches. */
9117 end_sequence ();
9120 if (target != op0)
9121 emit_move_insn (target, op0);
9123 lab = gen_label_rtx ();
9124 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9125 unsignedp, mode, NULL_RTX, NULL, lab,
9126 profile_probability::uninitialized ());
9128 emit_move_insn (target, op1);
9129 emit_label (lab);
9130 return target;
9132 case BIT_NOT_EXPR:
9133 op0 = expand_expr (treeop0, subtarget,
9134 VOIDmode, EXPAND_NORMAL);
9135 if (modifier == EXPAND_STACK_PARM)
9136 target = 0;
9137 /* In case we have to reduce the result to bitfield precision
9138 for unsigned bitfield expand this as XOR with a proper constant
9139 instead. */
9140 if (reduce_bit_field && TYPE_UNSIGNED (type))
9142 int_mode = SCALAR_INT_TYPE_MODE (type);
9143 wide_int mask = wi::mask (TYPE_PRECISION (type),
9144 false, GET_MODE_PRECISION (int_mode));
9146 temp = expand_binop (int_mode, xor_optab, op0,
9147 immed_wide_int_const (mask, int_mode),
9148 target, 1, OPTAB_LIB_WIDEN);
9150 else
9151 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9152 gcc_assert (temp);
9153 return temp;
9155 /* ??? Can optimize bitwise operations with one arg constant.
9156 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9157 and (a bitwise1 b) bitwise2 b (etc)
9158 but that is probably not worth while. */
9160 case BIT_AND_EXPR:
9161 case BIT_IOR_EXPR:
9162 case BIT_XOR_EXPR:
9163 goto binop;
9165 case LROTATE_EXPR:
9166 case RROTATE_EXPR:
9167 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9168 || type_has_mode_precision_p (type));
9169 /* fall through */
9171 case LSHIFT_EXPR:
9172 case RSHIFT_EXPR:
9174 /* If this is a fixed-point operation, then we cannot use the code
9175 below because "expand_shift" doesn't support sat/no-sat fixed-point
9176 shifts. */
9177 if (ALL_FIXED_POINT_MODE_P (mode))
9178 goto binop;
9180 if (! safe_from_p (subtarget, treeop1, 1))
9181 subtarget = 0;
9182 if (modifier == EXPAND_STACK_PARM)
9183 target = 0;
9184 op0 = expand_expr (treeop0, subtarget,
9185 VOIDmode, EXPAND_NORMAL);
9187 /* Left shift optimization when shifting across word_size boundary.
9189 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9190 there isn't native instruction to support this wide mode
9191 left shift. Given below scenario:
9193 Type A = (Type) B << C
9195 |< T >|
9196 | dest_high | dest_low |
9198 | word_size |
9200 If the shift amount C caused we shift B to across the word
9201 size boundary, i.e part of B shifted into high half of
9202 destination register, and part of B remains in the low
9203 half, then GCC will use the following left shift expand
9204 logic:
9206 1. Initialize dest_low to B.
9207 2. Initialize every bit of dest_high to the sign bit of B.
9208 3. Logic left shift dest_low by C bit to finalize dest_low.
9209 The value of dest_low before this shift is kept in a temp D.
9210 4. Logic left shift dest_high by C.
9211 5. Logic right shift D by (word_size - C).
9212 6. Or the result of 4 and 5 to finalize dest_high.
9214 While, by checking gimple statements, if operand B is
9215 coming from signed extension, then we can simplify above
9216 expand logic into:
9218 1. dest_high = src_low >> (word_size - C).
9219 2. dest_low = src_low << C.
9221 We can use one arithmetic right shift to finish all the
9222 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9223 needed from 6 into 2.
9225 The case is similar for zero extension, except that we
9226 initialize dest_high to zero rather than copies of the sign
9227 bit from B. Furthermore, we need to use a logical right shift
9228 in this case.
9230 The choice of sign-extension versus zero-extension is
9231 determined entirely by whether or not B is signed and is
9232 independent of the current setting of unsignedp. */
9234 temp = NULL_RTX;
9235 if (code == LSHIFT_EXPR
9236 && target
9237 && REG_P (target)
9238 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9239 && mode == int_mode
9240 && TREE_CONSTANT (treeop1)
9241 && TREE_CODE (treeop0) == SSA_NAME)
9243 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9244 if (is_gimple_assign (def)
9245 && gimple_assign_rhs_code (def) == NOP_EXPR)
9247 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9248 (TREE_TYPE (gimple_assign_rhs1 (def)));
9250 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9251 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9252 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9253 >= GET_MODE_BITSIZE (word_mode)))
9255 rtx_insn *seq, *seq_old;
9256 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9257 int_mode);
9258 bool extend_unsigned
9259 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9260 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9261 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9262 rtx dest_high = simplify_gen_subreg (word_mode, target,
9263 int_mode, high_off);
9264 HOST_WIDE_INT ramount = (BITS_PER_WORD
9265 - TREE_INT_CST_LOW (treeop1));
9266 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9268 start_sequence ();
9269 /* dest_high = src_low >> (word_size - C). */
9270 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9271 rshift, dest_high,
9272 extend_unsigned);
9273 if (temp != dest_high)
9274 emit_move_insn (dest_high, temp);
9276 /* dest_low = src_low << C. */
9277 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9278 treeop1, dest_low, unsignedp);
9279 if (temp != dest_low)
9280 emit_move_insn (dest_low, temp);
9282 seq = get_insns ();
9283 end_sequence ();
9284 temp = target ;
9286 if (have_insn_for (ASHIFT, int_mode))
9288 bool speed_p = optimize_insn_for_speed_p ();
9289 start_sequence ();
9290 rtx ret_old = expand_variable_shift (code, int_mode,
9291 op0, treeop1,
9292 target,
9293 unsignedp);
9295 seq_old = get_insns ();
9296 end_sequence ();
9297 if (seq_cost (seq, speed_p)
9298 >= seq_cost (seq_old, speed_p))
9300 seq = seq_old;
9301 temp = ret_old;
9304 emit_insn (seq);
9309 if (temp == NULL_RTX)
9310 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9311 unsignedp);
9312 if (code == LSHIFT_EXPR)
9313 temp = REDUCE_BIT_FIELD (temp);
9314 return temp;
9317 /* Could determine the answer when only additive constants differ. Also,
9318 the addition of one can be handled by changing the condition. */
9319 case LT_EXPR:
9320 case LE_EXPR:
9321 case GT_EXPR:
9322 case GE_EXPR:
9323 case EQ_EXPR:
9324 case NE_EXPR:
9325 case UNORDERED_EXPR:
9326 case ORDERED_EXPR:
9327 case UNLT_EXPR:
9328 case UNLE_EXPR:
9329 case UNGT_EXPR:
9330 case UNGE_EXPR:
9331 case UNEQ_EXPR:
9332 case LTGT_EXPR:
9334 temp = do_store_flag (ops,
9335 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9336 tmode != VOIDmode ? tmode : mode);
9337 if (temp)
9338 return temp;
9340 /* Use a compare and a jump for BLKmode comparisons, or for function
9341 type comparisons is have_canonicalize_funcptr_for_compare. */
9343 if ((target == 0
9344 || modifier == EXPAND_STACK_PARM
9345 || ! safe_from_p (target, treeop0, 1)
9346 || ! safe_from_p (target, treeop1, 1)
9347 /* Make sure we don't have a hard reg (such as function's return
9348 value) live across basic blocks, if not optimizing. */
9349 || (!optimize && REG_P (target)
9350 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9351 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9353 emit_move_insn (target, const0_rtx);
9355 rtx_code_label *lab1 = gen_label_rtx ();
9356 jumpifnot_1 (code, treeop0, treeop1, lab1,
9357 profile_probability::uninitialized ());
9359 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9360 emit_move_insn (target, constm1_rtx);
9361 else
9362 emit_move_insn (target, const1_rtx);
9364 emit_label (lab1);
9365 return target;
9367 case COMPLEX_EXPR:
9368 /* Get the rtx code of the operands. */
9369 op0 = expand_normal (treeop0);
9370 op1 = expand_normal (treeop1);
9372 if (!target)
9373 target = gen_reg_rtx (TYPE_MODE (type));
9374 else
9375 /* If target overlaps with op1, then either we need to force
9376 op1 into a pseudo (if target also overlaps with op0),
9377 or write the complex parts in reverse order. */
9378 switch (GET_CODE (target))
9380 case CONCAT:
9381 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9383 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9385 complex_expr_force_op1:
9386 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9387 emit_move_insn (temp, op1);
9388 op1 = temp;
9389 break;
9391 complex_expr_swap_order:
9392 /* Move the imaginary (op1) and real (op0) parts to their
9393 location. */
9394 write_complex_part (target, op1, true);
9395 write_complex_part (target, op0, false);
9397 return target;
9399 break;
9400 case MEM:
9401 temp = adjust_address_nv (target,
9402 GET_MODE_INNER (GET_MODE (target)), 0);
9403 if (reg_overlap_mentioned_p (temp, op1))
9405 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9406 temp = adjust_address_nv (target, imode,
9407 GET_MODE_SIZE (imode));
9408 if (reg_overlap_mentioned_p (temp, op0))
9409 goto complex_expr_force_op1;
9410 goto complex_expr_swap_order;
9412 break;
9413 default:
9414 if (reg_overlap_mentioned_p (target, op1))
9416 if (reg_overlap_mentioned_p (target, op0))
9417 goto complex_expr_force_op1;
9418 goto complex_expr_swap_order;
9420 break;
9423 /* Move the real (op0) and imaginary (op1) parts to their location. */
9424 write_complex_part (target, op0, false);
9425 write_complex_part (target, op1, true);
9427 return target;
9429 case WIDEN_SUM_EXPR:
9431 tree oprnd0 = treeop0;
9432 tree oprnd1 = treeop1;
9434 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9435 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9436 target, unsignedp);
9437 return target;
9440 case VEC_UNPACK_HI_EXPR:
9441 case VEC_UNPACK_LO_EXPR:
9443 op0 = expand_normal (treeop0);
9444 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9445 target, unsignedp);
9446 gcc_assert (temp);
9447 return temp;
9450 case VEC_UNPACK_FLOAT_HI_EXPR:
9451 case VEC_UNPACK_FLOAT_LO_EXPR:
9453 op0 = expand_normal (treeop0);
9454 /* The signedness is determined from input operand. */
9455 temp = expand_widen_pattern_expr
9456 (ops, op0, NULL_RTX, NULL_RTX,
9457 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9459 gcc_assert (temp);
9460 return temp;
9463 case VEC_WIDEN_MULT_HI_EXPR:
9464 case VEC_WIDEN_MULT_LO_EXPR:
9465 case VEC_WIDEN_MULT_EVEN_EXPR:
9466 case VEC_WIDEN_MULT_ODD_EXPR:
9467 case VEC_WIDEN_LSHIFT_HI_EXPR:
9468 case VEC_WIDEN_LSHIFT_LO_EXPR:
9469 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9470 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9471 target, unsignedp);
9472 gcc_assert (target);
9473 return target;
9475 case VEC_PACK_TRUNC_EXPR:
9476 case VEC_PACK_SAT_EXPR:
9477 case VEC_PACK_FIX_TRUNC_EXPR:
9478 mode = TYPE_MODE (TREE_TYPE (treeop0));
9479 goto binop;
9481 case VEC_PERM_EXPR:
9482 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9483 op2 = expand_normal (treeop2);
9485 /* Careful here: if the target doesn't support integral vector modes,
9486 a constant selection vector could wind up smooshed into a normal
9487 integral constant. */
9488 if (CONSTANT_P (op2) && !VECTOR_MODE_P (GET_MODE (op2)))
9490 tree sel_type = TREE_TYPE (treeop2);
9491 machine_mode vmode
9492 = mode_for_vector (SCALAR_TYPE_MODE (TREE_TYPE (sel_type)),
9493 TYPE_VECTOR_SUBPARTS (sel_type)).require ();
9494 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9495 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9496 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9498 else
9499 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9501 temp = expand_vec_perm (mode, op0, op1, op2, target);
9502 gcc_assert (temp);
9503 return temp;
9505 case DOT_PROD_EXPR:
9507 tree oprnd0 = treeop0;
9508 tree oprnd1 = treeop1;
9509 tree oprnd2 = treeop2;
9510 rtx op2;
9512 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9513 op2 = expand_normal (oprnd2);
9514 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9515 target, unsignedp);
9516 return target;
9519 case SAD_EXPR:
9521 tree oprnd0 = treeop0;
9522 tree oprnd1 = treeop1;
9523 tree oprnd2 = treeop2;
9524 rtx op2;
9526 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9527 op2 = expand_normal (oprnd2);
9528 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9529 target, unsignedp);
9530 return target;
9533 case REALIGN_LOAD_EXPR:
9535 tree oprnd0 = treeop0;
9536 tree oprnd1 = treeop1;
9537 tree oprnd2 = treeop2;
9538 rtx op2;
9540 this_optab = optab_for_tree_code (code, type, optab_default);
9541 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9542 op2 = expand_normal (oprnd2);
9543 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9544 target, unsignedp);
9545 gcc_assert (temp);
9546 return temp;
9549 case COND_EXPR:
9551 /* A COND_EXPR with its type being VOID_TYPE represents a
9552 conditional jump and is handled in
9553 expand_gimple_cond_expr. */
9554 gcc_assert (!VOID_TYPE_P (type));
9556 /* Note that COND_EXPRs whose type is a structure or union
9557 are required to be constructed to contain assignments of
9558 a temporary variable, so that we can evaluate them here
9559 for side effect only. If type is void, we must do likewise. */
9561 gcc_assert (!TREE_ADDRESSABLE (type)
9562 && !ignore
9563 && TREE_TYPE (treeop1) != void_type_node
9564 && TREE_TYPE (treeop2) != void_type_node);
9566 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9567 if (temp)
9568 return temp;
9570 /* If we are not to produce a result, we have no target. Otherwise,
9571 if a target was specified use it; it will not be used as an
9572 intermediate target unless it is safe. If no target, use a
9573 temporary. */
9575 if (modifier != EXPAND_STACK_PARM
9576 && original_target
9577 && safe_from_p (original_target, treeop0, 1)
9578 && GET_MODE (original_target) == mode
9579 && !MEM_P (original_target))
9580 temp = original_target;
9581 else
9582 temp = assign_temp (type, 0, 1);
9584 do_pending_stack_adjust ();
9585 NO_DEFER_POP;
9586 rtx_code_label *lab0 = gen_label_rtx ();
9587 rtx_code_label *lab1 = gen_label_rtx ();
9588 jumpifnot (treeop0, lab0,
9589 profile_probability::uninitialized ());
9590 store_expr (treeop1, temp,
9591 modifier == EXPAND_STACK_PARM,
9592 false, false);
9594 emit_jump_insn (targetm.gen_jump (lab1));
9595 emit_barrier ();
9596 emit_label (lab0);
9597 store_expr (treeop2, temp,
9598 modifier == EXPAND_STACK_PARM,
9599 false, false);
9601 emit_label (lab1);
9602 OK_DEFER_POP;
9603 return temp;
9606 case VEC_COND_EXPR:
9607 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9608 return target;
9610 case VEC_DUPLICATE_EXPR:
9611 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9612 target = expand_vector_broadcast (mode, op0);
9613 gcc_assert (target);
9614 return target;
9616 case VEC_SERIES_EXPR:
9617 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
9618 return expand_vec_series_expr (mode, op0, op1, target);
9620 case BIT_INSERT_EXPR:
9622 unsigned bitpos = tree_to_uhwi (treeop2);
9623 unsigned bitsize;
9624 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9625 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9626 else
9627 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9628 rtx op0 = expand_normal (treeop0);
9629 rtx op1 = expand_normal (treeop1);
9630 rtx dst = gen_reg_rtx (mode);
9631 emit_move_insn (dst, op0);
9632 store_bit_field (dst, bitsize, bitpos, 0, 0,
9633 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9634 return dst;
9637 default:
9638 gcc_unreachable ();
9641 /* Here to do an ordinary binary operator. */
9642 binop:
9643 expand_operands (treeop0, treeop1,
9644 subtarget, &op0, &op1, EXPAND_NORMAL);
9645 binop2:
9646 this_optab = optab_for_tree_code (code, type, optab_default);
9647 binop3:
9648 if (modifier == EXPAND_STACK_PARM)
9649 target = 0;
9650 temp = expand_binop (mode, this_optab, op0, op1, target,
9651 unsignedp, OPTAB_LIB_WIDEN);
9652 gcc_assert (temp);
9653 /* Bitwise operations do not need bitfield reduction as we expect their
9654 operands being properly truncated. */
9655 if (code == BIT_XOR_EXPR
9656 || code == BIT_AND_EXPR
9657 || code == BIT_IOR_EXPR)
9658 return temp;
9659 return REDUCE_BIT_FIELD (temp);
9661 #undef REDUCE_BIT_FIELD
9664 /* Return TRUE if expression STMT is suitable for replacement.
9665 Never consider memory loads as replaceable, because those don't ever lead
9666 into constant expressions. */
9668 static bool
9669 stmt_is_replaceable_p (gimple *stmt)
9671 if (ssa_is_replaceable_p (stmt))
9673 /* Don't move around loads. */
9674 if (!gimple_assign_single_p (stmt)
9675 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9676 return true;
9678 return false;
9682 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9683 enum expand_modifier modifier, rtx *alt_rtl,
9684 bool inner_reference_p)
9686 rtx op0, op1, temp, decl_rtl;
9687 tree type;
9688 int unsignedp;
9689 machine_mode mode, dmode;
9690 enum tree_code code = TREE_CODE (exp);
9691 rtx subtarget, original_target;
9692 int ignore;
9693 tree context;
9694 bool reduce_bit_field;
9695 location_t loc = EXPR_LOCATION (exp);
9696 struct separate_ops ops;
9697 tree treeop0, treeop1, treeop2;
9698 tree ssa_name = NULL_TREE;
9699 gimple *g;
9701 type = TREE_TYPE (exp);
9702 mode = TYPE_MODE (type);
9703 unsignedp = TYPE_UNSIGNED (type);
9705 treeop0 = treeop1 = treeop2 = NULL_TREE;
9706 if (!VL_EXP_CLASS_P (exp))
9707 switch (TREE_CODE_LENGTH (code))
9709 default:
9710 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9711 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9712 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9713 case 0: break;
9715 ops.code = code;
9716 ops.type = type;
9717 ops.op0 = treeop0;
9718 ops.op1 = treeop1;
9719 ops.op2 = treeop2;
9720 ops.location = loc;
9722 ignore = (target == const0_rtx
9723 || ((CONVERT_EXPR_CODE_P (code)
9724 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9725 && TREE_CODE (type) == VOID_TYPE));
9727 /* An operation in what may be a bit-field type needs the
9728 result to be reduced to the precision of the bit-field type,
9729 which is narrower than that of the type's mode. */
9730 reduce_bit_field = (!ignore
9731 && INTEGRAL_TYPE_P (type)
9732 && !type_has_mode_precision_p (type));
9734 /* If we are going to ignore this result, we need only do something
9735 if there is a side-effect somewhere in the expression. If there
9736 is, short-circuit the most common cases here. Note that we must
9737 not call expand_expr with anything but const0_rtx in case this
9738 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9740 if (ignore)
9742 if (! TREE_SIDE_EFFECTS (exp))
9743 return const0_rtx;
9745 /* Ensure we reference a volatile object even if value is ignored, but
9746 don't do this if all we are doing is taking its address. */
9747 if (TREE_THIS_VOLATILE (exp)
9748 && TREE_CODE (exp) != FUNCTION_DECL
9749 && mode != VOIDmode && mode != BLKmode
9750 && modifier != EXPAND_CONST_ADDRESS)
9752 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9753 if (MEM_P (temp))
9754 copy_to_reg (temp);
9755 return const0_rtx;
9758 if (TREE_CODE_CLASS (code) == tcc_unary
9759 || code == BIT_FIELD_REF
9760 || code == COMPONENT_REF
9761 || code == INDIRECT_REF)
9762 return expand_expr (treeop0, const0_rtx, VOIDmode,
9763 modifier);
9765 else if (TREE_CODE_CLASS (code) == tcc_binary
9766 || TREE_CODE_CLASS (code) == tcc_comparison
9767 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9769 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9770 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9771 return const0_rtx;
9774 target = 0;
9777 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9778 target = 0;
9780 /* Use subtarget as the target for operand 0 of a binary operation. */
9781 subtarget = get_subtarget (target);
9782 original_target = target;
9784 switch (code)
9786 case LABEL_DECL:
9788 tree function = decl_function_context (exp);
9790 temp = label_rtx (exp);
9791 temp = gen_rtx_LABEL_REF (Pmode, temp);
9793 if (function != current_function_decl
9794 && function != 0)
9795 LABEL_REF_NONLOCAL_P (temp) = 1;
9797 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9798 return temp;
9801 case SSA_NAME:
9802 /* ??? ivopts calls expander, without any preparation from
9803 out-of-ssa. So fake instructions as if this was an access to the
9804 base variable. This unnecessarily allocates a pseudo, see how we can
9805 reuse it, if partition base vars have it set already. */
9806 if (!currently_expanding_to_rtl)
9808 tree var = SSA_NAME_VAR (exp);
9809 if (var && DECL_RTL_SET_P (var))
9810 return DECL_RTL (var);
9811 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9812 LAST_VIRTUAL_REGISTER + 1);
9815 g = get_gimple_for_ssa_name (exp);
9816 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9817 if (g == NULL
9818 && modifier == EXPAND_INITIALIZER
9819 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9820 && (optimize || !SSA_NAME_VAR (exp)
9821 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9822 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9823 g = SSA_NAME_DEF_STMT (exp);
9824 if (g)
9826 rtx r;
9827 location_t saved_loc = curr_insn_location ();
9828 location_t loc = gimple_location (g);
9829 if (loc != UNKNOWN_LOCATION)
9830 set_curr_insn_location (loc);
9831 ops.code = gimple_assign_rhs_code (g);
9832 switch (get_gimple_rhs_class (ops.code))
9834 case GIMPLE_TERNARY_RHS:
9835 ops.op2 = gimple_assign_rhs3 (g);
9836 /* Fallthru */
9837 case GIMPLE_BINARY_RHS:
9838 ops.op1 = gimple_assign_rhs2 (g);
9840 /* Try to expand conditonal compare. */
9841 if (targetm.gen_ccmp_first)
9843 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9844 r = expand_ccmp_expr (g, mode);
9845 if (r)
9846 break;
9848 /* Fallthru */
9849 case GIMPLE_UNARY_RHS:
9850 ops.op0 = gimple_assign_rhs1 (g);
9851 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9852 ops.location = loc;
9853 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9854 break;
9855 case GIMPLE_SINGLE_RHS:
9857 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9858 tmode, modifier, alt_rtl,
9859 inner_reference_p);
9860 break;
9862 default:
9863 gcc_unreachable ();
9865 set_curr_insn_location (saved_loc);
9866 if (REG_P (r) && !REG_EXPR (r))
9867 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9868 return r;
9871 ssa_name = exp;
9872 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9873 exp = SSA_NAME_VAR (ssa_name);
9874 goto expand_decl_rtl;
9876 case PARM_DECL:
9877 case VAR_DECL:
9878 /* If a static var's type was incomplete when the decl was written,
9879 but the type is complete now, lay out the decl now. */
9880 if (DECL_SIZE (exp) == 0
9881 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9882 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9883 layout_decl (exp, 0);
9885 /* fall through */
9887 case FUNCTION_DECL:
9888 case RESULT_DECL:
9889 decl_rtl = DECL_RTL (exp);
9890 expand_decl_rtl:
9891 gcc_assert (decl_rtl);
9893 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9894 settings for VECTOR_TYPE_P that might switch for the function. */
9895 if (currently_expanding_to_rtl
9896 && code == VAR_DECL && MEM_P (decl_rtl)
9897 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9898 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9899 else
9900 decl_rtl = copy_rtx (decl_rtl);
9902 /* Record writes to register variables. */
9903 if (modifier == EXPAND_WRITE
9904 && REG_P (decl_rtl)
9905 && HARD_REGISTER_P (decl_rtl))
9906 add_to_hard_reg_set (&crtl->asm_clobbers,
9907 GET_MODE (decl_rtl), REGNO (decl_rtl));
9909 /* Ensure variable marked as used even if it doesn't go through
9910 a parser. If it hasn't be used yet, write out an external
9911 definition. */
9912 if (exp)
9913 TREE_USED (exp) = 1;
9915 /* Show we haven't gotten RTL for this yet. */
9916 temp = 0;
9918 /* Variables inherited from containing functions should have
9919 been lowered by this point. */
9920 if (exp)
9921 context = decl_function_context (exp);
9922 gcc_assert (!exp
9923 || SCOPE_FILE_SCOPE_P (context)
9924 || context == current_function_decl
9925 || TREE_STATIC (exp)
9926 || DECL_EXTERNAL (exp)
9927 /* ??? C++ creates functions that are not TREE_STATIC. */
9928 || TREE_CODE (exp) == FUNCTION_DECL);
9930 /* This is the case of an array whose size is to be determined
9931 from its initializer, while the initializer is still being parsed.
9932 ??? We aren't parsing while expanding anymore. */
9934 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9935 temp = validize_mem (decl_rtl);
9937 /* If DECL_RTL is memory, we are in the normal case and the
9938 address is not valid, get the address into a register. */
9940 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9942 if (alt_rtl)
9943 *alt_rtl = decl_rtl;
9944 decl_rtl = use_anchored_address (decl_rtl);
9945 if (modifier != EXPAND_CONST_ADDRESS
9946 && modifier != EXPAND_SUM
9947 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9948 : GET_MODE (decl_rtl),
9949 XEXP (decl_rtl, 0),
9950 MEM_ADDR_SPACE (decl_rtl)))
9951 temp = replace_equiv_address (decl_rtl,
9952 copy_rtx (XEXP (decl_rtl, 0)));
9955 /* If we got something, return it. But first, set the alignment
9956 if the address is a register. */
9957 if (temp != 0)
9959 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9960 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9962 return temp;
9965 if (exp)
9966 dmode = DECL_MODE (exp);
9967 else
9968 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9970 /* If the mode of DECL_RTL does not match that of the decl,
9971 there are two cases: we are dealing with a BLKmode value
9972 that is returned in a register, or we are dealing with
9973 a promoted value. In the latter case, return a SUBREG
9974 of the wanted mode, but mark it so that we know that it
9975 was already extended. */
9976 if (REG_P (decl_rtl)
9977 && dmode != BLKmode
9978 && GET_MODE (decl_rtl) != dmode)
9980 machine_mode pmode;
9982 /* Get the signedness to be used for this variable. Ensure we get
9983 the same mode we got when the variable was declared. */
9984 if (code != SSA_NAME)
9985 pmode = promote_decl_mode (exp, &unsignedp);
9986 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9987 && gimple_code (g) == GIMPLE_CALL
9988 && !gimple_call_internal_p (g))
9989 pmode = promote_function_mode (type, mode, &unsignedp,
9990 gimple_call_fntype (g),
9992 else
9993 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9994 gcc_assert (GET_MODE (decl_rtl) == pmode);
9996 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9997 SUBREG_PROMOTED_VAR_P (temp) = 1;
9998 SUBREG_PROMOTED_SET (temp, unsignedp);
9999 return temp;
10002 return decl_rtl;
10004 case INTEGER_CST:
10006 /* Given that TYPE_PRECISION (type) is not always equal to
10007 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10008 the former to the latter according to the signedness of the
10009 type. */
10010 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
10011 temp = immed_wide_int_const
10012 (wi::to_wide (exp, GET_MODE_PRECISION (mode)), mode);
10013 return temp;
10016 case VECTOR_CST:
10018 tree tmp = NULL_TREE;
10019 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
10020 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
10021 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
10022 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
10023 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
10024 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
10025 return const_vector_from_tree (exp);
10026 scalar_int_mode int_mode;
10027 if (is_int_mode (mode, &int_mode))
10029 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
10030 return const_scalar_mask_from_tree (int_mode, exp);
10031 else
10033 tree type_for_mode
10034 = lang_hooks.types.type_for_mode (int_mode, 1);
10035 if (type_for_mode)
10036 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10037 type_for_mode, exp);
10040 if (!tmp)
10042 vec<constructor_elt, va_gc> *v;
10043 unsigned i;
10044 vec_alloc (v, VECTOR_CST_NELTS (exp));
10045 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
10046 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10047 tmp = build_constructor (type, v);
10049 return expand_expr (tmp, ignore ? const0_rtx : target,
10050 tmode, modifier);
10053 case CONST_DECL:
10054 if (modifier == EXPAND_WRITE)
10056 /* Writing into CONST_DECL is always invalid, but handle it
10057 gracefully. */
10058 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10059 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10060 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10061 EXPAND_NORMAL, as);
10062 op0 = memory_address_addr_space (mode, op0, as);
10063 temp = gen_rtx_MEM (mode, op0);
10064 set_mem_addr_space (temp, as);
10065 return temp;
10067 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10069 case REAL_CST:
10070 /* If optimized, generate immediate CONST_DOUBLE
10071 which will be turned into memory by reload if necessary.
10073 We used to force a register so that loop.c could see it. But
10074 this does not allow gen_* patterns to perform optimizations with
10075 the constants. It also produces two insns in cases like "x = 1.0;".
10076 On most machines, floating-point constants are not permitted in
10077 many insns, so we'd end up copying it to a register in any case.
10079 Now, we do the copying in expand_binop, if appropriate. */
10080 return const_double_from_real_value (TREE_REAL_CST (exp),
10081 TYPE_MODE (TREE_TYPE (exp)));
10083 case FIXED_CST:
10084 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10085 TYPE_MODE (TREE_TYPE (exp)));
10087 case COMPLEX_CST:
10088 /* Handle evaluating a complex constant in a CONCAT target. */
10089 if (original_target && GET_CODE (original_target) == CONCAT)
10091 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10092 rtx rtarg, itarg;
10094 rtarg = XEXP (original_target, 0);
10095 itarg = XEXP (original_target, 1);
10097 /* Move the real and imaginary parts separately. */
10098 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10099 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10101 if (op0 != rtarg)
10102 emit_move_insn (rtarg, op0);
10103 if (op1 != itarg)
10104 emit_move_insn (itarg, op1);
10106 return original_target;
10109 /* fall through */
10111 case STRING_CST:
10112 temp = expand_expr_constant (exp, 1, modifier);
10114 /* temp contains a constant address.
10115 On RISC machines where a constant address isn't valid,
10116 make some insns to get that address into a register. */
10117 if (modifier != EXPAND_CONST_ADDRESS
10118 && modifier != EXPAND_INITIALIZER
10119 && modifier != EXPAND_SUM
10120 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10121 MEM_ADDR_SPACE (temp)))
10122 return replace_equiv_address (temp,
10123 copy_rtx (XEXP (temp, 0)));
10124 return temp;
10126 case POLY_INT_CST:
10127 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10129 case SAVE_EXPR:
10131 tree val = treeop0;
10132 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10133 inner_reference_p);
10135 if (!SAVE_EXPR_RESOLVED_P (exp))
10137 /* We can indeed still hit this case, typically via builtin
10138 expanders calling save_expr immediately before expanding
10139 something. Assume this means that we only have to deal
10140 with non-BLKmode values. */
10141 gcc_assert (GET_MODE (ret) != BLKmode);
10143 val = build_decl (curr_insn_location (),
10144 VAR_DECL, NULL, TREE_TYPE (exp));
10145 DECL_ARTIFICIAL (val) = 1;
10146 DECL_IGNORED_P (val) = 1;
10147 treeop0 = val;
10148 TREE_OPERAND (exp, 0) = treeop0;
10149 SAVE_EXPR_RESOLVED_P (exp) = 1;
10151 if (!CONSTANT_P (ret))
10152 ret = copy_to_reg (ret);
10153 SET_DECL_RTL (val, ret);
10156 return ret;
10160 case CONSTRUCTOR:
10161 /* If we don't need the result, just ensure we evaluate any
10162 subexpressions. */
10163 if (ignore)
10165 unsigned HOST_WIDE_INT idx;
10166 tree value;
10168 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10169 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10171 return const0_rtx;
10174 return expand_constructor (exp, target, modifier, false);
10176 case TARGET_MEM_REF:
10178 addr_space_t as
10179 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10180 enum insn_code icode;
10181 unsigned int align;
10183 op0 = addr_for_mem_ref (exp, as, true);
10184 op0 = memory_address_addr_space (mode, op0, as);
10185 temp = gen_rtx_MEM (mode, op0);
10186 set_mem_attributes (temp, exp, 0);
10187 set_mem_addr_space (temp, as);
10188 align = get_object_alignment (exp);
10189 if (modifier != EXPAND_WRITE
10190 && modifier != EXPAND_MEMORY
10191 && mode != BLKmode
10192 && align < GET_MODE_ALIGNMENT (mode)
10193 /* If the target does not have special handling for unaligned
10194 loads of mode then it can use regular moves for them. */
10195 && ((icode = optab_handler (movmisalign_optab, mode))
10196 != CODE_FOR_nothing))
10198 struct expand_operand ops[2];
10200 /* We've already validated the memory, and we're creating a
10201 new pseudo destination. The predicates really can't fail,
10202 nor can the generator. */
10203 create_output_operand (&ops[0], NULL_RTX, mode);
10204 create_fixed_operand (&ops[1], temp);
10205 expand_insn (icode, 2, ops);
10206 temp = ops[0].value;
10208 return temp;
10211 case MEM_REF:
10213 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10214 addr_space_t as
10215 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10216 machine_mode address_mode;
10217 tree base = TREE_OPERAND (exp, 0);
10218 gimple *def_stmt;
10219 enum insn_code icode;
10220 unsigned align;
10221 /* Handle expansion of non-aliased memory with non-BLKmode. That
10222 might end up in a register. */
10223 if (mem_ref_refers_to_non_mem_p (exp))
10225 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10226 base = TREE_OPERAND (base, 0);
10227 if (known_eq (offset, 0)
10228 && !reverse
10229 && tree_fits_uhwi_p (TYPE_SIZE (type))
10230 && (GET_MODE_BITSIZE (DECL_MODE (base))
10231 == tree_to_uhwi (TYPE_SIZE (type))))
10232 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10233 target, tmode, modifier);
10234 if (TYPE_MODE (type) == BLKmode)
10236 temp = assign_stack_temp (DECL_MODE (base),
10237 GET_MODE_SIZE (DECL_MODE (base)));
10238 store_expr (base, temp, 0, false, false);
10239 temp = adjust_address (temp, BLKmode, offset);
10240 set_mem_size (temp, int_size_in_bytes (type));
10241 return temp;
10243 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10244 bitsize_int (offset * BITS_PER_UNIT));
10245 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10246 return expand_expr (exp, target, tmode, modifier);
10248 address_mode = targetm.addr_space.address_mode (as);
10249 base = TREE_OPERAND (exp, 0);
10250 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10252 tree mask = gimple_assign_rhs2 (def_stmt);
10253 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10254 gimple_assign_rhs1 (def_stmt), mask);
10255 TREE_OPERAND (exp, 0) = base;
10257 align = get_object_alignment (exp);
10258 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10259 op0 = memory_address_addr_space (mode, op0, as);
10260 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10262 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10263 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10264 op0 = memory_address_addr_space (mode, op0, as);
10266 temp = gen_rtx_MEM (mode, op0);
10267 set_mem_attributes (temp, exp, 0);
10268 set_mem_addr_space (temp, as);
10269 if (TREE_THIS_VOLATILE (exp))
10270 MEM_VOLATILE_P (temp) = 1;
10271 if (modifier != EXPAND_WRITE
10272 && modifier != EXPAND_MEMORY
10273 && !inner_reference_p
10274 && mode != BLKmode
10275 && align < GET_MODE_ALIGNMENT (mode))
10277 if ((icode = optab_handler (movmisalign_optab, mode))
10278 != CODE_FOR_nothing)
10280 struct expand_operand ops[2];
10282 /* We've already validated the memory, and we're creating a
10283 new pseudo destination. The predicates really can't fail,
10284 nor can the generator. */
10285 create_output_operand (&ops[0], NULL_RTX, mode);
10286 create_fixed_operand (&ops[1], temp);
10287 expand_insn (icode, 2, ops);
10288 temp = ops[0].value;
10290 else if (targetm.slow_unaligned_access (mode, align))
10291 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10292 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10293 (modifier == EXPAND_STACK_PARM
10294 ? NULL_RTX : target),
10295 mode, mode, false, alt_rtl);
10297 if (reverse
10298 && modifier != EXPAND_MEMORY
10299 && modifier != EXPAND_WRITE)
10300 temp = flip_storage_order (mode, temp);
10301 return temp;
10304 case ARRAY_REF:
10307 tree array = treeop0;
10308 tree index = treeop1;
10309 tree init;
10311 /* Fold an expression like: "foo"[2].
10312 This is not done in fold so it won't happen inside &.
10313 Don't fold if this is for wide characters since it's too
10314 difficult to do correctly and this is a very rare case. */
10316 if (modifier != EXPAND_CONST_ADDRESS
10317 && modifier != EXPAND_INITIALIZER
10318 && modifier != EXPAND_MEMORY)
10320 tree t = fold_read_from_constant_string (exp);
10322 if (t)
10323 return expand_expr (t, target, tmode, modifier);
10326 /* If this is a constant index into a constant array,
10327 just get the value from the array. Handle both the cases when
10328 we have an explicit constructor and when our operand is a variable
10329 that was declared const. */
10331 if (modifier != EXPAND_CONST_ADDRESS
10332 && modifier != EXPAND_INITIALIZER
10333 && modifier != EXPAND_MEMORY
10334 && TREE_CODE (array) == CONSTRUCTOR
10335 && ! TREE_SIDE_EFFECTS (array)
10336 && TREE_CODE (index) == INTEGER_CST)
10338 unsigned HOST_WIDE_INT ix;
10339 tree field, value;
10341 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10342 field, value)
10343 if (tree_int_cst_equal (field, index))
10345 if (!TREE_SIDE_EFFECTS (value))
10346 return expand_expr (fold (value), target, tmode, modifier);
10347 break;
10351 else if (optimize >= 1
10352 && modifier != EXPAND_CONST_ADDRESS
10353 && modifier != EXPAND_INITIALIZER
10354 && modifier != EXPAND_MEMORY
10355 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10356 && TREE_CODE (index) == INTEGER_CST
10357 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10358 && (init = ctor_for_folding (array)) != error_mark_node)
10360 if (init == NULL_TREE)
10362 tree value = build_zero_cst (type);
10363 if (TREE_CODE (value) == CONSTRUCTOR)
10365 /* If VALUE is a CONSTRUCTOR, this optimization is only
10366 useful if this doesn't store the CONSTRUCTOR into
10367 memory. If it does, it is more efficient to just
10368 load the data from the array directly. */
10369 rtx ret = expand_constructor (value, target,
10370 modifier, true);
10371 if (ret == NULL_RTX)
10372 value = NULL_TREE;
10375 if (value)
10376 return expand_expr (value, target, tmode, modifier);
10378 else if (TREE_CODE (init) == CONSTRUCTOR)
10380 unsigned HOST_WIDE_INT ix;
10381 tree field, value;
10383 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10384 field, value)
10385 if (tree_int_cst_equal (field, index))
10387 if (TREE_SIDE_EFFECTS (value))
10388 break;
10390 if (TREE_CODE (value) == CONSTRUCTOR)
10392 /* If VALUE is a CONSTRUCTOR, this
10393 optimization is only useful if
10394 this doesn't store the CONSTRUCTOR
10395 into memory. If it does, it is more
10396 efficient to just load the data from
10397 the array directly. */
10398 rtx ret = expand_constructor (value, target,
10399 modifier, true);
10400 if (ret == NULL_RTX)
10401 break;
10404 return
10405 expand_expr (fold (value), target, tmode, modifier);
10408 else if (TREE_CODE (init) == STRING_CST)
10410 tree low_bound = array_ref_low_bound (exp);
10411 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10413 /* Optimize the special case of a zero lower bound.
10415 We convert the lower bound to sizetype to avoid problems
10416 with constant folding. E.g. suppose the lower bound is
10417 1 and its mode is QI. Without the conversion
10418 (ARRAY + (INDEX - (unsigned char)1))
10419 becomes
10420 (ARRAY + (-(unsigned char)1) + INDEX)
10421 which becomes
10422 (ARRAY + 255 + INDEX). Oops! */
10423 if (!integer_zerop (low_bound))
10424 index1 = size_diffop_loc (loc, index1,
10425 fold_convert_loc (loc, sizetype,
10426 low_bound));
10428 if (tree_fits_uhwi_p (index1)
10429 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10431 tree type = TREE_TYPE (TREE_TYPE (init));
10432 scalar_int_mode mode;
10434 if (is_int_mode (TYPE_MODE (type), &mode)
10435 && GET_MODE_SIZE (mode) == 1)
10436 return gen_int_mode (TREE_STRING_POINTER (init)
10437 [TREE_INT_CST_LOW (index1)],
10438 mode);
10443 goto normal_inner_ref;
10445 case COMPONENT_REF:
10446 /* If the operand is a CONSTRUCTOR, we can just extract the
10447 appropriate field if it is present. */
10448 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10450 unsigned HOST_WIDE_INT idx;
10451 tree field, value;
10452 scalar_int_mode field_mode;
10454 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10455 idx, field, value)
10456 if (field == treeop1
10457 /* We can normally use the value of the field in the
10458 CONSTRUCTOR. However, if this is a bitfield in
10459 an integral mode that we can fit in a HOST_WIDE_INT,
10460 we must mask only the number of bits in the bitfield,
10461 since this is done implicitly by the constructor. If
10462 the bitfield does not meet either of those conditions,
10463 we can't do this optimization. */
10464 && (! DECL_BIT_FIELD (field)
10465 || (is_int_mode (DECL_MODE (field), &field_mode)
10466 && (GET_MODE_PRECISION (field_mode)
10467 <= HOST_BITS_PER_WIDE_INT))))
10469 if (DECL_BIT_FIELD (field)
10470 && modifier == EXPAND_STACK_PARM)
10471 target = 0;
10472 op0 = expand_expr (value, target, tmode, modifier);
10473 if (DECL_BIT_FIELD (field))
10475 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10476 scalar_int_mode imode
10477 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10479 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10481 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10482 imode);
10483 op0 = expand_and (imode, op0, op1, target);
10485 else
10487 int count = GET_MODE_PRECISION (imode) - bitsize;
10489 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10490 target, 0);
10491 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10492 target, 0);
10496 return op0;
10499 goto normal_inner_ref;
10501 case BIT_FIELD_REF:
10502 case ARRAY_RANGE_REF:
10503 normal_inner_ref:
10505 machine_mode mode1, mode2;
10506 poly_int64 bitsize, bitpos, bytepos;
10507 tree offset;
10508 int reversep, volatilep = 0, must_force_mem;
10509 tree tem
10510 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10511 &unsignedp, &reversep, &volatilep);
10512 rtx orig_op0, memloc;
10513 bool clear_mem_expr = false;
10515 /* If we got back the original object, something is wrong. Perhaps
10516 we are evaluating an expression too early. In any event, don't
10517 infinitely recurse. */
10518 gcc_assert (tem != exp);
10520 /* If TEM's type is a union of variable size, pass TARGET to the inner
10521 computation, since it will need a temporary and TARGET is known
10522 to have to do. This occurs in unchecked conversion in Ada. */
10523 orig_op0 = op0
10524 = expand_expr_real (tem,
10525 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10526 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10527 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10528 != INTEGER_CST)
10529 && modifier != EXPAND_STACK_PARM
10530 ? target : NULL_RTX),
10531 VOIDmode,
10532 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10533 NULL, true);
10535 /* If the field has a mode, we want to access it in the
10536 field's mode, not the computed mode.
10537 If a MEM has VOIDmode (external with incomplete type),
10538 use BLKmode for it instead. */
10539 if (MEM_P (op0))
10541 if (mode1 != VOIDmode)
10542 op0 = adjust_address (op0, mode1, 0);
10543 else if (GET_MODE (op0) == VOIDmode)
10544 op0 = adjust_address (op0, BLKmode, 0);
10547 mode2
10548 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10550 /* If we have either an offset, a BLKmode result, or a reference
10551 outside the underlying object, we must force it to memory.
10552 Such a case can occur in Ada if we have unchecked conversion
10553 of an expression from a scalar type to an aggregate type or
10554 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10555 passed a partially uninitialized object or a view-conversion
10556 to a larger size. */
10557 must_force_mem = (offset
10558 || mode1 == BLKmode
10559 || maybe_gt (bitpos + bitsize,
10560 GET_MODE_BITSIZE (mode2)));
10562 /* Handle CONCAT first. */
10563 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10565 if (known_eq (bitpos, 0)
10566 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
10567 && COMPLEX_MODE_P (mode1)
10568 && COMPLEX_MODE_P (GET_MODE (op0))
10569 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10570 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10572 if (reversep)
10573 op0 = flip_storage_order (GET_MODE (op0), op0);
10574 if (mode1 != GET_MODE (op0))
10576 rtx parts[2];
10577 for (int i = 0; i < 2; i++)
10579 rtx op = read_complex_part (op0, i != 0);
10580 if (GET_CODE (op) == SUBREG)
10581 op = force_reg (GET_MODE (op), op);
10582 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10583 op);
10584 if (temp)
10585 op = temp;
10586 else
10588 if (!REG_P (op) && !MEM_P (op))
10589 op = force_reg (GET_MODE (op), op);
10590 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10592 parts[i] = op;
10594 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10596 return op0;
10598 if (known_eq (bitpos, 0)
10599 && known_eq (bitsize,
10600 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10601 && maybe_ne (bitsize, 0))
10603 op0 = XEXP (op0, 0);
10604 mode2 = GET_MODE (op0);
10606 else if (known_eq (bitpos,
10607 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10608 && known_eq (bitsize,
10609 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
10610 && maybe_ne (bitpos, 0)
10611 && maybe_ne (bitsize, 0))
10613 op0 = XEXP (op0, 1);
10614 bitpos = 0;
10615 mode2 = GET_MODE (op0);
10617 else
10618 /* Otherwise force into memory. */
10619 must_force_mem = 1;
10622 /* If this is a constant, put it in a register if it is a legitimate
10623 constant and we don't need a memory reference. */
10624 if (CONSTANT_P (op0)
10625 && mode2 != BLKmode
10626 && targetm.legitimate_constant_p (mode2, op0)
10627 && !must_force_mem)
10628 op0 = force_reg (mode2, op0);
10630 /* Otherwise, if this is a constant, try to force it to the constant
10631 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10632 is a legitimate constant. */
10633 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10634 op0 = validize_mem (memloc);
10636 /* Otherwise, if this is a constant or the object is not in memory
10637 and need be, put it there. */
10638 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10640 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10641 emit_move_insn (memloc, op0);
10642 op0 = memloc;
10643 clear_mem_expr = true;
10646 if (offset)
10648 machine_mode address_mode;
10649 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10650 EXPAND_SUM);
10652 gcc_assert (MEM_P (op0));
10654 address_mode = get_address_mode (op0);
10655 if (GET_MODE (offset_rtx) != address_mode)
10657 /* We cannot be sure that the RTL in offset_rtx is valid outside
10658 of a memory address context, so force it into a register
10659 before attempting to convert it to the desired mode. */
10660 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10661 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10664 /* See the comment in expand_assignment for the rationale. */
10665 if (mode1 != VOIDmode
10666 && maybe_ne (bitpos, 0)
10667 && maybe_gt (bitsize, 0)
10668 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10669 && multiple_p (bitpos, bitsize)
10670 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
10671 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10673 op0 = adjust_address (op0, mode1, bytepos);
10674 bitpos = 0;
10677 op0 = offset_address (op0, offset_rtx,
10678 highest_pow2_factor (offset));
10681 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10682 record its alignment as BIGGEST_ALIGNMENT. */
10683 if (MEM_P (op0)
10684 && known_eq (bitpos, 0)
10685 && offset != 0
10686 && is_aligning_offset (offset, tem))
10687 set_mem_align (op0, BIGGEST_ALIGNMENT);
10689 /* Don't forget about volatility even if this is a bitfield. */
10690 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10692 if (op0 == orig_op0)
10693 op0 = copy_rtx (op0);
10695 MEM_VOLATILE_P (op0) = 1;
10698 /* In cases where an aligned union has an unaligned object
10699 as a field, we might be extracting a BLKmode value from
10700 an integer-mode (e.g., SImode) object. Handle this case
10701 by doing the extract into an object as wide as the field
10702 (which we know to be the width of a basic mode), then
10703 storing into memory, and changing the mode to BLKmode. */
10704 if (mode1 == VOIDmode
10705 || REG_P (op0) || GET_CODE (op0) == SUBREG
10706 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10707 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10708 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10709 && modifier != EXPAND_CONST_ADDRESS
10710 && modifier != EXPAND_INITIALIZER
10711 && modifier != EXPAND_MEMORY)
10712 /* If the bitfield is volatile and the bitsize
10713 is narrower than the access size of the bitfield,
10714 we need to extract bitfields from the access. */
10715 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10716 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10717 && mode1 != BLKmode
10718 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
10719 /* If the field isn't aligned enough to fetch as a memref,
10720 fetch it as a bit field. */
10721 || (mode1 != BLKmode
10722 && (((MEM_P (op0)
10723 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10724 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
10725 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10726 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
10727 && modifier != EXPAND_MEMORY
10728 && ((modifier == EXPAND_CONST_ADDRESS
10729 || modifier == EXPAND_INITIALIZER)
10730 ? STRICT_ALIGNMENT
10731 : targetm.slow_unaligned_access (mode1,
10732 MEM_ALIGN (op0))))
10733 || !multiple_p (bitpos, BITS_PER_UNIT)))
10734 /* If the type and the field are a constant size and the
10735 size of the type isn't the same size as the bitfield,
10736 we must use bitfield operations. */
10737 || (known_size_p (bitsize)
10738 && TYPE_SIZE (TREE_TYPE (exp))
10739 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
10740 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
10741 bitsize)))
10743 machine_mode ext_mode = mode;
10745 if (ext_mode == BLKmode
10746 && ! (target != 0 && MEM_P (op0)
10747 && MEM_P (target)
10748 && multiple_p (bitpos, BITS_PER_UNIT)))
10749 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
10751 if (ext_mode == BLKmode)
10753 if (target == 0)
10754 target = assign_temp (type, 1, 1);
10756 /* ??? Unlike the similar test a few lines below, this one is
10757 very likely obsolete. */
10758 if (known_eq (bitsize, 0))
10759 return target;
10761 /* In this case, BITPOS must start at a byte boundary and
10762 TARGET, if specified, must be a MEM. */
10763 gcc_assert (MEM_P (op0)
10764 && (!target || MEM_P (target)));
10766 bytepos = exact_div (bitpos, BITS_PER_UNIT);
10767 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
10768 emit_block_move (target,
10769 adjust_address (op0, VOIDmode, bytepos),
10770 gen_int_mode (bytesize, Pmode),
10771 (modifier == EXPAND_STACK_PARM
10772 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10774 return target;
10777 /* If we have nothing to extract, the result will be 0 for targets
10778 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10779 return 0 for the sake of consistency, as reading a zero-sized
10780 bitfield is valid in Ada and the value is fully specified. */
10781 if (known_eq (bitsize, 0))
10782 return const0_rtx;
10784 op0 = validize_mem (op0);
10786 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10787 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10789 /* If the result has a record type and the extraction is done in
10790 an integral mode, then the field may be not aligned on a byte
10791 boundary; in this case, if it has reverse storage order, it
10792 needs to be extracted as a scalar field with reverse storage
10793 order and put back into memory order afterwards. */
10794 if (TREE_CODE (type) == RECORD_TYPE
10795 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10796 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10798 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10799 (modifier == EXPAND_STACK_PARM
10800 ? NULL_RTX : target),
10801 ext_mode, ext_mode, reversep, alt_rtl);
10803 /* If the result has a record type and the mode of OP0 is an
10804 integral mode then, if BITSIZE is narrower than this mode
10805 and this is for big-endian data, we must put the field
10806 into the high-order bits. And we must also put it back
10807 into memory order if it has been previously reversed. */
10808 scalar_int_mode op0_mode;
10809 if (TREE_CODE (type) == RECORD_TYPE
10810 && is_int_mode (GET_MODE (op0), &op0_mode))
10812 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10814 gcc_checking_assert (known_le (bitsize, size));
10815 if (maybe_lt (bitsize, size)
10816 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10817 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10818 size - bitsize, op0, 1);
10820 if (reversep)
10821 op0 = flip_storage_order (op0_mode, op0);
10824 /* If the result type is BLKmode, store the data into a temporary
10825 of the appropriate type, but with the mode corresponding to the
10826 mode for the data we have (op0's mode). */
10827 if (mode == BLKmode)
10829 rtx new_rtx
10830 = assign_stack_temp_for_type (ext_mode,
10831 GET_MODE_BITSIZE (ext_mode),
10832 type);
10833 emit_move_insn (new_rtx, op0);
10834 op0 = copy_rtx (new_rtx);
10835 PUT_MODE (op0, BLKmode);
10838 return op0;
10841 /* If the result is BLKmode, use that to access the object
10842 now as well. */
10843 if (mode == BLKmode)
10844 mode1 = BLKmode;
10846 /* Get a reference to just this component. */
10847 bytepos = bits_to_bytes_round_down (bitpos);
10848 if (modifier == EXPAND_CONST_ADDRESS
10849 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10850 op0 = adjust_address_nv (op0, mode1, bytepos);
10851 else
10852 op0 = adjust_address (op0, mode1, bytepos);
10854 if (op0 == orig_op0)
10855 op0 = copy_rtx (op0);
10857 /* Don't set memory attributes if the base expression is
10858 SSA_NAME that got expanded as a MEM. In that case, we should
10859 just honor its original memory attributes. */
10860 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10861 set_mem_attributes (op0, exp, 0);
10863 if (REG_P (XEXP (op0, 0)))
10864 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10866 /* If op0 is a temporary because the original expressions was forced
10867 to memory, clear MEM_EXPR so that the original expression cannot
10868 be marked as addressable through MEM_EXPR of the temporary. */
10869 if (clear_mem_expr)
10870 set_mem_expr (op0, NULL_TREE);
10872 MEM_VOLATILE_P (op0) |= volatilep;
10874 if (reversep
10875 && modifier != EXPAND_MEMORY
10876 && modifier != EXPAND_WRITE)
10877 op0 = flip_storage_order (mode1, op0);
10879 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10880 || modifier == EXPAND_CONST_ADDRESS
10881 || modifier == EXPAND_INITIALIZER)
10882 return op0;
10884 if (target == 0)
10885 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10887 convert_move (target, op0, unsignedp);
10888 return target;
10891 case OBJ_TYPE_REF:
10892 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10894 case CALL_EXPR:
10895 /* All valid uses of __builtin_va_arg_pack () are removed during
10896 inlining. */
10897 if (CALL_EXPR_VA_ARG_PACK (exp))
10898 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10900 tree fndecl = get_callee_fndecl (exp), attr;
10902 if (fndecl
10903 && (attr = lookup_attribute ("error",
10904 DECL_ATTRIBUTES (fndecl))) != NULL)
10905 error ("%Kcall to %qs declared with attribute error: %s",
10906 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10907 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10908 if (fndecl
10909 && (attr = lookup_attribute ("warning",
10910 DECL_ATTRIBUTES (fndecl))) != NULL)
10911 warning_at (tree_nonartificial_location (exp),
10912 0, "%Kcall to %qs declared with attribute warning: %s",
10913 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10914 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10916 /* Check for a built-in function. */
10917 if (fndecl && DECL_BUILT_IN (fndecl))
10919 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10920 if (CALL_WITH_BOUNDS_P (exp))
10921 return expand_builtin_with_bounds (exp, target, subtarget,
10922 tmode, ignore);
10923 else
10924 return expand_builtin (exp, target, subtarget, tmode, ignore);
10927 return expand_call (exp, target, ignore);
10929 case VIEW_CONVERT_EXPR:
10930 op0 = NULL_RTX;
10932 /* If we are converting to BLKmode, try to avoid an intermediate
10933 temporary by fetching an inner memory reference. */
10934 if (mode == BLKmode
10935 && poly_int_tree_p (TYPE_SIZE (type))
10936 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10937 && handled_component_p (treeop0))
10939 machine_mode mode1;
10940 poly_int64 bitsize, bitpos, bytepos;
10941 tree offset;
10942 int unsignedp, reversep, volatilep = 0;
10943 tree tem
10944 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10945 &unsignedp, &reversep, &volatilep);
10946 rtx orig_op0;
10948 /* ??? We should work harder and deal with non-zero offsets. */
10949 if (!offset
10950 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10951 && !reversep
10952 && known_size_p (bitsize)
10953 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
10955 /* See the normal_inner_ref case for the rationale. */
10956 orig_op0
10957 = expand_expr_real (tem,
10958 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10959 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10960 != INTEGER_CST)
10961 && modifier != EXPAND_STACK_PARM
10962 ? target : NULL_RTX),
10963 VOIDmode,
10964 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10965 NULL, true);
10967 if (MEM_P (orig_op0))
10969 op0 = orig_op0;
10971 /* Get a reference to just this component. */
10972 if (modifier == EXPAND_CONST_ADDRESS
10973 || modifier == EXPAND_SUM
10974 || modifier == EXPAND_INITIALIZER)
10975 op0 = adjust_address_nv (op0, mode, bytepos);
10976 else
10977 op0 = adjust_address (op0, mode, bytepos);
10979 if (op0 == orig_op0)
10980 op0 = copy_rtx (op0);
10982 set_mem_attributes (op0, treeop0, 0);
10983 if (REG_P (XEXP (op0, 0)))
10984 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10986 MEM_VOLATILE_P (op0) |= volatilep;
10991 if (!op0)
10992 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10993 NULL, inner_reference_p);
10995 /* If the input and output modes are both the same, we are done. */
10996 if (mode == GET_MODE (op0))
10998 /* If neither mode is BLKmode, and both modes are the same size
10999 then we can use gen_lowpart. */
11000 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
11001 && (GET_MODE_PRECISION (mode)
11002 == GET_MODE_PRECISION (GET_MODE (op0)))
11003 && !COMPLEX_MODE_P (GET_MODE (op0)))
11005 if (GET_CODE (op0) == SUBREG)
11006 op0 = force_reg (GET_MODE (op0), op0);
11007 temp = gen_lowpart_common (mode, op0);
11008 if (temp)
11009 op0 = temp;
11010 else
11012 if (!REG_P (op0) && !MEM_P (op0))
11013 op0 = force_reg (GET_MODE (op0), op0);
11014 op0 = gen_lowpart (mode, op0);
11017 /* If both types are integral, convert from one mode to the other. */
11018 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11019 op0 = convert_modes (mode, GET_MODE (op0), op0,
11020 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11021 /* If the output type is a bit-field type, do an extraction. */
11022 else if (reduce_bit_field)
11023 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11024 TYPE_UNSIGNED (type), NULL_RTX,
11025 mode, mode, false, NULL);
11026 /* As a last resort, spill op0 to memory, and reload it in a
11027 different mode. */
11028 else if (!MEM_P (op0))
11030 /* If the operand is not a MEM, force it into memory. Since we
11031 are going to be changing the mode of the MEM, don't call
11032 force_const_mem for constants because we don't allow pool
11033 constants to change mode. */
11034 tree inner_type = TREE_TYPE (treeop0);
11036 gcc_assert (!TREE_ADDRESSABLE (exp));
11038 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11039 target
11040 = assign_stack_temp_for_type
11041 (TYPE_MODE (inner_type),
11042 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11044 emit_move_insn (target, op0);
11045 op0 = target;
11048 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11049 output type is such that the operand is known to be aligned, indicate
11050 that it is. Otherwise, we need only be concerned about alignment for
11051 non-BLKmode results. */
11052 if (MEM_P (op0))
11054 enum insn_code icode;
11056 if (modifier != EXPAND_WRITE
11057 && modifier != EXPAND_MEMORY
11058 && !inner_reference_p
11059 && mode != BLKmode
11060 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11062 /* If the target does have special handling for unaligned
11063 loads of mode then use them. */
11064 if ((icode = optab_handler (movmisalign_optab, mode))
11065 != CODE_FOR_nothing)
11067 rtx reg;
11069 op0 = adjust_address (op0, mode, 0);
11070 /* We've already validated the memory, and we're creating a
11071 new pseudo destination. The predicates really can't
11072 fail. */
11073 reg = gen_reg_rtx (mode);
11075 /* Nor can the insn generator. */
11076 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11077 emit_insn (insn);
11078 return reg;
11080 else if (STRICT_ALIGNMENT)
11082 tree inner_type = TREE_TYPE (treeop0);
11083 HOST_WIDE_INT temp_size
11084 = MAX (int_size_in_bytes (inner_type),
11085 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
11086 rtx new_rtx
11087 = assign_stack_temp_for_type (mode, temp_size, type);
11088 rtx new_with_op0_mode
11089 = adjust_address (new_rtx, GET_MODE (op0), 0);
11091 gcc_assert (!TREE_ADDRESSABLE (exp));
11093 if (GET_MODE (op0) == BLKmode)
11094 emit_block_move (new_with_op0_mode, op0,
11095 GEN_INT (GET_MODE_SIZE (mode)),
11096 (modifier == EXPAND_STACK_PARM
11097 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11098 else
11099 emit_move_insn (new_with_op0_mode, op0);
11101 op0 = new_rtx;
11105 op0 = adjust_address (op0, mode, 0);
11108 return op0;
11110 case MODIFY_EXPR:
11112 tree lhs = treeop0;
11113 tree rhs = treeop1;
11114 gcc_assert (ignore);
11116 /* Check for |= or &= of a bitfield of size one into another bitfield
11117 of size 1. In this case, (unless we need the result of the
11118 assignment) we can do this more efficiently with a
11119 test followed by an assignment, if necessary.
11121 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11122 things change so we do, this code should be enhanced to
11123 support it. */
11124 if (TREE_CODE (lhs) == COMPONENT_REF
11125 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11126 || TREE_CODE (rhs) == BIT_AND_EXPR)
11127 && TREE_OPERAND (rhs, 0) == lhs
11128 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11129 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11130 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11132 rtx_code_label *label = gen_label_rtx ();
11133 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11134 do_jump (TREE_OPERAND (rhs, 1),
11135 value ? label : 0,
11136 value ? 0 : label,
11137 profile_probability::uninitialized ());
11138 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11139 false);
11140 do_pending_stack_adjust ();
11141 emit_label (label);
11142 return const0_rtx;
11145 expand_assignment (lhs, rhs, false);
11146 return const0_rtx;
11149 case ADDR_EXPR:
11150 return expand_expr_addr_expr (exp, target, tmode, modifier);
11152 case REALPART_EXPR:
11153 op0 = expand_normal (treeop0);
11154 return read_complex_part (op0, false);
11156 case IMAGPART_EXPR:
11157 op0 = expand_normal (treeop0);
11158 return read_complex_part (op0, true);
11160 case RETURN_EXPR:
11161 case LABEL_EXPR:
11162 case GOTO_EXPR:
11163 case SWITCH_EXPR:
11164 case ASM_EXPR:
11165 /* Expanded in cfgexpand.c. */
11166 gcc_unreachable ();
11168 case TRY_CATCH_EXPR:
11169 case CATCH_EXPR:
11170 case EH_FILTER_EXPR:
11171 case TRY_FINALLY_EXPR:
11172 /* Lowered by tree-eh.c. */
11173 gcc_unreachable ();
11175 case WITH_CLEANUP_EXPR:
11176 case CLEANUP_POINT_EXPR:
11177 case TARGET_EXPR:
11178 case CASE_LABEL_EXPR:
11179 case VA_ARG_EXPR:
11180 case BIND_EXPR:
11181 case INIT_EXPR:
11182 case CONJ_EXPR:
11183 case COMPOUND_EXPR:
11184 case PREINCREMENT_EXPR:
11185 case PREDECREMENT_EXPR:
11186 case POSTINCREMENT_EXPR:
11187 case POSTDECREMENT_EXPR:
11188 case LOOP_EXPR:
11189 case EXIT_EXPR:
11190 case COMPOUND_LITERAL_EXPR:
11191 /* Lowered by gimplify.c. */
11192 gcc_unreachable ();
11194 case FDESC_EXPR:
11195 /* Function descriptors are not valid except for as
11196 initialization constants, and should not be expanded. */
11197 gcc_unreachable ();
11199 case WITH_SIZE_EXPR:
11200 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11201 have pulled out the size to use in whatever context it needed. */
11202 return expand_expr_real (treeop0, original_target, tmode,
11203 modifier, alt_rtl, inner_reference_p);
11205 default:
11206 return expand_expr_real_2 (&ops, target, tmode, modifier);
11210 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11211 signedness of TYPE), possibly returning the result in TARGET.
11212 TYPE is known to be a partial integer type. */
11213 static rtx
11214 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11216 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11217 if (target && GET_MODE (target) != GET_MODE (exp))
11218 target = 0;
11219 /* For constant values, reduce using build_int_cst_type. */
11220 if (CONST_INT_P (exp))
11222 HOST_WIDE_INT value = INTVAL (exp);
11223 tree t = build_int_cst_type (type, value);
11224 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11226 else if (TYPE_UNSIGNED (type))
11228 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11229 rtx mask = immed_wide_int_const
11230 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11231 return expand_and (mode, exp, mask, target);
11233 else
11235 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11236 int count = GET_MODE_PRECISION (mode) - prec;
11237 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11238 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11242 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11243 when applied to the address of EXP produces an address known to be
11244 aligned more than BIGGEST_ALIGNMENT. */
11246 static int
11247 is_aligning_offset (const_tree offset, const_tree exp)
11249 /* Strip off any conversions. */
11250 while (CONVERT_EXPR_P (offset))
11251 offset = TREE_OPERAND (offset, 0);
11253 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11254 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11255 if (TREE_CODE (offset) != BIT_AND_EXPR
11256 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11257 || compare_tree_int (TREE_OPERAND (offset, 1),
11258 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11259 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11260 return 0;
11262 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11263 It must be NEGATE_EXPR. Then strip any more conversions. */
11264 offset = TREE_OPERAND (offset, 0);
11265 while (CONVERT_EXPR_P (offset))
11266 offset = TREE_OPERAND (offset, 0);
11268 if (TREE_CODE (offset) != NEGATE_EXPR)
11269 return 0;
11271 offset = TREE_OPERAND (offset, 0);
11272 while (CONVERT_EXPR_P (offset))
11273 offset = TREE_OPERAND (offset, 0);
11275 /* This must now be the address of EXP. */
11276 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11279 /* Return the tree node if an ARG corresponds to a string constant or zero
11280 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11281 in bytes within the string that ARG is accessing. The type of the
11282 offset will be `sizetype'. */
11284 tree
11285 string_constant (tree arg, tree *ptr_offset)
11287 tree array, offset, lower_bound;
11288 STRIP_NOPS (arg);
11290 if (TREE_CODE (arg) == ADDR_EXPR)
11292 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11294 *ptr_offset = size_zero_node;
11295 return TREE_OPERAND (arg, 0);
11297 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11299 array = TREE_OPERAND (arg, 0);
11300 offset = size_zero_node;
11302 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11304 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11305 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11306 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11307 return 0;
11309 /* Check if the array has a nonzero lower bound. */
11310 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11311 if (!integer_zerop (lower_bound))
11313 /* If the offset and base aren't both constants, return 0. */
11314 if (TREE_CODE (lower_bound) != INTEGER_CST)
11315 return 0;
11316 if (TREE_CODE (offset) != INTEGER_CST)
11317 return 0;
11318 /* Adjust offset by the lower bound. */
11319 offset = size_diffop (fold_convert (sizetype, offset),
11320 fold_convert (sizetype, lower_bound));
11323 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11325 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11326 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11327 if (TREE_CODE (array) != ADDR_EXPR)
11328 return 0;
11329 array = TREE_OPERAND (array, 0);
11330 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11331 return 0;
11333 else
11334 return 0;
11336 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11338 tree arg0 = TREE_OPERAND (arg, 0);
11339 tree arg1 = TREE_OPERAND (arg, 1);
11341 STRIP_NOPS (arg0);
11342 STRIP_NOPS (arg1);
11344 if (TREE_CODE (arg0) == ADDR_EXPR
11345 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11346 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11348 array = TREE_OPERAND (arg0, 0);
11349 offset = arg1;
11351 else if (TREE_CODE (arg1) == ADDR_EXPR
11352 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11353 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11355 array = TREE_OPERAND (arg1, 0);
11356 offset = arg0;
11358 else
11359 return 0;
11361 else
11362 return 0;
11364 if (TREE_CODE (array) == STRING_CST)
11366 *ptr_offset = fold_convert (sizetype, offset);
11367 return array;
11369 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11371 int length;
11372 tree init = ctor_for_folding (array);
11374 /* Variables initialized to string literals can be handled too. */
11375 if (init == error_mark_node
11376 || !init
11377 || TREE_CODE (init) != STRING_CST)
11378 return 0;
11380 /* Avoid const char foo[4] = "abcde"; */
11381 if (DECL_SIZE_UNIT (array) == NULL_TREE
11382 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11383 || (length = TREE_STRING_LENGTH (init)) <= 0
11384 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11385 return 0;
11387 /* If variable is bigger than the string literal, OFFSET must be constant
11388 and inside of the bounds of the string literal. */
11389 offset = fold_convert (sizetype, offset);
11390 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11391 && (! tree_fits_uhwi_p (offset)
11392 || compare_tree_int (offset, length) >= 0))
11393 return 0;
11395 *ptr_offset = offset;
11396 return init;
11399 return 0;
11402 /* Generate code to calculate OPS, and exploded expression
11403 using a store-flag instruction and return an rtx for the result.
11404 OPS reflects a comparison.
11406 If TARGET is nonzero, store the result there if convenient.
11408 Return zero if there is no suitable set-flag instruction
11409 available on this machine.
11411 Once expand_expr has been called on the arguments of the comparison,
11412 we are committed to doing the store flag, since it is not safe to
11413 re-evaluate the expression. We emit the store-flag insn by calling
11414 emit_store_flag, but only expand the arguments if we have a reason
11415 to believe that emit_store_flag will be successful. If we think that
11416 it will, but it isn't, we have to simulate the store-flag with a
11417 set/jump/set sequence. */
11419 static rtx
11420 do_store_flag (sepops ops, rtx target, machine_mode mode)
11422 enum rtx_code code;
11423 tree arg0, arg1, type;
11424 machine_mode operand_mode;
11425 int unsignedp;
11426 rtx op0, op1;
11427 rtx subtarget = target;
11428 location_t loc = ops->location;
11430 arg0 = ops->op0;
11431 arg1 = ops->op1;
11433 /* Don't crash if the comparison was erroneous. */
11434 if (arg0 == error_mark_node || arg1 == error_mark_node)
11435 return const0_rtx;
11437 type = TREE_TYPE (arg0);
11438 operand_mode = TYPE_MODE (type);
11439 unsignedp = TYPE_UNSIGNED (type);
11441 /* We won't bother with BLKmode store-flag operations because it would mean
11442 passing a lot of information to emit_store_flag. */
11443 if (operand_mode == BLKmode)
11444 return 0;
11446 /* We won't bother with store-flag operations involving function pointers
11447 when function pointers must be canonicalized before comparisons. */
11448 if (targetm.have_canonicalize_funcptr_for_compare ()
11449 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11450 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11451 == FUNCTION_TYPE))
11452 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11453 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11454 == FUNCTION_TYPE))))
11455 return 0;
11457 STRIP_NOPS (arg0);
11458 STRIP_NOPS (arg1);
11460 /* For vector typed comparisons emit code to generate the desired
11461 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11462 expander for this. */
11463 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11465 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11466 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11467 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11468 return expand_vec_cmp_expr (ops->type, ifexp, target);
11469 else
11471 tree if_true = constant_boolean_node (true, ops->type);
11472 tree if_false = constant_boolean_node (false, ops->type);
11473 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11474 if_false, target);
11478 /* Get the rtx comparison code to use. We know that EXP is a comparison
11479 operation of some type. Some comparisons against 1 and -1 can be
11480 converted to comparisons with zero. Do so here so that the tests
11481 below will be aware that we have a comparison with zero. These
11482 tests will not catch constants in the first operand, but constants
11483 are rarely passed as the first operand. */
11485 switch (ops->code)
11487 case EQ_EXPR:
11488 code = EQ;
11489 break;
11490 case NE_EXPR:
11491 code = NE;
11492 break;
11493 case LT_EXPR:
11494 if (integer_onep (arg1))
11495 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11496 else
11497 code = unsignedp ? LTU : LT;
11498 break;
11499 case LE_EXPR:
11500 if (! unsignedp && integer_all_onesp (arg1))
11501 arg1 = integer_zero_node, code = LT;
11502 else
11503 code = unsignedp ? LEU : LE;
11504 break;
11505 case GT_EXPR:
11506 if (! unsignedp && integer_all_onesp (arg1))
11507 arg1 = integer_zero_node, code = GE;
11508 else
11509 code = unsignedp ? GTU : GT;
11510 break;
11511 case GE_EXPR:
11512 if (integer_onep (arg1))
11513 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11514 else
11515 code = unsignedp ? GEU : GE;
11516 break;
11518 case UNORDERED_EXPR:
11519 code = UNORDERED;
11520 break;
11521 case ORDERED_EXPR:
11522 code = ORDERED;
11523 break;
11524 case UNLT_EXPR:
11525 code = UNLT;
11526 break;
11527 case UNLE_EXPR:
11528 code = UNLE;
11529 break;
11530 case UNGT_EXPR:
11531 code = UNGT;
11532 break;
11533 case UNGE_EXPR:
11534 code = UNGE;
11535 break;
11536 case UNEQ_EXPR:
11537 code = UNEQ;
11538 break;
11539 case LTGT_EXPR:
11540 code = LTGT;
11541 break;
11543 default:
11544 gcc_unreachable ();
11547 /* Put a constant second. */
11548 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11549 || TREE_CODE (arg0) == FIXED_CST)
11551 std::swap (arg0, arg1);
11552 code = swap_condition (code);
11555 /* If this is an equality or inequality test of a single bit, we can
11556 do this by shifting the bit being tested to the low-order bit and
11557 masking the result with the constant 1. If the condition was EQ,
11558 we xor it with 1. This does not require an scc insn and is faster
11559 than an scc insn even if we have it.
11561 The code to make this transformation was moved into fold_single_bit_test,
11562 so we just call into the folder and expand its result. */
11564 if ((code == NE || code == EQ)
11565 && integer_zerop (arg1)
11566 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11568 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11569 if (srcstmt
11570 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11572 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11573 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11574 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11575 gimple_assign_rhs1 (srcstmt),
11576 gimple_assign_rhs2 (srcstmt));
11577 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11578 if (temp)
11579 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11583 if (! get_subtarget (target)
11584 || GET_MODE (subtarget) != operand_mode)
11585 subtarget = 0;
11587 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11589 if (target == 0)
11590 target = gen_reg_rtx (mode);
11592 /* Try a cstore if possible. */
11593 return emit_store_flag_force (target, code, op0, op1,
11594 operand_mode, unsignedp,
11595 (TYPE_PRECISION (ops->type) == 1
11596 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11599 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11600 0 otherwise (i.e. if there is no casesi instruction).
11602 DEFAULT_PROBABILITY is the probability of jumping to the default
11603 label. */
11605 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11606 rtx table_label, rtx default_label, rtx fallback_label,
11607 profile_probability default_probability)
11609 struct expand_operand ops[5];
11610 scalar_int_mode index_mode = SImode;
11611 rtx op1, op2, index;
11613 if (! targetm.have_casesi ())
11614 return 0;
11616 /* The index must be some form of integer. Convert it to SImode. */
11617 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
11618 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
11620 rtx rangertx = expand_normal (range);
11622 /* We must handle the endpoints in the original mode. */
11623 index_expr = build2 (MINUS_EXPR, index_type,
11624 index_expr, minval);
11625 minval = integer_zero_node;
11626 index = expand_normal (index_expr);
11627 if (default_label)
11628 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11629 omode, 1, default_label,
11630 default_probability);
11631 /* Now we can safely truncate. */
11632 index = convert_to_mode (index_mode, index, 0);
11634 else
11636 if (omode != index_mode)
11638 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11639 index_expr = fold_convert (index_type, index_expr);
11642 index = expand_normal (index_expr);
11645 do_pending_stack_adjust ();
11647 op1 = expand_normal (minval);
11648 op2 = expand_normal (range);
11650 create_input_operand (&ops[0], index, index_mode);
11651 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11652 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11653 create_fixed_operand (&ops[3], table_label);
11654 create_fixed_operand (&ops[4], (default_label
11655 ? default_label
11656 : fallback_label));
11657 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11658 return 1;
11661 /* Attempt to generate a tablejump instruction; same concept. */
11662 /* Subroutine of the next function.
11664 INDEX is the value being switched on, with the lowest value
11665 in the table already subtracted.
11666 MODE is its expected mode (needed if INDEX is constant).
11667 RANGE is the length of the jump table.
11668 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11670 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11671 index value is out of range.
11672 DEFAULT_PROBABILITY is the probability of jumping to
11673 the default label. */
11675 static void
11676 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11677 rtx default_label, profile_probability default_probability)
11679 rtx temp, vector;
11681 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11682 cfun->cfg->max_jumptable_ents = INTVAL (range);
11684 /* Do an unsigned comparison (in the proper mode) between the index
11685 expression and the value which represents the length of the range.
11686 Since we just finished subtracting the lower bound of the range
11687 from the index expression, this comparison allows us to simultaneously
11688 check that the original index expression value is both greater than
11689 or equal to the minimum value of the range and less than or equal to
11690 the maximum value of the range. */
11692 if (default_label)
11693 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11694 default_label, default_probability);
11697 /* If index is in range, it must fit in Pmode.
11698 Convert to Pmode so we can index with it. */
11699 if (mode != Pmode)
11700 index = convert_to_mode (Pmode, index, 1);
11702 /* Don't let a MEM slip through, because then INDEX that comes
11703 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11704 and break_out_memory_refs will go to work on it and mess it up. */
11705 #ifdef PIC_CASE_VECTOR_ADDRESS
11706 if (flag_pic && !REG_P (index))
11707 index = copy_to_mode_reg (Pmode, index);
11708 #endif
11710 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11711 GET_MODE_SIZE, because this indicates how large insns are. The other
11712 uses should all be Pmode, because they are addresses. This code
11713 could fail if addresses and insns are not the same size. */
11714 index = simplify_gen_binary (MULT, Pmode, index,
11715 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11716 Pmode));
11717 index = simplify_gen_binary (PLUS, Pmode, index,
11718 gen_rtx_LABEL_REF (Pmode, table_label));
11720 #ifdef PIC_CASE_VECTOR_ADDRESS
11721 if (flag_pic)
11722 index = PIC_CASE_VECTOR_ADDRESS (index);
11723 else
11724 #endif
11725 index = memory_address (CASE_VECTOR_MODE, index);
11726 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11727 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11728 convert_move (temp, vector, 0);
11730 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11732 /* If we are generating PIC code or if the table is PC-relative, the
11733 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11734 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11735 emit_barrier ();
11739 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11740 rtx table_label, rtx default_label,
11741 profile_probability default_probability)
11743 rtx index;
11745 if (! targetm.have_tablejump ())
11746 return 0;
11748 index_expr = fold_build2 (MINUS_EXPR, index_type,
11749 fold_convert (index_type, index_expr),
11750 fold_convert (index_type, minval));
11751 index = expand_normal (index_expr);
11752 do_pending_stack_adjust ();
11754 do_tablejump (index, TYPE_MODE (index_type),
11755 convert_modes (TYPE_MODE (index_type),
11756 TYPE_MODE (TREE_TYPE (range)),
11757 expand_normal (range),
11758 TYPE_UNSIGNED (TREE_TYPE (range))),
11759 table_label, default_label, default_probability);
11760 return 1;
11763 /* Return a CONST_VECTOR rtx representing vector mask for
11764 a VECTOR_CST of booleans. */
11765 static rtx
11766 const_vector_mask_from_tree (tree exp)
11768 rtvec v;
11769 unsigned i, units;
11770 tree elt;
11771 machine_mode inner, mode;
11773 mode = TYPE_MODE (TREE_TYPE (exp));
11774 units = VECTOR_CST_NELTS (exp);
11775 inner = GET_MODE_INNER (mode);
11777 v = rtvec_alloc (units);
11779 for (i = 0; i < units; ++i)
11781 elt = VECTOR_CST_ELT (exp, i);
11783 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11784 if (integer_zerop (elt))
11785 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11786 else if (integer_onep (elt)
11787 || integer_minus_onep (elt))
11788 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11789 else
11790 gcc_unreachable ();
11793 return gen_rtx_CONST_VECTOR (mode, v);
11796 /* EXP is a VECTOR_CST in which each element is either all-zeros or all-ones.
11797 Return a constant scalar rtx of mode MODE in which bit X is set if element
11798 X of EXP is nonzero. */
11799 static rtx
11800 const_scalar_mask_from_tree (scalar_int_mode mode, tree exp)
11802 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11803 tree elt;
11804 unsigned i;
11806 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11808 elt = VECTOR_CST_ELT (exp, i);
11809 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11810 if (integer_all_onesp (elt))
11811 res = wi::set_bit (res, i);
11812 else
11813 gcc_assert (integer_zerop (elt));
11816 return immed_wide_int_const (res, mode);
11819 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11820 static rtx
11821 const_vector_from_tree (tree exp)
11823 rtvec v;
11824 unsigned i, units;
11825 tree elt;
11826 machine_mode inner, mode;
11828 mode = TYPE_MODE (TREE_TYPE (exp));
11830 if (initializer_zerop (exp))
11831 return CONST0_RTX (mode);
11833 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11834 return const_vector_mask_from_tree (exp);
11836 units = VECTOR_CST_NELTS (exp);
11837 inner = GET_MODE_INNER (mode);
11839 v = rtvec_alloc (units);
11841 for (i = 0; i < units; ++i)
11843 elt = VECTOR_CST_ELT (exp, i);
11845 if (TREE_CODE (elt) == REAL_CST)
11846 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11847 inner);
11848 else if (TREE_CODE (elt) == FIXED_CST)
11849 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11850 inner);
11851 else
11852 RTVEC_ELT (v, i) = immed_wide_int_const (wi::to_poly_wide (elt),
11853 inner);
11856 return gen_rtx_CONST_VECTOR (mode, v);
11859 /* Build a decl for a personality function given a language prefix. */
11861 tree
11862 build_personality_function (const char *lang)
11864 const char *unwind_and_version;
11865 tree decl, type;
11866 char *name;
11868 switch (targetm_common.except_unwind_info (&global_options))
11870 case UI_NONE:
11871 return NULL;
11872 case UI_SJLJ:
11873 unwind_and_version = "_sj0";
11874 break;
11875 case UI_DWARF2:
11876 case UI_TARGET:
11877 unwind_and_version = "_v0";
11878 break;
11879 case UI_SEH:
11880 unwind_and_version = "_seh0";
11881 break;
11882 default:
11883 gcc_unreachable ();
11886 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11888 type = build_function_type_list (integer_type_node, integer_type_node,
11889 long_long_unsigned_type_node,
11890 ptr_type_node, ptr_type_node, NULL_TREE);
11891 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11892 get_identifier (name), type);
11893 DECL_ARTIFICIAL (decl) = 1;
11894 DECL_EXTERNAL (decl) = 1;
11895 TREE_PUBLIC (decl) = 1;
11897 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11898 are the flags assigned by targetm.encode_section_info. */
11899 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11901 return decl;
11904 /* Extracts the personality function of DECL and returns the corresponding
11905 libfunc. */
11908 get_personality_function (tree decl)
11910 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11911 enum eh_personality_kind pk;
11913 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11914 if (pk == eh_personality_none)
11915 return NULL;
11917 if (!personality
11918 && pk == eh_personality_any)
11919 personality = lang_hooks.eh_personality ();
11921 if (pk == eh_personality_lang)
11922 gcc_assert (personality != NULL_TREE);
11924 return XEXP (DECL_RTL (personality), 0);
11927 /* Returns a tree for the size of EXP in bytes. */
11929 static tree
11930 tree_expr_size (const_tree exp)
11932 if (DECL_P (exp)
11933 && DECL_SIZE_UNIT (exp) != 0)
11934 return DECL_SIZE_UNIT (exp);
11935 else
11936 return size_in_bytes (TREE_TYPE (exp));
11939 /* Return an rtx for the size in bytes of the value of EXP. */
11942 expr_size (tree exp)
11944 tree size;
11946 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11947 size = TREE_OPERAND (exp, 1);
11948 else
11950 size = tree_expr_size (exp);
11951 gcc_assert (size);
11952 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11955 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11958 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11959 if the size can vary or is larger than an integer. */
11961 static HOST_WIDE_INT
11962 int_expr_size (tree exp)
11964 tree size;
11966 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11967 size = TREE_OPERAND (exp, 1);
11968 else
11970 size = tree_expr_size (exp);
11971 gcc_assert (size);
11974 if (size == 0 || !tree_fits_shwi_p (size))
11975 return -1;
11977 return tree_to_shwi (size);