Define __void_t and SFINAE-friendly iterator_traits.
[official-gcc.git] / gcc / expr.c
blob0ef06ea98ae5d8d5a9fd1813d2a4c6f91ab11943
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"
85 #ifndef STACK_PUSH_CODE
86 #ifdef STACK_GROWS_DOWNWARD
87 #define STACK_PUSH_CODE PRE_DEC
88 #else
89 #define STACK_PUSH_CODE PRE_INC
90 #endif
91 #endif
94 /* If this is nonzero, we do not bother generating VOLATILE
95 around volatile memory references, and we are willing to
96 output indirect addresses. If cse is to follow, we reject
97 indirect addresses so a useful potential cse is generated;
98 if it is used only once, instruction combination will produce
99 the same indirect address eventually. */
100 int cse_not_expected;
102 /* This structure is used by move_by_pieces to describe the move to
103 be performed. */
104 struct move_by_pieces_d
106 rtx to;
107 rtx to_addr;
108 int autinc_to;
109 int explicit_inc_to;
110 rtx from;
111 rtx from_addr;
112 int autinc_from;
113 int explicit_inc_from;
114 unsigned HOST_WIDE_INT len;
115 HOST_WIDE_INT offset;
116 int reverse;
119 /* This structure is used by store_by_pieces to describe the clear to
120 be performed. */
122 struct store_by_pieces_d
124 rtx to;
125 rtx to_addr;
126 int autinc_to;
127 int explicit_inc_to;
128 unsigned HOST_WIDE_INT len;
129 HOST_WIDE_INT offset;
130 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
131 void *constfundata;
132 int reverse;
135 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
136 struct move_by_pieces_d *);
137 static bool block_move_libcall_safe_for_call_parm (void);
138 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
139 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
140 unsigned HOST_WIDE_INT);
141 static tree emit_block_move_libcall_fn (int);
142 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
143 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
144 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
145 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
146 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
147 struct store_by_pieces_d *);
148 static tree clear_storage_libcall_fn (int);
149 static rtx_insn *compress_float_constant (rtx, rtx);
150 static rtx get_subtarget (rtx);
151 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
152 HOST_WIDE_INT, machine_mode,
153 tree, int, alias_set_type);
154 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
155 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
156 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
157 machine_mode, tree, alias_set_type, bool);
159 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
161 static int is_aligning_offset (const_tree, const_tree);
162 static void expand_operands (tree, tree, rtx, rtx*, rtx*,
163 enum expand_modifier);
164 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
165 static rtx do_store_flag (sepops, rtx, machine_mode);
166 #ifdef PUSH_ROUNDING
167 static void emit_single_push_insn (machine_mode, rtx, tree);
168 #endif
169 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
170 static rtx const_vector_from_tree (tree);
171 static void write_complex_part (rtx, rtx, bool);
174 /* This is run to set up which modes can be used
175 directly in memory and to initialize the block move optab. It is run
176 at the beginning of compilation and when the target is reinitialized. */
178 void
179 init_expr_target (void)
181 rtx insn, pat;
182 machine_mode mode;
183 int num_clobbers;
184 rtx mem, mem1;
185 rtx reg;
187 /* Try indexing by frame ptr and try by stack ptr.
188 It is known that on the Convex the stack ptr isn't a valid index.
189 With luck, one or the other is valid on any machine. */
190 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
191 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
193 /* A scratch register we can modify in-place below to avoid
194 useless RTL allocations. */
195 reg = gen_rtx_REG (VOIDmode, -1);
197 insn = rtx_alloc (INSN);
198 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
199 PATTERN (insn) = pat;
201 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
202 mode = (machine_mode) ((int) mode + 1))
204 int regno;
206 direct_load[(int) mode] = direct_store[(int) mode] = 0;
207 PUT_MODE (mem, mode);
208 PUT_MODE (mem1, mode);
209 PUT_MODE (reg, mode);
211 /* See if there is some register that can be used in this mode and
212 directly loaded or stored from memory. */
214 if (mode != VOIDmode && mode != BLKmode)
215 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
216 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
217 regno++)
219 if (! HARD_REGNO_MODE_OK (regno, mode))
220 continue;
222 SET_REGNO (reg, regno);
224 SET_SRC (pat) = mem;
225 SET_DEST (pat) = reg;
226 if (recog (pat, insn, &num_clobbers) >= 0)
227 direct_load[(int) mode] = 1;
229 SET_SRC (pat) = mem1;
230 SET_DEST (pat) = reg;
231 if (recog (pat, insn, &num_clobbers) >= 0)
232 direct_load[(int) mode] = 1;
234 SET_SRC (pat) = reg;
235 SET_DEST (pat) = mem;
236 if (recog (pat, insn, &num_clobbers) >= 0)
237 direct_store[(int) mode] = 1;
239 SET_SRC (pat) = reg;
240 SET_DEST (pat) = mem1;
241 if (recog (pat, insn, &num_clobbers) >= 0)
242 direct_store[(int) mode] = 1;
246 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
248 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
249 mode = GET_MODE_WIDER_MODE (mode))
251 machine_mode srcmode;
252 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
253 srcmode = GET_MODE_WIDER_MODE (srcmode))
255 enum insn_code ic;
257 ic = can_extend_p (mode, srcmode, 0);
258 if (ic == CODE_FOR_nothing)
259 continue;
261 PUT_MODE (mem, srcmode);
263 if (insn_operand_matches (ic, 1, mem))
264 float_extend_from_mem[mode][srcmode] = true;
269 /* This is run at the start of compiling a function. */
271 void
272 init_expr (void)
274 memset (&crtl->expr, 0, sizeof (crtl->expr));
277 /* Copy data from FROM to TO, where the machine modes are not the same.
278 Both modes may be integer, or both may be floating, or both may be
279 fixed-point.
280 UNSIGNEDP should be nonzero if FROM is an unsigned type.
281 This causes zero-extension instead of sign-extension. */
283 void
284 convert_move (rtx to, rtx from, int unsignedp)
286 machine_mode to_mode = GET_MODE (to);
287 machine_mode from_mode = GET_MODE (from);
288 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
289 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
290 enum insn_code code;
291 rtx libcall;
293 /* rtx code for making an equivalent value. */
294 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
295 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
298 gcc_assert (to_real == from_real);
299 gcc_assert (to_mode != BLKmode);
300 gcc_assert (from_mode != BLKmode);
302 /* If the source and destination are already the same, then there's
303 nothing to do. */
304 if (to == from)
305 return;
307 /* If FROM is a SUBREG that indicates that we have already done at least
308 the required extension, strip it. We don't handle such SUBREGs as
309 TO here. */
311 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
312 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
313 >= GET_MODE_PRECISION (to_mode))
314 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
315 from = gen_lowpart (to_mode, from), from_mode = to_mode;
317 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
319 if (to_mode == from_mode
320 || (from_mode == VOIDmode && CONSTANT_P (from)))
322 emit_move_insn (to, from);
323 return;
326 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
328 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
330 if (VECTOR_MODE_P (to_mode))
331 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
332 else
333 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
335 emit_move_insn (to, from);
336 return;
339 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
341 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
342 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
343 return;
346 if (to_real)
348 rtx value;
349 rtx_insn *insns;
350 convert_optab tab;
352 gcc_assert ((GET_MODE_PRECISION (from_mode)
353 != GET_MODE_PRECISION (to_mode))
354 || (DECIMAL_FLOAT_MODE_P (from_mode)
355 != DECIMAL_FLOAT_MODE_P (to_mode)));
357 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
358 /* Conversion between decimal float and binary float, same size. */
359 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
360 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
361 tab = sext_optab;
362 else
363 tab = trunc_optab;
365 /* Try converting directly if the insn is supported. */
367 code = convert_optab_handler (tab, to_mode, from_mode);
368 if (code != CODE_FOR_nothing)
370 emit_unop_insn (code, to, from,
371 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
372 return;
375 /* Otherwise use a libcall. */
376 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
378 /* Is this conversion implemented yet? */
379 gcc_assert (libcall);
381 start_sequence ();
382 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
383 1, from, from_mode);
384 insns = get_insns ();
385 end_sequence ();
386 emit_libcall_block (insns, to, value,
387 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
388 from)
389 : gen_rtx_FLOAT_EXTEND (to_mode, from));
390 return;
393 /* Handle pointer conversion. */ /* SPEE 900220. */
394 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
396 convert_optab ctab;
398 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
399 ctab = trunc_optab;
400 else if (unsignedp)
401 ctab = zext_optab;
402 else
403 ctab = sext_optab;
405 if (convert_optab_handler (ctab, to_mode, from_mode)
406 != CODE_FOR_nothing)
408 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
409 to, from, UNKNOWN);
410 return;
414 /* Targets are expected to provide conversion insns between PxImode and
415 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
416 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
418 machine_mode full_mode
419 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
421 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
422 != CODE_FOR_nothing);
424 if (full_mode != from_mode)
425 from = convert_to_mode (full_mode, from, unsignedp);
426 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
427 to, from, UNKNOWN);
428 return;
430 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
432 rtx new_from;
433 machine_mode full_mode
434 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
435 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
436 enum insn_code icode;
438 icode = convert_optab_handler (ctab, full_mode, from_mode);
439 gcc_assert (icode != CODE_FOR_nothing);
441 if (to_mode == full_mode)
443 emit_unop_insn (icode, to, from, UNKNOWN);
444 return;
447 new_from = gen_reg_rtx (full_mode);
448 emit_unop_insn (icode, new_from, from, UNKNOWN);
450 /* else proceed to integer conversions below. */
451 from_mode = full_mode;
452 from = new_from;
455 /* Make sure both are fixed-point modes or both are not. */
456 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
457 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
458 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
460 /* If we widen from_mode to to_mode and they are in the same class,
461 we won't saturate the result.
462 Otherwise, always saturate the result to play safe. */
463 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
464 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
465 expand_fixed_convert (to, from, 0, 0);
466 else
467 expand_fixed_convert (to, from, 0, 1);
468 return;
471 /* Now both modes are integers. */
473 /* Handle expanding beyond a word. */
474 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
475 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
477 rtx_insn *insns;
478 rtx lowpart;
479 rtx fill_value;
480 rtx lowfrom;
481 int i;
482 machine_mode lowpart_mode;
483 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
485 /* Try converting directly if the insn is supported. */
486 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
487 != CODE_FOR_nothing)
489 /* If FROM is a SUBREG, put it into a register. Do this
490 so that we always generate the same set of insns for
491 better cse'ing; if an intermediate assignment occurred,
492 we won't be doing the operation directly on the SUBREG. */
493 if (optimize > 0 && GET_CODE (from) == SUBREG)
494 from = force_reg (from_mode, from);
495 emit_unop_insn (code, to, from, equiv_code);
496 return;
498 /* Next, try converting via full word. */
499 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
500 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
501 != CODE_FOR_nothing))
503 rtx word_to = gen_reg_rtx (word_mode);
504 if (REG_P (to))
506 if (reg_overlap_mentioned_p (to, from))
507 from = force_reg (from_mode, from);
508 emit_clobber (to);
510 convert_move (word_to, from, unsignedp);
511 emit_unop_insn (code, to, word_to, equiv_code);
512 return;
515 /* No special multiword conversion insn; do it by hand. */
516 start_sequence ();
518 /* Since we will turn this into a no conflict block, we must ensure the
519 the source does not overlap the target so force it into an isolated
520 register when maybe so. Likewise for any MEM input, since the
521 conversion sequence might require several references to it and we
522 must ensure we're getting the same value every time. */
524 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
525 from = force_reg (from_mode, from);
527 /* Get a copy of FROM widened to a word, if necessary. */
528 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
529 lowpart_mode = word_mode;
530 else
531 lowpart_mode = from_mode;
533 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
535 lowpart = gen_lowpart (lowpart_mode, to);
536 emit_move_insn (lowpart, lowfrom);
538 /* Compute the value to put in each remaining word. */
539 if (unsignedp)
540 fill_value = const0_rtx;
541 else
542 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
543 LT, lowfrom, const0_rtx,
544 lowpart_mode, 0, -1);
546 /* Fill the remaining words. */
547 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
549 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
550 rtx subword = operand_subword (to, index, 1, to_mode);
552 gcc_assert (subword);
554 if (fill_value != subword)
555 emit_move_insn (subword, fill_value);
558 insns = get_insns ();
559 end_sequence ();
561 emit_insn (insns);
562 return;
565 /* Truncating multi-word to a word or less. */
566 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
567 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
569 if (!((MEM_P (from)
570 && ! MEM_VOLATILE_P (from)
571 && direct_load[(int) to_mode]
572 && ! mode_dependent_address_p (XEXP (from, 0),
573 MEM_ADDR_SPACE (from)))
574 || REG_P (from)
575 || GET_CODE (from) == SUBREG))
576 from = force_reg (from_mode, from);
577 convert_move (to, gen_lowpart (word_mode, from), 0);
578 return;
581 /* Now follow all the conversions between integers
582 no more than a word long. */
584 /* For truncation, usually we can just refer to FROM in a narrower mode. */
585 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
586 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
588 if (!((MEM_P (from)
589 && ! MEM_VOLATILE_P (from)
590 && direct_load[(int) to_mode]
591 && ! mode_dependent_address_p (XEXP (from, 0),
592 MEM_ADDR_SPACE (from)))
593 || REG_P (from)
594 || GET_CODE (from) == SUBREG))
595 from = force_reg (from_mode, from);
596 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
597 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
598 from = copy_to_reg (from);
599 emit_move_insn (to, gen_lowpart (to_mode, from));
600 return;
603 /* Handle extension. */
604 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
606 /* Convert directly if that works. */
607 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
608 != CODE_FOR_nothing)
610 emit_unop_insn (code, to, from, equiv_code);
611 return;
613 else
615 machine_mode intermediate;
616 rtx tmp;
617 int shift_amount;
619 /* Search for a mode to convert via. */
620 for (intermediate = from_mode; intermediate != VOIDmode;
621 intermediate = GET_MODE_WIDER_MODE (intermediate))
622 if (((can_extend_p (to_mode, intermediate, unsignedp)
623 != CODE_FOR_nothing)
624 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
625 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
626 && (can_extend_p (intermediate, from_mode, unsignedp)
627 != CODE_FOR_nothing))
629 convert_move (to, convert_to_mode (intermediate, from,
630 unsignedp), unsignedp);
631 return;
634 /* No suitable intermediate mode.
635 Generate what we need with shifts. */
636 shift_amount = (GET_MODE_PRECISION (to_mode)
637 - GET_MODE_PRECISION (from_mode));
638 from = gen_lowpart (to_mode, force_reg (from_mode, from));
639 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
640 to, unsignedp);
641 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
642 to, unsignedp);
643 if (tmp != to)
644 emit_move_insn (to, tmp);
645 return;
649 /* Support special truncate insns for certain modes. */
650 if (convert_optab_handler (trunc_optab, to_mode,
651 from_mode) != CODE_FOR_nothing)
653 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
654 to, from, UNKNOWN);
655 return;
658 /* Handle truncation of volatile memrefs, and so on;
659 the things that couldn't be truncated directly,
660 and for which there was no special instruction.
662 ??? Code above formerly short-circuited this, for most integer
663 mode pairs, with a force_reg in from_mode followed by a recursive
664 call to this routine. Appears always to have been wrong. */
665 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
667 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
668 emit_move_insn (to, temp);
669 return;
672 /* Mode combination is not recognized. */
673 gcc_unreachable ();
676 /* Return an rtx for a value that would result
677 from converting X to mode MODE.
678 Both X and MODE may be floating, or both integer.
679 UNSIGNEDP is nonzero if X is an unsigned value.
680 This can be done by referring to a part of X in place
681 or by copying to a new temporary with conversion. */
684 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
686 return convert_modes (mode, VOIDmode, x, unsignedp);
689 /* Return an rtx for a value that would result
690 from converting X from mode OLDMODE to mode MODE.
691 Both modes may be floating, or both integer.
692 UNSIGNEDP is nonzero if X is an unsigned value.
694 This can be done by referring to a part of X in place
695 or by copying to a new temporary with conversion.
697 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
700 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
702 rtx temp;
704 /* If FROM is a SUBREG that indicates that we have already done at least
705 the required extension, strip it. */
707 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
708 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
709 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
710 x = gen_lowpart (mode, SUBREG_REG (x));
712 if (GET_MODE (x) != VOIDmode)
713 oldmode = GET_MODE (x);
715 if (mode == oldmode)
716 return x;
718 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
720 /* If the caller did not tell us the old mode, then there is not
721 much to do with respect to canonicalization. We have to
722 assume that all the bits are significant. */
723 if (GET_MODE_CLASS (oldmode) != MODE_INT)
724 oldmode = MAX_MODE_INT;
725 wide_int w = wide_int::from (std::make_pair (x, oldmode),
726 GET_MODE_PRECISION (mode),
727 unsignedp ? UNSIGNED : SIGNED);
728 return immed_wide_int_const (w, mode);
731 /* We can do this with a gen_lowpart if both desired and current modes
732 are integer, and this is either a constant integer, a register, or a
733 non-volatile MEM. */
734 if (GET_MODE_CLASS (mode) == MODE_INT
735 && GET_MODE_CLASS (oldmode) == MODE_INT
736 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
737 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
738 || (REG_P (x)
739 && (!HARD_REGISTER_P (x)
740 || HARD_REGNO_MODE_OK (REGNO (x), mode))
741 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
743 return gen_lowpart (mode, x);
745 /* Converting from integer constant into mode is always equivalent to an
746 subreg operation. */
747 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
749 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
750 return simplify_gen_subreg (mode, x, oldmode, 0);
753 temp = gen_reg_rtx (mode);
754 convert_move (temp, x, unsignedp);
755 return temp;
758 /* Return the largest alignment we can use for doing a move (or store)
759 of MAX_PIECES. ALIGN is the largest alignment we could use. */
761 static unsigned int
762 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
764 machine_mode tmode;
766 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
767 if (align >= GET_MODE_ALIGNMENT (tmode))
768 align = GET_MODE_ALIGNMENT (tmode);
769 else
771 machine_mode tmode, xmode;
773 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
774 tmode != VOIDmode;
775 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
776 if (GET_MODE_SIZE (tmode) > max_pieces
777 || SLOW_UNALIGNED_ACCESS (tmode, align))
778 break;
780 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
783 return align;
786 /* Return the widest integer mode no wider than SIZE. If no such mode
787 can be found, return VOIDmode. */
789 static machine_mode
790 widest_int_mode_for_size (unsigned int size)
792 machine_mode tmode, mode = VOIDmode;
794 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
795 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
796 if (GET_MODE_SIZE (tmode) < size)
797 mode = tmode;
799 return mode;
802 /* Determine whether the LEN bytes can be moved by using several move
803 instructions. Return nonzero if a call to move_by_pieces should
804 succeed. */
807 can_move_by_pieces (unsigned HOST_WIDE_INT len,
808 unsigned int align)
810 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
811 optimize_insn_for_speed_p ());
814 /* Generate several move instructions to copy LEN bytes from block FROM to
815 block TO. (These are MEM rtx's with BLKmode).
817 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
818 used to push FROM to the stack.
820 ALIGN is maximum stack alignment we can assume.
822 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
823 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
824 stpcpy. */
827 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
828 unsigned int align, int endp)
830 struct move_by_pieces_d data;
831 machine_mode to_addr_mode;
832 machine_mode from_addr_mode = get_address_mode (from);
833 rtx to_addr, from_addr = XEXP (from, 0);
834 unsigned int max_size = MOVE_MAX_PIECES + 1;
835 enum insn_code icode;
837 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
839 data.offset = 0;
840 data.from_addr = from_addr;
841 if (to)
843 to_addr_mode = get_address_mode (to);
844 to_addr = XEXP (to, 0);
845 data.to = to;
846 data.autinc_to
847 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
848 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
849 data.reverse
850 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
852 else
854 to_addr_mode = VOIDmode;
855 to_addr = NULL_RTX;
856 data.to = NULL_RTX;
857 data.autinc_to = 1;
858 #ifdef STACK_GROWS_DOWNWARD
859 data.reverse = 1;
860 #else
861 data.reverse = 0;
862 #endif
864 data.to_addr = to_addr;
865 data.from = from;
866 data.autinc_from
867 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
868 || GET_CODE (from_addr) == POST_INC
869 || GET_CODE (from_addr) == POST_DEC);
871 data.explicit_inc_from = 0;
872 data.explicit_inc_to = 0;
873 if (data.reverse) data.offset = len;
874 data.len = len;
876 /* If copying requires more than two move insns,
877 copy addresses to registers (to make displacements shorter)
878 and use post-increment if available. */
879 if (!(data.autinc_from && data.autinc_to)
880 && move_by_pieces_ninsns (len, align, max_size) > 2)
882 /* Find the mode of the largest move...
883 MODE might not be used depending on the definitions of the
884 USE_* macros below. */
885 machine_mode mode ATTRIBUTE_UNUSED
886 = widest_int_mode_for_size (max_size);
888 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
890 data.from_addr = copy_to_mode_reg (from_addr_mode,
891 plus_constant (from_addr_mode,
892 from_addr, len));
893 data.autinc_from = 1;
894 data.explicit_inc_from = -1;
896 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
898 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
899 data.autinc_from = 1;
900 data.explicit_inc_from = 1;
902 if (!data.autinc_from && CONSTANT_P (from_addr))
903 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
904 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
906 data.to_addr = copy_to_mode_reg (to_addr_mode,
907 plus_constant (to_addr_mode,
908 to_addr, len));
909 data.autinc_to = 1;
910 data.explicit_inc_to = -1;
912 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
914 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
915 data.autinc_to = 1;
916 data.explicit_inc_to = 1;
918 if (!data.autinc_to && CONSTANT_P (to_addr))
919 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
922 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
924 /* First move what we can in the largest integer mode, then go to
925 successively smaller modes. */
927 while (max_size > 1 && data.len > 0)
929 machine_mode mode = widest_int_mode_for_size (max_size);
931 if (mode == VOIDmode)
932 break;
934 icode = optab_handler (mov_optab, mode);
935 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
936 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
938 max_size = GET_MODE_SIZE (mode);
941 /* The code above should have handled everything. */
942 gcc_assert (!data.len);
944 if (endp)
946 rtx to1;
948 gcc_assert (!data.reverse);
949 if (data.autinc_to)
951 if (endp == 2)
953 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
954 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
955 else
956 data.to_addr = copy_to_mode_reg (to_addr_mode,
957 plus_constant (to_addr_mode,
958 data.to_addr,
959 -1));
961 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
962 data.offset);
964 else
966 if (endp == 2)
967 --data.offset;
968 to1 = adjust_address (data.to, QImode, data.offset);
970 return to1;
972 else
973 return data.to;
976 /* Return number of insns required to move L bytes by pieces.
977 ALIGN (in bits) is maximum alignment we can assume. */
979 unsigned HOST_WIDE_INT
980 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
981 unsigned int max_size)
983 unsigned HOST_WIDE_INT n_insns = 0;
985 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
987 while (max_size > 1 && l > 0)
989 machine_mode mode;
990 enum insn_code icode;
992 mode = widest_int_mode_for_size (max_size);
994 if (mode == VOIDmode)
995 break;
997 icode = optab_handler (mov_optab, mode);
998 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
999 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1001 max_size = GET_MODE_SIZE (mode);
1004 gcc_assert (!l);
1005 return n_insns;
1008 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1009 with move instructions for mode MODE. GENFUN is the gen_... function
1010 to make a move insn for that mode. DATA has all the other info. */
1012 static void
1013 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1014 struct move_by_pieces_d *data)
1016 unsigned int size = GET_MODE_SIZE (mode);
1017 rtx to1 = NULL_RTX, from1;
1019 while (data->len >= size)
1021 if (data->reverse)
1022 data->offset -= size;
1024 if (data->to)
1026 if (data->autinc_to)
1027 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1028 data->offset);
1029 else
1030 to1 = adjust_address (data->to, mode, data->offset);
1033 if (data->autinc_from)
1034 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1035 data->offset);
1036 else
1037 from1 = adjust_address (data->from, mode, data->offset);
1039 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1040 emit_insn (gen_add2_insn (data->to_addr,
1041 gen_int_mode (-(HOST_WIDE_INT) size,
1042 GET_MODE (data->to_addr))));
1043 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1044 emit_insn (gen_add2_insn (data->from_addr,
1045 gen_int_mode (-(HOST_WIDE_INT) size,
1046 GET_MODE (data->from_addr))));
1048 if (data->to)
1049 emit_insn ((*genfun) (to1, from1));
1050 else
1052 #ifdef PUSH_ROUNDING
1053 emit_single_push_insn (mode, from1, NULL);
1054 #else
1055 gcc_unreachable ();
1056 #endif
1059 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1060 emit_insn (gen_add2_insn (data->to_addr,
1061 gen_int_mode (size,
1062 GET_MODE (data->to_addr))));
1063 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1064 emit_insn (gen_add2_insn (data->from_addr,
1065 gen_int_mode (size,
1066 GET_MODE (data->from_addr))));
1068 if (! data->reverse)
1069 data->offset += size;
1071 data->len -= size;
1075 /* Emit code to move a block Y to a block X. This may be done with
1076 string-move instructions, with multiple scalar move instructions,
1077 or with a library call.
1079 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1080 SIZE is an rtx that says how long they are.
1081 ALIGN is the maximum alignment we can assume they have.
1082 METHOD describes what kind of copy this is, and what mechanisms may be used.
1083 MIN_SIZE is the minimal size of block to move
1084 MAX_SIZE is the maximal size of block to move, if it can not be represented
1085 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1087 Return the address of the new block, if memcpy is called and returns it,
1088 0 otherwise. */
1091 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1092 unsigned int expected_align, HOST_WIDE_INT expected_size,
1093 unsigned HOST_WIDE_INT min_size,
1094 unsigned HOST_WIDE_INT max_size,
1095 unsigned HOST_WIDE_INT probable_max_size)
1097 bool may_use_call;
1098 rtx retval = 0;
1099 unsigned int align;
1101 gcc_assert (size);
1102 if (CONST_INT_P (size)
1103 && INTVAL (size) == 0)
1104 return 0;
1106 switch (method)
1108 case BLOCK_OP_NORMAL:
1109 case BLOCK_OP_TAILCALL:
1110 may_use_call = true;
1111 break;
1113 case BLOCK_OP_CALL_PARM:
1114 may_use_call = block_move_libcall_safe_for_call_parm ();
1116 /* Make inhibit_defer_pop nonzero around the library call
1117 to force it to pop the arguments right away. */
1118 NO_DEFER_POP;
1119 break;
1121 case BLOCK_OP_NO_LIBCALL:
1122 may_use_call = false;
1123 break;
1125 default:
1126 gcc_unreachable ();
1129 gcc_assert (MEM_P (x) && MEM_P (y));
1130 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1131 gcc_assert (align >= BITS_PER_UNIT);
1133 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1134 block copy is more efficient for other large modes, e.g. DCmode. */
1135 x = adjust_address (x, BLKmode, 0);
1136 y = adjust_address (y, BLKmode, 0);
1138 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1139 can be incorrect is coming from __builtin_memcpy. */
1140 if (CONST_INT_P (size))
1142 x = shallow_copy_rtx (x);
1143 y = shallow_copy_rtx (y);
1144 set_mem_size (x, INTVAL (size));
1145 set_mem_size (y, INTVAL (size));
1148 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1149 move_by_pieces (x, y, INTVAL (size), align, 0);
1150 else if (emit_block_move_via_movmem (x, y, size, align,
1151 expected_align, expected_size,
1152 min_size, max_size, probable_max_size))
1154 else if (may_use_call
1155 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1156 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1158 /* Since x and y are passed to a libcall, mark the corresponding
1159 tree EXPR as addressable. */
1160 tree y_expr = MEM_EXPR (y);
1161 tree x_expr = MEM_EXPR (x);
1162 if (y_expr)
1163 mark_addressable (y_expr);
1164 if (x_expr)
1165 mark_addressable (x_expr);
1166 retval = emit_block_move_via_libcall (x, y, size,
1167 method == BLOCK_OP_TAILCALL);
1170 else
1171 emit_block_move_via_loop (x, y, size, align);
1173 if (method == BLOCK_OP_CALL_PARM)
1174 OK_DEFER_POP;
1176 return retval;
1180 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1182 unsigned HOST_WIDE_INT max, min = 0;
1183 if (GET_CODE (size) == CONST_INT)
1184 min = max = UINTVAL (size);
1185 else
1186 max = GET_MODE_MASK (GET_MODE (size));
1187 return emit_block_move_hints (x, y, size, method, 0, -1,
1188 min, max, max);
1191 /* A subroutine of emit_block_move. Returns true if calling the
1192 block move libcall will not clobber any parameters which may have
1193 already been placed on the stack. */
1195 static bool
1196 block_move_libcall_safe_for_call_parm (void)
1198 #if defined (REG_PARM_STACK_SPACE)
1199 tree fn;
1200 #endif
1202 /* If arguments are pushed on the stack, then they're safe. */
1203 if (PUSH_ARGS)
1204 return true;
1206 /* If registers go on the stack anyway, any argument is sure to clobber
1207 an outgoing argument. */
1208 #if defined (REG_PARM_STACK_SPACE)
1209 fn = emit_block_move_libcall_fn (false);
1210 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1211 depend on its argument. */
1212 (void) fn;
1213 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1214 && REG_PARM_STACK_SPACE (fn) != 0)
1215 return false;
1216 #endif
1218 /* If any argument goes in memory, then it might clobber an outgoing
1219 argument. */
1221 CUMULATIVE_ARGS args_so_far_v;
1222 cumulative_args_t args_so_far;
1223 tree fn, arg;
1225 fn = emit_block_move_libcall_fn (false);
1226 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1227 args_so_far = pack_cumulative_args (&args_so_far_v);
1229 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1230 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1232 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1233 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1234 NULL_TREE, true);
1235 if (!tmp || !REG_P (tmp))
1236 return false;
1237 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1238 return false;
1239 targetm.calls.function_arg_advance (args_so_far, mode,
1240 NULL_TREE, true);
1243 return true;
1246 /* A subroutine of emit_block_move. Expand a movmem pattern;
1247 return true if successful. */
1249 static bool
1250 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1251 unsigned int expected_align, HOST_WIDE_INT expected_size,
1252 unsigned HOST_WIDE_INT min_size,
1253 unsigned HOST_WIDE_INT max_size,
1254 unsigned HOST_WIDE_INT probable_max_size)
1256 int save_volatile_ok = volatile_ok;
1257 machine_mode mode;
1259 if (expected_align < align)
1260 expected_align = align;
1261 if (expected_size != -1)
1263 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1264 expected_size = probable_max_size;
1265 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1266 expected_size = min_size;
1269 /* Since this is a move insn, we don't care about volatility. */
1270 volatile_ok = 1;
1272 /* Try the most limited insn first, because there's no point
1273 including more than one in the machine description unless
1274 the more limited one has some advantage. */
1276 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1277 mode = GET_MODE_WIDER_MODE (mode))
1279 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1281 if (code != CODE_FOR_nothing
1282 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1283 here because if SIZE is less than the mode mask, as it is
1284 returned by the macro, it will definitely be less than the
1285 actual mode mask. Since SIZE is within the Pmode address
1286 space, we limit MODE to Pmode. */
1287 && ((CONST_INT_P (size)
1288 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1289 <= (GET_MODE_MASK (mode) >> 1)))
1290 || max_size <= (GET_MODE_MASK (mode) >> 1)
1291 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1293 struct expand_operand ops[9];
1294 unsigned int nops;
1296 /* ??? When called via emit_block_move_for_call, it'd be
1297 nice if there were some way to inform the backend, so
1298 that it doesn't fail the expansion because it thinks
1299 emitting the libcall would be more efficient. */
1300 nops = insn_data[(int) code].n_generator_args;
1301 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1303 create_fixed_operand (&ops[0], x);
1304 create_fixed_operand (&ops[1], y);
1305 /* The check above guarantees that this size conversion is valid. */
1306 create_convert_operand_to (&ops[2], size, mode, true);
1307 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1308 if (nops >= 6)
1310 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1311 create_integer_operand (&ops[5], expected_size);
1313 if (nops >= 8)
1315 create_integer_operand (&ops[6], min_size);
1316 /* If we can not represent the maximal size,
1317 make parameter NULL. */
1318 if ((HOST_WIDE_INT) max_size != -1)
1319 create_integer_operand (&ops[7], max_size);
1320 else
1321 create_fixed_operand (&ops[7], NULL);
1323 if (nops == 9)
1325 /* If we can not represent the maximal size,
1326 make parameter NULL. */
1327 if ((HOST_WIDE_INT) probable_max_size != -1)
1328 create_integer_operand (&ops[8], probable_max_size);
1329 else
1330 create_fixed_operand (&ops[8], NULL);
1332 if (maybe_expand_insn (code, nops, ops))
1334 volatile_ok = save_volatile_ok;
1335 return true;
1340 volatile_ok = save_volatile_ok;
1341 return false;
1344 /* A subroutine of emit_block_move. Expand a call to memcpy.
1345 Return the return value from memcpy, 0 otherwise. */
1348 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1350 rtx dst_addr, src_addr;
1351 tree call_expr, fn, src_tree, dst_tree, size_tree;
1352 machine_mode size_mode;
1353 rtx retval;
1355 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1356 pseudos. We can then place those new pseudos into a VAR_DECL and
1357 use them later. */
1359 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1360 src_addr = copy_addr_to_reg (XEXP (src, 0));
1362 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1363 src_addr = convert_memory_address (ptr_mode, src_addr);
1365 dst_tree = make_tree (ptr_type_node, dst_addr);
1366 src_tree = make_tree (ptr_type_node, src_addr);
1368 size_mode = TYPE_MODE (sizetype);
1370 size = convert_to_mode (size_mode, size, 1);
1371 size = copy_to_mode_reg (size_mode, size);
1373 /* It is incorrect to use the libcall calling conventions to call
1374 memcpy in this context. This could be a user call to memcpy and
1375 the user may wish to examine the return value from memcpy. For
1376 targets where libcalls and normal calls have different conventions
1377 for returning pointers, we could end up generating incorrect code. */
1379 size_tree = make_tree (sizetype, size);
1381 fn = emit_block_move_libcall_fn (true);
1382 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1383 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1385 retval = expand_normal (call_expr);
1387 return retval;
1390 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1391 for the function we use for block copies. */
1393 static GTY(()) tree block_move_fn;
1395 void
1396 init_block_move_fn (const char *asmspec)
1398 if (!block_move_fn)
1400 tree args, fn, attrs, attr_args;
1402 fn = get_identifier ("memcpy");
1403 args = build_function_type_list (ptr_type_node, ptr_type_node,
1404 const_ptr_type_node, sizetype,
1405 NULL_TREE);
1407 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1408 DECL_EXTERNAL (fn) = 1;
1409 TREE_PUBLIC (fn) = 1;
1410 DECL_ARTIFICIAL (fn) = 1;
1411 TREE_NOTHROW (fn) = 1;
1412 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1413 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1415 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1416 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1418 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1420 block_move_fn = fn;
1423 if (asmspec)
1424 set_user_assembler_name (block_move_fn, asmspec);
1427 static tree
1428 emit_block_move_libcall_fn (int for_call)
1430 static bool emitted_extern;
1432 if (!block_move_fn)
1433 init_block_move_fn (NULL);
1435 if (for_call && !emitted_extern)
1437 emitted_extern = true;
1438 make_decl_rtl (block_move_fn);
1441 return block_move_fn;
1444 /* A subroutine of emit_block_move. Copy the data via an explicit
1445 loop. This is used only when libcalls are forbidden. */
1446 /* ??? It'd be nice to copy in hunks larger than QImode. */
1448 static void
1449 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1450 unsigned int align ATTRIBUTE_UNUSED)
1452 rtx_code_label *cmp_label, *top_label;
1453 rtx iter, x_addr, y_addr, tmp;
1454 machine_mode x_addr_mode = get_address_mode (x);
1455 machine_mode y_addr_mode = get_address_mode (y);
1456 machine_mode iter_mode;
1458 iter_mode = GET_MODE (size);
1459 if (iter_mode == VOIDmode)
1460 iter_mode = word_mode;
1462 top_label = gen_label_rtx ();
1463 cmp_label = gen_label_rtx ();
1464 iter = gen_reg_rtx (iter_mode);
1466 emit_move_insn (iter, const0_rtx);
1468 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1469 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1470 do_pending_stack_adjust ();
1472 emit_jump (cmp_label);
1473 emit_label (top_label);
1475 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1476 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1478 if (x_addr_mode != y_addr_mode)
1479 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1480 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1482 x = change_address (x, QImode, x_addr);
1483 y = change_address (y, QImode, y_addr);
1485 emit_move_insn (x, y);
1487 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1488 true, OPTAB_LIB_WIDEN);
1489 if (tmp != iter)
1490 emit_move_insn (iter, tmp);
1492 emit_label (cmp_label);
1494 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1495 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1498 /* Copy all or part of a value X into registers starting at REGNO.
1499 The number of registers to be filled is NREGS. */
1501 void
1502 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1504 int i;
1505 #ifdef HAVE_load_multiple
1506 rtx pat;
1507 rtx_insn *last;
1508 #endif
1510 if (nregs == 0)
1511 return;
1513 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1514 x = validize_mem (force_const_mem (mode, x));
1516 /* See if the machine can do this with a load multiple insn. */
1517 #ifdef HAVE_load_multiple
1518 if (HAVE_load_multiple)
1520 last = get_last_insn ();
1521 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1522 GEN_INT (nregs));
1523 if (pat)
1525 emit_insn (pat);
1526 return;
1528 else
1529 delete_insns_since (last);
1531 #endif
1533 for (i = 0; i < nregs; i++)
1534 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1535 operand_subword_force (x, i, mode));
1538 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1539 The number of registers to be filled is NREGS. */
1541 void
1542 move_block_from_reg (int regno, rtx x, int nregs)
1544 int i;
1546 if (nregs == 0)
1547 return;
1549 /* See if the machine can do this with a store multiple insn. */
1550 #ifdef HAVE_store_multiple
1551 if (HAVE_store_multiple)
1553 rtx_insn *last = get_last_insn ();
1554 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1555 GEN_INT (nregs));
1556 if (pat)
1558 emit_insn (pat);
1559 return;
1561 else
1562 delete_insns_since (last);
1564 #endif
1566 for (i = 0; i < nregs; i++)
1568 rtx tem = operand_subword (x, i, 1, BLKmode);
1570 gcc_assert (tem);
1572 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1576 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1577 ORIG, where ORIG is a non-consecutive group of registers represented by
1578 a PARALLEL. The clone is identical to the original except in that the
1579 original set of registers is replaced by a new set of pseudo registers.
1580 The new set has the same modes as the original set. */
1583 gen_group_rtx (rtx orig)
1585 int i, length;
1586 rtx *tmps;
1588 gcc_assert (GET_CODE (orig) == PARALLEL);
1590 length = XVECLEN (orig, 0);
1591 tmps = XALLOCAVEC (rtx, length);
1593 /* Skip a NULL entry in first slot. */
1594 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1596 if (i)
1597 tmps[0] = 0;
1599 for (; i < length; i++)
1601 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1602 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1604 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1607 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1610 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1611 except that values are placed in TMPS[i], and must later be moved
1612 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1614 static void
1615 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1617 rtx src;
1618 int start, i;
1619 machine_mode m = GET_MODE (orig_src);
1621 gcc_assert (GET_CODE (dst) == PARALLEL);
1623 if (m != VOIDmode
1624 && !SCALAR_INT_MODE_P (m)
1625 && !MEM_P (orig_src)
1626 && GET_CODE (orig_src) != CONCAT)
1628 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1629 if (imode == BLKmode)
1630 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1631 else
1632 src = gen_reg_rtx (imode);
1633 if (imode != BLKmode)
1634 src = gen_lowpart (GET_MODE (orig_src), src);
1635 emit_move_insn (src, orig_src);
1636 /* ...and back again. */
1637 if (imode != BLKmode)
1638 src = gen_lowpart (imode, src);
1639 emit_group_load_1 (tmps, dst, src, type, ssize);
1640 return;
1643 /* Check for a NULL entry, used to indicate that the parameter goes
1644 both on the stack and in registers. */
1645 if (XEXP (XVECEXP (dst, 0, 0), 0))
1646 start = 0;
1647 else
1648 start = 1;
1650 /* Process the pieces. */
1651 for (i = start; i < XVECLEN (dst, 0); i++)
1653 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1654 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1655 unsigned int bytelen = GET_MODE_SIZE (mode);
1656 int shift = 0;
1658 /* Handle trailing fragments that run over the size of the struct. */
1659 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1661 /* Arrange to shift the fragment to where it belongs.
1662 extract_bit_field loads to the lsb of the reg. */
1663 if (
1664 #ifdef BLOCK_REG_PADDING
1665 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1666 == (BYTES_BIG_ENDIAN ? upward : downward)
1667 #else
1668 BYTES_BIG_ENDIAN
1669 #endif
1671 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1672 bytelen = ssize - bytepos;
1673 gcc_assert (bytelen > 0);
1676 /* If we won't be loading directly from memory, protect the real source
1677 from strange tricks we might play; but make sure that the source can
1678 be loaded directly into the destination. */
1679 src = orig_src;
1680 if (!MEM_P (orig_src)
1681 && (!CONSTANT_P (orig_src)
1682 || (GET_MODE (orig_src) != mode
1683 && GET_MODE (orig_src) != VOIDmode)))
1685 if (GET_MODE (orig_src) == VOIDmode)
1686 src = gen_reg_rtx (mode);
1687 else
1688 src = gen_reg_rtx (GET_MODE (orig_src));
1690 emit_move_insn (src, orig_src);
1693 /* Optimize the access just a bit. */
1694 if (MEM_P (src)
1695 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1696 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1697 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1698 && bytelen == GET_MODE_SIZE (mode))
1700 tmps[i] = gen_reg_rtx (mode);
1701 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1703 else if (COMPLEX_MODE_P (mode)
1704 && GET_MODE (src) == mode
1705 && bytelen == GET_MODE_SIZE (mode))
1706 /* Let emit_move_complex do the bulk of the work. */
1707 tmps[i] = src;
1708 else if (GET_CODE (src) == CONCAT)
1710 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1711 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1713 if ((bytepos == 0 && bytelen == slen0)
1714 || (bytepos != 0 && bytepos + bytelen <= slen))
1716 /* The following assumes that the concatenated objects all
1717 have the same size. In this case, a simple calculation
1718 can be used to determine the object and the bit field
1719 to be extracted. */
1720 tmps[i] = XEXP (src, bytepos / slen0);
1721 if (! CONSTANT_P (tmps[i])
1722 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1723 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1724 (bytepos % slen0) * BITS_PER_UNIT,
1725 1, NULL_RTX, mode, mode);
1727 else
1729 rtx mem;
1731 gcc_assert (!bytepos);
1732 mem = assign_stack_temp (GET_MODE (src), slen);
1733 emit_move_insn (mem, src);
1734 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1735 0, 1, NULL_RTX, mode, mode);
1738 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1739 SIMD register, which is currently broken. While we get GCC
1740 to emit proper RTL for these cases, let's dump to memory. */
1741 else if (VECTOR_MODE_P (GET_MODE (dst))
1742 && REG_P (src))
1744 int slen = GET_MODE_SIZE (GET_MODE (src));
1745 rtx mem;
1747 mem = assign_stack_temp (GET_MODE (src), slen);
1748 emit_move_insn (mem, src);
1749 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1751 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1752 && XVECLEN (dst, 0) > 1)
1753 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1754 else if (CONSTANT_P (src))
1756 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1758 if (len == ssize)
1759 tmps[i] = src;
1760 else
1762 rtx first, second;
1764 /* TODO: const_wide_int can have sizes other than this... */
1765 gcc_assert (2 * len == ssize);
1766 split_double (src, &first, &second);
1767 if (i)
1768 tmps[i] = second;
1769 else
1770 tmps[i] = first;
1773 else if (REG_P (src) && GET_MODE (src) == mode)
1774 tmps[i] = src;
1775 else
1776 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1777 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1778 mode, mode);
1780 if (shift)
1781 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1782 shift, tmps[i], 0);
1786 /* Emit code to move a block SRC of type TYPE to a block DST,
1787 where DST is non-consecutive registers represented by a PARALLEL.
1788 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1789 if not known. */
1791 void
1792 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1794 rtx *tmps;
1795 int i;
1797 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1798 emit_group_load_1 (tmps, dst, src, type, ssize);
1800 /* Copy the extracted pieces into the proper (probable) hard regs. */
1801 for (i = 0; i < XVECLEN (dst, 0); i++)
1803 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1804 if (d == NULL)
1805 continue;
1806 emit_move_insn (d, tmps[i]);
1810 /* Similar, but load SRC into new pseudos in a format that looks like
1811 PARALLEL. This can later be fed to emit_group_move to get things
1812 in the right place. */
1815 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1817 rtvec vec;
1818 int i;
1820 vec = rtvec_alloc (XVECLEN (parallel, 0));
1821 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1823 /* Convert the vector to look just like the original PARALLEL, except
1824 with the computed values. */
1825 for (i = 0; i < XVECLEN (parallel, 0); i++)
1827 rtx e = XVECEXP (parallel, 0, i);
1828 rtx d = XEXP (e, 0);
1830 if (d)
1832 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1833 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1835 RTVEC_ELT (vec, i) = e;
1838 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1841 /* Emit code to move a block SRC to block DST, where SRC and DST are
1842 non-consecutive groups of registers, each represented by a PARALLEL. */
1844 void
1845 emit_group_move (rtx dst, rtx src)
1847 int i;
1849 gcc_assert (GET_CODE (src) == PARALLEL
1850 && GET_CODE (dst) == PARALLEL
1851 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1853 /* Skip first entry if NULL. */
1854 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1855 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1856 XEXP (XVECEXP (src, 0, i), 0));
1859 /* Move a group of registers represented by a PARALLEL into pseudos. */
1862 emit_group_move_into_temps (rtx src)
1864 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1865 int i;
1867 for (i = 0; i < XVECLEN (src, 0); i++)
1869 rtx e = XVECEXP (src, 0, i);
1870 rtx d = XEXP (e, 0);
1872 if (d)
1873 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1874 RTVEC_ELT (vec, i) = e;
1877 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1880 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1881 where SRC is non-consecutive registers represented by a PARALLEL.
1882 SSIZE represents the total size of block ORIG_DST, or -1 if not
1883 known. */
1885 void
1886 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1888 rtx *tmps, dst;
1889 int start, finish, i;
1890 machine_mode m = GET_MODE (orig_dst);
1892 gcc_assert (GET_CODE (src) == PARALLEL);
1894 if (!SCALAR_INT_MODE_P (m)
1895 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1897 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1898 if (imode == BLKmode)
1899 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1900 else
1901 dst = gen_reg_rtx (imode);
1902 emit_group_store (dst, src, type, ssize);
1903 if (imode != BLKmode)
1904 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1905 emit_move_insn (orig_dst, dst);
1906 return;
1909 /* Check for a NULL entry, used to indicate that the parameter goes
1910 both on the stack and in registers. */
1911 if (XEXP (XVECEXP (src, 0, 0), 0))
1912 start = 0;
1913 else
1914 start = 1;
1915 finish = XVECLEN (src, 0);
1917 tmps = XALLOCAVEC (rtx, finish);
1919 /* Copy the (probable) hard regs into pseudos. */
1920 for (i = start; i < finish; i++)
1922 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1923 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1925 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1926 emit_move_insn (tmps[i], reg);
1928 else
1929 tmps[i] = reg;
1932 /* If we won't be storing directly into memory, protect the real destination
1933 from strange tricks we might play. */
1934 dst = orig_dst;
1935 if (GET_CODE (dst) == PARALLEL)
1937 rtx temp;
1939 /* We can get a PARALLEL dst if there is a conditional expression in
1940 a return statement. In that case, the dst and src are the same,
1941 so no action is necessary. */
1942 if (rtx_equal_p (dst, src))
1943 return;
1945 /* It is unclear if we can ever reach here, but we may as well handle
1946 it. Allocate a temporary, and split this into a store/load to/from
1947 the temporary. */
1948 temp = assign_stack_temp (GET_MODE (dst), ssize);
1949 emit_group_store (temp, src, type, ssize);
1950 emit_group_load (dst, temp, type, ssize);
1951 return;
1953 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1955 machine_mode outer = GET_MODE (dst);
1956 machine_mode inner;
1957 HOST_WIDE_INT bytepos;
1958 bool done = false;
1959 rtx temp;
1961 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1962 dst = gen_reg_rtx (outer);
1964 /* Make life a bit easier for combine. */
1965 /* If the first element of the vector is the low part
1966 of the destination mode, use a paradoxical subreg to
1967 initialize the destination. */
1968 if (start < finish)
1970 inner = GET_MODE (tmps[start]);
1971 bytepos = subreg_lowpart_offset (inner, outer);
1972 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1974 temp = simplify_gen_subreg (outer, tmps[start],
1975 inner, 0);
1976 if (temp)
1978 emit_move_insn (dst, temp);
1979 done = true;
1980 start++;
1985 /* If the first element wasn't the low part, try the last. */
1986 if (!done
1987 && start < finish - 1)
1989 inner = GET_MODE (tmps[finish - 1]);
1990 bytepos = subreg_lowpart_offset (inner, outer);
1991 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1993 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1994 inner, 0);
1995 if (temp)
1997 emit_move_insn (dst, temp);
1998 done = true;
1999 finish--;
2004 /* Otherwise, simply initialize the result to zero. */
2005 if (!done)
2006 emit_move_insn (dst, CONST0_RTX (outer));
2009 /* Process the pieces. */
2010 for (i = start; i < finish; i++)
2012 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2013 machine_mode mode = GET_MODE (tmps[i]);
2014 unsigned int bytelen = GET_MODE_SIZE (mode);
2015 unsigned int adj_bytelen;
2016 rtx dest = dst;
2018 /* Handle trailing fragments that run over the size of the struct. */
2019 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2020 adj_bytelen = ssize - bytepos;
2021 else
2022 adj_bytelen = bytelen;
2024 if (GET_CODE (dst) == CONCAT)
2026 if (bytepos + adj_bytelen
2027 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2028 dest = XEXP (dst, 0);
2029 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2031 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2032 dest = XEXP (dst, 1);
2034 else
2036 machine_mode dest_mode = GET_MODE (dest);
2037 machine_mode tmp_mode = GET_MODE (tmps[i]);
2039 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2041 if (GET_MODE_ALIGNMENT (dest_mode)
2042 >= GET_MODE_ALIGNMENT (tmp_mode))
2044 dest = assign_stack_temp (dest_mode,
2045 GET_MODE_SIZE (dest_mode));
2046 emit_move_insn (adjust_address (dest,
2047 tmp_mode,
2048 bytepos),
2049 tmps[i]);
2050 dst = dest;
2052 else
2054 dest = assign_stack_temp (tmp_mode,
2055 GET_MODE_SIZE (tmp_mode));
2056 emit_move_insn (dest, tmps[i]);
2057 dst = adjust_address (dest, dest_mode, bytepos);
2059 break;
2063 /* Handle trailing fragments that run over the size of the struct. */
2064 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2066 /* store_bit_field always takes its value from the lsb.
2067 Move the fragment to the lsb if it's not already there. */
2068 if (
2069 #ifdef BLOCK_REG_PADDING
2070 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2071 == (BYTES_BIG_ENDIAN ? upward : downward)
2072 #else
2073 BYTES_BIG_ENDIAN
2074 #endif
2077 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2078 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2079 shift, tmps[i], 0);
2082 /* Make sure not to write past the end of the struct. */
2083 store_bit_field (dest,
2084 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2085 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2086 VOIDmode, tmps[i]);
2089 /* Optimize the access just a bit. */
2090 else if (MEM_P (dest)
2091 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2092 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2093 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2094 && bytelen == GET_MODE_SIZE (mode))
2095 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2097 else
2098 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2099 0, 0, mode, tmps[i]);
2102 /* Copy from the pseudo into the (probable) hard reg. */
2103 if (orig_dst != dst)
2104 emit_move_insn (orig_dst, dst);
2107 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2108 of the value stored in X. */
2111 maybe_emit_group_store (rtx x, tree type)
2113 machine_mode mode = TYPE_MODE (type);
2114 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2115 if (GET_CODE (x) == PARALLEL)
2117 rtx result = gen_reg_rtx (mode);
2118 emit_group_store (result, x, type, int_size_in_bytes (type));
2119 return result;
2121 return x;
2124 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2126 This is used on targets that return BLKmode values in registers. */
2128 void
2129 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2131 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2132 rtx src = NULL, dst = NULL;
2133 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2134 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2135 machine_mode mode = GET_MODE (srcreg);
2136 machine_mode tmode = GET_MODE (target);
2137 machine_mode copy_mode;
2139 /* BLKmode registers created in the back-end shouldn't have survived. */
2140 gcc_assert (mode != BLKmode);
2142 /* If the structure doesn't take up a whole number of words, see whether
2143 SRCREG is padded on the left or on the right. If it's on the left,
2144 set PADDING_CORRECTION to the number of bits to skip.
2146 In most ABIs, the structure will be returned at the least end of
2147 the register, which translates to right padding on little-endian
2148 targets and left padding on big-endian targets. The opposite
2149 holds if the structure is returned at the most significant
2150 end of the register. */
2151 if (bytes % UNITS_PER_WORD != 0
2152 && (targetm.calls.return_in_msb (type)
2153 ? !BYTES_BIG_ENDIAN
2154 : BYTES_BIG_ENDIAN))
2155 padding_correction
2156 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2158 /* We can use a single move if we have an exact mode for the size. */
2159 else if (MEM_P (target)
2160 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2161 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2162 && bytes == GET_MODE_SIZE (mode))
2164 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2165 return;
2168 /* And if we additionally have the same mode for a register. */
2169 else if (REG_P (target)
2170 && GET_MODE (target) == mode
2171 && bytes == GET_MODE_SIZE (mode))
2173 emit_move_insn (target, srcreg);
2174 return;
2177 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2178 into a new pseudo which is a full word. */
2179 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2181 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2182 mode = word_mode;
2185 /* Copy the structure BITSIZE bits at a time. If the target lives in
2186 memory, take care of not reading/writing past its end by selecting
2187 a copy mode suited to BITSIZE. This should always be possible given
2188 how it is computed.
2190 If the target lives in register, make sure not to select a copy mode
2191 larger than the mode of the register.
2193 We could probably emit more efficient code for machines which do not use
2194 strict alignment, but it doesn't seem worth the effort at the current
2195 time. */
2197 copy_mode = word_mode;
2198 if (MEM_P (target))
2200 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2201 if (mem_mode != BLKmode)
2202 copy_mode = mem_mode;
2204 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2205 copy_mode = tmode;
2207 for (bitpos = 0, xbitpos = padding_correction;
2208 bitpos < bytes * BITS_PER_UNIT;
2209 bitpos += bitsize, xbitpos += bitsize)
2211 /* We need a new source operand each time xbitpos is on a
2212 word boundary and when xbitpos == padding_correction
2213 (the first time through). */
2214 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2215 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2217 /* We need a new destination operand each time bitpos is on
2218 a word boundary. */
2219 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2220 dst = target;
2221 else if (bitpos % BITS_PER_WORD == 0)
2222 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2224 /* Use xbitpos for the source extraction (right justified) and
2225 bitpos for the destination store (left justified). */
2226 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2227 extract_bit_field (src, bitsize,
2228 xbitpos % BITS_PER_WORD, 1,
2229 NULL_RTX, copy_mode, copy_mode));
2233 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2234 register if it contains any data, otherwise return null.
2236 This is used on targets that return BLKmode values in registers. */
2239 copy_blkmode_to_reg (machine_mode mode, tree src)
2241 int i, n_regs;
2242 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2243 unsigned int bitsize;
2244 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2245 machine_mode dst_mode;
2247 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2249 x = expand_normal (src);
2251 bytes = int_size_in_bytes (TREE_TYPE (src));
2252 if (bytes == 0)
2253 return NULL_RTX;
2255 /* If the structure doesn't take up a whole number of words, see
2256 whether the register value should be padded on the left or on
2257 the right. Set PADDING_CORRECTION to the number of padding
2258 bits needed on the left side.
2260 In most ABIs, the structure will be returned at the least end of
2261 the register, which translates to right padding on little-endian
2262 targets and left padding on big-endian targets. The opposite
2263 holds if the structure is returned at the most significant
2264 end of the register. */
2265 if (bytes % UNITS_PER_WORD != 0
2266 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2267 ? !BYTES_BIG_ENDIAN
2268 : BYTES_BIG_ENDIAN))
2269 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2270 * BITS_PER_UNIT));
2272 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2273 dst_words = XALLOCAVEC (rtx, n_regs);
2274 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2276 /* Copy the structure BITSIZE bits at a time. */
2277 for (bitpos = 0, xbitpos = padding_correction;
2278 bitpos < bytes * BITS_PER_UNIT;
2279 bitpos += bitsize, xbitpos += bitsize)
2281 /* We need a new destination pseudo each time xbitpos is
2282 on a word boundary and when xbitpos == padding_correction
2283 (the first time through). */
2284 if (xbitpos % BITS_PER_WORD == 0
2285 || xbitpos == padding_correction)
2287 /* Generate an appropriate register. */
2288 dst_word = gen_reg_rtx (word_mode);
2289 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2291 /* Clear the destination before we move anything into it. */
2292 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2295 /* We need a new source operand each time bitpos is on a word
2296 boundary. */
2297 if (bitpos % BITS_PER_WORD == 0)
2298 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2300 /* Use bitpos for the source extraction (left justified) and
2301 xbitpos for the destination store (right justified). */
2302 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2303 0, 0, word_mode,
2304 extract_bit_field (src_word, bitsize,
2305 bitpos % BITS_PER_WORD, 1,
2306 NULL_RTX, word_mode, word_mode));
2309 if (mode == BLKmode)
2311 /* Find the smallest integer mode large enough to hold the
2312 entire structure. */
2313 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2314 mode != VOIDmode;
2315 mode = GET_MODE_WIDER_MODE (mode))
2316 /* Have we found a large enough mode? */
2317 if (GET_MODE_SIZE (mode) >= bytes)
2318 break;
2320 /* A suitable mode should have been found. */
2321 gcc_assert (mode != VOIDmode);
2324 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2325 dst_mode = word_mode;
2326 else
2327 dst_mode = mode;
2328 dst = gen_reg_rtx (dst_mode);
2330 for (i = 0; i < n_regs; i++)
2331 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2333 if (mode != dst_mode)
2334 dst = gen_lowpart (mode, dst);
2336 return dst;
2339 /* Add a USE expression for REG to the (possibly empty) list pointed
2340 to by CALL_FUSAGE. REG must denote a hard register. */
2342 void
2343 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2345 gcc_assert (REG_P (reg));
2347 if (!HARD_REGISTER_P (reg))
2348 return;
2350 *call_fusage
2351 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2354 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2355 to by CALL_FUSAGE. REG must denote a hard register. */
2357 void
2358 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2360 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2362 *call_fusage
2363 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2366 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2367 starting at REGNO. All of these registers must be hard registers. */
2369 void
2370 use_regs (rtx *call_fusage, int regno, int nregs)
2372 int i;
2374 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2376 for (i = 0; i < nregs; i++)
2377 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2380 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2381 PARALLEL REGS. This is for calls that pass values in multiple
2382 non-contiguous locations. The Irix 6 ABI has examples of this. */
2384 void
2385 use_group_regs (rtx *call_fusage, rtx regs)
2387 int i;
2389 for (i = 0; i < XVECLEN (regs, 0); i++)
2391 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2393 /* A NULL entry means the parameter goes both on the stack and in
2394 registers. This can also be a MEM for targets that pass values
2395 partially on the stack and partially in registers. */
2396 if (reg != 0 && REG_P (reg))
2397 use_reg (call_fusage, reg);
2401 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2402 assigment and the code of the expresion on the RHS is CODE. Return
2403 NULL otherwise. */
2405 static gimple
2406 get_def_for_expr (tree name, enum tree_code code)
2408 gimple def_stmt;
2410 if (TREE_CODE (name) != SSA_NAME)
2411 return NULL;
2413 def_stmt = get_gimple_for_ssa_name (name);
2414 if (!def_stmt
2415 || gimple_assign_rhs_code (def_stmt) != code)
2416 return NULL;
2418 return def_stmt;
2421 #ifdef HAVE_conditional_move
2422 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2423 assigment and the class of the expresion on the RHS is CLASS. Return
2424 NULL otherwise. */
2426 static gimple
2427 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2429 gimple def_stmt;
2431 if (TREE_CODE (name) != SSA_NAME)
2432 return NULL;
2434 def_stmt = get_gimple_for_ssa_name (name);
2435 if (!def_stmt
2436 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2437 return NULL;
2439 return def_stmt;
2441 #endif
2444 /* Determine whether the LEN bytes generated by CONSTFUN can be
2445 stored to memory using several move instructions. CONSTFUNDATA is
2446 a pointer which will be passed as argument in every CONSTFUN call.
2447 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2448 a memset operation and false if it's a copy of a constant string.
2449 Return nonzero if a call to store_by_pieces should succeed. */
2452 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2453 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2454 void *constfundata, unsigned int align, bool memsetp)
2456 unsigned HOST_WIDE_INT l;
2457 unsigned int max_size;
2458 HOST_WIDE_INT offset = 0;
2459 machine_mode mode;
2460 enum insn_code icode;
2461 int reverse;
2462 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2463 rtx cst ATTRIBUTE_UNUSED;
2465 if (len == 0)
2466 return 1;
2468 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2469 memsetp
2470 ? SET_BY_PIECES
2471 : STORE_BY_PIECES,
2472 optimize_insn_for_speed_p ()))
2473 return 0;
2475 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2477 /* We would first store what we can in the largest integer mode, then go to
2478 successively smaller modes. */
2480 for (reverse = 0;
2481 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2482 reverse++)
2484 l = len;
2485 max_size = STORE_MAX_PIECES + 1;
2486 while (max_size > 1 && l > 0)
2488 mode = widest_int_mode_for_size (max_size);
2490 if (mode == VOIDmode)
2491 break;
2493 icode = optab_handler (mov_optab, mode);
2494 if (icode != CODE_FOR_nothing
2495 && align >= GET_MODE_ALIGNMENT (mode))
2497 unsigned int size = GET_MODE_SIZE (mode);
2499 while (l >= size)
2501 if (reverse)
2502 offset -= size;
2504 cst = (*constfun) (constfundata, offset, mode);
2505 if (!targetm.legitimate_constant_p (mode, cst))
2506 return 0;
2508 if (!reverse)
2509 offset += size;
2511 l -= size;
2515 max_size = GET_MODE_SIZE (mode);
2518 /* The code above should have handled everything. */
2519 gcc_assert (!l);
2522 return 1;
2525 /* Generate several move instructions to store LEN bytes generated by
2526 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2527 pointer which will be passed as argument in every CONSTFUN call.
2528 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2529 a memset operation and false if it's a copy of a constant string.
2530 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2531 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2532 stpcpy. */
2535 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2536 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2537 void *constfundata, unsigned int align, bool memsetp, int endp)
2539 machine_mode to_addr_mode = get_address_mode (to);
2540 struct store_by_pieces_d data;
2542 if (len == 0)
2544 gcc_assert (endp != 2);
2545 return to;
2548 gcc_assert (targetm.use_by_pieces_infrastructure_p
2549 (len, align,
2550 memsetp
2551 ? SET_BY_PIECES
2552 : STORE_BY_PIECES,
2553 optimize_insn_for_speed_p ()));
2555 data.constfun = constfun;
2556 data.constfundata = constfundata;
2557 data.len = len;
2558 data.to = to;
2559 store_by_pieces_1 (&data, align);
2560 if (endp)
2562 rtx to1;
2564 gcc_assert (!data.reverse);
2565 if (data.autinc_to)
2567 if (endp == 2)
2569 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2570 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2571 else
2572 data.to_addr = copy_to_mode_reg (to_addr_mode,
2573 plus_constant (to_addr_mode,
2574 data.to_addr,
2575 -1));
2577 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2578 data.offset);
2580 else
2582 if (endp == 2)
2583 --data.offset;
2584 to1 = adjust_address (data.to, QImode, data.offset);
2586 return to1;
2588 else
2589 return data.to;
2592 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2593 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2595 static void
2596 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2598 struct store_by_pieces_d data;
2600 if (len == 0)
2601 return;
2603 data.constfun = clear_by_pieces_1;
2604 data.constfundata = NULL;
2605 data.len = len;
2606 data.to = to;
2607 store_by_pieces_1 (&data, align);
2610 /* Callback routine for clear_by_pieces.
2611 Return const0_rtx unconditionally. */
2613 static rtx
2614 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2615 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2616 machine_mode mode ATTRIBUTE_UNUSED)
2618 return const0_rtx;
2621 /* Subroutine of clear_by_pieces and store_by_pieces.
2622 Generate several move instructions to store LEN bytes of block TO. (A MEM
2623 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2625 static void
2626 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2627 unsigned int align ATTRIBUTE_UNUSED)
2629 machine_mode to_addr_mode = get_address_mode (data->to);
2630 rtx to_addr = XEXP (data->to, 0);
2631 unsigned int max_size = STORE_MAX_PIECES + 1;
2632 enum insn_code icode;
2634 data->offset = 0;
2635 data->to_addr = to_addr;
2636 data->autinc_to
2637 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2638 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2640 data->explicit_inc_to = 0;
2641 data->reverse
2642 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2643 if (data->reverse)
2644 data->offset = data->len;
2646 /* If storing requires more than two move insns,
2647 copy addresses to registers (to make displacements shorter)
2648 and use post-increment if available. */
2649 if (!data->autinc_to
2650 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2652 /* Determine the main mode we'll be using.
2653 MODE might not be used depending on the definitions of the
2654 USE_* macros below. */
2655 machine_mode mode ATTRIBUTE_UNUSED
2656 = widest_int_mode_for_size (max_size);
2658 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2660 data->to_addr = copy_to_mode_reg (to_addr_mode,
2661 plus_constant (to_addr_mode,
2662 to_addr,
2663 data->len));
2664 data->autinc_to = 1;
2665 data->explicit_inc_to = -1;
2668 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2669 && ! data->autinc_to)
2671 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2672 data->autinc_to = 1;
2673 data->explicit_inc_to = 1;
2676 if ( !data->autinc_to && CONSTANT_P (to_addr))
2677 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2680 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2682 /* First store what we can in the largest integer mode, then go to
2683 successively smaller modes. */
2685 while (max_size > 1 && data->len > 0)
2687 machine_mode mode = widest_int_mode_for_size (max_size);
2689 if (mode == VOIDmode)
2690 break;
2692 icode = optab_handler (mov_optab, mode);
2693 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2694 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2696 max_size = GET_MODE_SIZE (mode);
2699 /* The code above should have handled everything. */
2700 gcc_assert (!data->len);
2703 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2704 with move instructions for mode MODE. GENFUN is the gen_... function
2705 to make a move insn for that mode. DATA has all the other info. */
2707 static void
2708 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2709 struct store_by_pieces_d *data)
2711 unsigned int size = GET_MODE_SIZE (mode);
2712 rtx to1, cst;
2714 while (data->len >= size)
2716 if (data->reverse)
2717 data->offset -= size;
2719 if (data->autinc_to)
2720 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2721 data->offset);
2722 else
2723 to1 = adjust_address (data->to, mode, data->offset);
2725 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2726 emit_insn (gen_add2_insn (data->to_addr,
2727 gen_int_mode (-(HOST_WIDE_INT) size,
2728 GET_MODE (data->to_addr))));
2730 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2731 emit_insn ((*genfun) (to1, cst));
2733 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2734 emit_insn (gen_add2_insn (data->to_addr,
2735 gen_int_mode (size,
2736 GET_MODE (data->to_addr))));
2738 if (! data->reverse)
2739 data->offset += size;
2741 data->len -= size;
2745 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2746 its length in bytes. */
2749 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2750 unsigned int expected_align, HOST_WIDE_INT expected_size,
2751 unsigned HOST_WIDE_INT min_size,
2752 unsigned HOST_WIDE_INT max_size,
2753 unsigned HOST_WIDE_INT probable_max_size)
2755 machine_mode mode = GET_MODE (object);
2756 unsigned int align;
2758 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2760 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2761 just move a zero. Otherwise, do this a piece at a time. */
2762 if (mode != BLKmode
2763 && CONST_INT_P (size)
2764 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2766 rtx zero = CONST0_RTX (mode);
2767 if (zero != NULL)
2769 emit_move_insn (object, zero);
2770 return NULL;
2773 if (COMPLEX_MODE_P (mode))
2775 zero = CONST0_RTX (GET_MODE_INNER (mode));
2776 if (zero != NULL)
2778 write_complex_part (object, zero, 0);
2779 write_complex_part (object, zero, 1);
2780 return NULL;
2785 if (size == const0_rtx)
2786 return NULL;
2788 align = MEM_ALIGN (object);
2790 if (CONST_INT_P (size)
2791 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2792 CLEAR_BY_PIECES,
2793 optimize_insn_for_speed_p ()))
2794 clear_by_pieces (object, INTVAL (size), align);
2795 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2796 expected_align, expected_size,
2797 min_size, max_size, probable_max_size))
2799 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2800 return set_storage_via_libcall (object, size, const0_rtx,
2801 method == BLOCK_OP_TAILCALL);
2802 else
2803 gcc_unreachable ();
2805 return NULL;
2809 clear_storage (rtx object, rtx size, enum block_op_methods method)
2811 unsigned HOST_WIDE_INT max, min = 0;
2812 if (GET_CODE (size) == CONST_INT)
2813 min = max = UINTVAL (size);
2814 else
2815 max = GET_MODE_MASK (GET_MODE (size));
2816 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2820 /* A subroutine of clear_storage. Expand a call to memset.
2821 Return the return value of memset, 0 otherwise. */
2824 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2826 tree call_expr, fn, object_tree, size_tree, val_tree;
2827 machine_mode size_mode;
2828 rtx retval;
2830 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2831 place those into new pseudos into a VAR_DECL and use them later. */
2833 object = copy_addr_to_reg (XEXP (object, 0));
2835 size_mode = TYPE_MODE (sizetype);
2836 size = convert_to_mode (size_mode, size, 1);
2837 size = copy_to_mode_reg (size_mode, size);
2839 /* It is incorrect to use the libcall calling conventions to call
2840 memset in this context. This could be a user call to memset and
2841 the user may wish to examine the return value from memset. For
2842 targets where libcalls and normal calls have different conventions
2843 for returning pointers, we could end up generating incorrect code. */
2845 object_tree = make_tree (ptr_type_node, object);
2846 if (!CONST_INT_P (val))
2847 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2848 size_tree = make_tree (sizetype, size);
2849 val_tree = make_tree (integer_type_node, val);
2851 fn = clear_storage_libcall_fn (true);
2852 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2853 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2855 retval = expand_normal (call_expr);
2857 return retval;
2860 /* A subroutine of set_storage_via_libcall. Create the tree node
2861 for the function we use for block clears. */
2863 tree block_clear_fn;
2865 void
2866 init_block_clear_fn (const char *asmspec)
2868 if (!block_clear_fn)
2870 tree fn, args;
2872 fn = get_identifier ("memset");
2873 args = build_function_type_list (ptr_type_node, ptr_type_node,
2874 integer_type_node, sizetype,
2875 NULL_TREE);
2877 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2878 DECL_EXTERNAL (fn) = 1;
2879 TREE_PUBLIC (fn) = 1;
2880 DECL_ARTIFICIAL (fn) = 1;
2881 TREE_NOTHROW (fn) = 1;
2882 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2883 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2885 block_clear_fn = fn;
2888 if (asmspec)
2889 set_user_assembler_name (block_clear_fn, asmspec);
2892 static tree
2893 clear_storage_libcall_fn (int for_call)
2895 static bool emitted_extern;
2897 if (!block_clear_fn)
2898 init_block_clear_fn (NULL);
2900 if (for_call && !emitted_extern)
2902 emitted_extern = true;
2903 make_decl_rtl (block_clear_fn);
2906 return block_clear_fn;
2909 /* Expand a setmem pattern; return true if successful. */
2911 bool
2912 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2913 unsigned int expected_align, HOST_WIDE_INT expected_size,
2914 unsigned HOST_WIDE_INT min_size,
2915 unsigned HOST_WIDE_INT max_size,
2916 unsigned HOST_WIDE_INT probable_max_size)
2918 /* Try the most limited insn first, because there's no point
2919 including more than one in the machine description unless
2920 the more limited one has some advantage. */
2922 machine_mode mode;
2924 if (expected_align < align)
2925 expected_align = align;
2926 if (expected_size != -1)
2928 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2929 expected_size = max_size;
2930 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2931 expected_size = min_size;
2934 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2935 mode = GET_MODE_WIDER_MODE (mode))
2937 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2939 if (code != CODE_FOR_nothing
2940 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2941 here because if SIZE is less than the mode mask, as it is
2942 returned by the macro, it will definitely be less than the
2943 actual mode mask. Since SIZE is within the Pmode address
2944 space, we limit MODE to Pmode. */
2945 && ((CONST_INT_P (size)
2946 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2947 <= (GET_MODE_MASK (mode) >> 1)))
2948 || max_size <= (GET_MODE_MASK (mode) >> 1)
2949 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2951 struct expand_operand ops[9];
2952 unsigned int nops;
2954 nops = insn_data[(int) code].n_generator_args;
2955 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2957 create_fixed_operand (&ops[0], object);
2958 /* The check above guarantees that this size conversion is valid. */
2959 create_convert_operand_to (&ops[1], size, mode, true);
2960 create_convert_operand_from (&ops[2], val, byte_mode, true);
2961 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2962 if (nops >= 6)
2964 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2965 create_integer_operand (&ops[5], expected_size);
2967 if (nops >= 8)
2969 create_integer_operand (&ops[6], min_size);
2970 /* If we can not represent the maximal size,
2971 make parameter NULL. */
2972 if ((HOST_WIDE_INT) max_size != -1)
2973 create_integer_operand (&ops[7], max_size);
2974 else
2975 create_fixed_operand (&ops[7], NULL);
2977 if (nops == 9)
2979 /* If we can not represent the maximal size,
2980 make parameter NULL. */
2981 if ((HOST_WIDE_INT) probable_max_size != -1)
2982 create_integer_operand (&ops[8], probable_max_size);
2983 else
2984 create_fixed_operand (&ops[8], NULL);
2986 if (maybe_expand_insn (code, nops, ops))
2987 return true;
2991 return false;
2995 /* Write to one of the components of the complex value CPLX. Write VAL to
2996 the real part if IMAG_P is false, and the imaginary part if its true. */
2998 static void
2999 write_complex_part (rtx cplx, rtx val, bool imag_p)
3001 machine_mode cmode;
3002 machine_mode imode;
3003 unsigned ibitsize;
3005 if (GET_CODE (cplx) == CONCAT)
3007 emit_move_insn (XEXP (cplx, imag_p), val);
3008 return;
3011 cmode = GET_MODE (cplx);
3012 imode = GET_MODE_INNER (cmode);
3013 ibitsize = GET_MODE_BITSIZE (imode);
3015 /* For MEMs simplify_gen_subreg may generate an invalid new address
3016 because, e.g., the original address is considered mode-dependent
3017 by the target, which restricts simplify_subreg from invoking
3018 adjust_address_nv. Instead of preparing fallback support for an
3019 invalid address, we call adjust_address_nv directly. */
3020 if (MEM_P (cplx))
3022 emit_move_insn (adjust_address_nv (cplx, imode,
3023 imag_p ? GET_MODE_SIZE (imode) : 0),
3024 val);
3025 return;
3028 /* If the sub-object is at least word sized, then we know that subregging
3029 will work. This special case is important, since store_bit_field
3030 wants to operate on integer modes, and there's rarely an OImode to
3031 correspond to TCmode. */
3032 if (ibitsize >= BITS_PER_WORD
3033 /* For hard regs we have exact predicates. Assume we can split
3034 the original object if it spans an even number of hard regs.
3035 This special case is important for SCmode on 64-bit platforms
3036 where the natural size of floating-point regs is 32-bit. */
3037 || (REG_P (cplx)
3038 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3039 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3041 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3042 imag_p ? GET_MODE_SIZE (imode) : 0);
3043 if (part)
3045 emit_move_insn (part, val);
3046 return;
3048 else
3049 /* simplify_gen_subreg may fail for sub-word MEMs. */
3050 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3053 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3056 /* Extract one of the components of the complex value CPLX. Extract the
3057 real part if IMAG_P is false, and the imaginary part if it's true. */
3059 static rtx
3060 read_complex_part (rtx cplx, bool imag_p)
3062 machine_mode cmode, imode;
3063 unsigned ibitsize;
3065 if (GET_CODE (cplx) == CONCAT)
3066 return XEXP (cplx, imag_p);
3068 cmode = GET_MODE (cplx);
3069 imode = GET_MODE_INNER (cmode);
3070 ibitsize = GET_MODE_BITSIZE (imode);
3072 /* Special case reads from complex constants that got spilled to memory. */
3073 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3075 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3076 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3078 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3079 if (CONSTANT_CLASS_P (part))
3080 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3084 /* For MEMs simplify_gen_subreg may generate an invalid new address
3085 because, e.g., the original address is considered mode-dependent
3086 by the target, which restricts simplify_subreg from invoking
3087 adjust_address_nv. Instead of preparing fallback support for an
3088 invalid address, we call adjust_address_nv directly. */
3089 if (MEM_P (cplx))
3090 return adjust_address_nv (cplx, imode,
3091 imag_p ? GET_MODE_SIZE (imode) : 0);
3093 /* If the sub-object is at least word sized, then we know that subregging
3094 will work. This special case is important, since extract_bit_field
3095 wants to operate on integer modes, and there's rarely an OImode to
3096 correspond to TCmode. */
3097 if (ibitsize >= BITS_PER_WORD
3098 /* For hard regs we have exact predicates. Assume we can split
3099 the original object if it spans an even number of hard regs.
3100 This special case is important for SCmode on 64-bit platforms
3101 where the natural size of floating-point regs is 32-bit. */
3102 || (REG_P (cplx)
3103 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3104 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3106 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3107 imag_p ? GET_MODE_SIZE (imode) : 0);
3108 if (ret)
3109 return ret;
3110 else
3111 /* simplify_gen_subreg may fail for sub-word MEMs. */
3112 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3115 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3116 true, NULL_RTX, imode, imode);
3119 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3120 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3121 represented in NEW_MODE. If FORCE is true, this will never happen, as
3122 we'll force-create a SUBREG if needed. */
3124 static rtx
3125 emit_move_change_mode (machine_mode new_mode,
3126 machine_mode old_mode, rtx x, bool force)
3128 rtx ret;
3130 if (push_operand (x, GET_MODE (x)))
3132 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3133 MEM_COPY_ATTRIBUTES (ret, x);
3135 else if (MEM_P (x))
3137 /* We don't have to worry about changing the address since the
3138 size in bytes is supposed to be the same. */
3139 if (reload_in_progress)
3141 /* Copy the MEM to change the mode and move any
3142 substitutions from the old MEM to the new one. */
3143 ret = adjust_address_nv (x, new_mode, 0);
3144 copy_replacements (x, ret);
3146 else
3147 ret = adjust_address (x, new_mode, 0);
3149 else
3151 /* Note that we do want simplify_subreg's behavior of validating
3152 that the new mode is ok for a hard register. If we were to use
3153 simplify_gen_subreg, we would create the subreg, but would
3154 probably run into the target not being able to implement it. */
3155 /* Except, of course, when FORCE is true, when this is exactly what
3156 we want. Which is needed for CCmodes on some targets. */
3157 if (force)
3158 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3159 else
3160 ret = simplify_subreg (new_mode, x, old_mode, 0);
3163 return ret;
3166 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3167 an integer mode of the same size as MODE. Returns the instruction
3168 emitted, or NULL if such a move could not be generated. */
3170 static rtx_insn *
3171 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3173 machine_mode imode;
3174 enum insn_code code;
3176 /* There must exist a mode of the exact size we require. */
3177 imode = int_mode_for_mode (mode);
3178 if (imode == BLKmode)
3179 return NULL;
3181 /* The target must support moves in this mode. */
3182 code = optab_handler (mov_optab, imode);
3183 if (code == CODE_FOR_nothing)
3184 return NULL;
3186 x = emit_move_change_mode (imode, mode, x, force);
3187 if (x == NULL_RTX)
3188 return NULL;
3189 y = emit_move_change_mode (imode, mode, y, force);
3190 if (y == NULL_RTX)
3191 return NULL;
3192 return emit_insn (GEN_FCN (code) (x, y));
3195 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3196 Return an equivalent MEM that does not use an auto-increment. */
3199 emit_move_resolve_push (machine_mode mode, rtx x)
3201 enum rtx_code code = GET_CODE (XEXP (x, 0));
3202 HOST_WIDE_INT adjust;
3203 rtx temp;
3205 adjust = GET_MODE_SIZE (mode);
3206 #ifdef PUSH_ROUNDING
3207 adjust = PUSH_ROUNDING (adjust);
3208 #endif
3209 if (code == PRE_DEC || code == POST_DEC)
3210 adjust = -adjust;
3211 else if (code == PRE_MODIFY || code == POST_MODIFY)
3213 rtx expr = XEXP (XEXP (x, 0), 1);
3214 HOST_WIDE_INT val;
3216 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3217 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3218 val = INTVAL (XEXP (expr, 1));
3219 if (GET_CODE (expr) == MINUS)
3220 val = -val;
3221 gcc_assert (adjust == val || adjust == -val);
3222 adjust = val;
3225 /* Do not use anti_adjust_stack, since we don't want to update
3226 stack_pointer_delta. */
3227 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3228 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3229 0, OPTAB_LIB_WIDEN);
3230 if (temp != stack_pointer_rtx)
3231 emit_move_insn (stack_pointer_rtx, temp);
3233 switch (code)
3235 case PRE_INC:
3236 case PRE_DEC:
3237 case PRE_MODIFY:
3238 temp = stack_pointer_rtx;
3239 break;
3240 case POST_INC:
3241 case POST_DEC:
3242 case POST_MODIFY:
3243 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3244 break;
3245 default:
3246 gcc_unreachable ();
3249 return replace_equiv_address (x, temp);
3252 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3253 X is known to satisfy push_operand, and MODE is known to be complex.
3254 Returns the last instruction emitted. */
3256 rtx_insn *
3257 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3259 machine_mode submode = GET_MODE_INNER (mode);
3260 bool imag_first;
3262 #ifdef PUSH_ROUNDING
3263 unsigned int submodesize = GET_MODE_SIZE (submode);
3265 /* In case we output to the stack, but the size is smaller than the
3266 machine can push exactly, we need to use move instructions. */
3267 if (PUSH_ROUNDING (submodesize) != submodesize)
3269 x = emit_move_resolve_push (mode, x);
3270 return emit_move_insn (x, y);
3272 #endif
3274 /* Note that the real part always precedes the imag part in memory
3275 regardless of machine's endianness. */
3276 switch (GET_CODE (XEXP (x, 0)))
3278 case PRE_DEC:
3279 case POST_DEC:
3280 imag_first = true;
3281 break;
3282 case PRE_INC:
3283 case POST_INC:
3284 imag_first = false;
3285 break;
3286 default:
3287 gcc_unreachable ();
3290 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3291 read_complex_part (y, imag_first));
3292 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3293 read_complex_part (y, !imag_first));
3296 /* A subroutine of emit_move_complex. Perform the move from Y to X
3297 via two moves of the parts. Returns the last instruction emitted. */
3299 rtx_insn *
3300 emit_move_complex_parts (rtx x, rtx y)
3302 /* Show the output dies here. This is necessary for SUBREGs
3303 of pseudos since we cannot track their lifetimes correctly;
3304 hard regs shouldn't appear here except as return values. */
3305 if (!reload_completed && !reload_in_progress
3306 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3307 emit_clobber (x);
3309 write_complex_part (x, read_complex_part (y, false), false);
3310 write_complex_part (x, read_complex_part (y, true), true);
3312 return get_last_insn ();
3315 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3316 MODE is known to be complex. Returns the last instruction emitted. */
3318 static rtx_insn *
3319 emit_move_complex (machine_mode mode, rtx x, rtx y)
3321 bool try_int;
3323 /* Need to take special care for pushes, to maintain proper ordering
3324 of the data, and possibly extra padding. */
3325 if (push_operand (x, mode))
3326 return emit_move_complex_push (mode, x, y);
3328 /* See if we can coerce the target into moving both values at once, except
3329 for floating point where we favor moving as parts if this is easy. */
3330 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3331 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3332 && !(REG_P (x)
3333 && HARD_REGISTER_P (x)
3334 && hard_regno_nregs[REGNO (x)][mode] == 1)
3335 && !(REG_P (y)
3336 && HARD_REGISTER_P (y)
3337 && hard_regno_nregs[REGNO (y)][mode] == 1))
3338 try_int = false;
3339 /* Not possible if the values are inherently not adjacent. */
3340 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3341 try_int = false;
3342 /* Is possible if both are registers (or subregs of registers). */
3343 else if (register_operand (x, mode) && register_operand (y, mode))
3344 try_int = true;
3345 /* If one of the operands is a memory, and alignment constraints
3346 are friendly enough, we may be able to do combined memory operations.
3347 We do not attempt this if Y is a constant because that combination is
3348 usually better with the by-parts thing below. */
3349 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3350 && (!STRICT_ALIGNMENT
3351 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3352 try_int = true;
3353 else
3354 try_int = false;
3356 if (try_int)
3358 rtx_insn *ret;
3360 /* For memory to memory moves, optimal behavior can be had with the
3361 existing block move logic. */
3362 if (MEM_P (x) && MEM_P (y))
3364 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3365 BLOCK_OP_NO_LIBCALL);
3366 return get_last_insn ();
3369 ret = emit_move_via_integer (mode, x, y, true);
3370 if (ret)
3371 return ret;
3374 return emit_move_complex_parts (x, y);
3377 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3378 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3380 static rtx_insn *
3381 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3383 rtx_insn *ret;
3385 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3386 if (mode != CCmode)
3388 enum insn_code code = optab_handler (mov_optab, CCmode);
3389 if (code != CODE_FOR_nothing)
3391 x = emit_move_change_mode (CCmode, mode, x, true);
3392 y = emit_move_change_mode (CCmode, mode, y, true);
3393 return emit_insn (GEN_FCN (code) (x, y));
3397 /* Otherwise, find the MODE_INT mode of the same width. */
3398 ret = emit_move_via_integer (mode, x, y, false);
3399 gcc_assert (ret != NULL);
3400 return ret;
3403 /* Return true if word I of OP lies entirely in the
3404 undefined bits of a paradoxical subreg. */
3406 static bool
3407 undefined_operand_subword_p (const_rtx op, int i)
3409 machine_mode innermode, innermostmode;
3410 int offset;
3411 if (GET_CODE (op) != SUBREG)
3412 return false;
3413 innermode = GET_MODE (op);
3414 innermostmode = GET_MODE (SUBREG_REG (op));
3415 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3416 /* The SUBREG_BYTE represents offset, as if the value were stored in
3417 memory, except for a paradoxical subreg where we define
3418 SUBREG_BYTE to be 0; undo this exception as in
3419 simplify_subreg. */
3420 if (SUBREG_BYTE (op) == 0
3421 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3423 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3424 if (WORDS_BIG_ENDIAN)
3425 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3426 if (BYTES_BIG_ENDIAN)
3427 offset += difference % UNITS_PER_WORD;
3429 if (offset >= GET_MODE_SIZE (innermostmode)
3430 || offset <= -GET_MODE_SIZE (word_mode))
3431 return true;
3432 return false;
3435 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3436 MODE is any multi-word or full-word mode that lacks a move_insn
3437 pattern. Note that you will get better code if you define such
3438 patterns, even if they must turn into multiple assembler instructions. */
3440 static rtx_insn *
3441 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3443 rtx_insn *last_insn = 0;
3444 rtx_insn *seq;
3445 rtx inner;
3446 bool need_clobber;
3447 int i;
3449 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3451 /* If X is a push on the stack, do the push now and replace
3452 X with a reference to the stack pointer. */
3453 if (push_operand (x, mode))
3454 x = emit_move_resolve_push (mode, x);
3456 /* If we are in reload, see if either operand is a MEM whose address
3457 is scheduled for replacement. */
3458 if (reload_in_progress && MEM_P (x)
3459 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3460 x = replace_equiv_address_nv (x, inner);
3461 if (reload_in_progress && MEM_P (y)
3462 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3463 y = replace_equiv_address_nv (y, inner);
3465 start_sequence ();
3467 need_clobber = false;
3468 for (i = 0;
3469 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3470 i++)
3472 rtx xpart = operand_subword (x, i, 1, mode);
3473 rtx ypart;
3475 /* Do not generate code for a move if it would come entirely
3476 from the undefined bits of a paradoxical subreg. */
3477 if (undefined_operand_subword_p (y, i))
3478 continue;
3480 ypart = operand_subword (y, i, 1, mode);
3482 /* If we can't get a part of Y, put Y into memory if it is a
3483 constant. Otherwise, force it into a register. Then we must
3484 be able to get a part of Y. */
3485 if (ypart == 0 && CONSTANT_P (y))
3487 y = use_anchored_address (force_const_mem (mode, y));
3488 ypart = operand_subword (y, i, 1, mode);
3490 else if (ypart == 0)
3491 ypart = operand_subword_force (y, i, mode);
3493 gcc_assert (xpart && ypart);
3495 need_clobber |= (GET_CODE (xpart) == SUBREG);
3497 last_insn = emit_move_insn (xpart, ypart);
3500 seq = get_insns ();
3501 end_sequence ();
3503 /* Show the output dies here. This is necessary for SUBREGs
3504 of pseudos since we cannot track their lifetimes correctly;
3505 hard regs shouldn't appear here except as return values.
3506 We never want to emit such a clobber after reload. */
3507 if (x != y
3508 && ! (reload_in_progress || reload_completed)
3509 && need_clobber != 0)
3510 emit_clobber (x);
3512 emit_insn (seq);
3514 return last_insn;
3517 /* Low level part of emit_move_insn.
3518 Called just like emit_move_insn, but assumes X and Y
3519 are basically valid. */
3521 rtx_insn *
3522 emit_move_insn_1 (rtx x, rtx y)
3524 machine_mode mode = GET_MODE (x);
3525 enum insn_code code;
3527 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3529 code = optab_handler (mov_optab, mode);
3530 if (code != CODE_FOR_nothing)
3531 return emit_insn (GEN_FCN (code) (x, y));
3533 /* Expand complex moves by moving real part and imag part. */
3534 if (COMPLEX_MODE_P (mode))
3535 return emit_move_complex (mode, x, y);
3537 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3538 || ALL_FIXED_POINT_MODE_P (mode))
3540 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3542 /* If we can't find an integer mode, use multi words. */
3543 if (result)
3544 return result;
3545 else
3546 return emit_move_multi_word (mode, x, y);
3549 if (GET_MODE_CLASS (mode) == MODE_CC)
3550 return emit_move_ccmode (mode, x, y);
3552 /* Try using a move pattern for the corresponding integer mode. This is
3553 only safe when simplify_subreg can convert MODE constants into integer
3554 constants. At present, it can only do this reliably if the value
3555 fits within a HOST_WIDE_INT. */
3556 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3558 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3560 if (ret)
3562 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3563 return ret;
3567 return emit_move_multi_word (mode, x, y);
3570 /* Generate code to copy Y into X.
3571 Both Y and X must have the same mode, except that
3572 Y can be a constant with VOIDmode.
3573 This mode cannot be BLKmode; use emit_block_move for that.
3575 Return the last instruction emitted. */
3577 rtx_insn *
3578 emit_move_insn (rtx x, rtx y)
3580 machine_mode mode = GET_MODE (x);
3581 rtx y_cst = NULL_RTX;
3582 rtx_insn *last_insn;
3583 rtx set;
3585 gcc_assert (mode != BLKmode
3586 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3588 if (CONSTANT_P (y))
3590 if (optimize
3591 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3592 && (last_insn = compress_float_constant (x, y)))
3593 return last_insn;
3595 y_cst = y;
3597 if (!targetm.legitimate_constant_p (mode, y))
3599 y = force_const_mem (mode, y);
3601 /* If the target's cannot_force_const_mem prevented the spill,
3602 assume that the target's move expanders will also take care
3603 of the non-legitimate constant. */
3604 if (!y)
3605 y = y_cst;
3606 else
3607 y = use_anchored_address (y);
3611 /* If X or Y are memory references, verify that their addresses are valid
3612 for the machine. */
3613 if (MEM_P (x)
3614 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3615 MEM_ADDR_SPACE (x))
3616 && ! push_operand (x, GET_MODE (x))))
3617 x = validize_mem (x);
3619 if (MEM_P (y)
3620 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3621 MEM_ADDR_SPACE (y)))
3622 y = validize_mem (y);
3624 gcc_assert (mode != BLKmode);
3626 last_insn = emit_move_insn_1 (x, y);
3628 if (y_cst && REG_P (x)
3629 && (set = single_set (last_insn)) != NULL_RTX
3630 && SET_DEST (set) == x
3631 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3632 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3634 return last_insn;
3637 /* Generate the body of an instruction to copy Y into X.
3638 It may be a list of insns, if one insn isn't enough. */
3641 gen_move_insn (rtx x, rtx y)
3643 rtx_insn *seq;
3645 start_sequence ();
3646 emit_move_insn_1 (x, y);
3647 seq = get_insns ();
3648 end_sequence ();
3649 return seq;
3652 /* If Y is representable exactly in a narrower mode, and the target can
3653 perform the extension directly from constant or memory, then emit the
3654 move as an extension. */
3656 static rtx_insn *
3657 compress_float_constant (rtx x, rtx y)
3659 machine_mode dstmode = GET_MODE (x);
3660 machine_mode orig_srcmode = GET_MODE (y);
3661 machine_mode srcmode;
3662 REAL_VALUE_TYPE r;
3663 int oldcost, newcost;
3664 bool speed = optimize_insn_for_speed_p ();
3666 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3668 if (targetm.legitimate_constant_p (dstmode, y))
3669 oldcost = set_src_cost (y, speed);
3670 else
3671 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3673 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3674 srcmode != orig_srcmode;
3675 srcmode = GET_MODE_WIDER_MODE (srcmode))
3677 enum insn_code ic;
3678 rtx trunc_y;
3679 rtx_insn *last_insn;
3681 /* Skip if the target can't extend this way. */
3682 ic = can_extend_p (dstmode, srcmode, 0);
3683 if (ic == CODE_FOR_nothing)
3684 continue;
3686 /* Skip if the narrowed value isn't exact. */
3687 if (! exact_real_truncate (srcmode, &r))
3688 continue;
3690 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3692 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3694 /* Skip if the target needs extra instructions to perform
3695 the extension. */
3696 if (!insn_operand_matches (ic, 1, trunc_y))
3697 continue;
3698 /* This is valid, but may not be cheaper than the original. */
3699 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3700 speed);
3701 if (oldcost < newcost)
3702 continue;
3704 else if (float_extend_from_mem[dstmode][srcmode])
3706 trunc_y = force_const_mem (srcmode, trunc_y);
3707 /* This is valid, but may not be cheaper than the original. */
3708 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3709 speed);
3710 if (oldcost < newcost)
3711 continue;
3712 trunc_y = validize_mem (trunc_y);
3714 else
3715 continue;
3717 /* For CSE's benefit, force the compressed constant pool entry
3718 into a new pseudo. This constant may be used in different modes,
3719 and if not, combine will put things back together for us. */
3720 trunc_y = force_reg (srcmode, trunc_y);
3722 /* If x is a hard register, perform the extension into a pseudo,
3723 so that e.g. stack realignment code is aware of it. */
3724 rtx target = x;
3725 if (REG_P (x) && HARD_REGISTER_P (x))
3726 target = gen_reg_rtx (dstmode);
3728 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3729 last_insn = get_last_insn ();
3731 if (REG_P (target))
3732 set_unique_reg_note (last_insn, REG_EQUAL, y);
3734 if (target != x)
3735 return emit_move_insn (x, target);
3736 return last_insn;
3739 return NULL;
3742 /* Pushing data onto the stack. */
3744 /* Push a block of length SIZE (perhaps variable)
3745 and return an rtx to address the beginning of the block.
3746 The value may be virtual_outgoing_args_rtx.
3748 EXTRA is the number of bytes of padding to push in addition to SIZE.
3749 BELOW nonzero means this padding comes at low addresses;
3750 otherwise, the padding comes at high addresses. */
3753 push_block (rtx size, int extra, int below)
3755 rtx temp;
3757 size = convert_modes (Pmode, ptr_mode, size, 1);
3758 if (CONSTANT_P (size))
3759 anti_adjust_stack (plus_constant (Pmode, size, extra));
3760 else if (REG_P (size) && extra == 0)
3761 anti_adjust_stack (size);
3762 else
3764 temp = copy_to_mode_reg (Pmode, size);
3765 if (extra != 0)
3766 temp = expand_binop (Pmode, add_optab, temp,
3767 gen_int_mode (extra, Pmode),
3768 temp, 0, OPTAB_LIB_WIDEN);
3769 anti_adjust_stack (temp);
3772 #ifndef STACK_GROWS_DOWNWARD
3773 if (0)
3774 #else
3775 if (1)
3776 #endif
3778 temp = virtual_outgoing_args_rtx;
3779 if (extra != 0 && below)
3780 temp = plus_constant (Pmode, temp, extra);
3782 else
3784 if (CONST_INT_P (size))
3785 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3786 -INTVAL (size) - (below ? 0 : extra));
3787 else if (extra != 0 && !below)
3788 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3789 negate_rtx (Pmode, plus_constant (Pmode, size,
3790 extra)));
3791 else
3792 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3793 negate_rtx (Pmode, size));
3796 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3799 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3801 static rtx
3802 mem_autoinc_base (rtx mem)
3804 if (MEM_P (mem))
3806 rtx addr = XEXP (mem, 0);
3807 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3808 return XEXP (addr, 0);
3810 return NULL;
3813 /* A utility routine used here, in reload, and in try_split. The insns
3814 after PREV up to and including LAST are known to adjust the stack,
3815 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3816 placing notes as appropriate. PREV may be NULL, indicating the
3817 entire insn sequence prior to LAST should be scanned.
3819 The set of allowed stack pointer modifications is small:
3820 (1) One or more auto-inc style memory references (aka pushes),
3821 (2) One or more addition/subtraction with the SP as destination,
3822 (3) A single move insn with the SP as destination,
3823 (4) A call_pop insn,
3824 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3826 Insns in the sequence that do not modify the SP are ignored,
3827 except for noreturn calls.
3829 The return value is the amount of adjustment that can be trivially
3830 verified, via immediate operand or auto-inc. If the adjustment
3831 cannot be trivially extracted, the return value is INT_MIN. */
3833 HOST_WIDE_INT
3834 find_args_size_adjust (rtx_insn *insn)
3836 rtx dest, set, pat;
3837 int i;
3839 pat = PATTERN (insn);
3840 set = NULL;
3842 /* Look for a call_pop pattern. */
3843 if (CALL_P (insn))
3845 /* We have to allow non-call_pop patterns for the case
3846 of emit_single_push_insn of a TLS address. */
3847 if (GET_CODE (pat) != PARALLEL)
3848 return 0;
3850 /* All call_pop have a stack pointer adjust in the parallel.
3851 The call itself is always first, and the stack adjust is
3852 usually last, so search from the end. */
3853 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3855 set = XVECEXP (pat, 0, i);
3856 if (GET_CODE (set) != SET)
3857 continue;
3858 dest = SET_DEST (set);
3859 if (dest == stack_pointer_rtx)
3860 break;
3862 /* We'd better have found the stack pointer adjust. */
3863 if (i == 0)
3864 return 0;
3865 /* Fall through to process the extracted SET and DEST
3866 as if it was a standalone insn. */
3868 else if (GET_CODE (pat) == SET)
3869 set = pat;
3870 else if ((set = single_set (insn)) != NULL)
3872 else if (GET_CODE (pat) == PARALLEL)
3874 /* ??? Some older ports use a parallel with a stack adjust
3875 and a store for a PUSH_ROUNDING pattern, rather than a
3876 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3877 /* ??? See h8300 and m68k, pushqi1. */
3878 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3880 set = XVECEXP (pat, 0, i);
3881 if (GET_CODE (set) != SET)
3882 continue;
3883 dest = SET_DEST (set);
3884 if (dest == stack_pointer_rtx)
3885 break;
3887 /* We do not expect an auto-inc of the sp in the parallel. */
3888 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3889 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3890 != stack_pointer_rtx);
3892 if (i < 0)
3893 return 0;
3895 else
3896 return 0;
3898 dest = SET_DEST (set);
3900 /* Look for direct modifications of the stack pointer. */
3901 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3903 /* Look for a trivial adjustment, otherwise assume nothing. */
3904 /* Note that the SPU restore_stack_block pattern refers to
3905 the stack pointer in V4SImode. Consider that non-trivial. */
3906 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3907 && GET_CODE (SET_SRC (set)) == PLUS
3908 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3909 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3910 return INTVAL (XEXP (SET_SRC (set), 1));
3911 /* ??? Reload can generate no-op moves, which will be cleaned
3912 up later. Recognize it and continue searching. */
3913 else if (rtx_equal_p (dest, SET_SRC (set)))
3914 return 0;
3915 else
3916 return HOST_WIDE_INT_MIN;
3918 else
3920 rtx mem, addr;
3922 /* Otherwise only think about autoinc patterns. */
3923 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3925 mem = dest;
3926 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3927 != stack_pointer_rtx);
3929 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3930 mem = SET_SRC (set);
3931 else
3932 return 0;
3934 addr = XEXP (mem, 0);
3935 switch (GET_CODE (addr))
3937 case PRE_INC:
3938 case POST_INC:
3939 return GET_MODE_SIZE (GET_MODE (mem));
3940 case PRE_DEC:
3941 case POST_DEC:
3942 return -GET_MODE_SIZE (GET_MODE (mem));
3943 case PRE_MODIFY:
3944 case POST_MODIFY:
3945 addr = XEXP (addr, 1);
3946 gcc_assert (GET_CODE (addr) == PLUS);
3947 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3948 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3949 return INTVAL (XEXP (addr, 1));
3950 default:
3951 gcc_unreachable ();
3957 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3959 int args_size = end_args_size;
3960 bool saw_unknown = false;
3961 rtx_insn *insn;
3963 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3965 HOST_WIDE_INT this_delta;
3967 if (!NONDEBUG_INSN_P (insn))
3968 continue;
3970 this_delta = find_args_size_adjust (insn);
3971 if (this_delta == 0)
3973 if (!CALL_P (insn)
3974 || ACCUMULATE_OUTGOING_ARGS
3975 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3976 continue;
3979 gcc_assert (!saw_unknown);
3980 if (this_delta == HOST_WIDE_INT_MIN)
3981 saw_unknown = true;
3983 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3984 #ifdef STACK_GROWS_DOWNWARD
3985 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3986 #endif
3987 args_size -= this_delta;
3990 return saw_unknown ? INT_MIN : args_size;
3993 #ifdef PUSH_ROUNDING
3994 /* Emit single push insn. */
3996 static void
3997 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
3999 rtx dest_addr;
4000 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4001 rtx dest;
4002 enum insn_code icode;
4004 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4005 /* If there is push pattern, use it. Otherwise try old way of throwing
4006 MEM representing push operation to move expander. */
4007 icode = optab_handler (push_optab, mode);
4008 if (icode != CODE_FOR_nothing)
4010 struct expand_operand ops[1];
4012 create_input_operand (&ops[0], x, mode);
4013 if (maybe_expand_insn (icode, 1, ops))
4014 return;
4016 if (GET_MODE_SIZE (mode) == rounded_size)
4017 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4018 /* If we are to pad downward, adjust the stack pointer first and
4019 then store X into the stack location using an offset. This is
4020 because emit_move_insn does not know how to pad; it does not have
4021 access to type. */
4022 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4024 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4025 HOST_WIDE_INT offset;
4027 emit_move_insn (stack_pointer_rtx,
4028 expand_binop (Pmode,
4029 #ifdef STACK_GROWS_DOWNWARD
4030 sub_optab,
4031 #else
4032 add_optab,
4033 #endif
4034 stack_pointer_rtx,
4035 gen_int_mode (rounded_size, Pmode),
4036 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4038 offset = (HOST_WIDE_INT) padding_size;
4039 #ifdef STACK_GROWS_DOWNWARD
4040 if (STACK_PUSH_CODE == POST_DEC)
4041 /* We have already decremented the stack pointer, so get the
4042 previous value. */
4043 offset += (HOST_WIDE_INT) rounded_size;
4044 #else
4045 if (STACK_PUSH_CODE == POST_INC)
4046 /* We have already incremented the stack pointer, so get the
4047 previous value. */
4048 offset -= (HOST_WIDE_INT) rounded_size;
4049 #endif
4050 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4051 gen_int_mode (offset, Pmode));
4053 else
4055 #ifdef STACK_GROWS_DOWNWARD
4056 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4057 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4058 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4059 Pmode));
4060 #else
4061 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4062 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4063 gen_int_mode (rounded_size, Pmode));
4064 #endif
4065 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4068 dest = gen_rtx_MEM (mode, dest_addr);
4070 if (type != 0)
4072 set_mem_attributes (dest, type, 1);
4074 if (cfun->tail_call_marked)
4075 /* Function incoming arguments may overlap with sibling call
4076 outgoing arguments and we cannot allow reordering of reads
4077 from function arguments with stores to outgoing arguments
4078 of sibling calls. */
4079 set_mem_alias_set (dest, 0);
4081 emit_move_insn (dest, x);
4084 /* Emit and annotate a single push insn. */
4086 static void
4087 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4089 int delta, old_delta = stack_pointer_delta;
4090 rtx_insn *prev = get_last_insn ();
4091 rtx_insn *last;
4093 emit_single_push_insn_1 (mode, x, type);
4095 last = get_last_insn ();
4097 /* Notice the common case where we emitted exactly one insn. */
4098 if (PREV_INSN (last) == prev)
4100 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4101 return;
4104 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4105 gcc_assert (delta == INT_MIN || delta == old_delta);
4107 #endif
4109 /* Generate code to push X onto the stack, assuming it has mode MODE and
4110 type TYPE.
4111 MODE is redundant except when X is a CONST_INT (since they don't
4112 carry mode info).
4113 SIZE is an rtx for the size of data to be copied (in bytes),
4114 needed only if X is BLKmode.
4116 ALIGN (in bits) is maximum alignment we can assume.
4118 If PARTIAL and REG are both nonzero, then copy that many of the first
4119 bytes of X into registers starting with REG, and push the rest of X.
4120 The amount of space pushed is decreased by PARTIAL bytes.
4121 REG must be a hard register in this case.
4122 If REG is zero but PARTIAL is not, take any all others actions for an
4123 argument partially in registers, but do not actually load any
4124 registers.
4126 EXTRA is the amount in bytes of extra space to leave next to this arg.
4127 This is ignored if an argument block has already been allocated.
4129 On a machine that lacks real push insns, ARGS_ADDR is the address of
4130 the bottom of the argument block for this call. We use indexing off there
4131 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4132 argument block has not been preallocated.
4134 ARGS_SO_FAR is the size of args previously pushed for this call.
4136 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4137 for arguments passed in registers. If nonzero, it will be the number
4138 of bytes required. */
4140 void
4141 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4142 unsigned int align, int partial, rtx reg, int extra,
4143 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4144 rtx alignment_pad)
4146 rtx xinner;
4147 enum direction stack_direction
4148 #ifdef STACK_GROWS_DOWNWARD
4149 = downward;
4150 #else
4151 = upward;
4152 #endif
4154 /* Decide where to pad the argument: `downward' for below,
4155 `upward' for above, or `none' for don't pad it.
4156 Default is below for small data on big-endian machines; else above. */
4157 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4159 /* Invert direction if stack is post-decrement.
4160 FIXME: why? */
4161 if (STACK_PUSH_CODE == POST_DEC)
4162 if (where_pad != none)
4163 where_pad = (where_pad == downward ? upward : downward);
4165 xinner = x;
4167 if (mode == BLKmode
4168 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4170 /* Copy a block into the stack, entirely or partially. */
4172 rtx temp;
4173 int used;
4174 int offset;
4175 int skip;
4177 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4178 used = partial - offset;
4180 if (mode != BLKmode)
4182 /* A value is to be stored in an insufficiently aligned
4183 stack slot; copy via a suitably aligned slot if
4184 necessary. */
4185 size = GEN_INT (GET_MODE_SIZE (mode));
4186 if (!MEM_P (xinner))
4188 temp = assign_temp (type, 1, 1);
4189 emit_move_insn (temp, xinner);
4190 xinner = temp;
4194 gcc_assert (size);
4196 /* USED is now the # of bytes we need not copy to the stack
4197 because registers will take care of them. */
4199 if (partial != 0)
4200 xinner = adjust_address (xinner, BLKmode, used);
4202 /* If the partial register-part of the arg counts in its stack size,
4203 skip the part of stack space corresponding to the registers.
4204 Otherwise, start copying to the beginning of the stack space,
4205 by setting SKIP to 0. */
4206 skip = (reg_parm_stack_space == 0) ? 0 : used;
4208 #ifdef PUSH_ROUNDING
4209 /* Do it with several push insns if that doesn't take lots of insns
4210 and if there is no difficulty with push insns that skip bytes
4211 on the stack for alignment purposes. */
4212 if (args_addr == 0
4213 && PUSH_ARGS
4214 && CONST_INT_P (size)
4215 && skip == 0
4216 && MEM_ALIGN (xinner) >= align
4217 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4218 /* Here we avoid the case of a structure whose weak alignment
4219 forces many pushes of a small amount of data,
4220 and such small pushes do rounding that causes trouble. */
4221 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4222 || align >= BIGGEST_ALIGNMENT
4223 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4224 == (align / BITS_PER_UNIT)))
4225 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4227 /* Push padding now if padding above and stack grows down,
4228 or if padding below and stack grows up.
4229 But if space already allocated, this has already been done. */
4230 if (extra && args_addr == 0
4231 && where_pad != none && where_pad != stack_direction)
4232 anti_adjust_stack (GEN_INT (extra));
4234 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4236 else
4237 #endif /* PUSH_ROUNDING */
4239 rtx target;
4241 /* Otherwise make space on the stack and copy the data
4242 to the address of that space. */
4244 /* Deduct words put into registers from the size we must copy. */
4245 if (partial != 0)
4247 if (CONST_INT_P (size))
4248 size = GEN_INT (INTVAL (size) - used);
4249 else
4250 size = expand_binop (GET_MODE (size), sub_optab, size,
4251 gen_int_mode (used, GET_MODE (size)),
4252 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4255 /* Get the address of the stack space.
4256 In this case, we do not deal with EXTRA separately.
4257 A single stack adjust will do. */
4258 if (! args_addr)
4260 temp = push_block (size, extra, where_pad == downward);
4261 extra = 0;
4263 else if (CONST_INT_P (args_so_far))
4264 temp = memory_address (BLKmode,
4265 plus_constant (Pmode, args_addr,
4266 skip + INTVAL (args_so_far)));
4267 else
4268 temp = memory_address (BLKmode,
4269 plus_constant (Pmode,
4270 gen_rtx_PLUS (Pmode,
4271 args_addr,
4272 args_so_far),
4273 skip));
4275 if (!ACCUMULATE_OUTGOING_ARGS)
4277 /* If the source is referenced relative to the stack pointer,
4278 copy it to another register to stabilize it. We do not need
4279 to do this if we know that we won't be changing sp. */
4281 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4282 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4283 temp = copy_to_reg (temp);
4286 target = gen_rtx_MEM (BLKmode, temp);
4288 /* We do *not* set_mem_attributes here, because incoming arguments
4289 may overlap with sibling call outgoing arguments and we cannot
4290 allow reordering of reads from function arguments with stores
4291 to outgoing arguments of sibling calls. We do, however, want
4292 to record the alignment of the stack slot. */
4293 /* ALIGN may well be better aligned than TYPE, e.g. due to
4294 PARM_BOUNDARY. Assume the caller isn't lying. */
4295 set_mem_align (target, align);
4297 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4300 else if (partial > 0)
4302 /* Scalar partly in registers. */
4304 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4305 int i;
4306 int not_stack;
4307 /* # bytes of start of argument
4308 that we must make space for but need not store. */
4309 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4310 int args_offset = INTVAL (args_so_far);
4311 int skip;
4313 /* Push padding now if padding above and stack grows down,
4314 or if padding below and stack grows up.
4315 But if space already allocated, this has already been done. */
4316 if (extra && args_addr == 0
4317 && where_pad != none && where_pad != stack_direction)
4318 anti_adjust_stack (GEN_INT (extra));
4320 /* If we make space by pushing it, we might as well push
4321 the real data. Otherwise, we can leave OFFSET nonzero
4322 and leave the space uninitialized. */
4323 if (args_addr == 0)
4324 offset = 0;
4326 /* Now NOT_STACK gets the number of words that we don't need to
4327 allocate on the stack. Convert OFFSET to words too. */
4328 not_stack = (partial - offset) / UNITS_PER_WORD;
4329 offset /= UNITS_PER_WORD;
4331 /* If the partial register-part of the arg counts in its stack size,
4332 skip the part of stack space corresponding to the registers.
4333 Otherwise, start copying to the beginning of the stack space,
4334 by setting SKIP to 0. */
4335 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4337 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4338 x = validize_mem (force_const_mem (mode, x));
4340 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4341 SUBREGs of such registers are not allowed. */
4342 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4343 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4344 x = copy_to_reg (x);
4346 /* Loop over all the words allocated on the stack for this arg. */
4347 /* We can do it by words, because any scalar bigger than a word
4348 has a size a multiple of a word. */
4349 for (i = size - 1; i >= not_stack; i--)
4350 if (i >= not_stack + offset)
4351 emit_push_insn (operand_subword_force (x, i, mode),
4352 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4353 0, args_addr,
4354 GEN_INT (args_offset + ((i - not_stack + skip)
4355 * UNITS_PER_WORD)),
4356 reg_parm_stack_space, alignment_pad);
4358 else
4360 rtx addr;
4361 rtx dest;
4363 /* Push padding now if padding above and stack grows down,
4364 or if padding below and stack grows up.
4365 But if space already allocated, this has already been done. */
4366 if (extra && args_addr == 0
4367 && where_pad != none && where_pad != stack_direction)
4368 anti_adjust_stack (GEN_INT (extra));
4370 #ifdef PUSH_ROUNDING
4371 if (args_addr == 0 && PUSH_ARGS)
4372 emit_single_push_insn (mode, x, type);
4373 else
4374 #endif
4376 if (CONST_INT_P (args_so_far))
4377 addr
4378 = memory_address (mode,
4379 plus_constant (Pmode, args_addr,
4380 INTVAL (args_so_far)));
4381 else
4382 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4383 args_so_far));
4384 dest = gen_rtx_MEM (mode, addr);
4386 /* We do *not* set_mem_attributes here, because incoming arguments
4387 may overlap with sibling call outgoing arguments and we cannot
4388 allow reordering of reads from function arguments with stores
4389 to outgoing arguments of sibling calls. We do, however, want
4390 to record the alignment of the stack slot. */
4391 /* ALIGN may well be better aligned than TYPE, e.g. due to
4392 PARM_BOUNDARY. Assume the caller isn't lying. */
4393 set_mem_align (dest, align);
4395 emit_move_insn (dest, x);
4399 /* If part should go in registers, copy that part
4400 into the appropriate registers. Do this now, at the end,
4401 since mem-to-mem copies above may do function calls. */
4402 if (partial > 0 && reg != 0)
4404 /* Handle calls that pass values in multiple non-contiguous locations.
4405 The Irix 6 ABI has examples of this. */
4406 if (GET_CODE (reg) == PARALLEL)
4407 emit_group_load (reg, x, type, -1);
4408 else
4410 gcc_assert (partial % UNITS_PER_WORD == 0);
4411 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4415 if (extra && args_addr == 0 && where_pad == stack_direction)
4416 anti_adjust_stack (GEN_INT (extra));
4418 if (alignment_pad && args_addr == 0)
4419 anti_adjust_stack (alignment_pad);
4422 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4423 operations. */
4425 static rtx
4426 get_subtarget (rtx x)
4428 return (optimize
4429 || x == 0
4430 /* Only registers can be subtargets. */
4431 || !REG_P (x)
4432 /* Don't use hard regs to avoid extending their life. */
4433 || REGNO (x) < FIRST_PSEUDO_REGISTER
4434 ? 0 : x);
4437 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4438 FIELD is a bitfield. Returns true if the optimization was successful,
4439 and there's nothing else to do. */
4441 static bool
4442 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4443 unsigned HOST_WIDE_INT bitpos,
4444 unsigned HOST_WIDE_INT bitregion_start,
4445 unsigned HOST_WIDE_INT bitregion_end,
4446 machine_mode mode1, rtx str_rtx,
4447 tree to, tree src)
4449 machine_mode str_mode = GET_MODE (str_rtx);
4450 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4451 tree op0, op1;
4452 rtx value, result;
4453 optab binop;
4454 gimple srcstmt;
4455 enum tree_code code;
4457 if (mode1 != VOIDmode
4458 || bitsize >= BITS_PER_WORD
4459 || str_bitsize > BITS_PER_WORD
4460 || TREE_SIDE_EFFECTS (to)
4461 || TREE_THIS_VOLATILE (to))
4462 return false;
4464 STRIP_NOPS (src);
4465 if (TREE_CODE (src) != SSA_NAME)
4466 return false;
4467 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4468 return false;
4470 srcstmt = get_gimple_for_ssa_name (src);
4471 if (!srcstmt
4472 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4473 return false;
4475 code = gimple_assign_rhs_code (srcstmt);
4477 op0 = gimple_assign_rhs1 (srcstmt);
4479 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4480 to find its initialization. Hopefully the initialization will
4481 be from a bitfield load. */
4482 if (TREE_CODE (op0) == SSA_NAME)
4484 gimple op0stmt = get_gimple_for_ssa_name (op0);
4486 /* We want to eventually have OP0 be the same as TO, which
4487 should be a bitfield. */
4488 if (!op0stmt
4489 || !is_gimple_assign (op0stmt)
4490 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4491 return false;
4492 op0 = gimple_assign_rhs1 (op0stmt);
4495 op1 = gimple_assign_rhs2 (srcstmt);
4497 if (!operand_equal_p (to, op0, 0))
4498 return false;
4500 if (MEM_P (str_rtx))
4502 unsigned HOST_WIDE_INT offset1;
4504 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4505 str_mode = word_mode;
4506 str_mode = get_best_mode (bitsize, bitpos,
4507 bitregion_start, bitregion_end,
4508 MEM_ALIGN (str_rtx), str_mode, 0);
4509 if (str_mode == VOIDmode)
4510 return false;
4511 str_bitsize = GET_MODE_BITSIZE (str_mode);
4513 offset1 = bitpos;
4514 bitpos %= str_bitsize;
4515 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4516 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4518 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4519 return false;
4521 /* If the bit field covers the whole REG/MEM, store_field
4522 will likely generate better code. */
4523 if (bitsize >= str_bitsize)
4524 return false;
4526 /* We can't handle fields split across multiple entities. */
4527 if (bitpos + bitsize > str_bitsize)
4528 return false;
4530 if (BYTES_BIG_ENDIAN)
4531 bitpos = str_bitsize - bitpos - bitsize;
4533 switch (code)
4535 case PLUS_EXPR:
4536 case MINUS_EXPR:
4537 /* For now, just optimize the case of the topmost bitfield
4538 where we don't need to do any masking and also
4539 1 bit bitfields where xor can be used.
4540 We might win by one instruction for the other bitfields
4541 too if insv/extv instructions aren't used, so that
4542 can be added later. */
4543 if (bitpos + bitsize != str_bitsize
4544 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4545 break;
4547 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4548 value = convert_modes (str_mode,
4549 TYPE_MODE (TREE_TYPE (op1)), value,
4550 TYPE_UNSIGNED (TREE_TYPE (op1)));
4552 /* We may be accessing data outside the field, which means
4553 we can alias adjacent data. */
4554 if (MEM_P (str_rtx))
4556 str_rtx = shallow_copy_rtx (str_rtx);
4557 set_mem_alias_set (str_rtx, 0);
4558 set_mem_expr (str_rtx, 0);
4561 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4562 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4564 value = expand_and (str_mode, value, const1_rtx, NULL);
4565 binop = xor_optab;
4567 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4568 result = expand_binop (str_mode, binop, str_rtx,
4569 value, str_rtx, 1, OPTAB_WIDEN);
4570 if (result != str_rtx)
4571 emit_move_insn (str_rtx, result);
4572 return true;
4574 case BIT_IOR_EXPR:
4575 case BIT_XOR_EXPR:
4576 if (TREE_CODE (op1) != INTEGER_CST)
4577 break;
4578 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4579 value = convert_modes (str_mode,
4580 TYPE_MODE (TREE_TYPE (op1)), value,
4581 TYPE_UNSIGNED (TREE_TYPE (op1)));
4583 /* We may be accessing data outside the field, which means
4584 we can alias adjacent data. */
4585 if (MEM_P (str_rtx))
4587 str_rtx = shallow_copy_rtx (str_rtx);
4588 set_mem_alias_set (str_rtx, 0);
4589 set_mem_expr (str_rtx, 0);
4592 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4593 if (bitpos + bitsize != str_bitsize)
4595 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4596 str_mode);
4597 value = expand_and (str_mode, value, mask, NULL_RTX);
4599 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4600 result = expand_binop (str_mode, binop, str_rtx,
4601 value, str_rtx, 1, OPTAB_WIDEN);
4602 if (result != str_rtx)
4603 emit_move_insn (str_rtx, result);
4604 return true;
4606 default:
4607 break;
4610 return false;
4613 /* In the C++ memory model, consecutive bit fields in a structure are
4614 considered one memory location.
4616 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4617 returns the bit range of consecutive bits in which this COMPONENT_REF
4618 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4619 and *OFFSET may be adjusted in the process.
4621 If the access does not need to be restricted, 0 is returned in both
4622 *BITSTART and *BITEND. */
4624 static void
4625 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4626 unsigned HOST_WIDE_INT *bitend,
4627 tree exp,
4628 HOST_WIDE_INT *bitpos,
4629 tree *offset)
4631 HOST_WIDE_INT bitoffset;
4632 tree field, repr;
4634 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4636 field = TREE_OPERAND (exp, 1);
4637 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4638 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4639 need to limit the range we can access. */
4640 if (!repr)
4642 *bitstart = *bitend = 0;
4643 return;
4646 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4647 part of a larger bit field, then the representative does not serve any
4648 useful purpose. This can occur in Ada. */
4649 if (handled_component_p (TREE_OPERAND (exp, 0)))
4651 machine_mode rmode;
4652 HOST_WIDE_INT rbitsize, rbitpos;
4653 tree roffset;
4654 int unsignedp;
4655 int volatilep = 0;
4656 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4657 &roffset, &rmode, &unsignedp, &volatilep, false);
4658 if ((rbitpos % BITS_PER_UNIT) != 0)
4660 *bitstart = *bitend = 0;
4661 return;
4665 /* Compute the adjustment to bitpos from the offset of the field
4666 relative to the representative. DECL_FIELD_OFFSET of field and
4667 repr are the same by construction if they are not constants,
4668 see finish_bitfield_layout. */
4669 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4670 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4671 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4672 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4673 else
4674 bitoffset = 0;
4675 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4676 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4678 /* If the adjustment is larger than bitpos, we would have a negative bit
4679 position for the lower bound and this may wreak havoc later. Adjust
4680 offset and bitpos to make the lower bound non-negative in that case. */
4681 if (bitoffset > *bitpos)
4683 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4684 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4686 *bitpos += adjust;
4687 if (*offset == NULL_TREE)
4688 *offset = size_int (-adjust / BITS_PER_UNIT);
4689 else
4690 *offset
4691 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4692 *bitstart = 0;
4694 else
4695 *bitstart = *bitpos - bitoffset;
4697 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4700 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4701 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4702 DECL_RTL was not set yet, return NORTL. */
4704 static inline bool
4705 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4707 if (TREE_CODE (addr) != ADDR_EXPR)
4708 return false;
4710 tree base = TREE_OPERAND (addr, 0);
4712 if (!DECL_P (base)
4713 || TREE_ADDRESSABLE (base)
4714 || DECL_MODE (base) == BLKmode)
4715 return false;
4717 if (!DECL_RTL_SET_P (base))
4718 return nortl;
4720 return (!MEM_P (DECL_RTL (base)));
4723 /* Returns true if the MEM_REF REF refers to an object that does not
4724 reside in memory and has non-BLKmode. */
4726 static inline bool
4727 mem_ref_refers_to_non_mem_p (tree ref)
4729 tree base = TREE_OPERAND (ref, 0);
4730 return addr_expr_of_non_mem_decl_p_1 (base, false);
4733 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4734 is true, try generating a nontemporal store. */
4736 void
4737 expand_assignment (tree to, tree from, bool nontemporal)
4739 rtx to_rtx = 0;
4740 rtx result;
4741 machine_mode mode;
4742 unsigned int align;
4743 enum insn_code icode;
4745 /* Don't crash if the lhs of the assignment was erroneous. */
4746 if (TREE_CODE (to) == ERROR_MARK)
4748 expand_normal (from);
4749 return;
4752 /* Optimize away no-op moves without side-effects. */
4753 if (operand_equal_p (to, from, 0))
4754 return;
4756 /* Handle misaligned stores. */
4757 mode = TYPE_MODE (TREE_TYPE (to));
4758 if ((TREE_CODE (to) == MEM_REF
4759 || TREE_CODE (to) == TARGET_MEM_REF)
4760 && mode != BLKmode
4761 && !mem_ref_refers_to_non_mem_p (to)
4762 && ((align = get_object_alignment (to))
4763 < GET_MODE_ALIGNMENT (mode))
4764 && (((icode = optab_handler (movmisalign_optab, mode))
4765 != CODE_FOR_nothing)
4766 || SLOW_UNALIGNED_ACCESS (mode, align)))
4768 rtx reg, mem;
4770 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4771 reg = force_not_mem (reg);
4772 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4774 if (icode != CODE_FOR_nothing)
4776 struct expand_operand ops[2];
4778 create_fixed_operand (&ops[0], mem);
4779 create_input_operand (&ops[1], reg, mode);
4780 /* The movmisalign<mode> pattern cannot fail, else the assignment
4781 would silently be omitted. */
4782 expand_insn (icode, 2, ops);
4784 else
4785 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4786 return;
4789 /* Assignment of a structure component needs special treatment
4790 if the structure component's rtx is not simply a MEM.
4791 Assignment of an array element at a constant index, and assignment of
4792 an array element in an unaligned packed structure field, has the same
4793 problem. Same for (partially) storing into a non-memory object. */
4794 if (handled_component_p (to)
4795 || (TREE_CODE (to) == MEM_REF
4796 && mem_ref_refers_to_non_mem_p (to))
4797 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4799 machine_mode mode1;
4800 HOST_WIDE_INT bitsize, bitpos;
4801 unsigned HOST_WIDE_INT bitregion_start = 0;
4802 unsigned HOST_WIDE_INT bitregion_end = 0;
4803 tree offset;
4804 int unsignedp;
4805 int volatilep = 0;
4806 tree tem;
4808 push_temp_slots ();
4809 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4810 &unsignedp, &volatilep, true);
4812 /* Make sure bitpos is not negative, it can wreak havoc later. */
4813 if (bitpos < 0)
4815 gcc_assert (offset == NULL_TREE);
4816 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4817 ? 3 : exact_log2 (BITS_PER_UNIT)));
4818 bitpos &= BITS_PER_UNIT - 1;
4821 if (TREE_CODE (to) == COMPONENT_REF
4822 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4823 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4824 /* The C++ memory model naturally applies to byte-aligned fields.
4825 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4826 BITSIZE are not byte-aligned, there is no need to limit the range
4827 we can access. This can occur with packed structures in Ada. */
4828 else if (bitsize > 0
4829 && bitsize % BITS_PER_UNIT == 0
4830 && bitpos % BITS_PER_UNIT == 0)
4832 bitregion_start = bitpos;
4833 bitregion_end = bitpos + bitsize - 1;
4836 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4838 /* If the field has a mode, we want to access it in the
4839 field's mode, not the computed mode.
4840 If a MEM has VOIDmode (external with incomplete type),
4841 use BLKmode for it instead. */
4842 if (MEM_P (to_rtx))
4844 if (mode1 != VOIDmode)
4845 to_rtx = adjust_address (to_rtx, mode1, 0);
4846 else if (GET_MODE (to_rtx) == VOIDmode)
4847 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4850 if (offset != 0)
4852 machine_mode address_mode;
4853 rtx offset_rtx;
4855 if (!MEM_P (to_rtx))
4857 /* We can get constant negative offsets into arrays with broken
4858 user code. Translate this to a trap instead of ICEing. */
4859 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4860 expand_builtin_trap ();
4861 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4864 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4865 address_mode = get_address_mode (to_rtx);
4866 if (GET_MODE (offset_rtx) != address_mode)
4867 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4869 /* If we have an expression in OFFSET_RTX and a non-zero
4870 byte offset in BITPOS, adding the byte offset before the
4871 OFFSET_RTX results in better intermediate code, which makes
4872 later rtl optimization passes perform better.
4874 We prefer intermediate code like this:
4876 r124:DI=r123:DI+0x18
4877 [r124:DI]=r121:DI
4879 ... instead of ...
4881 r124:DI=r123:DI+0x10
4882 [r124:DI+0x8]=r121:DI
4884 This is only done for aligned data values, as these can
4885 be expected to result in single move instructions. */
4886 if (mode1 != VOIDmode
4887 && bitpos != 0
4888 && bitsize > 0
4889 && (bitpos % bitsize) == 0
4890 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4891 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4893 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4894 bitregion_start = 0;
4895 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4896 bitregion_end -= bitpos;
4897 bitpos = 0;
4900 to_rtx = offset_address (to_rtx, offset_rtx,
4901 highest_pow2_factor_for_target (to,
4902 offset));
4905 /* No action is needed if the target is not a memory and the field
4906 lies completely outside that target. This can occur if the source
4907 code contains an out-of-bounds access to a small array. */
4908 if (!MEM_P (to_rtx)
4909 && GET_MODE (to_rtx) != BLKmode
4910 && (unsigned HOST_WIDE_INT) bitpos
4911 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4913 expand_normal (from);
4914 result = NULL;
4916 /* Handle expand_expr of a complex value returning a CONCAT. */
4917 else if (GET_CODE (to_rtx) == CONCAT)
4919 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4920 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4921 && bitpos == 0
4922 && bitsize == mode_bitsize)
4923 result = store_expr (from, to_rtx, false, nontemporal);
4924 else if (bitsize == mode_bitsize / 2
4925 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4926 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4927 nontemporal);
4928 else if (bitpos + bitsize <= mode_bitsize / 2)
4929 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4930 bitregion_start, bitregion_end,
4931 mode1, from,
4932 get_alias_set (to), nontemporal);
4933 else if (bitpos >= mode_bitsize / 2)
4934 result = store_field (XEXP (to_rtx, 1), bitsize,
4935 bitpos - mode_bitsize / 2,
4936 bitregion_start, bitregion_end,
4937 mode1, from,
4938 get_alias_set (to), nontemporal);
4939 else if (bitpos == 0 && bitsize == mode_bitsize)
4941 rtx from_rtx;
4942 result = expand_normal (from);
4943 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4944 TYPE_MODE (TREE_TYPE (from)), 0);
4945 emit_move_insn (XEXP (to_rtx, 0),
4946 read_complex_part (from_rtx, false));
4947 emit_move_insn (XEXP (to_rtx, 1),
4948 read_complex_part (from_rtx, true));
4950 else
4952 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4953 GET_MODE_SIZE (GET_MODE (to_rtx)));
4954 write_complex_part (temp, XEXP (to_rtx, 0), false);
4955 write_complex_part (temp, XEXP (to_rtx, 1), true);
4956 result = store_field (temp, bitsize, bitpos,
4957 bitregion_start, bitregion_end,
4958 mode1, from,
4959 get_alias_set (to), nontemporal);
4960 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4961 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4964 else
4966 if (MEM_P (to_rtx))
4968 /* If the field is at offset zero, we could have been given the
4969 DECL_RTX of the parent struct. Don't munge it. */
4970 to_rtx = shallow_copy_rtx (to_rtx);
4971 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4972 if (volatilep)
4973 MEM_VOLATILE_P (to_rtx) = 1;
4976 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4977 bitregion_start, bitregion_end,
4978 mode1,
4979 to_rtx, to, from))
4980 result = NULL;
4981 else
4982 result = store_field (to_rtx, bitsize, bitpos,
4983 bitregion_start, bitregion_end,
4984 mode1, from,
4985 get_alias_set (to), nontemporal);
4988 if (result)
4989 preserve_temp_slots (result);
4990 pop_temp_slots ();
4991 return;
4994 /* If the rhs is a function call and its value is not an aggregate,
4995 call the function before we start to compute the lhs.
4996 This is needed for correct code for cases such as
4997 val = setjmp (buf) on machines where reference to val
4998 requires loading up part of an address in a separate insn.
5000 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5001 since it might be a promoted variable where the zero- or sign- extension
5002 needs to be done. Handling this in the normal way is safe because no
5003 computation is done before the call. The same is true for SSA names. */
5004 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5005 && COMPLETE_TYPE_P (TREE_TYPE (from))
5006 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5007 && ! (((TREE_CODE (to) == VAR_DECL
5008 || TREE_CODE (to) == PARM_DECL
5009 || TREE_CODE (to) == RESULT_DECL)
5010 && REG_P (DECL_RTL (to)))
5011 || TREE_CODE (to) == SSA_NAME))
5013 rtx value;
5014 rtx bounds;
5016 push_temp_slots ();
5017 value = expand_normal (from);
5019 /* Split value and bounds to store them separately. */
5020 chkp_split_slot (value, &value, &bounds);
5022 if (to_rtx == 0)
5023 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5025 /* Handle calls that return values in multiple non-contiguous locations.
5026 The Irix 6 ABI has examples of this. */
5027 if (GET_CODE (to_rtx) == PARALLEL)
5029 if (GET_CODE (value) == PARALLEL)
5030 emit_group_move (to_rtx, value);
5031 else
5032 emit_group_load (to_rtx, value, TREE_TYPE (from),
5033 int_size_in_bytes (TREE_TYPE (from)));
5035 else if (GET_CODE (value) == PARALLEL)
5036 emit_group_store (to_rtx, value, TREE_TYPE (from),
5037 int_size_in_bytes (TREE_TYPE (from)));
5038 else if (GET_MODE (to_rtx) == BLKmode)
5040 /* Handle calls that return BLKmode values in registers. */
5041 if (REG_P (value))
5042 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5043 else
5044 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5046 else
5048 if (POINTER_TYPE_P (TREE_TYPE (to)))
5049 value = convert_memory_address_addr_space
5050 (GET_MODE (to_rtx), value,
5051 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5053 emit_move_insn (to_rtx, value);
5056 /* Store bounds if required. */
5057 if (bounds
5058 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5060 gcc_assert (MEM_P (to_rtx));
5061 chkp_emit_bounds_store (bounds, value, to_rtx);
5064 preserve_temp_slots (to_rtx);
5065 pop_temp_slots ();
5066 return;
5069 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5070 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5072 /* Don't move directly into a return register. */
5073 if (TREE_CODE (to) == RESULT_DECL
5074 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5076 rtx temp;
5078 push_temp_slots ();
5080 /* If the source is itself a return value, it still is in a pseudo at
5081 this point so we can move it back to the return register directly. */
5082 if (REG_P (to_rtx)
5083 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5084 && TREE_CODE (from) != CALL_EXPR)
5085 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5086 else
5087 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5089 /* Handle calls that return values in multiple non-contiguous locations.
5090 The Irix 6 ABI has examples of this. */
5091 if (GET_CODE (to_rtx) == PARALLEL)
5093 if (GET_CODE (temp) == PARALLEL)
5094 emit_group_move (to_rtx, temp);
5095 else
5096 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5097 int_size_in_bytes (TREE_TYPE (from)));
5099 else if (temp)
5100 emit_move_insn (to_rtx, temp);
5102 preserve_temp_slots (to_rtx);
5103 pop_temp_slots ();
5104 return;
5107 /* In case we are returning the contents of an object which overlaps
5108 the place the value is being stored, use a safe function when copying
5109 a value through a pointer into a structure value return block. */
5110 if (TREE_CODE (to) == RESULT_DECL
5111 && TREE_CODE (from) == INDIRECT_REF
5112 && ADDR_SPACE_GENERIC_P
5113 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5114 && refs_may_alias_p (to, from)
5115 && cfun->returns_struct
5116 && !cfun->returns_pcc_struct)
5118 rtx from_rtx, size;
5120 push_temp_slots ();
5121 size = expr_size (from);
5122 from_rtx = expand_normal (from);
5124 emit_library_call (memmove_libfunc, LCT_NORMAL,
5125 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5126 XEXP (from_rtx, 0), Pmode,
5127 convert_to_mode (TYPE_MODE (sizetype),
5128 size, TYPE_UNSIGNED (sizetype)),
5129 TYPE_MODE (sizetype));
5131 preserve_temp_slots (to_rtx);
5132 pop_temp_slots ();
5133 return;
5136 /* Compute FROM and store the value in the rtx we got. */
5138 push_temp_slots ();
5139 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5140 preserve_temp_slots (result);
5141 pop_temp_slots ();
5142 return;
5145 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5146 succeeded, false otherwise. */
5148 bool
5149 emit_storent_insn (rtx to, rtx from)
5151 struct expand_operand ops[2];
5152 machine_mode mode = GET_MODE (to);
5153 enum insn_code code = optab_handler (storent_optab, mode);
5155 if (code == CODE_FOR_nothing)
5156 return false;
5158 create_fixed_operand (&ops[0], to);
5159 create_input_operand (&ops[1], from, mode);
5160 return maybe_expand_insn (code, 2, ops);
5163 /* Generate code for computing expression EXP,
5164 and storing the value into TARGET.
5166 If the mode is BLKmode then we may return TARGET itself.
5167 It turns out that in BLKmode it doesn't cause a problem.
5168 because C has no operators that could combine two different
5169 assignments into the same BLKmode object with different values
5170 with no sequence point. Will other languages need this to
5171 be more thorough?
5173 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5174 stack, and block moves may need to be treated specially.
5176 If NONTEMPORAL is true, try using a nontemporal store instruction.
5178 If BTARGET is not NULL then computed bounds of EXP are
5179 associated with BTARGET. */
5182 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5183 bool nontemporal, tree btarget)
5185 rtx temp;
5186 rtx alt_rtl = NULL_RTX;
5187 location_t loc = curr_insn_location ();
5189 if (VOID_TYPE_P (TREE_TYPE (exp)))
5191 /* C++ can generate ?: expressions with a throw expression in one
5192 branch and an rvalue in the other. Here, we resolve attempts to
5193 store the throw expression's nonexistent result. */
5194 gcc_assert (!call_param_p);
5195 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5196 return NULL_RTX;
5198 if (TREE_CODE (exp) == COMPOUND_EXPR)
5200 /* Perform first part of compound expression, then assign from second
5201 part. */
5202 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5203 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5204 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5205 call_param_p, nontemporal, btarget);
5207 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5209 /* For conditional expression, get safe form of the target. Then
5210 test the condition, doing the appropriate assignment on either
5211 side. This avoids the creation of unnecessary temporaries.
5212 For non-BLKmode, it is more efficient not to do this. */
5214 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5216 do_pending_stack_adjust ();
5217 NO_DEFER_POP;
5218 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5219 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5220 nontemporal, btarget);
5221 emit_jump_insn (gen_jump (lab2));
5222 emit_barrier ();
5223 emit_label (lab1);
5224 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5225 nontemporal, btarget);
5226 emit_label (lab2);
5227 OK_DEFER_POP;
5229 return NULL_RTX;
5231 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5232 /* If this is a scalar in a register that is stored in a wider mode
5233 than the declared mode, compute the result into its declared mode
5234 and then convert to the wider mode. Our value is the computed
5235 expression. */
5237 rtx inner_target = 0;
5239 /* We can do the conversion inside EXP, which will often result
5240 in some optimizations. Do the conversion in two steps: first
5241 change the signedness, if needed, then the extend. But don't
5242 do this if the type of EXP is a subtype of something else
5243 since then the conversion might involve more than just
5244 converting modes. */
5245 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5246 && TREE_TYPE (TREE_TYPE (exp)) == 0
5247 && GET_MODE_PRECISION (GET_MODE (target))
5248 == TYPE_PRECISION (TREE_TYPE (exp)))
5250 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5251 TYPE_UNSIGNED (TREE_TYPE (exp))))
5253 /* Some types, e.g. Fortran's logical*4, won't have a signed
5254 version, so use the mode instead. */
5255 tree ntype
5256 = (signed_or_unsigned_type_for
5257 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5258 if (ntype == NULL)
5259 ntype = lang_hooks.types.type_for_mode
5260 (TYPE_MODE (TREE_TYPE (exp)),
5261 SUBREG_PROMOTED_SIGN (target));
5263 exp = fold_convert_loc (loc, ntype, exp);
5266 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5267 (GET_MODE (SUBREG_REG (target)),
5268 SUBREG_PROMOTED_SIGN (target)),
5269 exp);
5271 inner_target = SUBREG_REG (target);
5274 temp = expand_expr (exp, inner_target, VOIDmode,
5275 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5277 /* Handle bounds returned by call. */
5278 if (TREE_CODE (exp) == CALL_EXPR)
5280 rtx bounds;
5281 chkp_split_slot (temp, &temp, &bounds);
5282 if (bounds && btarget)
5284 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5285 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5286 chkp_set_rtl_bounds (btarget, tmp);
5290 /* If TEMP is a VOIDmode constant, use convert_modes to make
5291 sure that we properly convert it. */
5292 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5294 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5295 temp, SUBREG_PROMOTED_SIGN (target));
5296 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5297 GET_MODE (target), temp,
5298 SUBREG_PROMOTED_SIGN (target));
5301 convert_move (SUBREG_REG (target), temp,
5302 SUBREG_PROMOTED_SIGN (target));
5304 return NULL_RTX;
5306 else if ((TREE_CODE (exp) == STRING_CST
5307 || (TREE_CODE (exp) == MEM_REF
5308 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5309 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5310 == STRING_CST
5311 && integer_zerop (TREE_OPERAND (exp, 1))))
5312 && !nontemporal && !call_param_p
5313 && MEM_P (target))
5315 /* Optimize initialization of an array with a STRING_CST. */
5316 HOST_WIDE_INT exp_len, str_copy_len;
5317 rtx dest_mem;
5318 tree str = TREE_CODE (exp) == STRING_CST
5319 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5321 exp_len = int_expr_size (exp);
5322 if (exp_len <= 0)
5323 goto normal_expr;
5325 if (TREE_STRING_LENGTH (str) <= 0)
5326 goto normal_expr;
5328 str_copy_len = strlen (TREE_STRING_POINTER (str));
5329 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5330 goto normal_expr;
5332 str_copy_len = TREE_STRING_LENGTH (str);
5333 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5334 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5336 str_copy_len += STORE_MAX_PIECES - 1;
5337 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5339 str_copy_len = MIN (str_copy_len, exp_len);
5340 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5341 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5342 MEM_ALIGN (target), false))
5343 goto normal_expr;
5345 dest_mem = target;
5347 dest_mem = store_by_pieces (dest_mem,
5348 str_copy_len, builtin_strncpy_read_str,
5349 CONST_CAST (char *,
5350 TREE_STRING_POINTER (str)),
5351 MEM_ALIGN (target), false,
5352 exp_len > str_copy_len ? 1 : 0);
5353 if (exp_len > str_copy_len)
5354 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5355 GEN_INT (exp_len - str_copy_len),
5356 BLOCK_OP_NORMAL);
5357 return NULL_RTX;
5359 else
5361 rtx tmp_target;
5363 normal_expr:
5364 /* If we want to use a nontemporal store, force the value to
5365 register first. */
5366 tmp_target = nontemporal ? NULL_RTX : target;
5367 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5368 (call_param_p
5369 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5370 &alt_rtl, false);
5372 /* Handle bounds returned by call. */
5373 if (TREE_CODE (exp) == CALL_EXPR)
5375 rtx bounds;
5376 chkp_split_slot (temp, &temp, &bounds);
5377 if (bounds && btarget)
5379 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5380 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5381 chkp_set_rtl_bounds (btarget, tmp);
5386 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5387 the same as that of TARGET, adjust the constant. This is needed, for
5388 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5389 only a word-sized value. */
5390 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5391 && TREE_CODE (exp) != ERROR_MARK
5392 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5393 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5394 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5396 /* If value was not generated in the target, store it there.
5397 Convert the value to TARGET's type first if necessary and emit the
5398 pending incrementations that have been queued when expanding EXP.
5399 Note that we cannot emit the whole queue blindly because this will
5400 effectively disable the POST_INC optimization later.
5402 If TEMP and TARGET compare equal according to rtx_equal_p, but
5403 one or both of them are volatile memory refs, we have to distinguish
5404 two cases:
5405 - expand_expr has used TARGET. In this case, we must not generate
5406 another copy. This can be detected by TARGET being equal according
5407 to == .
5408 - expand_expr has not used TARGET - that means that the source just
5409 happens to have the same RTX form. Since temp will have been created
5410 by expand_expr, it will compare unequal according to == .
5411 We must generate a copy in this case, to reach the correct number
5412 of volatile memory references. */
5414 if ((! rtx_equal_p (temp, target)
5415 || (temp != target && (side_effects_p (temp)
5416 || side_effects_p (target))))
5417 && TREE_CODE (exp) != ERROR_MARK
5418 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5419 but TARGET is not valid memory reference, TEMP will differ
5420 from TARGET although it is really the same location. */
5421 && !(alt_rtl
5422 && rtx_equal_p (alt_rtl, target)
5423 && !side_effects_p (alt_rtl)
5424 && !side_effects_p (target))
5425 /* If there's nothing to copy, don't bother. Don't call
5426 expr_size unless necessary, because some front-ends (C++)
5427 expr_size-hook must not be given objects that are not
5428 supposed to be bit-copied or bit-initialized. */
5429 && expr_size (exp) != const0_rtx)
5431 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5433 if (GET_MODE (target) == BLKmode)
5435 /* Handle calls that return BLKmode values in registers. */
5436 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5437 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5438 else
5439 store_bit_field (target,
5440 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5441 0, 0, 0, GET_MODE (temp), temp);
5443 else
5444 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5447 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5449 /* Handle copying a string constant into an array. The string
5450 constant may be shorter than the array. So copy just the string's
5451 actual length, and clear the rest. First get the size of the data
5452 type of the string, which is actually the size of the target. */
5453 rtx size = expr_size (exp);
5455 if (CONST_INT_P (size)
5456 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5457 emit_block_move (target, temp, size,
5458 (call_param_p
5459 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5460 else
5462 machine_mode pointer_mode
5463 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5464 machine_mode address_mode = get_address_mode (target);
5466 /* Compute the size of the data to copy from the string. */
5467 tree copy_size
5468 = size_binop_loc (loc, MIN_EXPR,
5469 make_tree (sizetype, size),
5470 size_int (TREE_STRING_LENGTH (exp)));
5471 rtx copy_size_rtx
5472 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5473 (call_param_p
5474 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5475 rtx_code_label *label = 0;
5477 /* Copy that much. */
5478 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5479 TYPE_UNSIGNED (sizetype));
5480 emit_block_move (target, temp, copy_size_rtx,
5481 (call_param_p
5482 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5484 /* Figure out how much is left in TARGET that we have to clear.
5485 Do all calculations in pointer_mode. */
5486 if (CONST_INT_P (copy_size_rtx))
5488 size = plus_constant (address_mode, size,
5489 -INTVAL (copy_size_rtx));
5490 target = adjust_address (target, BLKmode,
5491 INTVAL (copy_size_rtx));
5493 else
5495 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5496 copy_size_rtx, NULL_RTX, 0,
5497 OPTAB_LIB_WIDEN);
5499 if (GET_MODE (copy_size_rtx) != address_mode)
5500 copy_size_rtx = convert_to_mode (address_mode,
5501 copy_size_rtx,
5502 TYPE_UNSIGNED (sizetype));
5504 target = offset_address (target, copy_size_rtx,
5505 highest_pow2_factor (copy_size));
5506 label = gen_label_rtx ();
5507 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5508 GET_MODE (size), 0, label);
5511 if (size != const0_rtx)
5512 clear_storage (target, size, BLOCK_OP_NORMAL);
5514 if (label)
5515 emit_label (label);
5518 /* Handle calls that return values in multiple non-contiguous locations.
5519 The Irix 6 ABI has examples of this. */
5520 else if (GET_CODE (target) == PARALLEL)
5522 if (GET_CODE (temp) == PARALLEL)
5523 emit_group_move (target, temp);
5524 else
5525 emit_group_load (target, temp, TREE_TYPE (exp),
5526 int_size_in_bytes (TREE_TYPE (exp)));
5528 else if (GET_CODE (temp) == PARALLEL)
5529 emit_group_store (target, temp, TREE_TYPE (exp),
5530 int_size_in_bytes (TREE_TYPE (exp)));
5531 else if (GET_MODE (temp) == BLKmode)
5532 emit_block_move (target, temp, expr_size (exp),
5533 (call_param_p
5534 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5535 /* If we emit a nontemporal store, there is nothing else to do. */
5536 else if (nontemporal && emit_storent_insn (target, temp))
5538 else
5540 temp = force_operand (temp, target);
5541 if (temp != target)
5542 emit_move_insn (target, temp);
5546 return NULL_RTX;
5549 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5551 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5553 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5556 /* Return true if field F of structure TYPE is a flexible array. */
5558 static bool
5559 flexible_array_member_p (const_tree f, const_tree type)
5561 const_tree tf;
5563 tf = TREE_TYPE (f);
5564 return (DECL_CHAIN (f) == NULL
5565 && TREE_CODE (tf) == ARRAY_TYPE
5566 && TYPE_DOMAIN (tf)
5567 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5568 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5569 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5570 && int_size_in_bytes (type) >= 0);
5573 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5574 must have in order for it to completely initialize a value of type TYPE.
5575 Return -1 if the number isn't known.
5577 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5579 static HOST_WIDE_INT
5580 count_type_elements (const_tree type, bool for_ctor_p)
5582 switch (TREE_CODE (type))
5584 case ARRAY_TYPE:
5586 tree nelts;
5588 nelts = array_type_nelts (type);
5589 if (nelts && tree_fits_uhwi_p (nelts))
5591 unsigned HOST_WIDE_INT n;
5593 n = tree_to_uhwi (nelts) + 1;
5594 if (n == 0 || for_ctor_p)
5595 return n;
5596 else
5597 return n * count_type_elements (TREE_TYPE (type), false);
5599 return for_ctor_p ? -1 : 1;
5602 case RECORD_TYPE:
5604 unsigned HOST_WIDE_INT n;
5605 tree f;
5607 n = 0;
5608 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5609 if (TREE_CODE (f) == FIELD_DECL)
5611 if (!for_ctor_p)
5612 n += count_type_elements (TREE_TYPE (f), false);
5613 else if (!flexible_array_member_p (f, type))
5614 /* Don't count flexible arrays, which are not supposed
5615 to be initialized. */
5616 n += 1;
5619 return n;
5622 case UNION_TYPE:
5623 case QUAL_UNION_TYPE:
5625 tree f;
5626 HOST_WIDE_INT n, m;
5628 gcc_assert (!for_ctor_p);
5629 /* Estimate the number of scalars in each field and pick the
5630 maximum. Other estimates would do instead; the idea is simply
5631 to make sure that the estimate is not sensitive to the ordering
5632 of the fields. */
5633 n = 1;
5634 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5635 if (TREE_CODE (f) == FIELD_DECL)
5637 m = count_type_elements (TREE_TYPE (f), false);
5638 /* If the field doesn't span the whole union, add an extra
5639 scalar for the rest. */
5640 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5641 TYPE_SIZE (type)) != 1)
5642 m++;
5643 if (n < m)
5644 n = m;
5646 return n;
5649 case COMPLEX_TYPE:
5650 return 2;
5652 case VECTOR_TYPE:
5653 return TYPE_VECTOR_SUBPARTS (type);
5655 case INTEGER_TYPE:
5656 case REAL_TYPE:
5657 case FIXED_POINT_TYPE:
5658 case ENUMERAL_TYPE:
5659 case BOOLEAN_TYPE:
5660 case POINTER_TYPE:
5661 case OFFSET_TYPE:
5662 case REFERENCE_TYPE:
5663 case NULLPTR_TYPE:
5664 return 1;
5666 case ERROR_MARK:
5667 return 0;
5669 case VOID_TYPE:
5670 case METHOD_TYPE:
5671 case FUNCTION_TYPE:
5672 case LANG_TYPE:
5673 default:
5674 gcc_unreachable ();
5678 /* Helper for categorize_ctor_elements. Identical interface. */
5680 static bool
5681 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5682 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5684 unsigned HOST_WIDE_INT idx;
5685 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5686 tree value, purpose, elt_type;
5688 /* Whether CTOR is a valid constant initializer, in accordance with what
5689 initializer_constant_valid_p does. If inferred from the constructor
5690 elements, true until proven otherwise. */
5691 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5692 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5694 nz_elts = 0;
5695 init_elts = 0;
5696 num_fields = 0;
5697 elt_type = NULL_TREE;
5699 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5701 HOST_WIDE_INT mult = 1;
5703 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5705 tree lo_index = TREE_OPERAND (purpose, 0);
5706 tree hi_index = TREE_OPERAND (purpose, 1);
5708 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5709 mult = (tree_to_uhwi (hi_index)
5710 - tree_to_uhwi (lo_index) + 1);
5712 num_fields += mult;
5713 elt_type = TREE_TYPE (value);
5715 switch (TREE_CODE (value))
5717 case CONSTRUCTOR:
5719 HOST_WIDE_INT nz = 0, ic = 0;
5721 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5722 p_complete);
5724 nz_elts += mult * nz;
5725 init_elts += mult * ic;
5727 if (const_from_elts_p && const_p)
5728 const_p = const_elt_p;
5730 break;
5732 case INTEGER_CST:
5733 case REAL_CST:
5734 case FIXED_CST:
5735 if (!initializer_zerop (value))
5736 nz_elts += mult;
5737 init_elts += mult;
5738 break;
5740 case STRING_CST:
5741 nz_elts += mult * TREE_STRING_LENGTH (value);
5742 init_elts += mult * TREE_STRING_LENGTH (value);
5743 break;
5745 case COMPLEX_CST:
5746 if (!initializer_zerop (TREE_REALPART (value)))
5747 nz_elts += mult;
5748 if (!initializer_zerop (TREE_IMAGPART (value)))
5749 nz_elts += mult;
5750 init_elts += mult;
5751 break;
5753 case VECTOR_CST:
5755 unsigned i;
5756 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5758 tree v = VECTOR_CST_ELT (value, i);
5759 if (!initializer_zerop (v))
5760 nz_elts += mult;
5761 init_elts += mult;
5764 break;
5766 default:
5768 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5769 nz_elts += mult * tc;
5770 init_elts += mult * tc;
5772 if (const_from_elts_p && const_p)
5773 const_p = initializer_constant_valid_p (value, elt_type)
5774 != NULL_TREE;
5776 break;
5780 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5781 num_fields, elt_type))
5782 *p_complete = false;
5784 *p_nz_elts += nz_elts;
5785 *p_init_elts += init_elts;
5787 return const_p;
5790 /* Examine CTOR to discover:
5791 * how many scalar fields are set to nonzero values,
5792 and place it in *P_NZ_ELTS;
5793 * how many scalar fields in total are in CTOR,
5794 and place it in *P_ELT_COUNT.
5795 * whether the constructor is complete -- in the sense that every
5796 meaningful byte is explicitly given a value --
5797 and place it in *P_COMPLETE.
5799 Return whether or not CTOR is a valid static constant initializer, the same
5800 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5802 bool
5803 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5804 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5806 *p_nz_elts = 0;
5807 *p_init_elts = 0;
5808 *p_complete = true;
5810 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5813 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5814 of which had type LAST_TYPE. Each element was itself a complete
5815 initializer, in the sense that every meaningful byte was explicitly
5816 given a value. Return true if the same is true for the constructor
5817 as a whole. */
5819 bool
5820 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5821 const_tree last_type)
5823 if (TREE_CODE (type) == UNION_TYPE
5824 || TREE_CODE (type) == QUAL_UNION_TYPE)
5826 if (num_elts == 0)
5827 return false;
5829 gcc_assert (num_elts == 1 && last_type);
5831 /* ??? We could look at each element of the union, and find the
5832 largest element. Which would avoid comparing the size of the
5833 initialized element against any tail padding in the union.
5834 Doesn't seem worth the effort... */
5835 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5838 return count_type_elements (type, true) == num_elts;
5841 /* Return 1 if EXP contains mostly (3/4) zeros. */
5843 static int
5844 mostly_zeros_p (const_tree exp)
5846 if (TREE_CODE (exp) == CONSTRUCTOR)
5848 HOST_WIDE_INT nz_elts, init_elts;
5849 bool complete_p;
5851 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5852 return !complete_p || nz_elts < init_elts / 4;
5855 return initializer_zerop (exp);
5858 /* Return 1 if EXP contains all zeros. */
5860 static int
5861 all_zeros_p (const_tree exp)
5863 if (TREE_CODE (exp) == CONSTRUCTOR)
5865 HOST_WIDE_INT nz_elts, init_elts;
5866 bool complete_p;
5868 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5869 return nz_elts == 0;
5872 return initializer_zerop (exp);
5875 /* Helper function for store_constructor.
5876 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5877 CLEARED is as for store_constructor.
5878 ALIAS_SET is the alias set to use for any stores.
5880 This provides a recursive shortcut back to store_constructor when it isn't
5881 necessary to go through store_field. This is so that we can pass through
5882 the cleared field to let store_constructor know that we may not have to
5883 clear a substructure if the outer structure has already been cleared. */
5885 static void
5886 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5887 HOST_WIDE_INT bitpos, machine_mode mode,
5888 tree exp, int cleared, alias_set_type alias_set)
5890 if (TREE_CODE (exp) == CONSTRUCTOR
5891 /* We can only call store_constructor recursively if the size and
5892 bit position are on a byte boundary. */
5893 && bitpos % BITS_PER_UNIT == 0
5894 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5895 /* If we have a nonzero bitpos for a register target, then we just
5896 let store_field do the bitfield handling. This is unlikely to
5897 generate unnecessary clear instructions anyways. */
5898 && (bitpos == 0 || MEM_P (target)))
5900 if (MEM_P (target))
5901 target
5902 = adjust_address (target,
5903 GET_MODE (target) == BLKmode
5904 || 0 != (bitpos
5905 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5906 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5909 /* Update the alias set, if required. */
5910 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5911 && MEM_ALIAS_SET (target) != 0)
5913 target = copy_rtx (target);
5914 set_mem_alias_set (target, alias_set);
5917 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5919 else
5920 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5924 /* Returns the number of FIELD_DECLs in TYPE. */
5926 static int
5927 fields_length (const_tree type)
5929 tree t = TYPE_FIELDS (type);
5930 int count = 0;
5932 for (; t; t = DECL_CHAIN (t))
5933 if (TREE_CODE (t) == FIELD_DECL)
5934 ++count;
5936 return count;
5940 /* Store the value of constructor EXP into the rtx TARGET.
5941 TARGET is either a REG or a MEM; we know it cannot conflict, since
5942 safe_from_p has been called.
5943 CLEARED is true if TARGET is known to have been zero'd.
5944 SIZE is the number of bytes of TARGET we are allowed to modify: this
5945 may not be the same as the size of EXP if we are assigning to a field
5946 which has been packed to exclude padding bits. */
5948 static void
5949 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5951 tree type = TREE_TYPE (exp);
5952 #ifdef WORD_REGISTER_OPERATIONS
5953 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5954 #endif
5956 switch (TREE_CODE (type))
5958 case RECORD_TYPE:
5959 case UNION_TYPE:
5960 case QUAL_UNION_TYPE:
5962 unsigned HOST_WIDE_INT idx;
5963 tree field, value;
5965 /* If size is zero or the target is already cleared, do nothing. */
5966 if (size == 0 || cleared)
5967 cleared = 1;
5968 /* We either clear the aggregate or indicate the value is dead. */
5969 else if ((TREE_CODE (type) == UNION_TYPE
5970 || TREE_CODE (type) == QUAL_UNION_TYPE)
5971 && ! CONSTRUCTOR_ELTS (exp))
5972 /* If the constructor is empty, clear the union. */
5974 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5975 cleared = 1;
5978 /* If we are building a static constructor into a register,
5979 set the initial value as zero so we can fold the value into
5980 a constant. But if more than one register is involved,
5981 this probably loses. */
5982 else if (REG_P (target) && TREE_STATIC (exp)
5983 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5985 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5986 cleared = 1;
5989 /* If the constructor has fewer fields than the structure or
5990 if we are initializing the structure to mostly zeros, clear
5991 the whole structure first. Don't do this if TARGET is a
5992 register whose mode size isn't equal to SIZE since
5993 clear_storage can't handle this case. */
5994 else if (size > 0
5995 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
5996 != fields_length (type))
5997 || mostly_zeros_p (exp))
5998 && (!REG_P (target)
5999 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6000 == size)))
6002 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6003 cleared = 1;
6006 if (REG_P (target) && !cleared)
6007 emit_clobber (target);
6009 /* Store each element of the constructor into the
6010 corresponding field of TARGET. */
6011 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6013 machine_mode mode;
6014 HOST_WIDE_INT bitsize;
6015 HOST_WIDE_INT bitpos = 0;
6016 tree offset;
6017 rtx to_rtx = target;
6019 /* Just ignore missing fields. We cleared the whole
6020 structure, above, if any fields are missing. */
6021 if (field == 0)
6022 continue;
6024 if (cleared && initializer_zerop (value))
6025 continue;
6027 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6028 bitsize = tree_to_uhwi (DECL_SIZE (field));
6029 else
6030 bitsize = -1;
6032 mode = DECL_MODE (field);
6033 if (DECL_BIT_FIELD (field))
6034 mode = VOIDmode;
6036 offset = DECL_FIELD_OFFSET (field);
6037 if (tree_fits_shwi_p (offset)
6038 && tree_fits_shwi_p (bit_position (field)))
6040 bitpos = int_bit_position (field);
6041 offset = 0;
6043 else
6044 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6046 if (offset)
6048 machine_mode address_mode;
6049 rtx offset_rtx;
6051 offset
6052 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6053 make_tree (TREE_TYPE (exp),
6054 target));
6056 offset_rtx = expand_normal (offset);
6057 gcc_assert (MEM_P (to_rtx));
6059 address_mode = get_address_mode (to_rtx);
6060 if (GET_MODE (offset_rtx) != address_mode)
6061 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6063 to_rtx = offset_address (to_rtx, offset_rtx,
6064 highest_pow2_factor (offset));
6067 #ifdef WORD_REGISTER_OPERATIONS
6068 /* If this initializes a field that is smaller than a
6069 word, at the start of a word, try to widen it to a full
6070 word. This special case allows us to output C++ member
6071 function initializations in a form that the optimizers
6072 can understand. */
6073 if (REG_P (target)
6074 && bitsize < BITS_PER_WORD
6075 && bitpos % BITS_PER_WORD == 0
6076 && GET_MODE_CLASS (mode) == MODE_INT
6077 && TREE_CODE (value) == INTEGER_CST
6078 && exp_size >= 0
6079 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6081 tree type = TREE_TYPE (value);
6083 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6085 type = lang_hooks.types.type_for_mode
6086 (word_mode, TYPE_UNSIGNED (type));
6087 value = fold_convert (type, value);
6090 if (BYTES_BIG_ENDIAN)
6091 value
6092 = fold_build2 (LSHIFT_EXPR, type, value,
6093 build_int_cst (type,
6094 BITS_PER_WORD - bitsize));
6095 bitsize = BITS_PER_WORD;
6096 mode = word_mode;
6098 #endif
6100 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6101 && DECL_NONADDRESSABLE_P (field))
6103 to_rtx = copy_rtx (to_rtx);
6104 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6107 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6108 value, cleared,
6109 get_alias_set (TREE_TYPE (field)));
6111 break;
6113 case ARRAY_TYPE:
6115 tree value, index;
6116 unsigned HOST_WIDE_INT i;
6117 int need_to_clear;
6118 tree domain;
6119 tree elttype = TREE_TYPE (type);
6120 int const_bounds_p;
6121 HOST_WIDE_INT minelt = 0;
6122 HOST_WIDE_INT maxelt = 0;
6124 domain = TYPE_DOMAIN (type);
6125 const_bounds_p = (TYPE_MIN_VALUE (domain)
6126 && TYPE_MAX_VALUE (domain)
6127 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6128 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6130 /* If we have constant bounds for the range of the type, get them. */
6131 if (const_bounds_p)
6133 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6134 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6137 /* If the constructor has fewer elements than the array, clear
6138 the whole array first. Similarly if this is static
6139 constructor of a non-BLKmode object. */
6140 if (cleared)
6141 need_to_clear = 0;
6142 else if (REG_P (target) && TREE_STATIC (exp))
6143 need_to_clear = 1;
6144 else
6146 unsigned HOST_WIDE_INT idx;
6147 tree index, value;
6148 HOST_WIDE_INT count = 0, zero_count = 0;
6149 need_to_clear = ! const_bounds_p;
6151 /* This loop is a more accurate version of the loop in
6152 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6153 is also needed to check for missing elements. */
6154 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6156 HOST_WIDE_INT this_node_count;
6158 if (need_to_clear)
6159 break;
6161 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6163 tree lo_index = TREE_OPERAND (index, 0);
6164 tree hi_index = TREE_OPERAND (index, 1);
6166 if (! tree_fits_uhwi_p (lo_index)
6167 || ! tree_fits_uhwi_p (hi_index))
6169 need_to_clear = 1;
6170 break;
6173 this_node_count = (tree_to_uhwi (hi_index)
6174 - tree_to_uhwi (lo_index) + 1);
6176 else
6177 this_node_count = 1;
6179 count += this_node_count;
6180 if (mostly_zeros_p (value))
6181 zero_count += this_node_count;
6184 /* Clear the entire array first if there are any missing
6185 elements, or if the incidence of zero elements is >=
6186 75%. */
6187 if (! need_to_clear
6188 && (count < maxelt - minelt + 1
6189 || 4 * zero_count >= 3 * count))
6190 need_to_clear = 1;
6193 if (need_to_clear && size > 0)
6195 if (REG_P (target))
6196 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6197 else
6198 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6199 cleared = 1;
6202 if (!cleared && REG_P (target))
6203 /* Inform later passes that the old value is dead. */
6204 emit_clobber (target);
6206 /* Store each element of the constructor into the
6207 corresponding element of TARGET, determined by counting the
6208 elements. */
6209 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6211 machine_mode mode;
6212 HOST_WIDE_INT bitsize;
6213 HOST_WIDE_INT bitpos;
6214 rtx xtarget = target;
6216 if (cleared && initializer_zerop (value))
6217 continue;
6219 mode = TYPE_MODE (elttype);
6220 if (mode == BLKmode)
6221 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6222 ? tree_to_uhwi (TYPE_SIZE (elttype))
6223 : -1);
6224 else
6225 bitsize = GET_MODE_BITSIZE (mode);
6227 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6229 tree lo_index = TREE_OPERAND (index, 0);
6230 tree hi_index = TREE_OPERAND (index, 1);
6231 rtx index_r, pos_rtx;
6232 HOST_WIDE_INT lo, hi, count;
6233 tree position;
6235 /* If the range is constant and "small", unroll the loop. */
6236 if (const_bounds_p
6237 && tree_fits_shwi_p (lo_index)
6238 && tree_fits_shwi_p (hi_index)
6239 && (lo = tree_to_shwi (lo_index),
6240 hi = tree_to_shwi (hi_index),
6241 count = hi - lo + 1,
6242 (!MEM_P (target)
6243 || count <= 2
6244 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6245 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6246 <= 40 * 8)))))
6248 lo -= minelt; hi -= minelt;
6249 for (; lo <= hi; lo++)
6251 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6253 if (MEM_P (target)
6254 && !MEM_KEEP_ALIAS_SET_P (target)
6255 && TREE_CODE (type) == ARRAY_TYPE
6256 && TYPE_NONALIASED_COMPONENT (type))
6258 target = copy_rtx (target);
6259 MEM_KEEP_ALIAS_SET_P (target) = 1;
6262 store_constructor_field
6263 (target, bitsize, bitpos, mode, value, cleared,
6264 get_alias_set (elttype));
6267 else
6269 rtx_code_label *loop_start = gen_label_rtx ();
6270 rtx_code_label *loop_end = gen_label_rtx ();
6271 tree exit_cond;
6273 expand_normal (hi_index);
6275 index = build_decl (EXPR_LOCATION (exp),
6276 VAR_DECL, NULL_TREE, domain);
6277 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6278 SET_DECL_RTL (index, index_r);
6279 store_expr (lo_index, index_r, 0, false);
6281 /* Build the head of the loop. */
6282 do_pending_stack_adjust ();
6283 emit_label (loop_start);
6285 /* Assign value to element index. */
6286 position =
6287 fold_convert (ssizetype,
6288 fold_build2 (MINUS_EXPR,
6289 TREE_TYPE (index),
6290 index,
6291 TYPE_MIN_VALUE (domain)));
6293 position =
6294 size_binop (MULT_EXPR, position,
6295 fold_convert (ssizetype,
6296 TYPE_SIZE_UNIT (elttype)));
6298 pos_rtx = expand_normal (position);
6299 xtarget = offset_address (target, pos_rtx,
6300 highest_pow2_factor (position));
6301 xtarget = adjust_address (xtarget, mode, 0);
6302 if (TREE_CODE (value) == CONSTRUCTOR)
6303 store_constructor (value, xtarget, cleared,
6304 bitsize / BITS_PER_UNIT);
6305 else
6306 store_expr (value, xtarget, 0, false);
6308 /* Generate a conditional jump to exit the loop. */
6309 exit_cond = build2 (LT_EXPR, integer_type_node,
6310 index, hi_index);
6311 jumpif (exit_cond, loop_end, -1);
6313 /* Update the loop counter, and jump to the head of
6314 the loop. */
6315 expand_assignment (index,
6316 build2 (PLUS_EXPR, TREE_TYPE (index),
6317 index, integer_one_node),
6318 false);
6320 emit_jump (loop_start);
6322 /* Build the end of the loop. */
6323 emit_label (loop_end);
6326 else if ((index != 0 && ! tree_fits_shwi_p (index))
6327 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6329 tree position;
6331 if (index == 0)
6332 index = ssize_int (1);
6334 if (minelt)
6335 index = fold_convert (ssizetype,
6336 fold_build2 (MINUS_EXPR,
6337 TREE_TYPE (index),
6338 index,
6339 TYPE_MIN_VALUE (domain)));
6341 position =
6342 size_binop (MULT_EXPR, index,
6343 fold_convert (ssizetype,
6344 TYPE_SIZE_UNIT (elttype)));
6345 xtarget = offset_address (target,
6346 expand_normal (position),
6347 highest_pow2_factor (position));
6348 xtarget = adjust_address (xtarget, mode, 0);
6349 store_expr (value, xtarget, 0, false);
6351 else
6353 if (index != 0)
6354 bitpos = ((tree_to_shwi (index) - minelt)
6355 * tree_to_uhwi (TYPE_SIZE (elttype)));
6356 else
6357 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6359 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6360 && TREE_CODE (type) == ARRAY_TYPE
6361 && TYPE_NONALIASED_COMPONENT (type))
6363 target = copy_rtx (target);
6364 MEM_KEEP_ALIAS_SET_P (target) = 1;
6366 store_constructor_field (target, bitsize, bitpos, mode, value,
6367 cleared, get_alias_set (elttype));
6370 break;
6373 case VECTOR_TYPE:
6375 unsigned HOST_WIDE_INT idx;
6376 constructor_elt *ce;
6377 int i;
6378 int need_to_clear;
6379 int icode = CODE_FOR_nothing;
6380 tree elttype = TREE_TYPE (type);
6381 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6382 machine_mode eltmode = TYPE_MODE (elttype);
6383 HOST_WIDE_INT bitsize;
6384 HOST_WIDE_INT bitpos;
6385 rtvec vector = NULL;
6386 unsigned n_elts;
6387 alias_set_type alias;
6389 gcc_assert (eltmode != BLKmode);
6391 n_elts = TYPE_VECTOR_SUBPARTS (type);
6392 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6394 machine_mode mode = GET_MODE (target);
6396 icode = (int) optab_handler (vec_init_optab, mode);
6397 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6398 if (icode != CODE_FOR_nothing)
6400 tree value;
6402 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6403 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6405 icode = CODE_FOR_nothing;
6406 break;
6409 if (icode != CODE_FOR_nothing)
6411 unsigned int i;
6413 vector = rtvec_alloc (n_elts);
6414 for (i = 0; i < n_elts; i++)
6415 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6419 /* If the constructor has fewer elements than the vector,
6420 clear the whole array first. Similarly if this is static
6421 constructor of a non-BLKmode object. */
6422 if (cleared)
6423 need_to_clear = 0;
6424 else if (REG_P (target) && TREE_STATIC (exp))
6425 need_to_clear = 1;
6426 else
6428 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6429 tree value;
6431 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6433 int n_elts_here = tree_to_uhwi
6434 (int_const_binop (TRUNC_DIV_EXPR,
6435 TYPE_SIZE (TREE_TYPE (value)),
6436 TYPE_SIZE (elttype)));
6438 count += n_elts_here;
6439 if (mostly_zeros_p (value))
6440 zero_count += n_elts_here;
6443 /* Clear the entire vector first if there are any missing elements,
6444 or if the incidence of zero elements is >= 75%. */
6445 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6448 if (need_to_clear && size > 0 && !vector)
6450 if (REG_P (target))
6451 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6452 else
6453 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6454 cleared = 1;
6457 /* Inform later passes that the old value is dead. */
6458 if (!cleared && !vector && REG_P (target))
6459 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6461 if (MEM_P (target))
6462 alias = MEM_ALIAS_SET (target);
6463 else
6464 alias = get_alias_set (elttype);
6466 /* Store each element of the constructor into the corresponding
6467 element of TARGET, determined by counting the elements. */
6468 for (idx = 0, i = 0;
6469 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6470 idx++, i += bitsize / elt_size)
6472 HOST_WIDE_INT eltpos;
6473 tree value = ce->value;
6475 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6476 if (cleared && initializer_zerop (value))
6477 continue;
6479 if (ce->index)
6480 eltpos = tree_to_uhwi (ce->index);
6481 else
6482 eltpos = i;
6484 if (vector)
6486 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6487 elements. */
6488 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6489 RTVEC_ELT (vector, eltpos)
6490 = expand_normal (value);
6492 else
6494 machine_mode value_mode =
6495 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6496 ? TYPE_MODE (TREE_TYPE (value))
6497 : eltmode;
6498 bitpos = eltpos * elt_size;
6499 store_constructor_field (target, bitsize, bitpos, value_mode,
6500 value, cleared, alias);
6504 if (vector)
6505 emit_insn (GEN_FCN (icode)
6506 (target,
6507 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6508 break;
6511 default:
6512 gcc_unreachable ();
6516 /* Store the value of EXP (an expression tree)
6517 into a subfield of TARGET which has mode MODE and occupies
6518 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6519 If MODE is VOIDmode, it means that we are storing into a bit-field.
6521 BITREGION_START is bitpos of the first bitfield in this region.
6522 BITREGION_END is the bitpos of the ending bitfield in this region.
6523 These two fields are 0, if the C++ memory model does not apply,
6524 or we are not interested in keeping track of bitfield regions.
6526 Always return const0_rtx unless we have something particular to
6527 return.
6529 ALIAS_SET is the alias set for the destination. This value will
6530 (in general) be different from that for TARGET, since TARGET is a
6531 reference to the containing structure.
6533 If NONTEMPORAL is true, try generating a nontemporal store. */
6535 static rtx
6536 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6537 unsigned HOST_WIDE_INT bitregion_start,
6538 unsigned HOST_WIDE_INT bitregion_end,
6539 machine_mode mode, tree exp,
6540 alias_set_type alias_set, bool nontemporal)
6542 if (TREE_CODE (exp) == ERROR_MARK)
6543 return const0_rtx;
6545 /* If we have nothing to store, do nothing unless the expression has
6546 side-effects. */
6547 if (bitsize == 0)
6548 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6550 if (GET_CODE (target) == CONCAT)
6552 /* We're storing into a struct containing a single __complex. */
6554 gcc_assert (!bitpos);
6555 return store_expr (exp, target, 0, nontemporal);
6558 /* If the structure is in a register or if the component
6559 is a bit field, we cannot use addressing to access it.
6560 Use bit-field techniques or SUBREG to store in it. */
6562 if (mode == VOIDmode
6563 || (mode != BLKmode && ! direct_store[(int) mode]
6564 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6565 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6566 || REG_P (target)
6567 || GET_CODE (target) == SUBREG
6568 /* If the field isn't aligned enough to store as an ordinary memref,
6569 store it as a bit field. */
6570 || (mode != BLKmode
6571 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6572 || bitpos % GET_MODE_ALIGNMENT (mode))
6573 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6574 || (bitpos % BITS_PER_UNIT != 0)))
6575 || (bitsize >= 0 && mode != BLKmode
6576 && GET_MODE_BITSIZE (mode) > bitsize)
6577 /* If the RHS and field are a constant size and the size of the
6578 RHS isn't the same size as the bitfield, we must use bitfield
6579 operations. */
6580 || (bitsize >= 0
6581 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6582 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6583 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6584 decl we must use bitfield operations. */
6585 || (bitsize >= 0
6586 && TREE_CODE (exp) == MEM_REF
6587 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6588 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6589 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6590 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6592 rtx temp;
6593 gimple nop_def;
6595 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6596 implies a mask operation. If the precision is the same size as
6597 the field we're storing into, that mask is redundant. This is
6598 particularly common with bit field assignments generated by the
6599 C front end. */
6600 nop_def = get_def_for_expr (exp, NOP_EXPR);
6601 if (nop_def)
6603 tree type = TREE_TYPE (exp);
6604 if (INTEGRAL_TYPE_P (type)
6605 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6606 && bitsize == TYPE_PRECISION (type))
6608 tree op = gimple_assign_rhs1 (nop_def);
6609 type = TREE_TYPE (op);
6610 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6611 exp = op;
6615 temp = expand_normal (exp);
6617 /* If BITSIZE is narrower than the size of the type of EXP
6618 we will be narrowing TEMP. Normally, what's wanted are the
6619 low-order bits. However, if EXP's type is a record and this is
6620 big-endian machine, we want the upper BITSIZE bits. */
6621 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6622 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6623 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6624 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6625 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6626 NULL_RTX, 1);
6628 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6629 if (mode != VOIDmode && mode != BLKmode
6630 && mode != TYPE_MODE (TREE_TYPE (exp)))
6631 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6633 /* If the modes of TEMP and TARGET are both BLKmode, both
6634 must be in memory and BITPOS must be aligned on a byte
6635 boundary. If so, we simply do a block copy. Likewise
6636 for a BLKmode-like TARGET. */
6637 if (GET_MODE (temp) == BLKmode
6638 && (GET_MODE (target) == BLKmode
6639 || (MEM_P (target)
6640 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6641 && (bitpos % BITS_PER_UNIT) == 0
6642 && (bitsize % BITS_PER_UNIT) == 0)))
6644 gcc_assert (MEM_P (target) && MEM_P (temp)
6645 && (bitpos % BITS_PER_UNIT) == 0);
6647 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6648 emit_block_move (target, temp,
6649 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6650 / BITS_PER_UNIT),
6651 BLOCK_OP_NORMAL);
6653 return const0_rtx;
6656 /* Handle calls that return values in multiple non-contiguous locations.
6657 The Irix 6 ABI has examples of this. */
6658 if (GET_CODE (temp) == PARALLEL)
6660 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6661 rtx temp_target;
6662 if (mode == BLKmode || mode == VOIDmode)
6663 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6664 temp_target = gen_reg_rtx (mode);
6665 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6666 temp = temp_target;
6668 else if (mode == BLKmode)
6670 /* Handle calls that return BLKmode values in registers. */
6671 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6673 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6674 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6675 temp = temp_target;
6677 else
6679 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6680 rtx temp_target;
6681 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6682 temp_target = gen_reg_rtx (mode);
6683 temp_target
6684 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6685 temp_target, mode, mode);
6686 temp = temp_target;
6690 /* Store the value in the bitfield. */
6691 store_bit_field (target, bitsize, bitpos,
6692 bitregion_start, bitregion_end,
6693 mode, temp);
6695 return const0_rtx;
6697 else
6699 /* Now build a reference to just the desired component. */
6700 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6702 if (to_rtx == target)
6703 to_rtx = copy_rtx (to_rtx);
6705 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6706 set_mem_alias_set (to_rtx, alias_set);
6708 return store_expr (exp, to_rtx, 0, nontemporal);
6712 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6713 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6714 codes and find the ultimate containing object, which we return.
6716 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6717 bit position, and *PUNSIGNEDP to the signedness of the field.
6718 If the position of the field is variable, we store a tree
6719 giving the variable offset (in units) in *POFFSET.
6720 This offset is in addition to the bit position.
6721 If the position is not variable, we store 0 in *POFFSET.
6723 If any of the extraction expressions is volatile,
6724 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6726 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6727 Otherwise, it is a mode that can be used to access the field.
6729 If the field describes a variable-sized object, *PMODE is set to
6730 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6731 this case, but the address of the object can be found.
6733 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6734 look through nodes that serve as markers of a greater alignment than
6735 the one that can be deduced from the expression. These nodes make it
6736 possible for front-ends to prevent temporaries from being created by
6737 the middle-end on alignment considerations. For that purpose, the
6738 normal operating mode at high-level is to always pass FALSE so that
6739 the ultimate containing object is really returned; moreover, the
6740 associated predicate handled_component_p will always return TRUE
6741 on these nodes, thus indicating that they are essentially handled
6742 by get_inner_reference. TRUE should only be passed when the caller
6743 is scanning the expression in order to build another representation
6744 and specifically knows how to handle these nodes; as such, this is
6745 the normal operating mode in the RTL expanders. */
6747 tree
6748 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6749 HOST_WIDE_INT *pbitpos, tree *poffset,
6750 machine_mode *pmode, int *punsignedp,
6751 int *pvolatilep, bool keep_aligning)
6753 tree size_tree = 0;
6754 machine_mode mode = VOIDmode;
6755 bool blkmode_bitfield = false;
6756 tree offset = size_zero_node;
6757 offset_int bit_offset = 0;
6759 /* First get the mode, signedness, and size. We do this from just the
6760 outermost expression. */
6761 *pbitsize = -1;
6762 if (TREE_CODE (exp) == COMPONENT_REF)
6764 tree field = TREE_OPERAND (exp, 1);
6765 size_tree = DECL_SIZE (field);
6766 if (flag_strict_volatile_bitfields > 0
6767 && TREE_THIS_VOLATILE (exp)
6768 && DECL_BIT_FIELD_TYPE (field)
6769 && DECL_MODE (field) != BLKmode)
6770 /* Volatile bitfields should be accessed in the mode of the
6771 field's type, not the mode computed based on the bit
6772 size. */
6773 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6774 else if (!DECL_BIT_FIELD (field))
6775 mode = DECL_MODE (field);
6776 else if (DECL_MODE (field) == BLKmode)
6777 blkmode_bitfield = true;
6779 *punsignedp = DECL_UNSIGNED (field);
6781 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6783 size_tree = TREE_OPERAND (exp, 1);
6784 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6785 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6787 /* For vector types, with the correct size of access, use the mode of
6788 inner type. */
6789 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6790 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6791 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6792 mode = TYPE_MODE (TREE_TYPE (exp));
6794 else
6796 mode = TYPE_MODE (TREE_TYPE (exp));
6797 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6799 if (mode == BLKmode)
6800 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6801 else
6802 *pbitsize = GET_MODE_BITSIZE (mode);
6805 if (size_tree != 0)
6807 if (! tree_fits_uhwi_p (size_tree))
6808 mode = BLKmode, *pbitsize = -1;
6809 else
6810 *pbitsize = tree_to_uhwi (size_tree);
6813 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6814 and find the ultimate containing object. */
6815 while (1)
6817 switch (TREE_CODE (exp))
6819 case BIT_FIELD_REF:
6820 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6821 break;
6823 case COMPONENT_REF:
6825 tree field = TREE_OPERAND (exp, 1);
6826 tree this_offset = component_ref_field_offset (exp);
6828 /* If this field hasn't been filled in yet, don't go past it.
6829 This should only happen when folding expressions made during
6830 type construction. */
6831 if (this_offset == 0)
6832 break;
6834 offset = size_binop (PLUS_EXPR, offset, this_offset);
6835 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6837 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6839 break;
6841 case ARRAY_REF:
6842 case ARRAY_RANGE_REF:
6844 tree index = TREE_OPERAND (exp, 1);
6845 tree low_bound = array_ref_low_bound (exp);
6846 tree unit_size = array_ref_element_size (exp);
6848 /* We assume all arrays have sizes that are a multiple of a byte.
6849 First subtract the lower bound, if any, in the type of the
6850 index, then convert to sizetype and multiply by the size of
6851 the array element. */
6852 if (! integer_zerop (low_bound))
6853 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6854 index, low_bound);
6856 offset = size_binop (PLUS_EXPR, offset,
6857 size_binop (MULT_EXPR,
6858 fold_convert (sizetype, index),
6859 unit_size));
6861 break;
6863 case REALPART_EXPR:
6864 break;
6866 case IMAGPART_EXPR:
6867 bit_offset += *pbitsize;
6868 break;
6870 case VIEW_CONVERT_EXPR:
6871 if (keep_aligning && STRICT_ALIGNMENT
6872 && (TYPE_ALIGN (TREE_TYPE (exp))
6873 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6874 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6875 < BIGGEST_ALIGNMENT)
6876 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6877 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6878 goto done;
6879 break;
6881 case MEM_REF:
6882 /* Hand back the decl for MEM[&decl, off]. */
6883 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6885 tree off = TREE_OPERAND (exp, 1);
6886 if (!integer_zerop (off))
6888 offset_int boff, coff = mem_ref_offset (exp);
6889 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6890 bit_offset += boff;
6892 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6894 goto done;
6896 default:
6897 goto done;
6900 /* If any reference in the chain is volatile, the effect is volatile. */
6901 if (TREE_THIS_VOLATILE (exp))
6902 *pvolatilep = 1;
6904 exp = TREE_OPERAND (exp, 0);
6906 done:
6908 /* If OFFSET is constant, see if we can return the whole thing as a
6909 constant bit position. Make sure to handle overflow during
6910 this conversion. */
6911 if (TREE_CODE (offset) == INTEGER_CST)
6913 offset_int tem = wi::sext (wi::to_offset (offset),
6914 TYPE_PRECISION (sizetype));
6915 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6916 tem += bit_offset;
6917 if (wi::fits_shwi_p (tem))
6919 *pbitpos = tem.to_shwi ();
6920 *poffset = offset = NULL_TREE;
6924 /* Otherwise, split it up. */
6925 if (offset)
6927 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6928 if (wi::neg_p (bit_offset))
6930 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6931 offset_int tem = bit_offset.and_not (mask);
6932 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6933 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6934 bit_offset -= tem;
6935 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6936 offset = size_binop (PLUS_EXPR, offset,
6937 wide_int_to_tree (sizetype, tem));
6940 *pbitpos = bit_offset.to_shwi ();
6941 *poffset = offset;
6944 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6945 if (mode == VOIDmode
6946 && blkmode_bitfield
6947 && (*pbitpos % BITS_PER_UNIT) == 0
6948 && (*pbitsize % BITS_PER_UNIT) == 0)
6949 *pmode = BLKmode;
6950 else
6951 *pmode = mode;
6953 return exp;
6956 /* Return a tree of sizetype representing the size, in bytes, of the element
6957 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6959 tree
6960 array_ref_element_size (tree exp)
6962 tree aligned_size = TREE_OPERAND (exp, 3);
6963 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6964 location_t loc = EXPR_LOCATION (exp);
6966 /* If a size was specified in the ARRAY_REF, it's the size measured
6967 in alignment units of the element type. So multiply by that value. */
6968 if (aligned_size)
6970 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6971 sizetype from another type of the same width and signedness. */
6972 if (TREE_TYPE (aligned_size) != sizetype)
6973 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6974 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6975 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6978 /* Otherwise, take the size from that of the element type. Substitute
6979 any PLACEHOLDER_EXPR that we have. */
6980 else
6981 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6984 /* Return a tree representing the lower bound of the array mentioned in
6985 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6987 tree
6988 array_ref_low_bound (tree exp)
6990 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6992 /* If a lower bound is specified in EXP, use it. */
6993 if (TREE_OPERAND (exp, 2))
6994 return TREE_OPERAND (exp, 2);
6996 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6997 substituting for a PLACEHOLDER_EXPR as needed. */
6998 if (domain_type && TYPE_MIN_VALUE (domain_type))
6999 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
7001 /* Otherwise, return a zero of the appropriate type. */
7002 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
7005 /* Returns true if REF is an array reference to an array at the end of
7006 a structure. If this is the case, the array may be allocated larger
7007 than its upper bound implies. */
7009 bool
7010 array_at_struct_end_p (tree ref)
7012 if (TREE_CODE (ref) != ARRAY_REF
7013 && TREE_CODE (ref) != ARRAY_RANGE_REF)
7014 return false;
7016 while (handled_component_p (ref))
7018 /* If the reference chain contains a component reference to a
7019 non-union type and there follows another field the reference
7020 is not at the end of a structure. */
7021 if (TREE_CODE (ref) == COMPONENT_REF
7022 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
7024 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
7025 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
7026 nextf = DECL_CHAIN (nextf);
7027 if (nextf)
7028 return false;
7031 ref = TREE_OPERAND (ref, 0);
7034 /* If the reference is based on a declared entity, the size of the array
7035 is constrained by its given domain. */
7036 if (DECL_P (ref))
7037 return false;
7039 return true;
7042 /* Return a tree representing the upper bound of the array mentioned in
7043 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
7045 tree
7046 array_ref_up_bound (tree exp)
7048 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
7050 /* If there is a domain type and it has an upper bound, use it, substituting
7051 for a PLACEHOLDER_EXPR as needed. */
7052 if (domain_type && TYPE_MAX_VALUE (domain_type))
7053 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
7055 /* Otherwise fail. */
7056 return NULL_TREE;
7059 /* Return a tree representing the offset, in bytes, of the field referenced
7060 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
7062 tree
7063 component_ref_field_offset (tree exp)
7065 tree aligned_offset = TREE_OPERAND (exp, 2);
7066 tree field = TREE_OPERAND (exp, 1);
7067 location_t loc = EXPR_LOCATION (exp);
7069 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
7070 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
7071 value. */
7072 if (aligned_offset)
7074 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7075 sizetype from another type of the same width and signedness. */
7076 if (TREE_TYPE (aligned_offset) != sizetype)
7077 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
7078 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
7079 size_int (DECL_OFFSET_ALIGN (field)
7080 / BITS_PER_UNIT));
7083 /* Otherwise, take the offset from that of the field. Substitute
7084 any PLACEHOLDER_EXPR that we have. */
7085 else
7086 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
7089 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7091 static unsigned HOST_WIDE_INT
7092 target_align (const_tree target)
7094 /* We might have a chain of nested references with intermediate misaligning
7095 bitfields components, so need to recurse to find out. */
7097 unsigned HOST_WIDE_INT this_align, outer_align;
7099 switch (TREE_CODE (target))
7101 case BIT_FIELD_REF:
7102 return 1;
7104 case COMPONENT_REF:
7105 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7106 outer_align = target_align (TREE_OPERAND (target, 0));
7107 return MIN (this_align, outer_align);
7109 case ARRAY_REF:
7110 case ARRAY_RANGE_REF:
7111 this_align = TYPE_ALIGN (TREE_TYPE (target));
7112 outer_align = target_align (TREE_OPERAND (target, 0));
7113 return MIN (this_align, outer_align);
7115 CASE_CONVERT:
7116 case NON_LVALUE_EXPR:
7117 case VIEW_CONVERT_EXPR:
7118 this_align = TYPE_ALIGN (TREE_TYPE (target));
7119 outer_align = target_align (TREE_OPERAND (target, 0));
7120 return MAX (this_align, outer_align);
7122 default:
7123 return TYPE_ALIGN (TREE_TYPE (target));
7128 /* Given an rtx VALUE that may contain additions and multiplications, return
7129 an equivalent value that just refers to a register, memory, or constant.
7130 This is done by generating instructions to perform the arithmetic and
7131 returning a pseudo-register containing the value.
7133 The returned value may be a REG, SUBREG, MEM or constant. */
7136 force_operand (rtx value, rtx target)
7138 rtx op1, op2;
7139 /* Use subtarget as the target for operand 0 of a binary operation. */
7140 rtx subtarget = get_subtarget (target);
7141 enum rtx_code code = GET_CODE (value);
7143 /* Check for subreg applied to an expression produced by loop optimizer. */
7144 if (code == SUBREG
7145 && !REG_P (SUBREG_REG (value))
7146 && !MEM_P (SUBREG_REG (value)))
7148 value
7149 = simplify_gen_subreg (GET_MODE (value),
7150 force_reg (GET_MODE (SUBREG_REG (value)),
7151 force_operand (SUBREG_REG (value),
7152 NULL_RTX)),
7153 GET_MODE (SUBREG_REG (value)),
7154 SUBREG_BYTE (value));
7155 code = GET_CODE (value);
7158 /* Check for a PIC address load. */
7159 if ((code == PLUS || code == MINUS)
7160 && XEXP (value, 0) == pic_offset_table_rtx
7161 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7162 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7163 || GET_CODE (XEXP (value, 1)) == CONST))
7165 if (!subtarget)
7166 subtarget = gen_reg_rtx (GET_MODE (value));
7167 emit_move_insn (subtarget, value);
7168 return subtarget;
7171 if (ARITHMETIC_P (value))
7173 op2 = XEXP (value, 1);
7174 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7175 subtarget = 0;
7176 if (code == MINUS && CONST_INT_P (op2))
7178 code = PLUS;
7179 op2 = negate_rtx (GET_MODE (value), op2);
7182 /* Check for an addition with OP2 a constant integer and our first
7183 operand a PLUS of a virtual register and something else. In that
7184 case, we want to emit the sum of the virtual register and the
7185 constant first and then add the other value. This allows virtual
7186 register instantiation to simply modify the constant rather than
7187 creating another one around this addition. */
7188 if (code == PLUS && CONST_INT_P (op2)
7189 && GET_CODE (XEXP (value, 0)) == PLUS
7190 && REG_P (XEXP (XEXP (value, 0), 0))
7191 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7192 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7194 rtx temp = expand_simple_binop (GET_MODE (value), code,
7195 XEXP (XEXP (value, 0), 0), op2,
7196 subtarget, 0, OPTAB_LIB_WIDEN);
7197 return expand_simple_binop (GET_MODE (value), code, temp,
7198 force_operand (XEXP (XEXP (value,
7199 0), 1), 0),
7200 target, 0, OPTAB_LIB_WIDEN);
7203 op1 = force_operand (XEXP (value, 0), subtarget);
7204 op2 = force_operand (op2, NULL_RTX);
7205 switch (code)
7207 case MULT:
7208 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7209 case DIV:
7210 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7211 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7212 target, 1, OPTAB_LIB_WIDEN);
7213 else
7214 return expand_divmod (0,
7215 FLOAT_MODE_P (GET_MODE (value))
7216 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7217 GET_MODE (value), op1, op2, target, 0);
7218 case MOD:
7219 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7220 target, 0);
7221 case UDIV:
7222 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7223 target, 1);
7224 case UMOD:
7225 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7226 target, 1);
7227 case ASHIFTRT:
7228 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7229 target, 0, OPTAB_LIB_WIDEN);
7230 default:
7231 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7232 target, 1, OPTAB_LIB_WIDEN);
7235 if (UNARY_P (value))
7237 if (!target)
7238 target = gen_reg_rtx (GET_MODE (value));
7239 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7240 switch (code)
7242 case ZERO_EXTEND:
7243 case SIGN_EXTEND:
7244 case TRUNCATE:
7245 case FLOAT_EXTEND:
7246 case FLOAT_TRUNCATE:
7247 convert_move (target, op1, code == ZERO_EXTEND);
7248 return target;
7250 case FIX:
7251 case UNSIGNED_FIX:
7252 expand_fix (target, op1, code == UNSIGNED_FIX);
7253 return target;
7255 case FLOAT:
7256 case UNSIGNED_FLOAT:
7257 expand_float (target, op1, code == UNSIGNED_FLOAT);
7258 return target;
7260 default:
7261 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7265 #ifdef INSN_SCHEDULING
7266 /* On machines that have insn scheduling, we want all memory reference to be
7267 explicit, so we need to deal with such paradoxical SUBREGs. */
7268 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7269 value
7270 = simplify_gen_subreg (GET_MODE (value),
7271 force_reg (GET_MODE (SUBREG_REG (value)),
7272 force_operand (SUBREG_REG (value),
7273 NULL_RTX)),
7274 GET_MODE (SUBREG_REG (value)),
7275 SUBREG_BYTE (value));
7276 #endif
7278 return value;
7281 /* Subroutine of expand_expr: return nonzero iff there is no way that
7282 EXP can reference X, which is being modified. TOP_P is nonzero if this
7283 call is going to be used to determine whether we need a temporary
7284 for EXP, as opposed to a recursive call to this function.
7286 It is always safe for this routine to return zero since it merely
7287 searches for optimization opportunities. */
7290 safe_from_p (const_rtx x, tree exp, int top_p)
7292 rtx exp_rtl = 0;
7293 int i, nops;
7295 if (x == 0
7296 /* If EXP has varying size, we MUST use a target since we currently
7297 have no way of allocating temporaries of variable size
7298 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7299 So we assume here that something at a higher level has prevented a
7300 clash. This is somewhat bogus, but the best we can do. Only
7301 do this when X is BLKmode and when we are at the top level. */
7302 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7303 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7304 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7305 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7306 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7307 != INTEGER_CST)
7308 && GET_MODE (x) == BLKmode)
7309 /* If X is in the outgoing argument area, it is always safe. */
7310 || (MEM_P (x)
7311 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7312 || (GET_CODE (XEXP (x, 0)) == PLUS
7313 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7314 return 1;
7316 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7317 find the underlying pseudo. */
7318 if (GET_CODE (x) == SUBREG)
7320 x = SUBREG_REG (x);
7321 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7322 return 0;
7325 /* Now look at our tree code and possibly recurse. */
7326 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7328 case tcc_declaration:
7329 exp_rtl = DECL_RTL_IF_SET (exp);
7330 break;
7332 case tcc_constant:
7333 return 1;
7335 case tcc_exceptional:
7336 if (TREE_CODE (exp) == TREE_LIST)
7338 while (1)
7340 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7341 return 0;
7342 exp = TREE_CHAIN (exp);
7343 if (!exp)
7344 return 1;
7345 if (TREE_CODE (exp) != TREE_LIST)
7346 return safe_from_p (x, exp, 0);
7349 else if (TREE_CODE (exp) == CONSTRUCTOR)
7351 constructor_elt *ce;
7352 unsigned HOST_WIDE_INT idx;
7354 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7355 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7356 || !safe_from_p (x, ce->value, 0))
7357 return 0;
7358 return 1;
7360 else if (TREE_CODE (exp) == ERROR_MARK)
7361 return 1; /* An already-visited SAVE_EXPR? */
7362 else
7363 return 0;
7365 case tcc_statement:
7366 /* The only case we look at here is the DECL_INITIAL inside a
7367 DECL_EXPR. */
7368 return (TREE_CODE (exp) != DECL_EXPR
7369 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7370 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7371 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7373 case tcc_binary:
7374 case tcc_comparison:
7375 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7376 return 0;
7377 /* Fall through. */
7379 case tcc_unary:
7380 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7382 case tcc_expression:
7383 case tcc_reference:
7384 case tcc_vl_exp:
7385 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7386 the expression. If it is set, we conflict iff we are that rtx or
7387 both are in memory. Otherwise, we check all operands of the
7388 expression recursively. */
7390 switch (TREE_CODE (exp))
7392 case ADDR_EXPR:
7393 /* If the operand is static or we are static, we can't conflict.
7394 Likewise if we don't conflict with the operand at all. */
7395 if (staticp (TREE_OPERAND (exp, 0))
7396 || TREE_STATIC (exp)
7397 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7398 return 1;
7400 /* Otherwise, the only way this can conflict is if we are taking
7401 the address of a DECL a that address if part of X, which is
7402 very rare. */
7403 exp = TREE_OPERAND (exp, 0);
7404 if (DECL_P (exp))
7406 if (!DECL_RTL_SET_P (exp)
7407 || !MEM_P (DECL_RTL (exp)))
7408 return 0;
7409 else
7410 exp_rtl = XEXP (DECL_RTL (exp), 0);
7412 break;
7414 case MEM_REF:
7415 if (MEM_P (x)
7416 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7417 get_alias_set (exp)))
7418 return 0;
7419 break;
7421 case CALL_EXPR:
7422 /* Assume that the call will clobber all hard registers and
7423 all of memory. */
7424 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7425 || MEM_P (x))
7426 return 0;
7427 break;
7429 case WITH_CLEANUP_EXPR:
7430 case CLEANUP_POINT_EXPR:
7431 /* Lowered by gimplify.c. */
7432 gcc_unreachable ();
7434 case SAVE_EXPR:
7435 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7437 default:
7438 break;
7441 /* If we have an rtx, we do not need to scan our operands. */
7442 if (exp_rtl)
7443 break;
7445 nops = TREE_OPERAND_LENGTH (exp);
7446 for (i = 0; i < nops; i++)
7447 if (TREE_OPERAND (exp, i) != 0
7448 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7449 return 0;
7451 break;
7453 case tcc_type:
7454 /* Should never get a type here. */
7455 gcc_unreachable ();
7458 /* If we have an rtl, find any enclosed object. Then see if we conflict
7459 with it. */
7460 if (exp_rtl)
7462 if (GET_CODE (exp_rtl) == SUBREG)
7464 exp_rtl = SUBREG_REG (exp_rtl);
7465 if (REG_P (exp_rtl)
7466 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7467 return 0;
7470 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7471 are memory and they conflict. */
7472 return ! (rtx_equal_p (x, exp_rtl)
7473 || (MEM_P (x) && MEM_P (exp_rtl)
7474 && true_dependence (exp_rtl, VOIDmode, x)));
7477 /* If we reach here, it is safe. */
7478 return 1;
7482 /* Return the highest power of two that EXP is known to be a multiple of.
7483 This is used in updating alignment of MEMs in array references. */
7485 unsigned HOST_WIDE_INT
7486 highest_pow2_factor (const_tree exp)
7488 unsigned HOST_WIDE_INT ret;
7489 int trailing_zeros = tree_ctz (exp);
7490 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7491 return BIGGEST_ALIGNMENT;
7492 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7493 if (ret > BIGGEST_ALIGNMENT)
7494 return BIGGEST_ALIGNMENT;
7495 return ret;
7498 /* Similar, except that the alignment requirements of TARGET are
7499 taken into account. Assume it is at least as aligned as its
7500 type, unless it is a COMPONENT_REF in which case the layout of
7501 the structure gives the alignment. */
7503 static unsigned HOST_WIDE_INT
7504 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7506 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7507 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7509 return MAX (factor, talign);
7512 #ifdef HAVE_conditional_move
7513 /* Convert the tree comparison code TCODE to the rtl one where the
7514 signedness is UNSIGNEDP. */
7516 static enum rtx_code
7517 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7519 enum rtx_code code;
7520 switch (tcode)
7522 case EQ_EXPR:
7523 code = EQ;
7524 break;
7525 case NE_EXPR:
7526 code = NE;
7527 break;
7528 case LT_EXPR:
7529 code = unsignedp ? LTU : LT;
7530 break;
7531 case LE_EXPR:
7532 code = unsignedp ? LEU : LE;
7533 break;
7534 case GT_EXPR:
7535 code = unsignedp ? GTU : GT;
7536 break;
7537 case GE_EXPR:
7538 code = unsignedp ? GEU : GE;
7539 break;
7540 case UNORDERED_EXPR:
7541 code = UNORDERED;
7542 break;
7543 case ORDERED_EXPR:
7544 code = ORDERED;
7545 break;
7546 case UNLT_EXPR:
7547 code = UNLT;
7548 break;
7549 case UNLE_EXPR:
7550 code = UNLE;
7551 break;
7552 case UNGT_EXPR:
7553 code = UNGT;
7554 break;
7555 case UNGE_EXPR:
7556 code = UNGE;
7557 break;
7558 case UNEQ_EXPR:
7559 code = UNEQ;
7560 break;
7561 case LTGT_EXPR:
7562 code = LTGT;
7563 break;
7565 default:
7566 gcc_unreachable ();
7568 return code;
7570 #endif
7572 /* Subroutine of expand_expr. Expand the two operands of a binary
7573 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7574 The value may be stored in TARGET if TARGET is nonzero. The
7575 MODIFIER argument is as documented by expand_expr. */
7577 static void
7578 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7579 enum expand_modifier modifier)
7581 if (! safe_from_p (target, exp1, 1))
7582 target = 0;
7583 if (operand_equal_p (exp0, exp1, 0))
7585 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7586 *op1 = copy_rtx (*op0);
7588 else
7590 /* If we need to preserve evaluation order, copy exp0 into its own
7591 temporary variable so that it can't be clobbered by exp1. */
7592 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7593 exp0 = save_expr (exp0);
7594 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7595 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7600 /* Return a MEM that contains constant EXP. DEFER is as for
7601 output_constant_def and MODIFIER is as for expand_expr. */
7603 static rtx
7604 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7606 rtx mem;
7608 mem = output_constant_def (exp, defer);
7609 if (modifier != EXPAND_INITIALIZER)
7610 mem = use_anchored_address (mem);
7611 return mem;
7614 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7615 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7617 static rtx
7618 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7619 enum expand_modifier modifier, addr_space_t as)
7621 rtx result, subtarget;
7622 tree inner, offset;
7623 HOST_WIDE_INT bitsize, bitpos;
7624 int volatilep, unsignedp;
7625 machine_mode mode1;
7627 /* If we are taking the address of a constant and are at the top level,
7628 we have to use output_constant_def since we can't call force_const_mem
7629 at top level. */
7630 /* ??? This should be considered a front-end bug. We should not be
7631 generating ADDR_EXPR of something that isn't an LVALUE. The only
7632 exception here is STRING_CST. */
7633 if (CONSTANT_CLASS_P (exp))
7635 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7636 if (modifier < EXPAND_SUM)
7637 result = force_operand (result, target);
7638 return result;
7641 /* Everything must be something allowed by is_gimple_addressable. */
7642 switch (TREE_CODE (exp))
7644 case INDIRECT_REF:
7645 /* This case will happen via recursion for &a->b. */
7646 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7648 case MEM_REF:
7650 tree tem = TREE_OPERAND (exp, 0);
7651 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7652 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7653 return expand_expr (tem, target, tmode, modifier);
7656 case CONST_DECL:
7657 /* Expand the initializer like constants above. */
7658 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7659 0, modifier), 0);
7660 if (modifier < EXPAND_SUM)
7661 result = force_operand (result, target);
7662 return result;
7664 case REALPART_EXPR:
7665 /* The real part of the complex number is always first, therefore
7666 the address is the same as the address of the parent object. */
7667 offset = 0;
7668 bitpos = 0;
7669 inner = TREE_OPERAND (exp, 0);
7670 break;
7672 case IMAGPART_EXPR:
7673 /* The imaginary part of the complex number is always second.
7674 The expression is therefore always offset by the size of the
7675 scalar type. */
7676 offset = 0;
7677 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7678 inner = TREE_OPERAND (exp, 0);
7679 break;
7681 case COMPOUND_LITERAL_EXPR:
7682 /* Allow COMPOUND_LITERAL_EXPR in initializers, if e.g.
7683 rtl_for_decl_init is called on DECL_INITIAL with
7684 COMPOUNT_LITERAL_EXPRs in it, they aren't gimplified. */
7685 if (modifier == EXPAND_INITIALIZER
7686 && COMPOUND_LITERAL_EXPR_DECL (exp))
7687 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7688 target, tmode, modifier, as);
7689 /* FALLTHRU */
7690 default:
7691 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7692 expand_expr, as that can have various side effects; LABEL_DECLs for
7693 example, may not have their DECL_RTL set yet. Expand the rtl of
7694 CONSTRUCTORs too, which should yield a memory reference for the
7695 constructor's contents. Assume language specific tree nodes can
7696 be expanded in some interesting way. */
7697 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7698 if (DECL_P (exp)
7699 || TREE_CODE (exp) == CONSTRUCTOR
7700 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7702 result = expand_expr (exp, target, tmode,
7703 modifier == EXPAND_INITIALIZER
7704 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7706 /* If the DECL isn't in memory, then the DECL wasn't properly
7707 marked TREE_ADDRESSABLE, which will be either a front-end
7708 or a tree optimizer bug. */
7710 if (TREE_ADDRESSABLE (exp)
7711 && ! MEM_P (result)
7712 && ! targetm.calls.allocate_stack_slots_for_args ())
7714 error ("local frame unavailable (naked function?)");
7715 return result;
7717 else
7718 gcc_assert (MEM_P (result));
7719 result = XEXP (result, 0);
7721 /* ??? Is this needed anymore? */
7722 if (DECL_P (exp))
7723 TREE_USED (exp) = 1;
7725 if (modifier != EXPAND_INITIALIZER
7726 && modifier != EXPAND_CONST_ADDRESS
7727 && modifier != EXPAND_SUM)
7728 result = force_operand (result, target);
7729 return result;
7732 /* Pass FALSE as the last argument to get_inner_reference although
7733 we are expanding to RTL. The rationale is that we know how to
7734 handle "aligning nodes" here: we can just bypass them because
7735 they won't change the final object whose address will be returned
7736 (they actually exist only for that purpose). */
7737 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7738 &mode1, &unsignedp, &volatilep, false);
7739 break;
7742 /* We must have made progress. */
7743 gcc_assert (inner != exp);
7745 subtarget = offset || bitpos ? NULL_RTX : target;
7746 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7747 inner alignment, force the inner to be sufficiently aligned. */
7748 if (CONSTANT_CLASS_P (inner)
7749 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7751 inner = copy_node (inner);
7752 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7753 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7754 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7756 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7758 if (offset)
7760 rtx tmp;
7762 if (modifier != EXPAND_NORMAL)
7763 result = force_operand (result, NULL);
7764 tmp = expand_expr (offset, NULL_RTX, tmode,
7765 modifier == EXPAND_INITIALIZER
7766 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7768 /* expand_expr is allowed to return an object in a mode other
7769 than TMODE. If it did, we need to convert. */
7770 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7771 tmp = convert_modes (tmode, GET_MODE (tmp),
7772 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7773 result = convert_memory_address_addr_space (tmode, result, as);
7774 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7776 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7777 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7778 else
7780 subtarget = bitpos ? NULL_RTX : target;
7781 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7782 1, OPTAB_LIB_WIDEN);
7786 if (bitpos)
7788 /* Someone beforehand should have rejected taking the address
7789 of such an object. */
7790 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7792 result = convert_memory_address_addr_space (tmode, result, as);
7793 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7794 if (modifier < EXPAND_SUM)
7795 result = force_operand (result, target);
7798 return result;
7801 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7802 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7804 static rtx
7805 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7806 enum expand_modifier modifier)
7808 addr_space_t as = ADDR_SPACE_GENERIC;
7809 machine_mode address_mode = Pmode;
7810 machine_mode pointer_mode = ptr_mode;
7811 machine_mode rmode;
7812 rtx result;
7814 /* Target mode of VOIDmode says "whatever's natural". */
7815 if (tmode == VOIDmode)
7816 tmode = TYPE_MODE (TREE_TYPE (exp));
7818 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7820 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7821 address_mode = targetm.addr_space.address_mode (as);
7822 pointer_mode = targetm.addr_space.pointer_mode (as);
7825 /* We can get called with some Weird Things if the user does silliness
7826 like "(short) &a". In that case, convert_memory_address won't do
7827 the right thing, so ignore the given target mode. */
7828 if (tmode != address_mode && tmode != pointer_mode)
7829 tmode = address_mode;
7831 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7832 tmode, modifier, as);
7834 /* Despite expand_expr claims concerning ignoring TMODE when not
7835 strictly convenient, stuff breaks if we don't honor it. Note
7836 that combined with the above, we only do this for pointer modes. */
7837 rmode = GET_MODE (result);
7838 if (rmode == VOIDmode)
7839 rmode = tmode;
7840 if (rmode != tmode)
7841 result = convert_memory_address_addr_space (tmode, result, as);
7843 return result;
7846 /* Generate code for computing CONSTRUCTOR EXP.
7847 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7848 is TRUE, instead of creating a temporary variable in memory
7849 NULL is returned and the caller needs to handle it differently. */
7851 static rtx
7852 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7853 bool avoid_temp_mem)
7855 tree type = TREE_TYPE (exp);
7856 machine_mode mode = TYPE_MODE (type);
7858 /* Try to avoid creating a temporary at all. This is possible
7859 if all of the initializer is zero.
7860 FIXME: try to handle all [0..255] initializers we can handle
7861 with memset. */
7862 if (TREE_STATIC (exp)
7863 && !TREE_ADDRESSABLE (exp)
7864 && target != 0 && mode == BLKmode
7865 && all_zeros_p (exp))
7867 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7868 return target;
7871 /* All elts simple constants => refer to a constant in memory. But
7872 if this is a non-BLKmode mode, let it store a field at a time
7873 since that should make a CONST_INT, CONST_WIDE_INT or
7874 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7875 use, it is best to store directly into the target unless the type
7876 is large enough that memcpy will be used. If we are making an
7877 initializer and all operands are constant, put it in memory as
7878 well.
7880 FIXME: Avoid trying to fill vector constructors piece-meal.
7881 Output them with output_constant_def below unless we're sure
7882 they're zeros. This should go away when vector initializers
7883 are treated like VECTOR_CST instead of arrays. */
7884 if ((TREE_STATIC (exp)
7885 && ((mode == BLKmode
7886 && ! (target != 0 && safe_from_p (target, exp, 1)))
7887 || TREE_ADDRESSABLE (exp)
7888 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7889 && (! can_move_by_pieces
7890 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7891 TYPE_ALIGN (type)))
7892 && ! mostly_zeros_p (exp))))
7893 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7894 && TREE_CONSTANT (exp)))
7896 rtx constructor;
7898 if (avoid_temp_mem)
7899 return NULL_RTX;
7901 constructor = expand_expr_constant (exp, 1, modifier);
7903 if (modifier != EXPAND_CONST_ADDRESS
7904 && modifier != EXPAND_INITIALIZER
7905 && modifier != EXPAND_SUM)
7906 constructor = validize_mem (constructor);
7908 return constructor;
7911 /* Handle calls that pass values in multiple non-contiguous
7912 locations. The Irix 6 ABI has examples of this. */
7913 if (target == 0 || ! safe_from_p (target, exp, 1)
7914 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7916 if (avoid_temp_mem)
7917 return NULL_RTX;
7919 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7922 store_constructor (exp, target, 0, int_expr_size (exp));
7923 return target;
7927 /* expand_expr: generate code for computing expression EXP.
7928 An rtx for the computed value is returned. The value is never null.
7929 In the case of a void EXP, const0_rtx is returned.
7931 The value may be stored in TARGET if TARGET is nonzero.
7932 TARGET is just a suggestion; callers must assume that
7933 the rtx returned may not be the same as TARGET.
7935 If TARGET is CONST0_RTX, it means that the value will be ignored.
7937 If TMODE is not VOIDmode, it suggests generating the
7938 result in mode TMODE. But this is done only when convenient.
7939 Otherwise, TMODE is ignored and the value generated in its natural mode.
7940 TMODE is just a suggestion; callers must assume that
7941 the rtx returned may not have mode TMODE.
7943 Note that TARGET may have neither TMODE nor MODE. In that case, it
7944 probably will not be used.
7946 If MODIFIER is EXPAND_SUM then when EXP is an addition
7947 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7948 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7949 products as above, or REG or MEM, or constant.
7950 Ordinarily in such cases we would output mul or add instructions
7951 and then return a pseudo reg containing the sum.
7953 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7954 it also marks a label as absolutely required (it can't be dead).
7955 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7956 This is used for outputting expressions used in initializers.
7958 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7959 with a constant address even if that address is not normally legitimate.
7960 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7962 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7963 a call parameter. Such targets require special care as we haven't yet
7964 marked TARGET so that it's safe from being trashed by libcalls. We
7965 don't want to use TARGET for anything but the final result;
7966 Intermediate values must go elsewhere. Additionally, calls to
7967 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7969 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7970 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7971 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7972 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7973 recursively.
7975 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7976 In this case, we don't adjust a returned MEM rtx that wouldn't be
7977 sufficiently aligned for its mode; instead, it's up to the caller
7978 to deal with it afterwards. This is used to make sure that unaligned
7979 base objects for which out-of-bounds accesses are supported, for
7980 example record types with trailing arrays, aren't realigned behind
7981 the back of the caller.
7982 The normal operating mode is to pass FALSE for this parameter. */
7985 expand_expr_real (tree exp, rtx target, machine_mode tmode,
7986 enum expand_modifier modifier, rtx *alt_rtl,
7987 bool inner_reference_p)
7989 rtx ret;
7991 /* Handle ERROR_MARK before anybody tries to access its type. */
7992 if (TREE_CODE (exp) == ERROR_MARK
7993 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7995 ret = CONST0_RTX (tmode);
7996 return ret ? ret : const0_rtx;
7999 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8000 inner_reference_p);
8001 return ret;
8004 /* Try to expand the conditional expression which is represented by
8005 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
8006 return the rtl reg which repsents the result. Otherwise return
8007 NULL_RTL. */
8009 static rtx
8010 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8011 tree treeop1 ATTRIBUTE_UNUSED,
8012 tree treeop2 ATTRIBUTE_UNUSED)
8014 #ifdef HAVE_conditional_move
8015 rtx insn;
8016 rtx op00, op01, op1, op2;
8017 enum rtx_code comparison_code;
8018 machine_mode comparison_mode;
8019 gimple srcstmt;
8020 rtx temp;
8021 tree type = TREE_TYPE (treeop1);
8022 int unsignedp = TYPE_UNSIGNED (type);
8023 machine_mode mode = TYPE_MODE (type);
8024 machine_mode orig_mode = mode;
8026 /* If we cannot do a conditional move on the mode, try doing it
8027 with the promoted mode. */
8028 if (!can_conditionally_move_p (mode))
8030 mode = promote_mode (type, mode, &unsignedp);
8031 if (!can_conditionally_move_p (mode))
8032 return NULL_RTX;
8033 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8035 else
8036 temp = assign_temp (type, 0, 1);
8038 start_sequence ();
8039 expand_operands (treeop1, treeop2,
8040 temp, &op1, &op2, EXPAND_NORMAL);
8042 if (TREE_CODE (treeop0) == SSA_NAME
8043 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8045 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8046 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8047 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8048 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8049 comparison_mode = TYPE_MODE (type);
8050 unsignedp = TYPE_UNSIGNED (type);
8051 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8053 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
8055 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8056 enum tree_code cmpcode = TREE_CODE (treeop0);
8057 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8058 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8059 unsignedp = TYPE_UNSIGNED (type);
8060 comparison_mode = TYPE_MODE (type);
8061 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8063 else
8065 op00 = expand_normal (treeop0);
8066 op01 = const0_rtx;
8067 comparison_code = NE;
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_RSHIFT_EXPR:
9148 target = expand_vec_shift_expr (ops, target);
9149 return target;
9152 case VEC_UNPACK_HI_EXPR:
9153 case VEC_UNPACK_LO_EXPR:
9155 op0 = expand_normal (treeop0);
9156 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9157 target, unsignedp);
9158 gcc_assert (temp);
9159 return temp;
9162 case VEC_UNPACK_FLOAT_HI_EXPR:
9163 case VEC_UNPACK_FLOAT_LO_EXPR:
9165 op0 = expand_normal (treeop0);
9166 /* The signedness is determined from input operand. */
9167 temp = expand_widen_pattern_expr
9168 (ops, op0, NULL_RTX, NULL_RTX,
9169 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9171 gcc_assert (temp);
9172 return temp;
9175 case VEC_WIDEN_MULT_HI_EXPR:
9176 case VEC_WIDEN_MULT_LO_EXPR:
9177 case VEC_WIDEN_MULT_EVEN_EXPR:
9178 case VEC_WIDEN_MULT_ODD_EXPR:
9179 case VEC_WIDEN_LSHIFT_HI_EXPR:
9180 case VEC_WIDEN_LSHIFT_LO_EXPR:
9181 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9182 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9183 target, unsignedp);
9184 gcc_assert (target);
9185 return target;
9187 case VEC_PACK_TRUNC_EXPR:
9188 case VEC_PACK_SAT_EXPR:
9189 case VEC_PACK_FIX_TRUNC_EXPR:
9190 mode = TYPE_MODE (TREE_TYPE (treeop0));
9191 goto binop;
9193 case VEC_PERM_EXPR:
9194 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9195 op2 = expand_normal (treeop2);
9197 /* Careful here: if the target doesn't support integral vector modes,
9198 a constant selection vector could wind up smooshed into a normal
9199 integral constant. */
9200 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9202 tree sel_type = TREE_TYPE (treeop2);
9203 machine_mode vmode
9204 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9205 TYPE_VECTOR_SUBPARTS (sel_type));
9206 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9207 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9208 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9210 else
9211 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9213 temp = expand_vec_perm (mode, op0, op1, op2, target);
9214 gcc_assert (temp);
9215 return temp;
9217 case DOT_PROD_EXPR:
9219 tree oprnd0 = treeop0;
9220 tree oprnd1 = treeop1;
9221 tree oprnd2 = treeop2;
9222 rtx op2;
9224 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9225 op2 = expand_normal (oprnd2);
9226 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9227 target, unsignedp);
9228 return target;
9231 case SAD_EXPR:
9233 tree oprnd0 = treeop0;
9234 tree oprnd1 = treeop1;
9235 tree oprnd2 = treeop2;
9236 rtx op2;
9238 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9239 op2 = expand_normal (oprnd2);
9240 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9241 target, unsignedp);
9242 return target;
9245 case REALIGN_LOAD_EXPR:
9247 tree oprnd0 = treeop0;
9248 tree oprnd1 = treeop1;
9249 tree oprnd2 = treeop2;
9250 rtx op2;
9252 this_optab = optab_for_tree_code (code, type, optab_default);
9253 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9254 op2 = expand_normal (oprnd2);
9255 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9256 target, unsignedp);
9257 gcc_assert (temp);
9258 return temp;
9261 case COND_EXPR:
9262 /* A COND_EXPR with its type being VOID_TYPE represents a
9263 conditional jump and is handled in
9264 expand_gimple_cond_expr. */
9265 gcc_assert (!VOID_TYPE_P (type));
9267 /* Note that COND_EXPRs whose type is a structure or union
9268 are required to be constructed to contain assignments of
9269 a temporary variable, so that we can evaluate them here
9270 for side effect only. If type is void, we must do likewise. */
9272 gcc_assert (!TREE_ADDRESSABLE (type)
9273 && !ignore
9274 && TREE_TYPE (treeop1) != void_type_node
9275 && TREE_TYPE (treeop2) != void_type_node);
9277 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9278 if (temp)
9279 return temp;
9281 /* If we are not to produce a result, we have no target. Otherwise,
9282 if a target was specified use it; it will not be used as an
9283 intermediate target unless it is safe. If no target, use a
9284 temporary. */
9286 if (modifier != EXPAND_STACK_PARM
9287 && original_target
9288 && safe_from_p (original_target, treeop0, 1)
9289 && GET_MODE (original_target) == mode
9290 && !MEM_P (original_target))
9291 temp = original_target;
9292 else
9293 temp = assign_temp (type, 0, 1);
9295 do_pending_stack_adjust ();
9296 NO_DEFER_POP;
9297 op0 = gen_label_rtx ();
9298 op1 = gen_label_rtx ();
9299 jumpifnot (treeop0, op0, -1);
9300 store_expr (treeop1, temp,
9301 modifier == EXPAND_STACK_PARM,
9302 false);
9304 emit_jump_insn (gen_jump (op1));
9305 emit_barrier ();
9306 emit_label (op0);
9307 store_expr (treeop2, temp,
9308 modifier == EXPAND_STACK_PARM,
9309 false);
9311 emit_label (op1);
9312 OK_DEFER_POP;
9313 return temp;
9315 case VEC_COND_EXPR:
9316 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9317 return target;
9319 default:
9320 gcc_unreachable ();
9323 /* Here to do an ordinary binary operator. */
9324 binop:
9325 expand_operands (treeop0, treeop1,
9326 subtarget, &op0, &op1, EXPAND_NORMAL);
9327 binop2:
9328 this_optab = optab_for_tree_code (code, type, optab_default);
9329 binop3:
9330 if (modifier == EXPAND_STACK_PARM)
9331 target = 0;
9332 temp = expand_binop (mode, this_optab, op0, op1, target,
9333 unsignedp, OPTAB_LIB_WIDEN);
9334 gcc_assert (temp);
9335 /* Bitwise operations do not need bitfield reduction as we expect their
9336 operands being properly truncated. */
9337 if (code == BIT_XOR_EXPR
9338 || code == BIT_AND_EXPR
9339 || code == BIT_IOR_EXPR)
9340 return temp;
9341 return REDUCE_BIT_FIELD (temp);
9343 #undef REDUCE_BIT_FIELD
9346 /* Return TRUE if expression STMT is suitable for replacement.
9347 Never consider memory loads as replaceable, because those don't ever lead
9348 into constant expressions. */
9350 static bool
9351 stmt_is_replaceable_p (gimple stmt)
9353 if (ssa_is_replaceable_p (stmt))
9355 /* Don't move around loads. */
9356 if (!gimple_assign_single_p (stmt)
9357 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9358 return true;
9360 return false;
9364 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9365 enum expand_modifier modifier, rtx *alt_rtl,
9366 bool inner_reference_p)
9368 rtx op0, op1, temp, decl_rtl;
9369 tree type;
9370 int unsignedp;
9371 machine_mode mode;
9372 enum tree_code code = TREE_CODE (exp);
9373 rtx subtarget, original_target;
9374 int ignore;
9375 tree context;
9376 bool reduce_bit_field;
9377 location_t loc = EXPR_LOCATION (exp);
9378 struct separate_ops ops;
9379 tree treeop0, treeop1, treeop2;
9380 tree ssa_name = NULL_TREE;
9381 gimple g;
9383 type = TREE_TYPE (exp);
9384 mode = TYPE_MODE (type);
9385 unsignedp = TYPE_UNSIGNED (type);
9387 treeop0 = treeop1 = treeop2 = NULL_TREE;
9388 if (!VL_EXP_CLASS_P (exp))
9389 switch (TREE_CODE_LENGTH (code))
9391 default:
9392 case 3: treeop2 = TREE_OPERAND (exp, 2);
9393 case 2: treeop1 = TREE_OPERAND (exp, 1);
9394 case 1: treeop0 = TREE_OPERAND (exp, 0);
9395 case 0: break;
9397 ops.code = code;
9398 ops.type = type;
9399 ops.op0 = treeop0;
9400 ops.op1 = treeop1;
9401 ops.op2 = treeop2;
9402 ops.location = loc;
9404 ignore = (target == const0_rtx
9405 || ((CONVERT_EXPR_CODE_P (code)
9406 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9407 && TREE_CODE (type) == VOID_TYPE));
9409 /* An operation in what may be a bit-field type needs the
9410 result to be reduced to the precision of the bit-field type,
9411 which is narrower than that of the type's mode. */
9412 reduce_bit_field = (!ignore
9413 && INTEGRAL_TYPE_P (type)
9414 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9416 /* If we are going to ignore this result, we need only do something
9417 if there is a side-effect somewhere in the expression. If there
9418 is, short-circuit the most common cases here. Note that we must
9419 not call expand_expr with anything but const0_rtx in case this
9420 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9422 if (ignore)
9424 if (! TREE_SIDE_EFFECTS (exp))
9425 return const0_rtx;
9427 /* Ensure we reference a volatile object even if value is ignored, but
9428 don't do this if all we are doing is taking its address. */
9429 if (TREE_THIS_VOLATILE (exp)
9430 && TREE_CODE (exp) != FUNCTION_DECL
9431 && mode != VOIDmode && mode != BLKmode
9432 && modifier != EXPAND_CONST_ADDRESS)
9434 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9435 if (MEM_P (temp))
9436 copy_to_reg (temp);
9437 return const0_rtx;
9440 if (TREE_CODE_CLASS (code) == tcc_unary
9441 || code == BIT_FIELD_REF
9442 || code == COMPONENT_REF
9443 || code == INDIRECT_REF)
9444 return expand_expr (treeop0, const0_rtx, VOIDmode,
9445 modifier);
9447 else if (TREE_CODE_CLASS (code) == tcc_binary
9448 || TREE_CODE_CLASS (code) == tcc_comparison
9449 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9451 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9452 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9453 return const0_rtx;
9456 target = 0;
9459 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9460 target = 0;
9462 /* Use subtarget as the target for operand 0 of a binary operation. */
9463 subtarget = get_subtarget (target);
9464 original_target = target;
9466 switch (code)
9468 case LABEL_DECL:
9470 tree function = decl_function_context (exp);
9472 temp = label_rtx (exp);
9473 temp = gen_rtx_LABEL_REF (Pmode, temp);
9475 if (function != current_function_decl
9476 && function != 0)
9477 LABEL_REF_NONLOCAL_P (temp) = 1;
9479 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9480 return temp;
9483 case SSA_NAME:
9484 /* ??? ivopts calls expander, without any preparation from
9485 out-of-ssa. So fake instructions as if this was an access to the
9486 base variable. This unnecessarily allocates a pseudo, see how we can
9487 reuse it, if partition base vars have it set already. */
9488 if (!currently_expanding_to_rtl)
9490 tree var = SSA_NAME_VAR (exp);
9491 if (var && DECL_RTL_SET_P (var))
9492 return DECL_RTL (var);
9493 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9494 LAST_VIRTUAL_REGISTER + 1);
9497 g = get_gimple_for_ssa_name (exp);
9498 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9499 if (g == NULL
9500 && modifier == EXPAND_INITIALIZER
9501 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9502 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9503 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9504 g = SSA_NAME_DEF_STMT (exp);
9505 if (g)
9507 rtx r;
9508 ops.code = gimple_assign_rhs_code (g);
9509 switch (get_gimple_rhs_class (ops.code))
9511 case GIMPLE_TERNARY_RHS:
9512 ops.op2 = gimple_assign_rhs3 (g);
9513 /* Fallthru */
9514 case GIMPLE_BINARY_RHS:
9515 ops.op1 = gimple_assign_rhs2 (g);
9516 /* Fallthru */
9517 case GIMPLE_UNARY_RHS:
9518 ops.op0 = gimple_assign_rhs1 (g);
9519 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9520 ops.location = gimple_location (g);
9521 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9522 break;
9523 case GIMPLE_SINGLE_RHS:
9525 location_t saved_loc = curr_insn_location ();
9526 set_curr_insn_location (gimple_location (g));
9527 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9528 tmode, modifier, NULL, inner_reference_p);
9529 set_curr_insn_location (saved_loc);
9530 break;
9532 default:
9533 gcc_unreachable ();
9535 if (REG_P (r) && !REG_EXPR (r))
9536 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9537 return r;
9540 ssa_name = exp;
9541 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9542 exp = SSA_NAME_VAR (ssa_name);
9543 goto expand_decl_rtl;
9545 case PARM_DECL:
9546 case VAR_DECL:
9547 /* If a static var's type was incomplete when the decl was written,
9548 but the type is complete now, lay out the decl now. */
9549 if (DECL_SIZE (exp) == 0
9550 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9551 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9552 layout_decl (exp, 0);
9554 /* ... fall through ... */
9556 case FUNCTION_DECL:
9557 case RESULT_DECL:
9558 decl_rtl = DECL_RTL (exp);
9559 expand_decl_rtl:
9560 gcc_assert (decl_rtl);
9561 decl_rtl = copy_rtx (decl_rtl);
9562 /* Record writes to register variables. */
9563 if (modifier == EXPAND_WRITE
9564 && REG_P (decl_rtl)
9565 && HARD_REGISTER_P (decl_rtl))
9566 add_to_hard_reg_set (&crtl->asm_clobbers,
9567 GET_MODE (decl_rtl), REGNO (decl_rtl));
9569 /* Ensure variable marked as used even if it doesn't go through
9570 a parser. If it hasn't be used yet, write out an external
9571 definition. */
9572 TREE_USED (exp) = 1;
9574 /* Show we haven't gotten RTL for this yet. */
9575 temp = 0;
9577 /* Variables inherited from containing functions should have
9578 been lowered by this point. */
9579 context = decl_function_context (exp);
9580 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9581 || context == current_function_decl
9582 || TREE_STATIC (exp)
9583 || DECL_EXTERNAL (exp)
9584 /* ??? C++ creates functions that are not TREE_STATIC. */
9585 || TREE_CODE (exp) == FUNCTION_DECL);
9587 /* This is the case of an array whose size is to be determined
9588 from its initializer, while the initializer is still being parsed.
9589 ??? We aren't parsing while expanding anymore. */
9591 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9592 temp = validize_mem (decl_rtl);
9594 /* If DECL_RTL is memory, we are in the normal case and the
9595 address is not valid, get the address into a register. */
9597 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9599 if (alt_rtl)
9600 *alt_rtl = decl_rtl;
9601 decl_rtl = use_anchored_address (decl_rtl);
9602 if (modifier != EXPAND_CONST_ADDRESS
9603 && modifier != EXPAND_SUM
9604 && !memory_address_addr_space_p (DECL_MODE (exp),
9605 XEXP (decl_rtl, 0),
9606 MEM_ADDR_SPACE (decl_rtl)))
9607 temp = replace_equiv_address (decl_rtl,
9608 copy_rtx (XEXP (decl_rtl, 0)));
9611 /* If we got something, return it. But first, set the alignment
9612 if the address is a register. */
9613 if (temp != 0)
9615 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9616 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9618 return temp;
9621 /* If the mode of DECL_RTL does not match that of the decl,
9622 there are two cases: we are dealing with a BLKmode value
9623 that is returned in a register, or we are dealing with
9624 a promoted value. In the latter case, return a SUBREG
9625 of the wanted mode, but mark it so that we know that it
9626 was already extended. */
9627 if (REG_P (decl_rtl)
9628 && DECL_MODE (exp) != BLKmode
9629 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9631 machine_mode pmode;
9633 /* Get the signedness to be used for this variable. Ensure we get
9634 the same mode we got when the variable was declared. */
9635 if (code == SSA_NAME
9636 && (g = SSA_NAME_DEF_STMT (ssa_name))
9637 && gimple_code (g) == GIMPLE_CALL
9638 && !gimple_call_internal_p (g))
9639 pmode = promote_function_mode (type, mode, &unsignedp,
9640 gimple_call_fntype (g),
9642 else
9643 pmode = promote_decl_mode (exp, &unsignedp);
9644 gcc_assert (GET_MODE (decl_rtl) == pmode);
9646 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9647 SUBREG_PROMOTED_VAR_P (temp) = 1;
9648 SUBREG_PROMOTED_SET (temp, unsignedp);
9649 return temp;
9652 return decl_rtl;
9654 case INTEGER_CST:
9655 /* Given that TYPE_PRECISION (type) is not always equal to
9656 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9657 the former to the latter according to the signedness of the
9658 type. */
9659 temp = immed_wide_int_const (wide_int::from
9660 (exp,
9661 GET_MODE_PRECISION (TYPE_MODE (type)),
9662 TYPE_SIGN (type)),
9663 TYPE_MODE (type));
9664 return temp;
9666 case VECTOR_CST:
9668 tree tmp = NULL_TREE;
9669 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9670 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9671 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9672 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9673 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9674 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9675 return const_vector_from_tree (exp);
9676 if (GET_MODE_CLASS (mode) == MODE_INT)
9678 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9679 if (type_for_mode)
9680 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9682 if (!tmp)
9684 vec<constructor_elt, va_gc> *v;
9685 unsigned i;
9686 vec_alloc (v, VECTOR_CST_NELTS (exp));
9687 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9688 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9689 tmp = build_constructor (type, v);
9691 return expand_expr (tmp, ignore ? const0_rtx : target,
9692 tmode, modifier);
9695 case CONST_DECL:
9696 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9698 case REAL_CST:
9699 /* If optimized, generate immediate CONST_DOUBLE
9700 which will be turned into memory by reload if necessary.
9702 We used to force a register so that loop.c could see it. But
9703 this does not allow gen_* patterns to perform optimizations with
9704 the constants. It also produces two insns in cases like "x = 1.0;".
9705 On most machines, floating-point constants are not permitted in
9706 many insns, so we'd end up copying it to a register in any case.
9708 Now, we do the copying in expand_binop, if appropriate. */
9709 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9710 TYPE_MODE (TREE_TYPE (exp)));
9712 case FIXED_CST:
9713 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9714 TYPE_MODE (TREE_TYPE (exp)));
9716 case COMPLEX_CST:
9717 /* Handle evaluating a complex constant in a CONCAT target. */
9718 if (original_target && GET_CODE (original_target) == CONCAT)
9720 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9721 rtx rtarg, itarg;
9723 rtarg = XEXP (original_target, 0);
9724 itarg = XEXP (original_target, 1);
9726 /* Move the real and imaginary parts separately. */
9727 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9728 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9730 if (op0 != rtarg)
9731 emit_move_insn (rtarg, op0);
9732 if (op1 != itarg)
9733 emit_move_insn (itarg, op1);
9735 return original_target;
9738 /* ... fall through ... */
9740 case STRING_CST:
9741 temp = expand_expr_constant (exp, 1, modifier);
9743 /* temp contains a constant address.
9744 On RISC machines where a constant address isn't valid,
9745 make some insns to get that address into a register. */
9746 if (modifier != EXPAND_CONST_ADDRESS
9747 && modifier != EXPAND_INITIALIZER
9748 && modifier != EXPAND_SUM
9749 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9750 MEM_ADDR_SPACE (temp)))
9751 return replace_equiv_address (temp,
9752 copy_rtx (XEXP (temp, 0)));
9753 return temp;
9755 case SAVE_EXPR:
9757 tree val = treeop0;
9758 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9759 inner_reference_p);
9761 if (!SAVE_EXPR_RESOLVED_P (exp))
9763 /* We can indeed still hit this case, typically via builtin
9764 expanders calling save_expr immediately before expanding
9765 something. Assume this means that we only have to deal
9766 with non-BLKmode values. */
9767 gcc_assert (GET_MODE (ret) != BLKmode);
9769 val = build_decl (curr_insn_location (),
9770 VAR_DECL, NULL, TREE_TYPE (exp));
9771 DECL_ARTIFICIAL (val) = 1;
9772 DECL_IGNORED_P (val) = 1;
9773 treeop0 = val;
9774 TREE_OPERAND (exp, 0) = treeop0;
9775 SAVE_EXPR_RESOLVED_P (exp) = 1;
9777 if (!CONSTANT_P (ret))
9778 ret = copy_to_reg (ret);
9779 SET_DECL_RTL (val, ret);
9782 return ret;
9786 case CONSTRUCTOR:
9787 /* If we don't need the result, just ensure we evaluate any
9788 subexpressions. */
9789 if (ignore)
9791 unsigned HOST_WIDE_INT idx;
9792 tree value;
9794 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9795 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9797 return const0_rtx;
9800 return expand_constructor (exp, target, modifier, false);
9802 case TARGET_MEM_REF:
9804 addr_space_t as
9805 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9806 enum insn_code icode;
9807 unsigned int align;
9809 op0 = addr_for_mem_ref (exp, as, true);
9810 op0 = memory_address_addr_space (mode, op0, as);
9811 temp = gen_rtx_MEM (mode, op0);
9812 set_mem_attributes (temp, exp, 0);
9813 set_mem_addr_space (temp, as);
9814 align = get_object_alignment (exp);
9815 if (modifier != EXPAND_WRITE
9816 && modifier != EXPAND_MEMORY
9817 && mode != BLKmode
9818 && align < GET_MODE_ALIGNMENT (mode)
9819 /* If the target does not have special handling for unaligned
9820 loads of mode then it can use regular moves for them. */
9821 && ((icode = optab_handler (movmisalign_optab, mode))
9822 != CODE_FOR_nothing))
9824 struct expand_operand ops[2];
9826 /* We've already validated the memory, and we're creating a
9827 new pseudo destination. The predicates really can't fail,
9828 nor can the generator. */
9829 create_output_operand (&ops[0], NULL_RTX, mode);
9830 create_fixed_operand (&ops[1], temp);
9831 expand_insn (icode, 2, ops);
9832 temp = ops[0].value;
9834 return temp;
9837 case MEM_REF:
9839 addr_space_t as
9840 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9841 machine_mode address_mode;
9842 tree base = TREE_OPERAND (exp, 0);
9843 gimple def_stmt;
9844 enum insn_code icode;
9845 unsigned align;
9846 /* Handle expansion of non-aliased memory with non-BLKmode. That
9847 might end up in a register. */
9848 if (mem_ref_refers_to_non_mem_p (exp))
9850 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9851 base = TREE_OPERAND (base, 0);
9852 if (offset == 0
9853 && tree_fits_uhwi_p (TYPE_SIZE (type))
9854 && (GET_MODE_BITSIZE (DECL_MODE (base))
9855 == tree_to_uhwi (TYPE_SIZE (type))))
9856 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9857 target, tmode, modifier);
9858 if (TYPE_MODE (type) == BLKmode)
9860 temp = assign_stack_temp (DECL_MODE (base),
9861 GET_MODE_SIZE (DECL_MODE (base)));
9862 store_expr (base, temp, 0, false);
9863 temp = adjust_address (temp, BLKmode, offset);
9864 set_mem_size (temp, int_size_in_bytes (type));
9865 return temp;
9867 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9868 bitsize_int (offset * BITS_PER_UNIT));
9869 return expand_expr (exp, target, tmode, modifier);
9871 address_mode = targetm.addr_space.address_mode (as);
9872 base = TREE_OPERAND (exp, 0);
9873 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9875 tree mask = gimple_assign_rhs2 (def_stmt);
9876 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9877 gimple_assign_rhs1 (def_stmt), mask);
9878 TREE_OPERAND (exp, 0) = base;
9880 align = get_object_alignment (exp);
9881 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9882 op0 = memory_address_addr_space (mode, op0, as);
9883 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9885 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9886 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9887 op0 = memory_address_addr_space (mode, op0, as);
9889 temp = gen_rtx_MEM (mode, op0);
9890 set_mem_attributes (temp, exp, 0);
9891 set_mem_addr_space (temp, as);
9892 if (TREE_THIS_VOLATILE (exp))
9893 MEM_VOLATILE_P (temp) = 1;
9894 if (modifier != EXPAND_WRITE
9895 && modifier != EXPAND_MEMORY
9896 && !inner_reference_p
9897 && mode != BLKmode
9898 && align < GET_MODE_ALIGNMENT (mode))
9900 if ((icode = optab_handler (movmisalign_optab, mode))
9901 != CODE_FOR_nothing)
9903 struct expand_operand ops[2];
9905 /* We've already validated the memory, and we're creating a
9906 new pseudo destination. The predicates really can't fail,
9907 nor can the generator. */
9908 create_output_operand (&ops[0], NULL_RTX, mode);
9909 create_fixed_operand (&ops[1], temp);
9910 expand_insn (icode, 2, ops);
9911 temp = ops[0].value;
9913 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9914 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9915 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9916 (modifier == EXPAND_STACK_PARM
9917 ? NULL_RTX : target),
9918 mode, mode);
9920 return temp;
9923 case ARRAY_REF:
9926 tree array = treeop0;
9927 tree index = treeop1;
9928 tree init;
9930 /* Fold an expression like: "foo"[2].
9931 This is not done in fold so it won't happen inside &.
9932 Don't fold if this is for wide characters since it's too
9933 difficult to do correctly and this is a very rare case. */
9935 if (modifier != EXPAND_CONST_ADDRESS
9936 && modifier != EXPAND_INITIALIZER
9937 && modifier != EXPAND_MEMORY)
9939 tree t = fold_read_from_constant_string (exp);
9941 if (t)
9942 return expand_expr (t, target, tmode, modifier);
9945 /* If this is a constant index into a constant array,
9946 just get the value from the array. Handle both the cases when
9947 we have an explicit constructor and when our operand is a variable
9948 that was declared const. */
9950 if (modifier != EXPAND_CONST_ADDRESS
9951 && modifier != EXPAND_INITIALIZER
9952 && modifier != EXPAND_MEMORY
9953 && TREE_CODE (array) == CONSTRUCTOR
9954 && ! TREE_SIDE_EFFECTS (array)
9955 && TREE_CODE (index) == INTEGER_CST)
9957 unsigned HOST_WIDE_INT ix;
9958 tree field, value;
9960 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9961 field, value)
9962 if (tree_int_cst_equal (field, index))
9964 if (!TREE_SIDE_EFFECTS (value))
9965 return expand_expr (fold (value), target, tmode, modifier);
9966 break;
9970 else if (optimize >= 1
9971 && modifier != EXPAND_CONST_ADDRESS
9972 && modifier != EXPAND_INITIALIZER
9973 && modifier != EXPAND_MEMORY
9974 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9975 && TREE_CODE (index) == INTEGER_CST
9976 && (TREE_CODE (array) == VAR_DECL
9977 || TREE_CODE (array) == CONST_DECL)
9978 && (init = ctor_for_folding (array)) != error_mark_node)
9980 if (init == NULL_TREE)
9982 tree value = build_zero_cst (type);
9983 if (TREE_CODE (value) == CONSTRUCTOR)
9985 /* If VALUE is a CONSTRUCTOR, this optimization is only
9986 useful if this doesn't store the CONSTRUCTOR into
9987 memory. If it does, it is more efficient to just
9988 load the data from the array directly. */
9989 rtx ret = expand_constructor (value, target,
9990 modifier, true);
9991 if (ret == NULL_RTX)
9992 value = NULL_TREE;
9995 if (value)
9996 return expand_expr (value, target, tmode, modifier);
9998 else if (TREE_CODE (init) == CONSTRUCTOR)
10000 unsigned HOST_WIDE_INT ix;
10001 tree field, value;
10003 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10004 field, value)
10005 if (tree_int_cst_equal (field, index))
10007 if (TREE_SIDE_EFFECTS (value))
10008 break;
10010 if (TREE_CODE (value) == CONSTRUCTOR)
10012 /* If VALUE is a CONSTRUCTOR, this
10013 optimization is only useful if
10014 this doesn't store the CONSTRUCTOR
10015 into memory. If it does, it is more
10016 efficient to just load the data from
10017 the array directly. */
10018 rtx ret = expand_constructor (value, target,
10019 modifier, true);
10020 if (ret == NULL_RTX)
10021 break;
10024 return
10025 expand_expr (fold (value), target, tmode, modifier);
10028 else if (TREE_CODE (init) == STRING_CST)
10030 tree low_bound = array_ref_low_bound (exp);
10031 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10033 /* Optimize the special case of a zero lower bound.
10035 We convert the lower bound to sizetype to avoid problems
10036 with constant folding. E.g. suppose the lower bound is
10037 1 and its mode is QI. Without the conversion
10038 (ARRAY + (INDEX - (unsigned char)1))
10039 becomes
10040 (ARRAY + (-(unsigned char)1) + INDEX)
10041 which becomes
10042 (ARRAY + 255 + INDEX). Oops! */
10043 if (!integer_zerop (low_bound))
10044 index1 = size_diffop_loc (loc, index1,
10045 fold_convert_loc (loc, sizetype,
10046 low_bound));
10048 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10050 tree type = TREE_TYPE (TREE_TYPE (init));
10051 machine_mode mode = TYPE_MODE (type);
10053 if (GET_MODE_CLASS (mode) == MODE_INT
10054 && GET_MODE_SIZE (mode) == 1)
10055 return gen_int_mode (TREE_STRING_POINTER (init)
10056 [TREE_INT_CST_LOW (index1)],
10057 mode);
10062 goto normal_inner_ref;
10064 case COMPONENT_REF:
10065 /* If the operand is a CONSTRUCTOR, we can just extract the
10066 appropriate field if it is present. */
10067 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10069 unsigned HOST_WIDE_INT idx;
10070 tree field, value;
10072 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10073 idx, field, value)
10074 if (field == treeop1
10075 /* We can normally use the value of the field in the
10076 CONSTRUCTOR. However, if this is a bitfield in
10077 an integral mode that we can fit in a HOST_WIDE_INT,
10078 we must mask only the number of bits in the bitfield,
10079 since this is done implicitly by the constructor. If
10080 the bitfield does not meet either of those conditions,
10081 we can't do this optimization. */
10082 && (! DECL_BIT_FIELD (field)
10083 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10084 && (GET_MODE_PRECISION (DECL_MODE (field))
10085 <= HOST_BITS_PER_WIDE_INT))))
10087 if (DECL_BIT_FIELD (field)
10088 && modifier == EXPAND_STACK_PARM)
10089 target = 0;
10090 op0 = expand_expr (value, target, tmode, modifier);
10091 if (DECL_BIT_FIELD (field))
10093 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10094 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10096 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10098 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10099 imode);
10100 op0 = expand_and (imode, op0, op1, target);
10102 else
10104 int count = GET_MODE_PRECISION (imode) - bitsize;
10106 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10107 target, 0);
10108 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10109 target, 0);
10113 return op0;
10116 goto normal_inner_ref;
10118 case BIT_FIELD_REF:
10119 case ARRAY_RANGE_REF:
10120 normal_inner_ref:
10122 machine_mode mode1, mode2;
10123 HOST_WIDE_INT bitsize, bitpos;
10124 tree offset;
10125 int volatilep = 0, must_force_mem;
10126 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10127 &mode1, &unsignedp, &volatilep, true);
10128 rtx orig_op0, memloc;
10129 bool mem_attrs_from_type = false;
10131 /* If we got back the original object, something is wrong. Perhaps
10132 we are evaluating an expression too early. In any event, don't
10133 infinitely recurse. */
10134 gcc_assert (tem != exp);
10136 /* If TEM's type is a union of variable size, pass TARGET to the inner
10137 computation, since it will need a temporary and TARGET is known
10138 to have to do. This occurs in unchecked conversion in Ada. */
10139 orig_op0 = op0
10140 = expand_expr_real (tem,
10141 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10142 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10143 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10144 != INTEGER_CST)
10145 && modifier != EXPAND_STACK_PARM
10146 ? target : NULL_RTX),
10147 VOIDmode,
10148 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10149 NULL, true);
10151 /* If the field has a mode, we want to access it in the
10152 field's mode, not the computed mode.
10153 If a MEM has VOIDmode (external with incomplete type),
10154 use BLKmode for it instead. */
10155 if (MEM_P (op0))
10157 if (mode1 != VOIDmode)
10158 op0 = adjust_address (op0, mode1, 0);
10159 else if (GET_MODE (op0) == VOIDmode)
10160 op0 = adjust_address (op0, BLKmode, 0);
10163 mode2
10164 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10166 /* If we have either an offset, a BLKmode result, or a reference
10167 outside the underlying object, we must force it to memory.
10168 Such a case can occur in Ada if we have unchecked conversion
10169 of an expression from a scalar type to an aggregate type or
10170 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10171 passed a partially uninitialized object or a view-conversion
10172 to a larger size. */
10173 must_force_mem = (offset
10174 || mode1 == BLKmode
10175 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10177 /* Handle CONCAT first. */
10178 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10180 if (bitpos == 0
10181 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10182 return op0;
10183 if (bitpos == 0
10184 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10185 && bitsize)
10187 op0 = XEXP (op0, 0);
10188 mode2 = GET_MODE (op0);
10190 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10191 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10192 && bitpos
10193 && bitsize)
10195 op0 = XEXP (op0, 1);
10196 bitpos = 0;
10197 mode2 = GET_MODE (op0);
10199 else
10200 /* Otherwise force into memory. */
10201 must_force_mem = 1;
10204 /* If this is a constant, put it in a register if it is a legitimate
10205 constant and we don't need a memory reference. */
10206 if (CONSTANT_P (op0)
10207 && mode2 != BLKmode
10208 && targetm.legitimate_constant_p (mode2, op0)
10209 && !must_force_mem)
10210 op0 = force_reg (mode2, op0);
10212 /* Otherwise, if this is a constant, try to force it to the constant
10213 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10214 is a legitimate constant. */
10215 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10216 op0 = validize_mem (memloc);
10218 /* Otherwise, if this is a constant or the object is not in memory
10219 and need be, put it there. */
10220 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10222 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10223 emit_move_insn (memloc, op0);
10224 op0 = memloc;
10225 mem_attrs_from_type = true;
10228 if (offset)
10230 machine_mode address_mode;
10231 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10232 EXPAND_SUM);
10234 gcc_assert (MEM_P (op0));
10236 address_mode = get_address_mode (op0);
10237 if (GET_MODE (offset_rtx) != address_mode)
10238 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10240 /* See the comment in expand_assignment for the rationale. */
10241 if (mode1 != VOIDmode
10242 && bitpos != 0
10243 && bitsize > 0
10244 && (bitpos % bitsize) == 0
10245 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10246 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10248 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10249 bitpos = 0;
10252 op0 = offset_address (op0, offset_rtx,
10253 highest_pow2_factor (offset));
10256 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10257 record its alignment as BIGGEST_ALIGNMENT. */
10258 if (MEM_P (op0) && bitpos == 0 && offset != 0
10259 && is_aligning_offset (offset, tem))
10260 set_mem_align (op0, BIGGEST_ALIGNMENT);
10262 /* Don't forget about volatility even if this is a bitfield. */
10263 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10265 if (op0 == orig_op0)
10266 op0 = copy_rtx (op0);
10268 MEM_VOLATILE_P (op0) = 1;
10271 /* In cases where an aligned union has an unaligned object
10272 as a field, we might be extracting a BLKmode value from
10273 an integer-mode (e.g., SImode) object. Handle this case
10274 by doing the extract into an object as wide as the field
10275 (which we know to be the width of a basic mode), then
10276 storing into memory, and changing the mode to BLKmode. */
10277 if (mode1 == VOIDmode
10278 || REG_P (op0) || GET_CODE (op0) == SUBREG
10279 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10280 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10281 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10282 && modifier != EXPAND_CONST_ADDRESS
10283 && modifier != EXPAND_INITIALIZER
10284 && modifier != EXPAND_MEMORY)
10285 /* If the bitfield is volatile and the bitsize
10286 is narrower than the access size of the bitfield,
10287 we need to extract bitfields from the access. */
10288 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10289 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10290 && mode1 != BLKmode
10291 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10292 /* If the field isn't aligned enough to fetch as a memref,
10293 fetch it as a bit field. */
10294 || (mode1 != BLKmode
10295 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10296 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10297 || (MEM_P (op0)
10298 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10299 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10300 && modifier != EXPAND_MEMORY
10301 && ((modifier == EXPAND_CONST_ADDRESS
10302 || modifier == EXPAND_INITIALIZER)
10303 ? STRICT_ALIGNMENT
10304 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10305 || (bitpos % BITS_PER_UNIT != 0)))
10306 /* If the type and the field are a constant size and the
10307 size of the type isn't the same size as the bitfield,
10308 we must use bitfield operations. */
10309 || (bitsize >= 0
10310 && TYPE_SIZE (TREE_TYPE (exp))
10311 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10312 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10313 bitsize)))
10315 machine_mode ext_mode = mode;
10317 if (ext_mode == BLKmode
10318 && ! (target != 0 && MEM_P (op0)
10319 && MEM_P (target)
10320 && bitpos % BITS_PER_UNIT == 0))
10321 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10323 if (ext_mode == BLKmode)
10325 if (target == 0)
10326 target = assign_temp (type, 1, 1);
10328 /* ??? Unlike the similar test a few lines below, this one is
10329 very likely obsolete. */
10330 if (bitsize == 0)
10331 return target;
10333 /* In this case, BITPOS must start at a byte boundary and
10334 TARGET, if specified, must be a MEM. */
10335 gcc_assert (MEM_P (op0)
10336 && (!target || MEM_P (target))
10337 && !(bitpos % BITS_PER_UNIT));
10339 emit_block_move (target,
10340 adjust_address (op0, VOIDmode,
10341 bitpos / BITS_PER_UNIT),
10342 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10343 / BITS_PER_UNIT),
10344 (modifier == EXPAND_STACK_PARM
10345 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10347 return target;
10350 /* If we have nothing to extract, the result will be 0 for targets
10351 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10352 return 0 for the sake of consistency, as reading a zero-sized
10353 bitfield is valid in Ada and the value is fully specified. */
10354 if (bitsize == 0)
10355 return const0_rtx;
10357 op0 = validize_mem (op0);
10359 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10360 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10362 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10363 (modifier == EXPAND_STACK_PARM
10364 ? NULL_RTX : target),
10365 ext_mode, ext_mode);
10367 /* If the result is a record type and BITSIZE is narrower than
10368 the mode of OP0, an integral mode, and this is a big endian
10369 machine, we must put the field into the high-order bits. */
10370 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10371 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10372 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10373 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10374 GET_MODE_BITSIZE (GET_MODE (op0))
10375 - bitsize, op0, 1);
10377 /* If the result type is BLKmode, store the data into a temporary
10378 of the appropriate type, but with the mode corresponding to the
10379 mode for the data we have (op0's mode). */
10380 if (mode == BLKmode)
10382 rtx new_rtx
10383 = assign_stack_temp_for_type (ext_mode,
10384 GET_MODE_BITSIZE (ext_mode),
10385 type);
10386 emit_move_insn (new_rtx, op0);
10387 op0 = copy_rtx (new_rtx);
10388 PUT_MODE (op0, BLKmode);
10391 return op0;
10394 /* If the result is BLKmode, use that to access the object
10395 now as well. */
10396 if (mode == BLKmode)
10397 mode1 = BLKmode;
10399 /* Get a reference to just this component. */
10400 if (modifier == EXPAND_CONST_ADDRESS
10401 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10402 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10403 else
10404 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10406 if (op0 == orig_op0)
10407 op0 = copy_rtx (op0);
10409 /* If op0 is a temporary because of forcing to memory, pass only the
10410 type to set_mem_attributes so that the original expression is never
10411 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10412 if (mem_attrs_from_type)
10413 set_mem_attributes (op0, type, 0);
10414 else
10415 set_mem_attributes (op0, exp, 0);
10417 if (REG_P (XEXP (op0, 0)))
10418 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10420 MEM_VOLATILE_P (op0) |= volatilep;
10421 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10422 || modifier == EXPAND_CONST_ADDRESS
10423 || modifier == EXPAND_INITIALIZER)
10424 return op0;
10426 if (target == 0)
10427 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10429 convert_move (target, op0, unsignedp);
10430 return target;
10433 case OBJ_TYPE_REF:
10434 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10436 case CALL_EXPR:
10437 /* All valid uses of __builtin_va_arg_pack () are removed during
10438 inlining. */
10439 if (CALL_EXPR_VA_ARG_PACK (exp))
10440 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10442 tree fndecl = get_callee_fndecl (exp), attr;
10444 if (fndecl
10445 && (attr = lookup_attribute ("error",
10446 DECL_ATTRIBUTES (fndecl))) != NULL)
10447 error ("%Kcall to %qs declared with attribute error: %s",
10448 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10449 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10450 if (fndecl
10451 && (attr = lookup_attribute ("warning",
10452 DECL_ATTRIBUTES (fndecl))) != NULL)
10453 warning_at (tree_nonartificial_location (exp),
10454 0, "%Kcall to %qs declared with attribute warning: %s",
10455 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10456 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10458 /* Check for a built-in function. */
10459 if (fndecl && DECL_BUILT_IN (fndecl))
10461 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10462 return expand_builtin (exp, target, subtarget, tmode, ignore);
10465 return expand_call (exp, target, ignore);
10467 case VIEW_CONVERT_EXPR:
10468 op0 = NULL_RTX;
10470 /* If we are converting to BLKmode, try to avoid an intermediate
10471 temporary by fetching an inner memory reference. */
10472 if (mode == BLKmode
10473 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10474 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10475 && handled_component_p (treeop0))
10477 machine_mode mode1;
10478 HOST_WIDE_INT bitsize, bitpos;
10479 tree offset;
10480 int unsignedp;
10481 int volatilep = 0;
10482 tree tem
10483 = get_inner_reference (treeop0, &bitsize, &bitpos,
10484 &offset, &mode1, &unsignedp, &volatilep,
10485 true);
10486 rtx orig_op0;
10488 /* ??? We should work harder and deal with non-zero offsets. */
10489 if (!offset
10490 && (bitpos % BITS_PER_UNIT) == 0
10491 && bitsize >= 0
10492 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10494 /* See the normal_inner_ref case for the rationale. */
10495 orig_op0
10496 = expand_expr_real (tem,
10497 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10498 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10499 != INTEGER_CST)
10500 && modifier != EXPAND_STACK_PARM
10501 ? target : NULL_RTX),
10502 VOIDmode,
10503 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10504 NULL, true);
10506 if (MEM_P (orig_op0))
10508 op0 = orig_op0;
10510 /* Get a reference to just this component. */
10511 if (modifier == EXPAND_CONST_ADDRESS
10512 || modifier == EXPAND_SUM
10513 || modifier == EXPAND_INITIALIZER)
10514 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10515 else
10516 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10518 if (op0 == orig_op0)
10519 op0 = copy_rtx (op0);
10521 set_mem_attributes (op0, treeop0, 0);
10522 if (REG_P (XEXP (op0, 0)))
10523 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10525 MEM_VOLATILE_P (op0) |= volatilep;
10530 if (!op0)
10531 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10532 NULL, inner_reference_p);
10534 /* If the input and output modes are both the same, we are done. */
10535 if (mode == GET_MODE (op0))
10537 /* If neither mode is BLKmode, and both modes are the same size
10538 then we can use gen_lowpart. */
10539 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10540 && (GET_MODE_PRECISION (mode)
10541 == GET_MODE_PRECISION (GET_MODE (op0)))
10542 && !COMPLEX_MODE_P (GET_MODE (op0)))
10544 if (GET_CODE (op0) == SUBREG)
10545 op0 = force_reg (GET_MODE (op0), op0);
10546 temp = gen_lowpart_common (mode, op0);
10547 if (temp)
10548 op0 = temp;
10549 else
10551 if (!REG_P (op0) && !MEM_P (op0))
10552 op0 = force_reg (GET_MODE (op0), op0);
10553 op0 = gen_lowpart (mode, op0);
10556 /* If both types are integral, convert from one mode to the other. */
10557 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10558 op0 = convert_modes (mode, GET_MODE (op0), op0,
10559 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10560 /* If the output type is a bit-field type, do an extraction. */
10561 else if (reduce_bit_field)
10562 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10563 TYPE_UNSIGNED (type), NULL_RTX,
10564 mode, mode);
10565 /* As a last resort, spill op0 to memory, and reload it in a
10566 different mode. */
10567 else if (!MEM_P (op0))
10569 /* If the operand is not a MEM, force it into memory. Since we
10570 are going to be changing the mode of the MEM, don't call
10571 force_const_mem for constants because we don't allow pool
10572 constants to change mode. */
10573 tree inner_type = TREE_TYPE (treeop0);
10575 gcc_assert (!TREE_ADDRESSABLE (exp));
10577 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10578 target
10579 = assign_stack_temp_for_type
10580 (TYPE_MODE (inner_type),
10581 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10583 emit_move_insn (target, op0);
10584 op0 = target;
10587 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10588 output type is such that the operand is known to be aligned, indicate
10589 that it is. Otherwise, we need only be concerned about alignment for
10590 non-BLKmode results. */
10591 if (MEM_P (op0))
10593 enum insn_code icode;
10595 if (TYPE_ALIGN_OK (type))
10597 /* ??? Copying the MEM without substantially changing it might
10598 run afoul of the code handling volatile memory references in
10599 store_expr, which assumes that TARGET is returned unmodified
10600 if it has been used. */
10601 op0 = copy_rtx (op0);
10602 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10604 else if (modifier != EXPAND_WRITE
10605 && modifier != EXPAND_MEMORY
10606 && !inner_reference_p
10607 && mode != BLKmode
10608 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10610 /* If the target does have special handling for unaligned
10611 loads of mode then use them. */
10612 if ((icode = optab_handler (movmisalign_optab, mode))
10613 != CODE_FOR_nothing)
10615 rtx reg, insn;
10617 op0 = adjust_address (op0, mode, 0);
10618 /* We've already validated the memory, and we're creating a
10619 new pseudo destination. The predicates really can't
10620 fail. */
10621 reg = gen_reg_rtx (mode);
10623 /* Nor can the insn generator. */
10624 insn = GEN_FCN (icode) (reg, op0);
10625 emit_insn (insn);
10626 return reg;
10628 else if (STRICT_ALIGNMENT)
10630 tree inner_type = TREE_TYPE (treeop0);
10631 HOST_WIDE_INT temp_size
10632 = MAX (int_size_in_bytes (inner_type),
10633 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10634 rtx new_rtx
10635 = assign_stack_temp_for_type (mode, temp_size, type);
10636 rtx new_with_op0_mode
10637 = adjust_address (new_rtx, GET_MODE (op0), 0);
10639 gcc_assert (!TREE_ADDRESSABLE (exp));
10641 if (GET_MODE (op0) == BLKmode)
10642 emit_block_move (new_with_op0_mode, op0,
10643 GEN_INT (GET_MODE_SIZE (mode)),
10644 (modifier == EXPAND_STACK_PARM
10645 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10646 else
10647 emit_move_insn (new_with_op0_mode, op0);
10649 op0 = new_rtx;
10653 op0 = adjust_address (op0, mode, 0);
10656 return op0;
10658 case MODIFY_EXPR:
10660 tree lhs = treeop0;
10661 tree rhs = treeop1;
10662 gcc_assert (ignore);
10664 /* Check for |= or &= of a bitfield of size one into another bitfield
10665 of size 1. In this case, (unless we need the result of the
10666 assignment) we can do this more efficiently with a
10667 test followed by an assignment, if necessary.
10669 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10670 things change so we do, this code should be enhanced to
10671 support it. */
10672 if (TREE_CODE (lhs) == COMPONENT_REF
10673 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10674 || TREE_CODE (rhs) == BIT_AND_EXPR)
10675 && TREE_OPERAND (rhs, 0) == lhs
10676 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10677 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10678 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10680 rtx_code_label *label = gen_label_rtx ();
10681 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10682 do_jump (TREE_OPERAND (rhs, 1),
10683 value ? label : 0,
10684 value ? 0 : label, -1);
10685 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10686 false);
10687 do_pending_stack_adjust ();
10688 emit_label (label);
10689 return const0_rtx;
10692 expand_assignment (lhs, rhs, false);
10693 return const0_rtx;
10696 case ADDR_EXPR:
10697 return expand_expr_addr_expr (exp, target, tmode, modifier);
10699 case REALPART_EXPR:
10700 op0 = expand_normal (treeop0);
10701 return read_complex_part (op0, false);
10703 case IMAGPART_EXPR:
10704 op0 = expand_normal (treeop0);
10705 return read_complex_part (op0, true);
10707 case RETURN_EXPR:
10708 case LABEL_EXPR:
10709 case GOTO_EXPR:
10710 case SWITCH_EXPR:
10711 case ASM_EXPR:
10712 /* Expanded in cfgexpand.c. */
10713 gcc_unreachable ();
10715 case TRY_CATCH_EXPR:
10716 case CATCH_EXPR:
10717 case EH_FILTER_EXPR:
10718 case TRY_FINALLY_EXPR:
10719 /* Lowered by tree-eh.c. */
10720 gcc_unreachable ();
10722 case WITH_CLEANUP_EXPR:
10723 case CLEANUP_POINT_EXPR:
10724 case TARGET_EXPR:
10725 case CASE_LABEL_EXPR:
10726 case VA_ARG_EXPR:
10727 case BIND_EXPR:
10728 case INIT_EXPR:
10729 case CONJ_EXPR:
10730 case COMPOUND_EXPR:
10731 case PREINCREMENT_EXPR:
10732 case PREDECREMENT_EXPR:
10733 case POSTINCREMENT_EXPR:
10734 case POSTDECREMENT_EXPR:
10735 case LOOP_EXPR:
10736 case EXIT_EXPR:
10737 case COMPOUND_LITERAL_EXPR:
10738 /* Lowered by gimplify.c. */
10739 gcc_unreachable ();
10741 case FDESC_EXPR:
10742 /* Function descriptors are not valid except for as
10743 initialization constants, and should not be expanded. */
10744 gcc_unreachable ();
10746 case WITH_SIZE_EXPR:
10747 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10748 have pulled out the size to use in whatever context it needed. */
10749 return expand_expr_real (treeop0, original_target, tmode,
10750 modifier, alt_rtl, inner_reference_p);
10752 default:
10753 return expand_expr_real_2 (&ops, target, tmode, modifier);
10757 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10758 signedness of TYPE), possibly returning the result in TARGET. */
10759 static rtx
10760 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10762 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10763 if (target && GET_MODE (target) != GET_MODE (exp))
10764 target = 0;
10765 /* For constant values, reduce using build_int_cst_type. */
10766 if (CONST_INT_P (exp))
10768 HOST_WIDE_INT value = INTVAL (exp);
10769 tree t = build_int_cst_type (type, value);
10770 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10772 else if (TYPE_UNSIGNED (type))
10774 machine_mode mode = GET_MODE (exp);
10775 rtx mask = immed_wide_int_const
10776 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10777 return expand_and (mode, exp, mask, target);
10779 else
10781 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10782 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10783 exp, count, target, 0);
10784 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10785 exp, count, target, 0);
10789 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10790 when applied to the address of EXP produces an address known to be
10791 aligned more than BIGGEST_ALIGNMENT. */
10793 static int
10794 is_aligning_offset (const_tree offset, const_tree exp)
10796 /* Strip off any conversions. */
10797 while (CONVERT_EXPR_P (offset))
10798 offset = TREE_OPERAND (offset, 0);
10800 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10801 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10802 if (TREE_CODE (offset) != BIT_AND_EXPR
10803 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10804 || compare_tree_int (TREE_OPERAND (offset, 1),
10805 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10806 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10807 return 0;
10809 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10810 It must be NEGATE_EXPR. Then strip any more conversions. */
10811 offset = TREE_OPERAND (offset, 0);
10812 while (CONVERT_EXPR_P (offset))
10813 offset = TREE_OPERAND (offset, 0);
10815 if (TREE_CODE (offset) != NEGATE_EXPR)
10816 return 0;
10818 offset = TREE_OPERAND (offset, 0);
10819 while (CONVERT_EXPR_P (offset))
10820 offset = TREE_OPERAND (offset, 0);
10822 /* This must now be the address of EXP. */
10823 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10826 /* Return the tree node if an ARG corresponds to a string constant or zero
10827 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10828 in bytes within the string that ARG is accessing. The type of the
10829 offset will be `sizetype'. */
10831 tree
10832 string_constant (tree arg, tree *ptr_offset)
10834 tree array, offset, lower_bound;
10835 STRIP_NOPS (arg);
10837 if (TREE_CODE (arg) == ADDR_EXPR)
10839 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10841 *ptr_offset = size_zero_node;
10842 return TREE_OPERAND (arg, 0);
10844 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10846 array = TREE_OPERAND (arg, 0);
10847 offset = size_zero_node;
10849 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10851 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10852 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10853 if (TREE_CODE (array) != STRING_CST
10854 && TREE_CODE (array) != VAR_DECL)
10855 return 0;
10857 /* Check if the array has a nonzero lower bound. */
10858 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10859 if (!integer_zerop (lower_bound))
10861 /* If the offset and base aren't both constants, return 0. */
10862 if (TREE_CODE (lower_bound) != INTEGER_CST)
10863 return 0;
10864 if (TREE_CODE (offset) != INTEGER_CST)
10865 return 0;
10866 /* Adjust offset by the lower bound. */
10867 offset = size_diffop (fold_convert (sizetype, offset),
10868 fold_convert (sizetype, lower_bound));
10871 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10873 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10874 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10875 if (TREE_CODE (array) != ADDR_EXPR)
10876 return 0;
10877 array = TREE_OPERAND (array, 0);
10878 if (TREE_CODE (array) != STRING_CST
10879 && TREE_CODE (array) != VAR_DECL)
10880 return 0;
10882 else
10883 return 0;
10885 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10887 tree arg0 = TREE_OPERAND (arg, 0);
10888 tree arg1 = TREE_OPERAND (arg, 1);
10890 STRIP_NOPS (arg0);
10891 STRIP_NOPS (arg1);
10893 if (TREE_CODE (arg0) == ADDR_EXPR
10894 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10895 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10897 array = TREE_OPERAND (arg0, 0);
10898 offset = arg1;
10900 else if (TREE_CODE (arg1) == ADDR_EXPR
10901 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10902 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10904 array = TREE_OPERAND (arg1, 0);
10905 offset = arg0;
10907 else
10908 return 0;
10910 else
10911 return 0;
10913 if (TREE_CODE (array) == STRING_CST)
10915 *ptr_offset = fold_convert (sizetype, offset);
10916 return array;
10918 else if (TREE_CODE (array) == VAR_DECL
10919 || TREE_CODE (array) == CONST_DECL)
10921 int length;
10922 tree init = ctor_for_folding (array);
10924 /* Variables initialized to string literals can be handled too. */
10925 if (init == error_mark_node
10926 || !init
10927 || TREE_CODE (init) != STRING_CST)
10928 return 0;
10930 /* Avoid const char foo[4] = "abcde"; */
10931 if (DECL_SIZE_UNIT (array) == NULL_TREE
10932 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10933 || (length = TREE_STRING_LENGTH (init)) <= 0
10934 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10935 return 0;
10937 /* If variable is bigger than the string literal, OFFSET must be constant
10938 and inside of the bounds of the string literal. */
10939 offset = fold_convert (sizetype, offset);
10940 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10941 && (! tree_fits_uhwi_p (offset)
10942 || compare_tree_int (offset, length) >= 0))
10943 return 0;
10945 *ptr_offset = offset;
10946 return init;
10949 return 0;
10952 /* Generate code to calculate OPS, and exploded expression
10953 using a store-flag instruction and return an rtx for the result.
10954 OPS reflects a comparison.
10956 If TARGET is nonzero, store the result there if convenient.
10958 Return zero if there is no suitable set-flag instruction
10959 available on this machine.
10961 Once expand_expr has been called on the arguments of the comparison,
10962 we are committed to doing the store flag, since it is not safe to
10963 re-evaluate the expression. We emit the store-flag insn by calling
10964 emit_store_flag, but only expand the arguments if we have a reason
10965 to believe that emit_store_flag will be successful. If we think that
10966 it will, but it isn't, we have to simulate the store-flag with a
10967 set/jump/set sequence. */
10969 static rtx
10970 do_store_flag (sepops ops, rtx target, machine_mode mode)
10972 enum rtx_code code;
10973 tree arg0, arg1, type;
10974 tree tem;
10975 machine_mode operand_mode;
10976 int unsignedp;
10977 rtx op0, op1;
10978 rtx subtarget = target;
10979 location_t loc = ops->location;
10981 arg0 = ops->op0;
10982 arg1 = ops->op1;
10984 /* Don't crash if the comparison was erroneous. */
10985 if (arg0 == error_mark_node || arg1 == error_mark_node)
10986 return const0_rtx;
10988 type = TREE_TYPE (arg0);
10989 operand_mode = TYPE_MODE (type);
10990 unsignedp = TYPE_UNSIGNED (type);
10992 /* We won't bother with BLKmode store-flag operations because it would mean
10993 passing a lot of information to emit_store_flag. */
10994 if (operand_mode == BLKmode)
10995 return 0;
10997 /* We won't bother with store-flag operations involving function pointers
10998 when function pointers must be canonicalized before comparisons. */
10999 #ifdef HAVE_canonicalize_funcptr_for_compare
11000 if (HAVE_canonicalize_funcptr_for_compare
11001 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11002 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11003 == FUNCTION_TYPE))
11004 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11005 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11006 == FUNCTION_TYPE))))
11007 return 0;
11008 #endif
11010 STRIP_NOPS (arg0);
11011 STRIP_NOPS (arg1);
11013 /* For vector typed comparisons emit code to generate the desired
11014 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11015 expander for this. */
11016 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11018 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11019 tree if_true = constant_boolean_node (true, ops->type);
11020 tree if_false = constant_boolean_node (false, ops->type);
11021 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
11024 /* Get the rtx comparison code to use. We know that EXP is a comparison
11025 operation of some type. Some comparisons against 1 and -1 can be
11026 converted to comparisons with zero. Do so here so that the tests
11027 below will be aware that we have a comparison with zero. These
11028 tests will not catch constants in the first operand, but constants
11029 are rarely passed as the first operand. */
11031 switch (ops->code)
11033 case EQ_EXPR:
11034 code = EQ;
11035 break;
11036 case NE_EXPR:
11037 code = NE;
11038 break;
11039 case LT_EXPR:
11040 if (integer_onep (arg1))
11041 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11042 else
11043 code = unsignedp ? LTU : LT;
11044 break;
11045 case LE_EXPR:
11046 if (! unsignedp && integer_all_onesp (arg1))
11047 arg1 = integer_zero_node, code = LT;
11048 else
11049 code = unsignedp ? LEU : LE;
11050 break;
11051 case GT_EXPR:
11052 if (! unsignedp && integer_all_onesp (arg1))
11053 arg1 = integer_zero_node, code = GE;
11054 else
11055 code = unsignedp ? GTU : GT;
11056 break;
11057 case GE_EXPR:
11058 if (integer_onep (arg1))
11059 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11060 else
11061 code = unsignedp ? GEU : GE;
11062 break;
11064 case UNORDERED_EXPR:
11065 code = UNORDERED;
11066 break;
11067 case ORDERED_EXPR:
11068 code = ORDERED;
11069 break;
11070 case UNLT_EXPR:
11071 code = UNLT;
11072 break;
11073 case UNLE_EXPR:
11074 code = UNLE;
11075 break;
11076 case UNGT_EXPR:
11077 code = UNGT;
11078 break;
11079 case UNGE_EXPR:
11080 code = UNGE;
11081 break;
11082 case UNEQ_EXPR:
11083 code = UNEQ;
11084 break;
11085 case LTGT_EXPR:
11086 code = LTGT;
11087 break;
11089 default:
11090 gcc_unreachable ();
11093 /* Put a constant second. */
11094 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11095 || TREE_CODE (arg0) == FIXED_CST)
11097 tem = arg0; arg0 = arg1; arg1 = tem;
11098 code = swap_condition (code);
11101 /* If this is an equality or inequality test of a single bit, we can
11102 do this by shifting the bit being tested to the low-order bit and
11103 masking the result with the constant 1. If the condition was EQ,
11104 we xor it with 1. This does not require an scc insn and is faster
11105 than an scc insn even if we have it.
11107 The code to make this transformation was moved into fold_single_bit_test,
11108 so we just call into the folder and expand its result. */
11110 if ((code == NE || code == EQ)
11111 && integer_zerop (arg1)
11112 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11114 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11115 if (srcstmt
11116 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11118 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11119 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11120 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11121 gimple_assign_rhs1 (srcstmt),
11122 gimple_assign_rhs2 (srcstmt));
11123 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11124 if (temp)
11125 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11129 if (! get_subtarget (target)
11130 || GET_MODE (subtarget) != operand_mode)
11131 subtarget = 0;
11133 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11135 if (target == 0)
11136 target = gen_reg_rtx (mode);
11138 /* Try a cstore if possible. */
11139 return emit_store_flag_force (target, code, op0, op1,
11140 operand_mode, unsignedp,
11141 (TYPE_PRECISION (ops->type) == 1
11142 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11146 /* Stubs in case we haven't got a casesi insn. */
11147 #ifndef HAVE_casesi
11148 # define HAVE_casesi 0
11149 # define gen_casesi(a, b, c, d, e) (0)
11150 # define CODE_FOR_casesi CODE_FOR_nothing
11151 #endif
11153 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11154 0 otherwise (i.e. if there is no casesi instruction).
11156 DEFAULT_PROBABILITY is the probability of jumping to the default
11157 label. */
11159 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11160 rtx table_label, rtx default_label, rtx fallback_label,
11161 int default_probability)
11163 struct expand_operand ops[5];
11164 machine_mode index_mode = SImode;
11165 rtx op1, op2, index;
11167 if (! HAVE_casesi)
11168 return 0;
11170 /* Convert the index to SImode. */
11171 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11173 machine_mode omode = TYPE_MODE (index_type);
11174 rtx rangertx = expand_normal (range);
11176 /* We must handle the endpoints in the original mode. */
11177 index_expr = build2 (MINUS_EXPR, index_type,
11178 index_expr, minval);
11179 minval = integer_zero_node;
11180 index = expand_normal (index_expr);
11181 if (default_label)
11182 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11183 omode, 1, default_label,
11184 default_probability);
11185 /* Now we can safely truncate. */
11186 index = convert_to_mode (index_mode, index, 0);
11188 else
11190 if (TYPE_MODE (index_type) != index_mode)
11192 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11193 index_expr = fold_convert (index_type, index_expr);
11196 index = expand_normal (index_expr);
11199 do_pending_stack_adjust ();
11201 op1 = expand_normal (minval);
11202 op2 = expand_normal (range);
11204 create_input_operand (&ops[0], index, index_mode);
11205 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11206 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11207 create_fixed_operand (&ops[3], table_label);
11208 create_fixed_operand (&ops[4], (default_label
11209 ? default_label
11210 : fallback_label));
11211 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11212 return 1;
11215 /* Attempt to generate a tablejump instruction; same concept. */
11216 #ifndef HAVE_tablejump
11217 #define HAVE_tablejump 0
11218 #define gen_tablejump(x, y) (0)
11219 #endif
11221 /* Subroutine of the next function.
11223 INDEX is the value being switched on, with the lowest value
11224 in the table already subtracted.
11225 MODE is its expected mode (needed if INDEX is constant).
11226 RANGE is the length of the jump table.
11227 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11229 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11230 index value is out of range.
11231 DEFAULT_PROBABILITY is the probability of jumping to
11232 the default label. */
11234 static void
11235 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11236 rtx default_label, int default_probability)
11238 rtx temp, vector;
11240 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11241 cfun->cfg->max_jumptable_ents = INTVAL (range);
11243 /* Do an unsigned comparison (in the proper mode) between the index
11244 expression and the value which represents the length of the range.
11245 Since we just finished subtracting the lower bound of the range
11246 from the index expression, this comparison allows us to simultaneously
11247 check that the original index expression value is both greater than
11248 or equal to the minimum value of the range and less than or equal to
11249 the maximum value of the range. */
11251 if (default_label)
11252 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11253 default_label, default_probability);
11256 /* If index is in range, it must fit in Pmode.
11257 Convert to Pmode so we can index with it. */
11258 if (mode != Pmode)
11259 index = convert_to_mode (Pmode, index, 1);
11261 /* Don't let a MEM slip through, because then INDEX that comes
11262 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11263 and break_out_memory_refs will go to work on it and mess it up. */
11264 #ifdef PIC_CASE_VECTOR_ADDRESS
11265 if (flag_pic && !REG_P (index))
11266 index = copy_to_mode_reg (Pmode, index);
11267 #endif
11269 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11270 GET_MODE_SIZE, because this indicates how large insns are. The other
11271 uses should all be Pmode, because they are addresses. This code
11272 could fail if addresses and insns are not the same size. */
11273 index = simplify_gen_binary (MULT, Pmode, index,
11274 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11275 Pmode));
11276 index = simplify_gen_binary (PLUS, Pmode, index,
11277 gen_rtx_LABEL_REF (Pmode, table_label));
11279 #ifdef PIC_CASE_VECTOR_ADDRESS
11280 if (flag_pic)
11281 index = PIC_CASE_VECTOR_ADDRESS (index);
11282 else
11283 #endif
11284 index = memory_address (CASE_VECTOR_MODE, index);
11285 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11286 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11287 convert_move (temp, vector, 0);
11289 emit_jump_insn (gen_tablejump (temp, table_label));
11291 /* If we are generating PIC code or if the table is PC-relative, the
11292 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11293 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11294 emit_barrier ();
11298 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11299 rtx table_label, rtx default_label, int default_probability)
11301 rtx index;
11303 if (! HAVE_tablejump)
11304 return 0;
11306 index_expr = fold_build2 (MINUS_EXPR, index_type,
11307 fold_convert (index_type, index_expr),
11308 fold_convert (index_type, minval));
11309 index = expand_normal (index_expr);
11310 do_pending_stack_adjust ();
11312 do_tablejump (index, TYPE_MODE (index_type),
11313 convert_modes (TYPE_MODE (index_type),
11314 TYPE_MODE (TREE_TYPE (range)),
11315 expand_normal (range),
11316 TYPE_UNSIGNED (TREE_TYPE (range))),
11317 table_label, default_label, default_probability);
11318 return 1;
11321 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11322 static rtx
11323 const_vector_from_tree (tree exp)
11325 rtvec v;
11326 unsigned i;
11327 int units;
11328 tree elt;
11329 machine_mode inner, mode;
11331 mode = TYPE_MODE (TREE_TYPE (exp));
11333 if (initializer_zerop (exp))
11334 return CONST0_RTX (mode);
11336 units = GET_MODE_NUNITS (mode);
11337 inner = GET_MODE_INNER (mode);
11339 v = rtvec_alloc (units);
11341 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11343 elt = VECTOR_CST_ELT (exp, i);
11345 if (TREE_CODE (elt) == REAL_CST)
11346 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11347 inner);
11348 else if (TREE_CODE (elt) == FIXED_CST)
11349 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11350 inner);
11351 else
11352 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11355 return gen_rtx_CONST_VECTOR (mode, v);
11358 /* Build a decl for a personality function given a language prefix. */
11360 tree
11361 build_personality_function (const char *lang)
11363 const char *unwind_and_version;
11364 tree decl, type;
11365 char *name;
11367 switch (targetm_common.except_unwind_info (&global_options))
11369 case UI_NONE:
11370 return NULL;
11371 case UI_SJLJ:
11372 unwind_and_version = "_sj0";
11373 break;
11374 case UI_DWARF2:
11375 case UI_TARGET:
11376 unwind_and_version = "_v0";
11377 break;
11378 case UI_SEH:
11379 unwind_and_version = "_seh0";
11380 break;
11381 default:
11382 gcc_unreachable ();
11385 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11387 type = build_function_type_list (integer_type_node, integer_type_node,
11388 long_long_unsigned_type_node,
11389 ptr_type_node, ptr_type_node, NULL_TREE);
11390 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11391 get_identifier (name), type);
11392 DECL_ARTIFICIAL (decl) = 1;
11393 DECL_EXTERNAL (decl) = 1;
11394 TREE_PUBLIC (decl) = 1;
11396 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11397 are the flags assigned by targetm.encode_section_info. */
11398 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11400 return decl;
11403 /* Extracts the personality function of DECL and returns the corresponding
11404 libfunc. */
11407 get_personality_function (tree decl)
11409 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11410 enum eh_personality_kind pk;
11412 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11413 if (pk == eh_personality_none)
11414 return NULL;
11416 if (!personality
11417 && pk == eh_personality_any)
11418 personality = lang_hooks.eh_personality ();
11420 if (pk == eh_personality_lang)
11421 gcc_assert (personality != NULL_TREE);
11423 return XEXP (DECL_RTL (personality), 0);
11426 #include "gt-expr.h"