Daily bump.
[official-gcc.git] / gcc / expr.c
blob00660293f72e5441a6421a280b04c57fca2922b8
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2018 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"
64 #include "rtx-vector-builder.h"
67 /* If this is nonzero, we do not bother generating VOLATILE
68 around volatile memory references, and we are willing to
69 output indirect addresses. If cse is to follow, we reject
70 indirect addresses so a useful potential cse is generated;
71 if it is used only once, instruction combination will produce
72 the same indirect address eventually. */
73 int cse_not_expected;
75 static bool block_move_libcall_safe_for_call_parm (void);
76 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
78 unsigned HOST_WIDE_INT);
79 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
80 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
81 static rtx_insn *compress_float_constant (rtx, rtx);
82 static rtx get_subtarget (rtx);
83 static void store_constructor (tree, rtx, int, poly_int64, bool);
84 static rtx store_field (rtx, poly_int64, poly_int64, poly_uint64, poly_uint64,
85 machine_mode, tree, alias_set_type, bool, bool);
87 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
89 static int is_aligning_offset (const_tree, const_tree);
90 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
91 static rtx do_store_flag (sepops, rtx, machine_mode);
92 #ifdef PUSH_ROUNDING
93 static void emit_single_push_insn (machine_mode, rtx, tree);
94 #endif
95 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx,
96 profile_probability);
97 static rtx const_vector_from_tree (tree);
98 static rtx const_scalar_mask_from_tree (scalar_int_mode, tree);
99 static tree tree_expr_size (const_tree);
100 static HOST_WIDE_INT int_expr_size (tree);
101 static void convert_mode_scalar (rtx, rtx, int);
104 /* This is run to set up which modes can be used
105 directly in memory and to initialize the block move optab. It is run
106 at the beginning of compilation and when the target is reinitialized. */
108 void
109 init_expr_target (void)
111 rtx pat;
112 int num_clobbers;
113 rtx mem, mem1;
114 rtx reg;
116 /* Try indexing by frame ptr and try by stack ptr.
117 It is known that on the Convex the stack ptr isn't a valid index.
118 With luck, one or the other is valid on any machine. */
119 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
120 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
122 /* A scratch register we can modify in-place below to avoid
123 useless RTL allocations. */
124 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
126 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
127 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
128 PATTERN (insn) = pat;
130 for (machine_mode mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
131 mode = (machine_mode) ((int) mode + 1))
133 int regno;
135 direct_load[(int) mode] = direct_store[(int) mode] = 0;
136 PUT_MODE (mem, mode);
137 PUT_MODE (mem1, mode);
139 /* See if there is some register that can be used in this mode and
140 directly loaded or stored from memory. */
142 if (mode != VOIDmode && mode != BLKmode)
143 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
144 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
145 regno++)
147 if (!targetm.hard_regno_mode_ok (regno, mode))
148 continue;
150 set_mode_and_regno (reg, mode, regno);
152 SET_SRC (pat) = mem;
153 SET_DEST (pat) = reg;
154 if (recog (pat, insn, &num_clobbers) >= 0)
155 direct_load[(int) mode] = 1;
157 SET_SRC (pat) = mem1;
158 SET_DEST (pat) = reg;
159 if (recog (pat, insn, &num_clobbers) >= 0)
160 direct_load[(int) mode] = 1;
162 SET_SRC (pat) = reg;
163 SET_DEST (pat) = mem;
164 if (recog (pat, insn, &num_clobbers) >= 0)
165 direct_store[(int) mode] = 1;
167 SET_SRC (pat) = reg;
168 SET_DEST (pat) = mem1;
169 if (recog (pat, insn, &num_clobbers) >= 0)
170 direct_store[(int) mode] = 1;
174 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
176 opt_scalar_float_mode mode_iter;
177 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
179 scalar_float_mode mode = mode_iter.require ();
180 scalar_float_mode srcmode;
181 FOR_EACH_MODE_UNTIL (srcmode, mode)
183 enum insn_code ic;
185 ic = can_extend_p (mode, srcmode, 0);
186 if (ic == CODE_FOR_nothing)
187 continue;
189 PUT_MODE (mem, srcmode);
191 if (insn_operand_matches (ic, 1, mem))
192 float_extend_from_mem[mode][srcmode] = true;
197 /* This is run at the start of compiling a function. */
199 void
200 init_expr (void)
202 memset (&crtl->expr, 0, sizeof (crtl->expr));
205 /* Copy data from FROM to TO, where the machine modes are not the same.
206 Both modes may be integer, or both may be floating, or both may be
207 fixed-point.
208 UNSIGNEDP should be nonzero if FROM is an unsigned type.
209 This causes zero-extension instead of sign-extension. */
211 void
212 convert_move (rtx to, rtx from, int unsignedp)
214 machine_mode to_mode = GET_MODE (to);
215 machine_mode from_mode = GET_MODE (from);
217 gcc_assert (to_mode != BLKmode);
218 gcc_assert (from_mode != BLKmode);
220 /* If the source and destination are already the same, then there's
221 nothing to do. */
222 if (to == from)
223 return;
225 /* If FROM is a SUBREG that indicates that we have already done at least
226 the required extension, strip it. We don't handle such SUBREGs as
227 TO here. */
229 scalar_int_mode to_int_mode;
230 if (GET_CODE (from) == SUBREG
231 && SUBREG_PROMOTED_VAR_P (from)
232 && is_a <scalar_int_mode> (to_mode, &to_int_mode)
233 && (GET_MODE_PRECISION (subreg_promoted_mode (from))
234 >= GET_MODE_PRECISION (to_int_mode))
235 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
236 from = gen_lowpart (to_int_mode, from), from_mode = to_int_mode;
238 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
240 if (to_mode == from_mode
241 || (from_mode == VOIDmode && CONSTANT_P (from)))
243 emit_move_insn (to, from);
244 return;
247 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
249 gcc_assert (known_eq (GET_MODE_BITSIZE (from_mode),
250 GET_MODE_BITSIZE (to_mode)));
252 if (VECTOR_MODE_P (to_mode))
253 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
254 else
255 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
257 emit_move_insn (to, from);
258 return;
261 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
263 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
264 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
265 return;
268 convert_mode_scalar (to, from, unsignedp);
271 /* Like convert_move, but deals only with scalar modes. */
273 static void
274 convert_mode_scalar (rtx to, rtx from, int unsignedp)
276 /* Both modes should be scalar types. */
277 scalar_mode from_mode = as_a <scalar_mode> (GET_MODE (from));
278 scalar_mode to_mode = as_a <scalar_mode> (GET_MODE (to));
279 bool to_real = SCALAR_FLOAT_MODE_P (to_mode);
280 bool from_real = SCALAR_FLOAT_MODE_P (from_mode);
281 enum insn_code code;
282 rtx libcall;
284 gcc_assert (to_real == from_real);
286 /* rtx code for making an equivalent value. */
287 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
288 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
290 if (to_real)
292 rtx value;
293 rtx_insn *insns;
294 convert_optab tab;
296 gcc_assert ((GET_MODE_PRECISION (from_mode)
297 != GET_MODE_PRECISION (to_mode))
298 || (DECIMAL_FLOAT_MODE_P (from_mode)
299 != DECIMAL_FLOAT_MODE_P (to_mode)));
301 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
302 /* Conversion between decimal float and binary float, same size. */
303 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
304 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
305 tab = sext_optab;
306 else
307 tab = trunc_optab;
309 /* Try converting directly if the insn is supported. */
311 code = convert_optab_handler (tab, to_mode, from_mode);
312 if (code != CODE_FOR_nothing)
314 emit_unop_insn (code, to, from,
315 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
316 return;
319 /* Otherwise use a libcall. */
320 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
322 /* Is this conversion implemented yet? */
323 gcc_assert (libcall);
325 start_sequence ();
326 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
327 from, from_mode);
328 insns = get_insns ();
329 end_sequence ();
330 emit_libcall_block (insns, to, value,
331 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
332 from)
333 : gen_rtx_FLOAT_EXTEND (to_mode, from));
334 return;
337 /* Handle pointer conversion. */ /* SPEE 900220. */
338 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
340 convert_optab ctab;
342 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
343 ctab = trunc_optab;
344 else if (unsignedp)
345 ctab = zext_optab;
346 else
347 ctab = sext_optab;
349 if (convert_optab_handler (ctab, to_mode, from_mode)
350 != CODE_FOR_nothing)
352 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
353 to, from, UNKNOWN);
354 return;
358 /* Targets are expected to provide conversion insns between PxImode and
359 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
360 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
362 scalar_int_mode full_mode
363 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
365 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
366 != CODE_FOR_nothing);
368 if (full_mode != from_mode)
369 from = convert_to_mode (full_mode, from, unsignedp);
370 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
371 to, from, UNKNOWN);
372 return;
374 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
376 rtx new_from;
377 scalar_int_mode full_mode
378 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
379 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
380 enum insn_code icode;
382 icode = convert_optab_handler (ctab, full_mode, from_mode);
383 gcc_assert (icode != CODE_FOR_nothing);
385 if (to_mode == full_mode)
387 emit_unop_insn (icode, to, from, UNKNOWN);
388 return;
391 new_from = gen_reg_rtx (full_mode);
392 emit_unop_insn (icode, new_from, from, UNKNOWN);
394 /* else proceed to integer conversions below. */
395 from_mode = full_mode;
396 from = new_from;
399 /* Make sure both are fixed-point modes or both are not. */
400 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
401 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
402 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
404 /* If we widen from_mode to to_mode and they are in the same class,
405 we won't saturate the result.
406 Otherwise, always saturate the result to play safe. */
407 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
408 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
409 expand_fixed_convert (to, from, 0, 0);
410 else
411 expand_fixed_convert (to, from, 0, 1);
412 return;
415 /* Now both modes are integers. */
417 /* Handle expanding beyond a word. */
418 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
419 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
421 rtx_insn *insns;
422 rtx lowpart;
423 rtx fill_value;
424 rtx lowfrom;
425 int i;
426 scalar_mode lowpart_mode;
427 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
429 /* Try converting directly if the insn is supported. */
430 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
431 != CODE_FOR_nothing)
433 /* If FROM is a SUBREG, put it into a register. Do this
434 so that we always generate the same set of insns for
435 better cse'ing; if an intermediate assignment occurred,
436 we won't be doing the operation directly on the SUBREG. */
437 if (optimize > 0 && GET_CODE (from) == SUBREG)
438 from = force_reg (from_mode, from);
439 emit_unop_insn (code, to, from, equiv_code);
440 return;
442 /* Next, try converting via full word. */
443 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
444 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
445 != CODE_FOR_nothing))
447 rtx word_to = gen_reg_rtx (word_mode);
448 if (REG_P (to))
450 if (reg_overlap_mentioned_p (to, from))
451 from = force_reg (from_mode, from);
452 emit_clobber (to);
454 convert_move (word_to, from, unsignedp);
455 emit_unop_insn (code, to, word_to, equiv_code);
456 return;
459 /* No special multiword conversion insn; do it by hand. */
460 start_sequence ();
462 /* Since we will turn this into a no conflict block, we must ensure
463 the source does not overlap the target so force it into an isolated
464 register when maybe so. Likewise for any MEM input, since the
465 conversion sequence might require several references to it and we
466 must ensure we're getting the same value every time. */
468 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
469 from = force_reg (from_mode, from);
471 /* Get a copy of FROM widened to a word, if necessary. */
472 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
473 lowpart_mode = word_mode;
474 else
475 lowpart_mode = from_mode;
477 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
479 lowpart = gen_lowpart (lowpart_mode, to);
480 emit_move_insn (lowpart, lowfrom);
482 /* Compute the value to put in each remaining word. */
483 if (unsignedp)
484 fill_value = const0_rtx;
485 else
486 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
487 LT, lowfrom, const0_rtx,
488 lowpart_mode, 0, -1);
490 /* Fill the remaining words. */
491 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
493 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
494 rtx subword = operand_subword (to, index, 1, to_mode);
496 gcc_assert (subword);
498 if (fill_value != subword)
499 emit_move_insn (subword, fill_value);
502 insns = get_insns ();
503 end_sequence ();
505 emit_insn (insns);
506 return;
509 /* Truncating multi-word to a word or less. */
510 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
511 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
513 if (!((MEM_P (from)
514 && ! MEM_VOLATILE_P (from)
515 && direct_load[(int) to_mode]
516 && ! mode_dependent_address_p (XEXP (from, 0),
517 MEM_ADDR_SPACE (from)))
518 || REG_P (from)
519 || GET_CODE (from) == SUBREG))
520 from = force_reg (from_mode, from);
521 convert_move (to, gen_lowpart (word_mode, from), 0);
522 return;
525 /* Now follow all the conversions between integers
526 no more than a word long. */
528 /* For truncation, usually we can just refer to FROM in a narrower mode. */
529 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
530 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
532 if (!((MEM_P (from)
533 && ! MEM_VOLATILE_P (from)
534 && direct_load[(int) to_mode]
535 && ! mode_dependent_address_p (XEXP (from, 0),
536 MEM_ADDR_SPACE (from)))
537 || REG_P (from)
538 || GET_CODE (from) == SUBREG))
539 from = force_reg (from_mode, from);
540 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
541 && !targetm.hard_regno_mode_ok (REGNO (from), to_mode))
542 from = copy_to_reg (from);
543 emit_move_insn (to, gen_lowpart (to_mode, from));
544 return;
547 /* Handle extension. */
548 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
550 /* Convert directly if that works. */
551 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
552 != CODE_FOR_nothing)
554 emit_unop_insn (code, to, from, equiv_code);
555 return;
557 else
559 scalar_mode intermediate;
560 rtx tmp;
561 int shift_amount;
563 /* Search for a mode to convert via. */
564 opt_scalar_mode intermediate_iter;
565 FOR_EACH_MODE_FROM (intermediate_iter, from_mode)
567 scalar_mode intermediate = intermediate_iter.require ();
568 if (((can_extend_p (to_mode, intermediate, unsignedp)
569 != CODE_FOR_nothing)
570 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
571 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode,
572 intermediate)))
573 && (can_extend_p (intermediate, from_mode, unsignedp)
574 != CODE_FOR_nothing))
576 convert_move (to, convert_to_mode (intermediate, from,
577 unsignedp), unsignedp);
578 return;
582 /* No suitable intermediate mode.
583 Generate what we need with shifts. */
584 shift_amount = (GET_MODE_PRECISION (to_mode)
585 - GET_MODE_PRECISION (from_mode));
586 from = gen_lowpart (to_mode, force_reg (from_mode, from));
587 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
588 to, unsignedp);
589 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
590 to, unsignedp);
591 if (tmp != to)
592 emit_move_insn (to, tmp);
593 return;
597 /* Support special truncate insns for certain modes. */
598 if (convert_optab_handler (trunc_optab, to_mode,
599 from_mode) != CODE_FOR_nothing)
601 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
602 to, from, UNKNOWN);
603 return;
606 /* Handle truncation of volatile memrefs, and so on;
607 the things that couldn't be truncated directly,
608 and for which there was no special instruction.
610 ??? Code above formerly short-circuited this, for most integer
611 mode pairs, with a force_reg in from_mode followed by a recursive
612 call to this routine. Appears always to have been wrong. */
613 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
615 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
616 emit_move_insn (to, temp);
617 return;
620 /* Mode combination is not recognized. */
621 gcc_unreachable ();
624 /* Return an rtx for a value that would result
625 from converting X to mode MODE.
626 Both X and MODE may be floating, or both integer.
627 UNSIGNEDP is nonzero if X is an unsigned value.
628 This can be done by referring to a part of X in place
629 or by copying to a new temporary with conversion. */
632 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
634 return convert_modes (mode, VOIDmode, x, unsignedp);
637 /* Return an rtx for a value that would result
638 from converting X from mode OLDMODE to mode MODE.
639 Both modes may be floating, or both integer.
640 UNSIGNEDP is nonzero if X is an unsigned value.
642 This can be done by referring to a part of X in place
643 or by copying to a new temporary with conversion.
645 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
648 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
650 rtx temp;
651 scalar_int_mode int_mode;
653 /* If FROM is a SUBREG that indicates that we have already done at least
654 the required extension, strip it. */
656 if (GET_CODE (x) == SUBREG
657 && SUBREG_PROMOTED_VAR_P (x)
658 && is_a <scalar_int_mode> (mode, &int_mode)
659 && (GET_MODE_PRECISION (subreg_promoted_mode (x))
660 >= GET_MODE_PRECISION (int_mode))
661 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
662 x = gen_lowpart (int_mode, SUBREG_REG (x));
664 if (GET_MODE (x) != VOIDmode)
665 oldmode = GET_MODE (x);
667 if (mode == oldmode)
668 return x;
670 if (CONST_SCALAR_INT_P (x)
671 && is_int_mode (mode, &int_mode))
673 /* If the caller did not tell us the old mode, then there is not
674 much to do with respect to canonicalization. We have to
675 assume that all the bits are significant. */
676 if (GET_MODE_CLASS (oldmode) != MODE_INT)
677 oldmode = MAX_MODE_INT;
678 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
679 GET_MODE_PRECISION (int_mode),
680 unsignedp ? UNSIGNED : SIGNED);
681 return immed_wide_int_const (w, int_mode);
684 /* We can do this with a gen_lowpart if both desired and current modes
685 are integer, and this is either a constant integer, a register, or a
686 non-volatile MEM. */
687 scalar_int_mode int_oldmode;
688 if (is_int_mode (mode, &int_mode)
689 && is_int_mode (oldmode, &int_oldmode)
690 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
691 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
692 || CONST_POLY_INT_P (x)
693 || (REG_P (x)
694 && (!HARD_REGISTER_P (x)
695 || targetm.hard_regno_mode_ok (REGNO (x), int_mode))
696 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
697 return gen_lowpart (int_mode, x);
699 /* Converting from integer constant into mode is always equivalent to an
700 subreg operation. */
701 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
703 gcc_assert (known_eq (GET_MODE_BITSIZE (mode),
704 GET_MODE_BITSIZE (oldmode)));
705 return simplify_gen_subreg (mode, x, oldmode, 0);
708 temp = gen_reg_rtx (mode);
709 convert_move (temp, x, unsignedp);
710 return temp;
713 /* Return the largest alignment we can use for doing a move (or store)
714 of MAX_PIECES. ALIGN is the largest alignment we could use. */
716 static unsigned int
717 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
719 scalar_int_mode tmode
720 = int_mode_for_size (max_pieces * BITS_PER_UNIT, 1).require ();
722 if (align >= GET_MODE_ALIGNMENT (tmode))
723 align = GET_MODE_ALIGNMENT (tmode);
724 else
726 scalar_int_mode xmode = NARROWEST_INT_MODE;
727 opt_scalar_int_mode mode_iter;
728 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
730 tmode = mode_iter.require ();
731 if (GET_MODE_SIZE (tmode) > max_pieces
732 || targetm.slow_unaligned_access (tmode, align))
733 break;
734 xmode = tmode;
737 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
740 return align;
743 /* Return the widest integer mode that is narrower than SIZE bytes. */
745 static scalar_int_mode
746 widest_int_mode_for_size (unsigned int size)
748 scalar_int_mode result = NARROWEST_INT_MODE;
750 gcc_checking_assert (size > 1);
752 opt_scalar_int_mode tmode;
753 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
754 if (GET_MODE_SIZE (tmode.require ()) < size)
755 result = tmode.require ();
757 return result;
760 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
761 and should be performed piecewise. */
763 static bool
764 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
765 enum by_pieces_operation op)
767 return targetm.use_by_pieces_infrastructure_p (len, align, op,
768 optimize_insn_for_speed_p ());
771 /* Determine whether the LEN bytes can be moved by using several move
772 instructions. Return nonzero if a call to move_by_pieces should
773 succeed. */
775 bool
776 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
778 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
781 /* Return number of insns required to perform operation OP by pieces
782 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
784 unsigned HOST_WIDE_INT
785 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
786 unsigned int max_size, by_pieces_operation op)
788 unsigned HOST_WIDE_INT n_insns = 0;
790 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
792 while (max_size > 1 && l > 0)
794 scalar_int_mode mode = widest_int_mode_for_size (max_size);
795 enum insn_code icode;
797 unsigned int modesize = GET_MODE_SIZE (mode);
799 icode = optab_handler (mov_optab, mode);
800 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
802 unsigned HOST_WIDE_INT n_pieces = l / modesize;
803 l %= modesize;
804 switch (op)
806 default:
807 n_insns += n_pieces;
808 break;
810 case COMPARE_BY_PIECES:
811 int batch = targetm.compare_by_pieces_branch_ratio (mode);
812 int batch_ops = 4 * batch - 1;
813 unsigned HOST_WIDE_INT full = n_pieces / batch;
814 n_insns += full * batch_ops;
815 if (n_pieces % batch != 0)
816 n_insns++;
817 break;
821 max_size = modesize;
824 gcc_assert (!l);
825 return n_insns;
828 /* Used when performing piecewise block operations, holds information
829 about one of the memory objects involved. The member functions
830 can be used to generate code for loading from the object and
831 updating the address when iterating. */
833 class pieces_addr
835 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
836 stack pushes. */
837 rtx m_obj;
838 /* The address of the object. Can differ from that seen in the
839 MEM rtx if we copied the address to a register. */
840 rtx m_addr;
841 /* Nonzero if the address on the object has an autoincrement already,
842 signifies whether that was an increment or decrement. */
843 signed char m_addr_inc;
844 /* Nonzero if we intend to use autoinc without the address already
845 having autoinc form. We will insert add insns around each memory
846 reference, expecting later passes to form autoinc addressing modes.
847 The only supported options are predecrement and postincrement. */
848 signed char m_explicit_inc;
849 /* True if we have either of the two possible cases of using
850 autoincrement. */
851 bool m_auto;
852 /* True if this is an address to be used for load operations rather
853 than stores. */
854 bool m_is_load;
856 /* Optionally, a function to obtain constants for any given offset into
857 the objects, and data associated with it. */
858 by_pieces_constfn m_constfn;
859 void *m_cfndata;
860 public:
861 pieces_addr (rtx, bool, by_pieces_constfn, void *);
862 rtx adjust (scalar_int_mode, HOST_WIDE_INT);
863 void increment_address (HOST_WIDE_INT);
864 void maybe_predec (HOST_WIDE_INT);
865 void maybe_postinc (HOST_WIDE_INT);
866 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
867 int get_addr_inc ()
869 return m_addr_inc;
873 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
874 true if the operation to be performed on this object is a load
875 rather than a store. For stores, OBJ can be NULL, in which case we
876 assume the operation is a stack push. For loads, the optional
877 CONSTFN and its associated CFNDATA can be used in place of the
878 memory load. */
880 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
881 void *cfndata)
882 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
884 m_addr_inc = 0;
885 m_auto = false;
886 if (obj)
888 rtx addr = XEXP (obj, 0);
889 rtx_code code = GET_CODE (addr);
890 m_addr = addr;
891 bool dec = code == PRE_DEC || code == POST_DEC;
892 bool inc = code == PRE_INC || code == POST_INC;
893 m_auto = inc || dec;
894 if (m_auto)
895 m_addr_inc = dec ? -1 : 1;
897 /* While we have always looked for these codes here, the code
898 implementing the memory operation has never handled them.
899 Support could be added later if necessary or beneficial. */
900 gcc_assert (code != PRE_INC && code != POST_DEC);
902 else
904 m_addr = NULL_RTX;
905 if (!is_load)
907 m_auto = true;
908 if (STACK_GROWS_DOWNWARD)
909 m_addr_inc = -1;
910 else
911 m_addr_inc = 1;
913 else
914 gcc_assert (constfn != NULL);
916 m_explicit_inc = 0;
917 if (constfn)
918 gcc_assert (is_load);
921 /* Decide whether to use autoinc for an address involved in a memory op.
922 MODE is the mode of the accesses, REVERSE is true if we've decided to
923 perform the operation starting from the end, and LEN is the length of
924 the operation. Don't override an earlier decision to set m_auto. */
926 void
927 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
928 HOST_WIDE_INT len)
930 if (m_auto || m_obj == NULL_RTX)
931 return;
933 bool use_predec = (m_is_load
934 ? USE_LOAD_PRE_DECREMENT (mode)
935 : USE_STORE_PRE_DECREMENT (mode));
936 bool use_postinc = (m_is_load
937 ? USE_LOAD_POST_INCREMENT (mode)
938 : USE_STORE_POST_INCREMENT (mode));
939 machine_mode addr_mode = get_address_mode (m_obj);
941 if (use_predec && reverse)
943 m_addr = copy_to_mode_reg (addr_mode,
944 plus_constant (addr_mode,
945 m_addr, len));
946 m_auto = true;
947 m_explicit_inc = -1;
949 else if (use_postinc && !reverse)
951 m_addr = copy_to_mode_reg (addr_mode, m_addr);
952 m_auto = true;
953 m_explicit_inc = 1;
955 else if (CONSTANT_P (m_addr))
956 m_addr = copy_to_mode_reg (addr_mode, m_addr);
959 /* Adjust the address to refer to the data at OFFSET in MODE. If we
960 are using autoincrement for this address, we don't add the offset,
961 but we still modify the MEM's properties. */
964 pieces_addr::adjust (scalar_int_mode mode, HOST_WIDE_INT offset)
966 if (m_constfn)
967 return m_constfn (m_cfndata, offset, mode);
968 if (m_obj == NULL_RTX)
969 return NULL_RTX;
970 if (m_auto)
971 return adjust_automodify_address (m_obj, mode, m_addr, offset);
972 else
973 return adjust_address (m_obj, mode, offset);
976 /* Emit an add instruction to increment the address by SIZE. */
978 void
979 pieces_addr::increment_address (HOST_WIDE_INT size)
981 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
982 emit_insn (gen_add2_insn (m_addr, amount));
985 /* If we are supposed to decrement the address after each access, emit code
986 to do so now. Increment by SIZE (which has should have the correct sign
987 already). */
989 void
990 pieces_addr::maybe_predec (HOST_WIDE_INT size)
992 if (m_explicit_inc >= 0)
993 return;
994 gcc_assert (HAVE_PRE_DECREMENT);
995 increment_address (size);
998 /* If we are supposed to decrement the address after each access, emit code
999 to do so now. Increment by SIZE. */
1001 void
1002 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
1004 if (m_explicit_inc <= 0)
1005 return;
1006 gcc_assert (HAVE_POST_INCREMENT);
1007 increment_address (size);
1010 /* This structure is used by do_op_by_pieces to describe the operation
1011 to be performed. */
1013 class op_by_pieces_d
1015 protected:
1016 pieces_addr m_to, m_from;
1017 unsigned HOST_WIDE_INT m_len;
1018 HOST_WIDE_INT m_offset;
1019 unsigned int m_align;
1020 unsigned int m_max_size;
1021 bool m_reverse;
1023 /* Virtual functions, overriden by derived classes for the specific
1024 operation. */
1025 virtual void generate (rtx, rtx, machine_mode) = 0;
1026 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1027 virtual void finish_mode (machine_mode)
1031 public:
1032 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1033 unsigned HOST_WIDE_INT, unsigned int);
1034 void run ();
1037 /* The constructor for an op_by_pieces_d structure. We require two
1038 objects named TO and FROM, which are identified as loads or stores
1039 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1040 and its associated FROM_CFN_DATA can be used to replace loads with
1041 constant values. LEN describes the length of the operation. */
1043 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1044 rtx from, bool from_load,
1045 by_pieces_constfn from_cfn,
1046 void *from_cfn_data,
1047 unsigned HOST_WIDE_INT len,
1048 unsigned int align)
1049 : m_to (to, to_load, NULL, NULL),
1050 m_from (from, from_load, from_cfn, from_cfn_data),
1051 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1053 int toi = m_to.get_addr_inc ();
1054 int fromi = m_from.get_addr_inc ();
1055 if (toi >= 0 && fromi >= 0)
1056 m_reverse = false;
1057 else if (toi <= 0 && fromi <= 0)
1058 m_reverse = true;
1059 else
1060 gcc_unreachable ();
1062 m_offset = m_reverse ? len : 0;
1063 align = MIN (to ? MEM_ALIGN (to) : align,
1064 from ? MEM_ALIGN (from) : align);
1066 /* If copying requires more than two move insns,
1067 copy addresses to registers (to make displacements shorter)
1068 and use post-increment if available. */
1069 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1071 /* Find the mode of the largest comparison. */
1072 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1074 m_from.decide_autoinc (mode, m_reverse, len);
1075 m_to.decide_autoinc (mode, m_reverse, len);
1078 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1079 m_align = align;
1082 /* This function contains the main loop used for expanding a block
1083 operation. First move what we can in the largest integer mode,
1084 then go to successively smaller modes. For every access, call
1085 GENFUN with the two operands and the EXTRA_DATA. */
1087 void
1088 op_by_pieces_d::run ()
1090 while (m_max_size > 1 && m_len > 0)
1092 scalar_int_mode mode = widest_int_mode_for_size (m_max_size);
1094 if (prepare_mode (mode, m_align))
1096 unsigned int size = GET_MODE_SIZE (mode);
1097 rtx to1 = NULL_RTX, from1;
1099 while (m_len >= size)
1101 if (m_reverse)
1102 m_offset -= size;
1104 to1 = m_to.adjust (mode, m_offset);
1105 from1 = m_from.adjust (mode, m_offset);
1107 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1108 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1110 generate (to1, from1, mode);
1112 m_to.maybe_postinc (size);
1113 m_from.maybe_postinc (size);
1115 if (!m_reverse)
1116 m_offset += size;
1118 m_len -= size;
1121 finish_mode (mode);
1124 m_max_size = GET_MODE_SIZE (mode);
1127 /* The code above should have handled everything. */
1128 gcc_assert (!m_len);
1131 /* Derived class from op_by_pieces_d, providing support for block move
1132 operations. */
1134 class move_by_pieces_d : public op_by_pieces_d
1136 insn_gen_fn m_gen_fun;
1137 void generate (rtx, rtx, machine_mode);
1138 bool prepare_mode (machine_mode, unsigned int);
1140 public:
1141 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1142 unsigned int align)
1143 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1146 rtx finish_endp (int);
1149 /* Return true if MODE can be used for a set of copies, given an
1150 alignment ALIGN. Prepare whatever data is necessary for later
1151 calls to generate. */
1153 bool
1154 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1156 insn_code icode = optab_handler (mov_optab, mode);
1157 m_gen_fun = GEN_FCN (icode);
1158 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1161 /* A callback used when iterating for a compare_by_pieces_operation.
1162 OP0 and OP1 are the values that have been loaded and should be
1163 compared in MODE. If OP0 is NULL, this means we should generate a
1164 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1165 gen function that should be used to generate the mode. */
1167 void
1168 move_by_pieces_d::generate (rtx op0, rtx op1,
1169 machine_mode mode ATTRIBUTE_UNUSED)
1171 #ifdef PUSH_ROUNDING
1172 if (op0 == NULL_RTX)
1174 emit_single_push_insn (mode, op1, NULL);
1175 return;
1177 #endif
1178 emit_insn (m_gen_fun (op0, op1));
1181 /* Perform the final adjustment at the end of a string to obtain the
1182 correct return value for the block operation. If ENDP is 1 return
1183 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1184 end minus one byte ala stpcpy. */
1187 move_by_pieces_d::finish_endp (int endp)
1189 gcc_assert (!m_reverse);
1190 if (endp == 2)
1192 m_to.maybe_postinc (-1);
1193 --m_offset;
1195 return m_to.adjust (QImode, m_offset);
1198 /* Generate several move instructions to copy LEN bytes from block FROM to
1199 block TO. (These are MEM rtx's with BLKmode).
1201 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1202 used to push FROM to the stack.
1204 ALIGN is maximum stack alignment we can assume.
1206 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1207 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1208 stpcpy. */
1211 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1212 unsigned int align, int endp)
1214 #ifndef PUSH_ROUNDING
1215 if (to == NULL)
1216 gcc_unreachable ();
1217 #endif
1219 move_by_pieces_d data (to, from, len, align);
1221 data.run ();
1223 if (endp)
1224 return data.finish_endp (endp);
1225 else
1226 return to;
1229 /* Derived class from op_by_pieces_d, providing support for block move
1230 operations. */
1232 class store_by_pieces_d : public op_by_pieces_d
1234 insn_gen_fn m_gen_fun;
1235 void generate (rtx, rtx, machine_mode);
1236 bool prepare_mode (machine_mode, unsigned int);
1238 public:
1239 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1240 unsigned HOST_WIDE_INT len, unsigned int align)
1241 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1244 rtx finish_endp (int);
1247 /* Return true if MODE can be used for a set of stores, given an
1248 alignment ALIGN. Prepare whatever data is necessary for later
1249 calls to generate. */
1251 bool
1252 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1254 insn_code icode = optab_handler (mov_optab, mode);
1255 m_gen_fun = GEN_FCN (icode);
1256 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1259 /* A callback used when iterating for a store_by_pieces_operation.
1260 OP0 and OP1 are the values that have been loaded and should be
1261 compared in MODE. If OP0 is NULL, this means we should generate a
1262 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1263 gen function that should be used to generate the mode. */
1265 void
1266 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1268 emit_insn (m_gen_fun (op0, op1));
1271 /* Perform the final adjustment at the end of a string to obtain the
1272 correct return value for the block operation. If ENDP is 1 return
1273 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1274 end minus one byte ala stpcpy. */
1277 store_by_pieces_d::finish_endp (int endp)
1279 gcc_assert (!m_reverse);
1280 if (endp == 2)
1282 m_to.maybe_postinc (-1);
1283 --m_offset;
1285 return m_to.adjust (QImode, m_offset);
1288 /* Determine whether the LEN bytes generated by CONSTFUN can be
1289 stored to memory using several move instructions. CONSTFUNDATA is
1290 a pointer which will be passed as argument in every CONSTFUN call.
1291 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1292 a memset operation and false if it's a copy of a constant string.
1293 Return nonzero if a call to store_by_pieces should succeed. */
1296 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1297 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1298 void *constfundata, unsigned int align, bool memsetp)
1300 unsigned HOST_WIDE_INT l;
1301 unsigned int max_size;
1302 HOST_WIDE_INT offset = 0;
1303 enum insn_code icode;
1304 int reverse;
1305 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1306 rtx cst ATTRIBUTE_UNUSED;
1308 if (len == 0)
1309 return 1;
1311 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1312 memsetp
1313 ? SET_BY_PIECES
1314 : STORE_BY_PIECES,
1315 optimize_insn_for_speed_p ()))
1316 return 0;
1318 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1320 /* We would first store what we can in the largest integer mode, then go to
1321 successively smaller modes. */
1323 for (reverse = 0;
1324 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1325 reverse++)
1327 l = len;
1328 max_size = STORE_MAX_PIECES + 1;
1329 while (max_size > 1 && l > 0)
1331 scalar_int_mode mode = widest_int_mode_for_size (max_size);
1333 icode = optab_handler (mov_optab, mode);
1334 if (icode != CODE_FOR_nothing
1335 && align >= GET_MODE_ALIGNMENT (mode))
1337 unsigned int size = GET_MODE_SIZE (mode);
1339 while (l >= size)
1341 if (reverse)
1342 offset -= size;
1344 cst = (*constfun) (constfundata, offset, mode);
1345 if (!targetm.legitimate_constant_p (mode, cst))
1346 return 0;
1348 if (!reverse)
1349 offset += size;
1351 l -= size;
1355 max_size = GET_MODE_SIZE (mode);
1358 /* The code above should have handled everything. */
1359 gcc_assert (!l);
1362 return 1;
1365 /* Generate several move instructions to store LEN bytes generated by
1366 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1367 pointer which will be passed as argument in every CONSTFUN call.
1368 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1369 a memset operation and false if it's a copy of a constant string.
1370 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1371 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1372 stpcpy. */
1375 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1376 rtx (*constfun) (void *, HOST_WIDE_INT, scalar_int_mode),
1377 void *constfundata, unsigned int align, bool memsetp, int endp)
1379 if (len == 0)
1381 gcc_assert (endp != 2);
1382 return to;
1385 gcc_assert (targetm.use_by_pieces_infrastructure_p
1386 (len, align,
1387 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1388 optimize_insn_for_speed_p ()));
1390 store_by_pieces_d data (to, constfun, constfundata, len, align);
1391 data.run ();
1393 if (endp)
1394 return data.finish_endp (endp);
1395 else
1396 return to;
1399 /* Callback routine for clear_by_pieces.
1400 Return const0_rtx unconditionally. */
1402 static rtx
1403 clear_by_pieces_1 (void *, HOST_WIDE_INT, scalar_int_mode)
1405 return const0_rtx;
1408 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1409 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1411 static void
1412 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1414 if (len == 0)
1415 return;
1417 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1418 data.run ();
1421 /* Context used by compare_by_pieces_genfn. It stores the fail label
1422 to jump to in case of miscomparison, and for branch ratios greater than 1,
1423 it stores an accumulator and the current and maximum counts before
1424 emitting another branch. */
1426 class compare_by_pieces_d : public op_by_pieces_d
1428 rtx_code_label *m_fail_label;
1429 rtx m_accumulator;
1430 int m_count, m_batch;
1432 void generate (rtx, rtx, machine_mode);
1433 bool prepare_mode (machine_mode, unsigned int);
1434 void finish_mode (machine_mode);
1435 public:
1436 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1437 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1438 rtx_code_label *fail_label)
1439 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1441 m_fail_label = fail_label;
1445 /* A callback used when iterating for a compare_by_pieces_operation.
1446 OP0 and OP1 are the values that have been loaded and should be
1447 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1448 context structure. */
1450 void
1451 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1453 if (m_batch > 1)
1455 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1456 true, OPTAB_LIB_WIDEN);
1457 if (m_count != 0)
1458 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1459 true, OPTAB_LIB_WIDEN);
1460 m_accumulator = temp;
1462 if (++m_count < m_batch)
1463 return;
1465 m_count = 0;
1466 op0 = m_accumulator;
1467 op1 = const0_rtx;
1468 m_accumulator = NULL_RTX;
1470 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1471 m_fail_label, profile_probability::uninitialized ());
1474 /* Return true if MODE can be used for a set of moves and comparisons,
1475 given an alignment ALIGN. Prepare whatever data is necessary for
1476 later calls to generate. */
1478 bool
1479 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1481 insn_code icode = optab_handler (mov_optab, mode);
1482 if (icode == CODE_FOR_nothing
1483 || align < GET_MODE_ALIGNMENT (mode)
1484 || !can_compare_p (EQ, mode, ccp_jump))
1485 return false;
1486 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1487 if (m_batch < 0)
1488 return false;
1489 m_accumulator = NULL_RTX;
1490 m_count = 0;
1491 return true;
1494 /* Called after expanding a series of comparisons in MODE. If we have
1495 accumulated results for which we haven't emitted a branch yet, do
1496 so now. */
1498 void
1499 compare_by_pieces_d::finish_mode (machine_mode mode)
1501 if (m_accumulator != NULL_RTX)
1502 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1503 NULL_RTX, NULL, m_fail_label,
1504 profile_probability::uninitialized ());
1507 /* Generate several move instructions to compare LEN bytes from blocks
1508 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1510 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1511 used to push FROM to the stack.
1513 ALIGN is maximum stack alignment we can assume.
1515 Optionally, the caller can pass a constfn and associated data in A1_CFN
1516 and A1_CFN_DATA. describing that the second operand being compared is a
1517 known constant and how to obtain its data. */
1519 static rtx
1520 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1521 rtx target, unsigned int align,
1522 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1524 rtx_code_label *fail_label = gen_label_rtx ();
1525 rtx_code_label *end_label = gen_label_rtx ();
1527 if (target == NULL_RTX
1528 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1529 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1531 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1532 fail_label);
1534 data.run ();
1536 emit_move_insn (target, const0_rtx);
1537 emit_jump (end_label);
1538 emit_barrier ();
1539 emit_label (fail_label);
1540 emit_move_insn (target, const1_rtx);
1541 emit_label (end_label);
1543 return target;
1546 /* Emit code to move a block Y to a block X. This may be done with
1547 string-move instructions, with multiple scalar move instructions,
1548 or with a library call.
1550 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1551 SIZE is an rtx that says how long they are.
1552 ALIGN is the maximum alignment we can assume they have.
1553 METHOD describes what kind of copy this is, and what mechanisms may be used.
1554 MIN_SIZE is the minimal size of block to move
1555 MAX_SIZE is the maximal size of block to move, if it can not be represented
1556 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1558 Return the address of the new block, if memcpy is called and returns it,
1559 0 otherwise. */
1562 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1563 unsigned int expected_align, HOST_WIDE_INT expected_size,
1564 unsigned HOST_WIDE_INT min_size,
1565 unsigned HOST_WIDE_INT max_size,
1566 unsigned HOST_WIDE_INT probable_max_size)
1568 bool may_use_call;
1569 rtx retval = 0;
1570 unsigned int align;
1572 gcc_assert (size);
1573 if (CONST_INT_P (size) && INTVAL (size) == 0)
1574 return 0;
1576 switch (method)
1578 case BLOCK_OP_NORMAL:
1579 case BLOCK_OP_TAILCALL:
1580 may_use_call = true;
1581 break;
1583 case BLOCK_OP_CALL_PARM:
1584 may_use_call = block_move_libcall_safe_for_call_parm ();
1586 /* Make inhibit_defer_pop nonzero around the library call
1587 to force it to pop the arguments right away. */
1588 NO_DEFER_POP;
1589 break;
1591 case BLOCK_OP_NO_LIBCALL:
1592 may_use_call = false;
1593 break;
1595 default:
1596 gcc_unreachable ();
1599 gcc_assert (MEM_P (x) && MEM_P (y));
1600 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1601 gcc_assert (align >= BITS_PER_UNIT);
1603 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1604 block copy is more efficient for other large modes, e.g. DCmode. */
1605 x = adjust_address (x, BLKmode, 0);
1606 y = adjust_address (y, BLKmode, 0);
1608 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1609 can be incorrect is coming from __builtin_memcpy. */
1610 if (CONST_INT_P (size))
1612 x = shallow_copy_rtx (x);
1613 y = shallow_copy_rtx (y);
1614 set_mem_size (x, INTVAL (size));
1615 set_mem_size (y, INTVAL (size));
1618 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1619 move_by_pieces (x, y, INTVAL (size), align, 0);
1620 else if (emit_block_move_via_movmem (x, y, size, align,
1621 expected_align, expected_size,
1622 min_size, max_size, probable_max_size))
1624 else if (may_use_call
1625 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1626 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1628 /* Since x and y are passed to a libcall, mark the corresponding
1629 tree EXPR as addressable. */
1630 tree y_expr = MEM_EXPR (y);
1631 tree x_expr = MEM_EXPR (x);
1632 if (y_expr)
1633 mark_addressable (y_expr);
1634 if (x_expr)
1635 mark_addressable (x_expr);
1636 retval = emit_block_copy_via_libcall (x, y, size,
1637 method == BLOCK_OP_TAILCALL);
1640 else
1641 emit_block_move_via_loop (x, y, size, align);
1643 if (method == BLOCK_OP_CALL_PARM)
1644 OK_DEFER_POP;
1646 return retval;
1650 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1652 unsigned HOST_WIDE_INT max, min = 0;
1653 if (GET_CODE (size) == CONST_INT)
1654 min = max = UINTVAL (size);
1655 else
1656 max = GET_MODE_MASK (GET_MODE (size));
1657 return emit_block_move_hints (x, y, size, method, 0, -1,
1658 min, max, max);
1661 /* A subroutine of emit_block_move. Returns true if calling the
1662 block move libcall will not clobber any parameters which may have
1663 already been placed on the stack. */
1665 static bool
1666 block_move_libcall_safe_for_call_parm (void)
1668 #if defined (REG_PARM_STACK_SPACE)
1669 tree fn;
1670 #endif
1672 /* If arguments are pushed on the stack, then they're safe. */
1673 if (PUSH_ARGS)
1674 return true;
1676 /* If registers go on the stack anyway, any argument is sure to clobber
1677 an outgoing argument. */
1678 #if defined (REG_PARM_STACK_SPACE)
1679 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1680 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1681 depend on its argument. */
1682 (void) fn;
1683 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1684 && REG_PARM_STACK_SPACE (fn) != 0)
1685 return false;
1686 #endif
1688 /* If any argument goes in memory, then it might clobber an outgoing
1689 argument. */
1691 CUMULATIVE_ARGS args_so_far_v;
1692 cumulative_args_t args_so_far;
1693 tree fn, arg;
1695 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1696 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1697 args_so_far = pack_cumulative_args (&args_so_far_v);
1699 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1700 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1702 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1703 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1704 NULL_TREE, true);
1705 if (!tmp || !REG_P (tmp))
1706 return false;
1707 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1708 return false;
1709 targetm.calls.function_arg_advance (args_so_far, mode,
1710 NULL_TREE, true);
1713 return true;
1716 /* A subroutine of emit_block_move. Expand a movmem pattern;
1717 return true if successful. */
1719 static bool
1720 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1721 unsigned int expected_align, HOST_WIDE_INT expected_size,
1722 unsigned HOST_WIDE_INT min_size,
1723 unsigned HOST_WIDE_INT max_size,
1724 unsigned HOST_WIDE_INT probable_max_size)
1726 int save_volatile_ok = volatile_ok;
1728 if (expected_align < align)
1729 expected_align = align;
1730 if (expected_size != -1)
1732 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1733 expected_size = probable_max_size;
1734 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1735 expected_size = min_size;
1738 /* Since this is a move insn, we don't care about volatility. */
1739 volatile_ok = 1;
1741 /* Try the most limited insn first, because there's no point
1742 including more than one in the machine description unless
1743 the more limited one has some advantage. */
1745 opt_scalar_int_mode mode_iter;
1746 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
1748 scalar_int_mode mode = mode_iter.require ();
1749 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1751 if (code != CODE_FOR_nothing
1752 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1753 here because if SIZE is less than the mode mask, as it is
1754 returned by the macro, it will definitely be less than the
1755 actual mode mask. Since SIZE is within the Pmode address
1756 space, we limit MODE to Pmode. */
1757 && ((CONST_INT_P (size)
1758 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1759 <= (GET_MODE_MASK (mode) >> 1)))
1760 || max_size <= (GET_MODE_MASK (mode) >> 1)
1761 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1763 struct expand_operand ops[9];
1764 unsigned int nops;
1766 /* ??? When called via emit_block_move_for_call, it'd be
1767 nice if there were some way to inform the backend, so
1768 that it doesn't fail the expansion because it thinks
1769 emitting the libcall would be more efficient. */
1770 nops = insn_data[(int) code].n_generator_args;
1771 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1773 create_fixed_operand (&ops[0], x);
1774 create_fixed_operand (&ops[1], y);
1775 /* The check above guarantees that this size conversion is valid. */
1776 create_convert_operand_to (&ops[2], size, mode, true);
1777 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1778 if (nops >= 6)
1780 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1781 create_integer_operand (&ops[5], expected_size);
1783 if (nops >= 8)
1785 create_integer_operand (&ops[6], min_size);
1786 /* If we can not represent the maximal size,
1787 make parameter NULL. */
1788 if ((HOST_WIDE_INT) max_size != -1)
1789 create_integer_operand (&ops[7], max_size);
1790 else
1791 create_fixed_operand (&ops[7], NULL);
1793 if (nops == 9)
1795 /* If we can not represent the maximal size,
1796 make parameter NULL. */
1797 if ((HOST_WIDE_INT) probable_max_size != -1)
1798 create_integer_operand (&ops[8], probable_max_size);
1799 else
1800 create_fixed_operand (&ops[8], NULL);
1802 if (maybe_expand_insn (code, nops, ops))
1804 volatile_ok = save_volatile_ok;
1805 return true;
1810 volatile_ok = save_volatile_ok;
1811 return false;
1814 /* A subroutine of emit_block_move. Copy the data via an explicit
1815 loop. This is used only when libcalls are forbidden. */
1816 /* ??? It'd be nice to copy in hunks larger than QImode. */
1818 static void
1819 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1820 unsigned int align ATTRIBUTE_UNUSED)
1822 rtx_code_label *cmp_label, *top_label;
1823 rtx iter, x_addr, y_addr, tmp;
1824 machine_mode x_addr_mode = get_address_mode (x);
1825 machine_mode y_addr_mode = get_address_mode (y);
1826 machine_mode iter_mode;
1828 iter_mode = GET_MODE (size);
1829 if (iter_mode == VOIDmode)
1830 iter_mode = word_mode;
1832 top_label = gen_label_rtx ();
1833 cmp_label = gen_label_rtx ();
1834 iter = gen_reg_rtx (iter_mode);
1836 emit_move_insn (iter, const0_rtx);
1838 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1839 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1840 do_pending_stack_adjust ();
1842 emit_jump (cmp_label);
1843 emit_label (top_label);
1845 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1846 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1848 if (x_addr_mode != y_addr_mode)
1849 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1850 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1852 x = change_address (x, QImode, x_addr);
1853 y = change_address (y, QImode, y_addr);
1855 emit_move_insn (x, y);
1857 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1858 true, OPTAB_LIB_WIDEN);
1859 if (tmp != iter)
1860 emit_move_insn (iter, tmp);
1862 emit_label (cmp_label);
1864 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1865 true, top_label,
1866 profile_probability::guessed_always ()
1867 .apply_scale (9, 10));
1870 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1871 TAILCALL is true if this is a tail call. */
1874 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1875 rtx size, bool tailcall)
1877 rtx dst_addr, src_addr;
1878 tree call_expr, dst_tree, src_tree, size_tree;
1879 machine_mode size_mode;
1881 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1882 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1883 dst_tree = make_tree (ptr_type_node, dst_addr);
1885 src_addr = copy_addr_to_reg (XEXP (src, 0));
1886 src_addr = convert_memory_address (ptr_mode, src_addr);
1887 src_tree = make_tree (ptr_type_node, src_addr);
1889 size_mode = TYPE_MODE (sizetype);
1890 size = convert_to_mode (size_mode, size, 1);
1891 size = copy_to_mode_reg (size_mode, size);
1892 size_tree = make_tree (sizetype, size);
1894 /* It is incorrect to use the libcall calling conventions for calls to
1895 memcpy/memmove/memcmp because they can be provided by the user. */
1896 tree fn = builtin_decl_implicit (fncode);
1897 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1898 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1900 return expand_call (call_expr, NULL_RTX, false);
1903 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1904 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1905 otherwise return null. */
1908 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1909 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1910 HOST_WIDE_INT align)
1912 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1914 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1915 target = NULL_RTX;
1917 struct expand_operand ops[5];
1918 create_output_operand (&ops[0], target, insn_mode);
1919 create_fixed_operand (&ops[1], arg1_rtx);
1920 create_fixed_operand (&ops[2], arg2_rtx);
1921 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1922 TYPE_UNSIGNED (arg3_type));
1923 create_integer_operand (&ops[4], align);
1924 if (maybe_expand_insn (icode, 5, ops))
1925 return ops[0].value;
1926 return NULL_RTX;
1929 /* Expand a block compare between X and Y with length LEN using the
1930 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1931 of the expression that was used to calculate the length. ALIGN
1932 gives the known minimum common alignment. */
1934 static rtx
1935 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1936 unsigned align)
1938 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1939 implementing memcmp because it will stop if it encounters two
1940 zero bytes. */
1941 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1943 if (icode == CODE_FOR_nothing)
1944 return NULL_RTX;
1946 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1949 /* Emit code to compare a block Y to a block X. This may be done with
1950 string-compare instructions, with multiple scalar instructions,
1951 or with a library call.
1953 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1954 they are. LEN_TYPE is the type of the expression that was used to
1955 calculate it.
1957 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1958 value of a normal memcmp call, instead we can just compare for equality.
1959 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1960 returning NULL_RTX.
1962 Optionally, the caller can pass a constfn and associated data in Y_CFN
1963 and Y_CFN_DATA. describing that the second operand being compared is a
1964 known constant and how to obtain its data.
1965 Return the result of the comparison, or NULL_RTX if we failed to
1966 perform the operation. */
1969 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1970 bool equality_only, by_pieces_constfn y_cfn,
1971 void *y_cfndata)
1973 rtx result = 0;
1975 if (CONST_INT_P (len) && INTVAL (len) == 0)
1976 return const0_rtx;
1978 gcc_assert (MEM_P (x) && MEM_P (y));
1979 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1980 gcc_assert (align >= BITS_PER_UNIT);
1982 x = adjust_address (x, BLKmode, 0);
1983 y = adjust_address (y, BLKmode, 0);
1985 if (equality_only
1986 && CONST_INT_P (len)
1987 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1988 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1989 y_cfn, y_cfndata);
1990 else
1991 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1993 return result;
1996 /* Copy all or part of a value X into registers starting at REGNO.
1997 The number of registers to be filled is NREGS. */
1999 void
2000 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
2002 if (nregs == 0)
2003 return;
2005 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
2006 x = validize_mem (force_const_mem (mode, x));
2008 /* See if the machine can do this with a load multiple insn. */
2009 if (targetm.have_load_multiple ())
2011 rtx_insn *last = get_last_insn ();
2012 rtx first = gen_rtx_REG (word_mode, regno);
2013 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
2014 GEN_INT (nregs)))
2016 emit_insn (pat);
2017 return;
2019 else
2020 delete_insns_since (last);
2023 for (int i = 0; i < nregs; i++)
2024 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2025 operand_subword_force (x, i, mode));
2028 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2029 The number of registers to be filled is NREGS. */
2031 void
2032 move_block_from_reg (int regno, rtx x, int nregs)
2034 if (nregs == 0)
2035 return;
2037 /* See if the machine can do this with a store multiple insn. */
2038 if (targetm.have_store_multiple ())
2040 rtx_insn *last = get_last_insn ();
2041 rtx first = gen_rtx_REG (word_mode, regno);
2042 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2043 GEN_INT (nregs)))
2045 emit_insn (pat);
2046 return;
2048 else
2049 delete_insns_since (last);
2052 for (int i = 0; i < nregs; i++)
2054 rtx tem = operand_subword (x, i, 1, BLKmode);
2056 gcc_assert (tem);
2058 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2062 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2063 ORIG, where ORIG is a non-consecutive group of registers represented by
2064 a PARALLEL. The clone is identical to the original except in that the
2065 original set of registers is replaced by a new set of pseudo registers.
2066 The new set has the same modes as the original set. */
2069 gen_group_rtx (rtx orig)
2071 int i, length;
2072 rtx *tmps;
2074 gcc_assert (GET_CODE (orig) == PARALLEL);
2076 length = XVECLEN (orig, 0);
2077 tmps = XALLOCAVEC (rtx, length);
2079 /* Skip a NULL entry in first slot. */
2080 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2082 if (i)
2083 tmps[0] = 0;
2085 for (; i < length; i++)
2087 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2088 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2090 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2093 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2096 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2097 except that values are placed in TMPS[i], and must later be moved
2098 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2100 static void
2101 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type,
2102 poly_int64 ssize)
2104 rtx src;
2105 int start, i;
2106 machine_mode m = GET_MODE (orig_src);
2108 gcc_assert (GET_CODE (dst) == PARALLEL);
2110 if (m != VOIDmode
2111 && !SCALAR_INT_MODE_P (m)
2112 && !MEM_P (orig_src)
2113 && GET_CODE (orig_src) != CONCAT)
2115 scalar_int_mode imode;
2116 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2118 src = gen_reg_rtx (imode);
2119 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2121 else
2123 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2124 emit_move_insn (src, orig_src);
2126 emit_group_load_1 (tmps, dst, src, type, ssize);
2127 return;
2130 /* Check for a NULL entry, used to indicate that the parameter goes
2131 both on the stack and in registers. */
2132 if (XEXP (XVECEXP (dst, 0, 0), 0))
2133 start = 0;
2134 else
2135 start = 1;
2137 /* Process the pieces. */
2138 for (i = start; i < XVECLEN (dst, 0); i++)
2140 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2141 poly_int64 bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2142 poly_int64 bytelen = GET_MODE_SIZE (mode);
2143 poly_int64 shift = 0;
2145 /* Handle trailing fragments that run over the size of the struct.
2146 It's the target's responsibility to make sure that the fragment
2147 cannot be strictly smaller in some cases and strictly larger
2148 in others. */
2149 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2150 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2152 /* Arrange to shift the fragment to where it belongs.
2153 extract_bit_field loads to the lsb of the reg. */
2154 if (
2155 #ifdef BLOCK_REG_PADDING
2156 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2157 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2158 #else
2159 BYTES_BIG_ENDIAN
2160 #endif
2162 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2163 bytelen = ssize - bytepos;
2164 gcc_assert (maybe_gt (bytelen, 0));
2167 /* If we won't be loading directly from memory, protect the real source
2168 from strange tricks we might play; but make sure that the source can
2169 be loaded directly into the destination. */
2170 src = orig_src;
2171 if (!MEM_P (orig_src)
2172 && (!CONSTANT_P (orig_src)
2173 || (GET_MODE (orig_src) != mode
2174 && GET_MODE (orig_src) != VOIDmode)))
2176 if (GET_MODE (orig_src) == VOIDmode)
2177 src = gen_reg_rtx (mode);
2178 else
2179 src = gen_reg_rtx (GET_MODE (orig_src));
2181 emit_move_insn (src, orig_src);
2184 /* Optimize the access just a bit. */
2185 if (MEM_P (src)
2186 && (! targetm.slow_unaligned_access (mode, MEM_ALIGN (src))
2187 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2188 && multiple_p (bytepos * BITS_PER_UNIT, GET_MODE_ALIGNMENT (mode))
2189 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2191 tmps[i] = gen_reg_rtx (mode);
2192 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2194 else if (COMPLEX_MODE_P (mode)
2195 && GET_MODE (src) == mode
2196 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2197 /* Let emit_move_complex do the bulk of the work. */
2198 tmps[i] = src;
2199 else if (GET_CODE (src) == CONCAT)
2201 poly_int64 slen = GET_MODE_SIZE (GET_MODE (src));
2202 poly_int64 slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2203 unsigned int elt;
2204 poly_int64 subpos;
2206 if (can_div_trunc_p (bytepos, slen0, &elt, &subpos)
2207 && known_le (subpos + bytelen, slen0))
2209 /* The following assumes that the concatenated objects all
2210 have the same size. In this case, a simple calculation
2211 can be used to determine the object and the bit field
2212 to be extracted. */
2213 tmps[i] = XEXP (src, elt);
2214 if (maybe_ne (subpos, 0)
2215 || maybe_ne (subpos + bytelen, slen0)
2216 || (!CONSTANT_P (tmps[i])
2217 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2218 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2219 subpos * BITS_PER_UNIT,
2220 1, NULL_RTX, mode, mode, false,
2221 NULL);
2223 else
2225 rtx mem;
2227 gcc_assert (known_eq (bytepos, 0));
2228 mem = assign_stack_temp (GET_MODE (src), slen);
2229 emit_move_insn (mem, src);
2230 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2231 0, 1, NULL_RTX, mode, mode, false,
2232 NULL);
2235 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2236 SIMD register, which is currently broken. While we get GCC
2237 to emit proper RTL for these cases, let's dump to memory. */
2238 else if (VECTOR_MODE_P (GET_MODE (dst))
2239 && REG_P (src))
2241 poly_uint64 slen = GET_MODE_SIZE (GET_MODE (src));
2242 rtx mem;
2244 mem = assign_stack_temp (GET_MODE (src), slen);
2245 emit_move_insn (mem, src);
2246 tmps[i] = adjust_address (mem, mode, bytepos);
2248 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2249 && XVECLEN (dst, 0) > 1)
2250 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2251 else if (CONSTANT_P (src))
2253 if (known_eq (bytelen, ssize))
2254 tmps[i] = src;
2255 else
2257 rtx first, second;
2259 /* TODO: const_wide_int can have sizes other than this... */
2260 gcc_assert (known_eq (2 * bytelen, ssize));
2261 split_double (src, &first, &second);
2262 if (i)
2263 tmps[i] = second;
2264 else
2265 tmps[i] = first;
2268 else if (REG_P (src) && GET_MODE (src) == mode)
2269 tmps[i] = src;
2270 else
2271 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2272 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2273 mode, mode, false, NULL);
2275 if (maybe_ne (shift, 0))
2276 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2277 shift, tmps[i], 0);
2281 /* Emit code to move a block SRC of type TYPE to a block DST,
2282 where DST is non-consecutive registers represented by a PARALLEL.
2283 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2284 if not known. */
2286 void
2287 emit_group_load (rtx dst, rtx src, tree type, poly_int64 ssize)
2289 rtx *tmps;
2290 int i;
2292 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2293 emit_group_load_1 (tmps, dst, src, type, ssize);
2295 /* Copy the extracted pieces into the proper (probable) hard regs. */
2296 for (i = 0; i < XVECLEN (dst, 0); i++)
2298 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2299 if (d == NULL)
2300 continue;
2301 emit_move_insn (d, tmps[i]);
2305 /* Similar, but load SRC into new pseudos in a format that looks like
2306 PARALLEL. This can later be fed to emit_group_move to get things
2307 in the right place. */
2310 emit_group_load_into_temps (rtx parallel, rtx src, tree type, poly_int64 ssize)
2312 rtvec vec;
2313 int i;
2315 vec = rtvec_alloc (XVECLEN (parallel, 0));
2316 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2318 /* Convert the vector to look just like the original PARALLEL, except
2319 with the computed values. */
2320 for (i = 0; i < XVECLEN (parallel, 0); i++)
2322 rtx e = XVECEXP (parallel, 0, i);
2323 rtx d = XEXP (e, 0);
2325 if (d)
2327 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2328 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2330 RTVEC_ELT (vec, i) = e;
2333 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2336 /* Emit code to move a block SRC to block DST, where SRC and DST are
2337 non-consecutive groups of registers, each represented by a PARALLEL. */
2339 void
2340 emit_group_move (rtx dst, rtx src)
2342 int i;
2344 gcc_assert (GET_CODE (src) == PARALLEL
2345 && GET_CODE (dst) == PARALLEL
2346 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2348 /* Skip first entry if NULL. */
2349 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2350 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2351 XEXP (XVECEXP (src, 0, i), 0));
2354 /* Move a group of registers represented by a PARALLEL into pseudos. */
2357 emit_group_move_into_temps (rtx src)
2359 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2360 int i;
2362 for (i = 0; i < XVECLEN (src, 0); i++)
2364 rtx e = XVECEXP (src, 0, i);
2365 rtx d = XEXP (e, 0);
2367 if (d)
2368 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2369 RTVEC_ELT (vec, i) = e;
2372 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2375 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2376 where SRC is non-consecutive registers represented by a PARALLEL.
2377 SSIZE represents the total size of block ORIG_DST, or -1 if not
2378 known. */
2380 void
2381 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED,
2382 poly_int64 ssize)
2384 rtx *tmps, dst;
2385 int start, finish, i;
2386 machine_mode m = GET_MODE (orig_dst);
2388 gcc_assert (GET_CODE (src) == PARALLEL);
2390 if (!SCALAR_INT_MODE_P (m)
2391 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2393 scalar_int_mode imode;
2394 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2396 dst = gen_reg_rtx (imode);
2397 emit_group_store (dst, src, type, ssize);
2398 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2400 else
2402 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2403 emit_group_store (dst, src, type, ssize);
2405 emit_move_insn (orig_dst, dst);
2406 return;
2409 /* Check for a NULL entry, used to indicate that the parameter goes
2410 both on the stack and in registers. */
2411 if (XEXP (XVECEXP (src, 0, 0), 0))
2412 start = 0;
2413 else
2414 start = 1;
2415 finish = XVECLEN (src, 0);
2417 tmps = XALLOCAVEC (rtx, finish);
2419 /* Copy the (probable) hard regs into pseudos. */
2420 for (i = start; i < finish; i++)
2422 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2423 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2425 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2426 emit_move_insn (tmps[i], reg);
2428 else
2429 tmps[i] = reg;
2432 /* If we won't be storing directly into memory, protect the real destination
2433 from strange tricks we might play. */
2434 dst = orig_dst;
2435 if (GET_CODE (dst) == PARALLEL)
2437 rtx temp;
2439 /* We can get a PARALLEL dst if there is a conditional expression in
2440 a return statement. In that case, the dst and src are the same,
2441 so no action is necessary. */
2442 if (rtx_equal_p (dst, src))
2443 return;
2445 /* It is unclear if we can ever reach here, but we may as well handle
2446 it. Allocate a temporary, and split this into a store/load to/from
2447 the temporary. */
2448 temp = assign_stack_temp (GET_MODE (dst), ssize);
2449 emit_group_store (temp, src, type, ssize);
2450 emit_group_load (dst, temp, type, ssize);
2451 return;
2453 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2455 machine_mode outer = GET_MODE (dst);
2456 machine_mode inner;
2457 poly_int64 bytepos;
2458 bool done = false;
2459 rtx temp;
2461 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2462 dst = gen_reg_rtx (outer);
2464 /* Make life a bit easier for combine. */
2465 /* If the first element of the vector is the low part
2466 of the destination mode, use a paradoxical subreg to
2467 initialize the destination. */
2468 if (start < finish)
2470 inner = GET_MODE (tmps[start]);
2471 bytepos = subreg_lowpart_offset (inner, outer);
2472 if (known_eq (INTVAL (XEXP (XVECEXP (src, 0, start), 1)), bytepos))
2474 temp = simplify_gen_subreg (outer, tmps[start],
2475 inner, 0);
2476 if (temp)
2478 emit_move_insn (dst, temp);
2479 done = true;
2480 start++;
2485 /* If the first element wasn't the low part, try the last. */
2486 if (!done
2487 && start < finish - 1)
2489 inner = GET_MODE (tmps[finish - 1]);
2490 bytepos = subreg_lowpart_offset (inner, outer);
2491 if (known_eq (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)),
2492 bytepos))
2494 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2495 inner, 0);
2496 if (temp)
2498 emit_move_insn (dst, temp);
2499 done = true;
2500 finish--;
2505 /* Otherwise, simply initialize the result to zero. */
2506 if (!done)
2507 emit_move_insn (dst, CONST0_RTX (outer));
2510 /* Process the pieces. */
2511 for (i = start; i < finish; i++)
2513 poly_int64 bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2514 machine_mode mode = GET_MODE (tmps[i]);
2515 poly_int64 bytelen = GET_MODE_SIZE (mode);
2516 poly_uint64 adj_bytelen;
2517 rtx dest = dst;
2519 /* Handle trailing fragments that run over the size of the struct.
2520 It's the target's responsibility to make sure that the fragment
2521 cannot be strictly smaller in some cases and strictly larger
2522 in others. */
2523 gcc_checking_assert (ordered_p (bytepos + bytelen, ssize));
2524 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2525 adj_bytelen = ssize - bytepos;
2526 else
2527 adj_bytelen = bytelen;
2529 if (GET_CODE (dst) == CONCAT)
2531 if (known_le (bytepos + adj_bytelen,
2532 GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2533 dest = XEXP (dst, 0);
2534 else if (known_ge (bytepos, GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)))))
2536 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2537 dest = XEXP (dst, 1);
2539 else
2541 machine_mode dest_mode = GET_MODE (dest);
2542 machine_mode tmp_mode = GET_MODE (tmps[i]);
2544 gcc_assert (known_eq (bytepos, 0) && XVECLEN (src, 0));
2546 if (GET_MODE_ALIGNMENT (dest_mode)
2547 >= GET_MODE_ALIGNMENT (tmp_mode))
2549 dest = assign_stack_temp (dest_mode,
2550 GET_MODE_SIZE (dest_mode));
2551 emit_move_insn (adjust_address (dest,
2552 tmp_mode,
2553 bytepos),
2554 tmps[i]);
2555 dst = dest;
2557 else
2559 dest = assign_stack_temp (tmp_mode,
2560 GET_MODE_SIZE (tmp_mode));
2561 emit_move_insn (dest, tmps[i]);
2562 dst = adjust_address (dest, dest_mode, bytepos);
2564 break;
2568 /* Handle trailing fragments that run over the size of the struct. */
2569 if (known_size_p (ssize) && maybe_gt (bytepos + bytelen, ssize))
2571 /* store_bit_field always takes its value from the lsb.
2572 Move the fragment to the lsb if it's not already there. */
2573 if (
2574 #ifdef BLOCK_REG_PADDING
2575 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2576 == (BYTES_BIG_ENDIAN ? PAD_UPWARD : PAD_DOWNWARD)
2577 #else
2578 BYTES_BIG_ENDIAN
2579 #endif
2582 poly_int64 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2583 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2584 shift, tmps[i], 0);
2587 /* Make sure not to write past the end of the struct. */
2588 store_bit_field (dest,
2589 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2590 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2591 VOIDmode, tmps[i], false);
2594 /* Optimize the access just a bit. */
2595 else if (MEM_P (dest)
2596 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (dest))
2597 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2598 && multiple_p (bytepos * BITS_PER_UNIT,
2599 GET_MODE_ALIGNMENT (mode))
2600 && known_eq (bytelen, GET_MODE_SIZE (mode)))
2601 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2603 else
2604 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2605 0, 0, mode, tmps[i], false);
2608 /* Copy from the pseudo into the (probable) hard reg. */
2609 if (orig_dst != dst)
2610 emit_move_insn (orig_dst, dst);
2613 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2614 of the value stored in X. */
2617 maybe_emit_group_store (rtx x, tree type)
2619 machine_mode mode = TYPE_MODE (type);
2620 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2621 if (GET_CODE (x) == PARALLEL)
2623 rtx result = gen_reg_rtx (mode);
2624 emit_group_store (result, x, type, int_size_in_bytes (type));
2625 return result;
2627 return x;
2630 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2632 This is used on targets that return BLKmode values in registers. */
2634 static void
2635 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2637 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2638 rtx src = NULL, dst = NULL;
2639 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2640 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2641 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2642 fixed_size_mode mode = as_a <fixed_size_mode> (GET_MODE (srcreg));
2643 fixed_size_mode tmode = as_a <fixed_size_mode> (GET_MODE (target));
2644 fixed_size_mode copy_mode;
2646 /* BLKmode registers created in the back-end shouldn't have survived. */
2647 gcc_assert (mode != BLKmode);
2649 /* If the structure doesn't take up a whole number of words, see whether
2650 SRCREG is padded on the left or on the right. If it's on the left,
2651 set PADDING_CORRECTION to the number of bits to skip.
2653 In most ABIs, the structure will be returned at the least end of
2654 the register, which translates to right padding on little-endian
2655 targets and left padding on big-endian targets. The opposite
2656 holds if the structure is returned at the most significant
2657 end of the register. */
2658 if (bytes % UNITS_PER_WORD != 0
2659 && (targetm.calls.return_in_msb (type)
2660 ? !BYTES_BIG_ENDIAN
2661 : BYTES_BIG_ENDIAN))
2662 padding_correction
2663 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2665 /* We can use a single move if we have an exact mode for the size. */
2666 else if (MEM_P (target)
2667 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (target))
2668 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2669 && bytes == GET_MODE_SIZE (mode))
2671 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2672 return;
2675 /* And if we additionally have the same mode for a register. */
2676 else if (REG_P (target)
2677 && GET_MODE (target) == mode
2678 && bytes == GET_MODE_SIZE (mode))
2680 emit_move_insn (target, srcreg);
2681 return;
2684 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2685 into a new pseudo which is a full word. */
2686 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2688 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2689 mode = word_mode;
2692 /* Copy the structure BITSIZE bits at a time. If the target lives in
2693 memory, take care of not reading/writing past its end by selecting
2694 a copy mode suited to BITSIZE. This should always be possible given
2695 how it is computed.
2697 If the target lives in register, make sure not to select a copy mode
2698 larger than the mode of the register.
2700 We could probably emit more efficient code for machines which do not use
2701 strict alignment, but it doesn't seem worth the effort at the current
2702 time. */
2704 copy_mode = word_mode;
2705 if (MEM_P (target))
2707 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2708 if (mem_mode.exists ())
2709 copy_mode = mem_mode.require ();
2711 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2712 copy_mode = tmode;
2714 for (bitpos = 0, xbitpos = padding_correction;
2715 bitpos < bytes * BITS_PER_UNIT;
2716 bitpos += bitsize, xbitpos += bitsize)
2718 /* We need a new source operand each time xbitpos is on a
2719 word boundary and when xbitpos == padding_correction
2720 (the first time through). */
2721 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2722 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2724 /* We need a new destination operand each time bitpos is on
2725 a word boundary. */
2726 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2727 dst = target;
2728 else if (bitpos % BITS_PER_WORD == 0)
2729 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2731 /* Use xbitpos for the source extraction (right justified) and
2732 bitpos for the destination store (left justified). */
2733 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2734 extract_bit_field (src, bitsize,
2735 xbitpos % BITS_PER_WORD, 1,
2736 NULL_RTX, copy_mode, copy_mode,
2737 false, NULL),
2738 false);
2742 /* Copy BLKmode value SRC into a register of mode MODE_IN. Return the
2743 register if it contains any data, otherwise return null.
2745 This is used on targets that return BLKmode values in registers. */
2748 copy_blkmode_to_reg (machine_mode mode_in, tree src)
2750 int i, n_regs;
2751 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2752 unsigned int bitsize;
2753 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2754 /* No current ABI uses variable-sized modes to pass a BLKmnode type. */
2755 fixed_size_mode mode = as_a <fixed_size_mode> (mode_in);
2756 fixed_size_mode dst_mode;
2758 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2760 x = expand_normal (src);
2762 bytes = arg_int_size_in_bytes (TREE_TYPE (src));
2763 if (bytes == 0)
2764 return NULL_RTX;
2766 /* If the structure doesn't take up a whole number of words, see
2767 whether the register value should be padded on the left or on
2768 the right. Set PADDING_CORRECTION to the number of padding
2769 bits needed on the left side.
2771 In most ABIs, the structure will be returned at the least end of
2772 the register, which translates to right padding on little-endian
2773 targets and left padding on big-endian targets. The opposite
2774 holds if the structure is returned at the most significant
2775 end of the register. */
2776 if (bytes % UNITS_PER_WORD != 0
2777 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2778 ? !BYTES_BIG_ENDIAN
2779 : BYTES_BIG_ENDIAN))
2780 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2781 * BITS_PER_UNIT));
2783 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2784 dst_words = XALLOCAVEC (rtx, n_regs);
2785 bitsize = BITS_PER_WORD;
2786 if (targetm.slow_unaligned_access (word_mode, TYPE_ALIGN (TREE_TYPE (src))))
2787 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2789 /* Copy the structure BITSIZE bits at a time. */
2790 for (bitpos = 0, xbitpos = padding_correction;
2791 bitpos < bytes * BITS_PER_UNIT;
2792 bitpos += bitsize, xbitpos += bitsize)
2794 /* We need a new destination pseudo each time xbitpos is
2795 on a word boundary and when xbitpos == padding_correction
2796 (the first time through). */
2797 if (xbitpos % BITS_PER_WORD == 0
2798 || xbitpos == padding_correction)
2800 /* Generate an appropriate register. */
2801 dst_word = gen_reg_rtx (word_mode);
2802 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2804 /* Clear the destination before we move anything into it. */
2805 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2808 /* We need a new source operand each time bitpos is on a word
2809 boundary. */
2810 if (bitpos % BITS_PER_WORD == 0)
2811 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2813 /* Use bitpos for the source extraction (left justified) and
2814 xbitpos for the destination store (right justified). */
2815 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2816 0, 0, word_mode,
2817 extract_bit_field (src_word, bitsize,
2818 bitpos % BITS_PER_WORD, 1,
2819 NULL_RTX, word_mode, word_mode,
2820 false, NULL),
2821 false);
2824 if (mode == BLKmode)
2826 /* Find the smallest integer mode large enough to hold the
2827 entire structure. */
2828 opt_scalar_int_mode mode_iter;
2829 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2830 if (GET_MODE_SIZE (mode_iter.require ()) >= bytes)
2831 break;
2833 /* A suitable mode should have been found. */
2834 mode = mode_iter.require ();
2837 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2838 dst_mode = word_mode;
2839 else
2840 dst_mode = mode;
2841 dst = gen_reg_rtx (dst_mode);
2843 for (i = 0; i < n_regs; i++)
2844 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2846 if (mode != dst_mode)
2847 dst = gen_lowpart (mode, dst);
2849 return dst;
2852 /* Add a USE expression for REG to the (possibly empty) list pointed
2853 to by CALL_FUSAGE. REG must denote a hard register. */
2855 void
2856 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2858 gcc_assert (REG_P (reg));
2860 if (!HARD_REGISTER_P (reg))
2861 return;
2863 *call_fusage
2864 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2867 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2868 to by CALL_FUSAGE. REG must denote a hard register. */
2870 void
2871 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2873 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2875 *call_fusage
2876 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2879 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2880 starting at REGNO. All of these registers must be hard registers. */
2882 void
2883 use_regs (rtx *call_fusage, int regno, int nregs)
2885 int i;
2887 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2889 for (i = 0; i < nregs; i++)
2890 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2893 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2894 PARALLEL REGS. This is for calls that pass values in multiple
2895 non-contiguous locations. The Irix 6 ABI has examples of this. */
2897 void
2898 use_group_regs (rtx *call_fusage, rtx regs)
2900 int i;
2902 for (i = 0; i < XVECLEN (regs, 0); i++)
2904 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2906 /* A NULL entry means the parameter goes both on the stack and in
2907 registers. This can also be a MEM for targets that pass values
2908 partially on the stack and partially in registers. */
2909 if (reg != 0 && REG_P (reg))
2910 use_reg (call_fusage, reg);
2914 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2915 assigment and the code of the expresion on the RHS is CODE. Return
2916 NULL otherwise. */
2918 static gimple *
2919 get_def_for_expr (tree name, enum tree_code code)
2921 gimple *def_stmt;
2923 if (TREE_CODE (name) != SSA_NAME)
2924 return NULL;
2926 def_stmt = get_gimple_for_ssa_name (name);
2927 if (!def_stmt
2928 || gimple_assign_rhs_code (def_stmt) != code)
2929 return NULL;
2931 return def_stmt;
2934 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2935 assigment and the class of the expresion on the RHS is CLASS. Return
2936 NULL otherwise. */
2938 static gimple *
2939 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2941 gimple *def_stmt;
2943 if (TREE_CODE (name) != SSA_NAME)
2944 return NULL;
2946 def_stmt = get_gimple_for_ssa_name (name);
2947 if (!def_stmt
2948 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2949 return NULL;
2951 return def_stmt;
2954 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2955 its length in bytes. */
2958 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2959 unsigned int expected_align, HOST_WIDE_INT expected_size,
2960 unsigned HOST_WIDE_INT min_size,
2961 unsigned HOST_WIDE_INT max_size,
2962 unsigned HOST_WIDE_INT probable_max_size)
2964 machine_mode mode = GET_MODE (object);
2965 unsigned int align;
2967 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2969 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2970 just move a zero. Otherwise, do this a piece at a time. */
2971 if (mode != BLKmode
2972 && CONST_INT_P (size)
2973 && known_eq (INTVAL (size), GET_MODE_SIZE (mode)))
2975 rtx zero = CONST0_RTX (mode);
2976 if (zero != NULL)
2978 emit_move_insn (object, zero);
2979 return NULL;
2982 if (COMPLEX_MODE_P (mode))
2984 zero = CONST0_RTX (GET_MODE_INNER (mode));
2985 if (zero != NULL)
2987 write_complex_part (object, zero, 0);
2988 write_complex_part (object, zero, 1);
2989 return NULL;
2994 if (size == const0_rtx)
2995 return NULL;
2997 align = MEM_ALIGN (object);
2999 if (CONST_INT_P (size)
3000 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
3001 CLEAR_BY_PIECES,
3002 optimize_insn_for_speed_p ()))
3003 clear_by_pieces (object, INTVAL (size), align);
3004 else if (set_storage_via_setmem (object, size, const0_rtx, align,
3005 expected_align, expected_size,
3006 min_size, max_size, probable_max_size))
3008 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
3009 return set_storage_via_libcall (object, size, const0_rtx,
3010 method == BLOCK_OP_TAILCALL);
3011 else
3012 gcc_unreachable ();
3014 return NULL;
3018 clear_storage (rtx object, rtx size, enum block_op_methods method)
3020 unsigned HOST_WIDE_INT max, min = 0;
3021 if (GET_CODE (size) == CONST_INT)
3022 min = max = UINTVAL (size);
3023 else
3024 max = GET_MODE_MASK (GET_MODE (size));
3025 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
3029 /* A subroutine of clear_storage. Expand a call to memset.
3030 Return the return value of memset, 0 otherwise. */
3033 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3035 tree call_expr, fn, object_tree, size_tree, val_tree;
3036 machine_mode size_mode;
3038 object = copy_addr_to_reg (XEXP (object, 0));
3039 object_tree = make_tree (ptr_type_node, object);
3041 if (!CONST_INT_P (val))
3042 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3043 val_tree = make_tree (integer_type_node, val);
3045 size_mode = TYPE_MODE (sizetype);
3046 size = convert_to_mode (size_mode, size, 1);
3047 size = copy_to_mode_reg (size_mode, size);
3048 size_tree = make_tree (sizetype, size);
3050 /* It is incorrect to use the libcall calling conventions for calls to
3051 memset because it can be provided by the user. */
3052 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3053 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3054 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3056 return expand_call (call_expr, NULL_RTX, false);
3059 /* Expand a setmem pattern; return true if successful. */
3061 bool
3062 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3063 unsigned int expected_align, HOST_WIDE_INT expected_size,
3064 unsigned HOST_WIDE_INT min_size,
3065 unsigned HOST_WIDE_INT max_size,
3066 unsigned HOST_WIDE_INT probable_max_size)
3068 /* Try the most limited insn first, because there's no point
3069 including more than one in the machine description unless
3070 the more limited one has some advantage. */
3072 if (expected_align < align)
3073 expected_align = align;
3074 if (expected_size != -1)
3076 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3077 expected_size = max_size;
3078 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3079 expected_size = min_size;
3082 opt_scalar_int_mode mode_iter;
3083 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
3085 scalar_int_mode mode = mode_iter.require ();
3086 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3088 if (code != CODE_FOR_nothing
3089 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3090 here because if SIZE is less than the mode mask, as it is
3091 returned by the macro, it will definitely be less than the
3092 actual mode mask. Since SIZE is within the Pmode address
3093 space, we limit MODE to Pmode. */
3094 && ((CONST_INT_P (size)
3095 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3096 <= (GET_MODE_MASK (mode) >> 1)))
3097 || max_size <= (GET_MODE_MASK (mode) >> 1)
3098 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3100 struct expand_operand ops[9];
3101 unsigned int nops;
3103 nops = insn_data[(int) code].n_generator_args;
3104 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3106 create_fixed_operand (&ops[0], object);
3107 /* The check above guarantees that this size conversion is valid. */
3108 create_convert_operand_to (&ops[1], size, mode, true);
3109 create_convert_operand_from (&ops[2], val, byte_mode, true);
3110 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3111 if (nops >= 6)
3113 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3114 create_integer_operand (&ops[5], expected_size);
3116 if (nops >= 8)
3118 create_integer_operand (&ops[6], min_size);
3119 /* If we can not represent the maximal size,
3120 make parameter NULL. */
3121 if ((HOST_WIDE_INT) max_size != -1)
3122 create_integer_operand (&ops[7], max_size);
3123 else
3124 create_fixed_operand (&ops[7], NULL);
3126 if (nops == 9)
3128 /* If we can not represent the maximal size,
3129 make parameter NULL. */
3130 if ((HOST_WIDE_INT) probable_max_size != -1)
3131 create_integer_operand (&ops[8], probable_max_size);
3132 else
3133 create_fixed_operand (&ops[8], NULL);
3135 if (maybe_expand_insn (code, nops, ops))
3136 return true;
3140 return false;
3144 /* Write to one of the components of the complex value CPLX. Write VAL to
3145 the real part if IMAG_P is false, and the imaginary part if its true. */
3147 void
3148 write_complex_part (rtx cplx, rtx val, bool imag_p)
3150 machine_mode cmode;
3151 scalar_mode imode;
3152 unsigned ibitsize;
3154 if (GET_CODE (cplx) == CONCAT)
3156 emit_move_insn (XEXP (cplx, imag_p), val);
3157 return;
3160 cmode = GET_MODE (cplx);
3161 imode = GET_MODE_INNER (cmode);
3162 ibitsize = GET_MODE_BITSIZE (imode);
3164 /* For MEMs simplify_gen_subreg may generate an invalid new address
3165 because, e.g., the original address is considered mode-dependent
3166 by the target, which restricts simplify_subreg from invoking
3167 adjust_address_nv. Instead of preparing fallback support for an
3168 invalid address, we call adjust_address_nv directly. */
3169 if (MEM_P (cplx))
3171 emit_move_insn (adjust_address_nv (cplx, imode,
3172 imag_p ? GET_MODE_SIZE (imode) : 0),
3173 val);
3174 return;
3177 /* If the sub-object is at least word sized, then we know that subregging
3178 will work. This special case is important, since store_bit_field
3179 wants to operate on integer modes, and there's rarely an OImode to
3180 correspond to TCmode. */
3181 if (ibitsize >= BITS_PER_WORD
3182 /* For hard regs we have exact predicates. Assume we can split
3183 the original object if it spans an even number of hard regs.
3184 This special case is important for SCmode on 64-bit platforms
3185 where the natural size of floating-point regs is 32-bit. */
3186 || (REG_P (cplx)
3187 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3188 && REG_NREGS (cplx) % 2 == 0))
3190 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3191 imag_p ? GET_MODE_SIZE (imode) : 0);
3192 if (part)
3194 emit_move_insn (part, val);
3195 return;
3197 else
3198 /* simplify_gen_subreg may fail for sub-word MEMs. */
3199 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3202 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3203 false);
3206 /* Extract one of the components of the complex value CPLX. Extract the
3207 real part if IMAG_P is false, and the imaginary part if it's true. */
3210 read_complex_part (rtx cplx, bool imag_p)
3212 machine_mode cmode;
3213 scalar_mode imode;
3214 unsigned ibitsize;
3216 if (GET_CODE (cplx) == CONCAT)
3217 return XEXP (cplx, imag_p);
3219 cmode = GET_MODE (cplx);
3220 imode = GET_MODE_INNER (cmode);
3221 ibitsize = GET_MODE_BITSIZE (imode);
3223 /* Special case reads from complex constants that got spilled to memory. */
3224 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3226 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3227 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3229 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3230 if (CONSTANT_CLASS_P (part))
3231 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3235 /* For MEMs simplify_gen_subreg may generate an invalid new address
3236 because, e.g., the original address is considered mode-dependent
3237 by the target, which restricts simplify_subreg from invoking
3238 adjust_address_nv. Instead of preparing fallback support for an
3239 invalid address, we call adjust_address_nv directly. */
3240 if (MEM_P (cplx))
3241 return adjust_address_nv (cplx, imode,
3242 imag_p ? GET_MODE_SIZE (imode) : 0);
3244 /* If the sub-object is at least word sized, then we know that subregging
3245 will work. This special case is important, since extract_bit_field
3246 wants to operate on integer modes, and there's rarely an OImode to
3247 correspond to TCmode. */
3248 if (ibitsize >= BITS_PER_WORD
3249 /* For hard regs we have exact predicates. Assume we can split
3250 the original object if it spans an even number of hard regs.
3251 This special case is important for SCmode on 64-bit platforms
3252 where the natural size of floating-point regs is 32-bit. */
3253 || (REG_P (cplx)
3254 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3255 && REG_NREGS (cplx) % 2 == 0))
3257 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3258 imag_p ? GET_MODE_SIZE (imode) : 0);
3259 if (ret)
3260 return ret;
3261 else
3262 /* simplify_gen_subreg may fail for sub-word MEMs. */
3263 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3266 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3267 true, NULL_RTX, imode, imode, false, NULL);
3270 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3271 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3272 represented in NEW_MODE. If FORCE is true, this will never happen, as
3273 we'll force-create a SUBREG if needed. */
3275 static rtx
3276 emit_move_change_mode (machine_mode new_mode,
3277 machine_mode old_mode, rtx x, bool force)
3279 rtx ret;
3281 if (push_operand (x, GET_MODE (x)))
3283 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3284 MEM_COPY_ATTRIBUTES (ret, x);
3286 else if (MEM_P (x))
3288 /* We don't have to worry about changing the address since the
3289 size in bytes is supposed to be the same. */
3290 if (reload_in_progress)
3292 /* Copy the MEM to change the mode and move any
3293 substitutions from the old MEM to the new one. */
3294 ret = adjust_address_nv (x, new_mode, 0);
3295 copy_replacements (x, ret);
3297 else
3298 ret = adjust_address (x, new_mode, 0);
3300 else
3302 /* Note that we do want simplify_subreg's behavior of validating
3303 that the new mode is ok for a hard register. If we were to use
3304 simplify_gen_subreg, we would create the subreg, but would
3305 probably run into the target not being able to implement it. */
3306 /* Except, of course, when FORCE is true, when this is exactly what
3307 we want. Which is needed for CCmodes on some targets. */
3308 if (force)
3309 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3310 else
3311 ret = simplify_subreg (new_mode, x, old_mode, 0);
3314 return ret;
3317 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3318 an integer mode of the same size as MODE. Returns the instruction
3319 emitted, or NULL if such a move could not be generated. */
3321 static rtx_insn *
3322 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3324 scalar_int_mode imode;
3325 enum insn_code code;
3327 /* There must exist a mode of the exact size we require. */
3328 if (!int_mode_for_mode (mode).exists (&imode))
3329 return NULL;
3331 /* The target must support moves in this mode. */
3332 code = optab_handler (mov_optab, imode);
3333 if (code == CODE_FOR_nothing)
3334 return NULL;
3336 x = emit_move_change_mode (imode, mode, x, force);
3337 if (x == NULL_RTX)
3338 return NULL;
3339 y = emit_move_change_mode (imode, mode, y, force);
3340 if (y == NULL_RTX)
3341 return NULL;
3342 return emit_insn (GEN_FCN (code) (x, y));
3345 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3346 Return an equivalent MEM that does not use an auto-increment. */
3349 emit_move_resolve_push (machine_mode mode, rtx x)
3351 enum rtx_code code = GET_CODE (XEXP (x, 0));
3352 rtx temp;
3354 poly_int64 adjust = GET_MODE_SIZE (mode);
3355 #ifdef PUSH_ROUNDING
3356 adjust = PUSH_ROUNDING (adjust);
3357 #endif
3358 if (code == PRE_DEC || code == POST_DEC)
3359 adjust = -adjust;
3360 else if (code == PRE_MODIFY || code == POST_MODIFY)
3362 rtx expr = XEXP (XEXP (x, 0), 1);
3364 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3365 poly_int64 val = rtx_to_poly_int64 (XEXP (expr, 1));
3366 if (GET_CODE (expr) == MINUS)
3367 val = -val;
3368 gcc_assert (known_eq (adjust, val) || known_eq (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 poly_int64 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 (maybe_ne (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_mode (GET_MODE_SIZE (mode), Pmode),
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, mode_size;
3578 /* This function can only handle cases where the number of words is
3579 known at compile time. */
3580 mode_size = GET_MODE_SIZE (mode).to_constant ();
3581 gcc_assert (mode_size >= UNITS_PER_WORD);
3583 /* If X is a push on the stack, do the push now and replace
3584 X with a reference to the stack pointer. */
3585 if (push_operand (x, mode))
3586 x = emit_move_resolve_push (mode, x);
3588 /* If we are in reload, see if either operand is a MEM whose address
3589 is scheduled for replacement. */
3590 if (reload_in_progress && MEM_P (x)
3591 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3592 x = replace_equiv_address_nv (x, inner);
3593 if (reload_in_progress && MEM_P (y)
3594 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3595 y = replace_equiv_address_nv (y, inner);
3597 start_sequence ();
3599 need_clobber = false;
3600 for (i = 0; i < CEIL (mode_size, UNITS_PER_WORD); i++)
3602 rtx xpart = operand_subword (x, i, 1, mode);
3603 rtx ypart;
3605 /* Do not generate code for a move if it would come entirely
3606 from the undefined bits of a paradoxical subreg. */
3607 if (undefined_operand_subword_p (y, i))
3608 continue;
3610 ypart = operand_subword (y, i, 1, mode);
3612 /* If we can't get a part of Y, put Y into memory if it is a
3613 constant. Otherwise, force it into a register. Then we must
3614 be able to get a part of Y. */
3615 if (ypart == 0 && CONSTANT_P (y))
3617 y = use_anchored_address (force_const_mem (mode, y));
3618 ypart = operand_subword (y, i, 1, mode);
3620 else if (ypart == 0)
3621 ypart = operand_subword_force (y, i, mode);
3623 gcc_assert (xpart && ypart);
3625 need_clobber |= (GET_CODE (xpart) == SUBREG);
3627 last_insn = emit_move_insn (xpart, ypart);
3630 seq = get_insns ();
3631 end_sequence ();
3633 /* Show the output dies here. This is necessary for SUBREGs
3634 of pseudos since we cannot track their lifetimes correctly;
3635 hard regs shouldn't appear here except as return values.
3636 We never want to emit such a clobber after reload. */
3637 if (x != y
3638 && ! (reload_in_progress || reload_completed)
3639 && need_clobber != 0)
3640 emit_clobber (x);
3642 emit_insn (seq);
3644 return last_insn;
3647 /* Low level part of emit_move_insn.
3648 Called just like emit_move_insn, but assumes X and Y
3649 are basically valid. */
3651 rtx_insn *
3652 emit_move_insn_1 (rtx x, rtx y)
3654 machine_mode mode = GET_MODE (x);
3655 enum insn_code code;
3657 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3659 code = optab_handler (mov_optab, mode);
3660 if (code != CODE_FOR_nothing)
3661 return emit_insn (GEN_FCN (code) (x, y));
3663 /* Expand complex moves by moving real part and imag part. */
3664 if (COMPLEX_MODE_P (mode))
3665 return emit_move_complex (mode, x, y);
3667 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3668 || ALL_FIXED_POINT_MODE_P (mode))
3670 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3672 /* If we can't find an integer mode, use multi words. */
3673 if (result)
3674 return result;
3675 else
3676 return emit_move_multi_word (mode, x, y);
3679 if (GET_MODE_CLASS (mode) == MODE_CC)
3680 return emit_move_ccmode (mode, x, y);
3682 /* Try using a move pattern for the corresponding integer mode. This is
3683 only safe when simplify_subreg can convert MODE constants into integer
3684 constants. At present, it can only do this reliably if the value
3685 fits within a HOST_WIDE_INT. */
3686 if (!CONSTANT_P (y)
3687 || known_le (GET_MODE_BITSIZE (mode), HOST_BITS_PER_WIDE_INT))
3689 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3691 if (ret)
3693 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3694 return ret;
3698 return emit_move_multi_word (mode, x, y);
3701 /* Generate code to copy Y into X.
3702 Both Y and X must have the same mode, except that
3703 Y can be a constant with VOIDmode.
3704 This mode cannot be BLKmode; use emit_block_move for that.
3706 Return the last instruction emitted. */
3708 rtx_insn *
3709 emit_move_insn (rtx x, rtx y)
3711 machine_mode mode = GET_MODE (x);
3712 rtx y_cst = NULL_RTX;
3713 rtx_insn *last_insn;
3714 rtx set;
3716 gcc_assert (mode != BLKmode
3717 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3719 if (CONSTANT_P (y))
3721 if (optimize
3722 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3723 && (last_insn = compress_float_constant (x, y)))
3724 return last_insn;
3726 y_cst = y;
3728 if (!targetm.legitimate_constant_p (mode, y))
3730 y = force_const_mem (mode, y);
3732 /* If the target's cannot_force_const_mem prevented the spill,
3733 assume that the target's move expanders will also take care
3734 of the non-legitimate constant. */
3735 if (!y)
3736 y = y_cst;
3737 else
3738 y = use_anchored_address (y);
3742 /* If X or Y are memory references, verify that their addresses are valid
3743 for the machine. */
3744 if (MEM_P (x)
3745 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3746 MEM_ADDR_SPACE (x))
3747 && ! push_operand (x, GET_MODE (x))))
3748 x = validize_mem (x);
3750 if (MEM_P (y)
3751 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3752 MEM_ADDR_SPACE (y)))
3753 y = validize_mem (y);
3755 gcc_assert (mode != BLKmode);
3757 last_insn = emit_move_insn_1 (x, y);
3759 if (y_cst && REG_P (x)
3760 && (set = single_set (last_insn)) != NULL_RTX
3761 && SET_DEST (set) == x
3762 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3763 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3765 return last_insn;
3768 /* Generate the body of an instruction to copy Y into X.
3769 It may be a list of insns, if one insn isn't enough. */
3771 rtx_insn *
3772 gen_move_insn (rtx x, rtx y)
3774 rtx_insn *seq;
3776 start_sequence ();
3777 emit_move_insn_1 (x, y);
3778 seq = get_insns ();
3779 end_sequence ();
3780 return seq;
3783 /* If Y is representable exactly in a narrower mode, and the target can
3784 perform the extension directly from constant or memory, then emit the
3785 move as an extension. */
3787 static rtx_insn *
3788 compress_float_constant (rtx x, rtx y)
3790 machine_mode dstmode = GET_MODE (x);
3791 machine_mode orig_srcmode = GET_MODE (y);
3792 machine_mode srcmode;
3793 const REAL_VALUE_TYPE *r;
3794 int oldcost, newcost;
3795 bool speed = optimize_insn_for_speed_p ();
3797 r = CONST_DOUBLE_REAL_VALUE (y);
3799 if (targetm.legitimate_constant_p (dstmode, y))
3800 oldcost = set_src_cost (y, orig_srcmode, speed);
3801 else
3802 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3804 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3806 enum insn_code ic;
3807 rtx trunc_y;
3808 rtx_insn *last_insn;
3810 /* Skip if the target can't extend this way. */
3811 ic = can_extend_p (dstmode, srcmode, 0);
3812 if (ic == CODE_FOR_nothing)
3813 continue;
3815 /* Skip if the narrowed value isn't exact. */
3816 if (! exact_real_truncate (srcmode, r))
3817 continue;
3819 trunc_y = const_double_from_real_value (*r, srcmode);
3821 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3823 /* Skip if the target needs extra instructions to perform
3824 the extension. */
3825 if (!insn_operand_matches (ic, 1, trunc_y))
3826 continue;
3827 /* This is valid, but may not be cheaper than the original. */
3828 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3829 dstmode, speed);
3830 if (oldcost < newcost)
3831 continue;
3833 else if (float_extend_from_mem[dstmode][srcmode])
3835 trunc_y = force_const_mem (srcmode, trunc_y);
3836 /* This is valid, but may not be cheaper than the original. */
3837 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3838 dstmode, speed);
3839 if (oldcost < newcost)
3840 continue;
3841 trunc_y = validize_mem (trunc_y);
3843 else
3844 continue;
3846 /* For CSE's benefit, force the compressed constant pool entry
3847 into a new pseudo. This constant may be used in different modes,
3848 and if not, combine will put things back together for us. */
3849 trunc_y = force_reg (srcmode, trunc_y);
3851 /* If x is a hard register, perform the extension into a pseudo,
3852 so that e.g. stack realignment code is aware of it. */
3853 rtx target = x;
3854 if (REG_P (x) && HARD_REGISTER_P (x))
3855 target = gen_reg_rtx (dstmode);
3857 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3858 last_insn = get_last_insn ();
3860 if (REG_P (target))
3861 set_unique_reg_note (last_insn, REG_EQUAL, y);
3863 if (target != x)
3864 return emit_move_insn (x, target);
3865 return last_insn;
3868 return NULL;
3871 /* Pushing data onto the stack. */
3873 /* Push a block of length SIZE (perhaps variable)
3874 and return an rtx to address the beginning of the block.
3875 The value may be virtual_outgoing_args_rtx.
3877 EXTRA is the number of bytes of padding to push in addition to SIZE.
3878 BELOW nonzero means this padding comes at low addresses;
3879 otherwise, the padding comes at high addresses. */
3882 push_block (rtx size, poly_int64 extra, int below)
3884 rtx temp;
3886 size = convert_modes (Pmode, ptr_mode, size, 1);
3887 if (CONSTANT_P (size))
3888 anti_adjust_stack (plus_constant (Pmode, size, extra));
3889 else if (REG_P (size) && known_eq (extra, 0))
3890 anti_adjust_stack (size);
3891 else
3893 temp = copy_to_mode_reg (Pmode, size);
3894 if (maybe_ne (extra, 0))
3895 temp = expand_binop (Pmode, add_optab, temp,
3896 gen_int_mode (extra, Pmode),
3897 temp, 0, OPTAB_LIB_WIDEN);
3898 anti_adjust_stack (temp);
3901 if (STACK_GROWS_DOWNWARD)
3903 temp = virtual_outgoing_args_rtx;
3904 if (maybe_ne (extra, 0) && below)
3905 temp = plus_constant (Pmode, temp, extra);
3907 else
3909 if (CONST_INT_P (size))
3910 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3911 -INTVAL (size) - (below ? 0 : extra));
3912 else if (maybe_ne (extra, 0) && !below)
3913 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3914 negate_rtx (Pmode, plus_constant (Pmode, size,
3915 extra)));
3916 else
3917 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3918 negate_rtx (Pmode, size));
3921 return memory_address (NARROWEST_INT_MODE, temp);
3924 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3926 static rtx
3927 mem_autoinc_base (rtx mem)
3929 if (MEM_P (mem))
3931 rtx addr = XEXP (mem, 0);
3932 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3933 return XEXP (addr, 0);
3935 return NULL;
3938 /* A utility routine used here, in reload, and in try_split. The insns
3939 after PREV up to and including LAST are known to adjust the stack,
3940 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3941 placing notes as appropriate. PREV may be NULL, indicating the
3942 entire insn sequence prior to LAST should be scanned.
3944 The set of allowed stack pointer modifications is small:
3945 (1) One or more auto-inc style memory references (aka pushes),
3946 (2) One or more addition/subtraction with the SP as destination,
3947 (3) A single move insn with the SP as destination,
3948 (4) A call_pop insn,
3949 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3951 Insns in the sequence that do not modify the SP are ignored,
3952 except for noreturn calls.
3954 The return value is the amount of adjustment that can be trivially
3955 verified, via immediate operand or auto-inc. If the adjustment
3956 cannot be trivially extracted, the return value is HOST_WIDE_INT_MIN. */
3958 poly_int64
3959 find_args_size_adjust (rtx_insn *insn)
3961 rtx dest, set, pat;
3962 int i;
3964 pat = PATTERN (insn);
3965 set = NULL;
3967 /* Look for a call_pop pattern. */
3968 if (CALL_P (insn))
3970 /* We have to allow non-call_pop patterns for the case
3971 of emit_single_push_insn of a TLS address. */
3972 if (GET_CODE (pat) != PARALLEL)
3973 return 0;
3975 /* All call_pop have a stack pointer adjust in the parallel.
3976 The call itself is always first, and the stack adjust is
3977 usually last, so search from the end. */
3978 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3980 set = XVECEXP (pat, 0, i);
3981 if (GET_CODE (set) != SET)
3982 continue;
3983 dest = SET_DEST (set);
3984 if (dest == stack_pointer_rtx)
3985 break;
3987 /* We'd better have found the stack pointer adjust. */
3988 if (i == 0)
3989 return 0;
3990 /* Fall through to process the extracted SET and DEST
3991 as if it was a standalone insn. */
3993 else if (GET_CODE (pat) == SET)
3994 set = pat;
3995 else if ((set = single_set (insn)) != NULL)
3997 else if (GET_CODE (pat) == PARALLEL)
3999 /* ??? Some older ports use a parallel with a stack adjust
4000 and a store for a PUSH_ROUNDING pattern, rather than a
4001 PRE/POST_MODIFY rtx. Don't force them to update yet... */
4002 /* ??? See h8300 and m68k, pushqi1. */
4003 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
4005 set = XVECEXP (pat, 0, i);
4006 if (GET_CODE (set) != SET)
4007 continue;
4008 dest = SET_DEST (set);
4009 if (dest == stack_pointer_rtx)
4010 break;
4012 /* We do not expect an auto-inc of the sp in the parallel. */
4013 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4014 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4015 != stack_pointer_rtx);
4017 if (i < 0)
4018 return 0;
4020 else
4021 return 0;
4023 dest = SET_DEST (set);
4025 /* Look for direct modifications of the stack pointer. */
4026 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4028 /* Look for a trivial adjustment, otherwise assume nothing. */
4029 /* Note that the SPU restore_stack_block pattern refers to
4030 the stack pointer in V4SImode. Consider that non-trivial. */
4031 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4032 && GET_CODE (SET_SRC (set)) == PLUS
4033 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4034 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4035 return INTVAL (XEXP (SET_SRC (set), 1));
4036 /* ??? Reload can generate no-op moves, which will be cleaned
4037 up later. Recognize it and continue searching. */
4038 else if (rtx_equal_p (dest, SET_SRC (set)))
4039 return 0;
4040 else
4041 return HOST_WIDE_INT_MIN;
4043 else
4045 rtx mem, addr;
4047 /* Otherwise only think about autoinc patterns. */
4048 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4050 mem = dest;
4051 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4052 != stack_pointer_rtx);
4054 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4055 mem = SET_SRC (set);
4056 else
4057 return 0;
4059 addr = XEXP (mem, 0);
4060 switch (GET_CODE (addr))
4062 case PRE_INC:
4063 case POST_INC:
4064 return GET_MODE_SIZE (GET_MODE (mem));
4065 case PRE_DEC:
4066 case POST_DEC:
4067 return -GET_MODE_SIZE (GET_MODE (mem));
4068 case PRE_MODIFY:
4069 case POST_MODIFY:
4070 addr = XEXP (addr, 1);
4071 gcc_assert (GET_CODE (addr) == PLUS);
4072 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4073 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4074 return INTVAL (XEXP (addr, 1));
4075 default:
4076 gcc_unreachable ();
4081 poly_int64
4082 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last,
4083 poly_int64 end_args_size)
4085 poly_int64 args_size = end_args_size;
4086 bool saw_unknown = false;
4087 rtx_insn *insn;
4089 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4091 if (!NONDEBUG_INSN_P (insn))
4092 continue;
4094 /* We might have existing REG_ARGS_SIZE notes, e.g. when pushing
4095 a call argument containing a TLS address that itself requires
4096 a call to __tls_get_addr. The handling of stack_pointer_delta
4097 in emit_single_push_insn is supposed to ensure that any such
4098 notes are already correct. */
4099 rtx note = find_reg_note (insn, REG_ARGS_SIZE, NULL_RTX);
4100 gcc_assert (!note || known_eq (args_size, get_args_size (note)));
4102 poly_int64 this_delta = find_args_size_adjust (insn);
4103 if (known_eq (this_delta, 0))
4105 if (!CALL_P (insn)
4106 || ACCUMULATE_OUTGOING_ARGS
4107 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4108 continue;
4111 gcc_assert (!saw_unknown);
4112 if (known_eq (this_delta, HOST_WIDE_INT_MIN))
4113 saw_unknown = true;
4115 if (!note)
4116 add_args_size_note (insn, args_size);
4117 if (STACK_GROWS_DOWNWARD)
4118 this_delta = -poly_uint64 (this_delta);
4120 if (saw_unknown)
4121 args_size = HOST_WIDE_INT_MIN;
4122 else
4123 args_size -= this_delta;
4126 return args_size;
4129 #ifdef PUSH_ROUNDING
4130 /* Emit single push insn. */
4132 static void
4133 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4135 rtx dest_addr;
4136 poly_int64 rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4137 rtx dest;
4138 enum insn_code icode;
4140 /* If there is push pattern, use it. Otherwise try old way of throwing
4141 MEM representing push operation to move expander. */
4142 icode = optab_handler (push_optab, mode);
4143 if (icode != CODE_FOR_nothing)
4145 struct expand_operand ops[1];
4147 create_input_operand (&ops[0], x, mode);
4148 if (maybe_expand_insn (icode, 1, ops))
4149 return;
4151 if (known_eq (GET_MODE_SIZE (mode), rounded_size))
4152 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4153 /* If we are to pad downward, adjust the stack pointer first and
4154 then store X into the stack location using an offset. This is
4155 because emit_move_insn does not know how to pad; it does not have
4156 access to type. */
4157 else if (targetm.calls.function_arg_padding (mode, type) == PAD_DOWNWARD)
4159 emit_move_insn (stack_pointer_rtx,
4160 expand_binop (Pmode,
4161 STACK_GROWS_DOWNWARD ? sub_optab
4162 : add_optab,
4163 stack_pointer_rtx,
4164 gen_int_mode (rounded_size, Pmode),
4165 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4167 poly_int64 offset = rounded_size - GET_MODE_SIZE (mode);
4168 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4169 /* We have already decremented the stack pointer, so get the
4170 previous value. */
4171 offset += rounded_size;
4173 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4174 /* We have already incremented the stack pointer, so get the
4175 previous value. */
4176 offset -= rounded_size;
4178 dest_addr = plus_constant (Pmode, stack_pointer_rtx, offset);
4180 else
4182 if (STACK_GROWS_DOWNWARD)
4183 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4184 dest_addr = plus_constant (Pmode, stack_pointer_rtx, -rounded_size);
4185 else
4186 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4187 dest_addr = plus_constant (Pmode, stack_pointer_rtx, rounded_size);
4189 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4192 dest = gen_rtx_MEM (mode, dest_addr);
4194 if (type != 0)
4196 set_mem_attributes (dest, type, 1);
4198 if (cfun->tail_call_marked)
4199 /* Function incoming arguments may overlap with sibling call
4200 outgoing arguments and we cannot allow reordering of reads
4201 from function arguments with stores to outgoing arguments
4202 of sibling calls. */
4203 set_mem_alias_set (dest, 0);
4205 emit_move_insn (dest, x);
4208 /* Emit and annotate a single push insn. */
4210 static void
4211 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4213 poly_int64 delta, old_delta = stack_pointer_delta;
4214 rtx_insn *prev = get_last_insn ();
4215 rtx_insn *last;
4217 emit_single_push_insn_1 (mode, x, type);
4219 /* Adjust stack_pointer_delta to describe the situation after the push
4220 we just performed. Note that we must do this after the push rather
4221 than before the push in case calculating X needs pushes and pops of
4222 its own (e.g. if calling __tls_get_addr). The REG_ARGS_SIZE notes
4223 for such pushes and pops must not include the effect of the future
4224 push of X. */
4225 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4227 last = get_last_insn ();
4229 /* Notice the common case where we emitted exactly one insn. */
4230 if (PREV_INSN (last) == prev)
4232 add_args_size_note (last, stack_pointer_delta);
4233 return;
4236 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4237 gcc_assert (known_eq (delta, HOST_WIDE_INT_MIN)
4238 || known_eq (delta, old_delta));
4240 #endif
4242 /* If reading SIZE bytes from X will end up reading from
4243 Y return the number of bytes that overlap. Return -1
4244 if there is no overlap or -2 if we can't determine
4245 (for example when X and Y have different base registers). */
4247 static int
4248 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4250 rtx tmp = plus_constant (Pmode, x, size);
4251 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4253 if (!CONST_INT_P (sub))
4254 return -2;
4256 HOST_WIDE_INT val = INTVAL (sub);
4258 return IN_RANGE (val, 1, size) ? val : -1;
4261 /* Generate code to push X onto the stack, assuming it has mode MODE and
4262 type TYPE.
4263 MODE is redundant except when X is a CONST_INT (since they don't
4264 carry mode info).
4265 SIZE is an rtx for the size of data to be copied (in bytes),
4266 needed only if X is BLKmode.
4267 Return true if successful. May return false if asked to push a
4268 partial argument during a sibcall optimization (as specified by
4269 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4270 to not overlap.
4272 ALIGN (in bits) is maximum alignment we can assume.
4274 If PARTIAL and REG are both nonzero, then copy that many of the first
4275 bytes of X into registers starting with REG, and push the rest of X.
4276 The amount of space pushed is decreased by PARTIAL bytes.
4277 REG must be a hard register in this case.
4278 If REG is zero but PARTIAL is not, take any all others actions for an
4279 argument partially in registers, but do not actually load any
4280 registers.
4282 EXTRA is the amount in bytes of extra space to leave next to this arg.
4283 This is ignored if an argument block has already been allocated.
4285 On a machine that lacks real push insns, ARGS_ADDR is the address of
4286 the bottom of the argument block for this call. We use indexing off there
4287 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4288 argument block has not been preallocated.
4290 ARGS_SO_FAR is the size of args previously pushed for this call.
4292 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4293 for arguments passed in registers. If nonzero, it will be the number
4294 of bytes required. */
4296 bool
4297 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4298 unsigned int align, int partial, rtx reg, poly_int64 extra,
4299 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4300 rtx alignment_pad, bool sibcall_p)
4302 rtx xinner;
4303 pad_direction stack_direction
4304 = STACK_GROWS_DOWNWARD ? PAD_DOWNWARD : PAD_UPWARD;
4306 /* Decide where to pad the argument: PAD_DOWNWARD for below,
4307 PAD_UPWARD for above, or PAD_NONE for don't pad it.
4308 Default is below for small data on big-endian machines; else above. */
4309 pad_direction where_pad = targetm.calls.function_arg_padding (mode, type);
4311 /* Invert direction if stack is post-decrement.
4312 FIXME: why? */
4313 if (STACK_PUSH_CODE == POST_DEC)
4314 if (where_pad != PAD_NONE)
4315 where_pad = (where_pad == PAD_DOWNWARD ? PAD_UPWARD : PAD_DOWNWARD);
4317 xinner = x;
4319 int nregs = partial / UNITS_PER_WORD;
4320 rtx *tmp_regs = NULL;
4321 int overlapping = 0;
4323 if (mode == BLKmode
4324 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4326 /* Copy a block into the stack, entirely or partially. */
4328 rtx temp;
4329 int used;
4330 int offset;
4331 int skip;
4333 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4334 used = partial - offset;
4336 if (mode != BLKmode)
4338 /* A value is to be stored in an insufficiently aligned
4339 stack slot; copy via a suitably aligned slot if
4340 necessary. */
4341 size = gen_int_mode (GET_MODE_SIZE (mode), Pmode);
4342 if (!MEM_P (xinner))
4344 temp = assign_temp (type, 1, 1);
4345 emit_move_insn (temp, xinner);
4346 xinner = temp;
4350 gcc_assert (size);
4352 /* USED is now the # of bytes we need not copy to the stack
4353 because registers will take care of them. */
4355 if (partial != 0)
4356 xinner = adjust_address (xinner, BLKmode, used);
4358 /* If the partial register-part of the arg counts in its stack size,
4359 skip the part of stack space corresponding to the registers.
4360 Otherwise, start copying to the beginning of the stack space,
4361 by setting SKIP to 0. */
4362 skip = (reg_parm_stack_space == 0) ? 0 : used;
4364 #ifdef PUSH_ROUNDING
4365 /* Do it with several push insns if that doesn't take lots of insns
4366 and if there is no difficulty with push insns that skip bytes
4367 on the stack for alignment purposes. */
4368 if (args_addr == 0
4369 && PUSH_ARGS
4370 && CONST_INT_P (size)
4371 && skip == 0
4372 && MEM_ALIGN (xinner) >= align
4373 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4374 /* Here we avoid the case of a structure whose weak alignment
4375 forces many pushes of a small amount of data,
4376 and such small pushes do rounding that causes trouble. */
4377 && ((!targetm.slow_unaligned_access (word_mode, align))
4378 || align >= BIGGEST_ALIGNMENT
4379 || known_eq (PUSH_ROUNDING (align / BITS_PER_UNIT),
4380 align / BITS_PER_UNIT))
4381 && known_eq (PUSH_ROUNDING (INTVAL (size)), INTVAL (size)))
4383 /* Push padding now if padding above and stack grows down,
4384 or if padding below and stack grows up.
4385 But if space already allocated, this has already been done. */
4386 if (maybe_ne (extra, 0)
4387 && args_addr == 0
4388 && where_pad != PAD_NONE
4389 && where_pad != stack_direction)
4390 anti_adjust_stack (gen_int_mode (extra, Pmode));
4392 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4394 else
4395 #endif /* PUSH_ROUNDING */
4397 rtx target;
4399 /* Otherwise make space on the stack and copy the data
4400 to the address of that space. */
4402 /* Deduct words put into registers from the size we must copy. */
4403 if (partial != 0)
4405 if (CONST_INT_P (size))
4406 size = GEN_INT (INTVAL (size) - used);
4407 else
4408 size = expand_binop (GET_MODE (size), sub_optab, size,
4409 gen_int_mode (used, GET_MODE (size)),
4410 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4413 /* Get the address of the stack space.
4414 In this case, we do not deal with EXTRA separately.
4415 A single stack adjust will do. */
4416 if (! args_addr)
4418 temp = push_block (size, extra, where_pad == PAD_DOWNWARD);
4419 extra = 0;
4421 else if (CONST_INT_P (args_so_far))
4422 temp = memory_address (BLKmode,
4423 plus_constant (Pmode, args_addr,
4424 skip + INTVAL (args_so_far)));
4425 else
4426 temp = memory_address (BLKmode,
4427 plus_constant (Pmode,
4428 gen_rtx_PLUS (Pmode,
4429 args_addr,
4430 args_so_far),
4431 skip));
4433 if (!ACCUMULATE_OUTGOING_ARGS)
4435 /* If the source is referenced relative to the stack pointer,
4436 copy it to another register to stabilize it. We do not need
4437 to do this if we know that we won't be changing sp. */
4439 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4440 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4441 temp = copy_to_reg (temp);
4444 target = gen_rtx_MEM (BLKmode, temp);
4446 /* We do *not* set_mem_attributes here, because incoming arguments
4447 may overlap with sibling call outgoing arguments and we cannot
4448 allow reordering of reads from function arguments with stores
4449 to outgoing arguments of sibling calls. We do, however, want
4450 to record the alignment of the stack slot. */
4451 /* ALIGN may well be better aligned than TYPE, e.g. due to
4452 PARM_BOUNDARY. Assume the caller isn't lying. */
4453 set_mem_align (target, align);
4455 /* If part should go in registers and pushing to that part would
4456 overwrite some of the values that need to go into regs, load the
4457 overlapping values into temporary pseudos to be moved into the hard
4458 regs at the end after the stack pushing has completed.
4459 We cannot load them directly into the hard regs here because
4460 they can be clobbered by the block move expansions.
4461 See PR 65358. */
4463 if (partial > 0 && reg != 0 && mode == BLKmode
4464 && GET_CODE (reg) != PARALLEL)
4466 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4467 if (overlapping > 0)
4469 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4470 overlapping /= UNITS_PER_WORD;
4472 tmp_regs = XALLOCAVEC (rtx, overlapping);
4474 for (int i = 0; i < overlapping; i++)
4475 tmp_regs[i] = gen_reg_rtx (word_mode);
4477 for (int i = 0; i < overlapping; i++)
4478 emit_move_insn (tmp_regs[i],
4479 operand_subword_force (target, i, mode));
4481 else if (overlapping == -1)
4482 overlapping = 0;
4483 /* Could not determine whether there is overlap.
4484 Fail the sibcall. */
4485 else
4487 overlapping = 0;
4488 if (sibcall_p)
4489 return false;
4492 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4495 else if (partial > 0)
4497 /* Scalar partly in registers. This case is only supported
4498 for fixed-wdth modes. */
4499 int size = GET_MODE_SIZE (mode).to_constant ();
4500 size /= UNITS_PER_WORD;
4501 int i;
4502 int not_stack;
4503 /* # bytes of start of argument
4504 that we must make space for but need not store. */
4505 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4506 int args_offset = INTVAL (args_so_far);
4507 int skip;
4509 /* Push padding now if padding above and stack grows down,
4510 or if padding below and stack grows up.
4511 But if space already allocated, this has already been done. */
4512 if (maybe_ne (extra, 0)
4513 && args_addr == 0
4514 && where_pad != PAD_NONE
4515 && where_pad != stack_direction)
4516 anti_adjust_stack (gen_int_mode (extra, Pmode));
4518 /* If we make space by pushing it, we might as well push
4519 the real data. Otherwise, we can leave OFFSET nonzero
4520 and leave the space uninitialized. */
4521 if (args_addr == 0)
4522 offset = 0;
4524 /* Now NOT_STACK gets the number of words that we don't need to
4525 allocate on the stack. Convert OFFSET to words too. */
4526 not_stack = (partial - offset) / UNITS_PER_WORD;
4527 offset /= UNITS_PER_WORD;
4529 /* If the partial register-part of the arg counts in its stack size,
4530 skip the part of stack space corresponding to the registers.
4531 Otherwise, start copying to the beginning of the stack space,
4532 by setting SKIP to 0. */
4533 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4535 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4536 x = validize_mem (force_const_mem (mode, x));
4538 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4539 SUBREGs of such registers are not allowed. */
4540 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4541 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4542 x = copy_to_reg (x);
4544 /* Loop over all the words allocated on the stack for this arg. */
4545 /* We can do it by words, because any scalar bigger than a word
4546 has a size a multiple of a word. */
4547 for (i = size - 1; i >= not_stack; i--)
4548 if (i >= not_stack + offset)
4549 if (!emit_push_insn (operand_subword_force (x, i, mode),
4550 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4551 0, args_addr,
4552 GEN_INT (args_offset + ((i - not_stack + skip)
4553 * UNITS_PER_WORD)),
4554 reg_parm_stack_space, alignment_pad, sibcall_p))
4555 return false;
4557 else
4559 rtx addr;
4560 rtx dest;
4562 /* Push padding now if padding above and stack grows down,
4563 or if padding below and stack grows up.
4564 But if space already allocated, this has already been done. */
4565 if (maybe_ne (extra, 0)
4566 && args_addr == 0
4567 && where_pad != PAD_NONE
4568 && where_pad != stack_direction)
4569 anti_adjust_stack (gen_int_mode (extra, Pmode));
4571 #ifdef PUSH_ROUNDING
4572 if (args_addr == 0 && PUSH_ARGS)
4573 emit_single_push_insn (mode, x, type);
4574 else
4575 #endif
4577 addr = simplify_gen_binary (PLUS, Pmode, args_addr, args_so_far);
4578 dest = gen_rtx_MEM (mode, memory_address (mode, addr));
4580 /* We do *not* set_mem_attributes here, because incoming arguments
4581 may overlap with sibling call outgoing arguments and we cannot
4582 allow reordering of reads from function arguments with stores
4583 to outgoing arguments of sibling calls. We do, however, want
4584 to record the alignment of the stack slot. */
4585 /* ALIGN may well be better aligned than TYPE, e.g. due to
4586 PARM_BOUNDARY. Assume the caller isn't lying. */
4587 set_mem_align (dest, align);
4589 emit_move_insn (dest, x);
4593 /* Move the partial arguments into the registers and any overlapping
4594 values that we moved into the pseudos in tmp_regs. */
4595 if (partial > 0 && reg != 0)
4597 /* Handle calls that pass values in multiple non-contiguous locations.
4598 The Irix 6 ABI has examples of this. */
4599 if (GET_CODE (reg) == PARALLEL)
4600 emit_group_load (reg, x, type, -1);
4601 else
4603 gcc_assert (partial % UNITS_PER_WORD == 0);
4604 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4606 for (int i = 0; i < overlapping; i++)
4607 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4608 + nregs - overlapping + i),
4609 tmp_regs[i]);
4614 if (maybe_ne (extra, 0) && args_addr == 0 && where_pad == stack_direction)
4615 anti_adjust_stack (gen_int_mode (extra, Pmode));
4617 if (alignment_pad && args_addr == 0)
4618 anti_adjust_stack (alignment_pad);
4620 return true;
4623 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4624 operations. */
4626 static rtx
4627 get_subtarget (rtx x)
4629 return (optimize
4630 || x == 0
4631 /* Only registers can be subtargets. */
4632 || !REG_P (x)
4633 /* Don't use hard regs to avoid extending their life. */
4634 || REGNO (x) < FIRST_PSEUDO_REGISTER
4635 ? 0 : x);
4638 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4639 FIELD is a bitfield. Returns true if the optimization was successful,
4640 and there's nothing else to do. */
4642 static bool
4643 optimize_bitfield_assignment_op (poly_uint64 pbitsize,
4644 poly_uint64 pbitpos,
4645 poly_uint64 pbitregion_start,
4646 poly_uint64 pbitregion_end,
4647 machine_mode mode1, rtx str_rtx,
4648 tree to, tree src, bool reverse)
4650 /* str_mode is not guaranteed to be a scalar type. */
4651 machine_mode str_mode = GET_MODE (str_rtx);
4652 unsigned int str_bitsize;
4653 tree op0, op1;
4654 rtx value, result;
4655 optab binop;
4656 gimple *srcstmt;
4657 enum tree_code code;
4659 unsigned HOST_WIDE_INT bitsize, bitpos, bitregion_start, bitregion_end;
4660 if (mode1 != VOIDmode
4661 || !pbitsize.is_constant (&bitsize)
4662 || !pbitpos.is_constant (&bitpos)
4663 || !pbitregion_start.is_constant (&bitregion_start)
4664 || !pbitregion_end.is_constant (&bitregion_end)
4665 || bitsize >= BITS_PER_WORD
4666 || !GET_MODE_BITSIZE (str_mode).is_constant (&str_bitsize)
4667 || str_bitsize > BITS_PER_WORD
4668 || TREE_SIDE_EFFECTS (to)
4669 || TREE_THIS_VOLATILE (to))
4670 return false;
4672 STRIP_NOPS (src);
4673 if (TREE_CODE (src) != SSA_NAME)
4674 return false;
4675 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4676 return false;
4678 srcstmt = get_gimple_for_ssa_name (src);
4679 if (!srcstmt
4680 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4681 return false;
4683 code = gimple_assign_rhs_code (srcstmt);
4685 op0 = gimple_assign_rhs1 (srcstmt);
4687 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4688 to find its initialization. Hopefully the initialization will
4689 be from a bitfield load. */
4690 if (TREE_CODE (op0) == SSA_NAME)
4692 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4694 /* We want to eventually have OP0 be the same as TO, which
4695 should be a bitfield. */
4696 if (!op0stmt
4697 || !is_gimple_assign (op0stmt)
4698 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4699 return false;
4700 op0 = gimple_assign_rhs1 (op0stmt);
4703 op1 = gimple_assign_rhs2 (srcstmt);
4705 if (!operand_equal_p (to, op0, 0))
4706 return false;
4708 if (MEM_P (str_rtx))
4710 unsigned HOST_WIDE_INT offset1;
4712 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4713 str_bitsize = BITS_PER_WORD;
4715 scalar_int_mode best_mode;
4716 if (!get_best_mode (bitsize, bitpos, bitregion_start, bitregion_end,
4717 MEM_ALIGN (str_rtx), str_bitsize, false, &best_mode))
4718 return false;
4719 str_mode = best_mode;
4720 str_bitsize = GET_MODE_BITSIZE (best_mode);
4722 offset1 = bitpos;
4723 bitpos %= str_bitsize;
4724 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4725 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4727 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4728 return false;
4730 /* If the bit field covers the whole REG/MEM, store_field
4731 will likely generate better code. */
4732 if (bitsize >= str_bitsize)
4733 return false;
4735 /* We can't handle fields split across multiple entities. */
4736 if (bitpos + bitsize > str_bitsize)
4737 return false;
4739 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4740 bitpos = str_bitsize - bitpos - bitsize;
4742 switch (code)
4744 case PLUS_EXPR:
4745 case MINUS_EXPR:
4746 /* For now, just optimize the case of the topmost bitfield
4747 where we don't need to do any masking and also
4748 1 bit bitfields where xor can be used.
4749 We might win by one instruction for the other bitfields
4750 too if insv/extv instructions aren't used, so that
4751 can be added later. */
4752 if ((reverse || bitpos + bitsize != str_bitsize)
4753 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4754 break;
4756 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4757 value = convert_modes (str_mode,
4758 TYPE_MODE (TREE_TYPE (op1)), value,
4759 TYPE_UNSIGNED (TREE_TYPE (op1)));
4761 /* We may be accessing data outside the field, which means
4762 we can alias adjacent data. */
4763 if (MEM_P (str_rtx))
4765 str_rtx = shallow_copy_rtx (str_rtx);
4766 set_mem_alias_set (str_rtx, 0);
4767 set_mem_expr (str_rtx, 0);
4770 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4772 value = expand_and (str_mode, value, const1_rtx, NULL);
4773 binop = xor_optab;
4775 else
4776 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4778 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4779 if (reverse)
4780 value = flip_storage_order (str_mode, value);
4781 result = expand_binop (str_mode, binop, str_rtx,
4782 value, str_rtx, 1, OPTAB_WIDEN);
4783 if (result != str_rtx)
4784 emit_move_insn (str_rtx, result);
4785 return true;
4787 case BIT_IOR_EXPR:
4788 case BIT_XOR_EXPR:
4789 if (TREE_CODE (op1) != INTEGER_CST)
4790 break;
4791 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4792 value = convert_modes (str_mode,
4793 TYPE_MODE (TREE_TYPE (op1)), value,
4794 TYPE_UNSIGNED (TREE_TYPE (op1)));
4796 /* We may be accessing data outside the field, which means
4797 we can alias adjacent data. */
4798 if (MEM_P (str_rtx))
4800 str_rtx = shallow_copy_rtx (str_rtx);
4801 set_mem_alias_set (str_rtx, 0);
4802 set_mem_expr (str_rtx, 0);
4805 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4806 if (bitpos + bitsize != str_bitsize)
4808 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4809 str_mode);
4810 value = expand_and (str_mode, value, mask, NULL_RTX);
4812 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4813 if (reverse)
4814 value = flip_storage_order (str_mode, value);
4815 result = expand_binop (str_mode, binop, str_rtx,
4816 value, str_rtx, 1, OPTAB_WIDEN);
4817 if (result != str_rtx)
4818 emit_move_insn (str_rtx, result);
4819 return true;
4821 default:
4822 break;
4825 return false;
4828 /* In the C++ memory model, consecutive bit fields in a structure are
4829 considered one memory location.
4831 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4832 returns the bit range of consecutive bits in which this COMPONENT_REF
4833 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4834 and *OFFSET may be adjusted in the process.
4836 If the access does not need to be restricted, 0 is returned in both
4837 *BITSTART and *BITEND. */
4839 void
4840 get_bit_range (poly_uint64_pod *bitstart, poly_uint64_pod *bitend, tree exp,
4841 poly_int64_pod *bitpos, tree *offset)
4843 poly_int64 bitoffset;
4844 tree field, repr;
4846 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4848 field = TREE_OPERAND (exp, 1);
4849 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4850 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4851 need to limit the range we can access. */
4852 if (!repr)
4854 *bitstart = *bitend = 0;
4855 return;
4858 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4859 part of a larger bit field, then the representative does not serve any
4860 useful purpose. This can occur in Ada. */
4861 if (handled_component_p (TREE_OPERAND (exp, 0)))
4863 machine_mode rmode;
4864 poly_int64 rbitsize, rbitpos;
4865 tree roffset;
4866 int unsignedp, reversep, volatilep = 0;
4867 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4868 &roffset, &rmode, &unsignedp, &reversep,
4869 &volatilep);
4870 if (!multiple_p (rbitpos, BITS_PER_UNIT))
4872 *bitstart = *bitend = 0;
4873 return;
4877 /* Compute the adjustment to bitpos from the offset of the field
4878 relative to the representative. DECL_FIELD_OFFSET of field and
4879 repr are the same by construction if they are not constants,
4880 see finish_bitfield_layout. */
4881 poly_uint64 field_offset, repr_offset;
4882 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
4883 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
4884 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
4885 else
4886 bitoffset = 0;
4887 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4888 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4890 /* If the adjustment is larger than bitpos, we would have a negative bit
4891 position for the lower bound and this may wreak havoc later. Adjust
4892 offset and bitpos to make the lower bound non-negative in that case. */
4893 if (maybe_gt (bitoffset, *bitpos))
4895 poly_int64 adjust_bits = upper_bound (bitoffset, *bitpos) - *bitpos;
4896 poly_int64 adjust_bytes = exact_div (adjust_bits, BITS_PER_UNIT);
4898 *bitpos += adjust_bits;
4899 if (*offset == NULL_TREE)
4900 *offset = size_int (-adjust_bytes);
4901 else
4902 *offset = size_binop (MINUS_EXPR, *offset, size_int (adjust_bytes));
4903 *bitstart = 0;
4905 else
4906 *bitstart = *bitpos - bitoffset;
4908 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4911 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4912 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4913 DECL_RTL was not set yet, return NORTL. */
4915 static inline bool
4916 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4918 if (TREE_CODE (addr) != ADDR_EXPR)
4919 return false;
4921 tree base = TREE_OPERAND (addr, 0);
4923 if (!DECL_P (base)
4924 || TREE_ADDRESSABLE (base)
4925 || DECL_MODE (base) == BLKmode)
4926 return false;
4928 if (!DECL_RTL_SET_P (base))
4929 return nortl;
4931 return (!MEM_P (DECL_RTL (base)));
4934 /* Returns true if the MEM_REF REF refers to an object that does not
4935 reside in memory and has non-BLKmode. */
4937 static inline bool
4938 mem_ref_refers_to_non_mem_p (tree ref)
4940 tree base = TREE_OPERAND (ref, 0);
4941 return addr_expr_of_non_mem_decl_p_1 (base, false);
4944 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4945 is true, try generating a nontemporal store. */
4947 void
4948 expand_assignment (tree to, tree from, bool nontemporal)
4950 rtx to_rtx = 0;
4951 rtx result;
4952 machine_mode mode;
4953 unsigned int align;
4954 enum insn_code icode;
4956 /* Don't crash if the lhs of the assignment was erroneous. */
4957 if (TREE_CODE (to) == ERROR_MARK)
4959 expand_normal (from);
4960 return;
4963 /* Optimize away no-op moves without side-effects. */
4964 if (operand_equal_p (to, from, 0))
4965 return;
4967 /* Handle misaligned stores. */
4968 mode = TYPE_MODE (TREE_TYPE (to));
4969 if ((TREE_CODE (to) == MEM_REF
4970 || TREE_CODE (to) == TARGET_MEM_REF)
4971 && mode != BLKmode
4972 && !mem_ref_refers_to_non_mem_p (to)
4973 && ((align = get_object_alignment (to))
4974 < GET_MODE_ALIGNMENT (mode))
4975 && (((icode = optab_handler (movmisalign_optab, mode))
4976 != CODE_FOR_nothing)
4977 || targetm.slow_unaligned_access (mode, align)))
4979 rtx reg, mem;
4981 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4982 reg = force_not_mem (reg);
4983 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4984 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4985 reg = flip_storage_order (mode, reg);
4987 if (icode != CODE_FOR_nothing)
4989 struct expand_operand ops[2];
4991 create_fixed_operand (&ops[0], mem);
4992 create_input_operand (&ops[1], reg, mode);
4993 /* The movmisalign<mode> pattern cannot fail, else the assignment
4994 would silently be omitted. */
4995 expand_insn (icode, 2, ops);
4997 else
4998 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4999 false);
5000 return;
5003 /* Assignment of a structure component needs special treatment
5004 if the structure component's rtx is not simply a MEM.
5005 Assignment of an array element at a constant index, and assignment of
5006 an array element in an unaligned packed structure field, has the same
5007 problem. Same for (partially) storing into a non-memory object. */
5008 if (handled_component_p (to)
5009 || (TREE_CODE (to) == MEM_REF
5010 && (REF_REVERSE_STORAGE_ORDER (to)
5011 || mem_ref_refers_to_non_mem_p (to)))
5012 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
5014 machine_mode mode1;
5015 poly_int64 bitsize, bitpos;
5016 poly_uint64 bitregion_start = 0;
5017 poly_uint64 bitregion_end = 0;
5018 tree offset;
5019 int unsignedp, reversep, volatilep = 0;
5020 tree tem;
5022 push_temp_slots ();
5023 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
5024 &unsignedp, &reversep, &volatilep);
5026 /* Make sure bitpos is not negative, it can wreak havoc later. */
5027 if (maybe_lt (bitpos, 0))
5029 gcc_assert (offset == NULL_TREE);
5030 offset = size_int (bits_to_bytes_round_down (bitpos));
5031 bitpos = num_trailing_bits (bitpos);
5034 if (TREE_CODE (to) == COMPONENT_REF
5035 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5036 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5037 /* The C++ memory model naturally applies to byte-aligned fields.
5038 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5039 BITSIZE are not byte-aligned, there is no need to limit the range
5040 we can access. This can occur with packed structures in Ada. */
5041 else if (maybe_gt (bitsize, 0)
5042 && multiple_p (bitsize, BITS_PER_UNIT)
5043 && multiple_p (bitpos, BITS_PER_UNIT))
5045 bitregion_start = bitpos;
5046 bitregion_end = bitpos + bitsize - 1;
5049 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5051 /* If the field has a mode, we want to access it in the
5052 field's mode, not the computed mode.
5053 If a MEM has VOIDmode (external with incomplete type),
5054 use BLKmode for it instead. */
5055 if (MEM_P (to_rtx))
5057 if (mode1 != VOIDmode)
5058 to_rtx = adjust_address (to_rtx, mode1, 0);
5059 else if (GET_MODE (to_rtx) == VOIDmode)
5060 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5063 if (offset != 0)
5065 machine_mode address_mode;
5066 rtx offset_rtx;
5068 if (!MEM_P (to_rtx))
5070 /* We can get constant negative offsets into arrays with broken
5071 user code. Translate this to a trap instead of ICEing. */
5072 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5073 expand_builtin_trap ();
5074 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5077 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5078 address_mode = get_address_mode (to_rtx);
5079 if (GET_MODE (offset_rtx) != address_mode)
5081 /* We cannot be sure that the RTL in offset_rtx is valid outside
5082 of a memory address context, so force it into a register
5083 before attempting to convert it to the desired mode. */
5084 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5085 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5088 /* If we have an expression in OFFSET_RTX and a non-zero
5089 byte offset in BITPOS, adding the byte offset before the
5090 OFFSET_RTX results in better intermediate code, which makes
5091 later rtl optimization passes perform better.
5093 We prefer intermediate code like this:
5095 r124:DI=r123:DI+0x18
5096 [r124:DI]=r121:DI
5098 ... instead of ...
5100 r124:DI=r123:DI+0x10
5101 [r124:DI+0x8]=r121:DI
5103 This is only done for aligned data values, as these can
5104 be expected to result in single move instructions. */
5105 poly_int64 bytepos;
5106 if (mode1 != VOIDmode
5107 && maybe_ne (bitpos, 0)
5108 && maybe_gt (bitsize, 0)
5109 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
5110 && multiple_p (bitpos, bitsize)
5111 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
5112 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5114 to_rtx = adjust_address (to_rtx, mode1, bytepos);
5115 bitregion_start = 0;
5116 if (known_ge (bitregion_end, poly_uint64 (bitpos)))
5117 bitregion_end -= bitpos;
5118 bitpos = 0;
5121 to_rtx = offset_address (to_rtx, offset_rtx,
5122 highest_pow2_factor_for_target (to,
5123 offset));
5126 /* No action is needed if the target is not a memory and the field
5127 lies completely outside that target. This can occur if the source
5128 code contains an out-of-bounds access to a small array. */
5129 if (!MEM_P (to_rtx)
5130 && GET_MODE (to_rtx) != BLKmode
5131 && known_ge (bitpos, GET_MODE_PRECISION (GET_MODE (to_rtx))))
5133 expand_normal (from);
5134 result = NULL;
5136 /* Handle expand_expr of a complex value returning a CONCAT. */
5137 else if (GET_CODE (to_rtx) == CONCAT)
5139 machine_mode to_mode = GET_MODE (to_rtx);
5140 gcc_checking_assert (COMPLEX_MODE_P (to_mode));
5141 poly_int64 mode_bitsize = GET_MODE_BITSIZE (to_mode);
5142 unsigned short inner_bitsize = GET_MODE_UNIT_BITSIZE (to_mode);
5143 if (TYPE_MODE (TREE_TYPE (from)) == GET_MODE (to_rtx)
5144 && COMPLEX_MODE_P (GET_MODE (to_rtx))
5145 && known_eq (bitpos, 0)
5146 && known_eq (bitsize, mode_bitsize))
5147 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5148 else if (known_eq (bitsize, inner_bitsize)
5149 && (known_eq (bitpos, 0)
5150 || known_eq (bitpos, inner_bitsize)))
5151 result = store_expr (from, XEXP (to_rtx, maybe_ne (bitpos, 0)),
5152 false, nontemporal, reversep);
5153 else if (known_le (bitpos + bitsize, inner_bitsize))
5154 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5155 bitregion_start, bitregion_end,
5156 mode1, from, get_alias_set (to),
5157 nontemporal, reversep);
5158 else if (known_ge (bitpos, inner_bitsize))
5159 result = store_field (XEXP (to_rtx, 1), bitsize,
5160 bitpos - inner_bitsize,
5161 bitregion_start, bitregion_end,
5162 mode1, from, get_alias_set (to),
5163 nontemporal, reversep);
5164 else if (known_eq (bitpos, 0) && known_eq (bitsize, mode_bitsize))
5166 result = expand_normal (from);
5167 if (GET_CODE (result) == CONCAT)
5169 to_mode = GET_MODE_INNER (to_mode);
5170 machine_mode from_mode = GET_MODE_INNER (GET_MODE (result));
5171 rtx from_real
5172 = simplify_gen_subreg (to_mode, XEXP (result, 0),
5173 from_mode, 0);
5174 rtx from_imag
5175 = simplify_gen_subreg (to_mode, XEXP (result, 1),
5176 from_mode, 0);
5177 if (!from_real || !from_imag)
5178 goto concat_store_slow;
5179 emit_move_insn (XEXP (to_rtx, 0), from_real);
5180 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5182 else
5184 rtx from_rtx
5185 = simplify_gen_subreg (to_mode, result,
5186 TYPE_MODE (TREE_TYPE (from)), 0);
5187 if (from_rtx)
5189 emit_move_insn (XEXP (to_rtx, 0),
5190 read_complex_part (from_rtx, false));
5191 emit_move_insn (XEXP (to_rtx, 1),
5192 read_complex_part (from_rtx, true));
5194 else
5196 machine_mode to_mode
5197 = GET_MODE_INNER (GET_MODE (to_rtx));
5198 rtx from_real
5199 = simplify_gen_subreg (to_mode, result,
5200 TYPE_MODE (TREE_TYPE (from)),
5202 rtx from_imag
5203 = simplify_gen_subreg (to_mode, result,
5204 TYPE_MODE (TREE_TYPE (from)),
5205 GET_MODE_SIZE (to_mode));
5206 if (!from_real || !from_imag)
5207 goto concat_store_slow;
5208 emit_move_insn (XEXP (to_rtx, 0), from_real);
5209 emit_move_insn (XEXP (to_rtx, 1), from_imag);
5213 else
5215 concat_store_slow:;
5216 rtx temp = assign_stack_temp (to_mode,
5217 GET_MODE_SIZE (GET_MODE (to_rtx)));
5218 write_complex_part (temp, XEXP (to_rtx, 0), false);
5219 write_complex_part (temp, XEXP (to_rtx, 1), true);
5220 result = store_field (temp, bitsize, bitpos,
5221 bitregion_start, bitregion_end,
5222 mode1, from, get_alias_set (to),
5223 nontemporal, reversep);
5224 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5225 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5228 else
5230 if (MEM_P (to_rtx))
5232 /* If the field is at offset zero, we could have been given the
5233 DECL_RTX of the parent struct. Don't munge it. */
5234 to_rtx = shallow_copy_rtx (to_rtx);
5235 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5236 if (volatilep)
5237 MEM_VOLATILE_P (to_rtx) = 1;
5240 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5241 bitregion_start, bitregion_end,
5242 mode1, to_rtx, to, from,
5243 reversep))
5244 result = NULL;
5245 else
5246 result = store_field (to_rtx, bitsize, bitpos,
5247 bitregion_start, bitregion_end,
5248 mode1, from, get_alias_set (to),
5249 nontemporal, reversep);
5252 if (result)
5253 preserve_temp_slots (result);
5254 pop_temp_slots ();
5255 return;
5258 /* If the rhs is a function call and its value is not an aggregate,
5259 call the function before we start to compute the lhs.
5260 This is needed for correct code for cases such as
5261 val = setjmp (buf) on machines where reference to val
5262 requires loading up part of an address in a separate insn.
5264 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5265 since it might be a promoted variable where the zero- or sign- extension
5266 needs to be done. Handling this in the normal way is safe because no
5267 computation is done before the call. The same is true for SSA names. */
5268 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5269 && COMPLETE_TYPE_P (TREE_TYPE (from))
5270 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5271 && ! (((VAR_P (to)
5272 || TREE_CODE (to) == PARM_DECL
5273 || TREE_CODE (to) == RESULT_DECL)
5274 && REG_P (DECL_RTL (to)))
5275 || TREE_CODE (to) == SSA_NAME))
5277 rtx value;
5278 rtx bounds;
5280 push_temp_slots ();
5281 value = expand_normal (from);
5283 /* Split value and bounds to store them separately. */
5284 chkp_split_slot (value, &value, &bounds);
5286 if (to_rtx == 0)
5287 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5289 /* Handle calls that return values in multiple non-contiguous locations.
5290 The Irix 6 ABI has examples of this. */
5291 if (GET_CODE (to_rtx) == PARALLEL)
5293 if (GET_CODE (value) == PARALLEL)
5294 emit_group_move (to_rtx, value);
5295 else
5296 emit_group_load (to_rtx, value, TREE_TYPE (from),
5297 int_size_in_bytes (TREE_TYPE (from)));
5299 else if (GET_CODE (value) == PARALLEL)
5300 emit_group_store (to_rtx, value, TREE_TYPE (from),
5301 int_size_in_bytes (TREE_TYPE (from)));
5302 else if (GET_MODE (to_rtx) == BLKmode)
5304 /* Handle calls that return BLKmode values in registers. */
5305 if (REG_P (value))
5306 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5307 else
5308 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5310 else
5312 if (POINTER_TYPE_P (TREE_TYPE (to)))
5313 value = convert_memory_address_addr_space
5314 (as_a <scalar_int_mode> (GET_MODE (to_rtx)), value,
5315 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5317 emit_move_insn (to_rtx, value);
5320 /* Store bounds if required. */
5321 if (bounds
5322 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5324 gcc_assert (MEM_P (to_rtx));
5325 chkp_emit_bounds_store (bounds, value, to_rtx);
5328 preserve_temp_slots (to_rtx);
5329 pop_temp_slots ();
5330 return;
5333 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5334 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5336 /* Don't move directly into a return register. */
5337 if (TREE_CODE (to) == RESULT_DECL
5338 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5340 rtx temp;
5342 push_temp_slots ();
5344 /* If the source is itself a return value, it still is in a pseudo at
5345 this point so we can move it back to the return register directly. */
5346 if (REG_P (to_rtx)
5347 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5348 && TREE_CODE (from) != CALL_EXPR)
5349 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5350 else
5351 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5353 /* Handle calls that return values in multiple non-contiguous locations.
5354 The Irix 6 ABI has examples of this. */
5355 if (GET_CODE (to_rtx) == PARALLEL)
5357 if (GET_CODE (temp) == PARALLEL)
5358 emit_group_move (to_rtx, temp);
5359 else
5360 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5361 int_size_in_bytes (TREE_TYPE (from)));
5363 else if (temp)
5364 emit_move_insn (to_rtx, temp);
5366 preserve_temp_slots (to_rtx);
5367 pop_temp_slots ();
5368 return;
5371 /* In case we are returning the contents of an object which overlaps
5372 the place the value is being stored, use a safe function when copying
5373 a value through a pointer into a structure value return block. */
5374 if (TREE_CODE (to) == RESULT_DECL
5375 && TREE_CODE (from) == INDIRECT_REF
5376 && ADDR_SPACE_GENERIC_P
5377 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5378 && refs_may_alias_p (to, from)
5379 && cfun->returns_struct
5380 && !cfun->returns_pcc_struct)
5382 rtx from_rtx, size;
5384 push_temp_slots ();
5385 size = expr_size (from);
5386 from_rtx = expand_normal (from);
5388 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5390 preserve_temp_slots (to_rtx);
5391 pop_temp_slots ();
5392 return;
5395 /* Compute FROM and store the value in the rtx we got. */
5397 push_temp_slots ();
5398 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5399 preserve_temp_slots (result);
5400 pop_temp_slots ();
5401 return;
5404 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5405 succeeded, false otherwise. */
5407 bool
5408 emit_storent_insn (rtx to, rtx from)
5410 struct expand_operand ops[2];
5411 machine_mode mode = GET_MODE (to);
5412 enum insn_code code = optab_handler (storent_optab, mode);
5414 if (code == CODE_FOR_nothing)
5415 return false;
5417 create_fixed_operand (&ops[0], to);
5418 create_input_operand (&ops[1], from, mode);
5419 return maybe_expand_insn (code, 2, ops);
5422 /* Generate code for computing expression EXP,
5423 and storing the value into TARGET.
5425 If the mode is BLKmode then we may return TARGET itself.
5426 It turns out that in BLKmode it doesn't cause a problem.
5427 because C has no operators that could combine two different
5428 assignments into the same BLKmode object with different values
5429 with no sequence point. Will other languages need this to
5430 be more thorough?
5432 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5433 stack, and block moves may need to be treated specially.
5435 If NONTEMPORAL is true, try using a nontemporal store instruction.
5437 If REVERSE is true, the store is to be done in reverse order.
5439 If BTARGET is not NULL then computed bounds of EXP are
5440 associated with BTARGET. */
5443 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5444 bool nontemporal, bool reverse, tree btarget)
5446 rtx temp;
5447 rtx alt_rtl = NULL_RTX;
5448 location_t loc = curr_insn_location ();
5450 if (VOID_TYPE_P (TREE_TYPE (exp)))
5452 /* C++ can generate ?: expressions with a throw expression in one
5453 branch and an rvalue in the other. Here, we resolve attempts to
5454 store the throw expression's nonexistent result. */
5455 gcc_assert (!call_param_p);
5456 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5457 return NULL_RTX;
5459 if (TREE_CODE (exp) == COMPOUND_EXPR)
5461 /* Perform first part of compound expression, then assign from second
5462 part. */
5463 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5464 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5465 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5466 call_param_p, nontemporal, reverse,
5467 btarget);
5469 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5471 /* For conditional expression, get safe form of the target. Then
5472 test the condition, doing the appropriate assignment on either
5473 side. This avoids the creation of unnecessary temporaries.
5474 For non-BLKmode, it is more efficient not to do this. */
5476 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5478 do_pending_stack_adjust ();
5479 NO_DEFER_POP;
5480 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5481 profile_probability::uninitialized ());
5482 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5483 nontemporal, reverse, btarget);
5484 emit_jump_insn (targetm.gen_jump (lab2));
5485 emit_barrier ();
5486 emit_label (lab1);
5487 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5488 nontemporal, reverse, btarget);
5489 emit_label (lab2);
5490 OK_DEFER_POP;
5492 return NULL_RTX;
5494 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5495 /* If this is a scalar in a register that is stored in a wider mode
5496 than the declared mode, compute the result into its declared mode
5497 and then convert to the wider mode. Our value is the computed
5498 expression. */
5500 rtx inner_target = 0;
5501 scalar_int_mode outer_mode = subreg_unpromoted_mode (target);
5502 scalar_int_mode inner_mode = subreg_promoted_mode (target);
5504 /* We can do the conversion inside EXP, which will often result
5505 in some optimizations. Do the conversion in two steps: first
5506 change the signedness, if needed, then the extend. But don't
5507 do this if the type of EXP is a subtype of something else
5508 since then the conversion might involve more than just
5509 converting modes. */
5510 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5511 && TREE_TYPE (TREE_TYPE (exp)) == 0
5512 && GET_MODE_PRECISION (outer_mode)
5513 == TYPE_PRECISION (TREE_TYPE (exp)))
5515 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5516 TYPE_UNSIGNED (TREE_TYPE (exp))))
5518 /* Some types, e.g. Fortran's logical*4, won't have a signed
5519 version, so use the mode instead. */
5520 tree ntype
5521 = (signed_or_unsigned_type_for
5522 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5523 if (ntype == NULL)
5524 ntype = lang_hooks.types.type_for_mode
5525 (TYPE_MODE (TREE_TYPE (exp)),
5526 SUBREG_PROMOTED_SIGN (target));
5528 exp = fold_convert_loc (loc, ntype, exp);
5531 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5532 (inner_mode, SUBREG_PROMOTED_SIGN (target)),
5533 exp);
5535 inner_target = SUBREG_REG (target);
5538 temp = expand_expr (exp, inner_target, VOIDmode,
5539 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5541 /* Handle bounds returned by call. */
5542 if (TREE_CODE (exp) == CALL_EXPR)
5544 rtx bounds;
5545 chkp_split_slot (temp, &temp, &bounds);
5546 if (bounds && btarget)
5548 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5549 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5550 chkp_set_rtl_bounds (btarget, tmp);
5554 /* If TEMP is a VOIDmode constant, use convert_modes to make
5555 sure that we properly convert it. */
5556 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5558 temp = convert_modes (outer_mode, TYPE_MODE (TREE_TYPE (exp)),
5559 temp, SUBREG_PROMOTED_SIGN (target));
5560 temp = convert_modes (inner_mode, outer_mode, temp,
5561 SUBREG_PROMOTED_SIGN (target));
5564 convert_move (SUBREG_REG (target), temp,
5565 SUBREG_PROMOTED_SIGN (target));
5567 return NULL_RTX;
5569 else if ((TREE_CODE (exp) == STRING_CST
5570 || (TREE_CODE (exp) == MEM_REF
5571 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5572 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5573 == STRING_CST
5574 && integer_zerop (TREE_OPERAND (exp, 1))))
5575 && !nontemporal && !call_param_p
5576 && MEM_P (target))
5578 /* Optimize initialization of an array with a STRING_CST. */
5579 HOST_WIDE_INT exp_len, str_copy_len;
5580 rtx dest_mem;
5581 tree str = TREE_CODE (exp) == STRING_CST
5582 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5584 exp_len = int_expr_size (exp);
5585 if (exp_len <= 0)
5586 goto normal_expr;
5588 if (TREE_STRING_LENGTH (str) <= 0)
5589 goto normal_expr;
5591 str_copy_len = strlen (TREE_STRING_POINTER (str));
5592 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5593 goto normal_expr;
5595 str_copy_len = TREE_STRING_LENGTH (str);
5596 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5597 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5599 str_copy_len += STORE_MAX_PIECES - 1;
5600 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5602 str_copy_len = MIN (str_copy_len, exp_len);
5603 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5604 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5605 MEM_ALIGN (target), false))
5606 goto normal_expr;
5608 dest_mem = target;
5610 dest_mem = store_by_pieces (dest_mem,
5611 str_copy_len, builtin_strncpy_read_str,
5612 CONST_CAST (char *,
5613 TREE_STRING_POINTER (str)),
5614 MEM_ALIGN (target), false,
5615 exp_len > str_copy_len ? 1 : 0);
5616 if (exp_len > str_copy_len)
5617 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5618 GEN_INT (exp_len - str_copy_len),
5619 BLOCK_OP_NORMAL);
5620 return NULL_RTX;
5622 else
5624 rtx tmp_target;
5626 normal_expr:
5627 /* If we want to use a nontemporal or a reverse order store, force the
5628 value into a register first. */
5629 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5630 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5631 (call_param_p
5632 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5633 &alt_rtl, false);
5635 /* Handle bounds returned by call. */
5636 if (TREE_CODE (exp) == CALL_EXPR)
5638 rtx bounds;
5639 chkp_split_slot (temp, &temp, &bounds);
5640 if (bounds && btarget)
5642 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5643 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5644 chkp_set_rtl_bounds (btarget, tmp);
5649 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5650 the same as that of TARGET, adjust the constant. This is needed, for
5651 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5652 only a word-sized value. */
5653 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5654 && TREE_CODE (exp) != ERROR_MARK
5655 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5657 if (GET_MODE_CLASS (GET_MODE (target))
5658 != GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (exp)))
5659 && known_eq (GET_MODE_BITSIZE (GET_MODE (target)),
5660 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)))))
5662 rtx t = simplify_gen_subreg (GET_MODE (target), temp,
5663 TYPE_MODE (TREE_TYPE (exp)), 0);
5664 if (t)
5665 temp = t;
5667 if (GET_MODE (temp) == VOIDmode)
5668 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5669 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5672 /* If value was not generated in the target, store it there.
5673 Convert the value to TARGET's type first if necessary and emit the
5674 pending incrementations that have been queued when expanding EXP.
5675 Note that we cannot emit the whole queue blindly because this will
5676 effectively disable the POST_INC optimization later.
5678 If TEMP and TARGET compare equal according to rtx_equal_p, but
5679 one or both of them are volatile memory refs, we have to distinguish
5680 two cases:
5681 - expand_expr has used TARGET. In this case, we must not generate
5682 another copy. This can be detected by TARGET being equal according
5683 to == .
5684 - expand_expr has not used TARGET - that means that the source just
5685 happens to have the same RTX form. Since temp will have been created
5686 by expand_expr, it will compare unequal according to == .
5687 We must generate a copy in this case, to reach the correct number
5688 of volatile memory references. */
5690 if ((! rtx_equal_p (temp, target)
5691 || (temp != target && (side_effects_p (temp)
5692 || side_effects_p (target))))
5693 && TREE_CODE (exp) != ERROR_MARK
5694 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5695 but TARGET is not valid memory reference, TEMP will differ
5696 from TARGET although it is really the same location. */
5697 && !(alt_rtl
5698 && rtx_equal_p (alt_rtl, target)
5699 && !side_effects_p (alt_rtl)
5700 && !side_effects_p (target))
5701 /* If there's nothing to copy, don't bother. Don't call
5702 expr_size unless necessary, because some front-ends (C++)
5703 expr_size-hook must not be given objects that are not
5704 supposed to be bit-copied or bit-initialized. */
5705 && expr_size (exp) != const0_rtx)
5707 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5709 if (GET_MODE (target) == BLKmode)
5711 /* Handle calls that return BLKmode values in registers. */
5712 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5713 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5714 else
5715 store_bit_field (target,
5716 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5717 0, 0, 0, GET_MODE (temp), temp, reverse);
5719 else
5720 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5723 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5725 /* Handle copying a string constant into an array. The string
5726 constant may be shorter than the array. So copy just the string's
5727 actual length, and clear the rest. First get the size of the data
5728 type of the string, which is actually the size of the target. */
5729 rtx size = expr_size (exp);
5731 if (CONST_INT_P (size)
5732 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5733 emit_block_move (target, temp, size,
5734 (call_param_p
5735 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5736 else
5738 machine_mode pointer_mode
5739 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5740 machine_mode address_mode = get_address_mode (target);
5742 /* Compute the size of the data to copy from the string. */
5743 tree copy_size
5744 = size_binop_loc (loc, MIN_EXPR,
5745 make_tree (sizetype, size),
5746 size_int (TREE_STRING_LENGTH (exp)));
5747 rtx copy_size_rtx
5748 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5749 (call_param_p
5750 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5751 rtx_code_label *label = 0;
5753 /* Copy that much. */
5754 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5755 TYPE_UNSIGNED (sizetype));
5756 emit_block_move (target, temp, copy_size_rtx,
5757 (call_param_p
5758 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5760 /* Figure out how much is left in TARGET that we have to clear.
5761 Do all calculations in pointer_mode. */
5762 if (CONST_INT_P (copy_size_rtx))
5764 size = plus_constant (address_mode, size,
5765 -INTVAL (copy_size_rtx));
5766 target = adjust_address (target, BLKmode,
5767 INTVAL (copy_size_rtx));
5769 else
5771 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5772 copy_size_rtx, NULL_RTX, 0,
5773 OPTAB_LIB_WIDEN);
5775 if (GET_MODE (copy_size_rtx) != address_mode)
5776 copy_size_rtx = convert_to_mode (address_mode,
5777 copy_size_rtx,
5778 TYPE_UNSIGNED (sizetype));
5780 target = offset_address (target, copy_size_rtx,
5781 highest_pow2_factor (copy_size));
5782 label = gen_label_rtx ();
5783 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5784 GET_MODE (size), 0, label);
5787 if (size != const0_rtx)
5788 clear_storage (target, size, BLOCK_OP_NORMAL);
5790 if (label)
5791 emit_label (label);
5794 /* Handle calls that return values in multiple non-contiguous locations.
5795 The Irix 6 ABI has examples of this. */
5796 else if (GET_CODE (target) == PARALLEL)
5798 if (GET_CODE (temp) == PARALLEL)
5799 emit_group_move (target, temp);
5800 else
5801 emit_group_load (target, temp, TREE_TYPE (exp),
5802 int_size_in_bytes (TREE_TYPE (exp)));
5804 else if (GET_CODE (temp) == PARALLEL)
5805 emit_group_store (target, temp, TREE_TYPE (exp),
5806 int_size_in_bytes (TREE_TYPE (exp)));
5807 else if (GET_MODE (temp) == BLKmode)
5808 emit_block_move (target, temp, expr_size (exp),
5809 (call_param_p
5810 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5811 /* If we emit a nontemporal store, there is nothing else to do. */
5812 else if (nontemporal && emit_storent_insn (target, temp))
5814 else
5816 if (reverse)
5817 temp = flip_storage_order (GET_MODE (target), temp);
5818 temp = force_operand (temp, target);
5819 if (temp != target)
5820 emit_move_insn (target, temp);
5824 return NULL_RTX;
5827 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5829 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5830 bool reverse)
5832 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5833 reverse, NULL);
5836 /* Return true if field F of structure TYPE is a flexible array. */
5838 static bool
5839 flexible_array_member_p (const_tree f, const_tree type)
5841 const_tree tf;
5843 tf = TREE_TYPE (f);
5844 return (DECL_CHAIN (f) == NULL
5845 && TREE_CODE (tf) == ARRAY_TYPE
5846 && TYPE_DOMAIN (tf)
5847 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5848 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5849 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5850 && int_size_in_bytes (type) >= 0);
5853 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5854 must have in order for it to completely initialize a value of type TYPE.
5855 Return -1 if the number isn't known.
5857 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5859 static HOST_WIDE_INT
5860 count_type_elements (const_tree type, bool for_ctor_p)
5862 switch (TREE_CODE (type))
5864 case ARRAY_TYPE:
5866 tree nelts;
5868 nelts = array_type_nelts (type);
5869 if (nelts && tree_fits_uhwi_p (nelts))
5871 unsigned HOST_WIDE_INT n;
5873 n = tree_to_uhwi (nelts) + 1;
5874 if (n == 0 || for_ctor_p)
5875 return n;
5876 else
5877 return n * count_type_elements (TREE_TYPE (type), false);
5879 return for_ctor_p ? -1 : 1;
5882 case RECORD_TYPE:
5884 unsigned HOST_WIDE_INT n;
5885 tree f;
5887 n = 0;
5888 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5889 if (TREE_CODE (f) == FIELD_DECL)
5891 if (!for_ctor_p)
5892 n += count_type_elements (TREE_TYPE (f), false);
5893 else if (!flexible_array_member_p (f, type))
5894 /* Don't count flexible arrays, which are not supposed
5895 to be initialized. */
5896 n += 1;
5899 return n;
5902 case UNION_TYPE:
5903 case QUAL_UNION_TYPE:
5905 tree f;
5906 HOST_WIDE_INT n, m;
5908 gcc_assert (!for_ctor_p);
5909 /* Estimate the number of scalars in each field and pick the
5910 maximum. Other estimates would do instead; the idea is simply
5911 to make sure that the estimate is not sensitive to the ordering
5912 of the fields. */
5913 n = 1;
5914 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5915 if (TREE_CODE (f) == FIELD_DECL)
5917 m = count_type_elements (TREE_TYPE (f), false);
5918 /* If the field doesn't span the whole union, add an extra
5919 scalar for the rest. */
5920 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5921 TYPE_SIZE (type)) != 1)
5922 m++;
5923 if (n < m)
5924 n = m;
5926 return n;
5929 case COMPLEX_TYPE:
5930 return 2;
5932 case VECTOR_TYPE:
5934 unsigned HOST_WIDE_INT nelts;
5935 if (TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
5936 return nelts;
5937 else
5938 return -1;
5941 case INTEGER_TYPE:
5942 case REAL_TYPE:
5943 case FIXED_POINT_TYPE:
5944 case ENUMERAL_TYPE:
5945 case BOOLEAN_TYPE:
5946 case POINTER_TYPE:
5947 case OFFSET_TYPE:
5948 case REFERENCE_TYPE:
5949 case NULLPTR_TYPE:
5950 return 1;
5952 case ERROR_MARK:
5953 return 0;
5955 case VOID_TYPE:
5956 case METHOD_TYPE:
5957 case FUNCTION_TYPE:
5958 case LANG_TYPE:
5959 default:
5960 gcc_unreachable ();
5964 /* Helper for categorize_ctor_elements. Identical interface. */
5966 static bool
5967 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5968 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5970 unsigned HOST_WIDE_INT idx;
5971 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5972 tree value, purpose, elt_type;
5974 /* Whether CTOR is a valid constant initializer, in accordance with what
5975 initializer_constant_valid_p does. If inferred from the constructor
5976 elements, true until proven otherwise. */
5977 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5978 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5980 nz_elts = 0;
5981 init_elts = 0;
5982 num_fields = 0;
5983 elt_type = NULL_TREE;
5985 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5987 HOST_WIDE_INT mult = 1;
5989 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5991 tree lo_index = TREE_OPERAND (purpose, 0);
5992 tree hi_index = TREE_OPERAND (purpose, 1);
5994 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5995 mult = (tree_to_uhwi (hi_index)
5996 - tree_to_uhwi (lo_index) + 1);
5998 num_fields += mult;
5999 elt_type = TREE_TYPE (value);
6001 switch (TREE_CODE (value))
6003 case CONSTRUCTOR:
6005 HOST_WIDE_INT nz = 0, ic = 0;
6007 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
6008 p_complete);
6010 nz_elts += mult * nz;
6011 init_elts += mult * ic;
6013 if (const_from_elts_p && const_p)
6014 const_p = const_elt_p;
6016 break;
6018 case INTEGER_CST:
6019 case REAL_CST:
6020 case FIXED_CST:
6021 if (!initializer_zerop (value))
6022 nz_elts += mult;
6023 init_elts += mult;
6024 break;
6026 case STRING_CST:
6027 nz_elts += mult * TREE_STRING_LENGTH (value);
6028 init_elts += mult * TREE_STRING_LENGTH (value);
6029 break;
6031 case COMPLEX_CST:
6032 if (!initializer_zerop (TREE_REALPART (value)))
6033 nz_elts += mult;
6034 if (!initializer_zerop (TREE_IMAGPART (value)))
6035 nz_elts += mult;
6036 init_elts += mult;
6037 break;
6039 case VECTOR_CST:
6041 /* We can only construct constant-length vectors using
6042 CONSTRUCTOR. */
6043 unsigned int nunits = VECTOR_CST_NELTS (value).to_constant ();
6044 for (unsigned int i = 0; i < nunits; ++i)
6046 tree v = VECTOR_CST_ELT (value, i);
6047 if (!initializer_zerop (v))
6048 nz_elts += mult;
6049 init_elts += mult;
6052 break;
6054 default:
6056 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
6057 nz_elts += mult * tc;
6058 init_elts += mult * tc;
6060 if (const_from_elts_p && const_p)
6061 const_p
6062 = initializer_constant_valid_p (value,
6063 elt_type,
6064 TYPE_REVERSE_STORAGE_ORDER
6065 (TREE_TYPE (ctor)))
6066 != NULL_TREE;
6068 break;
6072 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
6073 num_fields, elt_type))
6074 *p_complete = false;
6076 *p_nz_elts += nz_elts;
6077 *p_init_elts += init_elts;
6079 return const_p;
6082 /* Examine CTOR to discover:
6083 * how many scalar fields are set to nonzero values,
6084 and place it in *P_NZ_ELTS;
6085 * how many scalar fields in total are in CTOR,
6086 and place it in *P_ELT_COUNT.
6087 * whether the constructor is complete -- in the sense that every
6088 meaningful byte is explicitly given a value --
6089 and place it in *P_COMPLETE.
6091 Return whether or not CTOR is a valid static constant initializer, the same
6092 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6094 bool
6095 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6096 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6098 *p_nz_elts = 0;
6099 *p_init_elts = 0;
6100 *p_complete = true;
6102 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6105 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6106 of which had type LAST_TYPE. Each element was itself a complete
6107 initializer, in the sense that every meaningful byte was explicitly
6108 given a value. Return true if the same is true for the constructor
6109 as a whole. */
6111 bool
6112 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6113 const_tree last_type)
6115 if (TREE_CODE (type) == UNION_TYPE
6116 || TREE_CODE (type) == QUAL_UNION_TYPE)
6118 if (num_elts == 0)
6119 return false;
6121 gcc_assert (num_elts == 1 && last_type);
6123 /* ??? We could look at each element of the union, and find the
6124 largest element. Which would avoid comparing the size of the
6125 initialized element against any tail padding in the union.
6126 Doesn't seem worth the effort... */
6127 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6130 return count_type_elements (type, true) == num_elts;
6133 /* Return 1 if EXP contains mostly (3/4) zeros. */
6135 static int
6136 mostly_zeros_p (const_tree exp)
6138 if (TREE_CODE (exp) == CONSTRUCTOR)
6140 HOST_WIDE_INT nz_elts, init_elts;
6141 bool complete_p;
6143 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6144 return !complete_p || nz_elts < init_elts / 4;
6147 return initializer_zerop (exp);
6150 /* Return 1 if EXP contains all zeros. */
6152 static int
6153 all_zeros_p (const_tree exp)
6155 if (TREE_CODE (exp) == CONSTRUCTOR)
6157 HOST_WIDE_INT nz_elts, init_elts;
6158 bool complete_p;
6160 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6161 return nz_elts == 0;
6164 return initializer_zerop (exp);
6167 /* Helper function for store_constructor.
6168 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6169 CLEARED is as for store_constructor.
6170 ALIAS_SET is the alias set to use for any stores.
6171 If REVERSE is true, the store is to be done in reverse order.
6173 This provides a recursive shortcut back to store_constructor when it isn't
6174 necessary to go through store_field. This is so that we can pass through
6175 the cleared field to let store_constructor know that we may not have to
6176 clear a substructure if the outer structure has already been cleared. */
6178 static void
6179 store_constructor_field (rtx target, poly_uint64 bitsize, poly_int64 bitpos,
6180 poly_uint64 bitregion_start,
6181 poly_uint64 bitregion_end,
6182 machine_mode mode,
6183 tree exp, int cleared,
6184 alias_set_type alias_set, bool reverse)
6186 poly_int64 bytepos;
6187 poly_uint64 bytesize;
6188 if (TREE_CODE (exp) == CONSTRUCTOR
6189 /* We can only call store_constructor recursively if the size and
6190 bit position are on a byte boundary. */
6191 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
6192 && maybe_ne (bitsize, 0U)
6193 && multiple_p (bitsize, BITS_PER_UNIT, &bytesize)
6194 /* If we have a nonzero bitpos for a register target, then we just
6195 let store_field do the bitfield handling. This is unlikely to
6196 generate unnecessary clear instructions anyways. */
6197 && (known_eq (bitpos, 0) || MEM_P (target)))
6199 if (MEM_P (target))
6201 machine_mode target_mode = GET_MODE (target);
6202 if (target_mode != BLKmode
6203 && !multiple_p (bitpos, GET_MODE_ALIGNMENT (target_mode)))
6204 target_mode = BLKmode;
6205 target = adjust_address (target, target_mode, bytepos);
6209 /* Update the alias set, if required. */
6210 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6211 && MEM_ALIAS_SET (target) != 0)
6213 target = copy_rtx (target);
6214 set_mem_alias_set (target, alias_set);
6217 store_constructor (exp, target, cleared, bytesize, reverse);
6219 else
6220 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6221 exp, alias_set, false, reverse);
6225 /* Returns the number of FIELD_DECLs in TYPE. */
6227 static int
6228 fields_length (const_tree type)
6230 tree t = TYPE_FIELDS (type);
6231 int count = 0;
6233 for (; t; t = DECL_CHAIN (t))
6234 if (TREE_CODE (t) == FIELD_DECL)
6235 ++count;
6237 return count;
6241 /* Store the value of constructor EXP into the rtx TARGET.
6242 TARGET is either a REG or a MEM; we know it cannot conflict, since
6243 safe_from_p has been called.
6244 CLEARED is true if TARGET is known to have been zero'd.
6245 SIZE is the number of bytes of TARGET we are allowed to modify: this
6246 may not be the same as the size of EXP if we are assigning to a field
6247 which has been packed to exclude padding bits.
6248 If REVERSE is true, the store is to be done in reverse order. */
6250 static void
6251 store_constructor (tree exp, rtx target, int cleared, poly_int64 size,
6252 bool reverse)
6254 tree type = TREE_TYPE (exp);
6255 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6256 poly_int64 bitregion_end = known_gt (size, 0) ? size * BITS_PER_UNIT - 1 : 0;
6258 switch (TREE_CODE (type))
6260 case RECORD_TYPE:
6261 case UNION_TYPE:
6262 case QUAL_UNION_TYPE:
6264 unsigned HOST_WIDE_INT idx;
6265 tree field, value;
6267 /* The storage order is specified for every aggregate type. */
6268 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6270 /* If size is zero or the target is already cleared, do nothing. */
6271 if (known_eq (size, 0) || cleared)
6272 cleared = 1;
6273 /* We either clear the aggregate or indicate the value is dead. */
6274 else if ((TREE_CODE (type) == UNION_TYPE
6275 || TREE_CODE (type) == QUAL_UNION_TYPE)
6276 && ! CONSTRUCTOR_ELTS (exp))
6277 /* If the constructor is empty, clear the union. */
6279 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6280 cleared = 1;
6283 /* If we are building a static constructor into a register,
6284 set the initial value as zero so we can fold the value into
6285 a constant. But if more than one register is involved,
6286 this probably loses. */
6287 else if (REG_P (target) && TREE_STATIC (exp)
6288 && known_le (GET_MODE_SIZE (GET_MODE (target)),
6289 REGMODE_NATURAL_SIZE (GET_MODE (target))))
6291 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6292 cleared = 1;
6295 /* If the constructor has fewer fields than the structure or
6296 if we are initializing the structure to mostly zeros, clear
6297 the whole structure first. Don't do this if TARGET is a
6298 register whose mode size isn't equal to SIZE since
6299 clear_storage can't handle this case. */
6300 else if (known_size_p (size)
6301 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6302 || mostly_zeros_p (exp))
6303 && (!REG_P (target)
6304 || known_eq (GET_MODE_SIZE (GET_MODE (target)), size)))
6306 clear_storage (target, gen_int_mode (size, Pmode),
6307 BLOCK_OP_NORMAL);
6308 cleared = 1;
6311 if (REG_P (target) && !cleared)
6312 emit_clobber (target);
6314 /* Store each element of the constructor into the
6315 corresponding field of TARGET. */
6316 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6318 machine_mode mode;
6319 HOST_WIDE_INT bitsize;
6320 HOST_WIDE_INT bitpos = 0;
6321 tree offset;
6322 rtx to_rtx = target;
6324 /* Just ignore missing fields. We cleared the whole
6325 structure, above, if any fields are missing. */
6326 if (field == 0)
6327 continue;
6329 if (cleared && initializer_zerop (value))
6330 continue;
6332 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6333 bitsize = tree_to_uhwi (DECL_SIZE (field));
6334 else
6335 gcc_unreachable ();
6337 mode = DECL_MODE (field);
6338 if (DECL_BIT_FIELD (field))
6339 mode = VOIDmode;
6341 offset = DECL_FIELD_OFFSET (field);
6342 if (tree_fits_shwi_p (offset)
6343 && tree_fits_shwi_p (bit_position (field)))
6345 bitpos = int_bit_position (field);
6346 offset = NULL_TREE;
6348 else
6349 gcc_unreachable ();
6351 /* If this initializes a field that is smaller than a
6352 word, at the start of a word, try to widen it to a full
6353 word. This special case allows us to output C++ member
6354 function initializations in a form that the optimizers
6355 can understand. */
6356 if (WORD_REGISTER_OPERATIONS
6357 && REG_P (target)
6358 && bitsize < BITS_PER_WORD
6359 && bitpos % BITS_PER_WORD == 0
6360 && GET_MODE_CLASS (mode) == MODE_INT
6361 && TREE_CODE (value) == INTEGER_CST
6362 && exp_size >= 0
6363 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6365 tree type = TREE_TYPE (value);
6367 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6369 type = lang_hooks.types.type_for_mode
6370 (word_mode, TYPE_UNSIGNED (type));
6371 value = fold_convert (type, value);
6372 /* Make sure the bits beyond the original bitsize are zero
6373 so that we can correctly avoid extra zeroing stores in
6374 later constructor elements. */
6375 tree bitsize_mask
6376 = wide_int_to_tree (type, wi::mask (bitsize, false,
6377 BITS_PER_WORD));
6378 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6381 if (BYTES_BIG_ENDIAN)
6382 value
6383 = fold_build2 (LSHIFT_EXPR, type, value,
6384 build_int_cst (type,
6385 BITS_PER_WORD - bitsize));
6386 bitsize = BITS_PER_WORD;
6387 mode = word_mode;
6390 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6391 && DECL_NONADDRESSABLE_P (field))
6393 to_rtx = copy_rtx (to_rtx);
6394 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6397 store_constructor_field (to_rtx, bitsize, bitpos,
6398 0, bitregion_end, mode,
6399 value, cleared,
6400 get_alias_set (TREE_TYPE (field)),
6401 reverse);
6403 break;
6405 case ARRAY_TYPE:
6407 tree value, index;
6408 unsigned HOST_WIDE_INT i;
6409 int need_to_clear;
6410 tree domain;
6411 tree elttype = TREE_TYPE (type);
6412 int const_bounds_p;
6413 HOST_WIDE_INT minelt = 0;
6414 HOST_WIDE_INT maxelt = 0;
6416 /* The storage order is specified for every aggregate type. */
6417 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6419 domain = TYPE_DOMAIN (type);
6420 const_bounds_p = (TYPE_MIN_VALUE (domain)
6421 && TYPE_MAX_VALUE (domain)
6422 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6423 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6425 /* If we have constant bounds for the range of the type, get them. */
6426 if (const_bounds_p)
6428 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6429 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6432 /* If the constructor has fewer elements than the array, clear
6433 the whole array first. Similarly if this is static
6434 constructor of a non-BLKmode object. */
6435 if (cleared)
6436 need_to_clear = 0;
6437 else if (REG_P (target) && TREE_STATIC (exp))
6438 need_to_clear = 1;
6439 else
6441 unsigned HOST_WIDE_INT idx;
6442 tree index, value;
6443 HOST_WIDE_INT count = 0, zero_count = 0;
6444 need_to_clear = ! const_bounds_p;
6446 /* This loop is a more accurate version of the loop in
6447 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6448 is also needed to check for missing elements. */
6449 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6451 HOST_WIDE_INT this_node_count;
6453 if (need_to_clear)
6454 break;
6456 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6458 tree lo_index = TREE_OPERAND (index, 0);
6459 tree hi_index = TREE_OPERAND (index, 1);
6461 if (! tree_fits_uhwi_p (lo_index)
6462 || ! tree_fits_uhwi_p (hi_index))
6464 need_to_clear = 1;
6465 break;
6468 this_node_count = (tree_to_uhwi (hi_index)
6469 - tree_to_uhwi (lo_index) + 1);
6471 else
6472 this_node_count = 1;
6474 count += this_node_count;
6475 if (mostly_zeros_p (value))
6476 zero_count += this_node_count;
6479 /* Clear the entire array first if there are any missing
6480 elements, or if the incidence of zero elements is >=
6481 75%. */
6482 if (! need_to_clear
6483 && (count < maxelt - minelt + 1
6484 || 4 * zero_count >= 3 * count))
6485 need_to_clear = 1;
6488 if (need_to_clear && maybe_gt (size, 0))
6490 if (REG_P (target))
6491 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6492 else
6493 clear_storage (target, gen_int_mode (size, Pmode),
6494 BLOCK_OP_NORMAL);
6495 cleared = 1;
6498 if (!cleared && REG_P (target))
6499 /* Inform later passes that the old value is dead. */
6500 emit_clobber (target);
6502 /* Store each element of the constructor into the
6503 corresponding element of TARGET, determined by counting the
6504 elements. */
6505 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6507 machine_mode mode;
6508 poly_int64 bitsize;
6509 HOST_WIDE_INT bitpos;
6510 rtx xtarget = target;
6512 if (cleared && initializer_zerop (value))
6513 continue;
6515 mode = TYPE_MODE (elttype);
6516 if (mode == BLKmode)
6517 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6518 ? tree_to_uhwi (TYPE_SIZE (elttype))
6519 : -1);
6520 else
6521 bitsize = GET_MODE_BITSIZE (mode);
6523 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6525 tree lo_index = TREE_OPERAND (index, 0);
6526 tree hi_index = TREE_OPERAND (index, 1);
6527 rtx index_r, pos_rtx;
6528 HOST_WIDE_INT lo, hi, count;
6529 tree position;
6531 /* If the range is constant and "small", unroll the loop. */
6532 if (const_bounds_p
6533 && tree_fits_shwi_p (lo_index)
6534 && tree_fits_shwi_p (hi_index)
6535 && (lo = tree_to_shwi (lo_index),
6536 hi = tree_to_shwi (hi_index),
6537 count = hi - lo + 1,
6538 (!MEM_P (target)
6539 || count <= 2
6540 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6541 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6542 <= 40 * 8)))))
6544 lo -= minelt; hi -= minelt;
6545 for (; lo <= hi; lo++)
6547 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6549 if (MEM_P (target)
6550 && !MEM_KEEP_ALIAS_SET_P (target)
6551 && TREE_CODE (type) == ARRAY_TYPE
6552 && TYPE_NONALIASED_COMPONENT (type))
6554 target = copy_rtx (target);
6555 MEM_KEEP_ALIAS_SET_P (target) = 1;
6558 store_constructor_field
6559 (target, bitsize, bitpos, 0, bitregion_end,
6560 mode, value, cleared,
6561 get_alias_set (elttype), reverse);
6564 else
6566 rtx_code_label *loop_start = gen_label_rtx ();
6567 rtx_code_label *loop_end = gen_label_rtx ();
6568 tree exit_cond;
6570 expand_normal (hi_index);
6572 index = build_decl (EXPR_LOCATION (exp),
6573 VAR_DECL, NULL_TREE, domain);
6574 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6575 SET_DECL_RTL (index, index_r);
6576 store_expr (lo_index, index_r, 0, false, reverse);
6578 /* Build the head of the loop. */
6579 do_pending_stack_adjust ();
6580 emit_label (loop_start);
6582 /* Assign value to element index. */
6583 position =
6584 fold_convert (ssizetype,
6585 fold_build2 (MINUS_EXPR,
6586 TREE_TYPE (index),
6587 index,
6588 TYPE_MIN_VALUE (domain)));
6590 position =
6591 size_binop (MULT_EXPR, position,
6592 fold_convert (ssizetype,
6593 TYPE_SIZE_UNIT (elttype)));
6595 pos_rtx = expand_normal (position);
6596 xtarget = offset_address (target, pos_rtx,
6597 highest_pow2_factor (position));
6598 xtarget = adjust_address (xtarget, mode, 0);
6599 if (TREE_CODE (value) == CONSTRUCTOR)
6600 store_constructor (value, xtarget, cleared,
6601 exact_div (bitsize, BITS_PER_UNIT),
6602 reverse);
6603 else
6604 store_expr (value, xtarget, 0, false, reverse);
6606 /* Generate a conditional jump to exit the loop. */
6607 exit_cond = build2 (LT_EXPR, integer_type_node,
6608 index, hi_index);
6609 jumpif (exit_cond, loop_end,
6610 profile_probability::uninitialized ());
6612 /* Update the loop counter, and jump to the head of
6613 the loop. */
6614 expand_assignment (index,
6615 build2 (PLUS_EXPR, TREE_TYPE (index),
6616 index, integer_one_node),
6617 false);
6619 emit_jump (loop_start);
6621 /* Build the end of the loop. */
6622 emit_label (loop_end);
6625 else if ((index != 0 && ! tree_fits_shwi_p (index))
6626 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6628 tree position;
6630 if (index == 0)
6631 index = ssize_int (1);
6633 if (minelt)
6634 index = fold_convert (ssizetype,
6635 fold_build2 (MINUS_EXPR,
6636 TREE_TYPE (index),
6637 index,
6638 TYPE_MIN_VALUE (domain)));
6640 position =
6641 size_binop (MULT_EXPR, index,
6642 fold_convert (ssizetype,
6643 TYPE_SIZE_UNIT (elttype)));
6644 xtarget = offset_address (target,
6645 expand_normal (position),
6646 highest_pow2_factor (position));
6647 xtarget = adjust_address (xtarget, mode, 0);
6648 store_expr (value, xtarget, 0, false, reverse);
6650 else
6652 if (index != 0)
6653 bitpos = ((tree_to_shwi (index) - minelt)
6654 * tree_to_uhwi (TYPE_SIZE (elttype)));
6655 else
6656 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6658 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6659 && TREE_CODE (type) == ARRAY_TYPE
6660 && TYPE_NONALIASED_COMPONENT (type))
6662 target = copy_rtx (target);
6663 MEM_KEEP_ALIAS_SET_P (target) = 1;
6665 store_constructor_field (target, bitsize, bitpos, 0,
6666 bitregion_end, mode, value,
6667 cleared, get_alias_set (elttype),
6668 reverse);
6671 break;
6674 case VECTOR_TYPE:
6676 unsigned HOST_WIDE_INT idx;
6677 constructor_elt *ce;
6678 int i;
6679 int need_to_clear;
6680 insn_code icode = CODE_FOR_nothing;
6681 tree elt;
6682 tree elttype = TREE_TYPE (type);
6683 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6684 machine_mode eltmode = TYPE_MODE (elttype);
6685 HOST_WIDE_INT bitsize;
6686 HOST_WIDE_INT bitpos;
6687 rtvec vector = NULL;
6688 poly_uint64 n_elts;
6689 unsigned HOST_WIDE_INT const_n_elts;
6690 alias_set_type alias;
6691 bool vec_vec_init_p = false;
6692 machine_mode mode = GET_MODE (target);
6694 gcc_assert (eltmode != BLKmode);
6696 /* Try using vec_duplicate_optab for uniform vectors. */
6697 if (!TREE_SIDE_EFFECTS (exp)
6698 && VECTOR_MODE_P (mode)
6699 && eltmode == GET_MODE_INNER (mode)
6700 && ((icode = optab_handler (vec_duplicate_optab, mode))
6701 != CODE_FOR_nothing)
6702 && (elt = uniform_vector_p (exp)))
6704 struct expand_operand ops[2];
6705 create_output_operand (&ops[0], target, mode);
6706 create_input_operand (&ops[1], expand_normal (elt), eltmode);
6707 expand_insn (icode, 2, ops);
6708 if (!rtx_equal_p (target, ops[0].value))
6709 emit_move_insn (target, ops[0].value);
6710 break;
6713 n_elts = TYPE_VECTOR_SUBPARTS (type);
6714 if (REG_P (target)
6715 && VECTOR_MODE_P (mode)
6716 && n_elts.is_constant (&const_n_elts))
6718 machine_mode emode = eltmode;
6720 if (CONSTRUCTOR_NELTS (exp)
6721 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6722 == VECTOR_TYPE))
6724 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6725 gcc_assert (known_eq (CONSTRUCTOR_NELTS (exp)
6726 * TYPE_VECTOR_SUBPARTS (etype),
6727 n_elts));
6728 emode = TYPE_MODE (etype);
6730 icode = convert_optab_handler (vec_init_optab, mode, emode);
6731 if (icode != CODE_FOR_nothing)
6733 unsigned int i, n = const_n_elts;
6735 if (emode != eltmode)
6737 n = CONSTRUCTOR_NELTS (exp);
6738 vec_vec_init_p = true;
6740 vector = rtvec_alloc (n);
6741 for (i = 0; i < n; i++)
6742 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6746 /* If the constructor has fewer elements than the vector,
6747 clear the whole array first. Similarly if this is static
6748 constructor of a non-BLKmode object. */
6749 if (cleared)
6750 need_to_clear = 0;
6751 else if (REG_P (target) && TREE_STATIC (exp))
6752 need_to_clear = 1;
6753 else
6755 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6756 tree value;
6758 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6760 tree sz = TYPE_SIZE (TREE_TYPE (value));
6761 int n_elts_here
6762 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6763 TYPE_SIZE (elttype)));
6765 count += n_elts_here;
6766 if (mostly_zeros_p (value))
6767 zero_count += n_elts_here;
6770 /* Clear the entire vector first if there are any missing elements,
6771 or if the incidence of zero elements is >= 75%. */
6772 need_to_clear = (maybe_lt (count, n_elts)
6773 || 4 * zero_count >= 3 * count);
6776 if (need_to_clear && maybe_gt (size, 0) && !vector)
6778 if (REG_P (target))
6779 emit_move_insn (target, CONST0_RTX (mode));
6780 else
6781 clear_storage (target, gen_int_mode (size, Pmode),
6782 BLOCK_OP_NORMAL);
6783 cleared = 1;
6786 /* Inform later passes that the old value is dead. */
6787 if (!cleared && !vector && REG_P (target))
6788 emit_move_insn (target, CONST0_RTX (mode));
6790 if (MEM_P (target))
6791 alias = MEM_ALIAS_SET (target);
6792 else
6793 alias = get_alias_set (elttype);
6795 /* Store each element of the constructor into the corresponding
6796 element of TARGET, determined by counting the elements. */
6797 for (idx = 0, i = 0;
6798 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6799 idx++, i += bitsize / elt_size)
6801 HOST_WIDE_INT eltpos;
6802 tree value = ce->value;
6804 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6805 if (cleared && initializer_zerop (value))
6806 continue;
6808 if (ce->index)
6809 eltpos = tree_to_uhwi (ce->index);
6810 else
6811 eltpos = i;
6813 if (vector)
6815 if (vec_vec_init_p)
6817 gcc_assert (ce->index == NULL_TREE);
6818 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6819 eltpos = idx;
6821 else
6822 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6823 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6825 else
6827 machine_mode value_mode
6828 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6829 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6830 bitpos = eltpos * elt_size;
6831 store_constructor_field (target, bitsize, bitpos, 0,
6832 bitregion_end, value_mode,
6833 value, cleared, alias, reverse);
6837 if (vector)
6838 emit_insn (GEN_FCN (icode) (target,
6839 gen_rtx_PARALLEL (mode, vector)));
6840 break;
6843 default:
6844 gcc_unreachable ();
6848 /* Store the value of EXP (an expression tree)
6849 into a subfield of TARGET which has mode MODE and occupies
6850 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6851 If MODE is VOIDmode, it means that we are storing into a bit-field.
6853 BITREGION_START is bitpos of the first bitfield in this region.
6854 BITREGION_END is the bitpos of the ending bitfield in this region.
6855 These two fields are 0, if the C++ memory model does not apply,
6856 or we are not interested in keeping track of bitfield regions.
6858 Always return const0_rtx unless we have something particular to
6859 return.
6861 ALIAS_SET is the alias set for the destination. This value will
6862 (in general) be different from that for TARGET, since TARGET is a
6863 reference to the containing structure.
6865 If NONTEMPORAL is true, try generating a nontemporal store.
6867 If REVERSE is true, the store is to be done in reverse order. */
6869 static rtx
6870 store_field (rtx target, poly_int64 bitsize, poly_int64 bitpos,
6871 poly_uint64 bitregion_start, poly_uint64 bitregion_end,
6872 machine_mode mode, tree exp,
6873 alias_set_type alias_set, bool nontemporal, bool reverse)
6875 if (TREE_CODE (exp) == ERROR_MARK)
6876 return const0_rtx;
6878 /* If we have nothing to store, do nothing unless the expression has
6879 side-effects. Don't do that for zero sized addressable lhs of
6880 calls. */
6881 if (known_eq (bitsize, 0)
6882 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6883 || TREE_CODE (exp) != CALL_EXPR))
6884 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6886 if (GET_CODE (target) == CONCAT)
6888 /* We're storing into a struct containing a single __complex. */
6890 gcc_assert (known_eq (bitpos, 0));
6891 return store_expr (exp, target, 0, nontemporal, reverse);
6894 /* If the structure is in a register or if the component
6895 is a bit field, we cannot use addressing to access it.
6896 Use bit-field techniques or SUBREG to store in it. */
6898 poly_int64 decl_bitsize;
6899 if (mode == VOIDmode
6900 || (mode != BLKmode && ! direct_store[(int) mode]
6901 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6902 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6903 || REG_P (target)
6904 || GET_CODE (target) == SUBREG
6905 /* If the field isn't aligned enough to store as an ordinary memref,
6906 store it as a bit field. */
6907 || (mode != BLKmode
6908 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6909 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
6910 && targetm.slow_unaligned_access (mode, MEM_ALIGN (target)))
6911 || !multiple_p (bitpos, BITS_PER_UNIT)))
6912 || (known_size_p (bitsize)
6913 && mode != BLKmode
6914 && maybe_gt (GET_MODE_BITSIZE (mode), bitsize))
6915 /* If the RHS and field are a constant size and the size of the
6916 RHS isn't the same size as the bitfield, we must use bitfield
6917 operations. */
6918 || (known_size_p (bitsize)
6919 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
6920 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
6921 bitsize)
6922 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6923 we will handle specially below. */
6924 && !(TREE_CODE (exp) == CONSTRUCTOR
6925 && multiple_p (bitsize, BITS_PER_UNIT))
6926 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6927 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6928 includes some extra padding. store_expr / expand_expr will in
6929 that case call get_inner_reference that will have the bitsize
6930 we check here and thus the block move will not clobber the
6931 padding that shouldn't be clobbered. In the future we could
6932 replace the TREE_ADDRESSABLE check with a check that
6933 get_base_address needs to live in memory. */
6934 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6935 || TREE_CODE (exp) != COMPONENT_REF
6936 || !multiple_p (bitsize, BITS_PER_UNIT)
6937 || !multiple_p (bitpos, BITS_PER_UNIT)
6938 || !poly_int_tree_p (DECL_SIZE (TREE_OPERAND (exp, 1)),
6939 &decl_bitsize)
6940 || maybe_ne (decl_bitsize, bitsize)))
6941 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6942 decl we must use bitfield operations. */
6943 || (known_size_p (bitsize)
6944 && TREE_CODE (exp) == MEM_REF
6945 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6946 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6947 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6948 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6950 rtx temp;
6951 gimple *nop_def;
6953 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6954 implies a mask operation. If the precision is the same size as
6955 the field we're storing into, that mask is redundant. This is
6956 particularly common with bit field assignments generated by the
6957 C front end. */
6958 nop_def = get_def_for_expr (exp, NOP_EXPR);
6959 if (nop_def)
6961 tree type = TREE_TYPE (exp);
6962 if (INTEGRAL_TYPE_P (type)
6963 && maybe_ne (TYPE_PRECISION (type),
6964 GET_MODE_BITSIZE (TYPE_MODE (type)))
6965 && known_eq (bitsize, TYPE_PRECISION (type)))
6967 tree op = gimple_assign_rhs1 (nop_def);
6968 type = TREE_TYPE (op);
6969 if (INTEGRAL_TYPE_P (type)
6970 && known_ge (TYPE_PRECISION (type), bitsize))
6971 exp = op;
6975 temp = expand_normal (exp);
6977 /* We don't support variable-sized BLKmode bitfields, since our
6978 handling of BLKmode is bound up with the ability to break
6979 things into words. */
6980 gcc_assert (mode != BLKmode || bitsize.is_constant ());
6982 /* Handle calls that return values in multiple non-contiguous locations.
6983 The Irix 6 ABI has examples of this. */
6984 if (GET_CODE (temp) == PARALLEL)
6986 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6987 scalar_int_mode temp_mode
6988 = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6989 rtx temp_target = gen_reg_rtx (temp_mode);
6990 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6991 temp = temp_target;
6994 /* Handle calls that return BLKmode values in registers. */
6995 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6997 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6998 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6999 temp = temp_target;
7002 /* If the value has aggregate type and an integral mode then, if BITSIZE
7003 is narrower than this mode and this is for big-endian data, we first
7004 need to put the value into the low-order bits for store_bit_field,
7005 except when MODE is BLKmode and BITSIZE larger than the word size
7006 (see the handling of fields larger than a word in store_bit_field).
7007 Moreover, the field may be not aligned on a byte boundary; in this
7008 case, if it has reverse storage order, it needs to be accessed as a
7009 scalar field with reverse storage order and we must first put the
7010 value into target order. */
7011 scalar_int_mode temp_mode;
7012 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
7013 && is_int_mode (GET_MODE (temp), &temp_mode))
7015 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
7017 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
7019 if (reverse)
7020 temp = flip_storage_order (temp_mode, temp);
7022 gcc_checking_assert (known_le (bitsize, size));
7023 if (maybe_lt (bitsize, size)
7024 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
7025 /* Use of to_constant for BLKmode was checked above. */
7026 && !(mode == BLKmode && bitsize.to_constant () > BITS_PER_WORD))
7027 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
7028 size - bitsize, NULL_RTX, 1);
7031 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
7032 if (mode != VOIDmode && mode != BLKmode
7033 && mode != TYPE_MODE (TREE_TYPE (exp)))
7034 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
7036 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
7037 and BITPOS must be aligned on a byte boundary. If so, we simply do
7038 a block copy. Likewise for a BLKmode-like TARGET. */
7039 if (GET_MODE (temp) == BLKmode
7040 && (GET_MODE (target) == BLKmode
7041 || (MEM_P (target)
7042 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
7043 && multiple_p (bitpos, BITS_PER_UNIT)
7044 && multiple_p (bitsize, BITS_PER_UNIT))))
7046 gcc_assert (MEM_P (target) && MEM_P (temp));
7047 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
7048 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
7050 target = adjust_address (target, VOIDmode, bytepos);
7051 emit_block_move (target, temp,
7052 gen_int_mode (bytesize, Pmode),
7053 BLOCK_OP_NORMAL);
7055 return const0_rtx;
7058 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
7059 word size, we need to load the value (see again store_bit_field). */
7060 if (GET_MODE (temp) == BLKmode && known_le (bitsize, BITS_PER_WORD))
7062 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
7063 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
7064 temp_mode, false, NULL);
7067 /* Store the value in the bitfield. */
7068 store_bit_field (target, bitsize, bitpos,
7069 bitregion_start, bitregion_end,
7070 mode, temp, reverse);
7072 return const0_rtx;
7074 else
7076 /* Now build a reference to just the desired component. */
7077 rtx to_rtx = adjust_address (target, mode,
7078 exact_div (bitpos, BITS_PER_UNIT));
7080 if (to_rtx == target)
7081 to_rtx = copy_rtx (to_rtx);
7083 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
7084 set_mem_alias_set (to_rtx, alias_set);
7086 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
7087 into a target smaller than its type; handle that case now. */
7088 if (TREE_CODE (exp) == CONSTRUCTOR && known_size_p (bitsize))
7090 poly_int64 bytesize = exact_div (bitsize, BITS_PER_UNIT);
7091 store_constructor (exp, to_rtx, 0, bytesize, reverse);
7092 return to_rtx;
7095 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
7099 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
7100 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
7101 codes and find the ultimate containing object, which we return.
7103 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
7104 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
7105 storage order of the field.
7106 If the position of the field is variable, we store a tree
7107 giving the variable offset (in units) in *POFFSET.
7108 This offset is in addition to the bit position.
7109 If the position is not variable, we store 0 in *POFFSET.
7111 If any of the extraction expressions is volatile,
7112 we store 1 in *PVOLATILEP. Otherwise we don't change that.
7114 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
7115 Otherwise, it is a mode that can be used to access the field.
7117 If the field describes a variable-sized object, *PMODE is set to
7118 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
7119 this case, but the address of the object can be found. */
7121 tree
7122 get_inner_reference (tree exp, poly_int64_pod *pbitsize,
7123 poly_int64_pod *pbitpos, tree *poffset,
7124 machine_mode *pmode, int *punsignedp,
7125 int *preversep, int *pvolatilep)
7127 tree size_tree = 0;
7128 machine_mode mode = VOIDmode;
7129 bool blkmode_bitfield = false;
7130 tree offset = size_zero_node;
7131 poly_offset_int bit_offset = 0;
7133 /* First get the mode, signedness, storage order and size. We do this from
7134 just the outermost expression. */
7135 *pbitsize = -1;
7136 if (TREE_CODE (exp) == COMPONENT_REF)
7138 tree field = TREE_OPERAND (exp, 1);
7139 size_tree = DECL_SIZE (field);
7140 if (flag_strict_volatile_bitfields > 0
7141 && TREE_THIS_VOLATILE (exp)
7142 && DECL_BIT_FIELD_TYPE (field)
7143 && DECL_MODE (field) != BLKmode)
7144 /* Volatile bitfields should be accessed in the mode of the
7145 field's type, not the mode computed based on the bit
7146 size. */
7147 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7148 else if (!DECL_BIT_FIELD (field))
7150 mode = DECL_MODE (field);
7151 /* For vector fields re-check the target flags, as DECL_MODE
7152 could have been set with different target flags than
7153 the current function has. */
7154 if (mode == BLKmode
7155 && VECTOR_TYPE_P (TREE_TYPE (field))
7156 && VECTOR_MODE_P (TYPE_MODE_RAW (TREE_TYPE (field))))
7157 mode = TYPE_MODE (TREE_TYPE (field));
7159 else if (DECL_MODE (field) == BLKmode)
7160 blkmode_bitfield = true;
7162 *punsignedp = DECL_UNSIGNED (field);
7164 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7166 size_tree = TREE_OPERAND (exp, 1);
7167 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7168 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7170 /* For vector types, with the correct size of access, use the mode of
7171 inner type. */
7172 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7173 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7174 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7175 mode = TYPE_MODE (TREE_TYPE (exp));
7177 else
7179 mode = TYPE_MODE (TREE_TYPE (exp));
7180 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7182 if (mode == BLKmode)
7183 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7184 else
7185 *pbitsize = GET_MODE_BITSIZE (mode);
7188 if (size_tree != 0)
7190 if (! tree_fits_uhwi_p (size_tree))
7191 mode = BLKmode, *pbitsize = -1;
7192 else
7193 *pbitsize = tree_to_uhwi (size_tree);
7196 *preversep = reverse_storage_order_for_component_p (exp);
7198 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7199 and find the ultimate containing object. */
7200 while (1)
7202 switch (TREE_CODE (exp))
7204 case BIT_FIELD_REF:
7205 bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
7206 break;
7208 case COMPONENT_REF:
7210 tree field = TREE_OPERAND (exp, 1);
7211 tree this_offset = component_ref_field_offset (exp);
7213 /* If this field hasn't been filled in yet, don't go past it.
7214 This should only happen when folding expressions made during
7215 type construction. */
7216 if (this_offset == 0)
7217 break;
7219 offset = size_binop (PLUS_EXPR, offset, this_offset);
7220 bit_offset += wi::to_poly_offset (DECL_FIELD_BIT_OFFSET (field));
7222 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7224 break;
7226 case ARRAY_REF:
7227 case ARRAY_RANGE_REF:
7229 tree index = TREE_OPERAND (exp, 1);
7230 tree low_bound = array_ref_low_bound (exp);
7231 tree unit_size = array_ref_element_size (exp);
7233 /* We assume all arrays have sizes that are a multiple of a byte.
7234 First subtract the lower bound, if any, in the type of the
7235 index, then convert to sizetype and multiply by the size of
7236 the array element. */
7237 if (! integer_zerop (low_bound))
7238 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7239 index, low_bound);
7241 offset = size_binop (PLUS_EXPR, offset,
7242 size_binop (MULT_EXPR,
7243 fold_convert (sizetype, index),
7244 unit_size));
7246 break;
7248 case REALPART_EXPR:
7249 break;
7251 case IMAGPART_EXPR:
7252 bit_offset += *pbitsize;
7253 break;
7255 case VIEW_CONVERT_EXPR:
7256 break;
7258 case MEM_REF:
7259 /* Hand back the decl for MEM[&decl, off]. */
7260 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7262 tree off = TREE_OPERAND (exp, 1);
7263 if (!integer_zerop (off))
7265 poly_offset_int boff = mem_ref_offset (exp);
7266 boff <<= LOG2_BITS_PER_UNIT;
7267 bit_offset += boff;
7269 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7271 goto done;
7273 default:
7274 goto done;
7277 /* If any reference in the chain is volatile, the effect is volatile. */
7278 if (TREE_THIS_VOLATILE (exp))
7279 *pvolatilep = 1;
7281 exp = TREE_OPERAND (exp, 0);
7283 done:
7285 /* If OFFSET is constant, see if we can return the whole thing as a
7286 constant bit position. Make sure to handle overflow during
7287 this conversion. */
7288 if (poly_int_tree_p (offset))
7290 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
7291 TYPE_PRECISION (sizetype));
7292 tem <<= LOG2_BITS_PER_UNIT;
7293 tem += bit_offset;
7294 if (tem.to_shwi (pbitpos))
7295 *poffset = offset = NULL_TREE;
7298 /* Otherwise, split it up. */
7299 if (offset)
7301 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7302 if (!bit_offset.to_shwi (pbitpos) || maybe_lt (*pbitpos, 0))
7304 *pbitpos = num_trailing_bits (bit_offset.force_shwi ());
7305 poly_offset_int bytes = bits_to_bytes_round_down (bit_offset);
7306 offset = size_binop (PLUS_EXPR, offset,
7307 build_int_cst (sizetype, bytes.force_shwi ()));
7310 *poffset = offset;
7313 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7314 if (mode == VOIDmode
7315 && blkmode_bitfield
7316 && multiple_p (*pbitpos, BITS_PER_UNIT)
7317 && multiple_p (*pbitsize, BITS_PER_UNIT))
7318 *pmode = BLKmode;
7319 else
7320 *pmode = mode;
7322 return exp;
7325 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7327 static unsigned HOST_WIDE_INT
7328 target_align (const_tree target)
7330 /* We might have a chain of nested references with intermediate misaligning
7331 bitfields components, so need to recurse to find out. */
7333 unsigned HOST_WIDE_INT this_align, outer_align;
7335 switch (TREE_CODE (target))
7337 case BIT_FIELD_REF:
7338 return 1;
7340 case COMPONENT_REF:
7341 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7342 outer_align = target_align (TREE_OPERAND (target, 0));
7343 return MIN (this_align, outer_align);
7345 case ARRAY_REF:
7346 case ARRAY_RANGE_REF:
7347 this_align = TYPE_ALIGN (TREE_TYPE (target));
7348 outer_align = target_align (TREE_OPERAND (target, 0));
7349 return MIN (this_align, outer_align);
7351 CASE_CONVERT:
7352 case NON_LVALUE_EXPR:
7353 case VIEW_CONVERT_EXPR:
7354 this_align = TYPE_ALIGN (TREE_TYPE (target));
7355 outer_align = target_align (TREE_OPERAND (target, 0));
7356 return MAX (this_align, outer_align);
7358 default:
7359 return TYPE_ALIGN (TREE_TYPE (target));
7364 /* Given an rtx VALUE that may contain additions and multiplications, return
7365 an equivalent value that just refers to a register, memory, or constant.
7366 This is done by generating instructions to perform the arithmetic and
7367 returning a pseudo-register containing the value.
7369 The returned value may be a REG, SUBREG, MEM or constant. */
7372 force_operand (rtx value, rtx target)
7374 rtx op1, op2;
7375 /* Use subtarget as the target for operand 0 of a binary operation. */
7376 rtx subtarget = get_subtarget (target);
7377 enum rtx_code code = GET_CODE (value);
7379 /* Check for subreg applied to an expression produced by loop optimizer. */
7380 if (code == SUBREG
7381 && !REG_P (SUBREG_REG (value))
7382 && !MEM_P (SUBREG_REG (value)))
7384 value
7385 = simplify_gen_subreg (GET_MODE (value),
7386 force_reg (GET_MODE (SUBREG_REG (value)),
7387 force_operand (SUBREG_REG (value),
7388 NULL_RTX)),
7389 GET_MODE (SUBREG_REG (value)),
7390 SUBREG_BYTE (value));
7391 code = GET_CODE (value);
7394 /* Check for a PIC address load. */
7395 if ((code == PLUS || code == MINUS)
7396 && XEXP (value, 0) == pic_offset_table_rtx
7397 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7398 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7399 || GET_CODE (XEXP (value, 1)) == CONST))
7401 if (!subtarget)
7402 subtarget = gen_reg_rtx (GET_MODE (value));
7403 emit_move_insn (subtarget, value);
7404 return subtarget;
7407 if (ARITHMETIC_P (value))
7409 op2 = XEXP (value, 1);
7410 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7411 subtarget = 0;
7412 if (code == MINUS && CONST_INT_P (op2))
7414 code = PLUS;
7415 op2 = negate_rtx (GET_MODE (value), op2);
7418 /* Check for an addition with OP2 a constant integer and our first
7419 operand a PLUS of a virtual register and something else. In that
7420 case, we want to emit the sum of the virtual register and the
7421 constant first and then add the other value. This allows virtual
7422 register instantiation to simply modify the constant rather than
7423 creating another one around this addition. */
7424 if (code == PLUS && CONST_INT_P (op2)
7425 && GET_CODE (XEXP (value, 0)) == PLUS
7426 && REG_P (XEXP (XEXP (value, 0), 0))
7427 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7428 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7430 rtx temp = expand_simple_binop (GET_MODE (value), code,
7431 XEXP (XEXP (value, 0), 0), op2,
7432 subtarget, 0, OPTAB_LIB_WIDEN);
7433 return expand_simple_binop (GET_MODE (value), code, temp,
7434 force_operand (XEXP (XEXP (value,
7435 0), 1), 0),
7436 target, 0, OPTAB_LIB_WIDEN);
7439 op1 = force_operand (XEXP (value, 0), subtarget);
7440 op2 = force_operand (op2, NULL_RTX);
7441 switch (code)
7443 case MULT:
7444 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7445 case DIV:
7446 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7447 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7448 target, 1, OPTAB_LIB_WIDEN);
7449 else
7450 return expand_divmod (0,
7451 FLOAT_MODE_P (GET_MODE (value))
7452 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7453 GET_MODE (value), op1, op2, target, 0);
7454 case MOD:
7455 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7456 target, 0);
7457 case UDIV:
7458 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7459 target, 1);
7460 case UMOD:
7461 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7462 target, 1);
7463 case ASHIFTRT:
7464 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7465 target, 0, OPTAB_LIB_WIDEN);
7466 default:
7467 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7468 target, 1, OPTAB_LIB_WIDEN);
7471 if (UNARY_P (value))
7473 if (!target)
7474 target = gen_reg_rtx (GET_MODE (value));
7475 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7476 switch (code)
7478 case ZERO_EXTEND:
7479 case SIGN_EXTEND:
7480 case TRUNCATE:
7481 case FLOAT_EXTEND:
7482 case FLOAT_TRUNCATE:
7483 convert_move (target, op1, code == ZERO_EXTEND);
7484 return target;
7486 case FIX:
7487 case UNSIGNED_FIX:
7488 expand_fix (target, op1, code == UNSIGNED_FIX);
7489 return target;
7491 case FLOAT:
7492 case UNSIGNED_FLOAT:
7493 expand_float (target, op1, code == UNSIGNED_FLOAT);
7494 return target;
7496 default:
7497 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7501 #ifdef INSN_SCHEDULING
7502 /* On machines that have insn scheduling, we want all memory reference to be
7503 explicit, so we need to deal with such paradoxical SUBREGs. */
7504 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7505 value
7506 = simplify_gen_subreg (GET_MODE (value),
7507 force_reg (GET_MODE (SUBREG_REG (value)),
7508 force_operand (SUBREG_REG (value),
7509 NULL_RTX)),
7510 GET_MODE (SUBREG_REG (value)),
7511 SUBREG_BYTE (value));
7512 #endif
7514 return value;
7517 /* Subroutine of expand_expr: return nonzero iff there is no way that
7518 EXP can reference X, which is being modified. TOP_P is nonzero if this
7519 call is going to be used to determine whether we need a temporary
7520 for EXP, as opposed to a recursive call to this function.
7522 It is always safe for this routine to return zero since it merely
7523 searches for optimization opportunities. */
7526 safe_from_p (const_rtx x, tree exp, int top_p)
7528 rtx exp_rtl = 0;
7529 int i, nops;
7531 if (x == 0
7532 /* If EXP has varying size, we MUST use a target since we currently
7533 have no way of allocating temporaries of variable size
7534 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7535 So we assume here that something at a higher level has prevented a
7536 clash. This is somewhat bogus, but the best we can do. Only
7537 do this when X is BLKmode and when we are at the top level. */
7538 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7539 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7540 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7541 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7542 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7543 != INTEGER_CST)
7544 && GET_MODE (x) == BLKmode)
7545 /* If X is in the outgoing argument area, it is always safe. */
7546 || (MEM_P (x)
7547 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7548 || (GET_CODE (XEXP (x, 0)) == PLUS
7549 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7550 return 1;
7552 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7553 find the underlying pseudo. */
7554 if (GET_CODE (x) == SUBREG)
7556 x = SUBREG_REG (x);
7557 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7558 return 0;
7561 /* Now look at our tree code and possibly recurse. */
7562 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7564 case tcc_declaration:
7565 exp_rtl = DECL_RTL_IF_SET (exp);
7566 break;
7568 case tcc_constant:
7569 return 1;
7571 case tcc_exceptional:
7572 if (TREE_CODE (exp) == TREE_LIST)
7574 while (1)
7576 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7577 return 0;
7578 exp = TREE_CHAIN (exp);
7579 if (!exp)
7580 return 1;
7581 if (TREE_CODE (exp) != TREE_LIST)
7582 return safe_from_p (x, exp, 0);
7585 else if (TREE_CODE (exp) == CONSTRUCTOR)
7587 constructor_elt *ce;
7588 unsigned HOST_WIDE_INT idx;
7590 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7591 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7592 || !safe_from_p (x, ce->value, 0))
7593 return 0;
7594 return 1;
7596 else if (TREE_CODE (exp) == ERROR_MARK)
7597 return 1; /* An already-visited SAVE_EXPR? */
7598 else
7599 return 0;
7601 case tcc_statement:
7602 /* The only case we look at here is the DECL_INITIAL inside a
7603 DECL_EXPR. */
7604 return (TREE_CODE (exp) != DECL_EXPR
7605 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7606 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7607 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7609 case tcc_binary:
7610 case tcc_comparison:
7611 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7612 return 0;
7613 /* Fall through. */
7615 case tcc_unary:
7616 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7618 case tcc_expression:
7619 case tcc_reference:
7620 case tcc_vl_exp:
7621 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7622 the expression. If it is set, we conflict iff we are that rtx or
7623 both are in memory. Otherwise, we check all operands of the
7624 expression recursively. */
7626 switch (TREE_CODE (exp))
7628 case ADDR_EXPR:
7629 /* If the operand is static or we are static, we can't conflict.
7630 Likewise if we don't conflict with the operand at all. */
7631 if (staticp (TREE_OPERAND (exp, 0))
7632 || TREE_STATIC (exp)
7633 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7634 return 1;
7636 /* Otherwise, the only way this can conflict is if we are taking
7637 the address of a DECL a that address if part of X, which is
7638 very rare. */
7639 exp = TREE_OPERAND (exp, 0);
7640 if (DECL_P (exp))
7642 if (!DECL_RTL_SET_P (exp)
7643 || !MEM_P (DECL_RTL (exp)))
7644 return 0;
7645 else
7646 exp_rtl = XEXP (DECL_RTL (exp), 0);
7648 break;
7650 case MEM_REF:
7651 if (MEM_P (x)
7652 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7653 get_alias_set (exp)))
7654 return 0;
7655 break;
7657 case CALL_EXPR:
7658 /* Assume that the call will clobber all hard registers and
7659 all of memory. */
7660 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7661 || MEM_P (x))
7662 return 0;
7663 break;
7665 case WITH_CLEANUP_EXPR:
7666 case CLEANUP_POINT_EXPR:
7667 /* Lowered by gimplify.c. */
7668 gcc_unreachable ();
7670 case SAVE_EXPR:
7671 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7673 default:
7674 break;
7677 /* If we have an rtx, we do not need to scan our operands. */
7678 if (exp_rtl)
7679 break;
7681 nops = TREE_OPERAND_LENGTH (exp);
7682 for (i = 0; i < nops; i++)
7683 if (TREE_OPERAND (exp, i) != 0
7684 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7685 return 0;
7687 break;
7689 case tcc_type:
7690 /* Should never get a type here. */
7691 gcc_unreachable ();
7694 /* If we have an rtl, find any enclosed object. Then see if we conflict
7695 with it. */
7696 if (exp_rtl)
7698 if (GET_CODE (exp_rtl) == SUBREG)
7700 exp_rtl = SUBREG_REG (exp_rtl);
7701 if (REG_P (exp_rtl)
7702 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7703 return 0;
7706 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7707 are memory and they conflict. */
7708 return ! (rtx_equal_p (x, exp_rtl)
7709 || (MEM_P (x) && MEM_P (exp_rtl)
7710 && true_dependence (exp_rtl, VOIDmode, x)));
7713 /* If we reach here, it is safe. */
7714 return 1;
7718 /* Return the highest power of two that EXP is known to be a multiple of.
7719 This is used in updating alignment of MEMs in array references. */
7721 unsigned HOST_WIDE_INT
7722 highest_pow2_factor (const_tree exp)
7724 unsigned HOST_WIDE_INT ret;
7725 int trailing_zeros = tree_ctz (exp);
7726 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7727 return BIGGEST_ALIGNMENT;
7728 ret = HOST_WIDE_INT_1U << trailing_zeros;
7729 if (ret > BIGGEST_ALIGNMENT)
7730 return BIGGEST_ALIGNMENT;
7731 return ret;
7734 /* Similar, except that the alignment requirements of TARGET are
7735 taken into account. Assume it is at least as aligned as its
7736 type, unless it is a COMPONENT_REF in which case the layout of
7737 the structure gives the alignment. */
7739 static unsigned HOST_WIDE_INT
7740 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7742 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7743 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7745 return MAX (factor, talign);
7748 /* Convert the tree comparison code TCODE to the rtl one where the
7749 signedness is UNSIGNEDP. */
7751 static enum rtx_code
7752 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7754 enum rtx_code code;
7755 switch (tcode)
7757 case EQ_EXPR:
7758 code = EQ;
7759 break;
7760 case NE_EXPR:
7761 code = NE;
7762 break;
7763 case LT_EXPR:
7764 code = unsignedp ? LTU : LT;
7765 break;
7766 case LE_EXPR:
7767 code = unsignedp ? LEU : LE;
7768 break;
7769 case GT_EXPR:
7770 code = unsignedp ? GTU : GT;
7771 break;
7772 case GE_EXPR:
7773 code = unsignedp ? GEU : GE;
7774 break;
7775 case UNORDERED_EXPR:
7776 code = UNORDERED;
7777 break;
7778 case ORDERED_EXPR:
7779 code = ORDERED;
7780 break;
7781 case UNLT_EXPR:
7782 code = UNLT;
7783 break;
7784 case UNLE_EXPR:
7785 code = UNLE;
7786 break;
7787 case UNGT_EXPR:
7788 code = UNGT;
7789 break;
7790 case UNGE_EXPR:
7791 code = UNGE;
7792 break;
7793 case UNEQ_EXPR:
7794 code = UNEQ;
7795 break;
7796 case LTGT_EXPR:
7797 code = LTGT;
7798 break;
7800 default:
7801 gcc_unreachable ();
7803 return code;
7806 /* Subroutine of expand_expr. Expand the two operands of a binary
7807 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7808 The value may be stored in TARGET if TARGET is nonzero. The
7809 MODIFIER argument is as documented by expand_expr. */
7811 void
7812 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7813 enum expand_modifier modifier)
7815 if (! safe_from_p (target, exp1, 1))
7816 target = 0;
7817 if (operand_equal_p (exp0, exp1, 0))
7819 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7820 *op1 = copy_rtx (*op0);
7822 else
7824 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7825 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7830 /* Return a MEM that contains constant EXP. DEFER is as for
7831 output_constant_def and MODIFIER is as for expand_expr. */
7833 static rtx
7834 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7836 rtx mem;
7838 mem = output_constant_def (exp, defer);
7839 if (modifier != EXPAND_INITIALIZER)
7840 mem = use_anchored_address (mem);
7841 return mem;
7844 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7845 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7847 static rtx
7848 expand_expr_addr_expr_1 (tree exp, rtx target, scalar_int_mode tmode,
7849 enum expand_modifier modifier, addr_space_t as)
7851 rtx result, subtarget;
7852 tree inner, offset;
7853 poly_int64 bitsize, bitpos;
7854 int unsignedp, reversep, volatilep = 0;
7855 machine_mode mode1;
7857 /* If we are taking the address of a constant and are at the top level,
7858 we have to use output_constant_def since we can't call force_const_mem
7859 at top level. */
7860 /* ??? This should be considered a front-end bug. We should not be
7861 generating ADDR_EXPR of something that isn't an LVALUE. The only
7862 exception here is STRING_CST. */
7863 if (CONSTANT_CLASS_P (exp))
7865 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7866 if (modifier < EXPAND_SUM)
7867 result = force_operand (result, target);
7868 return result;
7871 /* Everything must be something allowed by is_gimple_addressable. */
7872 switch (TREE_CODE (exp))
7874 case INDIRECT_REF:
7875 /* This case will happen via recursion for &a->b. */
7876 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7878 case MEM_REF:
7880 tree tem = TREE_OPERAND (exp, 0);
7881 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7882 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7883 return expand_expr (tem, target, tmode, modifier);
7886 case TARGET_MEM_REF:
7887 return addr_for_mem_ref (exp, as, true);
7889 case CONST_DECL:
7890 /* Expand the initializer like constants above. */
7891 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7892 0, modifier), 0);
7893 if (modifier < EXPAND_SUM)
7894 result = force_operand (result, target);
7895 return result;
7897 case REALPART_EXPR:
7898 /* The real part of the complex number is always first, therefore
7899 the address is the same as the address of the parent object. */
7900 offset = 0;
7901 bitpos = 0;
7902 inner = TREE_OPERAND (exp, 0);
7903 break;
7905 case IMAGPART_EXPR:
7906 /* The imaginary part of the complex number is always second.
7907 The expression is therefore always offset by the size of the
7908 scalar type. */
7909 offset = 0;
7910 bitpos = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (exp)));
7911 inner = TREE_OPERAND (exp, 0);
7912 break;
7914 case COMPOUND_LITERAL_EXPR:
7915 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7916 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7917 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7918 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7919 the initializers aren't gimplified. */
7920 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7921 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7922 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7923 target, tmode, modifier, as);
7924 /* FALLTHRU */
7925 default:
7926 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7927 expand_expr, as that can have various side effects; LABEL_DECLs for
7928 example, may not have their DECL_RTL set yet. Expand the rtl of
7929 CONSTRUCTORs too, which should yield a memory reference for the
7930 constructor's contents. Assume language specific tree nodes can
7931 be expanded in some interesting way. */
7932 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7933 if (DECL_P (exp)
7934 || TREE_CODE (exp) == CONSTRUCTOR
7935 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7937 result = expand_expr (exp, target, tmode,
7938 modifier == EXPAND_INITIALIZER
7939 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7941 /* If the DECL isn't in memory, then the DECL wasn't properly
7942 marked TREE_ADDRESSABLE, which will be either a front-end
7943 or a tree optimizer bug. */
7945 gcc_assert (MEM_P (result));
7946 result = XEXP (result, 0);
7948 /* ??? Is this needed anymore? */
7949 if (DECL_P (exp))
7950 TREE_USED (exp) = 1;
7952 if (modifier != EXPAND_INITIALIZER
7953 && modifier != EXPAND_CONST_ADDRESS
7954 && modifier != EXPAND_SUM)
7955 result = force_operand (result, target);
7956 return result;
7959 /* Pass FALSE as the last argument to get_inner_reference although
7960 we are expanding to RTL. The rationale is that we know how to
7961 handle "aligning nodes" here: we can just bypass them because
7962 they won't change the final object whose address will be returned
7963 (they actually exist only for that purpose). */
7964 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7965 &unsignedp, &reversep, &volatilep);
7966 break;
7969 /* We must have made progress. */
7970 gcc_assert (inner != exp);
7972 subtarget = offset || maybe_ne (bitpos, 0) ? NULL_RTX : target;
7973 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7974 inner alignment, force the inner to be sufficiently aligned. */
7975 if (CONSTANT_CLASS_P (inner)
7976 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7978 inner = copy_node (inner);
7979 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7980 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7981 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7983 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7985 if (offset)
7987 rtx tmp;
7989 if (modifier != EXPAND_NORMAL)
7990 result = force_operand (result, NULL);
7991 tmp = expand_expr (offset, NULL_RTX, tmode,
7992 modifier == EXPAND_INITIALIZER
7993 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7995 /* expand_expr is allowed to return an object in a mode other
7996 than TMODE. If it did, we need to convert. */
7997 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7998 tmp = convert_modes (tmode, GET_MODE (tmp),
7999 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
8000 result = convert_memory_address_addr_space (tmode, result, as);
8001 tmp = convert_memory_address_addr_space (tmode, tmp, as);
8003 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8004 result = simplify_gen_binary (PLUS, tmode, result, tmp);
8005 else
8007 subtarget = maybe_ne (bitpos, 0) ? NULL_RTX : target;
8008 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
8009 1, OPTAB_LIB_WIDEN);
8013 if (maybe_ne (bitpos, 0))
8015 /* Someone beforehand should have rejected taking the address
8016 of an object that isn't byte-aligned. */
8017 poly_int64 bytepos = exact_div (bitpos, BITS_PER_UNIT);
8018 result = convert_memory_address_addr_space (tmode, result, as);
8019 result = plus_constant (tmode, result, bytepos);
8020 if (modifier < EXPAND_SUM)
8021 result = force_operand (result, target);
8024 return result;
8027 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
8028 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
8030 static rtx
8031 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
8032 enum expand_modifier modifier)
8034 addr_space_t as = ADDR_SPACE_GENERIC;
8035 scalar_int_mode address_mode = Pmode;
8036 scalar_int_mode pointer_mode = ptr_mode;
8037 machine_mode rmode;
8038 rtx result;
8040 /* Target mode of VOIDmode says "whatever's natural". */
8041 if (tmode == VOIDmode)
8042 tmode = TYPE_MODE (TREE_TYPE (exp));
8044 if (POINTER_TYPE_P (TREE_TYPE (exp)))
8046 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
8047 address_mode = targetm.addr_space.address_mode (as);
8048 pointer_mode = targetm.addr_space.pointer_mode (as);
8051 /* We can get called with some Weird Things if the user does silliness
8052 like "(short) &a". In that case, convert_memory_address won't do
8053 the right thing, so ignore the given target mode. */
8054 scalar_int_mode new_tmode = (tmode == pointer_mode
8055 ? pointer_mode
8056 : address_mode);
8058 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
8059 new_tmode, modifier, as);
8061 /* Despite expand_expr claims concerning ignoring TMODE when not
8062 strictly convenient, stuff breaks if we don't honor it. Note
8063 that combined with the above, we only do this for pointer modes. */
8064 rmode = GET_MODE (result);
8065 if (rmode == VOIDmode)
8066 rmode = new_tmode;
8067 if (rmode != new_tmode)
8068 result = convert_memory_address_addr_space (new_tmode, result, as);
8070 return result;
8073 /* Generate code for computing CONSTRUCTOR EXP.
8074 An rtx for the computed value is returned. If AVOID_TEMP_MEM
8075 is TRUE, instead of creating a temporary variable in memory
8076 NULL is returned and the caller needs to handle it differently. */
8078 static rtx
8079 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
8080 bool avoid_temp_mem)
8082 tree type = TREE_TYPE (exp);
8083 machine_mode mode = TYPE_MODE (type);
8085 /* Try to avoid creating a temporary at all. This is possible
8086 if all of the initializer is zero.
8087 FIXME: try to handle all [0..255] initializers we can handle
8088 with memset. */
8089 if (TREE_STATIC (exp)
8090 && !TREE_ADDRESSABLE (exp)
8091 && target != 0 && mode == BLKmode
8092 && all_zeros_p (exp))
8094 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
8095 return target;
8098 /* All elts simple constants => refer to a constant in memory. But
8099 if this is a non-BLKmode mode, let it store a field at a time
8100 since that should make a CONST_INT, CONST_WIDE_INT or
8101 CONST_DOUBLE when we fold. Likewise, if we have a target we can
8102 use, it is best to store directly into the target unless the type
8103 is large enough that memcpy will be used. If we are making an
8104 initializer and all operands are constant, put it in memory as
8105 well.
8107 FIXME: Avoid trying to fill vector constructors piece-meal.
8108 Output them with output_constant_def below unless we're sure
8109 they're zeros. This should go away when vector initializers
8110 are treated like VECTOR_CST instead of arrays. */
8111 if ((TREE_STATIC (exp)
8112 && ((mode == BLKmode
8113 && ! (target != 0 && safe_from_p (target, exp, 1)))
8114 || TREE_ADDRESSABLE (exp)
8115 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
8116 && (! can_move_by_pieces
8117 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
8118 TYPE_ALIGN (type)))
8119 && ! mostly_zeros_p (exp))))
8120 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
8121 && TREE_CONSTANT (exp)))
8123 rtx constructor;
8125 if (avoid_temp_mem)
8126 return NULL_RTX;
8128 constructor = expand_expr_constant (exp, 1, modifier);
8130 if (modifier != EXPAND_CONST_ADDRESS
8131 && modifier != EXPAND_INITIALIZER
8132 && modifier != EXPAND_SUM)
8133 constructor = validize_mem (constructor);
8135 return constructor;
8138 /* Handle calls that pass values in multiple non-contiguous
8139 locations. The Irix 6 ABI has examples of this. */
8140 if (target == 0 || ! safe_from_p (target, exp, 1)
8141 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8143 if (avoid_temp_mem)
8144 return NULL_RTX;
8146 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8149 store_constructor (exp, target, 0, int_expr_size (exp), false);
8150 return target;
8154 /* expand_expr: generate code for computing expression EXP.
8155 An rtx for the computed value is returned. The value is never null.
8156 In the case of a void EXP, const0_rtx is returned.
8158 The value may be stored in TARGET if TARGET is nonzero.
8159 TARGET is just a suggestion; callers must assume that
8160 the rtx returned may not be the same as TARGET.
8162 If TARGET is CONST0_RTX, it means that the value will be ignored.
8164 If TMODE is not VOIDmode, it suggests generating the
8165 result in mode TMODE. But this is done only when convenient.
8166 Otherwise, TMODE is ignored and the value generated in its natural mode.
8167 TMODE is just a suggestion; callers must assume that
8168 the rtx returned may not have mode TMODE.
8170 Note that TARGET may have neither TMODE nor MODE. In that case, it
8171 probably will not be used.
8173 If MODIFIER is EXPAND_SUM then when EXP is an addition
8174 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8175 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8176 products as above, or REG or MEM, or constant.
8177 Ordinarily in such cases we would output mul or add instructions
8178 and then return a pseudo reg containing the sum.
8180 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8181 it also marks a label as absolutely required (it can't be dead).
8182 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8183 This is used for outputting expressions used in initializers.
8185 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8186 with a constant address even if that address is not normally legitimate.
8187 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8189 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8190 a call parameter. Such targets require special care as we haven't yet
8191 marked TARGET so that it's safe from being trashed by libcalls. We
8192 don't want to use TARGET for anything but the final result;
8193 Intermediate values must go elsewhere. Additionally, calls to
8194 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8196 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8197 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8198 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8199 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8200 recursively.
8202 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8203 In this case, we don't adjust a returned MEM rtx that wouldn't be
8204 sufficiently aligned for its mode; instead, it's up to the caller
8205 to deal with it afterwards. This is used to make sure that unaligned
8206 base objects for which out-of-bounds accesses are supported, for
8207 example record types with trailing arrays, aren't realigned behind
8208 the back of the caller.
8209 The normal operating mode is to pass FALSE for this parameter. */
8212 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8213 enum expand_modifier modifier, rtx *alt_rtl,
8214 bool inner_reference_p)
8216 rtx ret;
8218 /* Handle ERROR_MARK before anybody tries to access its type. */
8219 if (TREE_CODE (exp) == ERROR_MARK
8220 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8222 ret = CONST0_RTX (tmode);
8223 return ret ? ret : const0_rtx;
8226 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8227 inner_reference_p);
8228 return ret;
8231 /* Try to expand the conditional expression which is represented by
8232 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8233 return the rtl reg which represents the result. Otherwise return
8234 NULL_RTX. */
8236 static rtx
8237 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8238 tree treeop1 ATTRIBUTE_UNUSED,
8239 tree treeop2 ATTRIBUTE_UNUSED)
8241 rtx insn;
8242 rtx op00, op01, op1, op2;
8243 enum rtx_code comparison_code;
8244 machine_mode comparison_mode;
8245 gimple *srcstmt;
8246 rtx temp;
8247 tree type = TREE_TYPE (treeop1);
8248 int unsignedp = TYPE_UNSIGNED (type);
8249 machine_mode mode = TYPE_MODE (type);
8250 machine_mode orig_mode = mode;
8251 static bool expanding_cond_expr_using_cmove = false;
8253 /* Conditional move expansion can end up TERing two operands which,
8254 when recursively hitting conditional expressions can result in
8255 exponential behavior if the cmove expansion ultimatively fails.
8256 It's hardly profitable to TER a cmove into a cmove so avoid doing
8257 that by failing early if we end up recursing. */
8258 if (expanding_cond_expr_using_cmove)
8259 return NULL_RTX;
8261 /* If we cannot do a conditional move on the mode, try doing it
8262 with the promoted mode. */
8263 if (!can_conditionally_move_p (mode))
8265 mode = promote_mode (type, mode, &unsignedp);
8266 if (!can_conditionally_move_p (mode))
8267 return NULL_RTX;
8268 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8270 else
8271 temp = assign_temp (type, 0, 1);
8273 expanding_cond_expr_using_cmove = true;
8274 start_sequence ();
8275 expand_operands (treeop1, treeop2,
8276 temp, &op1, &op2, EXPAND_NORMAL);
8278 if (TREE_CODE (treeop0) == SSA_NAME
8279 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8281 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8282 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8283 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8284 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8285 comparison_mode = TYPE_MODE (type);
8286 unsignedp = TYPE_UNSIGNED (type);
8287 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8289 else if (COMPARISON_CLASS_P (treeop0))
8291 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8292 enum tree_code cmpcode = TREE_CODE (treeop0);
8293 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8294 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8295 unsignedp = TYPE_UNSIGNED (type);
8296 comparison_mode = TYPE_MODE (type);
8297 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8299 else
8301 op00 = expand_normal (treeop0);
8302 op01 = const0_rtx;
8303 comparison_code = NE;
8304 comparison_mode = GET_MODE (op00);
8305 if (comparison_mode == VOIDmode)
8306 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8308 expanding_cond_expr_using_cmove = false;
8310 if (GET_MODE (op1) != mode)
8311 op1 = gen_lowpart (mode, op1);
8313 if (GET_MODE (op2) != mode)
8314 op2 = gen_lowpart (mode, op2);
8316 /* Try to emit the conditional move. */
8317 insn = emit_conditional_move (temp, comparison_code,
8318 op00, op01, comparison_mode,
8319 op1, op2, mode,
8320 unsignedp);
8322 /* If we could do the conditional move, emit the sequence,
8323 and return. */
8324 if (insn)
8326 rtx_insn *seq = get_insns ();
8327 end_sequence ();
8328 emit_insn (seq);
8329 return convert_modes (orig_mode, mode, temp, 0);
8332 /* Otherwise discard the sequence and fall back to code with
8333 branches. */
8334 end_sequence ();
8335 return NULL_RTX;
8339 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8340 enum expand_modifier modifier)
8342 rtx op0, op1, op2, temp;
8343 rtx_code_label *lab;
8344 tree type;
8345 int unsignedp;
8346 machine_mode mode;
8347 scalar_int_mode int_mode;
8348 enum tree_code code = ops->code;
8349 optab this_optab;
8350 rtx subtarget, original_target;
8351 int ignore;
8352 bool reduce_bit_field;
8353 location_t loc = ops->location;
8354 tree treeop0, treeop1, treeop2;
8355 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8356 ? reduce_to_bit_field_precision ((expr), \
8357 target, \
8358 type) \
8359 : (expr))
8361 type = ops->type;
8362 mode = TYPE_MODE (type);
8363 unsignedp = TYPE_UNSIGNED (type);
8365 treeop0 = ops->op0;
8366 treeop1 = ops->op1;
8367 treeop2 = ops->op2;
8369 /* We should be called only on simple (binary or unary) expressions,
8370 exactly those that are valid in gimple expressions that aren't
8371 GIMPLE_SINGLE_RHS (or invalid). */
8372 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8373 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8374 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8376 ignore = (target == const0_rtx
8377 || ((CONVERT_EXPR_CODE_P (code)
8378 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8379 && TREE_CODE (type) == VOID_TYPE));
8381 /* We should be called only if we need the result. */
8382 gcc_assert (!ignore);
8384 /* An operation in what may be a bit-field type needs the
8385 result to be reduced to the precision of the bit-field type,
8386 which is narrower than that of the type's mode. */
8387 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8388 && !type_has_mode_precision_p (type));
8390 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8391 target = 0;
8393 /* Use subtarget as the target for operand 0 of a binary operation. */
8394 subtarget = get_subtarget (target);
8395 original_target = target;
8397 switch (code)
8399 case NON_LVALUE_EXPR:
8400 case PAREN_EXPR:
8401 CASE_CONVERT:
8402 if (treeop0 == error_mark_node)
8403 return const0_rtx;
8405 if (TREE_CODE (type) == UNION_TYPE)
8407 tree valtype = TREE_TYPE (treeop0);
8409 /* If both input and output are BLKmode, this conversion isn't doing
8410 anything except possibly changing memory attribute. */
8411 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8413 rtx result = expand_expr (treeop0, target, tmode,
8414 modifier);
8416 result = copy_rtx (result);
8417 set_mem_attributes (result, type, 0);
8418 return result;
8421 if (target == 0)
8423 if (TYPE_MODE (type) != BLKmode)
8424 target = gen_reg_rtx (TYPE_MODE (type));
8425 else
8426 target = assign_temp (type, 1, 1);
8429 if (MEM_P (target))
8430 /* Store data into beginning of memory target. */
8431 store_expr (treeop0,
8432 adjust_address (target, TYPE_MODE (valtype), 0),
8433 modifier == EXPAND_STACK_PARM,
8434 false, TYPE_REVERSE_STORAGE_ORDER (type));
8436 else
8438 gcc_assert (REG_P (target)
8439 && !TYPE_REVERSE_STORAGE_ORDER (type));
8441 /* Store this field into a union of the proper type. */
8442 poly_uint64 op0_size
8443 = tree_to_poly_uint64 (TYPE_SIZE (TREE_TYPE (treeop0)));
8444 poly_uint64 union_size = GET_MODE_BITSIZE (mode);
8445 store_field (target,
8446 /* The conversion must be constructed so that
8447 we know at compile time how many bits
8448 to preserve. */
8449 ordered_min (op0_size, union_size),
8450 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8451 false, false);
8454 /* Return the entire union. */
8455 return target;
8458 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8460 op0 = expand_expr (treeop0, target, VOIDmode,
8461 modifier);
8463 /* If the signedness of the conversion differs and OP0 is
8464 a promoted SUBREG, clear that indication since we now
8465 have to do the proper extension. */
8466 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8467 && GET_CODE (op0) == SUBREG)
8468 SUBREG_PROMOTED_VAR_P (op0) = 0;
8470 return REDUCE_BIT_FIELD (op0);
8473 op0 = expand_expr (treeop0, NULL_RTX, mode,
8474 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8475 if (GET_MODE (op0) == mode)
8478 /* If OP0 is a constant, just convert it into the proper mode. */
8479 else if (CONSTANT_P (op0))
8481 tree inner_type = TREE_TYPE (treeop0);
8482 machine_mode inner_mode = GET_MODE (op0);
8484 if (inner_mode == VOIDmode)
8485 inner_mode = TYPE_MODE (inner_type);
8487 if (modifier == EXPAND_INITIALIZER)
8488 op0 = lowpart_subreg (mode, op0, inner_mode);
8489 else
8490 op0= convert_modes (mode, inner_mode, op0,
8491 TYPE_UNSIGNED (inner_type));
8494 else if (modifier == EXPAND_INITIALIZER)
8495 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8496 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8498 else if (target == 0)
8499 op0 = convert_to_mode (mode, op0,
8500 TYPE_UNSIGNED (TREE_TYPE
8501 (treeop0)));
8502 else
8504 convert_move (target, op0,
8505 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8506 op0 = target;
8509 return REDUCE_BIT_FIELD (op0);
8511 case ADDR_SPACE_CONVERT_EXPR:
8513 tree treeop0_type = TREE_TYPE (treeop0);
8515 gcc_assert (POINTER_TYPE_P (type));
8516 gcc_assert (POINTER_TYPE_P (treeop0_type));
8518 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8519 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8521 /* Conversions between pointers to the same address space should
8522 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8523 gcc_assert (as_to != as_from);
8525 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8527 /* Ask target code to handle conversion between pointers
8528 to overlapping address spaces. */
8529 if (targetm.addr_space.subset_p (as_to, as_from)
8530 || targetm.addr_space.subset_p (as_from, as_to))
8532 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8534 else
8536 /* For disjoint address spaces, converting anything but a null
8537 pointer invokes undefined behavior. We truncate or extend the
8538 value as if we'd converted via integers, which handles 0 as
8539 required, and all others as the programmer likely expects. */
8540 #ifndef POINTERS_EXTEND_UNSIGNED
8541 const int POINTERS_EXTEND_UNSIGNED = 1;
8542 #endif
8543 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8544 op0, POINTERS_EXTEND_UNSIGNED);
8546 gcc_assert (op0);
8547 return op0;
8550 case POINTER_PLUS_EXPR:
8551 /* Even though the sizetype mode and the pointer's mode can be different
8552 expand is able to handle this correctly and get the correct result out
8553 of the PLUS_EXPR code. */
8554 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8555 if sizetype precision is smaller than pointer precision. */
8556 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8557 treeop1 = fold_convert_loc (loc, type,
8558 fold_convert_loc (loc, ssizetype,
8559 treeop1));
8560 /* If sizetype precision is larger than pointer precision, truncate the
8561 offset to have matching modes. */
8562 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8563 treeop1 = fold_convert_loc (loc, type, treeop1);
8564 /* FALLTHRU */
8566 case PLUS_EXPR:
8567 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8568 something else, make sure we add the register to the constant and
8569 then to the other thing. This case can occur during strength
8570 reduction and doing it this way will produce better code if the
8571 frame pointer or argument pointer is eliminated.
8573 fold-const.c will ensure that the constant is always in the inner
8574 PLUS_EXPR, so the only case we need to do anything about is if
8575 sp, ap, or fp is our second argument, in which case we must swap
8576 the innermost first argument and our second argument. */
8578 if (TREE_CODE (treeop0) == PLUS_EXPR
8579 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8580 && VAR_P (treeop1)
8581 && (DECL_RTL (treeop1) == frame_pointer_rtx
8582 || DECL_RTL (treeop1) == stack_pointer_rtx
8583 || DECL_RTL (treeop1) == arg_pointer_rtx))
8585 gcc_unreachable ();
8588 /* If the result is to be ptr_mode and we are adding an integer to
8589 something, we might be forming a constant. So try to use
8590 plus_constant. If it produces a sum and we can't accept it,
8591 use force_operand. This allows P = &ARR[const] to generate
8592 efficient code on machines where a SYMBOL_REF is not a valid
8593 address.
8595 If this is an EXPAND_SUM call, always return the sum. */
8596 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8597 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8599 if (modifier == EXPAND_STACK_PARM)
8600 target = 0;
8601 if (TREE_CODE (treeop0) == INTEGER_CST
8602 && HWI_COMPUTABLE_MODE_P (mode)
8603 && TREE_CONSTANT (treeop1))
8605 rtx constant_part;
8606 HOST_WIDE_INT wc;
8607 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8609 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8610 EXPAND_SUM);
8611 /* Use wi::shwi to ensure that the constant is
8612 truncated according to the mode of OP1, then sign extended
8613 to a HOST_WIDE_INT. Using the constant directly can result
8614 in non-canonical RTL in a 64x32 cross compile. */
8615 wc = TREE_INT_CST_LOW (treeop0);
8616 constant_part =
8617 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8618 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8619 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8620 op1 = force_operand (op1, target);
8621 return REDUCE_BIT_FIELD (op1);
8624 else if (TREE_CODE (treeop1) == INTEGER_CST
8625 && HWI_COMPUTABLE_MODE_P (mode)
8626 && TREE_CONSTANT (treeop0))
8628 rtx constant_part;
8629 HOST_WIDE_INT wc;
8630 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8632 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8633 (modifier == EXPAND_INITIALIZER
8634 ? EXPAND_INITIALIZER : EXPAND_SUM));
8635 if (! CONSTANT_P (op0))
8637 op1 = expand_expr (treeop1, NULL_RTX,
8638 VOIDmode, modifier);
8639 /* Return a PLUS if modifier says it's OK. */
8640 if (modifier == EXPAND_SUM
8641 || modifier == EXPAND_INITIALIZER)
8642 return simplify_gen_binary (PLUS, mode, op0, op1);
8643 goto binop2;
8645 /* Use wi::shwi to ensure that the constant is
8646 truncated according to the mode of OP1, then sign extended
8647 to a HOST_WIDE_INT. Using the constant directly can result
8648 in non-canonical RTL in a 64x32 cross compile. */
8649 wc = TREE_INT_CST_LOW (treeop1);
8650 constant_part
8651 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8652 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8653 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8654 op0 = force_operand (op0, target);
8655 return REDUCE_BIT_FIELD (op0);
8659 /* Use TER to expand pointer addition of a negated value
8660 as pointer subtraction. */
8661 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8662 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8663 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8664 && TREE_CODE (treeop1) == SSA_NAME
8665 && TYPE_MODE (TREE_TYPE (treeop0))
8666 == TYPE_MODE (TREE_TYPE (treeop1)))
8668 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8669 if (def)
8671 treeop1 = gimple_assign_rhs1 (def);
8672 code = MINUS_EXPR;
8673 goto do_minus;
8677 /* No sense saving up arithmetic to be done
8678 if it's all in the wrong mode to form part of an address.
8679 And force_operand won't know whether to sign-extend or
8680 zero-extend. */
8681 if (modifier != EXPAND_INITIALIZER
8682 && (modifier != EXPAND_SUM || mode != ptr_mode))
8684 expand_operands (treeop0, treeop1,
8685 subtarget, &op0, &op1, modifier);
8686 if (op0 == const0_rtx)
8687 return op1;
8688 if (op1 == const0_rtx)
8689 return op0;
8690 goto binop2;
8693 expand_operands (treeop0, treeop1,
8694 subtarget, &op0, &op1, modifier);
8695 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8697 case MINUS_EXPR:
8698 case POINTER_DIFF_EXPR:
8699 do_minus:
8700 /* For initializers, we are allowed to return a MINUS of two
8701 symbolic constants. Here we handle all cases when both operands
8702 are constant. */
8703 /* Handle difference of two symbolic constants,
8704 for the sake of an initializer. */
8705 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8706 && really_constant_p (treeop0)
8707 && really_constant_p (treeop1))
8709 expand_operands (treeop0, treeop1,
8710 NULL_RTX, &op0, &op1, modifier);
8711 return simplify_gen_binary (MINUS, mode, op0, op1);
8714 /* No sense saving up arithmetic to be done
8715 if it's all in the wrong mode to form part of an address.
8716 And force_operand won't know whether to sign-extend or
8717 zero-extend. */
8718 if (modifier != EXPAND_INITIALIZER
8719 && (modifier != EXPAND_SUM || mode != ptr_mode))
8720 goto binop;
8722 expand_operands (treeop0, treeop1,
8723 subtarget, &op0, &op1, modifier);
8725 /* Convert A - const to A + (-const). */
8726 if (CONST_INT_P (op1))
8728 op1 = negate_rtx (mode, op1);
8729 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8732 goto binop2;
8734 case WIDEN_MULT_PLUS_EXPR:
8735 case WIDEN_MULT_MINUS_EXPR:
8736 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8737 op2 = expand_normal (treeop2);
8738 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8739 target, unsignedp);
8740 return target;
8742 case WIDEN_MULT_EXPR:
8743 /* If first operand is constant, swap them.
8744 Thus the following special case checks need only
8745 check the second operand. */
8746 if (TREE_CODE (treeop0) == INTEGER_CST)
8747 std::swap (treeop0, treeop1);
8749 /* First, check if we have a multiplication of one signed and one
8750 unsigned operand. */
8751 if (TREE_CODE (treeop1) != INTEGER_CST
8752 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8753 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8755 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8756 this_optab = usmul_widen_optab;
8757 if (find_widening_optab_handler (this_optab, mode, innermode)
8758 != CODE_FOR_nothing)
8760 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8761 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8762 EXPAND_NORMAL);
8763 else
8764 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8765 EXPAND_NORMAL);
8766 /* op0 and op1 might still be constant, despite the above
8767 != INTEGER_CST check. Handle it. */
8768 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8770 op0 = convert_modes (innermode, mode, op0, true);
8771 op1 = convert_modes (innermode, mode, op1, false);
8772 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8773 target, unsignedp));
8775 goto binop3;
8778 /* Check for a multiplication with matching signedness. */
8779 else if ((TREE_CODE (treeop1) == INTEGER_CST
8780 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8781 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8782 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8784 tree op0type = TREE_TYPE (treeop0);
8785 machine_mode innermode = TYPE_MODE (op0type);
8786 bool zextend_p = TYPE_UNSIGNED (op0type);
8787 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8788 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8790 if (TREE_CODE (treeop0) != INTEGER_CST)
8792 if (find_widening_optab_handler (this_optab, mode, innermode)
8793 != CODE_FOR_nothing)
8795 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8796 EXPAND_NORMAL);
8797 /* op0 and op1 might still be constant, despite the above
8798 != INTEGER_CST check. Handle it. */
8799 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8801 widen_mult_const:
8802 op0 = convert_modes (innermode, mode, op0, zextend_p);
8804 = convert_modes (innermode, mode, op1,
8805 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8806 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8807 target,
8808 unsignedp));
8810 temp = expand_widening_mult (mode, op0, op1, target,
8811 unsignedp, this_optab);
8812 return REDUCE_BIT_FIELD (temp);
8814 if (find_widening_optab_handler (other_optab, mode, innermode)
8815 != CODE_FOR_nothing
8816 && innermode == word_mode)
8818 rtx htem, hipart;
8819 op0 = expand_normal (treeop0);
8820 if (TREE_CODE (treeop1) == INTEGER_CST)
8821 op1 = convert_modes (word_mode, mode,
8822 expand_normal (treeop1),
8823 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8824 else
8825 op1 = expand_normal (treeop1);
8826 /* op0 and op1 might still be constant, despite the above
8827 != INTEGER_CST check. Handle it. */
8828 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8829 goto widen_mult_const;
8830 temp = expand_binop (mode, other_optab, op0, op1, target,
8831 unsignedp, OPTAB_LIB_WIDEN);
8832 hipart = gen_highpart (word_mode, temp);
8833 htem = expand_mult_highpart_adjust (word_mode, hipart,
8834 op0, op1, hipart,
8835 zextend_p);
8836 if (htem != hipart)
8837 emit_move_insn (hipart, htem);
8838 return REDUCE_BIT_FIELD (temp);
8842 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8843 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8844 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8845 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8847 case FMA_EXPR:
8849 optab opt = fma_optab;
8850 gimple *def0, *def2;
8852 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8853 call. */
8854 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8856 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8857 tree call_expr;
8859 gcc_assert (fn != NULL_TREE);
8860 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8861 return expand_builtin (call_expr, target, subtarget, mode, false);
8864 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8865 /* The multiplication is commutative - look at its 2nd operand
8866 if the first isn't fed by a negate. */
8867 if (!def0)
8869 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8870 /* Swap operands if the 2nd operand is fed by a negate. */
8871 if (def0)
8872 std::swap (treeop0, treeop1);
8874 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8876 op0 = op2 = NULL;
8878 if (def0 && def2
8879 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8881 opt = fnms_optab;
8882 op0 = expand_normal (gimple_assign_rhs1 (def0));
8883 op2 = expand_normal (gimple_assign_rhs1 (def2));
8885 else if (def0
8886 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8888 opt = fnma_optab;
8889 op0 = expand_normal (gimple_assign_rhs1 (def0));
8891 else if (def2
8892 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8894 opt = fms_optab;
8895 op2 = expand_normal (gimple_assign_rhs1 (def2));
8898 if (op0 == NULL)
8899 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8900 if (op2 == NULL)
8901 op2 = expand_normal (treeop2);
8902 op1 = expand_normal (treeop1);
8904 return expand_ternary_op (TYPE_MODE (type), opt,
8905 op0, op1, op2, target, 0);
8908 case MULT_EXPR:
8909 /* If this is a fixed-point operation, then we cannot use the code
8910 below because "expand_mult" doesn't support sat/no-sat fixed-point
8911 multiplications. */
8912 if (ALL_FIXED_POINT_MODE_P (mode))
8913 goto binop;
8915 /* If first operand is constant, swap them.
8916 Thus the following special case checks need only
8917 check the second operand. */
8918 if (TREE_CODE (treeop0) == INTEGER_CST)
8919 std::swap (treeop0, treeop1);
8921 /* Attempt to return something suitable for generating an
8922 indexed address, for machines that support that. */
8924 if (modifier == EXPAND_SUM && mode == ptr_mode
8925 && tree_fits_shwi_p (treeop1))
8927 tree exp1 = treeop1;
8929 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8930 EXPAND_SUM);
8932 if (!REG_P (op0))
8933 op0 = force_operand (op0, NULL_RTX);
8934 if (!REG_P (op0))
8935 op0 = copy_to_mode_reg (mode, op0);
8937 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8938 gen_int_mode (tree_to_shwi (exp1),
8939 TYPE_MODE (TREE_TYPE (exp1)))));
8942 if (modifier == EXPAND_STACK_PARM)
8943 target = 0;
8945 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8946 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8948 case TRUNC_MOD_EXPR:
8949 case FLOOR_MOD_EXPR:
8950 case CEIL_MOD_EXPR:
8951 case ROUND_MOD_EXPR:
8953 case TRUNC_DIV_EXPR:
8954 case FLOOR_DIV_EXPR:
8955 case CEIL_DIV_EXPR:
8956 case ROUND_DIV_EXPR:
8957 case EXACT_DIV_EXPR:
8959 /* If this is a fixed-point operation, then we cannot use the code
8960 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8961 divisions. */
8962 if (ALL_FIXED_POINT_MODE_P (mode))
8963 goto binop;
8965 if (modifier == EXPAND_STACK_PARM)
8966 target = 0;
8967 /* Possible optimization: compute the dividend with EXPAND_SUM
8968 then if the divisor is constant can optimize the case
8969 where some terms of the dividend have coeffs divisible by it. */
8970 expand_operands (treeop0, treeop1,
8971 subtarget, &op0, &op1, EXPAND_NORMAL);
8972 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8973 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8974 if (SCALAR_INT_MODE_P (mode)
8975 && optimize >= 2
8976 && get_range_pos_neg (treeop0) == 1
8977 && get_range_pos_neg (treeop1) == 1)
8979 /* If both arguments are known to be positive when interpreted
8980 as signed, we can expand it as both signed and unsigned
8981 division or modulo. Choose the cheaper sequence in that case. */
8982 bool speed_p = optimize_insn_for_speed_p ();
8983 do_pending_stack_adjust ();
8984 start_sequence ();
8985 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8986 rtx_insn *uns_insns = get_insns ();
8987 end_sequence ();
8988 start_sequence ();
8989 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8990 rtx_insn *sgn_insns = get_insns ();
8991 end_sequence ();
8992 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8993 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8995 /* If costs are the same then use as tie breaker the other
8996 other factor. */
8997 if (uns_cost == sgn_cost)
8999 uns_cost = seq_cost (uns_insns, !speed_p);
9000 sgn_cost = seq_cost (sgn_insns, !speed_p);
9003 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
9005 emit_insn (uns_insns);
9006 return uns_ret;
9008 emit_insn (sgn_insns);
9009 return sgn_ret;
9011 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
9013 case RDIV_EXPR:
9014 goto binop;
9016 case MULT_HIGHPART_EXPR:
9017 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
9018 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
9019 gcc_assert (temp);
9020 return temp;
9022 case FIXED_CONVERT_EXPR:
9023 op0 = expand_normal (treeop0);
9024 if (target == 0 || modifier == EXPAND_STACK_PARM)
9025 target = gen_reg_rtx (mode);
9027 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
9028 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
9029 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
9030 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
9031 else
9032 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
9033 return target;
9035 case FIX_TRUNC_EXPR:
9036 op0 = expand_normal (treeop0);
9037 if (target == 0 || modifier == EXPAND_STACK_PARM)
9038 target = gen_reg_rtx (mode);
9039 expand_fix (target, op0, unsignedp);
9040 return target;
9042 case FLOAT_EXPR:
9043 op0 = expand_normal (treeop0);
9044 if (target == 0 || modifier == EXPAND_STACK_PARM)
9045 target = gen_reg_rtx (mode);
9046 /* expand_float can't figure out what to do if FROM has VOIDmode.
9047 So give it the correct mode. With -O, cse will optimize this. */
9048 if (GET_MODE (op0) == VOIDmode)
9049 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
9050 op0);
9051 expand_float (target, op0,
9052 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9053 return target;
9055 case NEGATE_EXPR:
9056 op0 = expand_expr (treeop0, subtarget,
9057 VOIDmode, EXPAND_NORMAL);
9058 if (modifier == EXPAND_STACK_PARM)
9059 target = 0;
9060 temp = expand_unop (mode,
9061 optab_for_tree_code (NEGATE_EXPR, type,
9062 optab_default),
9063 op0, target, 0);
9064 gcc_assert (temp);
9065 return REDUCE_BIT_FIELD (temp);
9067 case ABS_EXPR:
9068 op0 = expand_expr (treeop0, subtarget,
9069 VOIDmode, EXPAND_NORMAL);
9070 if (modifier == EXPAND_STACK_PARM)
9071 target = 0;
9073 /* ABS_EXPR is not valid for complex arguments. */
9074 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9075 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
9077 /* Unsigned abs is simply the operand. Testing here means we don't
9078 risk generating incorrect code below. */
9079 if (TYPE_UNSIGNED (type))
9080 return op0;
9082 return expand_abs (mode, op0, target, unsignedp,
9083 safe_from_p (target, treeop0, 1));
9085 case MAX_EXPR:
9086 case MIN_EXPR:
9087 target = original_target;
9088 if (target == 0
9089 || modifier == EXPAND_STACK_PARM
9090 || (MEM_P (target) && MEM_VOLATILE_P (target))
9091 || GET_MODE (target) != mode
9092 || (REG_P (target)
9093 && REGNO (target) < FIRST_PSEUDO_REGISTER))
9094 target = gen_reg_rtx (mode);
9095 expand_operands (treeop0, treeop1,
9096 target, &op0, &op1, EXPAND_NORMAL);
9098 /* First try to do it with a special MIN or MAX instruction.
9099 If that does not win, use a conditional jump to select the proper
9100 value. */
9101 this_optab = optab_for_tree_code (code, type, optab_default);
9102 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
9103 OPTAB_WIDEN);
9104 if (temp != 0)
9105 return temp;
9107 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
9108 and similarly for MAX <x, y>. */
9109 if (VECTOR_TYPE_P (type))
9111 tree t0 = make_tree (type, op0);
9112 tree t1 = make_tree (type, op1);
9113 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
9114 type, t0, t1);
9115 return expand_vec_cond_expr (type, comparison, t0, t1,
9116 original_target);
9119 /* At this point, a MEM target is no longer useful; we will get better
9120 code without it. */
9122 if (! REG_P (target))
9123 target = gen_reg_rtx (mode);
9125 /* If op1 was placed in target, swap op0 and op1. */
9126 if (target != op0 && target == op1)
9127 std::swap (op0, op1);
9129 /* We generate better code and avoid problems with op1 mentioning
9130 target by forcing op1 into a pseudo if it isn't a constant. */
9131 if (! CONSTANT_P (op1))
9132 op1 = force_reg (mode, op1);
9135 enum rtx_code comparison_code;
9136 rtx cmpop1 = op1;
9138 if (code == MAX_EXPR)
9139 comparison_code = unsignedp ? GEU : GE;
9140 else
9141 comparison_code = unsignedp ? LEU : LE;
9143 /* Canonicalize to comparisons against 0. */
9144 if (op1 == const1_rtx)
9146 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9147 or (a != 0 ? a : 1) for unsigned.
9148 For MIN we are safe converting (a <= 1 ? a : 1)
9149 into (a <= 0 ? a : 1) */
9150 cmpop1 = const0_rtx;
9151 if (code == MAX_EXPR)
9152 comparison_code = unsignedp ? NE : GT;
9154 if (op1 == constm1_rtx && !unsignedp)
9156 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9157 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9158 cmpop1 = const0_rtx;
9159 if (code == MIN_EXPR)
9160 comparison_code = LT;
9163 /* Use a conditional move if possible. */
9164 if (can_conditionally_move_p (mode))
9166 rtx insn;
9168 start_sequence ();
9170 /* Try to emit the conditional move. */
9171 insn = emit_conditional_move (target, comparison_code,
9172 op0, cmpop1, mode,
9173 op0, op1, mode,
9174 unsignedp);
9176 /* If we could do the conditional move, emit the sequence,
9177 and return. */
9178 if (insn)
9180 rtx_insn *seq = get_insns ();
9181 end_sequence ();
9182 emit_insn (seq);
9183 return target;
9186 /* Otherwise discard the sequence and fall back to code with
9187 branches. */
9188 end_sequence ();
9191 if (target != op0)
9192 emit_move_insn (target, op0);
9194 lab = gen_label_rtx ();
9195 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9196 unsignedp, mode, NULL_RTX, NULL, lab,
9197 profile_probability::uninitialized ());
9199 emit_move_insn (target, op1);
9200 emit_label (lab);
9201 return target;
9203 case BIT_NOT_EXPR:
9204 op0 = expand_expr (treeop0, subtarget,
9205 VOIDmode, EXPAND_NORMAL);
9206 if (modifier == EXPAND_STACK_PARM)
9207 target = 0;
9208 /* In case we have to reduce the result to bitfield precision
9209 for unsigned bitfield expand this as XOR with a proper constant
9210 instead. */
9211 if (reduce_bit_field && TYPE_UNSIGNED (type))
9213 int_mode = SCALAR_INT_TYPE_MODE (type);
9214 wide_int mask = wi::mask (TYPE_PRECISION (type),
9215 false, GET_MODE_PRECISION (int_mode));
9217 temp = expand_binop (int_mode, xor_optab, op0,
9218 immed_wide_int_const (mask, int_mode),
9219 target, 1, OPTAB_LIB_WIDEN);
9221 else
9222 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9223 gcc_assert (temp);
9224 return temp;
9226 /* ??? Can optimize bitwise operations with one arg constant.
9227 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9228 and (a bitwise1 b) bitwise2 b (etc)
9229 but that is probably not worth while. */
9231 case BIT_AND_EXPR:
9232 case BIT_IOR_EXPR:
9233 case BIT_XOR_EXPR:
9234 goto binop;
9236 case LROTATE_EXPR:
9237 case RROTATE_EXPR:
9238 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9239 || type_has_mode_precision_p (type));
9240 /* fall through */
9242 case LSHIFT_EXPR:
9243 case RSHIFT_EXPR:
9245 /* If this is a fixed-point operation, then we cannot use the code
9246 below because "expand_shift" doesn't support sat/no-sat fixed-point
9247 shifts. */
9248 if (ALL_FIXED_POINT_MODE_P (mode))
9249 goto binop;
9251 if (! safe_from_p (subtarget, treeop1, 1))
9252 subtarget = 0;
9253 if (modifier == EXPAND_STACK_PARM)
9254 target = 0;
9255 op0 = expand_expr (treeop0, subtarget,
9256 VOIDmode, EXPAND_NORMAL);
9258 /* Left shift optimization when shifting across word_size boundary.
9260 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9261 there isn't native instruction to support this wide mode
9262 left shift. Given below scenario:
9264 Type A = (Type) B << C
9266 |< T >|
9267 | dest_high | dest_low |
9269 | word_size |
9271 If the shift amount C caused we shift B to across the word
9272 size boundary, i.e part of B shifted into high half of
9273 destination register, and part of B remains in the low
9274 half, then GCC will use the following left shift expand
9275 logic:
9277 1. Initialize dest_low to B.
9278 2. Initialize every bit of dest_high to the sign bit of B.
9279 3. Logic left shift dest_low by C bit to finalize dest_low.
9280 The value of dest_low before this shift is kept in a temp D.
9281 4. Logic left shift dest_high by C.
9282 5. Logic right shift D by (word_size - C).
9283 6. Or the result of 4 and 5 to finalize dest_high.
9285 While, by checking gimple statements, if operand B is
9286 coming from signed extension, then we can simplify above
9287 expand logic into:
9289 1. dest_high = src_low >> (word_size - C).
9290 2. dest_low = src_low << C.
9292 We can use one arithmetic right shift to finish all the
9293 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9294 needed from 6 into 2.
9296 The case is similar for zero extension, except that we
9297 initialize dest_high to zero rather than copies of the sign
9298 bit from B. Furthermore, we need to use a logical right shift
9299 in this case.
9301 The choice of sign-extension versus zero-extension is
9302 determined entirely by whether or not B is signed and is
9303 independent of the current setting of unsignedp. */
9305 temp = NULL_RTX;
9306 if (code == LSHIFT_EXPR
9307 && target
9308 && REG_P (target)
9309 && GET_MODE_2XWIDER_MODE (word_mode).exists (&int_mode)
9310 && mode == int_mode
9311 && TREE_CONSTANT (treeop1)
9312 && TREE_CODE (treeop0) == SSA_NAME)
9314 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9315 if (is_gimple_assign (def)
9316 && gimple_assign_rhs_code (def) == NOP_EXPR)
9318 scalar_int_mode rmode = SCALAR_INT_TYPE_MODE
9319 (TREE_TYPE (gimple_assign_rhs1 (def)));
9321 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (int_mode)
9322 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9323 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9324 >= GET_MODE_BITSIZE (word_mode)))
9326 rtx_insn *seq, *seq_old;
9327 poly_uint64 high_off = subreg_highpart_offset (word_mode,
9328 int_mode);
9329 bool extend_unsigned
9330 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9331 rtx low = lowpart_subreg (word_mode, op0, int_mode);
9332 rtx dest_low = lowpart_subreg (word_mode, target, int_mode);
9333 rtx dest_high = simplify_gen_subreg (word_mode, target,
9334 int_mode, high_off);
9335 HOST_WIDE_INT ramount = (BITS_PER_WORD
9336 - TREE_INT_CST_LOW (treeop1));
9337 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9339 start_sequence ();
9340 /* dest_high = src_low >> (word_size - C). */
9341 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9342 rshift, dest_high,
9343 extend_unsigned);
9344 if (temp != dest_high)
9345 emit_move_insn (dest_high, temp);
9347 /* dest_low = src_low << C. */
9348 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9349 treeop1, dest_low, unsignedp);
9350 if (temp != dest_low)
9351 emit_move_insn (dest_low, temp);
9353 seq = get_insns ();
9354 end_sequence ();
9355 temp = target ;
9357 if (have_insn_for (ASHIFT, int_mode))
9359 bool speed_p = optimize_insn_for_speed_p ();
9360 start_sequence ();
9361 rtx ret_old = expand_variable_shift (code, int_mode,
9362 op0, treeop1,
9363 target,
9364 unsignedp);
9366 seq_old = get_insns ();
9367 end_sequence ();
9368 if (seq_cost (seq, speed_p)
9369 >= seq_cost (seq_old, speed_p))
9371 seq = seq_old;
9372 temp = ret_old;
9375 emit_insn (seq);
9380 if (temp == NULL_RTX)
9381 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9382 unsignedp);
9383 if (code == LSHIFT_EXPR)
9384 temp = REDUCE_BIT_FIELD (temp);
9385 return temp;
9388 /* Could determine the answer when only additive constants differ. Also,
9389 the addition of one can be handled by changing the condition. */
9390 case LT_EXPR:
9391 case LE_EXPR:
9392 case GT_EXPR:
9393 case GE_EXPR:
9394 case EQ_EXPR:
9395 case NE_EXPR:
9396 case UNORDERED_EXPR:
9397 case ORDERED_EXPR:
9398 case UNLT_EXPR:
9399 case UNLE_EXPR:
9400 case UNGT_EXPR:
9401 case UNGE_EXPR:
9402 case UNEQ_EXPR:
9403 case LTGT_EXPR:
9405 temp = do_store_flag (ops,
9406 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9407 tmode != VOIDmode ? tmode : mode);
9408 if (temp)
9409 return temp;
9411 /* Use a compare and a jump for BLKmode comparisons, or for function
9412 type comparisons is have_canonicalize_funcptr_for_compare. */
9414 if ((target == 0
9415 || modifier == EXPAND_STACK_PARM
9416 || ! safe_from_p (target, treeop0, 1)
9417 || ! safe_from_p (target, treeop1, 1)
9418 /* Make sure we don't have a hard reg (such as function's return
9419 value) live across basic blocks, if not optimizing. */
9420 || (!optimize && REG_P (target)
9421 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9422 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9424 emit_move_insn (target, const0_rtx);
9426 rtx_code_label *lab1 = gen_label_rtx ();
9427 jumpifnot_1 (code, treeop0, treeop1, lab1,
9428 profile_probability::uninitialized ());
9430 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9431 emit_move_insn (target, constm1_rtx);
9432 else
9433 emit_move_insn (target, const1_rtx);
9435 emit_label (lab1);
9436 return target;
9438 case COMPLEX_EXPR:
9439 /* Get the rtx code of the operands. */
9440 op0 = expand_normal (treeop0);
9441 op1 = expand_normal (treeop1);
9443 if (!target)
9444 target = gen_reg_rtx (TYPE_MODE (type));
9445 else
9446 /* If target overlaps with op1, then either we need to force
9447 op1 into a pseudo (if target also overlaps with op0),
9448 or write the complex parts in reverse order. */
9449 switch (GET_CODE (target))
9451 case CONCAT:
9452 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9454 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9456 complex_expr_force_op1:
9457 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9458 emit_move_insn (temp, op1);
9459 op1 = temp;
9460 break;
9462 complex_expr_swap_order:
9463 /* Move the imaginary (op1) and real (op0) parts to their
9464 location. */
9465 write_complex_part (target, op1, true);
9466 write_complex_part (target, op0, false);
9468 return target;
9470 break;
9471 case MEM:
9472 temp = adjust_address_nv (target,
9473 GET_MODE_INNER (GET_MODE (target)), 0);
9474 if (reg_overlap_mentioned_p (temp, op1))
9476 scalar_mode imode = GET_MODE_INNER (GET_MODE (target));
9477 temp = adjust_address_nv (target, imode,
9478 GET_MODE_SIZE (imode));
9479 if (reg_overlap_mentioned_p (temp, op0))
9480 goto complex_expr_force_op1;
9481 goto complex_expr_swap_order;
9483 break;
9484 default:
9485 if (reg_overlap_mentioned_p (target, op1))
9487 if (reg_overlap_mentioned_p (target, op0))
9488 goto complex_expr_force_op1;
9489 goto complex_expr_swap_order;
9491 break;
9494 /* Move the real (op0) and imaginary (op1) parts to their location. */
9495 write_complex_part (target, op0, false);
9496 write_complex_part (target, op1, true);
9498 return target;
9500 case WIDEN_SUM_EXPR:
9502 tree oprnd0 = treeop0;
9503 tree oprnd1 = treeop1;
9505 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9506 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9507 target, unsignedp);
9508 return target;
9511 case VEC_UNPACK_HI_EXPR:
9512 case VEC_UNPACK_LO_EXPR:
9514 op0 = expand_normal (treeop0);
9515 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9516 target, unsignedp);
9517 gcc_assert (temp);
9518 return temp;
9521 case VEC_UNPACK_FLOAT_HI_EXPR:
9522 case VEC_UNPACK_FLOAT_LO_EXPR:
9524 op0 = expand_normal (treeop0);
9525 /* The signedness is determined from input operand. */
9526 temp = expand_widen_pattern_expr
9527 (ops, op0, NULL_RTX, NULL_RTX,
9528 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9530 gcc_assert (temp);
9531 return temp;
9534 case VEC_WIDEN_MULT_HI_EXPR:
9535 case VEC_WIDEN_MULT_LO_EXPR:
9536 case VEC_WIDEN_MULT_EVEN_EXPR:
9537 case VEC_WIDEN_MULT_ODD_EXPR:
9538 case VEC_WIDEN_LSHIFT_HI_EXPR:
9539 case VEC_WIDEN_LSHIFT_LO_EXPR:
9540 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9541 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9542 target, unsignedp);
9543 gcc_assert (target);
9544 return target;
9546 case VEC_PACK_TRUNC_EXPR:
9547 case VEC_PACK_SAT_EXPR:
9548 case VEC_PACK_FIX_TRUNC_EXPR:
9549 mode = TYPE_MODE (TREE_TYPE (treeop0));
9550 goto binop;
9552 case VEC_PERM_EXPR:
9554 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9555 vec_perm_builder sel;
9556 if (TREE_CODE (treeop2) == VECTOR_CST
9557 && tree_to_vec_perm_builder (&sel, treeop2))
9559 machine_mode sel_mode = TYPE_MODE (TREE_TYPE (treeop2));
9560 temp = expand_vec_perm_const (mode, op0, op1, sel,
9561 sel_mode, target);
9563 else
9565 op2 = expand_normal (treeop2);
9566 temp = expand_vec_perm_var (mode, op0, op1, op2, target);
9568 gcc_assert (temp);
9569 return temp;
9572 case DOT_PROD_EXPR:
9574 tree oprnd0 = treeop0;
9575 tree oprnd1 = treeop1;
9576 tree oprnd2 = treeop2;
9577 rtx op2;
9579 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9580 op2 = expand_normal (oprnd2);
9581 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9582 target, unsignedp);
9583 return target;
9586 case SAD_EXPR:
9588 tree oprnd0 = treeop0;
9589 tree oprnd1 = treeop1;
9590 tree oprnd2 = treeop2;
9591 rtx op2;
9593 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9594 op2 = expand_normal (oprnd2);
9595 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9596 target, unsignedp);
9597 return target;
9600 case REALIGN_LOAD_EXPR:
9602 tree oprnd0 = treeop0;
9603 tree oprnd1 = treeop1;
9604 tree oprnd2 = treeop2;
9605 rtx op2;
9607 this_optab = optab_for_tree_code (code, type, optab_default);
9608 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9609 op2 = expand_normal (oprnd2);
9610 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9611 target, unsignedp);
9612 gcc_assert (temp);
9613 return temp;
9616 case COND_EXPR:
9618 /* A COND_EXPR with its type being VOID_TYPE represents a
9619 conditional jump and is handled in
9620 expand_gimple_cond_expr. */
9621 gcc_assert (!VOID_TYPE_P (type));
9623 /* Note that COND_EXPRs whose type is a structure or union
9624 are required to be constructed to contain assignments of
9625 a temporary variable, so that we can evaluate them here
9626 for side effect only. If type is void, we must do likewise. */
9628 gcc_assert (!TREE_ADDRESSABLE (type)
9629 && !ignore
9630 && TREE_TYPE (treeop1) != void_type_node
9631 && TREE_TYPE (treeop2) != void_type_node);
9633 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9634 if (temp)
9635 return temp;
9637 /* If we are not to produce a result, we have no target. Otherwise,
9638 if a target was specified use it; it will not be used as an
9639 intermediate target unless it is safe. If no target, use a
9640 temporary. */
9642 if (modifier != EXPAND_STACK_PARM
9643 && original_target
9644 && safe_from_p (original_target, treeop0, 1)
9645 && GET_MODE (original_target) == mode
9646 && !MEM_P (original_target))
9647 temp = original_target;
9648 else
9649 temp = assign_temp (type, 0, 1);
9651 do_pending_stack_adjust ();
9652 NO_DEFER_POP;
9653 rtx_code_label *lab0 = gen_label_rtx ();
9654 rtx_code_label *lab1 = gen_label_rtx ();
9655 jumpifnot (treeop0, lab0,
9656 profile_probability::uninitialized ());
9657 store_expr (treeop1, temp,
9658 modifier == EXPAND_STACK_PARM,
9659 false, false);
9661 emit_jump_insn (targetm.gen_jump (lab1));
9662 emit_barrier ();
9663 emit_label (lab0);
9664 store_expr (treeop2, temp,
9665 modifier == EXPAND_STACK_PARM,
9666 false, false);
9668 emit_label (lab1);
9669 OK_DEFER_POP;
9670 return temp;
9673 case VEC_COND_EXPR:
9674 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9675 return target;
9677 case VEC_DUPLICATE_EXPR:
9678 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
9679 target = expand_vector_broadcast (mode, op0);
9680 gcc_assert (target);
9681 return target;
9683 case VEC_SERIES_EXPR:
9684 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, modifier);
9685 return expand_vec_series_expr (mode, op0, op1, target);
9687 case BIT_INSERT_EXPR:
9689 unsigned bitpos = tree_to_uhwi (treeop2);
9690 unsigned bitsize;
9691 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9692 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9693 else
9694 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9695 rtx op0 = expand_normal (treeop0);
9696 rtx op1 = expand_normal (treeop1);
9697 rtx dst = gen_reg_rtx (mode);
9698 emit_move_insn (dst, op0);
9699 store_bit_field (dst, bitsize, bitpos, 0, 0,
9700 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9701 return dst;
9704 default:
9705 gcc_unreachable ();
9708 /* Here to do an ordinary binary operator. */
9709 binop:
9710 expand_operands (treeop0, treeop1,
9711 subtarget, &op0, &op1, EXPAND_NORMAL);
9712 binop2:
9713 this_optab = optab_for_tree_code (code, type, optab_default);
9714 binop3:
9715 if (modifier == EXPAND_STACK_PARM)
9716 target = 0;
9717 temp = expand_binop (mode, this_optab, op0, op1, target,
9718 unsignedp, OPTAB_LIB_WIDEN);
9719 gcc_assert (temp);
9720 /* Bitwise operations do not need bitfield reduction as we expect their
9721 operands being properly truncated. */
9722 if (code == BIT_XOR_EXPR
9723 || code == BIT_AND_EXPR
9724 || code == BIT_IOR_EXPR)
9725 return temp;
9726 return REDUCE_BIT_FIELD (temp);
9728 #undef REDUCE_BIT_FIELD
9731 /* Return TRUE if expression STMT is suitable for replacement.
9732 Never consider memory loads as replaceable, because those don't ever lead
9733 into constant expressions. */
9735 static bool
9736 stmt_is_replaceable_p (gimple *stmt)
9738 if (ssa_is_replaceable_p (stmt))
9740 /* Don't move around loads. */
9741 if (!gimple_assign_single_p (stmt)
9742 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9743 return true;
9745 return false;
9749 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9750 enum expand_modifier modifier, rtx *alt_rtl,
9751 bool inner_reference_p)
9753 rtx op0, op1, temp, decl_rtl;
9754 tree type;
9755 int unsignedp;
9756 machine_mode mode, dmode;
9757 enum tree_code code = TREE_CODE (exp);
9758 rtx subtarget, original_target;
9759 int ignore;
9760 tree context;
9761 bool reduce_bit_field;
9762 location_t loc = EXPR_LOCATION (exp);
9763 struct separate_ops ops;
9764 tree treeop0, treeop1, treeop2;
9765 tree ssa_name = NULL_TREE;
9766 gimple *g;
9768 type = TREE_TYPE (exp);
9769 mode = TYPE_MODE (type);
9770 unsignedp = TYPE_UNSIGNED (type);
9772 treeop0 = treeop1 = treeop2 = NULL_TREE;
9773 if (!VL_EXP_CLASS_P (exp))
9774 switch (TREE_CODE_LENGTH (code))
9776 default:
9777 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9778 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9779 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9780 case 0: break;
9782 ops.code = code;
9783 ops.type = type;
9784 ops.op0 = treeop0;
9785 ops.op1 = treeop1;
9786 ops.op2 = treeop2;
9787 ops.location = loc;
9789 ignore = (target == const0_rtx
9790 || ((CONVERT_EXPR_CODE_P (code)
9791 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9792 && TREE_CODE (type) == VOID_TYPE));
9794 /* An operation in what may be a bit-field type needs the
9795 result to be reduced to the precision of the bit-field type,
9796 which is narrower than that of the type's mode. */
9797 reduce_bit_field = (!ignore
9798 && INTEGRAL_TYPE_P (type)
9799 && !type_has_mode_precision_p (type));
9801 /* If we are going to ignore this result, we need only do something
9802 if there is a side-effect somewhere in the expression. If there
9803 is, short-circuit the most common cases here. Note that we must
9804 not call expand_expr with anything but const0_rtx in case this
9805 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9807 if (ignore)
9809 if (! TREE_SIDE_EFFECTS (exp))
9810 return const0_rtx;
9812 /* Ensure we reference a volatile object even if value is ignored, but
9813 don't do this if all we are doing is taking its address. */
9814 if (TREE_THIS_VOLATILE (exp)
9815 && TREE_CODE (exp) != FUNCTION_DECL
9816 && mode != VOIDmode && mode != BLKmode
9817 && modifier != EXPAND_CONST_ADDRESS)
9819 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9820 if (MEM_P (temp))
9821 copy_to_reg (temp);
9822 return const0_rtx;
9825 if (TREE_CODE_CLASS (code) == tcc_unary
9826 || code == BIT_FIELD_REF
9827 || code == COMPONENT_REF
9828 || code == INDIRECT_REF)
9829 return expand_expr (treeop0, const0_rtx, VOIDmode,
9830 modifier);
9832 else if (TREE_CODE_CLASS (code) == tcc_binary
9833 || TREE_CODE_CLASS (code) == tcc_comparison
9834 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9836 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9837 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9838 return const0_rtx;
9841 target = 0;
9844 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9845 target = 0;
9847 /* Use subtarget as the target for operand 0 of a binary operation. */
9848 subtarget = get_subtarget (target);
9849 original_target = target;
9851 switch (code)
9853 case LABEL_DECL:
9855 tree function = decl_function_context (exp);
9857 temp = label_rtx (exp);
9858 temp = gen_rtx_LABEL_REF (Pmode, temp);
9860 if (function != current_function_decl
9861 && function != 0)
9862 LABEL_REF_NONLOCAL_P (temp) = 1;
9864 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9865 return temp;
9868 case SSA_NAME:
9869 /* ??? ivopts calls expander, without any preparation from
9870 out-of-ssa. So fake instructions as if this was an access to the
9871 base variable. This unnecessarily allocates a pseudo, see how we can
9872 reuse it, if partition base vars have it set already. */
9873 if (!currently_expanding_to_rtl)
9875 tree var = SSA_NAME_VAR (exp);
9876 if (var && DECL_RTL_SET_P (var))
9877 return DECL_RTL (var);
9878 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9879 LAST_VIRTUAL_REGISTER + 1);
9882 g = get_gimple_for_ssa_name (exp);
9883 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9884 if (g == NULL
9885 && modifier == EXPAND_INITIALIZER
9886 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9887 && (optimize || !SSA_NAME_VAR (exp)
9888 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9889 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9890 g = SSA_NAME_DEF_STMT (exp);
9891 if (g)
9893 rtx r;
9894 location_t saved_loc = curr_insn_location ();
9895 location_t loc = gimple_location (g);
9896 if (loc != UNKNOWN_LOCATION)
9897 set_curr_insn_location (loc);
9898 ops.code = gimple_assign_rhs_code (g);
9899 switch (get_gimple_rhs_class (ops.code))
9901 case GIMPLE_TERNARY_RHS:
9902 ops.op2 = gimple_assign_rhs3 (g);
9903 /* Fallthru */
9904 case GIMPLE_BINARY_RHS:
9905 ops.op1 = gimple_assign_rhs2 (g);
9907 /* Try to expand conditonal compare. */
9908 if (targetm.gen_ccmp_first)
9910 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9911 r = expand_ccmp_expr (g, mode);
9912 if (r)
9913 break;
9915 /* Fallthru */
9916 case GIMPLE_UNARY_RHS:
9917 ops.op0 = gimple_assign_rhs1 (g);
9918 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9919 ops.location = loc;
9920 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9921 break;
9922 case GIMPLE_SINGLE_RHS:
9924 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9925 tmode, modifier, alt_rtl,
9926 inner_reference_p);
9927 break;
9929 default:
9930 gcc_unreachable ();
9932 set_curr_insn_location (saved_loc);
9933 if (REG_P (r) && !REG_EXPR (r))
9934 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9935 return r;
9938 ssa_name = exp;
9939 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9940 exp = SSA_NAME_VAR (ssa_name);
9941 goto expand_decl_rtl;
9943 case PARM_DECL:
9944 case VAR_DECL:
9945 /* If a static var's type was incomplete when the decl was written,
9946 but the type is complete now, lay out the decl now. */
9947 if (DECL_SIZE (exp) == 0
9948 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9949 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9950 layout_decl (exp, 0);
9952 /* fall through */
9954 case FUNCTION_DECL:
9955 case RESULT_DECL:
9956 decl_rtl = DECL_RTL (exp);
9957 expand_decl_rtl:
9958 gcc_assert (decl_rtl);
9960 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9961 settings for VECTOR_TYPE_P that might switch for the function. */
9962 if (currently_expanding_to_rtl
9963 && code == VAR_DECL && MEM_P (decl_rtl)
9964 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9965 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9966 else
9967 decl_rtl = copy_rtx (decl_rtl);
9969 /* Record writes to register variables. */
9970 if (modifier == EXPAND_WRITE
9971 && REG_P (decl_rtl)
9972 && HARD_REGISTER_P (decl_rtl))
9973 add_to_hard_reg_set (&crtl->asm_clobbers,
9974 GET_MODE (decl_rtl), REGNO (decl_rtl));
9976 /* Ensure variable marked as used even if it doesn't go through
9977 a parser. If it hasn't be used yet, write out an external
9978 definition. */
9979 if (exp)
9980 TREE_USED (exp) = 1;
9982 /* Show we haven't gotten RTL for this yet. */
9983 temp = 0;
9985 /* Variables inherited from containing functions should have
9986 been lowered by this point. */
9987 if (exp)
9988 context = decl_function_context (exp);
9989 gcc_assert (!exp
9990 || SCOPE_FILE_SCOPE_P (context)
9991 || context == current_function_decl
9992 || TREE_STATIC (exp)
9993 || DECL_EXTERNAL (exp)
9994 /* ??? C++ creates functions that are not TREE_STATIC. */
9995 || TREE_CODE (exp) == FUNCTION_DECL);
9997 /* This is the case of an array whose size is to be determined
9998 from its initializer, while the initializer is still being parsed.
9999 ??? We aren't parsing while expanding anymore. */
10001 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
10002 temp = validize_mem (decl_rtl);
10004 /* If DECL_RTL is memory, we are in the normal case and the
10005 address is not valid, get the address into a register. */
10007 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
10009 if (alt_rtl)
10010 *alt_rtl = decl_rtl;
10011 decl_rtl = use_anchored_address (decl_rtl);
10012 if (modifier != EXPAND_CONST_ADDRESS
10013 && modifier != EXPAND_SUM
10014 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
10015 : GET_MODE (decl_rtl),
10016 XEXP (decl_rtl, 0),
10017 MEM_ADDR_SPACE (decl_rtl)))
10018 temp = replace_equiv_address (decl_rtl,
10019 copy_rtx (XEXP (decl_rtl, 0)));
10022 /* If we got something, return it. But first, set the alignment
10023 if the address is a register. */
10024 if (temp != 0)
10026 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
10027 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
10029 return temp;
10032 if (exp)
10033 dmode = DECL_MODE (exp);
10034 else
10035 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
10037 /* If the mode of DECL_RTL does not match that of the decl,
10038 there are two cases: we are dealing with a BLKmode value
10039 that is returned in a register, or we are dealing with
10040 a promoted value. In the latter case, return a SUBREG
10041 of the wanted mode, but mark it so that we know that it
10042 was already extended. */
10043 if (REG_P (decl_rtl)
10044 && dmode != BLKmode
10045 && GET_MODE (decl_rtl) != dmode)
10047 machine_mode pmode;
10049 /* Get the signedness to be used for this variable. Ensure we get
10050 the same mode we got when the variable was declared. */
10051 if (code != SSA_NAME)
10052 pmode = promote_decl_mode (exp, &unsignedp);
10053 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
10054 && gimple_code (g) == GIMPLE_CALL
10055 && !gimple_call_internal_p (g))
10056 pmode = promote_function_mode (type, mode, &unsignedp,
10057 gimple_call_fntype (g),
10059 else
10060 pmode = promote_ssa_mode (ssa_name, &unsignedp);
10061 gcc_assert (GET_MODE (decl_rtl) == pmode);
10063 temp = gen_lowpart_SUBREG (mode, decl_rtl);
10064 SUBREG_PROMOTED_VAR_P (temp) = 1;
10065 SUBREG_PROMOTED_SET (temp, unsignedp);
10066 return temp;
10069 return decl_rtl;
10071 case INTEGER_CST:
10073 /* Given that TYPE_PRECISION (type) is not always equal to
10074 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
10075 the former to the latter according to the signedness of the
10076 type. */
10077 scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
10078 temp = immed_wide_int_const
10079 (wi::to_wide (exp, GET_MODE_PRECISION (mode)), mode);
10080 return temp;
10083 case VECTOR_CST:
10085 tree tmp = NULL_TREE;
10086 if (VECTOR_MODE_P (mode))
10087 return const_vector_from_tree (exp);
10088 scalar_int_mode int_mode;
10089 if (is_int_mode (mode, &int_mode))
10091 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
10092 return const_scalar_mask_from_tree (int_mode, exp);
10093 else
10095 tree type_for_mode
10096 = lang_hooks.types.type_for_mode (int_mode, 1);
10097 if (type_for_mode)
10098 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
10099 type_for_mode, exp);
10102 if (!tmp)
10104 vec<constructor_elt, va_gc> *v;
10105 /* Constructors need to be fixed-length. FIXME. */
10106 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
10107 vec_alloc (v, nunits);
10108 for (unsigned int i = 0; i < nunits; ++i)
10109 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
10110 tmp = build_constructor (type, v);
10112 return expand_expr (tmp, ignore ? const0_rtx : target,
10113 tmode, modifier);
10116 case CONST_DECL:
10117 if (modifier == EXPAND_WRITE)
10119 /* Writing into CONST_DECL is always invalid, but handle it
10120 gracefully. */
10121 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
10122 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
10123 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10124 EXPAND_NORMAL, as);
10125 op0 = memory_address_addr_space (mode, op0, as);
10126 temp = gen_rtx_MEM (mode, op0);
10127 set_mem_addr_space (temp, as);
10128 return temp;
10130 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10132 case REAL_CST:
10133 /* If optimized, generate immediate CONST_DOUBLE
10134 which will be turned into memory by reload if necessary.
10136 We used to force a register so that loop.c could see it. But
10137 this does not allow gen_* patterns to perform optimizations with
10138 the constants. It also produces two insns in cases like "x = 1.0;".
10139 On most machines, floating-point constants are not permitted in
10140 many insns, so we'd end up copying it to a register in any case.
10142 Now, we do the copying in expand_binop, if appropriate. */
10143 return const_double_from_real_value (TREE_REAL_CST (exp),
10144 TYPE_MODE (TREE_TYPE (exp)));
10146 case FIXED_CST:
10147 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10148 TYPE_MODE (TREE_TYPE (exp)));
10150 case COMPLEX_CST:
10151 /* Handle evaluating a complex constant in a CONCAT target. */
10152 if (original_target && GET_CODE (original_target) == CONCAT)
10154 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10155 rtx rtarg, itarg;
10157 rtarg = XEXP (original_target, 0);
10158 itarg = XEXP (original_target, 1);
10160 /* Move the real and imaginary parts separately. */
10161 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10162 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10164 if (op0 != rtarg)
10165 emit_move_insn (rtarg, op0);
10166 if (op1 != itarg)
10167 emit_move_insn (itarg, op1);
10169 return original_target;
10172 /* fall through */
10174 case STRING_CST:
10175 temp = expand_expr_constant (exp, 1, modifier);
10177 /* temp contains a constant address.
10178 On RISC machines where a constant address isn't valid,
10179 make some insns to get that address into a register. */
10180 if (modifier != EXPAND_CONST_ADDRESS
10181 && modifier != EXPAND_INITIALIZER
10182 && modifier != EXPAND_SUM
10183 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10184 MEM_ADDR_SPACE (temp)))
10185 return replace_equiv_address (temp,
10186 copy_rtx (XEXP (temp, 0)));
10187 return temp;
10189 case POLY_INT_CST:
10190 return immed_wide_int_const (poly_int_cst_value (exp), mode);
10192 case SAVE_EXPR:
10194 tree val = treeop0;
10195 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10196 inner_reference_p);
10198 if (!SAVE_EXPR_RESOLVED_P (exp))
10200 /* We can indeed still hit this case, typically via builtin
10201 expanders calling save_expr immediately before expanding
10202 something. Assume this means that we only have to deal
10203 with non-BLKmode values. */
10204 gcc_assert (GET_MODE (ret) != BLKmode);
10206 val = build_decl (curr_insn_location (),
10207 VAR_DECL, NULL, TREE_TYPE (exp));
10208 DECL_ARTIFICIAL (val) = 1;
10209 DECL_IGNORED_P (val) = 1;
10210 treeop0 = val;
10211 TREE_OPERAND (exp, 0) = treeop0;
10212 SAVE_EXPR_RESOLVED_P (exp) = 1;
10214 if (!CONSTANT_P (ret))
10215 ret = copy_to_reg (ret);
10216 SET_DECL_RTL (val, ret);
10219 return ret;
10223 case CONSTRUCTOR:
10224 /* If we don't need the result, just ensure we evaluate any
10225 subexpressions. */
10226 if (ignore)
10228 unsigned HOST_WIDE_INT idx;
10229 tree value;
10231 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10232 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10234 return const0_rtx;
10237 return expand_constructor (exp, target, modifier, false);
10239 case TARGET_MEM_REF:
10241 addr_space_t as
10242 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10243 enum insn_code icode;
10244 unsigned int align;
10246 op0 = addr_for_mem_ref (exp, as, true);
10247 op0 = memory_address_addr_space (mode, op0, as);
10248 temp = gen_rtx_MEM (mode, op0);
10249 set_mem_attributes (temp, exp, 0);
10250 set_mem_addr_space (temp, as);
10251 align = get_object_alignment (exp);
10252 if (modifier != EXPAND_WRITE
10253 && modifier != EXPAND_MEMORY
10254 && mode != BLKmode
10255 && align < GET_MODE_ALIGNMENT (mode)
10256 /* If the target does not have special handling for unaligned
10257 loads of mode then it can use regular moves for them. */
10258 && ((icode = optab_handler (movmisalign_optab, mode))
10259 != CODE_FOR_nothing))
10261 struct expand_operand ops[2];
10263 /* We've already validated the memory, and we're creating a
10264 new pseudo destination. The predicates really can't fail,
10265 nor can the generator. */
10266 create_output_operand (&ops[0], NULL_RTX, mode);
10267 create_fixed_operand (&ops[1], temp);
10268 expand_insn (icode, 2, ops);
10269 temp = ops[0].value;
10271 return temp;
10274 case MEM_REF:
10276 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10277 addr_space_t as
10278 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10279 machine_mode address_mode;
10280 tree base = TREE_OPERAND (exp, 0);
10281 gimple *def_stmt;
10282 enum insn_code icode;
10283 unsigned align;
10284 /* Handle expansion of non-aliased memory with non-BLKmode. That
10285 might end up in a register. */
10286 if (mem_ref_refers_to_non_mem_p (exp))
10288 poly_int64 offset = mem_ref_offset (exp).force_shwi ();
10289 base = TREE_OPERAND (base, 0);
10290 if (known_eq (offset, 0)
10291 && !reverse
10292 && tree_fits_uhwi_p (TYPE_SIZE (type))
10293 && known_eq (GET_MODE_BITSIZE (DECL_MODE (base)),
10294 tree_to_uhwi (TYPE_SIZE (type))))
10295 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10296 target, tmode, modifier);
10297 if (TYPE_MODE (type) == BLKmode)
10299 temp = assign_stack_temp (DECL_MODE (base),
10300 GET_MODE_SIZE (DECL_MODE (base)));
10301 store_expr (base, temp, 0, false, false);
10302 temp = adjust_address (temp, BLKmode, offset);
10303 set_mem_size (temp, int_size_in_bytes (type));
10304 return temp;
10306 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10307 bitsize_int (offset * BITS_PER_UNIT));
10308 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10309 return expand_expr (exp, target, tmode, modifier);
10311 address_mode = targetm.addr_space.address_mode (as);
10312 base = TREE_OPERAND (exp, 0);
10313 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10315 tree mask = gimple_assign_rhs2 (def_stmt);
10316 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10317 gimple_assign_rhs1 (def_stmt), mask);
10318 TREE_OPERAND (exp, 0) = base;
10320 align = get_object_alignment (exp);
10321 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10322 op0 = memory_address_addr_space (mode, op0, as);
10323 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10325 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10326 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10327 op0 = memory_address_addr_space (mode, op0, as);
10329 temp = gen_rtx_MEM (mode, op0);
10330 set_mem_attributes (temp, exp, 0);
10331 set_mem_addr_space (temp, as);
10332 if (TREE_THIS_VOLATILE (exp))
10333 MEM_VOLATILE_P (temp) = 1;
10334 if (modifier != EXPAND_WRITE
10335 && modifier != EXPAND_MEMORY
10336 && !inner_reference_p
10337 && mode != BLKmode
10338 && align < GET_MODE_ALIGNMENT (mode))
10340 if ((icode = optab_handler (movmisalign_optab, mode))
10341 != CODE_FOR_nothing)
10343 struct expand_operand ops[2];
10345 /* We've already validated the memory, and we're creating a
10346 new pseudo destination. The predicates really can't fail,
10347 nor can the generator. */
10348 create_output_operand (&ops[0], NULL_RTX, mode);
10349 create_fixed_operand (&ops[1], temp);
10350 expand_insn (icode, 2, ops);
10351 temp = ops[0].value;
10353 else if (targetm.slow_unaligned_access (mode, align))
10354 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10355 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10356 (modifier == EXPAND_STACK_PARM
10357 ? NULL_RTX : target),
10358 mode, mode, false, alt_rtl);
10360 if (reverse
10361 && modifier != EXPAND_MEMORY
10362 && modifier != EXPAND_WRITE)
10363 temp = flip_storage_order (mode, temp);
10364 return temp;
10367 case ARRAY_REF:
10370 tree array = treeop0;
10371 tree index = treeop1;
10372 tree init;
10374 /* Fold an expression like: "foo"[2].
10375 This is not done in fold so it won't happen inside &.
10376 Don't fold if this is for wide characters since it's too
10377 difficult to do correctly and this is a very rare case. */
10379 if (modifier != EXPAND_CONST_ADDRESS
10380 && modifier != EXPAND_INITIALIZER
10381 && modifier != EXPAND_MEMORY)
10383 tree t = fold_read_from_constant_string (exp);
10385 if (t)
10386 return expand_expr (t, target, tmode, modifier);
10389 /* If this is a constant index into a constant array,
10390 just get the value from the array. Handle both the cases when
10391 we have an explicit constructor and when our operand is a variable
10392 that was declared const. */
10394 if (modifier != EXPAND_CONST_ADDRESS
10395 && modifier != EXPAND_INITIALIZER
10396 && modifier != EXPAND_MEMORY
10397 && TREE_CODE (array) == CONSTRUCTOR
10398 && ! TREE_SIDE_EFFECTS (array)
10399 && TREE_CODE (index) == INTEGER_CST)
10401 unsigned HOST_WIDE_INT ix;
10402 tree field, value;
10404 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10405 field, value)
10406 if (tree_int_cst_equal (field, index))
10408 if (!TREE_SIDE_EFFECTS (value))
10409 return expand_expr (fold (value), target, tmode, modifier);
10410 break;
10414 else if (optimize >= 1
10415 && modifier != EXPAND_CONST_ADDRESS
10416 && modifier != EXPAND_INITIALIZER
10417 && modifier != EXPAND_MEMORY
10418 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10419 && TREE_CODE (index) == INTEGER_CST
10420 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10421 && (init = ctor_for_folding (array)) != error_mark_node)
10423 if (init == NULL_TREE)
10425 tree value = build_zero_cst (type);
10426 if (TREE_CODE (value) == CONSTRUCTOR)
10428 /* If VALUE is a CONSTRUCTOR, this optimization is only
10429 useful if this doesn't store the CONSTRUCTOR into
10430 memory. If it does, it is more efficient to just
10431 load the data from the array directly. */
10432 rtx ret = expand_constructor (value, target,
10433 modifier, true);
10434 if (ret == NULL_RTX)
10435 value = NULL_TREE;
10438 if (value)
10439 return expand_expr (value, target, tmode, modifier);
10441 else if (TREE_CODE (init) == CONSTRUCTOR)
10443 unsigned HOST_WIDE_INT ix;
10444 tree field, value;
10446 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10447 field, value)
10448 if (tree_int_cst_equal (field, index))
10450 if (TREE_SIDE_EFFECTS (value))
10451 break;
10453 if (TREE_CODE (value) == CONSTRUCTOR)
10455 /* If VALUE is a CONSTRUCTOR, this
10456 optimization is only useful if
10457 this doesn't store the CONSTRUCTOR
10458 into memory. If it does, it is more
10459 efficient to just load the data from
10460 the array directly. */
10461 rtx ret = expand_constructor (value, target,
10462 modifier, true);
10463 if (ret == NULL_RTX)
10464 break;
10467 return
10468 expand_expr (fold (value), target, tmode, modifier);
10471 else if (TREE_CODE (init) == STRING_CST)
10473 tree low_bound = array_ref_low_bound (exp);
10474 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10476 /* Optimize the special case of a zero lower bound.
10478 We convert the lower bound to sizetype to avoid problems
10479 with constant folding. E.g. suppose the lower bound is
10480 1 and its mode is QI. Without the conversion
10481 (ARRAY + (INDEX - (unsigned char)1))
10482 becomes
10483 (ARRAY + (-(unsigned char)1) + INDEX)
10484 which becomes
10485 (ARRAY + 255 + INDEX). Oops! */
10486 if (!integer_zerop (low_bound))
10487 index1 = size_diffop_loc (loc, index1,
10488 fold_convert_loc (loc, sizetype,
10489 low_bound));
10491 if (tree_fits_uhwi_p (index1)
10492 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10494 tree type = TREE_TYPE (TREE_TYPE (init));
10495 scalar_int_mode mode;
10497 if (is_int_mode (TYPE_MODE (type), &mode)
10498 && GET_MODE_SIZE (mode) == 1)
10499 return gen_int_mode (TREE_STRING_POINTER (init)
10500 [TREE_INT_CST_LOW (index1)],
10501 mode);
10506 goto normal_inner_ref;
10508 case COMPONENT_REF:
10509 /* If the operand is a CONSTRUCTOR, we can just extract the
10510 appropriate field if it is present. */
10511 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10513 unsigned HOST_WIDE_INT idx;
10514 tree field, value;
10515 scalar_int_mode field_mode;
10517 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10518 idx, field, value)
10519 if (field == treeop1
10520 /* We can normally use the value of the field in the
10521 CONSTRUCTOR. However, if this is a bitfield in
10522 an integral mode that we can fit in a HOST_WIDE_INT,
10523 we must mask only the number of bits in the bitfield,
10524 since this is done implicitly by the constructor. If
10525 the bitfield does not meet either of those conditions,
10526 we can't do this optimization. */
10527 && (! DECL_BIT_FIELD (field)
10528 || (is_int_mode (DECL_MODE (field), &field_mode)
10529 && (GET_MODE_PRECISION (field_mode)
10530 <= HOST_BITS_PER_WIDE_INT))))
10532 if (DECL_BIT_FIELD (field)
10533 && modifier == EXPAND_STACK_PARM)
10534 target = 0;
10535 op0 = expand_expr (value, target, tmode, modifier);
10536 if (DECL_BIT_FIELD (field))
10538 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10539 scalar_int_mode imode
10540 = SCALAR_INT_TYPE_MODE (TREE_TYPE (field));
10542 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10544 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10545 imode);
10546 op0 = expand_and (imode, op0, op1, target);
10548 else
10550 int count = GET_MODE_PRECISION (imode) - bitsize;
10552 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10553 target, 0);
10554 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10555 target, 0);
10559 return op0;
10562 goto normal_inner_ref;
10564 case BIT_FIELD_REF:
10565 case ARRAY_RANGE_REF:
10566 normal_inner_ref:
10568 machine_mode mode1, mode2;
10569 poly_int64 bitsize, bitpos, bytepos;
10570 tree offset;
10571 int reversep, volatilep = 0, must_force_mem;
10572 tree tem
10573 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10574 &unsignedp, &reversep, &volatilep);
10575 rtx orig_op0, memloc;
10576 bool clear_mem_expr = false;
10578 /* If we got back the original object, something is wrong. Perhaps
10579 we are evaluating an expression too early. In any event, don't
10580 infinitely recurse. */
10581 gcc_assert (tem != exp);
10583 /* If TEM's type is a union of variable size, pass TARGET to the inner
10584 computation, since it will need a temporary and TARGET is known
10585 to have to do. This occurs in unchecked conversion in Ada. */
10586 orig_op0 = op0
10587 = expand_expr_real (tem,
10588 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10589 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10590 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10591 != INTEGER_CST)
10592 && modifier != EXPAND_STACK_PARM
10593 ? target : NULL_RTX),
10594 VOIDmode,
10595 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10596 NULL, true);
10598 /* If the field has a mode, we want to access it in the
10599 field's mode, not the computed mode.
10600 If a MEM has VOIDmode (external with incomplete type),
10601 use BLKmode for it instead. */
10602 if (MEM_P (op0))
10604 if (mode1 != VOIDmode)
10605 op0 = adjust_address (op0, mode1, 0);
10606 else if (GET_MODE (op0) == VOIDmode)
10607 op0 = adjust_address (op0, BLKmode, 0);
10610 mode2
10611 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10613 /* If we have either an offset, a BLKmode result, or a reference
10614 outside the underlying object, we must force it to memory.
10615 Such a case can occur in Ada if we have unchecked conversion
10616 of an expression from a scalar type to an aggregate type or
10617 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10618 passed a partially uninitialized object or a view-conversion
10619 to a larger size. */
10620 must_force_mem = (offset
10621 || mode1 == BLKmode
10622 || maybe_gt (bitpos + bitsize,
10623 GET_MODE_BITSIZE (mode2)));
10625 /* Handle CONCAT first. */
10626 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10628 if (known_eq (bitpos, 0)
10629 && known_eq (bitsize, GET_MODE_BITSIZE (GET_MODE (op0)))
10630 && COMPLEX_MODE_P (mode1)
10631 && COMPLEX_MODE_P (GET_MODE (op0))
10632 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10633 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10635 if (reversep)
10636 op0 = flip_storage_order (GET_MODE (op0), op0);
10637 if (mode1 != GET_MODE (op0))
10639 rtx parts[2];
10640 for (int i = 0; i < 2; i++)
10642 rtx op = read_complex_part (op0, i != 0);
10643 if (GET_CODE (op) == SUBREG)
10644 op = force_reg (GET_MODE (op), op);
10645 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10646 op);
10647 if (temp)
10648 op = temp;
10649 else
10651 if (!REG_P (op) && !MEM_P (op))
10652 op = force_reg (GET_MODE (op), op);
10653 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10655 parts[i] = op;
10657 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10659 return op0;
10661 if (known_eq (bitpos, 0)
10662 && known_eq (bitsize,
10663 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10664 && maybe_ne (bitsize, 0))
10666 op0 = XEXP (op0, 0);
10667 mode2 = GET_MODE (op0);
10669 else if (known_eq (bitpos,
10670 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))))
10671 && known_eq (bitsize,
10672 GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1))))
10673 && maybe_ne (bitpos, 0)
10674 && maybe_ne (bitsize, 0))
10676 op0 = XEXP (op0, 1);
10677 bitpos = 0;
10678 mode2 = GET_MODE (op0);
10680 else
10681 /* Otherwise force into memory. */
10682 must_force_mem = 1;
10685 /* If this is a constant, put it in a register if it is a legitimate
10686 constant and we don't need a memory reference. */
10687 if (CONSTANT_P (op0)
10688 && mode2 != BLKmode
10689 && targetm.legitimate_constant_p (mode2, op0)
10690 && !must_force_mem)
10691 op0 = force_reg (mode2, op0);
10693 /* Otherwise, if this is a constant, try to force it to the constant
10694 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10695 is a legitimate constant. */
10696 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10697 op0 = validize_mem (memloc);
10699 /* Otherwise, if this is a constant or the object is not in memory
10700 and need be, put it there. */
10701 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10703 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10704 emit_move_insn (memloc, op0);
10705 op0 = memloc;
10706 clear_mem_expr = true;
10709 if (offset)
10711 machine_mode address_mode;
10712 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10713 EXPAND_SUM);
10715 gcc_assert (MEM_P (op0));
10717 address_mode = get_address_mode (op0);
10718 if (GET_MODE (offset_rtx) != address_mode)
10720 /* We cannot be sure that the RTL in offset_rtx is valid outside
10721 of a memory address context, so force it into a register
10722 before attempting to convert it to the desired mode. */
10723 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10724 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10727 /* See the comment in expand_assignment for the rationale. */
10728 if (mode1 != VOIDmode
10729 && maybe_ne (bitpos, 0)
10730 && maybe_gt (bitsize, 0)
10731 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
10732 && multiple_p (bitpos, bitsize)
10733 && multiple_p (bitsize, GET_MODE_ALIGNMENT (mode1))
10734 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10736 op0 = adjust_address (op0, mode1, bytepos);
10737 bitpos = 0;
10740 op0 = offset_address (op0, offset_rtx,
10741 highest_pow2_factor (offset));
10744 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10745 record its alignment as BIGGEST_ALIGNMENT. */
10746 if (MEM_P (op0)
10747 && known_eq (bitpos, 0)
10748 && offset != 0
10749 && is_aligning_offset (offset, tem))
10750 set_mem_align (op0, BIGGEST_ALIGNMENT);
10752 /* Don't forget about volatility even if this is a bitfield. */
10753 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10755 if (op0 == orig_op0)
10756 op0 = copy_rtx (op0);
10758 MEM_VOLATILE_P (op0) = 1;
10761 /* In cases where an aligned union has an unaligned object
10762 as a field, we might be extracting a BLKmode value from
10763 an integer-mode (e.g., SImode) object. Handle this case
10764 by doing the extract into an object as wide as the field
10765 (which we know to be the width of a basic mode), then
10766 storing into memory, and changing the mode to BLKmode. */
10767 if (mode1 == VOIDmode
10768 || REG_P (op0) || GET_CODE (op0) == SUBREG
10769 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10770 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10771 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10772 && modifier != EXPAND_CONST_ADDRESS
10773 && modifier != EXPAND_INITIALIZER
10774 && modifier != EXPAND_MEMORY)
10775 /* If the bitfield is volatile and the bitsize
10776 is narrower than the access size of the bitfield,
10777 we need to extract bitfields from the access. */
10778 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10779 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10780 && mode1 != BLKmode
10781 && maybe_lt (bitsize, GET_MODE_SIZE (mode1) * BITS_PER_UNIT))
10782 /* If the field isn't aligned enough to fetch as a memref,
10783 fetch it as a bit field. */
10784 || (mode1 != BLKmode
10785 && (((MEM_P (op0)
10786 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10787 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode1))
10788 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10789 || !multiple_p (bitpos, GET_MODE_ALIGNMENT (mode)))
10790 && modifier != EXPAND_MEMORY
10791 && ((modifier == EXPAND_CONST_ADDRESS
10792 || modifier == EXPAND_INITIALIZER)
10793 ? STRICT_ALIGNMENT
10794 : targetm.slow_unaligned_access (mode1,
10795 MEM_ALIGN (op0))))
10796 || !multiple_p (bitpos, BITS_PER_UNIT)))
10797 /* If the type and the field are a constant size and the
10798 size of the type isn't the same size as the bitfield,
10799 we must use bitfield operations. */
10800 || (known_size_p (bitsize)
10801 && TYPE_SIZE (TREE_TYPE (exp))
10802 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
10803 && maybe_ne (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp))),
10804 bitsize)))
10806 machine_mode ext_mode = mode;
10808 if (ext_mode == BLKmode
10809 && ! (target != 0 && MEM_P (op0)
10810 && MEM_P (target)
10811 && multiple_p (bitpos, BITS_PER_UNIT)))
10812 ext_mode = int_mode_for_size (bitsize, 1).else_blk ();
10814 if (ext_mode == BLKmode)
10816 if (target == 0)
10817 target = assign_temp (type, 1, 1);
10819 /* ??? Unlike the similar test a few lines below, this one is
10820 very likely obsolete. */
10821 if (known_eq (bitsize, 0))
10822 return target;
10824 /* In this case, BITPOS must start at a byte boundary and
10825 TARGET, if specified, must be a MEM. */
10826 gcc_assert (MEM_P (op0)
10827 && (!target || MEM_P (target)));
10829 bytepos = exact_div (bitpos, BITS_PER_UNIT);
10830 poly_int64 bytesize = bits_to_bytes_round_up (bitsize);
10831 emit_block_move (target,
10832 adjust_address (op0, VOIDmode, bytepos),
10833 gen_int_mode (bytesize, Pmode),
10834 (modifier == EXPAND_STACK_PARM
10835 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10837 return target;
10840 /* If we have nothing to extract, the result will be 0 for targets
10841 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10842 return 0 for the sake of consistency, as reading a zero-sized
10843 bitfield is valid in Ada and the value is fully specified. */
10844 if (known_eq (bitsize, 0))
10845 return const0_rtx;
10847 op0 = validize_mem (op0);
10849 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10850 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10852 /* If the result has a record type and the extraction is done in
10853 an integral mode, then the field may be not aligned on a byte
10854 boundary; in this case, if it has reverse storage order, it
10855 needs to be extracted as a scalar field with reverse storage
10856 order and put back into memory order afterwards. */
10857 if (TREE_CODE (type) == RECORD_TYPE
10858 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10859 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10861 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10862 (modifier == EXPAND_STACK_PARM
10863 ? NULL_RTX : target),
10864 ext_mode, ext_mode, reversep, alt_rtl);
10866 /* If the result has a record type and the mode of OP0 is an
10867 integral mode then, if BITSIZE is narrower than this mode
10868 and this is for big-endian data, we must put the field
10869 into the high-order bits. And we must also put it back
10870 into memory order if it has been previously reversed. */
10871 scalar_int_mode op0_mode;
10872 if (TREE_CODE (type) == RECORD_TYPE
10873 && is_int_mode (GET_MODE (op0), &op0_mode))
10875 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10877 gcc_checking_assert (known_le (bitsize, size));
10878 if (maybe_lt (bitsize, size)
10879 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10880 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10881 size - bitsize, op0, 1);
10883 if (reversep)
10884 op0 = flip_storage_order (op0_mode, op0);
10887 /* If the result type is BLKmode, store the data into a temporary
10888 of the appropriate type, but with the mode corresponding to the
10889 mode for the data we have (op0's mode). */
10890 if (mode == BLKmode)
10892 rtx new_rtx
10893 = assign_stack_temp_for_type (ext_mode,
10894 GET_MODE_BITSIZE (ext_mode),
10895 type);
10896 emit_move_insn (new_rtx, op0);
10897 op0 = copy_rtx (new_rtx);
10898 PUT_MODE (op0, BLKmode);
10901 return op0;
10904 /* If the result is BLKmode, use that to access the object
10905 now as well. */
10906 if (mode == BLKmode)
10907 mode1 = BLKmode;
10909 /* Get a reference to just this component. */
10910 bytepos = bits_to_bytes_round_down (bitpos);
10911 if (modifier == EXPAND_CONST_ADDRESS
10912 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10913 op0 = adjust_address_nv (op0, mode1, bytepos);
10914 else
10915 op0 = adjust_address (op0, mode1, bytepos);
10917 if (op0 == orig_op0)
10918 op0 = copy_rtx (op0);
10920 /* Don't set memory attributes if the base expression is
10921 SSA_NAME that got expanded as a MEM. In that case, we should
10922 just honor its original memory attributes. */
10923 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10924 set_mem_attributes (op0, exp, 0);
10926 if (REG_P (XEXP (op0, 0)))
10927 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10929 /* If op0 is a temporary because the original expressions was forced
10930 to memory, clear MEM_EXPR so that the original expression cannot
10931 be marked as addressable through MEM_EXPR of the temporary. */
10932 if (clear_mem_expr)
10933 set_mem_expr (op0, NULL_TREE);
10935 MEM_VOLATILE_P (op0) |= volatilep;
10937 if (reversep
10938 && modifier != EXPAND_MEMORY
10939 && modifier != EXPAND_WRITE)
10940 op0 = flip_storage_order (mode1, op0);
10942 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10943 || modifier == EXPAND_CONST_ADDRESS
10944 || modifier == EXPAND_INITIALIZER)
10945 return op0;
10947 if (target == 0)
10948 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10950 convert_move (target, op0, unsignedp);
10951 return target;
10954 case OBJ_TYPE_REF:
10955 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10957 case CALL_EXPR:
10958 /* All valid uses of __builtin_va_arg_pack () are removed during
10959 inlining. */
10960 if (CALL_EXPR_VA_ARG_PACK (exp))
10961 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10963 tree fndecl = get_callee_fndecl (exp), attr;
10965 if (fndecl
10966 /* Don't diagnose the error attribute in thunks, those are
10967 artificially created. */
10968 && !CALL_FROM_THUNK_P (exp)
10969 && (attr = lookup_attribute ("error",
10970 DECL_ATTRIBUTES (fndecl))) != NULL)
10972 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
10973 error ("%Kcall to %qs declared with attribute error: %s", exp,
10974 identifier_to_locale (ident),
10975 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10977 if (fndecl
10978 /* Don't diagnose the warning attribute in thunks, those are
10979 artificially created. */
10980 && !CALL_FROM_THUNK_P (exp)
10981 && (attr = lookup_attribute ("warning",
10982 DECL_ATTRIBUTES (fndecl))) != NULL)
10984 const char *ident = lang_hooks.decl_printable_name (fndecl, 1);
10985 warning_at (tree_nonartificial_location (exp), 0,
10986 "%Kcall to %qs declared with attribute warning: %s",
10987 exp, identifier_to_locale (ident),
10988 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10991 /* Check for a built-in function. */
10992 if (fndecl && DECL_BUILT_IN (fndecl))
10994 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10995 if (CALL_WITH_BOUNDS_P (exp))
10996 return expand_builtin_with_bounds (exp, target, subtarget,
10997 tmode, ignore);
10998 else
10999 return expand_builtin (exp, target, subtarget, tmode, ignore);
11002 return expand_call (exp, target, ignore);
11004 case VIEW_CONVERT_EXPR:
11005 op0 = NULL_RTX;
11007 /* If we are converting to BLKmode, try to avoid an intermediate
11008 temporary by fetching an inner memory reference. */
11009 if (mode == BLKmode
11010 && poly_int_tree_p (TYPE_SIZE (type))
11011 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
11012 && handled_component_p (treeop0))
11014 machine_mode mode1;
11015 poly_int64 bitsize, bitpos, bytepos;
11016 tree offset;
11017 int unsignedp, reversep, volatilep = 0;
11018 tree tem
11019 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
11020 &unsignedp, &reversep, &volatilep);
11021 rtx orig_op0;
11023 /* ??? We should work harder and deal with non-zero offsets. */
11024 if (!offset
11025 && multiple_p (bitpos, BITS_PER_UNIT, &bytepos)
11026 && !reversep
11027 && known_size_p (bitsize)
11028 && known_eq (wi::to_poly_offset (TYPE_SIZE (type)), bitsize))
11030 /* See the normal_inner_ref case for the rationale. */
11031 orig_op0
11032 = expand_expr_real (tem,
11033 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
11034 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
11035 != INTEGER_CST)
11036 && modifier != EXPAND_STACK_PARM
11037 ? target : NULL_RTX),
11038 VOIDmode,
11039 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
11040 NULL, true);
11042 if (MEM_P (orig_op0))
11044 op0 = orig_op0;
11046 /* Get a reference to just this component. */
11047 if (modifier == EXPAND_CONST_ADDRESS
11048 || modifier == EXPAND_SUM
11049 || modifier == EXPAND_INITIALIZER)
11050 op0 = adjust_address_nv (op0, mode, bytepos);
11051 else
11052 op0 = adjust_address (op0, mode, bytepos);
11054 if (op0 == orig_op0)
11055 op0 = copy_rtx (op0);
11057 set_mem_attributes (op0, treeop0, 0);
11058 if (REG_P (XEXP (op0, 0)))
11059 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
11061 MEM_VOLATILE_P (op0) |= volatilep;
11066 if (!op0)
11067 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
11068 NULL, inner_reference_p);
11070 /* If the input and output modes are both the same, we are done. */
11071 if (mode == GET_MODE (op0))
11073 /* If neither mode is BLKmode, and both modes are the same size
11074 then we can use gen_lowpart. */
11075 else if (mode != BLKmode
11076 && GET_MODE (op0) != BLKmode
11077 && known_eq (GET_MODE_PRECISION (mode),
11078 GET_MODE_PRECISION (GET_MODE (op0)))
11079 && !COMPLEX_MODE_P (GET_MODE (op0)))
11081 if (GET_CODE (op0) == SUBREG)
11082 op0 = force_reg (GET_MODE (op0), op0);
11083 temp = gen_lowpart_common (mode, op0);
11084 if (temp)
11085 op0 = temp;
11086 else
11088 if (!REG_P (op0) && !MEM_P (op0))
11089 op0 = force_reg (GET_MODE (op0), op0);
11090 op0 = gen_lowpart (mode, op0);
11093 /* If both types are integral, convert from one mode to the other. */
11094 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
11095 op0 = convert_modes (mode, GET_MODE (op0), op0,
11096 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
11097 /* If the output type is a bit-field type, do an extraction. */
11098 else if (reduce_bit_field)
11099 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
11100 TYPE_UNSIGNED (type), NULL_RTX,
11101 mode, mode, false, NULL);
11102 /* As a last resort, spill op0 to memory, and reload it in a
11103 different mode. */
11104 else if (!MEM_P (op0))
11106 /* If the operand is not a MEM, force it into memory. Since we
11107 are going to be changing the mode of the MEM, don't call
11108 force_const_mem for constants because we don't allow pool
11109 constants to change mode. */
11110 tree inner_type = TREE_TYPE (treeop0);
11112 gcc_assert (!TREE_ADDRESSABLE (exp));
11114 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
11115 target
11116 = assign_stack_temp_for_type
11117 (TYPE_MODE (inner_type),
11118 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
11120 emit_move_insn (target, op0);
11121 op0 = target;
11124 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
11125 output type is such that the operand is known to be aligned, indicate
11126 that it is. Otherwise, we need only be concerned about alignment for
11127 non-BLKmode results. */
11128 if (MEM_P (op0))
11130 enum insn_code icode;
11132 if (modifier != EXPAND_WRITE
11133 && modifier != EXPAND_MEMORY
11134 && !inner_reference_p
11135 && mode != BLKmode
11136 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
11138 /* If the target does have special handling for unaligned
11139 loads of mode then use them. */
11140 if ((icode = optab_handler (movmisalign_optab, mode))
11141 != CODE_FOR_nothing)
11143 rtx reg;
11145 op0 = adjust_address (op0, mode, 0);
11146 /* We've already validated the memory, and we're creating a
11147 new pseudo destination. The predicates really can't
11148 fail. */
11149 reg = gen_reg_rtx (mode);
11151 /* Nor can the insn generator. */
11152 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11153 emit_insn (insn);
11154 return reg;
11156 else if (STRICT_ALIGNMENT)
11158 poly_uint64 mode_size = GET_MODE_SIZE (mode);
11159 poly_uint64 temp_size = mode_size;
11160 if (GET_MODE (op0) != BLKmode)
11161 temp_size = upper_bound (temp_size,
11162 GET_MODE_SIZE (GET_MODE (op0)));
11163 rtx new_rtx
11164 = assign_stack_temp_for_type (mode, temp_size, type);
11165 rtx new_with_op0_mode
11166 = adjust_address (new_rtx, GET_MODE (op0), 0);
11168 gcc_assert (!TREE_ADDRESSABLE (exp));
11170 if (GET_MODE (op0) == BLKmode)
11172 rtx size_rtx = gen_int_mode (mode_size, Pmode);
11173 emit_block_move (new_with_op0_mode, op0, size_rtx,
11174 (modifier == EXPAND_STACK_PARM
11175 ? BLOCK_OP_CALL_PARM
11176 : BLOCK_OP_NORMAL));
11178 else
11179 emit_move_insn (new_with_op0_mode, op0);
11181 op0 = new_rtx;
11185 op0 = adjust_address (op0, mode, 0);
11188 return op0;
11190 case MODIFY_EXPR:
11192 tree lhs = treeop0;
11193 tree rhs = treeop1;
11194 gcc_assert (ignore);
11196 /* Check for |= or &= of a bitfield of size one into another bitfield
11197 of size 1. In this case, (unless we need the result of the
11198 assignment) we can do this more efficiently with a
11199 test followed by an assignment, if necessary.
11201 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11202 things change so we do, this code should be enhanced to
11203 support it. */
11204 if (TREE_CODE (lhs) == COMPONENT_REF
11205 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11206 || TREE_CODE (rhs) == BIT_AND_EXPR)
11207 && TREE_OPERAND (rhs, 0) == lhs
11208 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11209 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11210 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11212 rtx_code_label *label = gen_label_rtx ();
11213 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11214 do_jump (TREE_OPERAND (rhs, 1),
11215 value ? label : 0,
11216 value ? 0 : label,
11217 profile_probability::uninitialized ());
11218 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11219 false);
11220 do_pending_stack_adjust ();
11221 emit_label (label);
11222 return const0_rtx;
11225 expand_assignment (lhs, rhs, false);
11226 return const0_rtx;
11229 case ADDR_EXPR:
11230 return expand_expr_addr_expr (exp, target, tmode, modifier);
11232 case REALPART_EXPR:
11233 op0 = expand_normal (treeop0);
11234 return read_complex_part (op0, false);
11236 case IMAGPART_EXPR:
11237 op0 = expand_normal (treeop0);
11238 return read_complex_part (op0, true);
11240 case RETURN_EXPR:
11241 case LABEL_EXPR:
11242 case GOTO_EXPR:
11243 case SWITCH_EXPR:
11244 case ASM_EXPR:
11245 /* Expanded in cfgexpand.c. */
11246 gcc_unreachable ();
11248 case TRY_CATCH_EXPR:
11249 case CATCH_EXPR:
11250 case EH_FILTER_EXPR:
11251 case TRY_FINALLY_EXPR:
11252 /* Lowered by tree-eh.c. */
11253 gcc_unreachable ();
11255 case WITH_CLEANUP_EXPR:
11256 case CLEANUP_POINT_EXPR:
11257 case TARGET_EXPR:
11258 case CASE_LABEL_EXPR:
11259 case VA_ARG_EXPR:
11260 case BIND_EXPR:
11261 case INIT_EXPR:
11262 case CONJ_EXPR:
11263 case COMPOUND_EXPR:
11264 case PREINCREMENT_EXPR:
11265 case PREDECREMENT_EXPR:
11266 case POSTINCREMENT_EXPR:
11267 case POSTDECREMENT_EXPR:
11268 case LOOP_EXPR:
11269 case EXIT_EXPR:
11270 case COMPOUND_LITERAL_EXPR:
11271 /* Lowered by gimplify.c. */
11272 gcc_unreachable ();
11274 case FDESC_EXPR:
11275 /* Function descriptors are not valid except for as
11276 initialization constants, and should not be expanded. */
11277 gcc_unreachable ();
11279 case WITH_SIZE_EXPR:
11280 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11281 have pulled out the size to use in whatever context it needed. */
11282 return expand_expr_real (treeop0, original_target, tmode,
11283 modifier, alt_rtl, inner_reference_p);
11285 default:
11286 return expand_expr_real_2 (&ops, target, tmode, modifier);
11290 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11291 signedness of TYPE), possibly returning the result in TARGET.
11292 TYPE is known to be a partial integer type. */
11293 static rtx
11294 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11296 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11297 if (target && GET_MODE (target) != GET_MODE (exp))
11298 target = 0;
11299 /* For constant values, reduce using build_int_cst_type. */
11300 if (CONST_INT_P (exp))
11302 HOST_WIDE_INT value = INTVAL (exp);
11303 tree t = build_int_cst_type (type, value);
11304 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11306 else if (TYPE_UNSIGNED (type))
11308 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11309 rtx mask = immed_wide_int_const
11310 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11311 return expand_and (mode, exp, mask, target);
11313 else
11315 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (exp));
11316 int count = GET_MODE_PRECISION (mode) - prec;
11317 exp = expand_shift (LSHIFT_EXPR, mode, exp, count, target, 0);
11318 return expand_shift (RSHIFT_EXPR, mode, exp, count, target, 0);
11322 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11323 when applied to the address of EXP produces an address known to be
11324 aligned more than BIGGEST_ALIGNMENT. */
11326 static int
11327 is_aligning_offset (const_tree offset, const_tree exp)
11329 /* Strip off any conversions. */
11330 while (CONVERT_EXPR_P (offset))
11331 offset = TREE_OPERAND (offset, 0);
11333 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11334 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11335 if (TREE_CODE (offset) != BIT_AND_EXPR
11336 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11337 || compare_tree_int (TREE_OPERAND (offset, 1),
11338 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11339 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11340 return 0;
11342 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11343 It must be NEGATE_EXPR. Then strip any more conversions. */
11344 offset = TREE_OPERAND (offset, 0);
11345 while (CONVERT_EXPR_P (offset))
11346 offset = TREE_OPERAND (offset, 0);
11348 if (TREE_CODE (offset) != NEGATE_EXPR)
11349 return 0;
11351 offset = TREE_OPERAND (offset, 0);
11352 while (CONVERT_EXPR_P (offset))
11353 offset = TREE_OPERAND (offset, 0);
11355 /* This must now be the address of EXP. */
11356 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11359 /* Return the tree node if an ARG corresponds to a string constant or zero
11360 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11361 in bytes within the string that ARG is accessing. The type of the
11362 offset will be `sizetype'. */
11364 tree
11365 string_constant (tree arg, tree *ptr_offset)
11367 tree array, offset, lower_bound;
11368 STRIP_NOPS (arg);
11370 if (TREE_CODE (arg) == ADDR_EXPR)
11372 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11374 *ptr_offset = size_zero_node;
11375 return TREE_OPERAND (arg, 0);
11377 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11379 array = TREE_OPERAND (arg, 0);
11380 offset = size_zero_node;
11382 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11384 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11385 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11386 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11387 return 0;
11389 /* Check if the array has a nonzero lower bound. */
11390 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11391 if (!integer_zerop (lower_bound))
11393 /* If the offset and base aren't both constants, return 0. */
11394 if (TREE_CODE (lower_bound) != INTEGER_CST)
11395 return 0;
11396 if (TREE_CODE (offset) != INTEGER_CST)
11397 return 0;
11398 /* Adjust offset by the lower bound. */
11399 offset = size_diffop (fold_convert (sizetype, offset),
11400 fold_convert (sizetype, lower_bound));
11403 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11405 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11406 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11407 if (TREE_CODE (array) != ADDR_EXPR)
11408 return 0;
11409 array = TREE_OPERAND (array, 0);
11410 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11411 return 0;
11413 else
11414 return 0;
11416 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11418 tree arg0 = TREE_OPERAND (arg, 0);
11419 tree arg1 = TREE_OPERAND (arg, 1);
11421 STRIP_NOPS (arg0);
11422 STRIP_NOPS (arg1);
11424 if (TREE_CODE (arg0) == ADDR_EXPR
11425 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11426 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11428 array = TREE_OPERAND (arg0, 0);
11429 offset = arg1;
11431 else if (TREE_CODE (arg1) == ADDR_EXPR
11432 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11433 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11435 array = TREE_OPERAND (arg1, 0);
11436 offset = arg0;
11438 else
11439 return 0;
11441 else
11442 return 0;
11444 if (TREE_CODE (array) == STRING_CST)
11446 *ptr_offset = fold_convert (sizetype, offset);
11447 return array;
11449 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11451 int length;
11452 tree init = ctor_for_folding (array);
11454 /* Variables initialized to string literals can be handled too. */
11455 if (init == error_mark_node
11456 || !init
11457 || TREE_CODE (init) != STRING_CST)
11458 return 0;
11460 /* Avoid const char foo[4] = "abcde"; */
11461 if (DECL_SIZE_UNIT (array) == NULL_TREE
11462 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11463 || (length = TREE_STRING_LENGTH (init)) <= 0
11464 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11465 return 0;
11467 /* If variable is bigger than the string literal, OFFSET must be constant
11468 and inside of the bounds of the string literal. */
11469 offset = fold_convert (sizetype, offset);
11470 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11471 && (! tree_fits_uhwi_p (offset)
11472 || compare_tree_int (offset, length) >= 0))
11473 return 0;
11475 *ptr_offset = offset;
11476 return init;
11479 return 0;
11482 /* Generate code to calculate OPS, and exploded expression
11483 using a store-flag instruction and return an rtx for the result.
11484 OPS reflects a comparison.
11486 If TARGET is nonzero, store the result there if convenient.
11488 Return zero if there is no suitable set-flag instruction
11489 available on this machine.
11491 Once expand_expr has been called on the arguments of the comparison,
11492 we are committed to doing the store flag, since it is not safe to
11493 re-evaluate the expression. We emit the store-flag insn by calling
11494 emit_store_flag, but only expand the arguments if we have a reason
11495 to believe that emit_store_flag will be successful. If we think that
11496 it will, but it isn't, we have to simulate the store-flag with a
11497 set/jump/set sequence. */
11499 static rtx
11500 do_store_flag (sepops ops, rtx target, machine_mode mode)
11502 enum rtx_code code;
11503 tree arg0, arg1, type;
11504 machine_mode operand_mode;
11505 int unsignedp;
11506 rtx op0, op1;
11507 rtx subtarget = target;
11508 location_t loc = ops->location;
11510 arg0 = ops->op0;
11511 arg1 = ops->op1;
11513 /* Don't crash if the comparison was erroneous. */
11514 if (arg0 == error_mark_node || arg1 == error_mark_node)
11515 return const0_rtx;
11517 type = TREE_TYPE (arg0);
11518 operand_mode = TYPE_MODE (type);
11519 unsignedp = TYPE_UNSIGNED (type);
11521 /* We won't bother with BLKmode store-flag operations because it would mean
11522 passing a lot of information to emit_store_flag. */
11523 if (operand_mode == BLKmode)
11524 return 0;
11526 /* We won't bother with store-flag operations involving function pointers
11527 when function pointers must be canonicalized before comparisons. */
11528 if (targetm.have_canonicalize_funcptr_for_compare ()
11529 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11530 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11531 == FUNCTION_TYPE))
11532 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11533 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11534 == FUNCTION_TYPE))))
11535 return 0;
11537 STRIP_NOPS (arg0);
11538 STRIP_NOPS (arg1);
11540 /* For vector typed comparisons emit code to generate the desired
11541 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11542 expander for this. */
11543 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11545 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11546 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11547 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11548 return expand_vec_cmp_expr (ops->type, ifexp, target);
11549 else
11551 tree if_true = constant_boolean_node (true, ops->type);
11552 tree if_false = constant_boolean_node (false, ops->type);
11553 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11554 if_false, target);
11558 /* Get the rtx comparison code to use. We know that EXP is a comparison
11559 operation of some type. Some comparisons against 1 and -1 can be
11560 converted to comparisons with zero. Do so here so that the tests
11561 below will be aware that we have a comparison with zero. These
11562 tests will not catch constants in the first operand, but constants
11563 are rarely passed as the first operand. */
11565 switch (ops->code)
11567 case EQ_EXPR:
11568 code = EQ;
11569 break;
11570 case NE_EXPR:
11571 code = NE;
11572 break;
11573 case LT_EXPR:
11574 if (integer_onep (arg1))
11575 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11576 else
11577 code = unsignedp ? LTU : LT;
11578 break;
11579 case LE_EXPR:
11580 if (! unsignedp && integer_all_onesp (arg1))
11581 arg1 = integer_zero_node, code = LT;
11582 else
11583 code = unsignedp ? LEU : LE;
11584 break;
11585 case GT_EXPR:
11586 if (! unsignedp && integer_all_onesp (arg1))
11587 arg1 = integer_zero_node, code = GE;
11588 else
11589 code = unsignedp ? GTU : GT;
11590 break;
11591 case GE_EXPR:
11592 if (integer_onep (arg1))
11593 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11594 else
11595 code = unsignedp ? GEU : GE;
11596 break;
11598 case UNORDERED_EXPR:
11599 code = UNORDERED;
11600 break;
11601 case ORDERED_EXPR:
11602 code = ORDERED;
11603 break;
11604 case UNLT_EXPR:
11605 code = UNLT;
11606 break;
11607 case UNLE_EXPR:
11608 code = UNLE;
11609 break;
11610 case UNGT_EXPR:
11611 code = UNGT;
11612 break;
11613 case UNGE_EXPR:
11614 code = UNGE;
11615 break;
11616 case UNEQ_EXPR:
11617 code = UNEQ;
11618 break;
11619 case LTGT_EXPR:
11620 code = LTGT;
11621 break;
11623 default:
11624 gcc_unreachable ();
11627 /* Put a constant second. */
11628 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11629 || TREE_CODE (arg0) == FIXED_CST)
11631 std::swap (arg0, arg1);
11632 code = swap_condition (code);
11635 /* If this is an equality or inequality test of a single bit, we can
11636 do this by shifting the bit being tested to the low-order bit and
11637 masking the result with the constant 1. If the condition was EQ,
11638 we xor it with 1. This does not require an scc insn and is faster
11639 than an scc insn even if we have it.
11641 The code to make this transformation was moved into fold_single_bit_test,
11642 so we just call into the folder and expand its result. */
11644 if ((code == NE || code == EQ)
11645 && integer_zerop (arg1)
11646 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11648 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11649 if (srcstmt
11650 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11652 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11653 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11654 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11655 gimple_assign_rhs1 (srcstmt),
11656 gimple_assign_rhs2 (srcstmt));
11657 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11658 if (temp)
11659 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11663 if (! get_subtarget (target)
11664 || GET_MODE (subtarget) != operand_mode)
11665 subtarget = 0;
11667 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11669 if (target == 0)
11670 target = gen_reg_rtx (mode);
11672 /* Try a cstore if possible. */
11673 return emit_store_flag_force (target, code, op0, op1,
11674 operand_mode, unsignedp,
11675 (TYPE_PRECISION (ops->type) == 1
11676 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11679 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11680 0 otherwise (i.e. if there is no casesi instruction).
11682 DEFAULT_PROBABILITY is the probability of jumping to the default
11683 label. */
11685 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11686 rtx table_label, rtx default_label, rtx fallback_label,
11687 profile_probability default_probability)
11689 struct expand_operand ops[5];
11690 scalar_int_mode index_mode = SImode;
11691 rtx op1, op2, index;
11693 if (! targetm.have_casesi ())
11694 return 0;
11696 /* The index must be some form of integer. Convert it to SImode. */
11697 scalar_int_mode omode = SCALAR_INT_TYPE_MODE (index_type);
11698 if (GET_MODE_BITSIZE (omode) > GET_MODE_BITSIZE (index_mode))
11700 rtx rangertx = expand_normal (range);
11702 /* We must handle the endpoints in the original mode. */
11703 index_expr = build2 (MINUS_EXPR, index_type,
11704 index_expr, minval);
11705 minval = integer_zero_node;
11706 index = expand_normal (index_expr);
11707 if (default_label)
11708 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11709 omode, 1, default_label,
11710 default_probability);
11711 /* Now we can safely truncate. */
11712 index = convert_to_mode (index_mode, index, 0);
11714 else
11716 if (omode != index_mode)
11718 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11719 index_expr = fold_convert (index_type, index_expr);
11722 index = expand_normal (index_expr);
11725 do_pending_stack_adjust ();
11727 op1 = expand_normal (minval);
11728 op2 = expand_normal (range);
11730 create_input_operand (&ops[0], index, index_mode);
11731 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11732 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11733 create_fixed_operand (&ops[3], table_label);
11734 create_fixed_operand (&ops[4], (default_label
11735 ? default_label
11736 : fallback_label));
11737 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11738 return 1;
11741 /* Attempt to generate a tablejump instruction; same concept. */
11742 /* Subroutine of the next function.
11744 INDEX is the value being switched on, with the lowest value
11745 in the table already subtracted.
11746 MODE is its expected mode (needed if INDEX is constant).
11747 RANGE is the length of the jump table.
11748 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11750 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11751 index value is out of range.
11752 DEFAULT_PROBABILITY is the probability of jumping to
11753 the default label. */
11755 static void
11756 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11757 rtx default_label, profile_probability default_probability)
11759 rtx temp, vector;
11761 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11762 cfun->cfg->max_jumptable_ents = INTVAL (range);
11764 /* Do an unsigned comparison (in the proper mode) between the index
11765 expression and the value which represents the length of the range.
11766 Since we just finished subtracting the lower bound of the range
11767 from the index expression, this comparison allows us to simultaneously
11768 check that the original index expression value is both greater than
11769 or equal to the minimum value of the range and less than or equal to
11770 the maximum value of the range. */
11772 if (default_label)
11773 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11774 default_label, default_probability);
11777 /* If index is in range, it must fit in Pmode.
11778 Convert to Pmode so we can index with it. */
11779 if (mode != Pmode)
11780 index = convert_to_mode (Pmode, index, 1);
11782 /* Don't let a MEM slip through, because then INDEX that comes
11783 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11784 and break_out_memory_refs will go to work on it and mess it up. */
11785 #ifdef PIC_CASE_VECTOR_ADDRESS
11786 if (flag_pic && !REG_P (index))
11787 index = copy_to_mode_reg (Pmode, index);
11788 #endif
11790 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11791 GET_MODE_SIZE, because this indicates how large insns are. The other
11792 uses should all be Pmode, because they are addresses. This code
11793 could fail if addresses and insns are not the same size. */
11794 index = simplify_gen_binary (MULT, Pmode, index,
11795 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11796 Pmode));
11797 index = simplify_gen_binary (PLUS, Pmode, index,
11798 gen_rtx_LABEL_REF (Pmode, table_label));
11800 #ifdef PIC_CASE_VECTOR_ADDRESS
11801 if (flag_pic)
11802 index = PIC_CASE_VECTOR_ADDRESS (index);
11803 else
11804 #endif
11805 index = memory_address (CASE_VECTOR_MODE, index);
11806 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11807 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11808 convert_move (temp, vector, 0);
11810 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11812 /* If we are generating PIC code or if the table is PC-relative, the
11813 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11814 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11815 emit_barrier ();
11819 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11820 rtx table_label, rtx default_label,
11821 profile_probability default_probability)
11823 rtx index;
11825 if (! targetm.have_tablejump ())
11826 return 0;
11828 index_expr = fold_build2 (MINUS_EXPR, index_type,
11829 fold_convert (index_type, index_expr),
11830 fold_convert (index_type, minval));
11831 index = expand_normal (index_expr);
11832 do_pending_stack_adjust ();
11834 do_tablejump (index, TYPE_MODE (index_type),
11835 convert_modes (TYPE_MODE (index_type),
11836 TYPE_MODE (TREE_TYPE (range)),
11837 expand_normal (range),
11838 TYPE_UNSIGNED (TREE_TYPE (range))),
11839 table_label, default_label, default_probability);
11840 return 1;
11843 /* Return a CONST_VECTOR rtx representing vector mask for
11844 a VECTOR_CST of booleans. */
11845 static rtx
11846 const_vector_mask_from_tree (tree exp)
11848 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11849 machine_mode inner = GET_MODE_INNER (mode);
11851 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
11852 VECTOR_CST_NELTS_PER_PATTERN (exp));
11853 unsigned int count = builder.encoded_nelts ();
11854 for (unsigned int i = 0; i < count; ++i)
11856 tree elt = VECTOR_CST_ELT (exp, i);
11857 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11858 if (integer_zerop (elt))
11859 builder.quick_push (CONST0_RTX (inner));
11860 else if (integer_onep (elt)
11861 || integer_minus_onep (elt))
11862 builder.quick_push (CONSTM1_RTX (inner));
11863 else
11864 gcc_unreachable ();
11866 return builder.build ();
11869 /* EXP is a VECTOR_CST in which each element is either all-zeros or all-ones.
11870 Return a constant scalar rtx of mode MODE in which bit X is set if element
11871 X of EXP is nonzero. */
11872 static rtx
11873 const_scalar_mask_from_tree (scalar_int_mode mode, tree exp)
11875 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11876 tree elt;
11878 /* The result has a fixed number of bits so the input must too. */
11879 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
11880 for (unsigned int i = 0; i < nunits; ++i)
11882 elt = VECTOR_CST_ELT (exp, i);
11883 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11884 if (integer_all_onesp (elt))
11885 res = wi::set_bit (res, i);
11886 else
11887 gcc_assert (integer_zerop (elt));
11890 return immed_wide_int_const (res, mode);
11893 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11894 static rtx
11895 const_vector_from_tree (tree exp)
11897 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11899 if (initializer_zerop (exp))
11900 return CONST0_RTX (mode);
11902 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11903 return const_vector_mask_from_tree (exp);
11905 machine_mode inner = GET_MODE_INNER (mode);
11907 rtx_vector_builder builder (mode, VECTOR_CST_NPATTERNS (exp),
11908 VECTOR_CST_NELTS_PER_PATTERN (exp));
11909 unsigned int count = builder.encoded_nelts ();
11910 for (unsigned int i = 0; i < count; ++i)
11912 tree elt = VECTOR_CST_ELT (exp, i);
11913 if (TREE_CODE (elt) == REAL_CST)
11914 builder.quick_push (const_double_from_real_value (TREE_REAL_CST (elt),
11915 inner));
11916 else if (TREE_CODE (elt) == FIXED_CST)
11917 builder.quick_push (CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11918 inner));
11919 else
11920 builder.quick_push (immed_wide_int_const (wi::to_poly_wide (elt),
11921 inner));
11923 return builder.build ();
11926 /* Build a decl for a personality function given a language prefix. */
11928 tree
11929 build_personality_function (const char *lang)
11931 const char *unwind_and_version;
11932 tree decl, type;
11933 char *name;
11935 switch (targetm_common.except_unwind_info (&global_options))
11937 case UI_NONE:
11938 return NULL;
11939 case UI_SJLJ:
11940 unwind_and_version = "_sj0";
11941 break;
11942 case UI_DWARF2:
11943 case UI_TARGET:
11944 unwind_and_version = "_v0";
11945 break;
11946 case UI_SEH:
11947 unwind_and_version = "_seh0";
11948 break;
11949 default:
11950 gcc_unreachable ();
11953 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11955 type = build_function_type_list (integer_type_node, integer_type_node,
11956 long_long_unsigned_type_node,
11957 ptr_type_node, ptr_type_node, NULL_TREE);
11958 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11959 get_identifier (name), type);
11960 DECL_ARTIFICIAL (decl) = 1;
11961 DECL_EXTERNAL (decl) = 1;
11962 TREE_PUBLIC (decl) = 1;
11964 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11965 are the flags assigned by targetm.encode_section_info. */
11966 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11968 return decl;
11971 /* Extracts the personality function of DECL and returns the corresponding
11972 libfunc. */
11975 get_personality_function (tree decl)
11977 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11978 enum eh_personality_kind pk;
11980 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11981 if (pk == eh_personality_none)
11982 return NULL;
11984 if (!personality
11985 && pk == eh_personality_any)
11986 personality = lang_hooks.eh_personality ();
11988 if (pk == eh_personality_lang)
11989 gcc_assert (personality != NULL_TREE);
11991 return XEXP (DECL_RTL (personality), 0);
11994 /* Returns a tree for the size of EXP in bytes. */
11996 static tree
11997 tree_expr_size (const_tree exp)
11999 if (DECL_P (exp)
12000 && DECL_SIZE_UNIT (exp) != 0)
12001 return DECL_SIZE_UNIT (exp);
12002 else
12003 return size_in_bytes (TREE_TYPE (exp));
12006 /* Return an rtx for the size in bytes of the value of EXP. */
12009 expr_size (tree exp)
12011 tree size;
12013 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
12014 size = TREE_OPERAND (exp, 1);
12015 else
12017 size = tree_expr_size (exp);
12018 gcc_assert (size);
12019 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
12022 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
12025 /* Return a wide integer for the size in bytes of the value of EXP, or -1
12026 if the size can vary or is larger than an integer. */
12028 static HOST_WIDE_INT
12029 int_expr_size (tree exp)
12031 tree size;
12033 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
12034 size = TREE_OPERAND (exp, 1);
12035 else
12037 size = tree_expr_size (exp);
12038 gcc_assert (size);
12041 if (size == 0 || !tree_fits_shwi_p (size))
12042 return -1;
12044 return tree_to_shwi (size);