Remove PUSH_ARGS_REVERSED from the RTL expander.
[official-gcc.git] / gcc / expr.c
blob767b889d899ac8edfc2163236a68e679f1f3875c
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 "function.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
38 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
39 #include "expr.h"
40 #include "optabs.h"
41 #include "libfuncs.h"
42 #include "recog.h"
43 #include "reload.h"
44 #include "typeclass.h"
45 #include "toplev.h"
46 #include "langhooks.h"
47 #include "intl.h"
48 #include "tm_p.h"
49 #include "tree-iterator.h"
50 #include "basic-block.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "gimple-expr.h"
54 #include "is-a.h"
55 #include "gimple.h"
56 #include "gimple-ssa.h"
57 #include "cgraph.h"
58 #include "tree-ssanames.h"
59 #include "target.h"
60 #include "common/common-target.h"
61 #include "timevar.h"
62 #include "df.h"
63 #include "diagnostic.h"
64 #include "tree-ssa-live.h"
65 #include "tree-outof-ssa.h"
66 #include "target-globals.h"
67 #include "params.h"
68 #include "tree-ssa-address.h"
69 #include "cfgexpand.h"
71 #ifndef STACK_PUSH_CODE
72 #ifdef STACK_GROWS_DOWNWARD
73 #define STACK_PUSH_CODE PRE_DEC
74 #else
75 #define STACK_PUSH_CODE PRE_INC
76 #endif
77 #endif
80 /* If this is nonzero, we do not bother generating VOLATILE
81 around volatile memory references, and we are willing to
82 output indirect addresses. If cse is to follow, we reject
83 indirect addresses so a useful potential cse is generated;
84 if it is used only once, instruction combination will produce
85 the same indirect address eventually. */
86 int cse_not_expected;
88 /* This structure is used by move_by_pieces to describe the move to
89 be performed. */
90 struct move_by_pieces_d
92 rtx to;
93 rtx to_addr;
94 int autinc_to;
95 int explicit_inc_to;
96 rtx from;
97 rtx from_addr;
98 int autinc_from;
99 int explicit_inc_from;
100 unsigned HOST_WIDE_INT len;
101 HOST_WIDE_INT offset;
102 int reverse;
105 /* This structure is used by store_by_pieces to describe the clear to
106 be performed. */
108 struct store_by_pieces_d
110 rtx to;
111 rtx to_addr;
112 int autinc_to;
113 int explicit_inc_to;
114 unsigned HOST_WIDE_INT len;
115 HOST_WIDE_INT offset;
116 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode);
117 void *constfundata;
118 int reverse;
121 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
122 struct move_by_pieces_d *);
123 static bool block_move_libcall_safe_for_call_parm (void);
124 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
125 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
126 unsigned HOST_WIDE_INT);
127 static tree emit_block_move_libcall_fn (int);
128 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
129 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, enum machine_mode);
130 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
131 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
132 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
133 struct store_by_pieces_d *);
134 static tree clear_storage_libcall_fn (int);
135 static rtx compress_float_constant (rtx, rtx);
136 static rtx get_subtarget (rtx);
137 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
138 HOST_WIDE_INT, enum machine_mode,
139 tree, int, alias_set_type);
140 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
141 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
142 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
143 enum machine_mode, tree, alias_set_type, bool);
145 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
147 static int is_aligning_offset (const_tree, const_tree);
148 static void expand_operands (tree, tree, rtx, rtx*, rtx*,
149 enum expand_modifier);
150 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
151 static rtx do_store_flag (sepops, rtx, enum machine_mode);
152 #ifdef PUSH_ROUNDING
153 static void emit_single_push_insn (enum machine_mode, rtx, tree);
154 #endif
155 static void do_tablejump (rtx, enum machine_mode, rtx, rtx, rtx, int);
156 static rtx const_vector_from_tree (tree);
157 static void write_complex_part (rtx, rtx, bool);
159 /* This macro is used to determine whether move_by_pieces should be called
160 to perform a structure copy. */
161 #ifndef MOVE_BY_PIECES_P
162 #define MOVE_BY_PIECES_P(SIZE, ALIGN) \
163 (move_by_pieces_ninsns (SIZE, ALIGN, MOVE_MAX_PIECES + 1) \
164 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
165 #endif
167 /* This macro is used to determine whether clear_by_pieces should be
168 called to clear storage. */
169 #ifndef CLEAR_BY_PIECES_P
170 #define CLEAR_BY_PIECES_P(SIZE, ALIGN) \
171 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
172 < (unsigned int) CLEAR_RATIO (optimize_insn_for_speed_p ()))
173 #endif
175 /* This macro is used to determine whether store_by_pieces should be
176 called to "memset" storage with byte values other than zero. */
177 #ifndef SET_BY_PIECES_P
178 #define SET_BY_PIECES_P(SIZE, ALIGN) \
179 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
180 < (unsigned int) SET_RATIO (optimize_insn_for_speed_p ()))
181 #endif
183 /* This macro is used to determine whether store_by_pieces should be
184 called to "memcpy" storage when the source is a constant string. */
185 #ifndef STORE_BY_PIECES_P
186 #define STORE_BY_PIECES_P(SIZE, ALIGN) \
187 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
188 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
189 #endif
191 /* This is run to set up which modes can be used
192 directly in memory and to initialize the block move optab. It is run
193 at the beginning of compilation and when the target is reinitialized. */
195 void
196 init_expr_target (void)
198 rtx insn, pat;
199 enum machine_mode mode;
200 int num_clobbers;
201 rtx mem, mem1;
202 rtx reg;
204 /* Try indexing by frame ptr and try by stack ptr.
205 It is known that on the Convex the stack ptr isn't a valid index.
206 With luck, one or the other is valid on any machine. */
207 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
208 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
210 /* A scratch register we can modify in-place below to avoid
211 useless RTL allocations. */
212 reg = gen_rtx_REG (VOIDmode, -1);
214 insn = rtx_alloc (INSN);
215 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
216 PATTERN (insn) = pat;
218 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
219 mode = (enum machine_mode) ((int) mode + 1))
221 int regno;
223 direct_load[(int) mode] = direct_store[(int) mode] = 0;
224 PUT_MODE (mem, mode);
225 PUT_MODE (mem1, mode);
226 PUT_MODE (reg, mode);
228 /* See if there is some register that can be used in this mode and
229 directly loaded or stored from memory. */
231 if (mode != VOIDmode && mode != BLKmode)
232 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
233 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
234 regno++)
236 if (! HARD_REGNO_MODE_OK (regno, mode))
237 continue;
239 SET_REGNO (reg, regno);
241 SET_SRC (pat) = mem;
242 SET_DEST (pat) = reg;
243 if (recog (pat, insn, &num_clobbers) >= 0)
244 direct_load[(int) mode] = 1;
246 SET_SRC (pat) = mem1;
247 SET_DEST (pat) = reg;
248 if (recog (pat, insn, &num_clobbers) >= 0)
249 direct_load[(int) mode] = 1;
251 SET_SRC (pat) = reg;
252 SET_DEST (pat) = mem;
253 if (recog (pat, insn, &num_clobbers) >= 0)
254 direct_store[(int) mode] = 1;
256 SET_SRC (pat) = reg;
257 SET_DEST (pat) = mem1;
258 if (recog (pat, insn, &num_clobbers) >= 0)
259 direct_store[(int) mode] = 1;
263 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
265 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
266 mode = GET_MODE_WIDER_MODE (mode))
268 enum machine_mode srcmode;
269 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
270 srcmode = GET_MODE_WIDER_MODE (srcmode))
272 enum insn_code ic;
274 ic = can_extend_p (mode, srcmode, 0);
275 if (ic == CODE_FOR_nothing)
276 continue;
278 PUT_MODE (mem, srcmode);
280 if (insn_operand_matches (ic, 1, mem))
281 float_extend_from_mem[mode][srcmode] = true;
286 /* This is run at the start of compiling a function. */
288 void
289 init_expr (void)
291 memset (&crtl->expr, 0, sizeof (crtl->expr));
294 /* Copy data from FROM to TO, where the machine modes are not the same.
295 Both modes may be integer, or both may be floating, or both may be
296 fixed-point.
297 UNSIGNEDP should be nonzero if FROM is an unsigned type.
298 This causes zero-extension instead of sign-extension. */
300 void
301 convert_move (rtx to, rtx from, int unsignedp)
303 enum machine_mode to_mode = GET_MODE (to);
304 enum machine_mode from_mode = GET_MODE (from);
305 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
306 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
307 enum insn_code code;
308 rtx libcall;
310 /* rtx code for making an equivalent value. */
311 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
312 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
315 gcc_assert (to_real == from_real);
316 gcc_assert (to_mode != BLKmode);
317 gcc_assert (from_mode != BLKmode);
319 /* If the source and destination are already the same, then there's
320 nothing to do. */
321 if (to == from)
322 return;
324 /* If FROM is a SUBREG that indicates that we have already done at least
325 the required extension, strip it. We don't handle such SUBREGs as
326 TO here. */
328 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
329 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
330 >= GET_MODE_PRECISION (to_mode))
331 && SUBREG_PROMOTED_UNSIGNED_P (from) == unsignedp)
332 from = gen_lowpart (to_mode, from), from_mode = to_mode;
334 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
336 if (to_mode == from_mode
337 || (from_mode == VOIDmode && CONSTANT_P (from)))
339 emit_move_insn (to, from);
340 return;
343 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
345 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
347 if (VECTOR_MODE_P (to_mode))
348 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
349 else
350 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
352 emit_move_insn (to, from);
353 return;
356 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
358 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
359 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
360 return;
363 if (to_real)
365 rtx value, insns;
366 convert_optab tab;
368 gcc_assert ((GET_MODE_PRECISION (from_mode)
369 != GET_MODE_PRECISION (to_mode))
370 || (DECIMAL_FLOAT_MODE_P (from_mode)
371 != DECIMAL_FLOAT_MODE_P (to_mode)));
373 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
374 /* Conversion between decimal float and binary float, same size. */
375 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
376 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
377 tab = sext_optab;
378 else
379 tab = trunc_optab;
381 /* Try converting directly if the insn is supported. */
383 code = convert_optab_handler (tab, to_mode, from_mode);
384 if (code != CODE_FOR_nothing)
386 emit_unop_insn (code, to, from,
387 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
388 return;
391 /* Otherwise use a libcall. */
392 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
394 /* Is this conversion implemented yet? */
395 gcc_assert (libcall);
397 start_sequence ();
398 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
399 1, from, from_mode);
400 insns = get_insns ();
401 end_sequence ();
402 emit_libcall_block (insns, to, value,
403 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
404 from)
405 : gen_rtx_FLOAT_EXTEND (to_mode, from));
406 return;
409 /* Handle pointer conversion. */ /* SPEE 900220. */
410 /* Targets are expected to provide conversion insns between PxImode and
411 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
412 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
414 enum machine_mode full_mode
415 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
417 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
418 != CODE_FOR_nothing);
420 if (full_mode != from_mode)
421 from = convert_to_mode (full_mode, from, unsignedp);
422 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
423 to, from, UNKNOWN);
424 return;
426 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
428 rtx new_from;
429 enum machine_mode full_mode
430 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
431 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
432 enum insn_code icode;
434 icode = convert_optab_handler (ctab, full_mode, from_mode);
435 gcc_assert (icode != CODE_FOR_nothing);
437 if (to_mode == full_mode)
439 emit_unop_insn (icode, to, from, UNKNOWN);
440 return;
443 new_from = gen_reg_rtx (full_mode);
444 emit_unop_insn (icode, new_from, from, UNKNOWN);
446 /* else proceed to integer conversions below. */
447 from_mode = full_mode;
448 from = new_from;
451 /* Make sure both are fixed-point modes or both are not. */
452 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
453 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
454 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
456 /* If we widen from_mode to to_mode and they are in the same class,
457 we won't saturate the result.
458 Otherwise, always saturate the result to play safe. */
459 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
460 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
461 expand_fixed_convert (to, from, 0, 0);
462 else
463 expand_fixed_convert (to, from, 0, 1);
464 return;
467 /* Now both modes are integers. */
469 /* Handle expanding beyond a word. */
470 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
471 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
473 rtx insns;
474 rtx lowpart;
475 rtx fill_value;
476 rtx lowfrom;
477 int i;
478 enum machine_mode lowpart_mode;
479 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
481 /* Try converting directly if the insn is supported. */
482 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
483 != CODE_FOR_nothing)
485 /* If FROM is a SUBREG, put it into a register. Do this
486 so that we always generate the same set of insns for
487 better cse'ing; if an intermediate assignment occurred,
488 we won't be doing the operation directly on the SUBREG. */
489 if (optimize > 0 && GET_CODE (from) == SUBREG)
490 from = force_reg (from_mode, from);
491 emit_unop_insn (code, to, from, equiv_code);
492 return;
494 /* Next, try converting via full word. */
495 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
496 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
497 != CODE_FOR_nothing))
499 rtx word_to = gen_reg_rtx (word_mode);
500 if (REG_P (to))
502 if (reg_overlap_mentioned_p (to, from))
503 from = force_reg (from_mode, from);
504 emit_clobber (to);
506 convert_move (word_to, from, unsignedp);
507 emit_unop_insn (code, to, word_to, equiv_code);
508 return;
511 /* No special multiword conversion insn; do it by hand. */
512 start_sequence ();
514 /* Since we will turn this into a no conflict block, we must ensure the
515 the source does not overlap the target so force it into an isolated
516 register when maybe so. Likewise for any MEM input, since the
517 conversion sequence might require several references to it and we
518 must ensure we're getting the same value every time. */
520 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
521 from = force_reg (from_mode, from);
523 /* Get a copy of FROM widened to a word, if necessary. */
524 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
525 lowpart_mode = word_mode;
526 else
527 lowpart_mode = from_mode;
529 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
531 lowpart = gen_lowpart (lowpart_mode, to);
532 emit_move_insn (lowpart, lowfrom);
534 /* Compute the value to put in each remaining word. */
535 if (unsignedp)
536 fill_value = const0_rtx;
537 else
538 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
539 LT, lowfrom, const0_rtx,
540 lowpart_mode, 0, -1);
542 /* Fill the remaining words. */
543 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
545 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
546 rtx subword = operand_subword (to, index, 1, to_mode);
548 gcc_assert (subword);
550 if (fill_value != subword)
551 emit_move_insn (subword, fill_value);
554 insns = get_insns ();
555 end_sequence ();
557 emit_insn (insns);
558 return;
561 /* Truncating multi-word to a word or less. */
562 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
563 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
565 if (!((MEM_P (from)
566 && ! MEM_VOLATILE_P (from)
567 && direct_load[(int) to_mode]
568 && ! mode_dependent_address_p (XEXP (from, 0),
569 MEM_ADDR_SPACE (from)))
570 || REG_P (from)
571 || GET_CODE (from) == SUBREG))
572 from = force_reg (from_mode, from);
573 convert_move (to, gen_lowpart (word_mode, from), 0);
574 return;
577 /* Now follow all the conversions between integers
578 no more than a word long. */
580 /* For truncation, usually we can just refer to FROM in a narrower mode. */
581 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
582 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
584 if (!((MEM_P (from)
585 && ! MEM_VOLATILE_P (from)
586 && direct_load[(int) to_mode]
587 && ! mode_dependent_address_p (XEXP (from, 0),
588 MEM_ADDR_SPACE (from)))
589 || REG_P (from)
590 || GET_CODE (from) == SUBREG))
591 from = force_reg (from_mode, from);
592 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
593 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
594 from = copy_to_reg (from);
595 emit_move_insn (to, gen_lowpart (to_mode, from));
596 return;
599 /* Handle extension. */
600 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
602 /* Convert directly if that works. */
603 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
604 != CODE_FOR_nothing)
606 emit_unop_insn (code, to, from, equiv_code);
607 return;
609 else
611 enum machine_mode intermediate;
612 rtx tmp;
613 int shift_amount;
615 /* Search for a mode to convert via. */
616 for (intermediate = from_mode; intermediate != VOIDmode;
617 intermediate = GET_MODE_WIDER_MODE (intermediate))
618 if (((can_extend_p (to_mode, intermediate, unsignedp)
619 != CODE_FOR_nothing)
620 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
621 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
622 && (can_extend_p (intermediate, from_mode, unsignedp)
623 != CODE_FOR_nothing))
625 convert_move (to, convert_to_mode (intermediate, from,
626 unsignedp), unsignedp);
627 return;
630 /* No suitable intermediate mode.
631 Generate what we need with shifts. */
632 shift_amount = (GET_MODE_PRECISION (to_mode)
633 - GET_MODE_PRECISION (from_mode));
634 from = gen_lowpart (to_mode, force_reg (from_mode, from));
635 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
636 to, unsignedp);
637 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
638 to, unsignedp);
639 if (tmp != to)
640 emit_move_insn (to, tmp);
641 return;
645 /* Support special truncate insns for certain modes. */
646 if (convert_optab_handler (trunc_optab, to_mode,
647 from_mode) != CODE_FOR_nothing)
649 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
650 to, from, UNKNOWN);
651 return;
654 /* Handle truncation of volatile memrefs, and so on;
655 the things that couldn't be truncated directly,
656 and for which there was no special instruction.
658 ??? Code above formerly short-circuited this, for most integer
659 mode pairs, with a force_reg in from_mode followed by a recursive
660 call to this routine. Appears always to have been wrong. */
661 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
663 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
664 emit_move_insn (to, temp);
665 return;
668 /* Mode combination is not recognized. */
669 gcc_unreachable ();
672 /* Return an rtx for a value that would result
673 from converting X to mode MODE.
674 Both X and MODE may be floating, or both integer.
675 UNSIGNEDP is nonzero if X is an unsigned value.
676 This can be done by referring to a part of X in place
677 or by copying to a new temporary with conversion. */
680 convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
682 return convert_modes (mode, VOIDmode, x, unsignedp);
685 /* Return an rtx for a value that would result
686 from converting X from mode OLDMODE to mode MODE.
687 Both modes may be floating, or both integer.
688 UNSIGNEDP is nonzero if X is an unsigned value.
690 This can be done by referring to a part of X in place
691 or by copying to a new temporary with conversion.
693 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
696 convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
698 rtx temp;
700 /* If FROM is a SUBREG that indicates that we have already done at least
701 the required extension, strip it. */
703 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
704 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
705 && SUBREG_PROMOTED_UNSIGNED_P (x) == unsignedp)
706 x = gen_lowpart (mode, SUBREG_REG (x));
708 if (GET_MODE (x) != VOIDmode)
709 oldmode = GET_MODE (x);
711 if (mode == oldmode)
712 return x;
714 /* There is one case that we must handle specially: If we are converting
715 a CONST_INT into a mode whose size is twice HOST_BITS_PER_WIDE_INT and
716 we are to interpret the constant as unsigned, gen_lowpart will do
717 the wrong if the constant appears negative. What we want to do is
718 make the high-order word of the constant zero, not all ones. */
720 if (unsignedp && GET_MODE_CLASS (mode) == MODE_INT
721 && GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT
722 && CONST_INT_P (x) && INTVAL (x) < 0)
724 double_int val = double_int::from_uhwi (INTVAL (x));
726 /* We need to zero extend VAL. */
727 if (oldmode != VOIDmode)
728 val = val.zext (GET_MODE_BITSIZE (oldmode));
730 return immed_double_int_const (val, mode);
733 /* We can do this with a gen_lowpart if both desired and current modes
734 are integer, and this is either a constant integer, a register, or a
735 non-volatile MEM. Except for the constant case where MODE is no
736 wider than HOST_BITS_PER_WIDE_INT, we must be narrowing the operand. */
738 if ((CONST_INT_P (x)
739 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT)
740 || (GET_MODE_CLASS (mode) == MODE_INT
741 && GET_MODE_CLASS (oldmode) == MODE_INT
742 && (CONST_DOUBLE_AS_INT_P (x)
743 || (GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
744 && ((MEM_P (x) && ! MEM_VOLATILE_P (x)
745 && direct_load[(int) mode])
746 || (REG_P (x)
747 && (! HARD_REGISTER_P (x)
748 || HARD_REGNO_MODE_OK (REGNO (x), mode))
749 && TRULY_NOOP_TRUNCATION_MODES_P (mode,
750 GET_MODE (x))))))))
752 /* ?? If we don't know OLDMODE, we have to assume here that
753 X does not need sign- or zero-extension. This may not be
754 the case, but it's the best we can do. */
755 if (CONST_INT_P (x) && oldmode != VOIDmode
756 && GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (oldmode))
758 HOST_WIDE_INT val = INTVAL (x);
760 /* We must sign or zero-extend in this case. Start by
761 zero-extending, then sign extend if we need to. */
762 val &= GET_MODE_MASK (oldmode);
763 if (! unsignedp
764 && val_signbit_known_set_p (oldmode, val))
765 val |= ~GET_MODE_MASK (oldmode);
767 return gen_int_mode (val, mode);
770 return gen_lowpart (mode, x);
773 /* Converting from integer constant into mode is always equivalent to an
774 subreg operation. */
775 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
777 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
778 return simplify_gen_subreg (mode, x, oldmode, 0);
781 temp = gen_reg_rtx (mode);
782 convert_move (temp, x, unsignedp);
783 return temp;
786 /* Return the largest alignment we can use for doing a move (or store)
787 of MAX_PIECES. ALIGN is the largest alignment we could use. */
789 static unsigned int
790 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
792 enum machine_mode tmode;
794 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
795 if (align >= GET_MODE_ALIGNMENT (tmode))
796 align = GET_MODE_ALIGNMENT (tmode);
797 else
799 enum machine_mode tmode, xmode;
801 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
802 tmode != VOIDmode;
803 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
804 if (GET_MODE_SIZE (tmode) > max_pieces
805 || SLOW_UNALIGNED_ACCESS (tmode, align))
806 break;
808 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
811 return align;
814 /* Return the widest integer mode no wider than SIZE. If no such mode
815 can be found, return VOIDmode. */
817 static enum machine_mode
818 widest_int_mode_for_size (unsigned int size)
820 enum machine_mode tmode, mode = VOIDmode;
822 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
823 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
824 if (GET_MODE_SIZE (tmode) < size)
825 mode = tmode;
827 return mode;
830 /* STORE_MAX_PIECES is the number of bytes at a time that we can
831 store efficiently. Due to internal GCC limitations, this is
832 MOVE_MAX_PIECES limited by the number of bytes GCC can represent
833 for an immediate constant. */
835 #define STORE_MAX_PIECES MIN (MOVE_MAX_PIECES, 2 * sizeof (HOST_WIDE_INT))
837 /* Determine whether the LEN bytes can be moved by using several move
838 instructions. Return nonzero if a call to move_by_pieces should
839 succeed. */
842 can_move_by_pieces (unsigned HOST_WIDE_INT len ATTRIBUTE_UNUSED,
843 unsigned int align ATTRIBUTE_UNUSED)
845 return MOVE_BY_PIECES_P (len, align);
848 /* Generate several move instructions to copy LEN bytes from block FROM to
849 block TO. (These are MEM rtx's with BLKmode).
851 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
852 used to push FROM to the stack.
854 ALIGN is maximum stack alignment we can assume.
856 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
857 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
858 stpcpy. */
861 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
862 unsigned int align, int endp)
864 struct move_by_pieces_d data;
865 enum machine_mode to_addr_mode;
866 enum machine_mode from_addr_mode = get_address_mode (from);
867 rtx to_addr, from_addr = XEXP (from, 0);
868 unsigned int max_size = MOVE_MAX_PIECES + 1;
869 enum insn_code icode;
871 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
873 data.offset = 0;
874 data.from_addr = from_addr;
875 if (to)
877 to_addr_mode = get_address_mode (to);
878 to_addr = XEXP (to, 0);
879 data.to = to;
880 data.autinc_to
881 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
882 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
883 data.reverse
884 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
886 else
888 to_addr_mode = VOIDmode;
889 to_addr = NULL_RTX;
890 data.to = NULL_RTX;
891 data.autinc_to = 1;
892 #ifdef STACK_GROWS_DOWNWARD
893 data.reverse = 1;
894 #else
895 data.reverse = 0;
896 #endif
898 data.to_addr = to_addr;
899 data.from = from;
900 data.autinc_from
901 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
902 || GET_CODE (from_addr) == POST_INC
903 || GET_CODE (from_addr) == POST_DEC);
905 data.explicit_inc_from = 0;
906 data.explicit_inc_to = 0;
907 if (data.reverse) data.offset = len;
908 data.len = len;
910 /* If copying requires more than two move insns,
911 copy addresses to registers (to make displacements shorter)
912 and use post-increment if available. */
913 if (!(data.autinc_from && data.autinc_to)
914 && move_by_pieces_ninsns (len, align, max_size) > 2)
916 /* Find the mode of the largest move...
917 MODE might not be used depending on the definitions of the
918 USE_* macros below. */
919 enum machine_mode mode ATTRIBUTE_UNUSED
920 = widest_int_mode_for_size (max_size);
922 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
924 data.from_addr = copy_to_mode_reg (from_addr_mode,
925 plus_constant (from_addr_mode,
926 from_addr, len));
927 data.autinc_from = 1;
928 data.explicit_inc_from = -1;
930 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
932 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
933 data.autinc_from = 1;
934 data.explicit_inc_from = 1;
936 if (!data.autinc_from && CONSTANT_P (from_addr))
937 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
938 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
940 data.to_addr = copy_to_mode_reg (to_addr_mode,
941 plus_constant (to_addr_mode,
942 to_addr, len));
943 data.autinc_to = 1;
944 data.explicit_inc_to = -1;
946 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
948 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
949 data.autinc_to = 1;
950 data.explicit_inc_to = 1;
952 if (!data.autinc_to && CONSTANT_P (to_addr))
953 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
956 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
958 /* First move what we can in the largest integer mode, then go to
959 successively smaller modes. */
961 while (max_size > 1 && data.len > 0)
963 enum machine_mode mode = widest_int_mode_for_size (max_size);
965 if (mode == VOIDmode)
966 break;
968 icode = optab_handler (mov_optab, mode);
969 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
970 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
972 max_size = GET_MODE_SIZE (mode);
975 /* The code above should have handled everything. */
976 gcc_assert (!data.len);
978 if (endp)
980 rtx to1;
982 gcc_assert (!data.reverse);
983 if (data.autinc_to)
985 if (endp == 2)
987 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
988 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
989 else
990 data.to_addr = copy_to_mode_reg (to_addr_mode,
991 plus_constant (to_addr_mode,
992 data.to_addr,
993 -1));
995 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
996 data.offset);
998 else
1000 if (endp == 2)
1001 --data.offset;
1002 to1 = adjust_address (data.to, QImode, data.offset);
1004 return to1;
1006 else
1007 return data.to;
1010 /* Return number of insns required to move L bytes by pieces.
1011 ALIGN (in bits) is maximum alignment we can assume. */
1013 unsigned HOST_WIDE_INT
1014 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1015 unsigned int max_size)
1017 unsigned HOST_WIDE_INT n_insns = 0;
1019 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1021 while (max_size > 1 && l > 0)
1023 enum machine_mode mode;
1024 enum insn_code icode;
1026 mode = widest_int_mode_for_size (max_size);
1028 if (mode == VOIDmode)
1029 break;
1031 icode = optab_handler (mov_optab, mode);
1032 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1033 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1035 max_size = GET_MODE_SIZE (mode);
1038 gcc_assert (!l);
1039 return n_insns;
1042 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1043 with move instructions for mode MODE. GENFUN is the gen_... function
1044 to make a move insn for that mode. DATA has all the other info. */
1046 static void
1047 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1048 struct move_by_pieces_d *data)
1050 unsigned int size = GET_MODE_SIZE (mode);
1051 rtx to1 = NULL_RTX, from1;
1053 while (data->len >= size)
1055 if (data->reverse)
1056 data->offset -= size;
1058 if (data->to)
1060 if (data->autinc_to)
1061 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1062 data->offset);
1063 else
1064 to1 = adjust_address (data->to, mode, data->offset);
1067 if (data->autinc_from)
1068 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1069 data->offset);
1070 else
1071 from1 = adjust_address (data->from, mode, data->offset);
1073 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1074 emit_insn (gen_add2_insn (data->to_addr,
1075 gen_int_mode (-(HOST_WIDE_INT) size,
1076 GET_MODE (data->to_addr))));
1077 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1078 emit_insn (gen_add2_insn (data->from_addr,
1079 gen_int_mode (-(HOST_WIDE_INT) size,
1080 GET_MODE (data->from_addr))));
1082 if (data->to)
1083 emit_insn ((*genfun) (to1, from1));
1084 else
1086 #ifdef PUSH_ROUNDING
1087 emit_single_push_insn (mode, from1, NULL);
1088 #else
1089 gcc_unreachable ();
1090 #endif
1093 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1094 emit_insn (gen_add2_insn (data->to_addr,
1095 gen_int_mode (size,
1096 GET_MODE (data->to_addr))));
1097 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1098 emit_insn (gen_add2_insn (data->from_addr,
1099 gen_int_mode (size,
1100 GET_MODE (data->from_addr))));
1102 if (! data->reverse)
1103 data->offset += size;
1105 data->len -= size;
1109 /* Emit code to move a block Y to a block X. This may be done with
1110 string-move instructions, with multiple scalar move instructions,
1111 or with a library call.
1113 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1114 SIZE is an rtx that says how long they are.
1115 ALIGN is the maximum alignment we can assume they have.
1116 METHOD describes what kind of copy this is, and what mechanisms may be used.
1117 MIN_SIZE is the minimal size of block to move
1118 MAX_SIZE is the maximal size of block to move, if it can not be represented
1119 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1121 Return the address of the new block, if memcpy is called and returns it,
1122 0 otherwise. */
1125 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1126 unsigned int expected_align, HOST_WIDE_INT expected_size,
1127 unsigned HOST_WIDE_INT min_size,
1128 unsigned HOST_WIDE_INT max_size,
1129 unsigned HOST_WIDE_INT probable_max_size)
1131 bool may_use_call;
1132 rtx retval = 0;
1133 unsigned int align;
1135 gcc_assert (size);
1136 if (CONST_INT_P (size)
1137 && INTVAL (size) == 0)
1138 return 0;
1140 switch (method)
1142 case BLOCK_OP_NORMAL:
1143 case BLOCK_OP_TAILCALL:
1144 may_use_call = true;
1145 break;
1147 case BLOCK_OP_CALL_PARM:
1148 may_use_call = block_move_libcall_safe_for_call_parm ();
1150 /* Make inhibit_defer_pop nonzero around the library call
1151 to force it to pop the arguments right away. */
1152 NO_DEFER_POP;
1153 break;
1155 case BLOCK_OP_NO_LIBCALL:
1156 may_use_call = false;
1157 break;
1159 default:
1160 gcc_unreachable ();
1163 gcc_assert (MEM_P (x) && MEM_P (y));
1164 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1165 gcc_assert (align >= BITS_PER_UNIT);
1167 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1168 block copy is more efficient for other large modes, e.g. DCmode. */
1169 x = adjust_address (x, BLKmode, 0);
1170 y = adjust_address (y, BLKmode, 0);
1172 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1173 can be incorrect is coming from __builtin_memcpy. */
1174 if (CONST_INT_P (size))
1176 x = shallow_copy_rtx (x);
1177 y = shallow_copy_rtx (y);
1178 set_mem_size (x, INTVAL (size));
1179 set_mem_size (y, INTVAL (size));
1182 if (CONST_INT_P (size) && MOVE_BY_PIECES_P (INTVAL (size), align))
1183 move_by_pieces (x, y, INTVAL (size), align, 0);
1184 else if (emit_block_move_via_movmem (x, y, size, align,
1185 expected_align, expected_size,
1186 min_size, max_size, probable_max_size))
1188 else if (may_use_call
1189 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1190 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1192 /* Since x and y are passed to a libcall, mark the corresponding
1193 tree EXPR as addressable. */
1194 tree y_expr = MEM_EXPR (y);
1195 tree x_expr = MEM_EXPR (x);
1196 if (y_expr)
1197 mark_addressable (y_expr);
1198 if (x_expr)
1199 mark_addressable (x_expr);
1200 retval = emit_block_move_via_libcall (x, y, size,
1201 method == BLOCK_OP_TAILCALL);
1204 else
1205 emit_block_move_via_loop (x, y, size, align);
1207 if (method == BLOCK_OP_CALL_PARM)
1208 OK_DEFER_POP;
1210 return retval;
1214 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1216 unsigned HOST_WIDE_INT max, min = 0;
1217 if (GET_CODE (size) == CONST_INT)
1218 min = max = UINTVAL (size);
1219 else
1220 max = GET_MODE_MASK (GET_MODE (size));
1221 return emit_block_move_hints (x, y, size, method, 0, -1,
1222 min, max, max);
1225 /* A subroutine of emit_block_move. Returns true if calling the
1226 block move libcall will not clobber any parameters which may have
1227 already been placed on the stack. */
1229 static bool
1230 block_move_libcall_safe_for_call_parm (void)
1232 #if defined (REG_PARM_STACK_SPACE)
1233 tree fn;
1234 #endif
1236 /* If arguments are pushed on the stack, then they're safe. */
1237 if (PUSH_ARGS)
1238 return true;
1240 /* If registers go on the stack anyway, any argument is sure to clobber
1241 an outgoing argument. */
1242 #if defined (REG_PARM_STACK_SPACE)
1243 fn = emit_block_move_libcall_fn (false);
1244 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1245 depend on its argument. */
1246 (void) fn;
1247 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1248 && REG_PARM_STACK_SPACE (fn) != 0)
1249 return false;
1250 #endif
1252 /* If any argument goes in memory, then it might clobber an outgoing
1253 argument. */
1255 CUMULATIVE_ARGS args_so_far_v;
1256 cumulative_args_t args_so_far;
1257 tree fn, arg;
1259 fn = emit_block_move_libcall_fn (false);
1260 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1261 args_so_far = pack_cumulative_args (&args_so_far_v);
1263 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1264 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1266 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1267 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1268 NULL_TREE, true);
1269 if (!tmp || !REG_P (tmp))
1270 return false;
1271 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1272 return false;
1273 targetm.calls.function_arg_advance (args_so_far, mode,
1274 NULL_TREE, true);
1277 return true;
1280 /* A subroutine of emit_block_move. Expand a movmem pattern;
1281 return true if successful. */
1283 static bool
1284 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1285 unsigned int expected_align, HOST_WIDE_INT expected_size,
1286 unsigned HOST_WIDE_INT min_size,
1287 unsigned HOST_WIDE_INT max_size,
1288 unsigned HOST_WIDE_INT probable_max_size)
1290 int save_volatile_ok = volatile_ok;
1291 enum machine_mode mode;
1293 if (expected_align < align)
1294 expected_align = align;
1295 if (expected_size != -1)
1297 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1298 expected_size = probable_max_size;
1299 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1300 expected_size = min_size;
1303 /* Since this is a move insn, we don't care about volatility. */
1304 volatile_ok = 1;
1306 /* Try the most limited insn first, because there's no point
1307 including more than one in the machine description unless
1308 the more limited one has some advantage. */
1310 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1311 mode = GET_MODE_WIDER_MODE (mode))
1313 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1315 if (code != CODE_FOR_nothing
1316 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1317 here because if SIZE is less than the mode mask, as it is
1318 returned by the macro, it will definitely be less than the
1319 actual mode mask. Since SIZE is within the Pmode address
1320 space, we limit MODE to Pmode. */
1321 && ((CONST_INT_P (size)
1322 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1323 <= (GET_MODE_MASK (mode) >> 1)))
1324 || max_size <= (GET_MODE_MASK (mode) >> 1)
1325 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1327 struct expand_operand ops[9];
1328 unsigned int nops;
1330 /* ??? When called via emit_block_move_for_call, it'd be
1331 nice if there were some way to inform the backend, so
1332 that it doesn't fail the expansion because it thinks
1333 emitting the libcall would be more efficient. */
1334 nops = insn_data[(int) code].n_generator_args;
1335 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1337 create_fixed_operand (&ops[0], x);
1338 create_fixed_operand (&ops[1], y);
1339 /* The check above guarantees that this size conversion is valid. */
1340 create_convert_operand_to (&ops[2], size, mode, true);
1341 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1342 if (nops >= 6)
1344 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1345 create_integer_operand (&ops[5], expected_size);
1347 if (nops >= 8)
1349 create_integer_operand (&ops[6], min_size);
1350 /* If we can not represent the maximal size,
1351 make parameter NULL. */
1352 if ((HOST_WIDE_INT) max_size != -1)
1353 create_integer_operand (&ops[7], max_size);
1354 else
1355 create_fixed_operand (&ops[7], NULL);
1357 if (nops == 9)
1359 /* If we can not represent the maximal size,
1360 make parameter NULL. */
1361 if ((HOST_WIDE_INT) probable_max_size != -1)
1362 create_integer_operand (&ops[8], probable_max_size);
1363 else
1364 create_fixed_operand (&ops[8], NULL);
1366 if (maybe_expand_insn (code, nops, ops))
1368 volatile_ok = save_volatile_ok;
1369 return true;
1374 volatile_ok = save_volatile_ok;
1375 return false;
1378 /* A subroutine of emit_block_move. Expand a call to memcpy.
1379 Return the return value from memcpy, 0 otherwise. */
1382 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1384 rtx dst_addr, src_addr;
1385 tree call_expr, fn, src_tree, dst_tree, size_tree;
1386 enum machine_mode size_mode;
1387 rtx retval;
1389 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1390 pseudos. We can then place those new pseudos into a VAR_DECL and
1391 use them later. */
1393 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1394 src_addr = copy_addr_to_reg (XEXP (src, 0));
1396 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1397 src_addr = convert_memory_address (ptr_mode, src_addr);
1399 dst_tree = make_tree (ptr_type_node, dst_addr);
1400 src_tree = make_tree (ptr_type_node, src_addr);
1402 size_mode = TYPE_MODE (sizetype);
1404 size = convert_to_mode (size_mode, size, 1);
1405 size = copy_to_mode_reg (size_mode, size);
1407 /* It is incorrect to use the libcall calling conventions to call
1408 memcpy in this context. This could be a user call to memcpy and
1409 the user may wish to examine the return value from memcpy. For
1410 targets where libcalls and normal calls have different conventions
1411 for returning pointers, we could end up generating incorrect code. */
1413 size_tree = make_tree (sizetype, size);
1415 fn = emit_block_move_libcall_fn (true);
1416 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1417 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1419 retval = expand_normal (call_expr);
1421 return retval;
1424 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1425 for the function we use for block copies. */
1427 static GTY(()) tree block_move_fn;
1429 void
1430 init_block_move_fn (const char *asmspec)
1432 if (!block_move_fn)
1434 tree args, fn, attrs, attr_args;
1436 fn = get_identifier ("memcpy");
1437 args = build_function_type_list (ptr_type_node, ptr_type_node,
1438 const_ptr_type_node, sizetype,
1439 NULL_TREE);
1441 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1442 DECL_EXTERNAL (fn) = 1;
1443 TREE_PUBLIC (fn) = 1;
1444 DECL_ARTIFICIAL (fn) = 1;
1445 TREE_NOTHROW (fn) = 1;
1446 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1447 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1449 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1450 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1452 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1454 block_move_fn = fn;
1457 if (asmspec)
1458 set_user_assembler_name (block_move_fn, asmspec);
1461 static tree
1462 emit_block_move_libcall_fn (int for_call)
1464 static bool emitted_extern;
1466 if (!block_move_fn)
1467 init_block_move_fn (NULL);
1469 if (for_call && !emitted_extern)
1471 emitted_extern = true;
1472 make_decl_rtl (block_move_fn);
1475 return block_move_fn;
1478 /* A subroutine of emit_block_move. Copy the data via an explicit
1479 loop. This is used only when libcalls are forbidden. */
1480 /* ??? It'd be nice to copy in hunks larger than QImode. */
1482 static void
1483 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1484 unsigned int align ATTRIBUTE_UNUSED)
1486 rtx cmp_label, top_label, iter, x_addr, y_addr, tmp;
1487 enum machine_mode x_addr_mode = get_address_mode (x);
1488 enum machine_mode y_addr_mode = get_address_mode (y);
1489 enum machine_mode iter_mode;
1491 iter_mode = GET_MODE (size);
1492 if (iter_mode == VOIDmode)
1493 iter_mode = word_mode;
1495 top_label = gen_label_rtx ();
1496 cmp_label = gen_label_rtx ();
1497 iter = gen_reg_rtx (iter_mode);
1499 emit_move_insn (iter, const0_rtx);
1501 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1502 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1503 do_pending_stack_adjust ();
1505 emit_jump (cmp_label);
1506 emit_label (top_label);
1508 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1509 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1511 if (x_addr_mode != y_addr_mode)
1512 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1513 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1515 x = change_address (x, QImode, x_addr);
1516 y = change_address (y, QImode, y_addr);
1518 emit_move_insn (x, y);
1520 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1521 true, OPTAB_LIB_WIDEN);
1522 if (tmp != iter)
1523 emit_move_insn (iter, tmp);
1525 emit_label (cmp_label);
1527 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1528 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1531 /* Copy all or part of a value X into registers starting at REGNO.
1532 The number of registers to be filled is NREGS. */
1534 void
1535 move_block_to_reg (int regno, rtx x, int nregs, enum machine_mode mode)
1537 int i;
1538 #ifdef HAVE_load_multiple
1539 rtx pat;
1540 rtx last;
1541 #endif
1543 if (nregs == 0)
1544 return;
1546 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1547 x = validize_mem (force_const_mem (mode, x));
1549 /* See if the machine can do this with a load multiple insn. */
1550 #ifdef HAVE_load_multiple
1551 if (HAVE_load_multiple)
1553 last = get_last_insn ();
1554 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
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++)
1567 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1568 operand_subword_force (x, i, mode));
1571 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1572 The number of registers to be filled is NREGS. */
1574 void
1575 move_block_from_reg (int regno, rtx x, int nregs)
1577 int i;
1579 if (nregs == 0)
1580 return;
1582 /* See if the machine can do this with a store multiple insn. */
1583 #ifdef HAVE_store_multiple
1584 if (HAVE_store_multiple)
1586 rtx last = get_last_insn ();
1587 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1588 GEN_INT (nregs));
1589 if (pat)
1591 emit_insn (pat);
1592 return;
1594 else
1595 delete_insns_since (last);
1597 #endif
1599 for (i = 0; i < nregs; i++)
1601 rtx tem = operand_subword (x, i, 1, BLKmode);
1603 gcc_assert (tem);
1605 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1609 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1610 ORIG, where ORIG is a non-consecutive group of registers represented by
1611 a PARALLEL. The clone is identical to the original except in that the
1612 original set of registers is replaced by a new set of pseudo registers.
1613 The new set has the same modes as the original set. */
1616 gen_group_rtx (rtx orig)
1618 int i, length;
1619 rtx *tmps;
1621 gcc_assert (GET_CODE (orig) == PARALLEL);
1623 length = XVECLEN (orig, 0);
1624 tmps = XALLOCAVEC (rtx, length);
1626 /* Skip a NULL entry in first slot. */
1627 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1629 if (i)
1630 tmps[0] = 0;
1632 for (; i < length; i++)
1634 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1635 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1637 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1640 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1643 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1644 except that values are placed in TMPS[i], and must later be moved
1645 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1647 static void
1648 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1650 rtx src;
1651 int start, i;
1652 enum machine_mode m = GET_MODE (orig_src);
1654 gcc_assert (GET_CODE (dst) == PARALLEL);
1656 if (m != VOIDmode
1657 && !SCALAR_INT_MODE_P (m)
1658 && !MEM_P (orig_src)
1659 && GET_CODE (orig_src) != CONCAT)
1661 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1662 if (imode == BLKmode)
1663 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1664 else
1665 src = gen_reg_rtx (imode);
1666 if (imode != BLKmode)
1667 src = gen_lowpart (GET_MODE (orig_src), src);
1668 emit_move_insn (src, orig_src);
1669 /* ...and back again. */
1670 if (imode != BLKmode)
1671 src = gen_lowpart (imode, src);
1672 emit_group_load_1 (tmps, dst, src, type, ssize);
1673 return;
1676 /* Check for a NULL entry, used to indicate that the parameter goes
1677 both on the stack and in registers. */
1678 if (XEXP (XVECEXP (dst, 0, 0), 0))
1679 start = 0;
1680 else
1681 start = 1;
1683 /* Process the pieces. */
1684 for (i = start; i < XVECLEN (dst, 0); i++)
1686 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1687 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1688 unsigned int bytelen = GET_MODE_SIZE (mode);
1689 int shift = 0;
1691 /* Handle trailing fragments that run over the size of the struct. */
1692 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1694 /* Arrange to shift the fragment to where it belongs.
1695 extract_bit_field loads to the lsb of the reg. */
1696 if (
1697 #ifdef BLOCK_REG_PADDING
1698 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1699 == (BYTES_BIG_ENDIAN ? upward : downward)
1700 #else
1701 BYTES_BIG_ENDIAN
1702 #endif
1704 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1705 bytelen = ssize - bytepos;
1706 gcc_assert (bytelen > 0);
1709 /* If we won't be loading directly from memory, protect the real source
1710 from strange tricks we might play; but make sure that the source can
1711 be loaded directly into the destination. */
1712 src = orig_src;
1713 if (!MEM_P (orig_src)
1714 && (!CONSTANT_P (orig_src)
1715 || (GET_MODE (orig_src) != mode
1716 && GET_MODE (orig_src) != VOIDmode)))
1718 if (GET_MODE (orig_src) == VOIDmode)
1719 src = gen_reg_rtx (mode);
1720 else
1721 src = gen_reg_rtx (GET_MODE (orig_src));
1723 emit_move_insn (src, orig_src);
1726 /* Optimize the access just a bit. */
1727 if (MEM_P (src)
1728 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1729 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1730 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1731 && bytelen == GET_MODE_SIZE (mode))
1733 tmps[i] = gen_reg_rtx (mode);
1734 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1736 else if (COMPLEX_MODE_P (mode)
1737 && GET_MODE (src) == mode
1738 && bytelen == GET_MODE_SIZE (mode))
1739 /* Let emit_move_complex do the bulk of the work. */
1740 tmps[i] = src;
1741 else if (GET_CODE (src) == CONCAT)
1743 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1744 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1746 if ((bytepos == 0 && bytelen == slen0)
1747 || (bytepos != 0 && bytepos + bytelen <= slen))
1749 /* The following assumes that the concatenated objects all
1750 have the same size. In this case, a simple calculation
1751 can be used to determine the object and the bit field
1752 to be extracted. */
1753 tmps[i] = XEXP (src, bytepos / slen0);
1754 if (! CONSTANT_P (tmps[i])
1755 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1756 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1757 (bytepos % slen0) * BITS_PER_UNIT,
1758 1, NULL_RTX, mode, mode);
1760 else
1762 rtx mem;
1764 gcc_assert (!bytepos);
1765 mem = assign_stack_temp (GET_MODE (src), slen);
1766 emit_move_insn (mem, src);
1767 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1768 0, 1, NULL_RTX, mode, mode);
1771 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1772 SIMD register, which is currently broken. While we get GCC
1773 to emit proper RTL for these cases, let's dump to memory. */
1774 else if (VECTOR_MODE_P (GET_MODE (dst))
1775 && REG_P (src))
1777 int slen = GET_MODE_SIZE (GET_MODE (src));
1778 rtx mem;
1780 mem = assign_stack_temp (GET_MODE (src), slen);
1781 emit_move_insn (mem, src);
1782 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1784 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1785 && XVECLEN (dst, 0) > 1)
1786 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1787 else if (CONSTANT_P (src))
1789 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1791 if (len == ssize)
1792 tmps[i] = src;
1793 else
1795 rtx first, second;
1797 gcc_assert (2 * len == ssize);
1798 split_double (src, &first, &second);
1799 if (i)
1800 tmps[i] = second;
1801 else
1802 tmps[i] = first;
1805 else if (REG_P (src) && GET_MODE (src) == mode)
1806 tmps[i] = src;
1807 else
1808 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1809 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1810 mode, mode);
1812 if (shift)
1813 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1814 shift, tmps[i], 0);
1818 /* Emit code to move a block SRC of type TYPE to a block DST,
1819 where DST is non-consecutive registers represented by a PARALLEL.
1820 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1821 if not known. */
1823 void
1824 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1826 rtx *tmps;
1827 int i;
1829 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1830 emit_group_load_1 (tmps, dst, src, type, ssize);
1832 /* Copy the extracted pieces into the proper (probable) hard regs. */
1833 for (i = 0; i < XVECLEN (dst, 0); i++)
1835 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1836 if (d == NULL)
1837 continue;
1838 emit_move_insn (d, tmps[i]);
1842 /* Similar, but load SRC into new pseudos in a format that looks like
1843 PARALLEL. This can later be fed to emit_group_move to get things
1844 in the right place. */
1847 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1849 rtvec vec;
1850 int i;
1852 vec = rtvec_alloc (XVECLEN (parallel, 0));
1853 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1855 /* Convert the vector to look just like the original PARALLEL, except
1856 with the computed values. */
1857 for (i = 0; i < XVECLEN (parallel, 0); i++)
1859 rtx e = XVECEXP (parallel, 0, i);
1860 rtx d = XEXP (e, 0);
1862 if (d)
1864 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1865 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1867 RTVEC_ELT (vec, i) = e;
1870 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1873 /* Emit code to move a block SRC to block DST, where SRC and DST are
1874 non-consecutive groups of registers, each represented by a PARALLEL. */
1876 void
1877 emit_group_move (rtx dst, rtx src)
1879 int i;
1881 gcc_assert (GET_CODE (src) == PARALLEL
1882 && GET_CODE (dst) == PARALLEL
1883 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1885 /* Skip first entry if NULL. */
1886 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1887 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1888 XEXP (XVECEXP (src, 0, i), 0));
1891 /* Move a group of registers represented by a PARALLEL into pseudos. */
1894 emit_group_move_into_temps (rtx src)
1896 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1897 int i;
1899 for (i = 0; i < XVECLEN (src, 0); i++)
1901 rtx e = XVECEXP (src, 0, i);
1902 rtx d = XEXP (e, 0);
1904 if (d)
1905 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1906 RTVEC_ELT (vec, i) = e;
1909 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1912 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1913 where SRC is non-consecutive registers represented by a PARALLEL.
1914 SSIZE represents the total size of block ORIG_DST, or -1 if not
1915 known. */
1917 void
1918 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1920 rtx *tmps, dst;
1921 int start, finish, i;
1922 enum machine_mode m = GET_MODE (orig_dst);
1924 gcc_assert (GET_CODE (src) == PARALLEL);
1926 if (!SCALAR_INT_MODE_P (m)
1927 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1929 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1930 if (imode == BLKmode)
1931 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1932 else
1933 dst = gen_reg_rtx (imode);
1934 emit_group_store (dst, src, type, ssize);
1935 if (imode != BLKmode)
1936 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1937 emit_move_insn (orig_dst, dst);
1938 return;
1941 /* Check for a NULL entry, used to indicate that the parameter goes
1942 both on the stack and in registers. */
1943 if (XEXP (XVECEXP (src, 0, 0), 0))
1944 start = 0;
1945 else
1946 start = 1;
1947 finish = XVECLEN (src, 0);
1949 tmps = XALLOCAVEC (rtx, finish);
1951 /* Copy the (probable) hard regs into pseudos. */
1952 for (i = start; i < finish; i++)
1954 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1955 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1957 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1958 emit_move_insn (tmps[i], reg);
1960 else
1961 tmps[i] = reg;
1964 /* If we won't be storing directly into memory, protect the real destination
1965 from strange tricks we might play. */
1966 dst = orig_dst;
1967 if (GET_CODE (dst) == PARALLEL)
1969 rtx temp;
1971 /* We can get a PARALLEL dst if there is a conditional expression in
1972 a return statement. In that case, the dst and src are the same,
1973 so no action is necessary. */
1974 if (rtx_equal_p (dst, src))
1975 return;
1977 /* It is unclear if we can ever reach here, but we may as well handle
1978 it. Allocate a temporary, and split this into a store/load to/from
1979 the temporary. */
1980 temp = assign_stack_temp (GET_MODE (dst), ssize);
1981 emit_group_store (temp, src, type, ssize);
1982 emit_group_load (dst, temp, type, ssize);
1983 return;
1985 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1987 enum machine_mode outer = GET_MODE (dst);
1988 enum machine_mode inner;
1989 HOST_WIDE_INT bytepos;
1990 bool done = false;
1991 rtx temp;
1993 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1994 dst = gen_reg_rtx (outer);
1996 /* Make life a bit easier for combine. */
1997 /* If the first element of the vector is the low part
1998 of the destination mode, use a paradoxical subreg to
1999 initialize the destination. */
2000 if (start < finish)
2002 inner = GET_MODE (tmps[start]);
2003 bytepos = subreg_lowpart_offset (inner, outer);
2004 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2006 temp = simplify_gen_subreg (outer, tmps[start],
2007 inner, 0);
2008 if (temp)
2010 emit_move_insn (dst, temp);
2011 done = true;
2012 start++;
2017 /* If the first element wasn't the low part, try the last. */
2018 if (!done
2019 && start < finish - 1)
2021 inner = GET_MODE (tmps[finish - 1]);
2022 bytepos = subreg_lowpart_offset (inner, outer);
2023 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2025 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2026 inner, 0);
2027 if (temp)
2029 emit_move_insn (dst, temp);
2030 done = true;
2031 finish--;
2036 /* Otherwise, simply initialize the result to zero. */
2037 if (!done)
2038 emit_move_insn (dst, CONST0_RTX (outer));
2041 /* Process the pieces. */
2042 for (i = start; i < finish; i++)
2044 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2045 enum machine_mode mode = GET_MODE (tmps[i]);
2046 unsigned int bytelen = GET_MODE_SIZE (mode);
2047 unsigned int adj_bytelen;
2048 rtx dest = dst;
2050 /* Handle trailing fragments that run over the size of the struct. */
2051 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2052 adj_bytelen = ssize - bytepos;
2053 else
2054 adj_bytelen = bytelen;
2056 if (GET_CODE (dst) == CONCAT)
2058 if (bytepos + adj_bytelen
2059 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2060 dest = XEXP (dst, 0);
2061 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2063 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2064 dest = XEXP (dst, 1);
2066 else
2068 enum machine_mode dest_mode = GET_MODE (dest);
2069 enum machine_mode tmp_mode = GET_MODE (tmps[i]);
2071 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2073 if (GET_MODE_ALIGNMENT (dest_mode)
2074 >= GET_MODE_ALIGNMENT (tmp_mode))
2076 dest = assign_stack_temp (dest_mode,
2077 GET_MODE_SIZE (dest_mode));
2078 emit_move_insn (adjust_address (dest,
2079 tmp_mode,
2080 bytepos),
2081 tmps[i]);
2082 dst = dest;
2084 else
2086 dest = assign_stack_temp (tmp_mode,
2087 GET_MODE_SIZE (tmp_mode));
2088 emit_move_insn (dest, tmps[i]);
2089 dst = adjust_address (dest, dest_mode, bytepos);
2091 break;
2095 /* Handle trailing fragments that run over the size of the struct. */
2096 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2098 /* store_bit_field always takes its value from the lsb.
2099 Move the fragment to the lsb if it's not already there. */
2100 if (
2101 #ifdef BLOCK_REG_PADDING
2102 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2103 == (BYTES_BIG_ENDIAN ? upward : downward)
2104 #else
2105 BYTES_BIG_ENDIAN
2106 #endif
2109 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2110 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2111 shift, tmps[i], 0);
2114 /* Make sure not to write past the end of the struct. */
2115 store_bit_field (dest,
2116 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2117 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2118 VOIDmode, tmps[i]);
2121 /* Optimize the access just a bit. */
2122 else if (MEM_P (dest)
2123 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2124 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2125 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2126 && bytelen == GET_MODE_SIZE (mode))
2127 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2129 else
2130 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2131 0, 0, mode, tmps[i]);
2134 /* Copy from the pseudo into the (probable) hard reg. */
2135 if (orig_dst != dst)
2136 emit_move_insn (orig_dst, dst);
2139 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2140 of the value stored in X. */
2143 maybe_emit_group_store (rtx x, tree type)
2145 enum machine_mode mode = TYPE_MODE (type);
2146 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2147 if (GET_CODE (x) == PARALLEL)
2149 rtx result = gen_reg_rtx (mode);
2150 emit_group_store (result, x, type, int_size_in_bytes (type));
2151 return result;
2153 return x;
2156 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2158 This is used on targets that return BLKmode values in registers. */
2160 void
2161 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2163 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2164 rtx src = NULL, dst = NULL;
2165 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2166 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2167 enum machine_mode mode = GET_MODE (srcreg);
2168 enum machine_mode tmode = GET_MODE (target);
2169 enum machine_mode copy_mode;
2171 /* BLKmode registers created in the back-end shouldn't have survived. */
2172 gcc_assert (mode != BLKmode);
2174 /* If the structure doesn't take up a whole number of words, see whether
2175 SRCREG is padded on the left or on the right. If it's on the left,
2176 set PADDING_CORRECTION to the number of bits to skip.
2178 In most ABIs, the structure will be returned at the least end of
2179 the register, which translates to right padding on little-endian
2180 targets and left padding on big-endian targets. The opposite
2181 holds if the structure is returned at the most significant
2182 end of the register. */
2183 if (bytes % UNITS_PER_WORD != 0
2184 && (targetm.calls.return_in_msb (type)
2185 ? !BYTES_BIG_ENDIAN
2186 : BYTES_BIG_ENDIAN))
2187 padding_correction
2188 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2190 /* We can use a single move if we have an exact mode for the size. */
2191 else if (MEM_P (target)
2192 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2193 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2194 && bytes == GET_MODE_SIZE (mode))
2196 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2197 return;
2200 /* And if we additionally have the same mode for a register. */
2201 else if (REG_P (target)
2202 && GET_MODE (target) == mode
2203 && bytes == GET_MODE_SIZE (mode))
2205 emit_move_insn (target, srcreg);
2206 return;
2209 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2210 into a new pseudo which is a full word. */
2211 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2213 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2214 mode = word_mode;
2217 /* Copy the structure BITSIZE bits at a time. If the target lives in
2218 memory, take care of not reading/writing past its end by selecting
2219 a copy mode suited to BITSIZE. This should always be possible given
2220 how it is computed.
2222 If the target lives in register, make sure not to select a copy mode
2223 larger than the mode of the register.
2225 We could probably emit more efficient code for machines which do not use
2226 strict alignment, but it doesn't seem worth the effort at the current
2227 time. */
2229 copy_mode = word_mode;
2230 if (MEM_P (target))
2232 enum machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2233 if (mem_mode != BLKmode)
2234 copy_mode = mem_mode;
2236 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2237 copy_mode = tmode;
2239 for (bitpos = 0, xbitpos = padding_correction;
2240 bitpos < bytes * BITS_PER_UNIT;
2241 bitpos += bitsize, xbitpos += bitsize)
2243 /* We need a new source operand each time xbitpos is on a
2244 word boundary and when xbitpos == padding_correction
2245 (the first time through). */
2246 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2247 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2249 /* We need a new destination operand each time bitpos is on
2250 a word boundary. */
2251 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2252 dst = target;
2253 else if (bitpos % BITS_PER_WORD == 0)
2254 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2256 /* Use xbitpos for the source extraction (right justified) and
2257 bitpos for the destination store (left justified). */
2258 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2259 extract_bit_field (src, bitsize,
2260 xbitpos % BITS_PER_WORD, 1,
2261 NULL_RTX, copy_mode, copy_mode));
2265 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2266 register if it contains any data, otherwise return null.
2268 This is used on targets that return BLKmode values in registers. */
2271 copy_blkmode_to_reg (enum machine_mode mode, tree src)
2273 int i, n_regs;
2274 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2275 unsigned int bitsize;
2276 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2277 enum machine_mode dst_mode;
2279 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2281 x = expand_normal (src);
2283 bytes = int_size_in_bytes (TREE_TYPE (src));
2284 if (bytes == 0)
2285 return NULL_RTX;
2287 /* If the structure doesn't take up a whole number of words, see
2288 whether the register value should be padded on the left or on
2289 the right. Set PADDING_CORRECTION to the number of padding
2290 bits needed on the left side.
2292 In most ABIs, the structure will be returned at the least end of
2293 the register, which translates to right padding on little-endian
2294 targets and left padding on big-endian targets. The opposite
2295 holds if the structure is returned at the most significant
2296 end of the register. */
2297 if (bytes % UNITS_PER_WORD != 0
2298 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2299 ? !BYTES_BIG_ENDIAN
2300 : BYTES_BIG_ENDIAN))
2301 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2302 * BITS_PER_UNIT));
2304 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2305 dst_words = XALLOCAVEC (rtx, n_regs);
2306 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2308 /* Copy the structure BITSIZE bits at a time. */
2309 for (bitpos = 0, xbitpos = padding_correction;
2310 bitpos < bytes * BITS_PER_UNIT;
2311 bitpos += bitsize, xbitpos += bitsize)
2313 /* We need a new destination pseudo each time xbitpos is
2314 on a word boundary and when xbitpos == padding_correction
2315 (the first time through). */
2316 if (xbitpos % BITS_PER_WORD == 0
2317 || xbitpos == padding_correction)
2319 /* Generate an appropriate register. */
2320 dst_word = gen_reg_rtx (word_mode);
2321 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2323 /* Clear the destination before we move anything into it. */
2324 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2327 /* We need a new source operand each time bitpos is on a word
2328 boundary. */
2329 if (bitpos % BITS_PER_WORD == 0)
2330 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2332 /* Use bitpos for the source extraction (left justified) and
2333 xbitpos for the destination store (right justified). */
2334 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2335 0, 0, word_mode,
2336 extract_bit_field (src_word, bitsize,
2337 bitpos % BITS_PER_WORD, 1,
2338 NULL_RTX, word_mode, word_mode));
2341 if (mode == BLKmode)
2343 /* Find the smallest integer mode large enough to hold the
2344 entire structure. */
2345 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2346 mode != VOIDmode;
2347 mode = GET_MODE_WIDER_MODE (mode))
2348 /* Have we found a large enough mode? */
2349 if (GET_MODE_SIZE (mode) >= bytes)
2350 break;
2352 /* A suitable mode should have been found. */
2353 gcc_assert (mode != VOIDmode);
2356 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2357 dst_mode = word_mode;
2358 else
2359 dst_mode = mode;
2360 dst = gen_reg_rtx (dst_mode);
2362 for (i = 0; i < n_regs; i++)
2363 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2365 if (mode != dst_mode)
2366 dst = gen_lowpart (mode, dst);
2368 return dst;
2371 /* Add a USE expression for REG to the (possibly empty) list pointed
2372 to by CALL_FUSAGE. REG must denote a hard register. */
2374 void
2375 use_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
2377 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2379 *call_fusage
2380 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2383 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2384 to by CALL_FUSAGE. REG must denote a hard register. */
2386 void
2387 clobber_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
2389 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2391 *call_fusage
2392 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2395 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2396 starting at REGNO. All of these registers must be hard registers. */
2398 void
2399 use_regs (rtx *call_fusage, int regno, int nregs)
2401 int i;
2403 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2405 for (i = 0; i < nregs; i++)
2406 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2409 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2410 PARALLEL REGS. This is for calls that pass values in multiple
2411 non-contiguous locations. The Irix 6 ABI has examples of this. */
2413 void
2414 use_group_regs (rtx *call_fusage, rtx regs)
2416 int i;
2418 for (i = 0; i < XVECLEN (regs, 0); i++)
2420 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2422 /* A NULL entry means the parameter goes both on the stack and in
2423 registers. This can also be a MEM for targets that pass values
2424 partially on the stack and partially in registers. */
2425 if (reg != 0 && REG_P (reg))
2426 use_reg (call_fusage, reg);
2430 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2431 assigment and the code of the expresion on the RHS is CODE. Return
2432 NULL otherwise. */
2434 static gimple
2435 get_def_for_expr (tree name, enum tree_code code)
2437 gimple def_stmt;
2439 if (TREE_CODE (name) != SSA_NAME)
2440 return NULL;
2442 def_stmt = get_gimple_for_ssa_name (name);
2443 if (!def_stmt
2444 || gimple_assign_rhs_code (def_stmt) != code)
2445 return NULL;
2447 return def_stmt;
2450 #ifdef HAVE_conditional_move
2451 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2452 assigment and the class of the expresion on the RHS is CLASS. Return
2453 NULL otherwise. */
2455 static gimple
2456 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2458 gimple def_stmt;
2460 if (TREE_CODE (name) != SSA_NAME)
2461 return NULL;
2463 def_stmt = get_gimple_for_ssa_name (name);
2464 if (!def_stmt
2465 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2466 return NULL;
2468 return def_stmt;
2470 #endif
2473 /* Determine whether the LEN bytes generated by CONSTFUN can be
2474 stored to memory using several move instructions. CONSTFUNDATA is
2475 a pointer which will be passed as argument in every CONSTFUN call.
2476 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2477 a memset operation and false if it's a copy of a constant string.
2478 Return nonzero if a call to store_by_pieces should succeed. */
2481 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2482 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2483 void *constfundata, unsigned int align, bool memsetp)
2485 unsigned HOST_WIDE_INT l;
2486 unsigned int max_size;
2487 HOST_WIDE_INT offset = 0;
2488 enum machine_mode mode;
2489 enum insn_code icode;
2490 int reverse;
2491 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2492 rtx cst ATTRIBUTE_UNUSED;
2494 if (len == 0)
2495 return 1;
2497 if (! (memsetp
2498 ? SET_BY_PIECES_P (len, align)
2499 : STORE_BY_PIECES_P (len, align)))
2500 return 0;
2502 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2504 /* We would first store what we can in the largest integer mode, then go to
2505 successively smaller modes. */
2507 for (reverse = 0;
2508 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2509 reverse++)
2511 l = len;
2512 max_size = STORE_MAX_PIECES + 1;
2513 while (max_size > 1 && l > 0)
2515 mode = widest_int_mode_for_size (max_size);
2517 if (mode == VOIDmode)
2518 break;
2520 icode = optab_handler (mov_optab, mode);
2521 if (icode != CODE_FOR_nothing
2522 && align >= GET_MODE_ALIGNMENT (mode))
2524 unsigned int size = GET_MODE_SIZE (mode);
2526 while (l >= size)
2528 if (reverse)
2529 offset -= size;
2531 cst = (*constfun) (constfundata, offset, mode);
2532 if (!targetm.legitimate_constant_p (mode, cst))
2533 return 0;
2535 if (!reverse)
2536 offset += size;
2538 l -= size;
2542 max_size = GET_MODE_SIZE (mode);
2545 /* The code above should have handled everything. */
2546 gcc_assert (!l);
2549 return 1;
2552 /* Generate several move instructions to store LEN bytes generated by
2553 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2554 pointer which will be passed as argument in every CONSTFUN call.
2555 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2556 a memset operation and false if it's a copy of a constant string.
2557 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2558 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2559 stpcpy. */
2562 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2563 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2564 void *constfundata, unsigned int align, bool memsetp, int endp)
2566 enum machine_mode to_addr_mode = get_address_mode (to);
2567 struct store_by_pieces_d data;
2569 if (len == 0)
2571 gcc_assert (endp != 2);
2572 return to;
2575 gcc_assert (memsetp
2576 ? SET_BY_PIECES_P (len, align)
2577 : STORE_BY_PIECES_P (len, align));
2578 data.constfun = constfun;
2579 data.constfundata = constfundata;
2580 data.len = len;
2581 data.to = to;
2582 store_by_pieces_1 (&data, align);
2583 if (endp)
2585 rtx to1;
2587 gcc_assert (!data.reverse);
2588 if (data.autinc_to)
2590 if (endp == 2)
2592 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2593 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2594 else
2595 data.to_addr = copy_to_mode_reg (to_addr_mode,
2596 plus_constant (to_addr_mode,
2597 data.to_addr,
2598 -1));
2600 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2601 data.offset);
2603 else
2605 if (endp == 2)
2606 --data.offset;
2607 to1 = adjust_address (data.to, QImode, data.offset);
2609 return to1;
2611 else
2612 return data.to;
2615 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2616 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2618 static void
2619 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2621 struct store_by_pieces_d data;
2623 if (len == 0)
2624 return;
2626 data.constfun = clear_by_pieces_1;
2627 data.constfundata = NULL;
2628 data.len = len;
2629 data.to = to;
2630 store_by_pieces_1 (&data, align);
2633 /* Callback routine for clear_by_pieces.
2634 Return const0_rtx unconditionally. */
2636 static rtx
2637 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2638 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2639 enum machine_mode mode ATTRIBUTE_UNUSED)
2641 return const0_rtx;
2644 /* Subroutine of clear_by_pieces and store_by_pieces.
2645 Generate several move instructions to store LEN bytes of block TO. (A MEM
2646 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2648 static void
2649 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2650 unsigned int align ATTRIBUTE_UNUSED)
2652 enum machine_mode to_addr_mode = get_address_mode (data->to);
2653 rtx to_addr = XEXP (data->to, 0);
2654 unsigned int max_size = STORE_MAX_PIECES + 1;
2655 enum insn_code icode;
2657 data->offset = 0;
2658 data->to_addr = to_addr;
2659 data->autinc_to
2660 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2661 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2663 data->explicit_inc_to = 0;
2664 data->reverse
2665 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2666 if (data->reverse)
2667 data->offset = data->len;
2669 /* If storing requires more than two move insns,
2670 copy addresses to registers (to make displacements shorter)
2671 and use post-increment if available. */
2672 if (!data->autinc_to
2673 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2675 /* Determine the main mode we'll be using.
2676 MODE might not be used depending on the definitions of the
2677 USE_* macros below. */
2678 enum machine_mode mode ATTRIBUTE_UNUSED
2679 = widest_int_mode_for_size (max_size);
2681 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2683 data->to_addr = copy_to_mode_reg (to_addr_mode,
2684 plus_constant (to_addr_mode,
2685 to_addr,
2686 data->len));
2687 data->autinc_to = 1;
2688 data->explicit_inc_to = -1;
2691 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2692 && ! data->autinc_to)
2694 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2695 data->autinc_to = 1;
2696 data->explicit_inc_to = 1;
2699 if ( !data->autinc_to && CONSTANT_P (to_addr))
2700 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2703 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2705 /* First store what we can in the largest integer mode, then go to
2706 successively smaller modes. */
2708 while (max_size > 1 && data->len > 0)
2710 enum machine_mode mode = widest_int_mode_for_size (max_size);
2712 if (mode == VOIDmode)
2713 break;
2715 icode = optab_handler (mov_optab, mode);
2716 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2717 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2719 max_size = GET_MODE_SIZE (mode);
2722 /* The code above should have handled everything. */
2723 gcc_assert (!data->len);
2726 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2727 with move instructions for mode MODE. GENFUN is the gen_... function
2728 to make a move insn for that mode. DATA has all the other info. */
2730 static void
2731 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2732 struct store_by_pieces_d *data)
2734 unsigned int size = GET_MODE_SIZE (mode);
2735 rtx to1, cst;
2737 while (data->len >= size)
2739 if (data->reverse)
2740 data->offset -= size;
2742 if (data->autinc_to)
2743 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2744 data->offset);
2745 else
2746 to1 = adjust_address (data->to, mode, data->offset);
2748 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2749 emit_insn (gen_add2_insn (data->to_addr,
2750 gen_int_mode (-(HOST_WIDE_INT) size,
2751 GET_MODE (data->to_addr))));
2753 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2754 emit_insn ((*genfun) (to1, cst));
2756 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2757 emit_insn (gen_add2_insn (data->to_addr,
2758 gen_int_mode (size,
2759 GET_MODE (data->to_addr))));
2761 if (! data->reverse)
2762 data->offset += size;
2764 data->len -= size;
2768 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2769 its length in bytes. */
2772 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2773 unsigned int expected_align, HOST_WIDE_INT expected_size,
2774 unsigned HOST_WIDE_INT min_size,
2775 unsigned HOST_WIDE_INT max_size,
2776 unsigned HOST_WIDE_INT probable_max_size)
2778 enum machine_mode mode = GET_MODE (object);
2779 unsigned int align;
2781 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2783 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2784 just move a zero. Otherwise, do this a piece at a time. */
2785 if (mode != BLKmode
2786 && CONST_INT_P (size)
2787 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2789 rtx zero = CONST0_RTX (mode);
2790 if (zero != NULL)
2792 emit_move_insn (object, zero);
2793 return NULL;
2796 if (COMPLEX_MODE_P (mode))
2798 zero = CONST0_RTX (GET_MODE_INNER (mode));
2799 if (zero != NULL)
2801 write_complex_part (object, zero, 0);
2802 write_complex_part (object, zero, 1);
2803 return NULL;
2808 if (size == const0_rtx)
2809 return NULL;
2811 align = MEM_ALIGN (object);
2813 if (CONST_INT_P (size)
2814 && CLEAR_BY_PIECES_P (INTVAL (size), align))
2815 clear_by_pieces (object, INTVAL (size), align);
2816 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2817 expected_align, expected_size,
2818 min_size, max_size, probable_max_size))
2820 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2821 return set_storage_via_libcall (object, size, const0_rtx,
2822 method == BLOCK_OP_TAILCALL);
2823 else
2824 gcc_unreachable ();
2826 return NULL;
2830 clear_storage (rtx object, rtx size, enum block_op_methods method)
2832 unsigned HOST_WIDE_INT max, min = 0;
2833 if (GET_CODE (size) == CONST_INT)
2834 min = max = UINTVAL (size);
2835 else
2836 max = GET_MODE_MASK (GET_MODE (size));
2837 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2841 /* A subroutine of clear_storage. Expand a call to memset.
2842 Return the return value of memset, 0 otherwise. */
2845 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2847 tree call_expr, fn, object_tree, size_tree, val_tree;
2848 enum machine_mode size_mode;
2849 rtx retval;
2851 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2852 place those into new pseudos into a VAR_DECL and use them later. */
2854 object = copy_addr_to_reg (XEXP (object, 0));
2856 size_mode = TYPE_MODE (sizetype);
2857 size = convert_to_mode (size_mode, size, 1);
2858 size = copy_to_mode_reg (size_mode, size);
2860 /* It is incorrect to use the libcall calling conventions to call
2861 memset in this context. This could be a user call to memset and
2862 the user may wish to examine the return value from memset. For
2863 targets where libcalls and normal calls have different conventions
2864 for returning pointers, we could end up generating incorrect code. */
2866 object_tree = make_tree (ptr_type_node, object);
2867 if (!CONST_INT_P (val))
2868 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2869 size_tree = make_tree (sizetype, size);
2870 val_tree = make_tree (integer_type_node, val);
2872 fn = clear_storage_libcall_fn (true);
2873 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2874 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2876 retval = expand_normal (call_expr);
2878 return retval;
2881 /* A subroutine of set_storage_via_libcall. Create the tree node
2882 for the function we use for block clears. */
2884 tree block_clear_fn;
2886 void
2887 init_block_clear_fn (const char *asmspec)
2889 if (!block_clear_fn)
2891 tree fn, args;
2893 fn = get_identifier ("memset");
2894 args = build_function_type_list (ptr_type_node, ptr_type_node,
2895 integer_type_node, sizetype,
2896 NULL_TREE);
2898 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2899 DECL_EXTERNAL (fn) = 1;
2900 TREE_PUBLIC (fn) = 1;
2901 DECL_ARTIFICIAL (fn) = 1;
2902 TREE_NOTHROW (fn) = 1;
2903 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2904 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2906 block_clear_fn = fn;
2909 if (asmspec)
2910 set_user_assembler_name (block_clear_fn, asmspec);
2913 static tree
2914 clear_storage_libcall_fn (int for_call)
2916 static bool emitted_extern;
2918 if (!block_clear_fn)
2919 init_block_clear_fn (NULL);
2921 if (for_call && !emitted_extern)
2923 emitted_extern = true;
2924 make_decl_rtl (block_clear_fn);
2927 return block_clear_fn;
2930 /* Expand a setmem pattern; return true if successful. */
2932 bool
2933 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2934 unsigned int expected_align, HOST_WIDE_INT expected_size,
2935 unsigned HOST_WIDE_INT min_size,
2936 unsigned HOST_WIDE_INT max_size,
2937 unsigned HOST_WIDE_INT probable_max_size)
2939 /* Try the most limited insn first, because there's no point
2940 including more than one in the machine description unless
2941 the more limited one has some advantage. */
2943 enum machine_mode mode;
2945 if (expected_align < align)
2946 expected_align = align;
2947 if (expected_size != -1)
2949 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2950 expected_size = max_size;
2951 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2952 expected_size = min_size;
2955 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2956 mode = GET_MODE_WIDER_MODE (mode))
2958 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2960 if (code != CODE_FOR_nothing
2961 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2962 here because if SIZE is less than the mode mask, as it is
2963 returned by the macro, it will definitely be less than the
2964 actual mode mask. Since SIZE is within the Pmode address
2965 space, we limit MODE to Pmode. */
2966 && ((CONST_INT_P (size)
2967 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2968 <= (GET_MODE_MASK (mode) >> 1)))
2969 || max_size <= (GET_MODE_MASK (mode) >> 1)
2970 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2972 struct expand_operand ops[9];
2973 unsigned int nops;
2975 nops = insn_data[(int) code].n_generator_args;
2976 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2978 create_fixed_operand (&ops[0], object);
2979 /* The check above guarantees that this size conversion is valid. */
2980 create_convert_operand_to (&ops[1], size, mode, true);
2981 create_convert_operand_from (&ops[2], val, byte_mode, true);
2982 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2983 if (nops >= 6)
2985 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2986 create_integer_operand (&ops[5], expected_size);
2988 if (nops >= 8)
2990 create_integer_operand (&ops[6], min_size);
2991 /* If we can not represent the maximal size,
2992 make parameter NULL. */
2993 if ((HOST_WIDE_INT) max_size != -1)
2994 create_integer_operand (&ops[7], max_size);
2995 else
2996 create_fixed_operand (&ops[7], NULL);
2998 if (nops == 9)
3000 /* If we can not represent the maximal size,
3001 make parameter NULL. */
3002 if ((HOST_WIDE_INT) probable_max_size != -1)
3003 create_integer_operand (&ops[8], probable_max_size);
3004 else
3005 create_fixed_operand (&ops[8], NULL);
3007 if (maybe_expand_insn (code, nops, ops))
3008 return true;
3012 return false;
3016 /* Write to one of the components of the complex value CPLX. Write VAL to
3017 the real part if IMAG_P is false, and the imaginary part if its true. */
3019 static void
3020 write_complex_part (rtx cplx, rtx val, bool imag_p)
3022 enum machine_mode cmode;
3023 enum machine_mode imode;
3024 unsigned ibitsize;
3026 if (GET_CODE (cplx) == CONCAT)
3028 emit_move_insn (XEXP (cplx, imag_p), val);
3029 return;
3032 cmode = GET_MODE (cplx);
3033 imode = GET_MODE_INNER (cmode);
3034 ibitsize = GET_MODE_BITSIZE (imode);
3036 /* For MEMs simplify_gen_subreg may generate an invalid new address
3037 because, e.g., the original address is considered mode-dependent
3038 by the target, which restricts simplify_subreg from invoking
3039 adjust_address_nv. Instead of preparing fallback support for an
3040 invalid address, we call adjust_address_nv directly. */
3041 if (MEM_P (cplx))
3043 emit_move_insn (adjust_address_nv (cplx, imode,
3044 imag_p ? GET_MODE_SIZE (imode) : 0),
3045 val);
3046 return;
3049 /* If the sub-object is at least word sized, then we know that subregging
3050 will work. This special case is important, since store_bit_field
3051 wants to operate on integer modes, and there's rarely an OImode to
3052 correspond to TCmode. */
3053 if (ibitsize >= BITS_PER_WORD
3054 /* For hard regs we have exact predicates. Assume we can split
3055 the original object if it spans an even number of hard regs.
3056 This special case is important for SCmode on 64-bit platforms
3057 where the natural size of floating-point regs is 32-bit. */
3058 || (REG_P (cplx)
3059 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3060 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3062 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3063 imag_p ? GET_MODE_SIZE (imode) : 0);
3064 if (part)
3066 emit_move_insn (part, val);
3067 return;
3069 else
3070 /* simplify_gen_subreg may fail for sub-word MEMs. */
3071 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3074 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3077 /* Extract one of the components of the complex value CPLX. Extract the
3078 real part if IMAG_P is false, and the imaginary part if it's true. */
3080 static rtx
3081 read_complex_part (rtx cplx, bool imag_p)
3083 enum machine_mode cmode, imode;
3084 unsigned ibitsize;
3086 if (GET_CODE (cplx) == CONCAT)
3087 return XEXP (cplx, imag_p);
3089 cmode = GET_MODE (cplx);
3090 imode = GET_MODE_INNER (cmode);
3091 ibitsize = GET_MODE_BITSIZE (imode);
3093 /* Special case reads from complex constants that got spilled to memory. */
3094 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3096 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3097 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3099 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3100 if (CONSTANT_CLASS_P (part))
3101 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3105 /* For MEMs simplify_gen_subreg may generate an invalid new address
3106 because, e.g., the original address is considered mode-dependent
3107 by the target, which restricts simplify_subreg from invoking
3108 adjust_address_nv. Instead of preparing fallback support for an
3109 invalid address, we call adjust_address_nv directly. */
3110 if (MEM_P (cplx))
3111 return adjust_address_nv (cplx, imode,
3112 imag_p ? GET_MODE_SIZE (imode) : 0);
3114 /* If the sub-object is at least word sized, then we know that subregging
3115 will work. This special case is important, since extract_bit_field
3116 wants to operate on integer modes, and there's rarely an OImode to
3117 correspond to TCmode. */
3118 if (ibitsize >= BITS_PER_WORD
3119 /* For hard regs we have exact predicates. Assume we can split
3120 the original object if it spans an even number of hard regs.
3121 This special case is important for SCmode on 64-bit platforms
3122 where the natural size of floating-point regs is 32-bit. */
3123 || (REG_P (cplx)
3124 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3125 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3127 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3128 imag_p ? GET_MODE_SIZE (imode) : 0);
3129 if (ret)
3130 return ret;
3131 else
3132 /* simplify_gen_subreg may fail for sub-word MEMs. */
3133 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3136 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3137 true, NULL_RTX, imode, imode);
3140 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3141 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3142 represented in NEW_MODE. If FORCE is true, this will never happen, as
3143 we'll force-create a SUBREG if needed. */
3145 static rtx
3146 emit_move_change_mode (enum machine_mode new_mode,
3147 enum machine_mode old_mode, rtx x, bool force)
3149 rtx ret;
3151 if (push_operand (x, GET_MODE (x)))
3153 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3154 MEM_COPY_ATTRIBUTES (ret, x);
3156 else if (MEM_P (x))
3158 /* We don't have to worry about changing the address since the
3159 size in bytes is supposed to be the same. */
3160 if (reload_in_progress)
3162 /* Copy the MEM to change the mode and move any
3163 substitutions from the old MEM to the new one. */
3164 ret = adjust_address_nv (x, new_mode, 0);
3165 copy_replacements (x, ret);
3167 else
3168 ret = adjust_address (x, new_mode, 0);
3170 else
3172 /* Note that we do want simplify_subreg's behavior of validating
3173 that the new mode is ok for a hard register. If we were to use
3174 simplify_gen_subreg, we would create the subreg, but would
3175 probably run into the target not being able to implement it. */
3176 /* Except, of course, when FORCE is true, when this is exactly what
3177 we want. Which is needed for CCmodes on some targets. */
3178 if (force)
3179 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3180 else
3181 ret = simplify_subreg (new_mode, x, old_mode, 0);
3184 return ret;
3187 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3188 an integer mode of the same size as MODE. Returns the instruction
3189 emitted, or NULL if such a move could not be generated. */
3191 static rtx
3192 emit_move_via_integer (enum machine_mode mode, rtx x, rtx y, bool force)
3194 enum machine_mode imode;
3195 enum insn_code code;
3197 /* There must exist a mode of the exact size we require. */
3198 imode = int_mode_for_mode (mode);
3199 if (imode == BLKmode)
3200 return NULL_RTX;
3202 /* The target must support moves in this mode. */
3203 code = optab_handler (mov_optab, imode);
3204 if (code == CODE_FOR_nothing)
3205 return NULL_RTX;
3207 x = emit_move_change_mode (imode, mode, x, force);
3208 if (x == NULL_RTX)
3209 return NULL_RTX;
3210 y = emit_move_change_mode (imode, mode, y, force);
3211 if (y == NULL_RTX)
3212 return NULL_RTX;
3213 return emit_insn (GEN_FCN (code) (x, y));
3216 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3217 Return an equivalent MEM that does not use an auto-increment. */
3220 emit_move_resolve_push (enum machine_mode mode, rtx x)
3222 enum rtx_code code = GET_CODE (XEXP (x, 0));
3223 HOST_WIDE_INT adjust;
3224 rtx temp;
3226 adjust = GET_MODE_SIZE (mode);
3227 #ifdef PUSH_ROUNDING
3228 adjust = PUSH_ROUNDING (adjust);
3229 #endif
3230 if (code == PRE_DEC || code == POST_DEC)
3231 adjust = -adjust;
3232 else if (code == PRE_MODIFY || code == POST_MODIFY)
3234 rtx expr = XEXP (XEXP (x, 0), 1);
3235 HOST_WIDE_INT val;
3237 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3238 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3239 val = INTVAL (XEXP (expr, 1));
3240 if (GET_CODE (expr) == MINUS)
3241 val = -val;
3242 gcc_assert (adjust == val || adjust == -val);
3243 adjust = val;
3246 /* Do not use anti_adjust_stack, since we don't want to update
3247 stack_pointer_delta. */
3248 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3249 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3250 0, OPTAB_LIB_WIDEN);
3251 if (temp != stack_pointer_rtx)
3252 emit_move_insn (stack_pointer_rtx, temp);
3254 switch (code)
3256 case PRE_INC:
3257 case PRE_DEC:
3258 case PRE_MODIFY:
3259 temp = stack_pointer_rtx;
3260 break;
3261 case POST_INC:
3262 case POST_DEC:
3263 case POST_MODIFY:
3264 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3265 break;
3266 default:
3267 gcc_unreachable ();
3270 return replace_equiv_address (x, temp);
3273 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3274 X is known to satisfy push_operand, and MODE is known to be complex.
3275 Returns the last instruction emitted. */
3278 emit_move_complex_push (enum machine_mode mode, rtx x, rtx y)
3280 enum machine_mode submode = GET_MODE_INNER (mode);
3281 bool imag_first;
3283 #ifdef PUSH_ROUNDING
3284 unsigned int submodesize = GET_MODE_SIZE (submode);
3286 /* In case we output to the stack, but the size is smaller than the
3287 machine can push exactly, we need to use move instructions. */
3288 if (PUSH_ROUNDING (submodesize) != submodesize)
3290 x = emit_move_resolve_push (mode, x);
3291 return emit_move_insn (x, y);
3293 #endif
3295 /* Note that the real part always precedes the imag part in memory
3296 regardless of machine's endianness. */
3297 switch (GET_CODE (XEXP (x, 0)))
3299 case PRE_DEC:
3300 case POST_DEC:
3301 imag_first = true;
3302 break;
3303 case PRE_INC:
3304 case POST_INC:
3305 imag_first = false;
3306 break;
3307 default:
3308 gcc_unreachable ();
3311 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3312 read_complex_part (y, imag_first));
3313 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3314 read_complex_part (y, !imag_first));
3317 /* A subroutine of emit_move_complex. Perform the move from Y to X
3318 via two moves of the parts. Returns the last instruction emitted. */
3321 emit_move_complex_parts (rtx x, rtx y)
3323 /* Show the output dies here. This is necessary for SUBREGs
3324 of pseudos since we cannot track their lifetimes correctly;
3325 hard regs shouldn't appear here except as return values. */
3326 if (!reload_completed && !reload_in_progress
3327 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3328 emit_clobber (x);
3330 write_complex_part (x, read_complex_part (y, false), false);
3331 write_complex_part (x, read_complex_part (y, true), true);
3333 return get_last_insn ();
3336 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3337 MODE is known to be complex. Returns the last instruction emitted. */
3339 static rtx
3340 emit_move_complex (enum machine_mode mode, rtx x, rtx y)
3342 bool try_int;
3344 /* Need to take special care for pushes, to maintain proper ordering
3345 of the data, and possibly extra padding. */
3346 if (push_operand (x, mode))
3347 return emit_move_complex_push (mode, x, y);
3349 /* See if we can coerce the target into moving both values at once, except
3350 for floating point where we favor moving as parts if this is easy. */
3351 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3352 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3353 && !(REG_P (x)
3354 && HARD_REGISTER_P (x)
3355 && hard_regno_nregs[REGNO (x)][mode] == 1)
3356 && !(REG_P (y)
3357 && HARD_REGISTER_P (y)
3358 && hard_regno_nregs[REGNO (y)][mode] == 1))
3359 try_int = false;
3360 /* Not possible if the values are inherently not adjacent. */
3361 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3362 try_int = false;
3363 /* Is possible if both are registers (or subregs of registers). */
3364 else if (register_operand (x, mode) && register_operand (y, mode))
3365 try_int = true;
3366 /* If one of the operands is a memory, and alignment constraints
3367 are friendly enough, we may be able to do combined memory operations.
3368 We do not attempt this if Y is a constant because that combination is
3369 usually better with the by-parts thing below. */
3370 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3371 && (!STRICT_ALIGNMENT
3372 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3373 try_int = true;
3374 else
3375 try_int = false;
3377 if (try_int)
3379 rtx ret;
3381 /* For memory to memory moves, optimal behavior can be had with the
3382 existing block move logic. */
3383 if (MEM_P (x) && MEM_P (y))
3385 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3386 BLOCK_OP_NO_LIBCALL);
3387 return get_last_insn ();
3390 ret = emit_move_via_integer (mode, x, y, true);
3391 if (ret)
3392 return ret;
3395 return emit_move_complex_parts (x, y);
3398 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3399 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3401 static rtx
3402 emit_move_ccmode (enum machine_mode mode, rtx x, rtx y)
3404 rtx ret;
3406 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3407 if (mode != CCmode)
3409 enum insn_code code = optab_handler (mov_optab, CCmode);
3410 if (code != CODE_FOR_nothing)
3412 x = emit_move_change_mode (CCmode, mode, x, true);
3413 y = emit_move_change_mode (CCmode, mode, y, true);
3414 return emit_insn (GEN_FCN (code) (x, y));
3418 /* Otherwise, find the MODE_INT mode of the same width. */
3419 ret = emit_move_via_integer (mode, x, y, false);
3420 gcc_assert (ret != NULL);
3421 return ret;
3424 /* Return true if word I of OP lies entirely in the
3425 undefined bits of a paradoxical subreg. */
3427 static bool
3428 undefined_operand_subword_p (const_rtx op, int i)
3430 enum machine_mode innermode, innermostmode;
3431 int offset;
3432 if (GET_CODE (op) != SUBREG)
3433 return false;
3434 innermode = GET_MODE (op);
3435 innermostmode = GET_MODE (SUBREG_REG (op));
3436 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3437 /* The SUBREG_BYTE represents offset, as if the value were stored in
3438 memory, except for a paradoxical subreg where we define
3439 SUBREG_BYTE to be 0; undo this exception as in
3440 simplify_subreg. */
3441 if (SUBREG_BYTE (op) == 0
3442 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3444 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3445 if (WORDS_BIG_ENDIAN)
3446 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3447 if (BYTES_BIG_ENDIAN)
3448 offset += difference % UNITS_PER_WORD;
3450 if (offset >= GET_MODE_SIZE (innermostmode)
3451 || offset <= -GET_MODE_SIZE (word_mode))
3452 return true;
3453 return false;
3456 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3457 MODE is any multi-word or full-word mode that lacks a move_insn
3458 pattern. Note that you will get better code if you define such
3459 patterns, even if they must turn into multiple assembler instructions. */
3461 static rtx
3462 emit_move_multi_word (enum machine_mode mode, rtx x, rtx y)
3464 rtx last_insn = 0;
3465 rtx seq, inner;
3466 bool need_clobber;
3467 int i;
3469 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3471 /* If X is a push on the stack, do the push now and replace
3472 X with a reference to the stack pointer. */
3473 if (push_operand (x, mode))
3474 x = emit_move_resolve_push (mode, x);
3476 /* If we are in reload, see if either operand is a MEM whose address
3477 is scheduled for replacement. */
3478 if (reload_in_progress && MEM_P (x)
3479 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3480 x = replace_equiv_address_nv (x, inner);
3481 if (reload_in_progress && MEM_P (y)
3482 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3483 y = replace_equiv_address_nv (y, inner);
3485 start_sequence ();
3487 need_clobber = false;
3488 for (i = 0;
3489 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3490 i++)
3492 rtx xpart = operand_subword (x, i, 1, mode);
3493 rtx ypart;
3495 /* Do not generate code for a move if it would come entirely
3496 from the undefined bits of a paradoxical subreg. */
3497 if (undefined_operand_subword_p (y, i))
3498 continue;
3500 ypart = operand_subword (y, i, 1, mode);
3502 /* If we can't get a part of Y, put Y into memory if it is a
3503 constant. Otherwise, force it into a register. Then we must
3504 be able to get a part of Y. */
3505 if (ypart == 0 && CONSTANT_P (y))
3507 y = use_anchored_address (force_const_mem (mode, y));
3508 ypart = operand_subword (y, i, 1, mode);
3510 else if (ypart == 0)
3511 ypart = operand_subword_force (y, i, mode);
3513 gcc_assert (xpart && ypart);
3515 need_clobber |= (GET_CODE (xpart) == SUBREG);
3517 last_insn = emit_move_insn (xpart, ypart);
3520 seq = get_insns ();
3521 end_sequence ();
3523 /* Show the output dies here. This is necessary for SUBREGs
3524 of pseudos since we cannot track their lifetimes correctly;
3525 hard regs shouldn't appear here except as return values.
3526 We never want to emit such a clobber after reload. */
3527 if (x != y
3528 && ! (reload_in_progress || reload_completed)
3529 && need_clobber != 0)
3530 emit_clobber (x);
3532 emit_insn (seq);
3534 return last_insn;
3537 /* Low level part of emit_move_insn.
3538 Called just like emit_move_insn, but assumes X and Y
3539 are basically valid. */
3542 emit_move_insn_1 (rtx x, rtx y)
3544 enum machine_mode mode = GET_MODE (x);
3545 enum insn_code code;
3547 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3549 code = optab_handler (mov_optab, mode);
3550 if (code != CODE_FOR_nothing)
3551 return emit_insn (GEN_FCN (code) (x, y));
3553 /* Expand complex moves by moving real part and imag part. */
3554 if (COMPLEX_MODE_P (mode))
3555 return emit_move_complex (mode, x, y);
3557 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3558 || ALL_FIXED_POINT_MODE_P (mode))
3560 rtx result = emit_move_via_integer (mode, x, y, true);
3562 /* If we can't find an integer mode, use multi words. */
3563 if (result)
3564 return result;
3565 else
3566 return emit_move_multi_word (mode, x, y);
3569 if (GET_MODE_CLASS (mode) == MODE_CC)
3570 return emit_move_ccmode (mode, x, y);
3572 /* Try using a move pattern for the corresponding integer mode. This is
3573 only safe when simplify_subreg can convert MODE constants into integer
3574 constants. At present, it can only do this reliably if the value
3575 fits within a HOST_WIDE_INT. */
3576 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3578 rtx ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3580 if (ret)
3582 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3583 return ret;
3587 return emit_move_multi_word (mode, x, y);
3590 /* Generate code to copy Y into X.
3591 Both Y and X must have the same mode, except that
3592 Y can be a constant with VOIDmode.
3593 This mode cannot be BLKmode; use emit_block_move for that.
3595 Return the last instruction emitted. */
3598 emit_move_insn (rtx x, rtx y)
3600 enum machine_mode mode = GET_MODE (x);
3601 rtx y_cst = NULL_RTX;
3602 rtx last_insn, set;
3604 gcc_assert (mode != BLKmode
3605 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3607 if (CONSTANT_P (y))
3609 if (optimize
3610 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3611 && (last_insn = compress_float_constant (x, y)))
3612 return last_insn;
3614 y_cst = y;
3616 if (!targetm.legitimate_constant_p (mode, y))
3618 y = force_const_mem (mode, y);
3620 /* If the target's cannot_force_const_mem prevented the spill,
3621 assume that the target's move expanders will also take care
3622 of the non-legitimate constant. */
3623 if (!y)
3624 y = y_cst;
3625 else
3626 y = use_anchored_address (y);
3630 /* If X or Y are memory references, verify that their addresses are valid
3631 for the machine. */
3632 if (MEM_P (x)
3633 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3634 MEM_ADDR_SPACE (x))
3635 && ! push_operand (x, GET_MODE (x))))
3636 x = validize_mem (x);
3638 if (MEM_P (y)
3639 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3640 MEM_ADDR_SPACE (y)))
3641 y = validize_mem (y);
3643 gcc_assert (mode != BLKmode);
3645 last_insn = emit_move_insn_1 (x, y);
3647 if (y_cst && REG_P (x)
3648 && (set = single_set (last_insn)) != NULL_RTX
3649 && SET_DEST (set) == x
3650 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3651 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3653 return last_insn;
3656 /* If Y is representable exactly in a narrower mode, and the target can
3657 perform the extension directly from constant or memory, then emit the
3658 move as an extension. */
3660 static rtx
3661 compress_float_constant (rtx x, rtx y)
3663 enum machine_mode dstmode = GET_MODE (x);
3664 enum machine_mode orig_srcmode = GET_MODE (y);
3665 enum machine_mode srcmode;
3666 REAL_VALUE_TYPE r;
3667 int oldcost, newcost;
3668 bool speed = optimize_insn_for_speed_p ();
3670 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3672 if (targetm.legitimate_constant_p (dstmode, y))
3673 oldcost = set_src_cost (y, speed);
3674 else
3675 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3677 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3678 srcmode != orig_srcmode;
3679 srcmode = GET_MODE_WIDER_MODE (srcmode))
3681 enum insn_code ic;
3682 rtx trunc_y, last_insn;
3684 /* Skip if the target can't extend this way. */
3685 ic = can_extend_p (dstmode, srcmode, 0);
3686 if (ic == CODE_FOR_nothing)
3687 continue;
3689 /* Skip if the narrowed value isn't exact. */
3690 if (! exact_real_truncate (srcmode, &r))
3691 continue;
3693 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3695 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3697 /* Skip if the target needs extra instructions to perform
3698 the extension. */
3699 if (!insn_operand_matches (ic, 1, trunc_y))
3700 continue;
3701 /* This is valid, but may not be cheaper than the original. */
3702 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3703 speed);
3704 if (oldcost < newcost)
3705 continue;
3707 else if (float_extend_from_mem[dstmode][srcmode])
3709 trunc_y = force_const_mem (srcmode, trunc_y);
3710 /* This is valid, but may not be cheaper than the original. */
3711 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3712 speed);
3713 if (oldcost < newcost)
3714 continue;
3715 trunc_y = validize_mem (trunc_y);
3717 else
3718 continue;
3720 /* For CSE's benefit, force the compressed constant pool entry
3721 into a new pseudo. This constant may be used in different modes,
3722 and if not, combine will put things back together for us. */
3723 trunc_y = force_reg (srcmode, trunc_y);
3725 /* If x is a hard register, perform the extension into a pseudo,
3726 so that e.g. stack realignment code is aware of it. */
3727 rtx target = x;
3728 if (REG_P (x) && HARD_REGISTER_P (x))
3729 target = gen_reg_rtx (dstmode);
3731 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3732 last_insn = get_last_insn ();
3734 if (REG_P (target))
3735 set_unique_reg_note (last_insn, REG_EQUAL, y);
3737 if (target != x)
3738 return emit_move_insn (x, target);
3739 return last_insn;
3742 return NULL_RTX;
3745 /* Pushing data onto the stack. */
3747 /* Push a block of length SIZE (perhaps variable)
3748 and return an rtx to address the beginning of the block.
3749 The value may be virtual_outgoing_args_rtx.
3751 EXTRA is the number of bytes of padding to push in addition to SIZE.
3752 BELOW nonzero means this padding comes at low addresses;
3753 otherwise, the padding comes at high addresses. */
3756 push_block (rtx size, int extra, int below)
3758 rtx temp;
3760 size = convert_modes (Pmode, ptr_mode, size, 1);
3761 if (CONSTANT_P (size))
3762 anti_adjust_stack (plus_constant (Pmode, size, extra));
3763 else if (REG_P (size) && extra == 0)
3764 anti_adjust_stack (size);
3765 else
3767 temp = copy_to_mode_reg (Pmode, size);
3768 if (extra != 0)
3769 temp = expand_binop (Pmode, add_optab, temp,
3770 gen_int_mode (extra, Pmode),
3771 temp, 0, OPTAB_LIB_WIDEN);
3772 anti_adjust_stack (temp);
3775 #ifndef STACK_GROWS_DOWNWARD
3776 if (0)
3777 #else
3778 if (1)
3779 #endif
3781 temp = virtual_outgoing_args_rtx;
3782 if (extra != 0 && below)
3783 temp = plus_constant (Pmode, temp, extra);
3785 else
3787 if (CONST_INT_P (size))
3788 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3789 -INTVAL (size) - (below ? 0 : extra));
3790 else if (extra != 0 && !below)
3791 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3792 negate_rtx (Pmode, plus_constant (Pmode, size,
3793 extra)));
3794 else
3795 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3796 negate_rtx (Pmode, size));
3799 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3802 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3804 static rtx
3805 mem_autoinc_base (rtx mem)
3807 if (MEM_P (mem))
3809 rtx addr = XEXP (mem, 0);
3810 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3811 return XEXP (addr, 0);
3813 return NULL;
3816 /* A utility routine used here, in reload, and in try_split. The insns
3817 after PREV up to and including LAST are known to adjust the stack,
3818 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3819 placing notes as appropriate. PREV may be NULL, indicating the
3820 entire insn sequence prior to LAST should be scanned.
3822 The set of allowed stack pointer modifications is small:
3823 (1) One or more auto-inc style memory references (aka pushes),
3824 (2) One or more addition/subtraction with the SP as destination,
3825 (3) A single move insn with the SP as destination,
3826 (4) A call_pop insn,
3827 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3829 Insns in the sequence that do not modify the SP are ignored,
3830 except for noreturn calls.
3832 The return value is the amount of adjustment that can be trivially
3833 verified, via immediate operand or auto-inc. If the adjustment
3834 cannot be trivially extracted, the return value is INT_MIN. */
3836 HOST_WIDE_INT
3837 find_args_size_adjust (rtx insn)
3839 rtx dest, set, pat;
3840 int i;
3842 pat = PATTERN (insn);
3843 set = NULL;
3845 /* Look for a call_pop pattern. */
3846 if (CALL_P (insn))
3848 /* We have to allow non-call_pop patterns for the case
3849 of emit_single_push_insn of a TLS address. */
3850 if (GET_CODE (pat) != PARALLEL)
3851 return 0;
3853 /* All call_pop have a stack pointer adjust in the parallel.
3854 The call itself is always first, and the stack adjust is
3855 usually last, so search from the end. */
3856 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3858 set = XVECEXP (pat, 0, i);
3859 if (GET_CODE (set) != SET)
3860 continue;
3861 dest = SET_DEST (set);
3862 if (dest == stack_pointer_rtx)
3863 break;
3865 /* We'd better have found the stack pointer adjust. */
3866 if (i == 0)
3867 return 0;
3868 /* Fall through to process the extracted SET and DEST
3869 as if it was a standalone insn. */
3871 else if (GET_CODE (pat) == SET)
3872 set = pat;
3873 else if ((set = single_set (insn)) != NULL)
3875 else if (GET_CODE (pat) == PARALLEL)
3877 /* ??? Some older ports use a parallel with a stack adjust
3878 and a store for a PUSH_ROUNDING pattern, rather than a
3879 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3880 /* ??? See h8300 and m68k, pushqi1. */
3881 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3883 set = XVECEXP (pat, 0, i);
3884 if (GET_CODE (set) != SET)
3885 continue;
3886 dest = SET_DEST (set);
3887 if (dest == stack_pointer_rtx)
3888 break;
3890 /* We do not expect an auto-inc of the sp in the parallel. */
3891 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3892 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3893 != stack_pointer_rtx);
3895 if (i < 0)
3896 return 0;
3898 else
3899 return 0;
3901 dest = SET_DEST (set);
3903 /* Look for direct modifications of the stack pointer. */
3904 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3906 /* Look for a trivial adjustment, otherwise assume nothing. */
3907 /* Note that the SPU restore_stack_block pattern refers to
3908 the stack pointer in V4SImode. Consider that non-trivial. */
3909 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3910 && GET_CODE (SET_SRC (set)) == PLUS
3911 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3912 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3913 return INTVAL (XEXP (SET_SRC (set), 1));
3914 /* ??? Reload can generate no-op moves, which will be cleaned
3915 up later. Recognize it and continue searching. */
3916 else if (rtx_equal_p (dest, SET_SRC (set)))
3917 return 0;
3918 else
3919 return HOST_WIDE_INT_MIN;
3921 else
3923 rtx mem, addr;
3925 /* Otherwise only think about autoinc patterns. */
3926 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3928 mem = dest;
3929 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3930 != stack_pointer_rtx);
3932 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3933 mem = SET_SRC (set);
3934 else
3935 return 0;
3937 addr = XEXP (mem, 0);
3938 switch (GET_CODE (addr))
3940 case PRE_INC:
3941 case POST_INC:
3942 return GET_MODE_SIZE (GET_MODE (mem));
3943 case PRE_DEC:
3944 case POST_DEC:
3945 return -GET_MODE_SIZE (GET_MODE (mem));
3946 case PRE_MODIFY:
3947 case POST_MODIFY:
3948 addr = XEXP (addr, 1);
3949 gcc_assert (GET_CODE (addr) == PLUS);
3950 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3951 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3952 return INTVAL (XEXP (addr, 1));
3953 default:
3954 gcc_unreachable ();
3960 fixup_args_size_notes (rtx prev, rtx last, int end_args_size)
3962 int args_size = end_args_size;
3963 bool saw_unknown = false;
3964 rtx insn;
3966 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3968 HOST_WIDE_INT this_delta;
3970 if (!NONDEBUG_INSN_P (insn))
3971 continue;
3973 this_delta = find_args_size_adjust (insn);
3974 if (this_delta == 0)
3976 if (!CALL_P (insn)
3977 || ACCUMULATE_OUTGOING_ARGS
3978 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3979 continue;
3982 gcc_assert (!saw_unknown);
3983 if (this_delta == HOST_WIDE_INT_MIN)
3984 saw_unknown = true;
3986 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3987 #ifdef STACK_GROWS_DOWNWARD
3988 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3989 #endif
3990 args_size -= this_delta;
3993 return saw_unknown ? INT_MIN : args_size;
3996 #ifdef PUSH_ROUNDING
3997 /* Emit single push insn. */
3999 static void
4000 emit_single_push_insn_1 (enum machine_mode mode, rtx x, tree type)
4002 rtx dest_addr;
4003 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4004 rtx dest;
4005 enum insn_code icode;
4007 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4008 /* If there is push pattern, use it. Otherwise try old way of throwing
4009 MEM representing push operation to move expander. */
4010 icode = optab_handler (push_optab, mode);
4011 if (icode != CODE_FOR_nothing)
4013 struct expand_operand ops[1];
4015 create_input_operand (&ops[0], x, mode);
4016 if (maybe_expand_insn (icode, 1, ops))
4017 return;
4019 if (GET_MODE_SIZE (mode) == rounded_size)
4020 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4021 /* If we are to pad downward, adjust the stack pointer first and
4022 then store X into the stack location using an offset. This is
4023 because emit_move_insn does not know how to pad; it does not have
4024 access to type. */
4025 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4027 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4028 HOST_WIDE_INT offset;
4030 emit_move_insn (stack_pointer_rtx,
4031 expand_binop (Pmode,
4032 #ifdef STACK_GROWS_DOWNWARD
4033 sub_optab,
4034 #else
4035 add_optab,
4036 #endif
4037 stack_pointer_rtx,
4038 gen_int_mode (rounded_size, Pmode),
4039 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4041 offset = (HOST_WIDE_INT) padding_size;
4042 #ifdef STACK_GROWS_DOWNWARD
4043 if (STACK_PUSH_CODE == POST_DEC)
4044 /* We have already decremented the stack pointer, so get the
4045 previous value. */
4046 offset += (HOST_WIDE_INT) rounded_size;
4047 #else
4048 if (STACK_PUSH_CODE == POST_INC)
4049 /* We have already incremented the stack pointer, so get the
4050 previous value. */
4051 offset -= (HOST_WIDE_INT) rounded_size;
4052 #endif
4053 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4054 gen_int_mode (offset, Pmode));
4056 else
4058 #ifdef STACK_GROWS_DOWNWARD
4059 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4060 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4061 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4062 Pmode));
4063 #else
4064 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4065 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4066 gen_int_mode (rounded_size, Pmode));
4067 #endif
4068 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4071 dest = gen_rtx_MEM (mode, dest_addr);
4073 if (type != 0)
4075 set_mem_attributes (dest, type, 1);
4077 if (cfun->tail_call_marked)
4078 /* Function incoming arguments may overlap with sibling call
4079 outgoing arguments and we cannot allow reordering of reads
4080 from function arguments with stores to outgoing arguments
4081 of sibling calls. */
4082 set_mem_alias_set (dest, 0);
4084 emit_move_insn (dest, x);
4087 /* Emit and annotate a single push insn. */
4089 static void
4090 emit_single_push_insn (enum machine_mode mode, rtx x, tree type)
4092 int delta, old_delta = stack_pointer_delta;
4093 rtx prev = get_last_insn ();
4094 rtx last;
4096 emit_single_push_insn_1 (mode, x, type);
4098 last = get_last_insn ();
4100 /* Notice the common case where we emitted exactly one insn. */
4101 if (PREV_INSN (last) == prev)
4103 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4104 return;
4107 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4108 gcc_assert (delta == INT_MIN || delta == old_delta);
4110 #endif
4112 /* Generate code to push X onto the stack, assuming it has mode MODE and
4113 type TYPE.
4114 MODE is redundant except when X is a CONST_INT (since they don't
4115 carry mode info).
4116 SIZE is an rtx for the size of data to be copied (in bytes),
4117 needed only if X is BLKmode.
4119 ALIGN (in bits) is maximum alignment we can assume.
4121 If PARTIAL and REG are both nonzero, then copy that many of the first
4122 bytes of X into registers starting with REG, and push the rest of X.
4123 The amount of space pushed is decreased by PARTIAL bytes.
4124 REG must be a hard register in this case.
4125 If REG is zero but PARTIAL is not, take any all others actions for an
4126 argument partially in registers, but do not actually load any
4127 registers.
4129 EXTRA is the amount in bytes of extra space to leave next to this arg.
4130 This is ignored if an argument block has already been allocated.
4132 On a machine that lacks real push insns, ARGS_ADDR is the address of
4133 the bottom of the argument block for this call. We use indexing off there
4134 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4135 argument block has not been preallocated.
4137 ARGS_SO_FAR is the size of args previously pushed for this call.
4139 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4140 for arguments passed in registers. If nonzero, it will be the number
4141 of bytes required. */
4143 void
4144 emit_push_insn (rtx x, enum machine_mode mode, tree type, rtx size,
4145 unsigned int align, int partial, rtx reg, int extra,
4146 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4147 rtx alignment_pad)
4149 rtx xinner;
4150 enum direction stack_direction
4151 #ifdef STACK_GROWS_DOWNWARD
4152 = downward;
4153 #else
4154 = upward;
4155 #endif
4157 /* Decide where to pad the argument: `downward' for below,
4158 `upward' for above, or `none' for don't pad it.
4159 Default is below for small data on big-endian machines; else above. */
4160 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4162 /* Invert direction if stack is post-decrement.
4163 FIXME: why? */
4164 if (STACK_PUSH_CODE == POST_DEC)
4165 if (where_pad != none)
4166 where_pad = (where_pad == downward ? upward : downward);
4168 xinner = x;
4170 if (mode == BLKmode
4171 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4173 /* Copy a block into the stack, entirely or partially. */
4175 rtx temp;
4176 int used;
4177 int offset;
4178 int skip;
4180 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4181 used = partial - offset;
4183 if (mode != BLKmode)
4185 /* A value is to be stored in an insufficiently aligned
4186 stack slot; copy via a suitably aligned slot if
4187 necessary. */
4188 size = GEN_INT (GET_MODE_SIZE (mode));
4189 if (!MEM_P (xinner))
4191 temp = assign_temp (type, 1, 1);
4192 emit_move_insn (temp, xinner);
4193 xinner = temp;
4197 gcc_assert (size);
4199 /* USED is now the # of bytes we need not copy to the stack
4200 because registers will take care of them. */
4202 if (partial != 0)
4203 xinner = adjust_address (xinner, BLKmode, used);
4205 /* If the partial register-part of the arg counts in its stack size,
4206 skip the part of stack space corresponding to the registers.
4207 Otherwise, start copying to the beginning of the stack space,
4208 by setting SKIP to 0. */
4209 skip = (reg_parm_stack_space == 0) ? 0 : used;
4211 #ifdef PUSH_ROUNDING
4212 /* Do it with several push insns if that doesn't take lots of insns
4213 and if there is no difficulty with push insns that skip bytes
4214 on the stack for alignment purposes. */
4215 if (args_addr == 0
4216 && PUSH_ARGS
4217 && CONST_INT_P (size)
4218 && skip == 0
4219 && MEM_ALIGN (xinner) >= align
4220 && (MOVE_BY_PIECES_P ((unsigned) INTVAL (size) - used, align))
4221 /* Here we avoid the case of a structure whose weak alignment
4222 forces many pushes of a small amount of data,
4223 and such small pushes do rounding that causes trouble. */
4224 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4225 || align >= BIGGEST_ALIGNMENT
4226 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4227 == (align / BITS_PER_UNIT)))
4228 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4230 /* Push padding now if padding above and stack grows down,
4231 or if padding below and stack grows up.
4232 But if space already allocated, this has already been done. */
4233 if (extra && args_addr == 0
4234 && where_pad != none && where_pad != stack_direction)
4235 anti_adjust_stack (GEN_INT (extra));
4237 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4239 else
4240 #endif /* PUSH_ROUNDING */
4242 rtx target;
4244 /* Otherwise make space on the stack and copy the data
4245 to the address of that space. */
4247 /* Deduct words put into registers from the size we must copy. */
4248 if (partial != 0)
4250 if (CONST_INT_P (size))
4251 size = GEN_INT (INTVAL (size) - used);
4252 else
4253 size = expand_binop (GET_MODE (size), sub_optab, size,
4254 gen_int_mode (used, GET_MODE (size)),
4255 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4258 /* Get the address of the stack space.
4259 In this case, we do not deal with EXTRA separately.
4260 A single stack adjust will do. */
4261 if (! args_addr)
4263 temp = push_block (size, extra, where_pad == downward);
4264 extra = 0;
4266 else if (CONST_INT_P (args_so_far))
4267 temp = memory_address (BLKmode,
4268 plus_constant (Pmode, args_addr,
4269 skip + INTVAL (args_so_far)));
4270 else
4271 temp = memory_address (BLKmode,
4272 plus_constant (Pmode,
4273 gen_rtx_PLUS (Pmode,
4274 args_addr,
4275 args_so_far),
4276 skip));
4278 if (!ACCUMULATE_OUTGOING_ARGS)
4280 /* If the source is referenced relative to the stack pointer,
4281 copy it to another register to stabilize it. We do not need
4282 to do this if we know that we won't be changing sp. */
4284 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4285 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4286 temp = copy_to_reg (temp);
4289 target = gen_rtx_MEM (BLKmode, temp);
4291 /* We do *not* set_mem_attributes here, because incoming arguments
4292 may overlap with sibling call outgoing arguments and we cannot
4293 allow reordering of reads from function arguments with stores
4294 to outgoing arguments of sibling calls. We do, however, want
4295 to record the alignment of the stack slot. */
4296 /* ALIGN may well be better aligned than TYPE, e.g. due to
4297 PARM_BOUNDARY. Assume the caller isn't lying. */
4298 set_mem_align (target, align);
4300 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4303 else if (partial > 0)
4305 /* Scalar partly in registers. */
4307 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4308 int i;
4309 int not_stack;
4310 /* # bytes of start of argument
4311 that we must make space for but need not store. */
4312 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4313 int args_offset = INTVAL (args_so_far);
4314 int skip;
4316 /* Push padding now if padding above and stack grows down,
4317 or if padding below and stack grows up.
4318 But if space already allocated, this has already been done. */
4319 if (extra && args_addr == 0
4320 && where_pad != none && where_pad != stack_direction)
4321 anti_adjust_stack (GEN_INT (extra));
4323 /* If we make space by pushing it, we might as well push
4324 the real data. Otherwise, we can leave OFFSET nonzero
4325 and leave the space uninitialized. */
4326 if (args_addr == 0)
4327 offset = 0;
4329 /* Now NOT_STACK gets the number of words that we don't need to
4330 allocate on the stack. Convert OFFSET to words too. */
4331 not_stack = (partial - offset) / UNITS_PER_WORD;
4332 offset /= UNITS_PER_WORD;
4334 /* If the partial register-part of the arg counts in its stack size,
4335 skip the part of stack space corresponding to the registers.
4336 Otherwise, start copying to the beginning of the stack space,
4337 by setting SKIP to 0. */
4338 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4340 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4341 x = validize_mem (force_const_mem (mode, x));
4343 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4344 SUBREGs of such registers are not allowed. */
4345 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4346 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4347 x = copy_to_reg (x);
4349 /* Loop over all the words allocated on the stack for this arg. */
4350 /* We can do it by words, because any scalar bigger than a word
4351 has a size a multiple of a word. */
4352 for (i = size - 1; i >= not_stack; i--)
4353 if (i >= not_stack + offset)
4354 emit_push_insn (operand_subword_force (x, i, mode),
4355 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4356 0, args_addr,
4357 GEN_INT (args_offset + ((i - not_stack + skip)
4358 * UNITS_PER_WORD)),
4359 reg_parm_stack_space, alignment_pad);
4361 else
4363 rtx addr;
4364 rtx dest;
4366 /* Push padding now if padding above and stack grows down,
4367 or if padding below and stack grows up.
4368 But if space already allocated, this has already been done. */
4369 if (extra && args_addr == 0
4370 && where_pad != none && where_pad != stack_direction)
4371 anti_adjust_stack (GEN_INT (extra));
4373 #ifdef PUSH_ROUNDING
4374 if (args_addr == 0 && PUSH_ARGS)
4375 emit_single_push_insn (mode, x, type);
4376 else
4377 #endif
4379 if (CONST_INT_P (args_so_far))
4380 addr
4381 = memory_address (mode,
4382 plus_constant (Pmode, args_addr,
4383 INTVAL (args_so_far)));
4384 else
4385 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4386 args_so_far));
4387 dest = gen_rtx_MEM (mode, addr);
4389 /* We do *not* set_mem_attributes here, because incoming arguments
4390 may overlap with sibling call outgoing arguments and we cannot
4391 allow reordering of reads from function arguments with stores
4392 to outgoing arguments of sibling calls. We do, however, want
4393 to record the alignment of the stack slot. */
4394 /* ALIGN may well be better aligned than TYPE, e.g. due to
4395 PARM_BOUNDARY. Assume the caller isn't lying. */
4396 set_mem_align (dest, align);
4398 emit_move_insn (dest, x);
4402 /* If part should go in registers, copy that part
4403 into the appropriate registers. Do this now, at the end,
4404 since mem-to-mem copies above may do function calls. */
4405 if (partial > 0 && reg != 0)
4407 /* Handle calls that pass values in multiple non-contiguous locations.
4408 The Irix 6 ABI has examples of this. */
4409 if (GET_CODE (reg) == PARALLEL)
4410 emit_group_load (reg, x, type, -1);
4411 else
4413 gcc_assert (partial % UNITS_PER_WORD == 0);
4414 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4418 if (extra && args_addr == 0 && where_pad == stack_direction)
4419 anti_adjust_stack (GEN_INT (extra));
4421 if (alignment_pad && args_addr == 0)
4422 anti_adjust_stack (alignment_pad);
4425 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4426 operations. */
4428 static rtx
4429 get_subtarget (rtx x)
4431 return (optimize
4432 || x == 0
4433 /* Only registers can be subtargets. */
4434 || !REG_P (x)
4435 /* Don't use hard regs to avoid extending their life. */
4436 || REGNO (x) < FIRST_PSEUDO_REGISTER
4437 ? 0 : x);
4440 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4441 FIELD is a bitfield. Returns true if the optimization was successful,
4442 and there's nothing else to do. */
4444 static bool
4445 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4446 unsigned HOST_WIDE_INT bitpos,
4447 unsigned HOST_WIDE_INT bitregion_start,
4448 unsigned HOST_WIDE_INT bitregion_end,
4449 enum machine_mode mode1, rtx str_rtx,
4450 tree to, tree src)
4452 enum machine_mode str_mode = GET_MODE (str_rtx);
4453 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4454 tree op0, op1;
4455 rtx value, result;
4456 optab binop;
4457 gimple srcstmt;
4458 enum tree_code code;
4460 if (mode1 != VOIDmode
4461 || bitsize >= BITS_PER_WORD
4462 || str_bitsize > BITS_PER_WORD
4463 || TREE_SIDE_EFFECTS (to)
4464 || TREE_THIS_VOLATILE (to))
4465 return false;
4467 STRIP_NOPS (src);
4468 if (TREE_CODE (src) != SSA_NAME)
4469 return false;
4470 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4471 return false;
4473 srcstmt = get_gimple_for_ssa_name (src);
4474 if (!srcstmt
4475 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4476 return false;
4478 code = gimple_assign_rhs_code (srcstmt);
4480 op0 = gimple_assign_rhs1 (srcstmt);
4482 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4483 to find its initialization. Hopefully the initialization will
4484 be from a bitfield load. */
4485 if (TREE_CODE (op0) == SSA_NAME)
4487 gimple op0stmt = get_gimple_for_ssa_name (op0);
4489 /* We want to eventually have OP0 be the same as TO, which
4490 should be a bitfield. */
4491 if (!op0stmt
4492 || !is_gimple_assign (op0stmt)
4493 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4494 return false;
4495 op0 = gimple_assign_rhs1 (op0stmt);
4498 op1 = gimple_assign_rhs2 (srcstmt);
4500 if (!operand_equal_p (to, op0, 0))
4501 return false;
4503 if (MEM_P (str_rtx))
4505 unsigned HOST_WIDE_INT offset1;
4507 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4508 str_mode = word_mode;
4509 str_mode = get_best_mode (bitsize, bitpos,
4510 bitregion_start, bitregion_end,
4511 MEM_ALIGN (str_rtx), str_mode, 0);
4512 if (str_mode == VOIDmode)
4513 return false;
4514 str_bitsize = GET_MODE_BITSIZE (str_mode);
4516 offset1 = bitpos;
4517 bitpos %= str_bitsize;
4518 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4519 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4521 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4522 return false;
4524 /* If the bit field covers the whole REG/MEM, store_field
4525 will likely generate better code. */
4526 if (bitsize >= str_bitsize)
4527 return false;
4529 /* We can't handle fields split across multiple entities. */
4530 if (bitpos + bitsize > str_bitsize)
4531 return false;
4533 if (BYTES_BIG_ENDIAN)
4534 bitpos = str_bitsize - bitpos - bitsize;
4536 switch (code)
4538 case PLUS_EXPR:
4539 case MINUS_EXPR:
4540 /* For now, just optimize the case of the topmost bitfield
4541 where we don't need to do any masking and also
4542 1 bit bitfields where xor can be used.
4543 We might win by one instruction for the other bitfields
4544 too if insv/extv instructions aren't used, so that
4545 can be added later. */
4546 if (bitpos + bitsize != str_bitsize
4547 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4548 break;
4550 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4551 value = convert_modes (str_mode,
4552 TYPE_MODE (TREE_TYPE (op1)), value,
4553 TYPE_UNSIGNED (TREE_TYPE (op1)));
4555 /* We may be accessing data outside the field, which means
4556 we can alias adjacent data. */
4557 if (MEM_P (str_rtx))
4559 str_rtx = shallow_copy_rtx (str_rtx);
4560 set_mem_alias_set (str_rtx, 0);
4561 set_mem_expr (str_rtx, 0);
4564 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4565 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4567 value = expand_and (str_mode, value, const1_rtx, NULL);
4568 binop = xor_optab;
4570 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4571 result = expand_binop (str_mode, binop, str_rtx,
4572 value, str_rtx, 1, OPTAB_WIDEN);
4573 if (result != str_rtx)
4574 emit_move_insn (str_rtx, result);
4575 return true;
4577 case BIT_IOR_EXPR:
4578 case BIT_XOR_EXPR:
4579 if (TREE_CODE (op1) != INTEGER_CST)
4580 break;
4581 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4582 value = convert_modes (str_mode,
4583 TYPE_MODE (TREE_TYPE (op1)), value,
4584 TYPE_UNSIGNED (TREE_TYPE (op1)));
4586 /* We may be accessing data outside the field, which means
4587 we can alias adjacent data. */
4588 if (MEM_P (str_rtx))
4590 str_rtx = shallow_copy_rtx (str_rtx);
4591 set_mem_alias_set (str_rtx, 0);
4592 set_mem_expr (str_rtx, 0);
4595 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4596 if (bitpos + bitsize != str_bitsize)
4598 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4599 str_mode);
4600 value = expand_and (str_mode, value, mask, NULL_RTX);
4602 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4603 result = expand_binop (str_mode, binop, str_rtx,
4604 value, str_rtx, 1, OPTAB_WIDEN);
4605 if (result != str_rtx)
4606 emit_move_insn (str_rtx, result);
4607 return true;
4609 default:
4610 break;
4613 return false;
4616 /* In the C++ memory model, consecutive bit fields in a structure are
4617 considered one memory location.
4619 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4620 returns the bit range of consecutive bits in which this COMPONENT_REF
4621 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4622 and *OFFSET may be adjusted in the process.
4624 If the access does not need to be restricted, 0 is returned in both
4625 *BITSTART and *BITEND. */
4627 static void
4628 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4629 unsigned HOST_WIDE_INT *bitend,
4630 tree exp,
4631 HOST_WIDE_INT *bitpos,
4632 tree *offset)
4634 HOST_WIDE_INT bitoffset;
4635 tree field, repr;
4637 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4639 field = TREE_OPERAND (exp, 1);
4640 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4641 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4642 need to limit the range we can access. */
4643 if (!repr)
4645 *bitstart = *bitend = 0;
4646 return;
4649 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4650 part of a larger bit field, then the representative does not serve any
4651 useful purpose. This can occur in Ada. */
4652 if (handled_component_p (TREE_OPERAND (exp, 0)))
4654 enum machine_mode rmode;
4655 HOST_WIDE_INT rbitsize, rbitpos;
4656 tree roffset;
4657 int unsignedp;
4658 int volatilep = 0;
4659 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4660 &roffset, &rmode, &unsignedp, &volatilep, false);
4661 if ((rbitpos % BITS_PER_UNIT) != 0)
4663 *bitstart = *bitend = 0;
4664 return;
4668 /* Compute the adjustment to bitpos from the offset of the field
4669 relative to the representative. DECL_FIELD_OFFSET of field and
4670 repr are the same by construction if they are not constants,
4671 see finish_bitfield_layout. */
4672 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4673 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4674 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4675 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4676 else
4677 bitoffset = 0;
4678 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4679 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4681 /* If the adjustment is larger than bitpos, we would have a negative bit
4682 position for the lower bound and this may wreak havoc later. Adjust
4683 offset and bitpos to make the lower bound non-negative in that case. */
4684 if (bitoffset > *bitpos)
4686 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4687 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4689 *bitpos += adjust;
4690 if (*offset == NULL_TREE)
4691 *offset = size_int (-adjust / BITS_PER_UNIT);
4692 else
4693 *offset
4694 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4695 *bitstart = 0;
4697 else
4698 *bitstart = *bitpos - bitoffset;
4700 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4703 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4704 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4705 DECL_RTL was not set yet, return NORTL. */
4707 static inline bool
4708 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4710 if (TREE_CODE (addr) != ADDR_EXPR)
4711 return false;
4713 tree base = TREE_OPERAND (addr, 0);
4715 if (!DECL_P (base)
4716 || TREE_ADDRESSABLE (base)
4717 || DECL_MODE (base) == BLKmode)
4718 return false;
4720 if (!DECL_RTL_SET_P (base))
4721 return nortl;
4723 return (!MEM_P (DECL_RTL (base)));
4726 /* Returns true if the MEM_REF REF refers to an object that does not
4727 reside in memory and has non-BLKmode. */
4729 static inline bool
4730 mem_ref_refers_to_non_mem_p (tree ref)
4732 tree base = TREE_OPERAND (ref, 0);
4733 return addr_expr_of_non_mem_decl_p_1 (base, false);
4736 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4737 is true, try generating a nontemporal store. */
4739 void
4740 expand_assignment (tree to, tree from, bool nontemporal)
4742 rtx to_rtx = 0;
4743 rtx result;
4744 enum machine_mode mode;
4745 unsigned int align;
4746 enum insn_code icode;
4748 /* Don't crash if the lhs of the assignment was erroneous. */
4749 if (TREE_CODE (to) == ERROR_MARK)
4751 expand_normal (from);
4752 return;
4755 /* Optimize away no-op moves without side-effects. */
4756 if (operand_equal_p (to, from, 0))
4757 return;
4759 /* Handle misaligned stores. */
4760 mode = TYPE_MODE (TREE_TYPE (to));
4761 if ((TREE_CODE (to) == MEM_REF
4762 || TREE_CODE (to) == TARGET_MEM_REF)
4763 && mode != BLKmode
4764 && !mem_ref_refers_to_non_mem_p (to)
4765 && ((align = get_object_alignment (to))
4766 < GET_MODE_ALIGNMENT (mode))
4767 && (((icode = optab_handler (movmisalign_optab, mode))
4768 != CODE_FOR_nothing)
4769 || SLOW_UNALIGNED_ACCESS (mode, align)))
4771 rtx reg, mem;
4773 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4774 reg = force_not_mem (reg);
4775 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4777 if (icode != CODE_FOR_nothing)
4779 struct expand_operand ops[2];
4781 create_fixed_operand (&ops[0], mem);
4782 create_input_operand (&ops[1], reg, mode);
4783 /* The movmisalign<mode> pattern cannot fail, else the assignment
4784 would silently be omitted. */
4785 expand_insn (icode, 2, ops);
4787 else
4788 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4789 return;
4792 /* Assignment of a structure component needs special treatment
4793 if the structure component's rtx is not simply a MEM.
4794 Assignment of an array element at a constant index, and assignment of
4795 an array element in an unaligned packed structure field, has the same
4796 problem. Same for (partially) storing into a non-memory object. */
4797 if (handled_component_p (to)
4798 || (TREE_CODE (to) == MEM_REF
4799 && mem_ref_refers_to_non_mem_p (to))
4800 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4802 enum machine_mode mode1;
4803 HOST_WIDE_INT bitsize, bitpos;
4804 unsigned HOST_WIDE_INT bitregion_start = 0;
4805 unsigned HOST_WIDE_INT bitregion_end = 0;
4806 tree offset;
4807 int unsignedp;
4808 int volatilep = 0;
4809 tree tem;
4811 push_temp_slots ();
4812 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4813 &unsignedp, &volatilep, true);
4815 /* Make sure bitpos is not negative, it can wreak havoc later. */
4816 if (bitpos < 0)
4818 gcc_assert (offset == NULL_TREE);
4819 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4820 ? 3 : exact_log2 (BITS_PER_UNIT)));
4821 bitpos &= BITS_PER_UNIT - 1;
4824 if (TREE_CODE (to) == COMPONENT_REF
4825 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4826 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4827 /* The C++ memory model naturally applies to byte-aligned fields.
4828 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4829 BITSIZE are not byte-aligned, there is no need to limit the range
4830 we can access. This can occur with packed structures in Ada. */
4831 else if (bitsize > 0
4832 && bitsize % BITS_PER_UNIT == 0
4833 && bitpos % BITS_PER_UNIT == 0)
4835 bitregion_start = bitpos;
4836 bitregion_end = bitpos + bitsize - 1;
4839 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4841 /* If the field has a mode, we want to access it in the
4842 field's mode, not the computed mode.
4843 If a MEM has VOIDmode (external with incomplete type),
4844 use BLKmode for it instead. */
4845 if (MEM_P (to_rtx))
4847 if (mode1 != VOIDmode)
4848 to_rtx = adjust_address (to_rtx, mode1, 0);
4849 else if (GET_MODE (to_rtx) == VOIDmode)
4850 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4853 if (offset != 0)
4855 enum machine_mode address_mode;
4856 rtx offset_rtx;
4858 if (!MEM_P (to_rtx))
4860 /* We can get constant negative offsets into arrays with broken
4861 user code. Translate this to a trap instead of ICEing. */
4862 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4863 expand_builtin_trap ();
4864 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4867 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4868 address_mode = get_address_mode (to_rtx);
4869 if (GET_MODE (offset_rtx) != address_mode)
4870 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4872 /* The check for a constant address in TO_RTX not having VOIDmode
4873 is probably no longer necessary. */
4874 if (MEM_P (to_rtx)
4875 && GET_MODE (to_rtx) == BLKmode
4876 && GET_MODE (XEXP (to_rtx, 0)) != VOIDmode
4877 && bitsize > 0
4878 && (bitpos % bitsize) == 0
4879 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4880 && MEM_ALIGN (to_rtx) == GET_MODE_ALIGNMENT (mode1))
4882 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4883 bitregion_start = 0;
4884 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4885 bitregion_end -= bitpos;
4886 bitpos = 0;
4889 to_rtx = offset_address (to_rtx, offset_rtx,
4890 highest_pow2_factor_for_target (to,
4891 offset));
4894 /* No action is needed if the target is not a memory and the field
4895 lies completely outside that target. This can occur if the source
4896 code contains an out-of-bounds access to a small array. */
4897 if (!MEM_P (to_rtx)
4898 && GET_MODE (to_rtx) != BLKmode
4899 && (unsigned HOST_WIDE_INT) bitpos
4900 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4902 expand_normal (from);
4903 result = NULL;
4905 /* Handle expand_expr of a complex value returning a CONCAT. */
4906 else if (GET_CODE (to_rtx) == CONCAT)
4908 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4909 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4910 && bitpos == 0
4911 && bitsize == mode_bitsize)
4912 result = store_expr (from, to_rtx, false, nontemporal);
4913 else if (bitsize == mode_bitsize / 2
4914 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4915 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4916 nontemporal);
4917 else if (bitpos + bitsize <= mode_bitsize / 2)
4918 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4919 bitregion_start, bitregion_end,
4920 mode1, from,
4921 get_alias_set (to), nontemporal);
4922 else if (bitpos >= mode_bitsize / 2)
4923 result = store_field (XEXP (to_rtx, 1), bitsize,
4924 bitpos - mode_bitsize / 2,
4925 bitregion_start, bitregion_end,
4926 mode1, from,
4927 get_alias_set (to), nontemporal);
4928 else if (bitpos == 0 && bitsize == mode_bitsize)
4930 rtx from_rtx;
4931 result = expand_normal (from);
4932 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4933 TYPE_MODE (TREE_TYPE (from)), 0);
4934 emit_move_insn (XEXP (to_rtx, 0),
4935 read_complex_part (from_rtx, false));
4936 emit_move_insn (XEXP (to_rtx, 1),
4937 read_complex_part (from_rtx, true));
4939 else
4941 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4942 GET_MODE_SIZE (GET_MODE (to_rtx)));
4943 write_complex_part (temp, XEXP (to_rtx, 0), false);
4944 write_complex_part (temp, XEXP (to_rtx, 1), true);
4945 result = store_field (temp, bitsize, bitpos,
4946 bitregion_start, bitregion_end,
4947 mode1, from,
4948 get_alias_set (to), nontemporal);
4949 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4950 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4953 else
4955 if (MEM_P (to_rtx))
4957 /* If the field is at offset zero, we could have been given the
4958 DECL_RTX of the parent struct. Don't munge it. */
4959 to_rtx = shallow_copy_rtx (to_rtx);
4960 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4961 if (volatilep)
4962 MEM_VOLATILE_P (to_rtx) = 1;
4965 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4966 bitregion_start, bitregion_end,
4967 mode1,
4968 to_rtx, to, from))
4969 result = NULL;
4970 else
4971 result = store_field (to_rtx, bitsize, bitpos,
4972 bitregion_start, bitregion_end,
4973 mode1, from,
4974 get_alias_set (to), nontemporal);
4977 if (result)
4978 preserve_temp_slots (result);
4979 pop_temp_slots ();
4980 return;
4983 /* If the rhs is a function call and its value is not an aggregate,
4984 call the function before we start to compute the lhs.
4985 This is needed for correct code for cases such as
4986 val = setjmp (buf) on machines where reference to val
4987 requires loading up part of an address in a separate insn.
4989 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4990 since it might be a promoted variable where the zero- or sign- extension
4991 needs to be done. Handling this in the normal way is safe because no
4992 computation is done before the call. The same is true for SSA names. */
4993 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
4994 && COMPLETE_TYPE_P (TREE_TYPE (from))
4995 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
4996 && ! (((TREE_CODE (to) == VAR_DECL
4997 || TREE_CODE (to) == PARM_DECL
4998 || TREE_CODE (to) == RESULT_DECL)
4999 && REG_P (DECL_RTL (to)))
5000 || TREE_CODE (to) == SSA_NAME))
5002 rtx value;
5004 push_temp_slots ();
5005 value = expand_normal (from);
5006 if (to_rtx == 0)
5007 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5009 /* Handle calls that return values in multiple non-contiguous locations.
5010 The Irix 6 ABI has examples of this. */
5011 if (GET_CODE (to_rtx) == PARALLEL)
5013 if (GET_CODE (value) == PARALLEL)
5014 emit_group_move (to_rtx, value);
5015 else
5016 emit_group_load (to_rtx, value, TREE_TYPE (from),
5017 int_size_in_bytes (TREE_TYPE (from)));
5019 else if (GET_CODE (value) == PARALLEL)
5020 emit_group_store (to_rtx, value, TREE_TYPE (from),
5021 int_size_in_bytes (TREE_TYPE (from)));
5022 else if (GET_MODE (to_rtx) == BLKmode)
5024 /* Handle calls that return BLKmode values in registers. */
5025 if (REG_P (value))
5026 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5027 else
5028 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5030 else
5032 if (POINTER_TYPE_P (TREE_TYPE (to)))
5033 value = convert_memory_address_addr_space
5034 (GET_MODE (to_rtx), value,
5035 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5037 emit_move_insn (to_rtx, value);
5039 preserve_temp_slots (to_rtx);
5040 pop_temp_slots ();
5041 return;
5044 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5045 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5047 /* Don't move directly into a return register. */
5048 if (TREE_CODE (to) == RESULT_DECL
5049 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5051 rtx temp;
5053 push_temp_slots ();
5055 /* If the source is itself a return value, it still is in a pseudo at
5056 this point so we can move it back to the return register directly. */
5057 if (REG_P (to_rtx)
5058 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5059 && TREE_CODE (from) != CALL_EXPR)
5060 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5061 else
5062 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5064 /* Handle calls that return values in multiple non-contiguous locations.
5065 The Irix 6 ABI has examples of this. */
5066 if (GET_CODE (to_rtx) == PARALLEL)
5068 if (GET_CODE (temp) == PARALLEL)
5069 emit_group_move (to_rtx, temp);
5070 else
5071 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5072 int_size_in_bytes (TREE_TYPE (from)));
5074 else if (temp)
5075 emit_move_insn (to_rtx, temp);
5077 preserve_temp_slots (to_rtx);
5078 pop_temp_slots ();
5079 return;
5082 /* In case we are returning the contents of an object which overlaps
5083 the place the value is being stored, use a safe function when copying
5084 a value through a pointer into a structure value return block. */
5085 if (TREE_CODE (to) == RESULT_DECL
5086 && TREE_CODE (from) == INDIRECT_REF
5087 && ADDR_SPACE_GENERIC_P
5088 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5089 && refs_may_alias_p (to, from)
5090 && cfun->returns_struct
5091 && !cfun->returns_pcc_struct)
5093 rtx from_rtx, size;
5095 push_temp_slots ();
5096 size = expr_size (from);
5097 from_rtx = expand_normal (from);
5099 emit_library_call (memmove_libfunc, LCT_NORMAL,
5100 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5101 XEXP (from_rtx, 0), Pmode,
5102 convert_to_mode (TYPE_MODE (sizetype),
5103 size, TYPE_UNSIGNED (sizetype)),
5104 TYPE_MODE (sizetype));
5106 preserve_temp_slots (to_rtx);
5107 pop_temp_slots ();
5108 return;
5111 /* Compute FROM and store the value in the rtx we got. */
5113 push_temp_slots ();
5114 result = store_expr (from, to_rtx, 0, nontemporal);
5115 preserve_temp_slots (result);
5116 pop_temp_slots ();
5117 return;
5120 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5121 succeeded, false otherwise. */
5123 bool
5124 emit_storent_insn (rtx to, rtx from)
5126 struct expand_operand ops[2];
5127 enum machine_mode mode = GET_MODE (to);
5128 enum insn_code code = optab_handler (storent_optab, mode);
5130 if (code == CODE_FOR_nothing)
5131 return false;
5133 create_fixed_operand (&ops[0], to);
5134 create_input_operand (&ops[1], from, mode);
5135 return maybe_expand_insn (code, 2, ops);
5138 /* Generate code for computing expression EXP,
5139 and storing the value into TARGET.
5141 If the mode is BLKmode then we may return TARGET itself.
5142 It turns out that in BLKmode it doesn't cause a problem.
5143 because C has no operators that could combine two different
5144 assignments into the same BLKmode object with different values
5145 with no sequence point. Will other languages need this to
5146 be more thorough?
5148 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5149 stack, and block moves may need to be treated specially.
5151 If NONTEMPORAL is true, try using a nontemporal store instruction. */
5154 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5156 rtx temp;
5157 rtx alt_rtl = NULL_RTX;
5158 location_t loc = curr_insn_location ();
5160 if (VOID_TYPE_P (TREE_TYPE (exp)))
5162 /* C++ can generate ?: expressions with a throw expression in one
5163 branch and an rvalue in the other. Here, we resolve attempts to
5164 store the throw expression's nonexistent result. */
5165 gcc_assert (!call_param_p);
5166 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5167 return NULL_RTX;
5169 if (TREE_CODE (exp) == COMPOUND_EXPR)
5171 /* Perform first part of compound expression, then assign from second
5172 part. */
5173 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5174 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5175 return store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5176 nontemporal);
5178 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5180 /* For conditional expression, get safe form of the target. Then
5181 test the condition, doing the appropriate assignment on either
5182 side. This avoids the creation of unnecessary temporaries.
5183 For non-BLKmode, it is more efficient not to do this. */
5185 rtx lab1 = gen_label_rtx (), lab2 = gen_label_rtx ();
5187 do_pending_stack_adjust ();
5188 NO_DEFER_POP;
5189 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5190 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5191 nontemporal);
5192 emit_jump_insn (gen_jump (lab2));
5193 emit_barrier ();
5194 emit_label (lab1);
5195 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5196 nontemporal);
5197 emit_label (lab2);
5198 OK_DEFER_POP;
5200 return NULL_RTX;
5202 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5203 /* If this is a scalar in a register that is stored in a wider mode
5204 than the declared mode, compute the result into its declared mode
5205 and then convert to the wider mode. Our value is the computed
5206 expression. */
5208 rtx inner_target = 0;
5210 /* We can do the conversion inside EXP, which will often result
5211 in some optimizations. Do the conversion in two steps: first
5212 change the signedness, if needed, then the extend. But don't
5213 do this if the type of EXP is a subtype of something else
5214 since then the conversion might involve more than just
5215 converting modes. */
5216 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5217 && TREE_TYPE (TREE_TYPE (exp)) == 0
5218 && GET_MODE_PRECISION (GET_MODE (target))
5219 == TYPE_PRECISION (TREE_TYPE (exp)))
5221 if (TYPE_UNSIGNED (TREE_TYPE (exp))
5222 != SUBREG_PROMOTED_UNSIGNED_P (target))
5224 /* Some types, e.g. Fortran's logical*4, won't have a signed
5225 version, so use the mode instead. */
5226 tree ntype
5227 = (signed_or_unsigned_type_for
5228 (SUBREG_PROMOTED_UNSIGNED_P (target), TREE_TYPE (exp)));
5229 if (ntype == NULL)
5230 ntype = lang_hooks.types.type_for_mode
5231 (TYPE_MODE (TREE_TYPE (exp)),
5232 SUBREG_PROMOTED_UNSIGNED_P (target));
5234 exp = fold_convert_loc (loc, ntype, exp);
5237 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5238 (GET_MODE (SUBREG_REG (target)),
5239 SUBREG_PROMOTED_UNSIGNED_P (target)),
5240 exp);
5242 inner_target = SUBREG_REG (target);
5245 temp = expand_expr (exp, inner_target, VOIDmode,
5246 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5248 /* If TEMP is a VOIDmode constant, use convert_modes to make
5249 sure that we properly convert it. */
5250 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5252 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5253 temp, SUBREG_PROMOTED_UNSIGNED_P (target));
5254 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5255 GET_MODE (target), temp,
5256 SUBREG_PROMOTED_UNSIGNED_P (target));
5259 convert_move (SUBREG_REG (target), temp,
5260 SUBREG_PROMOTED_UNSIGNED_P (target));
5262 return NULL_RTX;
5264 else if ((TREE_CODE (exp) == STRING_CST
5265 || (TREE_CODE (exp) == MEM_REF
5266 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5267 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5268 == STRING_CST
5269 && integer_zerop (TREE_OPERAND (exp, 1))))
5270 && !nontemporal && !call_param_p
5271 && MEM_P (target))
5273 /* Optimize initialization of an array with a STRING_CST. */
5274 HOST_WIDE_INT exp_len, str_copy_len;
5275 rtx dest_mem;
5276 tree str = TREE_CODE (exp) == STRING_CST
5277 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5279 exp_len = int_expr_size (exp);
5280 if (exp_len <= 0)
5281 goto normal_expr;
5283 if (TREE_STRING_LENGTH (str) <= 0)
5284 goto normal_expr;
5286 str_copy_len = strlen (TREE_STRING_POINTER (str));
5287 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5288 goto normal_expr;
5290 str_copy_len = TREE_STRING_LENGTH (str);
5291 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5292 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5294 str_copy_len += STORE_MAX_PIECES - 1;
5295 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5297 str_copy_len = MIN (str_copy_len, exp_len);
5298 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5299 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5300 MEM_ALIGN (target), false))
5301 goto normal_expr;
5303 dest_mem = target;
5305 dest_mem = store_by_pieces (dest_mem,
5306 str_copy_len, builtin_strncpy_read_str,
5307 CONST_CAST (char *,
5308 TREE_STRING_POINTER (str)),
5309 MEM_ALIGN (target), false,
5310 exp_len > str_copy_len ? 1 : 0);
5311 if (exp_len > str_copy_len)
5312 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5313 GEN_INT (exp_len - str_copy_len),
5314 BLOCK_OP_NORMAL);
5315 return NULL_RTX;
5317 else
5319 rtx tmp_target;
5321 normal_expr:
5322 /* If we want to use a nontemporal store, force the value to
5323 register first. */
5324 tmp_target = nontemporal ? NULL_RTX : target;
5325 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5326 (call_param_p
5327 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5328 &alt_rtl, false);
5331 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5332 the same as that of TARGET, adjust the constant. This is needed, for
5333 example, in case it is a CONST_DOUBLE and we want only a word-sized
5334 value. */
5335 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5336 && TREE_CODE (exp) != ERROR_MARK
5337 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5338 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5339 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5341 /* If value was not generated in the target, store it there.
5342 Convert the value to TARGET's type first if necessary and emit the
5343 pending incrementations that have been queued when expanding EXP.
5344 Note that we cannot emit the whole queue blindly because this will
5345 effectively disable the POST_INC optimization later.
5347 If TEMP and TARGET compare equal according to rtx_equal_p, but
5348 one or both of them are volatile memory refs, we have to distinguish
5349 two cases:
5350 - expand_expr has used TARGET. In this case, we must not generate
5351 another copy. This can be detected by TARGET being equal according
5352 to == .
5353 - expand_expr has not used TARGET - that means that the source just
5354 happens to have the same RTX form. Since temp will have been created
5355 by expand_expr, it will compare unequal according to == .
5356 We must generate a copy in this case, to reach the correct number
5357 of volatile memory references. */
5359 if ((! rtx_equal_p (temp, target)
5360 || (temp != target && (side_effects_p (temp)
5361 || side_effects_p (target))))
5362 && TREE_CODE (exp) != ERROR_MARK
5363 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5364 but TARGET is not valid memory reference, TEMP will differ
5365 from TARGET although it is really the same location. */
5366 && !(alt_rtl
5367 && rtx_equal_p (alt_rtl, target)
5368 && !side_effects_p (alt_rtl)
5369 && !side_effects_p (target))
5370 /* If there's nothing to copy, don't bother. Don't call
5371 expr_size unless necessary, because some front-ends (C++)
5372 expr_size-hook must not be given objects that are not
5373 supposed to be bit-copied or bit-initialized. */
5374 && expr_size (exp) != const0_rtx)
5376 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5378 if (GET_MODE (target) == BLKmode)
5380 /* Handle calls that return BLKmode values in registers. */
5381 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5382 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5383 else
5384 store_bit_field (target,
5385 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5386 0, 0, 0, GET_MODE (temp), temp);
5388 else
5389 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5392 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5394 /* Handle copying a string constant into an array. The string
5395 constant may be shorter than the array. So copy just the string's
5396 actual length, and clear the rest. First get the size of the data
5397 type of the string, which is actually the size of the target. */
5398 rtx size = expr_size (exp);
5400 if (CONST_INT_P (size)
5401 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5402 emit_block_move (target, temp, size,
5403 (call_param_p
5404 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5405 else
5407 enum machine_mode pointer_mode
5408 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5409 enum machine_mode address_mode = get_address_mode (target);
5411 /* Compute the size of the data to copy from the string. */
5412 tree copy_size
5413 = size_binop_loc (loc, MIN_EXPR,
5414 make_tree (sizetype, size),
5415 size_int (TREE_STRING_LENGTH (exp)));
5416 rtx copy_size_rtx
5417 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5418 (call_param_p
5419 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5420 rtx label = 0;
5422 /* Copy that much. */
5423 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5424 TYPE_UNSIGNED (sizetype));
5425 emit_block_move (target, temp, copy_size_rtx,
5426 (call_param_p
5427 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5429 /* Figure out how much is left in TARGET that we have to clear.
5430 Do all calculations in pointer_mode. */
5431 if (CONST_INT_P (copy_size_rtx))
5433 size = plus_constant (address_mode, size,
5434 -INTVAL (copy_size_rtx));
5435 target = adjust_address (target, BLKmode,
5436 INTVAL (copy_size_rtx));
5438 else
5440 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5441 copy_size_rtx, NULL_RTX, 0,
5442 OPTAB_LIB_WIDEN);
5444 if (GET_MODE (copy_size_rtx) != address_mode)
5445 copy_size_rtx = convert_to_mode (address_mode,
5446 copy_size_rtx,
5447 TYPE_UNSIGNED (sizetype));
5449 target = offset_address (target, copy_size_rtx,
5450 highest_pow2_factor (copy_size));
5451 label = gen_label_rtx ();
5452 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5453 GET_MODE (size), 0, label);
5456 if (size != const0_rtx)
5457 clear_storage (target, size, BLOCK_OP_NORMAL);
5459 if (label)
5460 emit_label (label);
5463 /* Handle calls that return values in multiple non-contiguous locations.
5464 The Irix 6 ABI has examples of this. */
5465 else if (GET_CODE (target) == PARALLEL)
5467 if (GET_CODE (temp) == PARALLEL)
5468 emit_group_move (target, temp);
5469 else
5470 emit_group_load (target, temp, TREE_TYPE (exp),
5471 int_size_in_bytes (TREE_TYPE (exp)));
5473 else if (GET_CODE (temp) == PARALLEL)
5474 emit_group_store (target, temp, TREE_TYPE (exp),
5475 int_size_in_bytes (TREE_TYPE (exp)));
5476 else if (GET_MODE (temp) == BLKmode)
5477 emit_block_move (target, temp, expr_size (exp),
5478 (call_param_p
5479 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5480 /* If we emit a nontemporal store, there is nothing else to do. */
5481 else if (nontemporal && emit_storent_insn (target, temp))
5483 else
5485 temp = force_operand (temp, target);
5486 if (temp != target)
5487 emit_move_insn (target, temp);
5491 return NULL_RTX;
5494 /* Return true if field F of structure TYPE is a flexible array. */
5496 static bool
5497 flexible_array_member_p (const_tree f, const_tree type)
5499 const_tree tf;
5501 tf = TREE_TYPE (f);
5502 return (DECL_CHAIN (f) == NULL
5503 && TREE_CODE (tf) == ARRAY_TYPE
5504 && TYPE_DOMAIN (tf)
5505 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5506 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5507 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5508 && int_size_in_bytes (type) >= 0);
5511 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5512 must have in order for it to completely initialize a value of type TYPE.
5513 Return -1 if the number isn't known.
5515 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5517 static HOST_WIDE_INT
5518 count_type_elements (const_tree type, bool for_ctor_p)
5520 switch (TREE_CODE (type))
5522 case ARRAY_TYPE:
5524 tree nelts;
5526 nelts = array_type_nelts (type);
5527 if (nelts && tree_fits_uhwi_p (nelts))
5529 unsigned HOST_WIDE_INT n;
5531 n = tree_to_uhwi (nelts) + 1;
5532 if (n == 0 || for_ctor_p)
5533 return n;
5534 else
5535 return n * count_type_elements (TREE_TYPE (type), false);
5537 return for_ctor_p ? -1 : 1;
5540 case RECORD_TYPE:
5542 unsigned HOST_WIDE_INT n;
5543 tree f;
5545 n = 0;
5546 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5547 if (TREE_CODE (f) == FIELD_DECL)
5549 if (!for_ctor_p)
5550 n += count_type_elements (TREE_TYPE (f), false);
5551 else if (!flexible_array_member_p (f, type))
5552 /* Don't count flexible arrays, which are not supposed
5553 to be initialized. */
5554 n += 1;
5557 return n;
5560 case UNION_TYPE:
5561 case QUAL_UNION_TYPE:
5563 tree f;
5564 HOST_WIDE_INT n, m;
5566 gcc_assert (!for_ctor_p);
5567 /* Estimate the number of scalars in each field and pick the
5568 maximum. Other estimates would do instead; the idea is simply
5569 to make sure that the estimate is not sensitive to the ordering
5570 of the fields. */
5571 n = 1;
5572 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5573 if (TREE_CODE (f) == FIELD_DECL)
5575 m = count_type_elements (TREE_TYPE (f), false);
5576 /* If the field doesn't span the whole union, add an extra
5577 scalar for the rest. */
5578 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5579 TYPE_SIZE (type)) != 1)
5580 m++;
5581 if (n < m)
5582 n = m;
5584 return n;
5587 case COMPLEX_TYPE:
5588 return 2;
5590 case VECTOR_TYPE:
5591 return TYPE_VECTOR_SUBPARTS (type);
5593 case INTEGER_TYPE:
5594 case REAL_TYPE:
5595 case FIXED_POINT_TYPE:
5596 case ENUMERAL_TYPE:
5597 case BOOLEAN_TYPE:
5598 case POINTER_TYPE:
5599 case OFFSET_TYPE:
5600 case REFERENCE_TYPE:
5601 case NULLPTR_TYPE:
5602 return 1;
5604 case ERROR_MARK:
5605 return 0;
5607 case VOID_TYPE:
5608 case METHOD_TYPE:
5609 case FUNCTION_TYPE:
5610 case LANG_TYPE:
5611 default:
5612 gcc_unreachable ();
5616 /* Helper for categorize_ctor_elements. Identical interface. */
5618 static bool
5619 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5620 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5622 unsigned HOST_WIDE_INT idx;
5623 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5624 tree value, purpose, elt_type;
5626 /* Whether CTOR is a valid constant initializer, in accordance with what
5627 initializer_constant_valid_p does. If inferred from the constructor
5628 elements, true until proven otherwise. */
5629 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5630 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5632 nz_elts = 0;
5633 init_elts = 0;
5634 num_fields = 0;
5635 elt_type = NULL_TREE;
5637 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5639 HOST_WIDE_INT mult = 1;
5641 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5643 tree lo_index = TREE_OPERAND (purpose, 0);
5644 tree hi_index = TREE_OPERAND (purpose, 1);
5646 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5647 mult = (tree_to_uhwi (hi_index)
5648 - tree_to_uhwi (lo_index) + 1);
5650 num_fields += mult;
5651 elt_type = TREE_TYPE (value);
5653 switch (TREE_CODE (value))
5655 case CONSTRUCTOR:
5657 HOST_WIDE_INT nz = 0, ic = 0;
5659 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5660 p_complete);
5662 nz_elts += mult * nz;
5663 init_elts += mult * ic;
5665 if (const_from_elts_p && const_p)
5666 const_p = const_elt_p;
5668 break;
5670 case INTEGER_CST:
5671 case REAL_CST:
5672 case FIXED_CST:
5673 if (!initializer_zerop (value))
5674 nz_elts += mult;
5675 init_elts += mult;
5676 break;
5678 case STRING_CST:
5679 nz_elts += mult * TREE_STRING_LENGTH (value);
5680 init_elts += mult * TREE_STRING_LENGTH (value);
5681 break;
5683 case COMPLEX_CST:
5684 if (!initializer_zerop (TREE_REALPART (value)))
5685 nz_elts += mult;
5686 if (!initializer_zerop (TREE_IMAGPART (value)))
5687 nz_elts += mult;
5688 init_elts += mult;
5689 break;
5691 case VECTOR_CST:
5693 unsigned i;
5694 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5696 tree v = VECTOR_CST_ELT (value, i);
5697 if (!initializer_zerop (v))
5698 nz_elts += mult;
5699 init_elts += mult;
5702 break;
5704 default:
5706 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5707 nz_elts += mult * tc;
5708 init_elts += mult * tc;
5710 if (const_from_elts_p && const_p)
5711 const_p = initializer_constant_valid_p (value, elt_type)
5712 != NULL_TREE;
5714 break;
5718 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5719 num_fields, elt_type))
5720 *p_complete = false;
5722 *p_nz_elts += nz_elts;
5723 *p_init_elts += init_elts;
5725 return const_p;
5728 /* Examine CTOR to discover:
5729 * how many scalar fields are set to nonzero values,
5730 and place it in *P_NZ_ELTS;
5731 * how many scalar fields in total are in CTOR,
5732 and place it in *P_ELT_COUNT.
5733 * whether the constructor is complete -- in the sense that every
5734 meaningful byte is explicitly given a value --
5735 and place it in *P_COMPLETE.
5737 Return whether or not CTOR is a valid static constant initializer, the same
5738 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5740 bool
5741 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5742 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5744 *p_nz_elts = 0;
5745 *p_init_elts = 0;
5746 *p_complete = true;
5748 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5751 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5752 of which had type LAST_TYPE. Each element was itself a complete
5753 initializer, in the sense that every meaningful byte was explicitly
5754 given a value. Return true if the same is true for the constructor
5755 as a whole. */
5757 bool
5758 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5759 const_tree last_type)
5761 if (TREE_CODE (type) == UNION_TYPE
5762 || TREE_CODE (type) == QUAL_UNION_TYPE)
5764 if (num_elts == 0)
5765 return false;
5767 gcc_assert (num_elts == 1 && last_type);
5769 /* ??? We could look at each element of the union, and find the
5770 largest element. Which would avoid comparing the size of the
5771 initialized element against any tail padding in the union.
5772 Doesn't seem worth the effort... */
5773 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5776 return count_type_elements (type, true) == num_elts;
5779 /* Return 1 if EXP contains mostly (3/4) zeros. */
5781 static int
5782 mostly_zeros_p (const_tree exp)
5784 if (TREE_CODE (exp) == CONSTRUCTOR)
5786 HOST_WIDE_INT nz_elts, init_elts;
5787 bool complete_p;
5789 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5790 return !complete_p || nz_elts < init_elts / 4;
5793 return initializer_zerop (exp);
5796 /* Return 1 if EXP contains all zeros. */
5798 static int
5799 all_zeros_p (const_tree exp)
5801 if (TREE_CODE (exp) == CONSTRUCTOR)
5803 HOST_WIDE_INT nz_elts, init_elts;
5804 bool complete_p;
5806 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5807 return nz_elts == 0;
5810 return initializer_zerop (exp);
5813 /* Helper function for store_constructor.
5814 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5815 CLEARED is as for store_constructor.
5816 ALIAS_SET is the alias set to use for any stores.
5818 This provides a recursive shortcut back to store_constructor when it isn't
5819 necessary to go through store_field. This is so that we can pass through
5820 the cleared field to let store_constructor know that we may not have to
5821 clear a substructure if the outer structure has already been cleared. */
5823 static void
5824 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5825 HOST_WIDE_INT bitpos, enum machine_mode mode,
5826 tree exp, int cleared, alias_set_type alias_set)
5828 if (TREE_CODE (exp) == CONSTRUCTOR
5829 /* We can only call store_constructor recursively if the size and
5830 bit position are on a byte boundary. */
5831 && bitpos % BITS_PER_UNIT == 0
5832 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5833 /* If we have a nonzero bitpos for a register target, then we just
5834 let store_field do the bitfield handling. This is unlikely to
5835 generate unnecessary clear instructions anyways. */
5836 && (bitpos == 0 || MEM_P (target)))
5838 if (MEM_P (target))
5839 target
5840 = adjust_address (target,
5841 GET_MODE (target) == BLKmode
5842 || 0 != (bitpos
5843 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5844 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5847 /* Update the alias set, if required. */
5848 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5849 && MEM_ALIAS_SET (target) != 0)
5851 target = copy_rtx (target);
5852 set_mem_alias_set (target, alias_set);
5855 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5857 else
5858 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5862 /* Returns the number of FIELD_DECLs in TYPE. */
5864 static int
5865 fields_length (const_tree type)
5867 tree t = TYPE_FIELDS (type);
5868 int count = 0;
5870 for (; t; t = DECL_CHAIN (t))
5871 if (TREE_CODE (t) == FIELD_DECL)
5872 ++count;
5874 return count;
5878 /* Store the value of constructor EXP into the rtx TARGET.
5879 TARGET is either a REG or a MEM; we know it cannot conflict, since
5880 safe_from_p has been called.
5881 CLEARED is true if TARGET is known to have been zero'd.
5882 SIZE is the number of bytes of TARGET we are allowed to modify: this
5883 may not be the same as the size of EXP if we are assigning to a field
5884 which has been packed to exclude padding bits. */
5886 static void
5887 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5889 tree type = TREE_TYPE (exp);
5890 #ifdef WORD_REGISTER_OPERATIONS
5891 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5892 #endif
5894 switch (TREE_CODE (type))
5896 case RECORD_TYPE:
5897 case UNION_TYPE:
5898 case QUAL_UNION_TYPE:
5900 unsigned HOST_WIDE_INT idx;
5901 tree field, value;
5903 /* If size is zero or the target is already cleared, do nothing. */
5904 if (size == 0 || cleared)
5905 cleared = 1;
5906 /* We either clear the aggregate or indicate the value is dead. */
5907 else if ((TREE_CODE (type) == UNION_TYPE
5908 || TREE_CODE (type) == QUAL_UNION_TYPE)
5909 && ! CONSTRUCTOR_ELTS (exp))
5910 /* If the constructor is empty, clear the union. */
5912 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5913 cleared = 1;
5916 /* If we are building a static constructor into a register,
5917 set the initial value as zero so we can fold the value into
5918 a constant. But if more than one register is involved,
5919 this probably loses. */
5920 else if (REG_P (target) && TREE_STATIC (exp)
5921 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5923 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5924 cleared = 1;
5927 /* If the constructor has fewer fields than the structure or
5928 if we are initializing the structure to mostly zeros, clear
5929 the whole structure first. Don't do this if TARGET is a
5930 register whose mode size isn't equal to SIZE since
5931 clear_storage can't handle this case. */
5932 else if (size > 0
5933 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
5934 != fields_length (type))
5935 || mostly_zeros_p (exp))
5936 && (!REG_P (target)
5937 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5938 == size)))
5940 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5941 cleared = 1;
5944 if (REG_P (target) && !cleared)
5945 emit_clobber (target);
5947 /* Store each element of the constructor into the
5948 corresponding field of TARGET. */
5949 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
5951 enum machine_mode mode;
5952 HOST_WIDE_INT bitsize;
5953 HOST_WIDE_INT bitpos = 0;
5954 tree offset;
5955 rtx to_rtx = target;
5957 /* Just ignore missing fields. We cleared the whole
5958 structure, above, if any fields are missing. */
5959 if (field == 0)
5960 continue;
5962 if (cleared && initializer_zerop (value))
5963 continue;
5965 if (tree_fits_uhwi_p (DECL_SIZE (field)))
5966 bitsize = tree_to_uhwi (DECL_SIZE (field));
5967 else
5968 bitsize = -1;
5970 mode = DECL_MODE (field);
5971 if (DECL_BIT_FIELD (field))
5972 mode = VOIDmode;
5974 offset = DECL_FIELD_OFFSET (field);
5975 if (tree_fits_shwi_p (offset)
5976 && tree_fits_shwi_p (bit_position (field)))
5978 bitpos = int_bit_position (field);
5979 offset = 0;
5981 else
5982 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
5984 if (offset)
5986 enum machine_mode address_mode;
5987 rtx offset_rtx;
5989 offset
5990 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
5991 make_tree (TREE_TYPE (exp),
5992 target));
5994 offset_rtx = expand_normal (offset);
5995 gcc_assert (MEM_P (to_rtx));
5997 address_mode = get_address_mode (to_rtx);
5998 if (GET_MODE (offset_rtx) != address_mode)
5999 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6001 to_rtx = offset_address (to_rtx, offset_rtx,
6002 highest_pow2_factor (offset));
6005 #ifdef WORD_REGISTER_OPERATIONS
6006 /* If this initializes a field that is smaller than a
6007 word, at the start of a word, try to widen it to a full
6008 word. This special case allows us to output C++ member
6009 function initializations in a form that the optimizers
6010 can understand. */
6011 if (REG_P (target)
6012 && bitsize < BITS_PER_WORD
6013 && bitpos % BITS_PER_WORD == 0
6014 && GET_MODE_CLASS (mode) == MODE_INT
6015 && TREE_CODE (value) == INTEGER_CST
6016 && exp_size >= 0
6017 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6019 tree type = TREE_TYPE (value);
6021 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6023 type = lang_hooks.types.type_for_mode
6024 (word_mode, TYPE_UNSIGNED (type));
6025 value = fold_convert (type, value);
6028 if (BYTES_BIG_ENDIAN)
6029 value
6030 = fold_build2 (LSHIFT_EXPR, type, value,
6031 build_int_cst (type,
6032 BITS_PER_WORD - bitsize));
6033 bitsize = BITS_PER_WORD;
6034 mode = word_mode;
6036 #endif
6038 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6039 && DECL_NONADDRESSABLE_P (field))
6041 to_rtx = copy_rtx (to_rtx);
6042 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6045 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6046 value, cleared,
6047 get_alias_set (TREE_TYPE (field)));
6049 break;
6051 case ARRAY_TYPE:
6053 tree value, index;
6054 unsigned HOST_WIDE_INT i;
6055 int need_to_clear;
6056 tree domain;
6057 tree elttype = TREE_TYPE (type);
6058 int const_bounds_p;
6059 HOST_WIDE_INT minelt = 0;
6060 HOST_WIDE_INT maxelt = 0;
6062 domain = TYPE_DOMAIN (type);
6063 const_bounds_p = (TYPE_MIN_VALUE (domain)
6064 && TYPE_MAX_VALUE (domain)
6065 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6066 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6068 /* If we have constant bounds for the range of the type, get them. */
6069 if (const_bounds_p)
6071 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6072 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6075 /* If the constructor has fewer elements than the array, clear
6076 the whole array first. Similarly if this is static
6077 constructor of a non-BLKmode object. */
6078 if (cleared)
6079 need_to_clear = 0;
6080 else if (REG_P (target) && TREE_STATIC (exp))
6081 need_to_clear = 1;
6082 else
6084 unsigned HOST_WIDE_INT idx;
6085 tree index, value;
6086 HOST_WIDE_INT count = 0, zero_count = 0;
6087 need_to_clear = ! const_bounds_p;
6089 /* This loop is a more accurate version of the loop in
6090 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6091 is also needed to check for missing elements. */
6092 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6094 HOST_WIDE_INT this_node_count;
6096 if (need_to_clear)
6097 break;
6099 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6101 tree lo_index = TREE_OPERAND (index, 0);
6102 tree hi_index = TREE_OPERAND (index, 1);
6104 if (! tree_fits_uhwi_p (lo_index)
6105 || ! tree_fits_uhwi_p (hi_index))
6107 need_to_clear = 1;
6108 break;
6111 this_node_count = (tree_to_uhwi (hi_index)
6112 - tree_to_uhwi (lo_index) + 1);
6114 else
6115 this_node_count = 1;
6117 count += this_node_count;
6118 if (mostly_zeros_p (value))
6119 zero_count += this_node_count;
6122 /* Clear the entire array first if there are any missing
6123 elements, or if the incidence of zero elements is >=
6124 75%. */
6125 if (! need_to_clear
6126 && (count < maxelt - minelt + 1
6127 || 4 * zero_count >= 3 * count))
6128 need_to_clear = 1;
6131 if (need_to_clear && size > 0)
6133 if (REG_P (target))
6134 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6135 else
6136 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6137 cleared = 1;
6140 if (!cleared && REG_P (target))
6141 /* Inform later passes that the old value is dead. */
6142 emit_clobber (target);
6144 /* Store each element of the constructor into the
6145 corresponding element of TARGET, determined by counting the
6146 elements. */
6147 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6149 enum machine_mode mode;
6150 HOST_WIDE_INT bitsize;
6151 HOST_WIDE_INT bitpos;
6152 rtx xtarget = target;
6154 if (cleared && initializer_zerop (value))
6155 continue;
6157 mode = TYPE_MODE (elttype);
6158 if (mode == BLKmode)
6159 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6160 ? tree_to_uhwi (TYPE_SIZE (elttype))
6161 : -1);
6162 else
6163 bitsize = GET_MODE_BITSIZE (mode);
6165 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6167 tree lo_index = TREE_OPERAND (index, 0);
6168 tree hi_index = TREE_OPERAND (index, 1);
6169 rtx index_r, pos_rtx;
6170 HOST_WIDE_INT lo, hi, count;
6171 tree position;
6173 /* If the range is constant and "small", unroll the loop. */
6174 if (const_bounds_p
6175 && tree_fits_shwi_p (lo_index)
6176 && tree_fits_shwi_p (hi_index)
6177 && (lo = tree_to_shwi (lo_index),
6178 hi = tree_to_shwi (hi_index),
6179 count = hi - lo + 1,
6180 (!MEM_P (target)
6181 || count <= 2
6182 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6183 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6184 <= 40 * 8)))))
6186 lo -= minelt; hi -= minelt;
6187 for (; lo <= hi; lo++)
6189 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6191 if (MEM_P (target)
6192 && !MEM_KEEP_ALIAS_SET_P (target)
6193 && TREE_CODE (type) == ARRAY_TYPE
6194 && TYPE_NONALIASED_COMPONENT (type))
6196 target = copy_rtx (target);
6197 MEM_KEEP_ALIAS_SET_P (target) = 1;
6200 store_constructor_field
6201 (target, bitsize, bitpos, mode, value, cleared,
6202 get_alias_set (elttype));
6205 else
6207 rtx loop_start = gen_label_rtx ();
6208 rtx loop_end = gen_label_rtx ();
6209 tree exit_cond;
6211 expand_normal (hi_index);
6213 index = build_decl (EXPR_LOCATION (exp),
6214 VAR_DECL, NULL_TREE, domain);
6215 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6216 SET_DECL_RTL (index, index_r);
6217 store_expr (lo_index, index_r, 0, false);
6219 /* Build the head of the loop. */
6220 do_pending_stack_adjust ();
6221 emit_label (loop_start);
6223 /* Assign value to element index. */
6224 position =
6225 fold_convert (ssizetype,
6226 fold_build2 (MINUS_EXPR,
6227 TREE_TYPE (index),
6228 index,
6229 TYPE_MIN_VALUE (domain)));
6231 position =
6232 size_binop (MULT_EXPR, position,
6233 fold_convert (ssizetype,
6234 TYPE_SIZE_UNIT (elttype)));
6236 pos_rtx = expand_normal (position);
6237 xtarget = offset_address (target, pos_rtx,
6238 highest_pow2_factor (position));
6239 xtarget = adjust_address (xtarget, mode, 0);
6240 if (TREE_CODE (value) == CONSTRUCTOR)
6241 store_constructor (value, xtarget, cleared,
6242 bitsize / BITS_PER_UNIT);
6243 else
6244 store_expr (value, xtarget, 0, false);
6246 /* Generate a conditional jump to exit the loop. */
6247 exit_cond = build2 (LT_EXPR, integer_type_node,
6248 index, hi_index);
6249 jumpif (exit_cond, loop_end, -1);
6251 /* Update the loop counter, and jump to the head of
6252 the loop. */
6253 expand_assignment (index,
6254 build2 (PLUS_EXPR, TREE_TYPE (index),
6255 index, integer_one_node),
6256 false);
6258 emit_jump (loop_start);
6260 /* Build the end of the loop. */
6261 emit_label (loop_end);
6264 else if ((index != 0 && ! tree_fits_shwi_p (index))
6265 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6267 tree position;
6269 if (index == 0)
6270 index = ssize_int (1);
6272 if (minelt)
6273 index = fold_convert (ssizetype,
6274 fold_build2 (MINUS_EXPR,
6275 TREE_TYPE (index),
6276 index,
6277 TYPE_MIN_VALUE (domain)));
6279 position =
6280 size_binop (MULT_EXPR, index,
6281 fold_convert (ssizetype,
6282 TYPE_SIZE_UNIT (elttype)));
6283 xtarget = offset_address (target,
6284 expand_normal (position),
6285 highest_pow2_factor (position));
6286 xtarget = adjust_address (xtarget, mode, 0);
6287 store_expr (value, xtarget, 0, false);
6289 else
6291 if (index != 0)
6292 bitpos = ((tree_to_shwi (index) - minelt)
6293 * tree_to_uhwi (TYPE_SIZE (elttype)));
6294 else
6295 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6297 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6298 && TREE_CODE (type) == ARRAY_TYPE
6299 && TYPE_NONALIASED_COMPONENT (type))
6301 target = copy_rtx (target);
6302 MEM_KEEP_ALIAS_SET_P (target) = 1;
6304 store_constructor_field (target, bitsize, bitpos, mode, value,
6305 cleared, get_alias_set (elttype));
6308 break;
6311 case VECTOR_TYPE:
6313 unsigned HOST_WIDE_INT idx;
6314 constructor_elt *ce;
6315 int i;
6316 int need_to_clear;
6317 int icode = CODE_FOR_nothing;
6318 tree elttype = TREE_TYPE (type);
6319 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6320 enum machine_mode eltmode = TYPE_MODE (elttype);
6321 HOST_WIDE_INT bitsize;
6322 HOST_WIDE_INT bitpos;
6323 rtvec vector = NULL;
6324 unsigned n_elts;
6325 alias_set_type alias;
6327 gcc_assert (eltmode != BLKmode);
6329 n_elts = TYPE_VECTOR_SUBPARTS (type);
6330 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6332 enum machine_mode mode = GET_MODE (target);
6334 icode = (int) optab_handler (vec_init_optab, mode);
6335 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6336 if (icode != CODE_FOR_nothing)
6338 tree value;
6340 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6341 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6343 icode = CODE_FOR_nothing;
6344 break;
6347 if (icode != CODE_FOR_nothing)
6349 unsigned int i;
6351 vector = rtvec_alloc (n_elts);
6352 for (i = 0; i < n_elts; i++)
6353 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6357 /* If the constructor has fewer elements than the vector,
6358 clear the whole array first. Similarly if this is static
6359 constructor of a non-BLKmode object. */
6360 if (cleared)
6361 need_to_clear = 0;
6362 else if (REG_P (target) && TREE_STATIC (exp))
6363 need_to_clear = 1;
6364 else
6366 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6367 tree value;
6369 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6371 int n_elts_here = tree_to_uhwi
6372 (int_const_binop (TRUNC_DIV_EXPR,
6373 TYPE_SIZE (TREE_TYPE (value)),
6374 TYPE_SIZE (elttype)));
6376 count += n_elts_here;
6377 if (mostly_zeros_p (value))
6378 zero_count += n_elts_here;
6381 /* Clear the entire vector first if there are any missing elements,
6382 or if the incidence of zero elements is >= 75%. */
6383 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6386 if (need_to_clear && size > 0 && !vector)
6388 if (REG_P (target))
6389 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6390 else
6391 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6392 cleared = 1;
6395 /* Inform later passes that the old value is dead. */
6396 if (!cleared && !vector && REG_P (target))
6397 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6399 if (MEM_P (target))
6400 alias = MEM_ALIAS_SET (target);
6401 else
6402 alias = get_alias_set (elttype);
6404 /* Store each element of the constructor into the corresponding
6405 element of TARGET, determined by counting the elements. */
6406 for (idx = 0, i = 0;
6407 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6408 idx++, i += bitsize / elt_size)
6410 HOST_WIDE_INT eltpos;
6411 tree value = ce->value;
6413 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6414 if (cleared && initializer_zerop (value))
6415 continue;
6417 if (ce->index)
6418 eltpos = tree_to_uhwi (ce->index);
6419 else
6420 eltpos = i;
6422 if (vector)
6424 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6425 elements. */
6426 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6427 RTVEC_ELT (vector, eltpos)
6428 = expand_normal (value);
6430 else
6432 enum machine_mode value_mode =
6433 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6434 ? TYPE_MODE (TREE_TYPE (value))
6435 : eltmode;
6436 bitpos = eltpos * elt_size;
6437 store_constructor_field (target, bitsize, bitpos, value_mode,
6438 value, cleared, alias);
6442 if (vector)
6443 emit_insn (GEN_FCN (icode)
6444 (target,
6445 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6446 break;
6449 default:
6450 gcc_unreachable ();
6454 /* Store the value of EXP (an expression tree)
6455 into a subfield of TARGET which has mode MODE and occupies
6456 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6457 If MODE is VOIDmode, it means that we are storing into a bit-field.
6459 BITREGION_START is bitpos of the first bitfield in this region.
6460 BITREGION_END is the bitpos of the ending bitfield in this region.
6461 These two fields are 0, if the C++ memory model does not apply,
6462 or we are not interested in keeping track of bitfield regions.
6464 Always return const0_rtx unless we have something particular to
6465 return.
6467 ALIAS_SET is the alias set for the destination. This value will
6468 (in general) be different from that for TARGET, since TARGET is a
6469 reference to the containing structure.
6471 If NONTEMPORAL is true, try generating a nontemporal store. */
6473 static rtx
6474 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6475 unsigned HOST_WIDE_INT bitregion_start,
6476 unsigned HOST_WIDE_INT bitregion_end,
6477 enum machine_mode mode, tree exp,
6478 alias_set_type alias_set, bool nontemporal)
6480 if (TREE_CODE (exp) == ERROR_MARK)
6481 return const0_rtx;
6483 /* If we have nothing to store, do nothing unless the expression has
6484 side-effects. */
6485 if (bitsize == 0)
6486 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6488 if (GET_CODE (target) == CONCAT)
6490 /* We're storing into a struct containing a single __complex. */
6492 gcc_assert (!bitpos);
6493 return store_expr (exp, target, 0, nontemporal);
6496 /* If the structure is in a register or if the component
6497 is a bit field, we cannot use addressing to access it.
6498 Use bit-field techniques or SUBREG to store in it. */
6500 if (mode == VOIDmode
6501 || (mode != BLKmode && ! direct_store[(int) mode]
6502 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6503 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6504 || REG_P (target)
6505 || GET_CODE (target) == SUBREG
6506 /* If the field isn't aligned enough to store as an ordinary memref,
6507 store it as a bit field. */
6508 || (mode != BLKmode
6509 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6510 || bitpos % GET_MODE_ALIGNMENT (mode))
6511 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6512 || (bitpos % BITS_PER_UNIT != 0)))
6513 || (bitsize >= 0 && mode != BLKmode
6514 && GET_MODE_BITSIZE (mode) > bitsize)
6515 /* If the RHS and field are a constant size and the size of the
6516 RHS isn't the same size as the bitfield, we must use bitfield
6517 operations. */
6518 || (bitsize >= 0
6519 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6520 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6521 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6522 decl we must use bitfield operations. */
6523 || (bitsize >= 0
6524 && TREE_CODE (exp) == MEM_REF
6525 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6526 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6527 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6528 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6530 rtx temp;
6531 gimple nop_def;
6533 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6534 implies a mask operation. If the precision is the same size as
6535 the field we're storing into, that mask is redundant. This is
6536 particularly common with bit field assignments generated by the
6537 C front end. */
6538 nop_def = get_def_for_expr (exp, NOP_EXPR);
6539 if (nop_def)
6541 tree type = TREE_TYPE (exp);
6542 if (INTEGRAL_TYPE_P (type)
6543 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6544 && bitsize == TYPE_PRECISION (type))
6546 tree op = gimple_assign_rhs1 (nop_def);
6547 type = TREE_TYPE (op);
6548 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6549 exp = op;
6553 temp = expand_normal (exp);
6555 /* If BITSIZE is narrower than the size of the type of EXP
6556 we will be narrowing TEMP. Normally, what's wanted are the
6557 low-order bits. However, if EXP's type is a record and this is
6558 big-endian machine, we want the upper BITSIZE bits. */
6559 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6560 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6561 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6562 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6563 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6564 NULL_RTX, 1);
6566 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6567 if (mode != VOIDmode && mode != BLKmode
6568 && mode != TYPE_MODE (TREE_TYPE (exp)))
6569 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6571 /* If the modes of TEMP and TARGET are both BLKmode, both
6572 must be in memory and BITPOS must be aligned on a byte
6573 boundary. If so, we simply do a block copy. Likewise
6574 for a BLKmode-like TARGET. */
6575 if (GET_MODE (temp) == BLKmode
6576 && (GET_MODE (target) == BLKmode
6577 || (MEM_P (target)
6578 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6579 && (bitpos % BITS_PER_UNIT) == 0
6580 && (bitsize % BITS_PER_UNIT) == 0)))
6582 gcc_assert (MEM_P (target) && MEM_P (temp)
6583 && (bitpos % BITS_PER_UNIT) == 0);
6585 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6586 emit_block_move (target, temp,
6587 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6588 / BITS_PER_UNIT),
6589 BLOCK_OP_NORMAL);
6591 return const0_rtx;
6594 /* Handle calls that return values in multiple non-contiguous locations.
6595 The Irix 6 ABI has examples of this. */
6596 if (GET_CODE (temp) == PARALLEL)
6598 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6599 rtx temp_target;
6600 if (mode == BLKmode)
6601 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6602 temp_target = gen_reg_rtx (mode);
6603 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6604 temp = temp_target;
6606 else if (mode == BLKmode)
6608 /* Handle calls that return BLKmode values in registers. */
6609 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6611 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6612 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6613 temp = temp_target;
6615 else
6617 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6618 rtx temp_target;
6619 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6620 temp_target = gen_reg_rtx (mode);
6621 temp_target
6622 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6623 temp_target, mode, mode);
6624 temp = temp_target;
6628 /* Store the value in the bitfield. */
6629 store_bit_field (target, bitsize, bitpos,
6630 bitregion_start, bitregion_end,
6631 mode, temp);
6633 return const0_rtx;
6635 else
6637 /* Now build a reference to just the desired component. */
6638 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6640 if (to_rtx == target)
6641 to_rtx = copy_rtx (to_rtx);
6643 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6644 set_mem_alias_set (to_rtx, alias_set);
6646 return store_expr (exp, to_rtx, 0, nontemporal);
6650 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6651 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6652 codes and find the ultimate containing object, which we return.
6654 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6655 bit position, and *PUNSIGNEDP to the signedness of the field.
6656 If the position of the field is variable, we store a tree
6657 giving the variable offset (in units) in *POFFSET.
6658 This offset is in addition to the bit position.
6659 If the position is not variable, we store 0 in *POFFSET.
6661 If any of the extraction expressions is volatile,
6662 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6664 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6665 Otherwise, it is a mode that can be used to access the field.
6667 If the field describes a variable-sized object, *PMODE is set to
6668 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6669 this case, but the address of the object can be found.
6671 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6672 look through nodes that serve as markers of a greater alignment than
6673 the one that can be deduced from the expression. These nodes make it
6674 possible for front-ends to prevent temporaries from being created by
6675 the middle-end on alignment considerations. For that purpose, the
6676 normal operating mode at high-level is to always pass FALSE so that
6677 the ultimate containing object is really returned; moreover, the
6678 associated predicate handled_component_p will always return TRUE
6679 on these nodes, thus indicating that they are essentially handled
6680 by get_inner_reference. TRUE should only be passed when the caller
6681 is scanning the expression in order to build another representation
6682 and specifically knows how to handle these nodes; as such, this is
6683 the normal operating mode in the RTL expanders. */
6685 tree
6686 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6687 HOST_WIDE_INT *pbitpos, tree *poffset,
6688 enum machine_mode *pmode, int *punsignedp,
6689 int *pvolatilep, bool keep_aligning)
6691 tree size_tree = 0;
6692 enum machine_mode mode = VOIDmode;
6693 bool blkmode_bitfield = false;
6694 tree offset = size_zero_node;
6695 double_int bit_offset = double_int_zero;
6697 /* First get the mode, signedness, and size. We do this from just the
6698 outermost expression. */
6699 *pbitsize = -1;
6700 if (TREE_CODE (exp) == COMPONENT_REF)
6702 tree field = TREE_OPERAND (exp, 1);
6703 size_tree = DECL_SIZE (field);
6704 if (flag_strict_volatile_bitfields > 0
6705 && TREE_THIS_VOLATILE (exp)
6706 && DECL_BIT_FIELD_TYPE (field)
6707 && DECL_MODE (field) != BLKmode)
6708 /* Volatile bitfields should be accessed in the mode of the
6709 field's type, not the mode computed based on the bit
6710 size. */
6711 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6712 else if (!DECL_BIT_FIELD (field))
6713 mode = DECL_MODE (field);
6714 else if (DECL_MODE (field) == BLKmode)
6715 blkmode_bitfield = true;
6717 *punsignedp = DECL_UNSIGNED (field);
6719 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6721 size_tree = TREE_OPERAND (exp, 1);
6722 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6723 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6725 /* For vector types, with the correct size of access, use the mode of
6726 inner type. */
6727 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6728 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6729 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6730 mode = TYPE_MODE (TREE_TYPE (exp));
6732 else
6734 mode = TYPE_MODE (TREE_TYPE (exp));
6735 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6737 if (mode == BLKmode)
6738 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6739 else
6740 *pbitsize = GET_MODE_BITSIZE (mode);
6743 if (size_tree != 0)
6745 if (! tree_fits_uhwi_p (size_tree))
6746 mode = BLKmode, *pbitsize = -1;
6747 else
6748 *pbitsize = tree_to_uhwi (size_tree);
6751 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6752 and find the ultimate containing object. */
6753 while (1)
6755 switch (TREE_CODE (exp))
6757 case BIT_FIELD_REF:
6758 bit_offset += tree_to_double_int (TREE_OPERAND (exp, 2));
6759 break;
6761 case COMPONENT_REF:
6763 tree field = TREE_OPERAND (exp, 1);
6764 tree this_offset = component_ref_field_offset (exp);
6766 /* If this field hasn't been filled in yet, don't go past it.
6767 This should only happen when folding expressions made during
6768 type construction. */
6769 if (this_offset == 0)
6770 break;
6772 offset = size_binop (PLUS_EXPR, offset, this_offset);
6773 bit_offset += tree_to_double_int (DECL_FIELD_BIT_OFFSET (field));
6775 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6777 break;
6779 case ARRAY_REF:
6780 case ARRAY_RANGE_REF:
6782 tree index = TREE_OPERAND (exp, 1);
6783 tree low_bound = array_ref_low_bound (exp);
6784 tree unit_size = array_ref_element_size (exp);
6786 /* We assume all arrays have sizes that are a multiple of a byte.
6787 First subtract the lower bound, if any, in the type of the
6788 index, then convert to sizetype and multiply by the size of
6789 the array element. */
6790 if (! integer_zerop (low_bound))
6791 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6792 index, low_bound);
6794 offset = size_binop (PLUS_EXPR, offset,
6795 size_binop (MULT_EXPR,
6796 fold_convert (sizetype, index),
6797 unit_size));
6799 break;
6801 case REALPART_EXPR:
6802 break;
6804 case IMAGPART_EXPR:
6805 bit_offset += double_int::from_uhwi (*pbitsize);
6806 break;
6808 case VIEW_CONVERT_EXPR:
6809 if (keep_aligning && STRICT_ALIGNMENT
6810 && (TYPE_ALIGN (TREE_TYPE (exp))
6811 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6812 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6813 < BIGGEST_ALIGNMENT)
6814 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6815 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6816 goto done;
6817 break;
6819 case MEM_REF:
6820 /* Hand back the decl for MEM[&decl, off]. */
6821 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6823 tree off = TREE_OPERAND (exp, 1);
6824 if (!integer_zerop (off))
6826 double_int boff, coff = mem_ref_offset (exp);
6827 boff = coff.lshift (BITS_PER_UNIT == 8
6828 ? 3 : exact_log2 (BITS_PER_UNIT));
6829 bit_offset += boff;
6831 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6833 goto done;
6835 default:
6836 goto done;
6839 /* If any reference in the chain is volatile, the effect is volatile. */
6840 if (TREE_THIS_VOLATILE (exp))
6841 *pvolatilep = 1;
6843 exp = TREE_OPERAND (exp, 0);
6845 done:
6847 /* If OFFSET is constant, see if we can return the whole thing as a
6848 constant bit position. Make sure to handle overflow during
6849 this conversion. */
6850 if (TREE_CODE (offset) == INTEGER_CST)
6852 double_int tem = tree_to_double_int (offset);
6853 tem = tem.sext (TYPE_PRECISION (sizetype));
6854 tem = tem.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
6855 tem += bit_offset;
6856 if (tem.fits_shwi ())
6858 *pbitpos = tem.to_shwi ();
6859 *poffset = offset = NULL_TREE;
6863 /* Otherwise, split it up. */
6864 if (offset)
6866 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6867 if (bit_offset.is_negative ())
6869 double_int mask
6870 = double_int::mask (BITS_PER_UNIT == 8
6871 ? 3 : exact_log2 (BITS_PER_UNIT));
6872 double_int tem = bit_offset.and_not (mask);
6873 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6874 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6875 bit_offset -= tem;
6876 tem = tem.arshift (BITS_PER_UNIT == 8
6877 ? 3 : exact_log2 (BITS_PER_UNIT),
6878 HOST_BITS_PER_DOUBLE_INT);
6879 offset = size_binop (PLUS_EXPR, offset,
6880 double_int_to_tree (sizetype, tem));
6883 *pbitpos = bit_offset.to_shwi ();
6884 *poffset = offset;
6887 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6888 if (mode == VOIDmode
6889 && blkmode_bitfield
6890 && (*pbitpos % BITS_PER_UNIT) == 0
6891 && (*pbitsize % BITS_PER_UNIT) == 0)
6892 *pmode = BLKmode;
6893 else
6894 *pmode = mode;
6896 return exp;
6899 /* Return a tree of sizetype representing the size, in bytes, of the element
6900 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6902 tree
6903 array_ref_element_size (tree exp)
6905 tree aligned_size = TREE_OPERAND (exp, 3);
6906 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6907 location_t loc = EXPR_LOCATION (exp);
6909 /* If a size was specified in the ARRAY_REF, it's the size measured
6910 in alignment units of the element type. So multiply by that value. */
6911 if (aligned_size)
6913 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6914 sizetype from another type of the same width and signedness. */
6915 if (TREE_TYPE (aligned_size) != sizetype)
6916 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6917 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6918 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6921 /* Otherwise, take the size from that of the element type. Substitute
6922 any PLACEHOLDER_EXPR that we have. */
6923 else
6924 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6927 /* Return a tree representing the lower bound of the array mentioned in
6928 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6930 tree
6931 array_ref_low_bound (tree exp)
6933 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6935 /* If a lower bound is specified in EXP, use it. */
6936 if (TREE_OPERAND (exp, 2))
6937 return TREE_OPERAND (exp, 2);
6939 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6940 substituting for a PLACEHOLDER_EXPR as needed. */
6941 if (domain_type && TYPE_MIN_VALUE (domain_type))
6942 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6944 /* Otherwise, return a zero of the appropriate type. */
6945 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
6948 /* Returns true if REF is an array reference to an array at the end of
6949 a structure. If this is the case, the array may be allocated larger
6950 than its upper bound implies. */
6952 bool
6953 array_at_struct_end_p (tree ref)
6955 if (TREE_CODE (ref) != ARRAY_REF
6956 && TREE_CODE (ref) != ARRAY_RANGE_REF)
6957 return false;
6959 while (handled_component_p (ref))
6961 /* If the reference chain contains a component reference to a
6962 non-union type and there follows another field the reference
6963 is not at the end of a structure. */
6964 if (TREE_CODE (ref) == COMPONENT_REF
6965 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
6967 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
6968 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
6969 nextf = DECL_CHAIN (nextf);
6970 if (nextf)
6971 return false;
6974 ref = TREE_OPERAND (ref, 0);
6977 /* If the reference is based on a declared entity, the size of the array
6978 is constrained by its given domain. */
6979 if (DECL_P (ref))
6980 return false;
6982 return true;
6985 /* Return a tree representing the upper bound of the array mentioned in
6986 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6988 tree
6989 array_ref_up_bound (tree exp)
6991 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6993 /* If there is a domain type and it has an upper bound, use it, substituting
6994 for a PLACEHOLDER_EXPR as needed. */
6995 if (domain_type && TYPE_MAX_VALUE (domain_type))
6996 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
6998 /* Otherwise fail. */
6999 return NULL_TREE;
7002 /* Return a tree representing the offset, in bytes, of the field referenced
7003 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
7005 tree
7006 component_ref_field_offset (tree exp)
7008 tree aligned_offset = TREE_OPERAND (exp, 2);
7009 tree field = TREE_OPERAND (exp, 1);
7010 location_t loc = EXPR_LOCATION (exp);
7012 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
7013 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
7014 value. */
7015 if (aligned_offset)
7017 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7018 sizetype from another type of the same width and signedness. */
7019 if (TREE_TYPE (aligned_offset) != sizetype)
7020 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
7021 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
7022 size_int (DECL_OFFSET_ALIGN (field)
7023 / BITS_PER_UNIT));
7026 /* Otherwise, take the offset from that of the field. Substitute
7027 any PLACEHOLDER_EXPR that we have. */
7028 else
7029 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
7032 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7034 static unsigned HOST_WIDE_INT
7035 target_align (const_tree target)
7037 /* We might have a chain of nested references with intermediate misaligning
7038 bitfields components, so need to recurse to find out. */
7040 unsigned HOST_WIDE_INT this_align, outer_align;
7042 switch (TREE_CODE (target))
7044 case BIT_FIELD_REF:
7045 return 1;
7047 case COMPONENT_REF:
7048 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7049 outer_align = target_align (TREE_OPERAND (target, 0));
7050 return MIN (this_align, outer_align);
7052 case ARRAY_REF:
7053 case ARRAY_RANGE_REF:
7054 this_align = TYPE_ALIGN (TREE_TYPE (target));
7055 outer_align = target_align (TREE_OPERAND (target, 0));
7056 return MIN (this_align, outer_align);
7058 CASE_CONVERT:
7059 case NON_LVALUE_EXPR:
7060 case VIEW_CONVERT_EXPR:
7061 this_align = TYPE_ALIGN (TREE_TYPE (target));
7062 outer_align = target_align (TREE_OPERAND (target, 0));
7063 return MAX (this_align, outer_align);
7065 default:
7066 return TYPE_ALIGN (TREE_TYPE (target));
7071 /* Given an rtx VALUE that may contain additions and multiplications, return
7072 an equivalent value that just refers to a register, memory, or constant.
7073 This is done by generating instructions to perform the arithmetic and
7074 returning a pseudo-register containing the value.
7076 The returned value may be a REG, SUBREG, MEM or constant. */
7079 force_operand (rtx value, rtx target)
7081 rtx op1, op2;
7082 /* Use subtarget as the target for operand 0 of a binary operation. */
7083 rtx subtarget = get_subtarget (target);
7084 enum rtx_code code = GET_CODE (value);
7086 /* Check for subreg applied to an expression produced by loop optimizer. */
7087 if (code == SUBREG
7088 && !REG_P (SUBREG_REG (value))
7089 && !MEM_P (SUBREG_REG (value)))
7091 value
7092 = simplify_gen_subreg (GET_MODE (value),
7093 force_reg (GET_MODE (SUBREG_REG (value)),
7094 force_operand (SUBREG_REG (value),
7095 NULL_RTX)),
7096 GET_MODE (SUBREG_REG (value)),
7097 SUBREG_BYTE (value));
7098 code = GET_CODE (value);
7101 /* Check for a PIC address load. */
7102 if ((code == PLUS || code == MINUS)
7103 && XEXP (value, 0) == pic_offset_table_rtx
7104 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7105 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7106 || GET_CODE (XEXP (value, 1)) == CONST))
7108 if (!subtarget)
7109 subtarget = gen_reg_rtx (GET_MODE (value));
7110 emit_move_insn (subtarget, value);
7111 return subtarget;
7114 if (ARITHMETIC_P (value))
7116 op2 = XEXP (value, 1);
7117 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7118 subtarget = 0;
7119 if (code == MINUS && CONST_INT_P (op2))
7121 code = PLUS;
7122 op2 = negate_rtx (GET_MODE (value), op2);
7125 /* Check for an addition with OP2 a constant integer and our first
7126 operand a PLUS of a virtual register and something else. In that
7127 case, we want to emit the sum of the virtual register and the
7128 constant first and then add the other value. This allows virtual
7129 register instantiation to simply modify the constant rather than
7130 creating another one around this addition. */
7131 if (code == PLUS && CONST_INT_P (op2)
7132 && GET_CODE (XEXP (value, 0)) == PLUS
7133 && REG_P (XEXP (XEXP (value, 0), 0))
7134 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7135 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7137 rtx temp = expand_simple_binop (GET_MODE (value), code,
7138 XEXP (XEXP (value, 0), 0), op2,
7139 subtarget, 0, OPTAB_LIB_WIDEN);
7140 return expand_simple_binop (GET_MODE (value), code, temp,
7141 force_operand (XEXP (XEXP (value,
7142 0), 1), 0),
7143 target, 0, OPTAB_LIB_WIDEN);
7146 op1 = force_operand (XEXP (value, 0), subtarget);
7147 op2 = force_operand (op2, NULL_RTX);
7148 switch (code)
7150 case MULT:
7151 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7152 case DIV:
7153 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7154 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7155 target, 1, OPTAB_LIB_WIDEN);
7156 else
7157 return expand_divmod (0,
7158 FLOAT_MODE_P (GET_MODE (value))
7159 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7160 GET_MODE (value), op1, op2, target, 0);
7161 case MOD:
7162 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7163 target, 0);
7164 case UDIV:
7165 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7166 target, 1);
7167 case UMOD:
7168 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7169 target, 1);
7170 case ASHIFTRT:
7171 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7172 target, 0, OPTAB_LIB_WIDEN);
7173 default:
7174 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7175 target, 1, OPTAB_LIB_WIDEN);
7178 if (UNARY_P (value))
7180 if (!target)
7181 target = gen_reg_rtx (GET_MODE (value));
7182 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7183 switch (code)
7185 case ZERO_EXTEND:
7186 case SIGN_EXTEND:
7187 case TRUNCATE:
7188 case FLOAT_EXTEND:
7189 case FLOAT_TRUNCATE:
7190 convert_move (target, op1, code == ZERO_EXTEND);
7191 return target;
7193 case FIX:
7194 case UNSIGNED_FIX:
7195 expand_fix (target, op1, code == UNSIGNED_FIX);
7196 return target;
7198 case FLOAT:
7199 case UNSIGNED_FLOAT:
7200 expand_float (target, op1, code == UNSIGNED_FLOAT);
7201 return target;
7203 default:
7204 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7208 #ifdef INSN_SCHEDULING
7209 /* On machines that have insn scheduling, we want all memory reference to be
7210 explicit, so we need to deal with such paradoxical SUBREGs. */
7211 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7212 value
7213 = simplify_gen_subreg (GET_MODE (value),
7214 force_reg (GET_MODE (SUBREG_REG (value)),
7215 force_operand (SUBREG_REG (value),
7216 NULL_RTX)),
7217 GET_MODE (SUBREG_REG (value)),
7218 SUBREG_BYTE (value));
7219 #endif
7221 return value;
7224 /* Subroutine of expand_expr: return nonzero iff there is no way that
7225 EXP can reference X, which is being modified. TOP_P is nonzero if this
7226 call is going to be used to determine whether we need a temporary
7227 for EXP, as opposed to a recursive call to this function.
7229 It is always safe for this routine to return zero since it merely
7230 searches for optimization opportunities. */
7233 safe_from_p (const_rtx x, tree exp, int top_p)
7235 rtx exp_rtl = 0;
7236 int i, nops;
7238 if (x == 0
7239 /* If EXP has varying size, we MUST use a target since we currently
7240 have no way of allocating temporaries of variable size
7241 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7242 So we assume here that something at a higher level has prevented a
7243 clash. This is somewhat bogus, but the best we can do. Only
7244 do this when X is BLKmode and when we are at the top level. */
7245 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7246 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7247 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7248 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7249 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7250 != INTEGER_CST)
7251 && GET_MODE (x) == BLKmode)
7252 /* If X is in the outgoing argument area, it is always safe. */
7253 || (MEM_P (x)
7254 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7255 || (GET_CODE (XEXP (x, 0)) == PLUS
7256 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7257 return 1;
7259 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7260 find the underlying pseudo. */
7261 if (GET_CODE (x) == SUBREG)
7263 x = SUBREG_REG (x);
7264 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7265 return 0;
7268 /* Now look at our tree code and possibly recurse. */
7269 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7271 case tcc_declaration:
7272 exp_rtl = DECL_RTL_IF_SET (exp);
7273 break;
7275 case tcc_constant:
7276 return 1;
7278 case tcc_exceptional:
7279 if (TREE_CODE (exp) == TREE_LIST)
7281 while (1)
7283 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7284 return 0;
7285 exp = TREE_CHAIN (exp);
7286 if (!exp)
7287 return 1;
7288 if (TREE_CODE (exp) != TREE_LIST)
7289 return safe_from_p (x, exp, 0);
7292 else if (TREE_CODE (exp) == CONSTRUCTOR)
7294 constructor_elt *ce;
7295 unsigned HOST_WIDE_INT idx;
7297 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7298 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7299 || !safe_from_p (x, ce->value, 0))
7300 return 0;
7301 return 1;
7303 else if (TREE_CODE (exp) == ERROR_MARK)
7304 return 1; /* An already-visited SAVE_EXPR? */
7305 else
7306 return 0;
7308 case tcc_statement:
7309 /* The only case we look at here is the DECL_INITIAL inside a
7310 DECL_EXPR. */
7311 return (TREE_CODE (exp) != DECL_EXPR
7312 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7313 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7314 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7316 case tcc_binary:
7317 case tcc_comparison:
7318 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7319 return 0;
7320 /* Fall through. */
7322 case tcc_unary:
7323 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7325 case tcc_expression:
7326 case tcc_reference:
7327 case tcc_vl_exp:
7328 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7329 the expression. If it is set, we conflict iff we are that rtx or
7330 both are in memory. Otherwise, we check all operands of the
7331 expression recursively. */
7333 switch (TREE_CODE (exp))
7335 case ADDR_EXPR:
7336 /* If the operand is static or we are static, we can't conflict.
7337 Likewise if we don't conflict with the operand at all. */
7338 if (staticp (TREE_OPERAND (exp, 0))
7339 || TREE_STATIC (exp)
7340 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7341 return 1;
7343 /* Otherwise, the only way this can conflict is if we are taking
7344 the address of a DECL a that address if part of X, which is
7345 very rare. */
7346 exp = TREE_OPERAND (exp, 0);
7347 if (DECL_P (exp))
7349 if (!DECL_RTL_SET_P (exp)
7350 || !MEM_P (DECL_RTL (exp)))
7351 return 0;
7352 else
7353 exp_rtl = XEXP (DECL_RTL (exp), 0);
7355 break;
7357 case MEM_REF:
7358 if (MEM_P (x)
7359 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7360 get_alias_set (exp)))
7361 return 0;
7362 break;
7364 case CALL_EXPR:
7365 /* Assume that the call will clobber all hard registers and
7366 all of memory. */
7367 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7368 || MEM_P (x))
7369 return 0;
7370 break;
7372 case WITH_CLEANUP_EXPR:
7373 case CLEANUP_POINT_EXPR:
7374 /* Lowered by gimplify.c. */
7375 gcc_unreachable ();
7377 case SAVE_EXPR:
7378 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7380 default:
7381 break;
7384 /* If we have an rtx, we do not need to scan our operands. */
7385 if (exp_rtl)
7386 break;
7388 nops = TREE_OPERAND_LENGTH (exp);
7389 for (i = 0; i < nops; i++)
7390 if (TREE_OPERAND (exp, i) != 0
7391 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7392 return 0;
7394 break;
7396 case tcc_type:
7397 /* Should never get a type here. */
7398 gcc_unreachable ();
7401 /* If we have an rtl, find any enclosed object. Then see if we conflict
7402 with it. */
7403 if (exp_rtl)
7405 if (GET_CODE (exp_rtl) == SUBREG)
7407 exp_rtl = SUBREG_REG (exp_rtl);
7408 if (REG_P (exp_rtl)
7409 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7410 return 0;
7413 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7414 are memory and they conflict. */
7415 return ! (rtx_equal_p (x, exp_rtl)
7416 || (MEM_P (x) && MEM_P (exp_rtl)
7417 && true_dependence (exp_rtl, VOIDmode, x)));
7420 /* If we reach here, it is safe. */
7421 return 1;
7425 /* Return the highest power of two that EXP is known to be a multiple of.
7426 This is used in updating alignment of MEMs in array references. */
7428 unsigned HOST_WIDE_INT
7429 highest_pow2_factor (const_tree exp)
7431 unsigned HOST_WIDE_INT ret;
7432 int trailing_zeros = tree_ctz (exp);
7433 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7434 return BIGGEST_ALIGNMENT;
7435 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7436 if (ret > BIGGEST_ALIGNMENT)
7437 return BIGGEST_ALIGNMENT;
7438 return ret;
7441 /* Similar, except that the alignment requirements of TARGET are
7442 taken into account. Assume it is at least as aligned as its
7443 type, unless it is a COMPONENT_REF in which case the layout of
7444 the structure gives the alignment. */
7446 static unsigned HOST_WIDE_INT
7447 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7449 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7450 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7452 return MAX (factor, talign);
7455 #ifdef HAVE_conditional_move
7456 /* Convert the tree comparison code TCODE to the rtl one where the
7457 signedness is UNSIGNEDP. */
7459 static enum rtx_code
7460 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7462 enum rtx_code code;
7463 switch (tcode)
7465 case EQ_EXPR:
7466 code = EQ;
7467 break;
7468 case NE_EXPR:
7469 code = NE;
7470 break;
7471 case LT_EXPR:
7472 code = unsignedp ? LTU : LT;
7473 break;
7474 case LE_EXPR:
7475 code = unsignedp ? LEU : LE;
7476 break;
7477 case GT_EXPR:
7478 code = unsignedp ? GTU : GT;
7479 break;
7480 case GE_EXPR:
7481 code = unsignedp ? GEU : GE;
7482 break;
7483 case UNORDERED_EXPR:
7484 code = UNORDERED;
7485 break;
7486 case ORDERED_EXPR:
7487 code = ORDERED;
7488 break;
7489 case UNLT_EXPR:
7490 code = UNLT;
7491 break;
7492 case UNLE_EXPR:
7493 code = UNLE;
7494 break;
7495 case UNGT_EXPR:
7496 code = UNGT;
7497 break;
7498 case UNGE_EXPR:
7499 code = UNGE;
7500 break;
7501 case UNEQ_EXPR:
7502 code = UNEQ;
7503 break;
7504 case LTGT_EXPR:
7505 code = LTGT;
7506 break;
7508 default:
7509 gcc_unreachable ();
7511 return code;
7513 #endif
7515 /* Subroutine of expand_expr. Expand the two operands of a binary
7516 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7517 The value may be stored in TARGET if TARGET is nonzero. The
7518 MODIFIER argument is as documented by expand_expr. */
7520 static void
7521 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7522 enum expand_modifier modifier)
7524 if (! safe_from_p (target, exp1, 1))
7525 target = 0;
7526 if (operand_equal_p (exp0, exp1, 0))
7528 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7529 *op1 = copy_rtx (*op0);
7531 else
7533 /* If we need to preserve evaluation order, copy exp0 into its own
7534 temporary variable so that it can't be clobbered by exp1. */
7535 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7536 exp0 = save_expr (exp0);
7537 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7538 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7543 /* Return a MEM that contains constant EXP. DEFER is as for
7544 output_constant_def and MODIFIER is as for expand_expr. */
7546 static rtx
7547 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7549 rtx mem;
7551 mem = output_constant_def (exp, defer);
7552 if (modifier != EXPAND_INITIALIZER)
7553 mem = use_anchored_address (mem);
7554 return mem;
7557 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7558 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7560 static rtx
7561 expand_expr_addr_expr_1 (tree exp, rtx target, enum machine_mode tmode,
7562 enum expand_modifier modifier, addr_space_t as)
7564 rtx result, subtarget;
7565 tree inner, offset;
7566 HOST_WIDE_INT bitsize, bitpos;
7567 int volatilep, unsignedp;
7568 enum machine_mode mode1;
7570 /* If we are taking the address of a constant and are at the top level,
7571 we have to use output_constant_def since we can't call force_const_mem
7572 at top level. */
7573 /* ??? This should be considered a front-end bug. We should not be
7574 generating ADDR_EXPR of something that isn't an LVALUE. The only
7575 exception here is STRING_CST. */
7576 if (CONSTANT_CLASS_P (exp))
7578 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7579 if (modifier < EXPAND_SUM)
7580 result = force_operand (result, target);
7581 return result;
7584 /* Everything must be something allowed by is_gimple_addressable. */
7585 switch (TREE_CODE (exp))
7587 case INDIRECT_REF:
7588 /* This case will happen via recursion for &a->b. */
7589 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7591 case MEM_REF:
7593 tree tem = TREE_OPERAND (exp, 0);
7594 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7595 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7596 return expand_expr (tem, target, tmode, modifier);
7599 case CONST_DECL:
7600 /* Expand the initializer like constants above. */
7601 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7602 0, modifier), 0);
7603 if (modifier < EXPAND_SUM)
7604 result = force_operand (result, target);
7605 return result;
7607 case REALPART_EXPR:
7608 /* The real part of the complex number is always first, therefore
7609 the address is the same as the address of the parent object. */
7610 offset = 0;
7611 bitpos = 0;
7612 inner = TREE_OPERAND (exp, 0);
7613 break;
7615 case IMAGPART_EXPR:
7616 /* The imaginary part of the complex number is always second.
7617 The expression is therefore always offset by the size of the
7618 scalar type. */
7619 offset = 0;
7620 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7621 inner = TREE_OPERAND (exp, 0);
7622 break;
7624 case COMPOUND_LITERAL_EXPR:
7625 /* Allow COMPOUND_LITERAL_EXPR in initializers, if e.g.
7626 rtl_for_decl_init is called on DECL_INITIAL with
7627 COMPOUNT_LITERAL_EXPRs in it, they aren't gimplified. */
7628 if (modifier == EXPAND_INITIALIZER
7629 && COMPOUND_LITERAL_EXPR_DECL (exp))
7630 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7631 target, tmode, modifier, as);
7632 /* FALLTHRU */
7633 default:
7634 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7635 expand_expr, as that can have various side effects; LABEL_DECLs for
7636 example, may not have their DECL_RTL set yet. Expand the rtl of
7637 CONSTRUCTORs too, which should yield a memory reference for the
7638 constructor's contents. Assume language specific tree nodes can
7639 be expanded in some interesting way. */
7640 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7641 if (DECL_P (exp)
7642 || TREE_CODE (exp) == CONSTRUCTOR
7643 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7645 result = expand_expr (exp, target, tmode,
7646 modifier == EXPAND_INITIALIZER
7647 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7649 /* If the DECL isn't in memory, then the DECL wasn't properly
7650 marked TREE_ADDRESSABLE, which will be either a front-end
7651 or a tree optimizer bug. */
7653 if (TREE_ADDRESSABLE (exp)
7654 && ! MEM_P (result)
7655 && ! targetm.calls.allocate_stack_slots_for_args ())
7657 error ("local frame unavailable (naked function?)");
7658 return result;
7660 else
7661 gcc_assert (MEM_P (result));
7662 result = XEXP (result, 0);
7664 /* ??? Is this needed anymore? */
7665 if (DECL_P (exp))
7666 TREE_USED (exp) = 1;
7668 if (modifier != EXPAND_INITIALIZER
7669 && modifier != EXPAND_CONST_ADDRESS
7670 && modifier != EXPAND_SUM)
7671 result = force_operand (result, target);
7672 return result;
7675 /* Pass FALSE as the last argument to get_inner_reference although
7676 we are expanding to RTL. The rationale is that we know how to
7677 handle "aligning nodes" here: we can just bypass them because
7678 they won't change the final object whose address will be returned
7679 (they actually exist only for that purpose). */
7680 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7681 &mode1, &unsignedp, &volatilep, false);
7682 break;
7685 /* We must have made progress. */
7686 gcc_assert (inner != exp);
7688 subtarget = offset || bitpos ? NULL_RTX : target;
7689 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7690 inner alignment, force the inner to be sufficiently aligned. */
7691 if (CONSTANT_CLASS_P (inner)
7692 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7694 inner = copy_node (inner);
7695 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7696 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7697 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7699 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7701 if (offset)
7703 rtx tmp;
7705 if (modifier != EXPAND_NORMAL)
7706 result = force_operand (result, NULL);
7707 tmp = expand_expr (offset, NULL_RTX, tmode,
7708 modifier == EXPAND_INITIALIZER
7709 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7711 /* expand_expr is allowed to return an object in a mode other
7712 than TMODE. If it did, we need to convert. */
7713 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7714 tmp = convert_modes (tmode, GET_MODE (tmp),
7715 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7716 result = convert_memory_address_addr_space (tmode, result, as);
7717 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7719 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7720 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7721 else
7723 subtarget = bitpos ? NULL_RTX : target;
7724 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7725 1, OPTAB_LIB_WIDEN);
7729 if (bitpos)
7731 /* Someone beforehand should have rejected taking the address
7732 of such an object. */
7733 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7735 result = convert_memory_address_addr_space (tmode, result, as);
7736 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7737 if (modifier < EXPAND_SUM)
7738 result = force_operand (result, target);
7741 return result;
7744 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7745 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7747 static rtx
7748 expand_expr_addr_expr (tree exp, rtx target, enum machine_mode tmode,
7749 enum expand_modifier modifier)
7751 addr_space_t as = ADDR_SPACE_GENERIC;
7752 enum machine_mode address_mode = Pmode;
7753 enum machine_mode pointer_mode = ptr_mode;
7754 enum machine_mode rmode;
7755 rtx result;
7757 /* Target mode of VOIDmode says "whatever's natural". */
7758 if (tmode == VOIDmode)
7759 tmode = TYPE_MODE (TREE_TYPE (exp));
7761 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7763 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7764 address_mode = targetm.addr_space.address_mode (as);
7765 pointer_mode = targetm.addr_space.pointer_mode (as);
7768 /* We can get called with some Weird Things if the user does silliness
7769 like "(short) &a". In that case, convert_memory_address won't do
7770 the right thing, so ignore the given target mode. */
7771 if (tmode != address_mode && tmode != pointer_mode)
7772 tmode = address_mode;
7774 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7775 tmode, modifier, as);
7777 /* Despite expand_expr claims concerning ignoring TMODE when not
7778 strictly convenient, stuff breaks if we don't honor it. Note
7779 that combined with the above, we only do this for pointer modes. */
7780 rmode = GET_MODE (result);
7781 if (rmode == VOIDmode)
7782 rmode = tmode;
7783 if (rmode != tmode)
7784 result = convert_memory_address_addr_space (tmode, result, as);
7786 return result;
7789 /* Generate code for computing CONSTRUCTOR EXP.
7790 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7791 is TRUE, instead of creating a temporary variable in memory
7792 NULL is returned and the caller needs to handle it differently. */
7794 static rtx
7795 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7796 bool avoid_temp_mem)
7798 tree type = TREE_TYPE (exp);
7799 enum machine_mode mode = TYPE_MODE (type);
7801 /* Try to avoid creating a temporary at all. This is possible
7802 if all of the initializer is zero.
7803 FIXME: try to handle all [0..255] initializers we can handle
7804 with memset. */
7805 if (TREE_STATIC (exp)
7806 && !TREE_ADDRESSABLE (exp)
7807 && target != 0 && mode == BLKmode
7808 && all_zeros_p (exp))
7810 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7811 return target;
7814 /* All elts simple constants => refer to a constant in memory. But
7815 if this is a non-BLKmode mode, let it store a field at a time
7816 since that should make a CONST_INT or CONST_DOUBLE when we
7817 fold. Likewise, if we have a target we can use, it is best to
7818 store directly into the target unless the type is large enough
7819 that memcpy will be used. If we are making an initializer and
7820 all operands are constant, put it in memory as well.
7822 FIXME: Avoid trying to fill vector constructors piece-meal.
7823 Output them with output_constant_def below unless we're sure
7824 they're zeros. This should go away when vector initializers
7825 are treated like VECTOR_CST instead of arrays. */
7826 if ((TREE_STATIC (exp)
7827 && ((mode == BLKmode
7828 && ! (target != 0 && safe_from_p (target, exp, 1)))
7829 || TREE_ADDRESSABLE (exp)
7830 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7831 && (! MOVE_BY_PIECES_P
7832 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7833 TYPE_ALIGN (type)))
7834 && ! mostly_zeros_p (exp))))
7835 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7836 && TREE_CONSTANT (exp)))
7838 rtx constructor;
7840 if (avoid_temp_mem)
7841 return NULL_RTX;
7843 constructor = expand_expr_constant (exp, 1, modifier);
7845 if (modifier != EXPAND_CONST_ADDRESS
7846 && modifier != EXPAND_INITIALIZER
7847 && modifier != EXPAND_SUM)
7848 constructor = validize_mem (constructor);
7850 return constructor;
7853 /* Handle calls that pass values in multiple non-contiguous
7854 locations. The Irix 6 ABI has examples of this. */
7855 if (target == 0 || ! safe_from_p (target, exp, 1)
7856 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7858 if (avoid_temp_mem)
7859 return NULL_RTX;
7861 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7864 store_constructor (exp, target, 0, int_expr_size (exp));
7865 return target;
7869 /* expand_expr: generate code for computing expression EXP.
7870 An rtx for the computed value is returned. The value is never null.
7871 In the case of a void EXP, const0_rtx is returned.
7873 The value may be stored in TARGET if TARGET is nonzero.
7874 TARGET is just a suggestion; callers must assume that
7875 the rtx returned may not be the same as TARGET.
7877 If TARGET is CONST0_RTX, it means that the value will be ignored.
7879 If TMODE is not VOIDmode, it suggests generating the
7880 result in mode TMODE. But this is done only when convenient.
7881 Otherwise, TMODE is ignored and the value generated in its natural mode.
7882 TMODE is just a suggestion; callers must assume that
7883 the rtx returned may not have mode TMODE.
7885 Note that TARGET may have neither TMODE nor MODE. In that case, it
7886 probably will not be used.
7888 If MODIFIER is EXPAND_SUM then when EXP is an addition
7889 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7890 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7891 products as above, or REG or MEM, or constant.
7892 Ordinarily in such cases we would output mul or add instructions
7893 and then return a pseudo reg containing the sum.
7895 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7896 it also marks a label as absolutely required (it can't be dead).
7897 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7898 This is used for outputting expressions used in initializers.
7900 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7901 with a constant address even if that address is not normally legitimate.
7902 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7904 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7905 a call parameter. Such targets require special care as we haven't yet
7906 marked TARGET so that it's safe from being trashed by libcalls. We
7907 don't want to use TARGET for anything but the final result;
7908 Intermediate values must go elsewhere. Additionally, calls to
7909 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7911 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7912 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7913 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7914 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7915 recursively.
7917 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7918 In this case, we don't adjust a returned MEM rtx that wouldn't be
7919 sufficiently aligned for its mode; instead, it's up to the caller
7920 to deal with it afterwards. This is used to make sure that unaligned
7921 base objects for which out-of-bounds accesses are supported, for
7922 example record types with trailing arrays, aren't realigned behind
7923 the back of the caller.
7924 The normal operating mode is to pass FALSE for this parameter. */
7927 expand_expr_real (tree exp, rtx target, enum machine_mode tmode,
7928 enum expand_modifier modifier, rtx *alt_rtl,
7929 bool inner_reference_p)
7931 rtx ret;
7933 /* Handle ERROR_MARK before anybody tries to access its type. */
7934 if (TREE_CODE (exp) == ERROR_MARK
7935 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7937 ret = CONST0_RTX (tmode);
7938 return ret ? ret : const0_rtx;
7941 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
7942 inner_reference_p);
7943 return ret;
7946 /* Try to expand the conditional expression which is represented by
7947 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
7948 return the rtl reg which repsents the result. Otherwise return
7949 NULL_RTL. */
7951 static rtx
7952 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7953 tree treeop1 ATTRIBUTE_UNUSED,
7954 tree treeop2 ATTRIBUTE_UNUSED)
7956 #ifdef HAVE_conditional_move
7957 rtx insn;
7958 rtx op00, op01, op1, op2;
7959 enum rtx_code comparison_code;
7960 enum machine_mode comparison_mode;
7961 gimple srcstmt;
7962 rtx temp;
7963 tree type = TREE_TYPE (treeop1);
7964 int unsignedp = TYPE_UNSIGNED (type);
7965 enum machine_mode mode = TYPE_MODE (type);
7966 enum machine_mode orig_mode = mode;
7968 /* If we cannot do a conditional move on the mode, try doing it
7969 with the promoted mode. */
7970 if (!can_conditionally_move_p (mode))
7972 mode = promote_mode (type, mode, &unsignedp);
7973 if (!can_conditionally_move_p (mode))
7974 return NULL_RTX;
7975 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
7977 else
7978 temp = assign_temp (type, 0, 1);
7980 start_sequence ();
7981 expand_operands (treeop1, treeop2,
7982 temp, &op1, &op2, EXPAND_NORMAL);
7984 if (TREE_CODE (treeop0) == SSA_NAME
7985 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7987 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7988 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7989 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7990 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7991 comparison_mode = TYPE_MODE (type);
7992 unsignedp = TYPE_UNSIGNED (type);
7993 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7995 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
7997 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7998 enum tree_code cmpcode = TREE_CODE (treeop0);
7999 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8000 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8001 unsignedp = TYPE_UNSIGNED (type);
8002 comparison_mode = TYPE_MODE (type);
8003 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8005 else
8007 op00 = expand_normal (treeop0);
8008 op01 = const0_rtx;
8009 comparison_code = NE;
8010 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8013 if (GET_MODE (op1) != mode)
8014 op1 = gen_lowpart (mode, op1);
8016 if (GET_MODE (op2) != mode)
8017 op2 = gen_lowpart (mode, op2);
8019 /* Try to emit the conditional move. */
8020 insn = emit_conditional_move (temp, comparison_code,
8021 op00, op01, comparison_mode,
8022 op1, op2, mode,
8023 unsignedp);
8025 /* If we could do the conditional move, emit the sequence,
8026 and return. */
8027 if (insn)
8029 rtx seq = get_insns ();
8030 end_sequence ();
8031 emit_insn (seq);
8032 return convert_modes (orig_mode, mode, temp, 0);
8035 /* Otherwise discard the sequence and fall back to code with
8036 branches. */
8037 end_sequence ();
8038 #endif
8039 return NULL_RTX;
8043 expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode,
8044 enum expand_modifier modifier)
8046 rtx op0, op1, op2, temp;
8047 tree type;
8048 int unsignedp;
8049 enum machine_mode mode;
8050 enum tree_code code = ops->code;
8051 optab this_optab;
8052 rtx subtarget, original_target;
8053 int ignore;
8054 bool reduce_bit_field;
8055 location_t loc = ops->location;
8056 tree treeop0, treeop1, treeop2;
8057 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8058 ? reduce_to_bit_field_precision ((expr), \
8059 target, \
8060 type) \
8061 : (expr))
8063 type = ops->type;
8064 mode = TYPE_MODE (type);
8065 unsignedp = TYPE_UNSIGNED (type);
8067 treeop0 = ops->op0;
8068 treeop1 = ops->op1;
8069 treeop2 = ops->op2;
8071 /* We should be called only on simple (binary or unary) expressions,
8072 exactly those that are valid in gimple expressions that aren't
8073 GIMPLE_SINGLE_RHS (or invalid). */
8074 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8075 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8076 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8078 ignore = (target == const0_rtx
8079 || ((CONVERT_EXPR_CODE_P (code)
8080 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8081 && TREE_CODE (type) == VOID_TYPE));
8083 /* We should be called only if we need the result. */
8084 gcc_assert (!ignore);
8086 /* An operation in what may be a bit-field type needs the
8087 result to be reduced to the precision of the bit-field type,
8088 which is narrower than that of the type's mode. */
8089 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8090 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8092 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8093 target = 0;
8095 /* Use subtarget as the target for operand 0 of a binary operation. */
8096 subtarget = get_subtarget (target);
8097 original_target = target;
8099 switch (code)
8101 case NON_LVALUE_EXPR:
8102 case PAREN_EXPR:
8103 CASE_CONVERT:
8104 if (treeop0 == error_mark_node)
8105 return const0_rtx;
8107 if (TREE_CODE (type) == UNION_TYPE)
8109 tree valtype = TREE_TYPE (treeop0);
8111 /* If both input and output are BLKmode, this conversion isn't doing
8112 anything except possibly changing memory attribute. */
8113 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8115 rtx result = expand_expr (treeop0, target, tmode,
8116 modifier);
8118 result = copy_rtx (result);
8119 set_mem_attributes (result, type, 0);
8120 return result;
8123 if (target == 0)
8125 if (TYPE_MODE (type) != BLKmode)
8126 target = gen_reg_rtx (TYPE_MODE (type));
8127 else
8128 target = assign_temp (type, 1, 1);
8131 if (MEM_P (target))
8132 /* Store data into beginning of memory target. */
8133 store_expr (treeop0,
8134 adjust_address (target, TYPE_MODE (valtype), 0),
8135 modifier == EXPAND_STACK_PARM,
8136 false);
8138 else
8140 gcc_assert (REG_P (target));
8142 /* Store this field into a union of the proper type. */
8143 store_field (target,
8144 MIN ((int_size_in_bytes (TREE_TYPE
8145 (treeop0))
8146 * BITS_PER_UNIT),
8147 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8148 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8151 /* Return the entire union. */
8152 return target;
8155 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8157 op0 = expand_expr (treeop0, target, VOIDmode,
8158 modifier);
8160 /* If the signedness of the conversion differs and OP0 is
8161 a promoted SUBREG, clear that indication since we now
8162 have to do the proper extension. */
8163 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8164 && GET_CODE (op0) == SUBREG)
8165 SUBREG_PROMOTED_VAR_P (op0) = 0;
8167 return REDUCE_BIT_FIELD (op0);
8170 op0 = expand_expr (treeop0, NULL_RTX, mode,
8171 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8172 if (GET_MODE (op0) == mode)
8175 /* If OP0 is a constant, just convert it into the proper mode. */
8176 else if (CONSTANT_P (op0))
8178 tree inner_type = TREE_TYPE (treeop0);
8179 enum machine_mode inner_mode = GET_MODE (op0);
8181 if (inner_mode == VOIDmode)
8182 inner_mode = TYPE_MODE (inner_type);
8184 if (modifier == EXPAND_INITIALIZER)
8185 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8186 subreg_lowpart_offset (mode,
8187 inner_mode));
8188 else
8189 op0= convert_modes (mode, inner_mode, op0,
8190 TYPE_UNSIGNED (inner_type));
8193 else if (modifier == EXPAND_INITIALIZER)
8194 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8196 else if (target == 0)
8197 op0 = convert_to_mode (mode, op0,
8198 TYPE_UNSIGNED (TREE_TYPE
8199 (treeop0)));
8200 else
8202 convert_move (target, op0,
8203 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8204 op0 = target;
8207 return REDUCE_BIT_FIELD (op0);
8209 case ADDR_SPACE_CONVERT_EXPR:
8211 tree treeop0_type = TREE_TYPE (treeop0);
8212 addr_space_t as_to;
8213 addr_space_t as_from;
8215 gcc_assert (POINTER_TYPE_P (type));
8216 gcc_assert (POINTER_TYPE_P (treeop0_type));
8218 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8219 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8221 /* Conversions between pointers to the same address space should
8222 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8223 gcc_assert (as_to != as_from);
8225 /* Ask target code to handle conversion between pointers
8226 to overlapping address spaces. */
8227 if (targetm.addr_space.subset_p (as_to, as_from)
8228 || targetm.addr_space.subset_p (as_from, as_to))
8230 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8231 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8232 gcc_assert (op0);
8233 return op0;
8236 /* For disjoint address spaces, converting anything but
8237 a null pointer invokes undefined behaviour. We simply
8238 always return a null pointer here. */
8239 return CONST0_RTX (mode);
8242 case POINTER_PLUS_EXPR:
8243 /* Even though the sizetype mode and the pointer's mode can be different
8244 expand is able to handle this correctly and get the correct result out
8245 of the PLUS_EXPR code. */
8246 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8247 if sizetype precision is smaller than pointer precision. */
8248 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8249 treeop1 = fold_convert_loc (loc, type,
8250 fold_convert_loc (loc, ssizetype,
8251 treeop1));
8252 /* If sizetype precision is larger than pointer precision, truncate the
8253 offset to have matching modes. */
8254 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8255 treeop1 = fold_convert_loc (loc, type, treeop1);
8257 case PLUS_EXPR:
8258 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8259 something else, make sure we add the register to the constant and
8260 then to the other thing. This case can occur during strength
8261 reduction and doing it this way will produce better code if the
8262 frame pointer or argument pointer is eliminated.
8264 fold-const.c will ensure that the constant is always in the inner
8265 PLUS_EXPR, so the only case we need to do anything about is if
8266 sp, ap, or fp is our second argument, in which case we must swap
8267 the innermost first argument and our second argument. */
8269 if (TREE_CODE (treeop0) == PLUS_EXPR
8270 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8271 && TREE_CODE (treeop1) == VAR_DECL
8272 && (DECL_RTL (treeop1) == frame_pointer_rtx
8273 || DECL_RTL (treeop1) == stack_pointer_rtx
8274 || DECL_RTL (treeop1) == arg_pointer_rtx))
8276 gcc_unreachable ();
8279 /* If the result is to be ptr_mode and we are adding an integer to
8280 something, we might be forming a constant. So try to use
8281 plus_constant. If it produces a sum and we can't accept it,
8282 use force_operand. This allows P = &ARR[const] to generate
8283 efficient code on machines where a SYMBOL_REF is not a valid
8284 address.
8286 If this is an EXPAND_SUM call, always return the sum. */
8287 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8288 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8290 if (modifier == EXPAND_STACK_PARM)
8291 target = 0;
8292 if (TREE_CODE (treeop0) == INTEGER_CST
8293 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8294 && TREE_CONSTANT (treeop1))
8296 rtx constant_part;
8298 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8299 EXPAND_SUM);
8300 /* Use immed_double_const to ensure that the constant is
8301 truncated according to the mode of OP1, then sign extended
8302 to a HOST_WIDE_INT. Using the constant directly can result
8303 in non-canonical RTL in a 64x32 cross compile. */
8304 constant_part
8305 = immed_double_const (TREE_INT_CST_LOW (treeop0),
8306 (HOST_WIDE_INT) 0,
8307 TYPE_MODE (TREE_TYPE (treeop1)));
8308 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8309 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8310 op1 = force_operand (op1, target);
8311 return REDUCE_BIT_FIELD (op1);
8314 else if (TREE_CODE (treeop1) == INTEGER_CST
8315 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8316 && TREE_CONSTANT (treeop0))
8318 rtx constant_part;
8320 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8321 (modifier == EXPAND_INITIALIZER
8322 ? EXPAND_INITIALIZER : EXPAND_SUM));
8323 if (! CONSTANT_P (op0))
8325 op1 = expand_expr (treeop1, NULL_RTX,
8326 VOIDmode, modifier);
8327 /* Return a PLUS if modifier says it's OK. */
8328 if (modifier == EXPAND_SUM
8329 || modifier == EXPAND_INITIALIZER)
8330 return simplify_gen_binary (PLUS, mode, op0, op1);
8331 goto binop2;
8333 /* Use immed_double_const to ensure that the constant is
8334 truncated according to the mode of OP1, then sign extended
8335 to a HOST_WIDE_INT. Using the constant directly can result
8336 in non-canonical RTL in a 64x32 cross compile. */
8337 constant_part
8338 = immed_double_const (TREE_INT_CST_LOW (treeop1),
8339 (HOST_WIDE_INT) 0,
8340 TYPE_MODE (TREE_TYPE (treeop0)));
8341 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8342 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8343 op0 = force_operand (op0, target);
8344 return REDUCE_BIT_FIELD (op0);
8348 /* Use TER to expand pointer addition of a negated value
8349 as pointer subtraction. */
8350 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8351 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8352 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8353 && TREE_CODE (treeop1) == SSA_NAME
8354 && TYPE_MODE (TREE_TYPE (treeop0))
8355 == TYPE_MODE (TREE_TYPE (treeop1)))
8357 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8358 if (def)
8360 treeop1 = gimple_assign_rhs1 (def);
8361 code = MINUS_EXPR;
8362 goto do_minus;
8366 /* No sense saving up arithmetic to be done
8367 if it's all in the wrong mode to form part of an address.
8368 And force_operand won't know whether to sign-extend or
8369 zero-extend. */
8370 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8371 || mode != ptr_mode)
8373 expand_operands (treeop0, treeop1,
8374 subtarget, &op0, &op1, EXPAND_NORMAL);
8375 if (op0 == const0_rtx)
8376 return op1;
8377 if (op1 == const0_rtx)
8378 return op0;
8379 goto binop2;
8382 expand_operands (treeop0, treeop1,
8383 subtarget, &op0, &op1, modifier);
8384 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8386 case MINUS_EXPR:
8387 do_minus:
8388 /* For initializers, we are allowed to return a MINUS of two
8389 symbolic constants. Here we handle all cases when both operands
8390 are constant. */
8391 /* Handle difference of two symbolic constants,
8392 for the sake of an initializer. */
8393 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8394 && really_constant_p (treeop0)
8395 && really_constant_p (treeop1))
8397 expand_operands (treeop0, treeop1,
8398 NULL_RTX, &op0, &op1, modifier);
8400 /* If the last operand is a CONST_INT, use plus_constant of
8401 the negated constant. Else make the MINUS. */
8402 if (CONST_INT_P (op1))
8403 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8404 -INTVAL (op1)));
8405 else
8406 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8409 /* No sense saving up arithmetic to be done
8410 if it's all in the wrong mode to form part of an address.
8411 And force_operand won't know whether to sign-extend or
8412 zero-extend. */
8413 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8414 || mode != ptr_mode)
8415 goto binop;
8417 expand_operands (treeop0, treeop1,
8418 subtarget, &op0, &op1, modifier);
8420 /* Convert A - const to A + (-const). */
8421 if (CONST_INT_P (op1))
8423 op1 = negate_rtx (mode, op1);
8424 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8427 goto binop2;
8429 case WIDEN_MULT_PLUS_EXPR:
8430 case WIDEN_MULT_MINUS_EXPR:
8431 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8432 op2 = expand_normal (treeop2);
8433 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8434 target, unsignedp);
8435 return target;
8437 case WIDEN_MULT_EXPR:
8438 /* If first operand is constant, swap them.
8439 Thus the following special case checks need only
8440 check the second operand. */
8441 if (TREE_CODE (treeop0) == INTEGER_CST)
8443 tree t1 = treeop0;
8444 treeop0 = treeop1;
8445 treeop1 = t1;
8448 /* First, check if we have a multiplication of one signed and one
8449 unsigned operand. */
8450 if (TREE_CODE (treeop1) != INTEGER_CST
8451 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8452 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8454 enum machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8455 this_optab = usmul_widen_optab;
8456 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8457 != CODE_FOR_nothing)
8459 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8460 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8461 EXPAND_NORMAL);
8462 else
8463 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8464 EXPAND_NORMAL);
8465 /* op0 and op1 might still be constant, despite the above
8466 != INTEGER_CST check. Handle it. */
8467 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8469 op0 = convert_modes (innermode, mode, op0, true);
8470 op1 = convert_modes (innermode, mode, op1, false);
8471 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8472 target, unsignedp));
8474 goto binop3;
8477 /* Check for a multiplication with matching signedness. */
8478 else if ((TREE_CODE (treeop1) == INTEGER_CST
8479 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8480 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8481 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8483 tree op0type = TREE_TYPE (treeop0);
8484 enum machine_mode innermode = TYPE_MODE (op0type);
8485 bool zextend_p = TYPE_UNSIGNED (op0type);
8486 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8487 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8489 if (TREE_CODE (treeop0) != INTEGER_CST)
8491 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8492 != CODE_FOR_nothing)
8494 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8495 EXPAND_NORMAL);
8496 /* op0 and op1 might still be constant, despite the above
8497 != INTEGER_CST check. Handle it. */
8498 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8500 widen_mult_const:
8501 op0 = convert_modes (innermode, mode, op0, zextend_p);
8503 = convert_modes (innermode, mode, op1,
8504 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8505 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8506 target,
8507 unsignedp));
8509 temp = expand_widening_mult (mode, op0, op1, target,
8510 unsignedp, this_optab);
8511 return REDUCE_BIT_FIELD (temp);
8513 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8514 != CODE_FOR_nothing
8515 && innermode == word_mode)
8517 rtx htem, hipart;
8518 op0 = expand_normal (treeop0);
8519 if (TREE_CODE (treeop1) == INTEGER_CST)
8520 op1 = convert_modes (innermode, mode,
8521 expand_normal (treeop1),
8522 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8523 else
8524 op1 = expand_normal (treeop1);
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)
8528 goto widen_mult_const;
8529 temp = expand_binop (mode, other_optab, op0, op1, target,
8530 unsignedp, OPTAB_LIB_WIDEN);
8531 hipart = gen_highpart (innermode, temp);
8532 htem = expand_mult_highpart_adjust (innermode, hipart,
8533 op0, op1, hipart,
8534 zextend_p);
8535 if (htem != hipart)
8536 emit_move_insn (hipart, htem);
8537 return REDUCE_BIT_FIELD (temp);
8541 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8542 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8543 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8544 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8546 case FMA_EXPR:
8548 optab opt = fma_optab;
8549 gimple def0, def2;
8551 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8552 call. */
8553 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8555 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8556 tree call_expr;
8558 gcc_assert (fn != NULL_TREE);
8559 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8560 return expand_builtin (call_expr, target, subtarget, mode, false);
8563 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8564 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8566 op0 = op2 = NULL;
8568 if (def0 && def2
8569 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8571 opt = fnms_optab;
8572 op0 = expand_normal (gimple_assign_rhs1 (def0));
8573 op2 = expand_normal (gimple_assign_rhs1 (def2));
8575 else if (def0
8576 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8578 opt = fnma_optab;
8579 op0 = expand_normal (gimple_assign_rhs1 (def0));
8581 else if (def2
8582 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8584 opt = fms_optab;
8585 op2 = expand_normal (gimple_assign_rhs1 (def2));
8588 if (op0 == NULL)
8589 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8590 if (op2 == NULL)
8591 op2 = expand_normal (treeop2);
8592 op1 = expand_normal (treeop1);
8594 return expand_ternary_op (TYPE_MODE (type), opt,
8595 op0, op1, op2, target, 0);
8598 case MULT_EXPR:
8599 /* If this is a fixed-point operation, then we cannot use the code
8600 below because "expand_mult" doesn't support sat/no-sat fixed-point
8601 multiplications. */
8602 if (ALL_FIXED_POINT_MODE_P (mode))
8603 goto binop;
8605 /* If first operand is constant, swap them.
8606 Thus the following special case checks need only
8607 check the second operand. */
8608 if (TREE_CODE (treeop0) == INTEGER_CST)
8610 tree t1 = treeop0;
8611 treeop0 = treeop1;
8612 treeop1 = t1;
8615 /* Attempt to return something suitable for generating an
8616 indexed address, for machines that support that. */
8618 if (modifier == EXPAND_SUM && mode == ptr_mode
8619 && tree_fits_shwi_p (treeop1))
8621 tree exp1 = treeop1;
8623 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8624 EXPAND_SUM);
8626 if (!REG_P (op0))
8627 op0 = force_operand (op0, NULL_RTX);
8628 if (!REG_P (op0))
8629 op0 = copy_to_mode_reg (mode, op0);
8631 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8632 gen_int_mode (tree_to_shwi (exp1),
8633 TYPE_MODE (TREE_TYPE (exp1)))));
8636 if (modifier == EXPAND_STACK_PARM)
8637 target = 0;
8639 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8640 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8642 case TRUNC_DIV_EXPR:
8643 case FLOOR_DIV_EXPR:
8644 case CEIL_DIV_EXPR:
8645 case ROUND_DIV_EXPR:
8646 case EXACT_DIV_EXPR:
8647 /* If this is a fixed-point operation, then we cannot use the code
8648 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8649 divisions. */
8650 if (ALL_FIXED_POINT_MODE_P (mode))
8651 goto binop;
8653 if (modifier == EXPAND_STACK_PARM)
8654 target = 0;
8655 /* Possible optimization: compute the dividend with EXPAND_SUM
8656 then if the divisor is constant can optimize the case
8657 where some terms of the dividend have coeffs divisible by it. */
8658 expand_operands (treeop0, treeop1,
8659 subtarget, &op0, &op1, EXPAND_NORMAL);
8660 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8662 case RDIV_EXPR:
8663 goto binop;
8665 case MULT_HIGHPART_EXPR:
8666 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8667 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8668 gcc_assert (temp);
8669 return temp;
8671 case TRUNC_MOD_EXPR:
8672 case FLOOR_MOD_EXPR:
8673 case CEIL_MOD_EXPR:
8674 case ROUND_MOD_EXPR:
8675 if (modifier == EXPAND_STACK_PARM)
8676 target = 0;
8677 expand_operands (treeop0, treeop1,
8678 subtarget, &op0, &op1, EXPAND_NORMAL);
8679 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8681 case FIXED_CONVERT_EXPR:
8682 op0 = expand_normal (treeop0);
8683 if (target == 0 || modifier == EXPAND_STACK_PARM)
8684 target = gen_reg_rtx (mode);
8686 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8687 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8688 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8689 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8690 else
8691 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8692 return target;
8694 case FIX_TRUNC_EXPR:
8695 op0 = expand_normal (treeop0);
8696 if (target == 0 || modifier == EXPAND_STACK_PARM)
8697 target = gen_reg_rtx (mode);
8698 expand_fix (target, op0, unsignedp);
8699 return target;
8701 case FLOAT_EXPR:
8702 op0 = expand_normal (treeop0);
8703 if (target == 0 || modifier == EXPAND_STACK_PARM)
8704 target = gen_reg_rtx (mode);
8705 /* expand_float can't figure out what to do if FROM has VOIDmode.
8706 So give it the correct mode. With -O, cse will optimize this. */
8707 if (GET_MODE (op0) == VOIDmode)
8708 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8709 op0);
8710 expand_float (target, op0,
8711 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8712 return target;
8714 case NEGATE_EXPR:
8715 op0 = expand_expr (treeop0, subtarget,
8716 VOIDmode, EXPAND_NORMAL);
8717 if (modifier == EXPAND_STACK_PARM)
8718 target = 0;
8719 temp = expand_unop (mode,
8720 optab_for_tree_code (NEGATE_EXPR, type,
8721 optab_default),
8722 op0, target, 0);
8723 gcc_assert (temp);
8724 return REDUCE_BIT_FIELD (temp);
8726 case ABS_EXPR:
8727 op0 = expand_expr (treeop0, subtarget,
8728 VOIDmode, EXPAND_NORMAL);
8729 if (modifier == EXPAND_STACK_PARM)
8730 target = 0;
8732 /* ABS_EXPR is not valid for complex arguments. */
8733 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8734 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8736 /* Unsigned abs is simply the operand. Testing here means we don't
8737 risk generating incorrect code below. */
8738 if (TYPE_UNSIGNED (type))
8739 return op0;
8741 return expand_abs (mode, op0, target, unsignedp,
8742 safe_from_p (target, treeop0, 1));
8744 case MAX_EXPR:
8745 case MIN_EXPR:
8746 target = original_target;
8747 if (target == 0
8748 || modifier == EXPAND_STACK_PARM
8749 || (MEM_P (target) && MEM_VOLATILE_P (target))
8750 || GET_MODE (target) != mode
8751 || (REG_P (target)
8752 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8753 target = gen_reg_rtx (mode);
8754 expand_operands (treeop0, treeop1,
8755 target, &op0, &op1, EXPAND_NORMAL);
8757 /* First try to do it with a special MIN or MAX instruction.
8758 If that does not win, use a conditional jump to select the proper
8759 value. */
8760 this_optab = optab_for_tree_code (code, type, optab_default);
8761 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8762 OPTAB_WIDEN);
8763 if (temp != 0)
8764 return temp;
8766 /* At this point, a MEM target is no longer useful; we will get better
8767 code without it. */
8769 if (! REG_P (target))
8770 target = gen_reg_rtx (mode);
8772 /* If op1 was placed in target, swap op0 and op1. */
8773 if (target != op0 && target == op1)
8775 temp = op0;
8776 op0 = op1;
8777 op1 = temp;
8780 /* We generate better code and avoid problems with op1 mentioning
8781 target by forcing op1 into a pseudo if it isn't a constant. */
8782 if (! CONSTANT_P (op1))
8783 op1 = force_reg (mode, op1);
8786 enum rtx_code comparison_code;
8787 rtx cmpop1 = op1;
8789 if (code == MAX_EXPR)
8790 comparison_code = unsignedp ? GEU : GE;
8791 else
8792 comparison_code = unsignedp ? LEU : LE;
8794 /* Canonicalize to comparisons against 0. */
8795 if (op1 == const1_rtx)
8797 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8798 or (a != 0 ? a : 1) for unsigned.
8799 For MIN we are safe converting (a <= 1 ? a : 1)
8800 into (a <= 0 ? a : 1) */
8801 cmpop1 = const0_rtx;
8802 if (code == MAX_EXPR)
8803 comparison_code = unsignedp ? NE : GT;
8805 if (op1 == constm1_rtx && !unsignedp)
8807 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8808 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8809 cmpop1 = const0_rtx;
8810 if (code == MIN_EXPR)
8811 comparison_code = LT;
8813 #ifdef HAVE_conditional_move
8814 /* Use a conditional move if possible. */
8815 if (can_conditionally_move_p (mode))
8817 rtx insn;
8819 start_sequence ();
8821 /* Try to emit the conditional move. */
8822 insn = emit_conditional_move (target, comparison_code,
8823 op0, cmpop1, mode,
8824 op0, op1, mode,
8825 unsignedp);
8827 /* If we could do the conditional move, emit the sequence,
8828 and return. */
8829 if (insn)
8831 rtx seq = get_insns ();
8832 end_sequence ();
8833 emit_insn (seq);
8834 return target;
8837 /* Otherwise discard the sequence and fall back to code with
8838 branches. */
8839 end_sequence ();
8841 #endif
8842 if (target != op0)
8843 emit_move_insn (target, op0);
8845 temp = gen_label_rtx ();
8846 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8847 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8848 -1);
8850 emit_move_insn (target, op1);
8851 emit_label (temp);
8852 return target;
8854 case BIT_NOT_EXPR:
8855 op0 = expand_expr (treeop0, subtarget,
8856 VOIDmode, EXPAND_NORMAL);
8857 if (modifier == EXPAND_STACK_PARM)
8858 target = 0;
8859 /* In case we have to reduce the result to bitfield precision
8860 for unsigned bitfield expand this as XOR with a proper constant
8861 instead. */
8862 if (reduce_bit_field && TYPE_UNSIGNED (type))
8863 temp = expand_binop (mode, xor_optab, op0,
8864 immed_double_int_const
8865 (double_int::mask (TYPE_PRECISION (type)), mode),
8866 target, 1, OPTAB_LIB_WIDEN);
8867 else
8868 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8869 gcc_assert (temp);
8870 return temp;
8872 /* ??? Can optimize bitwise operations with one arg constant.
8873 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8874 and (a bitwise1 b) bitwise2 b (etc)
8875 but that is probably not worth while. */
8877 case BIT_AND_EXPR:
8878 case BIT_IOR_EXPR:
8879 case BIT_XOR_EXPR:
8880 goto binop;
8882 case LROTATE_EXPR:
8883 case RROTATE_EXPR:
8884 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8885 || (GET_MODE_PRECISION (TYPE_MODE (type))
8886 == TYPE_PRECISION (type)));
8887 /* fall through */
8889 case LSHIFT_EXPR:
8890 case RSHIFT_EXPR:
8891 /* If this is a fixed-point operation, then we cannot use the code
8892 below because "expand_shift" doesn't support sat/no-sat fixed-point
8893 shifts. */
8894 if (ALL_FIXED_POINT_MODE_P (mode))
8895 goto binop;
8897 if (! safe_from_p (subtarget, treeop1, 1))
8898 subtarget = 0;
8899 if (modifier == EXPAND_STACK_PARM)
8900 target = 0;
8901 op0 = expand_expr (treeop0, subtarget,
8902 VOIDmode, EXPAND_NORMAL);
8903 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8904 unsignedp);
8905 if (code == LSHIFT_EXPR)
8906 temp = REDUCE_BIT_FIELD (temp);
8907 return temp;
8909 /* Could determine the answer when only additive constants differ. Also,
8910 the addition of one can be handled by changing the condition. */
8911 case LT_EXPR:
8912 case LE_EXPR:
8913 case GT_EXPR:
8914 case GE_EXPR:
8915 case EQ_EXPR:
8916 case NE_EXPR:
8917 case UNORDERED_EXPR:
8918 case ORDERED_EXPR:
8919 case UNLT_EXPR:
8920 case UNLE_EXPR:
8921 case UNGT_EXPR:
8922 case UNGE_EXPR:
8923 case UNEQ_EXPR:
8924 case LTGT_EXPR:
8925 temp = do_store_flag (ops,
8926 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8927 tmode != VOIDmode ? tmode : mode);
8928 if (temp)
8929 return temp;
8931 /* Use a compare and a jump for BLKmode comparisons, or for function
8932 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
8934 if ((target == 0
8935 || modifier == EXPAND_STACK_PARM
8936 || ! safe_from_p (target, treeop0, 1)
8937 || ! safe_from_p (target, treeop1, 1)
8938 /* Make sure we don't have a hard reg (such as function's return
8939 value) live across basic blocks, if not optimizing. */
8940 || (!optimize && REG_P (target)
8941 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8942 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8944 emit_move_insn (target, const0_rtx);
8946 op1 = gen_label_rtx ();
8947 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
8949 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8950 emit_move_insn (target, constm1_rtx);
8951 else
8952 emit_move_insn (target, const1_rtx);
8954 emit_label (op1);
8955 return target;
8957 case COMPLEX_EXPR:
8958 /* Get the rtx code of the operands. */
8959 op0 = expand_normal (treeop0);
8960 op1 = expand_normal (treeop1);
8962 if (!target)
8963 target = gen_reg_rtx (TYPE_MODE (type));
8964 else
8965 /* If target overlaps with op1, then either we need to force
8966 op1 into a pseudo (if target also overlaps with op0),
8967 or write the complex parts in reverse order. */
8968 switch (GET_CODE (target))
8970 case CONCAT:
8971 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
8973 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
8975 complex_expr_force_op1:
8976 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
8977 emit_move_insn (temp, op1);
8978 op1 = temp;
8979 break;
8981 complex_expr_swap_order:
8982 /* Move the imaginary (op1) and real (op0) parts to their
8983 location. */
8984 write_complex_part (target, op1, true);
8985 write_complex_part (target, op0, false);
8987 return target;
8989 break;
8990 case MEM:
8991 temp = adjust_address_nv (target,
8992 GET_MODE_INNER (GET_MODE (target)), 0);
8993 if (reg_overlap_mentioned_p (temp, op1))
8995 enum machine_mode imode = GET_MODE_INNER (GET_MODE (target));
8996 temp = adjust_address_nv (target, imode,
8997 GET_MODE_SIZE (imode));
8998 if (reg_overlap_mentioned_p (temp, op0))
8999 goto complex_expr_force_op1;
9000 goto complex_expr_swap_order;
9002 break;
9003 default:
9004 if (reg_overlap_mentioned_p (target, op1))
9006 if (reg_overlap_mentioned_p (target, op0))
9007 goto complex_expr_force_op1;
9008 goto complex_expr_swap_order;
9010 break;
9013 /* Move the real (op0) and imaginary (op1) parts to their location. */
9014 write_complex_part (target, op0, false);
9015 write_complex_part (target, op1, true);
9017 return target;
9019 case WIDEN_SUM_EXPR:
9021 tree oprnd0 = treeop0;
9022 tree oprnd1 = treeop1;
9024 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9025 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9026 target, unsignedp);
9027 return target;
9030 case REDUC_MAX_EXPR:
9031 case REDUC_MIN_EXPR:
9032 case REDUC_PLUS_EXPR:
9034 op0 = expand_normal (treeop0);
9035 this_optab = optab_for_tree_code (code, type, optab_default);
9036 temp = expand_unop (mode, this_optab, op0, target, unsignedp);
9037 gcc_assert (temp);
9038 return temp;
9041 case VEC_LSHIFT_EXPR:
9042 case VEC_RSHIFT_EXPR:
9044 target = expand_vec_shift_expr (ops, target);
9045 return target;
9048 case VEC_UNPACK_HI_EXPR:
9049 case VEC_UNPACK_LO_EXPR:
9051 op0 = expand_normal (treeop0);
9052 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9053 target, unsignedp);
9054 gcc_assert (temp);
9055 return temp;
9058 case VEC_UNPACK_FLOAT_HI_EXPR:
9059 case VEC_UNPACK_FLOAT_LO_EXPR:
9061 op0 = expand_normal (treeop0);
9062 /* The signedness is determined from input operand. */
9063 temp = expand_widen_pattern_expr
9064 (ops, op0, NULL_RTX, NULL_RTX,
9065 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9067 gcc_assert (temp);
9068 return temp;
9071 case VEC_WIDEN_MULT_HI_EXPR:
9072 case VEC_WIDEN_MULT_LO_EXPR:
9073 case VEC_WIDEN_MULT_EVEN_EXPR:
9074 case VEC_WIDEN_MULT_ODD_EXPR:
9075 case VEC_WIDEN_LSHIFT_HI_EXPR:
9076 case VEC_WIDEN_LSHIFT_LO_EXPR:
9077 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9078 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9079 target, unsignedp);
9080 gcc_assert (target);
9081 return target;
9083 case VEC_PACK_TRUNC_EXPR:
9084 case VEC_PACK_SAT_EXPR:
9085 case VEC_PACK_FIX_TRUNC_EXPR:
9086 mode = TYPE_MODE (TREE_TYPE (treeop0));
9087 goto binop;
9089 case VEC_PERM_EXPR:
9090 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9091 op2 = expand_normal (treeop2);
9093 /* Careful here: if the target doesn't support integral vector modes,
9094 a constant selection vector could wind up smooshed into a normal
9095 integral constant. */
9096 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9098 tree sel_type = TREE_TYPE (treeop2);
9099 enum machine_mode vmode
9100 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9101 TYPE_VECTOR_SUBPARTS (sel_type));
9102 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9103 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9104 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9106 else
9107 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9109 temp = expand_vec_perm (mode, op0, op1, op2, target);
9110 gcc_assert (temp);
9111 return temp;
9113 case DOT_PROD_EXPR:
9115 tree oprnd0 = treeop0;
9116 tree oprnd1 = treeop1;
9117 tree oprnd2 = treeop2;
9118 rtx op2;
9120 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9121 op2 = expand_normal (oprnd2);
9122 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9123 target, unsignedp);
9124 return target;
9127 case REALIGN_LOAD_EXPR:
9129 tree oprnd0 = treeop0;
9130 tree oprnd1 = treeop1;
9131 tree oprnd2 = treeop2;
9132 rtx op2;
9134 this_optab = optab_for_tree_code (code, type, optab_default);
9135 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9136 op2 = expand_normal (oprnd2);
9137 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9138 target, unsignedp);
9139 gcc_assert (temp);
9140 return temp;
9143 case COND_EXPR:
9144 /* A COND_EXPR with its type being VOID_TYPE represents a
9145 conditional jump and is handled in
9146 expand_gimple_cond_expr. */
9147 gcc_assert (!VOID_TYPE_P (type));
9149 /* Note that COND_EXPRs whose type is a structure or union
9150 are required to be constructed to contain assignments of
9151 a temporary variable, so that we can evaluate them here
9152 for side effect only. If type is void, we must do likewise. */
9154 gcc_assert (!TREE_ADDRESSABLE (type)
9155 && !ignore
9156 && TREE_TYPE (treeop1) != void_type_node
9157 && TREE_TYPE (treeop2) != void_type_node);
9159 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9160 if (temp)
9161 return temp;
9163 /* If we are not to produce a result, we have no target. Otherwise,
9164 if a target was specified use it; it will not be used as an
9165 intermediate target unless it is safe. If no target, use a
9166 temporary. */
9168 if (modifier != EXPAND_STACK_PARM
9169 && original_target
9170 && safe_from_p (original_target, treeop0, 1)
9171 && GET_MODE (original_target) == mode
9172 && !MEM_P (original_target))
9173 temp = original_target;
9174 else
9175 temp = assign_temp (type, 0, 1);
9177 do_pending_stack_adjust ();
9178 NO_DEFER_POP;
9179 op0 = gen_label_rtx ();
9180 op1 = gen_label_rtx ();
9181 jumpifnot (treeop0, op0, -1);
9182 store_expr (treeop1, temp,
9183 modifier == EXPAND_STACK_PARM,
9184 false);
9186 emit_jump_insn (gen_jump (op1));
9187 emit_barrier ();
9188 emit_label (op0);
9189 store_expr (treeop2, temp,
9190 modifier == EXPAND_STACK_PARM,
9191 false);
9193 emit_label (op1);
9194 OK_DEFER_POP;
9195 return temp;
9197 case VEC_COND_EXPR:
9198 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9199 return target;
9201 default:
9202 gcc_unreachable ();
9205 /* Here to do an ordinary binary operator. */
9206 binop:
9207 expand_operands (treeop0, treeop1,
9208 subtarget, &op0, &op1, EXPAND_NORMAL);
9209 binop2:
9210 this_optab = optab_for_tree_code (code, type, optab_default);
9211 binop3:
9212 if (modifier == EXPAND_STACK_PARM)
9213 target = 0;
9214 temp = expand_binop (mode, this_optab, op0, op1, target,
9215 unsignedp, OPTAB_LIB_WIDEN);
9216 gcc_assert (temp);
9217 /* Bitwise operations do not need bitfield reduction as we expect their
9218 operands being properly truncated. */
9219 if (code == BIT_XOR_EXPR
9220 || code == BIT_AND_EXPR
9221 || code == BIT_IOR_EXPR)
9222 return temp;
9223 return REDUCE_BIT_FIELD (temp);
9225 #undef REDUCE_BIT_FIELD
9228 /* Return TRUE if expression STMT is suitable for replacement.
9229 Never consider memory loads as replaceable, because those don't ever lead
9230 into constant expressions. */
9232 static bool
9233 stmt_is_replaceable_p (gimple stmt)
9235 if (ssa_is_replaceable_p (stmt))
9237 /* Don't move around loads. */
9238 if (!gimple_assign_single_p (stmt)
9239 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9240 return true;
9242 return false;
9246 expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode,
9247 enum expand_modifier modifier, rtx *alt_rtl,
9248 bool inner_reference_p)
9250 rtx op0, op1, temp, decl_rtl;
9251 tree type;
9252 int unsignedp;
9253 enum machine_mode mode;
9254 enum tree_code code = TREE_CODE (exp);
9255 rtx subtarget, original_target;
9256 int ignore;
9257 tree context;
9258 bool reduce_bit_field;
9259 location_t loc = EXPR_LOCATION (exp);
9260 struct separate_ops ops;
9261 tree treeop0, treeop1, treeop2;
9262 tree ssa_name = NULL_TREE;
9263 gimple g;
9265 type = TREE_TYPE (exp);
9266 mode = TYPE_MODE (type);
9267 unsignedp = TYPE_UNSIGNED (type);
9269 treeop0 = treeop1 = treeop2 = NULL_TREE;
9270 if (!VL_EXP_CLASS_P (exp))
9271 switch (TREE_CODE_LENGTH (code))
9273 default:
9274 case 3: treeop2 = TREE_OPERAND (exp, 2);
9275 case 2: treeop1 = TREE_OPERAND (exp, 1);
9276 case 1: treeop0 = TREE_OPERAND (exp, 0);
9277 case 0: break;
9279 ops.code = code;
9280 ops.type = type;
9281 ops.op0 = treeop0;
9282 ops.op1 = treeop1;
9283 ops.op2 = treeop2;
9284 ops.location = loc;
9286 ignore = (target == const0_rtx
9287 || ((CONVERT_EXPR_CODE_P (code)
9288 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9289 && TREE_CODE (type) == VOID_TYPE));
9291 /* An operation in what may be a bit-field type needs the
9292 result to be reduced to the precision of the bit-field type,
9293 which is narrower than that of the type's mode. */
9294 reduce_bit_field = (!ignore
9295 && INTEGRAL_TYPE_P (type)
9296 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9298 /* If we are going to ignore this result, we need only do something
9299 if there is a side-effect somewhere in the expression. If there
9300 is, short-circuit the most common cases here. Note that we must
9301 not call expand_expr with anything but const0_rtx in case this
9302 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9304 if (ignore)
9306 if (! TREE_SIDE_EFFECTS (exp))
9307 return const0_rtx;
9309 /* Ensure we reference a volatile object even if value is ignored, but
9310 don't do this if all we are doing is taking its address. */
9311 if (TREE_THIS_VOLATILE (exp)
9312 && TREE_CODE (exp) != FUNCTION_DECL
9313 && mode != VOIDmode && mode != BLKmode
9314 && modifier != EXPAND_CONST_ADDRESS)
9316 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9317 if (MEM_P (temp))
9318 copy_to_reg (temp);
9319 return const0_rtx;
9322 if (TREE_CODE_CLASS (code) == tcc_unary
9323 || code == BIT_FIELD_REF
9324 || code == COMPONENT_REF
9325 || code == INDIRECT_REF)
9326 return expand_expr (treeop0, const0_rtx, VOIDmode,
9327 modifier);
9329 else if (TREE_CODE_CLASS (code) == tcc_binary
9330 || TREE_CODE_CLASS (code) == tcc_comparison
9331 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9333 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9334 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9335 return const0_rtx;
9338 target = 0;
9341 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9342 target = 0;
9344 /* Use subtarget as the target for operand 0 of a binary operation. */
9345 subtarget = get_subtarget (target);
9346 original_target = target;
9348 switch (code)
9350 case LABEL_DECL:
9352 tree function = decl_function_context (exp);
9354 temp = label_rtx (exp);
9355 temp = gen_rtx_LABEL_REF (Pmode, temp);
9357 if (function != current_function_decl
9358 && function != 0)
9359 LABEL_REF_NONLOCAL_P (temp) = 1;
9361 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9362 return temp;
9365 case SSA_NAME:
9366 /* ??? ivopts calls expander, without any preparation from
9367 out-of-ssa. So fake instructions as if this was an access to the
9368 base variable. This unnecessarily allocates a pseudo, see how we can
9369 reuse it, if partition base vars have it set already. */
9370 if (!currently_expanding_to_rtl)
9372 tree var = SSA_NAME_VAR (exp);
9373 if (var && DECL_RTL_SET_P (var))
9374 return DECL_RTL (var);
9375 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9376 LAST_VIRTUAL_REGISTER + 1);
9379 g = get_gimple_for_ssa_name (exp);
9380 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9381 if (g == NULL
9382 && modifier == EXPAND_INITIALIZER
9383 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9384 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9385 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9386 g = SSA_NAME_DEF_STMT (exp);
9387 if (g)
9389 rtx r;
9390 ops.code = gimple_assign_rhs_code (g);
9391 switch (get_gimple_rhs_class (ops.code))
9393 case GIMPLE_TERNARY_RHS:
9394 ops.op2 = gimple_assign_rhs3 (g);
9395 /* Fallthru */
9396 case GIMPLE_BINARY_RHS:
9397 ops.op1 = gimple_assign_rhs2 (g);
9398 /* Fallthru */
9399 case GIMPLE_UNARY_RHS:
9400 ops.op0 = gimple_assign_rhs1 (g);
9401 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9402 ops.location = gimple_location (g);
9403 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9404 break;
9405 case GIMPLE_SINGLE_RHS:
9407 location_t saved_loc = curr_insn_location ();
9408 set_curr_insn_location (gimple_location (g));
9409 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9410 tmode, modifier, NULL, inner_reference_p);
9411 set_curr_insn_location (saved_loc);
9412 break;
9414 default:
9415 gcc_unreachable ();
9417 if (REG_P (r) && !REG_EXPR (r))
9418 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9419 return r;
9422 ssa_name = exp;
9423 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9424 exp = SSA_NAME_VAR (ssa_name);
9425 goto expand_decl_rtl;
9427 case PARM_DECL:
9428 case VAR_DECL:
9429 /* If a static var's type was incomplete when the decl was written,
9430 but the type is complete now, lay out the decl now. */
9431 if (DECL_SIZE (exp) == 0
9432 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9433 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9434 layout_decl (exp, 0);
9436 /* ... fall through ... */
9438 case FUNCTION_DECL:
9439 case RESULT_DECL:
9440 decl_rtl = DECL_RTL (exp);
9441 expand_decl_rtl:
9442 gcc_assert (decl_rtl);
9443 decl_rtl = copy_rtx (decl_rtl);
9444 /* Record writes to register variables. */
9445 if (modifier == EXPAND_WRITE
9446 && REG_P (decl_rtl)
9447 && HARD_REGISTER_P (decl_rtl))
9448 add_to_hard_reg_set (&crtl->asm_clobbers,
9449 GET_MODE (decl_rtl), REGNO (decl_rtl));
9451 /* Ensure variable marked as used even if it doesn't go through
9452 a parser. If it hasn't be used yet, write out an external
9453 definition. */
9454 TREE_USED (exp) = 1;
9456 /* Show we haven't gotten RTL for this yet. */
9457 temp = 0;
9459 /* Variables inherited from containing functions should have
9460 been lowered by this point. */
9461 context = decl_function_context (exp);
9462 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9463 || context == current_function_decl
9464 || TREE_STATIC (exp)
9465 || DECL_EXTERNAL (exp)
9466 /* ??? C++ creates functions that are not TREE_STATIC. */
9467 || TREE_CODE (exp) == FUNCTION_DECL);
9469 /* This is the case of an array whose size is to be determined
9470 from its initializer, while the initializer is still being parsed.
9471 ??? We aren't parsing while expanding anymore. */
9473 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9474 temp = validize_mem (decl_rtl);
9476 /* If DECL_RTL is memory, we are in the normal case and the
9477 address is not valid, get the address into a register. */
9479 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9481 if (alt_rtl)
9482 *alt_rtl = decl_rtl;
9483 decl_rtl = use_anchored_address (decl_rtl);
9484 if (modifier != EXPAND_CONST_ADDRESS
9485 && modifier != EXPAND_SUM
9486 && !memory_address_addr_space_p (DECL_MODE (exp),
9487 XEXP (decl_rtl, 0),
9488 MEM_ADDR_SPACE (decl_rtl)))
9489 temp = replace_equiv_address (decl_rtl,
9490 copy_rtx (XEXP (decl_rtl, 0)));
9493 /* If we got something, return it. But first, set the alignment
9494 if the address is a register. */
9495 if (temp != 0)
9497 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9498 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9500 return temp;
9503 /* If the mode of DECL_RTL does not match that of the decl,
9504 there are two cases: we are dealing with a BLKmode value
9505 that is returned in a register, or we are dealing with
9506 a promoted value. In the latter case, return a SUBREG
9507 of the wanted mode, but mark it so that we know that it
9508 was already extended. */
9509 if (REG_P (decl_rtl)
9510 && DECL_MODE (exp) != BLKmode
9511 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9513 enum machine_mode pmode;
9515 /* Get the signedness to be used for this variable. Ensure we get
9516 the same mode we got when the variable was declared. */
9517 if (code == SSA_NAME
9518 && (g = SSA_NAME_DEF_STMT (ssa_name))
9519 && gimple_code (g) == GIMPLE_CALL
9520 && !gimple_call_internal_p (g))
9521 pmode = promote_function_mode (type, mode, &unsignedp,
9522 gimple_call_fntype (g),
9524 else
9525 pmode = promote_decl_mode (exp, &unsignedp);
9526 gcc_assert (GET_MODE (decl_rtl) == pmode);
9528 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9529 SUBREG_PROMOTED_VAR_P (temp) = 1;
9530 SUBREG_PROMOTED_UNSIGNED_SET (temp, unsignedp);
9531 return temp;
9534 return decl_rtl;
9536 case INTEGER_CST:
9537 temp = immed_double_const (TREE_INT_CST_LOW (exp),
9538 TREE_INT_CST_HIGH (exp), mode);
9540 return temp;
9542 case VECTOR_CST:
9544 tree tmp = NULL_TREE;
9545 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9546 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9547 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9548 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9549 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9550 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9551 return const_vector_from_tree (exp);
9552 if (GET_MODE_CLASS (mode) == MODE_INT)
9554 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9555 if (type_for_mode)
9556 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9558 if (!tmp)
9560 vec<constructor_elt, va_gc> *v;
9561 unsigned i;
9562 vec_alloc (v, VECTOR_CST_NELTS (exp));
9563 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9564 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9565 tmp = build_constructor (type, v);
9567 return expand_expr (tmp, ignore ? const0_rtx : target,
9568 tmode, modifier);
9571 case CONST_DECL:
9572 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9574 case REAL_CST:
9575 /* If optimized, generate immediate CONST_DOUBLE
9576 which will be turned into memory by reload if necessary.
9578 We used to force a register so that loop.c could see it. But
9579 this does not allow gen_* patterns to perform optimizations with
9580 the constants. It also produces two insns in cases like "x = 1.0;".
9581 On most machines, floating-point constants are not permitted in
9582 many insns, so we'd end up copying it to a register in any case.
9584 Now, we do the copying in expand_binop, if appropriate. */
9585 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9586 TYPE_MODE (TREE_TYPE (exp)));
9588 case FIXED_CST:
9589 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9590 TYPE_MODE (TREE_TYPE (exp)));
9592 case COMPLEX_CST:
9593 /* Handle evaluating a complex constant in a CONCAT target. */
9594 if (original_target && GET_CODE (original_target) == CONCAT)
9596 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9597 rtx rtarg, itarg;
9599 rtarg = XEXP (original_target, 0);
9600 itarg = XEXP (original_target, 1);
9602 /* Move the real and imaginary parts separately. */
9603 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9604 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9606 if (op0 != rtarg)
9607 emit_move_insn (rtarg, op0);
9608 if (op1 != itarg)
9609 emit_move_insn (itarg, op1);
9611 return original_target;
9614 /* ... fall through ... */
9616 case STRING_CST:
9617 temp = expand_expr_constant (exp, 1, modifier);
9619 /* temp contains a constant address.
9620 On RISC machines where a constant address isn't valid,
9621 make some insns to get that address into a register. */
9622 if (modifier != EXPAND_CONST_ADDRESS
9623 && modifier != EXPAND_INITIALIZER
9624 && modifier != EXPAND_SUM
9625 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9626 MEM_ADDR_SPACE (temp)))
9627 return replace_equiv_address (temp,
9628 copy_rtx (XEXP (temp, 0)));
9629 return temp;
9631 case SAVE_EXPR:
9633 tree val = treeop0;
9634 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9635 inner_reference_p);
9637 if (!SAVE_EXPR_RESOLVED_P (exp))
9639 /* We can indeed still hit this case, typically via builtin
9640 expanders calling save_expr immediately before expanding
9641 something. Assume this means that we only have to deal
9642 with non-BLKmode values. */
9643 gcc_assert (GET_MODE (ret) != BLKmode);
9645 val = build_decl (curr_insn_location (),
9646 VAR_DECL, NULL, TREE_TYPE (exp));
9647 DECL_ARTIFICIAL (val) = 1;
9648 DECL_IGNORED_P (val) = 1;
9649 treeop0 = val;
9650 TREE_OPERAND (exp, 0) = treeop0;
9651 SAVE_EXPR_RESOLVED_P (exp) = 1;
9653 if (!CONSTANT_P (ret))
9654 ret = copy_to_reg (ret);
9655 SET_DECL_RTL (val, ret);
9658 return ret;
9662 case CONSTRUCTOR:
9663 /* If we don't need the result, just ensure we evaluate any
9664 subexpressions. */
9665 if (ignore)
9667 unsigned HOST_WIDE_INT idx;
9668 tree value;
9670 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9671 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9673 return const0_rtx;
9676 return expand_constructor (exp, target, modifier, false);
9678 case TARGET_MEM_REF:
9680 addr_space_t as
9681 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9682 enum insn_code icode;
9683 unsigned int align;
9685 op0 = addr_for_mem_ref (exp, as, true);
9686 op0 = memory_address_addr_space (mode, op0, as);
9687 temp = gen_rtx_MEM (mode, op0);
9688 set_mem_attributes (temp, exp, 0);
9689 set_mem_addr_space (temp, as);
9690 align = get_object_alignment (exp);
9691 if (modifier != EXPAND_WRITE
9692 && modifier != EXPAND_MEMORY
9693 && mode != BLKmode
9694 && align < GET_MODE_ALIGNMENT (mode)
9695 /* If the target does not have special handling for unaligned
9696 loads of mode then it can use regular moves for them. */
9697 && ((icode = optab_handler (movmisalign_optab, mode))
9698 != CODE_FOR_nothing))
9700 struct expand_operand ops[2];
9702 /* We've already validated the memory, and we're creating a
9703 new pseudo destination. The predicates really can't fail,
9704 nor can the generator. */
9705 create_output_operand (&ops[0], NULL_RTX, mode);
9706 create_fixed_operand (&ops[1], temp);
9707 expand_insn (icode, 2, ops);
9708 temp = ops[0].value;
9710 return temp;
9713 case MEM_REF:
9715 addr_space_t as
9716 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9717 enum machine_mode address_mode;
9718 tree base = TREE_OPERAND (exp, 0);
9719 gimple def_stmt;
9720 enum insn_code icode;
9721 unsigned align;
9722 /* Handle expansion of non-aliased memory with non-BLKmode. That
9723 might end up in a register. */
9724 if (mem_ref_refers_to_non_mem_p (exp))
9726 HOST_WIDE_INT offset = mem_ref_offset (exp).low;
9727 base = TREE_OPERAND (base, 0);
9728 if (offset == 0
9729 && tree_fits_uhwi_p (TYPE_SIZE (type))
9730 && (GET_MODE_BITSIZE (DECL_MODE (base))
9731 == tree_to_uhwi (TYPE_SIZE (type))))
9732 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9733 target, tmode, modifier);
9734 if (TYPE_MODE (type) == BLKmode)
9736 temp = assign_stack_temp (DECL_MODE (base),
9737 GET_MODE_SIZE (DECL_MODE (base)));
9738 store_expr (base, temp, 0, false);
9739 temp = adjust_address (temp, BLKmode, offset);
9740 set_mem_size (temp, int_size_in_bytes (type));
9741 return temp;
9743 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9744 bitsize_int (offset * BITS_PER_UNIT));
9745 return expand_expr (exp, target, tmode, modifier);
9747 address_mode = targetm.addr_space.address_mode (as);
9748 base = TREE_OPERAND (exp, 0);
9749 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9751 tree mask = gimple_assign_rhs2 (def_stmt);
9752 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9753 gimple_assign_rhs1 (def_stmt), mask);
9754 TREE_OPERAND (exp, 0) = base;
9756 align = get_object_alignment (exp);
9757 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9758 op0 = memory_address_addr_space (mode, op0, as);
9759 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9761 rtx off
9762 = immed_double_int_const (mem_ref_offset (exp), address_mode);
9763 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9764 op0 = memory_address_addr_space (mode, op0, as);
9766 temp = gen_rtx_MEM (mode, op0);
9767 set_mem_attributes (temp, exp, 0);
9768 set_mem_addr_space (temp, as);
9769 if (TREE_THIS_VOLATILE (exp))
9770 MEM_VOLATILE_P (temp) = 1;
9771 if (modifier != EXPAND_WRITE
9772 && modifier != EXPAND_MEMORY
9773 && !inner_reference_p
9774 && mode != BLKmode
9775 && align < GET_MODE_ALIGNMENT (mode))
9777 if ((icode = optab_handler (movmisalign_optab, mode))
9778 != CODE_FOR_nothing)
9780 struct expand_operand ops[2];
9782 /* We've already validated the memory, and we're creating a
9783 new pseudo destination. The predicates really can't fail,
9784 nor can the generator. */
9785 create_output_operand (&ops[0], NULL_RTX, mode);
9786 create_fixed_operand (&ops[1], temp);
9787 expand_insn (icode, 2, ops);
9788 temp = ops[0].value;
9790 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9791 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9792 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9793 (modifier == EXPAND_STACK_PARM
9794 ? NULL_RTX : target),
9795 mode, mode);
9797 return temp;
9800 case ARRAY_REF:
9803 tree array = treeop0;
9804 tree index = treeop1;
9805 tree init;
9807 /* Fold an expression like: "foo"[2].
9808 This is not done in fold so it won't happen inside &.
9809 Don't fold if this is for wide characters since it's too
9810 difficult to do correctly and this is a very rare case. */
9812 if (modifier != EXPAND_CONST_ADDRESS
9813 && modifier != EXPAND_INITIALIZER
9814 && modifier != EXPAND_MEMORY)
9816 tree t = fold_read_from_constant_string (exp);
9818 if (t)
9819 return expand_expr (t, target, tmode, modifier);
9822 /* If this is a constant index into a constant array,
9823 just get the value from the array. Handle both the cases when
9824 we have an explicit constructor and when our operand is a variable
9825 that was declared const. */
9827 if (modifier != EXPAND_CONST_ADDRESS
9828 && modifier != EXPAND_INITIALIZER
9829 && modifier != EXPAND_MEMORY
9830 && TREE_CODE (array) == CONSTRUCTOR
9831 && ! TREE_SIDE_EFFECTS (array)
9832 && TREE_CODE (index) == INTEGER_CST)
9834 unsigned HOST_WIDE_INT ix;
9835 tree field, value;
9837 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9838 field, value)
9839 if (tree_int_cst_equal (field, index))
9841 if (!TREE_SIDE_EFFECTS (value))
9842 return expand_expr (fold (value), target, tmode, modifier);
9843 break;
9847 else if (optimize >= 1
9848 && modifier != EXPAND_CONST_ADDRESS
9849 && modifier != EXPAND_INITIALIZER
9850 && modifier != EXPAND_MEMORY
9851 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9852 && TREE_CODE (index) == INTEGER_CST
9853 && (TREE_CODE (array) == VAR_DECL
9854 || TREE_CODE (array) == CONST_DECL)
9855 && (init = ctor_for_folding (array)) != error_mark_node)
9857 if (init == NULL_TREE)
9859 tree value = build_zero_cst (type);
9860 if (TREE_CODE (value) == CONSTRUCTOR)
9862 /* If VALUE is a CONSTRUCTOR, this optimization is only
9863 useful if this doesn't store the CONSTRUCTOR into
9864 memory. If it does, it is more efficient to just
9865 load the data from the array directly. */
9866 rtx ret = expand_constructor (value, target,
9867 modifier, true);
9868 if (ret == NULL_RTX)
9869 value = NULL_TREE;
9872 if (value)
9873 return expand_expr (value, target, tmode, modifier);
9875 else if (TREE_CODE (init) == CONSTRUCTOR)
9877 unsigned HOST_WIDE_INT ix;
9878 tree field, value;
9880 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9881 field, value)
9882 if (tree_int_cst_equal (field, index))
9884 if (TREE_SIDE_EFFECTS (value))
9885 break;
9887 if (TREE_CODE (value) == CONSTRUCTOR)
9889 /* If VALUE is a CONSTRUCTOR, this
9890 optimization is only useful if
9891 this doesn't store the CONSTRUCTOR
9892 into memory. If it does, it is more
9893 efficient to just load the data from
9894 the array directly. */
9895 rtx ret = expand_constructor (value, target,
9896 modifier, true);
9897 if (ret == NULL_RTX)
9898 break;
9901 return
9902 expand_expr (fold (value), target, tmode, modifier);
9905 else if (TREE_CODE (init) == STRING_CST)
9907 tree low_bound = array_ref_low_bound (exp);
9908 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
9910 /* Optimize the special case of a zero lower bound.
9912 We convert the lower bound to sizetype to avoid problems
9913 with constant folding. E.g. suppose the lower bound is
9914 1 and its mode is QI. Without the conversion
9915 (ARRAY + (INDEX - (unsigned char)1))
9916 becomes
9917 (ARRAY + (-(unsigned char)1) + INDEX)
9918 which becomes
9919 (ARRAY + 255 + INDEX). Oops! */
9920 if (!integer_zerop (low_bound))
9921 index1 = size_diffop_loc (loc, index1,
9922 fold_convert_loc (loc, sizetype,
9923 low_bound));
9925 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
9927 tree type = TREE_TYPE (TREE_TYPE (init));
9928 enum machine_mode mode = TYPE_MODE (type);
9930 if (GET_MODE_CLASS (mode) == MODE_INT
9931 && GET_MODE_SIZE (mode) == 1)
9932 return gen_int_mode (TREE_STRING_POINTER (init)
9933 [TREE_INT_CST_LOW (index1)],
9934 mode);
9939 goto normal_inner_ref;
9941 case COMPONENT_REF:
9942 /* If the operand is a CONSTRUCTOR, we can just extract the
9943 appropriate field if it is present. */
9944 if (TREE_CODE (treeop0) == CONSTRUCTOR)
9946 unsigned HOST_WIDE_INT idx;
9947 tree field, value;
9949 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
9950 idx, field, value)
9951 if (field == treeop1
9952 /* We can normally use the value of the field in the
9953 CONSTRUCTOR. However, if this is a bitfield in
9954 an integral mode that we can fit in a HOST_WIDE_INT,
9955 we must mask only the number of bits in the bitfield,
9956 since this is done implicitly by the constructor. If
9957 the bitfield does not meet either of those conditions,
9958 we can't do this optimization. */
9959 && (! DECL_BIT_FIELD (field)
9960 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
9961 && (GET_MODE_PRECISION (DECL_MODE (field))
9962 <= HOST_BITS_PER_WIDE_INT))))
9964 if (DECL_BIT_FIELD (field)
9965 && modifier == EXPAND_STACK_PARM)
9966 target = 0;
9967 op0 = expand_expr (value, target, tmode, modifier);
9968 if (DECL_BIT_FIELD (field))
9970 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
9971 enum machine_mode imode = TYPE_MODE (TREE_TYPE (field));
9973 if (TYPE_UNSIGNED (TREE_TYPE (field)))
9975 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
9976 imode);
9977 op0 = expand_and (imode, op0, op1, target);
9979 else
9981 int count = GET_MODE_PRECISION (imode) - bitsize;
9983 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
9984 target, 0);
9985 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
9986 target, 0);
9990 return op0;
9993 goto normal_inner_ref;
9995 case BIT_FIELD_REF:
9996 case ARRAY_RANGE_REF:
9997 normal_inner_ref:
9999 enum machine_mode mode1, mode2;
10000 HOST_WIDE_INT bitsize, bitpos;
10001 tree offset;
10002 int volatilep = 0, must_force_mem;
10003 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10004 &mode1, &unsignedp, &volatilep, true);
10005 rtx orig_op0, memloc;
10006 bool mem_attrs_from_type = false;
10008 /* If we got back the original object, something is wrong. Perhaps
10009 we are evaluating an expression too early. In any event, don't
10010 infinitely recurse. */
10011 gcc_assert (tem != exp);
10013 /* If TEM's type is a union of variable size, pass TARGET to the inner
10014 computation, since it will need a temporary and TARGET is known
10015 to have to do. This occurs in unchecked conversion in Ada. */
10016 orig_op0 = op0
10017 = expand_expr_real (tem,
10018 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10019 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10020 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10021 != INTEGER_CST)
10022 && modifier != EXPAND_STACK_PARM
10023 ? target : NULL_RTX),
10024 VOIDmode,
10025 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10026 NULL, true);
10028 /* If the field has a mode, we want to access it in the
10029 field's mode, not the computed mode.
10030 If a MEM has VOIDmode (external with incomplete type),
10031 use BLKmode for it instead. */
10032 if (MEM_P (op0))
10034 if (mode1 != VOIDmode)
10035 op0 = adjust_address (op0, mode1, 0);
10036 else if (GET_MODE (op0) == VOIDmode)
10037 op0 = adjust_address (op0, BLKmode, 0);
10040 mode2
10041 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10043 /* If we have either an offset, a BLKmode result, or a reference
10044 outside the underlying object, we must force it to memory.
10045 Such a case can occur in Ada if we have unchecked conversion
10046 of an expression from a scalar type to an aggregate type or
10047 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10048 passed a partially uninitialized object or a view-conversion
10049 to a larger size. */
10050 must_force_mem = (offset
10051 || mode1 == BLKmode
10052 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10054 /* Handle CONCAT first. */
10055 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10057 if (bitpos == 0
10058 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10059 return op0;
10060 if (bitpos == 0
10061 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10062 && bitsize)
10064 op0 = XEXP (op0, 0);
10065 mode2 = GET_MODE (op0);
10067 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10068 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10069 && bitpos
10070 && bitsize)
10072 op0 = XEXP (op0, 1);
10073 bitpos = 0;
10074 mode2 = GET_MODE (op0);
10076 else
10077 /* Otherwise force into memory. */
10078 must_force_mem = 1;
10081 /* If this is a constant, put it in a register if it is a legitimate
10082 constant and we don't need a memory reference. */
10083 if (CONSTANT_P (op0)
10084 && mode2 != BLKmode
10085 && targetm.legitimate_constant_p (mode2, op0)
10086 && !must_force_mem)
10087 op0 = force_reg (mode2, op0);
10089 /* Otherwise, if this is a constant, try to force it to the constant
10090 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10091 is a legitimate constant. */
10092 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10093 op0 = validize_mem (memloc);
10095 /* Otherwise, if this is a constant or the object is not in memory
10096 and need be, put it there. */
10097 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10099 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10100 emit_move_insn (memloc, op0);
10101 op0 = memloc;
10102 mem_attrs_from_type = true;
10105 if (offset)
10107 enum machine_mode address_mode;
10108 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10109 EXPAND_SUM);
10111 gcc_assert (MEM_P (op0));
10113 address_mode = get_address_mode (op0);
10114 if (GET_MODE (offset_rtx) != address_mode)
10115 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10117 if (GET_MODE (op0) == BLKmode
10118 /* The check for a constant address in OP0 not having VOIDmode
10119 is probably no longer necessary. */
10120 && GET_MODE (XEXP (op0, 0)) != VOIDmode
10121 && bitsize != 0
10122 && (bitpos % bitsize) == 0
10123 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10124 && MEM_ALIGN (op0) == GET_MODE_ALIGNMENT (mode1))
10126 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10127 bitpos = 0;
10130 op0 = offset_address (op0, offset_rtx,
10131 highest_pow2_factor (offset));
10134 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10135 record its alignment as BIGGEST_ALIGNMENT. */
10136 if (MEM_P (op0) && bitpos == 0 && offset != 0
10137 && is_aligning_offset (offset, tem))
10138 set_mem_align (op0, BIGGEST_ALIGNMENT);
10140 /* Don't forget about volatility even if this is a bitfield. */
10141 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10143 if (op0 == orig_op0)
10144 op0 = copy_rtx (op0);
10146 MEM_VOLATILE_P (op0) = 1;
10149 /* In cases where an aligned union has an unaligned object
10150 as a field, we might be extracting a BLKmode value from
10151 an integer-mode (e.g., SImode) object. Handle this case
10152 by doing the extract into an object as wide as the field
10153 (which we know to be the width of a basic mode), then
10154 storing into memory, and changing the mode to BLKmode. */
10155 if (mode1 == VOIDmode
10156 || REG_P (op0) || GET_CODE (op0) == SUBREG
10157 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10158 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10159 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10160 && modifier != EXPAND_CONST_ADDRESS
10161 && modifier != EXPAND_INITIALIZER
10162 && modifier != EXPAND_MEMORY)
10163 /* If the bitfield is volatile and the bitsize
10164 is narrower than the access size of the bitfield,
10165 we need to extract bitfields from the access. */
10166 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10167 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10168 && mode1 != BLKmode
10169 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10170 /* If the field isn't aligned enough to fetch as a memref,
10171 fetch it as a bit field. */
10172 || (mode1 != BLKmode
10173 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10174 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10175 || (MEM_P (op0)
10176 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10177 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10178 && modifier != EXPAND_MEMORY
10179 && ((modifier == EXPAND_CONST_ADDRESS
10180 || modifier == EXPAND_INITIALIZER)
10181 ? STRICT_ALIGNMENT
10182 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10183 || (bitpos % BITS_PER_UNIT != 0)))
10184 /* If the type and the field are a constant size and the
10185 size of the type isn't the same size as the bitfield,
10186 we must use bitfield operations. */
10187 || (bitsize >= 0
10188 && TYPE_SIZE (TREE_TYPE (exp))
10189 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10190 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10191 bitsize)))
10193 enum machine_mode ext_mode = mode;
10195 if (ext_mode == BLKmode
10196 && ! (target != 0 && MEM_P (op0)
10197 && MEM_P (target)
10198 && bitpos % BITS_PER_UNIT == 0))
10199 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10201 if (ext_mode == BLKmode)
10203 if (target == 0)
10204 target = assign_temp (type, 1, 1);
10206 /* ??? Unlike the similar test a few lines below, this one is
10207 very likely obsolete. */
10208 if (bitsize == 0)
10209 return target;
10211 /* In this case, BITPOS must start at a byte boundary and
10212 TARGET, if specified, must be a MEM. */
10213 gcc_assert (MEM_P (op0)
10214 && (!target || MEM_P (target))
10215 && !(bitpos % BITS_PER_UNIT));
10217 emit_block_move (target,
10218 adjust_address (op0, VOIDmode,
10219 bitpos / BITS_PER_UNIT),
10220 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10221 / BITS_PER_UNIT),
10222 (modifier == EXPAND_STACK_PARM
10223 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10225 return target;
10228 /* If we have nothing to extract, the result will be 0 for targets
10229 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10230 return 0 for the sake of consistency, as reading a zero-sized
10231 bitfield is valid in Ada and the value is fully specified. */
10232 if (bitsize == 0)
10233 return const0_rtx;
10235 op0 = validize_mem (op0);
10237 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10238 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10240 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10241 (modifier == EXPAND_STACK_PARM
10242 ? NULL_RTX : target),
10243 ext_mode, ext_mode);
10245 /* If the result is a record type and BITSIZE is narrower than
10246 the mode of OP0, an integral mode, and this is a big endian
10247 machine, we must put the field into the high-order bits. */
10248 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10249 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10250 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10251 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10252 GET_MODE_BITSIZE (GET_MODE (op0))
10253 - bitsize, op0, 1);
10255 /* If the result type is BLKmode, store the data into a temporary
10256 of the appropriate type, but with the mode corresponding to the
10257 mode for the data we have (op0's mode). */
10258 if (mode == BLKmode)
10260 rtx new_rtx
10261 = assign_stack_temp_for_type (ext_mode,
10262 GET_MODE_BITSIZE (ext_mode),
10263 type);
10264 emit_move_insn (new_rtx, op0);
10265 op0 = copy_rtx (new_rtx);
10266 PUT_MODE (op0, BLKmode);
10269 return op0;
10272 /* If the result is BLKmode, use that to access the object
10273 now as well. */
10274 if (mode == BLKmode)
10275 mode1 = BLKmode;
10277 /* Get a reference to just this component. */
10278 if (modifier == EXPAND_CONST_ADDRESS
10279 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10280 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10281 else
10282 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10284 if (op0 == orig_op0)
10285 op0 = copy_rtx (op0);
10287 /* If op0 is a temporary because of forcing to memory, pass only the
10288 type to set_mem_attributes so that the original expression is never
10289 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10290 if (mem_attrs_from_type)
10291 set_mem_attributes (op0, type, 0);
10292 else
10293 set_mem_attributes (op0, exp, 0);
10295 if (REG_P (XEXP (op0, 0)))
10296 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10298 MEM_VOLATILE_P (op0) |= volatilep;
10299 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10300 || modifier == EXPAND_CONST_ADDRESS
10301 || modifier == EXPAND_INITIALIZER)
10302 return op0;
10304 if (target == 0)
10305 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10307 convert_move (target, op0, unsignedp);
10308 return target;
10311 case OBJ_TYPE_REF:
10312 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10314 case CALL_EXPR:
10315 /* All valid uses of __builtin_va_arg_pack () are removed during
10316 inlining. */
10317 if (CALL_EXPR_VA_ARG_PACK (exp))
10318 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10320 tree fndecl = get_callee_fndecl (exp), attr;
10322 if (fndecl
10323 && (attr = lookup_attribute ("error",
10324 DECL_ATTRIBUTES (fndecl))) != NULL)
10325 error ("%Kcall to %qs declared with attribute error: %s",
10326 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10327 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10328 if (fndecl
10329 && (attr = lookup_attribute ("warning",
10330 DECL_ATTRIBUTES (fndecl))) != NULL)
10331 warning_at (tree_nonartificial_location (exp),
10332 0, "%Kcall to %qs declared with attribute warning: %s",
10333 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10334 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10336 /* Check for a built-in function. */
10337 if (fndecl && DECL_BUILT_IN (fndecl))
10339 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10340 return expand_builtin (exp, target, subtarget, tmode, ignore);
10343 return expand_call (exp, target, ignore);
10345 case VIEW_CONVERT_EXPR:
10346 op0 = NULL_RTX;
10348 /* If we are converting to BLKmode, try to avoid an intermediate
10349 temporary by fetching an inner memory reference. */
10350 if (mode == BLKmode
10351 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10352 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10353 && handled_component_p (treeop0))
10355 enum machine_mode mode1;
10356 HOST_WIDE_INT bitsize, bitpos;
10357 tree offset;
10358 int unsignedp;
10359 int volatilep = 0;
10360 tree tem
10361 = get_inner_reference (treeop0, &bitsize, &bitpos,
10362 &offset, &mode1, &unsignedp, &volatilep,
10363 true);
10364 rtx orig_op0;
10366 /* ??? We should work harder and deal with non-zero offsets. */
10367 if (!offset
10368 && (bitpos % BITS_PER_UNIT) == 0
10369 && bitsize >= 0
10370 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10372 /* See the normal_inner_ref case for the rationale. */
10373 orig_op0
10374 = expand_expr_real (tem,
10375 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10376 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10377 != INTEGER_CST)
10378 && modifier != EXPAND_STACK_PARM
10379 ? target : NULL_RTX),
10380 VOIDmode,
10381 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10382 NULL, true);
10384 if (MEM_P (orig_op0))
10386 op0 = orig_op0;
10388 /* Get a reference to just this component. */
10389 if (modifier == EXPAND_CONST_ADDRESS
10390 || modifier == EXPAND_SUM
10391 || modifier == EXPAND_INITIALIZER)
10392 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10393 else
10394 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10396 if (op0 == orig_op0)
10397 op0 = copy_rtx (op0);
10399 set_mem_attributes (op0, treeop0, 0);
10400 if (REG_P (XEXP (op0, 0)))
10401 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10403 MEM_VOLATILE_P (op0) |= volatilep;
10408 if (!op0)
10409 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10410 NULL, inner_reference_p);
10412 /* If the input and output modes are both the same, we are done. */
10413 if (mode == GET_MODE (op0))
10415 /* If neither mode is BLKmode, and both modes are the same size
10416 then we can use gen_lowpart. */
10417 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10418 && (GET_MODE_PRECISION (mode)
10419 == GET_MODE_PRECISION (GET_MODE (op0)))
10420 && !COMPLEX_MODE_P (GET_MODE (op0)))
10422 if (GET_CODE (op0) == SUBREG)
10423 op0 = force_reg (GET_MODE (op0), op0);
10424 temp = gen_lowpart_common (mode, op0);
10425 if (temp)
10426 op0 = temp;
10427 else
10429 if (!REG_P (op0) && !MEM_P (op0))
10430 op0 = force_reg (GET_MODE (op0), op0);
10431 op0 = gen_lowpart (mode, op0);
10434 /* If both types are integral, convert from one mode to the other. */
10435 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10436 op0 = convert_modes (mode, GET_MODE (op0), op0,
10437 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10438 /* If the output type is a bit-field type, do an extraction. */
10439 else if (reduce_bit_field)
10440 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10441 TYPE_UNSIGNED (type), NULL_RTX,
10442 mode, mode);
10443 /* As a last resort, spill op0 to memory, and reload it in a
10444 different mode. */
10445 else if (!MEM_P (op0))
10447 /* If the operand is not a MEM, force it into memory. Since we
10448 are going to be changing the mode of the MEM, don't call
10449 force_const_mem for constants because we don't allow pool
10450 constants to change mode. */
10451 tree inner_type = TREE_TYPE (treeop0);
10453 gcc_assert (!TREE_ADDRESSABLE (exp));
10455 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10456 target
10457 = assign_stack_temp_for_type
10458 (TYPE_MODE (inner_type),
10459 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10461 emit_move_insn (target, op0);
10462 op0 = target;
10465 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10466 output type is such that the operand is known to be aligned, indicate
10467 that it is. Otherwise, we need only be concerned about alignment for
10468 non-BLKmode results. */
10469 if (MEM_P (op0))
10471 enum insn_code icode;
10473 if (TYPE_ALIGN_OK (type))
10475 /* ??? Copying the MEM without substantially changing it might
10476 run afoul of the code handling volatile memory references in
10477 store_expr, which assumes that TARGET is returned unmodified
10478 if it has been used. */
10479 op0 = copy_rtx (op0);
10480 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10482 else if (modifier != EXPAND_WRITE
10483 && modifier != EXPAND_MEMORY
10484 && !inner_reference_p
10485 && mode != BLKmode
10486 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10488 /* If the target does have special handling for unaligned
10489 loads of mode then use them. */
10490 if ((icode = optab_handler (movmisalign_optab, mode))
10491 != CODE_FOR_nothing)
10493 rtx reg, insn;
10495 op0 = adjust_address (op0, mode, 0);
10496 /* We've already validated the memory, and we're creating a
10497 new pseudo destination. The predicates really can't
10498 fail. */
10499 reg = gen_reg_rtx (mode);
10501 /* Nor can the insn generator. */
10502 insn = GEN_FCN (icode) (reg, op0);
10503 emit_insn (insn);
10504 return reg;
10506 else if (STRICT_ALIGNMENT)
10508 tree inner_type = TREE_TYPE (treeop0);
10509 HOST_WIDE_INT temp_size
10510 = MAX (int_size_in_bytes (inner_type),
10511 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10512 rtx new_rtx
10513 = assign_stack_temp_for_type (mode, temp_size, type);
10514 rtx new_with_op0_mode
10515 = adjust_address (new_rtx, GET_MODE (op0), 0);
10517 gcc_assert (!TREE_ADDRESSABLE (exp));
10519 if (GET_MODE (op0) == BLKmode)
10520 emit_block_move (new_with_op0_mode, op0,
10521 GEN_INT (GET_MODE_SIZE (mode)),
10522 (modifier == EXPAND_STACK_PARM
10523 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10524 else
10525 emit_move_insn (new_with_op0_mode, op0);
10527 op0 = new_rtx;
10531 op0 = adjust_address (op0, mode, 0);
10534 return op0;
10536 case MODIFY_EXPR:
10538 tree lhs = treeop0;
10539 tree rhs = treeop1;
10540 gcc_assert (ignore);
10542 /* Check for |= or &= of a bitfield of size one into another bitfield
10543 of size 1. In this case, (unless we need the result of the
10544 assignment) we can do this more efficiently with a
10545 test followed by an assignment, if necessary.
10547 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10548 things change so we do, this code should be enhanced to
10549 support it. */
10550 if (TREE_CODE (lhs) == COMPONENT_REF
10551 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10552 || TREE_CODE (rhs) == BIT_AND_EXPR)
10553 && TREE_OPERAND (rhs, 0) == lhs
10554 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10555 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10556 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10558 rtx label = gen_label_rtx ();
10559 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10560 do_jump (TREE_OPERAND (rhs, 1),
10561 value ? label : 0,
10562 value ? 0 : label, -1);
10563 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10564 false);
10565 do_pending_stack_adjust ();
10566 emit_label (label);
10567 return const0_rtx;
10570 expand_assignment (lhs, rhs, false);
10571 return const0_rtx;
10574 case ADDR_EXPR:
10575 return expand_expr_addr_expr (exp, target, tmode, modifier);
10577 case REALPART_EXPR:
10578 op0 = expand_normal (treeop0);
10579 return read_complex_part (op0, false);
10581 case IMAGPART_EXPR:
10582 op0 = expand_normal (treeop0);
10583 return read_complex_part (op0, true);
10585 case RETURN_EXPR:
10586 case LABEL_EXPR:
10587 case GOTO_EXPR:
10588 case SWITCH_EXPR:
10589 case ASM_EXPR:
10590 /* Expanded in cfgexpand.c. */
10591 gcc_unreachable ();
10593 case TRY_CATCH_EXPR:
10594 case CATCH_EXPR:
10595 case EH_FILTER_EXPR:
10596 case TRY_FINALLY_EXPR:
10597 /* Lowered by tree-eh.c. */
10598 gcc_unreachable ();
10600 case WITH_CLEANUP_EXPR:
10601 case CLEANUP_POINT_EXPR:
10602 case TARGET_EXPR:
10603 case CASE_LABEL_EXPR:
10604 case VA_ARG_EXPR:
10605 case BIND_EXPR:
10606 case INIT_EXPR:
10607 case CONJ_EXPR:
10608 case COMPOUND_EXPR:
10609 case PREINCREMENT_EXPR:
10610 case PREDECREMENT_EXPR:
10611 case POSTINCREMENT_EXPR:
10612 case POSTDECREMENT_EXPR:
10613 case LOOP_EXPR:
10614 case EXIT_EXPR:
10615 case COMPOUND_LITERAL_EXPR:
10616 /* Lowered by gimplify.c. */
10617 gcc_unreachable ();
10619 case FDESC_EXPR:
10620 /* Function descriptors are not valid except for as
10621 initialization constants, and should not be expanded. */
10622 gcc_unreachable ();
10624 case WITH_SIZE_EXPR:
10625 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10626 have pulled out the size to use in whatever context it needed. */
10627 return expand_expr_real (treeop0, original_target, tmode,
10628 modifier, alt_rtl, inner_reference_p);
10630 default:
10631 return expand_expr_real_2 (&ops, target, tmode, modifier);
10635 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10636 signedness of TYPE), possibly returning the result in TARGET. */
10637 static rtx
10638 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10640 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10641 if (target && GET_MODE (target) != GET_MODE (exp))
10642 target = 0;
10643 /* For constant values, reduce using build_int_cst_type. */
10644 if (CONST_INT_P (exp))
10646 HOST_WIDE_INT value = INTVAL (exp);
10647 tree t = build_int_cst_type (type, value);
10648 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10650 else if (TYPE_UNSIGNED (type))
10652 rtx mask = immed_double_int_const (double_int::mask (prec),
10653 GET_MODE (exp));
10654 return expand_and (GET_MODE (exp), exp, mask, target);
10656 else
10658 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10659 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10660 exp, count, target, 0);
10661 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10662 exp, count, target, 0);
10666 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10667 when applied to the address of EXP produces an address known to be
10668 aligned more than BIGGEST_ALIGNMENT. */
10670 static int
10671 is_aligning_offset (const_tree offset, const_tree exp)
10673 /* Strip off any conversions. */
10674 while (CONVERT_EXPR_P (offset))
10675 offset = TREE_OPERAND (offset, 0);
10677 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10678 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10679 if (TREE_CODE (offset) != BIT_AND_EXPR
10680 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10681 || compare_tree_int (TREE_OPERAND (offset, 1),
10682 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10683 || !exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10684 return 0;
10686 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10687 It must be NEGATE_EXPR. Then strip any more conversions. */
10688 offset = TREE_OPERAND (offset, 0);
10689 while (CONVERT_EXPR_P (offset))
10690 offset = TREE_OPERAND (offset, 0);
10692 if (TREE_CODE (offset) != NEGATE_EXPR)
10693 return 0;
10695 offset = TREE_OPERAND (offset, 0);
10696 while (CONVERT_EXPR_P (offset))
10697 offset = TREE_OPERAND (offset, 0);
10699 /* This must now be the address of EXP. */
10700 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10703 /* Return the tree node if an ARG corresponds to a string constant or zero
10704 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10705 in bytes within the string that ARG is accessing. The type of the
10706 offset will be `sizetype'. */
10708 tree
10709 string_constant (tree arg, tree *ptr_offset)
10711 tree array, offset, lower_bound;
10712 STRIP_NOPS (arg);
10714 if (TREE_CODE (arg) == ADDR_EXPR)
10716 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10718 *ptr_offset = size_zero_node;
10719 return TREE_OPERAND (arg, 0);
10721 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10723 array = TREE_OPERAND (arg, 0);
10724 offset = size_zero_node;
10726 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10728 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10729 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10730 if (TREE_CODE (array) != STRING_CST
10731 && TREE_CODE (array) != VAR_DECL)
10732 return 0;
10734 /* Check if the array has a nonzero lower bound. */
10735 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10736 if (!integer_zerop (lower_bound))
10738 /* If the offset and base aren't both constants, return 0. */
10739 if (TREE_CODE (lower_bound) != INTEGER_CST)
10740 return 0;
10741 if (TREE_CODE (offset) != INTEGER_CST)
10742 return 0;
10743 /* Adjust offset by the lower bound. */
10744 offset = size_diffop (fold_convert (sizetype, offset),
10745 fold_convert (sizetype, lower_bound));
10748 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10750 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10751 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10752 if (TREE_CODE (array) != ADDR_EXPR)
10753 return 0;
10754 array = TREE_OPERAND (array, 0);
10755 if (TREE_CODE (array) != STRING_CST
10756 && TREE_CODE (array) != VAR_DECL)
10757 return 0;
10759 else
10760 return 0;
10762 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10764 tree arg0 = TREE_OPERAND (arg, 0);
10765 tree arg1 = TREE_OPERAND (arg, 1);
10767 STRIP_NOPS (arg0);
10768 STRIP_NOPS (arg1);
10770 if (TREE_CODE (arg0) == ADDR_EXPR
10771 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10772 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10774 array = TREE_OPERAND (arg0, 0);
10775 offset = arg1;
10777 else if (TREE_CODE (arg1) == ADDR_EXPR
10778 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10779 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10781 array = TREE_OPERAND (arg1, 0);
10782 offset = arg0;
10784 else
10785 return 0;
10787 else
10788 return 0;
10790 if (TREE_CODE (array) == STRING_CST)
10792 *ptr_offset = fold_convert (sizetype, offset);
10793 return array;
10795 else if (TREE_CODE (array) == VAR_DECL
10796 || TREE_CODE (array) == CONST_DECL)
10798 int length;
10799 tree init = ctor_for_folding (array);
10801 /* Variables initialized to string literals can be handled too. */
10802 if (init == error_mark_node
10803 || !init
10804 || TREE_CODE (init) != STRING_CST)
10805 return 0;
10807 /* Avoid const char foo[4] = "abcde"; */
10808 if (DECL_SIZE_UNIT (array) == NULL_TREE
10809 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10810 || (length = TREE_STRING_LENGTH (init)) <= 0
10811 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10812 return 0;
10814 /* If variable is bigger than the string literal, OFFSET must be constant
10815 and inside of the bounds of the string literal. */
10816 offset = fold_convert (sizetype, offset);
10817 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10818 && (! tree_fits_uhwi_p (offset)
10819 || compare_tree_int (offset, length) >= 0))
10820 return 0;
10822 *ptr_offset = offset;
10823 return init;
10826 return 0;
10829 /* Generate code to calculate OPS, and exploded expression
10830 using a store-flag instruction and return an rtx for the result.
10831 OPS reflects a comparison.
10833 If TARGET is nonzero, store the result there if convenient.
10835 Return zero if there is no suitable set-flag instruction
10836 available on this machine.
10838 Once expand_expr has been called on the arguments of the comparison,
10839 we are committed to doing the store flag, since it is not safe to
10840 re-evaluate the expression. We emit the store-flag insn by calling
10841 emit_store_flag, but only expand the arguments if we have a reason
10842 to believe that emit_store_flag will be successful. If we think that
10843 it will, but it isn't, we have to simulate the store-flag with a
10844 set/jump/set sequence. */
10846 static rtx
10847 do_store_flag (sepops ops, rtx target, enum machine_mode mode)
10849 enum rtx_code code;
10850 tree arg0, arg1, type;
10851 tree tem;
10852 enum machine_mode operand_mode;
10853 int unsignedp;
10854 rtx op0, op1;
10855 rtx subtarget = target;
10856 location_t loc = ops->location;
10858 arg0 = ops->op0;
10859 arg1 = ops->op1;
10861 /* Don't crash if the comparison was erroneous. */
10862 if (arg0 == error_mark_node || arg1 == error_mark_node)
10863 return const0_rtx;
10865 type = TREE_TYPE (arg0);
10866 operand_mode = TYPE_MODE (type);
10867 unsignedp = TYPE_UNSIGNED (type);
10869 /* We won't bother with BLKmode store-flag operations because it would mean
10870 passing a lot of information to emit_store_flag. */
10871 if (operand_mode == BLKmode)
10872 return 0;
10874 /* We won't bother with store-flag operations involving function pointers
10875 when function pointers must be canonicalized before comparisons. */
10876 #ifdef HAVE_canonicalize_funcptr_for_compare
10877 if (HAVE_canonicalize_funcptr_for_compare
10878 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10879 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10880 == FUNCTION_TYPE))
10881 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10882 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10883 == FUNCTION_TYPE))))
10884 return 0;
10885 #endif
10887 STRIP_NOPS (arg0);
10888 STRIP_NOPS (arg1);
10890 /* For vector typed comparisons emit code to generate the desired
10891 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10892 expander for this. */
10893 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10895 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10896 tree if_true = constant_boolean_node (true, ops->type);
10897 tree if_false = constant_boolean_node (false, ops->type);
10898 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10901 /* Get the rtx comparison code to use. We know that EXP is a comparison
10902 operation of some type. Some comparisons against 1 and -1 can be
10903 converted to comparisons with zero. Do so here so that the tests
10904 below will be aware that we have a comparison with zero. These
10905 tests will not catch constants in the first operand, but constants
10906 are rarely passed as the first operand. */
10908 switch (ops->code)
10910 case EQ_EXPR:
10911 code = EQ;
10912 break;
10913 case NE_EXPR:
10914 code = NE;
10915 break;
10916 case LT_EXPR:
10917 if (integer_onep (arg1))
10918 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10919 else
10920 code = unsignedp ? LTU : LT;
10921 break;
10922 case LE_EXPR:
10923 if (! unsignedp && integer_all_onesp (arg1))
10924 arg1 = integer_zero_node, code = LT;
10925 else
10926 code = unsignedp ? LEU : LE;
10927 break;
10928 case GT_EXPR:
10929 if (! unsignedp && integer_all_onesp (arg1))
10930 arg1 = integer_zero_node, code = GE;
10931 else
10932 code = unsignedp ? GTU : GT;
10933 break;
10934 case GE_EXPR:
10935 if (integer_onep (arg1))
10936 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
10937 else
10938 code = unsignedp ? GEU : GE;
10939 break;
10941 case UNORDERED_EXPR:
10942 code = UNORDERED;
10943 break;
10944 case ORDERED_EXPR:
10945 code = ORDERED;
10946 break;
10947 case UNLT_EXPR:
10948 code = UNLT;
10949 break;
10950 case UNLE_EXPR:
10951 code = UNLE;
10952 break;
10953 case UNGT_EXPR:
10954 code = UNGT;
10955 break;
10956 case UNGE_EXPR:
10957 code = UNGE;
10958 break;
10959 case UNEQ_EXPR:
10960 code = UNEQ;
10961 break;
10962 case LTGT_EXPR:
10963 code = LTGT;
10964 break;
10966 default:
10967 gcc_unreachable ();
10970 /* Put a constant second. */
10971 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
10972 || TREE_CODE (arg0) == FIXED_CST)
10974 tem = arg0; arg0 = arg1; arg1 = tem;
10975 code = swap_condition (code);
10978 /* If this is an equality or inequality test of a single bit, we can
10979 do this by shifting the bit being tested to the low-order bit and
10980 masking the result with the constant 1. If the condition was EQ,
10981 we xor it with 1. This does not require an scc insn and is faster
10982 than an scc insn even if we have it.
10984 The code to make this transformation was moved into fold_single_bit_test,
10985 so we just call into the folder and expand its result. */
10987 if ((code == NE || code == EQ)
10988 && integer_zerop (arg1)
10989 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
10991 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
10992 if (srcstmt
10993 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
10995 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
10996 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10997 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
10998 gimple_assign_rhs1 (srcstmt),
10999 gimple_assign_rhs2 (srcstmt));
11000 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11001 if (temp)
11002 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11006 if (! get_subtarget (target)
11007 || GET_MODE (subtarget) != operand_mode)
11008 subtarget = 0;
11010 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11012 if (target == 0)
11013 target = gen_reg_rtx (mode);
11015 /* Try a cstore if possible. */
11016 return emit_store_flag_force (target, code, op0, op1,
11017 operand_mode, unsignedp,
11018 (TYPE_PRECISION (ops->type) == 1
11019 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11023 /* Stubs in case we haven't got a casesi insn. */
11024 #ifndef HAVE_casesi
11025 # define HAVE_casesi 0
11026 # define gen_casesi(a, b, c, d, e) (0)
11027 # define CODE_FOR_casesi CODE_FOR_nothing
11028 #endif
11030 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11031 0 otherwise (i.e. if there is no casesi instruction).
11033 DEFAULT_PROBABILITY is the probability of jumping to the default
11034 label. */
11036 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11037 rtx table_label, rtx default_label, rtx fallback_label,
11038 int default_probability)
11040 struct expand_operand ops[5];
11041 enum machine_mode index_mode = SImode;
11042 rtx op1, op2, index;
11044 if (! HAVE_casesi)
11045 return 0;
11047 /* Convert the index to SImode. */
11048 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11050 enum machine_mode omode = TYPE_MODE (index_type);
11051 rtx rangertx = expand_normal (range);
11053 /* We must handle the endpoints in the original mode. */
11054 index_expr = build2 (MINUS_EXPR, index_type,
11055 index_expr, minval);
11056 minval = integer_zero_node;
11057 index = expand_normal (index_expr);
11058 if (default_label)
11059 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11060 omode, 1, default_label,
11061 default_probability);
11062 /* Now we can safely truncate. */
11063 index = convert_to_mode (index_mode, index, 0);
11065 else
11067 if (TYPE_MODE (index_type) != index_mode)
11069 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11070 index_expr = fold_convert (index_type, index_expr);
11073 index = expand_normal (index_expr);
11076 do_pending_stack_adjust ();
11078 op1 = expand_normal (minval);
11079 op2 = expand_normal (range);
11081 create_input_operand (&ops[0], index, index_mode);
11082 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11083 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11084 create_fixed_operand (&ops[3], table_label);
11085 create_fixed_operand (&ops[4], (default_label
11086 ? default_label
11087 : fallback_label));
11088 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11089 return 1;
11092 /* Attempt to generate a tablejump instruction; same concept. */
11093 #ifndef HAVE_tablejump
11094 #define HAVE_tablejump 0
11095 #define gen_tablejump(x, y) (0)
11096 #endif
11098 /* Subroutine of the next function.
11100 INDEX is the value being switched on, with the lowest value
11101 in the table already subtracted.
11102 MODE is its expected mode (needed if INDEX is constant).
11103 RANGE is the length of the jump table.
11104 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11106 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11107 index value is out of range.
11108 DEFAULT_PROBABILITY is the probability of jumping to
11109 the default label. */
11111 static void
11112 do_tablejump (rtx index, enum machine_mode mode, rtx range, rtx table_label,
11113 rtx default_label, int default_probability)
11115 rtx temp, vector;
11117 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11118 cfun->cfg->max_jumptable_ents = INTVAL (range);
11120 /* Do an unsigned comparison (in the proper mode) between the index
11121 expression and the value which represents the length of the range.
11122 Since we just finished subtracting the lower bound of the range
11123 from the index expression, this comparison allows us to simultaneously
11124 check that the original index expression value is both greater than
11125 or equal to the minimum value of the range and less than or equal to
11126 the maximum value of the range. */
11128 if (default_label)
11129 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11130 default_label, default_probability);
11133 /* If index is in range, it must fit in Pmode.
11134 Convert to Pmode so we can index with it. */
11135 if (mode != Pmode)
11136 index = convert_to_mode (Pmode, index, 1);
11138 /* Don't let a MEM slip through, because then INDEX that comes
11139 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11140 and break_out_memory_refs will go to work on it and mess it up. */
11141 #ifdef PIC_CASE_VECTOR_ADDRESS
11142 if (flag_pic && !REG_P (index))
11143 index = copy_to_mode_reg (Pmode, index);
11144 #endif
11146 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11147 GET_MODE_SIZE, because this indicates how large insns are. The other
11148 uses should all be Pmode, because they are addresses. This code
11149 could fail if addresses and insns are not the same size. */
11150 index = simplify_gen_binary (MULT, Pmode, index,
11151 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11152 Pmode));
11153 index = simplify_gen_binary (PLUS, Pmode, index,
11154 gen_rtx_LABEL_REF (Pmode, table_label));
11156 #ifdef PIC_CASE_VECTOR_ADDRESS
11157 if (flag_pic)
11158 index = PIC_CASE_VECTOR_ADDRESS (index);
11159 else
11160 #endif
11161 index = memory_address (CASE_VECTOR_MODE, index);
11162 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11163 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11164 convert_move (temp, vector, 0);
11166 emit_jump_insn (gen_tablejump (temp, table_label));
11168 /* If we are generating PIC code or if the table is PC-relative, the
11169 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11170 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11171 emit_barrier ();
11175 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11176 rtx table_label, rtx default_label, int default_probability)
11178 rtx index;
11180 if (! HAVE_tablejump)
11181 return 0;
11183 index_expr = fold_build2 (MINUS_EXPR, index_type,
11184 fold_convert (index_type, index_expr),
11185 fold_convert (index_type, minval));
11186 index = expand_normal (index_expr);
11187 do_pending_stack_adjust ();
11189 do_tablejump (index, TYPE_MODE (index_type),
11190 convert_modes (TYPE_MODE (index_type),
11191 TYPE_MODE (TREE_TYPE (range)),
11192 expand_normal (range),
11193 TYPE_UNSIGNED (TREE_TYPE (range))),
11194 table_label, default_label, default_probability);
11195 return 1;
11198 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11199 static rtx
11200 const_vector_from_tree (tree exp)
11202 rtvec v;
11203 unsigned i;
11204 int units;
11205 tree elt;
11206 enum machine_mode inner, mode;
11208 mode = TYPE_MODE (TREE_TYPE (exp));
11210 if (initializer_zerop (exp))
11211 return CONST0_RTX (mode);
11213 units = GET_MODE_NUNITS (mode);
11214 inner = GET_MODE_INNER (mode);
11216 v = rtvec_alloc (units);
11218 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11220 elt = VECTOR_CST_ELT (exp, i);
11222 if (TREE_CODE (elt) == REAL_CST)
11223 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11224 inner);
11225 else if (TREE_CODE (elt) == FIXED_CST)
11226 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11227 inner);
11228 else
11229 RTVEC_ELT (v, i) = immed_double_int_const (tree_to_double_int (elt),
11230 inner);
11233 return gen_rtx_CONST_VECTOR (mode, v);
11236 /* Build a decl for a personality function given a language prefix. */
11238 tree
11239 build_personality_function (const char *lang)
11241 const char *unwind_and_version;
11242 tree decl, type;
11243 char *name;
11245 switch (targetm_common.except_unwind_info (&global_options))
11247 case UI_NONE:
11248 return NULL;
11249 case UI_SJLJ:
11250 unwind_and_version = "_sj0";
11251 break;
11252 case UI_DWARF2:
11253 case UI_TARGET:
11254 unwind_and_version = "_v0";
11255 break;
11256 case UI_SEH:
11257 unwind_and_version = "_seh0";
11258 break;
11259 default:
11260 gcc_unreachable ();
11263 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11265 type = build_function_type_list (integer_type_node, integer_type_node,
11266 long_long_unsigned_type_node,
11267 ptr_type_node, ptr_type_node, NULL_TREE);
11268 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11269 get_identifier (name), type);
11270 DECL_ARTIFICIAL (decl) = 1;
11271 DECL_EXTERNAL (decl) = 1;
11272 TREE_PUBLIC (decl) = 1;
11274 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11275 are the flags assigned by targetm.encode_section_info. */
11276 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11278 return decl;
11281 /* Extracts the personality function of DECL and returns the corresponding
11282 libfunc. */
11285 get_personality_function (tree decl)
11287 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11288 enum eh_personality_kind pk;
11290 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11291 if (pk == eh_personality_none)
11292 return NULL;
11294 if (!personality
11295 && pk == eh_personality_any)
11296 personality = lang_hooks.eh_personality ();
11298 if (pk == eh_personality_lang)
11299 gcc_assert (personality != NULL_TREE);
11301 return XEXP (DECL_RTL (personality), 0);
11304 #include "gt-expr.h"