* omp-low.c (MASK_GANG, MASK_WORKER, MASK_VECTOR): Delete.
[official-gcc.git] / gcc / expr.c
blobda6887019288b0bc42aeb3bb4285b1cdb1ecc65e
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2015 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 "predict.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "df.h"
29 #include "ssa.h"
30 #include "alias.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "attribs.h"
34 #include "varasm.h"
35 #include "flags.h"
36 #include "regs.h"
37 #include "except.h"
38 #include "insn-config.h"
39 #include "insn-attr.h"
40 #include "expmed.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "calls.h"
44 #include "emit-rtl.h"
45 #include "stmt.h"
46 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
47 #include "expr.h"
48 #include "insn-codes.h"
49 #include "optabs-tree.h"
50 #include "optabs.h"
51 #include "libfuncs.h"
52 #include "recog.h"
53 #include "reload.h"
54 #include "typeclass.h"
55 #include "toplev.h"
56 #include "langhooks.h"
57 #include "intl.h"
58 #include "tm_p.h"
59 #include "tree-iterator.h"
60 #include "internal-fn.h"
61 #include "cgraph.h"
62 #include "target.h"
63 #include "common/common-target.h"
64 #include "timevar.h"
65 #include "diagnostic.h"
66 #include "tree-ssa-live.h"
67 #include "tree-outof-ssa.h"
68 #include "target-globals.h"
69 #include "params.h"
70 #include "tree-ssa-address.h"
71 #include "cfgexpand.h"
72 #include "builtins.h"
73 #include "tree-chkp.h"
74 #include "rtl-chkp.h"
75 #include "ccmp.h"
78 /* If this is nonzero, we do not bother generating VOLATILE
79 around volatile memory references, and we are willing to
80 output indirect addresses. If cse is to follow, we reject
81 indirect addresses so a useful potential cse is generated;
82 if it is used only once, instruction combination will produce
83 the same indirect address eventually. */
84 int cse_not_expected;
86 /* This structure is used by move_by_pieces to describe the move to
87 be performed. */
88 struct move_by_pieces_d
90 rtx to;
91 rtx to_addr;
92 int autinc_to;
93 int explicit_inc_to;
94 rtx from;
95 rtx from_addr;
96 int autinc_from;
97 int explicit_inc_from;
98 unsigned HOST_WIDE_INT len;
99 HOST_WIDE_INT offset;
100 int reverse;
103 /* This structure is used by store_by_pieces to describe the clear to
104 be performed. */
106 struct store_by_pieces_d
108 rtx to;
109 rtx to_addr;
110 int autinc_to;
111 int explicit_inc_to;
112 unsigned HOST_WIDE_INT len;
113 HOST_WIDE_INT offset;
114 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
115 void *constfundata;
116 int reverse;
119 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
120 struct move_by_pieces_d *);
121 static bool block_move_libcall_safe_for_call_parm (void);
122 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
123 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
124 unsigned HOST_WIDE_INT);
125 static tree emit_block_move_libcall_fn (int);
126 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
127 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
128 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
129 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
130 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
131 struct store_by_pieces_d *);
132 static tree clear_storage_libcall_fn (int);
133 static rtx_insn *compress_float_constant (rtx, rtx);
134 static rtx get_subtarget (rtx);
135 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
136 HOST_WIDE_INT, machine_mode,
137 tree, int, alias_set_type);
138 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
139 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
140 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
141 machine_mode, tree, alias_set_type, bool);
143 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
145 static int is_aligning_offset (const_tree, const_tree);
146 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
147 static rtx do_store_flag (sepops, rtx, machine_mode);
148 #ifdef PUSH_ROUNDING
149 static void emit_single_push_insn (machine_mode, rtx, tree);
150 #endif
151 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
152 static rtx const_vector_from_tree (tree);
153 static tree tree_expr_size (const_tree);
154 static HOST_WIDE_INT int_expr_size (tree);
157 /* This is run to set up which modes can be used
158 directly in memory and to initialize the block move optab. It is run
159 at the beginning of compilation and when the target is reinitialized. */
161 void
162 init_expr_target (void)
164 rtx insn, pat;
165 machine_mode mode;
166 int num_clobbers;
167 rtx mem, mem1;
168 rtx reg;
170 /* Try indexing by frame ptr and try by stack ptr.
171 It is known that on the Convex the stack ptr isn't a valid index.
172 With luck, one or the other is valid on any machine. */
173 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
174 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
176 /* A scratch register we can modify in-place below to avoid
177 useless RTL allocations. */
178 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
180 insn = rtx_alloc (INSN);
181 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
182 PATTERN (insn) = pat;
184 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
185 mode = (machine_mode) ((int) mode + 1))
187 int regno;
189 direct_load[(int) mode] = direct_store[(int) mode] = 0;
190 PUT_MODE (mem, mode);
191 PUT_MODE (mem1, mode);
193 /* See if there is some register that can be used in this mode and
194 directly loaded or stored from memory. */
196 if (mode != VOIDmode && mode != BLKmode)
197 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
198 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
199 regno++)
201 if (! HARD_REGNO_MODE_OK (regno, mode))
202 continue;
204 set_mode_and_regno (reg, mode, regno);
206 SET_SRC (pat) = mem;
207 SET_DEST (pat) = reg;
208 if (recog (pat, insn, &num_clobbers) >= 0)
209 direct_load[(int) mode] = 1;
211 SET_SRC (pat) = mem1;
212 SET_DEST (pat) = reg;
213 if (recog (pat, insn, &num_clobbers) >= 0)
214 direct_load[(int) mode] = 1;
216 SET_SRC (pat) = reg;
217 SET_DEST (pat) = mem;
218 if (recog (pat, insn, &num_clobbers) >= 0)
219 direct_store[(int) mode] = 1;
221 SET_SRC (pat) = reg;
222 SET_DEST (pat) = mem1;
223 if (recog (pat, insn, &num_clobbers) >= 0)
224 direct_store[(int) mode] = 1;
228 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
230 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
231 mode = GET_MODE_WIDER_MODE (mode))
233 machine_mode srcmode;
234 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
235 srcmode = GET_MODE_WIDER_MODE (srcmode))
237 enum insn_code ic;
239 ic = can_extend_p (mode, srcmode, 0);
240 if (ic == CODE_FOR_nothing)
241 continue;
243 PUT_MODE (mem, srcmode);
245 if (insn_operand_matches (ic, 1, mem))
246 float_extend_from_mem[mode][srcmode] = true;
251 /* This is run at the start of compiling a function. */
253 void
254 init_expr (void)
256 memset (&crtl->expr, 0, sizeof (crtl->expr));
259 /* Copy data from FROM to TO, where the machine modes are not the same.
260 Both modes may be integer, or both may be floating, or both may be
261 fixed-point.
262 UNSIGNEDP should be nonzero if FROM is an unsigned type.
263 This causes zero-extension instead of sign-extension. */
265 void
266 convert_move (rtx to, rtx from, int unsignedp)
268 machine_mode to_mode = GET_MODE (to);
269 machine_mode from_mode = GET_MODE (from);
270 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
271 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
272 enum insn_code code;
273 rtx libcall;
275 /* rtx code for making an equivalent value. */
276 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
277 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
280 gcc_assert (to_real == from_real);
281 gcc_assert (to_mode != BLKmode);
282 gcc_assert (from_mode != BLKmode);
284 /* If the source and destination are already the same, then there's
285 nothing to do. */
286 if (to == from)
287 return;
289 /* If FROM is a SUBREG that indicates that we have already done at least
290 the required extension, strip it. We don't handle such SUBREGs as
291 TO here. */
293 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
294 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
295 >= GET_MODE_PRECISION (to_mode))
296 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
297 from = gen_lowpart (to_mode, from), from_mode = to_mode;
299 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
301 if (to_mode == from_mode
302 || (from_mode == VOIDmode && CONSTANT_P (from)))
304 emit_move_insn (to, from);
305 return;
308 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
310 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
312 if (VECTOR_MODE_P (to_mode))
313 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
314 else
315 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
317 emit_move_insn (to, from);
318 return;
321 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
323 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
324 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
325 return;
328 if (to_real)
330 rtx value;
331 rtx_insn *insns;
332 convert_optab tab;
334 gcc_assert ((GET_MODE_PRECISION (from_mode)
335 != GET_MODE_PRECISION (to_mode))
336 || (DECIMAL_FLOAT_MODE_P (from_mode)
337 != DECIMAL_FLOAT_MODE_P (to_mode)));
339 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
340 /* Conversion between decimal float and binary float, same size. */
341 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
342 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
343 tab = sext_optab;
344 else
345 tab = trunc_optab;
347 /* Try converting directly if the insn is supported. */
349 code = convert_optab_handler (tab, to_mode, from_mode);
350 if (code != CODE_FOR_nothing)
352 emit_unop_insn (code, to, from,
353 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
354 return;
357 /* Otherwise use a libcall. */
358 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
360 /* Is this conversion implemented yet? */
361 gcc_assert (libcall);
363 start_sequence ();
364 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
365 1, from, from_mode);
366 insns = get_insns ();
367 end_sequence ();
368 emit_libcall_block (insns, to, value,
369 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
370 from)
371 : gen_rtx_FLOAT_EXTEND (to_mode, from));
372 return;
375 /* Handle pointer conversion. */ /* SPEE 900220. */
376 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
378 convert_optab ctab;
380 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
381 ctab = trunc_optab;
382 else if (unsignedp)
383 ctab = zext_optab;
384 else
385 ctab = sext_optab;
387 if (convert_optab_handler (ctab, to_mode, from_mode)
388 != CODE_FOR_nothing)
390 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
391 to, from, UNKNOWN);
392 return;
396 /* Targets are expected to provide conversion insns between PxImode and
397 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
398 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
400 machine_mode full_mode
401 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
403 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
404 != CODE_FOR_nothing);
406 if (full_mode != from_mode)
407 from = convert_to_mode (full_mode, from, unsignedp);
408 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
409 to, from, UNKNOWN);
410 return;
412 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
414 rtx new_from;
415 machine_mode full_mode
416 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
417 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
418 enum insn_code icode;
420 icode = convert_optab_handler (ctab, full_mode, from_mode);
421 gcc_assert (icode != CODE_FOR_nothing);
423 if (to_mode == full_mode)
425 emit_unop_insn (icode, to, from, UNKNOWN);
426 return;
429 new_from = gen_reg_rtx (full_mode);
430 emit_unop_insn (icode, new_from, from, UNKNOWN);
432 /* else proceed to integer conversions below. */
433 from_mode = full_mode;
434 from = new_from;
437 /* Make sure both are fixed-point modes or both are not. */
438 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
439 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
440 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
442 /* If we widen from_mode to to_mode and they are in the same class,
443 we won't saturate the result.
444 Otherwise, always saturate the result to play safe. */
445 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
446 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
447 expand_fixed_convert (to, from, 0, 0);
448 else
449 expand_fixed_convert (to, from, 0, 1);
450 return;
453 /* Now both modes are integers. */
455 /* Handle expanding beyond a word. */
456 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
457 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
459 rtx_insn *insns;
460 rtx lowpart;
461 rtx fill_value;
462 rtx lowfrom;
463 int i;
464 machine_mode lowpart_mode;
465 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
467 /* Try converting directly if the insn is supported. */
468 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
469 != CODE_FOR_nothing)
471 /* If FROM is a SUBREG, put it into a register. Do this
472 so that we always generate the same set of insns for
473 better cse'ing; if an intermediate assignment occurred,
474 we won't be doing the operation directly on the SUBREG. */
475 if (optimize > 0 && GET_CODE (from) == SUBREG)
476 from = force_reg (from_mode, from);
477 emit_unop_insn (code, to, from, equiv_code);
478 return;
480 /* Next, try converting via full word. */
481 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
482 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
483 != CODE_FOR_nothing))
485 rtx word_to = gen_reg_rtx (word_mode);
486 if (REG_P (to))
488 if (reg_overlap_mentioned_p (to, from))
489 from = force_reg (from_mode, from);
490 emit_clobber (to);
492 convert_move (word_to, from, unsignedp);
493 emit_unop_insn (code, to, word_to, equiv_code);
494 return;
497 /* No special multiword conversion insn; do it by hand. */
498 start_sequence ();
500 /* Since we will turn this into a no conflict block, we must ensure the
501 the source does not overlap the target so force it into an isolated
502 register when maybe so. Likewise for any MEM input, since the
503 conversion sequence might require several references to it and we
504 must ensure we're getting the same value every time. */
506 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
507 from = force_reg (from_mode, from);
509 /* Get a copy of FROM widened to a word, if necessary. */
510 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
511 lowpart_mode = word_mode;
512 else
513 lowpart_mode = from_mode;
515 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
517 lowpart = gen_lowpart (lowpart_mode, to);
518 emit_move_insn (lowpart, lowfrom);
520 /* Compute the value to put in each remaining word. */
521 if (unsignedp)
522 fill_value = const0_rtx;
523 else
524 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
525 LT, lowfrom, const0_rtx,
526 lowpart_mode, 0, -1);
528 /* Fill the remaining words. */
529 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
531 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
532 rtx subword = operand_subword (to, index, 1, to_mode);
534 gcc_assert (subword);
536 if (fill_value != subword)
537 emit_move_insn (subword, fill_value);
540 insns = get_insns ();
541 end_sequence ();
543 emit_insn (insns);
544 return;
547 /* Truncating multi-word to a word or less. */
548 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
549 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
551 if (!((MEM_P (from)
552 && ! MEM_VOLATILE_P (from)
553 && direct_load[(int) to_mode]
554 && ! mode_dependent_address_p (XEXP (from, 0),
555 MEM_ADDR_SPACE (from)))
556 || REG_P (from)
557 || GET_CODE (from) == SUBREG))
558 from = force_reg (from_mode, from);
559 convert_move (to, gen_lowpart (word_mode, from), 0);
560 return;
563 /* Now follow all the conversions between integers
564 no more than a word long. */
566 /* For truncation, usually we can just refer to FROM in a narrower mode. */
567 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
568 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
570 if (!((MEM_P (from)
571 && ! MEM_VOLATILE_P (from)
572 && direct_load[(int) to_mode]
573 && ! mode_dependent_address_p (XEXP (from, 0),
574 MEM_ADDR_SPACE (from)))
575 || REG_P (from)
576 || GET_CODE (from) == SUBREG))
577 from = force_reg (from_mode, from);
578 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
579 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
580 from = copy_to_reg (from);
581 emit_move_insn (to, gen_lowpart (to_mode, from));
582 return;
585 /* Handle extension. */
586 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
588 /* Convert directly if that works. */
589 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
590 != CODE_FOR_nothing)
592 emit_unop_insn (code, to, from, equiv_code);
593 return;
595 else
597 machine_mode intermediate;
598 rtx tmp;
599 int shift_amount;
601 /* Search for a mode to convert via. */
602 for (intermediate = from_mode; intermediate != VOIDmode;
603 intermediate = GET_MODE_WIDER_MODE (intermediate))
604 if (((can_extend_p (to_mode, intermediate, unsignedp)
605 != CODE_FOR_nothing)
606 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
607 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
608 && (can_extend_p (intermediate, from_mode, unsignedp)
609 != CODE_FOR_nothing))
611 convert_move (to, convert_to_mode (intermediate, from,
612 unsignedp), unsignedp);
613 return;
616 /* No suitable intermediate mode.
617 Generate what we need with shifts. */
618 shift_amount = (GET_MODE_PRECISION (to_mode)
619 - GET_MODE_PRECISION (from_mode));
620 from = gen_lowpart (to_mode, force_reg (from_mode, from));
621 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
622 to, unsignedp);
623 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
624 to, unsignedp);
625 if (tmp != to)
626 emit_move_insn (to, tmp);
627 return;
631 /* Support special truncate insns for certain modes. */
632 if (convert_optab_handler (trunc_optab, to_mode,
633 from_mode) != CODE_FOR_nothing)
635 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
636 to, from, UNKNOWN);
637 return;
640 /* Handle truncation of volatile memrefs, and so on;
641 the things that couldn't be truncated directly,
642 and for which there was no special instruction.
644 ??? Code above formerly short-circuited this, for most integer
645 mode pairs, with a force_reg in from_mode followed by a recursive
646 call to this routine. Appears always to have been wrong. */
647 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
649 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
650 emit_move_insn (to, temp);
651 return;
654 /* Mode combination is not recognized. */
655 gcc_unreachable ();
658 /* Return an rtx for a value that would result
659 from converting X to mode MODE.
660 Both X and MODE may be floating, or both integer.
661 UNSIGNEDP is nonzero if X is an unsigned value.
662 This can be done by referring to a part of X in place
663 or by copying to a new temporary with conversion. */
666 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
668 return convert_modes (mode, VOIDmode, x, unsignedp);
671 /* Return an rtx for a value that would result
672 from converting X from mode OLDMODE to mode MODE.
673 Both modes may be floating, or both integer.
674 UNSIGNEDP is nonzero if X is an unsigned value.
676 This can be done by referring to a part of X in place
677 or by copying to a new temporary with conversion.
679 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
682 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
684 rtx temp;
686 /* If FROM is a SUBREG that indicates that we have already done at least
687 the required extension, strip it. */
689 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
690 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
691 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
692 x = gen_lowpart (mode, SUBREG_REG (x));
694 if (GET_MODE (x) != VOIDmode)
695 oldmode = GET_MODE (x);
697 if (mode == oldmode)
698 return x;
700 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
702 /* If the caller did not tell us the old mode, then there is not
703 much to do with respect to canonicalization. We have to
704 assume that all the bits are significant. */
705 if (GET_MODE_CLASS (oldmode) != MODE_INT)
706 oldmode = MAX_MODE_INT;
707 wide_int w = wide_int::from (std::make_pair (x, oldmode),
708 GET_MODE_PRECISION (mode),
709 unsignedp ? UNSIGNED : SIGNED);
710 return immed_wide_int_const (w, mode);
713 /* We can do this with a gen_lowpart if both desired and current modes
714 are integer, and this is either a constant integer, a register, or a
715 non-volatile MEM. */
716 if (GET_MODE_CLASS (mode) == MODE_INT
717 && GET_MODE_CLASS (oldmode) == MODE_INT
718 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
719 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
720 || (REG_P (x)
721 && (!HARD_REGISTER_P (x)
722 || HARD_REGNO_MODE_OK (REGNO (x), mode))
723 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
725 return gen_lowpart (mode, x);
727 /* Converting from integer constant into mode is always equivalent to an
728 subreg operation. */
729 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
731 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
732 return simplify_gen_subreg (mode, x, oldmode, 0);
735 temp = gen_reg_rtx (mode);
736 convert_move (temp, x, unsignedp);
737 return temp;
740 /* Return the largest alignment we can use for doing a move (or store)
741 of MAX_PIECES. ALIGN is the largest alignment we could use. */
743 static unsigned int
744 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
746 machine_mode tmode;
748 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
749 if (align >= GET_MODE_ALIGNMENT (tmode))
750 align = GET_MODE_ALIGNMENT (tmode);
751 else
753 machine_mode tmode, xmode;
755 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
756 tmode != VOIDmode;
757 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
758 if (GET_MODE_SIZE (tmode) > max_pieces
759 || SLOW_UNALIGNED_ACCESS (tmode, align))
760 break;
762 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
765 return align;
768 /* Return the widest integer mode no wider than SIZE. If no such mode
769 can be found, return VOIDmode. */
771 static machine_mode
772 widest_int_mode_for_size (unsigned int size)
774 machine_mode tmode, mode = VOIDmode;
776 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
777 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
778 if (GET_MODE_SIZE (tmode) < size)
779 mode = tmode;
781 return mode;
784 /* Determine whether the LEN bytes can be moved by using several move
785 instructions. Return nonzero if a call to move_by_pieces should
786 succeed. */
789 can_move_by_pieces (unsigned HOST_WIDE_INT len,
790 unsigned int align)
792 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
793 optimize_insn_for_speed_p ());
796 /* Generate several move instructions to copy LEN bytes from block FROM to
797 block TO. (These are MEM rtx's with BLKmode).
799 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
800 used to push FROM to the stack.
802 ALIGN is maximum stack alignment we can assume.
804 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
805 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
806 stpcpy. */
809 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
810 unsigned int align, int endp)
812 struct move_by_pieces_d data;
813 machine_mode to_addr_mode;
814 machine_mode from_addr_mode = get_address_mode (from);
815 rtx to_addr, from_addr = XEXP (from, 0);
816 unsigned int max_size = MOVE_MAX_PIECES + 1;
817 enum insn_code icode;
819 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
821 data.offset = 0;
822 data.from_addr = from_addr;
823 if (to)
825 to_addr_mode = get_address_mode (to);
826 to_addr = XEXP (to, 0);
827 data.to = to;
828 data.autinc_to
829 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
830 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
831 data.reverse
832 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
834 else
836 to_addr_mode = VOIDmode;
837 to_addr = NULL_RTX;
838 data.to = NULL_RTX;
839 data.autinc_to = 1;
840 if (STACK_GROWS_DOWNWARD)
841 data.reverse = 1;
842 else
843 data.reverse = 0;
845 data.to_addr = to_addr;
846 data.from = from;
847 data.autinc_from
848 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
849 || GET_CODE (from_addr) == POST_INC
850 || GET_CODE (from_addr) == POST_DEC);
852 data.explicit_inc_from = 0;
853 data.explicit_inc_to = 0;
854 if (data.reverse) data.offset = len;
855 data.len = len;
857 /* If copying requires more than two move insns,
858 copy addresses to registers (to make displacements shorter)
859 and use post-increment if available. */
860 if (!(data.autinc_from && data.autinc_to)
861 && move_by_pieces_ninsns (len, align, max_size) > 2)
863 /* Find the mode of the largest move...
864 MODE might not be used depending on the definitions of the
865 USE_* macros below. */
866 machine_mode mode ATTRIBUTE_UNUSED
867 = widest_int_mode_for_size (max_size);
869 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
871 data.from_addr = copy_to_mode_reg (from_addr_mode,
872 plus_constant (from_addr_mode,
873 from_addr, len));
874 data.autinc_from = 1;
875 data.explicit_inc_from = -1;
877 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
879 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
880 data.autinc_from = 1;
881 data.explicit_inc_from = 1;
883 if (!data.autinc_from && CONSTANT_P (from_addr))
884 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
885 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
887 data.to_addr = copy_to_mode_reg (to_addr_mode,
888 plus_constant (to_addr_mode,
889 to_addr, len));
890 data.autinc_to = 1;
891 data.explicit_inc_to = -1;
893 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
895 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
896 data.autinc_to = 1;
897 data.explicit_inc_to = 1;
899 if (!data.autinc_to && CONSTANT_P (to_addr))
900 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
903 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
905 /* First move what we can in the largest integer mode, then go to
906 successively smaller modes. */
908 while (max_size > 1 && data.len > 0)
910 machine_mode mode = widest_int_mode_for_size (max_size);
912 if (mode == VOIDmode)
913 break;
915 icode = optab_handler (mov_optab, mode);
916 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
917 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
919 max_size = GET_MODE_SIZE (mode);
922 /* The code above should have handled everything. */
923 gcc_assert (!data.len);
925 if (endp)
927 rtx to1;
929 gcc_assert (!data.reverse);
930 if (data.autinc_to)
932 if (endp == 2)
934 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
935 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
936 else
937 data.to_addr = copy_to_mode_reg (to_addr_mode,
938 plus_constant (to_addr_mode,
939 data.to_addr,
940 -1));
942 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
943 data.offset);
945 else
947 if (endp == 2)
948 --data.offset;
949 to1 = adjust_address (data.to, QImode, data.offset);
951 return to1;
953 else
954 return data.to;
957 /* Return number of insns required to move L bytes by pieces.
958 ALIGN (in bits) is maximum alignment we can assume. */
960 unsigned HOST_WIDE_INT
961 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
962 unsigned int max_size)
964 unsigned HOST_WIDE_INT n_insns = 0;
966 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
968 while (max_size > 1 && l > 0)
970 machine_mode mode;
971 enum insn_code icode;
973 mode = widest_int_mode_for_size (max_size);
975 if (mode == VOIDmode)
976 break;
978 icode = optab_handler (mov_optab, mode);
979 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
980 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
982 max_size = GET_MODE_SIZE (mode);
985 gcc_assert (!l);
986 return n_insns;
989 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
990 with move instructions for mode MODE. GENFUN is the gen_... function
991 to make a move insn for that mode. DATA has all the other info. */
993 static void
994 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
995 struct move_by_pieces_d *data)
997 unsigned int size = GET_MODE_SIZE (mode);
998 rtx to1 = NULL_RTX, from1;
1000 while (data->len >= size)
1002 if (data->reverse)
1003 data->offset -= size;
1005 if (data->to)
1007 if (data->autinc_to)
1008 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1009 data->offset);
1010 else
1011 to1 = adjust_address (data->to, mode, data->offset);
1014 if (data->autinc_from)
1015 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1016 data->offset);
1017 else
1018 from1 = adjust_address (data->from, mode, data->offset);
1020 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1021 emit_insn (gen_add2_insn (data->to_addr,
1022 gen_int_mode (-(HOST_WIDE_INT) size,
1023 GET_MODE (data->to_addr))));
1024 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1025 emit_insn (gen_add2_insn (data->from_addr,
1026 gen_int_mode (-(HOST_WIDE_INT) size,
1027 GET_MODE (data->from_addr))));
1029 if (data->to)
1030 emit_insn ((*genfun) (to1, from1));
1031 else
1033 #ifdef PUSH_ROUNDING
1034 emit_single_push_insn (mode, from1, NULL);
1035 #else
1036 gcc_unreachable ();
1037 #endif
1040 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1041 emit_insn (gen_add2_insn (data->to_addr,
1042 gen_int_mode (size,
1043 GET_MODE (data->to_addr))));
1044 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1045 emit_insn (gen_add2_insn (data->from_addr,
1046 gen_int_mode (size,
1047 GET_MODE (data->from_addr))));
1049 if (! data->reverse)
1050 data->offset += size;
1052 data->len -= size;
1056 /* Emit code to move a block Y to a block X. This may be done with
1057 string-move instructions, with multiple scalar move instructions,
1058 or with a library call.
1060 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1061 SIZE is an rtx that says how long they are.
1062 ALIGN is the maximum alignment we can assume they have.
1063 METHOD describes what kind of copy this is, and what mechanisms may be used.
1064 MIN_SIZE is the minimal size of block to move
1065 MAX_SIZE is the maximal size of block to move, if it can not be represented
1066 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1068 Return the address of the new block, if memcpy is called and returns it,
1069 0 otherwise. */
1072 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1073 unsigned int expected_align, HOST_WIDE_INT expected_size,
1074 unsigned HOST_WIDE_INT min_size,
1075 unsigned HOST_WIDE_INT max_size,
1076 unsigned HOST_WIDE_INT probable_max_size)
1078 bool may_use_call;
1079 rtx retval = 0;
1080 unsigned int align;
1082 gcc_assert (size);
1083 if (CONST_INT_P (size)
1084 && INTVAL (size) == 0)
1085 return 0;
1087 switch (method)
1089 case BLOCK_OP_NORMAL:
1090 case BLOCK_OP_TAILCALL:
1091 may_use_call = true;
1092 break;
1094 case BLOCK_OP_CALL_PARM:
1095 may_use_call = block_move_libcall_safe_for_call_parm ();
1097 /* Make inhibit_defer_pop nonzero around the library call
1098 to force it to pop the arguments right away. */
1099 NO_DEFER_POP;
1100 break;
1102 case BLOCK_OP_NO_LIBCALL:
1103 may_use_call = false;
1104 break;
1106 default:
1107 gcc_unreachable ();
1110 gcc_assert (MEM_P (x) && MEM_P (y));
1111 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1112 gcc_assert (align >= BITS_PER_UNIT);
1114 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1115 block copy is more efficient for other large modes, e.g. DCmode. */
1116 x = adjust_address (x, BLKmode, 0);
1117 y = adjust_address (y, BLKmode, 0);
1119 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1120 can be incorrect is coming from __builtin_memcpy. */
1121 if (CONST_INT_P (size))
1123 x = shallow_copy_rtx (x);
1124 y = shallow_copy_rtx (y);
1125 set_mem_size (x, INTVAL (size));
1126 set_mem_size (y, INTVAL (size));
1129 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1130 move_by_pieces (x, y, INTVAL (size), align, 0);
1131 else if (emit_block_move_via_movmem (x, y, size, align,
1132 expected_align, expected_size,
1133 min_size, max_size, probable_max_size))
1135 else if (may_use_call
1136 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1137 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1139 /* Since x and y are passed to a libcall, mark the corresponding
1140 tree EXPR as addressable. */
1141 tree y_expr = MEM_EXPR (y);
1142 tree x_expr = MEM_EXPR (x);
1143 if (y_expr)
1144 mark_addressable (y_expr);
1145 if (x_expr)
1146 mark_addressable (x_expr);
1147 retval = emit_block_move_via_libcall (x, y, size,
1148 method == BLOCK_OP_TAILCALL);
1151 else
1152 emit_block_move_via_loop (x, y, size, align);
1154 if (method == BLOCK_OP_CALL_PARM)
1155 OK_DEFER_POP;
1157 return retval;
1161 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1163 unsigned HOST_WIDE_INT max, min = 0;
1164 if (GET_CODE (size) == CONST_INT)
1165 min = max = UINTVAL (size);
1166 else
1167 max = GET_MODE_MASK (GET_MODE (size));
1168 return emit_block_move_hints (x, y, size, method, 0, -1,
1169 min, max, max);
1172 /* A subroutine of emit_block_move. Returns true if calling the
1173 block move libcall will not clobber any parameters which may have
1174 already been placed on the stack. */
1176 static bool
1177 block_move_libcall_safe_for_call_parm (void)
1179 #if defined (REG_PARM_STACK_SPACE)
1180 tree fn;
1181 #endif
1183 /* If arguments are pushed on the stack, then they're safe. */
1184 if (PUSH_ARGS)
1185 return true;
1187 /* If registers go on the stack anyway, any argument is sure to clobber
1188 an outgoing argument. */
1189 #if defined (REG_PARM_STACK_SPACE)
1190 fn = emit_block_move_libcall_fn (false);
1191 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1192 depend on its argument. */
1193 (void) fn;
1194 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1195 && REG_PARM_STACK_SPACE (fn) != 0)
1196 return false;
1197 #endif
1199 /* If any argument goes in memory, then it might clobber an outgoing
1200 argument. */
1202 CUMULATIVE_ARGS args_so_far_v;
1203 cumulative_args_t args_so_far;
1204 tree fn, arg;
1206 fn = emit_block_move_libcall_fn (false);
1207 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1208 args_so_far = pack_cumulative_args (&args_so_far_v);
1210 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1211 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1213 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1214 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1215 NULL_TREE, true);
1216 if (!tmp || !REG_P (tmp))
1217 return false;
1218 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1219 return false;
1220 targetm.calls.function_arg_advance (args_so_far, mode,
1221 NULL_TREE, true);
1224 return true;
1227 /* A subroutine of emit_block_move. Expand a movmem pattern;
1228 return true if successful. */
1230 static bool
1231 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1232 unsigned int expected_align, HOST_WIDE_INT expected_size,
1233 unsigned HOST_WIDE_INT min_size,
1234 unsigned HOST_WIDE_INT max_size,
1235 unsigned HOST_WIDE_INT probable_max_size)
1237 int save_volatile_ok = volatile_ok;
1238 machine_mode mode;
1240 if (expected_align < align)
1241 expected_align = align;
1242 if (expected_size != -1)
1244 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1245 expected_size = probable_max_size;
1246 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1247 expected_size = min_size;
1250 /* Since this is a move insn, we don't care about volatility. */
1251 volatile_ok = 1;
1253 /* Try the most limited insn first, because there's no point
1254 including more than one in the machine description unless
1255 the more limited one has some advantage. */
1257 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1258 mode = GET_MODE_WIDER_MODE (mode))
1260 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1262 if (code != CODE_FOR_nothing
1263 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1264 here because if SIZE is less than the mode mask, as it is
1265 returned by the macro, it will definitely be less than the
1266 actual mode mask. Since SIZE is within the Pmode address
1267 space, we limit MODE to Pmode. */
1268 && ((CONST_INT_P (size)
1269 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1270 <= (GET_MODE_MASK (mode) >> 1)))
1271 || max_size <= (GET_MODE_MASK (mode) >> 1)
1272 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1274 struct expand_operand ops[9];
1275 unsigned int nops;
1277 /* ??? When called via emit_block_move_for_call, it'd be
1278 nice if there were some way to inform the backend, so
1279 that it doesn't fail the expansion because it thinks
1280 emitting the libcall would be more efficient. */
1281 nops = insn_data[(int) code].n_generator_args;
1282 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1284 create_fixed_operand (&ops[0], x);
1285 create_fixed_operand (&ops[1], y);
1286 /* The check above guarantees that this size conversion is valid. */
1287 create_convert_operand_to (&ops[2], size, mode, true);
1288 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1289 if (nops >= 6)
1291 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1292 create_integer_operand (&ops[5], expected_size);
1294 if (nops >= 8)
1296 create_integer_operand (&ops[6], min_size);
1297 /* If we can not represent the maximal size,
1298 make parameter NULL. */
1299 if ((HOST_WIDE_INT) max_size != -1)
1300 create_integer_operand (&ops[7], max_size);
1301 else
1302 create_fixed_operand (&ops[7], NULL);
1304 if (nops == 9)
1306 /* If we can not represent the maximal size,
1307 make parameter NULL. */
1308 if ((HOST_WIDE_INT) probable_max_size != -1)
1309 create_integer_operand (&ops[8], probable_max_size);
1310 else
1311 create_fixed_operand (&ops[8], NULL);
1313 if (maybe_expand_insn (code, nops, ops))
1315 volatile_ok = save_volatile_ok;
1316 return true;
1321 volatile_ok = save_volatile_ok;
1322 return false;
1325 /* A subroutine of emit_block_move. Expand a call to memcpy.
1326 Return the return value from memcpy, 0 otherwise. */
1329 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1331 rtx dst_addr, src_addr;
1332 tree call_expr, fn, src_tree, dst_tree, size_tree;
1333 machine_mode size_mode;
1334 rtx retval;
1336 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1337 pseudos. We can then place those new pseudos into a VAR_DECL and
1338 use them later. */
1340 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1341 src_addr = copy_addr_to_reg (XEXP (src, 0));
1343 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1344 src_addr = convert_memory_address (ptr_mode, src_addr);
1346 dst_tree = make_tree (ptr_type_node, dst_addr);
1347 src_tree = make_tree (ptr_type_node, src_addr);
1349 size_mode = TYPE_MODE (sizetype);
1351 size = convert_to_mode (size_mode, size, 1);
1352 size = copy_to_mode_reg (size_mode, size);
1354 /* It is incorrect to use the libcall calling conventions to call
1355 memcpy in this context. This could be a user call to memcpy and
1356 the user may wish to examine the return value from memcpy. For
1357 targets where libcalls and normal calls have different conventions
1358 for returning pointers, we could end up generating incorrect code. */
1360 size_tree = make_tree (sizetype, size);
1362 fn = emit_block_move_libcall_fn (true);
1363 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1364 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1366 retval = expand_normal (call_expr);
1368 return retval;
1371 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1372 for the function we use for block copies. */
1374 static GTY(()) tree block_move_fn;
1376 void
1377 init_block_move_fn (const char *asmspec)
1379 if (!block_move_fn)
1381 tree args, fn, attrs, attr_args;
1383 fn = get_identifier ("memcpy");
1384 args = build_function_type_list (ptr_type_node, ptr_type_node,
1385 const_ptr_type_node, sizetype,
1386 NULL_TREE);
1388 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1389 DECL_EXTERNAL (fn) = 1;
1390 TREE_PUBLIC (fn) = 1;
1391 DECL_ARTIFICIAL (fn) = 1;
1392 TREE_NOTHROW (fn) = 1;
1393 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1394 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1396 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1397 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1399 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1401 block_move_fn = fn;
1404 if (asmspec)
1405 set_user_assembler_name (block_move_fn, asmspec);
1408 static tree
1409 emit_block_move_libcall_fn (int for_call)
1411 static bool emitted_extern;
1413 if (!block_move_fn)
1414 init_block_move_fn (NULL);
1416 if (for_call && !emitted_extern)
1418 emitted_extern = true;
1419 make_decl_rtl (block_move_fn);
1422 return block_move_fn;
1425 /* A subroutine of emit_block_move. Copy the data via an explicit
1426 loop. This is used only when libcalls are forbidden. */
1427 /* ??? It'd be nice to copy in hunks larger than QImode. */
1429 static void
1430 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1431 unsigned int align ATTRIBUTE_UNUSED)
1433 rtx_code_label *cmp_label, *top_label;
1434 rtx iter, x_addr, y_addr, tmp;
1435 machine_mode x_addr_mode = get_address_mode (x);
1436 machine_mode y_addr_mode = get_address_mode (y);
1437 machine_mode iter_mode;
1439 iter_mode = GET_MODE (size);
1440 if (iter_mode == VOIDmode)
1441 iter_mode = word_mode;
1443 top_label = gen_label_rtx ();
1444 cmp_label = gen_label_rtx ();
1445 iter = gen_reg_rtx (iter_mode);
1447 emit_move_insn (iter, const0_rtx);
1449 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1450 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1451 do_pending_stack_adjust ();
1453 emit_jump (cmp_label);
1454 emit_label (top_label);
1456 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1457 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1459 if (x_addr_mode != y_addr_mode)
1460 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1461 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1463 x = change_address (x, QImode, x_addr);
1464 y = change_address (y, QImode, y_addr);
1466 emit_move_insn (x, y);
1468 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1469 true, OPTAB_LIB_WIDEN);
1470 if (tmp != iter)
1471 emit_move_insn (iter, tmp);
1473 emit_label (cmp_label);
1475 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1476 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1479 /* Copy all or part of a value X into registers starting at REGNO.
1480 The number of registers to be filled is NREGS. */
1482 void
1483 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1485 if (nregs == 0)
1486 return;
1488 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1489 x = validize_mem (force_const_mem (mode, x));
1491 /* See if the machine can do this with a load multiple insn. */
1492 if (targetm.have_load_multiple ())
1494 rtx_insn *last = get_last_insn ();
1495 rtx first = gen_rtx_REG (word_mode, regno);
1496 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1497 GEN_INT (nregs)))
1499 emit_insn (pat);
1500 return;
1502 else
1503 delete_insns_since (last);
1506 for (int i = 0; i < nregs; i++)
1507 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1508 operand_subword_force (x, i, mode));
1511 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1512 The number of registers to be filled is NREGS. */
1514 void
1515 move_block_from_reg (int regno, rtx x, int nregs)
1517 if (nregs == 0)
1518 return;
1520 /* See if the machine can do this with a store multiple insn. */
1521 if (targetm.have_store_multiple ())
1523 rtx_insn *last = get_last_insn ();
1524 rtx first = gen_rtx_REG (word_mode, regno);
1525 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
1526 GEN_INT (nregs)))
1528 emit_insn (pat);
1529 return;
1531 else
1532 delete_insns_since (last);
1535 for (int i = 0; i < nregs; i++)
1537 rtx tem = operand_subword (x, i, 1, BLKmode);
1539 gcc_assert (tem);
1541 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1545 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1546 ORIG, where ORIG is a non-consecutive group of registers represented by
1547 a PARALLEL. The clone is identical to the original except in that the
1548 original set of registers is replaced by a new set of pseudo registers.
1549 The new set has the same modes as the original set. */
1552 gen_group_rtx (rtx orig)
1554 int i, length;
1555 rtx *tmps;
1557 gcc_assert (GET_CODE (orig) == PARALLEL);
1559 length = XVECLEN (orig, 0);
1560 tmps = XALLOCAVEC (rtx, length);
1562 /* Skip a NULL entry in first slot. */
1563 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1565 if (i)
1566 tmps[0] = 0;
1568 for (; i < length; i++)
1570 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1571 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1573 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1576 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1579 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1580 except that values are placed in TMPS[i], and must later be moved
1581 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1583 static void
1584 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1586 rtx src;
1587 int start, i;
1588 machine_mode m = GET_MODE (orig_src);
1590 gcc_assert (GET_CODE (dst) == PARALLEL);
1592 if (m != VOIDmode
1593 && !SCALAR_INT_MODE_P (m)
1594 && !MEM_P (orig_src)
1595 && GET_CODE (orig_src) != CONCAT)
1597 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1598 if (imode == BLKmode)
1599 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1600 else
1601 src = gen_reg_rtx (imode);
1602 if (imode != BLKmode)
1603 src = gen_lowpart (GET_MODE (orig_src), src);
1604 emit_move_insn (src, orig_src);
1605 /* ...and back again. */
1606 if (imode != BLKmode)
1607 src = gen_lowpart (imode, src);
1608 emit_group_load_1 (tmps, dst, src, type, ssize);
1609 return;
1612 /* Check for a NULL entry, used to indicate that the parameter goes
1613 both on the stack and in registers. */
1614 if (XEXP (XVECEXP (dst, 0, 0), 0))
1615 start = 0;
1616 else
1617 start = 1;
1619 /* Process the pieces. */
1620 for (i = start; i < XVECLEN (dst, 0); i++)
1622 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1623 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1624 unsigned int bytelen = GET_MODE_SIZE (mode);
1625 int shift = 0;
1627 /* Handle trailing fragments that run over the size of the struct. */
1628 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1630 /* Arrange to shift the fragment to where it belongs.
1631 extract_bit_field loads to the lsb of the reg. */
1632 if (
1633 #ifdef BLOCK_REG_PADDING
1634 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1635 == (BYTES_BIG_ENDIAN ? upward : downward)
1636 #else
1637 BYTES_BIG_ENDIAN
1638 #endif
1640 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1641 bytelen = ssize - bytepos;
1642 gcc_assert (bytelen > 0);
1645 /* If we won't be loading directly from memory, protect the real source
1646 from strange tricks we might play; but make sure that the source can
1647 be loaded directly into the destination. */
1648 src = orig_src;
1649 if (!MEM_P (orig_src)
1650 && (!CONSTANT_P (orig_src)
1651 || (GET_MODE (orig_src) != mode
1652 && GET_MODE (orig_src) != VOIDmode)))
1654 if (GET_MODE (orig_src) == VOIDmode)
1655 src = gen_reg_rtx (mode);
1656 else
1657 src = gen_reg_rtx (GET_MODE (orig_src));
1659 emit_move_insn (src, orig_src);
1662 /* Optimize the access just a bit. */
1663 if (MEM_P (src)
1664 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1665 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1666 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1667 && bytelen == GET_MODE_SIZE (mode))
1669 tmps[i] = gen_reg_rtx (mode);
1670 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1672 else if (COMPLEX_MODE_P (mode)
1673 && GET_MODE (src) == mode
1674 && bytelen == GET_MODE_SIZE (mode))
1675 /* Let emit_move_complex do the bulk of the work. */
1676 tmps[i] = src;
1677 else if (GET_CODE (src) == CONCAT)
1679 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1680 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1682 if ((bytepos == 0 && bytelen == slen0)
1683 || (bytepos != 0 && bytepos + bytelen <= slen))
1685 /* The following assumes that the concatenated objects all
1686 have the same size. In this case, a simple calculation
1687 can be used to determine the object and the bit field
1688 to be extracted. */
1689 tmps[i] = XEXP (src, bytepos / slen0);
1690 if (! CONSTANT_P (tmps[i])
1691 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1692 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1693 (bytepos % slen0) * BITS_PER_UNIT,
1694 1, NULL_RTX, mode, mode);
1696 else
1698 rtx mem;
1700 gcc_assert (!bytepos);
1701 mem = assign_stack_temp (GET_MODE (src), slen);
1702 emit_move_insn (mem, src);
1703 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1704 0, 1, NULL_RTX, mode, mode);
1707 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1708 SIMD register, which is currently broken. While we get GCC
1709 to emit proper RTL for these cases, let's dump to memory. */
1710 else if (VECTOR_MODE_P (GET_MODE (dst))
1711 && REG_P (src))
1713 int slen = GET_MODE_SIZE (GET_MODE (src));
1714 rtx mem;
1716 mem = assign_stack_temp (GET_MODE (src), slen);
1717 emit_move_insn (mem, src);
1718 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1720 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1721 && XVECLEN (dst, 0) > 1)
1722 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1723 else if (CONSTANT_P (src))
1725 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1727 if (len == ssize)
1728 tmps[i] = src;
1729 else
1731 rtx first, second;
1733 /* TODO: const_wide_int can have sizes other than this... */
1734 gcc_assert (2 * len == ssize);
1735 split_double (src, &first, &second);
1736 if (i)
1737 tmps[i] = second;
1738 else
1739 tmps[i] = first;
1742 else if (REG_P (src) && GET_MODE (src) == mode)
1743 tmps[i] = src;
1744 else
1745 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1746 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1747 mode, mode);
1749 if (shift)
1750 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1751 shift, tmps[i], 0);
1755 /* Emit code to move a block SRC of type TYPE to a block DST,
1756 where DST is non-consecutive registers represented by a PARALLEL.
1757 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1758 if not known. */
1760 void
1761 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1763 rtx *tmps;
1764 int i;
1766 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1767 emit_group_load_1 (tmps, dst, src, type, ssize);
1769 /* Copy the extracted pieces into the proper (probable) hard regs. */
1770 for (i = 0; i < XVECLEN (dst, 0); i++)
1772 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1773 if (d == NULL)
1774 continue;
1775 emit_move_insn (d, tmps[i]);
1779 /* Similar, but load SRC into new pseudos in a format that looks like
1780 PARALLEL. This can later be fed to emit_group_move to get things
1781 in the right place. */
1784 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1786 rtvec vec;
1787 int i;
1789 vec = rtvec_alloc (XVECLEN (parallel, 0));
1790 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1792 /* Convert the vector to look just like the original PARALLEL, except
1793 with the computed values. */
1794 for (i = 0; i < XVECLEN (parallel, 0); i++)
1796 rtx e = XVECEXP (parallel, 0, i);
1797 rtx d = XEXP (e, 0);
1799 if (d)
1801 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1802 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1804 RTVEC_ELT (vec, i) = e;
1807 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1810 /* Emit code to move a block SRC to block DST, where SRC and DST are
1811 non-consecutive groups of registers, each represented by a PARALLEL. */
1813 void
1814 emit_group_move (rtx dst, rtx src)
1816 int i;
1818 gcc_assert (GET_CODE (src) == PARALLEL
1819 && GET_CODE (dst) == PARALLEL
1820 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1822 /* Skip first entry if NULL. */
1823 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1824 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1825 XEXP (XVECEXP (src, 0, i), 0));
1828 /* Move a group of registers represented by a PARALLEL into pseudos. */
1831 emit_group_move_into_temps (rtx src)
1833 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1834 int i;
1836 for (i = 0; i < XVECLEN (src, 0); i++)
1838 rtx e = XVECEXP (src, 0, i);
1839 rtx d = XEXP (e, 0);
1841 if (d)
1842 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1843 RTVEC_ELT (vec, i) = e;
1846 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1849 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1850 where SRC is non-consecutive registers represented by a PARALLEL.
1851 SSIZE represents the total size of block ORIG_DST, or -1 if not
1852 known. */
1854 void
1855 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1857 rtx *tmps, dst;
1858 int start, finish, i;
1859 machine_mode m = GET_MODE (orig_dst);
1861 gcc_assert (GET_CODE (src) == PARALLEL);
1863 if (!SCALAR_INT_MODE_P (m)
1864 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1866 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1867 if (imode == BLKmode)
1868 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1869 else
1870 dst = gen_reg_rtx (imode);
1871 emit_group_store (dst, src, type, ssize);
1872 if (imode != BLKmode)
1873 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1874 emit_move_insn (orig_dst, dst);
1875 return;
1878 /* Check for a NULL entry, used to indicate that the parameter goes
1879 both on the stack and in registers. */
1880 if (XEXP (XVECEXP (src, 0, 0), 0))
1881 start = 0;
1882 else
1883 start = 1;
1884 finish = XVECLEN (src, 0);
1886 tmps = XALLOCAVEC (rtx, finish);
1888 /* Copy the (probable) hard regs into pseudos. */
1889 for (i = start; i < finish; i++)
1891 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1892 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1894 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1895 emit_move_insn (tmps[i], reg);
1897 else
1898 tmps[i] = reg;
1901 /* If we won't be storing directly into memory, protect the real destination
1902 from strange tricks we might play. */
1903 dst = orig_dst;
1904 if (GET_CODE (dst) == PARALLEL)
1906 rtx temp;
1908 /* We can get a PARALLEL dst if there is a conditional expression in
1909 a return statement. In that case, the dst and src are the same,
1910 so no action is necessary. */
1911 if (rtx_equal_p (dst, src))
1912 return;
1914 /* It is unclear if we can ever reach here, but we may as well handle
1915 it. Allocate a temporary, and split this into a store/load to/from
1916 the temporary. */
1917 temp = assign_stack_temp (GET_MODE (dst), ssize);
1918 emit_group_store (temp, src, type, ssize);
1919 emit_group_load (dst, temp, type, ssize);
1920 return;
1922 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1924 machine_mode outer = GET_MODE (dst);
1925 machine_mode inner;
1926 HOST_WIDE_INT bytepos;
1927 bool done = false;
1928 rtx temp;
1930 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1931 dst = gen_reg_rtx (outer);
1933 /* Make life a bit easier for combine. */
1934 /* If the first element of the vector is the low part
1935 of the destination mode, use a paradoxical subreg to
1936 initialize the destination. */
1937 if (start < finish)
1939 inner = GET_MODE (tmps[start]);
1940 bytepos = subreg_lowpart_offset (inner, outer);
1941 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1943 temp = simplify_gen_subreg (outer, tmps[start],
1944 inner, 0);
1945 if (temp)
1947 emit_move_insn (dst, temp);
1948 done = true;
1949 start++;
1954 /* If the first element wasn't the low part, try the last. */
1955 if (!done
1956 && start < finish - 1)
1958 inner = GET_MODE (tmps[finish - 1]);
1959 bytepos = subreg_lowpart_offset (inner, outer);
1960 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1962 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1963 inner, 0);
1964 if (temp)
1966 emit_move_insn (dst, temp);
1967 done = true;
1968 finish--;
1973 /* Otherwise, simply initialize the result to zero. */
1974 if (!done)
1975 emit_move_insn (dst, CONST0_RTX (outer));
1978 /* Process the pieces. */
1979 for (i = start; i < finish; i++)
1981 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
1982 machine_mode mode = GET_MODE (tmps[i]);
1983 unsigned int bytelen = GET_MODE_SIZE (mode);
1984 unsigned int adj_bytelen;
1985 rtx dest = dst;
1987 /* Handle trailing fragments that run over the size of the struct. */
1988 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1989 adj_bytelen = ssize - bytepos;
1990 else
1991 adj_bytelen = bytelen;
1993 if (GET_CODE (dst) == CONCAT)
1995 if (bytepos + adj_bytelen
1996 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
1997 dest = XEXP (dst, 0);
1998 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2000 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2001 dest = XEXP (dst, 1);
2003 else
2005 machine_mode dest_mode = GET_MODE (dest);
2006 machine_mode tmp_mode = GET_MODE (tmps[i]);
2008 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2010 if (GET_MODE_ALIGNMENT (dest_mode)
2011 >= GET_MODE_ALIGNMENT (tmp_mode))
2013 dest = assign_stack_temp (dest_mode,
2014 GET_MODE_SIZE (dest_mode));
2015 emit_move_insn (adjust_address (dest,
2016 tmp_mode,
2017 bytepos),
2018 tmps[i]);
2019 dst = dest;
2021 else
2023 dest = assign_stack_temp (tmp_mode,
2024 GET_MODE_SIZE (tmp_mode));
2025 emit_move_insn (dest, tmps[i]);
2026 dst = adjust_address (dest, dest_mode, bytepos);
2028 break;
2032 /* Handle trailing fragments that run over the size of the struct. */
2033 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2035 /* store_bit_field always takes its value from the lsb.
2036 Move the fragment to the lsb if it's not already there. */
2037 if (
2038 #ifdef BLOCK_REG_PADDING
2039 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2040 == (BYTES_BIG_ENDIAN ? upward : downward)
2041 #else
2042 BYTES_BIG_ENDIAN
2043 #endif
2046 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2047 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2048 shift, tmps[i], 0);
2051 /* Make sure not to write past the end of the struct. */
2052 store_bit_field (dest,
2053 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2054 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2055 VOIDmode, tmps[i]);
2058 /* Optimize the access just a bit. */
2059 else if (MEM_P (dest)
2060 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2061 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2062 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2063 && bytelen == GET_MODE_SIZE (mode))
2064 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2066 else
2067 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2068 0, 0, mode, tmps[i]);
2071 /* Copy from the pseudo into the (probable) hard reg. */
2072 if (orig_dst != dst)
2073 emit_move_insn (orig_dst, dst);
2076 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2077 of the value stored in X. */
2080 maybe_emit_group_store (rtx x, tree type)
2082 machine_mode mode = TYPE_MODE (type);
2083 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2084 if (GET_CODE (x) == PARALLEL)
2086 rtx result = gen_reg_rtx (mode);
2087 emit_group_store (result, x, type, int_size_in_bytes (type));
2088 return result;
2090 return x;
2093 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2095 This is used on targets that return BLKmode values in registers. */
2097 void
2098 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2100 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2101 rtx src = NULL, dst = NULL;
2102 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2103 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2104 machine_mode mode = GET_MODE (srcreg);
2105 machine_mode tmode = GET_MODE (target);
2106 machine_mode copy_mode;
2108 /* BLKmode registers created in the back-end shouldn't have survived. */
2109 gcc_assert (mode != BLKmode);
2111 /* If the structure doesn't take up a whole number of words, see whether
2112 SRCREG is padded on the left or on the right. If it's on the left,
2113 set PADDING_CORRECTION to the number of bits to skip.
2115 In most ABIs, the structure will be returned at the least end of
2116 the register, which translates to right padding on little-endian
2117 targets and left padding on big-endian targets. The opposite
2118 holds if the structure is returned at the most significant
2119 end of the register. */
2120 if (bytes % UNITS_PER_WORD != 0
2121 && (targetm.calls.return_in_msb (type)
2122 ? !BYTES_BIG_ENDIAN
2123 : BYTES_BIG_ENDIAN))
2124 padding_correction
2125 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2127 /* We can use a single move if we have an exact mode for the size. */
2128 else if (MEM_P (target)
2129 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2130 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2131 && bytes == GET_MODE_SIZE (mode))
2133 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2134 return;
2137 /* And if we additionally have the same mode for a register. */
2138 else if (REG_P (target)
2139 && GET_MODE (target) == mode
2140 && bytes == GET_MODE_SIZE (mode))
2142 emit_move_insn (target, srcreg);
2143 return;
2146 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2147 into a new pseudo which is a full word. */
2148 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2150 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2151 mode = word_mode;
2154 /* Copy the structure BITSIZE bits at a time. If the target lives in
2155 memory, take care of not reading/writing past its end by selecting
2156 a copy mode suited to BITSIZE. This should always be possible given
2157 how it is computed.
2159 If the target lives in register, make sure not to select a copy mode
2160 larger than the mode of the register.
2162 We could probably emit more efficient code for machines which do not use
2163 strict alignment, but it doesn't seem worth the effort at the current
2164 time. */
2166 copy_mode = word_mode;
2167 if (MEM_P (target))
2169 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2170 if (mem_mode != BLKmode)
2171 copy_mode = mem_mode;
2173 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2174 copy_mode = tmode;
2176 for (bitpos = 0, xbitpos = padding_correction;
2177 bitpos < bytes * BITS_PER_UNIT;
2178 bitpos += bitsize, xbitpos += bitsize)
2180 /* We need a new source operand each time xbitpos is on a
2181 word boundary and when xbitpos == padding_correction
2182 (the first time through). */
2183 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2184 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2186 /* We need a new destination operand each time bitpos is on
2187 a word boundary. */
2188 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2189 dst = target;
2190 else if (bitpos % BITS_PER_WORD == 0)
2191 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2193 /* Use xbitpos for the source extraction (right justified) and
2194 bitpos for the destination store (left justified). */
2195 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2196 extract_bit_field (src, bitsize,
2197 xbitpos % BITS_PER_WORD, 1,
2198 NULL_RTX, copy_mode, copy_mode));
2202 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2203 register if it contains any data, otherwise return null.
2205 This is used on targets that return BLKmode values in registers. */
2208 copy_blkmode_to_reg (machine_mode mode, tree src)
2210 int i, n_regs;
2211 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2212 unsigned int bitsize;
2213 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2214 machine_mode dst_mode;
2216 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2218 x = expand_normal (src);
2220 bytes = int_size_in_bytes (TREE_TYPE (src));
2221 if (bytes == 0)
2222 return NULL_RTX;
2224 /* If the structure doesn't take up a whole number of words, see
2225 whether the register value should be padded on the left or on
2226 the right. Set PADDING_CORRECTION to the number of padding
2227 bits needed on the left side.
2229 In most ABIs, the structure will be returned at the least end of
2230 the register, which translates to right padding on little-endian
2231 targets and left padding on big-endian targets. The opposite
2232 holds if the structure is returned at the most significant
2233 end of the register. */
2234 if (bytes % UNITS_PER_WORD != 0
2235 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2236 ? !BYTES_BIG_ENDIAN
2237 : BYTES_BIG_ENDIAN))
2238 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2239 * BITS_PER_UNIT));
2241 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2242 dst_words = XALLOCAVEC (rtx, n_regs);
2243 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2245 /* Copy the structure BITSIZE bits at a time. */
2246 for (bitpos = 0, xbitpos = padding_correction;
2247 bitpos < bytes * BITS_PER_UNIT;
2248 bitpos += bitsize, xbitpos += bitsize)
2250 /* We need a new destination pseudo each time xbitpos is
2251 on a word boundary and when xbitpos == padding_correction
2252 (the first time through). */
2253 if (xbitpos % BITS_PER_WORD == 0
2254 || xbitpos == padding_correction)
2256 /* Generate an appropriate register. */
2257 dst_word = gen_reg_rtx (word_mode);
2258 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2260 /* Clear the destination before we move anything into it. */
2261 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2264 /* We need a new source operand each time bitpos is on a word
2265 boundary. */
2266 if (bitpos % BITS_PER_WORD == 0)
2267 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2269 /* Use bitpos for the source extraction (left justified) and
2270 xbitpos for the destination store (right justified). */
2271 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2272 0, 0, word_mode,
2273 extract_bit_field (src_word, bitsize,
2274 bitpos % BITS_PER_WORD, 1,
2275 NULL_RTX, word_mode, word_mode));
2278 if (mode == BLKmode)
2280 /* Find the smallest integer mode large enough to hold the
2281 entire structure. */
2282 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2283 mode != VOIDmode;
2284 mode = GET_MODE_WIDER_MODE (mode))
2285 /* Have we found a large enough mode? */
2286 if (GET_MODE_SIZE (mode) >= bytes)
2287 break;
2289 /* A suitable mode should have been found. */
2290 gcc_assert (mode != VOIDmode);
2293 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2294 dst_mode = word_mode;
2295 else
2296 dst_mode = mode;
2297 dst = gen_reg_rtx (dst_mode);
2299 for (i = 0; i < n_regs; i++)
2300 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2302 if (mode != dst_mode)
2303 dst = gen_lowpart (mode, dst);
2305 return dst;
2308 /* Add a USE expression for REG to the (possibly empty) list pointed
2309 to by CALL_FUSAGE. REG must denote a hard register. */
2311 void
2312 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2314 gcc_assert (REG_P (reg));
2316 if (!HARD_REGISTER_P (reg))
2317 return;
2319 *call_fusage
2320 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2323 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2324 to by CALL_FUSAGE. REG must denote a hard register. */
2326 void
2327 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2329 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2331 *call_fusage
2332 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2335 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2336 starting at REGNO. All of these registers must be hard registers. */
2338 void
2339 use_regs (rtx *call_fusage, int regno, int nregs)
2341 int i;
2343 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2345 for (i = 0; i < nregs; i++)
2346 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2349 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2350 PARALLEL REGS. This is for calls that pass values in multiple
2351 non-contiguous locations. The Irix 6 ABI has examples of this. */
2353 void
2354 use_group_regs (rtx *call_fusage, rtx regs)
2356 int i;
2358 for (i = 0; i < XVECLEN (regs, 0); i++)
2360 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2362 /* A NULL entry means the parameter goes both on the stack and in
2363 registers. This can also be a MEM for targets that pass values
2364 partially on the stack and partially in registers. */
2365 if (reg != 0 && REG_P (reg))
2366 use_reg (call_fusage, reg);
2370 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2371 assigment and the code of the expresion on the RHS is CODE. Return
2372 NULL otherwise. */
2374 static gimple *
2375 get_def_for_expr (tree name, enum tree_code code)
2377 gimple *def_stmt;
2379 if (TREE_CODE (name) != SSA_NAME)
2380 return NULL;
2382 def_stmt = get_gimple_for_ssa_name (name);
2383 if (!def_stmt
2384 || gimple_assign_rhs_code (def_stmt) != code)
2385 return NULL;
2387 return def_stmt;
2390 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2391 assigment and the class of the expresion on the RHS is CLASS. Return
2392 NULL otherwise. */
2394 static gimple *
2395 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2397 gimple *def_stmt;
2399 if (TREE_CODE (name) != SSA_NAME)
2400 return NULL;
2402 def_stmt = get_gimple_for_ssa_name (name);
2403 if (!def_stmt
2404 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2405 return NULL;
2407 return def_stmt;
2411 /* Determine whether the LEN bytes generated by CONSTFUN can be
2412 stored to memory using several move instructions. CONSTFUNDATA is
2413 a pointer which will be passed as argument in every CONSTFUN call.
2414 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2415 a memset operation and false if it's a copy of a constant string.
2416 Return nonzero if a call to store_by_pieces should succeed. */
2419 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2420 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2421 void *constfundata, unsigned int align, bool memsetp)
2423 unsigned HOST_WIDE_INT l;
2424 unsigned int max_size;
2425 HOST_WIDE_INT offset = 0;
2426 machine_mode mode;
2427 enum insn_code icode;
2428 int reverse;
2429 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2430 rtx cst ATTRIBUTE_UNUSED;
2432 if (len == 0)
2433 return 1;
2435 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2436 memsetp
2437 ? SET_BY_PIECES
2438 : STORE_BY_PIECES,
2439 optimize_insn_for_speed_p ()))
2440 return 0;
2442 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2444 /* We would first store what we can in the largest integer mode, then go to
2445 successively smaller modes. */
2447 for (reverse = 0;
2448 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2449 reverse++)
2451 l = len;
2452 max_size = STORE_MAX_PIECES + 1;
2453 while (max_size > 1 && l > 0)
2455 mode = widest_int_mode_for_size (max_size);
2457 if (mode == VOIDmode)
2458 break;
2460 icode = optab_handler (mov_optab, mode);
2461 if (icode != CODE_FOR_nothing
2462 && align >= GET_MODE_ALIGNMENT (mode))
2464 unsigned int size = GET_MODE_SIZE (mode);
2466 while (l >= size)
2468 if (reverse)
2469 offset -= size;
2471 cst = (*constfun) (constfundata, offset, mode);
2472 if (!targetm.legitimate_constant_p (mode, cst))
2473 return 0;
2475 if (!reverse)
2476 offset += size;
2478 l -= size;
2482 max_size = GET_MODE_SIZE (mode);
2485 /* The code above should have handled everything. */
2486 gcc_assert (!l);
2489 return 1;
2492 /* Generate several move instructions to store LEN bytes generated by
2493 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2494 pointer which will be passed as argument in every CONSTFUN call.
2495 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2496 a memset operation and false if it's a copy of a constant string.
2497 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2498 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2499 stpcpy. */
2502 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2503 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2504 void *constfundata, unsigned int align, bool memsetp, int endp)
2506 machine_mode to_addr_mode = get_address_mode (to);
2507 struct store_by_pieces_d data;
2509 if (len == 0)
2511 gcc_assert (endp != 2);
2512 return to;
2515 gcc_assert (targetm.use_by_pieces_infrastructure_p
2516 (len, align,
2517 memsetp
2518 ? SET_BY_PIECES
2519 : STORE_BY_PIECES,
2520 optimize_insn_for_speed_p ()));
2522 data.constfun = constfun;
2523 data.constfundata = constfundata;
2524 data.len = len;
2525 data.to = to;
2526 store_by_pieces_1 (&data, align);
2527 if (endp)
2529 rtx to1;
2531 gcc_assert (!data.reverse);
2532 if (data.autinc_to)
2534 if (endp == 2)
2536 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2537 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2538 else
2539 data.to_addr = copy_to_mode_reg (to_addr_mode,
2540 plus_constant (to_addr_mode,
2541 data.to_addr,
2542 -1));
2544 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2545 data.offset);
2547 else
2549 if (endp == 2)
2550 --data.offset;
2551 to1 = adjust_address (data.to, QImode, data.offset);
2553 return to1;
2555 else
2556 return data.to;
2559 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2560 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2562 static void
2563 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2565 struct store_by_pieces_d data;
2567 if (len == 0)
2568 return;
2570 data.constfun = clear_by_pieces_1;
2571 data.constfundata = NULL;
2572 data.len = len;
2573 data.to = to;
2574 store_by_pieces_1 (&data, align);
2577 /* Callback routine for clear_by_pieces.
2578 Return const0_rtx unconditionally. */
2580 static rtx
2581 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2582 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2583 machine_mode mode ATTRIBUTE_UNUSED)
2585 return const0_rtx;
2588 /* Subroutine of clear_by_pieces and store_by_pieces.
2589 Generate several move instructions to store LEN bytes of block TO. (A MEM
2590 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2592 static void
2593 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2594 unsigned int align ATTRIBUTE_UNUSED)
2596 machine_mode to_addr_mode = get_address_mode (data->to);
2597 rtx to_addr = XEXP (data->to, 0);
2598 unsigned int max_size = STORE_MAX_PIECES + 1;
2599 enum insn_code icode;
2601 data->offset = 0;
2602 data->to_addr = to_addr;
2603 data->autinc_to
2604 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2605 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2607 data->explicit_inc_to = 0;
2608 data->reverse
2609 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2610 if (data->reverse)
2611 data->offset = data->len;
2613 /* If storing requires more than two move insns,
2614 copy addresses to registers (to make displacements shorter)
2615 and use post-increment if available. */
2616 if (!data->autinc_to
2617 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2619 /* Determine the main mode we'll be using.
2620 MODE might not be used depending on the definitions of the
2621 USE_* macros below. */
2622 machine_mode mode ATTRIBUTE_UNUSED
2623 = widest_int_mode_for_size (max_size);
2625 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2627 data->to_addr = copy_to_mode_reg (to_addr_mode,
2628 plus_constant (to_addr_mode,
2629 to_addr,
2630 data->len));
2631 data->autinc_to = 1;
2632 data->explicit_inc_to = -1;
2635 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2636 && ! data->autinc_to)
2638 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2639 data->autinc_to = 1;
2640 data->explicit_inc_to = 1;
2643 if ( !data->autinc_to && CONSTANT_P (to_addr))
2644 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2647 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2649 /* First store what we can in the largest integer mode, then go to
2650 successively smaller modes. */
2652 while (max_size > 1 && data->len > 0)
2654 machine_mode mode = widest_int_mode_for_size (max_size);
2656 if (mode == VOIDmode)
2657 break;
2659 icode = optab_handler (mov_optab, mode);
2660 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2661 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2663 max_size = GET_MODE_SIZE (mode);
2666 /* The code above should have handled everything. */
2667 gcc_assert (!data->len);
2670 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2671 with move instructions for mode MODE. GENFUN is the gen_... function
2672 to make a move insn for that mode. DATA has all the other info. */
2674 static void
2675 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2676 struct store_by_pieces_d *data)
2678 unsigned int size = GET_MODE_SIZE (mode);
2679 rtx to1, cst;
2681 while (data->len >= size)
2683 if (data->reverse)
2684 data->offset -= size;
2686 if (data->autinc_to)
2687 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2688 data->offset);
2689 else
2690 to1 = adjust_address (data->to, mode, data->offset);
2692 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2693 emit_insn (gen_add2_insn (data->to_addr,
2694 gen_int_mode (-(HOST_WIDE_INT) size,
2695 GET_MODE (data->to_addr))));
2697 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2698 emit_insn ((*genfun) (to1, cst));
2700 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2701 emit_insn (gen_add2_insn (data->to_addr,
2702 gen_int_mode (size,
2703 GET_MODE (data->to_addr))));
2705 if (! data->reverse)
2706 data->offset += size;
2708 data->len -= size;
2712 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2713 its length in bytes. */
2716 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2717 unsigned int expected_align, HOST_WIDE_INT expected_size,
2718 unsigned HOST_WIDE_INT min_size,
2719 unsigned HOST_WIDE_INT max_size,
2720 unsigned HOST_WIDE_INT probable_max_size)
2722 machine_mode mode = GET_MODE (object);
2723 unsigned int align;
2725 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2727 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2728 just move a zero. Otherwise, do this a piece at a time. */
2729 if (mode != BLKmode
2730 && CONST_INT_P (size)
2731 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2733 rtx zero = CONST0_RTX (mode);
2734 if (zero != NULL)
2736 emit_move_insn (object, zero);
2737 return NULL;
2740 if (COMPLEX_MODE_P (mode))
2742 zero = CONST0_RTX (GET_MODE_INNER (mode));
2743 if (zero != NULL)
2745 write_complex_part (object, zero, 0);
2746 write_complex_part (object, zero, 1);
2747 return NULL;
2752 if (size == const0_rtx)
2753 return NULL;
2755 align = MEM_ALIGN (object);
2757 if (CONST_INT_P (size)
2758 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2759 CLEAR_BY_PIECES,
2760 optimize_insn_for_speed_p ()))
2761 clear_by_pieces (object, INTVAL (size), align);
2762 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2763 expected_align, expected_size,
2764 min_size, max_size, probable_max_size))
2766 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2767 return set_storage_via_libcall (object, size, const0_rtx,
2768 method == BLOCK_OP_TAILCALL);
2769 else
2770 gcc_unreachable ();
2772 return NULL;
2776 clear_storage (rtx object, rtx size, enum block_op_methods method)
2778 unsigned HOST_WIDE_INT max, min = 0;
2779 if (GET_CODE (size) == CONST_INT)
2780 min = max = UINTVAL (size);
2781 else
2782 max = GET_MODE_MASK (GET_MODE (size));
2783 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2787 /* A subroutine of clear_storage. Expand a call to memset.
2788 Return the return value of memset, 0 otherwise. */
2791 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2793 tree call_expr, fn, object_tree, size_tree, val_tree;
2794 machine_mode size_mode;
2795 rtx retval;
2797 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2798 place those into new pseudos into a VAR_DECL and use them later. */
2800 object = copy_addr_to_reg (XEXP (object, 0));
2802 size_mode = TYPE_MODE (sizetype);
2803 size = convert_to_mode (size_mode, size, 1);
2804 size = copy_to_mode_reg (size_mode, size);
2806 /* It is incorrect to use the libcall calling conventions to call
2807 memset in this context. This could be a user call to memset and
2808 the user may wish to examine the return value from memset. For
2809 targets where libcalls and normal calls have different conventions
2810 for returning pointers, we could end up generating incorrect code. */
2812 object_tree = make_tree (ptr_type_node, object);
2813 if (!CONST_INT_P (val))
2814 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2815 size_tree = make_tree (sizetype, size);
2816 val_tree = make_tree (integer_type_node, val);
2818 fn = clear_storage_libcall_fn (true);
2819 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2820 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2822 retval = expand_normal (call_expr);
2824 return retval;
2827 /* A subroutine of set_storage_via_libcall. Create the tree node
2828 for the function we use for block clears. */
2830 tree block_clear_fn;
2832 void
2833 init_block_clear_fn (const char *asmspec)
2835 if (!block_clear_fn)
2837 tree fn, args;
2839 fn = get_identifier ("memset");
2840 args = build_function_type_list (ptr_type_node, ptr_type_node,
2841 integer_type_node, sizetype,
2842 NULL_TREE);
2844 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2845 DECL_EXTERNAL (fn) = 1;
2846 TREE_PUBLIC (fn) = 1;
2847 DECL_ARTIFICIAL (fn) = 1;
2848 TREE_NOTHROW (fn) = 1;
2849 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2850 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2852 block_clear_fn = fn;
2855 if (asmspec)
2856 set_user_assembler_name (block_clear_fn, asmspec);
2859 static tree
2860 clear_storage_libcall_fn (int for_call)
2862 static bool emitted_extern;
2864 if (!block_clear_fn)
2865 init_block_clear_fn (NULL);
2867 if (for_call && !emitted_extern)
2869 emitted_extern = true;
2870 make_decl_rtl (block_clear_fn);
2873 return block_clear_fn;
2876 /* Expand a setmem pattern; return true if successful. */
2878 bool
2879 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2880 unsigned int expected_align, HOST_WIDE_INT expected_size,
2881 unsigned HOST_WIDE_INT min_size,
2882 unsigned HOST_WIDE_INT max_size,
2883 unsigned HOST_WIDE_INT probable_max_size)
2885 /* Try the most limited insn first, because there's no point
2886 including more than one in the machine description unless
2887 the more limited one has some advantage. */
2889 machine_mode mode;
2891 if (expected_align < align)
2892 expected_align = align;
2893 if (expected_size != -1)
2895 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2896 expected_size = max_size;
2897 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2898 expected_size = min_size;
2901 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2902 mode = GET_MODE_WIDER_MODE (mode))
2904 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2906 if (code != CODE_FOR_nothing
2907 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2908 here because if SIZE is less than the mode mask, as it is
2909 returned by the macro, it will definitely be less than the
2910 actual mode mask. Since SIZE is within the Pmode address
2911 space, we limit MODE to Pmode. */
2912 && ((CONST_INT_P (size)
2913 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2914 <= (GET_MODE_MASK (mode) >> 1)))
2915 || max_size <= (GET_MODE_MASK (mode) >> 1)
2916 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2918 struct expand_operand ops[9];
2919 unsigned int nops;
2921 nops = insn_data[(int) code].n_generator_args;
2922 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2924 create_fixed_operand (&ops[0], object);
2925 /* The check above guarantees that this size conversion is valid. */
2926 create_convert_operand_to (&ops[1], size, mode, true);
2927 create_convert_operand_from (&ops[2], val, byte_mode, true);
2928 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2929 if (nops >= 6)
2931 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2932 create_integer_operand (&ops[5], expected_size);
2934 if (nops >= 8)
2936 create_integer_operand (&ops[6], min_size);
2937 /* If we can not represent the maximal size,
2938 make parameter NULL. */
2939 if ((HOST_WIDE_INT) max_size != -1)
2940 create_integer_operand (&ops[7], max_size);
2941 else
2942 create_fixed_operand (&ops[7], NULL);
2944 if (nops == 9)
2946 /* If we can not represent the maximal size,
2947 make parameter NULL. */
2948 if ((HOST_WIDE_INT) probable_max_size != -1)
2949 create_integer_operand (&ops[8], probable_max_size);
2950 else
2951 create_fixed_operand (&ops[8], NULL);
2953 if (maybe_expand_insn (code, nops, ops))
2954 return true;
2958 return false;
2962 /* Write to one of the components of the complex value CPLX. Write VAL to
2963 the real part if IMAG_P is false, and the imaginary part if its true. */
2965 void
2966 write_complex_part (rtx cplx, rtx val, bool imag_p)
2968 machine_mode cmode;
2969 machine_mode imode;
2970 unsigned ibitsize;
2972 if (GET_CODE (cplx) == CONCAT)
2974 emit_move_insn (XEXP (cplx, imag_p), val);
2975 return;
2978 cmode = GET_MODE (cplx);
2979 imode = GET_MODE_INNER (cmode);
2980 ibitsize = GET_MODE_BITSIZE (imode);
2982 /* For MEMs simplify_gen_subreg may generate an invalid new address
2983 because, e.g., the original address is considered mode-dependent
2984 by the target, which restricts simplify_subreg from invoking
2985 adjust_address_nv. Instead of preparing fallback support for an
2986 invalid address, we call adjust_address_nv directly. */
2987 if (MEM_P (cplx))
2989 emit_move_insn (adjust_address_nv (cplx, imode,
2990 imag_p ? GET_MODE_SIZE (imode) : 0),
2991 val);
2992 return;
2995 /* If the sub-object is at least word sized, then we know that subregging
2996 will work. This special case is important, since store_bit_field
2997 wants to operate on integer modes, and there's rarely an OImode to
2998 correspond to TCmode. */
2999 if (ibitsize >= BITS_PER_WORD
3000 /* For hard regs we have exact predicates. Assume we can split
3001 the original object if it spans an even number of hard regs.
3002 This special case is important for SCmode on 64-bit platforms
3003 where the natural size of floating-point regs is 32-bit. */
3004 || (REG_P (cplx)
3005 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3006 && REG_NREGS (cplx) % 2 == 0))
3008 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3009 imag_p ? GET_MODE_SIZE (imode) : 0);
3010 if (part)
3012 emit_move_insn (part, val);
3013 return;
3015 else
3016 /* simplify_gen_subreg may fail for sub-word MEMs. */
3017 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3020 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3023 /* Extract one of the components of the complex value CPLX. Extract the
3024 real part if IMAG_P is false, and the imaginary part if it's true. */
3027 read_complex_part (rtx cplx, bool imag_p)
3029 machine_mode cmode, imode;
3030 unsigned ibitsize;
3032 if (GET_CODE (cplx) == CONCAT)
3033 return XEXP (cplx, imag_p);
3035 cmode = GET_MODE (cplx);
3036 imode = GET_MODE_INNER (cmode);
3037 ibitsize = GET_MODE_BITSIZE (imode);
3039 /* Special case reads from complex constants that got spilled to memory. */
3040 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3042 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3043 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3045 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3046 if (CONSTANT_CLASS_P (part))
3047 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3051 /* For MEMs simplify_gen_subreg may generate an invalid new address
3052 because, e.g., the original address is considered mode-dependent
3053 by the target, which restricts simplify_subreg from invoking
3054 adjust_address_nv. Instead of preparing fallback support for an
3055 invalid address, we call adjust_address_nv directly. */
3056 if (MEM_P (cplx))
3057 return adjust_address_nv (cplx, imode,
3058 imag_p ? GET_MODE_SIZE (imode) : 0);
3060 /* If the sub-object is at least word sized, then we know that subregging
3061 will work. This special case is important, since extract_bit_field
3062 wants to operate on integer modes, and there's rarely an OImode to
3063 correspond to TCmode. */
3064 if (ibitsize >= BITS_PER_WORD
3065 /* For hard regs we have exact predicates. Assume we can split
3066 the original object if it spans an even number of hard regs.
3067 This special case is important for SCmode on 64-bit platforms
3068 where the natural size of floating-point regs is 32-bit. */
3069 || (REG_P (cplx)
3070 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3071 && REG_NREGS (cplx) % 2 == 0))
3073 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3074 imag_p ? GET_MODE_SIZE (imode) : 0);
3075 if (ret)
3076 return ret;
3077 else
3078 /* simplify_gen_subreg may fail for sub-word MEMs. */
3079 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3082 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3083 true, NULL_RTX, imode, imode);
3086 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3087 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3088 represented in NEW_MODE. If FORCE is true, this will never happen, as
3089 we'll force-create a SUBREG if needed. */
3091 static rtx
3092 emit_move_change_mode (machine_mode new_mode,
3093 machine_mode old_mode, rtx x, bool force)
3095 rtx ret;
3097 if (push_operand (x, GET_MODE (x)))
3099 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3100 MEM_COPY_ATTRIBUTES (ret, x);
3102 else if (MEM_P (x))
3104 /* We don't have to worry about changing the address since the
3105 size in bytes is supposed to be the same. */
3106 if (reload_in_progress)
3108 /* Copy the MEM to change the mode and move any
3109 substitutions from the old MEM to the new one. */
3110 ret = adjust_address_nv (x, new_mode, 0);
3111 copy_replacements (x, ret);
3113 else
3114 ret = adjust_address (x, new_mode, 0);
3116 else
3118 /* Note that we do want simplify_subreg's behavior of validating
3119 that the new mode is ok for a hard register. If we were to use
3120 simplify_gen_subreg, we would create the subreg, but would
3121 probably run into the target not being able to implement it. */
3122 /* Except, of course, when FORCE is true, when this is exactly what
3123 we want. Which is needed for CCmodes on some targets. */
3124 if (force)
3125 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3126 else
3127 ret = simplify_subreg (new_mode, x, old_mode, 0);
3130 return ret;
3133 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3134 an integer mode of the same size as MODE. Returns the instruction
3135 emitted, or NULL if such a move could not be generated. */
3137 static rtx_insn *
3138 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3140 machine_mode imode;
3141 enum insn_code code;
3143 /* There must exist a mode of the exact size we require. */
3144 imode = int_mode_for_mode (mode);
3145 if (imode == BLKmode)
3146 return NULL;
3148 /* The target must support moves in this mode. */
3149 code = optab_handler (mov_optab, imode);
3150 if (code == CODE_FOR_nothing)
3151 return NULL;
3153 x = emit_move_change_mode (imode, mode, x, force);
3154 if (x == NULL_RTX)
3155 return NULL;
3156 y = emit_move_change_mode (imode, mode, y, force);
3157 if (y == NULL_RTX)
3158 return NULL;
3159 return emit_insn (GEN_FCN (code) (x, y));
3162 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3163 Return an equivalent MEM that does not use an auto-increment. */
3166 emit_move_resolve_push (machine_mode mode, rtx x)
3168 enum rtx_code code = GET_CODE (XEXP (x, 0));
3169 HOST_WIDE_INT adjust;
3170 rtx temp;
3172 adjust = GET_MODE_SIZE (mode);
3173 #ifdef PUSH_ROUNDING
3174 adjust = PUSH_ROUNDING (adjust);
3175 #endif
3176 if (code == PRE_DEC || code == POST_DEC)
3177 adjust = -adjust;
3178 else if (code == PRE_MODIFY || code == POST_MODIFY)
3180 rtx expr = XEXP (XEXP (x, 0), 1);
3181 HOST_WIDE_INT val;
3183 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3184 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3185 val = INTVAL (XEXP (expr, 1));
3186 if (GET_CODE (expr) == MINUS)
3187 val = -val;
3188 gcc_assert (adjust == val || adjust == -val);
3189 adjust = val;
3192 /* Do not use anti_adjust_stack, since we don't want to update
3193 stack_pointer_delta. */
3194 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3195 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3196 0, OPTAB_LIB_WIDEN);
3197 if (temp != stack_pointer_rtx)
3198 emit_move_insn (stack_pointer_rtx, temp);
3200 switch (code)
3202 case PRE_INC:
3203 case PRE_DEC:
3204 case PRE_MODIFY:
3205 temp = stack_pointer_rtx;
3206 break;
3207 case POST_INC:
3208 case POST_DEC:
3209 case POST_MODIFY:
3210 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3211 break;
3212 default:
3213 gcc_unreachable ();
3216 return replace_equiv_address (x, temp);
3219 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3220 X is known to satisfy push_operand, and MODE is known to be complex.
3221 Returns the last instruction emitted. */
3223 rtx_insn *
3224 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3226 machine_mode submode = GET_MODE_INNER (mode);
3227 bool imag_first;
3229 #ifdef PUSH_ROUNDING
3230 unsigned int submodesize = GET_MODE_SIZE (submode);
3232 /* In case we output to the stack, but the size is smaller than the
3233 machine can push exactly, we need to use move instructions. */
3234 if (PUSH_ROUNDING (submodesize) != submodesize)
3236 x = emit_move_resolve_push (mode, x);
3237 return emit_move_insn (x, y);
3239 #endif
3241 /* Note that the real part always precedes the imag part in memory
3242 regardless of machine's endianness. */
3243 switch (GET_CODE (XEXP (x, 0)))
3245 case PRE_DEC:
3246 case POST_DEC:
3247 imag_first = true;
3248 break;
3249 case PRE_INC:
3250 case POST_INC:
3251 imag_first = false;
3252 break;
3253 default:
3254 gcc_unreachable ();
3257 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3258 read_complex_part (y, imag_first));
3259 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3260 read_complex_part (y, !imag_first));
3263 /* A subroutine of emit_move_complex. Perform the move from Y to X
3264 via two moves of the parts. Returns the last instruction emitted. */
3266 rtx_insn *
3267 emit_move_complex_parts (rtx x, rtx y)
3269 /* Show the output dies here. This is necessary for SUBREGs
3270 of pseudos since we cannot track their lifetimes correctly;
3271 hard regs shouldn't appear here except as return values. */
3272 if (!reload_completed && !reload_in_progress
3273 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3274 emit_clobber (x);
3276 write_complex_part (x, read_complex_part (y, false), false);
3277 write_complex_part (x, read_complex_part (y, true), true);
3279 return get_last_insn ();
3282 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3283 MODE is known to be complex. Returns the last instruction emitted. */
3285 static rtx_insn *
3286 emit_move_complex (machine_mode mode, rtx x, rtx y)
3288 bool try_int;
3290 /* Need to take special care for pushes, to maintain proper ordering
3291 of the data, and possibly extra padding. */
3292 if (push_operand (x, mode))
3293 return emit_move_complex_push (mode, x, y);
3295 /* See if we can coerce the target into moving both values at once, except
3296 for floating point where we favor moving as parts if this is easy. */
3297 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3298 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3299 && !(REG_P (x)
3300 && HARD_REGISTER_P (x)
3301 && REG_NREGS (x) == 1)
3302 && !(REG_P (y)
3303 && HARD_REGISTER_P (y)
3304 && REG_NREGS (y) == 1))
3305 try_int = false;
3306 /* Not possible if the values are inherently not adjacent. */
3307 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3308 try_int = false;
3309 /* Is possible if both are registers (or subregs of registers). */
3310 else if (register_operand (x, mode) && register_operand (y, mode))
3311 try_int = true;
3312 /* If one of the operands is a memory, and alignment constraints
3313 are friendly enough, we may be able to do combined memory operations.
3314 We do not attempt this if Y is a constant because that combination is
3315 usually better with the by-parts thing below. */
3316 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3317 && (!STRICT_ALIGNMENT
3318 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3319 try_int = true;
3320 else
3321 try_int = false;
3323 if (try_int)
3325 rtx_insn *ret;
3327 /* For memory to memory moves, optimal behavior can be had with the
3328 existing block move logic. */
3329 if (MEM_P (x) && MEM_P (y))
3331 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3332 BLOCK_OP_NO_LIBCALL);
3333 return get_last_insn ();
3336 ret = emit_move_via_integer (mode, x, y, true);
3337 if (ret)
3338 return ret;
3341 return emit_move_complex_parts (x, y);
3344 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3345 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3347 static rtx_insn *
3348 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3350 rtx_insn *ret;
3352 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3353 if (mode != CCmode)
3355 enum insn_code code = optab_handler (mov_optab, CCmode);
3356 if (code != CODE_FOR_nothing)
3358 x = emit_move_change_mode (CCmode, mode, x, true);
3359 y = emit_move_change_mode (CCmode, mode, y, true);
3360 return emit_insn (GEN_FCN (code) (x, y));
3364 /* Otherwise, find the MODE_INT mode of the same width. */
3365 ret = emit_move_via_integer (mode, x, y, false);
3366 gcc_assert (ret != NULL);
3367 return ret;
3370 /* Return true if word I of OP lies entirely in the
3371 undefined bits of a paradoxical subreg. */
3373 static bool
3374 undefined_operand_subword_p (const_rtx op, int i)
3376 machine_mode innermode, innermostmode;
3377 int offset;
3378 if (GET_CODE (op) != SUBREG)
3379 return false;
3380 innermode = GET_MODE (op);
3381 innermostmode = GET_MODE (SUBREG_REG (op));
3382 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3383 /* The SUBREG_BYTE represents offset, as if the value were stored in
3384 memory, except for a paradoxical subreg where we define
3385 SUBREG_BYTE to be 0; undo this exception as in
3386 simplify_subreg. */
3387 if (SUBREG_BYTE (op) == 0
3388 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3390 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3391 if (WORDS_BIG_ENDIAN)
3392 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3393 if (BYTES_BIG_ENDIAN)
3394 offset += difference % UNITS_PER_WORD;
3396 if (offset >= GET_MODE_SIZE (innermostmode)
3397 || offset <= -GET_MODE_SIZE (word_mode))
3398 return true;
3399 return false;
3402 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3403 MODE is any multi-word or full-word mode that lacks a move_insn
3404 pattern. Note that you will get better code if you define such
3405 patterns, even if they must turn into multiple assembler instructions. */
3407 static rtx_insn *
3408 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3410 rtx_insn *last_insn = 0;
3411 rtx_insn *seq;
3412 rtx inner;
3413 bool need_clobber;
3414 int i;
3416 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3418 /* If X is a push on the stack, do the push now and replace
3419 X with a reference to the stack pointer. */
3420 if (push_operand (x, mode))
3421 x = emit_move_resolve_push (mode, x);
3423 /* If we are in reload, see if either operand is a MEM whose address
3424 is scheduled for replacement. */
3425 if (reload_in_progress && MEM_P (x)
3426 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3427 x = replace_equiv_address_nv (x, inner);
3428 if (reload_in_progress && MEM_P (y)
3429 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3430 y = replace_equiv_address_nv (y, inner);
3432 start_sequence ();
3434 need_clobber = false;
3435 for (i = 0;
3436 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3437 i++)
3439 rtx xpart = operand_subword (x, i, 1, mode);
3440 rtx ypart;
3442 /* Do not generate code for a move if it would come entirely
3443 from the undefined bits of a paradoxical subreg. */
3444 if (undefined_operand_subword_p (y, i))
3445 continue;
3447 ypart = operand_subword (y, i, 1, mode);
3449 /* If we can't get a part of Y, put Y into memory if it is a
3450 constant. Otherwise, force it into a register. Then we must
3451 be able to get a part of Y. */
3452 if (ypart == 0 && CONSTANT_P (y))
3454 y = use_anchored_address (force_const_mem (mode, y));
3455 ypart = operand_subword (y, i, 1, mode);
3457 else if (ypart == 0)
3458 ypart = operand_subword_force (y, i, mode);
3460 gcc_assert (xpart && ypart);
3462 need_clobber |= (GET_CODE (xpart) == SUBREG);
3464 last_insn = emit_move_insn (xpart, ypart);
3467 seq = get_insns ();
3468 end_sequence ();
3470 /* Show the output dies here. This is necessary for SUBREGs
3471 of pseudos since we cannot track their lifetimes correctly;
3472 hard regs shouldn't appear here except as return values.
3473 We never want to emit such a clobber after reload. */
3474 if (x != y
3475 && ! (reload_in_progress || reload_completed)
3476 && need_clobber != 0)
3477 emit_clobber (x);
3479 emit_insn (seq);
3481 return last_insn;
3484 /* Low level part of emit_move_insn.
3485 Called just like emit_move_insn, but assumes X and Y
3486 are basically valid. */
3488 rtx_insn *
3489 emit_move_insn_1 (rtx x, rtx y)
3491 machine_mode mode = GET_MODE (x);
3492 enum insn_code code;
3494 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3496 code = optab_handler (mov_optab, mode);
3497 if (code != CODE_FOR_nothing)
3498 return emit_insn (GEN_FCN (code) (x, y));
3500 /* Expand complex moves by moving real part and imag part. */
3501 if (COMPLEX_MODE_P (mode))
3502 return emit_move_complex (mode, x, y);
3504 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3505 || ALL_FIXED_POINT_MODE_P (mode))
3507 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3509 /* If we can't find an integer mode, use multi words. */
3510 if (result)
3511 return result;
3512 else
3513 return emit_move_multi_word (mode, x, y);
3516 if (GET_MODE_CLASS (mode) == MODE_CC)
3517 return emit_move_ccmode (mode, x, y);
3519 /* Try using a move pattern for the corresponding integer mode. This is
3520 only safe when simplify_subreg can convert MODE constants into integer
3521 constants. At present, it can only do this reliably if the value
3522 fits within a HOST_WIDE_INT. */
3523 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3525 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3527 if (ret)
3529 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3530 return ret;
3534 return emit_move_multi_word (mode, x, y);
3537 /* Generate code to copy Y into X.
3538 Both Y and X must have the same mode, except that
3539 Y can be a constant with VOIDmode.
3540 This mode cannot be BLKmode; use emit_block_move for that.
3542 Return the last instruction emitted. */
3544 rtx_insn *
3545 emit_move_insn (rtx x, rtx y)
3547 machine_mode mode = GET_MODE (x);
3548 rtx y_cst = NULL_RTX;
3549 rtx_insn *last_insn;
3550 rtx set;
3552 gcc_assert (mode != BLKmode
3553 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3555 if (CONSTANT_P (y))
3557 if (optimize
3558 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3559 && (last_insn = compress_float_constant (x, y)))
3560 return last_insn;
3562 y_cst = y;
3564 if (!targetm.legitimate_constant_p (mode, y))
3566 y = force_const_mem (mode, y);
3568 /* If the target's cannot_force_const_mem prevented the spill,
3569 assume that the target's move expanders will also take care
3570 of the non-legitimate constant. */
3571 if (!y)
3572 y = y_cst;
3573 else
3574 y = use_anchored_address (y);
3578 /* If X or Y are memory references, verify that their addresses are valid
3579 for the machine. */
3580 if (MEM_P (x)
3581 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3582 MEM_ADDR_SPACE (x))
3583 && ! push_operand (x, GET_MODE (x))))
3584 x = validize_mem (x);
3586 if (MEM_P (y)
3587 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3588 MEM_ADDR_SPACE (y)))
3589 y = validize_mem (y);
3591 gcc_assert (mode != BLKmode);
3593 last_insn = emit_move_insn_1 (x, y);
3595 if (y_cst && REG_P (x)
3596 && (set = single_set (last_insn)) != NULL_RTX
3597 && SET_DEST (set) == x
3598 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3599 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3601 return last_insn;
3604 /* Generate the body of an instruction to copy Y into X.
3605 It may be a list of insns, if one insn isn't enough. */
3607 rtx_insn *
3608 gen_move_insn (rtx x, rtx y)
3610 rtx_insn *seq;
3612 start_sequence ();
3613 emit_move_insn_1 (x, y);
3614 seq = get_insns ();
3615 end_sequence ();
3616 return seq;
3619 /* If Y is representable exactly in a narrower mode, and the target can
3620 perform the extension directly from constant or memory, then emit the
3621 move as an extension. */
3623 static rtx_insn *
3624 compress_float_constant (rtx x, rtx y)
3626 machine_mode dstmode = GET_MODE (x);
3627 machine_mode orig_srcmode = GET_MODE (y);
3628 machine_mode srcmode;
3629 const REAL_VALUE_TYPE *r;
3630 int oldcost, newcost;
3631 bool speed = optimize_insn_for_speed_p ();
3633 r = CONST_DOUBLE_REAL_VALUE (y);
3635 if (targetm.legitimate_constant_p (dstmode, y))
3636 oldcost = set_src_cost (y, orig_srcmode, speed);
3637 else
3638 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3640 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3641 srcmode != orig_srcmode;
3642 srcmode = GET_MODE_WIDER_MODE (srcmode))
3644 enum insn_code ic;
3645 rtx trunc_y;
3646 rtx_insn *last_insn;
3648 /* Skip if the target can't extend this way. */
3649 ic = can_extend_p (dstmode, srcmode, 0);
3650 if (ic == CODE_FOR_nothing)
3651 continue;
3653 /* Skip if the narrowed value isn't exact. */
3654 if (! exact_real_truncate (srcmode, r))
3655 continue;
3657 trunc_y = const_double_from_real_value (*r, srcmode);
3659 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3661 /* Skip if the target needs extra instructions to perform
3662 the extension. */
3663 if (!insn_operand_matches (ic, 1, trunc_y))
3664 continue;
3665 /* This is valid, but may not be cheaper than the original. */
3666 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3667 dstmode, speed);
3668 if (oldcost < newcost)
3669 continue;
3671 else if (float_extend_from_mem[dstmode][srcmode])
3673 trunc_y = force_const_mem (srcmode, trunc_y);
3674 /* This is valid, but may not be cheaper than the original. */
3675 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3676 dstmode, speed);
3677 if (oldcost < newcost)
3678 continue;
3679 trunc_y = validize_mem (trunc_y);
3681 else
3682 continue;
3684 /* For CSE's benefit, force the compressed constant pool entry
3685 into a new pseudo. This constant may be used in different modes,
3686 and if not, combine will put things back together for us. */
3687 trunc_y = force_reg (srcmode, trunc_y);
3689 /* If x is a hard register, perform the extension into a pseudo,
3690 so that e.g. stack realignment code is aware of it. */
3691 rtx target = x;
3692 if (REG_P (x) && HARD_REGISTER_P (x))
3693 target = gen_reg_rtx (dstmode);
3695 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3696 last_insn = get_last_insn ();
3698 if (REG_P (target))
3699 set_unique_reg_note (last_insn, REG_EQUAL, y);
3701 if (target != x)
3702 return emit_move_insn (x, target);
3703 return last_insn;
3706 return NULL;
3709 /* Pushing data onto the stack. */
3711 /* Push a block of length SIZE (perhaps variable)
3712 and return an rtx to address the beginning of the block.
3713 The value may be virtual_outgoing_args_rtx.
3715 EXTRA is the number of bytes of padding to push in addition to SIZE.
3716 BELOW nonzero means this padding comes at low addresses;
3717 otherwise, the padding comes at high addresses. */
3720 push_block (rtx size, int extra, int below)
3722 rtx temp;
3724 size = convert_modes (Pmode, ptr_mode, size, 1);
3725 if (CONSTANT_P (size))
3726 anti_adjust_stack (plus_constant (Pmode, size, extra));
3727 else if (REG_P (size) && extra == 0)
3728 anti_adjust_stack (size);
3729 else
3731 temp = copy_to_mode_reg (Pmode, size);
3732 if (extra != 0)
3733 temp = expand_binop (Pmode, add_optab, temp,
3734 gen_int_mode (extra, Pmode),
3735 temp, 0, OPTAB_LIB_WIDEN);
3736 anti_adjust_stack (temp);
3739 if (STACK_GROWS_DOWNWARD)
3741 temp = virtual_outgoing_args_rtx;
3742 if (extra != 0 && below)
3743 temp = plus_constant (Pmode, temp, extra);
3745 else
3747 if (CONST_INT_P (size))
3748 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3749 -INTVAL (size) - (below ? 0 : extra));
3750 else if (extra != 0 && !below)
3751 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3752 negate_rtx (Pmode, plus_constant (Pmode, size,
3753 extra)));
3754 else
3755 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3756 negate_rtx (Pmode, size));
3759 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3762 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3764 static rtx
3765 mem_autoinc_base (rtx mem)
3767 if (MEM_P (mem))
3769 rtx addr = XEXP (mem, 0);
3770 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3771 return XEXP (addr, 0);
3773 return NULL;
3776 /* A utility routine used here, in reload, and in try_split. The insns
3777 after PREV up to and including LAST are known to adjust the stack,
3778 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3779 placing notes as appropriate. PREV may be NULL, indicating the
3780 entire insn sequence prior to LAST should be scanned.
3782 The set of allowed stack pointer modifications is small:
3783 (1) One or more auto-inc style memory references (aka pushes),
3784 (2) One or more addition/subtraction with the SP as destination,
3785 (3) A single move insn with the SP as destination,
3786 (4) A call_pop insn,
3787 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3789 Insns in the sequence that do not modify the SP are ignored,
3790 except for noreturn calls.
3792 The return value is the amount of adjustment that can be trivially
3793 verified, via immediate operand or auto-inc. If the adjustment
3794 cannot be trivially extracted, the return value is INT_MIN. */
3796 HOST_WIDE_INT
3797 find_args_size_adjust (rtx_insn *insn)
3799 rtx dest, set, pat;
3800 int i;
3802 pat = PATTERN (insn);
3803 set = NULL;
3805 /* Look for a call_pop pattern. */
3806 if (CALL_P (insn))
3808 /* We have to allow non-call_pop patterns for the case
3809 of emit_single_push_insn of a TLS address. */
3810 if (GET_CODE (pat) != PARALLEL)
3811 return 0;
3813 /* All call_pop have a stack pointer adjust in the parallel.
3814 The call itself is always first, and the stack adjust is
3815 usually last, so search from the end. */
3816 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3818 set = XVECEXP (pat, 0, i);
3819 if (GET_CODE (set) != SET)
3820 continue;
3821 dest = SET_DEST (set);
3822 if (dest == stack_pointer_rtx)
3823 break;
3825 /* We'd better have found the stack pointer adjust. */
3826 if (i == 0)
3827 return 0;
3828 /* Fall through to process the extracted SET and DEST
3829 as if it was a standalone insn. */
3831 else if (GET_CODE (pat) == SET)
3832 set = pat;
3833 else if ((set = single_set (insn)) != NULL)
3835 else if (GET_CODE (pat) == PARALLEL)
3837 /* ??? Some older ports use a parallel with a stack adjust
3838 and a store for a PUSH_ROUNDING pattern, rather than a
3839 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3840 /* ??? See h8300 and m68k, pushqi1. */
3841 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3843 set = XVECEXP (pat, 0, i);
3844 if (GET_CODE (set) != SET)
3845 continue;
3846 dest = SET_DEST (set);
3847 if (dest == stack_pointer_rtx)
3848 break;
3850 /* We do not expect an auto-inc of the sp in the parallel. */
3851 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3852 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3853 != stack_pointer_rtx);
3855 if (i < 0)
3856 return 0;
3858 else
3859 return 0;
3861 dest = SET_DEST (set);
3863 /* Look for direct modifications of the stack pointer. */
3864 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3866 /* Look for a trivial adjustment, otherwise assume nothing. */
3867 /* Note that the SPU restore_stack_block pattern refers to
3868 the stack pointer in V4SImode. Consider that non-trivial. */
3869 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3870 && GET_CODE (SET_SRC (set)) == PLUS
3871 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3872 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3873 return INTVAL (XEXP (SET_SRC (set), 1));
3874 /* ??? Reload can generate no-op moves, which will be cleaned
3875 up later. Recognize it and continue searching. */
3876 else if (rtx_equal_p (dest, SET_SRC (set)))
3877 return 0;
3878 else
3879 return HOST_WIDE_INT_MIN;
3881 else
3883 rtx mem, addr;
3885 /* Otherwise only think about autoinc patterns. */
3886 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3888 mem = dest;
3889 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3890 != stack_pointer_rtx);
3892 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3893 mem = SET_SRC (set);
3894 else
3895 return 0;
3897 addr = XEXP (mem, 0);
3898 switch (GET_CODE (addr))
3900 case PRE_INC:
3901 case POST_INC:
3902 return GET_MODE_SIZE (GET_MODE (mem));
3903 case PRE_DEC:
3904 case POST_DEC:
3905 return -GET_MODE_SIZE (GET_MODE (mem));
3906 case PRE_MODIFY:
3907 case POST_MODIFY:
3908 addr = XEXP (addr, 1);
3909 gcc_assert (GET_CODE (addr) == PLUS);
3910 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3911 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3912 return INTVAL (XEXP (addr, 1));
3913 default:
3914 gcc_unreachable ();
3920 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3922 int args_size = end_args_size;
3923 bool saw_unknown = false;
3924 rtx_insn *insn;
3926 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3928 HOST_WIDE_INT this_delta;
3930 if (!NONDEBUG_INSN_P (insn))
3931 continue;
3933 this_delta = find_args_size_adjust (insn);
3934 if (this_delta == 0)
3936 if (!CALL_P (insn)
3937 || ACCUMULATE_OUTGOING_ARGS
3938 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3939 continue;
3942 gcc_assert (!saw_unknown);
3943 if (this_delta == HOST_WIDE_INT_MIN)
3944 saw_unknown = true;
3946 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3947 if (STACK_GROWS_DOWNWARD)
3948 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3950 args_size -= this_delta;
3953 return saw_unknown ? INT_MIN : args_size;
3956 #ifdef PUSH_ROUNDING
3957 /* Emit single push insn. */
3959 static void
3960 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
3962 rtx dest_addr;
3963 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3964 rtx dest;
3965 enum insn_code icode;
3967 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3968 /* If there is push pattern, use it. Otherwise try old way of throwing
3969 MEM representing push operation to move expander. */
3970 icode = optab_handler (push_optab, mode);
3971 if (icode != CODE_FOR_nothing)
3973 struct expand_operand ops[1];
3975 create_input_operand (&ops[0], x, mode);
3976 if (maybe_expand_insn (icode, 1, ops))
3977 return;
3979 if (GET_MODE_SIZE (mode) == rounded_size)
3980 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
3981 /* If we are to pad downward, adjust the stack pointer first and
3982 then store X into the stack location using an offset. This is
3983 because emit_move_insn does not know how to pad; it does not have
3984 access to type. */
3985 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
3987 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
3988 HOST_WIDE_INT offset;
3990 emit_move_insn (stack_pointer_rtx,
3991 expand_binop (Pmode,
3992 STACK_GROWS_DOWNWARD ? sub_optab
3993 : add_optab,
3994 stack_pointer_rtx,
3995 gen_int_mode (rounded_size, Pmode),
3996 NULL_RTX, 0, OPTAB_LIB_WIDEN));
3998 offset = (HOST_WIDE_INT) padding_size;
3999 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4000 /* We have already decremented the stack pointer, so get the
4001 previous value. */
4002 offset += (HOST_WIDE_INT) rounded_size;
4004 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4005 /* We have already incremented the stack pointer, so get the
4006 previous value. */
4007 offset -= (HOST_WIDE_INT) rounded_size;
4009 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4010 gen_int_mode (offset, Pmode));
4012 else
4014 if (STACK_GROWS_DOWNWARD)
4015 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4016 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4017 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4018 Pmode));
4019 else
4020 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4021 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4022 gen_int_mode (rounded_size, Pmode));
4024 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4027 dest = gen_rtx_MEM (mode, dest_addr);
4029 if (type != 0)
4031 set_mem_attributes (dest, type, 1);
4033 if (cfun->tail_call_marked)
4034 /* Function incoming arguments may overlap with sibling call
4035 outgoing arguments and we cannot allow reordering of reads
4036 from function arguments with stores to outgoing arguments
4037 of sibling calls. */
4038 set_mem_alias_set (dest, 0);
4040 emit_move_insn (dest, x);
4043 /* Emit and annotate a single push insn. */
4045 static void
4046 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4048 int delta, old_delta = stack_pointer_delta;
4049 rtx_insn *prev = get_last_insn ();
4050 rtx_insn *last;
4052 emit_single_push_insn_1 (mode, x, type);
4054 last = get_last_insn ();
4056 /* Notice the common case where we emitted exactly one insn. */
4057 if (PREV_INSN (last) == prev)
4059 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4060 return;
4063 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4064 gcc_assert (delta == INT_MIN || delta == old_delta);
4066 #endif
4068 /* If reading SIZE bytes from X will end up reading from
4069 Y return the number of bytes that overlap. Return -1
4070 if there is no overlap or -2 if we can't determine
4071 (for example when X and Y have different base registers). */
4073 static int
4074 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4076 rtx tmp = plus_constant (Pmode, x, size);
4077 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4079 if (!CONST_INT_P (sub))
4080 return -2;
4082 HOST_WIDE_INT val = INTVAL (sub);
4084 return IN_RANGE (val, 1, size) ? val : -1;
4087 /* Generate code to push X onto the stack, assuming it has mode MODE and
4088 type TYPE.
4089 MODE is redundant except when X is a CONST_INT (since they don't
4090 carry mode info).
4091 SIZE is an rtx for the size of data to be copied (in bytes),
4092 needed only if X is BLKmode.
4093 Return true if successful. May return false if asked to push a
4094 partial argument during a sibcall optimization (as specified by
4095 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4096 to not overlap.
4098 ALIGN (in bits) is maximum alignment we can assume.
4100 If PARTIAL and REG are both nonzero, then copy that many of the first
4101 bytes of X into registers starting with REG, and push the rest of X.
4102 The amount of space pushed is decreased by PARTIAL bytes.
4103 REG must be a hard register in this case.
4104 If REG is zero but PARTIAL is not, take any all others actions for an
4105 argument partially in registers, but do not actually load any
4106 registers.
4108 EXTRA is the amount in bytes of extra space to leave next to this arg.
4109 This is ignored if an argument block has already been allocated.
4111 On a machine that lacks real push insns, ARGS_ADDR is the address of
4112 the bottom of the argument block for this call. We use indexing off there
4113 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4114 argument block has not been preallocated.
4116 ARGS_SO_FAR is the size of args previously pushed for this call.
4118 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4119 for arguments passed in registers. If nonzero, it will be the number
4120 of bytes required. */
4122 bool
4123 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4124 unsigned int align, int partial, rtx reg, int extra,
4125 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4126 rtx alignment_pad, bool sibcall_p)
4128 rtx xinner;
4129 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4131 /* Decide where to pad the argument: `downward' for below,
4132 `upward' for above, or `none' for don't pad it.
4133 Default is below for small data on big-endian machines; else above. */
4134 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4136 /* Invert direction if stack is post-decrement.
4137 FIXME: why? */
4138 if (STACK_PUSH_CODE == POST_DEC)
4139 if (where_pad != none)
4140 where_pad = (where_pad == downward ? upward : downward);
4142 xinner = x;
4144 int nregs = partial / UNITS_PER_WORD;
4145 rtx *tmp_regs = NULL;
4146 int overlapping = 0;
4148 if (mode == BLKmode
4149 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4151 /* Copy a block into the stack, entirely or partially. */
4153 rtx temp;
4154 int used;
4155 int offset;
4156 int skip;
4158 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4159 used = partial - offset;
4161 if (mode != BLKmode)
4163 /* A value is to be stored in an insufficiently aligned
4164 stack slot; copy via a suitably aligned slot if
4165 necessary. */
4166 size = GEN_INT (GET_MODE_SIZE (mode));
4167 if (!MEM_P (xinner))
4169 temp = assign_temp (type, 1, 1);
4170 emit_move_insn (temp, xinner);
4171 xinner = temp;
4175 gcc_assert (size);
4177 /* USED is now the # of bytes we need not copy to the stack
4178 because registers will take care of them. */
4180 if (partial != 0)
4181 xinner = adjust_address (xinner, BLKmode, used);
4183 /* If the partial register-part of the arg counts in its stack size,
4184 skip the part of stack space corresponding to the registers.
4185 Otherwise, start copying to the beginning of the stack space,
4186 by setting SKIP to 0. */
4187 skip = (reg_parm_stack_space == 0) ? 0 : used;
4189 #ifdef PUSH_ROUNDING
4190 /* Do it with several push insns if that doesn't take lots of insns
4191 and if there is no difficulty with push insns that skip bytes
4192 on the stack for alignment purposes. */
4193 if (args_addr == 0
4194 && PUSH_ARGS
4195 && CONST_INT_P (size)
4196 && skip == 0
4197 && MEM_ALIGN (xinner) >= align
4198 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4199 /* Here we avoid the case of a structure whose weak alignment
4200 forces many pushes of a small amount of data,
4201 and such small pushes do rounding that causes trouble. */
4202 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4203 || align >= BIGGEST_ALIGNMENT
4204 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4205 == (align / BITS_PER_UNIT)))
4206 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4208 /* Push padding now if padding above and stack grows down,
4209 or if padding below and stack grows up.
4210 But if space already allocated, this has already been done. */
4211 if (extra && args_addr == 0
4212 && where_pad != none && where_pad != stack_direction)
4213 anti_adjust_stack (GEN_INT (extra));
4215 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4217 else
4218 #endif /* PUSH_ROUNDING */
4220 rtx target;
4222 /* Otherwise make space on the stack and copy the data
4223 to the address of that space. */
4225 /* Deduct words put into registers from the size we must copy. */
4226 if (partial != 0)
4228 if (CONST_INT_P (size))
4229 size = GEN_INT (INTVAL (size) - used);
4230 else
4231 size = expand_binop (GET_MODE (size), sub_optab, size,
4232 gen_int_mode (used, GET_MODE (size)),
4233 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4236 /* Get the address of the stack space.
4237 In this case, we do not deal with EXTRA separately.
4238 A single stack adjust will do. */
4239 if (! args_addr)
4241 temp = push_block (size, extra, where_pad == downward);
4242 extra = 0;
4244 else if (CONST_INT_P (args_so_far))
4245 temp = memory_address (BLKmode,
4246 plus_constant (Pmode, args_addr,
4247 skip + INTVAL (args_so_far)));
4248 else
4249 temp = memory_address (BLKmode,
4250 plus_constant (Pmode,
4251 gen_rtx_PLUS (Pmode,
4252 args_addr,
4253 args_so_far),
4254 skip));
4256 if (!ACCUMULATE_OUTGOING_ARGS)
4258 /* If the source is referenced relative to the stack pointer,
4259 copy it to another register to stabilize it. We do not need
4260 to do this if we know that we won't be changing sp. */
4262 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4263 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4264 temp = copy_to_reg (temp);
4267 target = gen_rtx_MEM (BLKmode, temp);
4269 /* We do *not* set_mem_attributes here, because incoming arguments
4270 may overlap with sibling call outgoing arguments and we cannot
4271 allow reordering of reads from function arguments with stores
4272 to outgoing arguments of sibling calls. We do, however, want
4273 to record the alignment of the stack slot. */
4274 /* ALIGN may well be better aligned than TYPE, e.g. due to
4275 PARM_BOUNDARY. Assume the caller isn't lying. */
4276 set_mem_align (target, align);
4278 /* If part should go in registers and pushing to that part would
4279 overwrite some of the values that need to go into regs, load the
4280 overlapping values into temporary pseudos to be moved into the hard
4281 regs at the end after the stack pushing has completed.
4282 We cannot load them directly into the hard regs here because
4283 they can be clobbered by the block move expansions.
4284 See PR 65358. */
4286 if (partial > 0 && reg != 0 && mode == BLKmode
4287 && GET_CODE (reg) != PARALLEL)
4289 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4290 if (overlapping > 0)
4292 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4293 overlapping /= UNITS_PER_WORD;
4295 tmp_regs = XALLOCAVEC (rtx, overlapping);
4297 for (int i = 0; i < overlapping; i++)
4298 tmp_regs[i] = gen_reg_rtx (word_mode);
4300 for (int i = 0; i < overlapping; i++)
4301 emit_move_insn (tmp_regs[i],
4302 operand_subword_force (target, i, mode));
4304 else if (overlapping == -1)
4305 overlapping = 0;
4306 /* Could not determine whether there is overlap.
4307 Fail the sibcall. */
4308 else
4310 overlapping = 0;
4311 if (sibcall_p)
4312 return false;
4315 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4318 else if (partial > 0)
4320 /* Scalar partly in registers. */
4322 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4323 int i;
4324 int not_stack;
4325 /* # bytes of start of argument
4326 that we must make space for but need not store. */
4327 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4328 int args_offset = INTVAL (args_so_far);
4329 int skip;
4331 /* Push padding now if padding above and stack grows down,
4332 or if padding below and stack grows up.
4333 But if space already allocated, this has already been done. */
4334 if (extra && args_addr == 0
4335 && where_pad != none && where_pad != stack_direction)
4336 anti_adjust_stack (GEN_INT (extra));
4338 /* If we make space by pushing it, we might as well push
4339 the real data. Otherwise, we can leave OFFSET nonzero
4340 and leave the space uninitialized. */
4341 if (args_addr == 0)
4342 offset = 0;
4344 /* Now NOT_STACK gets the number of words that we don't need to
4345 allocate on the stack. Convert OFFSET to words too. */
4346 not_stack = (partial - offset) / UNITS_PER_WORD;
4347 offset /= UNITS_PER_WORD;
4349 /* If the partial register-part of the arg counts in its stack size,
4350 skip the part of stack space corresponding to the registers.
4351 Otherwise, start copying to the beginning of the stack space,
4352 by setting SKIP to 0. */
4353 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4355 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4356 x = validize_mem (force_const_mem (mode, x));
4358 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4359 SUBREGs of such registers are not allowed. */
4360 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4361 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4362 x = copy_to_reg (x);
4364 /* Loop over all the words allocated on the stack for this arg. */
4365 /* We can do it by words, because any scalar bigger than a word
4366 has a size a multiple of a word. */
4367 for (i = size - 1; i >= not_stack; i--)
4368 if (i >= not_stack + offset)
4369 if (!emit_push_insn (operand_subword_force (x, i, mode),
4370 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4371 0, args_addr,
4372 GEN_INT (args_offset + ((i - not_stack + skip)
4373 * UNITS_PER_WORD)),
4374 reg_parm_stack_space, alignment_pad, sibcall_p))
4375 return false;
4377 else
4379 rtx addr;
4380 rtx dest;
4382 /* Push padding now if padding above and stack grows down,
4383 or if padding below and stack grows up.
4384 But if space already allocated, this has already been done. */
4385 if (extra && args_addr == 0
4386 && where_pad != none && where_pad != stack_direction)
4387 anti_adjust_stack (GEN_INT (extra));
4389 #ifdef PUSH_ROUNDING
4390 if (args_addr == 0 && PUSH_ARGS)
4391 emit_single_push_insn (mode, x, type);
4392 else
4393 #endif
4395 if (CONST_INT_P (args_so_far))
4396 addr
4397 = memory_address (mode,
4398 plus_constant (Pmode, args_addr,
4399 INTVAL (args_so_far)));
4400 else
4401 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4402 args_so_far));
4403 dest = gen_rtx_MEM (mode, addr);
4405 /* We do *not* set_mem_attributes here, because incoming arguments
4406 may overlap with sibling call outgoing arguments and we cannot
4407 allow reordering of reads from function arguments with stores
4408 to outgoing arguments of sibling calls. We do, however, want
4409 to record the alignment of the stack slot. */
4410 /* ALIGN may well be better aligned than TYPE, e.g. due to
4411 PARM_BOUNDARY. Assume the caller isn't lying. */
4412 set_mem_align (dest, align);
4414 emit_move_insn (dest, x);
4418 /* Move the partial arguments into the registers and any overlapping
4419 values that we moved into the pseudos in tmp_regs. */
4420 if (partial > 0 && reg != 0)
4422 /* Handle calls that pass values in multiple non-contiguous locations.
4423 The Irix 6 ABI has examples of this. */
4424 if (GET_CODE (reg) == PARALLEL)
4425 emit_group_load (reg, x, type, -1);
4426 else
4428 gcc_assert (partial % UNITS_PER_WORD == 0);
4429 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4431 for (int i = 0; i < overlapping; i++)
4432 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4433 + nregs - overlapping + i),
4434 tmp_regs[i]);
4439 if (extra && args_addr == 0 && where_pad == stack_direction)
4440 anti_adjust_stack (GEN_INT (extra));
4442 if (alignment_pad && args_addr == 0)
4443 anti_adjust_stack (alignment_pad);
4445 return true;
4448 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4449 operations. */
4451 static rtx
4452 get_subtarget (rtx x)
4454 return (optimize
4455 || x == 0
4456 /* Only registers can be subtargets. */
4457 || !REG_P (x)
4458 /* Don't use hard regs to avoid extending their life. */
4459 || REGNO (x) < FIRST_PSEUDO_REGISTER
4460 ? 0 : x);
4463 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4464 FIELD is a bitfield. Returns true if the optimization was successful,
4465 and there's nothing else to do. */
4467 static bool
4468 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4469 unsigned HOST_WIDE_INT bitpos,
4470 unsigned HOST_WIDE_INT bitregion_start,
4471 unsigned HOST_WIDE_INT bitregion_end,
4472 machine_mode mode1, rtx str_rtx,
4473 tree to, tree src)
4475 machine_mode str_mode = GET_MODE (str_rtx);
4476 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4477 tree op0, op1;
4478 rtx value, result;
4479 optab binop;
4480 gimple *srcstmt;
4481 enum tree_code code;
4483 if (mode1 != VOIDmode
4484 || bitsize >= BITS_PER_WORD
4485 || str_bitsize > BITS_PER_WORD
4486 || TREE_SIDE_EFFECTS (to)
4487 || TREE_THIS_VOLATILE (to))
4488 return false;
4490 STRIP_NOPS (src);
4491 if (TREE_CODE (src) != SSA_NAME)
4492 return false;
4493 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4494 return false;
4496 srcstmt = get_gimple_for_ssa_name (src);
4497 if (!srcstmt
4498 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4499 return false;
4501 code = gimple_assign_rhs_code (srcstmt);
4503 op0 = gimple_assign_rhs1 (srcstmt);
4505 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4506 to find its initialization. Hopefully the initialization will
4507 be from a bitfield load. */
4508 if (TREE_CODE (op0) == SSA_NAME)
4510 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4512 /* We want to eventually have OP0 be the same as TO, which
4513 should be a bitfield. */
4514 if (!op0stmt
4515 || !is_gimple_assign (op0stmt)
4516 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4517 return false;
4518 op0 = gimple_assign_rhs1 (op0stmt);
4521 op1 = gimple_assign_rhs2 (srcstmt);
4523 if (!operand_equal_p (to, op0, 0))
4524 return false;
4526 if (MEM_P (str_rtx))
4528 unsigned HOST_WIDE_INT offset1;
4530 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4531 str_mode = word_mode;
4532 str_mode = get_best_mode (bitsize, bitpos,
4533 bitregion_start, bitregion_end,
4534 MEM_ALIGN (str_rtx), str_mode, 0);
4535 if (str_mode == VOIDmode)
4536 return false;
4537 str_bitsize = GET_MODE_BITSIZE (str_mode);
4539 offset1 = bitpos;
4540 bitpos %= str_bitsize;
4541 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4542 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4544 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4545 return false;
4547 /* If the bit field covers the whole REG/MEM, store_field
4548 will likely generate better code. */
4549 if (bitsize >= str_bitsize)
4550 return false;
4552 /* We can't handle fields split across multiple entities. */
4553 if (bitpos + bitsize > str_bitsize)
4554 return false;
4556 if (BYTES_BIG_ENDIAN)
4557 bitpos = str_bitsize - bitpos - bitsize;
4559 switch (code)
4561 case PLUS_EXPR:
4562 case MINUS_EXPR:
4563 /* For now, just optimize the case of the topmost bitfield
4564 where we don't need to do any masking and also
4565 1 bit bitfields where xor can be used.
4566 We might win by one instruction for the other bitfields
4567 too if insv/extv instructions aren't used, so that
4568 can be added later. */
4569 if (bitpos + bitsize != str_bitsize
4570 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4571 break;
4573 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4574 value = convert_modes (str_mode,
4575 TYPE_MODE (TREE_TYPE (op1)), value,
4576 TYPE_UNSIGNED (TREE_TYPE (op1)));
4578 /* We may be accessing data outside the field, which means
4579 we can alias adjacent data. */
4580 if (MEM_P (str_rtx))
4582 str_rtx = shallow_copy_rtx (str_rtx);
4583 set_mem_alias_set (str_rtx, 0);
4584 set_mem_expr (str_rtx, 0);
4587 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4588 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4590 value = expand_and (str_mode, value, const1_rtx, NULL);
4591 binop = xor_optab;
4593 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4594 result = expand_binop (str_mode, binop, str_rtx,
4595 value, str_rtx, 1, OPTAB_WIDEN);
4596 if (result != str_rtx)
4597 emit_move_insn (str_rtx, result);
4598 return true;
4600 case BIT_IOR_EXPR:
4601 case BIT_XOR_EXPR:
4602 if (TREE_CODE (op1) != INTEGER_CST)
4603 break;
4604 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4605 value = convert_modes (str_mode,
4606 TYPE_MODE (TREE_TYPE (op1)), value,
4607 TYPE_UNSIGNED (TREE_TYPE (op1)));
4609 /* We may be accessing data outside the field, which means
4610 we can alias adjacent data. */
4611 if (MEM_P (str_rtx))
4613 str_rtx = shallow_copy_rtx (str_rtx);
4614 set_mem_alias_set (str_rtx, 0);
4615 set_mem_expr (str_rtx, 0);
4618 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4619 if (bitpos + bitsize != str_bitsize)
4621 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4622 str_mode);
4623 value = expand_and (str_mode, value, mask, NULL_RTX);
4625 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4626 result = expand_binop (str_mode, binop, str_rtx,
4627 value, str_rtx, 1, OPTAB_WIDEN);
4628 if (result != str_rtx)
4629 emit_move_insn (str_rtx, result);
4630 return true;
4632 default:
4633 break;
4636 return false;
4639 /* In the C++ memory model, consecutive bit fields in a structure are
4640 considered one memory location.
4642 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4643 returns the bit range of consecutive bits in which this COMPONENT_REF
4644 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4645 and *OFFSET may be adjusted in the process.
4647 If the access does not need to be restricted, 0 is returned in both
4648 *BITSTART and *BITEND. */
4650 static void
4651 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4652 unsigned HOST_WIDE_INT *bitend,
4653 tree exp,
4654 HOST_WIDE_INT *bitpos,
4655 tree *offset)
4657 HOST_WIDE_INT bitoffset;
4658 tree field, repr;
4660 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4662 field = TREE_OPERAND (exp, 1);
4663 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4664 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4665 need to limit the range we can access. */
4666 if (!repr)
4668 *bitstart = *bitend = 0;
4669 return;
4672 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4673 part of a larger bit field, then the representative does not serve any
4674 useful purpose. This can occur in Ada. */
4675 if (handled_component_p (TREE_OPERAND (exp, 0)))
4677 machine_mode rmode;
4678 HOST_WIDE_INT rbitsize, rbitpos;
4679 tree roffset;
4680 int unsignedp;
4681 int volatilep = 0;
4682 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4683 &roffset, &rmode, &unsignedp, &volatilep, false);
4684 if ((rbitpos % BITS_PER_UNIT) != 0)
4686 *bitstart = *bitend = 0;
4687 return;
4691 /* Compute the adjustment to bitpos from the offset of the field
4692 relative to the representative. DECL_FIELD_OFFSET of field and
4693 repr are the same by construction if they are not constants,
4694 see finish_bitfield_layout. */
4695 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4696 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4697 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4698 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4699 else
4700 bitoffset = 0;
4701 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4702 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4704 /* If the adjustment is larger than bitpos, we would have a negative bit
4705 position for the lower bound and this may wreak havoc later. Adjust
4706 offset and bitpos to make the lower bound non-negative in that case. */
4707 if (bitoffset > *bitpos)
4709 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4710 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4712 *bitpos += adjust;
4713 if (*offset == NULL_TREE)
4714 *offset = size_int (-adjust / BITS_PER_UNIT);
4715 else
4716 *offset
4717 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4718 *bitstart = 0;
4720 else
4721 *bitstart = *bitpos - bitoffset;
4723 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4726 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4727 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4728 DECL_RTL was not set yet, return NORTL. */
4730 static inline bool
4731 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4733 if (TREE_CODE (addr) != ADDR_EXPR)
4734 return false;
4736 tree base = TREE_OPERAND (addr, 0);
4738 if (!DECL_P (base)
4739 || TREE_ADDRESSABLE (base)
4740 || DECL_MODE (base) == BLKmode)
4741 return false;
4743 if (!DECL_RTL_SET_P (base))
4744 return nortl;
4746 return (!MEM_P (DECL_RTL (base)));
4749 /* Returns true if the MEM_REF REF refers to an object that does not
4750 reside in memory and has non-BLKmode. */
4752 static inline bool
4753 mem_ref_refers_to_non_mem_p (tree ref)
4755 tree base = TREE_OPERAND (ref, 0);
4756 return addr_expr_of_non_mem_decl_p_1 (base, false);
4759 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4760 is true, try generating a nontemporal store. */
4762 void
4763 expand_assignment (tree to, tree from, bool nontemporal)
4765 rtx to_rtx = 0;
4766 rtx result;
4767 machine_mode mode;
4768 unsigned int align;
4769 enum insn_code icode;
4771 /* Don't crash if the lhs of the assignment was erroneous. */
4772 if (TREE_CODE (to) == ERROR_MARK)
4774 expand_normal (from);
4775 return;
4778 /* Optimize away no-op moves without side-effects. */
4779 if (operand_equal_p (to, from, 0))
4780 return;
4782 /* Handle misaligned stores. */
4783 mode = TYPE_MODE (TREE_TYPE (to));
4784 if ((TREE_CODE (to) == MEM_REF
4785 || TREE_CODE (to) == TARGET_MEM_REF)
4786 && mode != BLKmode
4787 && !mem_ref_refers_to_non_mem_p (to)
4788 && ((align = get_object_alignment (to))
4789 < GET_MODE_ALIGNMENT (mode))
4790 && (((icode = optab_handler (movmisalign_optab, mode))
4791 != CODE_FOR_nothing)
4792 || SLOW_UNALIGNED_ACCESS (mode, align)))
4794 rtx reg, mem;
4796 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4797 reg = force_not_mem (reg);
4798 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4800 if (icode != CODE_FOR_nothing)
4802 struct expand_operand ops[2];
4804 create_fixed_operand (&ops[0], mem);
4805 create_input_operand (&ops[1], reg, mode);
4806 /* The movmisalign<mode> pattern cannot fail, else the assignment
4807 would silently be omitted. */
4808 expand_insn (icode, 2, ops);
4810 else
4811 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4812 return;
4815 /* Assignment of a structure component needs special treatment
4816 if the structure component's rtx is not simply a MEM.
4817 Assignment of an array element at a constant index, and assignment of
4818 an array element in an unaligned packed structure field, has the same
4819 problem. Same for (partially) storing into a non-memory object. */
4820 if (handled_component_p (to)
4821 || (TREE_CODE (to) == MEM_REF
4822 && mem_ref_refers_to_non_mem_p (to))
4823 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4825 machine_mode mode1;
4826 HOST_WIDE_INT bitsize, bitpos;
4827 unsigned HOST_WIDE_INT bitregion_start = 0;
4828 unsigned HOST_WIDE_INT bitregion_end = 0;
4829 tree offset;
4830 int unsignedp;
4831 int volatilep = 0;
4832 tree tem;
4834 push_temp_slots ();
4835 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4836 &unsignedp, &volatilep, true);
4838 /* Make sure bitpos is not negative, it can wreak havoc later. */
4839 if (bitpos < 0)
4841 gcc_assert (offset == NULL_TREE);
4842 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4843 ? 3 : exact_log2 (BITS_PER_UNIT)));
4844 bitpos &= BITS_PER_UNIT - 1;
4847 if (TREE_CODE (to) == COMPONENT_REF
4848 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4849 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4850 /* The C++ memory model naturally applies to byte-aligned fields.
4851 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4852 BITSIZE are not byte-aligned, there is no need to limit the range
4853 we can access. This can occur with packed structures in Ada. */
4854 else if (bitsize > 0
4855 && bitsize % BITS_PER_UNIT == 0
4856 && bitpos % BITS_PER_UNIT == 0)
4858 bitregion_start = bitpos;
4859 bitregion_end = bitpos + bitsize - 1;
4862 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4864 /* If the field has a mode, we want to access it in the
4865 field's mode, not the computed mode.
4866 If a MEM has VOIDmode (external with incomplete type),
4867 use BLKmode for it instead. */
4868 if (MEM_P (to_rtx))
4870 if (mode1 != VOIDmode)
4871 to_rtx = adjust_address (to_rtx, mode1, 0);
4872 else if (GET_MODE (to_rtx) == VOIDmode)
4873 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4876 if (offset != 0)
4878 machine_mode address_mode;
4879 rtx offset_rtx;
4881 if (!MEM_P (to_rtx))
4883 /* We can get constant negative offsets into arrays with broken
4884 user code. Translate this to a trap instead of ICEing. */
4885 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4886 expand_builtin_trap ();
4887 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4890 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4891 address_mode = get_address_mode (to_rtx);
4892 if (GET_MODE (offset_rtx) != address_mode)
4894 /* We cannot be sure that the RTL in offset_rtx is valid outside
4895 of a memory address context, so force it into a register
4896 before attempting to convert it to the desired mode. */
4897 offset_rtx = force_operand (offset_rtx, NULL_RTX);
4898 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4901 /* If we have an expression in OFFSET_RTX and a non-zero
4902 byte offset in BITPOS, adding the byte offset before the
4903 OFFSET_RTX results in better intermediate code, which makes
4904 later rtl optimization passes perform better.
4906 We prefer intermediate code like this:
4908 r124:DI=r123:DI+0x18
4909 [r124:DI]=r121:DI
4911 ... instead of ...
4913 r124:DI=r123:DI+0x10
4914 [r124:DI+0x8]=r121:DI
4916 This is only done for aligned data values, as these can
4917 be expected to result in single move instructions. */
4918 if (mode1 != VOIDmode
4919 && bitpos != 0
4920 && bitsize > 0
4921 && (bitpos % bitsize) == 0
4922 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4923 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4925 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4926 bitregion_start = 0;
4927 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4928 bitregion_end -= bitpos;
4929 bitpos = 0;
4932 to_rtx = offset_address (to_rtx, offset_rtx,
4933 highest_pow2_factor_for_target (to,
4934 offset));
4937 /* No action is needed if the target is not a memory and the field
4938 lies completely outside that target. This can occur if the source
4939 code contains an out-of-bounds access to a small array. */
4940 if (!MEM_P (to_rtx)
4941 && GET_MODE (to_rtx) != BLKmode
4942 && (unsigned HOST_WIDE_INT) bitpos
4943 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4945 expand_normal (from);
4946 result = NULL;
4948 /* Handle expand_expr of a complex value returning a CONCAT. */
4949 else if (GET_CODE (to_rtx) == CONCAT)
4951 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4952 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4953 && bitpos == 0
4954 && bitsize == mode_bitsize)
4955 result = store_expr (from, to_rtx, false, nontemporal);
4956 else if (bitsize == mode_bitsize / 2
4957 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4958 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4959 nontemporal);
4960 else if (bitpos + bitsize <= mode_bitsize / 2)
4961 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4962 bitregion_start, bitregion_end,
4963 mode1, from,
4964 get_alias_set (to), nontemporal);
4965 else if (bitpos >= mode_bitsize / 2)
4966 result = store_field (XEXP (to_rtx, 1), bitsize,
4967 bitpos - mode_bitsize / 2,
4968 bitregion_start, bitregion_end,
4969 mode1, from,
4970 get_alias_set (to), nontemporal);
4971 else if (bitpos == 0 && bitsize == mode_bitsize)
4973 rtx from_rtx;
4974 result = expand_normal (from);
4975 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4976 TYPE_MODE (TREE_TYPE (from)), 0);
4977 emit_move_insn (XEXP (to_rtx, 0),
4978 read_complex_part (from_rtx, false));
4979 emit_move_insn (XEXP (to_rtx, 1),
4980 read_complex_part (from_rtx, true));
4982 else
4984 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4985 GET_MODE_SIZE (GET_MODE (to_rtx)));
4986 write_complex_part (temp, XEXP (to_rtx, 0), false);
4987 write_complex_part (temp, XEXP (to_rtx, 1), true);
4988 result = store_field (temp, bitsize, bitpos,
4989 bitregion_start, bitregion_end,
4990 mode1, from,
4991 get_alias_set (to), nontemporal);
4992 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4993 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4996 else
4998 if (MEM_P (to_rtx))
5000 /* If the field is at offset zero, we could have been given the
5001 DECL_RTX of the parent struct. Don't munge it. */
5002 to_rtx = shallow_copy_rtx (to_rtx);
5003 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5004 if (volatilep)
5005 MEM_VOLATILE_P (to_rtx) = 1;
5008 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5009 bitregion_start, bitregion_end,
5010 mode1,
5011 to_rtx, to, from))
5012 result = NULL;
5013 else
5014 result = store_field (to_rtx, bitsize, bitpos,
5015 bitregion_start, bitregion_end,
5016 mode1, from,
5017 get_alias_set (to), nontemporal);
5020 if (result)
5021 preserve_temp_slots (result);
5022 pop_temp_slots ();
5023 return;
5026 /* If the rhs is a function call and its value is not an aggregate,
5027 call the function before we start to compute the lhs.
5028 This is needed for correct code for cases such as
5029 val = setjmp (buf) on machines where reference to val
5030 requires loading up part of an address in a separate insn.
5032 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5033 since it might be a promoted variable where the zero- or sign- extension
5034 needs to be done. Handling this in the normal way is safe because no
5035 computation is done before the call. The same is true for SSA names. */
5036 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5037 && COMPLETE_TYPE_P (TREE_TYPE (from))
5038 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5039 && ! (((TREE_CODE (to) == VAR_DECL
5040 || TREE_CODE (to) == PARM_DECL
5041 || TREE_CODE (to) == RESULT_DECL)
5042 && REG_P (DECL_RTL (to)))
5043 || TREE_CODE (to) == SSA_NAME))
5045 rtx value;
5046 rtx bounds;
5048 push_temp_slots ();
5049 value = expand_normal (from);
5051 /* Split value and bounds to store them separately. */
5052 chkp_split_slot (value, &value, &bounds);
5054 if (to_rtx == 0)
5055 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5057 /* Handle calls that return values in multiple non-contiguous locations.
5058 The Irix 6 ABI has examples of this. */
5059 if (GET_CODE (to_rtx) == PARALLEL)
5061 if (GET_CODE (value) == PARALLEL)
5062 emit_group_move (to_rtx, value);
5063 else
5064 emit_group_load (to_rtx, value, TREE_TYPE (from),
5065 int_size_in_bytes (TREE_TYPE (from)));
5067 else if (GET_CODE (value) == PARALLEL)
5068 emit_group_store (to_rtx, value, TREE_TYPE (from),
5069 int_size_in_bytes (TREE_TYPE (from)));
5070 else if (GET_MODE (to_rtx) == BLKmode)
5072 /* Handle calls that return BLKmode values in registers. */
5073 if (REG_P (value))
5074 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5075 else
5076 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5078 else
5080 if (POINTER_TYPE_P (TREE_TYPE (to)))
5081 value = convert_memory_address_addr_space
5082 (GET_MODE (to_rtx), value,
5083 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5085 emit_move_insn (to_rtx, value);
5088 /* Store bounds if required. */
5089 if (bounds
5090 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5092 gcc_assert (MEM_P (to_rtx));
5093 chkp_emit_bounds_store (bounds, value, to_rtx);
5096 preserve_temp_slots (to_rtx);
5097 pop_temp_slots ();
5098 return;
5101 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5102 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5104 /* Don't move directly into a return register. */
5105 if (TREE_CODE (to) == RESULT_DECL
5106 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5108 rtx temp;
5110 push_temp_slots ();
5112 /* If the source is itself a return value, it still is in a pseudo at
5113 this point so we can move it back to the return register directly. */
5114 if (REG_P (to_rtx)
5115 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5116 && TREE_CODE (from) != CALL_EXPR)
5117 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5118 else
5119 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5121 /* Handle calls that return values in multiple non-contiguous locations.
5122 The Irix 6 ABI has examples of this. */
5123 if (GET_CODE (to_rtx) == PARALLEL)
5125 if (GET_CODE (temp) == PARALLEL)
5126 emit_group_move (to_rtx, temp);
5127 else
5128 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5129 int_size_in_bytes (TREE_TYPE (from)));
5131 else if (temp)
5132 emit_move_insn (to_rtx, temp);
5134 preserve_temp_slots (to_rtx);
5135 pop_temp_slots ();
5136 return;
5139 /* In case we are returning the contents of an object which overlaps
5140 the place the value is being stored, use a safe function when copying
5141 a value through a pointer into a structure value return block. */
5142 if (TREE_CODE (to) == RESULT_DECL
5143 && TREE_CODE (from) == INDIRECT_REF
5144 && ADDR_SPACE_GENERIC_P
5145 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5146 && refs_may_alias_p (to, from)
5147 && cfun->returns_struct
5148 && !cfun->returns_pcc_struct)
5150 rtx from_rtx, size;
5152 push_temp_slots ();
5153 size = expr_size (from);
5154 from_rtx = expand_normal (from);
5156 emit_library_call (memmove_libfunc, LCT_NORMAL,
5157 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5158 XEXP (from_rtx, 0), Pmode,
5159 convert_to_mode (TYPE_MODE (sizetype),
5160 size, TYPE_UNSIGNED (sizetype)),
5161 TYPE_MODE (sizetype));
5163 preserve_temp_slots (to_rtx);
5164 pop_temp_slots ();
5165 return;
5168 /* Compute FROM and store the value in the rtx we got. */
5170 push_temp_slots ();
5171 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5172 preserve_temp_slots (result);
5173 pop_temp_slots ();
5174 return;
5177 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5178 succeeded, false otherwise. */
5180 bool
5181 emit_storent_insn (rtx to, rtx from)
5183 struct expand_operand ops[2];
5184 machine_mode mode = GET_MODE (to);
5185 enum insn_code code = optab_handler (storent_optab, mode);
5187 if (code == CODE_FOR_nothing)
5188 return false;
5190 create_fixed_operand (&ops[0], to);
5191 create_input_operand (&ops[1], from, mode);
5192 return maybe_expand_insn (code, 2, ops);
5195 /* Generate code for computing expression EXP,
5196 and storing the value into TARGET.
5198 If the mode is BLKmode then we may return TARGET itself.
5199 It turns out that in BLKmode it doesn't cause a problem.
5200 because C has no operators that could combine two different
5201 assignments into the same BLKmode object with different values
5202 with no sequence point. Will other languages need this to
5203 be more thorough?
5205 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5206 stack, and block moves may need to be treated specially.
5208 If NONTEMPORAL is true, try using a nontemporal store instruction.
5210 If BTARGET is not NULL then computed bounds of EXP are
5211 associated with BTARGET. */
5214 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5215 bool nontemporal, tree btarget)
5217 rtx temp;
5218 rtx alt_rtl = NULL_RTX;
5219 location_t loc = curr_insn_location ();
5221 if (VOID_TYPE_P (TREE_TYPE (exp)))
5223 /* C++ can generate ?: expressions with a throw expression in one
5224 branch and an rvalue in the other. Here, we resolve attempts to
5225 store the throw expression's nonexistent result. */
5226 gcc_assert (!call_param_p);
5227 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5228 return NULL_RTX;
5230 if (TREE_CODE (exp) == COMPOUND_EXPR)
5232 /* Perform first part of compound expression, then assign from second
5233 part. */
5234 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5235 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5236 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5237 call_param_p, nontemporal, btarget);
5239 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5241 /* For conditional expression, get safe form of the target. Then
5242 test the condition, doing the appropriate assignment on either
5243 side. This avoids the creation of unnecessary temporaries.
5244 For non-BLKmode, it is more efficient not to do this. */
5246 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5248 do_pending_stack_adjust ();
5249 NO_DEFER_POP;
5250 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5251 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5252 nontemporal, btarget);
5253 emit_jump_insn (targetm.gen_jump (lab2));
5254 emit_barrier ();
5255 emit_label (lab1);
5256 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5257 nontemporal, btarget);
5258 emit_label (lab2);
5259 OK_DEFER_POP;
5261 return NULL_RTX;
5263 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5264 /* If this is a scalar in a register that is stored in a wider mode
5265 than the declared mode, compute the result into its declared mode
5266 and then convert to the wider mode. Our value is the computed
5267 expression. */
5269 rtx inner_target = 0;
5271 /* We can do the conversion inside EXP, which will often result
5272 in some optimizations. Do the conversion in two steps: first
5273 change the signedness, if needed, then the extend. But don't
5274 do this if the type of EXP is a subtype of something else
5275 since then the conversion might involve more than just
5276 converting modes. */
5277 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5278 && TREE_TYPE (TREE_TYPE (exp)) == 0
5279 && GET_MODE_PRECISION (GET_MODE (target))
5280 == TYPE_PRECISION (TREE_TYPE (exp)))
5282 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5283 TYPE_UNSIGNED (TREE_TYPE (exp))))
5285 /* Some types, e.g. Fortran's logical*4, won't have a signed
5286 version, so use the mode instead. */
5287 tree ntype
5288 = (signed_or_unsigned_type_for
5289 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5290 if (ntype == NULL)
5291 ntype = lang_hooks.types.type_for_mode
5292 (TYPE_MODE (TREE_TYPE (exp)),
5293 SUBREG_PROMOTED_SIGN (target));
5295 exp = fold_convert_loc (loc, ntype, exp);
5298 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5299 (GET_MODE (SUBREG_REG (target)),
5300 SUBREG_PROMOTED_SIGN (target)),
5301 exp);
5303 inner_target = SUBREG_REG (target);
5306 temp = expand_expr (exp, inner_target, VOIDmode,
5307 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5309 /* Handle bounds returned by call. */
5310 if (TREE_CODE (exp) == CALL_EXPR)
5312 rtx bounds;
5313 chkp_split_slot (temp, &temp, &bounds);
5314 if (bounds && btarget)
5316 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5317 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5318 chkp_set_rtl_bounds (btarget, tmp);
5322 /* If TEMP is a VOIDmode constant, use convert_modes to make
5323 sure that we properly convert it. */
5324 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5326 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5327 temp, SUBREG_PROMOTED_SIGN (target));
5328 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5329 GET_MODE (target), temp,
5330 SUBREG_PROMOTED_SIGN (target));
5333 convert_move (SUBREG_REG (target), temp,
5334 SUBREG_PROMOTED_SIGN (target));
5336 return NULL_RTX;
5338 else if ((TREE_CODE (exp) == STRING_CST
5339 || (TREE_CODE (exp) == MEM_REF
5340 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5341 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5342 == STRING_CST
5343 && integer_zerop (TREE_OPERAND (exp, 1))))
5344 && !nontemporal && !call_param_p
5345 && MEM_P (target))
5347 /* Optimize initialization of an array with a STRING_CST. */
5348 HOST_WIDE_INT exp_len, str_copy_len;
5349 rtx dest_mem;
5350 tree str = TREE_CODE (exp) == STRING_CST
5351 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5353 exp_len = int_expr_size (exp);
5354 if (exp_len <= 0)
5355 goto normal_expr;
5357 if (TREE_STRING_LENGTH (str) <= 0)
5358 goto normal_expr;
5360 str_copy_len = strlen (TREE_STRING_POINTER (str));
5361 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5362 goto normal_expr;
5364 str_copy_len = TREE_STRING_LENGTH (str);
5365 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5366 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5368 str_copy_len += STORE_MAX_PIECES - 1;
5369 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5371 str_copy_len = MIN (str_copy_len, exp_len);
5372 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5373 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5374 MEM_ALIGN (target), false))
5375 goto normal_expr;
5377 dest_mem = target;
5379 dest_mem = store_by_pieces (dest_mem,
5380 str_copy_len, builtin_strncpy_read_str,
5381 CONST_CAST (char *,
5382 TREE_STRING_POINTER (str)),
5383 MEM_ALIGN (target), false,
5384 exp_len > str_copy_len ? 1 : 0);
5385 if (exp_len > str_copy_len)
5386 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5387 GEN_INT (exp_len - str_copy_len),
5388 BLOCK_OP_NORMAL);
5389 return NULL_RTX;
5391 else
5393 rtx tmp_target;
5395 normal_expr:
5396 /* If we want to use a nontemporal store, force the value to
5397 register first. */
5398 tmp_target = nontemporal ? NULL_RTX : target;
5399 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5400 (call_param_p
5401 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5402 &alt_rtl, false);
5404 /* Handle bounds returned by call. */
5405 if (TREE_CODE (exp) == CALL_EXPR)
5407 rtx bounds;
5408 chkp_split_slot (temp, &temp, &bounds);
5409 if (bounds && btarget)
5411 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5412 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5413 chkp_set_rtl_bounds (btarget, tmp);
5418 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5419 the same as that of TARGET, adjust the constant. This is needed, for
5420 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5421 only a word-sized value. */
5422 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5423 && TREE_CODE (exp) != ERROR_MARK
5424 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5425 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5426 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5428 /* If value was not generated in the target, store it there.
5429 Convert the value to TARGET's type first if necessary and emit the
5430 pending incrementations that have been queued when expanding EXP.
5431 Note that we cannot emit the whole queue blindly because this will
5432 effectively disable the POST_INC optimization later.
5434 If TEMP and TARGET compare equal according to rtx_equal_p, but
5435 one or both of them are volatile memory refs, we have to distinguish
5436 two cases:
5437 - expand_expr has used TARGET. In this case, we must not generate
5438 another copy. This can be detected by TARGET being equal according
5439 to == .
5440 - expand_expr has not used TARGET - that means that the source just
5441 happens to have the same RTX form. Since temp will have been created
5442 by expand_expr, it will compare unequal according to == .
5443 We must generate a copy in this case, to reach the correct number
5444 of volatile memory references. */
5446 if ((! rtx_equal_p (temp, target)
5447 || (temp != target && (side_effects_p (temp)
5448 || side_effects_p (target))))
5449 && TREE_CODE (exp) != ERROR_MARK
5450 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5451 but TARGET is not valid memory reference, TEMP will differ
5452 from TARGET although it is really the same location. */
5453 && !(alt_rtl
5454 && rtx_equal_p (alt_rtl, target)
5455 && !side_effects_p (alt_rtl)
5456 && !side_effects_p (target))
5457 /* If there's nothing to copy, don't bother. Don't call
5458 expr_size unless necessary, because some front-ends (C++)
5459 expr_size-hook must not be given objects that are not
5460 supposed to be bit-copied or bit-initialized. */
5461 && expr_size (exp) != const0_rtx)
5463 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5465 if (GET_MODE (target) == BLKmode)
5467 /* Handle calls that return BLKmode values in registers. */
5468 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5469 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5470 else
5471 store_bit_field (target,
5472 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5473 0, 0, 0, GET_MODE (temp), temp);
5475 else
5476 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5479 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5481 /* Handle copying a string constant into an array. The string
5482 constant may be shorter than the array. So copy just the string's
5483 actual length, and clear the rest. First get the size of the data
5484 type of the string, which is actually the size of the target. */
5485 rtx size = expr_size (exp);
5487 if (CONST_INT_P (size)
5488 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5489 emit_block_move (target, temp, size,
5490 (call_param_p
5491 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5492 else
5494 machine_mode pointer_mode
5495 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5496 machine_mode address_mode = get_address_mode (target);
5498 /* Compute the size of the data to copy from the string. */
5499 tree copy_size
5500 = size_binop_loc (loc, MIN_EXPR,
5501 make_tree (sizetype, size),
5502 size_int (TREE_STRING_LENGTH (exp)));
5503 rtx copy_size_rtx
5504 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5505 (call_param_p
5506 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5507 rtx_code_label *label = 0;
5509 /* Copy that much. */
5510 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5511 TYPE_UNSIGNED (sizetype));
5512 emit_block_move (target, temp, copy_size_rtx,
5513 (call_param_p
5514 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5516 /* Figure out how much is left in TARGET that we have to clear.
5517 Do all calculations in pointer_mode. */
5518 if (CONST_INT_P (copy_size_rtx))
5520 size = plus_constant (address_mode, size,
5521 -INTVAL (copy_size_rtx));
5522 target = adjust_address (target, BLKmode,
5523 INTVAL (copy_size_rtx));
5525 else
5527 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5528 copy_size_rtx, NULL_RTX, 0,
5529 OPTAB_LIB_WIDEN);
5531 if (GET_MODE (copy_size_rtx) != address_mode)
5532 copy_size_rtx = convert_to_mode (address_mode,
5533 copy_size_rtx,
5534 TYPE_UNSIGNED (sizetype));
5536 target = offset_address (target, copy_size_rtx,
5537 highest_pow2_factor (copy_size));
5538 label = gen_label_rtx ();
5539 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5540 GET_MODE (size), 0, label);
5543 if (size != const0_rtx)
5544 clear_storage (target, size, BLOCK_OP_NORMAL);
5546 if (label)
5547 emit_label (label);
5550 /* Handle calls that return values in multiple non-contiguous locations.
5551 The Irix 6 ABI has examples of this. */
5552 else if (GET_CODE (target) == PARALLEL)
5554 if (GET_CODE (temp) == PARALLEL)
5555 emit_group_move (target, temp);
5556 else
5557 emit_group_load (target, temp, TREE_TYPE (exp),
5558 int_size_in_bytes (TREE_TYPE (exp)));
5560 else if (GET_CODE (temp) == PARALLEL)
5561 emit_group_store (target, temp, TREE_TYPE (exp),
5562 int_size_in_bytes (TREE_TYPE (exp)));
5563 else if (GET_MODE (temp) == BLKmode)
5564 emit_block_move (target, temp, expr_size (exp),
5565 (call_param_p
5566 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5567 /* If we emit a nontemporal store, there is nothing else to do. */
5568 else if (nontemporal && emit_storent_insn (target, temp))
5570 else
5572 temp = force_operand (temp, target);
5573 if (temp != target)
5574 emit_move_insn (target, temp);
5578 return NULL_RTX;
5581 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5583 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5585 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5588 /* Return true if field F of structure TYPE is a flexible array. */
5590 static bool
5591 flexible_array_member_p (const_tree f, const_tree type)
5593 const_tree tf;
5595 tf = TREE_TYPE (f);
5596 return (DECL_CHAIN (f) == NULL
5597 && TREE_CODE (tf) == ARRAY_TYPE
5598 && TYPE_DOMAIN (tf)
5599 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5600 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5601 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5602 && int_size_in_bytes (type) >= 0);
5605 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5606 must have in order for it to completely initialize a value of type TYPE.
5607 Return -1 if the number isn't known.
5609 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5611 static HOST_WIDE_INT
5612 count_type_elements (const_tree type, bool for_ctor_p)
5614 switch (TREE_CODE (type))
5616 case ARRAY_TYPE:
5618 tree nelts;
5620 nelts = array_type_nelts (type);
5621 if (nelts && tree_fits_uhwi_p (nelts))
5623 unsigned HOST_WIDE_INT n;
5625 n = tree_to_uhwi (nelts) + 1;
5626 if (n == 0 || for_ctor_p)
5627 return n;
5628 else
5629 return n * count_type_elements (TREE_TYPE (type), false);
5631 return for_ctor_p ? -1 : 1;
5634 case RECORD_TYPE:
5636 unsigned HOST_WIDE_INT n;
5637 tree f;
5639 n = 0;
5640 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5641 if (TREE_CODE (f) == FIELD_DECL)
5643 if (!for_ctor_p)
5644 n += count_type_elements (TREE_TYPE (f), false);
5645 else if (!flexible_array_member_p (f, type))
5646 /* Don't count flexible arrays, which are not supposed
5647 to be initialized. */
5648 n += 1;
5651 return n;
5654 case UNION_TYPE:
5655 case QUAL_UNION_TYPE:
5657 tree f;
5658 HOST_WIDE_INT n, m;
5660 gcc_assert (!for_ctor_p);
5661 /* Estimate the number of scalars in each field and pick the
5662 maximum. Other estimates would do instead; the idea is simply
5663 to make sure that the estimate is not sensitive to the ordering
5664 of the fields. */
5665 n = 1;
5666 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5667 if (TREE_CODE (f) == FIELD_DECL)
5669 m = count_type_elements (TREE_TYPE (f), false);
5670 /* If the field doesn't span the whole union, add an extra
5671 scalar for the rest. */
5672 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5673 TYPE_SIZE (type)) != 1)
5674 m++;
5675 if (n < m)
5676 n = m;
5678 return n;
5681 case COMPLEX_TYPE:
5682 return 2;
5684 case VECTOR_TYPE:
5685 return TYPE_VECTOR_SUBPARTS (type);
5687 case INTEGER_TYPE:
5688 case REAL_TYPE:
5689 case FIXED_POINT_TYPE:
5690 case ENUMERAL_TYPE:
5691 case BOOLEAN_TYPE:
5692 case POINTER_TYPE:
5693 case OFFSET_TYPE:
5694 case REFERENCE_TYPE:
5695 case NULLPTR_TYPE:
5696 return 1;
5698 case ERROR_MARK:
5699 return 0;
5701 case VOID_TYPE:
5702 case METHOD_TYPE:
5703 case FUNCTION_TYPE:
5704 case LANG_TYPE:
5705 default:
5706 gcc_unreachable ();
5710 /* Helper for categorize_ctor_elements. Identical interface. */
5712 static bool
5713 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5714 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5716 unsigned HOST_WIDE_INT idx;
5717 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5718 tree value, purpose, elt_type;
5720 /* Whether CTOR is a valid constant initializer, in accordance with what
5721 initializer_constant_valid_p does. If inferred from the constructor
5722 elements, true until proven otherwise. */
5723 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5724 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5726 nz_elts = 0;
5727 init_elts = 0;
5728 num_fields = 0;
5729 elt_type = NULL_TREE;
5731 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5733 HOST_WIDE_INT mult = 1;
5735 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5737 tree lo_index = TREE_OPERAND (purpose, 0);
5738 tree hi_index = TREE_OPERAND (purpose, 1);
5740 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5741 mult = (tree_to_uhwi (hi_index)
5742 - tree_to_uhwi (lo_index) + 1);
5744 num_fields += mult;
5745 elt_type = TREE_TYPE (value);
5747 switch (TREE_CODE (value))
5749 case CONSTRUCTOR:
5751 HOST_WIDE_INT nz = 0, ic = 0;
5753 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5754 p_complete);
5756 nz_elts += mult * nz;
5757 init_elts += mult * ic;
5759 if (const_from_elts_p && const_p)
5760 const_p = const_elt_p;
5762 break;
5764 case INTEGER_CST:
5765 case REAL_CST:
5766 case FIXED_CST:
5767 if (!initializer_zerop (value))
5768 nz_elts += mult;
5769 init_elts += mult;
5770 break;
5772 case STRING_CST:
5773 nz_elts += mult * TREE_STRING_LENGTH (value);
5774 init_elts += mult * TREE_STRING_LENGTH (value);
5775 break;
5777 case COMPLEX_CST:
5778 if (!initializer_zerop (TREE_REALPART (value)))
5779 nz_elts += mult;
5780 if (!initializer_zerop (TREE_IMAGPART (value)))
5781 nz_elts += mult;
5782 init_elts += mult;
5783 break;
5785 case VECTOR_CST:
5787 unsigned i;
5788 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5790 tree v = VECTOR_CST_ELT (value, i);
5791 if (!initializer_zerop (v))
5792 nz_elts += mult;
5793 init_elts += mult;
5796 break;
5798 default:
5800 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5801 nz_elts += mult * tc;
5802 init_elts += mult * tc;
5804 if (const_from_elts_p && const_p)
5805 const_p = initializer_constant_valid_p (value, elt_type)
5806 != NULL_TREE;
5808 break;
5812 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5813 num_fields, elt_type))
5814 *p_complete = false;
5816 *p_nz_elts += nz_elts;
5817 *p_init_elts += init_elts;
5819 return const_p;
5822 /* Examine CTOR to discover:
5823 * how many scalar fields are set to nonzero values,
5824 and place it in *P_NZ_ELTS;
5825 * how many scalar fields in total are in CTOR,
5826 and place it in *P_ELT_COUNT.
5827 * whether the constructor is complete -- in the sense that every
5828 meaningful byte is explicitly given a value --
5829 and place it in *P_COMPLETE.
5831 Return whether or not CTOR is a valid static constant initializer, the same
5832 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5834 bool
5835 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5836 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5838 *p_nz_elts = 0;
5839 *p_init_elts = 0;
5840 *p_complete = true;
5842 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5845 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5846 of which had type LAST_TYPE. Each element was itself a complete
5847 initializer, in the sense that every meaningful byte was explicitly
5848 given a value. Return true if the same is true for the constructor
5849 as a whole. */
5851 bool
5852 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5853 const_tree last_type)
5855 if (TREE_CODE (type) == UNION_TYPE
5856 || TREE_CODE (type) == QUAL_UNION_TYPE)
5858 if (num_elts == 0)
5859 return false;
5861 gcc_assert (num_elts == 1 && last_type);
5863 /* ??? We could look at each element of the union, and find the
5864 largest element. Which would avoid comparing the size of the
5865 initialized element against any tail padding in the union.
5866 Doesn't seem worth the effort... */
5867 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5870 return count_type_elements (type, true) == num_elts;
5873 /* Return 1 if EXP contains mostly (3/4) zeros. */
5875 static int
5876 mostly_zeros_p (const_tree exp)
5878 if (TREE_CODE (exp) == CONSTRUCTOR)
5880 HOST_WIDE_INT nz_elts, init_elts;
5881 bool complete_p;
5883 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5884 return !complete_p || nz_elts < init_elts / 4;
5887 return initializer_zerop (exp);
5890 /* Return 1 if EXP contains all zeros. */
5892 static int
5893 all_zeros_p (const_tree exp)
5895 if (TREE_CODE (exp) == CONSTRUCTOR)
5897 HOST_WIDE_INT nz_elts, init_elts;
5898 bool complete_p;
5900 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5901 return nz_elts == 0;
5904 return initializer_zerop (exp);
5907 /* Helper function for store_constructor.
5908 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5909 CLEARED is as for store_constructor.
5910 ALIAS_SET is the alias set to use for any stores.
5912 This provides a recursive shortcut back to store_constructor when it isn't
5913 necessary to go through store_field. This is so that we can pass through
5914 the cleared field to let store_constructor know that we may not have to
5915 clear a substructure if the outer structure has already been cleared. */
5917 static void
5918 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5919 HOST_WIDE_INT bitpos, machine_mode mode,
5920 tree exp, int cleared, alias_set_type alias_set)
5922 if (TREE_CODE (exp) == CONSTRUCTOR
5923 /* We can only call store_constructor recursively if the size and
5924 bit position are on a byte boundary. */
5925 && bitpos % BITS_PER_UNIT == 0
5926 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5927 /* If we have a nonzero bitpos for a register target, then we just
5928 let store_field do the bitfield handling. This is unlikely to
5929 generate unnecessary clear instructions anyways. */
5930 && (bitpos == 0 || MEM_P (target)))
5932 if (MEM_P (target))
5933 target
5934 = adjust_address (target,
5935 GET_MODE (target) == BLKmode
5936 || 0 != (bitpos
5937 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5938 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5941 /* Update the alias set, if required. */
5942 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5943 && MEM_ALIAS_SET (target) != 0)
5945 target = copy_rtx (target);
5946 set_mem_alias_set (target, alias_set);
5949 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5951 else
5952 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5956 /* Returns the number of FIELD_DECLs in TYPE. */
5958 static int
5959 fields_length (const_tree type)
5961 tree t = TYPE_FIELDS (type);
5962 int count = 0;
5964 for (; t; t = DECL_CHAIN (t))
5965 if (TREE_CODE (t) == FIELD_DECL)
5966 ++count;
5968 return count;
5972 /* Store the value of constructor EXP into the rtx TARGET.
5973 TARGET is either a REG or a MEM; we know it cannot conflict, since
5974 safe_from_p has been called.
5975 CLEARED is true if TARGET is known to have been zero'd.
5976 SIZE is the number of bytes of TARGET we are allowed to modify: this
5977 may not be the same as the size of EXP if we are assigning to a field
5978 which has been packed to exclude padding bits. */
5980 static void
5981 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5983 tree type = TREE_TYPE (exp);
5984 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5986 switch (TREE_CODE (type))
5988 case RECORD_TYPE:
5989 case UNION_TYPE:
5990 case QUAL_UNION_TYPE:
5992 unsigned HOST_WIDE_INT idx;
5993 tree field, value;
5995 /* If size is zero or the target is already cleared, do nothing. */
5996 if (size == 0 || cleared)
5997 cleared = 1;
5998 /* We either clear the aggregate or indicate the value is dead. */
5999 else if ((TREE_CODE (type) == UNION_TYPE
6000 || TREE_CODE (type) == QUAL_UNION_TYPE)
6001 && ! CONSTRUCTOR_ELTS (exp))
6002 /* If the constructor is empty, clear the union. */
6004 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6005 cleared = 1;
6008 /* If we are building a static constructor into a register,
6009 set the initial value as zero so we can fold the value into
6010 a constant. But if more than one register is involved,
6011 this probably loses. */
6012 else if (REG_P (target) && TREE_STATIC (exp)
6013 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6015 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6016 cleared = 1;
6019 /* If the constructor has fewer fields than the structure or
6020 if we are initializing the structure to mostly zeros, clear
6021 the whole structure first. Don't do this if TARGET is a
6022 register whose mode size isn't equal to SIZE since
6023 clear_storage can't handle this case. */
6024 else if (size > 0
6025 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
6026 != fields_length (type))
6027 || mostly_zeros_p (exp))
6028 && (!REG_P (target)
6029 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6030 == size)))
6032 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6033 cleared = 1;
6036 if (REG_P (target) && !cleared)
6037 emit_clobber (target);
6039 /* Store each element of the constructor into the
6040 corresponding field of TARGET. */
6041 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6043 machine_mode mode;
6044 HOST_WIDE_INT bitsize;
6045 HOST_WIDE_INT bitpos = 0;
6046 tree offset;
6047 rtx to_rtx = target;
6049 /* Just ignore missing fields. We cleared the whole
6050 structure, above, if any fields are missing. */
6051 if (field == 0)
6052 continue;
6054 if (cleared && initializer_zerop (value))
6055 continue;
6057 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6058 bitsize = tree_to_uhwi (DECL_SIZE (field));
6059 else
6060 bitsize = -1;
6062 mode = DECL_MODE (field);
6063 if (DECL_BIT_FIELD (field))
6064 mode = VOIDmode;
6066 offset = DECL_FIELD_OFFSET (field);
6067 if (tree_fits_shwi_p (offset)
6068 && tree_fits_shwi_p (bit_position (field)))
6070 bitpos = int_bit_position (field);
6071 offset = 0;
6073 else
6074 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6076 if (offset)
6078 machine_mode address_mode;
6079 rtx offset_rtx;
6081 offset
6082 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6083 make_tree (TREE_TYPE (exp),
6084 target));
6086 offset_rtx = expand_normal (offset);
6087 gcc_assert (MEM_P (to_rtx));
6089 address_mode = get_address_mode (to_rtx);
6090 if (GET_MODE (offset_rtx) != address_mode)
6091 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6093 to_rtx = offset_address (to_rtx, offset_rtx,
6094 highest_pow2_factor (offset));
6097 /* If this initializes a field that is smaller than a
6098 word, at the start of a word, try to widen it to a full
6099 word. This special case allows us to output C++ member
6100 function initializations in a form that the optimizers
6101 can understand. */
6102 if (WORD_REGISTER_OPERATIONS
6103 && REG_P (target)
6104 && bitsize < BITS_PER_WORD
6105 && bitpos % BITS_PER_WORD == 0
6106 && GET_MODE_CLASS (mode) == MODE_INT
6107 && TREE_CODE (value) == INTEGER_CST
6108 && exp_size >= 0
6109 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6111 tree type = TREE_TYPE (value);
6113 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6115 type = lang_hooks.types.type_for_mode
6116 (word_mode, TYPE_UNSIGNED (type));
6117 value = fold_convert (type, value);
6120 if (BYTES_BIG_ENDIAN)
6121 value
6122 = fold_build2 (LSHIFT_EXPR, type, value,
6123 build_int_cst (type,
6124 BITS_PER_WORD - bitsize));
6125 bitsize = BITS_PER_WORD;
6126 mode = word_mode;
6129 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6130 && DECL_NONADDRESSABLE_P (field))
6132 to_rtx = copy_rtx (to_rtx);
6133 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6136 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6137 value, cleared,
6138 get_alias_set (TREE_TYPE (field)));
6140 break;
6142 case ARRAY_TYPE:
6144 tree value, index;
6145 unsigned HOST_WIDE_INT i;
6146 int need_to_clear;
6147 tree domain;
6148 tree elttype = TREE_TYPE (type);
6149 int const_bounds_p;
6150 HOST_WIDE_INT minelt = 0;
6151 HOST_WIDE_INT maxelt = 0;
6153 domain = TYPE_DOMAIN (type);
6154 const_bounds_p = (TYPE_MIN_VALUE (domain)
6155 && TYPE_MAX_VALUE (domain)
6156 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6157 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6159 /* If we have constant bounds for the range of the type, get them. */
6160 if (const_bounds_p)
6162 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6163 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6166 /* If the constructor has fewer elements than the array, clear
6167 the whole array first. Similarly if this is static
6168 constructor of a non-BLKmode object. */
6169 if (cleared)
6170 need_to_clear = 0;
6171 else if (REG_P (target) && TREE_STATIC (exp))
6172 need_to_clear = 1;
6173 else
6175 unsigned HOST_WIDE_INT idx;
6176 tree index, value;
6177 HOST_WIDE_INT count = 0, zero_count = 0;
6178 need_to_clear = ! const_bounds_p;
6180 /* This loop is a more accurate version of the loop in
6181 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6182 is also needed to check for missing elements. */
6183 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6185 HOST_WIDE_INT this_node_count;
6187 if (need_to_clear)
6188 break;
6190 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6192 tree lo_index = TREE_OPERAND (index, 0);
6193 tree hi_index = TREE_OPERAND (index, 1);
6195 if (! tree_fits_uhwi_p (lo_index)
6196 || ! tree_fits_uhwi_p (hi_index))
6198 need_to_clear = 1;
6199 break;
6202 this_node_count = (tree_to_uhwi (hi_index)
6203 - tree_to_uhwi (lo_index) + 1);
6205 else
6206 this_node_count = 1;
6208 count += this_node_count;
6209 if (mostly_zeros_p (value))
6210 zero_count += this_node_count;
6213 /* Clear the entire array first if there are any missing
6214 elements, or if the incidence of zero elements is >=
6215 75%. */
6216 if (! need_to_clear
6217 && (count < maxelt - minelt + 1
6218 || 4 * zero_count >= 3 * count))
6219 need_to_clear = 1;
6222 if (need_to_clear && size > 0)
6224 if (REG_P (target))
6225 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6226 else
6227 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6228 cleared = 1;
6231 if (!cleared && REG_P (target))
6232 /* Inform later passes that the old value is dead. */
6233 emit_clobber (target);
6235 /* Store each element of the constructor into the
6236 corresponding element of TARGET, determined by counting the
6237 elements. */
6238 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6240 machine_mode mode;
6241 HOST_WIDE_INT bitsize;
6242 HOST_WIDE_INT bitpos;
6243 rtx xtarget = target;
6245 if (cleared && initializer_zerop (value))
6246 continue;
6248 mode = TYPE_MODE (elttype);
6249 if (mode == BLKmode)
6250 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6251 ? tree_to_uhwi (TYPE_SIZE (elttype))
6252 : -1);
6253 else
6254 bitsize = GET_MODE_BITSIZE (mode);
6256 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6258 tree lo_index = TREE_OPERAND (index, 0);
6259 tree hi_index = TREE_OPERAND (index, 1);
6260 rtx index_r, pos_rtx;
6261 HOST_WIDE_INT lo, hi, count;
6262 tree position;
6264 /* If the range is constant and "small", unroll the loop. */
6265 if (const_bounds_p
6266 && tree_fits_shwi_p (lo_index)
6267 && tree_fits_shwi_p (hi_index)
6268 && (lo = tree_to_shwi (lo_index),
6269 hi = tree_to_shwi (hi_index),
6270 count = hi - lo + 1,
6271 (!MEM_P (target)
6272 || count <= 2
6273 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6274 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6275 <= 40 * 8)))))
6277 lo -= minelt; hi -= minelt;
6278 for (; lo <= hi; lo++)
6280 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6282 if (MEM_P (target)
6283 && !MEM_KEEP_ALIAS_SET_P (target)
6284 && TREE_CODE (type) == ARRAY_TYPE
6285 && TYPE_NONALIASED_COMPONENT (type))
6287 target = copy_rtx (target);
6288 MEM_KEEP_ALIAS_SET_P (target) = 1;
6291 store_constructor_field
6292 (target, bitsize, bitpos, mode, value, cleared,
6293 get_alias_set (elttype));
6296 else
6298 rtx_code_label *loop_start = gen_label_rtx ();
6299 rtx_code_label *loop_end = gen_label_rtx ();
6300 tree exit_cond;
6302 expand_normal (hi_index);
6304 index = build_decl (EXPR_LOCATION (exp),
6305 VAR_DECL, NULL_TREE, domain);
6306 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6307 SET_DECL_RTL (index, index_r);
6308 store_expr (lo_index, index_r, 0, false);
6310 /* Build the head of the loop. */
6311 do_pending_stack_adjust ();
6312 emit_label (loop_start);
6314 /* Assign value to element index. */
6315 position =
6316 fold_convert (ssizetype,
6317 fold_build2 (MINUS_EXPR,
6318 TREE_TYPE (index),
6319 index,
6320 TYPE_MIN_VALUE (domain)));
6322 position =
6323 size_binop (MULT_EXPR, position,
6324 fold_convert (ssizetype,
6325 TYPE_SIZE_UNIT (elttype)));
6327 pos_rtx = expand_normal (position);
6328 xtarget = offset_address (target, pos_rtx,
6329 highest_pow2_factor (position));
6330 xtarget = adjust_address (xtarget, mode, 0);
6331 if (TREE_CODE (value) == CONSTRUCTOR)
6332 store_constructor (value, xtarget, cleared,
6333 bitsize / BITS_PER_UNIT);
6334 else
6335 store_expr (value, xtarget, 0, false);
6337 /* Generate a conditional jump to exit the loop. */
6338 exit_cond = build2 (LT_EXPR, integer_type_node,
6339 index, hi_index);
6340 jumpif (exit_cond, loop_end, -1);
6342 /* Update the loop counter, and jump to the head of
6343 the loop. */
6344 expand_assignment (index,
6345 build2 (PLUS_EXPR, TREE_TYPE (index),
6346 index, integer_one_node),
6347 false);
6349 emit_jump (loop_start);
6351 /* Build the end of the loop. */
6352 emit_label (loop_end);
6355 else if ((index != 0 && ! tree_fits_shwi_p (index))
6356 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6358 tree position;
6360 if (index == 0)
6361 index = ssize_int (1);
6363 if (minelt)
6364 index = fold_convert (ssizetype,
6365 fold_build2 (MINUS_EXPR,
6366 TREE_TYPE (index),
6367 index,
6368 TYPE_MIN_VALUE (domain)));
6370 position =
6371 size_binop (MULT_EXPR, index,
6372 fold_convert (ssizetype,
6373 TYPE_SIZE_UNIT (elttype)));
6374 xtarget = offset_address (target,
6375 expand_normal (position),
6376 highest_pow2_factor (position));
6377 xtarget = adjust_address (xtarget, mode, 0);
6378 store_expr (value, xtarget, 0, false);
6380 else
6382 if (index != 0)
6383 bitpos = ((tree_to_shwi (index) - minelt)
6384 * tree_to_uhwi (TYPE_SIZE (elttype)));
6385 else
6386 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6388 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6389 && TREE_CODE (type) == ARRAY_TYPE
6390 && TYPE_NONALIASED_COMPONENT (type))
6392 target = copy_rtx (target);
6393 MEM_KEEP_ALIAS_SET_P (target) = 1;
6395 store_constructor_field (target, bitsize, bitpos, mode, value,
6396 cleared, get_alias_set (elttype));
6399 break;
6402 case VECTOR_TYPE:
6404 unsigned HOST_WIDE_INT idx;
6405 constructor_elt *ce;
6406 int i;
6407 int need_to_clear;
6408 int icode = CODE_FOR_nothing;
6409 tree elttype = TREE_TYPE (type);
6410 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6411 machine_mode eltmode = TYPE_MODE (elttype);
6412 HOST_WIDE_INT bitsize;
6413 HOST_WIDE_INT bitpos;
6414 rtvec vector = NULL;
6415 unsigned n_elts;
6416 alias_set_type alias;
6418 gcc_assert (eltmode != BLKmode);
6420 n_elts = TYPE_VECTOR_SUBPARTS (type);
6421 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6423 machine_mode mode = GET_MODE (target);
6425 icode = (int) optab_handler (vec_init_optab, mode);
6426 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6427 if (icode != CODE_FOR_nothing)
6429 tree value;
6431 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6432 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6434 icode = CODE_FOR_nothing;
6435 break;
6438 if (icode != CODE_FOR_nothing)
6440 unsigned int i;
6442 vector = rtvec_alloc (n_elts);
6443 for (i = 0; i < n_elts; i++)
6444 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6448 /* If the constructor has fewer elements than the vector,
6449 clear the whole array first. Similarly if this is static
6450 constructor of a non-BLKmode object. */
6451 if (cleared)
6452 need_to_clear = 0;
6453 else if (REG_P (target) && TREE_STATIC (exp))
6454 need_to_clear = 1;
6455 else
6457 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6458 tree value;
6460 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6462 int n_elts_here = tree_to_uhwi
6463 (int_const_binop (TRUNC_DIV_EXPR,
6464 TYPE_SIZE (TREE_TYPE (value)),
6465 TYPE_SIZE (elttype)));
6467 count += n_elts_here;
6468 if (mostly_zeros_p (value))
6469 zero_count += n_elts_here;
6472 /* Clear the entire vector first if there are any missing elements,
6473 or if the incidence of zero elements is >= 75%. */
6474 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6477 if (need_to_clear && size > 0 && !vector)
6479 if (REG_P (target))
6480 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6481 else
6482 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6483 cleared = 1;
6486 /* Inform later passes that the old value is dead. */
6487 if (!cleared && !vector && REG_P (target))
6488 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6490 if (MEM_P (target))
6491 alias = MEM_ALIAS_SET (target);
6492 else
6493 alias = get_alias_set (elttype);
6495 /* Store each element of the constructor into the corresponding
6496 element of TARGET, determined by counting the elements. */
6497 for (idx = 0, i = 0;
6498 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6499 idx++, i += bitsize / elt_size)
6501 HOST_WIDE_INT eltpos;
6502 tree value = ce->value;
6504 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6505 if (cleared && initializer_zerop (value))
6506 continue;
6508 if (ce->index)
6509 eltpos = tree_to_uhwi (ce->index);
6510 else
6511 eltpos = i;
6513 if (vector)
6515 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6516 elements. */
6517 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6518 RTVEC_ELT (vector, eltpos)
6519 = expand_normal (value);
6521 else
6523 machine_mode value_mode =
6524 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6525 ? TYPE_MODE (TREE_TYPE (value))
6526 : eltmode;
6527 bitpos = eltpos * elt_size;
6528 store_constructor_field (target, bitsize, bitpos, value_mode,
6529 value, cleared, alias);
6533 if (vector)
6534 emit_insn (GEN_FCN (icode)
6535 (target,
6536 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6537 break;
6540 default:
6541 gcc_unreachable ();
6545 /* Store the value of EXP (an expression tree)
6546 into a subfield of TARGET which has mode MODE and occupies
6547 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6548 If MODE is VOIDmode, it means that we are storing into a bit-field.
6550 BITREGION_START is bitpos of the first bitfield in this region.
6551 BITREGION_END is the bitpos of the ending bitfield in this region.
6552 These two fields are 0, if the C++ memory model does not apply,
6553 or we are not interested in keeping track of bitfield regions.
6555 Always return const0_rtx unless we have something particular to
6556 return.
6558 ALIAS_SET is the alias set for the destination. This value will
6559 (in general) be different from that for TARGET, since TARGET is a
6560 reference to the containing structure.
6562 If NONTEMPORAL is true, try generating a nontemporal store. */
6564 static rtx
6565 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6566 unsigned HOST_WIDE_INT bitregion_start,
6567 unsigned HOST_WIDE_INT bitregion_end,
6568 machine_mode mode, tree exp,
6569 alias_set_type alias_set, bool nontemporal)
6571 if (TREE_CODE (exp) == ERROR_MARK)
6572 return const0_rtx;
6574 /* If we have nothing to store, do nothing unless the expression has
6575 side-effects. */
6576 if (bitsize == 0)
6577 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6579 if (GET_CODE (target) == CONCAT)
6581 /* We're storing into a struct containing a single __complex. */
6583 gcc_assert (!bitpos);
6584 return store_expr (exp, target, 0, nontemporal);
6587 /* If the structure is in a register or if the component
6588 is a bit field, we cannot use addressing to access it.
6589 Use bit-field techniques or SUBREG to store in it. */
6591 if (mode == VOIDmode
6592 || (mode != BLKmode && ! direct_store[(int) mode]
6593 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6594 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6595 || REG_P (target)
6596 || GET_CODE (target) == SUBREG
6597 /* If the field isn't aligned enough to store as an ordinary memref,
6598 store it as a bit field. */
6599 || (mode != BLKmode
6600 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6601 || bitpos % GET_MODE_ALIGNMENT (mode))
6602 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6603 || (bitpos % BITS_PER_UNIT != 0)))
6604 || (bitsize >= 0 && mode != BLKmode
6605 && GET_MODE_BITSIZE (mode) > bitsize)
6606 /* If the RHS and field are a constant size and the size of the
6607 RHS isn't the same size as the bitfield, we must use bitfield
6608 operations. */
6609 || (bitsize >= 0
6610 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6611 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6612 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6613 we will handle specially below. */
6614 && !(TREE_CODE (exp) == CONSTRUCTOR
6615 && bitsize % BITS_PER_UNIT == 0))
6616 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6617 decl we must use bitfield operations. */
6618 || (bitsize >= 0
6619 && TREE_CODE (exp) == MEM_REF
6620 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6621 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6622 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6623 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6625 rtx temp;
6626 gimple *nop_def;
6628 /* Using bitwise copy is not safe for TREE_ADDRESSABLE types. */
6629 gcc_assert (!TREE_ADDRESSABLE (TREE_TYPE (exp)));
6631 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6632 implies a mask operation. If the precision is the same size as
6633 the field we're storing into, that mask is redundant. This is
6634 particularly common with bit field assignments generated by the
6635 C front end. */
6636 nop_def = get_def_for_expr (exp, NOP_EXPR);
6637 if (nop_def)
6639 tree type = TREE_TYPE (exp);
6640 if (INTEGRAL_TYPE_P (type)
6641 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6642 && bitsize == TYPE_PRECISION (type))
6644 tree op = gimple_assign_rhs1 (nop_def);
6645 type = TREE_TYPE (op);
6646 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6647 exp = op;
6651 temp = expand_normal (exp);
6653 /* If BITSIZE is narrower than the size of the type of EXP
6654 we will be narrowing TEMP. Normally, what's wanted are the
6655 low-order bits. However, if EXP's type is a record and this is
6656 big-endian machine, we want the upper BITSIZE bits. */
6657 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6658 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6659 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6660 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6661 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6662 NULL_RTX, 1);
6664 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6665 if (mode != VOIDmode && mode != BLKmode
6666 && mode != TYPE_MODE (TREE_TYPE (exp)))
6667 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6669 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6670 are both BLKmode, both must be in memory and BITPOS must be aligned
6671 on a byte boundary. If so, we simply do a block copy. Likewise for
6672 a BLKmode-like TARGET. */
6673 if (GET_CODE (temp) != PARALLEL
6674 && GET_MODE (temp) == BLKmode
6675 && (GET_MODE (target) == BLKmode
6676 || (MEM_P (target)
6677 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6678 && (bitpos % BITS_PER_UNIT) == 0
6679 && (bitsize % BITS_PER_UNIT) == 0)))
6681 gcc_assert (MEM_P (target) && MEM_P (temp)
6682 && (bitpos % BITS_PER_UNIT) == 0);
6684 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6685 emit_block_move (target, temp,
6686 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6687 / BITS_PER_UNIT),
6688 BLOCK_OP_NORMAL);
6690 return const0_rtx;
6693 /* Handle calls that return values in multiple non-contiguous locations.
6694 The Irix 6 ABI has examples of this. */
6695 if (GET_CODE (temp) == PARALLEL)
6697 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6698 rtx temp_target;
6699 if (mode == BLKmode || mode == VOIDmode)
6700 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6701 temp_target = gen_reg_rtx (mode);
6702 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6703 temp = temp_target;
6705 else if (mode == BLKmode)
6707 /* Handle calls that return BLKmode values in registers. */
6708 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6710 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6711 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6712 temp = temp_target;
6714 else
6716 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6717 rtx temp_target;
6718 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6719 temp_target = gen_reg_rtx (mode);
6720 temp_target
6721 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6722 temp_target, mode, mode);
6723 temp = temp_target;
6727 /* Store the value in the bitfield. */
6728 store_bit_field (target, bitsize, bitpos,
6729 bitregion_start, bitregion_end,
6730 mode, temp);
6732 return const0_rtx;
6734 else
6736 /* Now build a reference to just the desired component. */
6737 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6739 if (to_rtx == target)
6740 to_rtx = copy_rtx (to_rtx);
6742 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6743 set_mem_alias_set (to_rtx, alias_set);
6745 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6746 into a target smaller than its type; handle that case now. */
6747 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6749 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6750 store_constructor (exp, to_rtx, 0, bitsize/BITS_PER_UNIT);
6751 return to_rtx;
6754 return store_expr (exp, to_rtx, 0, nontemporal);
6758 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6759 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6760 codes and find the ultimate containing object, which we return.
6762 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6763 bit position, and *PUNSIGNEDP to the signedness of the field.
6764 If the position of the field is variable, we store a tree
6765 giving the variable offset (in units) in *POFFSET.
6766 This offset is in addition to the bit position.
6767 If the position is not variable, we store 0 in *POFFSET.
6769 If any of the extraction expressions is volatile,
6770 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6772 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6773 Otherwise, it is a mode that can be used to access the field.
6775 If the field describes a variable-sized object, *PMODE is set to
6776 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6777 this case, but the address of the object can be found.
6779 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6780 look through nodes that serve as markers of a greater alignment than
6781 the one that can be deduced from the expression. These nodes make it
6782 possible for front-ends to prevent temporaries from being created by
6783 the middle-end on alignment considerations. For that purpose, the
6784 normal operating mode at high-level is to always pass FALSE so that
6785 the ultimate containing object is really returned; moreover, the
6786 associated predicate handled_component_p will always return TRUE
6787 on these nodes, thus indicating that they are essentially handled
6788 by get_inner_reference. TRUE should only be passed when the caller
6789 is scanning the expression in order to build another representation
6790 and specifically knows how to handle these nodes; as such, this is
6791 the normal operating mode in the RTL expanders. */
6793 tree
6794 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6795 HOST_WIDE_INT *pbitpos, tree *poffset,
6796 machine_mode *pmode, int *punsignedp,
6797 int *pvolatilep, bool keep_aligning)
6799 tree size_tree = 0;
6800 machine_mode mode = VOIDmode;
6801 bool blkmode_bitfield = false;
6802 tree offset = size_zero_node;
6803 offset_int bit_offset = 0;
6805 /* First get the mode, signedness, and size. We do this from just the
6806 outermost expression. */
6807 *pbitsize = -1;
6808 if (TREE_CODE (exp) == COMPONENT_REF)
6810 tree field = TREE_OPERAND (exp, 1);
6811 size_tree = DECL_SIZE (field);
6812 if (flag_strict_volatile_bitfields > 0
6813 && TREE_THIS_VOLATILE (exp)
6814 && DECL_BIT_FIELD_TYPE (field)
6815 && DECL_MODE (field) != BLKmode)
6816 /* Volatile bitfields should be accessed in the mode of the
6817 field's type, not the mode computed based on the bit
6818 size. */
6819 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6820 else if (!DECL_BIT_FIELD (field))
6821 mode = DECL_MODE (field);
6822 else if (DECL_MODE (field) == BLKmode)
6823 blkmode_bitfield = true;
6825 *punsignedp = DECL_UNSIGNED (field);
6827 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6829 size_tree = TREE_OPERAND (exp, 1);
6830 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6831 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6833 /* For vector types, with the correct size of access, use the mode of
6834 inner type. */
6835 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6836 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6837 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6838 mode = TYPE_MODE (TREE_TYPE (exp));
6840 else
6842 mode = TYPE_MODE (TREE_TYPE (exp));
6843 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6845 if (mode == BLKmode)
6846 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6847 else
6848 *pbitsize = GET_MODE_BITSIZE (mode);
6851 if (size_tree != 0)
6853 if (! tree_fits_uhwi_p (size_tree))
6854 mode = BLKmode, *pbitsize = -1;
6855 else
6856 *pbitsize = tree_to_uhwi (size_tree);
6859 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6860 and find the ultimate containing object. */
6861 while (1)
6863 switch (TREE_CODE (exp))
6865 case BIT_FIELD_REF:
6866 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6867 break;
6869 case COMPONENT_REF:
6871 tree field = TREE_OPERAND (exp, 1);
6872 tree this_offset = component_ref_field_offset (exp);
6874 /* If this field hasn't been filled in yet, don't go past it.
6875 This should only happen when folding expressions made during
6876 type construction. */
6877 if (this_offset == 0)
6878 break;
6880 offset = size_binop (PLUS_EXPR, offset, this_offset);
6881 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6883 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6885 break;
6887 case ARRAY_REF:
6888 case ARRAY_RANGE_REF:
6890 tree index = TREE_OPERAND (exp, 1);
6891 tree low_bound = array_ref_low_bound (exp);
6892 tree unit_size = array_ref_element_size (exp);
6894 /* We assume all arrays have sizes that are a multiple of a byte.
6895 First subtract the lower bound, if any, in the type of the
6896 index, then convert to sizetype and multiply by the size of
6897 the array element. */
6898 if (! integer_zerop (low_bound))
6899 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6900 index, low_bound);
6902 offset = size_binop (PLUS_EXPR, offset,
6903 size_binop (MULT_EXPR,
6904 fold_convert (sizetype, index),
6905 unit_size));
6907 break;
6909 case REALPART_EXPR:
6910 break;
6912 case IMAGPART_EXPR:
6913 bit_offset += *pbitsize;
6914 break;
6916 case VIEW_CONVERT_EXPR:
6917 if (keep_aligning && STRICT_ALIGNMENT
6918 && (TYPE_ALIGN (TREE_TYPE (exp))
6919 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6920 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6921 < BIGGEST_ALIGNMENT)
6922 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6923 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6924 goto done;
6925 break;
6927 case MEM_REF:
6928 /* Hand back the decl for MEM[&decl, off]. */
6929 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6931 tree off = TREE_OPERAND (exp, 1);
6932 if (!integer_zerop (off))
6934 offset_int boff, coff = mem_ref_offset (exp);
6935 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6936 bit_offset += boff;
6938 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6940 goto done;
6942 default:
6943 goto done;
6946 /* If any reference in the chain is volatile, the effect is volatile. */
6947 if (TREE_THIS_VOLATILE (exp))
6948 *pvolatilep = 1;
6950 exp = TREE_OPERAND (exp, 0);
6952 done:
6954 /* If OFFSET is constant, see if we can return the whole thing as a
6955 constant bit position. Make sure to handle overflow during
6956 this conversion. */
6957 if (TREE_CODE (offset) == INTEGER_CST)
6959 offset_int tem = wi::sext (wi::to_offset (offset),
6960 TYPE_PRECISION (sizetype));
6961 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6962 tem += bit_offset;
6963 if (wi::fits_shwi_p (tem))
6965 *pbitpos = tem.to_shwi ();
6966 *poffset = offset = NULL_TREE;
6970 /* Otherwise, split it up. */
6971 if (offset)
6973 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6974 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
6976 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6977 offset_int tem = bit_offset.and_not (mask);
6978 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6979 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6980 bit_offset -= tem;
6981 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6982 offset = size_binop (PLUS_EXPR, offset,
6983 wide_int_to_tree (sizetype, tem));
6986 *pbitpos = bit_offset.to_shwi ();
6987 *poffset = offset;
6990 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6991 if (mode == VOIDmode
6992 && blkmode_bitfield
6993 && (*pbitpos % BITS_PER_UNIT) == 0
6994 && (*pbitsize % BITS_PER_UNIT) == 0)
6995 *pmode = BLKmode;
6996 else
6997 *pmode = mode;
6999 return exp;
7002 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7004 static unsigned HOST_WIDE_INT
7005 target_align (const_tree target)
7007 /* We might have a chain of nested references with intermediate misaligning
7008 bitfields components, so need to recurse to find out. */
7010 unsigned HOST_WIDE_INT this_align, outer_align;
7012 switch (TREE_CODE (target))
7014 case BIT_FIELD_REF:
7015 return 1;
7017 case COMPONENT_REF:
7018 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7019 outer_align = target_align (TREE_OPERAND (target, 0));
7020 return MIN (this_align, outer_align);
7022 case ARRAY_REF:
7023 case ARRAY_RANGE_REF:
7024 this_align = TYPE_ALIGN (TREE_TYPE (target));
7025 outer_align = target_align (TREE_OPERAND (target, 0));
7026 return MIN (this_align, outer_align);
7028 CASE_CONVERT:
7029 case NON_LVALUE_EXPR:
7030 case VIEW_CONVERT_EXPR:
7031 this_align = TYPE_ALIGN (TREE_TYPE (target));
7032 outer_align = target_align (TREE_OPERAND (target, 0));
7033 return MAX (this_align, outer_align);
7035 default:
7036 return TYPE_ALIGN (TREE_TYPE (target));
7041 /* Given an rtx VALUE that may contain additions and multiplications, return
7042 an equivalent value that just refers to a register, memory, or constant.
7043 This is done by generating instructions to perform the arithmetic and
7044 returning a pseudo-register containing the value.
7046 The returned value may be a REG, SUBREG, MEM or constant. */
7049 force_operand (rtx value, rtx target)
7051 rtx op1, op2;
7052 /* Use subtarget as the target for operand 0 of a binary operation. */
7053 rtx subtarget = get_subtarget (target);
7054 enum rtx_code code = GET_CODE (value);
7056 /* Check for subreg applied to an expression produced by loop optimizer. */
7057 if (code == SUBREG
7058 && !REG_P (SUBREG_REG (value))
7059 && !MEM_P (SUBREG_REG (value)))
7061 value
7062 = simplify_gen_subreg (GET_MODE (value),
7063 force_reg (GET_MODE (SUBREG_REG (value)),
7064 force_operand (SUBREG_REG (value),
7065 NULL_RTX)),
7066 GET_MODE (SUBREG_REG (value)),
7067 SUBREG_BYTE (value));
7068 code = GET_CODE (value);
7071 /* Check for a PIC address load. */
7072 if ((code == PLUS || code == MINUS)
7073 && XEXP (value, 0) == pic_offset_table_rtx
7074 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7075 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7076 || GET_CODE (XEXP (value, 1)) == CONST))
7078 if (!subtarget)
7079 subtarget = gen_reg_rtx (GET_MODE (value));
7080 emit_move_insn (subtarget, value);
7081 return subtarget;
7084 if (ARITHMETIC_P (value))
7086 op2 = XEXP (value, 1);
7087 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7088 subtarget = 0;
7089 if (code == MINUS && CONST_INT_P (op2))
7091 code = PLUS;
7092 op2 = negate_rtx (GET_MODE (value), op2);
7095 /* Check for an addition with OP2 a constant integer and our first
7096 operand a PLUS of a virtual register and something else. In that
7097 case, we want to emit the sum of the virtual register and the
7098 constant first and then add the other value. This allows virtual
7099 register instantiation to simply modify the constant rather than
7100 creating another one around this addition. */
7101 if (code == PLUS && CONST_INT_P (op2)
7102 && GET_CODE (XEXP (value, 0)) == PLUS
7103 && REG_P (XEXP (XEXP (value, 0), 0))
7104 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7105 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7107 rtx temp = expand_simple_binop (GET_MODE (value), code,
7108 XEXP (XEXP (value, 0), 0), op2,
7109 subtarget, 0, OPTAB_LIB_WIDEN);
7110 return expand_simple_binop (GET_MODE (value), code, temp,
7111 force_operand (XEXP (XEXP (value,
7112 0), 1), 0),
7113 target, 0, OPTAB_LIB_WIDEN);
7116 op1 = force_operand (XEXP (value, 0), subtarget);
7117 op2 = force_operand (op2, NULL_RTX);
7118 switch (code)
7120 case MULT:
7121 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7122 case DIV:
7123 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7124 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7125 target, 1, OPTAB_LIB_WIDEN);
7126 else
7127 return expand_divmod (0,
7128 FLOAT_MODE_P (GET_MODE (value))
7129 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7130 GET_MODE (value), op1, op2, target, 0);
7131 case MOD:
7132 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7133 target, 0);
7134 case UDIV:
7135 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7136 target, 1);
7137 case UMOD:
7138 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7139 target, 1);
7140 case ASHIFTRT:
7141 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7142 target, 0, OPTAB_LIB_WIDEN);
7143 default:
7144 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7145 target, 1, OPTAB_LIB_WIDEN);
7148 if (UNARY_P (value))
7150 if (!target)
7151 target = gen_reg_rtx (GET_MODE (value));
7152 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7153 switch (code)
7155 case ZERO_EXTEND:
7156 case SIGN_EXTEND:
7157 case TRUNCATE:
7158 case FLOAT_EXTEND:
7159 case FLOAT_TRUNCATE:
7160 convert_move (target, op1, code == ZERO_EXTEND);
7161 return target;
7163 case FIX:
7164 case UNSIGNED_FIX:
7165 expand_fix (target, op1, code == UNSIGNED_FIX);
7166 return target;
7168 case FLOAT:
7169 case UNSIGNED_FLOAT:
7170 expand_float (target, op1, code == UNSIGNED_FLOAT);
7171 return target;
7173 default:
7174 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7178 #ifdef INSN_SCHEDULING
7179 /* On machines that have insn scheduling, we want all memory reference to be
7180 explicit, so we need to deal with such paradoxical SUBREGs. */
7181 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7182 value
7183 = simplify_gen_subreg (GET_MODE (value),
7184 force_reg (GET_MODE (SUBREG_REG (value)),
7185 force_operand (SUBREG_REG (value),
7186 NULL_RTX)),
7187 GET_MODE (SUBREG_REG (value)),
7188 SUBREG_BYTE (value));
7189 #endif
7191 return value;
7194 /* Subroutine of expand_expr: return nonzero iff there is no way that
7195 EXP can reference X, which is being modified. TOP_P is nonzero if this
7196 call is going to be used to determine whether we need a temporary
7197 for EXP, as opposed to a recursive call to this function.
7199 It is always safe for this routine to return zero since it merely
7200 searches for optimization opportunities. */
7203 safe_from_p (const_rtx x, tree exp, int top_p)
7205 rtx exp_rtl = 0;
7206 int i, nops;
7208 if (x == 0
7209 /* If EXP has varying size, we MUST use a target since we currently
7210 have no way of allocating temporaries of variable size
7211 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7212 So we assume here that something at a higher level has prevented a
7213 clash. This is somewhat bogus, but the best we can do. Only
7214 do this when X is BLKmode and when we are at the top level. */
7215 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7216 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7217 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7218 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7219 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7220 != INTEGER_CST)
7221 && GET_MODE (x) == BLKmode)
7222 /* If X is in the outgoing argument area, it is always safe. */
7223 || (MEM_P (x)
7224 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7225 || (GET_CODE (XEXP (x, 0)) == PLUS
7226 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7227 return 1;
7229 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7230 find the underlying pseudo. */
7231 if (GET_CODE (x) == SUBREG)
7233 x = SUBREG_REG (x);
7234 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7235 return 0;
7238 /* Now look at our tree code and possibly recurse. */
7239 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7241 case tcc_declaration:
7242 exp_rtl = DECL_RTL_IF_SET (exp);
7243 break;
7245 case tcc_constant:
7246 return 1;
7248 case tcc_exceptional:
7249 if (TREE_CODE (exp) == TREE_LIST)
7251 while (1)
7253 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7254 return 0;
7255 exp = TREE_CHAIN (exp);
7256 if (!exp)
7257 return 1;
7258 if (TREE_CODE (exp) != TREE_LIST)
7259 return safe_from_p (x, exp, 0);
7262 else if (TREE_CODE (exp) == CONSTRUCTOR)
7264 constructor_elt *ce;
7265 unsigned HOST_WIDE_INT idx;
7267 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7268 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7269 || !safe_from_p (x, ce->value, 0))
7270 return 0;
7271 return 1;
7273 else if (TREE_CODE (exp) == ERROR_MARK)
7274 return 1; /* An already-visited SAVE_EXPR? */
7275 else
7276 return 0;
7278 case tcc_statement:
7279 /* The only case we look at here is the DECL_INITIAL inside a
7280 DECL_EXPR. */
7281 return (TREE_CODE (exp) != DECL_EXPR
7282 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7283 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7284 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7286 case tcc_binary:
7287 case tcc_comparison:
7288 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7289 return 0;
7290 /* Fall through. */
7292 case tcc_unary:
7293 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7295 case tcc_expression:
7296 case tcc_reference:
7297 case tcc_vl_exp:
7298 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7299 the expression. If it is set, we conflict iff we are that rtx or
7300 both are in memory. Otherwise, we check all operands of the
7301 expression recursively. */
7303 switch (TREE_CODE (exp))
7305 case ADDR_EXPR:
7306 /* If the operand is static or we are static, we can't conflict.
7307 Likewise if we don't conflict with the operand at all. */
7308 if (staticp (TREE_OPERAND (exp, 0))
7309 || TREE_STATIC (exp)
7310 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7311 return 1;
7313 /* Otherwise, the only way this can conflict is if we are taking
7314 the address of a DECL a that address if part of X, which is
7315 very rare. */
7316 exp = TREE_OPERAND (exp, 0);
7317 if (DECL_P (exp))
7319 if (!DECL_RTL_SET_P (exp)
7320 || !MEM_P (DECL_RTL (exp)))
7321 return 0;
7322 else
7323 exp_rtl = XEXP (DECL_RTL (exp), 0);
7325 break;
7327 case MEM_REF:
7328 if (MEM_P (x)
7329 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7330 get_alias_set (exp)))
7331 return 0;
7332 break;
7334 case CALL_EXPR:
7335 /* Assume that the call will clobber all hard registers and
7336 all of memory. */
7337 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7338 || MEM_P (x))
7339 return 0;
7340 break;
7342 case WITH_CLEANUP_EXPR:
7343 case CLEANUP_POINT_EXPR:
7344 /* Lowered by gimplify.c. */
7345 gcc_unreachable ();
7347 case SAVE_EXPR:
7348 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7350 default:
7351 break;
7354 /* If we have an rtx, we do not need to scan our operands. */
7355 if (exp_rtl)
7356 break;
7358 nops = TREE_OPERAND_LENGTH (exp);
7359 for (i = 0; i < nops; i++)
7360 if (TREE_OPERAND (exp, i) != 0
7361 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7362 return 0;
7364 break;
7366 case tcc_type:
7367 /* Should never get a type here. */
7368 gcc_unreachable ();
7371 /* If we have an rtl, find any enclosed object. Then see if we conflict
7372 with it. */
7373 if (exp_rtl)
7375 if (GET_CODE (exp_rtl) == SUBREG)
7377 exp_rtl = SUBREG_REG (exp_rtl);
7378 if (REG_P (exp_rtl)
7379 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7380 return 0;
7383 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7384 are memory and they conflict. */
7385 return ! (rtx_equal_p (x, exp_rtl)
7386 || (MEM_P (x) && MEM_P (exp_rtl)
7387 && true_dependence (exp_rtl, VOIDmode, x)));
7390 /* If we reach here, it is safe. */
7391 return 1;
7395 /* Return the highest power of two that EXP is known to be a multiple of.
7396 This is used in updating alignment of MEMs in array references. */
7398 unsigned HOST_WIDE_INT
7399 highest_pow2_factor (const_tree exp)
7401 unsigned HOST_WIDE_INT ret;
7402 int trailing_zeros = tree_ctz (exp);
7403 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7404 return BIGGEST_ALIGNMENT;
7405 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7406 if (ret > BIGGEST_ALIGNMENT)
7407 return BIGGEST_ALIGNMENT;
7408 return ret;
7411 /* Similar, except that the alignment requirements of TARGET are
7412 taken into account. Assume it is at least as aligned as its
7413 type, unless it is a COMPONENT_REF in which case the layout of
7414 the structure gives the alignment. */
7416 static unsigned HOST_WIDE_INT
7417 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7419 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7420 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7422 return MAX (factor, talign);
7425 /* Convert the tree comparison code TCODE to the rtl one where the
7426 signedness is UNSIGNEDP. */
7428 static enum rtx_code
7429 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7431 enum rtx_code code;
7432 switch (tcode)
7434 case EQ_EXPR:
7435 code = EQ;
7436 break;
7437 case NE_EXPR:
7438 code = NE;
7439 break;
7440 case LT_EXPR:
7441 code = unsignedp ? LTU : LT;
7442 break;
7443 case LE_EXPR:
7444 code = unsignedp ? LEU : LE;
7445 break;
7446 case GT_EXPR:
7447 code = unsignedp ? GTU : GT;
7448 break;
7449 case GE_EXPR:
7450 code = unsignedp ? GEU : GE;
7451 break;
7452 case UNORDERED_EXPR:
7453 code = UNORDERED;
7454 break;
7455 case ORDERED_EXPR:
7456 code = ORDERED;
7457 break;
7458 case UNLT_EXPR:
7459 code = UNLT;
7460 break;
7461 case UNLE_EXPR:
7462 code = UNLE;
7463 break;
7464 case UNGT_EXPR:
7465 code = UNGT;
7466 break;
7467 case UNGE_EXPR:
7468 code = UNGE;
7469 break;
7470 case UNEQ_EXPR:
7471 code = UNEQ;
7472 break;
7473 case LTGT_EXPR:
7474 code = LTGT;
7475 break;
7477 default:
7478 gcc_unreachable ();
7480 return code;
7483 /* Subroutine of expand_expr. Expand the two operands of a binary
7484 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7485 The value may be stored in TARGET if TARGET is nonzero. The
7486 MODIFIER argument is as documented by expand_expr. */
7488 void
7489 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7490 enum expand_modifier modifier)
7492 if (! safe_from_p (target, exp1, 1))
7493 target = 0;
7494 if (operand_equal_p (exp0, exp1, 0))
7496 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7497 *op1 = copy_rtx (*op0);
7499 else
7501 /* If we need to preserve evaluation order, copy exp0 into its own
7502 temporary variable so that it can't be clobbered by exp1. */
7503 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7504 exp0 = save_expr (exp0);
7505 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7506 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7511 /* Return a MEM that contains constant EXP. DEFER is as for
7512 output_constant_def and MODIFIER is as for expand_expr. */
7514 static rtx
7515 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7517 rtx mem;
7519 mem = output_constant_def (exp, defer);
7520 if (modifier != EXPAND_INITIALIZER)
7521 mem = use_anchored_address (mem);
7522 return mem;
7525 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7526 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7528 static rtx
7529 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7530 enum expand_modifier modifier, addr_space_t as)
7532 rtx result, subtarget;
7533 tree inner, offset;
7534 HOST_WIDE_INT bitsize, bitpos;
7535 int volatilep, unsignedp;
7536 machine_mode mode1;
7538 /* If we are taking the address of a constant and are at the top level,
7539 we have to use output_constant_def since we can't call force_const_mem
7540 at top level. */
7541 /* ??? This should be considered a front-end bug. We should not be
7542 generating ADDR_EXPR of something that isn't an LVALUE. The only
7543 exception here is STRING_CST. */
7544 if (CONSTANT_CLASS_P (exp))
7546 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7547 if (modifier < EXPAND_SUM)
7548 result = force_operand (result, target);
7549 return result;
7552 /* Everything must be something allowed by is_gimple_addressable. */
7553 switch (TREE_CODE (exp))
7555 case INDIRECT_REF:
7556 /* This case will happen via recursion for &a->b. */
7557 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7559 case MEM_REF:
7561 tree tem = TREE_OPERAND (exp, 0);
7562 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7563 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7564 return expand_expr (tem, target, tmode, modifier);
7567 case CONST_DECL:
7568 /* Expand the initializer like constants above. */
7569 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7570 0, modifier), 0);
7571 if (modifier < EXPAND_SUM)
7572 result = force_operand (result, target);
7573 return result;
7575 case REALPART_EXPR:
7576 /* The real part of the complex number is always first, therefore
7577 the address is the same as the address of the parent object. */
7578 offset = 0;
7579 bitpos = 0;
7580 inner = TREE_OPERAND (exp, 0);
7581 break;
7583 case IMAGPART_EXPR:
7584 /* The imaginary part of the complex number is always second.
7585 The expression is therefore always offset by the size of the
7586 scalar type. */
7587 offset = 0;
7588 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7589 inner = TREE_OPERAND (exp, 0);
7590 break;
7592 case COMPOUND_LITERAL_EXPR:
7593 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7594 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7595 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7596 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7597 the initializers aren't gimplified. */
7598 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7599 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7600 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7601 target, tmode, modifier, as);
7602 /* FALLTHRU */
7603 default:
7604 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7605 expand_expr, as that can have various side effects; LABEL_DECLs for
7606 example, may not have their DECL_RTL set yet. Expand the rtl of
7607 CONSTRUCTORs too, which should yield a memory reference for the
7608 constructor's contents. Assume language specific tree nodes can
7609 be expanded in some interesting way. */
7610 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7611 if (DECL_P (exp)
7612 || TREE_CODE (exp) == CONSTRUCTOR
7613 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7615 result = expand_expr (exp, target, tmode,
7616 modifier == EXPAND_INITIALIZER
7617 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7619 /* If the DECL isn't in memory, then the DECL wasn't properly
7620 marked TREE_ADDRESSABLE, which will be either a front-end
7621 or a tree optimizer bug. */
7623 gcc_assert (MEM_P (result));
7624 result = XEXP (result, 0);
7626 /* ??? Is this needed anymore? */
7627 if (DECL_P (exp))
7628 TREE_USED (exp) = 1;
7630 if (modifier != EXPAND_INITIALIZER
7631 && modifier != EXPAND_CONST_ADDRESS
7632 && modifier != EXPAND_SUM)
7633 result = force_operand (result, target);
7634 return result;
7637 /* Pass FALSE as the last argument to get_inner_reference although
7638 we are expanding to RTL. The rationale is that we know how to
7639 handle "aligning nodes" here: we can just bypass them because
7640 they won't change the final object whose address will be returned
7641 (they actually exist only for that purpose). */
7642 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7643 &mode1, &unsignedp, &volatilep, false);
7644 break;
7647 /* We must have made progress. */
7648 gcc_assert (inner != exp);
7650 subtarget = offset || bitpos ? NULL_RTX : target;
7651 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7652 inner alignment, force the inner to be sufficiently aligned. */
7653 if (CONSTANT_CLASS_P (inner)
7654 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7656 inner = copy_node (inner);
7657 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7658 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7659 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7661 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7663 if (offset)
7665 rtx tmp;
7667 if (modifier != EXPAND_NORMAL)
7668 result = force_operand (result, NULL);
7669 tmp = expand_expr (offset, NULL_RTX, tmode,
7670 modifier == EXPAND_INITIALIZER
7671 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7673 /* expand_expr is allowed to return an object in a mode other
7674 than TMODE. If it did, we need to convert. */
7675 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7676 tmp = convert_modes (tmode, GET_MODE (tmp),
7677 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7678 result = convert_memory_address_addr_space (tmode, result, as);
7679 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7681 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7682 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7683 else
7685 subtarget = bitpos ? NULL_RTX : target;
7686 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7687 1, OPTAB_LIB_WIDEN);
7691 if (bitpos)
7693 /* Someone beforehand should have rejected taking the address
7694 of such an object. */
7695 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7697 result = convert_memory_address_addr_space (tmode, result, as);
7698 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7699 if (modifier < EXPAND_SUM)
7700 result = force_operand (result, target);
7703 return result;
7706 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7707 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7709 static rtx
7710 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7711 enum expand_modifier modifier)
7713 addr_space_t as = ADDR_SPACE_GENERIC;
7714 machine_mode address_mode = Pmode;
7715 machine_mode pointer_mode = ptr_mode;
7716 machine_mode rmode;
7717 rtx result;
7719 /* Target mode of VOIDmode says "whatever's natural". */
7720 if (tmode == VOIDmode)
7721 tmode = TYPE_MODE (TREE_TYPE (exp));
7723 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7725 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7726 address_mode = targetm.addr_space.address_mode (as);
7727 pointer_mode = targetm.addr_space.pointer_mode (as);
7730 /* We can get called with some Weird Things if the user does silliness
7731 like "(short) &a". In that case, convert_memory_address won't do
7732 the right thing, so ignore the given target mode. */
7733 if (tmode != address_mode && tmode != pointer_mode)
7734 tmode = address_mode;
7736 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7737 tmode, modifier, as);
7739 /* Despite expand_expr claims concerning ignoring TMODE when not
7740 strictly convenient, stuff breaks if we don't honor it. Note
7741 that combined with the above, we only do this for pointer modes. */
7742 rmode = GET_MODE (result);
7743 if (rmode == VOIDmode)
7744 rmode = tmode;
7745 if (rmode != tmode)
7746 result = convert_memory_address_addr_space (tmode, result, as);
7748 return result;
7751 /* Generate code for computing CONSTRUCTOR EXP.
7752 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7753 is TRUE, instead of creating a temporary variable in memory
7754 NULL is returned and the caller needs to handle it differently. */
7756 static rtx
7757 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7758 bool avoid_temp_mem)
7760 tree type = TREE_TYPE (exp);
7761 machine_mode mode = TYPE_MODE (type);
7763 /* Try to avoid creating a temporary at all. This is possible
7764 if all of the initializer is zero.
7765 FIXME: try to handle all [0..255] initializers we can handle
7766 with memset. */
7767 if (TREE_STATIC (exp)
7768 && !TREE_ADDRESSABLE (exp)
7769 && target != 0 && mode == BLKmode
7770 && all_zeros_p (exp))
7772 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7773 return target;
7776 /* All elts simple constants => refer to a constant in memory. But
7777 if this is a non-BLKmode mode, let it store a field at a time
7778 since that should make a CONST_INT, CONST_WIDE_INT or
7779 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7780 use, it is best to store directly into the target unless the type
7781 is large enough that memcpy will be used. If we are making an
7782 initializer and all operands are constant, put it in memory as
7783 well.
7785 FIXME: Avoid trying to fill vector constructors piece-meal.
7786 Output them with output_constant_def below unless we're sure
7787 they're zeros. This should go away when vector initializers
7788 are treated like VECTOR_CST instead of arrays. */
7789 if ((TREE_STATIC (exp)
7790 && ((mode == BLKmode
7791 && ! (target != 0 && safe_from_p (target, exp, 1)))
7792 || TREE_ADDRESSABLE (exp)
7793 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7794 && (! can_move_by_pieces
7795 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7796 TYPE_ALIGN (type)))
7797 && ! mostly_zeros_p (exp))))
7798 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7799 && TREE_CONSTANT (exp)))
7801 rtx constructor;
7803 if (avoid_temp_mem)
7804 return NULL_RTX;
7806 constructor = expand_expr_constant (exp, 1, modifier);
7808 if (modifier != EXPAND_CONST_ADDRESS
7809 && modifier != EXPAND_INITIALIZER
7810 && modifier != EXPAND_SUM)
7811 constructor = validize_mem (constructor);
7813 return constructor;
7816 /* Handle calls that pass values in multiple non-contiguous
7817 locations. The Irix 6 ABI has examples of this. */
7818 if (target == 0 || ! safe_from_p (target, exp, 1)
7819 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7821 if (avoid_temp_mem)
7822 return NULL_RTX;
7824 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7827 store_constructor (exp, target, 0, int_expr_size (exp));
7828 return target;
7832 /* expand_expr: generate code for computing expression EXP.
7833 An rtx for the computed value is returned. The value is never null.
7834 In the case of a void EXP, const0_rtx is returned.
7836 The value may be stored in TARGET if TARGET is nonzero.
7837 TARGET is just a suggestion; callers must assume that
7838 the rtx returned may not be the same as TARGET.
7840 If TARGET is CONST0_RTX, it means that the value will be ignored.
7842 If TMODE is not VOIDmode, it suggests generating the
7843 result in mode TMODE. But this is done only when convenient.
7844 Otherwise, TMODE is ignored and the value generated in its natural mode.
7845 TMODE is just a suggestion; callers must assume that
7846 the rtx returned may not have mode TMODE.
7848 Note that TARGET may have neither TMODE nor MODE. In that case, it
7849 probably will not be used.
7851 If MODIFIER is EXPAND_SUM then when EXP is an addition
7852 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7853 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7854 products as above, or REG or MEM, or constant.
7855 Ordinarily in such cases we would output mul or add instructions
7856 and then return a pseudo reg containing the sum.
7858 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7859 it also marks a label as absolutely required (it can't be dead).
7860 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7861 This is used for outputting expressions used in initializers.
7863 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7864 with a constant address even if that address is not normally legitimate.
7865 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7867 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7868 a call parameter. Such targets require special care as we haven't yet
7869 marked TARGET so that it's safe from being trashed by libcalls. We
7870 don't want to use TARGET for anything but the final result;
7871 Intermediate values must go elsewhere. Additionally, calls to
7872 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7874 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7875 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7876 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7877 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7878 recursively.
7880 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7881 In this case, we don't adjust a returned MEM rtx that wouldn't be
7882 sufficiently aligned for its mode; instead, it's up to the caller
7883 to deal with it afterwards. This is used to make sure that unaligned
7884 base objects for which out-of-bounds accesses are supported, for
7885 example record types with trailing arrays, aren't realigned behind
7886 the back of the caller.
7887 The normal operating mode is to pass FALSE for this parameter. */
7890 expand_expr_real (tree exp, rtx target, machine_mode tmode,
7891 enum expand_modifier modifier, rtx *alt_rtl,
7892 bool inner_reference_p)
7894 rtx ret;
7896 /* Handle ERROR_MARK before anybody tries to access its type. */
7897 if (TREE_CODE (exp) == ERROR_MARK
7898 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7900 ret = CONST0_RTX (tmode);
7901 return ret ? ret : const0_rtx;
7904 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
7905 inner_reference_p);
7906 return ret;
7909 /* Try to expand the conditional expression which is represented by
7910 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
7911 return the rtl reg which represents the result. Otherwise return
7912 NULL_RTX. */
7914 static rtx
7915 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7916 tree treeop1 ATTRIBUTE_UNUSED,
7917 tree treeop2 ATTRIBUTE_UNUSED)
7919 rtx insn;
7920 rtx op00, op01, op1, op2;
7921 enum rtx_code comparison_code;
7922 machine_mode comparison_mode;
7923 gimple *srcstmt;
7924 rtx temp;
7925 tree type = TREE_TYPE (treeop1);
7926 int unsignedp = TYPE_UNSIGNED (type);
7927 machine_mode mode = TYPE_MODE (type);
7928 machine_mode orig_mode = mode;
7930 /* If we cannot do a conditional move on the mode, try doing it
7931 with the promoted mode. */
7932 if (!can_conditionally_move_p (mode))
7934 mode = promote_mode (type, mode, &unsignedp);
7935 if (!can_conditionally_move_p (mode))
7936 return NULL_RTX;
7937 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
7939 else
7940 temp = assign_temp (type, 0, 1);
7942 start_sequence ();
7943 expand_operands (treeop1, treeop2,
7944 temp, &op1, &op2, EXPAND_NORMAL);
7946 if (TREE_CODE (treeop0) == SSA_NAME
7947 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7949 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7950 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7951 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7952 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7953 comparison_mode = TYPE_MODE (type);
7954 unsignedp = TYPE_UNSIGNED (type);
7955 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7957 else if (COMPARISON_CLASS_P (treeop0))
7959 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7960 enum tree_code cmpcode = TREE_CODE (treeop0);
7961 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
7962 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
7963 unsignedp = TYPE_UNSIGNED (type);
7964 comparison_mode = TYPE_MODE (type);
7965 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7967 else
7969 op00 = expand_normal (treeop0);
7970 op01 = const0_rtx;
7971 comparison_code = NE;
7972 comparison_mode = GET_MODE (op00);
7973 if (comparison_mode == VOIDmode)
7974 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
7977 if (GET_MODE (op1) != mode)
7978 op1 = gen_lowpart (mode, op1);
7980 if (GET_MODE (op2) != mode)
7981 op2 = gen_lowpart (mode, op2);
7983 /* Try to emit the conditional move. */
7984 insn = emit_conditional_move (temp, comparison_code,
7985 op00, op01, comparison_mode,
7986 op1, op2, mode,
7987 unsignedp);
7989 /* If we could do the conditional move, emit the sequence,
7990 and return. */
7991 if (insn)
7993 rtx_insn *seq = get_insns ();
7994 end_sequence ();
7995 emit_insn (seq);
7996 return convert_modes (orig_mode, mode, temp, 0);
7999 /* Otherwise discard the sequence and fall back to code with
8000 branches. */
8001 end_sequence ();
8002 return NULL_RTX;
8006 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8007 enum expand_modifier modifier)
8009 rtx op0, op1, op2, temp;
8010 rtx_code_label *lab;
8011 tree type;
8012 int unsignedp;
8013 machine_mode mode;
8014 enum tree_code code = ops->code;
8015 optab this_optab;
8016 rtx subtarget, original_target;
8017 int ignore;
8018 bool reduce_bit_field;
8019 location_t loc = ops->location;
8020 tree treeop0, treeop1, treeop2;
8021 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8022 ? reduce_to_bit_field_precision ((expr), \
8023 target, \
8024 type) \
8025 : (expr))
8027 type = ops->type;
8028 mode = TYPE_MODE (type);
8029 unsignedp = TYPE_UNSIGNED (type);
8031 treeop0 = ops->op0;
8032 treeop1 = ops->op1;
8033 treeop2 = ops->op2;
8035 /* We should be called only on simple (binary or unary) expressions,
8036 exactly those that are valid in gimple expressions that aren't
8037 GIMPLE_SINGLE_RHS (or invalid). */
8038 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8039 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8040 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8042 ignore = (target == const0_rtx
8043 || ((CONVERT_EXPR_CODE_P (code)
8044 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8045 && TREE_CODE (type) == VOID_TYPE));
8047 /* We should be called only if we need the result. */
8048 gcc_assert (!ignore);
8050 /* An operation in what may be a bit-field type needs the
8051 result to be reduced to the precision of the bit-field type,
8052 which is narrower than that of the type's mode. */
8053 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8054 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8056 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8057 target = 0;
8059 /* Use subtarget as the target for operand 0 of a binary operation. */
8060 subtarget = get_subtarget (target);
8061 original_target = target;
8063 switch (code)
8065 case NON_LVALUE_EXPR:
8066 case PAREN_EXPR:
8067 CASE_CONVERT:
8068 if (treeop0 == error_mark_node)
8069 return const0_rtx;
8071 if (TREE_CODE (type) == UNION_TYPE)
8073 tree valtype = TREE_TYPE (treeop0);
8075 /* If both input and output are BLKmode, this conversion isn't doing
8076 anything except possibly changing memory attribute. */
8077 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8079 rtx result = expand_expr (treeop0, target, tmode,
8080 modifier);
8082 result = copy_rtx (result);
8083 set_mem_attributes (result, type, 0);
8084 return result;
8087 if (target == 0)
8089 if (TYPE_MODE (type) != BLKmode)
8090 target = gen_reg_rtx (TYPE_MODE (type));
8091 else
8092 target = assign_temp (type, 1, 1);
8095 if (MEM_P (target))
8096 /* Store data into beginning of memory target. */
8097 store_expr (treeop0,
8098 adjust_address (target, TYPE_MODE (valtype), 0),
8099 modifier == EXPAND_STACK_PARM,
8100 false);
8102 else
8104 gcc_assert (REG_P (target));
8106 /* Store this field into a union of the proper type. */
8107 store_field (target,
8108 MIN ((int_size_in_bytes (TREE_TYPE
8109 (treeop0))
8110 * BITS_PER_UNIT),
8111 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8112 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8115 /* Return the entire union. */
8116 return target;
8119 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8121 op0 = expand_expr (treeop0, target, VOIDmode,
8122 modifier);
8124 /* If the signedness of the conversion differs and OP0 is
8125 a promoted SUBREG, clear that indication since we now
8126 have to do the proper extension. */
8127 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8128 && GET_CODE (op0) == SUBREG)
8129 SUBREG_PROMOTED_VAR_P (op0) = 0;
8131 return REDUCE_BIT_FIELD (op0);
8134 op0 = expand_expr (treeop0, NULL_RTX, mode,
8135 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8136 if (GET_MODE (op0) == mode)
8139 /* If OP0 is a constant, just convert it into the proper mode. */
8140 else if (CONSTANT_P (op0))
8142 tree inner_type = TREE_TYPE (treeop0);
8143 machine_mode inner_mode = GET_MODE (op0);
8145 if (inner_mode == VOIDmode)
8146 inner_mode = TYPE_MODE (inner_type);
8148 if (modifier == EXPAND_INITIALIZER)
8149 op0 = lowpart_subreg (mode, op0, inner_mode);
8150 else
8151 op0= convert_modes (mode, inner_mode, op0,
8152 TYPE_UNSIGNED (inner_type));
8155 else if (modifier == EXPAND_INITIALIZER)
8156 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8158 else if (target == 0)
8159 op0 = convert_to_mode (mode, op0,
8160 TYPE_UNSIGNED (TREE_TYPE
8161 (treeop0)));
8162 else
8164 convert_move (target, op0,
8165 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8166 op0 = target;
8169 return REDUCE_BIT_FIELD (op0);
8171 case ADDR_SPACE_CONVERT_EXPR:
8173 tree treeop0_type = TREE_TYPE (treeop0);
8175 gcc_assert (POINTER_TYPE_P (type));
8176 gcc_assert (POINTER_TYPE_P (treeop0_type));
8178 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8179 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8181 /* Conversions between pointers to the same address space should
8182 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8183 gcc_assert (as_to != as_from);
8185 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8187 /* Ask target code to handle conversion between pointers
8188 to overlapping address spaces. */
8189 if (targetm.addr_space.subset_p (as_to, as_from)
8190 || targetm.addr_space.subset_p (as_from, as_to))
8192 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8194 else
8196 /* For disjoint address spaces, converting anything but a null
8197 pointer invokes undefined behaviour. We truncate or extend the
8198 value as if we'd converted via integers, which handles 0 as
8199 required, and all others as the programmer likely expects. */
8200 #ifndef POINTERS_EXTEND_UNSIGNED
8201 const int POINTERS_EXTEND_UNSIGNED = 1;
8202 #endif
8203 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8204 op0, POINTERS_EXTEND_UNSIGNED);
8206 gcc_assert (op0);
8207 return op0;
8210 case POINTER_PLUS_EXPR:
8211 /* Even though the sizetype mode and the pointer's mode can be different
8212 expand is able to handle this correctly and get the correct result out
8213 of the PLUS_EXPR code. */
8214 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8215 if sizetype precision is smaller than pointer precision. */
8216 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8217 treeop1 = fold_convert_loc (loc, type,
8218 fold_convert_loc (loc, ssizetype,
8219 treeop1));
8220 /* If sizetype precision is larger than pointer precision, truncate the
8221 offset to have matching modes. */
8222 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8223 treeop1 = fold_convert_loc (loc, type, treeop1);
8225 case PLUS_EXPR:
8226 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8227 something else, make sure we add the register to the constant and
8228 then to the other thing. This case can occur during strength
8229 reduction and doing it this way will produce better code if the
8230 frame pointer or argument pointer is eliminated.
8232 fold-const.c will ensure that the constant is always in the inner
8233 PLUS_EXPR, so the only case we need to do anything about is if
8234 sp, ap, or fp is our second argument, in which case we must swap
8235 the innermost first argument and our second argument. */
8237 if (TREE_CODE (treeop0) == PLUS_EXPR
8238 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8239 && TREE_CODE (treeop1) == VAR_DECL
8240 && (DECL_RTL (treeop1) == frame_pointer_rtx
8241 || DECL_RTL (treeop1) == stack_pointer_rtx
8242 || DECL_RTL (treeop1) == arg_pointer_rtx))
8244 gcc_unreachable ();
8247 /* If the result is to be ptr_mode and we are adding an integer to
8248 something, we might be forming a constant. So try to use
8249 plus_constant. If it produces a sum and we can't accept it,
8250 use force_operand. This allows P = &ARR[const] to generate
8251 efficient code on machines where a SYMBOL_REF is not a valid
8252 address.
8254 If this is an EXPAND_SUM call, always return the sum. */
8255 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8256 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8258 if (modifier == EXPAND_STACK_PARM)
8259 target = 0;
8260 if (TREE_CODE (treeop0) == INTEGER_CST
8261 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8262 && TREE_CONSTANT (treeop1))
8264 rtx constant_part;
8265 HOST_WIDE_INT wc;
8266 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8268 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8269 EXPAND_SUM);
8270 /* Use wi::shwi to ensure that the constant is
8271 truncated according to the mode of OP1, then sign extended
8272 to a HOST_WIDE_INT. Using the constant directly can result
8273 in non-canonical RTL in a 64x32 cross compile. */
8274 wc = TREE_INT_CST_LOW (treeop0);
8275 constant_part =
8276 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8277 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8278 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8279 op1 = force_operand (op1, target);
8280 return REDUCE_BIT_FIELD (op1);
8283 else if (TREE_CODE (treeop1) == INTEGER_CST
8284 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8285 && TREE_CONSTANT (treeop0))
8287 rtx constant_part;
8288 HOST_WIDE_INT wc;
8289 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8291 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8292 (modifier == EXPAND_INITIALIZER
8293 ? EXPAND_INITIALIZER : EXPAND_SUM));
8294 if (! CONSTANT_P (op0))
8296 op1 = expand_expr (treeop1, NULL_RTX,
8297 VOIDmode, modifier);
8298 /* Return a PLUS if modifier says it's OK. */
8299 if (modifier == EXPAND_SUM
8300 || modifier == EXPAND_INITIALIZER)
8301 return simplify_gen_binary (PLUS, mode, op0, op1);
8302 goto binop2;
8304 /* Use wi::shwi to ensure that the constant is
8305 truncated according to the mode of OP1, then sign extended
8306 to a HOST_WIDE_INT. Using the constant directly can result
8307 in non-canonical RTL in a 64x32 cross compile. */
8308 wc = TREE_INT_CST_LOW (treeop1);
8309 constant_part
8310 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8311 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8312 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8313 op0 = force_operand (op0, target);
8314 return REDUCE_BIT_FIELD (op0);
8318 /* Use TER to expand pointer addition of a negated value
8319 as pointer subtraction. */
8320 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8321 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8322 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8323 && TREE_CODE (treeop1) == SSA_NAME
8324 && TYPE_MODE (TREE_TYPE (treeop0))
8325 == TYPE_MODE (TREE_TYPE (treeop1)))
8327 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8328 if (def)
8330 treeop1 = gimple_assign_rhs1 (def);
8331 code = MINUS_EXPR;
8332 goto do_minus;
8336 /* No sense saving up arithmetic to be done
8337 if it's all in the wrong mode to form part of an address.
8338 And force_operand won't know whether to sign-extend or
8339 zero-extend. */
8340 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8341 || mode != ptr_mode)
8343 expand_operands (treeop0, treeop1,
8344 subtarget, &op0, &op1, EXPAND_NORMAL);
8345 if (op0 == const0_rtx)
8346 return op1;
8347 if (op1 == const0_rtx)
8348 return op0;
8349 goto binop2;
8352 expand_operands (treeop0, treeop1,
8353 subtarget, &op0, &op1, modifier);
8354 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8356 case MINUS_EXPR:
8357 do_minus:
8358 /* For initializers, we are allowed to return a MINUS of two
8359 symbolic constants. Here we handle all cases when both operands
8360 are constant. */
8361 /* Handle difference of two symbolic constants,
8362 for the sake of an initializer. */
8363 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8364 && really_constant_p (treeop0)
8365 && really_constant_p (treeop1))
8367 expand_operands (treeop0, treeop1,
8368 NULL_RTX, &op0, &op1, modifier);
8370 /* If the last operand is a CONST_INT, use plus_constant of
8371 the negated constant. Else make the MINUS. */
8372 if (CONST_INT_P (op1))
8373 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8374 -INTVAL (op1)));
8375 else
8376 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8379 /* No sense saving up arithmetic to be done
8380 if it's all in the wrong mode to form part of an address.
8381 And force_operand won't know whether to sign-extend or
8382 zero-extend. */
8383 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8384 || mode != ptr_mode)
8385 goto binop;
8387 expand_operands (treeop0, treeop1,
8388 subtarget, &op0, &op1, modifier);
8390 /* Convert A - const to A + (-const). */
8391 if (CONST_INT_P (op1))
8393 op1 = negate_rtx (mode, op1);
8394 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8397 goto binop2;
8399 case WIDEN_MULT_PLUS_EXPR:
8400 case WIDEN_MULT_MINUS_EXPR:
8401 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8402 op2 = expand_normal (treeop2);
8403 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8404 target, unsignedp);
8405 return target;
8407 case WIDEN_MULT_EXPR:
8408 /* If first operand is constant, swap them.
8409 Thus the following special case checks need only
8410 check the second operand. */
8411 if (TREE_CODE (treeop0) == INTEGER_CST)
8412 std::swap (treeop0, treeop1);
8414 /* First, check if we have a multiplication of one signed and one
8415 unsigned operand. */
8416 if (TREE_CODE (treeop1) != INTEGER_CST
8417 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8418 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8420 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8421 this_optab = usmul_widen_optab;
8422 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8423 != CODE_FOR_nothing)
8425 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8426 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8427 EXPAND_NORMAL);
8428 else
8429 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8430 EXPAND_NORMAL);
8431 /* op0 and op1 might still be constant, despite the above
8432 != INTEGER_CST check. Handle it. */
8433 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8435 op0 = convert_modes (innermode, mode, op0, true);
8436 op1 = convert_modes (innermode, mode, op1, false);
8437 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8438 target, unsignedp));
8440 goto binop3;
8443 /* Check for a multiplication with matching signedness. */
8444 else if ((TREE_CODE (treeop1) == INTEGER_CST
8445 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8446 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8447 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8449 tree op0type = TREE_TYPE (treeop0);
8450 machine_mode innermode = TYPE_MODE (op0type);
8451 bool zextend_p = TYPE_UNSIGNED (op0type);
8452 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8453 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8455 if (TREE_CODE (treeop0) != INTEGER_CST)
8457 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8458 != CODE_FOR_nothing)
8460 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8461 EXPAND_NORMAL);
8462 /* op0 and op1 might still be constant, despite the above
8463 != INTEGER_CST check. Handle it. */
8464 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8466 widen_mult_const:
8467 op0 = convert_modes (innermode, mode, op0, zextend_p);
8469 = convert_modes (innermode, mode, op1,
8470 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8471 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8472 target,
8473 unsignedp));
8475 temp = expand_widening_mult (mode, op0, op1, target,
8476 unsignedp, this_optab);
8477 return REDUCE_BIT_FIELD (temp);
8479 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8480 != CODE_FOR_nothing
8481 && innermode == word_mode)
8483 rtx htem, hipart;
8484 op0 = expand_normal (treeop0);
8485 if (TREE_CODE (treeop1) == INTEGER_CST)
8486 op1 = convert_modes (innermode, mode,
8487 expand_normal (treeop1),
8488 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8489 else
8490 op1 = expand_normal (treeop1);
8491 /* op0 and op1 might still be constant, despite the above
8492 != INTEGER_CST check. Handle it. */
8493 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8494 goto widen_mult_const;
8495 temp = expand_binop (mode, other_optab, op0, op1, target,
8496 unsignedp, OPTAB_LIB_WIDEN);
8497 hipart = gen_highpart (innermode, temp);
8498 htem = expand_mult_highpart_adjust (innermode, hipart,
8499 op0, op1, hipart,
8500 zextend_p);
8501 if (htem != hipart)
8502 emit_move_insn (hipart, htem);
8503 return REDUCE_BIT_FIELD (temp);
8507 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8508 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8509 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8510 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8512 case FMA_EXPR:
8514 optab opt = fma_optab;
8515 gimple *def0, *def2;
8517 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8518 call. */
8519 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8521 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8522 tree call_expr;
8524 gcc_assert (fn != NULL_TREE);
8525 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8526 return expand_builtin (call_expr, target, subtarget, mode, false);
8529 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8530 /* The multiplication is commutative - look at its 2nd operand
8531 if the first isn't fed by a negate. */
8532 if (!def0)
8534 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8535 /* Swap operands if the 2nd operand is fed by a negate. */
8536 if (def0)
8537 std::swap (treeop0, treeop1);
8539 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8541 op0 = op2 = NULL;
8543 if (def0 && def2
8544 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8546 opt = fnms_optab;
8547 op0 = expand_normal (gimple_assign_rhs1 (def0));
8548 op2 = expand_normal (gimple_assign_rhs1 (def2));
8550 else if (def0
8551 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8553 opt = fnma_optab;
8554 op0 = expand_normal (gimple_assign_rhs1 (def0));
8556 else if (def2
8557 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8559 opt = fms_optab;
8560 op2 = expand_normal (gimple_assign_rhs1 (def2));
8563 if (op0 == NULL)
8564 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8565 if (op2 == NULL)
8566 op2 = expand_normal (treeop2);
8567 op1 = expand_normal (treeop1);
8569 return expand_ternary_op (TYPE_MODE (type), opt,
8570 op0, op1, op2, target, 0);
8573 case MULT_EXPR:
8574 /* If this is a fixed-point operation, then we cannot use the code
8575 below because "expand_mult" doesn't support sat/no-sat fixed-point
8576 multiplications. */
8577 if (ALL_FIXED_POINT_MODE_P (mode))
8578 goto binop;
8580 /* If first operand is constant, swap them.
8581 Thus the following special case checks need only
8582 check the second operand. */
8583 if (TREE_CODE (treeop0) == INTEGER_CST)
8584 std::swap (treeop0, treeop1);
8586 /* Attempt to return something suitable for generating an
8587 indexed address, for machines that support that. */
8589 if (modifier == EXPAND_SUM && mode == ptr_mode
8590 && tree_fits_shwi_p (treeop1))
8592 tree exp1 = treeop1;
8594 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8595 EXPAND_SUM);
8597 if (!REG_P (op0))
8598 op0 = force_operand (op0, NULL_RTX);
8599 if (!REG_P (op0))
8600 op0 = copy_to_mode_reg (mode, op0);
8602 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8603 gen_int_mode (tree_to_shwi (exp1),
8604 TYPE_MODE (TREE_TYPE (exp1)))));
8607 if (modifier == EXPAND_STACK_PARM)
8608 target = 0;
8610 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8611 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8613 case TRUNC_DIV_EXPR:
8614 case FLOOR_DIV_EXPR:
8615 case CEIL_DIV_EXPR:
8616 case ROUND_DIV_EXPR:
8617 case EXACT_DIV_EXPR:
8618 /* If this is a fixed-point operation, then we cannot use the code
8619 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8620 divisions. */
8621 if (ALL_FIXED_POINT_MODE_P (mode))
8622 goto binop;
8624 if (modifier == EXPAND_STACK_PARM)
8625 target = 0;
8626 /* Possible optimization: compute the dividend with EXPAND_SUM
8627 then if the divisor is constant can optimize the case
8628 where some terms of the dividend have coeffs divisible by it. */
8629 expand_operands (treeop0, treeop1,
8630 subtarget, &op0, &op1, EXPAND_NORMAL);
8631 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8633 case RDIV_EXPR:
8634 goto binop;
8636 case MULT_HIGHPART_EXPR:
8637 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8638 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8639 gcc_assert (temp);
8640 return temp;
8642 case TRUNC_MOD_EXPR:
8643 case FLOOR_MOD_EXPR:
8644 case CEIL_MOD_EXPR:
8645 case ROUND_MOD_EXPR:
8646 if (modifier == EXPAND_STACK_PARM)
8647 target = 0;
8648 expand_operands (treeop0, treeop1,
8649 subtarget, &op0, &op1, EXPAND_NORMAL);
8650 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8652 case FIXED_CONVERT_EXPR:
8653 op0 = expand_normal (treeop0);
8654 if (target == 0 || modifier == EXPAND_STACK_PARM)
8655 target = gen_reg_rtx (mode);
8657 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8658 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8659 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8660 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8661 else
8662 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8663 return target;
8665 case FIX_TRUNC_EXPR:
8666 op0 = expand_normal (treeop0);
8667 if (target == 0 || modifier == EXPAND_STACK_PARM)
8668 target = gen_reg_rtx (mode);
8669 expand_fix (target, op0, unsignedp);
8670 return target;
8672 case FLOAT_EXPR:
8673 op0 = expand_normal (treeop0);
8674 if (target == 0 || modifier == EXPAND_STACK_PARM)
8675 target = gen_reg_rtx (mode);
8676 /* expand_float can't figure out what to do if FROM has VOIDmode.
8677 So give it the correct mode. With -O, cse will optimize this. */
8678 if (GET_MODE (op0) == VOIDmode)
8679 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8680 op0);
8681 expand_float (target, op0,
8682 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8683 return target;
8685 case NEGATE_EXPR:
8686 op0 = expand_expr (treeop0, subtarget,
8687 VOIDmode, EXPAND_NORMAL);
8688 if (modifier == EXPAND_STACK_PARM)
8689 target = 0;
8690 temp = expand_unop (mode,
8691 optab_for_tree_code (NEGATE_EXPR, type,
8692 optab_default),
8693 op0, target, 0);
8694 gcc_assert (temp);
8695 return REDUCE_BIT_FIELD (temp);
8697 case ABS_EXPR:
8698 op0 = expand_expr (treeop0, subtarget,
8699 VOIDmode, EXPAND_NORMAL);
8700 if (modifier == EXPAND_STACK_PARM)
8701 target = 0;
8703 /* ABS_EXPR is not valid for complex arguments. */
8704 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8705 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8707 /* Unsigned abs is simply the operand. Testing here means we don't
8708 risk generating incorrect code below. */
8709 if (TYPE_UNSIGNED (type))
8710 return op0;
8712 return expand_abs (mode, op0, target, unsignedp,
8713 safe_from_p (target, treeop0, 1));
8715 case MAX_EXPR:
8716 case MIN_EXPR:
8717 target = original_target;
8718 if (target == 0
8719 || modifier == EXPAND_STACK_PARM
8720 || (MEM_P (target) && MEM_VOLATILE_P (target))
8721 || GET_MODE (target) != mode
8722 || (REG_P (target)
8723 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8724 target = gen_reg_rtx (mode);
8725 expand_operands (treeop0, treeop1,
8726 target, &op0, &op1, EXPAND_NORMAL);
8728 /* First try to do it with a special MIN or MAX instruction.
8729 If that does not win, use a conditional jump to select the proper
8730 value. */
8731 this_optab = optab_for_tree_code (code, type, optab_default);
8732 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8733 OPTAB_WIDEN);
8734 if (temp != 0)
8735 return temp;
8737 /* At this point, a MEM target is no longer useful; we will get better
8738 code without it. */
8740 if (! REG_P (target))
8741 target = gen_reg_rtx (mode);
8743 /* If op1 was placed in target, swap op0 and op1. */
8744 if (target != op0 && target == op1)
8745 std::swap (op0, op1);
8747 /* We generate better code and avoid problems with op1 mentioning
8748 target by forcing op1 into a pseudo if it isn't a constant. */
8749 if (! CONSTANT_P (op1))
8750 op1 = force_reg (mode, op1);
8753 enum rtx_code comparison_code;
8754 rtx cmpop1 = op1;
8756 if (code == MAX_EXPR)
8757 comparison_code = unsignedp ? GEU : GE;
8758 else
8759 comparison_code = unsignedp ? LEU : LE;
8761 /* Canonicalize to comparisons against 0. */
8762 if (op1 == const1_rtx)
8764 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8765 or (a != 0 ? a : 1) for unsigned.
8766 For MIN we are safe converting (a <= 1 ? a : 1)
8767 into (a <= 0 ? a : 1) */
8768 cmpop1 = const0_rtx;
8769 if (code == MAX_EXPR)
8770 comparison_code = unsignedp ? NE : GT;
8772 if (op1 == constm1_rtx && !unsignedp)
8774 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8775 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8776 cmpop1 = const0_rtx;
8777 if (code == MIN_EXPR)
8778 comparison_code = LT;
8781 /* Use a conditional move if possible. */
8782 if (can_conditionally_move_p (mode))
8784 rtx insn;
8786 start_sequence ();
8788 /* Try to emit the conditional move. */
8789 insn = emit_conditional_move (target, comparison_code,
8790 op0, cmpop1, mode,
8791 op0, op1, mode,
8792 unsignedp);
8794 /* If we could do the conditional move, emit the sequence,
8795 and return. */
8796 if (insn)
8798 rtx_insn *seq = get_insns ();
8799 end_sequence ();
8800 emit_insn (seq);
8801 return target;
8804 /* Otherwise discard the sequence and fall back to code with
8805 branches. */
8806 end_sequence ();
8809 if (target != op0)
8810 emit_move_insn (target, op0);
8812 lab = gen_label_rtx ();
8813 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8814 unsignedp, mode, NULL_RTX, NULL, lab,
8815 -1);
8817 emit_move_insn (target, op1);
8818 emit_label (lab);
8819 return target;
8821 case BIT_NOT_EXPR:
8822 op0 = expand_expr (treeop0, subtarget,
8823 VOIDmode, EXPAND_NORMAL);
8824 if (modifier == EXPAND_STACK_PARM)
8825 target = 0;
8826 /* In case we have to reduce the result to bitfield precision
8827 for unsigned bitfield expand this as XOR with a proper constant
8828 instead. */
8829 if (reduce_bit_field && TYPE_UNSIGNED (type))
8831 wide_int mask = wi::mask (TYPE_PRECISION (type),
8832 false, GET_MODE_PRECISION (mode));
8834 temp = expand_binop (mode, xor_optab, op0,
8835 immed_wide_int_const (mask, mode),
8836 target, 1, OPTAB_LIB_WIDEN);
8838 else
8839 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8840 gcc_assert (temp);
8841 return temp;
8843 /* ??? Can optimize bitwise operations with one arg constant.
8844 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8845 and (a bitwise1 b) bitwise2 b (etc)
8846 but that is probably not worth while. */
8848 case BIT_AND_EXPR:
8849 case BIT_IOR_EXPR:
8850 case BIT_XOR_EXPR:
8851 goto binop;
8853 case LROTATE_EXPR:
8854 case RROTATE_EXPR:
8855 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8856 || (GET_MODE_PRECISION (TYPE_MODE (type))
8857 == TYPE_PRECISION (type)));
8858 /* fall through */
8860 case LSHIFT_EXPR:
8861 case RSHIFT_EXPR:
8863 /* If this is a fixed-point operation, then we cannot use the code
8864 below because "expand_shift" doesn't support sat/no-sat fixed-point
8865 shifts. */
8866 if (ALL_FIXED_POINT_MODE_P (mode))
8867 goto binop;
8869 if (! safe_from_p (subtarget, treeop1, 1))
8870 subtarget = 0;
8871 if (modifier == EXPAND_STACK_PARM)
8872 target = 0;
8873 op0 = expand_expr (treeop0, subtarget,
8874 VOIDmode, EXPAND_NORMAL);
8876 /* Left shift optimization when shifting across word_size boundary.
8878 If mode == GET_MODE_WIDER_MODE (word_mode), then normally there isn't
8879 native instruction to support this wide mode left shift. Given below
8880 scenario:
8882 Type A = (Type) B << C
8884 |< T >|
8885 | dest_high | dest_low |
8887 | word_size |
8889 If the shift amount C caused we shift B to across the word size
8890 boundary, i.e part of B shifted into high half of destination
8891 register, and part of B remains in the low half, then GCC will use
8892 the following left shift expand logic:
8894 1. Initialize dest_low to B.
8895 2. Initialize every bit of dest_high to the sign bit of B.
8896 3. Logic left shift dest_low by C bit to finalize dest_low.
8897 The value of dest_low before this shift is kept in a temp D.
8898 4. Logic left shift dest_high by C.
8899 5. Logic right shift D by (word_size - C).
8900 6. Or the result of 4 and 5 to finalize dest_high.
8902 While, by checking gimple statements, if operand B is coming from
8903 signed extension, then we can simplify above expand logic into:
8905 1. dest_high = src_low >> (word_size - C).
8906 2. dest_low = src_low << C.
8908 We can use one arithmetic right shift to finish all the purpose of
8909 steps 2, 4, 5, 6, thus we reduce the steps needed from 6 into 2. */
8911 temp = NULL_RTX;
8912 if (code == LSHIFT_EXPR
8913 && target
8914 && REG_P (target)
8915 && ! unsignedp
8916 && mode == GET_MODE_WIDER_MODE (word_mode)
8917 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
8918 && TREE_CONSTANT (treeop1)
8919 && TREE_CODE (treeop0) == SSA_NAME)
8921 gimple *def = SSA_NAME_DEF_STMT (treeop0);
8922 if (is_gimple_assign (def)
8923 && gimple_assign_rhs_code (def) == NOP_EXPR)
8925 machine_mode rmode = TYPE_MODE
8926 (TREE_TYPE (gimple_assign_rhs1 (def)));
8928 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
8929 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
8930 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
8931 >= GET_MODE_BITSIZE (word_mode)))
8933 rtx_insn *seq, *seq_old;
8934 unsigned int high_off = subreg_highpart_offset (word_mode,
8935 mode);
8936 rtx low = lowpart_subreg (word_mode, op0, mode);
8937 rtx dest_low = lowpart_subreg (word_mode, target, mode);
8938 rtx dest_high = simplify_gen_subreg (word_mode, target,
8939 mode, high_off);
8940 HOST_WIDE_INT ramount = (BITS_PER_WORD
8941 - TREE_INT_CST_LOW (treeop1));
8942 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
8944 start_sequence ();
8945 /* dest_high = src_low >> (word_size - C). */
8946 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
8947 rshift, dest_high, unsignedp);
8948 if (temp != dest_high)
8949 emit_move_insn (dest_high, temp);
8951 /* dest_low = src_low << C. */
8952 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
8953 treeop1, dest_low, unsignedp);
8954 if (temp != dest_low)
8955 emit_move_insn (dest_low, temp);
8957 seq = get_insns ();
8958 end_sequence ();
8959 temp = target ;
8961 if (have_insn_for (ASHIFT, mode))
8963 bool speed_p = optimize_insn_for_speed_p ();
8964 start_sequence ();
8965 rtx ret_old = expand_variable_shift (code, mode, op0,
8966 treeop1, target,
8967 unsignedp);
8969 seq_old = get_insns ();
8970 end_sequence ();
8971 if (seq_cost (seq, speed_p)
8972 >= seq_cost (seq_old, speed_p))
8974 seq = seq_old;
8975 temp = ret_old;
8978 emit_insn (seq);
8983 if (temp == NULL_RTX)
8984 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8985 unsignedp);
8986 if (code == LSHIFT_EXPR)
8987 temp = REDUCE_BIT_FIELD (temp);
8988 return temp;
8991 /* Could determine the answer when only additive constants differ. Also,
8992 the addition of one can be handled by changing the condition. */
8993 case LT_EXPR:
8994 case LE_EXPR:
8995 case GT_EXPR:
8996 case GE_EXPR:
8997 case EQ_EXPR:
8998 case NE_EXPR:
8999 case UNORDERED_EXPR:
9000 case ORDERED_EXPR:
9001 case UNLT_EXPR:
9002 case UNLE_EXPR:
9003 case UNGT_EXPR:
9004 case UNGE_EXPR:
9005 case UNEQ_EXPR:
9006 case LTGT_EXPR:
9008 temp = do_store_flag (ops,
9009 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9010 tmode != VOIDmode ? tmode : mode);
9011 if (temp)
9012 return temp;
9014 /* Use a compare and a jump for BLKmode comparisons, or for function
9015 type comparisons is have_canonicalize_funcptr_for_compare. */
9017 if ((target == 0
9018 || modifier == EXPAND_STACK_PARM
9019 || ! safe_from_p (target, treeop0, 1)
9020 || ! safe_from_p (target, treeop1, 1)
9021 /* Make sure we don't have a hard reg (such as function's return
9022 value) live across basic blocks, if not optimizing. */
9023 || (!optimize && REG_P (target)
9024 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9025 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9027 emit_move_insn (target, const0_rtx);
9029 rtx_code_label *lab1 = gen_label_rtx ();
9030 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9032 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9033 emit_move_insn (target, constm1_rtx);
9034 else
9035 emit_move_insn (target, const1_rtx);
9037 emit_label (lab1);
9038 return target;
9040 case COMPLEX_EXPR:
9041 /* Get the rtx code of the operands. */
9042 op0 = expand_normal (treeop0);
9043 op1 = expand_normal (treeop1);
9045 if (!target)
9046 target = gen_reg_rtx (TYPE_MODE (type));
9047 else
9048 /* If target overlaps with op1, then either we need to force
9049 op1 into a pseudo (if target also overlaps with op0),
9050 or write the complex parts in reverse order. */
9051 switch (GET_CODE (target))
9053 case CONCAT:
9054 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9056 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9058 complex_expr_force_op1:
9059 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9060 emit_move_insn (temp, op1);
9061 op1 = temp;
9062 break;
9064 complex_expr_swap_order:
9065 /* Move the imaginary (op1) and real (op0) parts to their
9066 location. */
9067 write_complex_part (target, op1, true);
9068 write_complex_part (target, op0, false);
9070 return target;
9072 break;
9073 case MEM:
9074 temp = adjust_address_nv (target,
9075 GET_MODE_INNER (GET_MODE (target)), 0);
9076 if (reg_overlap_mentioned_p (temp, op1))
9078 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9079 temp = adjust_address_nv (target, imode,
9080 GET_MODE_SIZE (imode));
9081 if (reg_overlap_mentioned_p (temp, op0))
9082 goto complex_expr_force_op1;
9083 goto complex_expr_swap_order;
9085 break;
9086 default:
9087 if (reg_overlap_mentioned_p (target, op1))
9089 if (reg_overlap_mentioned_p (target, op0))
9090 goto complex_expr_force_op1;
9091 goto complex_expr_swap_order;
9093 break;
9096 /* Move the real (op0) and imaginary (op1) parts to their location. */
9097 write_complex_part (target, op0, false);
9098 write_complex_part (target, op1, true);
9100 return target;
9102 case WIDEN_SUM_EXPR:
9104 tree oprnd0 = treeop0;
9105 tree oprnd1 = treeop1;
9107 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9108 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9109 target, unsignedp);
9110 return target;
9113 case REDUC_MAX_EXPR:
9114 case REDUC_MIN_EXPR:
9115 case REDUC_PLUS_EXPR:
9117 op0 = expand_normal (treeop0);
9118 this_optab = optab_for_tree_code (code, type, optab_default);
9119 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9121 if (optab_handler (this_optab, vec_mode) != CODE_FOR_nothing)
9123 struct expand_operand ops[2];
9124 enum insn_code icode = optab_handler (this_optab, vec_mode);
9126 create_output_operand (&ops[0], target, mode);
9127 create_input_operand (&ops[1], op0, vec_mode);
9128 if (maybe_expand_insn (icode, 2, ops))
9130 target = ops[0].value;
9131 if (GET_MODE (target) != mode)
9132 return gen_lowpart (tmode, target);
9133 return target;
9136 /* Fall back to optab with vector result, and then extract scalar. */
9137 this_optab = scalar_reduc_to_vector (this_optab, type);
9138 temp = expand_unop (vec_mode, this_optab, op0, NULL_RTX, unsignedp);
9139 gcc_assert (temp);
9140 /* The tree code produces a scalar result, but (somewhat by convention)
9141 the optab produces a vector with the result in element 0 if
9142 little-endian, or element N-1 if big-endian. So pull the scalar
9143 result out of that element. */
9144 int index = BYTES_BIG_ENDIAN ? GET_MODE_NUNITS (vec_mode) - 1 : 0;
9145 int bitsize = GET_MODE_UNIT_BITSIZE (vec_mode);
9146 temp = extract_bit_field (temp, bitsize, bitsize * index, unsignedp,
9147 target, mode, mode);
9148 gcc_assert (temp);
9149 return temp;
9152 case VEC_UNPACK_HI_EXPR:
9153 case VEC_UNPACK_LO_EXPR:
9155 op0 = expand_normal (treeop0);
9156 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9157 target, unsignedp);
9158 gcc_assert (temp);
9159 return temp;
9162 case VEC_UNPACK_FLOAT_HI_EXPR:
9163 case VEC_UNPACK_FLOAT_LO_EXPR:
9165 op0 = expand_normal (treeop0);
9166 /* The signedness is determined from input operand. */
9167 temp = expand_widen_pattern_expr
9168 (ops, op0, NULL_RTX, NULL_RTX,
9169 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9171 gcc_assert (temp);
9172 return temp;
9175 case VEC_WIDEN_MULT_HI_EXPR:
9176 case VEC_WIDEN_MULT_LO_EXPR:
9177 case VEC_WIDEN_MULT_EVEN_EXPR:
9178 case VEC_WIDEN_MULT_ODD_EXPR:
9179 case VEC_WIDEN_LSHIFT_HI_EXPR:
9180 case VEC_WIDEN_LSHIFT_LO_EXPR:
9181 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9182 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9183 target, unsignedp);
9184 gcc_assert (target);
9185 return target;
9187 case VEC_PACK_TRUNC_EXPR:
9188 case VEC_PACK_SAT_EXPR:
9189 case VEC_PACK_FIX_TRUNC_EXPR:
9190 mode = TYPE_MODE (TREE_TYPE (treeop0));
9191 goto binop;
9193 case VEC_PERM_EXPR:
9194 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9195 op2 = expand_normal (treeop2);
9197 /* Careful here: if the target doesn't support integral vector modes,
9198 a constant selection vector could wind up smooshed into a normal
9199 integral constant. */
9200 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9202 tree sel_type = TREE_TYPE (treeop2);
9203 machine_mode vmode
9204 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9205 TYPE_VECTOR_SUBPARTS (sel_type));
9206 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9207 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9208 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9210 else
9211 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9213 temp = expand_vec_perm (mode, op0, op1, op2, target);
9214 gcc_assert (temp);
9215 return temp;
9217 case DOT_PROD_EXPR:
9219 tree oprnd0 = treeop0;
9220 tree oprnd1 = treeop1;
9221 tree oprnd2 = treeop2;
9222 rtx op2;
9224 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9225 op2 = expand_normal (oprnd2);
9226 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9227 target, unsignedp);
9228 return target;
9231 case SAD_EXPR:
9233 tree oprnd0 = treeop0;
9234 tree oprnd1 = treeop1;
9235 tree oprnd2 = treeop2;
9236 rtx op2;
9238 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9239 op2 = expand_normal (oprnd2);
9240 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9241 target, unsignedp);
9242 return target;
9245 case REALIGN_LOAD_EXPR:
9247 tree oprnd0 = treeop0;
9248 tree oprnd1 = treeop1;
9249 tree oprnd2 = treeop2;
9250 rtx op2;
9252 this_optab = optab_for_tree_code (code, type, optab_default);
9253 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9254 op2 = expand_normal (oprnd2);
9255 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9256 target, unsignedp);
9257 gcc_assert (temp);
9258 return temp;
9261 case COND_EXPR:
9263 /* A COND_EXPR with its type being VOID_TYPE represents a
9264 conditional jump and is handled in
9265 expand_gimple_cond_expr. */
9266 gcc_assert (!VOID_TYPE_P (type));
9268 /* Note that COND_EXPRs whose type is a structure or union
9269 are required to be constructed to contain assignments of
9270 a temporary variable, so that we can evaluate them here
9271 for side effect only. If type is void, we must do likewise. */
9273 gcc_assert (!TREE_ADDRESSABLE (type)
9274 && !ignore
9275 && TREE_TYPE (treeop1) != void_type_node
9276 && TREE_TYPE (treeop2) != void_type_node);
9278 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9279 if (temp)
9280 return temp;
9282 /* If we are not to produce a result, we have no target. Otherwise,
9283 if a target was specified use it; it will not be used as an
9284 intermediate target unless it is safe. If no target, use a
9285 temporary. */
9287 if (modifier != EXPAND_STACK_PARM
9288 && original_target
9289 && safe_from_p (original_target, treeop0, 1)
9290 && GET_MODE (original_target) == mode
9291 && !MEM_P (original_target))
9292 temp = original_target;
9293 else
9294 temp = assign_temp (type, 0, 1);
9296 do_pending_stack_adjust ();
9297 NO_DEFER_POP;
9298 rtx_code_label *lab0 = gen_label_rtx ();
9299 rtx_code_label *lab1 = gen_label_rtx ();
9300 jumpifnot (treeop0, lab0, -1);
9301 store_expr (treeop1, temp,
9302 modifier == EXPAND_STACK_PARM,
9303 false);
9305 emit_jump_insn (targetm.gen_jump (lab1));
9306 emit_barrier ();
9307 emit_label (lab0);
9308 store_expr (treeop2, temp,
9309 modifier == EXPAND_STACK_PARM,
9310 false);
9312 emit_label (lab1);
9313 OK_DEFER_POP;
9314 return temp;
9317 case VEC_COND_EXPR:
9318 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9319 return target;
9321 default:
9322 gcc_unreachable ();
9325 /* Here to do an ordinary binary operator. */
9326 binop:
9327 expand_operands (treeop0, treeop1,
9328 subtarget, &op0, &op1, EXPAND_NORMAL);
9329 binop2:
9330 this_optab = optab_for_tree_code (code, type, optab_default);
9331 binop3:
9332 if (modifier == EXPAND_STACK_PARM)
9333 target = 0;
9334 temp = expand_binop (mode, this_optab, op0, op1, target,
9335 unsignedp, OPTAB_LIB_WIDEN);
9336 gcc_assert (temp);
9337 /* Bitwise operations do not need bitfield reduction as we expect their
9338 operands being properly truncated. */
9339 if (code == BIT_XOR_EXPR
9340 || code == BIT_AND_EXPR
9341 || code == BIT_IOR_EXPR)
9342 return temp;
9343 return REDUCE_BIT_FIELD (temp);
9345 #undef REDUCE_BIT_FIELD
9348 /* Return TRUE if expression STMT is suitable for replacement.
9349 Never consider memory loads as replaceable, because those don't ever lead
9350 into constant expressions. */
9352 static bool
9353 stmt_is_replaceable_p (gimple *stmt)
9355 if (ssa_is_replaceable_p (stmt))
9357 /* Don't move around loads. */
9358 if (!gimple_assign_single_p (stmt)
9359 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9360 return true;
9362 return false;
9366 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9367 enum expand_modifier modifier, rtx *alt_rtl,
9368 bool inner_reference_p)
9370 rtx op0, op1, temp, decl_rtl;
9371 tree type;
9372 int unsignedp;
9373 machine_mode mode, dmode;
9374 enum tree_code code = TREE_CODE (exp);
9375 rtx subtarget, original_target;
9376 int ignore;
9377 tree context;
9378 bool reduce_bit_field;
9379 location_t loc = EXPR_LOCATION (exp);
9380 struct separate_ops ops;
9381 tree treeop0, treeop1, treeop2;
9382 tree ssa_name = NULL_TREE;
9383 gimple *g;
9385 type = TREE_TYPE (exp);
9386 mode = TYPE_MODE (type);
9387 unsignedp = TYPE_UNSIGNED (type);
9389 treeop0 = treeop1 = treeop2 = NULL_TREE;
9390 if (!VL_EXP_CLASS_P (exp))
9391 switch (TREE_CODE_LENGTH (code))
9393 default:
9394 case 3: treeop2 = TREE_OPERAND (exp, 2);
9395 case 2: treeop1 = TREE_OPERAND (exp, 1);
9396 case 1: treeop0 = TREE_OPERAND (exp, 0);
9397 case 0: break;
9399 ops.code = code;
9400 ops.type = type;
9401 ops.op0 = treeop0;
9402 ops.op1 = treeop1;
9403 ops.op2 = treeop2;
9404 ops.location = loc;
9406 ignore = (target == const0_rtx
9407 || ((CONVERT_EXPR_CODE_P (code)
9408 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9409 && TREE_CODE (type) == VOID_TYPE));
9411 /* An operation in what may be a bit-field type needs the
9412 result to be reduced to the precision of the bit-field type,
9413 which is narrower than that of the type's mode. */
9414 reduce_bit_field = (!ignore
9415 && INTEGRAL_TYPE_P (type)
9416 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9418 /* If we are going to ignore this result, we need only do something
9419 if there is a side-effect somewhere in the expression. If there
9420 is, short-circuit the most common cases here. Note that we must
9421 not call expand_expr with anything but const0_rtx in case this
9422 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9424 if (ignore)
9426 if (! TREE_SIDE_EFFECTS (exp))
9427 return const0_rtx;
9429 /* Ensure we reference a volatile object even if value is ignored, but
9430 don't do this if all we are doing is taking its address. */
9431 if (TREE_THIS_VOLATILE (exp)
9432 && TREE_CODE (exp) != FUNCTION_DECL
9433 && mode != VOIDmode && mode != BLKmode
9434 && modifier != EXPAND_CONST_ADDRESS)
9436 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9437 if (MEM_P (temp))
9438 copy_to_reg (temp);
9439 return const0_rtx;
9442 if (TREE_CODE_CLASS (code) == tcc_unary
9443 || code == BIT_FIELD_REF
9444 || code == COMPONENT_REF
9445 || code == INDIRECT_REF)
9446 return expand_expr (treeop0, const0_rtx, VOIDmode,
9447 modifier);
9449 else if (TREE_CODE_CLASS (code) == tcc_binary
9450 || TREE_CODE_CLASS (code) == tcc_comparison
9451 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9453 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9454 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9455 return const0_rtx;
9458 target = 0;
9461 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9462 target = 0;
9464 /* Use subtarget as the target for operand 0 of a binary operation. */
9465 subtarget = get_subtarget (target);
9466 original_target = target;
9468 switch (code)
9470 case LABEL_DECL:
9472 tree function = decl_function_context (exp);
9474 temp = label_rtx (exp);
9475 temp = gen_rtx_LABEL_REF (Pmode, temp);
9477 if (function != current_function_decl
9478 && function != 0)
9479 LABEL_REF_NONLOCAL_P (temp) = 1;
9481 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9482 return temp;
9485 case SSA_NAME:
9486 /* ??? ivopts calls expander, without any preparation from
9487 out-of-ssa. So fake instructions as if this was an access to the
9488 base variable. This unnecessarily allocates a pseudo, see how we can
9489 reuse it, if partition base vars have it set already. */
9490 if (!currently_expanding_to_rtl)
9492 tree var = SSA_NAME_VAR (exp);
9493 if (var && DECL_RTL_SET_P (var))
9494 return DECL_RTL (var);
9495 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9496 LAST_VIRTUAL_REGISTER + 1);
9499 g = get_gimple_for_ssa_name (exp);
9500 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9501 if (g == NULL
9502 && modifier == EXPAND_INITIALIZER
9503 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9504 && (optimize || !SSA_NAME_VAR (exp)
9505 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9506 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9507 g = SSA_NAME_DEF_STMT (exp);
9508 if (g)
9510 rtx r;
9511 location_t saved_loc = curr_insn_location ();
9512 location_t loc = gimple_location (g);
9513 if (loc != UNKNOWN_LOCATION)
9514 set_curr_insn_location (loc);
9515 ops.code = gimple_assign_rhs_code (g);
9516 switch (get_gimple_rhs_class (ops.code))
9518 case GIMPLE_TERNARY_RHS:
9519 ops.op2 = gimple_assign_rhs3 (g);
9520 /* Fallthru */
9521 case GIMPLE_BINARY_RHS:
9522 ops.op1 = gimple_assign_rhs2 (g);
9524 /* Try to expand conditonal compare. */
9525 if (targetm.gen_ccmp_first)
9527 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9528 r = expand_ccmp_expr (g);
9529 if (r)
9530 break;
9532 /* Fallthru */
9533 case GIMPLE_UNARY_RHS:
9534 ops.op0 = gimple_assign_rhs1 (g);
9535 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9536 ops.location = loc;
9537 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9538 break;
9539 case GIMPLE_SINGLE_RHS:
9541 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9542 tmode, modifier, NULL, inner_reference_p);
9543 break;
9545 default:
9546 gcc_unreachable ();
9548 set_curr_insn_location (saved_loc);
9549 if (REG_P (r) && !REG_EXPR (r))
9550 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9551 return r;
9554 ssa_name = exp;
9555 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9556 exp = SSA_NAME_VAR (ssa_name);
9557 goto expand_decl_rtl;
9559 case PARM_DECL:
9560 case VAR_DECL:
9561 /* If a static var's type was incomplete when the decl was written,
9562 but the type is complete now, lay out the decl now. */
9563 if (DECL_SIZE (exp) == 0
9564 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9565 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9566 layout_decl (exp, 0);
9568 /* ... fall through ... */
9570 case FUNCTION_DECL:
9571 case RESULT_DECL:
9572 decl_rtl = DECL_RTL (exp);
9573 expand_decl_rtl:
9574 gcc_assert (decl_rtl);
9575 decl_rtl = copy_rtx (decl_rtl);
9576 /* Record writes to register variables. */
9577 if (modifier == EXPAND_WRITE
9578 && REG_P (decl_rtl)
9579 && HARD_REGISTER_P (decl_rtl))
9580 add_to_hard_reg_set (&crtl->asm_clobbers,
9581 GET_MODE (decl_rtl), REGNO (decl_rtl));
9583 /* Ensure variable marked as used even if it doesn't go through
9584 a parser. If it hasn't be used yet, write out an external
9585 definition. */
9586 if (exp)
9587 TREE_USED (exp) = 1;
9589 /* Show we haven't gotten RTL for this yet. */
9590 temp = 0;
9592 /* Variables inherited from containing functions should have
9593 been lowered by this point. */
9594 if (exp)
9595 context = decl_function_context (exp);
9596 gcc_assert (!exp
9597 || SCOPE_FILE_SCOPE_P (context)
9598 || context == current_function_decl
9599 || TREE_STATIC (exp)
9600 || DECL_EXTERNAL (exp)
9601 /* ??? C++ creates functions that are not TREE_STATIC. */
9602 || TREE_CODE (exp) == FUNCTION_DECL);
9604 /* This is the case of an array whose size is to be determined
9605 from its initializer, while the initializer is still being parsed.
9606 ??? We aren't parsing while expanding anymore. */
9608 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9609 temp = validize_mem (decl_rtl);
9611 /* If DECL_RTL is memory, we are in the normal case and the
9612 address is not valid, get the address into a register. */
9614 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9616 if (alt_rtl)
9617 *alt_rtl = decl_rtl;
9618 decl_rtl = use_anchored_address (decl_rtl);
9619 if (modifier != EXPAND_CONST_ADDRESS
9620 && modifier != EXPAND_SUM
9621 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9622 : GET_MODE (decl_rtl),
9623 XEXP (decl_rtl, 0),
9624 MEM_ADDR_SPACE (decl_rtl)))
9625 temp = replace_equiv_address (decl_rtl,
9626 copy_rtx (XEXP (decl_rtl, 0)));
9629 /* If we got something, return it. But first, set the alignment
9630 if the address is a register. */
9631 if (temp != 0)
9633 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9634 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9636 return temp;
9639 if (exp)
9640 dmode = DECL_MODE (exp);
9641 else
9642 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9644 /* If the mode of DECL_RTL does not match that of the decl,
9645 there are two cases: we are dealing with a BLKmode value
9646 that is returned in a register, or we are dealing with
9647 a promoted value. In the latter case, return a SUBREG
9648 of the wanted mode, but mark it so that we know that it
9649 was already extended. */
9650 if (REG_P (decl_rtl)
9651 && dmode != BLKmode
9652 && GET_MODE (decl_rtl) != dmode)
9654 machine_mode pmode;
9656 /* Get the signedness to be used for this variable. Ensure we get
9657 the same mode we got when the variable was declared. */
9658 if (code != SSA_NAME)
9659 pmode = promote_decl_mode (exp, &unsignedp);
9660 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9661 && gimple_code (g) == GIMPLE_CALL
9662 && !gimple_call_internal_p (g))
9663 pmode = promote_function_mode (type, mode, &unsignedp,
9664 gimple_call_fntype (g),
9666 else
9667 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9668 gcc_assert (GET_MODE (decl_rtl) == pmode);
9670 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9671 SUBREG_PROMOTED_VAR_P (temp) = 1;
9672 SUBREG_PROMOTED_SET (temp, unsignedp);
9673 return temp;
9676 return decl_rtl;
9678 case INTEGER_CST:
9679 /* Given that TYPE_PRECISION (type) is not always equal to
9680 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9681 the former to the latter according to the signedness of the
9682 type. */
9683 temp = immed_wide_int_const (wide_int::from
9684 (exp,
9685 GET_MODE_PRECISION (TYPE_MODE (type)),
9686 TYPE_SIGN (type)),
9687 TYPE_MODE (type));
9688 return temp;
9690 case VECTOR_CST:
9692 tree tmp = NULL_TREE;
9693 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9694 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9695 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9696 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9697 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9698 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9699 return const_vector_from_tree (exp);
9700 if (GET_MODE_CLASS (mode) == MODE_INT)
9702 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9703 if (type_for_mode)
9704 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9706 if (!tmp)
9708 vec<constructor_elt, va_gc> *v;
9709 unsigned i;
9710 vec_alloc (v, VECTOR_CST_NELTS (exp));
9711 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9712 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9713 tmp = build_constructor (type, v);
9715 return expand_expr (tmp, ignore ? const0_rtx : target,
9716 tmode, modifier);
9719 case CONST_DECL:
9720 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9722 case REAL_CST:
9723 /* If optimized, generate immediate CONST_DOUBLE
9724 which will be turned into memory by reload if necessary.
9726 We used to force a register so that loop.c could see it. But
9727 this does not allow gen_* patterns to perform optimizations with
9728 the constants. It also produces two insns in cases like "x = 1.0;".
9729 On most machines, floating-point constants are not permitted in
9730 many insns, so we'd end up copying it to a register in any case.
9732 Now, we do the copying in expand_binop, if appropriate. */
9733 return const_double_from_real_value (TREE_REAL_CST (exp),
9734 TYPE_MODE (TREE_TYPE (exp)));
9736 case FIXED_CST:
9737 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9738 TYPE_MODE (TREE_TYPE (exp)));
9740 case COMPLEX_CST:
9741 /* Handle evaluating a complex constant in a CONCAT target. */
9742 if (original_target && GET_CODE (original_target) == CONCAT)
9744 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9745 rtx rtarg, itarg;
9747 rtarg = XEXP (original_target, 0);
9748 itarg = XEXP (original_target, 1);
9750 /* Move the real and imaginary parts separately. */
9751 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9752 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9754 if (op0 != rtarg)
9755 emit_move_insn (rtarg, op0);
9756 if (op1 != itarg)
9757 emit_move_insn (itarg, op1);
9759 return original_target;
9762 /* ... fall through ... */
9764 case STRING_CST:
9765 temp = expand_expr_constant (exp, 1, modifier);
9767 /* temp contains a constant address.
9768 On RISC machines where a constant address isn't valid,
9769 make some insns to get that address into a register. */
9770 if (modifier != EXPAND_CONST_ADDRESS
9771 && modifier != EXPAND_INITIALIZER
9772 && modifier != EXPAND_SUM
9773 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9774 MEM_ADDR_SPACE (temp)))
9775 return replace_equiv_address (temp,
9776 copy_rtx (XEXP (temp, 0)));
9777 return temp;
9779 case SAVE_EXPR:
9781 tree val = treeop0;
9782 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9783 inner_reference_p);
9785 if (!SAVE_EXPR_RESOLVED_P (exp))
9787 /* We can indeed still hit this case, typically via builtin
9788 expanders calling save_expr immediately before expanding
9789 something. Assume this means that we only have to deal
9790 with non-BLKmode values. */
9791 gcc_assert (GET_MODE (ret) != BLKmode);
9793 val = build_decl (curr_insn_location (),
9794 VAR_DECL, NULL, TREE_TYPE (exp));
9795 DECL_ARTIFICIAL (val) = 1;
9796 DECL_IGNORED_P (val) = 1;
9797 treeop0 = val;
9798 TREE_OPERAND (exp, 0) = treeop0;
9799 SAVE_EXPR_RESOLVED_P (exp) = 1;
9801 if (!CONSTANT_P (ret))
9802 ret = copy_to_reg (ret);
9803 SET_DECL_RTL (val, ret);
9806 return ret;
9810 case CONSTRUCTOR:
9811 /* If we don't need the result, just ensure we evaluate any
9812 subexpressions. */
9813 if (ignore)
9815 unsigned HOST_WIDE_INT idx;
9816 tree value;
9818 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9819 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9821 return const0_rtx;
9824 return expand_constructor (exp, target, modifier, false);
9826 case TARGET_MEM_REF:
9828 addr_space_t as
9829 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9830 enum insn_code icode;
9831 unsigned int align;
9833 op0 = addr_for_mem_ref (exp, as, true);
9834 op0 = memory_address_addr_space (mode, op0, as);
9835 temp = gen_rtx_MEM (mode, op0);
9836 set_mem_attributes (temp, exp, 0);
9837 set_mem_addr_space (temp, as);
9838 align = get_object_alignment (exp);
9839 if (modifier != EXPAND_WRITE
9840 && modifier != EXPAND_MEMORY
9841 && mode != BLKmode
9842 && align < GET_MODE_ALIGNMENT (mode)
9843 /* If the target does not have special handling for unaligned
9844 loads of mode then it can use regular moves for them. */
9845 && ((icode = optab_handler (movmisalign_optab, mode))
9846 != CODE_FOR_nothing))
9848 struct expand_operand ops[2];
9850 /* We've already validated the memory, and we're creating a
9851 new pseudo destination. The predicates really can't fail,
9852 nor can the generator. */
9853 create_output_operand (&ops[0], NULL_RTX, mode);
9854 create_fixed_operand (&ops[1], temp);
9855 expand_insn (icode, 2, ops);
9856 temp = ops[0].value;
9858 return temp;
9861 case MEM_REF:
9863 addr_space_t as
9864 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9865 machine_mode address_mode;
9866 tree base = TREE_OPERAND (exp, 0);
9867 gimple *def_stmt;
9868 enum insn_code icode;
9869 unsigned align;
9870 /* Handle expansion of non-aliased memory with non-BLKmode. That
9871 might end up in a register. */
9872 if (mem_ref_refers_to_non_mem_p (exp))
9874 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9875 base = TREE_OPERAND (base, 0);
9876 if (offset == 0
9877 && tree_fits_uhwi_p (TYPE_SIZE (type))
9878 && (GET_MODE_BITSIZE (DECL_MODE (base))
9879 == tree_to_uhwi (TYPE_SIZE (type))))
9880 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9881 target, tmode, modifier);
9882 if (TYPE_MODE (type) == BLKmode)
9884 temp = assign_stack_temp (DECL_MODE (base),
9885 GET_MODE_SIZE (DECL_MODE (base)));
9886 store_expr (base, temp, 0, false);
9887 temp = adjust_address (temp, BLKmode, offset);
9888 set_mem_size (temp, int_size_in_bytes (type));
9889 return temp;
9891 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9892 bitsize_int (offset * BITS_PER_UNIT));
9893 return expand_expr (exp, target, tmode, modifier);
9895 address_mode = targetm.addr_space.address_mode (as);
9896 base = TREE_OPERAND (exp, 0);
9897 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9899 tree mask = gimple_assign_rhs2 (def_stmt);
9900 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9901 gimple_assign_rhs1 (def_stmt), mask);
9902 TREE_OPERAND (exp, 0) = base;
9904 align = get_object_alignment (exp);
9905 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9906 op0 = memory_address_addr_space (mode, op0, as);
9907 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9909 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9910 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9911 op0 = memory_address_addr_space (mode, op0, as);
9913 temp = gen_rtx_MEM (mode, op0);
9914 set_mem_attributes (temp, exp, 0);
9915 set_mem_addr_space (temp, as);
9916 if (TREE_THIS_VOLATILE (exp))
9917 MEM_VOLATILE_P (temp) = 1;
9918 if (modifier != EXPAND_WRITE
9919 && modifier != EXPAND_MEMORY
9920 && !inner_reference_p
9921 && mode != BLKmode
9922 && align < GET_MODE_ALIGNMENT (mode))
9924 if ((icode = optab_handler (movmisalign_optab, mode))
9925 != CODE_FOR_nothing)
9927 struct expand_operand ops[2];
9929 /* We've already validated the memory, and we're creating a
9930 new pseudo destination. The predicates really can't fail,
9931 nor can the generator. */
9932 create_output_operand (&ops[0], NULL_RTX, mode);
9933 create_fixed_operand (&ops[1], temp);
9934 expand_insn (icode, 2, ops);
9935 temp = ops[0].value;
9937 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9938 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9939 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9940 (modifier == EXPAND_STACK_PARM
9941 ? NULL_RTX : target),
9942 mode, mode);
9944 return temp;
9947 case ARRAY_REF:
9950 tree array = treeop0;
9951 tree index = treeop1;
9952 tree init;
9954 /* Fold an expression like: "foo"[2].
9955 This is not done in fold so it won't happen inside &.
9956 Don't fold if this is for wide characters since it's too
9957 difficult to do correctly and this is a very rare case. */
9959 if (modifier != EXPAND_CONST_ADDRESS
9960 && modifier != EXPAND_INITIALIZER
9961 && modifier != EXPAND_MEMORY)
9963 tree t = fold_read_from_constant_string (exp);
9965 if (t)
9966 return expand_expr (t, target, tmode, modifier);
9969 /* If this is a constant index into a constant array,
9970 just get the value from the array. Handle both the cases when
9971 we have an explicit constructor and when our operand is a variable
9972 that was declared const. */
9974 if (modifier != EXPAND_CONST_ADDRESS
9975 && modifier != EXPAND_INITIALIZER
9976 && modifier != EXPAND_MEMORY
9977 && TREE_CODE (array) == CONSTRUCTOR
9978 && ! TREE_SIDE_EFFECTS (array)
9979 && TREE_CODE (index) == INTEGER_CST)
9981 unsigned HOST_WIDE_INT ix;
9982 tree field, value;
9984 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9985 field, value)
9986 if (tree_int_cst_equal (field, index))
9988 if (!TREE_SIDE_EFFECTS (value))
9989 return expand_expr (fold (value), target, tmode, modifier);
9990 break;
9994 else if (optimize >= 1
9995 && modifier != EXPAND_CONST_ADDRESS
9996 && modifier != EXPAND_INITIALIZER
9997 && modifier != EXPAND_MEMORY
9998 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9999 && TREE_CODE (index) == INTEGER_CST
10000 && (TREE_CODE (array) == VAR_DECL
10001 || TREE_CODE (array) == CONST_DECL)
10002 && (init = ctor_for_folding (array)) != error_mark_node)
10004 if (init == NULL_TREE)
10006 tree value = build_zero_cst (type);
10007 if (TREE_CODE (value) == CONSTRUCTOR)
10009 /* If VALUE is a CONSTRUCTOR, this optimization is only
10010 useful if this doesn't store the CONSTRUCTOR into
10011 memory. If it does, it is more efficient to just
10012 load the data from the array directly. */
10013 rtx ret = expand_constructor (value, target,
10014 modifier, true);
10015 if (ret == NULL_RTX)
10016 value = NULL_TREE;
10019 if (value)
10020 return expand_expr (value, target, tmode, modifier);
10022 else if (TREE_CODE (init) == CONSTRUCTOR)
10024 unsigned HOST_WIDE_INT ix;
10025 tree field, value;
10027 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10028 field, value)
10029 if (tree_int_cst_equal (field, index))
10031 if (TREE_SIDE_EFFECTS (value))
10032 break;
10034 if (TREE_CODE (value) == CONSTRUCTOR)
10036 /* If VALUE is a CONSTRUCTOR, this
10037 optimization is only useful if
10038 this doesn't store the CONSTRUCTOR
10039 into memory. If it does, it is more
10040 efficient to just load the data from
10041 the array directly. */
10042 rtx ret = expand_constructor (value, target,
10043 modifier, true);
10044 if (ret == NULL_RTX)
10045 break;
10048 return
10049 expand_expr (fold (value), target, tmode, modifier);
10052 else if (TREE_CODE (init) == STRING_CST)
10054 tree low_bound = array_ref_low_bound (exp);
10055 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10057 /* Optimize the special case of a zero lower bound.
10059 We convert the lower bound to sizetype to avoid problems
10060 with constant folding. E.g. suppose the lower bound is
10061 1 and its mode is QI. Without the conversion
10062 (ARRAY + (INDEX - (unsigned char)1))
10063 becomes
10064 (ARRAY + (-(unsigned char)1) + INDEX)
10065 which becomes
10066 (ARRAY + 255 + INDEX). Oops! */
10067 if (!integer_zerop (low_bound))
10068 index1 = size_diffop_loc (loc, index1,
10069 fold_convert_loc (loc, sizetype,
10070 low_bound));
10072 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10074 tree type = TREE_TYPE (TREE_TYPE (init));
10075 machine_mode mode = TYPE_MODE (type);
10077 if (GET_MODE_CLASS (mode) == MODE_INT
10078 && GET_MODE_SIZE (mode) == 1)
10079 return gen_int_mode (TREE_STRING_POINTER (init)
10080 [TREE_INT_CST_LOW (index1)],
10081 mode);
10086 goto normal_inner_ref;
10088 case COMPONENT_REF:
10089 /* If the operand is a CONSTRUCTOR, we can just extract the
10090 appropriate field if it is present. */
10091 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10093 unsigned HOST_WIDE_INT idx;
10094 tree field, value;
10096 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10097 idx, field, value)
10098 if (field == treeop1
10099 /* We can normally use the value of the field in the
10100 CONSTRUCTOR. However, if this is a bitfield in
10101 an integral mode that we can fit in a HOST_WIDE_INT,
10102 we must mask only the number of bits in the bitfield,
10103 since this is done implicitly by the constructor. If
10104 the bitfield does not meet either of those conditions,
10105 we can't do this optimization. */
10106 && (! DECL_BIT_FIELD (field)
10107 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10108 && (GET_MODE_PRECISION (DECL_MODE (field))
10109 <= HOST_BITS_PER_WIDE_INT))))
10111 if (DECL_BIT_FIELD (field)
10112 && modifier == EXPAND_STACK_PARM)
10113 target = 0;
10114 op0 = expand_expr (value, target, tmode, modifier);
10115 if (DECL_BIT_FIELD (field))
10117 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10118 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10120 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10122 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10123 imode);
10124 op0 = expand_and (imode, op0, op1, target);
10126 else
10128 int count = GET_MODE_PRECISION (imode) - bitsize;
10130 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10131 target, 0);
10132 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10133 target, 0);
10137 return op0;
10140 goto normal_inner_ref;
10142 case BIT_FIELD_REF:
10143 case ARRAY_RANGE_REF:
10144 normal_inner_ref:
10146 machine_mode mode1, mode2;
10147 HOST_WIDE_INT bitsize, bitpos;
10148 tree offset;
10149 int volatilep = 0, must_force_mem;
10150 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10151 &mode1, &unsignedp, &volatilep, true);
10152 rtx orig_op0, memloc;
10153 bool clear_mem_expr = false;
10155 /* If we got back the original object, something is wrong. Perhaps
10156 we are evaluating an expression too early. In any event, don't
10157 infinitely recurse. */
10158 gcc_assert (tem != exp);
10160 /* If TEM's type is a union of variable size, pass TARGET to the inner
10161 computation, since it will need a temporary and TARGET is known
10162 to have to do. This occurs in unchecked conversion in Ada. */
10163 orig_op0 = op0
10164 = expand_expr_real (tem,
10165 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10166 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10167 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10168 != INTEGER_CST)
10169 && modifier != EXPAND_STACK_PARM
10170 ? target : NULL_RTX),
10171 VOIDmode,
10172 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10173 NULL, true);
10175 /* If the field has a mode, we want to access it in the
10176 field's mode, not the computed mode.
10177 If a MEM has VOIDmode (external with incomplete type),
10178 use BLKmode for it instead. */
10179 if (MEM_P (op0))
10181 if (mode1 != VOIDmode)
10182 op0 = adjust_address (op0, mode1, 0);
10183 else if (GET_MODE (op0) == VOIDmode)
10184 op0 = adjust_address (op0, BLKmode, 0);
10187 mode2
10188 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10190 /* If we have either an offset, a BLKmode result, or a reference
10191 outside the underlying object, we must force it to memory.
10192 Such a case can occur in Ada if we have unchecked conversion
10193 of an expression from a scalar type to an aggregate type or
10194 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10195 passed a partially uninitialized object or a view-conversion
10196 to a larger size. */
10197 must_force_mem = (offset
10198 || mode1 == BLKmode
10199 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10201 /* Handle CONCAT first. */
10202 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10204 if (bitpos == 0
10205 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10206 return op0;
10207 if (bitpos == 0
10208 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10209 && bitsize)
10211 op0 = XEXP (op0, 0);
10212 mode2 = GET_MODE (op0);
10214 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10215 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10216 && bitpos
10217 && bitsize)
10219 op0 = XEXP (op0, 1);
10220 bitpos = 0;
10221 mode2 = GET_MODE (op0);
10223 else
10224 /* Otherwise force into memory. */
10225 must_force_mem = 1;
10228 /* If this is a constant, put it in a register if it is a legitimate
10229 constant and we don't need a memory reference. */
10230 if (CONSTANT_P (op0)
10231 && mode2 != BLKmode
10232 && targetm.legitimate_constant_p (mode2, op0)
10233 && !must_force_mem)
10234 op0 = force_reg (mode2, op0);
10236 /* Otherwise, if this is a constant, try to force it to the constant
10237 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10238 is a legitimate constant. */
10239 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10240 op0 = validize_mem (memloc);
10242 /* Otherwise, if this is a constant or the object is not in memory
10243 and need be, put it there. */
10244 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10246 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10247 emit_move_insn (memloc, op0);
10248 op0 = memloc;
10249 clear_mem_expr = true;
10252 if (offset)
10254 machine_mode address_mode;
10255 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10256 EXPAND_SUM);
10258 gcc_assert (MEM_P (op0));
10260 address_mode = get_address_mode (op0);
10261 if (GET_MODE (offset_rtx) != address_mode)
10263 /* We cannot be sure that the RTL in offset_rtx is valid outside
10264 of a memory address context, so force it into a register
10265 before attempting to convert it to the desired mode. */
10266 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10267 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10270 /* See the comment in expand_assignment for the rationale. */
10271 if (mode1 != VOIDmode
10272 && bitpos != 0
10273 && bitsize > 0
10274 && (bitpos % bitsize) == 0
10275 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10276 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10278 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10279 bitpos = 0;
10282 op0 = offset_address (op0, offset_rtx,
10283 highest_pow2_factor (offset));
10286 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10287 record its alignment as BIGGEST_ALIGNMENT. */
10288 if (MEM_P (op0) && bitpos == 0 && offset != 0
10289 && is_aligning_offset (offset, tem))
10290 set_mem_align (op0, BIGGEST_ALIGNMENT);
10292 /* Don't forget about volatility even if this is a bitfield. */
10293 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10295 if (op0 == orig_op0)
10296 op0 = copy_rtx (op0);
10298 MEM_VOLATILE_P (op0) = 1;
10301 /* In cases where an aligned union has an unaligned object
10302 as a field, we might be extracting a BLKmode value from
10303 an integer-mode (e.g., SImode) object. Handle this case
10304 by doing the extract into an object as wide as the field
10305 (which we know to be the width of a basic mode), then
10306 storing into memory, and changing the mode to BLKmode. */
10307 if (mode1 == VOIDmode
10308 || REG_P (op0) || GET_CODE (op0) == SUBREG
10309 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10310 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10311 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10312 && modifier != EXPAND_CONST_ADDRESS
10313 && modifier != EXPAND_INITIALIZER
10314 && modifier != EXPAND_MEMORY)
10315 /* If the bitfield is volatile and the bitsize
10316 is narrower than the access size of the bitfield,
10317 we need to extract bitfields from the access. */
10318 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10319 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10320 && mode1 != BLKmode
10321 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10322 /* If the field isn't aligned enough to fetch as a memref,
10323 fetch it as a bit field. */
10324 || (mode1 != BLKmode
10325 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10326 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10327 || (MEM_P (op0)
10328 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10329 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10330 && modifier != EXPAND_MEMORY
10331 && ((modifier == EXPAND_CONST_ADDRESS
10332 || modifier == EXPAND_INITIALIZER)
10333 ? STRICT_ALIGNMENT
10334 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10335 || (bitpos % BITS_PER_UNIT != 0)))
10336 /* If the type and the field are a constant size and the
10337 size of the type isn't the same size as the bitfield,
10338 we must use bitfield operations. */
10339 || (bitsize >= 0
10340 && TYPE_SIZE (TREE_TYPE (exp))
10341 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10342 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10343 bitsize)))
10345 machine_mode ext_mode = mode;
10347 if (ext_mode == BLKmode
10348 && ! (target != 0 && MEM_P (op0)
10349 && MEM_P (target)
10350 && bitpos % BITS_PER_UNIT == 0))
10351 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10353 if (ext_mode == BLKmode)
10355 if (target == 0)
10356 target = assign_temp (type, 1, 1);
10358 /* ??? Unlike the similar test a few lines below, this one is
10359 very likely obsolete. */
10360 if (bitsize == 0)
10361 return target;
10363 /* In this case, BITPOS must start at a byte boundary and
10364 TARGET, if specified, must be a MEM. */
10365 gcc_assert (MEM_P (op0)
10366 && (!target || MEM_P (target))
10367 && !(bitpos % BITS_PER_UNIT));
10369 emit_block_move (target,
10370 adjust_address (op0, VOIDmode,
10371 bitpos / BITS_PER_UNIT),
10372 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10373 / BITS_PER_UNIT),
10374 (modifier == EXPAND_STACK_PARM
10375 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10377 return target;
10380 /* If we have nothing to extract, the result will be 0 for targets
10381 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10382 return 0 for the sake of consistency, as reading a zero-sized
10383 bitfield is valid in Ada and the value is fully specified. */
10384 if (bitsize == 0)
10385 return const0_rtx;
10387 op0 = validize_mem (op0);
10389 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10390 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10392 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10393 (modifier == EXPAND_STACK_PARM
10394 ? NULL_RTX : target),
10395 ext_mode, ext_mode);
10397 /* If the result is a record type and BITSIZE is narrower than
10398 the mode of OP0, an integral mode, and this is a big endian
10399 machine, we must put the field into the high-order bits. */
10400 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10401 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10402 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10403 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10404 GET_MODE_BITSIZE (GET_MODE (op0))
10405 - bitsize, op0, 1);
10407 /* If the result type is BLKmode, store the data into a temporary
10408 of the appropriate type, but with the mode corresponding to the
10409 mode for the data we have (op0's mode). */
10410 if (mode == BLKmode)
10412 rtx new_rtx
10413 = assign_stack_temp_for_type (ext_mode,
10414 GET_MODE_BITSIZE (ext_mode),
10415 type);
10416 emit_move_insn (new_rtx, op0);
10417 op0 = copy_rtx (new_rtx);
10418 PUT_MODE (op0, BLKmode);
10421 return op0;
10424 /* If the result is BLKmode, use that to access the object
10425 now as well. */
10426 if (mode == BLKmode)
10427 mode1 = BLKmode;
10429 /* Get a reference to just this component. */
10430 if (modifier == EXPAND_CONST_ADDRESS
10431 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10432 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10433 else
10434 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10436 if (op0 == orig_op0)
10437 op0 = copy_rtx (op0);
10439 set_mem_attributes (op0, exp, 0);
10441 if (REG_P (XEXP (op0, 0)))
10442 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10444 /* If op0 is a temporary because the original expressions was forced
10445 to memory, clear MEM_EXPR so that the original expression cannot
10446 be marked as addressable through MEM_EXPR of the temporary. */
10447 if (clear_mem_expr)
10448 set_mem_expr (op0, NULL_TREE);
10450 MEM_VOLATILE_P (op0) |= volatilep;
10451 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10452 || modifier == EXPAND_CONST_ADDRESS
10453 || modifier == EXPAND_INITIALIZER)
10454 return op0;
10456 if (target == 0)
10457 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10459 convert_move (target, op0, unsignedp);
10460 return target;
10463 case OBJ_TYPE_REF:
10464 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10466 case CALL_EXPR:
10467 /* All valid uses of __builtin_va_arg_pack () are removed during
10468 inlining. */
10469 if (CALL_EXPR_VA_ARG_PACK (exp))
10470 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10472 tree fndecl = get_callee_fndecl (exp), attr;
10474 if (fndecl
10475 && (attr = lookup_attribute ("error",
10476 DECL_ATTRIBUTES (fndecl))) != NULL)
10477 error ("%Kcall to %qs declared with attribute error: %s",
10478 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10479 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10480 if (fndecl
10481 && (attr = lookup_attribute ("warning",
10482 DECL_ATTRIBUTES (fndecl))) != NULL)
10483 warning_at (tree_nonartificial_location (exp),
10484 0, "%Kcall to %qs declared with attribute warning: %s",
10485 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10486 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10488 /* Check for a built-in function. */
10489 if (fndecl && DECL_BUILT_IN (fndecl))
10491 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10492 if (CALL_WITH_BOUNDS_P (exp))
10493 return expand_builtin_with_bounds (exp, target, subtarget,
10494 tmode, ignore);
10495 else
10496 return expand_builtin (exp, target, subtarget, tmode, ignore);
10499 return expand_call (exp, target, ignore);
10501 case VIEW_CONVERT_EXPR:
10502 op0 = NULL_RTX;
10504 /* If we are converting to BLKmode, try to avoid an intermediate
10505 temporary by fetching an inner memory reference. */
10506 if (mode == BLKmode
10507 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10508 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10509 && handled_component_p (treeop0))
10511 machine_mode mode1;
10512 HOST_WIDE_INT bitsize, bitpos;
10513 tree offset;
10514 int unsignedp;
10515 int volatilep = 0;
10516 tree tem
10517 = get_inner_reference (treeop0, &bitsize, &bitpos,
10518 &offset, &mode1, &unsignedp, &volatilep,
10519 true);
10520 rtx orig_op0;
10522 /* ??? We should work harder and deal with non-zero offsets. */
10523 if (!offset
10524 && (bitpos % BITS_PER_UNIT) == 0
10525 && bitsize >= 0
10526 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10528 /* See the normal_inner_ref case for the rationale. */
10529 orig_op0
10530 = expand_expr_real (tem,
10531 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10532 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10533 != INTEGER_CST)
10534 && modifier != EXPAND_STACK_PARM
10535 ? target : NULL_RTX),
10536 VOIDmode,
10537 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10538 NULL, true);
10540 if (MEM_P (orig_op0))
10542 op0 = orig_op0;
10544 /* Get a reference to just this component. */
10545 if (modifier == EXPAND_CONST_ADDRESS
10546 || modifier == EXPAND_SUM
10547 || modifier == EXPAND_INITIALIZER)
10548 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10549 else
10550 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10552 if (op0 == orig_op0)
10553 op0 = copy_rtx (op0);
10555 set_mem_attributes (op0, treeop0, 0);
10556 if (REG_P (XEXP (op0, 0)))
10557 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10559 MEM_VOLATILE_P (op0) |= volatilep;
10564 if (!op0)
10565 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10566 NULL, inner_reference_p);
10568 /* If the input and output modes are both the same, we are done. */
10569 if (mode == GET_MODE (op0))
10571 /* If neither mode is BLKmode, and both modes are the same size
10572 then we can use gen_lowpart. */
10573 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10574 && (GET_MODE_PRECISION (mode)
10575 == GET_MODE_PRECISION (GET_MODE (op0)))
10576 && !COMPLEX_MODE_P (GET_MODE (op0)))
10578 if (GET_CODE (op0) == SUBREG)
10579 op0 = force_reg (GET_MODE (op0), op0);
10580 temp = gen_lowpart_common (mode, op0);
10581 if (temp)
10582 op0 = temp;
10583 else
10585 if (!REG_P (op0) && !MEM_P (op0))
10586 op0 = force_reg (GET_MODE (op0), op0);
10587 op0 = gen_lowpart (mode, op0);
10590 /* If both types are integral, convert from one mode to the other. */
10591 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10592 op0 = convert_modes (mode, GET_MODE (op0), op0,
10593 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10594 /* If the output type is a bit-field type, do an extraction. */
10595 else if (reduce_bit_field)
10596 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10597 TYPE_UNSIGNED (type), NULL_RTX,
10598 mode, mode);
10599 /* As a last resort, spill op0 to memory, and reload it in a
10600 different mode. */
10601 else if (!MEM_P (op0))
10603 /* If the operand is not a MEM, force it into memory. Since we
10604 are going to be changing the mode of the MEM, don't call
10605 force_const_mem for constants because we don't allow pool
10606 constants to change mode. */
10607 tree inner_type = TREE_TYPE (treeop0);
10609 gcc_assert (!TREE_ADDRESSABLE (exp));
10611 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10612 target
10613 = assign_stack_temp_for_type
10614 (TYPE_MODE (inner_type),
10615 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10617 emit_move_insn (target, op0);
10618 op0 = target;
10621 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10622 output type is such that the operand is known to be aligned, indicate
10623 that it is. Otherwise, we need only be concerned about alignment for
10624 non-BLKmode results. */
10625 if (MEM_P (op0))
10627 enum insn_code icode;
10629 if (TYPE_ALIGN_OK (type))
10631 /* ??? Copying the MEM without substantially changing it might
10632 run afoul of the code handling volatile memory references in
10633 store_expr, which assumes that TARGET is returned unmodified
10634 if it has been used. */
10635 op0 = copy_rtx (op0);
10636 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10638 else if (modifier != EXPAND_WRITE
10639 && modifier != EXPAND_MEMORY
10640 && !inner_reference_p
10641 && mode != BLKmode
10642 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10644 /* If the target does have special handling for unaligned
10645 loads of mode then use them. */
10646 if ((icode = optab_handler (movmisalign_optab, mode))
10647 != CODE_FOR_nothing)
10649 rtx reg;
10651 op0 = adjust_address (op0, mode, 0);
10652 /* We've already validated the memory, and we're creating a
10653 new pseudo destination. The predicates really can't
10654 fail. */
10655 reg = gen_reg_rtx (mode);
10657 /* Nor can the insn generator. */
10658 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10659 emit_insn (insn);
10660 return reg;
10662 else if (STRICT_ALIGNMENT)
10664 tree inner_type = TREE_TYPE (treeop0);
10665 HOST_WIDE_INT temp_size
10666 = MAX (int_size_in_bytes (inner_type),
10667 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10668 rtx new_rtx
10669 = assign_stack_temp_for_type (mode, temp_size, type);
10670 rtx new_with_op0_mode
10671 = adjust_address (new_rtx, GET_MODE (op0), 0);
10673 gcc_assert (!TREE_ADDRESSABLE (exp));
10675 if (GET_MODE (op0) == BLKmode)
10676 emit_block_move (new_with_op0_mode, op0,
10677 GEN_INT (GET_MODE_SIZE (mode)),
10678 (modifier == EXPAND_STACK_PARM
10679 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10680 else
10681 emit_move_insn (new_with_op0_mode, op0);
10683 op0 = new_rtx;
10687 op0 = adjust_address (op0, mode, 0);
10690 return op0;
10692 case MODIFY_EXPR:
10694 tree lhs = treeop0;
10695 tree rhs = treeop1;
10696 gcc_assert (ignore);
10698 /* Check for |= or &= of a bitfield of size one into another bitfield
10699 of size 1. In this case, (unless we need the result of the
10700 assignment) we can do this more efficiently with a
10701 test followed by an assignment, if necessary.
10703 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10704 things change so we do, this code should be enhanced to
10705 support it. */
10706 if (TREE_CODE (lhs) == COMPONENT_REF
10707 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10708 || TREE_CODE (rhs) == BIT_AND_EXPR)
10709 && TREE_OPERAND (rhs, 0) == lhs
10710 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10711 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10712 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10714 rtx_code_label *label = gen_label_rtx ();
10715 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10716 do_jump (TREE_OPERAND (rhs, 1),
10717 value ? label : 0,
10718 value ? 0 : label, -1);
10719 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10720 false);
10721 do_pending_stack_adjust ();
10722 emit_label (label);
10723 return const0_rtx;
10726 expand_assignment (lhs, rhs, false);
10727 return const0_rtx;
10730 case ADDR_EXPR:
10731 return expand_expr_addr_expr (exp, target, tmode, modifier);
10733 case REALPART_EXPR:
10734 op0 = expand_normal (treeop0);
10735 return read_complex_part (op0, false);
10737 case IMAGPART_EXPR:
10738 op0 = expand_normal (treeop0);
10739 return read_complex_part (op0, true);
10741 case RETURN_EXPR:
10742 case LABEL_EXPR:
10743 case GOTO_EXPR:
10744 case SWITCH_EXPR:
10745 case ASM_EXPR:
10746 /* Expanded in cfgexpand.c. */
10747 gcc_unreachable ();
10749 case TRY_CATCH_EXPR:
10750 case CATCH_EXPR:
10751 case EH_FILTER_EXPR:
10752 case TRY_FINALLY_EXPR:
10753 /* Lowered by tree-eh.c. */
10754 gcc_unreachable ();
10756 case WITH_CLEANUP_EXPR:
10757 case CLEANUP_POINT_EXPR:
10758 case TARGET_EXPR:
10759 case CASE_LABEL_EXPR:
10760 case VA_ARG_EXPR:
10761 case BIND_EXPR:
10762 case INIT_EXPR:
10763 case CONJ_EXPR:
10764 case COMPOUND_EXPR:
10765 case PREINCREMENT_EXPR:
10766 case PREDECREMENT_EXPR:
10767 case POSTINCREMENT_EXPR:
10768 case POSTDECREMENT_EXPR:
10769 case LOOP_EXPR:
10770 case EXIT_EXPR:
10771 case COMPOUND_LITERAL_EXPR:
10772 /* Lowered by gimplify.c. */
10773 gcc_unreachable ();
10775 case FDESC_EXPR:
10776 /* Function descriptors are not valid except for as
10777 initialization constants, and should not be expanded. */
10778 gcc_unreachable ();
10780 case WITH_SIZE_EXPR:
10781 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10782 have pulled out the size to use in whatever context it needed. */
10783 return expand_expr_real (treeop0, original_target, tmode,
10784 modifier, alt_rtl, inner_reference_p);
10786 default:
10787 return expand_expr_real_2 (&ops, target, tmode, modifier);
10791 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10792 signedness of TYPE), possibly returning the result in TARGET. */
10793 static rtx
10794 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10796 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10797 if (target && GET_MODE (target) != GET_MODE (exp))
10798 target = 0;
10799 /* For constant values, reduce using build_int_cst_type. */
10800 if (CONST_INT_P (exp))
10802 HOST_WIDE_INT value = INTVAL (exp);
10803 tree t = build_int_cst_type (type, value);
10804 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10806 else if (TYPE_UNSIGNED (type))
10808 machine_mode mode = GET_MODE (exp);
10809 rtx mask = immed_wide_int_const
10810 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10811 return expand_and (mode, exp, mask, target);
10813 else
10815 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10816 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10817 exp, count, target, 0);
10818 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10819 exp, count, target, 0);
10823 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10824 when applied to the address of EXP produces an address known to be
10825 aligned more than BIGGEST_ALIGNMENT. */
10827 static int
10828 is_aligning_offset (const_tree offset, const_tree exp)
10830 /* Strip off any conversions. */
10831 while (CONVERT_EXPR_P (offset))
10832 offset = TREE_OPERAND (offset, 0);
10834 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10835 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10836 if (TREE_CODE (offset) != BIT_AND_EXPR
10837 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10838 || compare_tree_int (TREE_OPERAND (offset, 1),
10839 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10840 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10841 return 0;
10843 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10844 It must be NEGATE_EXPR. Then strip any more conversions. */
10845 offset = TREE_OPERAND (offset, 0);
10846 while (CONVERT_EXPR_P (offset))
10847 offset = TREE_OPERAND (offset, 0);
10849 if (TREE_CODE (offset) != NEGATE_EXPR)
10850 return 0;
10852 offset = TREE_OPERAND (offset, 0);
10853 while (CONVERT_EXPR_P (offset))
10854 offset = TREE_OPERAND (offset, 0);
10856 /* This must now be the address of EXP. */
10857 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10860 /* Return the tree node if an ARG corresponds to a string constant or zero
10861 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10862 in bytes within the string that ARG is accessing. The type of the
10863 offset will be `sizetype'. */
10865 tree
10866 string_constant (tree arg, tree *ptr_offset)
10868 tree array, offset, lower_bound;
10869 STRIP_NOPS (arg);
10871 if (TREE_CODE (arg) == ADDR_EXPR)
10873 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10875 *ptr_offset = size_zero_node;
10876 return TREE_OPERAND (arg, 0);
10878 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10880 array = TREE_OPERAND (arg, 0);
10881 offset = size_zero_node;
10883 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10885 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10886 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10887 if (TREE_CODE (array) != STRING_CST
10888 && TREE_CODE (array) != VAR_DECL)
10889 return 0;
10891 /* Check if the array has a nonzero lower bound. */
10892 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10893 if (!integer_zerop (lower_bound))
10895 /* If the offset and base aren't both constants, return 0. */
10896 if (TREE_CODE (lower_bound) != INTEGER_CST)
10897 return 0;
10898 if (TREE_CODE (offset) != INTEGER_CST)
10899 return 0;
10900 /* Adjust offset by the lower bound. */
10901 offset = size_diffop (fold_convert (sizetype, offset),
10902 fold_convert (sizetype, lower_bound));
10905 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10907 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10908 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10909 if (TREE_CODE (array) != ADDR_EXPR)
10910 return 0;
10911 array = TREE_OPERAND (array, 0);
10912 if (TREE_CODE (array) != STRING_CST
10913 && TREE_CODE (array) != VAR_DECL)
10914 return 0;
10916 else
10917 return 0;
10919 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10921 tree arg0 = TREE_OPERAND (arg, 0);
10922 tree arg1 = TREE_OPERAND (arg, 1);
10924 STRIP_NOPS (arg0);
10925 STRIP_NOPS (arg1);
10927 if (TREE_CODE (arg0) == ADDR_EXPR
10928 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10929 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10931 array = TREE_OPERAND (arg0, 0);
10932 offset = arg1;
10934 else if (TREE_CODE (arg1) == ADDR_EXPR
10935 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10936 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10938 array = TREE_OPERAND (arg1, 0);
10939 offset = arg0;
10941 else
10942 return 0;
10944 else
10945 return 0;
10947 if (TREE_CODE (array) == STRING_CST)
10949 *ptr_offset = fold_convert (sizetype, offset);
10950 return array;
10952 else if (TREE_CODE (array) == VAR_DECL
10953 || TREE_CODE (array) == CONST_DECL)
10955 int length;
10956 tree init = ctor_for_folding (array);
10958 /* Variables initialized to string literals can be handled too. */
10959 if (init == error_mark_node
10960 || !init
10961 || TREE_CODE (init) != STRING_CST)
10962 return 0;
10964 /* Avoid const char foo[4] = "abcde"; */
10965 if (DECL_SIZE_UNIT (array) == NULL_TREE
10966 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10967 || (length = TREE_STRING_LENGTH (init)) <= 0
10968 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10969 return 0;
10971 /* If variable is bigger than the string literal, OFFSET must be constant
10972 and inside of the bounds of the string literal. */
10973 offset = fold_convert (sizetype, offset);
10974 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10975 && (! tree_fits_uhwi_p (offset)
10976 || compare_tree_int (offset, length) >= 0))
10977 return 0;
10979 *ptr_offset = offset;
10980 return init;
10983 return 0;
10986 /* Generate code to calculate OPS, and exploded expression
10987 using a store-flag instruction and return an rtx for the result.
10988 OPS reflects a comparison.
10990 If TARGET is nonzero, store the result there if convenient.
10992 Return zero if there is no suitable set-flag instruction
10993 available on this machine.
10995 Once expand_expr has been called on the arguments of the comparison,
10996 we are committed to doing the store flag, since it is not safe to
10997 re-evaluate the expression. We emit the store-flag insn by calling
10998 emit_store_flag, but only expand the arguments if we have a reason
10999 to believe that emit_store_flag will be successful. If we think that
11000 it will, but it isn't, we have to simulate the store-flag with a
11001 set/jump/set sequence. */
11003 static rtx
11004 do_store_flag (sepops ops, rtx target, machine_mode mode)
11006 enum rtx_code code;
11007 tree arg0, arg1, type;
11008 machine_mode operand_mode;
11009 int unsignedp;
11010 rtx op0, op1;
11011 rtx subtarget = target;
11012 location_t loc = ops->location;
11014 arg0 = ops->op0;
11015 arg1 = ops->op1;
11017 /* Don't crash if the comparison was erroneous. */
11018 if (arg0 == error_mark_node || arg1 == error_mark_node)
11019 return const0_rtx;
11021 type = TREE_TYPE (arg0);
11022 operand_mode = TYPE_MODE (type);
11023 unsignedp = TYPE_UNSIGNED (type);
11025 /* We won't bother with BLKmode store-flag operations because it would mean
11026 passing a lot of information to emit_store_flag. */
11027 if (operand_mode == BLKmode)
11028 return 0;
11030 /* We won't bother with store-flag operations involving function pointers
11031 when function pointers must be canonicalized before comparisons. */
11032 if (targetm.have_canonicalize_funcptr_for_compare ()
11033 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11034 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11035 == FUNCTION_TYPE))
11036 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11037 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11038 == FUNCTION_TYPE))))
11039 return 0;
11041 STRIP_NOPS (arg0);
11042 STRIP_NOPS (arg1);
11044 /* For vector typed comparisons emit code to generate the desired
11045 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11046 expander for this. */
11047 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11049 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11050 tree if_true = constant_boolean_node (true, ops->type);
11051 tree if_false = constant_boolean_node (false, ops->type);
11052 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
11055 /* Get the rtx comparison code to use. We know that EXP is a comparison
11056 operation of some type. Some comparisons against 1 and -1 can be
11057 converted to comparisons with zero. Do so here so that the tests
11058 below will be aware that we have a comparison with zero. These
11059 tests will not catch constants in the first operand, but constants
11060 are rarely passed as the first operand. */
11062 switch (ops->code)
11064 case EQ_EXPR:
11065 code = EQ;
11066 break;
11067 case NE_EXPR:
11068 code = NE;
11069 break;
11070 case LT_EXPR:
11071 if (integer_onep (arg1))
11072 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11073 else
11074 code = unsignedp ? LTU : LT;
11075 break;
11076 case LE_EXPR:
11077 if (! unsignedp && integer_all_onesp (arg1))
11078 arg1 = integer_zero_node, code = LT;
11079 else
11080 code = unsignedp ? LEU : LE;
11081 break;
11082 case GT_EXPR:
11083 if (! unsignedp && integer_all_onesp (arg1))
11084 arg1 = integer_zero_node, code = GE;
11085 else
11086 code = unsignedp ? GTU : GT;
11087 break;
11088 case GE_EXPR:
11089 if (integer_onep (arg1))
11090 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11091 else
11092 code = unsignedp ? GEU : GE;
11093 break;
11095 case UNORDERED_EXPR:
11096 code = UNORDERED;
11097 break;
11098 case ORDERED_EXPR:
11099 code = ORDERED;
11100 break;
11101 case UNLT_EXPR:
11102 code = UNLT;
11103 break;
11104 case UNLE_EXPR:
11105 code = UNLE;
11106 break;
11107 case UNGT_EXPR:
11108 code = UNGT;
11109 break;
11110 case UNGE_EXPR:
11111 code = UNGE;
11112 break;
11113 case UNEQ_EXPR:
11114 code = UNEQ;
11115 break;
11116 case LTGT_EXPR:
11117 code = LTGT;
11118 break;
11120 default:
11121 gcc_unreachable ();
11124 /* Put a constant second. */
11125 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11126 || TREE_CODE (arg0) == FIXED_CST)
11128 std::swap (arg0, arg1);
11129 code = swap_condition (code);
11132 /* If this is an equality or inequality test of a single bit, we can
11133 do this by shifting the bit being tested to the low-order bit and
11134 masking the result with the constant 1. If the condition was EQ,
11135 we xor it with 1. This does not require an scc insn and is faster
11136 than an scc insn even if we have it.
11138 The code to make this transformation was moved into fold_single_bit_test,
11139 so we just call into the folder and expand its result. */
11141 if ((code == NE || code == EQ)
11142 && integer_zerop (arg1)
11143 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11145 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11146 if (srcstmt
11147 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11149 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11150 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11151 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11152 gimple_assign_rhs1 (srcstmt),
11153 gimple_assign_rhs2 (srcstmt));
11154 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11155 if (temp)
11156 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11160 if (! get_subtarget (target)
11161 || GET_MODE (subtarget) != operand_mode)
11162 subtarget = 0;
11164 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11166 if (target == 0)
11167 target = gen_reg_rtx (mode);
11169 /* Try a cstore if possible. */
11170 return emit_store_flag_force (target, code, op0, op1,
11171 operand_mode, unsignedp,
11172 (TYPE_PRECISION (ops->type) == 1
11173 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11176 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11177 0 otherwise (i.e. if there is no casesi instruction).
11179 DEFAULT_PROBABILITY is the probability of jumping to the default
11180 label. */
11182 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11183 rtx table_label, rtx default_label, rtx fallback_label,
11184 int default_probability)
11186 struct expand_operand ops[5];
11187 machine_mode index_mode = SImode;
11188 rtx op1, op2, index;
11190 if (! targetm.have_casesi ())
11191 return 0;
11193 /* Convert the index to SImode. */
11194 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11196 machine_mode omode = TYPE_MODE (index_type);
11197 rtx rangertx = expand_normal (range);
11199 /* We must handle the endpoints in the original mode. */
11200 index_expr = build2 (MINUS_EXPR, index_type,
11201 index_expr, minval);
11202 minval = integer_zero_node;
11203 index = expand_normal (index_expr);
11204 if (default_label)
11205 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11206 omode, 1, default_label,
11207 default_probability);
11208 /* Now we can safely truncate. */
11209 index = convert_to_mode (index_mode, index, 0);
11211 else
11213 if (TYPE_MODE (index_type) != index_mode)
11215 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11216 index_expr = fold_convert (index_type, index_expr);
11219 index = expand_normal (index_expr);
11222 do_pending_stack_adjust ();
11224 op1 = expand_normal (minval);
11225 op2 = expand_normal (range);
11227 create_input_operand (&ops[0], index, index_mode);
11228 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11229 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11230 create_fixed_operand (&ops[3], table_label);
11231 create_fixed_operand (&ops[4], (default_label
11232 ? default_label
11233 : fallback_label));
11234 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11235 return 1;
11238 /* Attempt to generate a tablejump instruction; same concept. */
11239 /* Subroutine of the next function.
11241 INDEX is the value being switched on, with the lowest value
11242 in the table already subtracted.
11243 MODE is its expected mode (needed if INDEX is constant).
11244 RANGE is the length of the jump table.
11245 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11247 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11248 index value is out of range.
11249 DEFAULT_PROBABILITY is the probability of jumping to
11250 the default label. */
11252 static void
11253 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11254 rtx default_label, int default_probability)
11256 rtx temp, vector;
11258 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11259 cfun->cfg->max_jumptable_ents = INTVAL (range);
11261 /* Do an unsigned comparison (in the proper mode) between the index
11262 expression and the value which represents the length of the range.
11263 Since we just finished subtracting the lower bound of the range
11264 from the index expression, this comparison allows us to simultaneously
11265 check that the original index expression value is both greater than
11266 or equal to the minimum value of the range and less than or equal to
11267 the maximum value of the range. */
11269 if (default_label)
11270 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11271 default_label, default_probability);
11274 /* If index is in range, it must fit in Pmode.
11275 Convert to Pmode so we can index with it. */
11276 if (mode != Pmode)
11277 index = convert_to_mode (Pmode, index, 1);
11279 /* Don't let a MEM slip through, because then INDEX that comes
11280 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11281 and break_out_memory_refs will go to work on it and mess it up. */
11282 #ifdef PIC_CASE_VECTOR_ADDRESS
11283 if (flag_pic && !REG_P (index))
11284 index = copy_to_mode_reg (Pmode, index);
11285 #endif
11287 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11288 GET_MODE_SIZE, because this indicates how large insns are. The other
11289 uses should all be Pmode, because they are addresses. This code
11290 could fail if addresses and insns are not the same size. */
11291 index = simplify_gen_binary (MULT, Pmode, index,
11292 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11293 Pmode));
11294 index = simplify_gen_binary (PLUS, Pmode, index,
11295 gen_rtx_LABEL_REF (Pmode, table_label));
11297 #ifdef PIC_CASE_VECTOR_ADDRESS
11298 if (flag_pic)
11299 index = PIC_CASE_VECTOR_ADDRESS (index);
11300 else
11301 #endif
11302 index = memory_address (CASE_VECTOR_MODE, index);
11303 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11304 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11305 convert_move (temp, vector, 0);
11307 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11309 /* If we are generating PIC code or if the table is PC-relative, the
11310 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11311 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11312 emit_barrier ();
11316 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11317 rtx table_label, rtx default_label, int default_probability)
11319 rtx index;
11321 if (! targetm.have_tablejump ())
11322 return 0;
11324 index_expr = fold_build2 (MINUS_EXPR, index_type,
11325 fold_convert (index_type, index_expr),
11326 fold_convert (index_type, minval));
11327 index = expand_normal (index_expr);
11328 do_pending_stack_adjust ();
11330 do_tablejump (index, TYPE_MODE (index_type),
11331 convert_modes (TYPE_MODE (index_type),
11332 TYPE_MODE (TREE_TYPE (range)),
11333 expand_normal (range),
11334 TYPE_UNSIGNED (TREE_TYPE (range))),
11335 table_label, default_label, default_probability);
11336 return 1;
11339 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11340 static rtx
11341 const_vector_from_tree (tree exp)
11343 rtvec v;
11344 unsigned i;
11345 int units;
11346 tree elt;
11347 machine_mode inner, mode;
11349 mode = TYPE_MODE (TREE_TYPE (exp));
11351 if (initializer_zerop (exp))
11352 return CONST0_RTX (mode);
11354 units = GET_MODE_NUNITS (mode);
11355 inner = GET_MODE_INNER (mode);
11357 v = rtvec_alloc (units);
11359 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11361 elt = VECTOR_CST_ELT (exp, i);
11363 if (TREE_CODE (elt) == REAL_CST)
11364 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11365 inner);
11366 else if (TREE_CODE (elt) == FIXED_CST)
11367 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11368 inner);
11369 else
11370 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11373 return gen_rtx_CONST_VECTOR (mode, v);
11376 /* Build a decl for a personality function given a language prefix. */
11378 tree
11379 build_personality_function (const char *lang)
11381 const char *unwind_and_version;
11382 tree decl, type;
11383 char *name;
11385 switch (targetm_common.except_unwind_info (&global_options))
11387 case UI_NONE:
11388 return NULL;
11389 case UI_SJLJ:
11390 unwind_and_version = "_sj0";
11391 break;
11392 case UI_DWARF2:
11393 case UI_TARGET:
11394 unwind_and_version = "_v0";
11395 break;
11396 case UI_SEH:
11397 unwind_and_version = "_seh0";
11398 break;
11399 default:
11400 gcc_unreachable ();
11403 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11405 type = build_function_type_list (integer_type_node, integer_type_node,
11406 long_long_unsigned_type_node,
11407 ptr_type_node, ptr_type_node, NULL_TREE);
11408 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11409 get_identifier (name), type);
11410 DECL_ARTIFICIAL (decl) = 1;
11411 DECL_EXTERNAL (decl) = 1;
11412 TREE_PUBLIC (decl) = 1;
11414 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11415 are the flags assigned by targetm.encode_section_info. */
11416 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11418 return decl;
11421 /* Extracts the personality function of DECL and returns the corresponding
11422 libfunc. */
11425 get_personality_function (tree decl)
11427 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11428 enum eh_personality_kind pk;
11430 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11431 if (pk == eh_personality_none)
11432 return NULL;
11434 if (!personality
11435 && pk == eh_personality_any)
11436 personality = lang_hooks.eh_personality ();
11438 if (pk == eh_personality_lang)
11439 gcc_assert (personality != NULL_TREE);
11441 return XEXP (DECL_RTL (personality), 0);
11444 /* Returns a tree for the size of EXP in bytes. */
11446 static tree
11447 tree_expr_size (const_tree exp)
11449 if (DECL_P (exp)
11450 && DECL_SIZE_UNIT (exp) != 0)
11451 return DECL_SIZE_UNIT (exp);
11452 else
11453 return size_in_bytes (TREE_TYPE (exp));
11456 /* Return an rtx for the size in bytes of the value of EXP. */
11459 expr_size (tree exp)
11461 tree size;
11463 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11464 size = TREE_OPERAND (exp, 1);
11465 else
11467 size = tree_expr_size (exp);
11468 gcc_assert (size);
11469 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11472 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11475 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11476 if the size can vary or is larger than an integer. */
11478 static HOST_WIDE_INT
11479 int_expr_size (tree exp)
11481 tree size;
11483 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11484 size = TREE_OPERAND (exp, 1);
11485 else
11487 size = tree_expr_size (exp);
11488 gcc_assert (size);
11491 if (size == 0 || !tree_fits_shwi_p (size))
11492 return -1;
11494 return tree_to_shwi (size);
11497 #include "gt-expr.h"