2014-11-25 Martin Liska <mliska@suse.cz>
[official-gcc.git] / gcc / expr.c
blobc7621b0e9c851dca270b8b055a5219006dd47aae
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2014 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 "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "attribs.h"
30 #include "varasm.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "hashtab.h"
36 #include "hash-set.h"
37 #include "vec.h"
38 #include "input.h"
39 #include "function.h"
40 #include "insn-config.h"
41 #include "insn-attr.h"
42 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
43 #include "expr.h"
44 #include "insn-codes.h"
45 #include "optabs.h"
46 #include "libfuncs.h"
47 #include "recog.h"
48 #include "reload.h"
49 #include "typeclass.h"
50 #include "toplev.h"
51 #include "langhooks.h"
52 #include "intl.h"
53 #include "tm_p.h"
54 #include "tree-iterator.h"
55 #include "predict.h"
56 #include "dominance.h"
57 #include "cfg.h"
58 #include "basic-block.h"
59 #include "tree-ssa-alias.h"
60 #include "internal-fn.h"
61 #include "gimple-expr.h"
62 #include "is-a.h"
63 #include "gimple.h"
64 #include "gimple-ssa.h"
65 #include "hash-map.h"
66 #include "plugin-api.h"
67 #include "ipa-ref.h"
68 #include "cgraph.h"
69 #include "tree-ssanames.h"
70 #include "target.h"
71 #include "common/common-target.h"
72 #include "timevar.h"
73 #include "df.h"
74 #include "diagnostic.h"
75 #include "tree-ssa-live.h"
76 #include "tree-outof-ssa.h"
77 #include "target-globals.h"
78 #include "params.h"
79 #include "tree-ssa-address.h"
80 #include "cfgexpand.h"
81 #include "builtins.h"
82 #include "tree-chkp.h"
83 #include "rtl-chkp.h"
84 #include "ccmp.h"
86 #ifndef STACK_PUSH_CODE
87 #ifdef STACK_GROWS_DOWNWARD
88 #define STACK_PUSH_CODE PRE_DEC
89 #else
90 #define STACK_PUSH_CODE PRE_INC
91 #endif
92 #endif
95 /* If this is nonzero, we do not bother generating VOLATILE
96 around volatile memory references, and we are willing to
97 output indirect addresses. If cse is to follow, we reject
98 indirect addresses so a useful potential cse is generated;
99 if it is used only once, instruction combination will produce
100 the same indirect address eventually. */
101 int cse_not_expected;
103 /* This structure is used by move_by_pieces to describe the move to
104 be performed. */
105 struct move_by_pieces_d
107 rtx to;
108 rtx to_addr;
109 int autinc_to;
110 int explicit_inc_to;
111 rtx from;
112 rtx from_addr;
113 int autinc_from;
114 int explicit_inc_from;
115 unsigned HOST_WIDE_INT len;
116 HOST_WIDE_INT offset;
117 int reverse;
120 /* This structure is used by store_by_pieces to describe the clear to
121 be performed. */
123 struct store_by_pieces_d
125 rtx to;
126 rtx to_addr;
127 int autinc_to;
128 int explicit_inc_to;
129 unsigned HOST_WIDE_INT len;
130 HOST_WIDE_INT offset;
131 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
132 void *constfundata;
133 int reverse;
136 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
137 struct move_by_pieces_d *);
138 static bool block_move_libcall_safe_for_call_parm (void);
139 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
140 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
141 unsigned HOST_WIDE_INT);
142 static tree emit_block_move_libcall_fn (int);
143 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
144 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
145 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
146 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
147 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
148 struct store_by_pieces_d *);
149 static tree clear_storage_libcall_fn (int);
150 static rtx_insn *compress_float_constant (rtx, rtx);
151 static rtx get_subtarget (rtx);
152 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
153 HOST_WIDE_INT, machine_mode,
154 tree, int, alias_set_type);
155 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
156 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
157 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
158 machine_mode, tree, alias_set_type, bool);
160 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
162 static int is_aligning_offset (const_tree, const_tree);
163 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
164 static rtx do_store_flag (sepops, rtx, machine_mode);
165 #ifdef PUSH_ROUNDING
166 static void emit_single_push_insn (machine_mode, rtx, tree);
167 #endif
168 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
169 static rtx const_vector_from_tree (tree);
172 /* This is run to set up which modes can be used
173 directly in memory and to initialize the block move optab. It is run
174 at the beginning of compilation and when the target is reinitialized. */
176 void
177 init_expr_target (void)
179 rtx insn, pat;
180 machine_mode mode;
181 int num_clobbers;
182 rtx mem, mem1;
183 rtx reg;
185 /* Try indexing by frame ptr and try by stack ptr.
186 It is known that on the Convex the stack ptr isn't a valid index.
187 With luck, one or the other is valid on any machine. */
188 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
189 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
191 /* A scratch register we can modify in-place below to avoid
192 useless RTL allocations. */
193 reg = gen_rtx_REG (VOIDmode, -1);
195 insn = rtx_alloc (INSN);
196 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
197 PATTERN (insn) = pat;
199 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
200 mode = (machine_mode) ((int) mode + 1))
202 int regno;
204 direct_load[(int) mode] = direct_store[(int) mode] = 0;
205 PUT_MODE (mem, mode);
206 PUT_MODE (mem1, mode);
207 PUT_MODE (reg, mode);
209 /* See if there is some register that can be used in this mode and
210 directly loaded or stored from memory. */
212 if (mode != VOIDmode && mode != BLKmode)
213 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
214 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
215 regno++)
217 if (! HARD_REGNO_MODE_OK (regno, mode))
218 continue;
220 SET_REGNO (reg, regno);
222 SET_SRC (pat) = mem;
223 SET_DEST (pat) = reg;
224 if (recog (pat, insn, &num_clobbers) >= 0)
225 direct_load[(int) mode] = 1;
227 SET_SRC (pat) = mem1;
228 SET_DEST (pat) = reg;
229 if (recog (pat, insn, &num_clobbers) >= 0)
230 direct_load[(int) mode] = 1;
232 SET_SRC (pat) = reg;
233 SET_DEST (pat) = mem;
234 if (recog (pat, insn, &num_clobbers) >= 0)
235 direct_store[(int) mode] = 1;
237 SET_SRC (pat) = reg;
238 SET_DEST (pat) = mem1;
239 if (recog (pat, insn, &num_clobbers) >= 0)
240 direct_store[(int) mode] = 1;
244 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
246 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
247 mode = GET_MODE_WIDER_MODE (mode))
249 machine_mode srcmode;
250 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
251 srcmode = GET_MODE_WIDER_MODE (srcmode))
253 enum insn_code ic;
255 ic = can_extend_p (mode, srcmode, 0);
256 if (ic == CODE_FOR_nothing)
257 continue;
259 PUT_MODE (mem, srcmode);
261 if (insn_operand_matches (ic, 1, mem))
262 float_extend_from_mem[mode][srcmode] = true;
267 /* This is run at the start of compiling a function. */
269 void
270 init_expr (void)
272 memset (&crtl->expr, 0, sizeof (crtl->expr));
275 /* Copy data from FROM to TO, where the machine modes are not the same.
276 Both modes may be integer, or both may be floating, or both may be
277 fixed-point.
278 UNSIGNEDP should be nonzero if FROM is an unsigned type.
279 This causes zero-extension instead of sign-extension. */
281 void
282 convert_move (rtx to, rtx from, int unsignedp)
284 machine_mode to_mode = GET_MODE (to);
285 machine_mode from_mode = GET_MODE (from);
286 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
287 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
288 enum insn_code code;
289 rtx libcall;
291 /* rtx code for making an equivalent value. */
292 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
293 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
296 gcc_assert (to_real == from_real);
297 gcc_assert (to_mode != BLKmode);
298 gcc_assert (from_mode != BLKmode);
300 /* If the source and destination are already the same, then there's
301 nothing to do. */
302 if (to == from)
303 return;
305 /* If FROM is a SUBREG that indicates that we have already done at least
306 the required extension, strip it. We don't handle such SUBREGs as
307 TO here. */
309 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
310 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
311 >= GET_MODE_PRECISION (to_mode))
312 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
313 from = gen_lowpart (to_mode, from), from_mode = to_mode;
315 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
317 if (to_mode == from_mode
318 || (from_mode == VOIDmode && CONSTANT_P (from)))
320 emit_move_insn (to, from);
321 return;
324 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
326 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
328 if (VECTOR_MODE_P (to_mode))
329 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
330 else
331 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
333 emit_move_insn (to, from);
334 return;
337 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
339 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
340 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
341 return;
344 if (to_real)
346 rtx value;
347 rtx_insn *insns;
348 convert_optab tab;
350 gcc_assert ((GET_MODE_PRECISION (from_mode)
351 != GET_MODE_PRECISION (to_mode))
352 || (DECIMAL_FLOAT_MODE_P (from_mode)
353 != DECIMAL_FLOAT_MODE_P (to_mode)));
355 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
356 /* Conversion between decimal float and binary float, same size. */
357 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
358 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
359 tab = sext_optab;
360 else
361 tab = trunc_optab;
363 /* Try converting directly if the insn is supported. */
365 code = convert_optab_handler (tab, to_mode, from_mode);
366 if (code != CODE_FOR_nothing)
368 emit_unop_insn (code, to, from,
369 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
370 return;
373 /* Otherwise use a libcall. */
374 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
376 /* Is this conversion implemented yet? */
377 gcc_assert (libcall);
379 start_sequence ();
380 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
381 1, from, from_mode);
382 insns = get_insns ();
383 end_sequence ();
384 emit_libcall_block (insns, to, value,
385 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
386 from)
387 : gen_rtx_FLOAT_EXTEND (to_mode, from));
388 return;
391 /* Handle pointer conversion. */ /* SPEE 900220. */
392 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
394 convert_optab ctab;
396 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
397 ctab = trunc_optab;
398 else if (unsignedp)
399 ctab = zext_optab;
400 else
401 ctab = sext_optab;
403 if (convert_optab_handler (ctab, to_mode, from_mode)
404 != CODE_FOR_nothing)
406 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
407 to, from, UNKNOWN);
408 return;
412 /* Targets are expected to provide conversion insns between PxImode and
413 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
414 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
416 machine_mode full_mode
417 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
419 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
420 != CODE_FOR_nothing);
422 if (full_mode != from_mode)
423 from = convert_to_mode (full_mode, from, unsignedp);
424 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
425 to, from, UNKNOWN);
426 return;
428 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
430 rtx new_from;
431 machine_mode full_mode
432 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
433 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
434 enum insn_code icode;
436 icode = convert_optab_handler (ctab, full_mode, from_mode);
437 gcc_assert (icode != CODE_FOR_nothing);
439 if (to_mode == full_mode)
441 emit_unop_insn (icode, to, from, UNKNOWN);
442 return;
445 new_from = gen_reg_rtx (full_mode);
446 emit_unop_insn (icode, new_from, from, UNKNOWN);
448 /* else proceed to integer conversions below. */
449 from_mode = full_mode;
450 from = new_from;
453 /* Make sure both are fixed-point modes or both are not. */
454 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
455 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
456 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
458 /* If we widen from_mode to to_mode and they are in the same class,
459 we won't saturate the result.
460 Otherwise, always saturate the result to play safe. */
461 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
462 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
463 expand_fixed_convert (to, from, 0, 0);
464 else
465 expand_fixed_convert (to, from, 0, 1);
466 return;
469 /* Now both modes are integers. */
471 /* Handle expanding beyond a word. */
472 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
473 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
475 rtx_insn *insns;
476 rtx lowpart;
477 rtx fill_value;
478 rtx lowfrom;
479 int i;
480 machine_mode lowpart_mode;
481 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
483 /* Try converting directly if the insn is supported. */
484 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
485 != CODE_FOR_nothing)
487 /* If FROM is a SUBREG, put it into a register. Do this
488 so that we always generate the same set of insns for
489 better cse'ing; if an intermediate assignment occurred,
490 we won't be doing the operation directly on the SUBREG. */
491 if (optimize > 0 && GET_CODE (from) == SUBREG)
492 from = force_reg (from_mode, from);
493 emit_unop_insn (code, to, from, equiv_code);
494 return;
496 /* Next, try converting via full word. */
497 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
498 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
499 != CODE_FOR_nothing))
501 rtx word_to = gen_reg_rtx (word_mode);
502 if (REG_P (to))
504 if (reg_overlap_mentioned_p (to, from))
505 from = force_reg (from_mode, from);
506 emit_clobber (to);
508 convert_move (word_to, from, unsignedp);
509 emit_unop_insn (code, to, word_to, equiv_code);
510 return;
513 /* No special multiword conversion insn; do it by hand. */
514 start_sequence ();
516 /* Since we will turn this into a no conflict block, we must ensure the
517 the source does not overlap the target so force it into an isolated
518 register when maybe so. Likewise for any MEM input, since the
519 conversion sequence might require several references to it and we
520 must ensure we're getting the same value every time. */
522 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
523 from = force_reg (from_mode, from);
525 /* Get a copy of FROM widened to a word, if necessary. */
526 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
527 lowpart_mode = word_mode;
528 else
529 lowpart_mode = from_mode;
531 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
533 lowpart = gen_lowpart (lowpart_mode, to);
534 emit_move_insn (lowpart, lowfrom);
536 /* Compute the value to put in each remaining word. */
537 if (unsignedp)
538 fill_value = const0_rtx;
539 else
540 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
541 LT, lowfrom, const0_rtx,
542 lowpart_mode, 0, -1);
544 /* Fill the remaining words. */
545 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
547 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
548 rtx subword = operand_subword (to, index, 1, to_mode);
550 gcc_assert (subword);
552 if (fill_value != subword)
553 emit_move_insn (subword, fill_value);
556 insns = get_insns ();
557 end_sequence ();
559 emit_insn (insns);
560 return;
563 /* Truncating multi-word to a word or less. */
564 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
565 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
567 if (!((MEM_P (from)
568 && ! MEM_VOLATILE_P (from)
569 && direct_load[(int) to_mode]
570 && ! mode_dependent_address_p (XEXP (from, 0),
571 MEM_ADDR_SPACE (from)))
572 || REG_P (from)
573 || GET_CODE (from) == SUBREG))
574 from = force_reg (from_mode, from);
575 convert_move (to, gen_lowpart (word_mode, from), 0);
576 return;
579 /* Now follow all the conversions between integers
580 no more than a word long. */
582 /* For truncation, usually we can just refer to FROM in a narrower mode. */
583 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
584 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
586 if (!((MEM_P (from)
587 && ! MEM_VOLATILE_P (from)
588 && direct_load[(int) to_mode]
589 && ! mode_dependent_address_p (XEXP (from, 0),
590 MEM_ADDR_SPACE (from)))
591 || REG_P (from)
592 || GET_CODE (from) == SUBREG))
593 from = force_reg (from_mode, from);
594 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
595 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
596 from = copy_to_reg (from);
597 emit_move_insn (to, gen_lowpart (to_mode, from));
598 return;
601 /* Handle extension. */
602 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
604 /* Convert directly if that works. */
605 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
606 != CODE_FOR_nothing)
608 emit_unop_insn (code, to, from, equiv_code);
609 return;
611 else
613 machine_mode intermediate;
614 rtx tmp;
615 int shift_amount;
617 /* Search for a mode to convert via. */
618 for (intermediate = from_mode; intermediate != VOIDmode;
619 intermediate = GET_MODE_WIDER_MODE (intermediate))
620 if (((can_extend_p (to_mode, intermediate, unsignedp)
621 != CODE_FOR_nothing)
622 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
623 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
624 && (can_extend_p (intermediate, from_mode, unsignedp)
625 != CODE_FOR_nothing))
627 convert_move (to, convert_to_mode (intermediate, from,
628 unsignedp), unsignedp);
629 return;
632 /* No suitable intermediate mode.
633 Generate what we need with shifts. */
634 shift_amount = (GET_MODE_PRECISION (to_mode)
635 - GET_MODE_PRECISION (from_mode));
636 from = gen_lowpart (to_mode, force_reg (from_mode, from));
637 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
638 to, unsignedp);
639 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
640 to, unsignedp);
641 if (tmp != to)
642 emit_move_insn (to, tmp);
643 return;
647 /* Support special truncate insns for certain modes. */
648 if (convert_optab_handler (trunc_optab, to_mode,
649 from_mode) != CODE_FOR_nothing)
651 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
652 to, from, UNKNOWN);
653 return;
656 /* Handle truncation of volatile memrefs, and so on;
657 the things that couldn't be truncated directly,
658 and for which there was no special instruction.
660 ??? Code above formerly short-circuited this, for most integer
661 mode pairs, with a force_reg in from_mode followed by a recursive
662 call to this routine. Appears always to have been wrong. */
663 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
665 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
666 emit_move_insn (to, temp);
667 return;
670 /* Mode combination is not recognized. */
671 gcc_unreachable ();
674 /* Return an rtx for a value that would result
675 from converting X to mode MODE.
676 Both X and MODE may be floating, or both integer.
677 UNSIGNEDP is nonzero if X is an unsigned value.
678 This can be done by referring to a part of X in place
679 or by copying to a new temporary with conversion. */
682 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
684 return convert_modes (mode, VOIDmode, x, unsignedp);
687 /* Return an rtx for a value that would result
688 from converting X from mode OLDMODE to mode MODE.
689 Both modes may be floating, or both integer.
690 UNSIGNEDP is nonzero if X is an unsigned value.
692 This can be done by referring to a part of X in place
693 or by copying to a new temporary with conversion.
695 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
698 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
700 rtx temp;
702 /* If FROM is a SUBREG that indicates that we have already done at least
703 the required extension, strip it. */
705 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
706 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
707 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
708 x = gen_lowpart (mode, SUBREG_REG (x));
710 if (GET_MODE (x) != VOIDmode)
711 oldmode = GET_MODE (x);
713 if (mode == oldmode)
714 return x;
716 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
718 /* If the caller did not tell us the old mode, then there is not
719 much to do with respect to canonicalization. We have to
720 assume that all the bits are significant. */
721 if (GET_MODE_CLASS (oldmode) != MODE_INT)
722 oldmode = MAX_MODE_INT;
723 wide_int w = wide_int::from (std::make_pair (x, oldmode),
724 GET_MODE_PRECISION (mode),
725 unsignedp ? UNSIGNED : SIGNED);
726 return immed_wide_int_const (w, mode);
729 /* We can do this with a gen_lowpart if both desired and current modes
730 are integer, and this is either a constant integer, a register, or a
731 non-volatile MEM. */
732 if (GET_MODE_CLASS (mode) == MODE_INT
733 && GET_MODE_CLASS (oldmode) == MODE_INT
734 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
735 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
736 || (REG_P (x)
737 && (!HARD_REGISTER_P (x)
738 || HARD_REGNO_MODE_OK (REGNO (x), mode))
739 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
741 return gen_lowpart (mode, x);
743 /* Converting from integer constant into mode is always equivalent to an
744 subreg operation. */
745 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
747 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
748 return simplify_gen_subreg (mode, x, oldmode, 0);
751 temp = gen_reg_rtx (mode);
752 convert_move (temp, x, unsignedp);
753 return temp;
756 /* Return the largest alignment we can use for doing a move (or store)
757 of MAX_PIECES. ALIGN is the largest alignment we could use. */
759 static unsigned int
760 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
762 machine_mode tmode;
764 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
765 if (align >= GET_MODE_ALIGNMENT (tmode))
766 align = GET_MODE_ALIGNMENT (tmode);
767 else
769 machine_mode tmode, xmode;
771 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
772 tmode != VOIDmode;
773 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
774 if (GET_MODE_SIZE (tmode) > max_pieces
775 || SLOW_UNALIGNED_ACCESS (tmode, align))
776 break;
778 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
781 return align;
784 /* Return the widest integer mode no wider than SIZE. If no such mode
785 can be found, return VOIDmode. */
787 static machine_mode
788 widest_int_mode_for_size (unsigned int size)
790 machine_mode tmode, mode = VOIDmode;
792 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
793 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
794 if (GET_MODE_SIZE (tmode) < size)
795 mode = tmode;
797 return mode;
800 /* Determine whether the LEN bytes can be moved by using several move
801 instructions. Return nonzero if a call to move_by_pieces should
802 succeed. */
805 can_move_by_pieces (unsigned HOST_WIDE_INT len,
806 unsigned int align)
808 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
809 optimize_insn_for_speed_p ());
812 /* Generate several move instructions to copy LEN bytes from block FROM to
813 block TO. (These are MEM rtx's with BLKmode).
815 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
816 used to push FROM to the stack.
818 ALIGN is maximum stack alignment we can assume.
820 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
821 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
822 stpcpy. */
825 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
826 unsigned int align, int endp)
828 struct move_by_pieces_d data;
829 machine_mode to_addr_mode;
830 machine_mode from_addr_mode = get_address_mode (from);
831 rtx to_addr, from_addr = XEXP (from, 0);
832 unsigned int max_size = MOVE_MAX_PIECES + 1;
833 enum insn_code icode;
835 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
837 data.offset = 0;
838 data.from_addr = from_addr;
839 if (to)
841 to_addr_mode = get_address_mode (to);
842 to_addr = XEXP (to, 0);
843 data.to = to;
844 data.autinc_to
845 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
846 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
847 data.reverse
848 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
850 else
852 to_addr_mode = VOIDmode;
853 to_addr = NULL_RTX;
854 data.to = NULL_RTX;
855 data.autinc_to = 1;
856 #ifdef STACK_GROWS_DOWNWARD
857 data.reverse = 1;
858 #else
859 data.reverse = 0;
860 #endif
862 data.to_addr = to_addr;
863 data.from = from;
864 data.autinc_from
865 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
866 || GET_CODE (from_addr) == POST_INC
867 || GET_CODE (from_addr) == POST_DEC);
869 data.explicit_inc_from = 0;
870 data.explicit_inc_to = 0;
871 if (data.reverse) data.offset = len;
872 data.len = len;
874 /* If copying requires more than two move insns,
875 copy addresses to registers (to make displacements shorter)
876 and use post-increment if available. */
877 if (!(data.autinc_from && data.autinc_to)
878 && move_by_pieces_ninsns (len, align, max_size) > 2)
880 /* Find the mode of the largest move...
881 MODE might not be used depending on the definitions of the
882 USE_* macros below. */
883 machine_mode mode ATTRIBUTE_UNUSED
884 = widest_int_mode_for_size (max_size);
886 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
888 data.from_addr = copy_to_mode_reg (from_addr_mode,
889 plus_constant (from_addr_mode,
890 from_addr, len));
891 data.autinc_from = 1;
892 data.explicit_inc_from = -1;
894 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
896 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
897 data.autinc_from = 1;
898 data.explicit_inc_from = 1;
900 if (!data.autinc_from && CONSTANT_P (from_addr))
901 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
902 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
904 data.to_addr = copy_to_mode_reg (to_addr_mode,
905 plus_constant (to_addr_mode,
906 to_addr, len));
907 data.autinc_to = 1;
908 data.explicit_inc_to = -1;
910 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
912 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
913 data.autinc_to = 1;
914 data.explicit_inc_to = 1;
916 if (!data.autinc_to && CONSTANT_P (to_addr))
917 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
920 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
922 /* First move what we can in the largest integer mode, then go to
923 successively smaller modes. */
925 while (max_size > 1 && data.len > 0)
927 machine_mode mode = widest_int_mode_for_size (max_size);
929 if (mode == VOIDmode)
930 break;
932 icode = optab_handler (mov_optab, mode);
933 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
934 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
936 max_size = GET_MODE_SIZE (mode);
939 /* The code above should have handled everything. */
940 gcc_assert (!data.len);
942 if (endp)
944 rtx to1;
946 gcc_assert (!data.reverse);
947 if (data.autinc_to)
949 if (endp == 2)
951 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
952 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
953 else
954 data.to_addr = copy_to_mode_reg (to_addr_mode,
955 plus_constant (to_addr_mode,
956 data.to_addr,
957 -1));
959 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
960 data.offset);
962 else
964 if (endp == 2)
965 --data.offset;
966 to1 = adjust_address (data.to, QImode, data.offset);
968 return to1;
970 else
971 return data.to;
974 /* Return number of insns required to move L bytes by pieces.
975 ALIGN (in bits) is maximum alignment we can assume. */
977 unsigned HOST_WIDE_INT
978 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
979 unsigned int max_size)
981 unsigned HOST_WIDE_INT n_insns = 0;
983 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
985 while (max_size > 1 && l > 0)
987 machine_mode mode;
988 enum insn_code icode;
990 mode = widest_int_mode_for_size (max_size);
992 if (mode == VOIDmode)
993 break;
995 icode = optab_handler (mov_optab, mode);
996 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
997 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
999 max_size = GET_MODE_SIZE (mode);
1002 gcc_assert (!l);
1003 return n_insns;
1006 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1007 with move instructions for mode MODE. GENFUN is the gen_... function
1008 to make a move insn for that mode. DATA has all the other info. */
1010 static void
1011 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1012 struct move_by_pieces_d *data)
1014 unsigned int size = GET_MODE_SIZE (mode);
1015 rtx to1 = NULL_RTX, from1;
1017 while (data->len >= size)
1019 if (data->reverse)
1020 data->offset -= size;
1022 if (data->to)
1024 if (data->autinc_to)
1025 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1026 data->offset);
1027 else
1028 to1 = adjust_address (data->to, mode, data->offset);
1031 if (data->autinc_from)
1032 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1033 data->offset);
1034 else
1035 from1 = adjust_address (data->from, mode, data->offset);
1037 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1038 emit_insn (gen_add2_insn (data->to_addr,
1039 gen_int_mode (-(HOST_WIDE_INT) size,
1040 GET_MODE (data->to_addr))));
1041 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1042 emit_insn (gen_add2_insn (data->from_addr,
1043 gen_int_mode (-(HOST_WIDE_INT) size,
1044 GET_MODE (data->from_addr))));
1046 if (data->to)
1047 emit_insn ((*genfun) (to1, from1));
1048 else
1050 #ifdef PUSH_ROUNDING
1051 emit_single_push_insn (mode, from1, NULL);
1052 #else
1053 gcc_unreachable ();
1054 #endif
1057 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1058 emit_insn (gen_add2_insn (data->to_addr,
1059 gen_int_mode (size,
1060 GET_MODE (data->to_addr))));
1061 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1062 emit_insn (gen_add2_insn (data->from_addr,
1063 gen_int_mode (size,
1064 GET_MODE (data->from_addr))));
1066 if (! data->reverse)
1067 data->offset += size;
1069 data->len -= size;
1073 /* Emit code to move a block Y to a block X. This may be done with
1074 string-move instructions, with multiple scalar move instructions,
1075 or with a library call.
1077 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1078 SIZE is an rtx that says how long they are.
1079 ALIGN is the maximum alignment we can assume they have.
1080 METHOD describes what kind of copy this is, and what mechanisms may be used.
1081 MIN_SIZE is the minimal size of block to move
1082 MAX_SIZE is the maximal size of block to move, if it can not be represented
1083 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1085 Return the address of the new block, if memcpy is called and returns it,
1086 0 otherwise. */
1089 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1090 unsigned int expected_align, HOST_WIDE_INT expected_size,
1091 unsigned HOST_WIDE_INT min_size,
1092 unsigned HOST_WIDE_INT max_size,
1093 unsigned HOST_WIDE_INT probable_max_size)
1095 bool may_use_call;
1096 rtx retval = 0;
1097 unsigned int align;
1099 gcc_assert (size);
1100 if (CONST_INT_P (size)
1101 && INTVAL (size) == 0)
1102 return 0;
1104 switch (method)
1106 case BLOCK_OP_NORMAL:
1107 case BLOCK_OP_TAILCALL:
1108 may_use_call = true;
1109 break;
1111 case BLOCK_OP_CALL_PARM:
1112 may_use_call = block_move_libcall_safe_for_call_parm ();
1114 /* Make inhibit_defer_pop nonzero around the library call
1115 to force it to pop the arguments right away. */
1116 NO_DEFER_POP;
1117 break;
1119 case BLOCK_OP_NO_LIBCALL:
1120 may_use_call = false;
1121 break;
1123 default:
1124 gcc_unreachable ();
1127 gcc_assert (MEM_P (x) && MEM_P (y));
1128 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1129 gcc_assert (align >= BITS_PER_UNIT);
1131 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1132 block copy is more efficient for other large modes, e.g. DCmode. */
1133 x = adjust_address (x, BLKmode, 0);
1134 y = adjust_address (y, BLKmode, 0);
1136 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1137 can be incorrect is coming from __builtin_memcpy. */
1138 if (CONST_INT_P (size))
1140 x = shallow_copy_rtx (x);
1141 y = shallow_copy_rtx (y);
1142 set_mem_size (x, INTVAL (size));
1143 set_mem_size (y, INTVAL (size));
1146 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1147 move_by_pieces (x, y, INTVAL (size), align, 0);
1148 else if (emit_block_move_via_movmem (x, y, size, align,
1149 expected_align, expected_size,
1150 min_size, max_size, probable_max_size))
1152 else if (may_use_call
1153 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1154 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1156 /* Since x and y are passed to a libcall, mark the corresponding
1157 tree EXPR as addressable. */
1158 tree y_expr = MEM_EXPR (y);
1159 tree x_expr = MEM_EXPR (x);
1160 if (y_expr)
1161 mark_addressable (y_expr);
1162 if (x_expr)
1163 mark_addressable (x_expr);
1164 retval = emit_block_move_via_libcall (x, y, size,
1165 method == BLOCK_OP_TAILCALL);
1168 else
1169 emit_block_move_via_loop (x, y, size, align);
1171 if (method == BLOCK_OP_CALL_PARM)
1172 OK_DEFER_POP;
1174 return retval;
1178 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1180 unsigned HOST_WIDE_INT max, min = 0;
1181 if (GET_CODE (size) == CONST_INT)
1182 min = max = UINTVAL (size);
1183 else
1184 max = GET_MODE_MASK (GET_MODE (size));
1185 return emit_block_move_hints (x, y, size, method, 0, -1,
1186 min, max, max);
1189 /* A subroutine of emit_block_move. Returns true if calling the
1190 block move libcall will not clobber any parameters which may have
1191 already been placed on the stack. */
1193 static bool
1194 block_move_libcall_safe_for_call_parm (void)
1196 #if defined (REG_PARM_STACK_SPACE)
1197 tree fn;
1198 #endif
1200 /* If arguments are pushed on the stack, then they're safe. */
1201 if (PUSH_ARGS)
1202 return true;
1204 /* If registers go on the stack anyway, any argument is sure to clobber
1205 an outgoing argument. */
1206 #if defined (REG_PARM_STACK_SPACE)
1207 fn = emit_block_move_libcall_fn (false);
1208 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1209 depend on its argument. */
1210 (void) fn;
1211 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1212 && REG_PARM_STACK_SPACE (fn) != 0)
1213 return false;
1214 #endif
1216 /* If any argument goes in memory, then it might clobber an outgoing
1217 argument. */
1219 CUMULATIVE_ARGS args_so_far_v;
1220 cumulative_args_t args_so_far;
1221 tree fn, arg;
1223 fn = emit_block_move_libcall_fn (false);
1224 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1225 args_so_far = pack_cumulative_args (&args_so_far_v);
1227 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1228 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1230 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1231 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1232 NULL_TREE, true);
1233 if (!tmp || !REG_P (tmp))
1234 return false;
1235 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1236 return false;
1237 targetm.calls.function_arg_advance (args_so_far, mode,
1238 NULL_TREE, true);
1241 return true;
1244 /* A subroutine of emit_block_move. Expand a movmem pattern;
1245 return true if successful. */
1247 static bool
1248 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1249 unsigned int expected_align, HOST_WIDE_INT expected_size,
1250 unsigned HOST_WIDE_INT min_size,
1251 unsigned HOST_WIDE_INT max_size,
1252 unsigned HOST_WIDE_INT probable_max_size)
1254 int save_volatile_ok = volatile_ok;
1255 machine_mode mode;
1257 if (expected_align < align)
1258 expected_align = align;
1259 if (expected_size != -1)
1261 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1262 expected_size = probable_max_size;
1263 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1264 expected_size = min_size;
1267 /* Since this is a move insn, we don't care about volatility. */
1268 volatile_ok = 1;
1270 /* Try the most limited insn first, because there's no point
1271 including more than one in the machine description unless
1272 the more limited one has some advantage. */
1274 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1275 mode = GET_MODE_WIDER_MODE (mode))
1277 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1279 if (code != CODE_FOR_nothing
1280 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1281 here because if SIZE is less than the mode mask, as it is
1282 returned by the macro, it will definitely be less than the
1283 actual mode mask. Since SIZE is within the Pmode address
1284 space, we limit MODE to Pmode. */
1285 && ((CONST_INT_P (size)
1286 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1287 <= (GET_MODE_MASK (mode) >> 1)))
1288 || max_size <= (GET_MODE_MASK (mode) >> 1)
1289 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1291 struct expand_operand ops[9];
1292 unsigned int nops;
1294 /* ??? When called via emit_block_move_for_call, it'd be
1295 nice if there were some way to inform the backend, so
1296 that it doesn't fail the expansion because it thinks
1297 emitting the libcall would be more efficient. */
1298 nops = insn_data[(int) code].n_generator_args;
1299 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1301 create_fixed_operand (&ops[0], x);
1302 create_fixed_operand (&ops[1], y);
1303 /* The check above guarantees that this size conversion is valid. */
1304 create_convert_operand_to (&ops[2], size, mode, true);
1305 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1306 if (nops >= 6)
1308 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1309 create_integer_operand (&ops[5], expected_size);
1311 if (nops >= 8)
1313 create_integer_operand (&ops[6], min_size);
1314 /* If we can not represent the maximal size,
1315 make parameter NULL. */
1316 if ((HOST_WIDE_INT) max_size != -1)
1317 create_integer_operand (&ops[7], max_size);
1318 else
1319 create_fixed_operand (&ops[7], NULL);
1321 if (nops == 9)
1323 /* If we can not represent the maximal size,
1324 make parameter NULL. */
1325 if ((HOST_WIDE_INT) probable_max_size != -1)
1326 create_integer_operand (&ops[8], probable_max_size);
1327 else
1328 create_fixed_operand (&ops[8], NULL);
1330 if (maybe_expand_insn (code, nops, ops))
1332 volatile_ok = save_volatile_ok;
1333 return true;
1338 volatile_ok = save_volatile_ok;
1339 return false;
1342 /* A subroutine of emit_block_move. Expand a call to memcpy.
1343 Return the return value from memcpy, 0 otherwise. */
1346 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1348 rtx dst_addr, src_addr;
1349 tree call_expr, fn, src_tree, dst_tree, size_tree;
1350 machine_mode size_mode;
1351 rtx retval;
1353 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1354 pseudos. We can then place those new pseudos into a VAR_DECL and
1355 use them later. */
1357 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1358 src_addr = copy_addr_to_reg (XEXP (src, 0));
1360 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1361 src_addr = convert_memory_address (ptr_mode, src_addr);
1363 dst_tree = make_tree (ptr_type_node, dst_addr);
1364 src_tree = make_tree (ptr_type_node, src_addr);
1366 size_mode = TYPE_MODE (sizetype);
1368 size = convert_to_mode (size_mode, size, 1);
1369 size = copy_to_mode_reg (size_mode, size);
1371 /* It is incorrect to use the libcall calling conventions to call
1372 memcpy in this context. This could be a user call to memcpy and
1373 the user may wish to examine the return value from memcpy. For
1374 targets where libcalls and normal calls have different conventions
1375 for returning pointers, we could end up generating incorrect code. */
1377 size_tree = make_tree (sizetype, size);
1379 fn = emit_block_move_libcall_fn (true);
1380 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1381 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1383 retval = expand_normal (call_expr);
1385 return retval;
1388 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1389 for the function we use for block copies. */
1391 static GTY(()) tree block_move_fn;
1393 void
1394 init_block_move_fn (const char *asmspec)
1396 if (!block_move_fn)
1398 tree args, fn, attrs, attr_args;
1400 fn = get_identifier ("memcpy");
1401 args = build_function_type_list (ptr_type_node, ptr_type_node,
1402 const_ptr_type_node, sizetype,
1403 NULL_TREE);
1405 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1406 DECL_EXTERNAL (fn) = 1;
1407 TREE_PUBLIC (fn) = 1;
1408 DECL_ARTIFICIAL (fn) = 1;
1409 TREE_NOTHROW (fn) = 1;
1410 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1411 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1413 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1414 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1416 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1418 block_move_fn = fn;
1421 if (asmspec)
1422 set_user_assembler_name (block_move_fn, asmspec);
1425 static tree
1426 emit_block_move_libcall_fn (int for_call)
1428 static bool emitted_extern;
1430 if (!block_move_fn)
1431 init_block_move_fn (NULL);
1433 if (for_call && !emitted_extern)
1435 emitted_extern = true;
1436 make_decl_rtl (block_move_fn);
1439 return block_move_fn;
1442 /* A subroutine of emit_block_move. Copy the data via an explicit
1443 loop. This is used only when libcalls are forbidden. */
1444 /* ??? It'd be nice to copy in hunks larger than QImode. */
1446 static void
1447 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1448 unsigned int align ATTRIBUTE_UNUSED)
1450 rtx_code_label *cmp_label, *top_label;
1451 rtx iter, x_addr, y_addr, tmp;
1452 machine_mode x_addr_mode = get_address_mode (x);
1453 machine_mode y_addr_mode = get_address_mode (y);
1454 machine_mode iter_mode;
1456 iter_mode = GET_MODE (size);
1457 if (iter_mode == VOIDmode)
1458 iter_mode = word_mode;
1460 top_label = gen_label_rtx ();
1461 cmp_label = gen_label_rtx ();
1462 iter = gen_reg_rtx (iter_mode);
1464 emit_move_insn (iter, const0_rtx);
1466 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1467 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1468 do_pending_stack_adjust ();
1470 emit_jump (cmp_label);
1471 emit_label (top_label);
1473 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1474 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1476 if (x_addr_mode != y_addr_mode)
1477 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1478 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1480 x = change_address (x, QImode, x_addr);
1481 y = change_address (y, QImode, y_addr);
1483 emit_move_insn (x, y);
1485 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1486 true, OPTAB_LIB_WIDEN);
1487 if (tmp != iter)
1488 emit_move_insn (iter, tmp);
1490 emit_label (cmp_label);
1492 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1493 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1496 /* Copy all or part of a value X into registers starting at REGNO.
1497 The number of registers to be filled is NREGS. */
1499 void
1500 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1502 int i;
1503 #ifdef HAVE_load_multiple
1504 rtx pat;
1505 rtx_insn *last;
1506 #endif
1508 if (nregs == 0)
1509 return;
1511 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1512 x = validize_mem (force_const_mem (mode, x));
1514 /* See if the machine can do this with a load multiple insn. */
1515 #ifdef HAVE_load_multiple
1516 if (HAVE_load_multiple)
1518 last = get_last_insn ();
1519 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1520 GEN_INT (nregs));
1521 if (pat)
1523 emit_insn (pat);
1524 return;
1526 else
1527 delete_insns_since (last);
1529 #endif
1531 for (i = 0; i < nregs; i++)
1532 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1533 operand_subword_force (x, i, mode));
1536 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1537 The number of registers to be filled is NREGS. */
1539 void
1540 move_block_from_reg (int regno, rtx x, int nregs)
1542 int i;
1544 if (nregs == 0)
1545 return;
1547 /* See if the machine can do this with a store multiple insn. */
1548 #ifdef HAVE_store_multiple
1549 if (HAVE_store_multiple)
1551 rtx_insn *last = get_last_insn ();
1552 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1553 GEN_INT (nregs));
1554 if (pat)
1556 emit_insn (pat);
1557 return;
1559 else
1560 delete_insns_since (last);
1562 #endif
1564 for (i = 0; i < nregs; i++)
1566 rtx tem = operand_subword (x, i, 1, BLKmode);
1568 gcc_assert (tem);
1570 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1574 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1575 ORIG, where ORIG is a non-consecutive group of registers represented by
1576 a PARALLEL. The clone is identical to the original except in that the
1577 original set of registers is replaced by a new set of pseudo registers.
1578 The new set has the same modes as the original set. */
1581 gen_group_rtx (rtx orig)
1583 int i, length;
1584 rtx *tmps;
1586 gcc_assert (GET_CODE (orig) == PARALLEL);
1588 length = XVECLEN (orig, 0);
1589 tmps = XALLOCAVEC (rtx, length);
1591 /* Skip a NULL entry in first slot. */
1592 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1594 if (i)
1595 tmps[0] = 0;
1597 for (; i < length; i++)
1599 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1600 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1602 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1605 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1608 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1609 except that values are placed in TMPS[i], and must later be moved
1610 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1612 static void
1613 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1615 rtx src;
1616 int start, i;
1617 machine_mode m = GET_MODE (orig_src);
1619 gcc_assert (GET_CODE (dst) == PARALLEL);
1621 if (m != VOIDmode
1622 && !SCALAR_INT_MODE_P (m)
1623 && !MEM_P (orig_src)
1624 && GET_CODE (orig_src) != CONCAT)
1626 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1627 if (imode == BLKmode)
1628 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1629 else
1630 src = gen_reg_rtx (imode);
1631 if (imode != BLKmode)
1632 src = gen_lowpart (GET_MODE (orig_src), src);
1633 emit_move_insn (src, orig_src);
1634 /* ...and back again. */
1635 if (imode != BLKmode)
1636 src = gen_lowpart (imode, src);
1637 emit_group_load_1 (tmps, dst, src, type, ssize);
1638 return;
1641 /* Check for a NULL entry, used to indicate that the parameter goes
1642 both on the stack and in registers. */
1643 if (XEXP (XVECEXP (dst, 0, 0), 0))
1644 start = 0;
1645 else
1646 start = 1;
1648 /* Process the pieces. */
1649 for (i = start; i < XVECLEN (dst, 0); i++)
1651 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1652 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1653 unsigned int bytelen = GET_MODE_SIZE (mode);
1654 int shift = 0;
1656 /* Handle trailing fragments that run over the size of the struct. */
1657 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1659 /* Arrange to shift the fragment to where it belongs.
1660 extract_bit_field loads to the lsb of the reg. */
1661 if (
1662 #ifdef BLOCK_REG_PADDING
1663 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1664 == (BYTES_BIG_ENDIAN ? upward : downward)
1665 #else
1666 BYTES_BIG_ENDIAN
1667 #endif
1669 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1670 bytelen = ssize - bytepos;
1671 gcc_assert (bytelen > 0);
1674 /* If we won't be loading directly from memory, protect the real source
1675 from strange tricks we might play; but make sure that the source can
1676 be loaded directly into the destination. */
1677 src = orig_src;
1678 if (!MEM_P (orig_src)
1679 && (!CONSTANT_P (orig_src)
1680 || (GET_MODE (orig_src) != mode
1681 && GET_MODE (orig_src) != VOIDmode)))
1683 if (GET_MODE (orig_src) == VOIDmode)
1684 src = gen_reg_rtx (mode);
1685 else
1686 src = gen_reg_rtx (GET_MODE (orig_src));
1688 emit_move_insn (src, orig_src);
1691 /* Optimize the access just a bit. */
1692 if (MEM_P (src)
1693 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1694 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1695 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1696 && bytelen == GET_MODE_SIZE (mode))
1698 tmps[i] = gen_reg_rtx (mode);
1699 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1701 else if (COMPLEX_MODE_P (mode)
1702 && GET_MODE (src) == mode
1703 && bytelen == GET_MODE_SIZE (mode))
1704 /* Let emit_move_complex do the bulk of the work. */
1705 tmps[i] = src;
1706 else if (GET_CODE (src) == CONCAT)
1708 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1709 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1711 if ((bytepos == 0 && bytelen == slen0)
1712 || (bytepos != 0 && bytepos + bytelen <= slen))
1714 /* The following assumes that the concatenated objects all
1715 have the same size. In this case, a simple calculation
1716 can be used to determine the object and the bit field
1717 to be extracted. */
1718 tmps[i] = XEXP (src, bytepos / slen0);
1719 if (! CONSTANT_P (tmps[i])
1720 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1721 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1722 (bytepos % slen0) * BITS_PER_UNIT,
1723 1, NULL_RTX, mode, mode);
1725 else
1727 rtx mem;
1729 gcc_assert (!bytepos);
1730 mem = assign_stack_temp (GET_MODE (src), slen);
1731 emit_move_insn (mem, src);
1732 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1733 0, 1, NULL_RTX, mode, mode);
1736 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1737 SIMD register, which is currently broken. While we get GCC
1738 to emit proper RTL for these cases, let's dump to memory. */
1739 else if (VECTOR_MODE_P (GET_MODE (dst))
1740 && REG_P (src))
1742 int slen = GET_MODE_SIZE (GET_MODE (src));
1743 rtx mem;
1745 mem = assign_stack_temp (GET_MODE (src), slen);
1746 emit_move_insn (mem, src);
1747 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1749 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1750 && XVECLEN (dst, 0) > 1)
1751 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1752 else if (CONSTANT_P (src))
1754 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1756 if (len == ssize)
1757 tmps[i] = src;
1758 else
1760 rtx first, second;
1762 /* TODO: const_wide_int can have sizes other than this... */
1763 gcc_assert (2 * len == ssize);
1764 split_double (src, &first, &second);
1765 if (i)
1766 tmps[i] = second;
1767 else
1768 tmps[i] = first;
1771 else if (REG_P (src) && GET_MODE (src) == mode)
1772 tmps[i] = src;
1773 else
1774 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1775 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1776 mode, mode);
1778 if (shift)
1779 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1780 shift, tmps[i], 0);
1784 /* Emit code to move a block SRC of type TYPE to a block DST,
1785 where DST is non-consecutive registers represented by a PARALLEL.
1786 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1787 if not known. */
1789 void
1790 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1792 rtx *tmps;
1793 int i;
1795 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1796 emit_group_load_1 (tmps, dst, src, type, ssize);
1798 /* Copy the extracted pieces into the proper (probable) hard regs. */
1799 for (i = 0; i < XVECLEN (dst, 0); i++)
1801 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1802 if (d == NULL)
1803 continue;
1804 emit_move_insn (d, tmps[i]);
1808 /* Similar, but load SRC into new pseudos in a format that looks like
1809 PARALLEL. This can later be fed to emit_group_move to get things
1810 in the right place. */
1813 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1815 rtvec vec;
1816 int i;
1818 vec = rtvec_alloc (XVECLEN (parallel, 0));
1819 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1821 /* Convert the vector to look just like the original PARALLEL, except
1822 with the computed values. */
1823 for (i = 0; i < XVECLEN (parallel, 0); i++)
1825 rtx e = XVECEXP (parallel, 0, i);
1826 rtx d = XEXP (e, 0);
1828 if (d)
1830 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1831 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1833 RTVEC_ELT (vec, i) = e;
1836 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1839 /* Emit code to move a block SRC to block DST, where SRC and DST are
1840 non-consecutive groups of registers, each represented by a PARALLEL. */
1842 void
1843 emit_group_move (rtx dst, rtx src)
1845 int i;
1847 gcc_assert (GET_CODE (src) == PARALLEL
1848 && GET_CODE (dst) == PARALLEL
1849 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1851 /* Skip first entry if NULL. */
1852 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1853 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1854 XEXP (XVECEXP (src, 0, i), 0));
1857 /* Move a group of registers represented by a PARALLEL into pseudos. */
1860 emit_group_move_into_temps (rtx src)
1862 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1863 int i;
1865 for (i = 0; i < XVECLEN (src, 0); i++)
1867 rtx e = XVECEXP (src, 0, i);
1868 rtx d = XEXP (e, 0);
1870 if (d)
1871 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1872 RTVEC_ELT (vec, i) = e;
1875 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1878 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1879 where SRC is non-consecutive registers represented by a PARALLEL.
1880 SSIZE represents the total size of block ORIG_DST, or -1 if not
1881 known. */
1883 void
1884 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1886 rtx *tmps, dst;
1887 int start, finish, i;
1888 machine_mode m = GET_MODE (orig_dst);
1890 gcc_assert (GET_CODE (src) == PARALLEL);
1892 if (!SCALAR_INT_MODE_P (m)
1893 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1895 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1896 if (imode == BLKmode)
1897 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1898 else
1899 dst = gen_reg_rtx (imode);
1900 emit_group_store (dst, src, type, ssize);
1901 if (imode != BLKmode)
1902 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1903 emit_move_insn (orig_dst, dst);
1904 return;
1907 /* Check for a NULL entry, used to indicate that the parameter goes
1908 both on the stack and in registers. */
1909 if (XEXP (XVECEXP (src, 0, 0), 0))
1910 start = 0;
1911 else
1912 start = 1;
1913 finish = XVECLEN (src, 0);
1915 tmps = XALLOCAVEC (rtx, finish);
1917 /* Copy the (probable) hard regs into pseudos. */
1918 for (i = start; i < finish; i++)
1920 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1921 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1923 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1924 emit_move_insn (tmps[i], reg);
1926 else
1927 tmps[i] = reg;
1930 /* If we won't be storing directly into memory, protect the real destination
1931 from strange tricks we might play. */
1932 dst = orig_dst;
1933 if (GET_CODE (dst) == PARALLEL)
1935 rtx temp;
1937 /* We can get a PARALLEL dst if there is a conditional expression in
1938 a return statement. In that case, the dst and src are the same,
1939 so no action is necessary. */
1940 if (rtx_equal_p (dst, src))
1941 return;
1943 /* It is unclear if we can ever reach here, but we may as well handle
1944 it. Allocate a temporary, and split this into a store/load to/from
1945 the temporary. */
1946 temp = assign_stack_temp (GET_MODE (dst), ssize);
1947 emit_group_store (temp, src, type, ssize);
1948 emit_group_load (dst, temp, type, ssize);
1949 return;
1951 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1953 machine_mode outer = GET_MODE (dst);
1954 machine_mode inner;
1955 HOST_WIDE_INT bytepos;
1956 bool done = false;
1957 rtx temp;
1959 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1960 dst = gen_reg_rtx (outer);
1962 /* Make life a bit easier for combine. */
1963 /* If the first element of the vector is the low part
1964 of the destination mode, use a paradoxical subreg to
1965 initialize the destination. */
1966 if (start < finish)
1968 inner = GET_MODE (tmps[start]);
1969 bytepos = subreg_lowpart_offset (inner, outer);
1970 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1972 temp = simplify_gen_subreg (outer, tmps[start],
1973 inner, 0);
1974 if (temp)
1976 emit_move_insn (dst, temp);
1977 done = true;
1978 start++;
1983 /* If the first element wasn't the low part, try the last. */
1984 if (!done
1985 && start < finish - 1)
1987 inner = GET_MODE (tmps[finish - 1]);
1988 bytepos = subreg_lowpart_offset (inner, outer);
1989 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1991 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1992 inner, 0);
1993 if (temp)
1995 emit_move_insn (dst, temp);
1996 done = true;
1997 finish--;
2002 /* Otherwise, simply initialize the result to zero. */
2003 if (!done)
2004 emit_move_insn (dst, CONST0_RTX (outer));
2007 /* Process the pieces. */
2008 for (i = start; i < finish; i++)
2010 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2011 machine_mode mode = GET_MODE (tmps[i]);
2012 unsigned int bytelen = GET_MODE_SIZE (mode);
2013 unsigned int adj_bytelen;
2014 rtx dest = dst;
2016 /* Handle trailing fragments that run over the size of the struct. */
2017 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2018 adj_bytelen = ssize - bytepos;
2019 else
2020 adj_bytelen = bytelen;
2022 if (GET_CODE (dst) == CONCAT)
2024 if (bytepos + adj_bytelen
2025 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2026 dest = XEXP (dst, 0);
2027 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2029 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2030 dest = XEXP (dst, 1);
2032 else
2034 machine_mode dest_mode = GET_MODE (dest);
2035 machine_mode tmp_mode = GET_MODE (tmps[i]);
2037 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2039 if (GET_MODE_ALIGNMENT (dest_mode)
2040 >= GET_MODE_ALIGNMENT (tmp_mode))
2042 dest = assign_stack_temp (dest_mode,
2043 GET_MODE_SIZE (dest_mode));
2044 emit_move_insn (adjust_address (dest,
2045 tmp_mode,
2046 bytepos),
2047 tmps[i]);
2048 dst = dest;
2050 else
2052 dest = assign_stack_temp (tmp_mode,
2053 GET_MODE_SIZE (tmp_mode));
2054 emit_move_insn (dest, tmps[i]);
2055 dst = adjust_address (dest, dest_mode, bytepos);
2057 break;
2061 /* Handle trailing fragments that run over the size of the struct. */
2062 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2064 /* store_bit_field always takes its value from the lsb.
2065 Move the fragment to the lsb if it's not already there. */
2066 if (
2067 #ifdef BLOCK_REG_PADDING
2068 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2069 == (BYTES_BIG_ENDIAN ? upward : downward)
2070 #else
2071 BYTES_BIG_ENDIAN
2072 #endif
2075 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2076 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2077 shift, tmps[i], 0);
2080 /* Make sure not to write past the end of the struct. */
2081 store_bit_field (dest,
2082 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2083 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2084 VOIDmode, tmps[i]);
2087 /* Optimize the access just a bit. */
2088 else if (MEM_P (dest)
2089 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2090 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2091 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2092 && bytelen == GET_MODE_SIZE (mode))
2093 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2095 else
2096 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2097 0, 0, mode, tmps[i]);
2100 /* Copy from the pseudo into the (probable) hard reg. */
2101 if (orig_dst != dst)
2102 emit_move_insn (orig_dst, dst);
2105 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2106 of the value stored in X. */
2109 maybe_emit_group_store (rtx x, tree type)
2111 machine_mode mode = TYPE_MODE (type);
2112 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2113 if (GET_CODE (x) == PARALLEL)
2115 rtx result = gen_reg_rtx (mode);
2116 emit_group_store (result, x, type, int_size_in_bytes (type));
2117 return result;
2119 return x;
2122 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2124 This is used on targets that return BLKmode values in registers. */
2126 void
2127 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2129 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2130 rtx src = NULL, dst = NULL;
2131 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2132 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2133 machine_mode mode = GET_MODE (srcreg);
2134 machine_mode tmode = GET_MODE (target);
2135 machine_mode copy_mode;
2137 /* BLKmode registers created in the back-end shouldn't have survived. */
2138 gcc_assert (mode != BLKmode);
2140 /* If the structure doesn't take up a whole number of words, see whether
2141 SRCREG is padded on the left or on the right. If it's on the left,
2142 set PADDING_CORRECTION to the number of bits to skip.
2144 In most ABIs, the structure will be returned at the least end of
2145 the register, which translates to right padding on little-endian
2146 targets and left padding on big-endian targets. The opposite
2147 holds if the structure is returned at the most significant
2148 end of the register. */
2149 if (bytes % UNITS_PER_WORD != 0
2150 && (targetm.calls.return_in_msb (type)
2151 ? !BYTES_BIG_ENDIAN
2152 : BYTES_BIG_ENDIAN))
2153 padding_correction
2154 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2156 /* We can use a single move if we have an exact mode for the size. */
2157 else if (MEM_P (target)
2158 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2159 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2160 && bytes == GET_MODE_SIZE (mode))
2162 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2163 return;
2166 /* And if we additionally have the same mode for a register. */
2167 else if (REG_P (target)
2168 && GET_MODE (target) == mode
2169 && bytes == GET_MODE_SIZE (mode))
2171 emit_move_insn (target, srcreg);
2172 return;
2175 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2176 into a new pseudo which is a full word. */
2177 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2179 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2180 mode = word_mode;
2183 /* Copy the structure BITSIZE bits at a time. If the target lives in
2184 memory, take care of not reading/writing past its end by selecting
2185 a copy mode suited to BITSIZE. This should always be possible given
2186 how it is computed.
2188 If the target lives in register, make sure not to select a copy mode
2189 larger than the mode of the register.
2191 We could probably emit more efficient code for machines which do not use
2192 strict alignment, but it doesn't seem worth the effort at the current
2193 time. */
2195 copy_mode = word_mode;
2196 if (MEM_P (target))
2198 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2199 if (mem_mode != BLKmode)
2200 copy_mode = mem_mode;
2202 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2203 copy_mode = tmode;
2205 for (bitpos = 0, xbitpos = padding_correction;
2206 bitpos < bytes * BITS_PER_UNIT;
2207 bitpos += bitsize, xbitpos += bitsize)
2209 /* We need a new source operand each time xbitpos is on a
2210 word boundary and when xbitpos == padding_correction
2211 (the first time through). */
2212 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2213 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2215 /* We need a new destination operand each time bitpos is on
2216 a word boundary. */
2217 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2218 dst = target;
2219 else if (bitpos % BITS_PER_WORD == 0)
2220 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2222 /* Use xbitpos for the source extraction (right justified) and
2223 bitpos for the destination store (left justified). */
2224 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2225 extract_bit_field (src, bitsize,
2226 xbitpos % BITS_PER_WORD, 1,
2227 NULL_RTX, copy_mode, copy_mode));
2231 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2232 register if it contains any data, otherwise return null.
2234 This is used on targets that return BLKmode values in registers. */
2237 copy_blkmode_to_reg (machine_mode mode, tree src)
2239 int i, n_regs;
2240 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2241 unsigned int bitsize;
2242 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2243 machine_mode dst_mode;
2245 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2247 x = expand_normal (src);
2249 bytes = int_size_in_bytes (TREE_TYPE (src));
2250 if (bytes == 0)
2251 return NULL_RTX;
2253 /* If the structure doesn't take up a whole number of words, see
2254 whether the register value should be padded on the left or on
2255 the right. Set PADDING_CORRECTION to the number of padding
2256 bits needed on the left side.
2258 In most ABIs, the structure will be returned at the least end of
2259 the register, which translates to right padding on little-endian
2260 targets and left padding on big-endian targets. The opposite
2261 holds if the structure is returned at the most significant
2262 end of the register. */
2263 if (bytes % UNITS_PER_WORD != 0
2264 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2265 ? !BYTES_BIG_ENDIAN
2266 : BYTES_BIG_ENDIAN))
2267 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2268 * BITS_PER_UNIT));
2270 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2271 dst_words = XALLOCAVEC (rtx, n_regs);
2272 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2274 /* Copy the structure BITSIZE bits at a time. */
2275 for (bitpos = 0, xbitpos = padding_correction;
2276 bitpos < bytes * BITS_PER_UNIT;
2277 bitpos += bitsize, xbitpos += bitsize)
2279 /* We need a new destination pseudo each time xbitpos is
2280 on a word boundary and when xbitpos == padding_correction
2281 (the first time through). */
2282 if (xbitpos % BITS_PER_WORD == 0
2283 || xbitpos == padding_correction)
2285 /* Generate an appropriate register. */
2286 dst_word = gen_reg_rtx (word_mode);
2287 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2289 /* Clear the destination before we move anything into it. */
2290 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2293 /* We need a new source operand each time bitpos is on a word
2294 boundary. */
2295 if (bitpos % BITS_PER_WORD == 0)
2296 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2298 /* Use bitpos for the source extraction (left justified) and
2299 xbitpos for the destination store (right justified). */
2300 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2301 0, 0, word_mode,
2302 extract_bit_field (src_word, bitsize,
2303 bitpos % BITS_PER_WORD, 1,
2304 NULL_RTX, word_mode, word_mode));
2307 if (mode == BLKmode)
2309 /* Find the smallest integer mode large enough to hold the
2310 entire structure. */
2311 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2312 mode != VOIDmode;
2313 mode = GET_MODE_WIDER_MODE (mode))
2314 /* Have we found a large enough mode? */
2315 if (GET_MODE_SIZE (mode) >= bytes)
2316 break;
2318 /* A suitable mode should have been found. */
2319 gcc_assert (mode != VOIDmode);
2322 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2323 dst_mode = word_mode;
2324 else
2325 dst_mode = mode;
2326 dst = gen_reg_rtx (dst_mode);
2328 for (i = 0; i < n_regs; i++)
2329 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2331 if (mode != dst_mode)
2332 dst = gen_lowpart (mode, dst);
2334 return dst;
2337 /* Add a USE expression for REG to the (possibly empty) list pointed
2338 to by CALL_FUSAGE. REG must denote a hard register. */
2340 void
2341 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2343 gcc_assert (REG_P (reg));
2345 if (!HARD_REGISTER_P (reg))
2346 return;
2348 *call_fusage
2349 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2352 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2353 to by CALL_FUSAGE. REG must denote a hard register. */
2355 void
2356 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2358 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2360 *call_fusage
2361 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2364 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2365 starting at REGNO. All of these registers must be hard registers. */
2367 void
2368 use_regs (rtx *call_fusage, int regno, int nregs)
2370 int i;
2372 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2374 for (i = 0; i < nregs; i++)
2375 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2378 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2379 PARALLEL REGS. This is for calls that pass values in multiple
2380 non-contiguous locations. The Irix 6 ABI has examples of this. */
2382 void
2383 use_group_regs (rtx *call_fusage, rtx regs)
2385 int i;
2387 for (i = 0; i < XVECLEN (regs, 0); i++)
2389 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2391 /* A NULL entry means the parameter goes both on the stack and in
2392 registers. This can also be a MEM for targets that pass values
2393 partially on the stack and partially in registers. */
2394 if (reg != 0 && REG_P (reg))
2395 use_reg (call_fusage, reg);
2399 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2400 assigment and the code of the expresion on the RHS is CODE. Return
2401 NULL otherwise. */
2403 static gimple
2404 get_def_for_expr (tree name, enum tree_code code)
2406 gimple def_stmt;
2408 if (TREE_CODE (name) != SSA_NAME)
2409 return NULL;
2411 def_stmt = get_gimple_for_ssa_name (name);
2412 if (!def_stmt
2413 || gimple_assign_rhs_code (def_stmt) != code)
2414 return NULL;
2416 return def_stmt;
2419 #ifdef HAVE_conditional_move
2420 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2421 assigment and the class of the expresion on the RHS is CLASS. Return
2422 NULL otherwise. */
2424 static gimple
2425 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2427 gimple def_stmt;
2429 if (TREE_CODE (name) != SSA_NAME)
2430 return NULL;
2432 def_stmt = get_gimple_for_ssa_name (name);
2433 if (!def_stmt
2434 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2435 return NULL;
2437 return def_stmt;
2439 #endif
2442 /* Determine whether the LEN bytes generated by CONSTFUN can be
2443 stored to memory using several move instructions. CONSTFUNDATA is
2444 a pointer which will be passed as argument in every CONSTFUN call.
2445 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2446 a memset operation and false if it's a copy of a constant string.
2447 Return nonzero if a call to store_by_pieces should succeed. */
2450 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2451 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2452 void *constfundata, unsigned int align, bool memsetp)
2454 unsigned HOST_WIDE_INT l;
2455 unsigned int max_size;
2456 HOST_WIDE_INT offset = 0;
2457 machine_mode mode;
2458 enum insn_code icode;
2459 int reverse;
2460 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2461 rtx cst ATTRIBUTE_UNUSED;
2463 if (len == 0)
2464 return 1;
2466 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2467 memsetp
2468 ? SET_BY_PIECES
2469 : STORE_BY_PIECES,
2470 optimize_insn_for_speed_p ()))
2471 return 0;
2473 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2475 /* We would first store what we can in the largest integer mode, then go to
2476 successively smaller modes. */
2478 for (reverse = 0;
2479 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2480 reverse++)
2482 l = len;
2483 max_size = STORE_MAX_PIECES + 1;
2484 while (max_size > 1 && l > 0)
2486 mode = widest_int_mode_for_size (max_size);
2488 if (mode == VOIDmode)
2489 break;
2491 icode = optab_handler (mov_optab, mode);
2492 if (icode != CODE_FOR_nothing
2493 && align >= GET_MODE_ALIGNMENT (mode))
2495 unsigned int size = GET_MODE_SIZE (mode);
2497 while (l >= size)
2499 if (reverse)
2500 offset -= size;
2502 cst = (*constfun) (constfundata, offset, mode);
2503 if (!targetm.legitimate_constant_p (mode, cst))
2504 return 0;
2506 if (!reverse)
2507 offset += size;
2509 l -= size;
2513 max_size = GET_MODE_SIZE (mode);
2516 /* The code above should have handled everything. */
2517 gcc_assert (!l);
2520 return 1;
2523 /* Generate several move instructions to store LEN bytes generated by
2524 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2525 pointer which will be passed as argument in every CONSTFUN call.
2526 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2527 a memset operation and false if it's a copy of a constant string.
2528 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2529 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2530 stpcpy. */
2533 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2534 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2535 void *constfundata, unsigned int align, bool memsetp, int endp)
2537 machine_mode to_addr_mode = get_address_mode (to);
2538 struct store_by_pieces_d data;
2540 if (len == 0)
2542 gcc_assert (endp != 2);
2543 return to;
2546 gcc_assert (targetm.use_by_pieces_infrastructure_p
2547 (len, align,
2548 memsetp
2549 ? SET_BY_PIECES
2550 : STORE_BY_PIECES,
2551 optimize_insn_for_speed_p ()));
2553 data.constfun = constfun;
2554 data.constfundata = constfundata;
2555 data.len = len;
2556 data.to = to;
2557 store_by_pieces_1 (&data, align);
2558 if (endp)
2560 rtx to1;
2562 gcc_assert (!data.reverse);
2563 if (data.autinc_to)
2565 if (endp == 2)
2567 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2568 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2569 else
2570 data.to_addr = copy_to_mode_reg (to_addr_mode,
2571 plus_constant (to_addr_mode,
2572 data.to_addr,
2573 -1));
2575 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2576 data.offset);
2578 else
2580 if (endp == 2)
2581 --data.offset;
2582 to1 = adjust_address (data.to, QImode, data.offset);
2584 return to1;
2586 else
2587 return data.to;
2590 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2591 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2593 static void
2594 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2596 struct store_by_pieces_d data;
2598 if (len == 0)
2599 return;
2601 data.constfun = clear_by_pieces_1;
2602 data.constfundata = NULL;
2603 data.len = len;
2604 data.to = to;
2605 store_by_pieces_1 (&data, align);
2608 /* Callback routine for clear_by_pieces.
2609 Return const0_rtx unconditionally. */
2611 static rtx
2612 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2613 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2614 machine_mode mode ATTRIBUTE_UNUSED)
2616 return const0_rtx;
2619 /* Subroutine of clear_by_pieces and store_by_pieces.
2620 Generate several move instructions to store LEN bytes of block TO. (A MEM
2621 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2623 static void
2624 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2625 unsigned int align ATTRIBUTE_UNUSED)
2627 machine_mode to_addr_mode = get_address_mode (data->to);
2628 rtx to_addr = XEXP (data->to, 0);
2629 unsigned int max_size = STORE_MAX_PIECES + 1;
2630 enum insn_code icode;
2632 data->offset = 0;
2633 data->to_addr = to_addr;
2634 data->autinc_to
2635 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2636 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2638 data->explicit_inc_to = 0;
2639 data->reverse
2640 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2641 if (data->reverse)
2642 data->offset = data->len;
2644 /* If storing requires more than two move insns,
2645 copy addresses to registers (to make displacements shorter)
2646 and use post-increment if available. */
2647 if (!data->autinc_to
2648 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2650 /* Determine the main mode we'll be using.
2651 MODE might not be used depending on the definitions of the
2652 USE_* macros below. */
2653 machine_mode mode ATTRIBUTE_UNUSED
2654 = widest_int_mode_for_size (max_size);
2656 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2658 data->to_addr = copy_to_mode_reg (to_addr_mode,
2659 plus_constant (to_addr_mode,
2660 to_addr,
2661 data->len));
2662 data->autinc_to = 1;
2663 data->explicit_inc_to = -1;
2666 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2667 && ! data->autinc_to)
2669 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2670 data->autinc_to = 1;
2671 data->explicit_inc_to = 1;
2674 if ( !data->autinc_to && CONSTANT_P (to_addr))
2675 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2678 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2680 /* First store what we can in the largest integer mode, then go to
2681 successively smaller modes. */
2683 while (max_size > 1 && data->len > 0)
2685 machine_mode mode = widest_int_mode_for_size (max_size);
2687 if (mode == VOIDmode)
2688 break;
2690 icode = optab_handler (mov_optab, mode);
2691 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2692 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2694 max_size = GET_MODE_SIZE (mode);
2697 /* The code above should have handled everything. */
2698 gcc_assert (!data->len);
2701 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2702 with move instructions for mode MODE. GENFUN is the gen_... function
2703 to make a move insn for that mode. DATA has all the other info. */
2705 static void
2706 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2707 struct store_by_pieces_d *data)
2709 unsigned int size = GET_MODE_SIZE (mode);
2710 rtx to1, cst;
2712 while (data->len >= size)
2714 if (data->reverse)
2715 data->offset -= size;
2717 if (data->autinc_to)
2718 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2719 data->offset);
2720 else
2721 to1 = adjust_address (data->to, mode, data->offset);
2723 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2724 emit_insn (gen_add2_insn (data->to_addr,
2725 gen_int_mode (-(HOST_WIDE_INT) size,
2726 GET_MODE (data->to_addr))));
2728 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2729 emit_insn ((*genfun) (to1, cst));
2731 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2732 emit_insn (gen_add2_insn (data->to_addr,
2733 gen_int_mode (size,
2734 GET_MODE (data->to_addr))));
2736 if (! data->reverse)
2737 data->offset += size;
2739 data->len -= size;
2743 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2744 its length in bytes. */
2747 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2748 unsigned int expected_align, HOST_WIDE_INT expected_size,
2749 unsigned HOST_WIDE_INT min_size,
2750 unsigned HOST_WIDE_INT max_size,
2751 unsigned HOST_WIDE_INT probable_max_size)
2753 machine_mode mode = GET_MODE (object);
2754 unsigned int align;
2756 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2758 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2759 just move a zero. Otherwise, do this a piece at a time. */
2760 if (mode != BLKmode
2761 && CONST_INT_P (size)
2762 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2764 rtx zero = CONST0_RTX (mode);
2765 if (zero != NULL)
2767 emit_move_insn (object, zero);
2768 return NULL;
2771 if (COMPLEX_MODE_P (mode))
2773 zero = CONST0_RTX (GET_MODE_INNER (mode));
2774 if (zero != NULL)
2776 write_complex_part (object, zero, 0);
2777 write_complex_part (object, zero, 1);
2778 return NULL;
2783 if (size == const0_rtx)
2784 return NULL;
2786 align = MEM_ALIGN (object);
2788 if (CONST_INT_P (size)
2789 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2790 CLEAR_BY_PIECES,
2791 optimize_insn_for_speed_p ()))
2792 clear_by_pieces (object, INTVAL (size), align);
2793 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2794 expected_align, expected_size,
2795 min_size, max_size, probable_max_size))
2797 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2798 return set_storage_via_libcall (object, size, const0_rtx,
2799 method == BLOCK_OP_TAILCALL);
2800 else
2801 gcc_unreachable ();
2803 return NULL;
2807 clear_storage (rtx object, rtx size, enum block_op_methods method)
2809 unsigned HOST_WIDE_INT max, min = 0;
2810 if (GET_CODE (size) == CONST_INT)
2811 min = max = UINTVAL (size);
2812 else
2813 max = GET_MODE_MASK (GET_MODE (size));
2814 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2818 /* A subroutine of clear_storage. Expand a call to memset.
2819 Return the return value of memset, 0 otherwise. */
2822 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2824 tree call_expr, fn, object_tree, size_tree, val_tree;
2825 machine_mode size_mode;
2826 rtx retval;
2828 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2829 place those into new pseudos into a VAR_DECL and use them later. */
2831 object = copy_addr_to_reg (XEXP (object, 0));
2833 size_mode = TYPE_MODE (sizetype);
2834 size = convert_to_mode (size_mode, size, 1);
2835 size = copy_to_mode_reg (size_mode, size);
2837 /* It is incorrect to use the libcall calling conventions to call
2838 memset in this context. This could be a user call to memset and
2839 the user may wish to examine the return value from memset. For
2840 targets where libcalls and normal calls have different conventions
2841 for returning pointers, we could end up generating incorrect code. */
2843 object_tree = make_tree (ptr_type_node, object);
2844 if (!CONST_INT_P (val))
2845 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2846 size_tree = make_tree (sizetype, size);
2847 val_tree = make_tree (integer_type_node, val);
2849 fn = clear_storage_libcall_fn (true);
2850 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2851 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2853 retval = expand_normal (call_expr);
2855 return retval;
2858 /* A subroutine of set_storage_via_libcall. Create the tree node
2859 for the function we use for block clears. */
2861 tree block_clear_fn;
2863 void
2864 init_block_clear_fn (const char *asmspec)
2866 if (!block_clear_fn)
2868 tree fn, args;
2870 fn = get_identifier ("memset");
2871 args = build_function_type_list (ptr_type_node, ptr_type_node,
2872 integer_type_node, sizetype,
2873 NULL_TREE);
2875 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2876 DECL_EXTERNAL (fn) = 1;
2877 TREE_PUBLIC (fn) = 1;
2878 DECL_ARTIFICIAL (fn) = 1;
2879 TREE_NOTHROW (fn) = 1;
2880 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2881 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2883 block_clear_fn = fn;
2886 if (asmspec)
2887 set_user_assembler_name (block_clear_fn, asmspec);
2890 static tree
2891 clear_storage_libcall_fn (int for_call)
2893 static bool emitted_extern;
2895 if (!block_clear_fn)
2896 init_block_clear_fn (NULL);
2898 if (for_call && !emitted_extern)
2900 emitted_extern = true;
2901 make_decl_rtl (block_clear_fn);
2904 return block_clear_fn;
2907 /* Expand a setmem pattern; return true if successful. */
2909 bool
2910 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2911 unsigned int expected_align, HOST_WIDE_INT expected_size,
2912 unsigned HOST_WIDE_INT min_size,
2913 unsigned HOST_WIDE_INT max_size,
2914 unsigned HOST_WIDE_INT probable_max_size)
2916 /* Try the most limited insn first, because there's no point
2917 including more than one in the machine description unless
2918 the more limited one has some advantage. */
2920 machine_mode mode;
2922 if (expected_align < align)
2923 expected_align = align;
2924 if (expected_size != -1)
2926 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2927 expected_size = max_size;
2928 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2929 expected_size = min_size;
2932 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2933 mode = GET_MODE_WIDER_MODE (mode))
2935 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2937 if (code != CODE_FOR_nothing
2938 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2939 here because if SIZE is less than the mode mask, as it is
2940 returned by the macro, it will definitely be less than the
2941 actual mode mask. Since SIZE is within the Pmode address
2942 space, we limit MODE to Pmode. */
2943 && ((CONST_INT_P (size)
2944 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2945 <= (GET_MODE_MASK (mode) >> 1)))
2946 || max_size <= (GET_MODE_MASK (mode) >> 1)
2947 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2949 struct expand_operand ops[9];
2950 unsigned int nops;
2952 nops = insn_data[(int) code].n_generator_args;
2953 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2955 create_fixed_operand (&ops[0], object);
2956 /* The check above guarantees that this size conversion is valid. */
2957 create_convert_operand_to (&ops[1], size, mode, true);
2958 create_convert_operand_from (&ops[2], val, byte_mode, true);
2959 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2960 if (nops >= 6)
2962 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2963 create_integer_operand (&ops[5], expected_size);
2965 if (nops >= 8)
2967 create_integer_operand (&ops[6], min_size);
2968 /* If we can not represent the maximal size,
2969 make parameter NULL. */
2970 if ((HOST_WIDE_INT) max_size != -1)
2971 create_integer_operand (&ops[7], max_size);
2972 else
2973 create_fixed_operand (&ops[7], NULL);
2975 if (nops == 9)
2977 /* If we can not represent the maximal size,
2978 make parameter NULL. */
2979 if ((HOST_WIDE_INT) probable_max_size != -1)
2980 create_integer_operand (&ops[8], probable_max_size);
2981 else
2982 create_fixed_operand (&ops[8], NULL);
2984 if (maybe_expand_insn (code, nops, ops))
2985 return true;
2989 return false;
2993 /* Write to one of the components of the complex value CPLX. Write VAL to
2994 the real part if IMAG_P is false, and the imaginary part if its true. */
2996 void
2997 write_complex_part (rtx cplx, rtx val, bool imag_p)
2999 machine_mode cmode;
3000 machine_mode imode;
3001 unsigned ibitsize;
3003 if (GET_CODE (cplx) == CONCAT)
3005 emit_move_insn (XEXP (cplx, imag_p), val);
3006 return;
3009 cmode = GET_MODE (cplx);
3010 imode = GET_MODE_INNER (cmode);
3011 ibitsize = GET_MODE_BITSIZE (imode);
3013 /* For MEMs simplify_gen_subreg may generate an invalid new address
3014 because, e.g., the original address is considered mode-dependent
3015 by the target, which restricts simplify_subreg from invoking
3016 adjust_address_nv. Instead of preparing fallback support for an
3017 invalid address, we call adjust_address_nv directly. */
3018 if (MEM_P (cplx))
3020 emit_move_insn (adjust_address_nv (cplx, imode,
3021 imag_p ? GET_MODE_SIZE (imode) : 0),
3022 val);
3023 return;
3026 /* If the sub-object is at least word sized, then we know that subregging
3027 will work. This special case is important, since store_bit_field
3028 wants to operate on integer modes, and there's rarely an OImode to
3029 correspond to TCmode. */
3030 if (ibitsize >= BITS_PER_WORD
3031 /* For hard regs we have exact predicates. Assume we can split
3032 the original object if it spans an even number of hard regs.
3033 This special case is important for SCmode on 64-bit platforms
3034 where the natural size of floating-point regs is 32-bit. */
3035 || (REG_P (cplx)
3036 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3037 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3039 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3040 imag_p ? GET_MODE_SIZE (imode) : 0);
3041 if (part)
3043 emit_move_insn (part, val);
3044 return;
3046 else
3047 /* simplify_gen_subreg may fail for sub-word MEMs. */
3048 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3051 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3054 /* Extract one of the components of the complex value CPLX. Extract the
3055 real part if IMAG_P is false, and the imaginary part if it's true. */
3057 static rtx
3058 read_complex_part (rtx cplx, bool imag_p)
3060 machine_mode cmode, imode;
3061 unsigned ibitsize;
3063 if (GET_CODE (cplx) == CONCAT)
3064 return XEXP (cplx, imag_p);
3066 cmode = GET_MODE (cplx);
3067 imode = GET_MODE_INNER (cmode);
3068 ibitsize = GET_MODE_BITSIZE (imode);
3070 /* Special case reads from complex constants that got spilled to memory. */
3071 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3073 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3074 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3076 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3077 if (CONSTANT_CLASS_P (part))
3078 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3082 /* For MEMs simplify_gen_subreg may generate an invalid new address
3083 because, e.g., the original address is considered mode-dependent
3084 by the target, which restricts simplify_subreg from invoking
3085 adjust_address_nv. Instead of preparing fallback support for an
3086 invalid address, we call adjust_address_nv directly. */
3087 if (MEM_P (cplx))
3088 return adjust_address_nv (cplx, imode,
3089 imag_p ? GET_MODE_SIZE (imode) : 0);
3091 /* If the sub-object is at least word sized, then we know that subregging
3092 will work. This special case is important, since extract_bit_field
3093 wants to operate on integer modes, and there's rarely an OImode to
3094 correspond to TCmode. */
3095 if (ibitsize >= BITS_PER_WORD
3096 /* For hard regs we have exact predicates. Assume we can split
3097 the original object if it spans an even number of hard regs.
3098 This special case is important for SCmode on 64-bit platforms
3099 where the natural size of floating-point regs is 32-bit. */
3100 || (REG_P (cplx)
3101 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3102 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3104 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3105 imag_p ? GET_MODE_SIZE (imode) : 0);
3106 if (ret)
3107 return ret;
3108 else
3109 /* simplify_gen_subreg may fail for sub-word MEMs. */
3110 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3113 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3114 true, NULL_RTX, imode, imode);
3117 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3118 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3119 represented in NEW_MODE. If FORCE is true, this will never happen, as
3120 we'll force-create a SUBREG if needed. */
3122 static rtx
3123 emit_move_change_mode (machine_mode new_mode,
3124 machine_mode old_mode, rtx x, bool force)
3126 rtx ret;
3128 if (push_operand (x, GET_MODE (x)))
3130 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3131 MEM_COPY_ATTRIBUTES (ret, x);
3133 else if (MEM_P (x))
3135 /* We don't have to worry about changing the address since the
3136 size in bytes is supposed to be the same. */
3137 if (reload_in_progress)
3139 /* Copy the MEM to change the mode and move any
3140 substitutions from the old MEM to the new one. */
3141 ret = adjust_address_nv (x, new_mode, 0);
3142 copy_replacements (x, ret);
3144 else
3145 ret = adjust_address (x, new_mode, 0);
3147 else
3149 /* Note that we do want simplify_subreg's behavior of validating
3150 that the new mode is ok for a hard register. If we were to use
3151 simplify_gen_subreg, we would create the subreg, but would
3152 probably run into the target not being able to implement it. */
3153 /* Except, of course, when FORCE is true, when this is exactly what
3154 we want. Which is needed for CCmodes on some targets. */
3155 if (force)
3156 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3157 else
3158 ret = simplify_subreg (new_mode, x, old_mode, 0);
3161 return ret;
3164 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3165 an integer mode of the same size as MODE. Returns the instruction
3166 emitted, or NULL if such a move could not be generated. */
3168 static rtx_insn *
3169 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3171 machine_mode imode;
3172 enum insn_code code;
3174 /* There must exist a mode of the exact size we require. */
3175 imode = int_mode_for_mode (mode);
3176 if (imode == BLKmode)
3177 return NULL;
3179 /* The target must support moves in this mode. */
3180 code = optab_handler (mov_optab, imode);
3181 if (code == CODE_FOR_nothing)
3182 return NULL;
3184 x = emit_move_change_mode (imode, mode, x, force);
3185 if (x == NULL_RTX)
3186 return NULL;
3187 y = emit_move_change_mode (imode, mode, y, force);
3188 if (y == NULL_RTX)
3189 return NULL;
3190 return emit_insn (GEN_FCN (code) (x, y));
3193 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3194 Return an equivalent MEM that does not use an auto-increment. */
3197 emit_move_resolve_push (machine_mode mode, rtx x)
3199 enum rtx_code code = GET_CODE (XEXP (x, 0));
3200 HOST_WIDE_INT adjust;
3201 rtx temp;
3203 adjust = GET_MODE_SIZE (mode);
3204 #ifdef PUSH_ROUNDING
3205 adjust = PUSH_ROUNDING (adjust);
3206 #endif
3207 if (code == PRE_DEC || code == POST_DEC)
3208 adjust = -adjust;
3209 else if (code == PRE_MODIFY || code == POST_MODIFY)
3211 rtx expr = XEXP (XEXP (x, 0), 1);
3212 HOST_WIDE_INT val;
3214 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3215 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3216 val = INTVAL (XEXP (expr, 1));
3217 if (GET_CODE (expr) == MINUS)
3218 val = -val;
3219 gcc_assert (adjust == val || adjust == -val);
3220 adjust = val;
3223 /* Do not use anti_adjust_stack, since we don't want to update
3224 stack_pointer_delta. */
3225 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3226 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3227 0, OPTAB_LIB_WIDEN);
3228 if (temp != stack_pointer_rtx)
3229 emit_move_insn (stack_pointer_rtx, temp);
3231 switch (code)
3233 case PRE_INC:
3234 case PRE_DEC:
3235 case PRE_MODIFY:
3236 temp = stack_pointer_rtx;
3237 break;
3238 case POST_INC:
3239 case POST_DEC:
3240 case POST_MODIFY:
3241 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3242 break;
3243 default:
3244 gcc_unreachable ();
3247 return replace_equiv_address (x, temp);
3250 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3251 X is known to satisfy push_operand, and MODE is known to be complex.
3252 Returns the last instruction emitted. */
3254 rtx_insn *
3255 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3257 machine_mode submode = GET_MODE_INNER (mode);
3258 bool imag_first;
3260 #ifdef PUSH_ROUNDING
3261 unsigned int submodesize = GET_MODE_SIZE (submode);
3263 /* In case we output to the stack, but the size is smaller than the
3264 machine can push exactly, we need to use move instructions. */
3265 if (PUSH_ROUNDING (submodesize) != submodesize)
3267 x = emit_move_resolve_push (mode, x);
3268 return emit_move_insn (x, y);
3270 #endif
3272 /* Note that the real part always precedes the imag part in memory
3273 regardless of machine's endianness. */
3274 switch (GET_CODE (XEXP (x, 0)))
3276 case PRE_DEC:
3277 case POST_DEC:
3278 imag_first = true;
3279 break;
3280 case PRE_INC:
3281 case POST_INC:
3282 imag_first = false;
3283 break;
3284 default:
3285 gcc_unreachable ();
3288 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3289 read_complex_part (y, imag_first));
3290 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3291 read_complex_part (y, !imag_first));
3294 /* A subroutine of emit_move_complex. Perform the move from Y to X
3295 via two moves of the parts. Returns the last instruction emitted. */
3297 rtx_insn *
3298 emit_move_complex_parts (rtx x, rtx y)
3300 /* Show the output dies here. This is necessary for SUBREGs
3301 of pseudos since we cannot track their lifetimes correctly;
3302 hard regs shouldn't appear here except as return values. */
3303 if (!reload_completed && !reload_in_progress
3304 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3305 emit_clobber (x);
3307 write_complex_part (x, read_complex_part (y, false), false);
3308 write_complex_part (x, read_complex_part (y, true), true);
3310 return get_last_insn ();
3313 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3314 MODE is known to be complex. Returns the last instruction emitted. */
3316 static rtx_insn *
3317 emit_move_complex (machine_mode mode, rtx x, rtx y)
3319 bool try_int;
3321 /* Need to take special care for pushes, to maintain proper ordering
3322 of the data, and possibly extra padding. */
3323 if (push_operand (x, mode))
3324 return emit_move_complex_push (mode, x, y);
3326 /* See if we can coerce the target into moving both values at once, except
3327 for floating point where we favor moving as parts if this is easy. */
3328 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3329 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3330 && !(REG_P (x)
3331 && HARD_REGISTER_P (x)
3332 && hard_regno_nregs[REGNO (x)][mode] == 1)
3333 && !(REG_P (y)
3334 && HARD_REGISTER_P (y)
3335 && hard_regno_nregs[REGNO (y)][mode] == 1))
3336 try_int = false;
3337 /* Not possible if the values are inherently not adjacent. */
3338 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3339 try_int = false;
3340 /* Is possible if both are registers (or subregs of registers). */
3341 else if (register_operand (x, mode) && register_operand (y, mode))
3342 try_int = true;
3343 /* If one of the operands is a memory, and alignment constraints
3344 are friendly enough, we may be able to do combined memory operations.
3345 We do not attempt this if Y is a constant because that combination is
3346 usually better with the by-parts thing below. */
3347 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3348 && (!STRICT_ALIGNMENT
3349 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3350 try_int = true;
3351 else
3352 try_int = false;
3354 if (try_int)
3356 rtx_insn *ret;
3358 /* For memory to memory moves, optimal behavior can be had with the
3359 existing block move logic. */
3360 if (MEM_P (x) && MEM_P (y))
3362 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3363 BLOCK_OP_NO_LIBCALL);
3364 return get_last_insn ();
3367 ret = emit_move_via_integer (mode, x, y, true);
3368 if (ret)
3369 return ret;
3372 return emit_move_complex_parts (x, y);
3375 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3376 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3378 static rtx_insn *
3379 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3381 rtx_insn *ret;
3383 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3384 if (mode != CCmode)
3386 enum insn_code code = optab_handler (mov_optab, CCmode);
3387 if (code != CODE_FOR_nothing)
3389 x = emit_move_change_mode (CCmode, mode, x, true);
3390 y = emit_move_change_mode (CCmode, mode, y, true);
3391 return emit_insn (GEN_FCN (code) (x, y));
3395 /* Otherwise, find the MODE_INT mode of the same width. */
3396 ret = emit_move_via_integer (mode, x, y, false);
3397 gcc_assert (ret != NULL);
3398 return ret;
3401 /* Return true if word I of OP lies entirely in the
3402 undefined bits of a paradoxical subreg. */
3404 static bool
3405 undefined_operand_subword_p (const_rtx op, int i)
3407 machine_mode innermode, innermostmode;
3408 int offset;
3409 if (GET_CODE (op) != SUBREG)
3410 return false;
3411 innermode = GET_MODE (op);
3412 innermostmode = GET_MODE (SUBREG_REG (op));
3413 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3414 /* The SUBREG_BYTE represents offset, as if the value were stored in
3415 memory, except for a paradoxical subreg where we define
3416 SUBREG_BYTE to be 0; undo this exception as in
3417 simplify_subreg. */
3418 if (SUBREG_BYTE (op) == 0
3419 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3421 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3422 if (WORDS_BIG_ENDIAN)
3423 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3424 if (BYTES_BIG_ENDIAN)
3425 offset += difference % UNITS_PER_WORD;
3427 if (offset >= GET_MODE_SIZE (innermostmode)
3428 || offset <= -GET_MODE_SIZE (word_mode))
3429 return true;
3430 return false;
3433 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3434 MODE is any multi-word or full-word mode that lacks a move_insn
3435 pattern. Note that you will get better code if you define such
3436 patterns, even if they must turn into multiple assembler instructions. */
3438 static rtx_insn *
3439 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3441 rtx_insn *last_insn = 0;
3442 rtx_insn *seq;
3443 rtx inner;
3444 bool need_clobber;
3445 int i;
3447 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3449 /* If X is a push on the stack, do the push now and replace
3450 X with a reference to the stack pointer. */
3451 if (push_operand (x, mode))
3452 x = emit_move_resolve_push (mode, x);
3454 /* If we are in reload, see if either operand is a MEM whose address
3455 is scheduled for replacement. */
3456 if (reload_in_progress && MEM_P (x)
3457 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3458 x = replace_equiv_address_nv (x, inner);
3459 if (reload_in_progress && MEM_P (y)
3460 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3461 y = replace_equiv_address_nv (y, inner);
3463 start_sequence ();
3465 need_clobber = false;
3466 for (i = 0;
3467 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3468 i++)
3470 rtx xpart = operand_subword (x, i, 1, mode);
3471 rtx ypart;
3473 /* Do not generate code for a move if it would come entirely
3474 from the undefined bits of a paradoxical subreg. */
3475 if (undefined_operand_subword_p (y, i))
3476 continue;
3478 ypart = operand_subword (y, i, 1, mode);
3480 /* If we can't get a part of Y, put Y into memory if it is a
3481 constant. Otherwise, force it into a register. Then we must
3482 be able to get a part of Y. */
3483 if (ypart == 0 && CONSTANT_P (y))
3485 y = use_anchored_address (force_const_mem (mode, y));
3486 ypart = operand_subword (y, i, 1, mode);
3488 else if (ypart == 0)
3489 ypart = operand_subword_force (y, i, mode);
3491 gcc_assert (xpart && ypart);
3493 need_clobber |= (GET_CODE (xpart) == SUBREG);
3495 last_insn = emit_move_insn (xpart, ypart);
3498 seq = get_insns ();
3499 end_sequence ();
3501 /* Show the output dies here. This is necessary for SUBREGs
3502 of pseudos since we cannot track their lifetimes correctly;
3503 hard regs shouldn't appear here except as return values.
3504 We never want to emit such a clobber after reload. */
3505 if (x != y
3506 && ! (reload_in_progress || reload_completed)
3507 && need_clobber != 0)
3508 emit_clobber (x);
3510 emit_insn (seq);
3512 return last_insn;
3515 /* Low level part of emit_move_insn.
3516 Called just like emit_move_insn, but assumes X and Y
3517 are basically valid. */
3519 rtx_insn *
3520 emit_move_insn_1 (rtx x, rtx y)
3522 machine_mode mode = GET_MODE (x);
3523 enum insn_code code;
3525 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3527 code = optab_handler (mov_optab, mode);
3528 if (code != CODE_FOR_nothing)
3529 return emit_insn (GEN_FCN (code) (x, y));
3531 /* Expand complex moves by moving real part and imag part. */
3532 if (COMPLEX_MODE_P (mode))
3533 return emit_move_complex (mode, x, y);
3535 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3536 || ALL_FIXED_POINT_MODE_P (mode))
3538 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3540 /* If we can't find an integer mode, use multi words. */
3541 if (result)
3542 return result;
3543 else
3544 return emit_move_multi_word (mode, x, y);
3547 if (GET_MODE_CLASS (mode) == MODE_CC)
3548 return emit_move_ccmode (mode, x, y);
3550 /* Try using a move pattern for the corresponding integer mode. This is
3551 only safe when simplify_subreg can convert MODE constants into integer
3552 constants. At present, it can only do this reliably if the value
3553 fits within a HOST_WIDE_INT. */
3554 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3556 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3558 if (ret)
3560 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3561 return ret;
3565 return emit_move_multi_word (mode, x, y);
3568 /* Generate code to copy Y into X.
3569 Both Y and X must have the same mode, except that
3570 Y can be a constant with VOIDmode.
3571 This mode cannot be BLKmode; use emit_block_move for that.
3573 Return the last instruction emitted. */
3575 rtx_insn *
3576 emit_move_insn (rtx x, rtx y)
3578 machine_mode mode = GET_MODE (x);
3579 rtx y_cst = NULL_RTX;
3580 rtx_insn *last_insn;
3581 rtx set;
3583 gcc_assert (mode != BLKmode
3584 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3586 if (CONSTANT_P (y))
3588 if (optimize
3589 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3590 && (last_insn = compress_float_constant (x, y)))
3591 return last_insn;
3593 y_cst = y;
3595 if (!targetm.legitimate_constant_p (mode, y))
3597 y = force_const_mem (mode, y);
3599 /* If the target's cannot_force_const_mem prevented the spill,
3600 assume that the target's move expanders will also take care
3601 of the non-legitimate constant. */
3602 if (!y)
3603 y = y_cst;
3604 else
3605 y = use_anchored_address (y);
3609 /* If X or Y are memory references, verify that their addresses are valid
3610 for the machine. */
3611 if (MEM_P (x)
3612 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3613 MEM_ADDR_SPACE (x))
3614 && ! push_operand (x, GET_MODE (x))))
3615 x = validize_mem (x);
3617 if (MEM_P (y)
3618 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3619 MEM_ADDR_SPACE (y)))
3620 y = validize_mem (y);
3622 gcc_assert (mode != BLKmode);
3624 last_insn = emit_move_insn_1 (x, y);
3626 if (y_cst && REG_P (x)
3627 && (set = single_set (last_insn)) != NULL_RTX
3628 && SET_DEST (set) == x
3629 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3630 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3632 return last_insn;
3635 /* Generate the body of an instruction to copy Y into X.
3636 It may be a list of insns, if one insn isn't enough. */
3639 gen_move_insn (rtx x, rtx y)
3641 rtx_insn *seq;
3643 start_sequence ();
3644 emit_move_insn_1 (x, y);
3645 seq = get_insns ();
3646 end_sequence ();
3647 return seq;
3650 /* If Y is representable exactly in a narrower mode, and the target can
3651 perform the extension directly from constant or memory, then emit the
3652 move as an extension. */
3654 static rtx_insn *
3655 compress_float_constant (rtx x, rtx y)
3657 machine_mode dstmode = GET_MODE (x);
3658 machine_mode orig_srcmode = GET_MODE (y);
3659 machine_mode srcmode;
3660 REAL_VALUE_TYPE r;
3661 int oldcost, newcost;
3662 bool speed = optimize_insn_for_speed_p ();
3664 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3666 if (targetm.legitimate_constant_p (dstmode, y))
3667 oldcost = set_src_cost (y, speed);
3668 else
3669 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3671 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3672 srcmode != orig_srcmode;
3673 srcmode = GET_MODE_WIDER_MODE (srcmode))
3675 enum insn_code ic;
3676 rtx trunc_y;
3677 rtx_insn *last_insn;
3679 /* Skip if the target can't extend this way. */
3680 ic = can_extend_p (dstmode, srcmode, 0);
3681 if (ic == CODE_FOR_nothing)
3682 continue;
3684 /* Skip if the narrowed value isn't exact. */
3685 if (! exact_real_truncate (srcmode, &r))
3686 continue;
3688 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3690 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3692 /* Skip if the target needs extra instructions to perform
3693 the extension. */
3694 if (!insn_operand_matches (ic, 1, trunc_y))
3695 continue;
3696 /* This is valid, but may not be cheaper than the original. */
3697 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3698 speed);
3699 if (oldcost < newcost)
3700 continue;
3702 else if (float_extend_from_mem[dstmode][srcmode])
3704 trunc_y = force_const_mem (srcmode, trunc_y);
3705 /* This is valid, but may not be cheaper than the original. */
3706 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3707 speed);
3708 if (oldcost < newcost)
3709 continue;
3710 trunc_y = validize_mem (trunc_y);
3712 else
3713 continue;
3715 /* For CSE's benefit, force the compressed constant pool entry
3716 into a new pseudo. This constant may be used in different modes,
3717 and if not, combine will put things back together for us. */
3718 trunc_y = force_reg (srcmode, trunc_y);
3720 /* If x is a hard register, perform the extension into a pseudo,
3721 so that e.g. stack realignment code is aware of it. */
3722 rtx target = x;
3723 if (REG_P (x) && HARD_REGISTER_P (x))
3724 target = gen_reg_rtx (dstmode);
3726 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3727 last_insn = get_last_insn ();
3729 if (REG_P (target))
3730 set_unique_reg_note (last_insn, REG_EQUAL, y);
3732 if (target != x)
3733 return emit_move_insn (x, target);
3734 return last_insn;
3737 return NULL;
3740 /* Pushing data onto the stack. */
3742 /* Push a block of length SIZE (perhaps variable)
3743 and return an rtx to address the beginning of the block.
3744 The value may be virtual_outgoing_args_rtx.
3746 EXTRA is the number of bytes of padding to push in addition to SIZE.
3747 BELOW nonzero means this padding comes at low addresses;
3748 otherwise, the padding comes at high addresses. */
3751 push_block (rtx size, int extra, int below)
3753 rtx temp;
3755 size = convert_modes (Pmode, ptr_mode, size, 1);
3756 if (CONSTANT_P (size))
3757 anti_adjust_stack (plus_constant (Pmode, size, extra));
3758 else if (REG_P (size) && extra == 0)
3759 anti_adjust_stack (size);
3760 else
3762 temp = copy_to_mode_reg (Pmode, size);
3763 if (extra != 0)
3764 temp = expand_binop (Pmode, add_optab, temp,
3765 gen_int_mode (extra, Pmode),
3766 temp, 0, OPTAB_LIB_WIDEN);
3767 anti_adjust_stack (temp);
3770 #ifndef STACK_GROWS_DOWNWARD
3771 if (0)
3772 #else
3773 if (1)
3774 #endif
3776 temp = virtual_outgoing_args_rtx;
3777 if (extra != 0 && below)
3778 temp = plus_constant (Pmode, temp, extra);
3780 else
3782 if (CONST_INT_P (size))
3783 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3784 -INTVAL (size) - (below ? 0 : extra));
3785 else if (extra != 0 && !below)
3786 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3787 negate_rtx (Pmode, plus_constant (Pmode, size,
3788 extra)));
3789 else
3790 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3791 negate_rtx (Pmode, size));
3794 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3797 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3799 static rtx
3800 mem_autoinc_base (rtx mem)
3802 if (MEM_P (mem))
3804 rtx addr = XEXP (mem, 0);
3805 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3806 return XEXP (addr, 0);
3808 return NULL;
3811 /* A utility routine used here, in reload, and in try_split. The insns
3812 after PREV up to and including LAST are known to adjust the stack,
3813 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3814 placing notes as appropriate. PREV may be NULL, indicating the
3815 entire insn sequence prior to LAST should be scanned.
3817 The set of allowed stack pointer modifications is small:
3818 (1) One or more auto-inc style memory references (aka pushes),
3819 (2) One or more addition/subtraction with the SP as destination,
3820 (3) A single move insn with the SP as destination,
3821 (4) A call_pop insn,
3822 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3824 Insns in the sequence that do not modify the SP are ignored,
3825 except for noreturn calls.
3827 The return value is the amount of adjustment that can be trivially
3828 verified, via immediate operand or auto-inc. If the adjustment
3829 cannot be trivially extracted, the return value is INT_MIN. */
3831 HOST_WIDE_INT
3832 find_args_size_adjust (rtx_insn *insn)
3834 rtx dest, set, pat;
3835 int i;
3837 pat = PATTERN (insn);
3838 set = NULL;
3840 /* Look for a call_pop pattern. */
3841 if (CALL_P (insn))
3843 /* We have to allow non-call_pop patterns for the case
3844 of emit_single_push_insn of a TLS address. */
3845 if (GET_CODE (pat) != PARALLEL)
3846 return 0;
3848 /* All call_pop have a stack pointer adjust in the parallel.
3849 The call itself is always first, and the stack adjust is
3850 usually last, so search from the end. */
3851 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3853 set = XVECEXP (pat, 0, i);
3854 if (GET_CODE (set) != SET)
3855 continue;
3856 dest = SET_DEST (set);
3857 if (dest == stack_pointer_rtx)
3858 break;
3860 /* We'd better have found the stack pointer adjust. */
3861 if (i == 0)
3862 return 0;
3863 /* Fall through to process the extracted SET and DEST
3864 as if it was a standalone insn. */
3866 else if (GET_CODE (pat) == SET)
3867 set = pat;
3868 else if ((set = single_set (insn)) != NULL)
3870 else if (GET_CODE (pat) == PARALLEL)
3872 /* ??? Some older ports use a parallel with a stack adjust
3873 and a store for a PUSH_ROUNDING pattern, rather than a
3874 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3875 /* ??? See h8300 and m68k, pushqi1. */
3876 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3878 set = XVECEXP (pat, 0, i);
3879 if (GET_CODE (set) != SET)
3880 continue;
3881 dest = SET_DEST (set);
3882 if (dest == stack_pointer_rtx)
3883 break;
3885 /* We do not expect an auto-inc of the sp in the parallel. */
3886 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3887 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3888 != stack_pointer_rtx);
3890 if (i < 0)
3891 return 0;
3893 else
3894 return 0;
3896 dest = SET_DEST (set);
3898 /* Look for direct modifications of the stack pointer. */
3899 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3901 /* Look for a trivial adjustment, otherwise assume nothing. */
3902 /* Note that the SPU restore_stack_block pattern refers to
3903 the stack pointer in V4SImode. Consider that non-trivial. */
3904 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3905 && GET_CODE (SET_SRC (set)) == PLUS
3906 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3907 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3908 return INTVAL (XEXP (SET_SRC (set), 1));
3909 /* ??? Reload can generate no-op moves, which will be cleaned
3910 up later. Recognize it and continue searching. */
3911 else if (rtx_equal_p (dest, SET_SRC (set)))
3912 return 0;
3913 else
3914 return HOST_WIDE_INT_MIN;
3916 else
3918 rtx mem, addr;
3920 /* Otherwise only think about autoinc patterns. */
3921 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3923 mem = dest;
3924 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3925 != stack_pointer_rtx);
3927 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3928 mem = SET_SRC (set);
3929 else
3930 return 0;
3932 addr = XEXP (mem, 0);
3933 switch (GET_CODE (addr))
3935 case PRE_INC:
3936 case POST_INC:
3937 return GET_MODE_SIZE (GET_MODE (mem));
3938 case PRE_DEC:
3939 case POST_DEC:
3940 return -GET_MODE_SIZE (GET_MODE (mem));
3941 case PRE_MODIFY:
3942 case POST_MODIFY:
3943 addr = XEXP (addr, 1);
3944 gcc_assert (GET_CODE (addr) == PLUS);
3945 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3946 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3947 return INTVAL (XEXP (addr, 1));
3948 default:
3949 gcc_unreachable ();
3955 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3957 int args_size = end_args_size;
3958 bool saw_unknown = false;
3959 rtx_insn *insn;
3961 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3963 HOST_WIDE_INT this_delta;
3965 if (!NONDEBUG_INSN_P (insn))
3966 continue;
3968 this_delta = find_args_size_adjust (insn);
3969 if (this_delta == 0)
3971 if (!CALL_P (insn)
3972 || ACCUMULATE_OUTGOING_ARGS
3973 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3974 continue;
3977 gcc_assert (!saw_unknown);
3978 if (this_delta == HOST_WIDE_INT_MIN)
3979 saw_unknown = true;
3981 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3982 #ifdef STACK_GROWS_DOWNWARD
3983 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3984 #endif
3985 args_size -= this_delta;
3988 return saw_unknown ? INT_MIN : args_size;
3991 #ifdef PUSH_ROUNDING
3992 /* Emit single push insn. */
3994 static void
3995 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
3997 rtx dest_addr;
3998 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3999 rtx dest;
4000 enum insn_code icode;
4002 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4003 /* If there is push pattern, use it. Otherwise try old way of throwing
4004 MEM representing push operation to move expander. */
4005 icode = optab_handler (push_optab, mode);
4006 if (icode != CODE_FOR_nothing)
4008 struct expand_operand ops[1];
4010 create_input_operand (&ops[0], x, mode);
4011 if (maybe_expand_insn (icode, 1, ops))
4012 return;
4014 if (GET_MODE_SIZE (mode) == rounded_size)
4015 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4016 /* If we are to pad downward, adjust the stack pointer first and
4017 then store X into the stack location using an offset. This is
4018 because emit_move_insn does not know how to pad; it does not have
4019 access to type. */
4020 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4022 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4023 HOST_WIDE_INT offset;
4025 emit_move_insn (stack_pointer_rtx,
4026 expand_binop (Pmode,
4027 #ifdef STACK_GROWS_DOWNWARD
4028 sub_optab,
4029 #else
4030 add_optab,
4031 #endif
4032 stack_pointer_rtx,
4033 gen_int_mode (rounded_size, Pmode),
4034 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4036 offset = (HOST_WIDE_INT) padding_size;
4037 #ifdef STACK_GROWS_DOWNWARD
4038 if (STACK_PUSH_CODE == POST_DEC)
4039 /* We have already decremented the stack pointer, so get the
4040 previous value. */
4041 offset += (HOST_WIDE_INT) rounded_size;
4042 #else
4043 if (STACK_PUSH_CODE == POST_INC)
4044 /* We have already incremented the stack pointer, so get the
4045 previous value. */
4046 offset -= (HOST_WIDE_INT) rounded_size;
4047 #endif
4048 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4049 gen_int_mode (offset, Pmode));
4051 else
4053 #ifdef STACK_GROWS_DOWNWARD
4054 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4055 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4056 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4057 Pmode));
4058 #else
4059 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4060 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4061 gen_int_mode (rounded_size, Pmode));
4062 #endif
4063 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4066 dest = gen_rtx_MEM (mode, dest_addr);
4068 if (type != 0)
4070 set_mem_attributes (dest, type, 1);
4072 if (cfun->tail_call_marked)
4073 /* Function incoming arguments may overlap with sibling call
4074 outgoing arguments and we cannot allow reordering of reads
4075 from function arguments with stores to outgoing arguments
4076 of sibling calls. */
4077 set_mem_alias_set (dest, 0);
4079 emit_move_insn (dest, x);
4082 /* Emit and annotate a single push insn. */
4084 static void
4085 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4087 int delta, old_delta = stack_pointer_delta;
4088 rtx_insn *prev = get_last_insn ();
4089 rtx_insn *last;
4091 emit_single_push_insn_1 (mode, x, type);
4093 last = get_last_insn ();
4095 /* Notice the common case where we emitted exactly one insn. */
4096 if (PREV_INSN (last) == prev)
4098 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4099 return;
4102 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4103 gcc_assert (delta == INT_MIN || delta == old_delta);
4105 #endif
4107 /* Generate code to push X onto the stack, assuming it has mode MODE and
4108 type TYPE.
4109 MODE is redundant except when X is a CONST_INT (since they don't
4110 carry mode info).
4111 SIZE is an rtx for the size of data to be copied (in bytes),
4112 needed only if X is BLKmode.
4114 ALIGN (in bits) is maximum alignment we can assume.
4116 If PARTIAL and REG are both nonzero, then copy that many of the first
4117 bytes of X into registers starting with REG, and push the rest of X.
4118 The amount of space pushed is decreased by PARTIAL bytes.
4119 REG must be a hard register in this case.
4120 If REG is zero but PARTIAL is not, take any all others actions for an
4121 argument partially in registers, but do not actually load any
4122 registers.
4124 EXTRA is the amount in bytes of extra space to leave next to this arg.
4125 This is ignored if an argument block has already been allocated.
4127 On a machine that lacks real push insns, ARGS_ADDR is the address of
4128 the bottom of the argument block for this call. We use indexing off there
4129 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4130 argument block has not been preallocated.
4132 ARGS_SO_FAR is the size of args previously pushed for this call.
4134 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4135 for arguments passed in registers. If nonzero, it will be the number
4136 of bytes required. */
4138 void
4139 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4140 unsigned int align, int partial, rtx reg, int extra,
4141 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4142 rtx alignment_pad)
4144 rtx xinner;
4145 enum direction stack_direction
4146 #ifdef STACK_GROWS_DOWNWARD
4147 = downward;
4148 #else
4149 = upward;
4150 #endif
4152 /* Decide where to pad the argument: `downward' for below,
4153 `upward' for above, or `none' for don't pad it.
4154 Default is below for small data on big-endian machines; else above. */
4155 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4157 /* Invert direction if stack is post-decrement.
4158 FIXME: why? */
4159 if (STACK_PUSH_CODE == POST_DEC)
4160 if (where_pad != none)
4161 where_pad = (where_pad == downward ? upward : downward);
4163 xinner = x;
4165 if (mode == BLKmode
4166 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4168 /* Copy a block into the stack, entirely or partially. */
4170 rtx temp;
4171 int used;
4172 int offset;
4173 int skip;
4175 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4176 used = partial - offset;
4178 if (mode != BLKmode)
4180 /* A value is to be stored in an insufficiently aligned
4181 stack slot; copy via a suitably aligned slot if
4182 necessary. */
4183 size = GEN_INT (GET_MODE_SIZE (mode));
4184 if (!MEM_P (xinner))
4186 temp = assign_temp (type, 1, 1);
4187 emit_move_insn (temp, xinner);
4188 xinner = temp;
4192 gcc_assert (size);
4194 /* USED is now the # of bytes we need not copy to the stack
4195 because registers will take care of them. */
4197 if (partial != 0)
4198 xinner = adjust_address (xinner, BLKmode, used);
4200 /* If the partial register-part of the arg counts in its stack size,
4201 skip the part of stack space corresponding to the registers.
4202 Otherwise, start copying to the beginning of the stack space,
4203 by setting SKIP to 0. */
4204 skip = (reg_parm_stack_space == 0) ? 0 : used;
4206 #ifdef PUSH_ROUNDING
4207 /* Do it with several push insns if that doesn't take lots of insns
4208 and if there is no difficulty with push insns that skip bytes
4209 on the stack for alignment purposes. */
4210 if (args_addr == 0
4211 && PUSH_ARGS
4212 && CONST_INT_P (size)
4213 && skip == 0
4214 && MEM_ALIGN (xinner) >= align
4215 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4216 /* Here we avoid the case of a structure whose weak alignment
4217 forces many pushes of a small amount of data,
4218 and such small pushes do rounding that causes trouble. */
4219 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4220 || align >= BIGGEST_ALIGNMENT
4221 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4222 == (align / BITS_PER_UNIT)))
4223 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4225 /* Push padding now if padding above and stack grows down,
4226 or if padding below and stack grows up.
4227 But if space already allocated, this has already been done. */
4228 if (extra && args_addr == 0
4229 && where_pad != none && where_pad != stack_direction)
4230 anti_adjust_stack (GEN_INT (extra));
4232 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4234 else
4235 #endif /* PUSH_ROUNDING */
4237 rtx target;
4239 /* Otherwise make space on the stack and copy the data
4240 to the address of that space. */
4242 /* Deduct words put into registers from the size we must copy. */
4243 if (partial != 0)
4245 if (CONST_INT_P (size))
4246 size = GEN_INT (INTVAL (size) - used);
4247 else
4248 size = expand_binop (GET_MODE (size), sub_optab, size,
4249 gen_int_mode (used, GET_MODE (size)),
4250 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4253 /* Get the address of the stack space.
4254 In this case, we do not deal with EXTRA separately.
4255 A single stack adjust will do. */
4256 if (! args_addr)
4258 temp = push_block (size, extra, where_pad == downward);
4259 extra = 0;
4261 else if (CONST_INT_P (args_so_far))
4262 temp = memory_address (BLKmode,
4263 plus_constant (Pmode, args_addr,
4264 skip + INTVAL (args_so_far)));
4265 else
4266 temp = memory_address (BLKmode,
4267 plus_constant (Pmode,
4268 gen_rtx_PLUS (Pmode,
4269 args_addr,
4270 args_so_far),
4271 skip));
4273 if (!ACCUMULATE_OUTGOING_ARGS)
4275 /* If the source is referenced relative to the stack pointer,
4276 copy it to another register to stabilize it. We do not need
4277 to do this if we know that we won't be changing sp. */
4279 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4280 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4281 temp = copy_to_reg (temp);
4284 target = gen_rtx_MEM (BLKmode, temp);
4286 /* We do *not* set_mem_attributes here, because incoming arguments
4287 may overlap with sibling call outgoing arguments and we cannot
4288 allow reordering of reads from function arguments with stores
4289 to outgoing arguments of sibling calls. We do, however, want
4290 to record the alignment of the stack slot. */
4291 /* ALIGN may well be better aligned than TYPE, e.g. due to
4292 PARM_BOUNDARY. Assume the caller isn't lying. */
4293 set_mem_align (target, align);
4295 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4298 else if (partial > 0)
4300 /* Scalar partly in registers. */
4302 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4303 int i;
4304 int not_stack;
4305 /* # bytes of start of argument
4306 that we must make space for but need not store. */
4307 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4308 int args_offset = INTVAL (args_so_far);
4309 int skip;
4311 /* Push padding now if padding above and stack grows down,
4312 or if padding below and stack grows up.
4313 But if space already allocated, this has already been done. */
4314 if (extra && args_addr == 0
4315 && where_pad != none && where_pad != stack_direction)
4316 anti_adjust_stack (GEN_INT (extra));
4318 /* If we make space by pushing it, we might as well push
4319 the real data. Otherwise, we can leave OFFSET nonzero
4320 and leave the space uninitialized. */
4321 if (args_addr == 0)
4322 offset = 0;
4324 /* Now NOT_STACK gets the number of words that we don't need to
4325 allocate on the stack. Convert OFFSET to words too. */
4326 not_stack = (partial - offset) / UNITS_PER_WORD;
4327 offset /= UNITS_PER_WORD;
4329 /* If the partial register-part of the arg counts in its stack size,
4330 skip the part of stack space corresponding to the registers.
4331 Otherwise, start copying to the beginning of the stack space,
4332 by setting SKIP to 0. */
4333 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4335 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4336 x = validize_mem (force_const_mem (mode, x));
4338 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4339 SUBREGs of such registers are not allowed. */
4340 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4341 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4342 x = copy_to_reg (x);
4344 /* Loop over all the words allocated on the stack for this arg. */
4345 /* We can do it by words, because any scalar bigger than a word
4346 has a size a multiple of a word. */
4347 for (i = size - 1; i >= not_stack; i--)
4348 if (i >= not_stack + offset)
4349 emit_push_insn (operand_subword_force (x, i, mode),
4350 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4351 0, args_addr,
4352 GEN_INT (args_offset + ((i - not_stack + skip)
4353 * UNITS_PER_WORD)),
4354 reg_parm_stack_space, alignment_pad);
4356 else
4358 rtx addr;
4359 rtx dest;
4361 /* Push padding now if padding above and stack grows down,
4362 or if padding below and stack grows up.
4363 But if space already allocated, this has already been done. */
4364 if (extra && args_addr == 0
4365 && where_pad != none && where_pad != stack_direction)
4366 anti_adjust_stack (GEN_INT (extra));
4368 #ifdef PUSH_ROUNDING
4369 if (args_addr == 0 && PUSH_ARGS)
4370 emit_single_push_insn (mode, x, type);
4371 else
4372 #endif
4374 if (CONST_INT_P (args_so_far))
4375 addr
4376 = memory_address (mode,
4377 plus_constant (Pmode, args_addr,
4378 INTVAL (args_so_far)));
4379 else
4380 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4381 args_so_far));
4382 dest = gen_rtx_MEM (mode, addr);
4384 /* We do *not* set_mem_attributes here, because incoming arguments
4385 may overlap with sibling call outgoing arguments and we cannot
4386 allow reordering of reads from function arguments with stores
4387 to outgoing arguments of sibling calls. We do, however, want
4388 to record the alignment of the stack slot. */
4389 /* ALIGN may well be better aligned than TYPE, e.g. due to
4390 PARM_BOUNDARY. Assume the caller isn't lying. */
4391 set_mem_align (dest, align);
4393 emit_move_insn (dest, x);
4397 /* If part should go in registers, copy that part
4398 into the appropriate registers. Do this now, at the end,
4399 since mem-to-mem copies above may do function calls. */
4400 if (partial > 0 && reg != 0)
4402 /* Handle calls that pass values in multiple non-contiguous locations.
4403 The Irix 6 ABI has examples of this. */
4404 if (GET_CODE (reg) == PARALLEL)
4405 emit_group_load (reg, x, type, -1);
4406 else
4408 gcc_assert (partial % UNITS_PER_WORD == 0);
4409 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4413 if (extra && args_addr == 0 && where_pad == stack_direction)
4414 anti_adjust_stack (GEN_INT (extra));
4416 if (alignment_pad && args_addr == 0)
4417 anti_adjust_stack (alignment_pad);
4420 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4421 operations. */
4423 static rtx
4424 get_subtarget (rtx x)
4426 return (optimize
4427 || x == 0
4428 /* Only registers can be subtargets. */
4429 || !REG_P (x)
4430 /* Don't use hard regs to avoid extending their life. */
4431 || REGNO (x) < FIRST_PSEUDO_REGISTER
4432 ? 0 : x);
4435 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4436 FIELD is a bitfield. Returns true if the optimization was successful,
4437 and there's nothing else to do. */
4439 static bool
4440 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4441 unsigned HOST_WIDE_INT bitpos,
4442 unsigned HOST_WIDE_INT bitregion_start,
4443 unsigned HOST_WIDE_INT bitregion_end,
4444 machine_mode mode1, rtx str_rtx,
4445 tree to, tree src)
4447 machine_mode str_mode = GET_MODE (str_rtx);
4448 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4449 tree op0, op1;
4450 rtx value, result;
4451 optab binop;
4452 gimple srcstmt;
4453 enum tree_code code;
4455 if (mode1 != VOIDmode
4456 || bitsize >= BITS_PER_WORD
4457 || str_bitsize > BITS_PER_WORD
4458 || TREE_SIDE_EFFECTS (to)
4459 || TREE_THIS_VOLATILE (to))
4460 return false;
4462 STRIP_NOPS (src);
4463 if (TREE_CODE (src) != SSA_NAME)
4464 return false;
4465 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4466 return false;
4468 srcstmt = get_gimple_for_ssa_name (src);
4469 if (!srcstmt
4470 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4471 return false;
4473 code = gimple_assign_rhs_code (srcstmt);
4475 op0 = gimple_assign_rhs1 (srcstmt);
4477 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4478 to find its initialization. Hopefully the initialization will
4479 be from a bitfield load. */
4480 if (TREE_CODE (op0) == SSA_NAME)
4482 gimple op0stmt = get_gimple_for_ssa_name (op0);
4484 /* We want to eventually have OP0 be the same as TO, which
4485 should be a bitfield. */
4486 if (!op0stmt
4487 || !is_gimple_assign (op0stmt)
4488 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4489 return false;
4490 op0 = gimple_assign_rhs1 (op0stmt);
4493 op1 = gimple_assign_rhs2 (srcstmt);
4495 if (!operand_equal_p (to, op0, 0))
4496 return false;
4498 if (MEM_P (str_rtx))
4500 unsigned HOST_WIDE_INT offset1;
4502 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4503 str_mode = word_mode;
4504 str_mode = get_best_mode (bitsize, bitpos,
4505 bitregion_start, bitregion_end,
4506 MEM_ALIGN (str_rtx), str_mode, 0);
4507 if (str_mode == VOIDmode)
4508 return false;
4509 str_bitsize = GET_MODE_BITSIZE (str_mode);
4511 offset1 = bitpos;
4512 bitpos %= str_bitsize;
4513 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4514 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4516 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4517 return false;
4519 /* If the bit field covers the whole REG/MEM, store_field
4520 will likely generate better code. */
4521 if (bitsize >= str_bitsize)
4522 return false;
4524 /* We can't handle fields split across multiple entities. */
4525 if (bitpos + bitsize > str_bitsize)
4526 return false;
4528 if (BYTES_BIG_ENDIAN)
4529 bitpos = str_bitsize - bitpos - bitsize;
4531 switch (code)
4533 case PLUS_EXPR:
4534 case MINUS_EXPR:
4535 /* For now, just optimize the case of the topmost bitfield
4536 where we don't need to do any masking and also
4537 1 bit bitfields where xor can be used.
4538 We might win by one instruction for the other bitfields
4539 too if insv/extv instructions aren't used, so that
4540 can be added later. */
4541 if (bitpos + bitsize != str_bitsize
4542 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4543 break;
4545 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4546 value = convert_modes (str_mode,
4547 TYPE_MODE (TREE_TYPE (op1)), value,
4548 TYPE_UNSIGNED (TREE_TYPE (op1)));
4550 /* We may be accessing data outside the field, which means
4551 we can alias adjacent data. */
4552 if (MEM_P (str_rtx))
4554 str_rtx = shallow_copy_rtx (str_rtx);
4555 set_mem_alias_set (str_rtx, 0);
4556 set_mem_expr (str_rtx, 0);
4559 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4560 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4562 value = expand_and (str_mode, value, const1_rtx, NULL);
4563 binop = xor_optab;
4565 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4566 result = expand_binop (str_mode, binop, str_rtx,
4567 value, str_rtx, 1, OPTAB_WIDEN);
4568 if (result != str_rtx)
4569 emit_move_insn (str_rtx, result);
4570 return true;
4572 case BIT_IOR_EXPR:
4573 case BIT_XOR_EXPR:
4574 if (TREE_CODE (op1) != INTEGER_CST)
4575 break;
4576 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4577 value = convert_modes (str_mode,
4578 TYPE_MODE (TREE_TYPE (op1)), value,
4579 TYPE_UNSIGNED (TREE_TYPE (op1)));
4581 /* We may be accessing data outside the field, which means
4582 we can alias adjacent data. */
4583 if (MEM_P (str_rtx))
4585 str_rtx = shallow_copy_rtx (str_rtx);
4586 set_mem_alias_set (str_rtx, 0);
4587 set_mem_expr (str_rtx, 0);
4590 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4591 if (bitpos + bitsize != str_bitsize)
4593 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4594 str_mode);
4595 value = expand_and (str_mode, value, mask, NULL_RTX);
4597 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4598 result = expand_binop (str_mode, binop, str_rtx,
4599 value, str_rtx, 1, OPTAB_WIDEN);
4600 if (result != str_rtx)
4601 emit_move_insn (str_rtx, result);
4602 return true;
4604 default:
4605 break;
4608 return false;
4611 /* In the C++ memory model, consecutive bit fields in a structure are
4612 considered one memory location.
4614 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4615 returns the bit range of consecutive bits in which this COMPONENT_REF
4616 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4617 and *OFFSET may be adjusted in the process.
4619 If the access does not need to be restricted, 0 is returned in both
4620 *BITSTART and *BITEND. */
4622 static void
4623 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4624 unsigned HOST_WIDE_INT *bitend,
4625 tree exp,
4626 HOST_WIDE_INT *bitpos,
4627 tree *offset)
4629 HOST_WIDE_INT bitoffset;
4630 tree field, repr;
4632 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4634 field = TREE_OPERAND (exp, 1);
4635 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4636 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4637 need to limit the range we can access. */
4638 if (!repr)
4640 *bitstart = *bitend = 0;
4641 return;
4644 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4645 part of a larger bit field, then the representative does not serve any
4646 useful purpose. This can occur in Ada. */
4647 if (handled_component_p (TREE_OPERAND (exp, 0)))
4649 machine_mode rmode;
4650 HOST_WIDE_INT rbitsize, rbitpos;
4651 tree roffset;
4652 int unsignedp;
4653 int volatilep = 0;
4654 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4655 &roffset, &rmode, &unsignedp, &volatilep, false);
4656 if ((rbitpos % BITS_PER_UNIT) != 0)
4658 *bitstart = *bitend = 0;
4659 return;
4663 /* Compute the adjustment to bitpos from the offset of the field
4664 relative to the representative. DECL_FIELD_OFFSET of field and
4665 repr are the same by construction if they are not constants,
4666 see finish_bitfield_layout. */
4667 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4668 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4669 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4670 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4671 else
4672 bitoffset = 0;
4673 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4674 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4676 /* If the adjustment is larger than bitpos, we would have a negative bit
4677 position for the lower bound and this may wreak havoc later. Adjust
4678 offset and bitpos to make the lower bound non-negative in that case. */
4679 if (bitoffset > *bitpos)
4681 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4682 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4684 *bitpos += adjust;
4685 if (*offset == NULL_TREE)
4686 *offset = size_int (-adjust / BITS_PER_UNIT);
4687 else
4688 *offset
4689 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4690 *bitstart = 0;
4692 else
4693 *bitstart = *bitpos - bitoffset;
4695 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4698 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4699 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4700 DECL_RTL was not set yet, return NORTL. */
4702 static inline bool
4703 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4705 if (TREE_CODE (addr) != ADDR_EXPR)
4706 return false;
4708 tree base = TREE_OPERAND (addr, 0);
4710 if (!DECL_P (base)
4711 || TREE_ADDRESSABLE (base)
4712 || DECL_MODE (base) == BLKmode)
4713 return false;
4715 if (!DECL_RTL_SET_P (base))
4716 return nortl;
4718 return (!MEM_P (DECL_RTL (base)));
4721 /* Returns true if the MEM_REF REF refers to an object that does not
4722 reside in memory and has non-BLKmode. */
4724 static inline bool
4725 mem_ref_refers_to_non_mem_p (tree ref)
4727 tree base = TREE_OPERAND (ref, 0);
4728 return addr_expr_of_non_mem_decl_p_1 (base, false);
4731 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4732 is true, try generating a nontemporal store. */
4734 void
4735 expand_assignment (tree to, tree from, bool nontemporal)
4737 rtx to_rtx = 0;
4738 rtx result;
4739 machine_mode mode;
4740 unsigned int align;
4741 enum insn_code icode;
4743 /* Don't crash if the lhs of the assignment was erroneous. */
4744 if (TREE_CODE (to) == ERROR_MARK)
4746 expand_normal (from);
4747 return;
4750 /* Optimize away no-op moves without side-effects. */
4751 if (operand_equal_p (to, from, 0))
4752 return;
4754 /* Handle misaligned stores. */
4755 mode = TYPE_MODE (TREE_TYPE (to));
4756 if ((TREE_CODE (to) == MEM_REF
4757 || TREE_CODE (to) == TARGET_MEM_REF)
4758 && mode != BLKmode
4759 && !mem_ref_refers_to_non_mem_p (to)
4760 && ((align = get_object_alignment (to))
4761 < GET_MODE_ALIGNMENT (mode))
4762 && (((icode = optab_handler (movmisalign_optab, mode))
4763 != CODE_FOR_nothing)
4764 || SLOW_UNALIGNED_ACCESS (mode, align)))
4766 rtx reg, mem;
4768 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4769 reg = force_not_mem (reg);
4770 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4772 if (icode != CODE_FOR_nothing)
4774 struct expand_operand ops[2];
4776 create_fixed_operand (&ops[0], mem);
4777 create_input_operand (&ops[1], reg, mode);
4778 /* The movmisalign<mode> pattern cannot fail, else the assignment
4779 would silently be omitted. */
4780 expand_insn (icode, 2, ops);
4782 else
4783 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4784 return;
4787 /* Assignment of a structure component needs special treatment
4788 if the structure component's rtx is not simply a MEM.
4789 Assignment of an array element at a constant index, and assignment of
4790 an array element in an unaligned packed structure field, has the same
4791 problem. Same for (partially) storing into a non-memory object. */
4792 if (handled_component_p (to)
4793 || (TREE_CODE (to) == MEM_REF
4794 && mem_ref_refers_to_non_mem_p (to))
4795 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4797 machine_mode mode1;
4798 HOST_WIDE_INT bitsize, bitpos;
4799 unsigned HOST_WIDE_INT bitregion_start = 0;
4800 unsigned HOST_WIDE_INT bitregion_end = 0;
4801 tree offset;
4802 int unsignedp;
4803 int volatilep = 0;
4804 tree tem;
4806 push_temp_slots ();
4807 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4808 &unsignedp, &volatilep, true);
4810 /* Make sure bitpos is not negative, it can wreak havoc later. */
4811 if (bitpos < 0)
4813 gcc_assert (offset == NULL_TREE);
4814 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4815 ? 3 : exact_log2 (BITS_PER_UNIT)));
4816 bitpos &= BITS_PER_UNIT - 1;
4819 if (TREE_CODE (to) == COMPONENT_REF
4820 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4821 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4822 /* The C++ memory model naturally applies to byte-aligned fields.
4823 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4824 BITSIZE are not byte-aligned, there is no need to limit the range
4825 we can access. This can occur with packed structures in Ada. */
4826 else if (bitsize > 0
4827 && bitsize % BITS_PER_UNIT == 0
4828 && bitpos % BITS_PER_UNIT == 0)
4830 bitregion_start = bitpos;
4831 bitregion_end = bitpos + bitsize - 1;
4834 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4836 /* If the field has a mode, we want to access it in the
4837 field's mode, not the computed mode.
4838 If a MEM has VOIDmode (external with incomplete type),
4839 use BLKmode for it instead. */
4840 if (MEM_P (to_rtx))
4842 if (mode1 != VOIDmode)
4843 to_rtx = adjust_address (to_rtx, mode1, 0);
4844 else if (GET_MODE (to_rtx) == VOIDmode)
4845 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4848 if (offset != 0)
4850 machine_mode address_mode;
4851 rtx offset_rtx;
4853 if (!MEM_P (to_rtx))
4855 /* We can get constant negative offsets into arrays with broken
4856 user code. Translate this to a trap instead of ICEing. */
4857 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4858 expand_builtin_trap ();
4859 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4862 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4863 address_mode = get_address_mode (to_rtx);
4864 if (GET_MODE (offset_rtx) != address_mode)
4865 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4867 /* If we have an expression in OFFSET_RTX and a non-zero
4868 byte offset in BITPOS, adding the byte offset before the
4869 OFFSET_RTX results in better intermediate code, which makes
4870 later rtl optimization passes perform better.
4872 We prefer intermediate code like this:
4874 r124:DI=r123:DI+0x18
4875 [r124:DI]=r121:DI
4877 ... instead of ...
4879 r124:DI=r123:DI+0x10
4880 [r124:DI+0x8]=r121:DI
4882 This is only done for aligned data values, as these can
4883 be expected to result in single move instructions. */
4884 if (mode1 != VOIDmode
4885 && bitpos != 0
4886 && bitsize > 0
4887 && (bitpos % bitsize) == 0
4888 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4889 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4891 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4892 bitregion_start = 0;
4893 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4894 bitregion_end -= bitpos;
4895 bitpos = 0;
4898 to_rtx = offset_address (to_rtx, offset_rtx,
4899 highest_pow2_factor_for_target (to,
4900 offset));
4903 /* No action is needed if the target is not a memory and the field
4904 lies completely outside that target. This can occur if the source
4905 code contains an out-of-bounds access to a small array. */
4906 if (!MEM_P (to_rtx)
4907 && GET_MODE (to_rtx) != BLKmode
4908 && (unsigned HOST_WIDE_INT) bitpos
4909 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4911 expand_normal (from);
4912 result = NULL;
4914 /* Handle expand_expr of a complex value returning a CONCAT. */
4915 else if (GET_CODE (to_rtx) == CONCAT)
4917 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4918 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4919 && bitpos == 0
4920 && bitsize == mode_bitsize)
4921 result = store_expr (from, to_rtx, false, nontemporal);
4922 else if (bitsize == mode_bitsize / 2
4923 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4924 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4925 nontemporal);
4926 else if (bitpos + bitsize <= mode_bitsize / 2)
4927 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4928 bitregion_start, bitregion_end,
4929 mode1, from,
4930 get_alias_set (to), nontemporal);
4931 else if (bitpos >= mode_bitsize / 2)
4932 result = store_field (XEXP (to_rtx, 1), bitsize,
4933 bitpos - mode_bitsize / 2,
4934 bitregion_start, bitregion_end,
4935 mode1, from,
4936 get_alias_set (to), nontemporal);
4937 else if (bitpos == 0 && bitsize == mode_bitsize)
4939 rtx from_rtx;
4940 result = expand_normal (from);
4941 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4942 TYPE_MODE (TREE_TYPE (from)), 0);
4943 emit_move_insn (XEXP (to_rtx, 0),
4944 read_complex_part (from_rtx, false));
4945 emit_move_insn (XEXP (to_rtx, 1),
4946 read_complex_part (from_rtx, true));
4948 else
4950 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4951 GET_MODE_SIZE (GET_MODE (to_rtx)));
4952 write_complex_part (temp, XEXP (to_rtx, 0), false);
4953 write_complex_part (temp, XEXP (to_rtx, 1), true);
4954 result = store_field (temp, bitsize, bitpos,
4955 bitregion_start, bitregion_end,
4956 mode1, from,
4957 get_alias_set (to), nontemporal);
4958 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4959 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4962 else
4964 if (MEM_P (to_rtx))
4966 /* If the field is at offset zero, we could have been given the
4967 DECL_RTX of the parent struct. Don't munge it. */
4968 to_rtx = shallow_copy_rtx (to_rtx);
4969 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4970 if (volatilep)
4971 MEM_VOLATILE_P (to_rtx) = 1;
4974 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4975 bitregion_start, bitregion_end,
4976 mode1,
4977 to_rtx, to, from))
4978 result = NULL;
4979 else
4980 result = store_field (to_rtx, bitsize, bitpos,
4981 bitregion_start, bitregion_end,
4982 mode1, from,
4983 get_alias_set (to), nontemporal);
4986 if (result)
4987 preserve_temp_slots (result);
4988 pop_temp_slots ();
4989 return;
4992 /* If the rhs is a function call and its value is not an aggregate,
4993 call the function before we start to compute the lhs.
4994 This is needed for correct code for cases such as
4995 val = setjmp (buf) on machines where reference to val
4996 requires loading up part of an address in a separate insn.
4998 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4999 since it might be a promoted variable where the zero- or sign- extension
5000 needs to be done. Handling this in the normal way is safe because no
5001 computation is done before the call. The same is true for SSA names. */
5002 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5003 && COMPLETE_TYPE_P (TREE_TYPE (from))
5004 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5005 && ! (((TREE_CODE (to) == VAR_DECL
5006 || TREE_CODE (to) == PARM_DECL
5007 || TREE_CODE (to) == RESULT_DECL)
5008 && REG_P (DECL_RTL (to)))
5009 || TREE_CODE (to) == SSA_NAME))
5011 rtx value;
5012 rtx bounds;
5014 push_temp_slots ();
5015 value = expand_normal (from);
5017 /* Split value and bounds to store them separately. */
5018 chkp_split_slot (value, &value, &bounds);
5020 if (to_rtx == 0)
5021 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5023 /* Handle calls that return values in multiple non-contiguous locations.
5024 The Irix 6 ABI has examples of this. */
5025 if (GET_CODE (to_rtx) == PARALLEL)
5027 if (GET_CODE (value) == PARALLEL)
5028 emit_group_move (to_rtx, value);
5029 else
5030 emit_group_load (to_rtx, value, TREE_TYPE (from),
5031 int_size_in_bytes (TREE_TYPE (from)));
5033 else if (GET_CODE (value) == PARALLEL)
5034 emit_group_store (to_rtx, value, TREE_TYPE (from),
5035 int_size_in_bytes (TREE_TYPE (from)));
5036 else if (GET_MODE (to_rtx) == BLKmode)
5038 /* Handle calls that return BLKmode values in registers. */
5039 if (REG_P (value))
5040 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5041 else
5042 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5044 else
5046 if (POINTER_TYPE_P (TREE_TYPE (to)))
5047 value = convert_memory_address_addr_space
5048 (GET_MODE (to_rtx), value,
5049 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5051 emit_move_insn (to_rtx, value);
5054 /* Store bounds if required. */
5055 if (bounds
5056 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5058 gcc_assert (MEM_P (to_rtx));
5059 chkp_emit_bounds_store (bounds, value, to_rtx);
5062 preserve_temp_slots (to_rtx);
5063 pop_temp_slots ();
5064 return;
5067 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5068 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5070 /* Don't move directly into a return register. */
5071 if (TREE_CODE (to) == RESULT_DECL
5072 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5074 rtx temp;
5076 push_temp_slots ();
5078 /* If the source is itself a return value, it still is in a pseudo at
5079 this point so we can move it back to the return register directly. */
5080 if (REG_P (to_rtx)
5081 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5082 && TREE_CODE (from) != CALL_EXPR)
5083 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5084 else
5085 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5087 /* Handle calls that return values in multiple non-contiguous locations.
5088 The Irix 6 ABI has examples of this. */
5089 if (GET_CODE (to_rtx) == PARALLEL)
5091 if (GET_CODE (temp) == PARALLEL)
5092 emit_group_move (to_rtx, temp);
5093 else
5094 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5095 int_size_in_bytes (TREE_TYPE (from)));
5097 else if (temp)
5098 emit_move_insn (to_rtx, temp);
5100 preserve_temp_slots (to_rtx);
5101 pop_temp_slots ();
5102 return;
5105 /* In case we are returning the contents of an object which overlaps
5106 the place the value is being stored, use a safe function when copying
5107 a value through a pointer into a structure value return block. */
5108 if (TREE_CODE (to) == RESULT_DECL
5109 && TREE_CODE (from) == INDIRECT_REF
5110 && ADDR_SPACE_GENERIC_P
5111 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5112 && refs_may_alias_p (to, from)
5113 && cfun->returns_struct
5114 && !cfun->returns_pcc_struct)
5116 rtx from_rtx, size;
5118 push_temp_slots ();
5119 size = expr_size (from);
5120 from_rtx = expand_normal (from);
5122 emit_library_call (memmove_libfunc, LCT_NORMAL,
5123 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5124 XEXP (from_rtx, 0), Pmode,
5125 convert_to_mode (TYPE_MODE (sizetype),
5126 size, TYPE_UNSIGNED (sizetype)),
5127 TYPE_MODE (sizetype));
5129 preserve_temp_slots (to_rtx);
5130 pop_temp_slots ();
5131 return;
5134 /* Compute FROM and store the value in the rtx we got. */
5136 push_temp_slots ();
5137 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5138 preserve_temp_slots (result);
5139 pop_temp_slots ();
5140 return;
5143 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5144 succeeded, false otherwise. */
5146 bool
5147 emit_storent_insn (rtx to, rtx from)
5149 struct expand_operand ops[2];
5150 machine_mode mode = GET_MODE (to);
5151 enum insn_code code = optab_handler (storent_optab, mode);
5153 if (code == CODE_FOR_nothing)
5154 return false;
5156 create_fixed_operand (&ops[0], to);
5157 create_input_operand (&ops[1], from, mode);
5158 return maybe_expand_insn (code, 2, ops);
5161 /* Generate code for computing expression EXP,
5162 and storing the value into TARGET.
5164 If the mode is BLKmode then we may return TARGET itself.
5165 It turns out that in BLKmode it doesn't cause a problem.
5166 because C has no operators that could combine two different
5167 assignments into the same BLKmode object with different values
5168 with no sequence point. Will other languages need this to
5169 be more thorough?
5171 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5172 stack, and block moves may need to be treated specially.
5174 If NONTEMPORAL is true, try using a nontemporal store instruction.
5176 If BTARGET is not NULL then computed bounds of EXP are
5177 associated with BTARGET. */
5180 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5181 bool nontemporal, tree btarget)
5183 rtx temp;
5184 rtx alt_rtl = NULL_RTX;
5185 location_t loc = curr_insn_location ();
5187 if (VOID_TYPE_P (TREE_TYPE (exp)))
5189 /* C++ can generate ?: expressions with a throw expression in one
5190 branch and an rvalue in the other. Here, we resolve attempts to
5191 store the throw expression's nonexistent result. */
5192 gcc_assert (!call_param_p);
5193 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5194 return NULL_RTX;
5196 if (TREE_CODE (exp) == COMPOUND_EXPR)
5198 /* Perform first part of compound expression, then assign from second
5199 part. */
5200 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5201 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5202 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5203 call_param_p, nontemporal, btarget);
5205 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5207 /* For conditional expression, get safe form of the target. Then
5208 test the condition, doing the appropriate assignment on either
5209 side. This avoids the creation of unnecessary temporaries.
5210 For non-BLKmode, it is more efficient not to do this. */
5212 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5214 do_pending_stack_adjust ();
5215 NO_DEFER_POP;
5216 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5217 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5218 nontemporal, btarget);
5219 emit_jump_insn (gen_jump (lab2));
5220 emit_barrier ();
5221 emit_label (lab1);
5222 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5223 nontemporal, btarget);
5224 emit_label (lab2);
5225 OK_DEFER_POP;
5227 return NULL_RTX;
5229 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5230 /* If this is a scalar in a register that is stored in a wider mode
5231 than the declared mode, compute the result into its declared mode
5232 and then convert to the wider mode. Our value is the computed
5233 expression. */
5235 rtx inner_target = 0;
5237 /* We can do the conversion inside EXP, which will often result
5238 in some optimizations. Do the conversion in two steps: first
5239 change the signedness, if needed, then the extend. But don't
5240 do this if the type of EXP is a subtype of something else
5241 since then the conversion might involve more than just
5242 converting modes. */
5243 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5244 && TREE_TYPE (TREE_TYPE (exp)) == 0
5245 && GET_MODE_PRECISION (GET_MODE (target))
5246 == TYPE_PRECISION (TREE_TYPE (exp)))
5248 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5249 TYPE_UNSIGNED (TREE_TYPE (exp))))
5251 /* Some types, e.g. Fortran's logical*4, won't have a signed
5252 version, so use the mode instead. */
5253 tree ntype
5254 = (signed_or_unsigned_type_for
5255 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5256 if (ntype == NULL)
5257 ntype = lang_hooks.types.type_for_mode
5258 (TYPE_MODE (TREE_TYPE (exp)),
5259 SUBREG_PROMOTED_SIGN (target));
5261 exp = fold_convert_loc (loc, ntype, exp);
5264 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5265 (GET_MODE (SUBREG_REG (target)),
5266 SUBREG_PROMOTED_SIGN (target)),
5267 exp);
5269 inner_target = SUBREG_REG (target);
5272 temp = expand_expr (exp, inner_target, VOIDmode,
5273 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5275 /* Handle bounds returned by call. */
5276 if (TREE_CODE (exp) == CALL_EXPR)
5278 rtx bounds;
5279 chkp_split_slot (temp, &temp, &bounds);
5280 if (bounds && btarget)
5282 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5283 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5284 chkp_set_rtl_bounds (btarget, tmp);
5288 /* If TEMP is a VOIDmode constant, use convert_modes to make
5289 sure that we properly convert it. */
5290 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5292 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5293 temp, SUBREG_PROMOTED_SIGN (target));
5294 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5295 GET_MODE (target), temp,
5296 SUBREG_PROMOTED_SIGN (target));
5299 convert_move (SUBREG_REG (target), temp,
5300 SUBREG_PROMOTED_SIGN (target));
5302 return NULL_RTX;
5304 else if ((TREE_CODE (exp) == STRING_CST
5305 || (TREE_CODE (exp) == MEM_REF
5306 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5307 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5308 == STRING_CST
5309 && integer_zerop (TREE_OPERAND (exp, 1))))
5310 && !nontemporal && !call_param_p
5311 && MEM_P (target))
5313 /* Optimize initialization of an array with a STRING_CST. */
5314 HOST_WIDE_INT exp_len, str_copy_len;
5315 rtx dest_mem;
5316 tree str = TREE_CODE (exp) == STRING_CST
5317 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5319 exp_len = int_expr_size (exp);
5320 if (exp_len <= 0)
5321 goto normal_expr;
5323 if (TREE_STRING_LENGTH (str) <= 0)
5324 goto normal_expr;
5326 str_copy_len = strlen (TREE_STRING_POINTER (str));
5327 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5328 goto normal_expr;
5330 str_copy_len = TREE_STRING_LENGTH (str);
5331 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5332 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5334 str_copy_len += STORE_MAX_PIECES - 1;
5335 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5337 str_copy_len = MIN (str_copy_len, exp_len);
5338 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5339 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5340 MEM_ALIGN (target), false))
5341 goto normal_expr;
5343 dest_mem = target;
5345 dest_mem = store_by_pieces (dest_mem,
5346 str_copy_len, builtin_strncpy_read_str,
5347 CONST_CAST (char *,
5348 TREE_STRING_POINTER (str)),
5349 MEM_ALIGN (target), false,
5350 exp_len > str_copy_len ? 1 : 0);
5351 if (exp_len > str_copy_len)
5352 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5353 GEN_INT (exp_len - str_copy_len),
5354 BLOCK_OP_NORMAL);
5355 return NULL_RTX;
5357 else
5359 rtx tmp_target;
5361 normal_expr:
5362 /* If we want to use a nontemporal store, force the value to
5363 register first. */
5364 tmp_target = nontemporal ? NULL_RTX : target;
5365 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5366 (call_param_p
5367 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5368 &alt_rtl, false);
5370 /* Handle bounds returned by call. */
5371 if (TREE_CODE (exp) == CALL_EXPR)
5373 rtx bounds;
5374 chkp_split_slot (temp, &temp, &bounds);
5375 if (bounds && btarget)
5377 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5378 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5379 chkp_set_rtl_bounds (btarget, tmp);
5384 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5385 the same as that of TARGET, adjust the constant. This is needed, for
5386 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5387 only a word-sized value. */
5388 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5389 && TREE_CODE (exp) != ERROR_MARK
5390 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5391 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5392 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5394 /* If value was not generated in the target, store it there.
5395 Convert the value to TARGET's type first if necessary and emit the
5396 pending incrementations that have been queued when expanding EXP.
5397 Note that we cannot emit the whole queue blindly because this will
5398 effectively disable the POST_INC optimization later.
5400 If TEMP and TARGET compare equal according to rtx_equal_p, but
5401 one or both of them are volatile memory refs, we have to distinguish
5402 two cases:
5403 - expand_expr has used TARGET. In this case, we must not generate
5404 another copy. This can be detected by TARGET being equal according
5405 to == .
5406 - expand_expr has not used TARGET - that means that the source just
5407 happens to have the same RTX form. Since temp will have been created
5408 by expand_expr, it will compare unequal according to == .
5409 We must generate a copy in this case, to reach the correct number
5410 of volatile memory references. */
5412 if ((! rtx_equal_p (temp, target)
5413 || (temp != target && (side_effects_p (temp)
5414 || side_effects_p (target))))
5415 && TREE_CODE (exp) != ERROR_MARK
5416 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5417 but TARGET is not valid memory reference, TEMP will differ
5418 from TARGET although it is really the same location. */
5419 && !(alt_rtl
5420 && rtx_equal_p (alt_rtl, target)
5421 && !side_effects_p (alt_rtl)
5422 && !side_effects_p (target))
5423 /* If there's nothing to copy, don't bother. Don't call
5424 expr_size unless necessary, because some front-ends (C++)
5425 expr_size-hook must not be given objects that are not
5426 supposed to be bit-copied or bit-initialized. */
5427 && expr_size (exp) != const0_rtx)
5429 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5431 if (GET_MODE (target) == BLKmode)
5433 /* Handle calls that return BLKmode values in registers. */
5434 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5435 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5436 else
5437 store_bit_field (target,
5438 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5439 0, 0, 0, GET_MODE (temp), temp);
5441 else
5442 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5445 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5447 /* Handle copying a string constant into an array. The string
5448 constant may be shorter than the array. So copy just the string's
5449 actual length, and clear the rest. First get the size of the data
5450 type of the string, which is actually the size of the target. */
5451 rtx size = expr_size (exp);
5453 if (CONST_INT_P (size)
5454 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5455 emit_block_move (target, temp, size,
5456 (call_param_p
5457 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5458 else
5460 machine_mode pointer_mode
5461 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5462 machine_mode address_mode = get_address_mode (target);
5464 /* Compute the size of the data to copy from the string. */
5465 tree copy_size
5466 = size_binop_loc (loc, MIN_EXPR,
5467 make_tree (sizetype, size),
5468 size_int (TREE_STRING_LENGTH (exp)));
5469 rtx copy_size_rtx
5470 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5471 (call_param_p
5472 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5473 rtx_code_label *label = 0;
5475 /* Copy that much. */
5476 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5477 TYPE_UNSIGNED (sizetype));
5478 emit_block_move (target, temp, copy_size_rtx,
5479 (call_param_p
5480 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5482 /* Figure out how much is left in TARGET that we have to clear.
5483 Do all calculations in pointer_mode. */
5484 if (CONST_INT_P (copy_size_rtx))
5486 size = plus_constant (address_mode, size,
5487 -INTVAL (copy_size_rtx));
5488 target = adjust_address (target, BLKmode,
5489 INTVAL (copy_size_rtx));
5491 else
5493 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5494 copy_size_rtx, NULL_RTX, 0,
5495 OPTAB_LIB_WIDEN);
5497 if (GET_MODE (copy_size_rtx) != address_mode)
5498 copy_size_rtx = convert_to_mode (address_mode,
5499 copy_size_rtx,
5500 TYPE_UNSIGNED (sizetype));
5502 target = offset_address (target, copy_size_rtx,
5503 highest_pow2_factor (copy_size));
5504 label = gen_label_rtx ();
5505 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5506 GET_MODE (size), 0, label);
5509 if (size != const0_rtx)
5510 clear_storage (target, size, BLOCK_OP_NORMAL);
5512 if (label)
5513 emit_label (label);
5516 /* Handle calls that return values in multiple non-contiguous locations.
5517 The Irix 6 ABI has examples of this. */
5518 else if (GET_CODE (target) == PARALLEL)
5520 if (GET_CODE (temp) == PARALLEL)
5521 emit_group_move (target, temp);
5522 else
5523 emit_group_load (target, temp, TREE_TYPE (exp),
5524 int_size_in_bytes (TREE_TYPE (exp)));
5526 else if (GET_CODE (temp) == PARALLEL)
5527 emit_group_store (target, temp, TREE_TYPE (exp),
5528 int_size_in_bytes (TREE_TYPE (exp)));
5529 else if (GET_MODE (temp) == BLKmode)
5530 emit_block_move (target, temp, expr_size (exp),
5531 (call_param_p
5532 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5533 /* If we emit a nontemporal store, there is nothing else to do. */
5534 else if (nontemporal && emit_storent_insn (target, temp))
5536 else
5538 temp = force_operand (temp, target);
5539 if (temp != target)
5540 emit_move_insn (target, temp);
5544 return NULL_RTX;
5547 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5549 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5551 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5554 /* Return true if field F of structure TYPE is a flexible array. */
5556 static bool
5557 flexible_array_member_p (const_tree f, const_tree type)
5559 const_tree tf;
5561 tf = TREE_TYPE (f);
5562 return (DECL_CHAIN (f) == NULL
5563 && TREE_CODE (tf) == ARRAY_TYPE
5564 && TYPE_DOMAIN (tf)
5565 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5566 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5567 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5568 && int_size_in_bytes (type) >= 0);
5571 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5572 must have in order for it to completely initialize a value of type TYPE.
5573 Return -1 if the number isn't known.
5575 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5577 static HOST_WIDE_INT
5578 count_type_elements (const_tree type, bool for_ctor_p)
5580 switch (TREE_CODE (type))
5582 case ARRAY_TYPE:
5584 tree nelts;
5586 nelts = array_type_nelts (type);
5587 if (nelts && tree_fits_uhwi_p (nelts))
5589 unsigned HOST_WIDE_INT n;
5591 n = tree_to_uhwi (nelts) + 1;
5592 if (n == 0 || for_ctor_p)
5593 return n;
5594 else
5595 return n * count_type_elements (TREE_TYPE (type), false);
5597 return for_ctor_p ? -1 : 1;
5600 case RECORD_TYPE:
5602 unsigned HOST_WIDE_INT n;
5603 tree f;
5605 n = 0;
5606 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5607 if (TREE_CODE (f) == FIELD_DECL)
5609 if (!for_ctor_p)
5610 n += count_type_elements (TREE_TYPE (f), false);
5611 else if (!flexible_array_member_p (f, type))
5612 /* Don't count flexible arrays, which are not supposed
5613 to be initialized. */
5614 n += 1;
5617 return n;
5620 case UNION_TYPE:
5621 case QUAL_UNION_TYPE:
5623 tree f;
5624 HOST_WIDE_INT n, m;
5626 gcc_assert (!for_ctor_p);
5627 /* Estimate the number of scalars in each field and pick the
5628 maximum. Other estimates would do instead; the idea is simply
5629 to make sure that the estimate is not sensitive to the ordering
5630 of the fields. */
5631 n = 1;
5632 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5633 if (TREE_CODE (f) == FIELD_DECL)
5635 m = count_type_elements (TREE_TYPE (f), false);
5636 /* If the field doesn't span the whole union, add an extra
5637 scalar for the rest. */
5638 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5639 TYPE_SIZE (type)) != 1)
5640 m++;
5641 if (n < m)
5642 n = m;
5644 return n;
5647 case COMPLEX_TYPE:
5648 return 2;
5650 case VECTOR_TYPE:
5651 return TYPE_VECTOR_SUBPARTS (type);
5653 case INTEGER_TYPE:
5654 case REAL_TYPE:
5655 case FIXED_POINT_TYPE:
5656 case ENUMERAL_TYPE:
5657 case BOOLEAN_TYPE:
5658 case POINTER_TYPE:
5659 case OFFSET_TYPE:
5660 case REFERENCE_TYPE:
5661 case NULLPTR_TYPE:
5662 return 1;
5664 case ERROR_MARK:
5665 return 0;
5667 case VOID_TYPE:
5668 case METHOD_TYPE:
5669 case FUNCTION_TYPE:
5670 case LANG_TYPE:
5671 default:
5672 gcc_unreachable ();
5676 /* Helper for categorize_ctor_elements. Identical interface. */
5678 static bool
5679 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5680 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5682 unsigned HOST_WIDE_INT idx;
5683 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5684 tree value, purpose, elt_type;
5686 /* Whether CTOR is a valid constant initializer, in accordance with what
5687 initializer_constant_valid_p does. If inferred from the constructor
5688 elements, true until proven otherwise. */
5689 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5690 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5692 nz_elts = 0;
5693 init_elts = 0;
5694 num_fields = 0;
5695 elt_type = NULL_TREE;
5697 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5699 HOST_WIDE_INT mult = 1;
5701 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5703 tree lo_index = TREE_OPERAND (purpose, 0);
5704 tree hi_index = TREE_OPERAND (purpose, 1);
5706 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5707 mult = (tree_to_uhwi (hi_index)
5708 - tree_to_uhwi (lo_index) + 1);
5710 num_fields += mult;
5711 elt_type = TREE_TYPE (value);
5713 switch (TREE_CODE (value))
5715 case CONSTRUCTOR:
5717 HOST_WIDE_INT nz = 0, ic = 0;
5719 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5720 p_complete);
5722 nz_elts += mult * nz;
5723 init_elts += mult * ic;
5725 if (const_from_elts_p && const_p)
5726 const_p = const_elt_p;
5728 break;
5730 case INTEGER_CST:
5731 case REAL_CST:
5732 case FIXED_CST:
5733 if (!initializer_zerop (value))
5734 nz_elts += mult;
5735 init_elts += mult;
5736 break;
5738 case STRING_CST:
5739 nz_elts += mult * TREE_STRING_LENGTH (value);
5740 init_elts += mult * TREE_STRING_LENGTH (value);
5741 break;
5743 case COMPLEX_CST:
5744 if (!initializer_zerop (TREE_REALPART (value)))
5745 nz_elts += mult;
5746 if (!initializer_zerop (TREE_IMAGPART (value)))
5747 nz_elts += mult;
5748 init_elts += mult;
5749 break;
5751 case VECTOR_CST:
5753 unsigned i;
5754 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5756 tree v = VECTOR_CST_ELT (value, i);
5757 if (!initializer_zerop (v))
5758 nz_elts += mult;
5759 init_elts += mult;
5762 break;
5764 default:
5766 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5767 nz_elts += mult * tc;
5768 init_elts += mult * tc;
5770 if (const_from_elts_p && const_p)
5771 const_p = initializer_constant_valid_p (value, elt_type)
5772 != NULL_TREE;
5774 break;
5778 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5779 num_fields, elt_type))
5780 *p_complete = false;
5782 *p_nz_elts += nz_elts;
5783 *p_init_elts += init_elts;
5785 return const_p;
5788 /* Examine CTOR to discover:
5789 * how many scalar fields are set to nonzero values,
5790 and place it in *P_NZ_ELTS;
5791 * how many scalar fields in total are in CTOR,
5792 and place it in *P_ELT_COUNT.
5793 * whether the constructor is complete -- in the sense that every
5794 meaningful byte is explicitly given a value --
5795 and place it in *P_COMPLETE.
5797 Return whether or not CTOR is a valid static constant initializer, the same
5798 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5800 bool
5801 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5802 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5804 *p_nz_elts = 0;
5805 *p_init_elts = 0;
5806 *p_complete = true;
5808 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5811 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5812 of which had type LAST_TYPE. Each element was itself a complete
5813 initializer, in the sense that every meaningful byte was explicitly
5814 given a value. Return true if the same is true for the constructor
5815 as a whole. */
5817 bool
5818 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5819 const_tree last_type)
5821 if (TREE_CODE (type) == UNION_TYPE
5822 || TREE_CODE (type) == QUAL_UNION_TYPE)
5824 if (num_elts == 0)
5825 return false;
5827 gcc_assert (num_elts == 1 && last_type);
5829 /* ??? We could look at each element of the union, and find the
5830 largest element. Which would avoid comparing the size of the
5831 initialized element against any tail padding in the union.
5832 Doesn't seem worth the effort... */
5833 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5836 return count_type_elements (type, true) == num_elts;
5839 /* Return 1 if EXP contains mostly (3/4) zeros. */
5841 static int
5842 mostly_zeros_p (const_tree exp)
5844 if (TREE_CODE (exp) == CONSTRUCTOR)
5846 HOST_WIDE_INT nz_elts, init_elts;
5847 bool complete_p;
5849 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5850 return !complete_p || nz_elts < init_elts / 4;
5853 return initializer_zerop (exp);
5856 /* Return 1 if EXP contains all zeros. */
5858 static int
5859 all_zeros_p (const_tree exp)
5861 if (TREE_CODE (exp) == CONSTRUCTOR)
5863 HOST_WIDE_INT nz_elts, init_elts;
5864 bool complete_p;
5866 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5867 return nz_elts == 0;
5870 return initializer_zerop (exp);
5873 /* Helper function for store_constructor.
5874 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5875 CLEARED is as for store_constructor.
5876 ALIAS_SET is the alias set to use for any stores.
5878 This provides a recursive shortcut back to store_constructor when it isn't
5879 necessary to go through store_field. This is so that we can pass through
5880 the cleared field to let store_constructor know that we may not have to
5881 clear a substructure if the outer structure has already been cleared. */
5883 static void
5884 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5885 HOST_WIDE_INT bitpos, machine_mode mode,
5886 tree exp, int cleared, alias_set_type alias_set)
5888 if (TREE_CODE (exp) == CONSTRUCTOR
5889 /* We can only call store_constructor recursively if the size and
5890 bit position are on a byte boundary. */
5891 && bitpos % BITS_PER_UNIT == 0
5892 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5893 /* If we have a nonzero bitpos for a register target, then we just
5894 let store_field do the bitfield handling. This is unlikely to
5895 generate unnecessary clear instructions anyways. */
5896 && (bitpos == 0 || MEM_P (target)))
5898 if (MEM_P (target))
5899 target
5900 = adjust_address (target,
5901 GET_MODE (target) == BLKmode
5902 || 0 != (bitpos
5903 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5904 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5907 /* Update the alias set, if required. */
5908 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5909 && MEM_ALIAS_SET (target) != 0)
5911 target = copy_rtx (target);
5912 set_mem_alias_set (target, alias_set);
5915 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5917 else
5918 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5922 /* Returns the number of FIELD_DECLs in TYPE. */
5924 static int
5925 fields_length (const_tree type)
5927 tree t = TYPE_FIELDS (type);
5928 int count = 0;
5930 for (; t; t = DECL_CHAIN (t))
5931 if (TREE_CODE (t) == FIELD_DECL)
5932 ++count;
5934 return count;
5938 /* Store the value of constructor EXP into the rtx TARGET.
5939 TARGET is either a REG or a MEM; we know it cannot conflict, since
5940 safe_from_p has been called.
5941 CLEARED is true if TARGET is known to have been zero'd.
5942 SIZE is the number of bytes of TARGET we are allowed to modify: this
5943 may not be the same as the size of EXP if we are assigning to a field
5944 which has been packed to exclude padding bits. */
5946 static void
5947 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5949 tree type = TREE_TYPE (exp);
5950 #ifdef WORD_REGISTER_OPERATIONS
5951 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5952 #endif
5954 switch (TREE_CODE (type))
5956 case RECORD_TYPE:
5957 case UNION_TYPE:
5958 case QUAL_UNION_TYPE:
5960 unsigned HOST_WIDE_INT idx;
5961 tree field, value;
5963 /* If size is zero or the target is already cleared, do nothing. */
5964 if (size == 0 || cleared)
5965 cleared = 1;
5966 /* We either clear the aggregate or indicate the value is dead. */
5967 else if ((TREE_CODE (type) == UNION_TYPE
5968 || TREE_CODE (type) == QUAL_UNION_TYPE)
5969 && ! CONSTRUCTOR_ELTS (exp))
5970 /* If the constructor is empty, clear the union. */
5972 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5973 cleared = 1;
5976 /* If we are building a static constructor into a register,
5977 set the initial value as zero so we can fold the value into
5978 a constant. But if more than one register is involved,
5979 this probably loses. */
5980 else if (REG_P (target) && TREE_STATIC (exp)
5981 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5983 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5984 cleared = 1;
5987 /* If the constructor has fewer fields than the structure or
5988 if we are initializing the structure to mostly zeros, clear
5989 the whole structure first. Don't do this if TARGET is a
5990 register whose mode size isn't equal to SIZE since
5991 clear_storage can't handle this case. */
5992 else if (size > 0
5993 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
5994 != fields_length (type))
5995 || mostly_zeros_p (exp))
5996 && (!REG_P (target)
5997 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5998 == size)))
6000 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6001 cleared = 1;
6004 if (REG_P (target) && !cleared)
6005 emit_clobber (target);
6007 /* Store each element of the constructor into the
6008 corresponding field of TARGET. */
6009 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6011 machine_mode mode;
6012 HOST_WIDE_INT bitsize;
6013 HOST_WIDE_INT bitpos = 0;
6014 tree offset;
6015 rtx to_rtx = target;
6017 /* Just ignore missing fields. We cleared the whole
6018 structure, above, if any fields are missing. */
6019 if (field == 0)
6020 continue;
6022 if (cleared && initializer_zerop (value))
6023 continue;
6025 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6026 bitsize = tree_to_uhwi (DECL_SIZE (field));
6027 else
6028 bitsize = -1;
6030 mode = DECL_MODE (field);
6031 if (DECL_BIT_FIELD (field))
6032 mode = VOIDmode;
6034 offset = DECL_FIELD_OFFSET (field);
6035 if (tree_fits_shwi_p (offset)
6036 && tree_fits_shwi_p (bit_position (field)))
6038 bitpos = int_bit_position (field);
6039 offset = 0;
6041 else
6042 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6044 if (offset)
6046 machine_mode address_mode;
6047 rtx offset_rtx;
6049 offset
6050 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6051 make_tree (TREE_TYPE (exp),
6052 target));
6054 offset_rtx = expand_normal (offset);
6055 gcc_assert (MEM_P (to_rtx));
6057 address_mode = get_address_mode (to_rtx);
6058 if (GET_MODE (offset_rtx) != address_mode)
6059 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6061 to_rtx = offset_address (to_rtx, offset_rtx,
6062 highest_pow2_factor (offset));
6065 #ifdef WORD_REGISTER_OPERATIONS
6066 /* If this initializes a field that is smaller than a
6067 word, at the start of a word, try to widen it to a full
6068 word. This special case allows us to output C++ member
6069 function initializations in a form that the optimizers
6070 can understand. */
6071 if (REG_P (target)
6072 && bitsize < BITS_PER_WORD
6073 && bitpos % BITS_PER_WORD == 0
6074 && GET_MODE_CLASS (mode) == MODE_INT
6075 && TREE_CODE (value) == INTEGER_CST
6076 && exp_size >= 0
6077 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6079 tree type = TREE_TYPE (value);
6081 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6083 type = lang_hooks.types.type_for_mode
6084 (word_mode, TYPE_UNSIGNED (type));
6085 value = fold_convert (type, value);
6088 if (BYTES_BIG_ENDIAN)
6089 value
6090 = fold_build2 (LSHIFT_EXPR, type, value,
6091 build_int_cst (type,
6092 BITS_PER_WORD - bitsize));
6093 bitsize = BITS_PER_WORD;
6094 mode = word_mode;
6096 #endif
6098 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6099 && DECL_NONADDRESSABLE_P (field))
6101 to_rtx = copy_rtx (to_rtx);
6102 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6105 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6106 value, cleared,
6107 get_alias_set (TREE_TYPE (field)));
6109 break;
6111 case ARRAY_TYPE:
6113 tree value, index;
6114 unsigned HOST_WIDE_INT i;
6115 int need_to_clear;
6116 tree domain;
6117 tree elttype = TREE_TYPE (type);
6118 int const_bounds_p;
6119 HOST_WIDE_INT minelt = 0;
6120 HOST_WIDE_INT maxelt = 0;
6122 domain = TYPE_DOMAIN (type);
6123 const_bounds_p = (TYPE_MIN_VALUE (domain)
6124 && TYPE_MAX_VALUE (domain)
6125 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6126 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6128 /* If we have constant bounds for the range of the type, get them. */
6129 if (const_bounds_p)
6131 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6132 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6135 /* If the constructor has fewer elements than the array, clear
6136 the whole array first. Similarly if this is static
6137 constructor of a non-BLKmode object. */
6138 if (cleared)
6139 need_to_clear = 0;
6140 else if (REG_P (target) && TREE_STATIC (exp))
6141 need_to_clear = 1;
6142 else
6144 unsigned HOST_WIDE_INT idx;
6145 tree index, value;
6146 HOST_WIDE_INT count = 0, zero_count = 0;
6147 need_to_clear = ! const_bounds_p;
6149 /* This loop is a more accurate version of the loop in
6150 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6151 is also needed to check for missing elements. */
6152 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6154 HOST_WIDE_INT this_node_count;
6156 if (need_to_clear)
6157 break;
6159 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6161 tree lo_index = TREE_OPERAND (index, 0);
6162 tree hi_index = TREE_OPERAND (index, 1);
6164 if (! tree_fits_uhwi_p (lo_index)
6165 || ! tree_fits_uhwi_p (hi_index))
6167 need_to_clear = 1;
6168 break;
6171 this_node_count = (tree_to_uhwi (hi_index)
6172 - tree_to_uhwi (lo_index) + 1);
6174 else
6175 this_node_count = 1;
6177 count += this_node_count;
6178 if (mostly_zeros_p (value))
6179 zero_count += this_node_count;
6182 /* Clear the entire array first if there are any missing
6183 elements, or if the incidence of zero elements is >=
6184 75%. */
6185 if (! need_to_clear
6186 && (count < maxelt - minelt + 1
6187 || 4 * zero_count >= 3 * count))
6188 need_to_clear = 1;
6191 if (need_to_clear && size > 0)
6193 if (REG_P (target))
6194 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6195 else
6196 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6197 cleared = 1;
6200 if (!cleared && REG_P (target))
6201 /* Inform later passes that the old value is dead. */
6202 emit_clobber (target);
6204 /* Store each element of the constructor into the
6205 corresponding element of TARGET, determined by counting the
6206 elements. */
6207 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6209 machine_mode mode;
6210 HOST_WIDE_INT bitsize;
6211 HOST_WIDE_INT bitpos;
6212 rtx xtarget = target;
6214 if (cleared && initializer_zerop (value))
6215 continue;
6217 mode = TYPE_MODE (elttype);
6218 if (mode == BLKmode)
6219 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6220 ? tree_to_uhwi (TYPE_SIZE (elttype))
6221 : -1);
6222 else
6223 bitsize = GET_MODE_BITSIZE (mode);
6225 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6227 tree lo_index = TREE_OPERAND (index, 0);
6228 tree hi_index = TREE_OPERAND (index, 1);
6229 rtx index_r, pos_rtx;
6230 HOST_WIDE_INT lo, hi, count;
6231 tree position;
6233 /* If the range is constant and "small", unroll the loop. */
6234 if (const_bounds_p
6235 && tree_fits_shwi_p (lo_index)
6236 && tree_fits_shwi_p (hi_index)
6237 && (lo = tree_to_shwi (lo_index),
6238 hi = tree_to_shwi (hi_index),
6239 count = hi - lo + 1,
6240 (!MEM_P (target)
6241 || count <= 2
6242 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6243 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6244 <= 40 * 8)))))
6246 lo -= minelt; hi -= minelt;
6247 for (; lo <= hi; lo++)
6249 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6251 if (MEM_P (target)
6252 && !MEM_KEEP_ALIAS_SET_P (target)
6253 && TREE_CODE (type) == ARRAY_TYPE
6254 && TYPE_NONALIASED_COMPONENT (type))
6256 target = copy_rtx (target);
6257 MEM_KEEP_ALIAS_SET_P (target) = 1;
6260 store_constructor_field
6261 (target, bitsize, bitpos, mode, value, cleared,
6262 get_alias_set (elttype));
6265 else
6267 rtx_code_label *loop_start = gen_label_rtx ();
6268 rtx_code_label *loop_end = gen_label_rtx ();
6269 tree exit_cond;
6271 expand_normal (hi_index);
6273 index = build_decl (EXPR_LOCATION (exp),
6274 VAR_DECL, NULL_TREE, domain);
6275 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6276 SET_DECL_RTL (index, index_r);
6277 store_expr (lo_index, index_r, 0, false);
6279 /* Build the head of the loop. */
6280 do_pending_stack_adjust ();
6281 emit_label (loop_start);
6283 /* Assign value to element index. */
6284 position =
6285 fold_convert (ssizetype,
6286 fold_build2 (MINUS_EXPR,
6287 TREE_TYPE (index),
6288 index,
6289 TYPE_MIN_VALUE (domain)));
6291 position =
6292 size_binop (MULT_EXPR, position,
6293 fold_convert (ssizetype,
6294 TYPE_SIZE_UNIT (elttype)));
6296 pos_rtx = expand_normal (position);
6297 xtarget = offset_address (target, pos_rtx,
6298 highest_pow2_factor (position));
6299 xtarget = adjust_address (xtarget, mode, 0);
6300 if (TREE_CODE (value) == CONSTRUCTOR)
6301 store_constructor (value, xtarget, cleared,
6302 bitsize / BITS_PER_UNIT);
6303 else
6304 store_expr (value, xtarget, 0, false);
6306 /* Generate a conditional jump to exit the loop. */
6307 exit_cond = build2 (LT_EXPR, integer_type_node,
6308 index, hi_index);
6309 jumpif (exit_cond, loop_end, -1);
6311 /* Update the loop counter, and jump to the head of
6312 the loop. */
6313 expand_assignment (index,
6314 build2 (PLUS_EXPR, TREE_TYPE (index),
6315 index, integer_one_node),
6316 false);
6318 emit_jump (loop_start);
6320 /* Build the end of the loop. */
6321 emit_label (loop_end);
6324 else if ((index != 0 && ! tree_fits_shwi_p (index))
6325 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6327 tree position;
6329 if (index == 0)
6330 index = ssize_int (1);
6332 if (minelt)
6333 index = fold_convert (ssizetype,
6334 fold_build2 (MINUS_EXPR,
6335 TREE_TYPE (index),
6336 index,
6337 TYPE_MIN_VALUE (domain)));
6339 position =
6340 size_binop (MULT_EXPR, index,
6341 fold_convert (ssizetype,
6342 TYPE_SIZE_UNIT (elttype)));
6343 xtarget = offset_address (target,
6344 expand_normal (position),
6345 highest_pow2_factor (position));
6346 xtarget = adjust_address (xtarget, mode, 0);
6347 store_expr (value, xtarget, 0, false);
6349 else
6351 if (index != 0)
6352 bitpos = ((tree_to_shwi (index) - minelt)
6353 * tree_to_uhwi (TYPE_SIZE (elttype)));
6354 else
6355 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6357 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6358 && TREE_CODE (type) == ARRAY_TYPE
6359 && TYPE_NONALIASED_COMPONENT (type))
6361 target = copy_rtx (target);
6362 MEM_KEEP_ALIAS_SET_P (target) = 1;
6364 store_constructor_field (target, bitsize, bitpos, mode, value,
6365 cleared, get_alias_set (elttype));
6368 break;
6371 case VECTOR_TYPE:
6373 unsigned HOST_WIDE_INT idx;
6374 constructor_elt *ce;
6375 int i;
6376 int need_to_clear;
6377 int icode = CODE_FOR_nothing;
6378 tree elttype = TREE_TYPE (type);
6379 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6380 machine_mode eltmode = TYPE_MODE (elttype);
6381 HOST_WIDE_INT bitsize;
6382 HOST_WIDE_INT bitpos;
6383 rtvec vector = NULL;
6384 unsigned n_elts;
6385 alias_set_type alias;
6387 gcc_assert (eltmode != BLKmode);
6389 n_elts = TYPE_VECTOR_SUBPARTS (type);
6390 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6392 machine_mode mode = GET_MODE (target);
6394 icode = (int) optab_handler (vec_init_optab, mode);
6395 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6396 if (icode != CODE_FOR_nothing)
6398 tree value;
6400 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6401 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6403 icode = CODE_FOR_nothing;
6404 break;
6407 if (icode != CODE_FOR_nothing)
6409 unsigned int i;
6411 vector = rtvec_alloc (n_elts);
6412 for (i = 0; i < n_elts; i++)
6413 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6417 /* If the constructor has fewer elements than the vector,
6418 clear the whole array first. Similarly if this is static
6419 constructor of a non-BLKmode object. */
6420 if (cleared)
6421 need_to_clear = 0;
6422 else if (REG_P (target) && TREE_STATIC (exp))
6423 need_to_clear = 1;
6424 else
6426 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6427 tree value;
6429 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6431 int n_elts_here = tree_to_uhwi
6432 (int_const_binop (TRUNC_DIV_EXPR,
6433 TYPE_SIZE (TREE_TYPE (value)),
6434 TYPE_SIZE (elttype)));
6436 count += n_elts_here;
6437 if (mostly_zeros_p (value))
6438 zero_count += n_elts_here;
6441 /* Clear the entire vector first if there are any missing elements,
6442 or if the incidence of zero elements is >= 75%. */
6443 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6446 if (need_to_clear && size > 0 && !vector)
6448 if (REG_P (target))
6449 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6450 else
6451 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6452 cleared = 1;
6455 /* Inform later passes that the old value is dead. */
6456 if (!cleared && !vector && REG_P (target))
6457 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6459 if (MEM_P (target))
6460 alias = MEM_ALIAS_SET (target);
6461 else
6462 alias = get_alias_set (elttype);
6464 /* Store each element of the constructor into the corresponding
6465 element of TARGET, determined by counting the elements. */
6466 for (idx = 0, i = 0;
6467 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6468 idx++, i += bitsize / elt_size)
6470 HOST_WIDE_INT eltpos;
6471 tree value = ce->value;
6473 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6474 if (cleared && initializer_zerop (value))
6475 continue;
6477 if (ce->index)
6478 eltpos = tree_to_uhwi (ce->index);
6479 else
6480 eltpos = i;
6482 if (vector)
6484 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6485 elements. */
6486 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6487 RTVEC_ELT (vector, eltpos)
6488 = expand_normal (value);
6490 else
6492 machine_mode value_mode =
6493 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6494 ? TYPE_MODE (TREE_TYPE (value))
6495 : eltmode;
6496 bitpos = eltpos * elt_size;
6497 store_constructor_field (target, bitsize, bitpos, value_mode,
6498 value, cleared, alias);
6502 if (vector)
6503 emit_insn (GEN_FCN (icode)
6504 (target,
6505 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6506 break;
6509 default:
6510 gcc_unreachable ();
6514 /* Store the value of EXP (an expression tree)
6515 into a subfield of TARGET which has mode MODE and occupies
6516 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6517 If MODE is VOIDmode, it means that we are storing into a bit-field.
6519 BITREGION_START is bitpos of the first bitfield in this region.
6520 BITREGION_END is the bitpos of the ending bitfield in this region.
6521 These two fields are 0, if the C++ memory model does not apply,
6522 or we are not interested in keeping track of bitfield regions.
6524 Always return const0_rtx unless we have something particular to
6525 return.
6527 ALIAS_SET is the alias set for the destination. This value will
6528 (in general) be different from that for TARGET, since TARGET is a
6529 reference to the containing structure.
6531 If NONTEMPORAL is true, try generating a nontemporal store. */
6533 static rtx
6534 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6535 unsigned HOST_WIDE_INT bitregion_start,
6536 unsigned HOST_WIDE_INT bitregion_end,
6537 machine_mode mode, tree exp,
6538 alias_set_type alias_set, bool nontemporal)
6540 if (TREE_CODE (exp) == ERROR_MARK)
6541 return const0_rtx;
6543 /* If we have nothing to store, do nothing unless the expression has
6544 side-effects. */
6545 if (bitsize == 0)
6546 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6548 if (GET_CODE (target) == CONCAT)
6550 /* We're storing into a struct containing a single __complex. */
6552 gcc_assert (!bitpos);
6553 return store_expr (exp, target, 0, nontemporal);
6556 /* If the structure is in a register or if the component
6557 is a bit field, we cannot use addressing to access it.
6558 Use bit-field techniques or SUBREG to store in it. */
6560 if (mode == VOIDmode
6561 || (mode != BLKmode && ! direct_store[(int) mode]
6562 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6563 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6564 || REG_P (target)
6565 || GET_CODE (target) == SUBREG
6566 /* If the field isn't aligned enough to store as an ordinary memref,
6567 store it as a bit field. */
6568 || (mode != BLKmode
6569 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6570 || bitpos % GET_MODE_ALIGNMENT (mode))
6571 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6572 || (bitpos % BITS_PER_UNIT != 0)))
6573 || (bitsize >= 0 && mode != BLKmode
6574 && GET_MODE_BITSIZE (mode) > bitsize)
6575 /* If the RHS and field are a constant size and the size of the
6576 RHS isn't the same size as the bitfield, we must use bitfield
6577 operations. */
6578 || (bitsize >= 0
6579 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6580 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6581 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6582 decl we must use bitfield operations. */
6583 || (bitsize >= 0
6584 && TREE_CODE (exp) == MEM_REF
6585 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6586 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6587 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6588 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6590 rtx temp;
6591 gimple nop_def;
6593 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6594 implies a mask operation. If the precision is the same size as
6595 the field we're storing into, that mask is redundant. This is
6596 particularly common with bit field assignments generated by the
6597 C front end. */
6598 nop_def = get_def_for_expr (exp, NOP_EXPR);
6599 if (nop_def)
6601 tree type = TREE_TYPE (exp);
6602 if (INTEGRAL_TYPE_P (type)
6603 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6604 && bitsize == TYPE_PRECISION (type))
6606 tree op = gimple_assign_rhs1 (nop_def);
6607 type = TREE_TYPE (op);
6608 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6609 exp = op;
6613 temp = expand_normal (exp);
6615 /* If BITSIZE is narrower than the size of the type of EXP
6616 we will be narrowing TEMP. Normally, what's wanted are the
6617 low-order bits. However, if EXP's type is a record and this is
6618 big-endian machine, we want the upper BITSIZE bits. */
6619 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6620 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6621 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6622 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6623 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6624 NULL_RTX, 1);
6626 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6627 if (mode != VOIDmode && mode != BLKmode
6628 && mode != TYPE_MODE (TREE_TYPE (exp)))
6629 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6631 /* If the modes of TEMP and TARGET are both BLKmode, both
6632 must be in memory and BITPOS must be aligned on a byte
6633 boundary. If so, we simply do a block copy. Likewise
6634 for a BLKmode-like TARGET. */
6635 if (GET_MODE (temp) == BLKmode
6636 && (GET_MODE (target) == BLKmode
6637 || (MEM_P (target)
6638 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6639 && (bitpos % BITS_PER_UNIT) == 0
6640 && (bitsize % BITS_PER_UNIT) == 0)))
6642 gcc_assert (MEM_P (target) && MEM_P (temp)
6643 && (bitpos % BITS_PER_UNIT) == 0);
6645 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6646 emit_block_move (target, temp,
6647 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6648 / BITS_PER_UNIT),
6649 BLOCK_OP_NORMAL);
6651 return const0_rtx;
6654 /* Handle calls that return values in multiple non-contiguous locations.
6655 The Irix 6 ABI has examples of this. */
6656 if (GET_CODE (temp) == PARALLEL)
6658 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6659 rtx temp_target;
6660 if (mode == BLKmode || mode == VOIDmode)
6661 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6662 temp_target = gen_reg_rtx (mode);
6663 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6664 temp = temp_target;
6666 else if (mode == BLKmode)
6668 /* Handle calls that return BLKmode values in registers. */
6669 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6671 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6672 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6673 temp = temp_target;
6675 else
6677 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6678 rtx temp_target;
6679 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6680 temp_target = gen_reg_rtx (mode);
6681 temp_target
6682 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6683 temp_target, mode, mode);
6684 temp = temp_target;
6688 /* Store the value in the bitfield. */
6689 store_bit_field (target, bitsize, bitpos,
6690 bitregion_start, bitregion_end,
6691 mode, temp);
6693 return const0_rtx;
6695 else
6697 /* Now build a reference to just the desired component. */
6698 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6700 if (to_rtx == target)
6701 to_rtx = copy_rtx (to_rtx);
6703 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6704 set_mem_alias_set (to_rtx, alias_set);
6706 return store_expr (exp, to_rtx, 0, nontemporal);
6710 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6711 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6712 codes and find the ultimate containing object, which we return.
6714 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6715 bit position, and *PUNSIGNEDP to the signedness of the field.
6716 If the position of the field is variable, we store a tree
6717 giving the variable offset (in units) in *POFFSET.
6718 This offset is in addition to the bit position.
6719 If the position is not variable, we store 0 in *POFFSET.
6721 If any of the extraction expressions is volatile,
6722 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6724 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6725 Otherwise, it is a mode that can be used to access the field.
6727 If the field describes a variable-sized object, *PMODE is set to
6728 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6729 this case, but the address of the object can be found.
6731 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6732 look through nodes that serve as markers of a greater alignment than
6733 the one that can be deduced from the expression. These nodes make it
6734 possible for front-ends to prevent temporaries from being created by
6735 the middle-end on alignment considerations. For that purpose, the
6736 normal operating mode at high-level is to always pass FALSE so that
6737 the ultimate containing object is really returned; moreover, the
6738 associated predicate handled_component_p will always return TRUE
6739 on these nodes, thus indicating that they are essentially handled
6740 by get_inner_reference. TRUE should only be passed when the caller
6741 is scanning the expression in order to build another representation
6742 and specifically knows how to handle these nodes; as such, this is
6743 the normal operating mode in the RTL expanders. */
6745 tree
6746 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6747 HOST_WIDE_INT *pbitpos, tree *poffset,
6748 machine_mode *pmode, int *punsignedp,
6749 int *pvolatilep, bool keep_aligning)
6751 tree size_tree = 0;
6752 machine_mode mode = VOIDmode;
6753 bool blkmode_bitfield = false;
6754 tree offset = size_zero_node;
6755 offset_int bit_offset = 0;
6757 /* First get the mode, signedness, and size. We do this from just the
6758 outermost expression. */
6759 *pbitsize = -1;
6760 if (TREE_CODE (exp) == COMPONENT_REF)
6762 tree field = TREE_OPERAND (exp, 1);
6763 size_tree = DECL_SIZE (field);
6764 if (flag_strict_volatile_bitfields > 0
6765 && TREE_THIS_VOLATILE (exp)
6766 && DECL_BIT_FIELD_TYPE (field)
6767 && DECL_MODE (field) != BLKmode)
6768 /* Volatile bitfields should be accessed in the mode of the
6769 field's type, not the mode computed based on the bit
6770 size. */
6771 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6772 else if (!DECL_BIT_FIELD (field))
6773 mode = DECL_MODE (field);
6774 else if (DECL_MODE (field) == BLKmode)
6775 blkmode_bitfield = true;
6777 *punsignedp = DECL_UNSIGNED (field);
6779 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6781 size_tree = TREE_OPERAND (exp, 1);
6782 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6783 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6785 /* For vector types, with the correct size of access, use the mode of
6786 inner type. */
6787 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6788 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6789 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6790 mode = TYPE_MODE (TREE_TYPE (exp));
6792 else
6794 mode = TYPE_MODE (TREE_TYPE (exp));
6795 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6797 if (mode == BLKmode)
6798 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6799 else
6800 *pbitsize = GET_MODE_BITSIZE (mode);
6803 if (size_tree != 0)
6805 if (! tree_fits_uhwi_p (size_tree))
6806 mode = BLKmode, *pbitsize = -1;
6807 else
6808 *pbitsize = tree_to_uhwi (size_tree);
6811 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6812 and find the ultimate containing object. */
6813 while (1)
6815 switch (TREE_CODE (exp))
6817 case BIT_FIELD_REF:
6818 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6819 break;
6821 case COMPONENT_REF:
6823 tree field = TREE_OPERAND (exp, 1);
6824 tree this_offset = component_ref_field_offset (exp);
6826 /* If this field hasn't been filled in yet, don't go past it.
6827 This should only happen when folding expressions made during
6828 type construction. */
6829 if (this_offset == 0)
6830 break;
6832 offset = size_binop (PLUS_EXPR, offset, this_offset);
6833 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6835 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6837 break;
6839 case ARRAY_REF:
6840 case ARRAY_RANGE_REF:
6842 tree index = TREE_OPERAND (exp, 1);
6843 tree low_bound = array_ref_low_bound (exp);
6844 tree unit_size = array_ref_element_size (exp);
6846 /* We assume all arrays have sizes that are a multiple of a byte.
6847 First subtract the lower bound, if any, in the type of the
6848 index, then convert to sizetype and multiply by the size of
6849 the array element. */
6850 if (! integer_zerop (low_bound))
6851 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6852 index, low_bound);
6854 offset = size_binop (PLUS_EXPR, offset,
6855 size_binop (MULT_EXPR,
6856 fold_convert (sizetype, index),
6857 unit_size));
6859 break;
6861 case REALPART_EXPR:
6862 break;
6864 case IMAGPART_EXPR:
6865 bit_offset += *pbitsize;
6866 break;
6868 case VIEW_CONVERT_EXPR:
6869 if (keep_aligning && STRICT_ALIGNMENT
6870 && (TYPE_ALIGN (TREE_TYPE (exp))
6871 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6872 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6873 < BIGGEST_ALIGNMENT)
6874 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6875 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6876 goto done;
6877 break;
6879 case MEM_REF:
6880 /* Hand back the decl for MEM[&decl, off]. */
6881 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6883 tree off = TREE_OPERAND (exp, 1);
6884 if (!integer_zerop (off))
6886 offset_int boff, coff = mem_ref_offset (exp);
6887 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6888 bit_offset += boff;
6890 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6892 goto done;
6894 default:
6895 goto done;
6898 /* If any reference in the chain is volatile, the effect is volatile. */
6899 if (TREE_THIS_VOLATILE (exp))
6900 *pvolatilep = 1;
6902 exp = TREE_OPERAND (exp, 0);
6904 done:
6906 /* If OFFSET is constant, see if we can return the whole thing as a
6907 constant bit position. Make sure to handle overflow during
6908 this conversion. */
6909 if (TREE_CODE (offset) == INTEGER_CST)
6911 offset_int tem = wi::sext (wi::to_offset (offset),
6912 TYPE_PRECISION (sizetype));
6913 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6914 tem += bit_offset;
6915 if (wi::fits_shwi_p (tem))
6917 *pbitpos = tem.to_shwi ();
6918 *poffset = offset = NULL_TREE;
6922 /* Otherwise, split it up. */
6923 if (offset)
6925 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6926 if (wi::neg_p (bit_offset))
6928 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6929 offset_int tem = bit_offset.and_not (mask);
6930 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6931 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6932 bit_offset -= tem;
6933 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6934 offset = size_binop (PLUS_EXPR, offset,
6935 wide_int_to_tree (sizetype, tem));
6938 *pbitpos = bit_offset.to_shwi ();
6939 *poffset = offset;
6942 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6943 if (mode == VOIDmode
6944 && blkmode_bitfield
6945 && (*pbitpos % BITS_PER_UNIT) == 0
6946 && (*pbitsize % BITS_PER_UNIT) == 0)
6947 *pmode = BLKmode;
6948 else
6949 *pmode = mode;
6951 return exp;
6954 /* Return a tree of sizetype representing the size, in bytes, of the element
6955 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6957 tree
6958 array_ref_element_size (tree exp)
6960 tree aligned_size = TREE_OPERAND (exp, 3);
6961 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6962 location_t loc = EXPR_LOCATION (exp);
6964 /* If a size was specified in the ARRAY_REF, it's the size measured
6965 in alignment units of the element type. So multiply by that value. */
6966 if (aligned_size)
6968 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6969 sizetype from another type of the same width and signedness. */
6970 if (TREE_TYPE (aligned_size) != sizetype)
6971 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6972 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6973 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6976 /* Otherwise, take the size from that of the element type. Substitute
6977 any PLACEHOLDER_EXPR that we have. */
6978 else
6979 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6982 /* Return a tree representing the lower bound of the array mentioned in
6983 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6985 tree
6986 array_ref_low_bound (tree exp)
6988 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6990 /* If a lower bound is specified in EXP, use it. */
6991 if (TREE_OPERAND (exp, 2))
6992 return TREE_OPERAND (exp, 2);
6994 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6995 substituting for a PLACEHOLDER_EXPR as needed. */
6996 if (domain_type && TYPE_MIN_VALUE (domain_type))
6997 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6999 /* Otherwise, return a zero of the appropriate type. */
7000 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
7003 /* Returns true if REF is an array reference to an array at the end of
7004 a structure. If this is the case, the array may be allocated larger
7005 than its upper bound implies. */
7007 bool
7008 array_at_struct_end_p (tree ref)
7010 if (TREE_CODE (ref) != ARRAY_REF
7011 && TREE_CODE (ref) != ARRAY_RANGE_REF)
7012 return false;
7014 while (handled_component_p (ref))
7016 /* If the reference chain contains a component reference to a
7017 non-union type and there follows another field the reference
7018 is not at the end of a structure. */
7019 if (TREE_CODE (ref) == COMPONENT_REF
7020 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
7022 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
7023 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
7024 nextf = DECL_CHAIN (nextf);
7025 if (nextf)
7026 return false;
7029 ref = TREE_OPERAND (ref, 0);
7032 /* If the reference is based on a declared entity, the size of the array
7033 is constrained by its given domain. */
7034 if (DECL_P (ref))
7035 return false;
7037 return true;
7040 /* Return a tree representing the upper bound of the array mentioned in
7041 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
7043 tree
7044 array_ref_up_bound (tree exp)
7046 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
7048 /* If there is a domain type and it has an upper bound, use it, substituting
7049 for a PLACEHOLDER_EXPR as needed. */
7050 if (domain_type && TYPE_MAX_VALUE (domain_type))
7051 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
7053 /* Otherwise fail. */
7054 return NULL_TREE;
7057 /* Return a tree representing the offset, in bytes, of the field referenced
7058 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
7060 tree
7061 component_ref_field_offset (tree exp)
7063 tree aligned_offset = TREE_OPERAND (exp, 2);
7064 tree field = TREE_OPERAND (exp, 1);
7065 location_t loc = EXPR_LOCATION (exp);
7067 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
7068 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
7069 value. */
7070 if (aligned_offset)
7072 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7073 sizetype from another type of the same width and signedness. */
7074 if (TREE_TYPE (aligned_offset) != sizetype)
7075 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
7076 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
7077 size_int (DECL_OFFSET_ALIGN (field)
7078 / BITS_PER_UNIT));
7081 /* Otherwise, take the offset from that of the field. Substitute
7082 any PLACEHOLDER_EXPR that we have. */
7083 else
7084 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
7087 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7089 static unsigned HOST_WIDE_INT
7090 target_align (const_tree target)
7092 /* We might have a chain of nested references with intermediate misaligning
7093 bitfields components, so need to recurse to find out. */
7095 unsigned HOST_WIDE_INT this_align, outer_align;
7097 switch (TREE_CODE (target))
7099 case BIT_FIELD_REF:
7100 return 1;
7102 case COMPONENT_REF:
7103 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7104 outer_align = target_align (TREE_OPERAND (target, 0));
7105 return MIN (this_align, outer_align);
7107 case ARRAY_REF:
7108 case ARRAY_RANGE_REF:
7109 this_align = TYPE_ALIGN (TREE_TYPE (target));
7110 outer_align = target_align (TREE_OPERAND (target, 0));
7111 return MIN (this_align, outer_align);
7113 CASE_CONVERT:
7114 case NON_LVALUE_EXPR:
7115 case VIEW_CONVERT_EXPR:
7116 this_align = TYPE_ALIGN (TREE_TYPE (target));
7117 outer_align = target_align (TREE_OPERAND (target, 0));
7118 return MAX (this_align, outer_align);
7120 default:
7121 return TYPE_ALIGN (TREE_TYPE (target));
7126 /* Given an rtx VALUE that may contain additions and multiplications, return
7127 an equivalent value that just refers to a register, memory, or constant.
7128 This is done by generating instructions to perform the arithmetic and
7129 returning a pseudo-register containing the value.
7131 The returned value may be a REG, SUBREG, MEM or constant. */
7134 force_operand (rtx value, rtx target)
7136 rtx op1, op2;
7137 /* Use subtarget as the target for operand 0 of a binary operation. */
7138 rtx subtarget = get_subtarget (target);
7139 enum rtx_code code = GET_CODE (value);
7141 /* Check for subreg applied to an expression produced by loop optimizer. */
7142 if (code == SUBREG
7143 && !REG_P (SUBREG_REG (value))
7144 && !MEM_P (SUBREG_REG (value)))
7146 value
7147 = simplify_gen_subreg (GET_MODE (value),
7148 force_reg (GET_MODE (SUBREG_REG (value)),
7149 force_operand (SUBREG_REG (value),
7150 NULL_RTX)),
7151 GET_MODE (SUBREG_REG (value)),
7152 SUBREG_BYTE (value));
7153 code = GET_CODE (value);
7156 /* Check for a PIC address load. */
7157 if ((code == PLUS || code == MINUS)
7158 && XEXP (value, 0) == pic_offset_table_rtx
7159 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7160 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7161 || GET_CODE (XEXP (value, 1)) == CONST))
7163 if (!subtarget)
7164 subtarget = gen_reg_rtx (GET_MODE (value));
7165 emit_move_insn (subtarget, value);
7166 return subtarget;
7169 if (ARITHMETIC_P (value))
7171 op2 = XEXP (value, 1);
7172 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7173 subtarget = 0;
7174 if (code == MINUS && CONST_INT_P (op2))
7176 code = PLUS;
7177 op2 = negate_rtx (GET_MODE (value), op2);
7180 /* Check for an addition with OP2 a constant integer and our first
7181 operand a PLUS of a virtual register and something else. In that
7182 case, we want to emit the sum of the virtual register and the
7183 constant first and then add the other value. This allows virtual
7184 register instantiation to simply modify the constant rather than
7185 creating another one around this addition. */
7186 if (code == PLUS && CONST_INT_P (op2)
7187 && GET_CODE (XEXP (value, 0)) == PLUS
7188 && REG_P (XEXP (XEXP (value, 0), 0))
7189 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7190 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7192 rtx temp = expand_simple_binop (GET_MODE (value), code,
7193 XEXP (XEXP (value, 0), 0), op2,
7194 subtarget, 0, OPTAB_LIB_WIDEN);
7195 return expand_simple_binop (GET_MODE (value), code, temp,
7196 force_operand (XEXP (XEXP (value,
7197 0), 1), 0),
7198 target, 0, OPTAB_LIB_WIDEN);
7201 op1 = force_operand (XEXP (value, 0), subtarget);
7202 op2 = force_operand (op2, NULL_RTX);
7203 switch (code)
7205 case MULT:
7206 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7207 case DIV:
7208 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7209 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7210 target, 1, OPTAB_LIB_WIDEN);
7211 else
7212 return expand_divmod (0,
7213 FLOAT_MODE_P (GET_MODE (value))
7214 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7215 GET_MODE (value), op1, op2, target, 0);
7216 case MOD:
7217 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7218 target, 0);
7219 case UDIV:
7220 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7221 target, 1);
7222 case UMOD:
7223 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7224 target, 1);
7225 case ASHIFTRT:
7226 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7227 target, 0, OPTAB_LIB_WIDEN);
7228 default:
7229 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7230 target, 1, OPTAB_LIB_WIDEN);
7233 if (UNARY_P (value))
7235 if (!target)
7236 target = gen_reg_rtx (GET_MODE (value));
7237 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7238 switch (code)
7240 case ZERO_EXTEND:
7241 case SIGN_EXTEND:
7242 case TRUNCATE:
7243 case FLOAT_EXTEND:
7244 case FLOAT_TRUNCATE:
7245 convert_move (target, op1, code == ZERO_EXTEND);
7246 return target;
7248 case FIX:
7249 case UNSIGNED_FIX:
7250 expand_fix (target, op1, code == UNSIGNED_FIX);
7251 return target;
7253 case FLOAT:
7254 case UNSIGNED_FLOAT:
7255 expand_float (target, op1, code == UNSIGNED_FLOAT);
7256 return target;
7258 default:
7259 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7263 #ifdef INSN_SCHEDULING
7264 /* On machines that have insn scheduling, we want all memory reference to be
7265 explicit, so we need to deal with such paradoxical SUBREGs. */
7266 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7267 value
7268 = simplify_gen_subreg (GET_MODE (value),
7269 force_reg (GET_MODE (SUBREG_REG (value)),
7270 force_operand (SUBREG_REG (value),
7271 NULL_RTX)),
7272 GET_MODE (SUBREG_REG (value)),
7273 SUBREG_BYTE (value));
7274 #endif
7276 return value;
7279 /* Subroutine of expand_expr: return nonzero iff there is no way that
7280 EXP can reference X, which is being modified. TOP_P is nonzero if this
7281 call is going to be used to determine whether we need a temporary
7282 for EXP, as opposed to a recursive call to this function.
7284 It is always safe for this routine to return zero since it merely
7285 searches for optimization opportunities. */
7288 safe_from_p (const_rtx x, tree exp, int top_p)
7290 rtx exp_rtl = 0;
7291 int i, nops;
7293 if (x == 0
7294 /* If EXP has varying size, we MUST use a target since we currently
7295 have no way of allocating temporaries of variable size
7296 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7297 So we assume here that something at a higher level has prevented a
7298 clash. This is somewhat bogus, but the best we can do. Only
7299 do this when X is BLKmode and when we are at the top level. */
7300 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7301 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7302 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7303 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7304 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7305 != INTEGER_CST)
7306 && GET_MODE (x) == BLKmode)
7307 /* If X is in the outgoing argument area, it is always safe. */
7308 || (MEM_P (x)
7309 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7310 || (GET_CODE (XEXP (x, 0)) == PLUS
7311 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7312 return 1;
7314 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7315 find the underlying pseudo. */
7316 if (GET_CODE (x) == SUBREG)
7318 x = SUBREG_REG (x);
7319 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7320 return 0;
7323 /* Now look at our tree code and possibly recurse. */
7324 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7326 case tcc_declaration:
7327 exp_rtl = DECL_RTL_IF_SET (exp);
7328 break;
7330 case tcc_constant:
7331 return 1;
7333 case tcc_exceptional:
7334 if (TREE_CODE (exp) == TREE_LIST)
7336 while (1)
7338 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7339 return 0;
7340 exp = TREE_CHAIN (exp);
7341 if (!exp)
7342 return 1;
7343 if (TREE_CODE (exp) != TREE_LIST)
7344 return safe_from_p (x, exp, 0);
7347 else if (TREE_CODE (exp) == CONSTRUCTOR)
7349 constructor_elt *ce;
7350 unsigned HOST_WIDE_INT idx;
7352 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7353 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7354 || !safe_from_p (x, ce->value, 0))
7355 return 0;
7356 return 1;
7358 else if (TREE_CODE (exp) == ERROR_MARK)
7359 return 1; /* An already-visited SAVE_EXPR? */
7360 else
7361 return 0;
7363 case tcc_statement:
7364 /* The only case we look at here is the DECL_INITIAL inside a
7365 DECL_EXPR. */
7366 return (TREE_CODE (exp) != DECL_EXPR
7367 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7368 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7369 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7371 case tcc_binary:
7372 case tcc_comparison:
7373 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7374 return 0;
7375 /* Fall through. */
7377 case tcc_unary:
7378 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7380 case tcc_expression:
7381 case tcc_reference:
7382 case tcc_vl_exp:
7383 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7384 the expression. If it is set, we conflict iff we are that rtx or
7385 both are in memory. Otherwise, we check all operands of the
7386 expression recursively. */
7388 switch (TREE_CODE (exp))
7390 case ADDR_EXPR:
7391 /* If the operand is static or we are static, we can't conflict.
7392 Likewise if we don't conflict with the operand at all. */
7393 if (staticp (TREE_OPERAND (exp, 0))
7394 || TREE_STATIC (exp)
7395 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7396 return 1;
7398 /* Otherwise, the only way this can conflict is if we are taking
7399 the address of a DECL a that address if part of X, which is
7400 very rare. */
7401 exp = TREE_OPERAND (exp, 0);
7402 if (DECL_P (exp))
7404 if (!DECL_RTL_SET_P (exp)
7405 || !MEM_P (DECL_RTL (exp)))
7406 return 0;
7407 else
7408 exp_rtl = XEXP (DECL_RTL (exp), 0);
7410 break;
7412 case MEM_REF:
7413 if (MEM_P (x)
7414 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7415 get_alias_set (exp)))
7416 return 0;
7417 break;
7419 case CALL_EXPR:
7420 /* Assume that the call will clobber all hard registers and
7421 all of memory. */
7422 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7423 || MEM_P (x))
7424 return 0;
7425 break;
7427 case WITH_CLEANUP_EXPR:
7428 case CLEANUP_POINT_EXPR:
7429 /* Lowered by gimplify.c. */
7430 gcc_unreachable ();
7432 case SAVE_EXPR:
7433 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7435 default:
7436 break;
7439 /* If we have an rtx, we do not need to scan our operands. */
7440 if (exp_rtl)
7441 break;
7443 nops = TREE_OPERAND_LENGTH (exp);
7444 for (i = 0; i < nops; i++)
7445 if (TREE_OPERAND (exp, i) != 0
7446 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7447 return 0;
7449 break;
7451 case tcc_type:
7452 /* Should never get a type here. */
7453 gcc_unreachable ();
7456 /* If we have an rtl, find any enclosed object. Then see if we conflict
7457 with it. */
7458 if (exp_rtl)
7460 if (GET_CODE (exp_rtl) == SUBREG)
7462 exp_rtl = SUBREG_REG (exp_rtl);
7463 if (REG_P (exp_rtl)
7464 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7465 return 0;
7468 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7469 are memory and they conflict. */
7470 return ! (rtx_equal_p (x, exp_rtl)
7471 || (MEM_P (x) && MEM_P (exp_rtl)
7472 && true_dependence (exp_rtl, VOIDmode, x)));
7475 /* If we reach here, it is safe. */
7476 return 1;
7480 /* Return the highest power of two that EXP is known to be a multiple of.
7481 This is used in updating alignment of MEMs in array references. */
7483 unsigned HOST_WIDE_INT
7484 highest_pow2_factor (const_tree exp)
7486 unsigned HOST_WIDE_INT ret;
7487 int trailing_zeros = tree_ctz (exp);
7488 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7489 return BIGGEST_ALIGNMENT;
7490 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7491 if (ret > BIGGEST_ALIGNMENT)
7492 return BIGGEST_ALIGNMENT;
7493 return ret;
7496 /* Similar, except that the alignment requirements of TARGET are
7497 taken into account. Assume it is at least as aligned as its
7498 type, unless it is a COMPONENT_REF in which case the layout of
7499 the structure gives the alignment. */
7501 static unsigned HOST_WIDE_INT
7502 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7504 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7505 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7507 return MAX (factor, talign);
7510 #ifdef HAVE_conditional_move
7511 /* Convert the tree comparison code TCODE to the rtl one where the
7512 signedness is UNSIGNEDP. */
7514 static enum rtx_code
7515 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7517 enum rtx_code code;
7518 switch (tcode)
7520 case EQ_EXPR:
7521 code = EQ;
7522 break;
7523 case NE_EXPR:
7524 code = NE;
7525 break;
7526 case LT_EXPR:
7527 code = unsignedp ? LTU : LT;
7528 break;
7529 case LE_EXPR:
7530 code = unsignedp ? LEU : LE;
7531 break;
7532 case GT_EXPR:
7533 code = unsignedp ? GTU : GT;
7534 break;
7535 case GE_EXPR:
7536 code = unsignedp ? GEU : GE;
7537 break;
7538 case UNORDERED_EXPR:
7539 code = UNORDERED;
7540 break;
7541 case ORDERED_EXPR:
7542 code = ORDERED;
7543 break;
7544 case UNLT_EXPR:
7545 code = UNLT;
7546 break;
7547 case UNLE_EXPR:
7548 code = UNLE;
7549 break;
7550 case UNGT_EXPR:
7551 code = UNGT;
7552 break;
7553 case UNGE_EXPR:
7554 code = UNGE;
7555 break;
7556 case UNEQ_EXPR:
7557 code = UNEQ;
7558 break;
7559 case LTGT_EXPR:
7560 code = LTGT;
7561 break;
7563 default:
7564 gcc_unreachable ();
7566 return code;
7568 #endif
7570 /* Subroutine of expand_expr. Expand the two operands of a binary
7571 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7572 The value may be stored in TARGET if TARGET is nonzero. The
7573 MODIFIER argument is as documented by expand_expr. */
7575 void
7576 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7577 enum expand_modifier modifier)
7579 if (! safe_from_p (target, exp1, 1))
7580 target = 0;
7581 if (operand_equal_p (exp0, exp1, 0))
7583 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7584 *op1 = copy_rtx (*op0);
7586 else
7588 /* If we need to preserve evaluation order, copy exp0 into its own
7589 temporary variable so that it can't be clobbered by exp1. */
7590 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7591 exp0 = save_expr (exp0);
7592 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7593 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7598 /* Return a MEM that contains constant EXP. DEFER is as for
7599 output_constant_def and MODIFIER is as for expand_expr. */
7601 static rtx
7602 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7604 rtx mem;
7606 mem = output_constant_def (exp, defer);
7607 if (modifier != EXPAND_INITIALIZER)
7608 mem = use_anchored_address (mem);
7609 return mem;
7612 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7613 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7615 static rtx
7616 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7617 enum expand_modifier modifier, addr_space_t as)
7619 rtx result, subtarget;
7620 tree inner, offset;
7621 HOST_WIDE_INT bitsize, bitpos;
7622 int volatilep, unsignedp;
7623 machine_mode mode1;
7625 /* If we are taking the address of a constant and are at the top level,
7626 we have to use output_constant_def since we can't call force_const_mem
7627 at top level. */
7628 /* ??? This should be considered a front-end bug. We should not be
7629 generating ADDR_EXPR of something that isn't an LVALUE. The only
7630 exception here is STRING_CST. */
7631 if (CONSTANT_CLASS_P (exp))
7633 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7634 if (modifier < EXPAND_SUM)
7635 result = force_operand (result, target);
7636 return result;
7639 /* Everything must be something allowed by is_gimple_addressable. */
7640 switch (TREE_CODE (exp))
7642 case INDIRECT_REF:
7643 /* This case will happen via recursion for &a->b. */
7644 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7646 case MEM_REF:
7648 tree tem = TREE_OPERAND (exp, 0);
7649 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7650 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7651 return expand_expr (tem, target, tmode, modifier);
7654 case CONST_DECL:
7655 /* Expand the initializer like constants above. */
7656 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7657 0, modifier), 0);
7658 if (modifier < EXPAND_SUM)
7659 result = force_operand (result, target);
7660 return result;
7662 case REALPART_EXPR:
7663 /* The real part of the complex number is always first, therefore
7664 the address is the same as the address of the parent object. */
7665 offset = 0;
7666 bitpos = 0;
7667 inner = TREE_OPERAND (exp, 0);
7668 break;
7670 case IMAGPART_EXPR:
7671 /* The imaginary part of the complex number is always second.
7672 The expression is therefore always offset by the size of the
7673 scalar type. */
7674 offset = 0;
7675 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7676 inner = TREE_OPERAND (exp, 0);
7677 break;
7679 case COMPOUND_LITERAL_EXPR:
7680 /* Allow COMPOUND_LITERAL_EXPR in initializers, if e.g.
7681 rtl_for_decl_init is called on DECL_INITIAL with
7682 COMPOUNT_LITERAL_EXPRs in it, they aren't gimplified. */
7683 if (modifier == EXPAND_INITIALIZER
7684 && COMPOUND_LITERAL_EXPR_DECL (exp))
7685 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7686 target, tmode, modifier, as);
7687 /* FALLTHRU */
7688 default:
7689 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7690 expand_expr, as that can have various side effects; LABEL_DECLs for
7691 example, may not have their DECL_RTL set yet. Expand the rtl of
7692 CONSTRUCTORs too, which should yield a memory reference for the
7693 constructor's contents. Assume language specific tree nodes can
7694 be expanded in some interesting way. */
7695 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7696 if (DECL_P (exp)
7697 || TREE_CODE (exp) == CONSTRUCTOR
7698 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7700 result = expand_expr (exp, target, tmode,
7701 modifier == EXPAND_INITIALIZER
7702 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7704 /* If the DECL isn't in memory, then the DECL wasn't properly
7705 marked TREE_ADDRESSABLE, which will be either a front-end
7706 or a tree optimizer bug. */
7708 if (TREE_ADDRESSABLE (exp)
7709 && ! MEM_P (result)
7710 && ! targetm.calls.allocate_stack_slots_for_args ())
7712 error ("local frame unavailable (naked function?)");
7713 return result;
7715 else
7716 gcc_assert (MEM_P (result));
7717 result = XEXP (result, 0);
7719 /* ??? Is this needed anymore? */
7720 if (DECL_P (exp))
7721 TREE_USED (exp) = 1;
7723 if (modifier != EXPAND_INITIALIZER
7724 && modifier != EXPAND_CONST_ADDRESS
7725 && modifier != EXPAND_SUM)
7726 result = force_operand (result, target);
7727 return result;
7730 /* Pass FALSE as the last argument to get_inner_reference although
7731 we are expanding to RTL. The rationale is that we know how to
7732 handle "aligning nodes" here: we can just bypass them because
7733 they won't change the final object whose address will be returned
7734 (they actually exist only for that purpose). */
7735 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7736 &mode1, &unsignedp, &volatilep, false);
7737 break;
7740 /* We must have made progress. */
7741 gcc_assert (inner != exp);
7743 subtarget = offset || bitpos ? NULL_RTX : target;
7744 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7745 inner alignment, force the inner to be sufficiently aligned. */
7746 if (CONSTANT_CLASS_P (inner)
7747 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7749 inner = copy_node (inner);
7750 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7751 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7752 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7754 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7756 if (offset)
7758 rtx tmp;
7760 if (modifier != EXPAND_NORMAL)
7761 result = force_operand (result, NULL);
7762 tmp = expand_expr (offset, NULL_RTX, tmode,
7763 modifier == EXPAND_INITIALIZER
7764 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7766 /* expand_expr is allowed to return an object in a mode other
7767 than TMODE. If it did, we need to convert. */
7768 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7769 tmp = convert_modes (tmode, GET_MODE (tmp),
7770 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7771 result = convert_memory_address_addr_space (tmode, result, as);
7772 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7774 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7775 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7776 else
7778 subtarget = bitpos ? NULL_RTX : target;
7779 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7780 1, OPTAB_LIB_WIDEN);
7784 if (bitpos)
7786 /* Someone beforehand should have rejected taking the address
7787 of such an object. */
7788 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7790 result = convert_memory_address_addr_space (tmode, result, as);
7791 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7792 if (modifier < EXPAND_SUM)
7793 result = force_operand (result, target);
7796 return result;
7799 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7800 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7802 static rtx
7803 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7804 enum expand_modifier modifier)
7806 addr_space_t as = ADDR_SPACE_GENERIC;
7807 machine_mode address_mode = Pmode;
7808 machine_mode pointer_mode = ptr_mode;
7809 machine_mode rmode;
7810 rtx result;
7812 /* Target mode of VOIDmode says "whatever's natural". */
7813 if (tmode == VOIDmode)
7814 tmode = TYPE_MODE (TREE_TYPE (exp));
7816 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7818 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7819 address_mode = targetm.addr_space.address_mode (as);
7820 pointer_mode = targetm.addr_space.pointer_mode (as);
7823 /* We can get called with some Weird Things if the user does silliness
7824 like "(short) &a". In that case, convert_memory_address won't do
7825 the right thing, so ignore the given target mode. */
7826 if (tmode != address_mode && tmode != pointer_mode)
7827 tmode = address_mode;
7829 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7830 tmode, modifier, as);
7832 /* Despite expand_expr claims concerning ignoring TMODE when not
7833 strictly convenient, stuff breaks if we don't honor it. Note
7834 that combined with the above, we only do this for pointer modes. */
7835 rmode = GET_MODE (result);
7836 if (rmode == VOIDmode)
7837 rmode = tmode;
7838 if (rmode != tmode)
7839 result = convert_memory_address_addr_space (tmode, result, as);
7841 return result;
7844 /* Generate code for computing CONSTRUCTOR EXP.
7845 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7846 is TRUE, instead of creating a temporary variable in memory
7847 NULL is returned and the caller needs to handle it differently. */
7849 static rtx
7850 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7851 bool avoid_temp_mem)
7853 tree type = TREE_TYPE (exp);
7854 machine_mode mode = TYPE_MODE (type);
7856 /* Try to avoid creating a temporary at all. This is possible
7857 if all of the initializer is zero.
7858 FIXME: try to handle all [0..255] initializers we can handle
7859 with memset. */
7860 if (TREE_STATIC (exp)
7861 && !TREE_ADDRESSABLE (exp)
7862 && target != 0 && mode == BLKmode
7863 && all_zeros_p (exp))
7865 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7866 return target;
7869 /* All elts simple constants => refer to a constant in memory. But
7870 if this is a non-BLKmode mode, let it store a field at a time
7871 since that should make a CONST_INT, CONST_WIDE_INT or
7872 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7873 use, it is best to store directly into the target unless the type
7874 is large enough that memcpy will be used. If we are making an
7875 initializer and all operands are constant, put it in memory as
7876 well.
7878 FIXME: Avoid trying to fill vector constructors piece-meal.
7879 Output them with output_constant_def below unless we're sure
7880 they're zeros. This should go away when vector initializers
7881 are treated like VECTOR_CST instead of arrays. */
7882 if ((TREE_STATIC (exp)
7883 && ((mode == BLKmode
7884 && ! (target != 0 && safe_from_p (target, exp, 1)))
7885 || TREE_ADDRESSABLE (exp)
7886 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7887 && (! can_move_by_pieces
7888 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7889 TYPE_ALIGN (type)))
7890 && ! mostly_zeros_p (exp))))
7891 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7892 && TREE_CONSTANT (exp)))
7894 rtx constructor;
7896 if (avoid_temp_mem)
7897 return NULL_RTX;
7899 constructor = expand_expr_constant (exp, 1, modifier);
7901 if (modifier != EXPAND_CONST_ADDRESS
7902 && modifier != EXPAND_INITIALIZER
7903 && modifier != EXPAND_SUM)
7904 constructor = validize_mem (constructor);
7906 return constructor;
7909 /* Handle calls that pass values in multiple non-contiguous
7910 locations. The Irix 6 ABI has examples of this. */
7911 if (target == 0 || ! safe_from_p (target, exp, 1)
7912 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7914 if (avoid_temp_mem)
7915 return NULL_RTX;
7917 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7920 store_constructor (exp, target, 0, int_expr_size (exp));
7921 return target;
7925 /* expand_expr: generate code for computing expression EXP.
7926 An rtx for the computed value is returned. The value is never null.
7927 In the case of a void EXP, const0_rtx is returned.
7929 The value may be stored in TARGET if TARGET is nonzero.
7930 TARGET is just a suggestion; callers must assume that
7931 the rtx returned may not be the same as TARGET.
7933 If TARGET is CONST0_RTX, it means that the value will be ignored.
7935 If TMODE is not VOIDmode, it suggests generating the
7936 result in mode TMODE. But this is done only when convenient.
7937 Otherwise, TMODE is ignored and the value generated in its natural mode.
7938 TMODE is just a suggestion; callers must assume that
7939 the rtx returned may not have mode TMODE.
7941 Note that TARGET may have neither TMODE nor MODE. In that case, it
7942 probably will not be used.
7944 If MODIFIER is EXPAND_SUM then when EXP is an addition
7945 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7946 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7947 products as above, or REG or MEM, or constant.
7948 Ordinarily in such cases we would output mul or add instructions
7949 and then return a pseudo reg containing the sum.
7951 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7952 it also marks a label as absolutely required (it can't be dead).
7953 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7954 This is used for outputting expressions used in initializers.
7956 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7957 with a constant address even if that address is not normally legitimate.
7958 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7960 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7961 a call parameter. Such targets require special care as we haven't yet
7962 marked TARGET so that it's safe from being trashed by libcalls. We
7963 don't want to use TARGET for anything but the final result;
7964 Intermediate values must go elsewhere. Additionally, calls to
7965 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7967 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7968 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7969 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7970 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7971 recursively.
7973 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7974 In this case, we don't adjust a returned MEM rtx that wouldn't be
7975 sufficiently aligned for its mode; instead, it's up to the caller
7976 to deal with it afterwards. This is used to make sure that unaligned
7977 base objects for which out-of-bounds accesses are supported, for
7978 example record types with trailing arrays, aren't realigned behind
7979 the back of the caller.
7980 The normal operating mode is to pass FALSE for this parameter. */
7983 expand_expr_real (tree exp, rtx target, machine_mode tmode,
7984 enum expand_modifier modifier, rtx *alt_rtl,
7985 bool inner_reference_p)
7987 rtx ret;
7989 /* Handle ERROR_MARK before anybody tries to access its type. */
7990 if (TREE_CODE (exp) == ERROR_MARK
7991 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7993 ret = CONST0_RTX (tmode);
7994 return ret ? ret : const0_rtx;
7997 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
7998 inner_reference_p);
7999 return ret;
8002 /* Try to expand the conditional expression which is represented by
8003 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
8004 return the rtl reg which repsents the result. Otherwise return
8005 NULL_RTL. */
8007 static rtx
8008 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8009 tree treeop1 ATTRIBUTE_UNUSED,
8010 tree treeop2 ATTRIBUTE_UNUSED)
8012 #ifdef HAVE_conditional_move
8013 rtx insn;
8014 rtx op00, op01, op1, op2;
8015 enum rtx_code comparison_code;
8016 machine_mode comparison_mode;
8017 gimple srcstmt;
8018 rtx temp;
8019 tree type = TREE_TYPE (treeop1);
8020 int unsignedp = TYPE_UNSIGNED (type);
8021 machine_mode mode = TYPE_MODE (type);
8022 machine_mode orig_mode = mode;
8024 /* If we cannot do a conditional move on the mode, try doing it
8025 with the promoted mode. */
8026 if (!can_conditionally_move_p (mode))
8028 mode = promote_mode (type, mode, &unsignedp);
8029 if (!can_conditionally_move_p (mode))
8030 return NULL_RTX;
8031 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8033 else
8034 temp = assign_temp (type, 0, 1);
8036 start_sequence ();
8037 expand_operands (treeop1, treeop2,
8038 temp, &op1, &op2, EXPAND_NORMAL);
8040 if (TREE_CODE (treeop0) == SSA_NAME
8041 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8043 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8044 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8045 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8046 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8047 comparison_mode = TYPE_MODE (type);
8048 unsignedp = TYPE_UNSIGNED (type);
8049 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8051 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
8053 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8054 enum tree_code cmpcode = TREE_CODE (treeop0);
8055 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8056 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8057 unsignedp = TYPE_UNSIGNED (type);
8058 comparison_mode = TYPE_MODE (type);
8059 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8061 else
8063 op00 = expand_normal (treeop0);
8064 op01 = const0_rtx;
8065 comparison_code = NE;
8066 comparison_mode = GET_MODE (op00);
8067 if (comparison_mode == VOIDmode)
8068 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8071 if (GET_MODE (op1) != mode)
8072 op1 = gen_lowpart (mode, op1);
8074 if (GET_MODE (op2) != mode)
8075 op2 = gen_lowpart (mode, op2);
8077 /* Try to emit the conditional move. */
8078 insn = emit_conditional_move (temp, comparison_code,
8079 op00, op01, comparison_mode,
8080 op1, op2, mode,
8081 unsignedp);
8083 /* If we could do the conditional move, emit the sequence,
8084 and return. */
8085 if (insn)
8087 rtx_insn *seq = get_insns ();
8088 end_sequence ();
8089 emit_insn (seq);
8090 return convert_modes (orig_mode, mode, temp, 0);
8093 /* Otherwise discard the sequence and fall back to code with
8094 branches. */
8095 end_sequence ();
8096 #endif
8097 return NULL_RTX;
8101 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8102 enum expand_modifier modifier)
8104 rtx op0, op1, op2, temp;
8105 tree type;
8106 int unsignedp;
8107 machine_mode mode;
8108 enum tree_code code = ops->code;
8109 optab this_optab;
8110 rtx subtarget, original_target;
8111 int ignore;
8112 bool reduce_bit_field;
8113 location_t loc = ops->location;
8114 tree treeop0, treeop1, treeop2;
8115 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8116 ? reduce_to_bit_field_precision ((expr), \
8117 target, \
8118 type) \
8119 : (expr))
8121 type = ops->type;
8122 mode = TYPE_MODE (type);
8123 unsignedp = TYPE_UNSIGNED (type);
8125 treeop0 = ops->op0;
8126 treeop1 = ops->op1;
8127 treeop2 = ops->op2;
8129 /* We should be called only on simple (binary or unary) expressions,
8130 exactly those that are valid in gimple expressions that aren't
8131 GIMPLE_SINGLE_RHS (or invalid). */
8132 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8133 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8134 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8136 ignore = (target == const0_rtx
8137 || ((CONVERT_EXPR_CODE_P (code)
8138 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8139 && TREE_CODE (type) == VOID_TYPE));
8141 /* We should be called only if we need the result. */
8142 gcc_assert (!ignore);
8144 /* An operation in what may be a bit-field type needs the
8145 result to be reduced to the precision of the bit-field type,
8146 which is narrower than that of the type's mode. */
8147 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8148 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8150 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8151 target = 0;
8153 /* Use subtarget as the target for operand 0 of a binary operation. */
8154 subtarget = get_subtarget (target);
8155 original_target = target;
8157 switch (code)
8159 case NON_LVALUE_EXPR:
8160 case PAREN_EXPR:
8161 CASE_CONVERT:
8162 if (treeop0 == error_mark_node)
8163 return const0_rtx;
8165 if (TREE_CODE (type) == UNION_TYPE)
8167 tree valtype = TREE_TYPE (treeop0);
8169 /* If both input and output are BLKmode, this conversion isn't doing
8170 anything except possibly changing memory attribute. */
8171 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8173 rtx result = expand_expr (treeop0, target, tmode,
8174 modifier);
8176 result = copy_rtx (result);
8177 set_mem_attributes (result, type, 0);
8178 return result;
8181 if (target == 0)
8183 if (TYPE_MODE (type) != BLKmode)
8184 target = gen_reg_rtx (TYPE_MODE (type));
8185 else
8186 target = assign_temp (type, 1, 1);
8189 if (MEM_P (target))
8190 /* Store data into beginning of memory target. */
8191 store_expr (treeop0,
8192 adjust_address (target, TYPE_MODE (valtype), 0),
8193 modifier == EXPAND_STACK_PARM,
8194 false);
8196 else
8198 gcc_assert (REG_P (target));
8200 /* Store this field into a union of the proper type. */
8201 store_field (target,
8202 MIN ((int_size_in_bytes (TREE_TYPE
8203 (treeop0))
8204 * BITS_PER_UNIT),
8205 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8206 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8209 /* Return the entire union. */
8210 return target;
8213 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8215 op0 = expand_expr (treeop0, target, VOIDmode,
8216 modifier);
8218 /* If the signedness of the conversion differs and OP0 is
8219 a promoted SUBREG, clear that indication since we now
8220 have to do the proper extension. */
8221 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8222 && GET_CODE (op0) == SUBREG)
8223 SUBREG_PROMOTED_VAR_P (op0) = 0;
8225 return REDUCE_BIT_FIELD (op0);
8228 op0 = expand_expr (treeop0, NULL_RTX, mode,
8229 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8230 if (GET_MODE (op0) == mode)
8233 /* If OP0 is a constant, just convert it into the proper mode. */
8234 else if (CONSTANT_P (op0))
8236 tree inner_type = TREE_TYPE (treeop0);
8237 machine_mode inner_mode = GET_MODE (op0);
8239 if (inner_mode == VOIDmode)
8240 inner_mode = TYPE_MODE (inner_type);
8242 if (modifier == EXPAND_INITIALIZER)
8243 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8244 subreg_lowpart_offset (mode,
8245 inner_mode));
8246 else
8247 op0= convert_modes (mode, inner_mode, op0,
8248 TYPE_UNSIGNED (inner_type));
8251 else if (modifier == EXPAND_INITIALIZER)
8252 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8254 else if (target == 0)
8255 op0 = convert_to_mode (mode, op0,
8256 TYPE_UNSIGNED (TREE_TYPE
8257 (treeop0)));
8258 else
8260 convert_move (target, op0,
8261 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8262 op0 = target;
8265 return REDUCE_BIT_FIELD (op0);
8267 case ADDR_SPACE_CONVERT_EXPR:
8269 tree treeop0_type = TREE_TYPE (treeop0);
8270 addr_space_t as_to;
8271 addr_space_t as_from;
8273 gcc_assert (POINTER_TYPE_P (type));
8274 gcc_assert (POINTER_TYPE_P (treeop0_type));
8276 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8277 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8279 /* Conversions between pointers to the same address space should
8280 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8281 gcc_assert (as_to != as_from);
8283 /* Ask target code to handle conversion between pointers
8284 to overlapping address spaces. */
8285 if (targetm.addr_space.subset_p (as_to, as_from)
8286 || targetm.addr_space.subset_p (as_from, as_to))
8288 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8289 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8290 gcc_assert (op0);
8291 return op0;
8294 /* For disjoint address spaces, converting anything but
8295 a null pointer invokes undefined behaviour. We simply
8296 always return a null pointer here. */
8297 return CONST0_RTX (mode);
8300 case POINTER_PLUS_EXPR:
8301 /* Even though the sizetype mode and the pointer's mode can be different
8302 expand is able to handle this correctly and get the correct result out
8303 of the PLUS_EXPR code. */
8304 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8305 if sizetype precision is smaller than pointer precision. */
8306 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8307 treeop1 = fold_convert_loc (loc, type,
8308 fold_convert_loc (loc, ssizetype,
8309 treeop1));
8310 /* If sizetype precision is larger than pointer precision, truncate the
8311 offset to have matching modes. */
8312 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8313 treeop1 = fold_convert_loc (loc, type, treeop1);
8315 case PLUS_EXPR:
8316 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8317 something else, make sure we add the register to the constant and
8318 then to the other thing. This case can occur during strength
8319 reduction and doing it this way will produce better code if the
8320 frame pointer or argument pointer is eliminated.
8322 fold-const.c will ensure that the constant is always in the inner
8323 PLUS_EXPR, so the only case we need to do anything about is if
8324 sp, ap, or fp is our second argument, in which case we must swap
8325 the innermost first argument and our second argument. */
8327 if (TREE_CODE (treeop0) == PLUS_EXPR
8328 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8329 && TREE_CODE (treeop1) == VAR_DECL
8330 && (DECL_RTL (treeop1) == frame_pointer_rtx
8331 || DECL_RTL (treeop1) == stack_pointer_rtx
8332 || DECL_RTL (treeop1) == arg_pointer_rtx))
8334 gcc_unreachable ();
8337 /* If the result is to be ptr_mode and we are adding an integer to
8338 something, we might be forming a constant. So try to use
8339 plus_constant. If it produces a sum and we can't accept it,
8340 use force_operand. This allows P = &ARR[const] to generate
8341 efficient code on machines where a SYMBOL_REF is not a valid
8342 address.
8344 If this is an EXPAND_SUM call, always return the sum. */
8345 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8346 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8348 if (modifier == EXPAND_STACK_PARM)
8349 target = 0;
8350 if (TREE_CODE (treeop0) == INTEGER_CST
8351 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8352 && TREE_CONSTANT (treeop1))
8354 rtx constant_part;
8355 HOST_WIDE_INT wc;
8356 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8358 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8359 EXPAND_SUM);
8360 /* Use wi::shwi to ensure that the constant is
8361 truncated according to the mode of OP1, then sign extended
8362 to a HOST_WIDE_INT. Using the constant directly can result
8363 in non-canonical RTL in a 64x32 cross compile. */
8364 wc = TREE_INT_CST_LOW (treeop0);
8365 constant_part =
8366 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8367 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8368 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8369 op1 = force_operand (op1, target);
8370 return REDUCE_BIT_FIELD (op1);
8373 else if (TREE_CODE (treeop1) == INTEGER_CST
8374 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8375 && TREE_CONSTANT (treeop0))
8377 rtx constant_part;
8378 HOST_WIDE_INT wc;
8379 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8381 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8382 (modifier == EXPAND_INITIALIZER
8383 ? EXPAND_INITIALIZER : EXPAND_SUM));
8384 if (! CONSTANT_P (op0))
8386 op1 = expand_expr (treeop1, NULL_RTX,
8387 VOIDmode, modifier);
8388 /* Return a PLUS if modifier says it's OK. */
8389 if (modifier == EXPAND_SUM
8390 || modifier == EXPAND_INITIALIZER)
8391 return simplify_gen_binary (PLUS, mode, op0, op1);
8392 goto binop2;
8394 /* Use wi::shwi to ensure that the constant is
8395 truncated according to the mode of OP1, then sign extended
8396 to a HOST_WIDE_INT. Using the constant directly can result
8397 in non-canonical RTL in a 64x32 cross compile. */
8398 wc = TREE_INT_CST_LOW (treeop1);
8399 constant_part
8400 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8401 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8402 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8403 op0 = force_operand (op0, target);
8404 return REDUCE_BIT_FIELD (op0);
8408 /* Use TER to expand pointer addition of a negated value
8409 as pointer subtraction. */
8410 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8411 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8412 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8413 && TREE_CODE (treeop1) == SSA_NAME
8414 && TYPE_MODE (TREE_TYPE (treeop0))
8415 == TYPE_MODE (TREE_TYPE (treeop1)))
8417 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8418 if (def)
8420 treeop1 = gimple_assign_rhs1 (def);
8421 code = MINUS_EXPR;
8422 goto do_minus;
8426 /* No sense saving up arithmetic to be done
8427 if it's all in the wrong mode to form part of an address.
8428 And force_operand won't know whether to sign-extend or
8429 zero-extend. */
8430 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8431 || mode != ptr_mode)
8433 expand_operands (treeop0, treeop1,
8434 subtarget, &op0, &op1, EXPAND_NORMAL);
8435 if (op0 == const0_rtx)
8436 return op1;
8437 if (op1 == const0_rtx)
8438 return op0;
8439 goto binop2;
8442 expand_operands (treeop0, treeop1,
8443 subtarget, &op0, &op1, modifier);
8444 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8446 case MINUS_EXPR:
8447 do_minus:
8448 /* For initializers, we are allowed to return a MINUS of two
8449 symbolic constants. Here we handle all cases when both operands
8450 are constant. */
8451 /* Handle difference of two symbolic constants,
8452 for the sake of an initializer. */
8453 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8454 && really_constant_p (treeop0)
8455 && really_constant_p (treeop1))
8457 expand_operands (treeop0, treeop1,
8458 NULL_RTX, &op0, &op1, modifier);
8460 /* If the last operand is a CONST_INT, use plus_constant of
8461 the negated constant. Else make the MINUS. */
8462 if (CONST_INT_P (op1))
8463 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8464 -INTVAL (op1)));
8465 else
8466 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8469 /* No sense saving up arithmetic to be done
8470 if it's all in the wrong mode to form part of an address.
8471 And force_operand won't know whether to sign-extend or
8472 zero-extend. */
8473 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8474 || mode != ptr_mode)
8475 goto binop;
8477 expand_operands (treeop0, treeop1,
8478 subtarget, &op0, &op1, modifier);
8480 /* Convert A - const to A + (-const). */
8481 if (CONST_INT_P (op1))
8483 op1 = negate_rtx (mode, op1);
8484 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8487 goto binop2;
8489 case WIDEN_MULT_PLUS_EXPR:
8490 case WIDEN_MULT_MINUS_EXPR:
8491 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8492 op2 = expand_normal (treeop2);
8493 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8494 target, unsignedp);
8495 return target;
8497 case WIDEN_MULT_EXPR:
8498 /* If first operand is constant, swap them.
8499 Thus the following special case checks need only
8500 check the second operand. */
8501 if (TREE_CODE (treeop0) == INTEGER_CST)
8503 tree t1 = treeop0;
8504 treeop0 = treeop1;
8505 treeop1 = t1;
8508 /* First, check if we have a multiplication of one signed and one
8509 unsigned operand. */
8510 if (TREE_CODE (treeop1) != INTEGER_CST
8511 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8512 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8514 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8515 this_optab = usmul_widen_optab;
8516 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8517 != CODE_FOR_nothing)
8519 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8520 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8521 EXPAND_NORMAL);
8522 else
8523 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8524 EXPAND_NORMAL);
8525 /* op0 and op1 might still be constant, despite the above
8526 != INTEGER_CST check. Handle it. */
8527 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8529 op0 = convert_modes (innermode, mode, op0, true);
8530 op1 = convert_modes (innermode, mode, op1, false);
8531 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8532 target, unsignedp));
8534 goto binop3;
8537 /* Check for a multiplication with matching signedness. */
8538 else if ((TREE_CODE (treeop1) == INTEGER_CST
8539 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8540 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8541 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8543 tree op0type = TREE_TYPE (treeop0);
8544 machine_mode innermode = TYPE_MODE (op0type);
8545 bool zextend_p = TYPE_UNSIGNED (op0type);
8546 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8547 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8549 if (TREE_CODE (treeop0) != INTEGER_CST)
8551 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8552 != CODE_FOR_nothing)
8554 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8555 EXPAND_NORMAL);
8556 /* op0 and op1 might still be constant, despite the above
8557 != INTEGER_CST check. Handle it. */
8558 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8560 widen_mult_const:
8561 op0 = convert_modes (innermode, mode, op0, zextend_p);
8563 = convert_modes (innermode, mode, op1,
8564 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8565 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8566 target,
8567 unsignedp));
8569 temp = expand_widening_mult (mode, op0, op1, target,
8570 unsignedp, this_optab);
8571 return REDUCE_BIT_FIELD (temp);
8573 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8574 != CODE_FOR_nothing
8575 && innermode == word_mode)
8577 rtx htem, hipart;
8578 op0 = expand_normal (treeop0);
8579 if (TREE_CODE (treeop1) == INTEGER_CST)
8580 op1 = convert_modes (innermode, mode,
8581 expand_normal (treeop1),
8582 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8583 else
8584 op1 = expand_normal (treeop1);
8585 /* op0 and op1 might still be constant, despite the above
8586 != INTEGER_CST check. Handle it. */
8587 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8588 goto widen_mult_const;
8589 temp = expand_binop (mode, other_optab, op0, op1, target,
8590 unsignedp, OPTAB_LIB_WIDEN);
8591 hipart = gen_highpart (innermode, temp);
8592 htem = expand_mult_highpart_adjust (innermode, hipart,
8593 op0, op1, hipart,
8594 zextend_p);
8595 if (htem != hipart)
8596 emit_move_insn (hipart, htem);
8597 return REDUCE_BIT_FIELD (temp);
8601 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8602 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8603 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8604 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8606 case FMA_EXPR:
8608 optab opt = fma_optab;
8609 gimple def0, def2;
8611 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8612 call. */
8613 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8615 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8616 tree call_expr;
8618 gcc_assert (fn != NULL_TREE);
8619 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8620 return expand_builtin (call_expr, target, subtarget, mode, false);
8623 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8624 /* The multiplication is commutative - look at its 2nd operand
8625 if the first isn't fed by a negate. */
8626 if (!def0)
8628 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8629 /* Swap operands if the 2nd operand is fed by a negate. */
8630 if (def0)
8632 tree tem = treeop0;
8633 treeop0 = treeop1;
8634 treeop1 = tem;
8637 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8639 op0 = op2 = NULL;
8641 if (def0 && def2
8642 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8644 opt = fnms_optab;
8645 op0 = expand_normal (gimple_assign_rhs1 (def0));
8646 op2 = expand_normal (gimple_assign_rhs1 (def2));
8648 else if (def0
8649 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8651 opt = fnma_optab;
8652 op0 = expand_normal (gimple_assign_rhs1 (def0));
8654 else if (def2
8655 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8657 opt = fms_optab;
8658 op2 = expand_normal (gimple_assign_rhs1 (def2));
8661 if (op0 == NULL)
8662 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8663 if (op2 == NULL)
8664 op2 = expand_normal (treeop2);
8665 op1 = expand_normal (treeop1);
8667 return expand_ternary_op (TYPE_MODE (type), opt,
8668 op0, op1, op2, target, 0);
8671 case MULT_EXPR:
8672 /* If this is a fixed-point operation, then we cannot use the code
8673 below because "expand_mult" doesn't support sat/no-sat fixed-point
8674 multiplications. */
8675 if (ALL_FIXED_POINT_MODE_P (mode))
8676 goto binop;
8678 /* If first operand is constant, swap them.
8679 Thus the following special case checks need only
8680 check the second operand. */
8681 if (TREE_CODE (treeop0) == INTEGER_CST)
8683 tree t1 = treeop0;
8684 treeop0 = treeop1;
8685 treeop1 = t1;
8688 /* Attempt to return something suitable for generating an
8689 indexed address, for machines that support that. */
8691 if (modifier == EXPAND_SUM && mode == ptr_mode
8692 && tree_fits_shwi_p (treeop1))
8694 tree exp1 = treeop1;
8696 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8697 EXPAND_SUM);
8699 if (!REG_P (op0))
8700 op0 = force_operand (op0, NULL_RTX);
8701 if (!REG_P (op0))
8702 op0 = copy_to_mode_reg (mode, op0);
8704 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8705 gen_int_mode (tree_to_shwi (exp1),
8706 TYPE_MODE (TREE_TYPE (exp1)))));
8709 if (modifier == EXPAND_STACK_PARM)
8710 target = 0;
8712 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8713 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8715 case TRUNC_DIV_EXPR:
8716 case FLOOR_DIV_EXPR:
8717 case CEIL_DIV_EXPR:
8718 case ROUND_DIV_EXPR:
8719 case EXACT_DIV_EXPR:
8720 /* If this is a fixed-point operation, then we cannot use the code
8721 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8722 divisions. */
8723 if (ALL_FIXED_POINT_MODE_P (mode))
8724 goto binop;
8726 if (modifier == EXPAND_STACK_PARM)
8727 target = 0;
8728 /* Possible optimization: compute the dividend with EXPAND_SUM
8729 then if the divisor is constant can optimize the case
8730 where some terms of the dividend have coeffs divisible by it. */
8731 expand_operands (treeop0, treeop1,
8732 subtarget, &op0, &op1, EXPAND_NORMAL);
8733 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8735 case RDIV_EXPR:
8736 goto binop;
8738 case MULT_HIGHPART_EXPR:
8739 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8740 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8741 gcc_assert (temp);
8742 return temp;
8744 case TRUNC_MOD_EXPR:
8745 case FLOOR_MOD_EXPR:
8746 case CEIL_MOD_EXPR:
8747 case ROUND_MOD_EXPR:
8748 if (modifier == EXPAND_STACK_PARM)
8749 target = 0;
8750 expand_operands (treeop0, treeop1,
8751 subtarget, &op0, &op1, EXPAND_NORMAL);
8752 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8754 case FIXED_CONVERT_EXPR:
8755 op0 = expand_normal (treeop0);
8756 if (target == 0 || modifier == EXPAND_STACK_PARM)
8757 target = gen_reg_rtx (mode);
8759 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8760 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8761 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8762 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8763 else
8764 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8765 return target;
8767 case FIX_TRUNC_EXPR:
8768 op0 = expand_normal (treeop0);
8769 if (target == 0 || modifier == EXPAND_STACK_PARM)
8770 target = gen_reg_rtx (mode);
8771 expand_fix (target, op0, unsignedp);
8772 return target;
8774 case FLOAT_EXPR:
8775 op0 = expand_normal (treeop0);
8776 if (target == 0 || modifier == EXPAND_STACK_PARM)
8777 target = gen_reg_rtx (mode);
8778 /* expand_float can't figure out what to do if FROM has VOIDmode.
8779 So give it the correct mode. With -O, cse will optimize this. */
8780 if (GET_MODE (op0) == VOIDmode)
8781 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8782 op0);
8783 expand_float (target, op0,
8784 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8785 return target;
8787 case NEGATE_EXPR:
8788 op0 = expand_expr (treeop0, subtarget,
8789 VOIDmode, EXPAND_NORMAL);
8790 if (modifier == EXPAND_STACK_PARM)
8791 target = 0;
8792 temp = expand_unop (mode,
8793 optab_for_tree_code (NEGATE_EXPR, type,
8794 optab_default),
8795 op0, target, 0);
8796 gcc_assert (temp);
8797 return REDUCE_BIT_FIELD (temp);
8799 case ABS_EXPR:
8800 op0 = expand_expr (treeop0, subtarget,
8801 VOIDmode, EXPAND_NORMAL);
8802 if (modifier == EXPAND_STACK_PARM)
8803 target = 0;
8805 /* ABS_EXPR is not valid for complex arguments. */
8806 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8807 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8809 /* Unsigned abs is simply the operand. Testing here means we don't
8810 risk generating incorrect code below. */
8811 if (TYPE_UNSIGNED (type))
8812 return op0;
8814 return expand_abs (mode, op0, target, unsignedp,
8815 safe_from_p (target, treeop0, 1));
8817 case MAX_EXPR:
8818 case MIN_EXPR:
8819 target = original_target;
8820 if (target == 0
8821 || modifier == EXPAND_STACK_PARM
8822 || (MEM_P (target) && MEM_VOLATILE_P (target))
8823 || GET_MODE (target) != mode
8824 || (REG_P (target)
8825 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8826 target = gen_reg_rtx (mode);
8827 expand_operands (treeop0, treeop1,
8828 target, &op0, &op1, EXPAND_NORMAL);
8830 /* First try to do it with a special MIN or MAX instruction.
8831 If that does not win, use a conditional jump to select the proper
8832 value. */
8833 this_optab = optab_for_tree_code (code, type, optab_default);
8834 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8835 OPTAB_WIDEN);
8836 if (temp != 0)
8837 return temp;
8839 /* At this point, a MEM target is no longer useful; we will get better
8840 code without it. */
8842 if (! REG_P (target))
8843 target = gen_reg_rtx (mode);
8845 /* If op1 was placed in target, swap op0 and op1. */
8846 if (target != op0 && target == op1)
8848 temp = op0;
8849 op0 = op1;
8850 op1 = temp;
8853 /* We generate better code and avoid problems with op1 mentioning
8854 target by forcing op1 into a pseudo if it isn't a constant. */
8855 if (! CONSTANT_P (op1))
8856 op1 = force_reg (mode, op1);
8859 enum rtx_code comparison_code;
8860 rtx cmpop1 = op1;
8862 if (code == MAX_EXPR)
8863 comparison_code = unsignedp ? GEU : GE;
8864 else
8865 comparison_code = unsignedp ? LEU : LE;
8867 /* Canonicalize to comparisons against 0. */
8868 if (op1 == const1_rtx)
8870 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8871 or (a != 0 ? a : 1) for unsigned.
8872 For MIN we are safe converting (a <= 1 ? a : 1)
8873 into (a <= 0 ? a : 1) */
8874 cmpop1 = const0_rtx;
8875 if (code == MAX_EXPR)
8876 comparison_code = unsignedp ? NE : GT;
8878 if (op1 == constm1_rtx && !unsignedp)
8880 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8881 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8882 cmpop1 = const0_rtx;
8883 if (code == MIN_EXPR)
8884 comparison_code = LT;
8886 #ifdef HAVE_conditional_move
8887 /* Use a conditional move if possible. */
8888 if (can_conditionally_move_p (mode))
8890 rtx insn;
8892 start_sequence ();
8894 /* Try to emit the conditional move. */
8895 insn = emit_conditional_move (target, comparison_code,
8896 op0, cmpop1, mode,
8897 op0, op1, mode,
8898 unsignedp);
8900 /* If we could do the conditional move, emit the sequence,
8901 and return. */
8902 if (insn)
8904 rtx_insn *seq = get_insns ();
8905 end_sequence ();
8906 emit_insn (seq);
8907 return target;
8910 /* Otherwise discard the sequence and fall back to code with
8911 branches. */
8912 end_sequence ();
8914 #endif
8915 if (target != op0)
8916 emit_move_insn (target, op0);
8918 temp = gen_label_rtx ();
8919 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8920 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8921 -1);
8923 emit_move_insn (target, op1);
8924 emit_label (temp);
8925 return target;
8927 case BIT_NOT_EXPR:
8928 op0 = expand_expr (treeop0, subtarget,
8929 VOIDmode, EXPAND_NORMAL);
8930 if (modifier == EXPAND_STACK_PARM)
8931 target = 0;
8932 /* In case we have to reduce the result to bitfield precision
8933 for unsigned bitfield expand this as XOR with a proper constant
8934 instead. */
8935 if (reduce_bit_field && TYPE_UNSIGNED (type))
8937 wide_int mask = wi::mask (TYPE_PRECISION (type),
8938 false, GET_MODE_PRECISION (mode));
8940 temp = expand_binop (mode, xor_optab, op0,
8941 immed_wide_int_const (mask, mode),
8942 target, 1, OPTAB_LIB_WIDEN);
8944 else
8945 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8946 gcc_assert (temp);
8947 return temp;
8949 /* ??? Can optimize bitwise operations with one arg constant.
8950 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8951 and (a bitwise1 b) bitwise2 b (etc)
8952 but that is probably not worth while. */
8954 case BIT_AND_EXPR:
8955 case BIT_IOR_EXPR:
8956 case BIT_XOR_EXPR:
8957 goto binop;
8959 case LROTATE_EXPR:
8960 case RROTATE_EXPR:
8961 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8962 || (GET_MODE_PRECISION (TYPE_MODE (type))
8963 == TYPE_PRECISION (type)));
8964 /* fall through */
8966 case LSHIFT_EXPR:
8967 case RSHIFT_EXPR:
8968 /* If this is a fixed-point operation, then we cannot use the code
8969 below because "expand_shift" doesn't support sat/no-sat fixed-point
8970 shifts. */
8971 if (ALL_FIXED_POINT_MODE_P (mode))
8972 goto binop;
8974 if (! safe_from_p (subtarget, treeop1, 1))
8975 subtarget = 0;
8976 if (modifier == EXPAND_STACK_PARM)
8977 target = 0;
8978 op0 = expand_expr (treeop0, subtarget,
8979 VOIDmode, EXPAND_NORMAL);
8980 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8981 unsignedp);
8982 if (code == LSHIFT_EXPR)
8983 temp = REDUCE_BIT_FIELD (temp);
8984 return temp;
8986 /* Could determine the answer when only additive constants differ. Also,
8987 the addition of one can be handled by changing the condition. */
8988 case LT_EXPR:
8989 case LE_EXPR:
8990 case GT_EXPR:
8991 case GE_EXPR:
8992 case EQ_EXPR:
8993 case NE_EXPR:
8994 case UNORDERED_EXPR:
8995 case ORDERED_EXPR:
8996 case UNLT_EXPR:
8997 case UNLE_EXPR:
8998 case UNGT_EXPR:
8999 case UNGE_EXPR:
9000 case UNEQ_EXPR:
9001 case LTGT_EXPR:
9002 temp = do_store_flag (ops,
9003 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9004 tmode != VOIDmode ? tmode : mode);
9005 if (temp)
9006 return temp;
9008 /* Use a compare and a jump for BLKmode comparisons, or for function
9009 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
9011 if ((target == 0
9012 || modifier == EXPAND_STACK_PARM
9013 || ! safe_from_p (target, treeop0, 1)
9014 || ! safe_from_p (target, treeop1, 1)
9015 /* Make sure we don't have a hard reg (such as function's return
9016 value) live across basic blocks, if not optimizing. */
9017 || (!optimize && REG_P (target)
9018 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9019 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9021 emit_move_insn (target, const0_rtx);
9023 op1 = gen_label_rtx ();
9024 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
9026 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9027 emit_move_insn (target, constm1_rtx);
9028 else
9029 emit_move_insn (target, const1_rtx);
9031 emit_label (op1);
9032 return target;
9034 case COMPLEX_EXPR:
9035 /* Get the rtx code of the operands. */
9036 op0 = expand_normal (treeop0);
9037 op1 = expand_normal (treeop1);
9039 if (!target)
9040 target = gen_reg_rtx (TYPE_MODE (type));
9041 else
9042 /* If target overlaps with op1, then either we need to force
9043 op1 into a pseudo (if target also overlaps with op0),
9044 or write the complex parts in reverse order. */
9045 switch (GET_CODE (target))
9047 case CONCAT:
9048 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9050 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9052 complex_expr_force_op1:
9053 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9054 emit_move_insn (temp, op1);
9055 op1 = temp;
9056 break;
9058 complex_expr_swap_order:
9059 /* Move the imaginary (op1) and real (op0) parts to their
9060 location. */
9061 write_complex_part (target, op1, true);
9062 write_complex_part (target, op0, false);
9064 return target;
9066 break;
9067 case MEM:
9068 temp = adjust_address_nv (target,
9069 GET_MODE_INNER (GET_MODE (target)), 0);
9070 if (reg_overlap_mentioned_p (temp, op1))
9072 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9073 temp = adjust_address_nv (target, imode,
9074 GET_MODE_SIZE (imode));
9075 if (reg_overlap_mentioned_p (temp, op0))
9076 goto complex_expr_force_op1;
9077 goto complex_expr_swap_order;
9079 break;
9080 default:
9081 if (reg_overlap_mentioned_p (target, op1))
9083 if (reg_overlap_mentioned_p (target, op0))
9084 goto complex_expr_force_op1;
9085 goto complex_expr_swap_order;
9087 break;
9090 /* Move the real (op0) and imaginary (op1) parts to their location. */
9091 write_complex_part (target, op0, false);
9092 write_complex_part (target, op1, true);
9094 return target;
9096 case WIDEN_SUM_EXPR:
9098 tree oprnd0 = treeop0;
9099 tree oprnd1 = treeop1;
9101 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9102 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9103 target, unsignedp);
9104 return target;
9107 case REDUC_MAX_EXPR:
9108 case REDUC_MIN_EXPR:
9109 case REDUC_PLUS_EXPR:
9111 op0 = expand_normal (treeop0);
9112 this_optab = optab_for_tree_code (code, type, optab_default);
9113 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9115 if (optab_handler (this_optab, vec_mode) != CODE_FOR_nothing)
9117 struct expand_operand ops[2];
9118 enum insn_code icode = optab_handler (this_optab, vec_mode);
9120 create_output_operand (&ops[0], target, mode);
9121 create_input_operand (&ops[1], op0, vec_mode);
9122 if (maybe_expand_insn (icode, 2, ops))
9124 target = ops[0].value;
9125 if (GET_MODE (target) != mode)
9126 return gen_lowpart (tmode, target);
9127 return target;
9130 /* Fall back to optab with vector result, and then extract scalar. */
9131 this_optab = scalar_reduc_to_vector (this_optab, type);
9132 temp = expand_unop (vec_mode, this_optab, op0, NULL_RTX, unsignedp);
9133 gcc_assert (temp);
9134 /* The tree code produces a scalar result, but (somewhat by convention)
9135 the optab produces a vector with the result in element 0 if
9136 little-endian, or element N-1 if big-endian. So pull the scalar
9137 result out of that element. */
9138 int index = BYTES_BIG_ENDIAN ? GET_MODE_NUNITS (vec_mode) - 1 : 0;
9139 int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (vec_mode));
9140 temp = extract_bit_field (temp, bitsize, bitsize * index, unsignedp,
9141 target, mode, mode);
9142 gcc_assert (temp);
9143 return temp;
9146 case VEC_UNPACK_HI_EXPR:
9147 case VEC_UNPACK_LO_EXPR:
9149 op0 = expand_normal (treeop0);
9150 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9151 target, unsignedp);
9152 gcc_assert (temp);
9153 return temp;
9156 case VEC_UNPACK_FLOAT_HI_EXPR:
9157 case VEC_UNPACK_FLOAT_LO_EXPR:
9159 op0 = expand_normal (treeop0);
9160 /* The signedness is determined from input operand. */
9161 temp = expand_widen_pattern_expr
9162 (ops, op0, NULL_RTX, NULL_RTX,
9163 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9165 gcc_assert (temp);
9166 return temp;
9169 case VEC_WIDEN_MULT_HI_EXPR:
9170 case VEC_WIDEN_MULT_LO_EXPR:
9171 case VEC_WIDEN_MULT_EVEN_EXPR:
9172 case VEC_WIDEN_MULT_ODD_EXPR:
9173 case VEC_WIDEN_LSHIFT_HI_EXPR:
9174 case VEC_WIDEN_LSHIFT_LO_EXPR:
9175 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9176 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9177 target, unsignedp);
9178 gcc_assert (target);
9179 return target;
9181 case VEC_PACK_TRUNC_EXPR:
9182 case VEC_PACK_SAT_EXPR:
9183 case VEC_PACK_FIX_TRUNC_EXPR:
9184 mode = TYPE_MODE (TREE_TYPE (treeop0));
9185 goto binop;
9187 case VEC_PERM_EXPR:
9188 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9189 op2 = expand_normal (treeop2);
9191 /* Careful here: if the target doesn't support integral vector modes,
9192 a constant selection vector could wind up smooshed into a normal
9193 integral constant. */
9194 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9196 tree sel_type = TREE_TYPE (treeop2);
9197 machine_mode vmode
9198 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9199 TYPE_VECTOR_SUBPARTS (sel_type));
9200 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9201 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9202 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9204 else
9205 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9207 temp = expand_vec_perm (mode, op0, op1, op2, target);
9208 gcc_assert (temp);
9209 return temp;
9211 case DOT_PROD_EXPR:
9213 tree oprnd0 = treeop0;
9214 tree oprnd1 = treeop1;
9215 tree oprnd2 = treeop2;
9216 rtx op2;
9218 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9219 op2 = expand_normal (oprnd2);
9220 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9221 target, unsignedp);
9222 return target;
9225 case SAD_EXPR:
9227 tree oprnd0 = treeop0;
9228 tree oprnd1 = treeop1;
9229 tree oprnd2 = treeop2;
9230 rtx op2;
9232 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9233 op2 = expand_normal (oprnd2);
9234 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9235 target, unsignedp);
9236 return target;
9239 case REALIGN_LOAD_EXPR:
9241 tree oprnd0 = treeop0;
9242 tree oprnd1 = treeop1;
9243 tree oprnd2 = treeop2;
9244 rtx op2;
9246 this_optab = optab_for_tree_code (code, type, optab_default);
9247 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9248 op2 = expand_normal (oprnd2);
9249 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9250 target, unsignedp);
9251 gcc_assert (temp);
9252 return temp;
9255 case COND_EXPR:
9256 /* A COND_EXPR with its type being VOID_TYPE represents a
9257 conditional jump and is handled in
9258 expand_gimple_cond_expr. */
9259 gcc_assert (!VOID_TYPE_P (type));
9261 /* Note that COND_EXPRs whose type is a structure or union
9262 are required to be constructed to contain assignments of
9263 a temporary variable, so that we can evaluate them here
9264 for side effect only. If type is void, we must do likewise. */
9266 gcc_assert (!TREE_ADDRESSABLE (type)
9267 && !ignore
9268 && TREE_TYPE (treeop1) != void_type_node
9269 && TREE_TYPE (treeop2) != void_type_node);
9271 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9272 if (temp)
9273 return temp;
9275 /* If we are not to produce a result, we have no target. Otherwise,
9276 if a target was specified use it; it will not be used as an
9277 intermediate target unless it is safe. If no target, use a
9278 temporary. */
9280 if (modifier != EXPAND_STACK_PARM
9281 && original_target
9282 && safe_from_p (original_target, treeop0, 1)
9283 && GET_MODE (original_target) == mode
9284 && !MEM_P (original_target))
9285 temp = original_target;
9286 else
9287 temp = assign_temp (type, 0, 1);
9289 do_pending_stack_adjust ();
9290 NO_DEFER_POP;
9291 op0 = gen_label_rtx ();
9292 op1 = gen_label_rtx ();
9293 jumpifnot (treeop0, op0, -1);
9294 store_expr (treeop1, temp,
9295 modifier == EXPAND_STACK_PARM,
9296 false);
9298 emit_jump_insn (gen_jump (op1));
9299 emit_barrier ();
9300 emit_label (op0);
9301 store_expr (treeop2, temp,
9302 modifier == EXPAND_STACK_PARM,
9303 false);
9305 emit_label (op1);
9306 OK_DEFER_POP;
9307 return temp;
9309 case VEC_COND_EXPR:
9310 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9311 return target;
9313 default:
9314 gcc_unreachable ();
9317 /* Here to do an ordinary binary operator. */
9318 binop:
9319 expand_operands (treeop0, treeop1,
9320 subtarget, &op0, &op1, EXPAND_NORMAL);
9321 binop2:
9322 this_optab = optab_for_tree_code (code, type, optab_default);
9323 binop3:
9324 if (modifier == EXPAND_STACK_PARM)
9325 target = 0;
9326 temp = expand_binop (mode, this_optab, op0, op1, target,
9327 unsignedp, OPTAB_LIB_WIDEN);
9328 gcc_assert (temp);
9329 /* Bitwise operations do not need bitfield reduction as we expect their
9330 operands being properly truncated. */
9331 if (code == BIT_XOR_EXPR
9332 || code == BIT_AND_EXPR
9333 || code == BIT_IOR_EXPR)
9334 return temp;
9335 return REDUCE_BIT_FIELD (temp);
9337 #undef REDUCE_BIT_FIELD
9340 /* Return TRUE if expression STMT is suitable for replacement.
9341 Never consider memory loads as replaceable, because those don't ever lead
9342 into constant expressions. */
9344 static bool
9345 stmt_is_replaceable_p (gimple stmt)
9347 if (ssa_is_replaceable_p (stmt))
9349 /* Don't move around loads. */
9350 if (!gimple_assign_single_p (stmt)
9351 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9352 return true;
9354 return false;
9358 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9359 enum expand_modifier modifier, rtx *alt_rtl,
9360 bool inner_reference_p)
9362 rtx op0, op1, temp, decl_rtl;
9363 tree type;
9364 int unsignedp;
9365 machine_mode mode;
9366 enum tree_code code = TREE_CODE (exp);
9367 rtx subtarget, original_target;
9368 int ignore;
9369 tree context;
9370 bool reduce_bit_field;
9371 location_t loc = EXPR_LOCATION (exp);
9372 struct separate_ops ops;
9373 tree treeop0, treeop1, treeop2;
9374 tree ssa_name = NULL_TREE;
9375 gimple g;
9377 type = TREE_TYPE (exp);
9378 mode = TYPE_MODE (type);
9379 unsignedp = TYPE_UNSIGNED (type);
9381 treeop0 = treeop1 = treeop2 = NULL_TREE;
9382 if (!VL_EXP_CLASS_P (exp))
9383 switch (TREE_CODE_LENGTH (code))
9385 default:
9386 case 3: treeop2 = TREE_OPERAND (exp, 2);
9387 case 2: treeop1 = TREE_OPERAND (exp, 1);
9388 case 1: treeop0 = TREE_OPERAND (exp, 0);
9389 case 0: break;
9391 ops.code = code;
9392 ops.type = type;
9393 ops.op0 = treeop0;
9394 ops.op1 = treeop1;
9395 ops.op2 = treeop2;
9396 ops.location = loc;
9398 ignore = (target == const0_rtx
9399 || ((CONVERT_EXPR_CODE_P (code)
9400 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9401 && TREE_CODE (type) == VOID_TYPE));
9403 /* An operation in what may be a bit-field type needs the
9404 result to be reduced to the precision of the bit-field type,
9405 which is narrower than that of the type's mode. */
9406 reduce_bit_field = (!ignore
9407 && INTEGRAL_TYPE_P (type)
9408 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9410 /* If we are going to ignore this result, we need only do something
9411 if there is a side-effect somewhere in the expression. If there
9412 is, short-circuit the most common cases here. Note that we must
9413 not call expand_expr with anything but const0_rtx in case this
9414 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9416 if (ignore)
9418 if (! TREE_SIDE_EFFECTS (exp))
9419 return const0_rtx;
9421 /* Ensure we reference a volatile object even if value is ignored, but
9422 don't do this if all we are doing is taking its address. */
9423 if (TREE_THIS_VOLATILE (exp)
9424 && TREE_CODE (exp) != FUNCTION_DECL
9425 && mode != VOIDmode && mode != BLKmode
9426 && modifier != EXPAND_CONST_ADDRESS)
9428 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9429 if (MEM_P (temp))
9430 copy_to_reg (temp);
9431 return const0_rtx;
9434 if (TREE_CODE_CLASS (code) == tcc_unary
9435 || code == BIT_FIELD_REF
9436 || code == COMPONENT_REF
9437 || code == INDIRECT_REF)
9438 return expand_expr (treeop0, const0_rtx, VOIDmode,
9439 modifier);
9441 else if (TREE_CODE_CLASS (code) == tcc_binary
9442 || TREE_CODE_CLASS (code) == tcc_comparison
9443 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9445 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9446 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9447 return const0_rtx;
9450 target = 0;
9453 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9454 target = 0;
9456 /* Use subtarget as the target for operand 0 of a binary operation. */
9457 subtarget = get_subtarget (target);
9458 original_target = target;
9460 switch (code)
9462 case LABEL_DECL:
9464 tree function = decl_function_context (exp);
9466 temp = label_rtx (exp);
9467 temp = gen_rtx_LABEL_REF (Pmode, temp);
9469 if (function != current_function_decl
9470 && function != 0)
9471 LABEL_REF_NONLOCAL_P (temp) = 1;
9473 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9474 return temp;
9477 case SSA_NAME:
9478 /* ??? ivopts calls expander, without any preparation from
9479 out-of-ssa. So fake instructions as if this was an access to the
9480 base variable. This unnecessarily allocates a pseudo, see how we can
9481 reuse it, if partition base vars have it set already. */
9482 if (!currently_expanding_to_rtl)
9484 tree var = SSA_NAME_VAR (exp);
9485 if (var && DECL_RTL_SET_P (var))
9486 return DECL_RTL (var);
9487 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9488 LAST_VIRTUAL_REGISTER + 1);
9491 g = get_gimple_for_ssa_name (exp);
9492 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9493 if (g == NULL
9494 && modifier == EXPAND_INITIALIZER
9495 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9496 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9497 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9498 g = SSA_NAME_DEF_STMT (exp);
9499 if (g)
9501 rtx r;
9502 ops.code = gimple_assign_rhs_code (g);
9503 switch (get_gimple_rhs_class (ops.code))
9505 case GIMPLE_TERNARY_RHS:
9506 ops.op2 = gimple_assign_rhs3 (g);
9507 /* Fallthru */
9508 case GIMPLE_BINARY_RHS:
9509 ops.op1 = gimple_assign_rhs2 (g);
9511 /* Try to expand conditonal compare. */
9512 if (targetm.gen_ccmp_first)
9514 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9515 r = expand_ccmp_expr (g);
9516 if (r)
9517 break;
9519 /* Fallthru */
9520 case GIMPLE_UNARY_RHS:
9521 ops.op0 = gimple_assign_rhs1 (g);
9522 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9523 ops.location = gimple_location (g);
9524 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9525 break;
9526 case GIMPLE_SINGLE_RHS:
9528 location_t saved_loc = curr_insn_location ();
9529 set_curr_insn_location (gimple_location (g));
9530 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9531 tmode, modifier, NULL, inner_reference_p);
9532 set_curr_insn_location (saved_loc);
9533 break;
9535 default:
9536 gcc_unreachable ();
9538 if (REG_P (r) && !REG_EXPR (r))
9539 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9540 return r;
9543 ssa_name = exp;
9544 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9545 exp = SSA_NAME_VAR (ssa_name);
9546 goto expand_decl_rtl;
9548 case PARM_DECL:
9549 case VAR_DECL:
9550 /* If a static var's type was incomplete when the decl was written,
9551 but the type is complete now, lay out the decl now. */
9552 if (DECL_SIZE (exp) == 0
9553 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9554 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9555 layout_decl (exp, 0);
9557 /* ... fall through ... */
9559 case FUNCTION_DECL:
9560 case RESULT_DECL:
9561 decl_rtl = DECL_RTL (exp);
9562 expand_decl_rtl:
9563 gcc_assert (decl_rtl);
9564 decl_rtl = copy_rtx (decl_rtl);
9565 /* Record writes to register variables. */
9566 if (modifier == EXPAND_WRITE
9567 && REG_P (decl_rtl)
9568 && HARD_REGISTER_P (decl_rtl))
9569 add_to_hard_reg_set (&crtl->asm_clobbers,
9570 GET_MODE (decl_rtl), REGNO (decl_rtl));
9572 /* Ensure variable marked as used even if it doesn't go through
9573 a parser. If it hasn't be used yet, write out an external
9574 definition. */
9575 TREE_USED (exp) = 1;
9577 /* Show we haven't gotten RTL for this yet. */
9578 temp = 0;
9580 /* Variables inherited from containing functions should have
9581 been lowered by this point. */
9582 context = decl_function_context (exp);
9583 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9584 || context == current_function_decl
9585 || TREE_STATIC (exp)
9586 || DECL_EXTERNAL (exp)
9587 /* ??? C++ creates functions that are not TREE_STATIC. */
9588 || TREE_CODE (exp) == FUNCTION_DECL);
9590 /* This is the case of an array whose size is to be determined
9591 from its initializer, while the initializer is still being parsed.
9592 ??? We aren't parsing while expanding anymore. */
9594 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9595 temp = validize_mem (decl_rtl);
9597 /* If DECL_RTL is memory, we are in the normal case and the
9598 address is not valid, get the address into a register. */
9600 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9602 if (alt_rtl)
9603 *alt_rtl = decl_rtl;
9604 decl_rtl = use_anchored_address (decl_rtl);
9605 if (modifier != EXPAND_CONST_ADDRESS
9606 && modifier != EXPAND_SUM
9607 && !memory_address_addr_space_p (DECL_MODE (exp),
9608 XEXP (decl_rtl, 0),
9609 MEM_ADDR_SPACE (decl_rtl)))
9610 temp = replace_equiv_address (decl_rtl,
9611 copy_rtx (XEXP (decl_rtl, 0)));
9614 /* If we got something, return it. But first, set the alignment
9615 if the address is a register. */
9616 if (temp != 0)
9618 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9619 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9621 return temp;
9624 /* If the mode of DECL_RTL does not match that of the decl,
9625 there are two cases: we are dealing with a BLKmode value
9626 that is returned in a register, or we are dealing with
9627 a promoted value. In the latter case, return a SUBREG
9628 of the wanted mode, but mark it so that we know that it
9629 was already extended. */
9630 if (REG_P (decl_rtl)
9631 && DECL_MODE (exp) != BLKmode
9632 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9634 machine_mode pmode;
9636 /* Get the signedness to be used for this variable. Ensure we get
9637 the same mode we got when the variable was declared. */
9638 if (code == SSA_NAME
9639 && (g = SSA_NAME_DEF_STMT (ssa_name))
9640 && gimple_code (g) == GIMPLE_CALL
9641 && !gimple_call_internal_p (g))
9642 pmode = promote_function_mode (type, mode, &unsignedp,
9643 gimple_call_fntype (g),
9645 else
9646 pmode = promote_decl_mode (exp, &unsignedp);
9647 gcc_assert (GET_MODE (decl_rtl) == pmode);
9649 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9650 SUBREG_PROMOTED_VAR_P (temp) = 1;
9651 SUBREG_PROMOTED_SET (temp, unsignedp);
9652 return temp;
9655 return decl_rtl;
9657 case INTEGER_CST:
9658 /* Given that TYPE_PRECISION (type) is not always equal to
9659 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9660 the former to the latter according to the signedness of the
9661 type. */
9662 temp = immed_wide_int_const (wide_int::from
9663 (exp,
9664 GET_MODE_PRECISION (TYPE_MODE (type)),
9665 TYPE_SIGN (type)),
9666 TYPE_MODE (type));
9667 return temp;
9669 case VECTOR_CST:
9671 tree tmp = NULL_TREE;
9672 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9673 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9674 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9675 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9676 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9677 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9678 return const_vector_from_tree (exp);
9679 if (GET_MODE_CLASS (mode) == MODE_INT)
9681 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9682 if (type_for_mode)
9683 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9685 if (!tmp)
9687 vec<constructor_elt, va_gc> *v;
9688 unsigned i;
9689 vec_alloc (v, VECTOR_CST_NELTS (exp));
9690 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9691 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9692 tmp = build_constructor (type, v);
9694 return expand_expr (tmp, ignore ? const0_rtx : target,
9695 tmode, modifier);
9698 case CONST_DECL:
9699 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9701 case REAL_CST:
9702 /* If optimized, generate immediate CONST_DOUBLE
9703 which will be turned into memory by reload if necessary.
9705 We used to force a register so that loop.c could see it. But
9706 this does not allow gen_* patterns to perform optimizations with
9707 the constants. It also produces two insns in cases like "x = 1.0;".
9708 On most machines, floating-point constants are not permitted in
9709 many insns, so we'd end up copying it to a register in any case.
9711 Now, we do the copying in expand_binop, if appropriate. */
9712 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9713 TYPE_MODE (TREE_TYPE (exp)));
9715 case FIXED_CST:
9716 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9717 TYPE_MODE (TREE_TYPE (exp)));
9719 case COMPLEX_CST:
9720 /* Handle evaluating a complex constant in a CONCAT target. */
9721 if (original_target && GET_CODE (original_target) == CONCAT)
9723 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9724 rtx rtarg, itarg;
9726 rtarg = XEXP (original_target, 0);
9727 itarg = XEXP (original_target, 1);
9729 /* Move the real and imaginary parts separately. */
9730 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9731 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9733 if (op0 != rtarg)
9734 emit_move_insn (rtarg, op0);
9735 if (op1 != itarg)
9736 emit_move_insn (itarg, op1);
9738 return original_target;
9741 /* ... fall through ... */
9743 case STRING_CST:
9744 temp = expand_expr_constant (exp, 1, modifier);
9746 /* temp contains a constant address.
9747 On RISC machines where a constant address isn't valid,
9748 make some insns to get that address into a register. */
9749 if (modifier != EXPAND_CONST_ADDRESS
9750 && modifier != EXPAND_INITIALIZER
9751 && modifier != EXPAND_SUM
9752 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9753 MEM_ADDR_SPACE (temp)))
9754 return replace_equiv_address (temp,
9755 copy_rtx (XEXP (temp, 0)));
9756 return temp;
9758 case SAVE_EXPR:
9760 tree val = treeop0;
9761 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9762 inner_reference_p);
9764 if (!SAVE_EXPR_RESOLVED_P (exp))
9766 /* We can indeed still hit this case, typically via builtin
9767 expanders calling save_expr immediately before expanding
9768 something. Assume this means that we only have to deal
9769 with non-BLKmode values. */
9770 gcc_assert (GET_MODE (ret) != BLKmode);
9772 val = build_decl (curr_insn_location (),
9773 VAR_DECL, NULL, TREE_TYPE (exp));
9774 DECL_ARTIFICIAL (val) = 1;
9775 DECL_IGNORED_P (val) = 1;
9776 treeop0 = val;
9777 TREE_OPERAND (exp, 0) = treeop0;
9778 SAVE_EXPR_RESOLVED_P (exp) = 1;
9780 if (!CONSTANT_P (ret))
9781 ret = copy_to_reg (ret);
9782 SET_DECL_RTL (val, ret);
9785 return ret;
9789 case CONSTRUCTOR:
9790 /* If we don't need the result, just ensure we evaluate any
9791 subexpressions. */
9792 if (ignore)
9794 unsigned HOST_WIDE_INT idx;
9795 tree value;
9797 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9798 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9800 return const0_rtx;
9803 return expand_constructor (exp, target, modifier, false);
9805 case TARGET_MEM_REF:
9807 addr_space_t as
9808 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9809 enum insn_code icode;
9810 unsigned int align;
9812 op0 = addr_for_mem_ref (exp, as, true);
9813 op0 = memory_address_addr_space (mode, op0, as);
9814 temp = gen_rtx_MEM (mode, op0);
9815 set_mem_attributes (temp, exp, 0);
9816 set_mem_addr_space (temp, as);
9817 align = get_object_alignment (exp);
9818 if (modifier != EXPAND_WRITE
9819 && modifier != EXPAND_MEMORY
9820 && mode != BLKmode
9821 && align < GET_MODE_ALIGNMENT (mode)
9822 /* If the target does not have special handling for unaligned
9823 loads of mode then it can use regular moves for them. */
9824 && ((icode = optab_handler (movmisalign_optab, mode))
9825 != CODE_FOR_nothing))
9827 struct expand_operand ops[2];
9829 /* We've already validated the memory, and we're creating a
9830 new pseudo destination. The predicates really can't fail,
9831 nor can the generator. */
9832 create_output_operand (&ops[0], NULL_RTX, mode);
9833 create_fixed_operand (&ops[1], temp);
9834 expand_insn (icode, 2, ops);
9835 temp = ops[0].value;
9837 return temp;
9840 case MEM_REF:
9842 addr_space_t as
9843 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9844 machine_mode address_mode;
9845 tree base = TREE_OPERAND (exp, 0);
9846 gimple def_stmt;
9847 enum insn_code icode;
9848 unsigned align;
9849 /* Handle expansion of non-aliased memory with non-BLKmode. That
9850 might end up in a register. */
9851 if (mem_ref_refers_to_non_mem_p (exp))
9853 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9854 base = TREE_OPERAND (base, 0);
9855 if (offset == 0
9856 && tree_fits_uhwi_p (TYPE_SIZE (type))
9857 && (GET_MODE_BITSIZE (DECL_MODE (base))
9858 == tree_to_uhwi (TYPE_SIZE (type))))
9859 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9860 target, tmode, modifier);
9861 if (TYPE_MODE (type) == BLKmode)
9863 temp = assign_stack_temp (DECL_MODE (base),
9864 GET_MODE_SIZE (DECL_MODE (base)));
9865 store_expr (base, temp, 0, false);
9866 temp = adjust_address (temp, BLKmode, offset);
9867 set_mem_size (temp, int_size_in_bytes (type));
9868 return temp;
9870 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9871 bitsize_int (offset * BITS_PER_UNIT));
9872 return expand_expr (exp, target, tmode, modifier);
9874 address_mode = targetm.addr_space.address_mode (as);
9875 base = TREE_OPERAND (exp, 0);
9876 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9878 tree mask = gimple_assign_rhs2 (def_stmt);
9879 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9880 gimple_assign_rhs1 (def_stmt), mask);
9881 TREE_OPERAND (exp, 0) = base;
9883 align = get_object_alignment (exp);
9884 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9885 op0 = memory_address_addr_space (mode, op0, as);
9886 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9888 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9889 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9890 op0 = memory_address_addr_space (mode, op0, as);
9892 temp = gen_rtx_MEM (mode, op0);
9893 set_mem_attributes (temp, exp, 0);
9894 set_mem_addr_space (temp, as);
9895 if (TREE_THIS_VOLATILE (exp))
9896 MEM_VOLATILE_P (temp) = 1;
9897 if (modifier != EXPAND_WRITE
9898 && modifier != EXPAND_MEMORY
9899 && !inner_reference_p
9900 && mode != BLKmode
9901 && align < GET_MODE_ALIGNMENT (mode))
9903 if ((icode = optab_handler (movmisalign_optab, mode))
9904 != CODE_FOR_nothing)
9906 struct expand_operand ops[2];
9908 /* We've already validated the memory, and we're creating a
9909 new pseudo destination. The predicates really can't fail,
9910 nor can the generator. */
9911 create_output_operand (&ops[0], NULL_RTX, mode);
9912 create_fixed_operand (&ops[1], temp);
9913 expand_insn (icode, 2, ops);
9914 temp = ops[0].value;
9916 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9917 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9918 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9919 (modifier == EXPAND_STACK_PARM
9920 ? NULL_RTX : target),
9921 mode, mode);
9923 return temp;
9926 case ARRAY_REF:
9929 tree array = treeop0;
9930 tree index = treeop1;
9931 tree init;
9933 /* Fold an expression like: "foo"[2].
9934 This is not done in fold so it won't happen inside &.
9935 Don't fold if this is for wide characters since it's too
9936 difficult to do correctly and this is a very rare case. */
9938 if (modifier != EXPAND_CONST_ADDRESS
9939 && modifier != EXPAND_INITIALIZER
9940 && modifier != EXPAND_MEMORY)
9942 tree t = fold_read_from_constant_string (exp);
9944 if (t)
9945 return expand_expr (t, target, tmode, modifier);
9948 /* If this is a constant index into a constant array,
9949 just get the value from the array. Handle both the cases when
9950 we have an explicit constructor and when our operand is a variable
9951 that was declared const. */
9953 if (modifier != EXPAND_CONST_ADDRESS
9954 && modifier != EXPAND_INITIALIZER
9955 && modifier != EXPAND_MEMORY
9956 && TREE_CODE (array) == CONSTRUCTOR
9957 && ! TREE_SIDE_EFFECTS (array)
9958 && TREE_CODE (index) == INTEGER_CST)
9960 unsigned HOST_WIDE_INT ix;
9961 tree field, value;
9963 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9964 field, value)
9965 if (tree_int_cst_equal (field, index))
9967 if (!TREE_SIDE_EFFECTS (value))
9968 return expand_expr (fold (value), target, tmode, modifier);
9969 break;
9973 else if (optimize >= 1
9974 && modifier != EXPAND_CONST_ADDRESS
9975 && modifier != EXPAND_INITIALIZER
9976 && modifier != EXPAND_MEMORY
9977 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9978 && TREE_CODE (index) == INTEGER_CST
9979 && (TREE_CODE (array) == VAR_DECL
9980 || TREE_CODE (array) == CONST_DECL)
9981 && (init = ctor_for_folding (array)) != error_mark_node)
9983 if (init == NULL_TREE)
9985 tree value = build_zero_cst (type);
9986 if (TREE_CODE (value) == CONSTRUCTOR)
9988 /* If VALUE is a CONSTRUCTOR, this optimization is only
9989 useful if this doesn't store the CONSTRUCTOR into
9990 memory. If it does, it is more efficient to just
9991 load the data from the array directly. */
9992 rtx ret = expand_constructor (value, target,
9993 modifier, true);
9994 if (ret == NULL_RTX)
9995 value = NULL_TREE;
9998 if (value)
9999 return expand_expr (value, target, tmode, modifier);
10001 else if (TREE_CODE (init) == CONSTRUCTOR)
10003 unsigned HOST_WIDE_INT ix;
10004 tree field, value;
10006 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10007 field, value)
10008 if (tree_int_cst_equal (field, index))
10010 if (TREE_SIDE_EFFECTS (value))
10011 break;
10013 if (TREE_CODE (value) == CONSTRUCTOR)
10015 /* If VALUE is a CONSTRUCTOR, this
10016 optimization is only useful if
10017 this doesn't store the CONSTRUCTOR
10018 into memory. If it does, it is more
10019 efficient to just load the data from
10020 the array directly. */
10021 rtx ret = expand_constructor (value, target,
10022 modifier, true);
10023 if (ret == NULL_RTX)
10024 break;
10027 return
10028 expand_expr (fold (value), target, tmode, modifier);
10031 else if (TREE_CODE (init) == STRING_CST)
10033 tree low_bound = array_ref_low_bound (exp);
10034 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10036 /* Optimize the special case of a zero lower bound.
10038 We convert the lower bound to sizetype to avoid problems
10039 with constant folding. E.g. suppose the lower bound is
10040 1 and its mode is QI. Without the conversion
10041 (ARRAY + (INDEX - (unsigned char)1))
10042 becomes
10043 (ARRAY + (-(unsigned char)1) + INDEX)
10044 which becomes
10045 (ARRAY + 255 + INDEX). Oops! */
10046 if (!integer_zerop (low_bound))
10047 index1 = size_diffop_loc (loc, index1,
10048 fold_convert_loc (loc, sizetype,
10049 low_bound));
10051 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10053 tree type = TREE_TYPE (TREE_TYPE (init));
10054 machine_mode mode = TYPE_MODE (type);
10056 if (GET_MODE_CLASS (mode) == MODE_INT
10057 && GET_MODE_SIZE (mode) == 1)
10058 return gen_int_mode (TREE_STRING_POINTER (init)
10059 [TREE_INT_CST_LOW (index1)],
10060 mode);
10065 goto normal_inner_ref;
10067 case COMPONENT_REF:
10068 /* If the operand is a CONSTRUCTOR, we can just extract the
10069 appropriate field if it is present. */
10070 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10072 unsigned HOST_WIDE_INT idx;
10073 tree field, value;
10075 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10076 idx, field, value)
10077 if (field == treeop1
10078 /* We can normally use the value of the field in the
10079 CONSTRUCTOR. However, if this is a bitfield in
10080 an integral mode that we can fit in a HOST_WIDE_INT,
10081 we must mask only the number of bits in the bitfield,
10082 since this is done implicitly by the constructor. If
10083 the bitfield does not meet either of those conditions,
10084 we can't do this optimization. */
10085 && (! DECL_BIT_FIELD (field)
10086 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10087 && (GET_MODE_PRECISION (DECL_MODE (field))
10088 <= HOST_BITS_PER_WIDE_INT))))
10090 if (DECL_BIT_FIELD (field)
10091 && modifier == EXPAND_STACK_PARM)
10092 target = 0;
10093 op0 = expand_expr (value, target, tmode, modifier);
10094 if (DECL_BIT_FIELD (field))
10096 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10097 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10099 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10101 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10102 imode);
10103 op0 = expand_and (imode, op0, op1, target);
10105 else
10107 int count = GET_MODE_PRECISION (imode) - bitsize;
10109 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10110 target, 0);
10111 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10112 target, 0);
10116 return op0;
10119 goto normal_inner_ref;
10121 case BIT_FIELD_REF:
10122 case ARRAY_RANGE_REF:
10123 normal_inner_ref:
10125 machine_mode mode1, mode2;
10126 HOST_WIDE_INT bitsize, bitpos;
10127 tree offset;
10128 int volatilep = 0, must_force_mem;
10129 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10130 &mode1, &unsignedp, &volatilep, true);
10131 rtx orig_op0, memloc;
10132 bool mem_attrs_from_type = false;
10134 /* If we got back the original object, something is wrong. Perhaps
10135 we are evaluating an expression too early. In any event, don't
10136 infinitely recurse. */
10137 gcc_assert (tem != exp);
10139 /* If TEM's type is a union of variable size, pass TARGET to the inner
10140 computation, since it will need a temporary and TARGET is known
10141 to have to do. This occurs in unchecked conversion in Ada. */
10142 orig_op0 = op0
10143 = expand_expr_real (tem,
10144 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10145 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10146 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10147 != INTEGER_CST)
10148 && modifier != EXPAND_STACK_PARM
10149 ? target : NULL_RTX),
10150 VOIDmode,
10151 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10152 NULL, true);
10154 /* If the field has a mode, we want to access it in the
10155 field's mode, not the computed mode.
10156 If a MEM has VOIDmode (external with incomplete type),
10157 use BLKmode for it instead. */
10158 if (MEM_P (op0))
10160 if (mode1 != VOIDmode)
10161 op0 = adjust_address (op0, mode1, 0);
10162 else if (GET_MODE (op0) == VOIDmode)
10163 op0 = adjust_address (op0, BLKmode, 0);
10166 mode2
10167 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10169 /* If we have either an offset, a BLKmode result, or a reference
10170 outside the underlying object, we must force it to memory.
10171 Such a case can occur in Ada if we have unchecked conversion
10172 of an expression from a scalar type to an aggregate type or
10173 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10174 passed a partially uninitialized object or a view-conversion
10175 to a larger size. */
10176 must_force_mem = (offset
10177 || mode1 == BLKmode
10178 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10180 /* Handle CONCAT first. */
10181 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10183 if (bitpos == 0
10184 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10185 return op0;
10186 if (bitpos == 0
10187 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10188 && bitsize)
10190 op0 = XEXP (op0, 0);
10191 mode2 = GET_MODE (op0);
10193 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10194 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10195 && bitpos
10196 && bitsize)
10198 op0 = XEXP (op0, 1);
10199 bitpos = 0;
10200 mode2 = GET_MODE (op0);
10202 else
10203 /* Otherwise force into memory. */
10204 must_force_mem = 1;
10207 /* If this is a constant, put it in a register if it is a legitimate
10208 constant and we don't need a memory reference. */
10209 if (CONSTANT_P (op0)
10210 && mode2 != BLKmode
10211 && targetm.legitimate_constant_p (mode2, op0)
10212 && !must_force_mem)
10213 op0 = force_reg (mode2, op0);
10215 /* Otherwise, if this is a constant, try to force it to the constant
10216 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10217 is a legitimate constant. */
10218 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10219 op0 = validize_mem (memloc);
10221 /* Otherwise, if this is a constant or the object is not in memory
10222 and need be, put it there. */
10223 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10225 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10226 emit_move_insn (memloc, op0);
10227 op0 = memloc;
10228 mem_attrs_from_type = true;
10231 if (offset)
10233 machine_mode address_mode;
10234 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10235 EXPAND_SUM);
10237 gcc_assert (MEM_P (op0));
10239 address_mode = get_address_mode (op0);
10240 if (GET_MODE (offset_rtx) != address_mode)
10241 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10243 /* See the comment in expand_assignment for the rationale. */
10244 if (mode1 != VOIDmode
10245 && bitpos != 0
10246 && bitsize > 0
10247 && (bitpos % bitsize) == 0
10248 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10249 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10251 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10252 bitpos = 0;
10255 op0 = offset_address (op0, offset_rtx,
10256 highest_pow2_factor (offset));
10259 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10260 record its alignment as BIGGEST_ALIGNMENT. */
10261 if (MEM_P (op0) && bitpos == 0 && offset != 0
10262 && is_aligning_offset (offset, tem))
10263 set_mem_align (op0, BIGGEST_ALIGNMENT);
10265 /* Don't forget about volatility even if this is a bitfield. */
10266 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10268 if (op0 == orig_op0)
10269 op0 = copy_rtx (op0);
10271 MEM_VOLATILE_P (op0) = 1;
10274 /* In cases where an aligned union has an unaligned object
10275 as a field, we might be extracting a BLKmode value from
10276 an integer-mode (e.g., SImode) object. Handle this case
10277 by doing the extract into an object as wide as the field
10278 (which we know to be the width of a basic mode), then
10279 storing into memory, and changing the mode to BLKmode. */
10280 if (mode1 == VOIDmode
10281 || REG_P (op0) || GET_CODE (op0) == SUBREG
10282 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10283 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10284 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10285 && modifier != EXPAND_CONST_ADDRESS
10286 && modifier != EXPAND_INITIALIZER
10287 && modifier != EXPAND_MEMORY)
10288 /* If the bitfield is volatile and the bitsize
10289 is narrower than the access size of the bitfield,
10290 we need to extract bitfields from the access. */
10291 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10292 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10293 && mode1 != BLKmode
10294 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10295 /* If the field isn't aligned enough to fetch as a memref,
10296 fetch it as a bit field. */
10297 || (mode1 != BLKmode
10298 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10299 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10300 || (MEM_P (op0)
10301 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10302 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10303 && modifier != EXPAND_MEMORY
10304 && ((modifier == EXPAND_CONST_ADDRESS
10305 || modifier == EXPAND_INITIALIZER)
10306 ? STRICT_ALIGNMENT
10307 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10308 || (bitpos % BITS_PER_UNIT != 0)))
10309 /* If the type and the field are a constant size and the
10310 size of the type isn't the same size as the bitfield,
10311 we must use bitfield operations. */
10312 || (bitsize >= 0
10313 && TYPE_SIZE (TREE_TYPE (exp))
10314 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10315 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10316 bitsize)))
10318 machine_mode ext_mode = mode;
10320 if (ext_mode == BLKmode
10321 && ! (target != 0 && MEM_P (op0)
10322 && MEM_P (target)
10323 && bitpos % BITS_PER_UNIT == 0))
10324 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10326 if (ext_mode == BLKmode)
10328 if (target == 0)
10329 target = assign_temp (type, 1, 1);
10331 /* ??? Unlike the similar test a few lines below, this one is
10332 very likely obsolete. */
10333 if (bitsize == 0)
10334 return target;
10336 /* In this case, BITPOS must start at a byte boundary and
10337 TARGET, if specified, must be a MEM. */
10338 gcc_assert (MEM_P (op0)
10339 && (!target || MEM_P (target))
10340 && !(bitpos % BITS_PER_UNIT));
10342 emit_block_move (target,
10343 adjust_address (op0, VOIDmode,
10344 bitpos / BITS_PER_UNIT),
10345 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10346 / BITS_PER_UNIT),
10347 (modifier == EXPAND_STACK_PARM
10348 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10350 return target;
10353 /* If we have nothing to extract, the result will be 0 for targets
10354 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10355 return 0 for the sake of consistency, as reading a zero-sized
10356 bitfield is valid in Ada and the value is fully specified. */
10357 if (bitsize == 0)
10358 return const0_rtx;
10360 op0 = validize_mem (op0);
10362 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10363 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10365 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10366 (modifier == EXPAND_STACK_PARM
10367 ? NULL_RTX : target),
10368 ext_mode, ext_mode);
10370 /* If the result is a record type and BITSIZE is narrower than
10371 the mode of OP0, an integral mode, and this is a big endian
10372 machine, we must put the field into the high-order bits. */
10373 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10374 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10375 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10376 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10377 GET_MODE_BITSIZE (GET_MODE (op0))
10378 - bitsize, op0, 1);
10380 /* If the result type is BLKmode, store the data into a temporary
10381 of the appropriate type, but with the mode corresponding to the
10382 mode for the data we have (op0's mode). */
10383 if (mode == BLKmode)
10385 rtx new_rtx
10386 = assign_stack_temp_for_type (ext_mode,
10387 GET_MODE_BITSIZE (ext_mode),
10388 type);
10389 emit_move_insn (new_rtx, op0);
10390 op0 = copy_rtx (new_rtx);
10391 PUT_MODE (op0, BLKmode);
10394 return op0;
10397 /* If the result is BLKmode, use that to access the object
10398 now as well. */
10399 if (mode == BLKmode)
10400 mode1 = BLKmode;
10402 /* Get a reference to just this component. */
10403 if (modifier == EXPAND_CONST_ADDRESS
10404 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10405 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10406 else
10407 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10409 if (op0 == orig_op0)
10410 op0 = copy_rtx (op0);
10412 /* If op0 is a temporary because of forcing to memory, pass only the
10413 type to set_mem_attributes so that the original expression is never
10414 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10415 if (mem_attrs_from_type)
10416 set_mem_attributes (op0, type, 0);
10417 else
10418 set_mem_attributes (op0, exp, 0);
10420 if (REG_P (XEXP (op0, 0)))
10421 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10423 MEM_VOLATILE_P (op0) |= volatilep;
10424 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10425 || modifier == EXPAND_CONST_ADDRESS
10426 || modifier == EXPAND_INITIALIZER)
10427 return op0;
10429 if (target == 0)
10430 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10432 convert_move (target, op0, unsignedp);
10433 return target;
10436 case OBJ_TYPE_REF:
10437 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10439 case CALL_EXPR:
10440 /* All valid uses of __builtin_va_arg_pack () are removed during
10441 inlining. */
10442 if (CALL_EXPR_VA_ARG_PACK (exp))
10443 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10445 tree fndecl = get_callee_fndecl (exp), attr;
10447 if (fndecl
10448 && (attr = lookup_attribute ("error",
10449 DECL_ATTRIBUTES (fndecl))) != NULL)
10450 error ("%Kcall to %qs declared with attribute error: %s",
10451 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10452 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10453 if (fndecl
10454 && (attr = lookup_attribute ("warning",
10455 DECL_ATTRIBUTES (fndecl))) != NULL)
10456 warning_at (tree_nonartificial_location (exp),
10457 0, "%Kcall to %qs declared with attribute warning: %s",
10458 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10459 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10461 /* Check for a built-in function. */
10462 if (fndecl && DECL_BUILT_IN (fndecl))
10464 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10465 if (CALL_WITH_BOUNDS_P (exp))
10466 return expand_builtin_with_bounds (exp, target, subtarget,
10467 tmode, ignore);
10468 else
10469 return expand_builtin (exp, target, subtarget, tmode, ignore);
10472 return expand_call (exp, target, ignore);
10474 case VIEW_CONVERT_EXPR:
10475 op0 = NULL_RTX;
10477 /* If we are converting to BLKmode, try to avoid an intermediate
10478 temporary by fetching an inner memory reference. */
10479 if (mode == BLKmode
10480 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10481 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10482 && handled_component_p (treeop0))
10484 machine_mode mode1;
10485 HOST_WIDE_INT bitsize, bitpos;
10486 tree offset;
10487 int unsignedp;
10488 int volatilep = 0;
10489 tree tem
10490 = get_inner_reference (treeop0, &bitsize, &bitpos,
10491 &offset, &mode1, &unsignedp, &volatilep,
10492 true);
10493 rtx orig_op0;
10495 /* ??? We should work harder and deal with non-zero offsets. */
10496 if (!offset
10497 && (bitpos % BITS_PER_UNIT) == 0
10498 && bitsize >= 0
10499 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10501 /* See the normal_inner_ref case for the rationale. */
10502 orig_op0
10503 = expand_expr_real (tem,
10504 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10505 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10506 != INTEGER_CST)
10507 && modifier != EXPAND_STACK_PARM
10508 ? target : NULL_RTX),
10509 VOIDmode,
10510 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10511 NULL, true);
10513 if (MEM_P (orig_op0))
10515 op0 = orig_op0;
10517 /* Get a reference to just this component. */
10518 if (modifier == EXPAND_CONST_ADDRESS
10519 || modifier == EXPAND_SUM
10520 || modifier == EXPAND_INITIALIZER)
10521 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10522 else
10523 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10525 if (op0 == orig_op0)
10526 op0 = copy_rtx (op0);
10528 set_mem_attributes (op0, treeop0, 0);
10529 if (REG_P (XEXP (op0, 0)))
10530 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10532 MEM_VOLATILE_P (op0) |= volatilep;
10537 if (!op0)
10538 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10539 NULL, inner_reference_p);
10541 /* If the input and output modes are both the same, we are done. */
10542 if (mode == GET_MODE (op0))
10544 /* If neither mode is BLKmode, and both modes are the same size
10545 then we can use gen_lowpart. */
10546 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10547 && (GET_MODE_PRECISION (mode)
10548 == GET_MODE_PRECISION (GET_MODE (op0)))
10549 && !COMPLEX_MODE_P (GET_MODE (op0)))
10551 if (GET_CODE (op0) == SUBREG)
10552 op0 = force_reg (GET_MODE (op0), op0);
10553 temp = gen_lowpart_common (mode, op0);
10554 if (temp)
10555 op0 = temp;
10556 else
10558 if (!REG_P (op0) && !MEM_P (op0))
10559 op0 = force_reg (GET_MODE (op0), op0);
10560 op0 = gen_lowpart (mode, op0);
10563 /* If both types are integral, convert from one mode to the other. */
10564 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10565 op0 = convert_modes (mode, GET_MODE (op0), op0,
10566 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10567 /* If the output type is a bit-field type, do an extraction. */
10568 else if (reduce_bit_field)
10569 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10570 TYPE_UNSIGNED (type), NULL_RTX,
10571 mode, mode);
10572 /* As a last resort, spill op0 to memory, and reload it in a
10573 different mode. */
10574 else if (!MEM_P (op0))
10576 /* If the operand is not a MEM, force it into memory. Since we
10577 are going to be changing the mode of the MEM, don't call
10578 force_const_mem for constants because we don't allow pool
10579 constants to change mode. */
10580 tree inner_type = TREE_TYPE (treeop0);
10582 gcc_assert (!TREE_ADDRESSABLE (exp));
10584 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10585 target
10586 = assign_stack_temp_for_type
10587 (TYPE_MODE (inner_type),
10588 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10590 emit_move_insn (target, op0);
10591 op0 = target;
10594 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10595 output type is such that the operand is known to be aligned, indicate
10596 that it is. Otherwise, we need only be concerned about alignment for
10597 non-BLKmode results. */
10598 if (MEM_P (op0))
10600 enum insn_code icode;
10602 if (TYPE_ALIGN_OK (type))
10604 /* ??? Copying the MEM without substantially changing it might
10605 run afoul of the code handling volatile memory references in
10606 store_expr, which assumes that TARGET is returned unmodified
10607 if it has been used. */
10608 op0 = copy_rtx (op0);
10609 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10611 else if (modifier != EXPAND_WRITE
10612 && modifier != EXPAND_MEMORY
10613 && !inner_reference_p
10614 && mode != BLKmode
10615 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10617 /* If the target does have special handling for unaligned
10618 loads of mode then use them. */
10619 if ((icode = optab_handler (movmisalign_optab, mode))
10620 != CODE_FOR_nothing)
10622 rtx reg, insn;
10624 op0 = adjust_address (op0, mode, 0);
10625 /* We've already validated the memory, and we're creating a
10626 new pseudo destination. The predicates really can't
10627 fail. */
10628 reg = gen_reg_rtx (mode);
10630 /* Nor can the insn generator. */
10631 insn = GEN_FCN (icode) (reg, op0);
10632 emit_insn (insn);
10633 return reg;
10635 else if (STRICT_ALIGNMENT)
10637 tree inner_type = TREE_TYPE (treeop0);
10638 HOST_WIDE_INT temp_size
10639 = MAX (int_size_in_bytes (inner_type),
10640 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10641 rtx new_rtx
10642 = assign_stack_temp_for_type (mode, temp_size, type);
10643 rtx new_with_op0_mode
10644 = adjust_address (new_rtx, GET_MODE (op0), 0);
10646 gcc_assert (!TREE_ADDRESSABLE (exp));
10648 if (GET_MODE (op0) == BLKmode)
10649 emit_block_move (new_with_op0_mode, op0,
10650 GEN_INT (GET_MODE_SIZE (mode)),
10651 (modifier == EXPAND_STACK_PARM
10652 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10653 else
10654 emit_move_insn (new_with_op0_mode, op0);
10656 op0 = new_rtx;
10660 op0 = adjust_address (op0, mode, 0);
10663 return op0;
10665 case MODIFY_EXPR:
10667 tree lhs = treeop0;
10668 tree rhs = treeop1;
10669 gcc_assert (ignore);
10671 /* Check for |= or &= of a bitfield of size one into another bitfield
10672 of size 1. In this case, (unless we need the result of the
10673 assignment) we can do this more efficiently with a
10674 test followed by an assignment, if necessary.
10676 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10677 things change so we do, this code should be enhanced to
10678 support it. */
10679 if (TREE_CODE (lhs) == COMPONENT_REF
10680 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10681 || TREE_CODE (rhs) == BIT_AND_EXPR)
10682 && TREE_OPERAND (rhs, 0) == lhs
10683 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10684 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10685 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10687 rtx_code_label *label = gen_label_rtx ();
10688 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10689 do_jump (TREE_OPERAND (rhs, 1),
10690 value ? label : 0,
10691 value ? 0 : label, -1);
10692 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10693 false);
10694 do_pending_stack_adjust ();
10695 emit_label (label);
10696 return const0_rtx;
10699 expand_assignment (lhs, rhs, false);
10700 return const0_rtx;
10703 case ADDR_EXPR:
10704 return expand_expr_addr_expr (exp, target, tmode, modifier);
10706 case REALPART_EXPR:
10707 op0 = expand_normal (treeop0);
10708 return read_complex_part (op0, false);
10710 case IMAGPART_EXPR:
10711 op0 = expand_normal (treeop0);
10712 return read_complex_part (op0, true);
10714 case RETURN_EXPR:
10715 case LABEL_EXPR:
10716 case GOTO_EXPR:
10717 case SWITCH_EXPR:
10718 case ASM_EXPR:
10719 /* Expanded in cfgexpand.c. */
10720 gcc_unreachable ();
10722 case TRY_CATCH_EXPR:
10723 case CATCH_EXPR:
10724 case EH_FILTER_EXPR:
10725 case TRY_FINALLY_EXPR:
10726 /* Lowered by tree-eh.c. */
10727 gcc_unreachable ();
10729 case WITH_CLEANUP_EXPR:
10730 case CLEANUP_POINT_EXPR:
10731 case TARGET_EXPR:
10732 case CASE_LABEL_EXPR:
10733 case VA_ARG_EXPR:
10734 case BIND_EXPR:
10735 case INIT_EXPR:
10736 case CONJ_EXPR:
10737 case COMPOUND_EXPR:
10738 case PREINCREMENT_EXPR:
10739 case PREDECREMENT_EXPR:
10740 case POSTINCREMENT_EXPR:
10741 case POSTDECREMENT_EXPR:
10742 case LOOP_EXPR:
10743 case EXIT_EXPR:
10744 case COMPOUND_LITERAL_EXPR:
10745 /* Lowered by gimplify.c. */
10746 gcc_unreachable ();
10748 case FDESC_EXPR:
10749 /* Function descriptors are not valid except for as
10750 initialization constants, and should not be expanded. */
10751 gcc_unreachable ();
10753 case WITH_SIZE_EXPR:
10754 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10755 have pulled out the size to use in whatever context it needed. */
10756 return expand_expr_real (treeop0, original_target, tmode,
10757 modifier, alt_rtl, inner_reference_p);
10759 default:
10760 return expand_expr_real_2 (&ops, target, tmode, modifier);
10764 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10765 signedness of TYPE), possibly returning the result in TARGET. */
10766 static rtx
10767 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10769 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10770 if (target && GET_MODE (target) != GET_MODE (exp))
10771 target = 0;
10772 /* For constant values, reduce using build_int_cst_type. */
10773 if (CONST_INT_P (exp))
10775 HOST_WIDE_INT value = INTVAL (exp);
10776 tree t = build_int_cst_type (type, value);
10777 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10779 else if (TYPE_UNSIGNED (type))
10781 machine_mode mode = GET_MODE (exp);
10782 rtx mask = immed_wide_int_const
10783 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10784 return expand_and (mode, exp, mask, target);
10786 else
10788 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10789 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10790 exp, count, target, 0);
10791 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10792 exp, count, target, 0);
10796 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10797 when applied to the address of EXP produces an address known to be
10798 aligned more than BIGGEST_ALIGNMENT. */
10800 static int
10801 is_aligning_offset (const_tree offset, const_tree exp)
10803 /* Strip off any conversions. */
10804 while (CONVERT_EXPR_P (offset))
10805 offset = TREE_OPERAND (offset, 0);
10807 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10808 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10809 if (TREE_CODE (offset) != BIT_AND_EXPR
10810 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10811 || compare_tree_int (TREE_OPERAND (offset, 1),
10812 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10813 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10814 return 0;
10816 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10817 It must be NEGATE_EXPR. Then strip any more conversions. */
10818 offset = TREE_OPERAND (offset, 0);
10819 while (CONVERT_EXPR_P (offset))
10820 offset = TREE_OPERAND (offset, 0);
10822 if (TREE_CODE (offset) != NEGATE_EXPR)
10823 return 0;
10825 offset = TREE_OPERAND (offset, 0);
10826 while (CONVERT_EXPR_P (offset))
10827 offset = TREE_OPERAND (offset, 0);
10829 /* This must now be the address of EXP. */
10830 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10833 /* Return the tree node if an ARG corresponds to a string constant or zero
10834 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10835 in bytes within the string that ARG is accessing. The type of the
10836 offset will be `sizetype'. */
10838 tree
10839 string_constant (tree arg, tree *ptr_offset)
10841 tree array, offset, lower_bound;
10842 STRIP_NOPS (arg);
10844 if (TREE_CODE (arg) == ADDR_EXPR)
10846 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10848 *ptr_offset = size_zero_node;
10849 return TREE_OPERAND (arg, 0);
10851 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10853 array = TREE_OPERAND (arg, 0);
10854 offset = size_zero_node;
10856 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10858 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10859 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10860 if (TREE_CODE (array) != STRING_CST
10861 && TREE_CODE (array) != VAR_DECL)
10862 return 0;
10864 /* Check if the array has a nonzero lower bound. */
10865 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10866 if (!integer_zerop (lower_bound))
10868 /* If the offset and base aren't both constants, return 0. */
10869 if (TREE_CODE (lower_bound) != INTEGER_CST)
10870 return 0;
10871 if (TREE_CODE (offset) != INTEGER_CST)
10872 return 0;
10873 /* Adjust offset by the lower bound. */
10874 offset = size_diffop (fold_convert (sizetype, offset),
10875 fold_convert (sizetype, lower_bound));
10878 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10880 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10881 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10882 if (TREE_CODE (array) != ADDR_EXPR)
10883 return 0;
10884 array = TREE_OPERAND (array, 0);
10885 if (TREE_CODE (array) != STRING_CST
10886 && TREE_CODE (array) != VAR_DECL)
10887 return 0;
10889 else
10890 return 0;
10892 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10894 tree arg0 = TREE_OPERAND (arg, 0);
10895 tree arg1 = TREE_OPERAND (arg, 1);
10897 STRIP_NOPS (arg0);
10898 STRIP_NOPS (arg1);
10900 if (TREE_CODE (arg0) == ADDR_EXPR
10901 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10902 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10904 array = TREE_OPERAND (arg0, 0);
10905 offset = arg1;
10907 else if (TREE_CODE (arg1) == ADDR_EXPR
10908 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10909 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10911 array = TREE_OPERAND (arg1, 0);
10912 offset = arg0;
10914 else
10915 return 0;
10917 else
10918 return 0;
10920 if (TREE_CODE (array) == STRING_CST)
10922 *ptr_offset = fold_convert (sizetype, offset);
10923 return array;
10925 else if (TREE_CODE (array) == VAR_DECL
10926 || TREE_CODE (array) == CONST_DECL)
10928 int length;
10929 tree init = ctor_for_folding (array);
10931 /* Variables initialized to string literals can be handled too. */
10932 if (init == error_mark_node
10933 || !init
10934 || TREE_CODE (init) != STRING_CST)
10935 return 0;
10937 /* Avoid const char foo[4] = "abcde"; */
10938 if (DECL_SIZE_UNIT (array) == NULL_TREE
10939 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10940 || (length = TREE_STRING_LENGTH (init)) <= 0
10941 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10942 return 0;
10944 /* If variable is bigger than the string literal, OFFSET must be constant
10945 and inside of the bounds of the string literal. */
10946 offset = fold_convert (sizetype, offset);
10947 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10948 && (! tree_fits_uhwi_p (offset)
10949 || compare_tree_int (offset, length) >= 0))
10950 return 0;
10952 *ptr_offset = offset;
10953 return init;
10956 return 0;
10959 /* Generate code to calculate OPS, and exploded expression
10960 using a store-flag instruction and return an rtx for the result.
10961 OPS reflects a comparison.
10963 If TARGET is nonzero, store the result there if convenient.
10965 Return zero if there is no suitable set-flag instruction
10966 available on this machine.
10968 Once expand_expr has been called on the arguments of the comparison,
10969 we are committed to doing the store flag, since it is not safe to
10970 re-evaluate the expression. We emit the store-flag insn by calling
10971 emit_store_flag, but only expand the arguments if we have a reason
10972 to believe that emit_store_flag will be successful. If we think that
10973 it will, but it isn't, we have to simulate the store-flag with a
10974 set/jump/set sequence. */
10976 static rtx
10977 do_store_flag (sepops ops, rtx target, machine_mode mode)
10979 enum rtx_code code;
10980 tree arg0, arg1, type;
10981 tree tem;
10982 machine_mode operand_mode;
10983 int unsignedp;
10984 rtx op0, op1;
10985 rtx subtarget = target;
10986 location_t loc = ops->location;
10988 arg0 = ops->op0;
10989 arg1 = ops->op1;
10991 /* Don't crash if the comparison was erroneous. */
10992 if (arg0 == error_mark_node || arg1 == error_mark_node)
10993 return const0_rtx;
10995 type = TREE_TYPE (arg0);
10996 operand_mode = TYPE_MODE (type);
10997 unsignedp = TYPE_UNSIGNED (type);
10999 /* We won't bother with BLKmode store-flag operations because it would mean
11000 passing a lot of information to emit_store_flag. */
11001 if (operand_mode == BLKmode)
11002 return 0;
11004 /* We won't bother with store-flag operations involving function pointers
11005 when function pointers must be canonicalized before comparisons. */
11006 #ifdef HAVE_canonicalize_funcptr_for_compare
11007 if (HAVE_canonicalize_funcptr_for_compare
11008 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11009 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11010 == FUNCTION_TYPE))
11011 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11012 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11013 == FUNCTION_TYPE))))
11014 return 0;
11015 #endif
11017 STRIP_NOPS (arg0);
11018 STRIP_NOPS (arg1);
11020 /* For vector typed comparisons emit code to generate the desired
11021 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11022 expander for this. */
11023 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11025 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11026 tree if_true = constant_boolean_node (true, ops->type);
11027 tree if_false = constant_boolean_node (false, ops->type);
11028 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
11031 /* Get the rtx comparison code to use. We know that EXP is a comparison
11032 operation of some type. Some comparisons against 1 and -1 can be
11033 converted to comparisons with zero. Do so here so that the tests
11034 below will be aware that we have a comparison with zero. These
11035 tests will not catch constants in the first operand, but constants
11036 are rarely passed as the first operand. */
11038 switch (ops->code)
11040 case EQ_EXPR:
11041 code = EQ;
11042 break;
11043 case NE_EXPR:
11044 code = NE;
11045 break;
11046 case LT_EXPR:
11047 if (integer_onep (arg1))
11048 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11049 else
11050 code = unsignedp ? LTU : LT;
11051 break;
11052 case LE_EXPR:
11053 if (! unsignedp && integer_all_onesp (arg1))
11054 arg1 = integer_zero_node, code = LT;
11055 else
11056 code = unsignedp ? LEU : LE;
11057 break;
11058 case GT_EXPR:
11059 if (! unsignedp && integer_all_onesp (arg1))
11060 arg1 = integer_zero_node, code = GE;
11061 else
11062 code = unsignedp ? GTU : GT;
11063 break;
11064 case GE_EXPR:
11065 if (integer_onep (arg1))
11066 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11067 else
11068 code = unsignedp ? GEU : GE;
11069 break;
11071 case UNORDERED_EXPR:
11072 code = UNORDERED;
11073 break;
11074 case ORDERED_EXPR:
11075 code = ORDERED;
11076 break;
11077 case UNLT_EXPR:
11078 code = UNLT;
11079 break;
11080 case UNLE_EXPR:
11081 code = UNLE;
11082 break;
11083 case UNGT_EXPR:
11084 code = UNGT;
11085 break;
11086 case UNGE_EXPR:
11087 code = UNGE;
11088 break;
11089 case UNEQ_EXPR:
11090 code = UNEQ;
11091 break;
11092 case LTGT_EXPR:
11093 code = LTGT;
11094 break;
11096 default:
11097 gcc_unreachable ();
11100 /* Put a constant second. */
11101 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11102 || TREE_CODE (arg0) == FIXED_CST)
11104 tem = arg0; arg0 = arg1; arg1 = tem;
11105 code = swap_condition (code);
11108 /* If this is an equality or inequality test of a single bit, we can
11109 do this by shifting the bit being tested to the low-order bit and
11110 masking the result with the constant 1. If the condition was EQ,
11111 we xor it with 1. This does not require an scc insn and is faster
11112 than an scc insn even if we have it.
11114 The code to make this transformation was moved into fold_single_bit_test,
11115 so we just call into the folder and expand its result. */
11117 if ((code == NE || code == EQ)
11118 && integer_zerop (arg1)
11119 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11121 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11122 if (srcstmt
11123 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11125 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11126 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11127 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11128 gimple_assign_rhs1 (srcstmt),
11129 gimple_assign_rhs2 (srcstmt));
11130 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11131 if (temp)
11132 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11136 if (! get_subtarget (target)
11137 || GET_MODE (subtarget) != operand_mode)
11138 subtarget = 0;
11140 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11142 if (target == 0)
11143 target = gen_reg_rtx (mode);
11145 /* Try a cstore if possible. */
11146 return emit_store_flag_force (target, code, op0, op1,
11147 operand_mode, unsignedp,
11148 (TYPE_PRECISION (ops->type) == 1
11149 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11153 /* Stubs in case we haven't got a casesi insn. */
11154 #ifndef HAVE_casesi
11155 # define HAVE_casesi 0
11156 # define gen_casesi(a, b, c, d, e) (0)
11157 # define CODE_FOR_casesi CODE_FOR_nothing
11158 #endif
11160 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11161 0 otherwise (i.e. if there is no casesi instruction).
11163 DEFAULT_PROBABILITY is the probability of jumping to the default
11164 label. */
11166 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11167 rtx table_label, rtx default_label, rtx fallback_label,
11168 int default_probability)
11170 struct expand_operand ops[5];
11171 machine_mode index_mode = SImode;
11172 rtx op1, op2, index;
11174 if (! HAVE_casesi)
11175 return 0;
11177 /* Convert the index to SImode. */
11178 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11180 machine_mode omode = TYPE_MODE (index_type);
11181 rtx rangertx = expand_normal (range);
11183 /* We must handle the endpoints in the original mode. */
11184 index_expr = build2 (MINUS_EXPR, index_type,
11185 index_expr, minval);
11186 minval = integer_zero_node;
11187 index = expand_normal (index_expr);
11188 if (default_label)
11189 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11190 omode, 1, default_label,
11191 default_probability);
11192 /* Now we can safely truncate. */
11193 index = convert_to_mode (index_mode, index, 0);
11195 else
11197 if (TYPE_MODE (index_type) != index_mode)
11199 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11200 index_expr = fold_convert (index_type, index_expr);
11203 index = expand_normal (index_expr);
11206 do_pending_stack_adjust ();
11208 op1 = expand_normal (minval);
11209 op2 = expand_normal (range);
11211 create_input_operand (&ops[0], index, index_mode);
11212 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11213 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11214 create_fixed_operand (&ops[3], table_label);
11215 create_fixed_operand (&ops[4], (default_label
11216 ? default_label
11217 : fallback_label));
11218 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11219 return 1;
11222 /* Attempt to generate a tablejump instruction; same concept. */
11223 #ifndef HAVE_tablejump
11224 #define HAVE_tablejump 0
11225 #define gen_tablejump(x, y) (0)
11226 #endif
11228 /* Subroutine of the next function.
11230 INDEX is the value being switched on, with the lowest value
11231 in the table already subtracted.
11232 MODE is its expected mode (needed if INDEX is constant).
11233 RANGE is the length of the jump table.
11234 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11236 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11237 index value is out of range.
11238 DEFAULT_PROBABILITY is the probability of jumping to
11239 the default label. */
11241 static void
11242 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11243 rtx default_label, int default_probability)
11245 rtx temp, vector;
11247 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11248 cfun->cfg->max_jumptable_ents = INTVAL (range);
11250 /* Do an unsigned comparison (in the proper mode) between the index
11251 expression and the value which represents the length of the range.
11252 Since we just finished subtracting the lower bound of the range
11253 from the index expression, this comparison allows us to simultaneously
11254 check that the original index expression value is both greater than
11255 or equal to the minimum value of the range and less than or equal to
11256 the maximum value of the range. */
11258 if (default_label)
11259 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11260 default_label, default_probability);
11263 /* If index is in range, it must fit in Pmode.
11264 Convert to Pmode so we can index with it. */
11265 if (mode != Pmode)
11266 index = convert_to_mode (Pmode, index, 1);
11268 /* Don't let a MEM slip through, because then INDEX that comes
11269 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11270 and break_out_memory_refs will go to work on it and mess it up. */
11271 #ifdef PIC_CASE_VECTOR_ADDRESS
11272 if (flag_pic && !REG_P (index))
11273 index = copy_to_mode_reg (Pmode, index);
11274 #endif
11276 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11277 GET_MODE_SIZE, because this indicates how large insns are. The other
11278 uses should all be Pmode, because they are addresses. This code
11279 could fail if addresses and insns are not the same size. */
11280 index = simplify_gen_binary (MULT, Pmode, index,
11281 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11282 Pmode));
11283 index = simplify_gen_binary (PLUS, Pmode, index,
11284 gen_rtx_LABEL_REF (Pmode, table_label));
11286 #ifdef PIC_CASE_VECTOR_ADDRESS
11287 if (flag_pic)
11288 index = PIC_CASE_VECTOR_ADDRESS (index);
11289 else
11290 #endif
11291 index = memory_address (CASE_VECTOR_MODE, index);
11292 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11293 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11294 convert_move (temp, vector, 0);
11296 emit_jump_insn (gen_tablejump (temp, table_label));
11298 /* If we are generating PIC code or if the table is PC-relative, the
11299 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11300 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11301 emit_barrier ();
11305 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11306 rtx table_label, rtx default_label, int default_probability)
11308 rtx index;
11310 if (! HAVE_tablejump)
11311 return 0;
11313 index_expr = fold_build2 (MINUS_EXPR, index_type,
11314 fold_convert (index_type, index_expr),
11315 fold_convert (index_type, minval));
11316 index = expand_normal (index_expr);
11317 do_pending_stack_adjust ();
11319 do_tablejump (index, TYPE_MODE (index_type),
11320 convert_modes (TYPE_MODE (index_type),
11321 TYPE_MODE (TREE_TYPE (range)),
11322 expand_normal (range),
11323 TYPE_UNSIGNED (TREE_TYPE (range))),
11324 table_label, default_label, default_probability);
11325 return 1;
11328 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11329 static rtx
11330 const_vector_from_tree (tree exp)
11332 rtvec v;
11333 unsigned i;
11334 int units;
11335 tree elt;
11336 machine_mode inner, mode;
11338 mode = TYPE_MODE (TREE_TYPE (exp));
11340 if (initializer_zerop (exp))
11341 return CONST0_RTX (mode);
11343 units = GET_MODE_NUNITS (mode);
11344 inner = GET_MODE_INNER (mode);
11346 v = rtvec_alloc (units);
11348 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11350 elt = VECTOR_CST_ELT (exp, i);
11352 if (TREE_CODE (elt) == REAL_CST)
11353 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11354 inner);
11355 else if (TREE_CODE (elt) == FIXED_CST)
11356 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11357 inner);
11358 else
11359 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11362 return gen_rtx_CONST_VECTOR (mode, v);
11365 /* Build a decl for a personality function given a language prefix. */
11367 tree
11368 build_personality_function (const char *lang)
11370 const char *unwind_and_version;
11371 tree decl, type;
11372 char *name;
11374 switch (targetm_common.except_unwind_info (&global_options))
11376 case UI_NONE:
11377 return NULL;
11378 case UI_SJLJ:
11379 unwind_and_version = "_sj0";
11380 break;
11381 case UI_DWARF2:
11382 case UI_TARGET:
11383 unwind_and_version = "_v0";
11384 break;
11385 case UI_SEH:
11386 unwind_and_version = "_seh0";
11387 break;
11388 default:
11389 gcc_unreachable ();
11392 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11394 type = build_function_type_list (integer_type_node, integer_type_node,
11395 long_long_unsigned_type_node,
11396 ptr_type_node, ptr_type_node, NULL_TREE);
11397 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11398 get_identifier (name), type);
11399 DECL_ARTIFICIAL (decl) = 1;
11400 DECL_EXTERNAL (decl) = 1;
11401 TREE_PUBLIC (decl) = 1;
11403 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11404 are the flags assigned by targetm.encode_section_info. */
11405 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11407 return decl;
11410 /* Extracts the personality function of DECL and returns the corresponding
11411 libfunc. */
11414 get_personality_function (tree decl)
11416 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11417 enum eh_personality_kind pk;
11419 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11420 if (pk == eh_personality_none)
11421 return NULL;
11423 if (!personality
11424 && pk == eh_personality_any)
11425 personality = lang_hooks.eh_personality ();
11427 if (pk == eh_personality_lang)
11428 gcc_assert (personality != NULL_TREE);
11430 return XEXP (DECL_RTL (personality), 0);
11433 #include "gt-expr.h"