Allow indirect branch via GOT slot for x32
[official-gcc.git] / gcc / expr.c
blob8d75ef55f23e6e379172f5cb72f8ceacad6e4aac
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "hash-set.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stringpool.h"
37 #include "stor-layout.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "flags.h"
41 #include "regs.h"
42 #include "hard-reg-set.h"
43 #include "except.h"
44 #include "function.h"
45 #include "insn-config.h"
46 #include "insn-attr.h"
47 #include "hashtab.h"
48 #include "statistics.h"
49 #include "real.h"
50 #include "fixed-value.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "stmt.h"
57 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
58 #include "expr.h"
59 #include "insn-codes.h"
60 #include "optabs.h"
61 #include "libfuncs.h"
62 #include "recog.h"
63 #include "reload.h"
64 #include "typeclass.h"
65 #include "toplev.h"
66 #include "langhooks.h"
67 #include "intl.h"
68 #include "tm_p.h"
69 #include "tree-iterator.h"
70 #include "predict.h"
71 #include "dominance.h"
72 #include "cfg.h"
73 #include "basic-block.h"
74 #include "tree-ssa-alias.h"
75 #include "internal-fn.h"
76 #include "gimple-expr.h"
77 #include "is-a.h"
78 #include "gimple.h"
79 #include "gimple-ssa.h"
80 #include "hash-map.h"
81 #include "plugin-api.h"
82 #include "ipa-ref.h"
83 #include "cgraph.h"
84 #include "tree-ssanames.h"
85 #include "target.h"
86 #include "common/common-target.h"
87 #include "timevar.h"
88 #include "df.h"
89 #include "diagnostic.h"
90 #include "tree-ssa-live.h"
91 #include "tree-outof-ssa.h"
92 #include "target-globals.h"
93 #include "params.h"
94 #include "tree-ssa-address.h"
95 #include "cfgexpand.h"
96 #include "builtins.h"
97 #include "tree-chkp.h"
98 #include "rtl-chkp.h"
99 #include "ccmp.h"
101 #ifndef STACK_PUSH_CODE
102 #ifdef STACK_GROWS_DOWNWARD
103 #define STACK_PUSH_CODE PRE_DEC
104 #else
105 #define STACK_PUSH_CODE PRE_INC
106 #endif
107 #endif
110 /* If this is nonzero, we do not bother generating VOLATILE
111 around volatile memory references, and we are willing to
112 output indirect addresses. If cse is to follow, we reject
113 indirect addresses so a useful potential cse is generated;
114 if it is used only once, instruction combination will produce
115 the same indirect address eventually. */
116 int cse_not_expected;
118 /* This structure is used by move_by_pieces to describe the move to
119 be performed. */
120 struct move_by_pieces_d
122 rtx to;
123 rtx to_addr;
124 int autinc_to;
125 int explicit_inc_to;
126 rtx from;
127 rtx from_addr;
128 int autinc_from;
129 int explicit_inc_from;
130 unsigned HOST_WIDE_INT len;
131 HOST_WIDE_INT offset;
132 int reverse;
135 /* This structure is used by store_by_pieces to describe the clear to
136 be performed. */
138 struct store_by_pieces_d
140 rtx to;
141 rtx to_addr;
142 int autinc_to;
143 int explicit_inc_to;
144 unsigned HOST_WIDE_INT len;
145 HOST_WIDE_INT offset;
146 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
147 void *constfundata;
148 int reverse;
151 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
152 struct move_by_pieces_d *);
153 static bool block_move_libcall_safe_for_call_parm (void);
154 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
155 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
156 unsigned HOST_WIDE_INT);
157 static tree emit_block_move_libcall_fn (int);
158 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
159 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
160 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
161 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
162 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
163 struct store_by_pieces_d *);
164 static tree clear_storage_libcall_fn (int);
165 static rtx_insn *compress_float_constant (rtx, rtx);
166 static rtx get_subtarget (rtx);
167 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
168 HOST_WIDE_INT, machine_mode,
169 tree, int, alias_set_type);
170 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
171 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
172 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
173 machine_mode, tree, alias_set_type, bool);
175 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
177 static int is_aligning_offset (const_tree, const_tree);
178 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
179 static rtx do_store_flag (sepops, rtx, machine_mode);
180 #ifdef PUSH_ROUNDING
181 static void emit_single_push_insn (machine_mode, rtx, tree);
182 #endif
183 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
184 static rtx const_vector_from_tree (tree);
185 static tree tree_expr_size (const_tree);
186 static HOST_WIDE_INT int_expr_size (tree);
189 /* This is run to set up which modes can be used
190 directly in memory and to initialize the block move optab. It is run
191 at the beginning of compilation and when the target is reinitialized. */
193 void
194 init_expr_target (void)
196 rtx insn, pat;
197 machine_mode mode;
198 int num_clobbers;
199 rtx mem, mem1;
200 rtx reg;
202 /* Try indexing by frame ptr and try by stack ptr.
203 It is known that on the Convex the stack ptr isn't a valid index.
204 With luck, one or the other is valid on any machine. */
205 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
206 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
208 /* A scratch register we can modify in-place below to avoid
209 useless RTL allocations. */
210 reg = gen_rtx_REG (word_mode, FIRST_PSEUDO_REGISTER);
212 insn = rtx_alloc (INSN);
213 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
214 PATTERN (insn) = pat;
216 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
217 mode = (machine_mode) ((int) mode + 1))
219 int regno;
221 direct_load[(int) mode] = direct_store[(int) mode] = 0;
222 PUT_MODE (mem, mode);
223 PUT_MODE (mem1, mode);
225 /* See if there is some register that can be used in this mode and
226 directly loaded or stored from memory. */
228 if (mode != VOIDmode && mode != BLKmode)
229 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
230 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
231 regno++)
233 if (! HARD_REGNO_MODE_OK (regno, mode))
234 continue;
236 set_mode_and_regno (reg, mode, regno);
238 SET_SRC (pat) = mem;
239 SET_DEST (pat) = reg;
240 if (recog (pat, insn, &num_clobbers) >= 0)
241 direct_load[(int) mode] = 1;
243 SET_SRC (pat) = mem1;
244 SET_DEST (pat) = reg;
245 if (recog (pat, insn, &num_clobbers) >= 0)
246 direct_load[(int) mode] = 1;
248 SET_SRC (pat) = reg;
249 SET_DEST (pat) = mem;
250 if (recog (pat, insn, &num_clobbers) >= 0)
251 direct_store[(int) mode] = 1;
253 SET_SRC (pat) = reg;
254 SET_DEST (pat) = mem1;
255 if (recog (pat, insn, &num_clobbers) >= 0)
256 direct_store[(int) mode] = 1;
260 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER));
262 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
263 mode = GET_MODE_WIDER_MODE (mode))
265 machine_mode srcmode;
266 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
267 srcmode = GET_MODE_WIDER_MODE (srcmode))
269 enum insn_code ic;
271 ic = can_extend_p (mode, srcmode, 0);
272 if (ic == CODE_FOR_nothing)
273 continue;
275 PUT_MODE (mem, srcmode);
277 if (insn_operand_matches (ic, 1, mem))
278 float_extend_from_mem[mode][srcmode] = true;
283 /* This is run at the start of compiling a function. */
285 void
286 init_expr (void)
288 memset (&crtl->expr, 0, sizeof (crtl->expr));
291 /* Copy data from FROM to TO, where the machine modes are not the same.
292 Both modes may be integer, or both may be floating, or both may be
293 fixed-point.
294 UNSIGNEDP should be nonzero if FROM is an unsigned type.
295 This causes zero-extension instead of sign-extension. */
297 void
298 convert_move (rtx to, rtx from, int unsignedp)
300 machine_mode to_mode = GET_MODE (to);
301 machine_mode from_mode = GET_MODE (from);
302 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
303 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
304 enum insn_code code;
305 rtx libcall;
307 /* rtx code for making an equivalent value. */
308 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
309 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
312 gcc_assert (to_real == from_real);
313 gcc_assert (to_mode != BLKmode);
314 gcc_assert (from_mode != BLKmode);
316 /* If the source and destination are already the same, then there's
317 nothing to do. */
318 if (to == from)
319 return;
321 /* If FROM is a SUBREG that indicates that we have already done at least
322 the required extension, strip it. We don't handle such SUBREGs as
323 TO here. */
325 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
326 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
327 >= GET_MODE_PRECISION (to_mode))
328 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
329 from = gen_lowpart (to_mode, from), from_mode = to_mode;
331 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
333 if (to_mode == from_mode
334 || (from_mode == VOIDmode && CONSTANT_P (from)))
336 emit_move_insn (to, from);
337 return;
340 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
342 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
344 if (VECTOR_MODE_P (to_mode))
345 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
346 else
347 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
349 emit_move_insn (to, from);
350 return;
353 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
355 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
356 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
357 return;
360 if (to_real)
362 rtx value;
363 rtx_insn *insns;
364 convert_optab tab;
366 gcc_assert ((GET_MODE_PRECISION (from_mode)
367 != GET_MODE_PRECISION (to_mode))
368 || (DECIMAL_FLOAT_MODE_P (from_mode)
369 != DECIMAL_FLOAT_MODE_P (to_mode)));
371 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
372 /* Conversion between decimal float and binary float, same size. */
373 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
374 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
375 tab = sext_optab;
376 else
377 tab = trunc_optab;
379 /* Try converting directly if the insn is supported. */
381 code = convert_optab_handler (tab, to_mode, from_mode);
382 if (code != CODE_FOR_nothing)
384 emit_unop_insn (code, to, from,
385 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
386 return;
389 /* Otherwise use a libcall. */
390 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
392 /* Is this conversion implemented yet? */
393 gcc_assert (libcall);
395 start_sequence ();
396 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
397 1, from, from_mode);
398 insns = get_insns ();
399 end_sequence ();
400 emit_libcall_block (insns, to, value,
401 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
402 from)
403 : gen_rtx_FLOAT_EXTEND (to_mode, from));
404 return;
407 /* Handle pointer conversion. */ /* SPEE 900220. */
408 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
410 convert_optab ctab;
412 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
413 ctab = trunc_optab;
414 else if (unsignedp)
415 ctab = zext_optab;
416 else
417 ctab = sext_optab;
419 if (convert_optab_handler (ctab, to_mode, from_mode)
420 != CODE_FOR_nothing)
422 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
423 to, from, UNKNOWN);
424 return;
428 /* Targets are expected to provide conversion insns between PxImode and
429 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
430 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
432 machine_mode full_mode
433 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
435 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
436 != CODE_FOR_nothing);
438 if (full_mode != from_mode)
439 from = convert_to_mode (full_mode, from, unsignedp);
440 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
441 to, from, UNKNOWN);
442 return;
444 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
446 rtx new_from;
447 machine_mode full_mode
448 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
449 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
450 enum insn_code icode;
452 icode = convert_optab_handler (ctab, full_mode, from_mode);
453 gcc_assert (icode != CODE_FOR_nothing);
455 if (to_mode == full_mode)
457 emit_unop_insn (icode, to, from, UNKNOWN);
458 return;
461 new_from = gen_reg_rtx (full_mode);
462 emit_unop_insn (icode, new_from, from, UNKNOWN);
464 /* else proceed to integer conversions below. */
465 from_mode = full_mode;
466 from = new_from;
469 /* Make sure both are fixed-point modes or both are not. */
470 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
471 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
472 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
474 /* If we widen from_mode to to_mode and they are in the same class,
475 we won't saturate the result.
476 Otherwise, always saturate the result to play safe. */
477 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
478 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
479 expand_fixed_convert (to, from, 0, 0);
480 else
481 expand_fixed_convert (to, from, 0, 1);
482 return;
485 /* Now both modes are integers. */
487 /* Handle expanding beyond a word. */
488 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
489 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
491 rtx_insn *insns;
492 rtx lowpart;
493 rtx fill_value;
494 rtx lowfrom;
495 int i;
496 machine_mode lowpart_mode;
497 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
499 /* Try converting directly if the insn is supported. */
500 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
501 != CODE_FOR_nothing)
503 /* If FROM is a SUBREG, put it into a register. Do this
504 so that we always generate the same set of insns for
505 better cse'ing; if an intermediate assignment occurred,
506 we won't be doing the operation directly on the SUBREG. */
507 if (optimize > 0 && GET_CODE (from) == SUBREG)
508 from = force_reg (from_mode, from);
509 emit_unop_insn (code, to, from, equiv_code);
510 return;
512 /* Next, try converting via full word. */
513 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
514 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
515 != CODE_FOR_nothing))
517 rtx word_to = gen_reg_rtx (word_mode);
518 if (REG_P (to))
520 if (reg_overlap_mentioned_p (to, from))
521 from = force_reg (from_mode, from);
522 emit_clobber (to);
524 convert_move (word_to, from, unsignedp);
525 emit_unop_insn (code, to, word_to, equiv_code);
526 return;
529 /* No special multiword conversion insn; do it by hand. */
530 start_sequence ();
532 /* Since we will turn this into a no conflict block, we must ensure the
533 the source does not overlap the target so force it into an isolated
534 register when maybe so. Likewise for any MEM input, since the
535 conversion sequence might require several references to it and we
536 must ensure we're getting the same value every time. */
538 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
539 from = force_reg (from_mode, from);
541 /* Get a copy of FROM widened to a word, if necessary. */
542 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
543 lowpart_mode = word_mode;
544 else
545 lowpart_mode = from_mode;
547 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
549 lowpart = gen_lowpart (lowpart_mode, to);
550 emit_move_insn (lowpart, lowfrom);
552 /* Compute the value to put in each remaining word. */
553 if (unsignedp)
554 fill_value = const0_rtx;
555 else
556 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
557 LT, lowfrom, const0_rtx,
558 lowpart_mode, 0, -1);
560 /* Fill the remaining words. */
561 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
563 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
564 rtx subword = operand_subword (to, index, 1, to_mode);
566 gcc_assert (subword);
568 if (fill_value != subword)
569 emit_move_insn (subword, fill_value);
572 insns = get_insns ();
573 end_sequence ();
575 emit_insn (insns);
576 return;
579 /* Truncating multi-word to a word or less. */
580 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
581 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
583 if (!((MEM_P (from)
584 && ! MEM_VOLATILE_P (from)
585 && direct_load[(int) to_mode]
586 && ! mode_dependent_address_p (XEXP (from, 0),
587 MEM_ADDR_SPACE (from)))
588 || REG_P (from)
589 || GET_CODE (from) == SUBREG))
590 from = force_reg (from_mode, from);
591 convert_move (to, gen_lowpart (word_mode, from), 0);
592 return;
595 /* Now follow all the conversions between integers
596 no more than a word long. */
598 /* For truncation, usually we can just refer to FROM in a narrower mode. */
599 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
600 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
602 if (!((MEM_P (from)
603 && ! MEM_VOLATILE_P (from)
604 && direct_load[(int) to_mode]
605 && ! mode_dependent_address_p (XEXP (from, 0),
606 MEM_ADDR_SPACE (from)))
607 || REG_P (from)
608 || GET_CODE (from) == SUBREG))
609 from = force_reg (from_mode, from);
610 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
611 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
612 from = copy_to_reg (from);
613 emit_move_insn (to, gen_lowpart (to_mode, from));
614 return;
617 /* Handle extension. */
618 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
620 /* Convert directly if that works. */
621 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
622 != CODE_FOR_nothing)
624 emit_unop_insn (code, to, from, equiv_code);
625 return;
627 else
629 machine_mode intermediate;
630 rtx tmp;
631 int shift_amount;
633 /* Search for a mode to convert via. */
634 for (intermediate = from_mode; intermediate != VOIDmode;
635 intermediate = GET_MODE_WIDER_MODE (intermediate))
636 if (((can_extend_p (to_mode, intermediate, unsignedp)
637 != CODE_FOR_nothing)
638 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
639 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
640 && (can_extend_p (intermediate, from_mode, unsignedp)
641 != CODE_FOR_nothing))
643 convert_move (to, convert_to_mode (intermediate, from,
644 unsignedp), unsignedp);
645 return;
648 /* No suitable intermediate mode.
649 Generate what we need with shifts. */
650 shift_amount = (GET_MODE_PRECISION (to_mode)
651 - GET_MODE_PRECISION (from_mode));
652 from = gen_lowpart (to_mode, force_reg (from_mode, from));
653 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
654 to, unsignedp);
655 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
656 to, unsignedp);
657 if (tmp != to)
658 emit_move_insn (to, tmp);
659 return;
663 /* Support special truncate insns for certain modes. */
664 if (convert_optab_handler (trunc_optab, to_mode,
665 from_mode) != CODE_FOR_nothing)
667 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
668 to, from, UNKNOWN);
669 return;
672 /* Handle truncation of volatile memrefs, and so on;
673 the things that couldn't be truncated directly,
674 and for which there was no special instruction.
676 ??? Code above formerly short-circuited this, for most integer
677 mode pairs, with a force_reg in from_mode followed by a recursive
678 call to this routine. Appears always to have been wrong. */
679 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
681 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
682 emit_move_insn (to, temp);
683 return;
686 /* Mode combination is not recognized. */
687 gcc_unreachable ();
690 /* Return an rtx for a value that would result
691 from converting X to mode MODE.
692 Both X and MODE may be floating, or both integer.
693 UNSIGNEDP is nonzero if X is an unsigned value.
694 This can be done by referring to a part of X in place
695 or by copying to a new temporary with conversion. */
698 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
700 return convert_modes (mode, VOIDmode, x, unsignedp);
703 /* Return an rtx for a value that would result
704 from converting X from mode OLDMODE to mode MODE.
705 Both modes may be floating, or both integer.
706 UNSIGNEDP is nonzero if X is an unsigned value.
708 This can be done by referring to a part of X in place
709 or by copying to a new temporary with conversion.
711 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
714 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
716 rtx temp;
718 /* If FROM is a SUBREG that indicates that we have already done at least
719 the required extension, strip it. */
721 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
722 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
723 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
724 x = gen_lowpart (mode, SUBREG_REG (x));
726 if (GET_MODE (x) != VOIDmode)
727 oldmode = GET_MODE (x);
729 if (mode == oldmode)
730 return x;
732 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
734 /* If the caller did not tell us the old mode, then there is not
735 much to do with respect to canonicalization. We have to
736 assume that all the bits are significant. */
737 if (GET_MODE_CLASS (oldmode) != MODE_INT)
738 oldmode = MAX_MODE_INT;
739 wide_int w = wide_int::from (std::make_pair (x, oldmode),
740 GET_MODE_PRECISION (mode),
741 unsignedp ? UNSIGNED : SIGNED);
742 return immed_wide_int_const (w, mode);
745 /* We can do this with a gen_lowpart if both desired and current modes
746 are integer, and this is either a constant integer, a register, or a
747 non-volatile MEM. */
748 if (GET_MODE_CLASS (mode) == MODE_INT
749 && GET_MODE_CLASS (oldmode) == MODE_INT
750 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
751 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
752 || (REG_P (x)
753 && (!HARD_REGISTER_P (x)
754 || HARD_REGNO_MODE_OK (REGNO (x), mode))
755 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
757 return gen_lowpart (mode, x);
759 /* Converting from integer constant into mode is always equivalent to an
760 subreg operation. */
761 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
763 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
764 return simplify_gen_subreg (mode, x, oldmode, 0);
767 temp = gen_reg_rtx (mode);
768 convert_move (temp, x, unsignedp);
769 return temp;
772 /* Return the largest alignment we can use for doing a move (or store)
773 of MAX_PIECES. ALIGN is the largest alignment we could use. */
775 static unsigned int
776 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
778 machine_mode tmode;
780 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
781 if (align >= GET_MODE_ALIGNMENT (tmode))
782 align = GET_MODE_ALIGNMENT (tmode);
783 else
785 machine_mode tmode, xmode;
787 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
788 tmode != VOIDmode;
789 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
790 if (GET_MODE_SIZE (tmode) > max_pieces
791 || SLOW_UNALIGNED_ACCESS (tmode, align))
792 break;
794 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
797 return align;
800 /* Return the widest integer mode no wider than SIZE. If no such mode
801 can be found, return VOIDmode. */
803 static machine_mode
804 widest_int_mode_for_size (unsigned int size)
806 machine_mode tmode, mode = VOIDmode;
808 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
809 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
810 if (GET_MODE_SIZE (tmode) < size)
811 mode = tmode;
813 return mode;
816 /* Determine whether the LEN bytes can be moved by using several move
817 instructions. Return nonzero if a call to move_by_pieces should
818 succeed. */
821 can_move_by_pieces (unsigned HOST_WIDE_INT len,
822 unsigned int align)
824 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
825 optimize_insn_for_speed_p ());
828 /* Generate several move instructions to copy LEN bytes from block FROM to
829 block TO. (These are MEM rtx's with BLKmode).
831 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
832 used to push FROM to the stack.
834 ALIGN is maximum stack alignment we can assume.
836 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
837 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
838 stpcpy. */
841 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
842 unsigned int align, int endp)
844 struct move_by_pieces_d data;
845 machine_mode to_addr_mode;
846 machine_mode from_addr_mode = get_address_mode (from);
847 rtx to_addr, from_addr = XEXP (from, 0);
848 unsigned int max_size = MOVE_MAX_PIECES + 1;
849 enum insn_code icode;
851 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
853 data.offset = 0;
854 data.from_addr = from_addr;
855 if (to)
857 to_addr_mode = get_address_mode (to);
858 to_addr = XEXP (to, 0);
859 data.to = to;
860 data.autinc_to
861 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
862 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
863 data.reverse
864 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
866 else
868 to_addr_mode = VOIDmode;
869 to_addr = NULL_RTX;
870 data.to = NULL_RTX;
871 data.autinc_to = 1;
872 #ifdef STACK_GROWS_DOWNWARD
873 data.reverse = 1;
874 #else
875 data.reverse = 0;
876 #endif
878 data.to_addr = to_addr;
879 data.from = from;
880 data.autinc_from
881 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
882 || GET_CODE (from_addr) == POST_INC
883 || GET_CODE (from_addr) == POST_DEC);
885 data.explicit_inc_from = 0;
886 data.explicit_inc_to = 0;
887 if (data.reverse) data.offset = len;
888 data.len = len;
890 /* If copying requires more than two move insns,
891 copy addresses to registers (to make displacements shorter)
892 and use post-increment if available. */
893 if (!(data.autinc_from && data.autinc_to)
894 && move_by_pieces_ninsns (len, align, max_size) > 2)
896 /* Find the mode of the largest move...
897 MODE might not be used depending on the definitions of the
898 USE_* macros below. */
899 machine_mode mode ATTRIBUTE_UNUSED
900 = widest_int_mode_for_size (max_size);
902 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
904 data.from_addr = copy_to_mode_reg (from_addr_mode,
905 plus_constant (from_addr_mode,
906 from_addr, len));
907 data.autinc_from = 1;
908 data.explicit_inc_from = -1;
910 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
912 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
913 data.autinc_from = 1;
914 data.explicit_inc_from = 1;
916 if (!data.autinc_from && CONSTANT_P (from_addr))
917 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
918 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
920 data.to_addr = copy_to_mode_reg (to_addr_mode,
921 plus_constant (to_addr_mode,
922 to_addr, len));
923 data.autinc_to = 1;
924 data.explicit_inc_to = -1;
926 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
928 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
929 data.autinc_to = 1;
930 data.explicit_inc_to = 1;
932 if (!data.autinc_to && CONSTANT_P (to_addr))
933 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
936 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
938 /* First move what we can in the largest integer mode, then go to
939 successively smaller modes. */
941 while (max_size > 1 && data.len > 0)
943 machine_mode mode = widest_int_mode_for_size (max_size);
945 if (mode == VOIDmode)
946 break;
948 icode = optab_handler (mov_optab, mode);
949 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
950 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
952 max_size = GET_MODE_SIZE (mode);
955 /* The code above should have handled everything. */
956 gcc_assert (!data.len);
958 if (endp)
960 rtx to1;
962 gcc_assert (!data.reverse);
963 if (data.autinc_to)
965 if (endp == 2)
967 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
968 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
969 else
970 data.to_addr = copy_to_mode_reg (to_addr_mode,
971 plus_constant (to_addr_mode,
972 data.to_addr,
973 -1));
975 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
976 data.offset);
978 else
980 if (endp == 2)
981 --data.offset;
982 to1 = adjust_address (data.to, QImode, data.offset);
984 return to1;
986 else
987 return data.to;
990 /* Return number of insns required to move L bytes by pieces.
991 ALIGN (in bits) is maximum alignment we can assume. */
993 unsigned HOST_WIDE_INT
994 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
995 unsigned int max_size)
997 unsigned HOST_WIDE_INT n_insns = 0;
999 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1001 while (max_size > 1 && l > 0)
1003 machine_mode mode;
1004 enum insn_code icode;
1006 mode = widest_int_mode_for_size (max_size);
1008 if (mode == VOIDmode)
1009 break;
1011 icode = optab_handler (mov_optab, mode);
1012 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1013 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1015 max_size = GET_MODE_SIZE (mode);
1018 gcc_assert (!l);
1019 return n_insns;
1022 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1023 with move instructions for mode MODE. GENFUN is the gen_... function
1024 to make a move insn for that mode. DATA has all the other info. */
1026 static void
1027 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1028 struct move_by_pieces_d *data)
1030 unsigned int size = GET_MODE_SIZE (mode);
1031 rtx to1 = NULL_RTX, from1;
1033 while (data->len >= size)
1035 if (data->reverse)
1036 data->offset -= size;
1038 if (data->to)
1040 if (data->autinc_to)
1041 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1042 data->offset);
1043 else
1044 to1 = adjust_address (data->to, mode, data->offset);
1047 if (data->autinc_from)
1048 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1049 data->offset);
1050 else
1051 from1 = adjust_address (data->from, mode, data->offset);
1053 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1054 emit_insn (gen_add2_insn (data->to_addr,
1055 gen_int_mode (-(HOST_WIDE_INT) size,
1056 GET_MODE (data->to_addr))));
1057 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1058 emit_insn (gen_add2_insn (data->from_addr,
1059 gen_int_mode (-(HOST_WIDE_INT) size,
1060 GET_MODE (data->from_addr))));
1062 if (data->to)
1063 emit_insn ((*genfun) (to1, from1));
1064 else
1066 #ifdef PUSH_ROUNDING
1067 emit_single_push_insn (mode, from1, NULL);
1068 #else
1069 gcc_unreachable ();
1070 #endif
1073 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1074 emit_insn (gen_add2_insn (data->to_addr,
1075 gen_int_mode (size,
1076 GET_MODE (data->to_addr))));
1077 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1078 emit_insn (gen_add2_insn (data->from_addr,
1079 gen_int_mode (size,
1080 GET_MODE (data->from_addr))));
1082 if (! data->reverse)
1083 data->offset += size;
1085 data->len -= size;
1089 /* Emit code to move a block Y to a block X. This may be done with
1090 string-move instructions, with multiple scalar move instructions,
1091 or with a library call.
1093 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1094 SIZE is an rtx that says how long they are.
1095 ALIGN is the maximum alignment we can assume they have.
1096 METHOD describes what kind of copy this is, and what mechanisms may be used.
1097 MIN_SIZE is the minimal size of block to move
1098 MAX_SIZE is the maximal size of block to move, if it can not be represented
1099 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1101 Return the address of the new block, if memcpy is called and returns it,
1102 0 otherwise. */
1105 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1106 unsigned int expected_align, HOST_WIDE_INT expected_size,
1107 unsigned HOST_WIDE_INT min_size,
1108 unsigned HOST_WIDE_INT max_size,
1109 unsigned HOST_WIDE_INT probable_max_size)
1111 bool may_use_call;
1112 rtx retval = 0;
1113 unsigned int align;
1115 gcc_assert (size);
1116 if (CONST_INT_P (size)
1117 && INTVAL (size) == 0)
1118 return 0;
1120 switch (method)
1122 case BLOCK_OP_NORMAL:
1123 case BLOCK_OP_TAILCALL:
1124 may_use_call = true;
1125 break;
1127 case BLOCK_OP_CALL_PARM:
1128 may_use_call = block_move_libcall_safe_for_call_parm ();
1130 /* Make inhibit_defer_pop nonzero around the library call
1131 to force it to pop the arguments right away. */
1132 NO_DEFER_POP;
1133 break;
1135 case BLOCK_OP_NO_LIBCALL:
1136 may_use_call = false;
1137 break;
1139 default:
1140 gcc_unreachable ();
1143 gcc_assert (MEM_P (x) && MEM_P (y));
1144 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1145 gcc_assert (align >= BITS_PER_UNIT);
1147 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1148 block copy is more efficient for other large modes, e.g. DCmode. */
1149 x = adjust_address (x, BLKmode, 0);
1150 y = adjust_address (y, BLKmode, 0);
1152 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1153 can be incorrect is coming from __builtin_memcpy. */
1154 if (CONST_INT_P (size))
1156 x = shallow_copy_rtx (x);
1157 y = shallow_copy_rtx (y);
1158 set_mem_size (x, INTVAL (size));
1159 set_mem_size (y, INTVAL (size));
1162 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1163 move_by_pieces (x, y, INTVAL (size), align, 0);
1164 else if (emit_block_move_via_movmem (x, y, size, align,
1165 expected_align, expected_size,
1166 min_size, max_size, probable_max_size))
1168 else if (may_use_call
1169 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1170 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1172 /* Since x and y are passed to a libcall, mark the corresponding
1173 tree EXPR as addressable. */
1174 tree y_expr = MEM_EXPR (y);
1175 tree x_expr = MEM_EXPR (x);
1176 if (y_expr)
1177 mark_addressable (y_expr);
1178 if (x_expr)
1179 mark_addressable (x_expr);
1180 retval = emit_block_move_via_libcall (x, y, size,
1181 method == BLOCK_OP_TAILCALL);
1184 else
1185 emit_block_move_via_loop (x, y, size, align);
1187 if (method == BLOCK_OP_CALL_PARM)
1188 OK_DEFER_POP;
1190 return retval;
1194 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1196 unsigned HOST_WIDE_INT max, min = 0;
1197 if (GET_CODE (size) == CONST_INT)
1198 min = max = UINTVAL (size);
1199 else
1200 max = GET_MODE_MASK (GET_MODE (size));
1201 return emit_block_move_hints (x, y, size, method, 0, -1,
1202 min, max, max);
1205 /* A subroutine of emit_block_move. Returns true if calling the
1206 block move libcall will not clobber any parameters which may have
1207 already been placed on the stack. */
1209 static bool
1210 block_move_libcall_safe_for_call_parm (void)
1212 #if defined (REG_PARM_STACK_SPACE)
1213 tree fn;
1214 #endif
1216 /* If arguments are pushed on the stack, then they're safe. */
1217 if (PUSH_ARGS)
1218 return true;
1220 /* If registers go on the stack anyway, any argument is sure to clobber
1221 an outgoing argument. */
1222 #if defined (REG_PARM_STACK_SPACE)
1223 fn = emit_block_move_libcall_fn (false);
1224 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1225 depend on its argument. */
1226 (void) fn;
1227 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1228 && REG_PARM_STACK_SPACE (fn) != 0)
1229 return false;
1230 #endif
1232 /* If any argument goes in memory, then it might clobber an outgoing
1233 argument. */
1235 CUMULATIVE_ARGS args_so_far_v;
1236 cumulative_args_t args_so_far;
1237 tree fn, arg;
1239 fn = emit_block_move_libcall_fn (false);
1240 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1241 args_so_far = pack_cumulative_args (&args_so_far_v);
1243 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1244 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1246 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1247 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1248 NULL_TREE, true);
1249 if (!tmp || !REG_P (tmp))
1250 return false;
1251 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1252 return false;
1253 targetm.calls.function_arg_advance (args_so_far, mode,
1254 NULL_TREE, true);
1257 return true;
1260 /* A subroutine of emit_block_move. Expand a movmem pattern;
1261 return true if successful. */
1263 static bool
1264 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1265 unsigned int expected_align, HOST_WIDE_INT expected_size,
1266 unsigned HOST_WIDE_INT min_size,
1267 unsigned HOST_WIDE_INT max_size,
1268 unsigned HOST_WIDE_INT probable_max_size)
1270 int save_volatile_ok = volatile_ok;
1271 machine_mode mode;
1273 if (expected_align < align)
1274 expected_align = align;
1275 if (expected_size != -1)
1277 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1278 expected_size = probable_max_size;
1279 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1280 expected_size = min_size;
1283 /* Since this is a move insn, we don't care about volatility. */
1284 volatile_ok = 1;
1286 /* Try the most limited insn first, because there's no point
1287 including more than one in the machine description unless
1288 the more limited one has some advantage. */
1290 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1291 mode = GET_MODE_WIDER_MODE (mode))
1293 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1295 if (code != CODE_FOR_nothing
1296 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1297 here because if SIZE is less than the mode mask, as it is
1298 returned by the macro, it will definitely be less than the
1299 actual mode mask. Since SIZE is within the Pmode address
1300 space, we limit MODE to Pmode. */
1301 && ((CONST_INT_P (size)
1302 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1303 <= (GET_MODE_MASK (mode) >> 1)))
1304 || max_size <= (GET_MODE_MASK (mode) >> 1)
1305 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1307 struct expand_operand ops[9];
1308 unsigned int nops;
1310 /* ??? When called via emit_block_move_for_call, it'd be
1311 nice if there were some way to inform the backend, so
1312 that it doesn't fail the expansion because it thinks
1313 emitting the libcall would be more efficient. */
1314 nops = insn_data[(int) code].n_generator_args;
1315 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1317 create_fixed_operand (&ops[0], x);
1318 create_fixed_operand (&ops[1], y);
1319 /* The check above guarantees that this size conversion is valid. */
1320 create_convert_operand_to (&ops[2], size, mode, true);
1321 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1322 if (nops >= 6)
1324 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1325 create_integer_operand (&ops[5], expected_size);
1327 if (nops >= 8)
1329 create_integer_operand (&ops[6], min_size);
1330 /* If we can not represent the maximal size,
1331 make parameter NULL. */
1332 if ((HOST_WIDE_INT) max_size != -1)
1333 create_integer_operand (&ops[7], max_size);
1334 else
1335 create_fixed_operand (&ops[7], NULL);
1337 if (nops == 9)
1339 /* If we can not represent the maximal size,
1340 make parameter NULL. */
1341 if ((HOST_WIDE_INT) probable_max_size != -1)
1342 create_integer_operand (&ops[8], probable_max_size);
1343 else
1344 create_fixed_operand (&ops[8], NULL);
1346 if (maybe_expand_insn (code, nops, ops))
1348 volatile_ok = save_volatile_ok;
1349 return true;
1354 volatile_ok = save_volatile_ok;
1355 return false;
1358 /* A subroutine of emit_block_move. Expand a call to memcpy.
1359 Return the return value from memcpy, 0 otherwise. */
1362 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1364 rtx dst_addr, src_addr;
1365 tree call_expr, fn, src_tree, dst_tree, size_tree;
1366 machine_mode size_mode;
1367 rtx retval;
1369 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1370 pseudos. We can then place those new pseudos into a VAR_DECL and
1371 use them later. */
1373 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1374 src_addr = copy_addr_to_reg (XEXP (src, 0));
1376 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1377 src_addr = convert_memory_address (ptr_mode, src_addr);
1379 dst_tree = make_tree (ptr_type_node, dst_addr);
1380 src_tree = make_tree (ptr_type_node, src_addr);
1382 size_mode = TYPE_MODE (sizetype);
1384 size = convert_to_mode (size_mode, size, 1);
1385 size = copy_to_mode_reg (size_mode, size);
1387 /* It is incorrect to use the libcall calling conventions to call
1388 memcpy in this context. This could be a user call to memcpy and
1389 the user may wish to examine the return value from memcpy. For
1390 targets where libcalls and normal calls have different conventions
1391 for returning pointers, we could end up generating incorrect code. */
1393 size_tree = make_tree (sizetype, size);
1395 fn = emit_block_move_libcall_fn (true);
1396 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1397 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1399 retval = expand_normal (call_expr);
1401 return retval;
1404 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1405 for the function we use for block copies. */
1407 static GTY(()) tree block_move_fn;
1409 void
1410 init_block_move_fn (const char *asmspec)
1412 if (!block_move_fn)
1414 tree args, fn, attrs, attr_args;
1416 fn = get_identifier ("memcpy");
1417 args = build_function_type_list (ptr_type_node, ptr_type_node,
1418 const_ptr_type_node, sizetype,
1419 NULL_TREE);
1421 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1422 DECL_EXTERNAL (fn) = 1;
1423 TREE_PUBLIC (fn) = 1;
1424 DECL_ARTIFICIAL (fn) = 1;
1425 TREE_NOTHROW (fn) = 1;
1426 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1427 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1429 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1430 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1432 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1434 block_move_fn = fn;
1437 if (asmspec)
1438 set_user_assembler_name (block_move_fn, asmspec);
1441 static tree
1442 emit_block_move_libcall_fn (int for_call)
1444 static bool emitted_extern;
1446 if (!block_move_fn)
1447 init_block_move_fn (NULL);
1449 if (for_call && !emitted_extern)
1451 emitted_extern = true;
1452 make_decl_rtl (block_move_fn);
1455 return block_move_fn;
1458 /* A subroutine of emit_block_move. Copy the data via an explicit
1459 loop. This is used only when libcalls are forbidden. */
1460 /* ??? It'd be nice to copy in hunks larger than QImode. */
1462 static void
1463 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1464 unsigned int align ATTRIBUTE_UNUSED)
1466 rtx_code_label *cmp_label, *top_label;
1467 rtx iter, x_addr, y_addr, tmp;
1468 machine_mode x_addr_mode = get_address_mode (x);
1469 machine_mode y_addr_mode = get_address_mode (y);
1470 machine_mode iter_mode;
1472 iter_mode = GET_MODE (size);
1473 if (iter_mode == VOIDmode)
1474 iter_mode = word_mode;
1476 top_label = gen_label_rtx ();
1477 cmp_label = gen_label_rtx ();
1478 iter = gen_reg_rtx (iter_mode);
1480 emit_move_insn (iter, const0_rtx);
1482 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1483 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1484 do_pending_stack_adjust ();
1486 emit_jump (cmp_label);
1487 emit_label (top_label);
1489 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1490 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1492 if (x_addr_mode != y_addr_mode)
1493 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1494 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1496 x = change_address (x, QImode, x_addr);
1497 y = change_address (y, QImode, y_addr);
1499 emit_move_insn (x, y);
1501 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1502 true, OPTAB_LIB_WIDEN);
1503 if (tmp != iter)
1504 emit_move_insn (iter, tmp);
1506 emit_label (cmp_label);
1508 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1509 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1512 /* Copy all or part of a value X into registers starting at REGNO.
1513 The number of registers to be filled is NREGS. */
1515 void
1516 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1518 int i;
1519 #ifdef HAVE_load_multiple
1520 rtx pat;
1521 rtx_insn *last;
1522 #endif
1524 if (nregs == 0)
1525 return;
1527 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1528 x = validize_mem (force_const_mem (mode, x));
1530 /* See if the machine can do this with a load multiple insn. */
1531 #ifdef HAVE_load_multiple
1532 if (HAVE_load_multiple)
1534 last = get_last_insn ();
1535 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1536 GEN_INT (nregs));
1537 if (pat)
1539 emit_insn (pat);
1540 return;
1542 else
1543 delete_insns_since (last);
1545 #endif
1547 for (i = 0; i < nregs; i++)
1548 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1549 operand_subword_force (x, i, mode));
1552 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1553 The number of registers to be filled is NREGS. */
1555 void
1556 move_block_from_reg (int regno, rtx x, int nregs)
1558 int i;
1560 if (nregs == 0)
1561 return;
1563 /* See if the machine can do this with a store multiple insn. */
1564 #ifdef HAVE_store_multiple
1565 if (HAVE_store_multiple)
1567 rtx_insn *last = get_last_insn ();
1568 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1569 GEN_INT (nregs));
1570 if (pat)
1572 emit_insn (pat);
1573 return;
1575 else
1576 delete_insns_since (last);
1578 #endif
1580 for (i = 0; i < nregs; i++)
1582 rtx tem = operand_subword (x, i, 1, BLKmode);
1584 gcc_assert (tem);
1586 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1590 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1591 ORIG, where ORIG is a non-consecutive group of registers represented by
1592 a PARALLEL. The clone is identical to the original except in that the
1593 original set of registers is replaced by a new set of pseudo registers.
1594 The new set has the same modes as the original set. */
1597 gen_group_rtx (rtx orig)
1599 int i, length;
1600 rtx *tmps;
1602 gcc_assert (GET_CODE (orig) == PARALLEL);
1604 length = XVECLEN (orig, 0);
1605 tmps = XALLOCAVEC (rtx, length);
1607 /* Skip a NULL entry in first slot. */
1608 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1610 if (i)
1611 tmps[0] = 0;
1613 for (; i < length; i++)
1615 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1616 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1618 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1621 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1624 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1625 except that values are placed in TMPS[i], and must later be moved
1626 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1628 static void
1629 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1631 rtx src;
1632 int start, i;
1633 machine_mode m = GET_MODE (orig_src);
1635 gcc_assert (GET_CODE (dst) == PARALLEL);
1637 if (m != VOIDmode
1638 && !SCALAR_INT_MODE_P (m)
1639 && !MEM_P (orig_src)
1640 && GET_CODE (orig_src) != CONCAT)
1642 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1643 if (imode == BLKmode)
1644 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1645 else
1646 src = gen_reg_rtx (imode);
1647 if (imode != BLKmode)
1648 src = gen_lowpart (GET_MODE (orig_src), src);
1649 emit_move_insn (src, orig_src);
1650 /* ...and back again. */
1651 if (imode != BLKmode)
1652 src = gen_lowpart (imode, src);
1653 emit_group_load_1 (tmps, dst, src, type, ssize);
1654 return;
1657 /* Check for a NULL entry, used to indicate that the parameter goes
1658 both on the stack and in registers. */
1659 if (XEXP (XVECEXP (dst, 0, 0), 0))
1660 start = 0;
1661 else
1662 start = 1;
1664 /* Process the pieces. */
1665 for (i = start; i < XVECLEN (dst, 0); i++)
1667 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1668 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1669 unsigned int bytelen = GET_MODE_SIZE (mode);
1670 int shift = 0;
1672 /* Handle trailing fragments that run over the size of the struct. */
1673 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1675 /* Arrange to shift the fragment to where it belongs.
1676 extract_bit_field loads to the lsb of the reg. */
1677 if (
1678 #ifdef BLOCK_REG_PADDING
1679 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1680 == (BYTES_BIG_ENDIAN ? upward : downward)
1681 #else
1682 BYTES_BIG_ENDIAN
1683 #endif
1685 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1686 bytelen = ssize - bytepos;
1687 gcc_assert (bytelen > 0);
1690 /* If we won't be loading directly from memory, protect the real source
1691 from strange tricks we might play; but make sure that the source can
1692 be loaded directly into the destination. */
1693 src = orig_src;
1694 if (!MEM_P (orig_src)
1695 && (!CONSTANT_P (orig_src)
1696 || (GET_MODE (orig_src) != mode
1697 && GET_MODE (orig_src) != VOIDmode)))
1699 if (GET_MODE (orig_src) == VOIDmode)
1700 src = gen_reg_rtx (mode);
1701 else
1702 src = gen_reg_rtx (GET_MODE (orig_src));
1704 emit_move_insn (src, orig_src);
1707 /* Optimize the access just a bit. */
1708 if (MEM_P (src)
1709 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1710 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1711 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1712 && bytelen == GET_MODE_SIZE (mode))
1714 tmps[i] = gen_reg_rtx (mode);
1715 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1717 else if (COMPLEX_MODE_P (mode)
1718 && GET_MODE (src) == mode
1719 && bytelen == GET_MODE_SIZE (mode))
1720 /* Let emit_move_complex do the bulk of the work. */
1721 tmps[i] = src;
1722 else if (GET_CODE (src) == CONCAT)
1724 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1725 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1727 if ((bytepos == 0 && bytelen == slen0)
1728 || (bytepos != 0 && bytepos + bytelen <= slen))
1730 /* The following assumes that the concatenated objects all
1731 have the same size. In this case, a simple calculation
1732 can be used to determine the object and the bit field
1733 to be extracted. */
1734 tmps[i] = XEXP (src, bytepos / slen0);
1735 if (! CONSTANT_P (tmps[i])
1736 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1737 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1738 (bytepos % slen0) * BITS_PER_UNIT,
1739 1, NULL_RTX, mode, mode);
1741 else
1743 rtx mem;
1745 gcc_assert (!bytepos);
1746 mem = assign_stack_temp (GET_MODE (src), slen);
1747 emit_move_insn (mem, src);
1748 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1749 0, 1, NULL_RTX, mode, mode);
1752 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1753 SIMD register, which is currently broken. While we get GCC
1754 to emit proper RTL for these cases, let's dump to memory. */
1755 else if (VECTOR_MODE_P (GET_MODE (dst))
1756 && REG_P (src))
1758 int slen = GET_MODE_SIZE (GET_MODE (src));
1759 rtx mem;
1761 mem = assign_stack_temp (GET_MODE (src), slen);
1762 emit_move_insn (mem, src);
1763 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1765 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1766 && XVECLEN (dst, 0) > 1)
1767 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1768 else if (CONSTANT_P (src))
1770 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1772 if (len == ssize)
1773 tmps[i] = src;
1774 else
1776 rtx first, second;
1778 /* TODO: const_wide_int can have sizes other than this... */
1779 gcc_assert (2 * len == ssize);
1780 split_double (src, &first, &second);
1781 if (i)
1782 tmps[i] = second;
1783 else
1784 tmps[i] = first;
1787 else if (REG_P (src) && GET_MODE (src) == mode)
1788 tmps[i] = src;
1789 else
1790 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1791 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1792 mode, mode);
1794 if (shift)
1795 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1796 shift, tmps[i], 0);
1800 /* Emit code to move a block SRC of type TYPE to a block DST,
1801 where DST is non-consecutive registers represented by a PARALLEL.
1802 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1803 if not known. */
1805 void
1806 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1808 rtx *tmps;
1809 int i;
1811 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1812 emit_group_load_1 (tmps, dst, src, type, ssize);
1814 /* Copy the extracted pieces into the proper (probable) hard regs. */
1815 for (i = 0; i < XVECLEN (dst, 0); i++)
1817 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1818 if (d == NULL)
1819 continue;
1820 emit_move_insn (d, tmps[i]);
1824 /* Similar, but load SRC into new pseudos in a format that looks like
1825 PARALLEL. This can later be fed to emit_group_move to get things
1826 in the right place. */
1829 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1831 rtvec vec;
1832 int i;
1834 vec = rtvec_alloc (XVECLEN (parallel, 0));
1835 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1837 /* Convert the vector to look just like the original PARALLEL, except
1838 with the computed values. */
1839 for (i = 0; i < XVECLEN (parallel, 0); i++)
1841 rtx e = XVECEXP (parallel, 0, i);
1842 rtx d = XEXP (e, 0);
1844 if (d)
1846 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1847 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1849 RTVEC_ELT (vec, i) = e;
1852 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1855 /* Emit code to move a block SRC to block DST, where SRC and DST are
1856 non-consecutive groups of registers, each represented by a PARALLEL. */
1858 void
1859 emit_group_move (rtx dst, rtx src)
1861 int i;
1863 gcc_assert (GET_CODE (src) == PARALLEL
1864 && GET_CODE (dst) == PARALLEL
1865 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1867 /* Skip first entry if NULL. */
1868 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1869 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1870 XEXP (XVECEXP (src, 0, i), 0));
1873 /* Move a group of registers represented by a PARALLEL into pseudos. */
1876 emit_group_move_into_temps (rtx src)
1878 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1879 int i;
1881 for (i = 0; i < XVECLEN (src, 0); i++)
1883 rtx e = XVECEXP (src, 0, i);
1884 rtx d = XEXP (e, 0);
1886 if (d)
1887 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1888 RTVEC_ELT (vec, i) = e;
1891 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1894 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1895 where SRC is non-consecutive registers represented by a PARALLEL.
1896 SSIZE represents the total size of block ORIG_DST, or -1 if not
1897 known. */
1899 void
1900 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1902 rtx *tmps, dst;
1903 int start, finish, i;
1904 machine_mode m = GET_MODE (orig_dst);
1906 gcc_assert (GET_CODE (src) == PARALLEL);
1908 if (!SCALAR_INT_MODE_P (m)
1909 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1911 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1912 if (imode == BLKmode)
1913 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1914 else
1915 dst = gen_reg_rtx (imode);
1916 emit_group_store (dst, src, type, ssize);
1917 if (imode != BLKmode)
1918 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1919 emit_move_insn (orig_dst, dst);
1920 return;
1923 /* Check for a NULL entry, used to indicate that the parameter goes
1924 both on the stack and in registers. */
1925 if (XEXP (XVECEXP (src, 0, 0), 0))
1926 start = 0;
1927 else
1928 start = 1;
1929 finish = XVECLEN (src, 0);
1931 tmps = XALLOCAVEC (rtx, finish);
1933 /* Copy the (probable) hard regs into pseudos. */
1934 for (i = start; i < finish; i++)
1936 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1937 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1939 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1940 emit_move_insn (tmps[i], reg);
1942 else
1943 tmps[i] = reg;
1946 /* If we won't be storing directly into memory, protect the real destination
1947 from strange tricks we might play. */
1948 dst = orig_dst;
1949 if (GET_CODE (dst) == PARALLEL)
1951 rtx temp;
1953 /* We can get a PARALLEL dst if there is a conditional expression in
1954 a return statement. In that case, the dst and src are the same,
1955 so no action is necessary. */
1956 if (rtx_equal_p (dst, src))
1957 return;
1959 /* It is unclear if we can ever reach here, but we may as well handle
1960 it. Allocate a temporary, and split this into a store/load to/from
1961 the temporary. */
1962 temp = assign_stack_temp (GET_MODE (dst), ssize);
1963 emit_group_store (temp, src, type, ssize);
1964 emit_group_load (dst, temp, type, ssize);
1965 return;
1967 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1969 machine_mode outer = GET_MODE (dst);
1970 machine_mode inner;
1971 HOST_WIDE_INT bytepos;
1972 bool done = false;
1973 rtx temp;
1975 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1976 dst = gen_reg_rtx (outer);
1978 /* Make life a bit easier for combine. */
1979 /* If the first element of the vector is the low part
1980 of the destination mode, use a paradoxical subreg to
1981 initialize the destination. */
1982 if (start < finish)
1984 inner = GET_MODE (tmps[start]);
1985 bytepos = subreg_lowpart_offset (inner, outer);
1986 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1988 temp = simplify_gen_subreg (outer, tmps[start],
1989 inner, 0);
1990 if (temp)
1992 emit_move_insn (dst, temp);
1993 done = true;
1994 start++;
1999 /* If the first element wasn't the low part, try the last. */
2000 if (!done
2001 && start < finish - 1)
2003 inner = GET_MODE (tmps[finish - 1]);
2004 bytepos = subreg_lowpart_offset (inner, outer);
2005 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2007 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2008 inner, 0);
2009 if (temp)
2011 emit_move_insn (dst, temp);
2012 done = true;
2013 finish--;
2018 /* Otherwise, simply initialize the result to zero. */
2019 if (!done)
2020 emit_move_insn (dst, CONST0_RTX (outer));
2023 /* Process the pieces. */
2024 for (i = start; i < finish; i++)
2026 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2027 machine_mode mode = GET_MODE (tmps[i]);
2028 unsigned int bytelen = GET_MODE_SIZE (mode);
2029 unsigned int adj_bytelen;
2030 rtx dest = dst;
2032 /* Handle trailing fragments that run over the size of the struct. */
2033 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2034 adj_bytelen = ssize - bytepos;
2035 else
2036 adj_bytelen = bytelen;
2038 if (GET_CODE (dst) == CONCAT)
2040 if (bytepos + adj_bytelen
2041 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2042 dest = XEXP (dst, 0);
2043 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2045 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2046 dest = XEXP (dst, 1);
2048 else
2050 machine_mode dest_mode = GET_MODE (dest);
2051 machine_mode tmp_mode = GET_MODE (tmps[i]);
2053 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2055 if (GET_MODE_ALIGNMENT (dest_mode)
2056 >= GET_MODE_ALIGNMENT (tmp_mode))
2058 dest = assign_stack_temp (dest_mode,
2059 GET_MODE_SIZE (dest_mode));
2060 emit_move_insn (adjust_address (dest,
2061 tmp_mode,
2062 bytepos),
2063 tmps[i]);
2064 dst = dest;
2066 else
2068 dest = assign_stack_temp (tmp_mode,
2069 GET_MODE_SIZE (tmp_mode));
2070 emit_move_insn (dest, tmps[i]);
2071 dst = adjust_address (dest, dest_mode, bytepos);
2073 break;
2077 /* Handle trailing fragments that run over the size of the struct. */
2078 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2080 /* store_bit_field always takes its value from the lsb.
2081 Move the fragment to the lsb if it's not already there. */
2082 if (
2083 #ifdef BLOCK_REG_PADDING
2084 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2085 == (BYTES_BIG_ENDIAN ? upward : downward)
2086 #else
2087 BYTES_BIG_ENDIAN
2088 #endif
2091 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2092 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2093 shift, tmps[i], 0);
2096 /* Make sure not to write past the end of the struct. */
2097 store_bit_field (dest,
2098 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2099 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2100 VOIDmode, tmps[i]);
2103 /* Optimize the access just a bit. */
2104 else if (MEM_P (dest)
2105 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2106 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2107 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2108 && bytelen == GET_MODE_SIZE (mode))
2109 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2111 else
2112 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2113 0, 0, mode, tmps[i]);
2116 /* Copy from the pseudo into the (probable) hard reg. */
2117 if (orig_dst != dst)
2118 emit_move_insn (orig_dst, dst);
2121 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2122 of the value stored in X. */
2125 maybe_emit_group_store (rtx x, tree type)
2127 machine_mode mode = TYPE_MODE (type);
2128 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2129 if (GET_CODE (x) == PARALLEL)
2131 rtx result = gen_reg_rtx (mode);
2132 emit_group_store (result, x, type, int_size_in_bytes (type));
2133 return result;
2135 return x;
2138 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2140 This is used on targets that return BLKmode values in registers. */
2142 void
2143 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2145 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2146 rtx src = NULL, dst = NULL;
2147 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2148 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2149 machine_mode mode = GET_MODE (srcreg);
2150 machine_mode tmode = GET_MODE (target);
2151 machine_mode copy_mode;
2153 /* BLKmode registers created in the back-end shouldn't have survived. */
2154 gcc_assert (mode != BLKmode);
2156 /* If the structure doesn't take up a whole number of words, see whether
2157 SRCREG is padded on the left or on the right. If it's on the left,
2158 set PADDING_CORRECTION to the number of bits to skip.
2160 In most ABIs, the structure will be returned at the least end of
2161 the register, which translates to right padding on little-endian
2162 targets and left padding on big-endian targets. The opposite
2163 holds if the structure is returned at the most significant
2164 end of the register. */
2165 if (bytes % UNITS_PER_WORD != 0
2166 && (targetm.calls.return_in_msb (type)
2167 ? !BYTES_BIG_ENDIAN
2168 : BYTES_BIG_ENDIAN))
2169 padding_correction
2170 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2172 /* We can use a single move if we have an exact mode for the size. */
2173 else if (MEM_P (target)
2174 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2175 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2176 && bytes == GET_MODE_SIZE (mode))
2178 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2179 return;
2182 /* And if we additionally have the same mode for a register. */
2183 else if (REG_P (target)
2184 && GET_MODE (target) == mode
2185 && bytes == GET_MODE_SIZE (mode))
2187 emit_move_insn (target, srcreg);
2188 return;
2191 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2192 into a new pseudo which is a full word. */
2193 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2195 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2196 mode = word_mode;
2199 /* Copy the structure BITSIZE bits at a time. If the target lives in
2200 memory, take care of not reading/writing past its end by selecting
2201 a copy mode suited to BITSIZE. This should always be possible given
2202 how it is computed.
2204 If the target lives in register, make sure not to select a copy mode
2205 larger than the mode of the register.
2207 We could probably emit more efficient code for machines which do not use
2208 strict alignment, but it doesn't seem worth the effort at the current
2209 time. */
2211 copy_mode = word_mode;
2212 if (MEM_P (target))
2214 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2215 if (mem_mode != BLKmode)
2216 copy_mode = mem_mode;
2218 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2219 copy_mode = tmode;
2221 for (bitpos = 0, xbitpos = padding_correction;
2222 bitpos < bytes * BITS_PER_UNIT;
2223 bitpos += bitsize, xbitpos += bitsize)
2225 /* We need a new source operand each time xbitpos is on a
2226 word boundary and when xbitpos == padding_correction
2227 (the first time through). */
2228 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2229 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2231 /* We need a new destination operand each time bitpos is on
2232 a word boundary. */
2233 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2234 dst = target;
2235 else if (bitpos % BITS_PER_WORD == 0)
2236 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2238 /* Use xbitpos for the source extraction (right justified) and
2239 bitpos for the destination store (left justified). */
2240 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2241 extract_bit_field (src, bitsize,
2242 xbitpos % BITS_PER_WORD, 1,
2243 NULL_RTX, copy_mode, copy_mode));
2247 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2248 register if it contains any data, otherwise return null.
2250 This is used on targets that return BLKmode values in registers. */
2253 copy_blkmode_to_reg (machine_mode mode, tree src)
2255 int i, n_regs;
2256 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2257 unsigned int bitsize;
2258 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2259 machine_mode dst_mode;
2261 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2263 x = expand_normal (src);
2265 bytes = int_size_in_bytes (TREE_TYPE (src));
2266 if (bytes == 0)
2267 return NULL_RTX;
2269 /* If the structure doesn't take up a whole number of words, see
2270 whether the register value should be padded on the left or on
2271 the right. Set PADDING_CORRECTION to the number of padding
2272 bits needed on the left side.
2274 In most ABIs, the structure will be returned at the least end of
2275 the register, which translates to right padding on little-endian
2276 targets and left padding on big-endian targets. The opposite
2277 holds if the structure is returned at the most significant
2278 end of the register. */
2279 if (bytes % UNITS_PER_WORD != 0
2280 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2281 ? !BYTES_BIG_ENDIAN
2282 : BYTES_BIG_ENDIAN))
2283 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2284 * BITS_PER_UNIT));
2286 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2287 dst_words = XALLOCAVEC (rtx, n_regs);
2288 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2290 /* Copy the structure BITSIZE bits at a time. */
2291 for (bitpos = 0, xbitpos = padding_correction;
2292 bitpos < bytes * BITS_PER_UNIT;
2293 bitpos += bitsize, xbitpos += bitsize)
2295 /* We need a new destination pseudo each time xbitpos is
2296 on a word boundary and when xbitpos == padding_correction
2297 (the first time through). */
2298 if (xbitpos % BITS_PER_WORD == 0
2299 || xbitpos == padding_correction)
2301 /* Generate an appropriate register. */
2302 dst_word = gen_reg_rtx (word_mode);
2303 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2305 /* Clear the destination before we move anything into it. */
2306 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2309 /* We need a new source operand each time bitpos is on a word
2310 boundary. */
2311 if (bitpos % BITS_PER_WORD == 0)
2312 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2314 /* Use bitpos for the source extraction (left justified) and
2315 xbitpos for the destination store (right justified). */
2316 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2317 0, 0, word_mode,
2318 extract_bit_field (src_word, bitsize,
2319 bitpos % BITS_PER_WORD, 1,
2320 NULL_RTX, word_mode, word_mode));
2323 if (mode == BLKmode)
2325 /* Find the smallest integer mode large enough to hold the
2326 entire structure. */
2327 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2328 mode != VOIDmode;
2329 mode = GET_MODE_WIDER_MODE (mode))
2330 /* Have we found a large enough mode? */
2331 if (GET_MODE_SIZE (mode) >= bytes)
2332 break;
2334 /* A suitable mode should have been found. */
2335 gcc_assert (mode != VOIDmode);
2338 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2339 dst_mode = word_mode;
2340 else
2341 dst_mode = mode;
2342 dst = gen_reg_rtx (dst_mode);
2344 for (i = 0; i < n_regs; i++)
2345 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2347 if (mode != dst_mode)
2348 dst = gen_lowpart (mode, dst);
2350 return dst;
2353 /* Add a USE expression for REG to the (possibly empty) list pointed
2354 to by CALL_FUSAGE. REG must denote a hard register. */
2356 void
2357 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2359 gcc_assert (REG_P (reg));
2361 if (!HARD_REGISTER_P (reg))
2362 return;
2364 *call_fusage
2365 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2368 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2369 to by CALL_FUSAGE. REG must denote a hard register. */
2371 void
2372 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2374 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2376 *call_fusage
2377 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2380 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2381 starting at REGNO. All of these registers must be hard registers. */
2383 void
2384 use_regs (rtx *call_fusage, int regno, int nregs)
2386 int i;
2388 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2390 for (i = 0; i < nregs; i++)
2391 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2394 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2395 PARALLEL REGS. This is for calls that pass values in multiple
2396 non-contiguous locations. The Irix 6 ABI has examples of this. */
2398 void
2399 use_group_regs (rtx *call_fusage, rtx regs)
2401 int i;
2403 for (i = 0; i < XVECLEN (regs, 0); i++)
2405 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2407 /* A NULL entry means the parameter goes both on the stack and in
2408 registers. This can also be a MEM for targets that pass values
2409 partially on the stack and partially in registers. */
2410 if (reg != 0 && REG_P (reg))
2411 use_reg (call_fusage, reg);
2415 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2416 assigment and the code of the expresion on the RHS is CODE. Return
2417 NULL otherwise. */
2419 static gimple
2420 get_def_for_expr (tree name, enum tree_code code)
2422 gimple def_stmt;
2424 if (TREE_CODE (name) != SSA_NAME)
2425 return NULL;
2427 def_stmt = get_gimple_for_ssa_name (name);
2428 if (!def_stmt
2429 || gimple_assign_rhs_code (def_stmt) != code)
2430 return NULL;
2432 return def_stmt;
2435 #ifdef HAVE_conditional_move
2436 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2437 assigment and the class of the expresion on the RHS is CLASS. Return
2438 NULL otherwise. */
2440 static gimple
2441 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2443 gimple def_stmt;
2445 if (TREE_CODE (name) != SSA_NAME)
2446 return NULL;
2448 def_stmt = get_gimple_for_ssa_name (name);
2449 if (!def_stmt
2450 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2451 return NULL;
2453 return def_stmt;
2455 #endif
2458 /* Determine whether the LEN bytes generated by CONSTFUN can be
2459 stored to memory using several move instructions. CONSTFUNDATA is
2460 a pointer which will be passed as argument in every CONSTFUN call.
2461 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2462 a memset operation and false if it's a copy of a constant string.
2463 Return nonzero if a call to store_by_pieces should succeed. */
2466 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2467 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2468 void *constfundata, unsigned int align, bool memsetp)
2470 unsigned HOST_WIDE_INT l;
2471 unsigned int max_size;
2472 HOST_WIDE_INT offset = 0;
2473 machine_mode mode;
2474 enum insn_code icode;
2475 int reverse;
2476 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2477 rtx cst ATTRIBUTE_UNUSED;
2479 if (len == 0)
2480 return 1;
2482 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2483 memsetp
2484 ? SET_BY_PIECES
2485 : STORE_BY_PIECES,
2486 optimize_insn_for_speed_p ()))
2487 return 0;
2489 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2491 /* We would first store what we can in the largest integer mode, then go to
2492 successively smaller modes. */
2494 for (reverse = 0;
2495 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2496 reverse++)
2498 l = len;
2499 max_size = STORE_MAX_PIECES + 1;
2500 while (max_size > 1 && l > 0)
2502 mode = widest_int_mode_for_size (max_size);
2504 if (mode == VOIDmode)
2505 break;
2507 icode = optab_handler (mov_optab, mode);
2508 if (icode != CODE_FOR_nothing
2509 && align >= GET_MODE_ALIGNMENT (mode))
2511 unsigned int size = GET_MODE_SIZE (mode);
2513 while (l >= size)
2515 if (reverse)
2516 offset -= size;
2518 cst = (*constfun) (constfundata, offset, mode);
2519 if (!targetm.legitimate_constant_p (mode, cst))
2520 return 0;
2522 if (!reverse)
2523 offset += size;
2525 l -= size;
2529 max_size = GET_MODE_SIZE (mode);
2532 /* The code above should have handled everything. */
2533 gcc_assert (!l);
2536 return 1;
2539 /* Generate several move instructions to store LEN bytes generated by
2540 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2541 pointer which will be passed as argument in every CONSTFUN call.
2542 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2543 a memset operation and false if it's a copy of a constant string.
2544 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2545 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2546 stpcpy. */
2549 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2550 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2551 void *constfundata, unsigned int align, bool memsetp, int endp)
2553 machine_mode to_addr_mode = get_address_mode (to);
2554 struct store_by_pieces_d data;
2556 if (len == 0)
2558 gcc_assert (endp != 2);
2559 return to;
2562 gcc_assert (targetm.use_by_pieces_infrastructure_p
2563 (len, align,
2564 memsetp
2565 ? SET_BY_PIECES
2566 : STORE_BY_PIECES,
2567 optimize_insn_for_speed_p ()));
2569 data.constfun = constfun;
2570 data.constfundata = constfundata;
2571 data.len = len;
2572 data.to = to;
2573 store_by_pieces_1 (&data, align);
2574 if (endp)
2576 rtx to1;
2578 gcc_assert (!data.reverse);
2579 if (data.autinc_to)
2581 if (endp == 2)
2583 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2584 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2585 else
2586 data.to_addr = copy_to_mode_reg (to_addr_mode,
2587 plus_constant (to_addr_mode,
2588 data.to_addr,
2589 -1));
2591 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2592 data.offset);
2594 else
2596 if (endp == 2)
2597 --data.offset;
2598 to1 = adjust_address (data.to, QImode, data.offset);
2600 return to1;
2602 else
2603 return data.to;
2606 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2607 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2609 static void
2610 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2612 struct store_by_pieces_d data;
2614 if (len == 0)
2615 return;
2617 data.constfun = clear_by_pieces_1;
2618 data.constfundata = NULL;
2619 data.len = len;
2620 data.to = to;
2621 store_by_pieces_1 (&data, align);
2624 /* Callback routine for clear_by_pieces.
2625 Return const0_rtx unconditionally. */
2627 static rtx
2628 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2629 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2630 machine_mode mode ATTRIBUTE_UNUSED)
2632 return const0_rtx;
2635 /* Subroutine of clear_by_pieces and store_by_pieces.
2636 Generate several move instructions to store LEN bytes of block TO. (A MEM
2637 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2639 static void
2640 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2641 unsigned int align ATTRIBUTE_UNUSED)
2643 machine_mode to_addr_mode = get_address_mode (data->to);
2644 rtx to_addr = XEXP (data->to, 0);
2645 unsigned int max_size = STORE_MAX_PIECES + 1;
2646 enum insn_code icode;
2648 data->offset = 0;
2649 data->to_addr = to_addr;
2650 data->autinc_to
2651 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2652 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2654 data->explicit_inc_to = 0;
2655 data->reverse
2656 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2657 if (data->reverse)
2658 data->offset = data->len;
2660 /* If storing requires more than two move insns,
2661 copy addresses to registers (to make displacements shorter)
2662 and use post-increment if available. */
2663 if (!data->autinc_to
2664 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2666 /* Determine the main mode we'll be using.
2667 MODE might not be used depending on the definitions of the
2668 USE_* macros below. */
2669 machine_mode mode ATTRIBUTE_UNUSED
2670 = widest_int_mode_for_size (max_size);
2672 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2674 data->to_addr = copy_to_mode_reg (to_addr_mode,
2675 plus_constant (to_addr_mode,
2676 to_addr,
2677 data->len));
2678 data->autinc_to = 1;
2679 data->explicit_inc_to = -1;
2682 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2683 && ! data->autinc_to)
2685 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2686 data->autinc_to = 1;
2687 data->explicit_inc_to = 1;
2690 if ( !data->autinc_to && CONSTANT_P (to_addr))
2691 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2694 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2696 /* First store what we can in the largest integer mode, then go to
2697 successively smaller modes. */
2699 while (max_size > 1 && data->len > 0)
2701 machine_mode mode = widest_int_mode_for_size (max_size);
2703 if (mode == VOIDmode)
2704 break;
2706 icode = optab_handler (mov_optab, mode);
2707 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2708 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2710 max_size = GET_MODE_SIZE (mode);
2713 /* The code above should have handled everything. */
2714 gcc_assert (!data->len);
2717 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2718 with move instructions for mode MODE. GENFUN is the gen_... function
2719 to make a move insn for that mode. DATA has all the other info. */
2721 static void
2722 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2723 struct store_by_pieces_d *data)
2725 unsigned int size = GET_MODE_SIZE (mode);
2726 rtx to1, cst;
2728 while (data->len >= size)
2730 if (data->reverse)
2731 data->offset -= size;
2733 if (data->autinc_to)
2734 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2735 data->offset);
2736 else
2737 to1 = adjust_address (data->to, mode, data->offset);
2739 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2740 emit_insn (gen_add2_insn (data->to_addr,
2741 gen_int_mode (-(HOST_WIDE_INT) size,
2742 GET_MODE (data->to_addr))));
2744 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2745 emit_insn ((*genfun) (to1, cst));
2747 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2748 emit_insn (gen_add2_insn (data->to_addr,
2749 gen_int_mode (size,
2750 GET_MODE (data->to_addr))));
2752 if (! data->reverse)
2753 data->offset += size;
2755 data->len -= size;
2759 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2760 its length in bytes. */
2763 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2764 unsigned int expected_align, HOST_WIDE_INT expected_size,
2765 unsigned HOST_WIDE_INT min_size,
2766 unsigned HOST_WIDE_INT max_size,
2767 unsigned HOST_WIDE_INT probable_max_size)
2769 machine_mode mode = GET_MODE (object);
2770 unsigned int align;
2772 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2774 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2775 just move a zero. Otherwise, do this a piece at a time. */
2776 if (mode != BLKmode
2777 && CONST_INT_P (size)
2778 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2780 rtx zero = CONST0_RTX (mode);
2781 if (zero != NULL)
2783 emit_move_insn (object, zero);
2784 return NULL;
2787 if (COMPLEX_MODE_P (mode))
2789 zero = CONST0_RTX (GET_MODE_INNER (mode));
2790 if (zero != NULL)
2792 write_complex_part (object, zero, 0);
2793 write_complex_part (object, zero, 1);
2794 return NULL;
2799 if (size == const0_rtx)
2800 return NULL;
2802 align = MEM_ALIGN (object);
2804 if (CONST_INT_P (size)
2805 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2806 CLEAR_BY_PIECES,
2807 optimize_insn_for_speed_p ()))
2808 clear_by_pieces (object, INTVAL (size), align);
2809 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2810 expected_align, expected_size,
2811 min_size, max_size, probable_max_size))
2813 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2814 return set_storage_via_libcall (object, size, const0_rtx,
2815 method == BLOCK_OP_TAILCALL);
2816 else
2817 gcc_unreachable ();
2819 return NULL;
2823 clear_storage (rtx object, rtx size, enum block_op_methods method)
2825 unsigned HOST_WIDE_INT max, min = 0;
2826 if (GET_CODE (size) == CONST_INT)
2827 min = max = UINTVAL (size);
2828 else
2829 max = GET_MODE_MASK (GET_MODE (size));
2830 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2834 /* A subroutine of clear_storage. Expand a call to memset.
2835 Return the return value of memset, 0 otherwise. */
2838 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2840 tree call_expr, fn, object_tree, size_tree, val_tree;
2841 machine_mode size_mode;
2842 rtx retval;
2844 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2845 place those into new pseudos into a VAR_DECL and use them later. */
2847 object = copy_addr_to_reg (XEXP (object, 0));
2849 size_mode = TYPE_MODE (sizetype);
2850 size = convert_to_mode (size_mode, size, 1);
2851 size = copy_to_mode_reg (size_mode, size);
2853 /* It is incorrect to use the libcall calling conventions to call
2854 memset in this context. This could be a user call to memset and
2855 the user may wish to examine the return value from memset. For
2856 targets where libcalls and normal calls have different conventions
2857 for returning pointers, we could end up generating incorrect code. */
2859 object_tree = make_tree (ptr_type_node, object);
2860 if (!CONST_INT_P (val))
2861 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2862 size_tree = make_tree (sizetype, size);
2863 val_tree = make_tree (integer_type_node, val);
2865 fn = clear_storage_libcall_fn (true);
2866 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2867 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2869 retval = expand_normal (call_expr);
2871 return retval;
2874 /* A subroutine of set_storage_via_libcall. Create the tree node
2875 for the function we use for block clears. */
2877 tree block_clear_fn;
2879 void
2880 init_block_clear_fn (const char *asmspec)
2882 if (!block_clear_fn)
2884 tree fn, args;
2886 fn = get_identifier ("memset");
2887 args = build_function_type_list (ptr_type_node, ptr_type_node,
2888 integer_type_node, sizetype,
2889 NULL_TREE);
2891 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2892 DECL_EXTERNAL (fn) = 1;
2893 TREE_PUBLIC (fn) = 1;
2894 DECL_ARTIFICIAL (fn) = 1;
2895 TREE_NOTHROW (fn) = 1;
2896 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2897 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2899 block_clear_fn = fn;
2902 if (asmspec)
2903 set_user_assembler_name (block_clear_fn, asmspec);
2906 static tree
2907 clear_storage_libcall_fn (int for_call)
2909 static bool emitted_extern;
2911 if (!block_clear_fn)
2912 init_block_clear_fn (NULL);
2914 if (for_call && !emitted_extern)
2916 emitted_extern = true;
2917 make_decl_rtl (block_clear_fn);
2920 return block_clear_fn;
2923 /* Expand a setmem pattern; return true if successful. */
2925 bool
2926 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2927 unsigned int expected_align, HOST_WIDE_INT expected_size,
2928 unsigned HOST_WIDE_INT min_size,
2929 unsigned HOST_WIDE_INT max_size,
2930 unsigned HOST_WIDE_INT probable_max_size)
2932 /* Try the most limited insn first, because there's no point
2933 including more than one in the machine description unless
2934 the more limited one has some advantage. */
2936 machine_mode mode;
2938 if (expected_align < align)
2939 expected_align = align;
2940 if (expected_size != -1)
2942 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2943 expected_size = max_size;
2944 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2945 expected_size = min_size;
2948 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2949 mode = GET_MODE_WIDER_MODE (mode))
2951 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2953 if (code != CODE_FOR_nothing
2954 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2955 here because if SIZE is less than the mode mask, as it is
2956 returned by the macro, it will definitely be less than the
2957 actual mode mask. Since SIZE is within the Pmode address
2958 space, we limit MODE to Pmode. */
2959 && ((CONST_INT_P (size)
2960 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2961 <= (GET_MODE_MASK (mode) >> 1)))
2962 || max_size <= (GET_MODE_MASK (mode) >> 1)
2963 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2965 struct expand_operand ops[9];
2966 unsigned int nops;
2968 nops = insn_data[(int) code].n_generator_args;
2969 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2971 create_fixed_operand (&ops[0], object);
2972 /* The check above guarantees that this size conversion is valid. */
2973 create_convert_operand_to (&ops[1], size, mode, true);
2974 create_convert_operand_from (&ops[2], val, byte_mode, true);
2975 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2976 if (nops >= 6)
2978 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2979 create_integer_operand (&ops[5], expected_size);
2981 if (nops >= 8)
2983 create_integer_operand (&ops[6], min_size);
2984 /* If we can not represent the maximal size,
2985 make parameter NULL. */
2986 if ((HOST_WIDE_INT) max_size != -1)
2987 create_integer_operand (&ops[7], max_size);
2988 else
2989 create_fixed_operand (&ops[7], NULL);
2991 if (nops == 9)
2993 /* If we can not represent the maximal size,
2994 make parameter NULL. */
2995 if ((HOST_WIDE_INT) probable_max_size != -1)
2996 create_integer_operand (&ops[8], probable_max_size);
2997 else
2998 create_fixed_operand (&ops[8], NULL);
3000 if (maybe_expand_insn (code, nops, ops))
3001 return true;
3005 return false;
3009 /* Write to one of the components of the complex value CPLX. Write VAL to
3010 the real part if IMAG_P is false, and the imaginary part if its true. */
3012 void
3013 write_complex_part (rtx cplx, rtx val, bool imag_p)
3015 machine_mode cmode;
3016 machine_mode imode;
3017 unsigned ibitsize;
3019 if (GET_CODE (cplx) == CONCAT)
3021 emit_move_insn (XEXP (cplx, imag_p), val);
3022 return;
3025 cmode = GET_MODE (cplx);
3026 imode = GET_MODE_INNER (cmode);
3027 ibitsize = GET_MODE_BITSIZE (imode);
3029 /* For MEMs simplify_gen_subreg may generate an invalid new address
3030 because, e.g., the original address is considered mode-dependent
3031 by the target, which restricts simplify_subreg from invoking
3032 adjust_address_nv. Instead of preparing fallback support for an
3033 invalid address, we call adjust_address_nv directly. */
3034 if (MEM_P (cplx))
3036 emit_move_insn (adjust_address_nv (cplx, imode,
3037 imag_p ? GET_MODE_SIZE (imode) : 0),
3038 val);
3039 return;
3042 /* If the sub-object is at least word sized, then we know that subregging
3043 will work. This special case is important, since store_bit_field
3044 wants to operate on integer modes, and there's rarely an OImode to
3045 correspond to TCmode. */
3046 if (ibitsize >= BITS_PER_WORD
3047 /* For hard regs we have exact predicates. Assume we can split
3048 the original object if it spans an even number of hard regs.
3049 This special case is important for SCmode on 64-bit platforms
3050 where the natural size of floating-point regs is 32-bit. */
3051 || (REG_P (cplx)
3052 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3053 && REG_NREGS (cplx) % 2 == 0))
3055 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3056 imag_p ? GET_MODE_SIZE (imode) : 0);
3057 if (part)
3059 emit_move_insn (part, val);
3060 return;
3062 else
3063 /* simplify_gen_subreg may fail for sub-word MEMs. */
3064 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3067 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3070 /* Extract one of the components of the complex value CPLX. Extract the
3071 real part if IMAG_P is false, and the imaginary part if it's true. */
3073 static rtx
3074 read_complex_part (rtx cplx, bool imag_p)
3076 machine_mode cmode, imode;
3077 unsigned ibitsize;
3079 if (GET_CODE (cplx) == CONCAT)
3080 return XEXP (cplx, imag_p);
3082 cmode = GET_MODE (cplx);
3083 imode = GET_MODE_INNER (cmode);
3084 ibitsize = GET_MODE_BITSIZE (imode);
3086 /* Special case reads from complex constants that got spilled to memory. */
3087 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3089 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3090 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3092 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3093 if (CONSTANT_CLASS_P (part))
3094 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3098 /* For MEMs simplify_gen_subreg may generate an invalid new address
3099 because, e.g., the original address is considered mode-dependent
3100 by the target, which restricts simplify_subreg from invoking
3101 adjust_address_nv. Instead of preparing fallback support for an
3102 invalid address, we call adjust_address_nv directly. */
3103 if (MEM_P (cplx))
3104 return adjust_address_nv (cplx, imode,
3105 imag_p ? GET_MODE_SIZE (imode) : 0);
3107 /* If the sub-object is at least word sized, then we know that subregging
3108 will work. This special case is important, since extract_bit_field
3109 wants to operate on integer modes, and there's rarely an OImode to
3110 correspond to TCmode. */
3111 if (ibitsize >= BITS_PER_WORD
3112 /* For hard regs we have exact predicates. Assume we can split
3113 the original object if it spans an even number of hard regs.
3114 This special case is important for SCmode on 64-bit platforms
3115 where the natural size of floating-point regs is 32-bit. */
3116 || (REG_P (cplx)
3117 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3118 && REG_NREGS (cplx) % 2 == 0))
3120 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3121 imag_p ? GET_MODE_SIZE (imode) : 0);
3122 if (ret)
3123 return ret;
3124 else
3125 /* simplify_gen_subreg may fail for sub-word MEMs. */
3126 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3129 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3130 true, NULL_RTX, imode, imode);
3133 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3134 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3135 represented in NEW_MODE. If FORCE is true, this will never happen, as
3136 we'll force-create a SUBREG if needed. */
3138 static rtx
3139 emit_move_change_mode (machine_mode new_mode,
3140 machine_mode old_mode, rtx x, bool force)
3142 rtx ret;
3144 if (push_operand (x, GET_MODE (x)))
3146 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3147 MEM_COPY_ATTRIBUTES (ret, x);
3149 else if (MEM_P (x))
3151 /* We don't have to worry about changing the address since the
3152 size in bytes is supposed to be the same. */
3153 if (reload_in_progress)
3155 /* Copy the MEM to change the mode and move any
3156 substitutions from the old MEM to the new one. */
3157 ret = adjust_address_nv (x, new_mode, 0);
3158 copy_replacements (x, ret);
3160 else
3161 ret = adjust_address (x, new_mode, 0);
3163 else
3165 /* Note that we do want simplify_subreg's behavior of validating
3166 that the new mode is ok for a hard register. If we were to use
3167 simplify_gen_subreg, we would create the subreg, but would
3168 probably run into the target not being able to implement it. */
3169 /* Except, of course, when FORCE is true, when this is exactly what
3170 we want. Which is needed for CCmodes on some targets. */
3171 if (force)
3172 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3173 else
3174 ret = simplify_subreg (new_mode, x, old_mode, 0);
3177 return ret;
3180 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3181 an integer mode of the same size as MODE. Returns the instruction
3182 emitted, or NULL if such a move could not be generated. */
3184 static rtx_insn *
3185 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3187 machine_mode imode;
3188 enum insn_code code;
3190 /* There must exist a mode of the exact size we require. */
3191 imode = int_mode_for_mode (mode);
3192 if (imode == BLKmode)
3193 return NULL;
3195 /* The target must support moves in this mode. */
3196 code = optab_handler (mov_optab, imode);
3197 if (code == CODE_FOR_nothing)
3198 return NULL;
3200 x = emit_move_change_mode (imode, mode, x, force);
3201 if (x == NULL_RTX)
3202 return NULL;
3203 y = emit_move_change_mode (imode, mode, y, force);
3204 if (y == NULL_RTX)
3205 return NULL;
3206 return emit_insn (GEN_FCN (code) (x, y));
3209 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3210 Return an equivalent MEM that does not use an auto-increment. */
3213 emit_move_resolve_push (machine_mode mode, rtx x)
3215 enum rtx_code code = GET_CODE (XEXP (x, 0));
3216 HOST_WIDE_INT adjust;
3217 rtx temp;
3219 adjust = GET_MODE_SIZE (mode);
3220 #ifdef PUSH_ROUNDING
3221 adjust = PUSH_ROUNDING (adjust);
3222 #endif
3223 if (code == PRE_DEC || code == POST_DEC)
3224 adjust = -adjust;
3225 else if (code == PRE_MODIFY || code == POST_MODIFY)
3227 rtx expr = XEXP (XEXP (x, 0), 1);
3228 HOST_WIDE_INT val;
3230 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3231 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3232 val = INTVAL (XEXP (expr, 1));
3233 if (GET_CODE (expr) == MINUS)
3234 val = -val;
3235 gcc_assert (adjust == val || adjust == -val);
3236 adjust = val;
3239 /* Do not use anti_adjust_stack, since we don't want to update
3240 stack_pointer_delta. */
3241 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3242 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3243 0, OPTAB_LIB_WIDEN);
3244 if (temp != stack_pointer_rtx)
3245 emit_move_insn (stack_pointer_rtx, temp);
3247 switch (code)
3249 case PRE_INC:
3250 case PRE_DEC:
3251 case PRE_MODIFY:
3252 temp = stack_pointer_rtx;
3253 break;
3254 case POST_INC:
3255 case POST_DEC:
3256 case POST_MODIFY:
3257 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3258 break;
3259 default:
3260 gcc_unreachable ();
3263 return replace_equiv_address (x, temp);
3266 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3267 X is known to satisfy push_operand, and MODE is known to be complex.
3268 Returns the last instruction emitted. */
3270 rtx_insn *
3271 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3273 machine_mode submode = GET_MODE_INNER (mode);
3274 bool imag_first;
3276 #ifdef PUSH_ROUNDING
3277 unsigned int submodesize = GET_MODE_SIZE (submode);
3279 /* In case we output to the stack, but the size is smaller than the
3280 machine can push exactly, we need to use move instructions. */
3281 if (PUSH_ROUNDING (submodesize) != submodesize)
3283 x = emit_move_resolve_push (mode, x);
3284 return emit_move_insn (x, y);
3286 #endif
3288 /* Note that the real part always precedes the imag part in memory
3289 regardless of machine's endianness. */
3290 switch (GET_CODE (XEXP (x, 0)))
3292 case PRE_DEC:
3293 case POST_DEC:
3294 imag_first = true;
3295 break;
3296 case PRE_INC:
3297 case POST_INC:
3298 imag_first = false;
3299 break;
3300 default:
3301 gcc_unreachable ();
3304 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3305 read_complex_part (y, imag_first));
3306 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3307 read_complex_part (y, !imag_first));
3310 /* A subroutine of emit_move_complex. Perform the move from Y to X
3311 via two moves of the parts. Returns the last instruction emitted. */
3313 rtx_insn *
3314 emit_move_complex_parts (rtx x, rtx y)
3316 /* Show the output dies here. This is necessary for SUBREGs
3317 of pseudos since we cannot track their lifetimes correctly;
3318 hard regs shouldn't appear here except as return values. */
3319 if (!reload_completed && !reload_in_progress
3320 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3321 emit_clobber (x);
3323 write_complex_part (x, read_complex_part (y, false), false);
3324 write_complex_part (x, read_complex_part (y, true), true);
3326 return get_last_insn ();
3329 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3330 MODE is known to be complex. Returns the last instruction emitted. */
3332 static rtx_insn *
3333 emit_move_complex (machine_mode mode, rtx x, rtx y)
3335 bool try_int;
3337 /* Need to take special care for pushes, to maintain proper ordering
3338 of the data, and possibly extra padding. */
3339 if (push_operand (x, mode))
3340 return emit_move_complex_push (mode, x, y);
3342 /* See if we can coerce the target into moving both values at once, except
3343 for floating point where we favor moving as parts if this is easy. */
3344 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3345 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3346 && !(REG_P (x)
3347 && HARD_REGISTER_P (x)
3348 && REG_NREGS (x) == 1)
3349 && !(REG_P (y)
3350 && HARD_REGISTER_P (y)
3351 && REG_NREGS (y) == 1))
3352 try_int = false;
3353 /* Not possible if the values are inherently not adjacent. */
3354 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3355 try_int = false;
3356 /* Is possible if both are registers (or subregs of registers). */
3357 else if (register_operand (x, mode) && register_operand (y, mode))
3358 try_int = true;
3359 /* If one of the operands is a memory, and alignment constraints
3360 are friendly enough, we may be able to do combined memory operations.
3361 We do not attempt this if Y is a constant because that combination is
3362 usually better with the by-parts thing below. */
3363 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3364 && (!STRICT_ALIGNMENT
3365 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3366 try_int = true;
3367 else
3368 try_int = false;
3370 if (try_int)
3372 rtx_insn *ret;
3374 /* For memory to memory moves, optimal behavior can be had with the
3375 existing block move logic. */
3376 if (MEM_P (x) && MEM_P (y))
3378 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3379 BLOCK_OP_NO_LIBCALL);
3380 return get_last_insn ();
3383 ret = emit_move_via_integer (mode, x, y, true);
3384 if (ret)
3385 return ret;
3388 return emit_move_complex_parts (x, y);
3391 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3392 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3394 static rtx_insn *
3395 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3397 rtx_insn *ret;
3399 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3400 if (mode != CCmode)
3402 enum insn_code code = optab_handler (mov_optab, CCmode);
3403 if (code != CODE_FOR_nothing)
3405 x = emit_move_change_mode (CCmode, mode, x, true);
3406 y = emit_move_change_mode (CCmode, mode, y, true);
3407 return emit_insn (GEN_FCN (code) (x, y));
3411 /* Otherwise, find the MODE_INT mode of the same width. */
3412 ret = emit_move_via_integer (mode, x, y, false);
3413 gcc_assert (ret != NULL);
3414 return ret;
3417 /* Return true if word I of OP lies entirely in the
3418 undefined bits of a paradoxical subreg. */
3420 static bool
3421 undefined_operand_subword_p (const_rtx op, int i)
3423 machine_mode innermode, innermostmode;
3424 int offset;
3425 if (GET_CODE (op) != SUBREG)
3426 return false;
3427 innermode = GET_MODE (op);
3428 innermostmode = GET_MODE (SUBREG_REG (op));
3429 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3430 /* The SUBREG_BYTE represents offset, as if the value were stored in
3431 memory, except for a paradoxical subreg where we define
3432 SUBREG_BYTE to be 0; undo this exception as in
3433 simplify_subreg. */
3434 if (SUBREG_BYTE (op) == 0
3435 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3437 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3438 if (WORDS_BIG_ENDIAN)
3439 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3440 if (BYTES_BIG_ENDIAN)
3441 offset += difference % UNITS_PER_WORD;
3443 if (offset >= GET_MODE_SIZE (innermostmode)
3444 || offset <= -GET_MODE_SIZE (word_mode))
3445 return true;
3446 return false;
3449 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3450 MODE is any multi-word or full-word mode that lacks a move_insn
3451 pattern. Note that you will get better code if you define such
3452 patterns, even if they must turn into multiple assembler instructions. */
3454 static rtx_insn *
3455 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3457 rtx_insn *last_insn = 0;
3458 rtx_insn *seq;
3459 rtx inner;
3460 bool need_clobber;
3461 int i;
3463 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3465 /* If X is a push on the stack, do the push now and replace
3466 X with a reference to the stack pointer. */
3467 if (push_operand (x, mode))
3468 x = emit_move_resolve_push (mode, x);
3470 /* If we are in reload, see if either operand is a MEM whose address
3471 is scheduled for replacement. */
3472 if (reload_in_progress && MEM_P (x)
3473 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3474 x = replace_equiv_address_nv (x, inner);
3475 if (reload_in_progress && MEM_P (y)
3476 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3477 y = replace_equiv_address_nv (y, inner);
3479 start_sequence ();
3481 need_clobber = false;
3482 for (i = 0;
3483 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3484 i++)
3486 rtx xpart = operand_subword (x, i, 1, mode);
3487 rtx ypart;
3489 /* Do not generate code for a move if it would come entirely
3490 from the undefined bits of a paradoxical subreg. */
3491 if (undefined_operand_subword_p (y, i))
3492 continue;
3494 ypart = operand_subword (y, i, 1, mode);
3496 /* If we can't get a part of Y, put Y into memory if it is a
3497 constant. Otherwise, force it into a register. Then we must
3498 be able to get a part of Y. */
3499 if (ypart == 0 && CONSTANT_P (y))
3501 y = use_anchored_address (force_const_mem (mode, y));
3502 ypart = operand_subword (y, i, 1, mode);
3504 else if (ypart == 0)
3505 ypart = operand_subword_force (y, i, mode);
3507 gcc_assert (xpart && ypart);
3509 need_clobber |= (GET_CODE (xpart) == SUBREG);
3511 last_insn = emit_move_insn (xpart, ypart);
3514 seq = get_insns ();
3515 end_sequence ();
3517 /* Show the output dies here. This is necessary for SUBREGs
3518 of pseudos since we cannot track their lifetimes correctly;
3519 hard regs shouldn't appear here except as return values.
3520 We never want to emit such a clobber after reload. */
3521 if (x != y
3522 && ! (reload_in_progress || reload_completed)
3523 && need_clobber != 0)
3524 emit_clobber (x);
3526 emit_insn (seq);
3528 return last_insn;
3531 /* Low level part of emit_move_insn.
3532 Called just like emit_move_insn, but assumes X and Y
3533 are basically valid. */
3535 rtx_insn *
3536 emit_move_insn_1 (rtx x, rtx y)
3538 machine_mode mode = GET_MODE (x);
3539 enum insn_code code;
3541 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3543 code = optab_handler (mov_optab, mode);
3544 if (code != CODE_FOR_nothing)
3545 return emit_insn (GEN_FCN (code) (x, y));
3547 /* Expand complex moves by moving real part and imag part. */
3548 if (COMPLEX_MODE_P (mode))
3549 return emit_move_complex (mode, x, y);
3551 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3552 || ALL_FIXED_POINT_MODE_P (mode))
3554 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3556 /* If we can't find an integer mode, use multi words. */
3557 if (result)
3558 return result;
3559 else
3560 return emit_move_multi_word (mode, x, y);
3563 if (GET_MODE_CLASS (mode) == MODE_CC)
3564 return emit_move_ccmode (mode, x, y);
3566 /* Try using a move pattern for the corresponding integer mode. This is
3567 only safe when simplify_subreg can convert MODE constants into integer
3568 constants. At present, it can only do this reliably if the value
3569 fits within a HOST_WIDE_INT. */
3570 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3572 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3574 if (ret)
3576 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3577 return ret;
3581 return emit_move_multi_word (mode, x, y);
3584 /* Generate code to copy Y into X.
3585 Both Y and X must have the same mode, except that
3586 Y can be a constant with VOIDmode.
3587 This mode cannot be BLKmode; use emit_block_move for that.
3589 Return the last instruction emitted. */
3591 rtx_insn *
3592 emit_move_insn (rtx x, rtx y)
3594 machine_mode mode = GET_MODE (x);
3595 rtx y_cst = NULL_RTX;
3596 rtx_insn *last_insn;
3597 rtx set;
3599 gcc_assert (mode != BLKmode
3600 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3602 if (CONSTANT_P (y))
3604 if (optimize
3605 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3606 && (last_insn = compress_float_constant (x, y)))
3607 return last_insn;
3609 y_cst = y;
3611 if (!targetm.legitimate_constant_p (mode, y))
3613 y = force_const_mem (mode, y);
3615 /* If the target's cannot_force_const_mem prevented the spill,
3616 assume that the target's move expanders will also take care
3617 of the non-legitimate constant. */
3618 if (!y)
3619 y = y_cst;
3620 else
3621 y = use_anchored_address (y);
3625 /* If X or Y are memory references, verify that their addresses are valid
3626 for the machine. */
3627 if (MEM_P (x)
3628 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3629 MEM_ADDR_SPACE (x))
3630 && ! push_operand (x, GET_MODE (x))))
3631 x = validize_mem (x);
3633 if (MEM_P (y)
3634 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3635 MEM_ADDR_SPACE (y)))
3636 y = validize_mem (y);
3638 gcc_assert (mode != BLKmode);
3640 last_insn = emit_move_insn_1 (x, y);
3642 if (y_cst && REG_P (x)
3643 && (set = single_set (last_insn)) != NULL_RTX
3644 && SET_DEST (set) == x
3645 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3646 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3648 return last_insn;
3651 /* Generate the body of an instruction to copy Y into X.
3652 It may be a list of insns, if one insn isn't enough. */
3654 rtx_insn *
3655 gen_move_insn (rtx x, rtx y)
3657 rtx_insn *seq;
3659 start_sequence ();
3660 emit_move_insn_1 (x, y);
3661 seq = get_insns ();
3662 end_sequence ();
3663 return seq;
3666 /* Same as above, but return rtx (used as a callback, which must have
3667 prototype compatible with other functions returning rtx). */
3670 gen_move_insn_uncast (rtx x, rtx y)
3672 return gen_move_insn (x, y);
3675 /* If Y is representable exactly in a narrower mode, and the target can
3676 perform the extension directly from constant or memory, then emit the
3677 move as an extension. */
3679 static rtx_insn *
3680 compress_float_constant (rtx x, rtx y)
3682 machine_mode dstmode = GET_MODE (x);
3683 machine_mode orig_srcmode = GET_MODE (y);
3684 machine_mode srcmode;
3685 REAL_VALUE_TYPE r;
3686 int oldcost, newcost;
3687 bool speed = optimize_insn_for_speed_p ();
3689 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3691 if (targetm.legitimate_constant_p (dstmode, y))
3692 oldcost = set_src_cost (y, speed);
3693 else
3694 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3696 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3697 srcmode != orig_srcmode;
3698 srcmode = GET_MODE_WIDER_MODE (srcmode))
3700 enum insn_code ic;
3701 rtx trunc_y;
3702 rtx_insn *last_insn;
3704 /* Skip if the target can't extend this way. */
3705 ic = can_extend_p (dstmode, srcmode, 0);
3706 if (ic == CODE_FOR_nothing)
3707 continue;
3709 /* Skip if the narrowed value isn't exact. */
3710 if (! exact_real_truncate (srcmode, &r))
3711 continue;
3713 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3715 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3717 /* Skip if the target needs extra instructions to perform
3718 the extension. */
3719 if (!insn_operand_matches (ic, 1, trunc_y))
3720 continue;
3721 /* This is valid, but may not be cheaper than the original. */
3722 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3723 speed);
3724 if (oldcost < newcost)
3725 continue;
3727 else if (float_extend_from_mem[dstmode][srcmode])
3729 trunc_y = force_const_mem (srcmode, trunc_y);
3730 /* This is valid, but may not be cheaper than the original. */
3731 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3732 speed);
3733 if (oldcost < newcost)
3734 continue;
3735 trunc_y = validize_mem (trunc_y);
3737 else
3738 continue;
3740 /* For CSE's benefit, force the compressed constant pool entry
3741 into a new pseudo. This constant may be used in different modes,
3742 and if not, combine will put things back together for us. */
3743 trunc_y = force_reg (srcmode, trunc_y);
3745 /* If x is a hard register, perform the extension into a pseudo,
3746 so that e.g. stack realignment code is aware of it. */
3747 rtx target = x;
3748 if (REG_P (x) && HARD_REGISTER_P (x))
3749 target = gen_reg_rtx (dstmode);
3751 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3752 last_insn = get_last_insn ();
3754 if (REG_P (target))
3755 set_unique_reg_note (last_insn, REG_EQUAL, y);
3757 if (target != x)
3758 return emit_move_insn (x, target);
3759 return last_insn;
3762 return NULL;
3765 /* Pushing data onto the stack. */
3767 /* Push a block of length SIZE (perhaps variable)
3768 and return an rtx to address the beginning of the block.
3769 The value may be virtual_outgoing_args_rtx.
3771 EXTRA is the number of bytes of padding to push in addition to SIZE.
3772 BELOW nonzero means this padding comes at low addresses;
3773 otherwise, the padding comes at high addresses. */
3776 push_block (rtx size, int extra, int below)
3778 rtx temp;
3780 size = convert_modes (Pmode, ptr_mode, size, 1);
3781 if (CONSTANT_P (size))
3782 anti_adjust_stack (plus_constant (Pmode, size, extra));
3783 else if (REG_P (size) && extra == 0)
3784 anti_adjust_stack (size);
3785 else
3787 temp = copy_to_mode_reg (Pmode, size);
3788 if (extra != 0)
3789 temp = expand_binop (Pmode, add_optab, temp,
3790 gen_int_mode (extra, Pmode),
3791 temp, 0, OPTAB_LIB_WIDEN);
3792 anti_adjust_stack (temp);
3795 #ifndef STACK_GROWS_DOWNWARD
3796 if (0)
3797 #else
3798 if (1)
3799 #endif
3801 temp = virtual_outgoing_args_rtx;
3802 if (extra != 0 && below)
3803 temp = plus_constant (Pmode, temp, extra);
3805 else
3807 if (CONST_INT_P (size))
3808 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3809 -INTVAL (size) - (below ? 0 : extra));
3810 else if (extra != 0 && !below)
3811 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3812 negate_rtx (Pmode, plus_constant (Pmode, size,
3813 extra)));
3814 else
3815 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3816 negate_rtx (Pmode, size));
3819 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3822 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3824 static rtx
3825 mem_autoinc_base (rtx mem)
3827 if (MEM_P (mem))
3829 rtx addr = XEXP (mem, 0);
3830 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3831 return XEXP (addr, 0);
3833 return NULL;
3836 /* A utility routine used here, in reload, and in try_split. The insns
3837 after PREV up to and including LAST are known to adjust the stack,
3838 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3839 placing notes as appropriate. PREV may be NULL, indicating the
3840 entire insn sequence prior to LAST should be scanned.
3842 The set of allowed stack pointer modifications is small:
3843 (1) One or more auto-inc style memory references (aka pushes),
3844 (2) One or more addition/subtraction with the SP as destination,
3845 (3) A single move insn with the SP as destination,
3846 (4) A call_pop insn,
3847 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3849 Insns in the sequence that do not modify the SP are ignored,
3850 except for noreturn calls.
3852 The return value is the amount of adjustment that can be trivially
3853 verified, via immediate operand or auto-inc. If the adjustment
3854 cannot be trivially extracted, the return value is INT_MIN. */
3856 HOST_WIDE_INT
3857 find_args_size_adjust (rtx_insn *insn)
3859 rtx dest, set, pat;
3860 int i;
3862 pat = PATTERN (insn);
3863 set = NULL;
3865 /* Look for a call_pop pattern. */
3866 if (CALL_P (insn))
3868 /* We have to allow non-call_pop patterns for the case
3869 of emit_single_push_insn of a TLS address. */
3870 if (GET_CODE (pat) != PARALLEL)
3871 return 0;
3873 /* All call_pop have a stack pointer adjust in the parallel.
3874 The call itself is always first, and the stack adjust is
3875 usually last, so search from the end. */
3876 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3878 set = XVECEXP (pat, 0, i);
3879 if (GET_CODE (set) != SET)
3880 continue;
3881 dest = SET_DEST (set);
3882 if (dest == stack_pointer_rtx)
3883 break;
3885 /* We'd better have found the stack pointer adjust. */
3886 if (i == 0)
3887 return 0;
3888 /* Fall through to process the extracted SET and DEST
3889 as if it was a standalone insn. */
3891 else if (GET_CODE (pat) == SET)
3892 set = pat;
3893 else if ((set = single_set (insn)) != NULL)
3895 else if (GET_CODE (pat) == PARALLEL)
3897 /* ??? Some older ports use a parallel with a stack adjust
3898 and a store for a PUSH_ROUNDING pattern, rather than a
3899 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3900 /* ??? See h8300 and m68k, pushqi1. */
3901 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3903 set = XVECEXP (pat, 0, i);
3904 if (GET_CODE (set) != SET)
3905 continue;
3906 dest = SET_DEST (set);
3907 if (dest == stack_pointer_rtx)
3908 break;
3910 /* We do not expect an auto-inc of the sp in the parallel. */
3911 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3912 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3913 != stack_pointer_rtx);
3915 if (i < 0)
3916 return 0;
3918 else
3919 return 0;
3921 dest = SET_DEST (set);
3923 /* Look for direct modifications of the stack pointer. */
3924 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3926 /* Look for a trivial adjustment, otherwise assume nothing. */
3927 /* Note that the SPU restore_stack_block pattern refers to
3928 the stack pointer in V4SImode. Consider that non-trivial. */
3929 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3930 && GET_CODE (SET_SRC (set)) == PLUS
3931 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3932 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3933 return INTVAL (XEXP (SET_SRC (set), 1));
3934 /* ??? Reload can generate no-op moves, which will be cleaned
3935 up later. Recognize it and continue searching. */
3936 else if (rtx_equal_p (dest, SET_SRC (set)))
3937 return 0;
3938 else
3939 return HOST_WIDE_INT_MIN;
3941 else
3943 rtx mem, addr;
3945 /* Otherwise only think about autoinc patterns. */
3946 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3948 mem = dest;
3949 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3950 != stack_pointer_rtx);
3952 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3953 mem = SET_SRC (set);
3954 else
3955 return 0;
3957 addr = XEXP (mem, 0);
3958 switch (GET_CODE (addr))
3960 case PRE_INC:
3961 case POST_INC:
3962 return GET_MODE_SIZE (GET_MODE (mem));
3963 case PRE_DEC:
3964 case POST_DEC:
3965 return -GET_MODE_SIZE (GET_MODE (mem));
3966 case PRE_MODIFY:
3967 case POST_MODIFY:
3968 addr = XEXP (addr, 1);
3969 gcc_assert (GET_CODE (addr) == PLUS);
3970 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3971 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3972 return INTVAL (XEXP (addr, 1));
3973 default:
3974 gcc_unreachable ();
3980 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3982 int args_size = end_args_size;
3983 bool saw_unknown = false;
3984 rtx_insn *insn;
3986 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3988 HOST_WIDE_INT this_delta;
3990 if (!NONDEBUG_INSN_P (insn))
3991 continue;
3993 this_delta = find_args_size_adjust (insn);
3994 if (this_delta == 0)
3996 if (!CALL_P (insn)
3997 || ACCUMULATE_OUTGOING_ARGS
3998 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3999 continue;
4002 gcc_assert (!saw_unknown);
4003 if (this_delta == HOST_WIDE_INT_MIN)
4004 saw_unknown = true;
4006 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4007 #ifdef STACK_GROWS_DOWNWARD
4008 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4009 #endif
4010 args_size -= this_delta;
4013 return saw_unknown ? INT_MIN : args_size;
4016 #ifdef PUSH_ROUNDING
4017 /* Emit single push insn. */
4019 static void
4020 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4022 rtx dest_addr;
4023 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4024 rtx dest;
4025 enum insn_code icode;
4027 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4028 /* If there is push pattern, use it. Otherwise try old way of throwing
4029 MEM representing push operation to move expander. */
4030 icode = optab_handler (push_optab, mode);
4031 if (icode != CODE_FOR_nothing)
4033 struct expand_operand ops[1];
4035 create_input_operand (&ops[0], x, mode);
4036 if (maybe_expand_insn (icode, 1, ops))
4037 return;
4039 if (GET_MODE_SIZE (mode) == rounded_size)
4040 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4041 /* If we are to pad downward, adjust the stack pointer first and
4042 then store X into the stack location using an offset. This is
4043 because emit_move_insn does not know how to pad; it does not have
4044 access to type. */
4045 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4047 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4048 HOST_WIDE_INT offset;
4050 emit_move_insn (stack_pointer_rtx,
4051 expand_binop (Pmode,
4052 #ifdef STACK_GROWS_DOWNWARD
4053 sub_optab,
4054 #else
4055 add_optab,
4056 #endif
4057 stack_pointer_rtx,
4058 gen_int_mode (rounded_size, Pmode),
4059 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4061 offset = (HOST_WIDE_INT) padding_size;
4062 #ifdef STACK_GROWS_DOWNWARD
4063 if (STACK_PUSH_CODE == POST_DEC)
4064 /* We have already decremented the stack pointer, so get the
4065 previous value. */
4066 offset += (HOST_WIDE_INT) rounded_size;
4067 #else
4068 if (STACK_PUSH_CODE == POST_INC)
4069 /* We have already incremented the stack pointer, so get the
4070 previous value. */
4071 offset -= (HOST_WIDE_INT) rounded_size;
4072 #endif
4073 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4074 gen_int_mode (offset, Pmode));
4076 else
4078 #ifdef STACK_GROWS_DOWNWARD
4079 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4080 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4081 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4082 Pmode));
4083 #else
4084 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4085 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4086 gen_int_mode (rounded_size, Pmode));
4087 #endif
4088 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4091 dest = gen_rtx_MEM (mode, dest_addr);
4093 if (type != 0)
4095 set_mem_attributes (dest, type, 1);
4097 if (cfun->tail_call_marked)
4098 /* Function incoming arguments may overlap with sibling call
4099 outgoing arguments and we cannot allow reordering of reads
4100 from function arguments with stores to outgoing arguments
4101 of sibling calls. */
4102 set_mem_alias_set (dest, 0);
4104 emit_move_insn (dest, x);
4107 /* Emit and annotate a single push insn. */
4109 static void
4110 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4112 int delta, old_delta = stack_pointer_delta;
4113 rtx_insn *prev = get_last_insn ();
4114 rtx_insn *last;
4116 emit_single_push_insn_1 (mode, x, type);
4118 last = get_last_insn ();
4120 /* Notice the common case where we emitted exactly one insn. */
4121 if (PREV_INSN (last) == prev)
4123 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4124 return;
4127 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4128 gcc_assert (delta == INT_MIN || delta == old_delta);
4130 #endif
4132 /* Generate code to push X onto the stack, assuming it has mode MODE and
4133 type TYPE.
4134 MODE is redundant except when X is a CONST_INT (since they don't
4135 carry mode info).
4136 SIZE is an rtx for the size of data to be copied (in bytes),
4137 needed only if X is BLKmode.
4139 ALIGN (in bits) is maximum alignment we can assume.
4141 If PARTIAL and REG are both nonzero, then copy that many of the first
4142 bytes of X into registers starting with REG, and push the rest of X.
4143 The amount of space pushed is decreased by PARTIAL bytes.
4144 REG must be a hard register in this case.
4145 If REG is zero but PARTIAL is not, take any all others actions for an
4146 argument partially in registers, but do not actually load any
4147 registers.
4149 EXTRA is the amount in bytes of extra space to leave next to this arg.
4150 This is ignored if an argument block has already been allocated.
4152 On a machine that lacks real push insns, ARGS_ADDR is the address of
4153 the bottom of the argument block for this call. We use indexing off there
4154 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4155 argument block has not been preallocated.
4157 ARGS_SO_FAR is the size of args previously pushed for this call.
4159 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4160 for arguments passed in registers. If nonzero, it will be the number
4161 of bytes required. */
4163 void
4164 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4165 unsigned int align, int partial, rtx reg, int extra,
4166 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4167 rtx alignment_pad)
4169 rtx xinner;
4170 enum direction stack_direction
4171 #ifdef STACK_GROWS_DOWNWARD
4172 = downward;
4173 #else
4174 = upward;
4175 #endif
4177 /* Decide where to pad the argument: `downward' for below,
4178 `upward' for above, or `none' for don't pad it.
4179 Default is below for small data on big-endian machines; else above. */
4180 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4182 /* Invert direction if stack is post-decrement.
4183 FIXME: why? */
4184 if (STACK_PUSH_CODE == POST_DEC)
4185 if (where_pad != none)
4186 where_pad = (where_pad == downward ? upward : downward);
4188 xinner = x;
4190 if (mode == BLKmode
4191 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4193 /* Copy a block into the stack, entirely or partially. */
4195 rtx temp;
4196 int used;
4197 int offset;
4198 int skip;
4200 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4201 used = partial - offset;
4203 if (mode != BLKmode)
4205 /* A value is to be stored in an insufficiently aligned
4206 stack slot; copy via a suitably aligned slot if
4207 necessary. */
4208 size = GEN_INT (GET_MODE_SIZE (mode));
4209 if (!MEM_P (xinner))
4211 temp = assign_temp (type, 1, 1);
4212 emit_move_insn (temp, xinner);
4213 xinner = temp;
4217 gcc_assert (size);
4219 /* USED is now the # of bytes we need not copy to the stack
4220 because registers will take care of them. */
4222 if (partial != 0)
4223 xinner = adjust_address (xinner, BLKmode, used);
4225 /* If the partial register-part of the arg counts in its stack size,
4226 skip the part of stack space corresponding to the registers.
4227 Otherwise, start copying to the beginning of the stack space,
4228 by setting SKIP to 0. */
4229 skip = (reg_parm_stack_space == 0) ? 0 : used;
4231 #ifdef PUSH_ROUNDING
4232 /* Do it with several push insns if that doesn't take lots of insns
4233 and if there is no difficulty with push insns that skip bytes
4234 on the stack for alignment purposes. */
4235 if (args_addr == 0
4236 && PUSH_ARGS
4237 && CONST_INT_P (size)
4238 && skip == 0
4239 && MEM_ALIGN (xinner) >= align
4240 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4241 /* Here we avoid the case of a structure whose weak alignment
4242 forces many pushes of a small amount of data,
4243 and such small pushes do rounding that causes trouble. */
4244 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4245 || align >= BIGGEST_ALIGNMENT
4246 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4247 == (align / BITS_PER_UNIT)))
4248 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4250 /* Push padding now if padding above and stack grows down,
4251 or if padding below and stack grows up.
4252 But if space already allocated, this has already been done. */
4253 if (extra && args_addr == 0
4254 && where_pad != none && where_pad != stack_direction)
4255 anti_adjust_stack (GEN_INT (extra));
4257 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4259 else
4260 #endif /* PUSH_ROUNDING */
4262 rtx target;
4264 /* Otherwise make space on the stack and copy the data
4265 to the address of that space. */
4267 /* Deduct words put into registers from the size we must copy. */
4268 if (partial != 0)
4270 if (CONST_INT_P (size))
4271 size = GEN_INT (INTVAL (size) - used);
4272 else
4273 size = expand_binop (GET_MODE (size), sub_optab, size,
4274 gen_int_mode (used, GET_MODE (size)),
4275 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4278 /* Get the address of the stack space.
4279 In this case, we do not deal with EXTRA separately.
4280 A single stack adjust will do. */
4281 if (! args_addr)
4283 temp = push_block (size, extra, where_pad == downward);
4284 extra = 0;
4286 else if (CONST_INT_P (args_so_far))
4287 temp = memory_address (BLKmode,
4288 plus_constant (Pmode, args_addr,
4289 skip + INTVAL (args_so_far)));
4290 else
4291 temp = memory_address (BLKmode,
4292 plus_constant (Pmode,
4293 gen_rtx_PLUS (Pmode,
4294 args_addr,
4295 args_so_far),
4296 skip));
4298 if (!ACCUMULATE_OUTGOING_ARGS)
4300 /* If the source is referenced relative to the stack pointer,
4301 copy it to another register to stabilize it. We do not need
4302 to do this if we know that we won't be changing sp. */
4304 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4305 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4306 temp = copy_to_reg (temp);
4309 target = gen_rtx_MEM (BLKmode, temp);
4311 /* We do *not* set_mem_attributes here, because incoming arguments
4312 may overlap with sibling call outgoing arguments and we cannot
4313 allow reordering of reads from function arguments with stores
4314 to outgoing arguments of sibling calls. We do, however, want
4315 to record the alignment of the stack slot. */
4316 /* ALIGN may well be better aligned than TYPE, e.g. due to
4317 PARM_BOUNDARY. Assume the caller isn't lying. */
4318 set_mem_align (target, align);
4320 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4323 else if (partial > 0)
4325 /* Scalar partly in registers. */
4327 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4328 int i;
4329 int not_stack;
4330 /* # bytes of start of argument
4331 that we must make space for but need not store. */
4332 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4333 int args_offset = INTVAL (args_so_far);
4334 int skip;
4336 /* Push padding now if padding above and stack grows down,
4337 or if padding below and stack grows up.
4338 But if space already allocated, this has already been done. */
4339 if (extra && args_addr == 0
4340 && where_pad != none && where_pad != stack_direction)
4341 anti_adjust_stack (GEN_INT (extra));
4343 /* If we make space by pushing it, we might as well push
4344 the real data. Otherwise, we can leave OFFSET nonzero
4345 and leave the space uninitialized. */
4346 if (args_addr == 0)
4347 offset = 0;
4349 /* Now NOT_STACK gets the number of words that we don't need to
4350 allocate on the stack. Convert OFFSET to words too. */
4351 not_stack = (partial - offset) / UNITS_PER_WORD;
4352 offset /= UNITS_PER_WORD;
4354 /* If the partial register-part of the arg counts in its stack size,
4355 skip the part of stack space corresponding to the registers.
4356 Otherwise, start copying to the beginning of the stack space,
4357 by setting SKIP to 0. */
4358 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4360 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4361 x = validize_mem (force_const_mem (mode, x));
4363 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4364 SUBREGs of such registers are not allowed. */
4365 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4366 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4367 x = copy_to_reg (x);
4369 /* Loop over all the words allocated on the stack for this arg. */
4370 /* We can do it by words, because any scalar bigger than a word
4371 has a size a multiple of a word. */
4372 for (i = size - 1; i >= not_stack; i--)
4373 if (i >= not_stack + offset)
4374 emit_push_insn (operand_subword_force (x, i, mode),
4375 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4376 0, args_addr,
4377 GEN_INT (args_offset + ((i - not_stack + skip)
4378 * UNITS_PER_WORD)),
4379 reg_parm_stack_space, alignment_pad);
4381 else
4383 rtx addr;
4384 rtx dest;
4386 /* Push padding now if padding above and stack grows down,
4387 or if padding below and stack grows up.
4388 But if space already allocated, this has already been done. */
4389 if (extra && args_addr == 0
4390 && where_pad != none && where_pad != stack_direction)
4391 anti_adjust_stack (GEN_INT (extra));
4393 #ifdef PUSH_ROUNDING
4394 if (args_addr == 0 && PUSH_ARGS)
4395 emit_single_push_insn (mode, x, type);
4396 else
4397 #endif
4399 if (CONST_INT_P (args_so_far))
4400 addr
4401 = memory_address (mode,
4402 plus_constant (Pmode, args_addr,
4403 INTVAL (args_so_far)));
4404 else
4405 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4406 args_so_far));
4407 dest = gen_rtx_MEM (mode, addr);
4409 /* We do *not* set_mem_attributes here, because incoming arguments
4410 may overlap with sibling call outgoing arguments and we cannot
4411 allow reordering of reads from function arguments with stores
4412 to outgoing arguments of sibling calls. We do, however, want
4413 to record the alignment of the stack slot. */
4414 /* ALIGN may well be better aligned than TYPE, e.g. due to
4415 PARM_BOUNDARY. Assume the caller isn't lying. */
4416 set_mem_align (dest, align);
4418 emit_move_insn (dest, x);
4422 /* If part should go in registers, copy that part
4423 into the appropriate registers. Do this now, at the end,
4424 since mem-to-mem copies above may do function calls. */
4425 if (partial > 0 && reg != 0)
4427 /* Handle calls that pass values in multiple non-contiguous locations.
4428 The Irix 6 ABI has examples of this. */
4429 if (GET_CODE (reg) == PARALLEL)
4430 emit_group_load (reg, x, type, -1);
4431 else
4433 gcc_assert (partial % UNITS_PER_WORD == 0);
4434 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4438 if (extra && args_addr == 0 && where_pad == stack_direction)
4439 anti_adjust_stack (GEN_INT (extra));
4441 if (alignment_pad && args_addr == 0)
4442 anti_adjust_stack (alignment_pad);
4445 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4446 operations. */
4448 static rtx
4449 get_subtarget (rtx x)
4451 return (optimize
4452 || x == 0
4453 /* Only registers can be subtargets. */
4454 || !REG_P (x)
4455 /* Don't use hard regs to avoid extending their life. */
4456 || REGNO (x) < FIRST_PSEUDO_REGISTER
4457 ? 0 : x);
4460 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4461 FIELD is a bitfield. Returns true if the optimization was successful,
4462 and there's nothing else to do. */
4464 static bool
4465 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4466 unsigned HOST_WIDE_INT bitpos,
4467 unsigned HOST_WIDE_INT bitregion_start,
4468 unsigned HOST_WIDE_INT bitregion_end,
4469 machine_mode mode1, rtx str_rtx,
4470 tree to, tree src)
4472 machine_mode str_mode = GET_MODE (str_rtx);
4473 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4474 tree op0, op1;
4475 rtx value, result;
4476 optab binop;
4477 gimple srcstmt;
4478 enum tree_code code;
4480 if (mode1 != VOIDmode
4481 || bitsize >= BITS_PER_WORD
4482 || str_bitsize > BITS_PER_WORD
4483 || TREE_SIDE_EFFECTS (to)
4484 || TREE_THIS_VOLATILE (to))
4485 return false;
4487 STRIP_NOPS (src);
4488 if (TREE_CODE (src) != SSA_NAME)
4489 return false;
4490 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4491 return false;
4493 srcstmt = get_gimple_for_ssa_name (src);
4494 if (!srcstmt
4495 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4496 return false;
4498 code = gimple_assign_rhs_code (srcstmt);
4500 op0 = gimple_assign_rhs1 (srcstmt);
4502 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4503 to find its initialization. Hopefully the initialization will
4504 be from a bitfield load. */
4505 if (TREE_CODE (op0) == SSA_NAME)
4507 gimple op0stmt = get_gimple_for_ssa_name (op0);
4509 /* We want to eventually have OP0 be the same as TO, which
4510 should be a bitfield. */
4511 if (!op0stmt
4512 || !is_gimple_assign (op0stmt)
4513 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4514 return false;
4515 op0 = gimple_assign_rhs1 (op0stmt);
4518 op1 = gimple_assign_rhs2 (srcstmt);
4520 if (!operand_equal_p (to, op0, 0))
4521 return false;
4523 if (MEM_P (str_rtx))
4525 unsigned HOST_WIDE_INT offset1;
4527 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4528 str_mode = word_mode;
4529 str_mode = get_best_mode (bitsize, bitpos,
4530 bitregion_start, bitregion_end,
4531 MEM_ALIGN (str_rtx), str_mode, 0);
4532 if (str_mode == VOIDmode)
4533 return false;
4534 str_bitsize = GET_MODE_BITSIZE (str_mode);
4536 offset1 = bitpos;
4537 bitpos %= str_bitsize;
4538 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4539 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4541 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4542 return false;
4544 /* If the bit field covers the whole REG/MEM, store_field
4545 will likely generate better code. */
4546 if (bitsize >= str_bitsize)
4547 return false;
4549 /* We can't handle fields split across multiple entities. */
4550 if (bitpos + bitsize > str_bitsize)
4551 return false;
4553 if (BYTES_BIG_ENDIAN)
4554 bitpos = str_bitsize - bitpos - bitsize;
4556 switch (code)
4558 case PLUS_EXPR:
4559 case MINUS_EXPR:
4560 /* For now, just optimize the case of the topmost bitfield
4561 where we don't need to do any masking and also
4562 1 bit bitfields where xor can be used.
4563 We might win by one instruction for the other bitfields
4564 too if insv/extv instructions aren't used, so that
4565 can be added later. */
4566 if (bitpos + bitsize != str_bitsize
4567 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4568 break;
4570 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4571 value = convert_modes (str_mode,
4572 TYPE_MODE (TREE_TYPE (op1)), value,
4573 TYPE_UNSIGNED (TREE_TYPE (op1)));
4575 /* We may be accessing data outside the field, which means
4576 we can alias adjacent data. */
4577 if (MEM_P (str_rtx))
4579 str_rtx = shallow_copy_rtx (str_rtx);
4580 set_mem_alias_set (str_rtx, 0);
4581 set_mem_expr (str_rtx, 0);
4584 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4585 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4587 value = expand_and (str_mode, value, const1_rtx, NULL);
4588 binop = xor_optab;
4590 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4591 result = expand_binop (str_mode, binop, str_rtx,
4592 value, str_rtx, 1, OPTAB_WIDEN);
4593 if (result != str_rtx)
4594 emit_move_insn (str_rtx, result);
4595 return true;
4597 case BIT_IOR_EXPR:
4598 case BIT_XOR_EXPR:
4599 if (TREE_CODE (op1) != INTEGER_CST)
4600 break;
4601 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4602 value = convert_modes (str_mode,
4603 TYPE_MODE (TREE_TYPE (op1)), value,
4604 TYPE_UNSIGNED (TREE_TYPE (op1)));
4606 /* We may be accessing data outside the field, which means
4607 we can alias adjacent data. */
4608 if (MEM_P (str_rtx))
4610 str_rtx = shallow_copy_rtx (str_rtx);
4611 set_mem_alias_set (str_rtx, 0);
4612 set_mem_expr (str_rtx, 0);
4615 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4616 if (bitpos + bitsize != str_bitsize)
4618 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4619 str_mode);
4620 value = expand_and (str_mode, value, mask, NULL_RTX);
4622 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4623 result = expand_binop (str_mode, binop, str_rtx,
4624 value, str_rtx, 1, OPTAB_WIDEN);
4625 if (result != str_rtx)
4626 emit_move_insn (str_rtx, result);
4627 return true;
4629 default:
4630 break;
4633 return false;
4636 /* In the C++ memory model, consecutive bit fields in a structure are
4637 considered one memory location.
4639 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4640 returns the bit range of consecutive bits in which this COMPONENT_REF
4641 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4642 and *OFFSET may be adjusted in the process.
4644 If the access does not need to be restricted, 0 is returned in both
4645 *BITSTART and *BITEND. */
4647 static void
4648 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4649 unsigned HOST_WIDE_INT *bitend,
4650 tree exp,
4651 HOST_WIDE_INT *bitpos,
4652 tree *offset)
4654 HOST_WIDE_INT bitoffset;
4655 tree field, repr;
4657 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4659 field = TREE_OPERAND (exp, 1);
4660 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4661 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4662 need to limit the range we can access. */
4663 if (!repr)
4665 *bitstart = *bitend = 0;
4666 return;
4669 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4670 part of a larger bit field, then the representative does not serve any
4671 useful purpose. This can occur in Ada. */
4672 if (handled_component_p (TREE_OPERAND (exp, 0)))
4674 machine_mode rmode;
4675 HOST_WIDE_INT rbitsize, rbitpos;
4676 tree roffset;
4677 int unsignedp;
4678 int volatilep = 0;
4679 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4680 &roffset, &rmode, &unsignedp, &volatilep, false);
4681 if ((rbitpos % BITS_PER_UNIT) != 0)
4683 *bitstart = *bitend = 0;
4684 return;
4688 /* Compute the adjustment to bitpos from the offset of the field
4689 relative to the representative. DECL_FIELD_OFFSET of field and
4690 repr are the same by construction if they are not constants,
4691 see finish_bitfield_layout. */
4692 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4693 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4694 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4695 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4696 else
4697 bitoffset = 0;
4698 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4699 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4701 /* If the adjustment is larger than bitpos, we would have a negative bit
4702 position for the lower bound and this may wreak havoc later. Adjust
4703 offset and bitpos to make the lower bound non-negative in that case. */
4704 if (bitoffset > *bitpos)
4706 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4707 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4709 *bitpos += adjust;
4710 if (*offset == NULL_TREE)
4711 *offset = size_int (-adjust / BITS_PER_UNIT);
4712 else
4713 *offset
4714 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4715 *bitstart = 0;
4717 else
4718 *bitstart = *bitpos - bitoffset;
4720 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4723 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4724 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4725 DECL_RTL was not set yet, return NORTL. */
4727 static inline bool
4728 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4730 if (TREE_CODE (addr) != ADDR_EXPR)
4731 return false;
4733 tree base = TREE_OPERAND (addr, 0);
4735 if (!DECL_P (base)
4736 || TREE_ADDRESSABLE (base)
4737 || DECL_MODE (base) == BLKmode)
4738 return false;
4740 if (!DECL_RTL_SET_P (base))
4741 return nortl;
4743 return (!MEM_P (DECL_RTL (base)));
4746 /* Returns true if the MEM_REF REF refers to an object that does not
4747 reside in memory and has non-BLKmode. */
4749 static inline bool
4750 mem_ref_refers_to_non_mem_p (tree ref)
4752 tree base = TREE_OPERAND (ref, 0);
4753 return addr_expr_of_non_mem_decl_p_1 (base, false);
4756 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4757 is true, try generating a nontemporal store. */
4759 void
4760 expand_assignment (tree to, tree from, bool nontemporal)
4762 rtx to_rtx = 0;
4763 rtx result;
4764 machine_mode mode;
4765 unsigned int align;
4766 enum insn_code icode;
4768 /* Don't crash if the lhs of the assignment was erroneous. */
4769 if (TREE_CODE (to) == ERROR_MARK)
4771 expand_normal (from);
4772 return;
4775 /* Optimize away no-op moves without side-effects. */
4776 if (operand_equal_p (to, from, 0))
4777 return;
4779 /* Handle misaligned stores. */
4780 mode = TYPE_MODE (TREE_TYPE (to));
4781 if ((TREE_CODE (to) == MEM_REF
4782 || TREE_CODE (to) == TARGET_MEM_REF)
4783 && mode != BLKmode
4784 && !mem_ref_refers_to_non_mem_p (to)
4785 && ((align = get_object_alignment (to))
4786 < GET_MODE_ALIGNMENT (mode))
4787 && (((icode = optab_handler (movmisalign_optab, mode))
4788 != CODE_FOR_nothing)
4789 || SLOW_UNALIGNED_ACCESS (mode, align)))
4791 rtx reg, mem;
4793 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4794 reg = force_not_mem (reg);
4795 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4797 if (icode != CODE_FOR_nothing)
4799 struct expand_operand ops[2];
4801 create_fixed_operand (&ops[0], mem);
4802 create_input_operand (&ops[1], reg, mode);
4803 /* The movmisalign<mode> pattern cannot fail, else the assignment
4804 would silently be omitted. */
4805 expand_insn (icode, 2, ops);
4807 else
4808 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4809 return;
4812 /* Assignment of a structure component needs special treatment
4813 if the structure component's rtx is not simply a MEM.
4814 Assignment of an array element at a constant index, and assignment of
4815 an array element in an unaligned packed structure field, has the same
4816 problem. Same for (partially) storing into a non-memory object. */
4817 if (handled_component_p (to)
4818 || (TREE_CODE (to) == MEM_REF
4819 && mem_ref_refers_to_non_mem_p (to))
4820 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4822 machine_mode mode1;
4823 HOST_WIDE_INT bitsize, bitpos;
4824 unsigned HOST_WIDE_INT bitregion_start = 0;
4825 unsigned HOST_WIDE_INT bitregion_end = 0;
4826 tree offset;
4827 int unsignedp;
4828 int volatilep = 0;
4829 tree tem;
4831 push_temp_slots ();
4832 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4833 &unsignedp, &volatilep, true);
4835 /* Make sure bitpos is not negative, it can wreak havoc later. */
4836 if (bitpos < 0)
4838 gcc_assert (offset == NULL_TREE);
4839 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4840 ? 3 : exact_log2 (BITS_PER_UNIT)));
4841 bitpos &= BITS_PER_UNIT - 1;
4844 if (TREE_CODE (to) == COMPONENT_REF
4845 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4846 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4847 /* The C++ memory model naturally applies to byte-aligned fields.
4848 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4849 BITSIZE are not byte-aligned, there is no need to limit the range
4850 we can access. This can occur with packed structures in Ada. */
4851 else if (bitsize > 0
4852 && bitsize % BITS_PER_UNIT == 0
4853 && bitpos % BITS_PER_UNIT == 0)
4855 bitregion_start = bitpos;
4856 bitregion_end = bitpos + bitsize - 1;
4859 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4861 /* If the field has a mode, we want to access it in the
4862 field's mode, not the computed mode.
4863 If a MEM has VOIDmode (external with incomplete type),
4864 use BLKmode for it instead. */
4865 if (MEM_P (to_rtx))
4867 if (mode1 != VOIDmode)
4868 to_rtx = adjust_address (to_rtx, mode1, 0);
4869 else if (GET_MODE (to_rtx) == VOIDmode)
4870 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4873 if (offset != 0)
4875 machine_mode address_mode;
4876 rtx offset_rtx;
4878 if (!MEM_P (to_rtx))
4880 /* We can get constant negative offsets into arrays with broken
4881 user code. Translate this to a trap instead of ICEing. */
4882 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4883 expand_builtin_trap ();
4884 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4887 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4888 address_mode = get_address_mode (to_rtx);
4889 if (GET_MODE (offset_rtx) != address_mode)
4891 /* We cannot be sure that the RTL in offset_rtx is valid outside
4892 of a memory address context, so force it into a register
4893 before attempting to convert it to the desired mode. */
4894 offset_rtx = force_operand (offset_rtx, NULL_RTX);
4895 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4898 /* If we have an expression in OFFSET_RTX and a non-zero
4899 byte offset in BITPOS, adding the byte offset before the
4900 OFFSET_RTX results in better intermediate code, which makes
4901 later rtl optimization passes perform better.
4903 We prefer intermediate code like this:
4905 r124:DI=r123:DI+0x18
4906 [r124:DI]=r121:DI
4908 ... instead of ...
4910 r124:DI=r123:DI+0x10
4911 [r124:DI+0x8]=r121:DI
4913 This is only done for aligned data values, as these can
4914 be expected to result in single move instructions. */
4915 if (mode1 != VOIDmode
4916 && bitpos != 0
4917 && bitsize > 0
4918 && (bitpos % bitsize) == 0
4919 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4920 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4922 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4923 bitregion_start = 0;
4924 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4925 bitregion_end -= bitpos;
4926 bitpos = 0;
4929 to_rtx = offset_address (to_rtx, offset_rtx,
4930 highest_pow2_factor_for_target (to,
4931 offset));
4934 /* No action is needed if the target is not a memory and the field
4935 lies completely outside that target. This can occur if the source
4936 code contains an out-of-bounds access to a small array. */
4937 if (!MEM_P (to_rtx)
4938 && GET_MODE (to_rtx) != BLKmode
4939 && (unsigned HOST_WIDE_INT) bitpos
4940 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4942 expand_normal (from);
4943 result = NULL;
4945 /* Handle expand_expr of a complex value returning a CONCAT. */
4946 else if (GET_CODE (to_rtx) == CONCAT)
4948 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4949 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4950 && bitpos == 0
4951 && bitsize == mode_bitsize)
4952 result = store_expr (from, to_rtx, false, nontemporal);
4953 else if (bitsize == mode_bitsize / 2
4954 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4955 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4956 nontemporal);
4957 else if (bitpos + bitsize <= mode_bitsize / 2)
4958 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4959 bitregion_start, bitregion_end,
4960 mode1, from,
4961 get_alias_set (to), nontemporal);
4962 else if (bitpos >= mode_bitsize / 2)
4963 result = store_field (XEXP (to_rtx, 1), bitsize,
4964 bitpos - mode_bitsize / 2,
4965 bitregion_start, bitregion_end,
4966 mode1, from,
4967 get_alias_set (to), nontemporal);
4968 else if (bitpos == 0 && bitsize == mode_bitsize)
4970 rtx from_rtx;
4971 result = expand_normal (from);
4972 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4973 TYPE_MODE (TREE_TYPE (from)), 0);
4974 emit_move_insn (XEXP (to_rtx, 0),
4975 read_complex_part (from_rtx, false));
4976 emit_move_insn (XEXP (to_rtx, 1),
4977 read_complex_part (from_rtx, true));
4979 else
4981 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4982 GET_MODE_SIZE (GET_MODE (to_rtx)));
4983 write_complex_part (temp, XEXP (to_rtx, 0), false);
4984 write_complex_part (temp, XEXP (to_rtx, 1), true);
4985 result = store_field (temp, bitsize, bitpos,
4986 bitregion_start, bitregion_end,
4987 mode1, from,
4988 get_alias_set (to), nontemporal);
4989 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4990 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4993 else
4995 if (MEM_P (to_rtx))
4997 /* If the field is at offset zero, we could have been given the
4998 DECL_RTX of the parent struct. Don't munge it. */
4999 to_rtx = shallow_copy_rtx (to_rtx);
5000 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5001 if (volatilep)
5002 MEM_VOLATILE_P (to_rtx) = 1;
5005 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5006 bitregion_start, bitregion_end,
5007 mode1,
5008 to_rtx, to, from))
5009 result = NULL;
5010 else
5011 result = store_field (to_rtx, bitsize, bitpos,
5012 bitregion_start, bitregion_end,
5013 mode1, from,
5014 get_alias_set (to), nontemporal);
5017 if (result)
5018 preserve_temp_slots (result);
5019 pop_temp_slots ();
5020 return;
5023 /* If the rhs is a function call and its value is not an aggregate,
5024 call the function before we start to compute the lhs.
5025 This is needed for correct code for cases such as
5026 val = setjmp (buf) on machines where reference to val
5027 requires loading up part of an address in a separate insn.
5029 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5030 since it might be a promoted variable where the zero- or sign- extension
5031 needs to be done. Handling this in the normal way is safe because no
5032 computation is done before the call. The same is true for SSA names. */
5033 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5034 && COMPLETE_TYPE_P (TREE_TYPE (from))
5035 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5036 && ! (((TREE_CODE (to) == VAR_DECL
5037 || TREE_CODE (to) == PARM_DECL
5038 || TREE_CODE (to) == RESULT_DECL)
5039 && REG_P (DECL_RTL (to)))
5040 || TREE_CODE (to) == SSA_NAME))
5042 rtx value;
5043 rtx bounds;
5045 push_temp_slots ();
5046 value = expand_normal (from);
5048 /* Split value and bounds to store them separately. */
5049 chkp_split_slot (value, &value, &bounds);
5051 if (to_rtx == 0)
5052 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5054 /* Handle calls that return values in multiple non-contiguous locations.
5055 The Irix 6 ABI has examples of this. */
5056 if (GET_CODE (to_rtx) == PARALLEL)
5058 if (GET_CODE (value) == PARALLEL)
5059 emit_group_move (to_rtx, value);
5060 else
5061 emit_group_load (to_rtx, value, TREE_TYPE (from),
5062 int_size_in_bytes (TREE_TYPE (from)));
5064 else if (GET_CODE (value) == PARALLEL)
5065 emit_group_store (to_rtx, value, TREE_TYPE (from),
5066 int_size_in_bytes (TREE_TYPE (from)));
5067 else if (GET_MODE (to_rtx) == BLKmode)
5069 /* Handle calls that return BLKmode values in registers. */
5070 if (REG_P (value))
5071 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5072 else
5073 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5075 else
5077 if (POINTER_TYPE_P (TREE_TYPE (to)))
5078 value = convert_memory_address_addr_space
5079 (GET_MODE (to_rtx), value,
5080 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5082 emit_move_insn (to_rtx, value);
5085 /* Store bounds if required. */
5086 if (bounds
5087 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5089 gcc_assert (MEM_P (to_rtx));
5090 chkp_emit_bounds_store (bounds, value, to_rtx);
5093 preserve_temp_slots (to_rtx);
5094 pop_temp_slots ();
5095 return;
5098 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5099 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5101 /* Don't move directly into a return register. */
5102 if (TREE_CODE (to) == RESULT_DECL
5103 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5105 rtx temp;
5107 push_temp_slots ();
5109 /* If the source is itself a return value, it still is in a pseudo at
5110 this point so we can move it back to the return register directly. */
5111 if (REG_P (to_rtx)
5112 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5113 && TREE_CODE (from) != CALL_EXPR)
5114 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5115 else
5116 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5118 /* Handle calls that return values in multiple non-contiguous locations.
5119 The Irix 6 ABI has examples of this. */
5120 if (GET_CODE (to_rtx) == PARALLEL)
5122 if (GET_CODE (temp) == PARALLEL)
5123 emit_group_move (to_rtx, temp);
5124 else
5125 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5126 int_size_in_bytes (TREE_TYPE (from)));
5128 else if (temp)
5129 emit_move_insn (to_rtx, temp);
5131 preserve_temp_slots (to_rtx);
5132 pop_temp_slots ();
5133 return;
5136 /* In case we are returning the contents of an object which overlaps
5137 the place the value is being stored, use a safe function when copying
5138 a value through a pointer into a structure value return block. */
5139 if (TREE_CODE (to) == RESULT_DECL
5140 && TREE_CODE (from) == INDIRECT_REF
5141 && ADDR_SPACE_GENERIC_P
5142 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5143 && refs_may_alias_p (to, from)
5144 && cfun->returns_struct
5145 && !cfun->returns_pcc_struct)
5147 rtx from_rtx, size;
5149 push_temp_slots ();
5150 size = expr_size (from);
5151 from_rtx = expand_normal (from);
5153 emit_library_call (memmove_libfunc, LCT_NORMAL,
5154 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5155 XEXP (from_rtx, 0), Pmode,
5156 convert_to_mode (TYPE_MODE (sizetype),
5157 size, TYPE_UNSIGNED (sizetype)),
5158 TYPE_MODE (sizetype));
5160 preserve_temp_slots (to_rtx);
5161 pop_temp_slots ();
5162 return;
5165 /* Compute FROM and store the value in the rtx we got. */
5167 push_temp_slots ();
5168 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5169 preserve_temp_slots (result);
5170 pop_temp_slots ();
5171 return;
5174 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5175 succeeded, false otherwise. */
5177 bool
5178 emit_storent_insn (rtx to, rtx from)
5180 struct expand_operand ops[2];
5181 machine_mode mode = GET_MODE (to);
5182 enum insn_code code = optab_handler (storent_optab, mode);
5184 if (code == CODE_FOR_nothing)
5185 return false;
5187 create_fixed_operand (&ops[0], to);
5188 create_input_operand (&ops[1], from, mode);
5189 return maybe_expand_insn (code, 2, ops);
5192 /* Generate code for computing expression EXP,
5193 and storing the value into TARGET.
5195 If the mode is BLKmode then we may return TARGET itself.
5196 It turns out that in BLKmode it doesn't cause a problem.
5197 because C has no operators that could combine two different
5198 assignments into the same BLKmode object with different values
5199 with no sequence point. Will other languages need this to
5200 be more thorough?
5202 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5203 stack, and block moves may need to be treated specially.
5205 If NONTEMPORAL is true, try using a nontemporal store instruction.
5207 If BTARGET is not NULL then computed bounds of EXP are
5208 associated with BTARGET. */
5211 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5212 bool nontemporal, tree btarget)
5214 rtx temp;
5215 rtx alt_rtl = NULL_RTX;
5216 location_t loc = curr_insn_location ();
5218 if (VOID_TYPE_P (TREE_TYPE (exp)))
5220 /* C++ can generate ?: expressions with a throw expression in one
5221 branch and an rvalue in the other. Here, we resolve attempts to
5222 store the throw expression's nonexistent result. */
5223 gcc_assert (!call_param_p);
5224 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5225 return NULL_RTX;
5227 if (TREE_CODE (exp) == COMPOUND_EXPR)
5229 /* Perform first part of compound expression, then assign from second
5230 part. */
5231 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5232 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5233 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5234 call_param_p, nontemporal, btarget);
5236 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5238 /* For conditional expression, get safe form of the target. Then
5239 test the condition, doing the appropriate assignment on either
5240 side. This avoids the creation of unnecessary temporaries.
5241 For non-BLKmode, it is more efficient not to do this. */
5243 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5245 do_pending_stack_adjust ();
5246 NO_DEFER_POP;
5247 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5248 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5249 nontemporal, btarget);
5250 emit_jump_insn (gen_jump (lab2));
5251 emit_barrier ();
5252 emit_label (lab1);
5253 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5254 nontemporal, btarget);
5255 emit_label (lab2);
5256 OK_DEFER_POP;
5258 return NULL_RTX;
5260 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5261 /* If this is a scalar in a register that is stored in a wider mode
5262 than the declared mode, compute the result into its declared mode
5263 and then convert to the wider mode. Our value is the computed
5264 expression. */
5266 rtx inner_target = 0;
5268 /* We can do the conversion inside EXP, which will often result
5269 in some optimizations. Do the conversion in two steps: first
5270 change the signedness, if needed, then the extend. But don't
5271 do this if the type of EXP is a subtype of something else
5272 since then the conversion might involve more than just
5273 converting modes. */
5274 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5275 && TREE_TYPE (TREE_TYPE (exp)) == 0
5276 && GET_MODE_PRECISION (GET_MODE (target))
5277 == TYPE_PRECISION (TREE_TYPE (exp)))
5279 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5280 TYPE_UNSIGNED (TREE_TYPE (exp))))
5282 /* Some types, e.g. Fortran's logical*4, won't have a signed
5283 version, so use the mode instead. */
5284 tree ntype
5285 = (signed_or_unsigned_type_for
5286 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5287 if (ntype == NULL)
5288 ntype = lang_hooks.types.type_for_mode
5289 (TYPE_MODE (TREE_TYPE (exp)),
5290 SUBREG_PROMOTED_SIGN (target));
5292 exp = fold_convert_loc (loc, ntype, exp);
5295 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5296 (GET_MODE (SUBREG_REG (target)),
5297 SUBREG_PROMOTED_SIGN (target)),
5298 exp);
5300 inner_target = SUBREG_REG (target);
5303 temp = expand_expr (exp, inner_target, VOIDmode,
5304 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5306 /* Handle bounds returned by call. */
5307 if (TREE_CODE (exp) == CALL_EXPR)
5309 rtx bounds;
5310 chkp_split_slot (temp, &temp, &bounds);
5311 if (bounds && btarget)
5313 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5314 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5315 chkp_set_rtl_bounds (btarget, tmp);
5319 /* If TEMP is a VOIDmode constant, use convert_modes to make
5320 sure that we properly convert it. */
5321 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5323 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5324 temp, SUBREG_PROMOTED_SIGN (target));
5325 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5326 GET_MODE (target), temp,
5327 SUBREG_PROMOTED_SIGN (target));
5330 convert_move (SUBREG_REG (target), temp,
5331 SUBREG_PROMOTED_SIGN (target));
5333 return NULL_RTX;
5335 else if ((TREE_CODE (exp) == STRING_CST
5336 || (TREE_CODE (exp) == MEM_REF
5337 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5338 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5339 == STRING_CST
5340 && integer_zerop (TREE_OPERAND (exp, 1))))
5341 && !nontemporal && !call_param_p
5342 && MEM_P (target))
5344 /* Optimize initialization of an array with a STRING_CST. */
5345 HOST_WIDE_INT exp_len, str_copy_len;
5346 rtx dest_mem;
5347 tree str = TREE_CODE (exp) == STRING_CST
5348 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5350 exp_len = int_expr_size (exp);
5351 if (exp_len <= 0)
5352 goto normal_expr;
5354 if (TREE_STRING_LENGTH (str) <= 0)
5355 goto normal_expr;
5357 str_copy_len = strlen (TREE_STRING_POINTER (str));
5358 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5359 goto normal_expr;
5361 str_copy_len = TREE_STRING_LENGTH (str);
5362 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5363 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5365 str_copy_len += STORE_MAX_PIECES - 1;
5366 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5368 str_copy_len = MIN (str_copy_len, exp_len);
5369 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5370 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5371 MEM_ALIGN (target), false))
5372 goto normal_expr;
5374 dest_mem = target;
5376 dest_mem = store_by_pieces (dest_mem,
5377 str_copy_len, builtin_strncpy_read_str,
5378 CONST_CAST (char *,
5379 TREE_STRING_POINTER (str)),
5380 MEM_ALIGN (target), false,
5381 exp_len > str_copy_len ? 1 : 0);
5382 if (exp_len > str_copy_len)
5383 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5384 GEN_INT (exp_len - str_copy_len),
5385 BLOCK_OP_NORMAL);
5386 return NULL_RTX;
5388 else
5390 rtx tmp_target;
5392 normal_expr:
5393 /* If we want to use a nontemporal store, force the value to
5394 register first. */
5395 tmp_target = nontemporal ? NULL_RTX : target;
5396 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5397 (call_param_p
5398 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5399 &alt_rtl, false);
5401 /* Handle bounds returned by call. */
5402 if (TREE_CODE (exp) == CALL_EXPR)
5404 rtx bounds;
5405 chkp_split_slot (temp, &temp, &bounds);
5406 if (bounds && btarget)
5408 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5409 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5410 chkp_set_rtl_bounds (btarget, tmp);
5415 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5416 the same as that of TARGET, adjust the constant. This is needed, for
5417 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5418 only a word-sized value. */
5419 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5420 && TREE_CODE (exp) != ERROR_MARK
5421 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5422 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5423 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5425 /* If value was not generated in the target, store it there.
5426 Convert the value to TARGET's type first if necessary and emit the
5427 pending incrementations that have been queued when expanding EXP.
5428 Note that we cannot emit the whole queue blindly because this will
5429 effectively disable the POST_INC optimization later.
5431 If TEMP and TARGET compare equal according to rtx_equal_p, but
5432 one or both of them are volatile memory refs, we have to distinguish
5433 two cases:
5434 - expand_expr has used TARGET. In this case, we must not generate
5435 another copy. This can be detected by TARGET being equal according
5436 to == .
5437 - expand_expr has not used TARGET - that means that the source just
5438 happens to have the same RTX form. Since temp will have been created
5439 by expand_expr, it will compare unequal according to == .
5440 We must generate a copy in this case, to reach the correct number
5441 of volatile memory references. */
5443 if ((! rtx_equal_p (temp, target)
5444 || (temp != target && (side_effects_p (temp)
5445 || side_effects_p (target))))
5446 && TREE_CODE (exp) != ERROR_MARK
5447 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5448 but TARGET is not valid memory reference, TEMP will differ
5449 from TARGET although it is really the same location. */
5450 && !(alt_rtl
5451 && rtx_equal_p (alt_rtl, target)
5452 && !side_effects_p (alt_rtl)
5453 && !side_effects_p (target))
5454 /* If there's nothing to copy, don't bother. Don't call
5455 expr_size unless necessary, because some front-ends (C++)
5456 expr_size-hook must not be given objects that are not
5457 supposed to be bit-copied or bit-initialized. */
5458 && expr_size (exp) != const0_rtx)
5460 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5462 if (GET_MODE (target) == BLKmode)
5464 /* Handle calls that return BLKmode values in registers. */
5465 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5466 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5467 else
5468 store_bit_field (target,
5469 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5470 0, 0, 0, GET_MODE (temp), temp);
5472 else
5473 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5476 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5478 /* Handle copying a string constant into an array. The string
5479 constant may be shorter than the array. So copy just the string's
5480 actual length, and clear the rest. First get the size of the data
5481 type of the string, which is actually the size of the target. */
5482 rtx size = expr_size (exp);
5484 if (CONST_INT_P (size)
5485 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5486 emit_block_move (target, temp, size,
5487 (call_param_p
5488 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5489 else
5491 machine_mode pointer_mode
5492 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5493 machine_mode address_mode = get_address_mode (target);
5495 /* Compute the size of the data to copy from the string. */
5496 tree copy_size
5497 = size_binop_loc (loc, MIN_EXPR,
5498 make_tree (sizetype, size),
5499 size_int (TREE_STRING_LENGTH (exp)));
5500 rtx copy_size_rtx
5501 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5502 (call_param_p
5503 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5504 rtx_code_label *label = 0;
5506 /* Copy that much. */
5507 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5508 TYPE_UNSIGNED (sizetype));
5509 emit_block_move (target, temp, copy_size_rtx,
5510 (call_param_p
5511 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5513 /* Figure out how much is left in TARGET that we have to clear.
5514 Do all calculations in pointer_mode. */
5515 if (CONST_INT_P (copy_size_rtx))
5517 size = plus_constant (address_mode, size,
5518 -INTVAL (copy_size_rtx));
5519 target = adjust_address (target, BLKmode,
5520 INTVAL (copy_size_rtx));
5522 else
5524 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5525 copy_size_rtx, NULL_RTX, 0,
5526 OPTAB_LIB_WIDEN);
5528 if (GET_MODE (copy_size_rtx) != address_mode)
5529 copy_size_rtx = convert_to_mode (address_mode,
5530 copy_size_rtx,
5531 TYPE_UNSIGNED (sizetype));
5533 target = offset_address (target, copy_size_rtx,
5534 highest_pow2_factor (copy_size));
5535 label = gen_label_rtx ();
5536 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5537 GET_MODE (size), 0, label);
5540 if (size != const0_rtx)
5541 clear_storage (target, size, BLOCK_OP_NORMAL);
5543 if (label)
5544 emit_label (label);
5547 /* Handle calls that return values in multiple non-contiguous locations.
5548 The Irix 6 ABI has examples of this. */
5549 else if (GET_CODE (target) == PARALLEL)
5551 if (GET_CODE (temp) == PARALLEL)
5552 emit_group_move (target, temp);
5553 else
5554 emit_group_load (target, temp, TREE_TYPE (exp),
5555 int_size_in_bytes (TREE_TYPE (exp)));
5557 else if (GET_CODE (temp) == PARALLEL)
5558 emit_group_store (target, temp, TREE_TYPE (exp),
5559 int_size_in_bytes (TREE_TYPE (exp)));
5560 else if (GET_MODE (temp) == BLKmode)
5561 emit_block_move (target, temp, expr_size (exp),
5562 (call_param_p
5563 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5564 /* If we emit a nontemporal store, there is nothing else to do. */
5565 else if (nontemporal && emit_storent_insn (target, temp))
5567 else
5569 temp = force_operand (temp, target);
5570 if (temp != target)
5571 emit_move_insn (target, temp);
5575 return NULL_RTX;
5578 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5580 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5582 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5585 /* Return true if field F of structure TYPE is a flexible array. */
5587 static bool
5588 flexible_array_member_p (const_tree f, const_tree type)
5590 const_tree tf;
5592 tf = TREE_TYPE (f);
5593 return (DECL_CHAIN (f) == NULL
5594 && TREE_CODE (tf) == ARRAY_TYPE
5595 && TYPE_DOMAIN (tf)
5596 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5597 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5598 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5599 && int_size_in_bytes (type) >= 0);
5602 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5603 must have in order for it to completely initialize a value of type TYPE.
5604 Return -1 if the number isn't known.
5606 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5608 static HOST_WIDE_INT
5609 count_type_elements (const_tree type, bool for_ctor_p)
5611 switch (TREE_CODE (type))
5613 case ARRAY_TYPE:
5615 tree nelts;
5617 nelts = array_type_nelts (type);
5618 if (nelts && tree_fits_uhwi_p (nelts))
5620 unsigned HOST_WIDE_INT n;
5622 n = tree_to_uhwi (nelts) + 1;
5623 if (n == 0 || for_ctor_p)
5624 return n;
5625 else
5626 return n * count_type_elements (TREE_TYPE (type), false);
5628 return for_ctor_p ? -1 : 1;
5631 case RECORD_TYPE:
5633 unsigned HOST_WIDE_INT n;
5634 tree f;
5636 n = 0;
5637 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5638 if (TREE_CODE (f) == FIELD_DECL)
5640 if (!for_ctor_p)
5641 n += count_type_elements (TREE_TYPE (f), false);
5642 else if (!flexible_array_member_p (f, type))
5643 /* Don't count flexible arrays, which are not supposed
5644 to be initialized. */
5645 n += 1;
5648 return n;
5651 case UNION_TYPE:
5652 case QUAL_UNION_TYPE:
5654 tree f;
5655 HOST_WIDE_INT n, m;
5657 gcc_assert (!for_ctor_p);
5658 /* Estimate the number of scalars in each field and pick the
5659 maximum. Other estimates would do instead; the idea is simply
5660 to make sure that the estimate is not sensitive to the ordering
5661 of the fields. */
5662 n = 1;
5663 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5664 if (TREE_CODE (f) == FIELD_DECL)
5666 m = count_type_elements (TREE_TYPE (f), false);
5667 /* If the field doesn't span the whole union, add an extra
5668 scalar for the rest. */
5669 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5670 TYPE_SIZE (type)) != 1)
5671 m++;
5672 if (n < m)
5673 n = m;
5675 return n;
5678 case COMPLEX_TYPE:
5679 return 2;
5681 case VECTOR_TYPE:
5682 return TYPE_VECTOR_SUBPARTS (type);
5684 case INTEGER_TYPE:
5685 case REAL_TYPE:
5686 case FIXED_POINT_TYPE:
5687 case ENUMERAL_TYPE:
5688 case BOOLEAN_TYPE:
5689 case POINTER_TYPE:
5690 case OFFSET_TYPE:
5691 case REFERENCE_TYPE:
5692 case NULLPTR_TYPE:
5693 return 1;
5695 case ERROR_MARK:
5696 return 0;
5698 case VOID_TYPE:
5699 case METHOD_TYPE:
5700 case FUNCTION_TYPE:
5701 case LANG_TYPE:
5702 default:
5703 gcc_unreachable ();
5707 /* Helper for categorize_ctor_elements. Identical interface. */
5709 static bool
5710 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5711 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5713 unsigned HOST_WIDE_INT idx;
5714 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5715 tree value, purpose, elt_type;
5717 /* Whether CTOR is a valid constant initializer, in accordance with what
5718 initializer_constant_valid_p does. If inferred from the constructor
5719 elements, true until proven otherwise. */
5720 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5721 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5723 nz_elts = 0;
5724 init_elts = 0;
5725 num_fields = 0;
5726 elt_type = NULL_TREE;
5728 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5730 HOST_WIDE_INT mult = 1;
5732 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5734 tree lo_index = TREE_OPERAND (purpose, 0);
5735 tree hi_index = TREE_OPERAND (purpose, 1);
5737 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5738 mult = (tree_to_uhwi (hi_index)
5739 - tree_to_uhwi (lo_index) + 1);
5741 num_fields += mult;
5742 elt_type = TREE_TYPE (value);
5744 switch (TREE_CODE (value))
5746 case CONSTRUCTOR:
5748 HOST_WIDE_INT nz = 0, ic = 0;
5750 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5751 p_complete);
5753 nz_elts += mult * nz;
5754 init_elts += mult * ic;
5756 if (const_from_elts_p && const_p)
5757 const_p = const_elt_p;
5759 break;
5761 case INTEGER_CST:
5762 case REAL_CST:
5763 case FIXED_CST:
5764 if (!initializer_zerop (value))
5765 nz_elts += mult;
5766 init_elts += mult;
5767 break;
5769 case STRING_CST:
5770 nz_elts += mult * TREE_STRING_LENGTH (value);
5771 init_elts += mult * TREE_STRING_LENGTH (value);
5772 break;
5774 case COMPLEX_CST:
5775 if (!initializer_zerop (TREE_REALPART (value)))
5776 nz_elts += mult;
5777 if (!initializer_zerop (TREE_IMAGPART (value)))
5778 nz_elts += mult;
5779 init_elts += mult;
5780 break;
5782 case VECTOR_CST:
5784 unsigned i;
5785 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5787 tree v = VECTOR_CST_ELT (value, i);
5788 if (!initializer_zerop (v))
5789 nz_elts += mult;
5790 init_elts += mult;
5793 break;
5795 default:
5797 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5798 nz_elts += mult * tc;
5799 init_elts += mult * tc;
5801 if (const_from_elts_p && const_p)
5802 const_p = initializer_constant_valid_p (value, elt_type)
5803 != NULL_TREE;
5805 break;
5809 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5810 num_fields, elt_type))
5811 *p_complete = false;
5813 *p_nz_elts += nz_elts;
5814 *p_init_elts += init_elts;
5816 return const_p;
5819 /* Examine CTOR to discover:
5820 * how many scalar fields are set to nonzero values,
5821 and place it in *P_NZ_ELTS;
5822 * how many scalar fields in total are in CTOR,
5823 and place it in *P_ELT_COUNT.
5824 * whether the constructor is complete -- in the sense that every
5825 meaningful byte is explicitly given a value --
5826 and place it in *P_COMPLETE.
5828 Return whether or not CTOR is a valid static constant initializer, the same
5829 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5831 bool
5832 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5833 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5835 *p_nz_elts = 0;
5836 *p_init_elts = 0;
5837 *p_complete = true;
5839 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5842 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5843 of which had type LAST_TYPE. Each element was itself a complete
5844 initializer, in the sense that every meaningful byte was explicitly
5845 given a value. Return true if the same is true for the constructor
5846 as a whole. */
5848 bool
5849 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5850 const_tree last_type)
5852 if (TREE_CODE (type) == UNION_TYPE
5853 || TREE_CODE (type) == QUAL_UNION_TYPE)
5855 if (num_elts == 0)
5856 return false;
5858 gcc_assert (num_elts == 1 && last_type);
5860 /* ??? We could look at each element of the union, and find the
5861 largest element. Which would avoid comparing the size of the
5862 initialized element against any tail padding in the union.
5863 Doesn't seem worth the effort... */
5864 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5867 return count_type_elements (type, true) == num_elts;
5870 /* Return 1 if EXP contains mostly (3/4) zeros. */
5872 static int
5873 mostly_zeros_p (const_tree exp)
5875 if (TREE_CODE (exp) == CONSTRUCTOR)
5877 HOST_WIDE_INT nz_elts, init_elts;
5878 bool complete_p;
5880 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5881 return !complete_p || nz_elts < init_elts / 4;
5884 return initializer_zerop (exp);
5887 /* Return 1 if EXP contains all zeros. */
5889 static int
5890 all_zeros_p (const_tree exp)
5892 if (TREE_CODE (exp) == CONSTRUCTOR)
5894 HOST_WIDE_INT nz_elts, init_elts;
5895 bool complete_p;
5897 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5898 return nz_elts == 0;
5901 return initializer_zerop (exp);
5904 /* Helper function for store_constructor.
5905 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5906 CLEARED is as for store_constructor.
5907 ALIAS_SET is the alias set to use for any stores.
5909 This provides a recursive shortcut back to store_constructor when it isn't
5910 necessary to go through store_field. This is so that we can pass through
5911 the cleared field to let store_constructor know that we may not have to
5912 clear a substructure if the outer structure has already been cleared. */
5914 static void
5915 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5916 HOST_WIDE_INT bitpos, machine_mode mode,
5917 tree exp, int cleared, alias_set_type alias_set)
5919 if (TREE_CODE (exp) == CONSTRUCTOR
5920 /* We can only call store_constructor recursively if the size and
5921 bit position are on a byte boundary. */
5922 && bitpos % BITS_PER_UNIT == 0
5923 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5924 /* If we have a nonzero bitpos for a register target, then we just
5925 let store_field do the bitfield handling. This is unlikely to
5926 generate unnecessary clear instructions anyways. */
5927 && (bitpos == 0 || MEM_P (target)))
5929 if (MEM_P (target))
5930 target
5931 = adjust_address (target,
5932 GET_MODE (target) == BLKmode
5933 || 0 != (bitpos
5934 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5935 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5938 /* Update the alias set, if required. */
5939 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5940 && MEM_ALIAS_SET (target) != 0)
5942 target = copy_rtx (target);
5943 set_mem_alias_set (target, alias_set);
5946 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5948 else
5949 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5953 /* Returns the number of FIELD_DECLs in TYPE. */
5955 static int
5956 fields_length (const_tree type)
5958 tree t = TYPE_FIELDS (type);
5959 int count = 0;
5961 for (; t; t = DECL_CHAIN (t))
5962 if (TREE_CODE (t) == FIELD_DECL)
5963 ++count;
5965 return count;
5969 /* Store the value of constructor EXP into the rtx TARGET.
5970 TARGET is either a REG or a MEM; we know it cannot conflict, since
5971 safe_from_p has been called.
5972 CLEARED is true if TARGET is known to have been zero'd.
5973 SIZE is the number of bytes of TARGET we are allowed to modify: this
5974 may not be the same as the size of EXP if we are assigning to a field
5975 which has been packed to exclude padding bits. */
5977 static void
5978 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5980 tree type = TREE_TYPE (exp);
5981 #ifdef WORD_REGISTER_OPERATIONS
5982 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5983 #endif
5985 switch (TREE_CODE (type))
5987 case RECORD_TYPE:
5988 case UNION_TYPE:
5989 case QUAL_UNION_TYPE:
5991 unsigned HOST_WIDE_INT idx;
5992 tree field, value;
5994 /* If size is zero or the target is already cleared, do nothing. */
5995 if (size == 0 || cleared)
5996 cleared = 1;
5997 /* We either clear the aggregate or indicate the value is dead. */
5998 else if ((TREE_CODE (type) == UNION_TYPE
5999 || TREE_CODE (type) == QUAL_UNION_TYPE)
6000 && ! CONSTRUCTOR_ELTS (exp))
6001 /* If the constructor is empty, clear the union. */
6003 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6004 cleared = 1;
6007 /* If we are building a static constructor into a register,
6008 set the initial value as zero so we can fold the value into
6009 a constant. But if more than one register is involved,
6010 this probably loses. */
6011 else if (REG_P (target) && TREE_STATIC (exp)
6012 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6014 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6015 cleared = 1;
6018 /* If the constructor has fewer fields than the structure or
6019 if we are initializing the structure to mostly zeros, clear
6020 the whole structure first. Don't do this if TARGET is a
6021 register whose mode size isn't equal to SIZE since
6022 clear_storage can't handle this case. */
6023 else if (size > 0
6024 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
6025 != fields_length (type))
6026 || mostly_zeros_p (exp))
6027 && (!REG_P (target)
6028 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6029 == size)))
6031 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6032 cleared = 1;
6035 if (REG_P (target) && !cleared)
6036 emit_clobber (target);
6038 /* Store each element of the constructor into the
6039 corresponding field of TARGET. */
6040 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6042 machine_mode mode;
6043 HOST_WIDE_INT bitsize;
6044 HOST_WIDE_INT bitpos = 0;
6045 tree offset;
6046 rtx to_rtx = target;
6048 /* Just ignore missing fields. We cleared the whole
6049 structure, above, if any fields are missing. */
6050 if (field == 0)
6051 continue;
6053 if (cleared && initializer_zerop (value))
6054 continue;
6056 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6057 bitsize = tree_to_uhwi (DECL_SIZE (field));
6058 else
6059 bitsize = -1;
6061 mode = DECL_MODE (field);
6062 if (DECL_BIT_FIELD (field))
6063 mode = VOIDmode;
6065 offset = DECL_FIELD_OFFSET (field);
6066 if (tree_fits_shwi_p (offset)
6067 && tree_fits_shwi_p (bit_position (field)))
6069 bitpos = int_bit_position (field);
6070 offset = 0;
6072 else
6073 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6075 if (offset)
6077 machine_mode address_mode;
6078 rtx offset_rtx;
6080 offset
6081 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6082 make_tree (TREE_TYPE (exp),
6083 target));
6085 offset_rtx = expand_normal (offset);
6086 gcc_assert (MEM_P (to_rtx));
6088 address_mode = get_address_mode (to_rtx);
6089 if (GET_MODE (offset_rtx) != address_mode)
6090 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6092 to_rtx = offset_address (to_rtx, offset_rtx,
6093 highest_pow2_factor (offset));
6096 #ifdef WORD_REGISTER_OPERATIONS
6097 /* If this initializes a field that is smaller than a
6098 word, at the start of a word, try to widen it to a full
6099 word. This special case allows us to output C++ member
6100 function initializations in a form that the optimizers
6101 can understand. */
6102 if (REG_P (target)
6103 && bitsize < BITS_PER_WORD
6104 && bitpos % BITS_PER_WORD == 0
6105 && GET_MODE_CLASS (mode) == MODE_INT
6106 && TREE_CODE (value) == INTEGER_CST
6107 && exp_size >= 0
6108 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6110 tree type = TREE_TYPE (value);
6112 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6114 type = lang_hooks.types.type_for_mode
6115 (word_mode, TYPE_UNSIGNED (type));
6116 value = fold_convert (type, value);
6119 if (BYTES_BIG_ENDIAN)
6120 value
6121 = fold_build2 (LSHIFT_EXPR, type, value,
6122 build_int_cst (type,
6123 BITS_PER_WORD - bitsize));
6124 bitsize = BITS_PER_WORD;
6125 mode = word_mode;
6127 #endif
6129 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6130 && DECL_NONADDRESSABLE_P (field))
6132 to_rtx = copy_rtx (to_rtx);
6133 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6136 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6137 value, cleared,
6138 get_alias_set (TREE_TYPE (field)));
6140 break;
6142 case ARRAY_TYPE:
6144 tree value, index;
6145 unsigned HOST_WIDE_INT i;
6146 int need_to_clear;
6147 tree domain;
6148 tree elttype = TREE_TYPE (type);
6149 int const_bounds_p;
6150 HOST_WIDE_INT minelt = 0;
6151 HOST_WIDE_INT maxelt = 0;
6153 domain = TYPE_DOMAIN (type);
6154 const_bounds_p = (TYPE_MIN_VALUE (domain)
6155 && TYPE_MAX_VALUE (domain)
6156 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6157 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6159 /* If we have constant bounds for the range of the type, get them. */
6160 if (const_bounds_p)
6162 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6163 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6166 /* If the constructor has fewer elements than the array, clear
6167 the whole array first. Similarly if this is static
6168 constructor of a non-BLKmode object. */
6169 if (cleared)
6170 need_to_clear = 0;
6171 else if (REG_P (target) && TREE_STATIC (exp))
6172 need_to_clear = 1;
6173 else
6175 unsigned HOST_WIDE_INT idx;
6176 tree index, value;
6177 HOST_WIDE_INT count = 0, zero_count = 0;
6178 need_to_clear = ! const_bounds_p;
6180 /* This loop is a more accurate version of the loop in
6181 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6182 is also needed to check for missing elements. */
6183 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6185 HOST_WIDE_INT this_node_count;
6187 if (need_to_clear)
6188 break;
6190 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6192 tree lo_index = TREE_OPERAND (index, 0);
6193 tree hi_index = TREE_OPERAND (index, 1);
6195 if (! tree_fits_uhwi_p (lo_index)
6196 || ! tree_fits_uhwi_p (hi_index))
6198 need_to_clear = 1;
6199 break;
6202 this_node_count = (tree_to_uhwi (hi_index)
6203 - tree_to_uhwi (lo_index) + 1);
6205 else
6206 this_node_count = 1;
6208 count += this_node_count;
6209 if (mostly_zeros_p (value))
6210 zero_count += this_node_count;
6213 /* Clear the entire array first if there are any missing
6214 elements, or if the incidence of zero elements is >=
6215 75%. */
6216 if (! need_to_clear
6217 && (count < maxelt - minelt + 1
6218 || 4 * zero_count >= 3 * count))
6219 need_to_clear = 1;
6222 if (need_to_clear && size > 0)
6224 if (REG_P (target))
6225 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6226 else
6227 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6228 cleared = 1;
6231 if (!cleared && REG_P (target))
6232 /* Inform later passes that the old value is dead. */
6233 emit_clobber (target);
6235 /* Store each element of the constructor into the
6236 corresponding element of TARGET, determined by counting the
6237 elements. */
6238 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6240 machine_mode mode;
6241 HOST_WIDE_INT bitsize;
6242 HOST_WIDE_INT bitpos;
6243 rtx xtarget = target;
6245 if (cleared && initializer_zerop (value))
6246 continue;
6248 mode = TYPE_MODE (elttype);
6249 if (mode == BLKmode)
6250 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6251 ? tree_to_uhwi (TYPE_SIZE (elttype))
6252 : -1);
6253 else
6254 bitsize = GET_MODE_BITSIZE (mode);
6256 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6258 tree lo_index = TREE_OPERAND (index, 0);
6259 tree hi_index = TREE_OPERAND (index, 1);
6260 rtx index_r, pos_rtx;
6261 HOST_WIDE_INT lo, hi, count;
6262 tree position;
6264 /* If the range is constant and "small", unroll the loop. */
6265 if (const_bounds_p
6266 && tree_fits_shwi_p (lo_index)
6267 && tree_fits_shwi_p (hi_index)
6268 && (lo = tree_to_shwi (lo_index),
6269 hi = tree_to_shwi (hi_index),
6270 count = hi - lo + 1,
6271 (!MEM_P (target)
6272 || count <= 2
6273 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6274 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6275 <= 40 * 8)))))
6277 lo -= minelt; hi -= minelt;
6278 for (; lo <= hi; lo++)
6280 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6282 if (MEM_P (target)
6283 && !MEM_KEEP_ALIAS_SET_P (target)
6284 && TREE_CODE (type) == ARRAY_TYPE
6285 && TYPE_NONALIASED_COMPONENT (type))
6287 target = copy_rtx (target);
6288 MEM_KEEP_ALIAS_SET_P (target) = 1;
6291 store_constructor_field
6292 (target, bitsize, bitpos, mode, value, cleared,
6293 get_alias_set (elttype));
6296 else
6298 rtx_code_label *loop_start = gen_label_rtx ();
6299 rtx_code_label *loop_end = gen_label_rtx ();
6300 tree exit_cond;
6302 expand_normal (hi_index);
6304 index = build_decl (EXPR_LOCATION (exp),
6305 VAR_DECL, NULL_TREE, domain);
6306 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6307 SET_DECL_RTL (index, index_r);
6308 store_expr (lo_index, index_r, 0, false);
6310 /* Build the head of the loop. */
6311 do_pending_stack_adjust ();
6312 emit_label (loop_start);
6314 /* Assign value to element index. */
6315 position =
6316 fold_convert (ssizetype,
6317 fold_build2 (MINUS_EXPR,
6318 TREE_TYPE (index),
6319 index,
6320 TYPE_MIN_VALUE (domain)));
6322 position =
6323 size_binop (MULT_EXPR, position,
6324 fold_convert (ssizetype,
6325 TYPE_SIZE_UNIT (elttype)));
6327 pos_rtx = expand_normal (position);
6328 xtarget = offset_address (target, pos_rtx,
6329 highest_pow2_factor (position));
6330 xtarget = adjust_address (xtarget, mode, 0);
6331 if (TREE_CODE (value) == CONSTRUCTOR)
6332 store_constructor (value, xtarget, cleared,
6333 bitsize / BITS_PER_UNIT);
6334 else
6335 store_expr (value, xtarget, 0, false);
6337 /* Generate a conditional jump to exit the loop. */
6338 exit_cond = build2 (LT_EXPR, integer_type_node,
6339 index, hi_index);
6340 jumpif (exit_cond, loop_end, -1);
6342 /* Update the loop counter, and jump to the head of
6343 the loop. */
6344 expand_assignment (index,
6345 build2 (PLUS_EXPR, TREE_TYPE (index),
6346 index, integer_one_node),
6347 false);
6349 emit_jump (loop_start);
6351 /* Build the end of the loop. */
6352 emit_label (loop_end);
6355 else if ((index != 0 && ! tree_fits_shwi_p (index))
6356 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6358 tree position;
6360 if (index == 0)
6361 index = ssize_int (1);
6363 if (minelt)
6364 index = fold_convert (ssizetype,
6365 fold_build2 (MINUS_EXPR,
6366 TREE_TYPE (index),
6367 index,
6368 TYPE_MIN_VALUE (domain)));
6370 position =
6371 size_binop (MULT_EXPR, index,
6372 fold_convert (ssizetype,
6373 TYPE_SIZE_UNIT (elttype)));
6374 xtarget = offset_address (target,
6375 expand_normal (position),
6376 highest_pow2_factor (position));
6377 xtarget = adjust_address (xtarget, mode, 0);
6378 store_expr (value, xtarget, 0, false);
6380 else
6382 if (index != 0)
6383 bitpos = ((tree_to_shwi (index) - minelt)
6384 * tree_to_uhwi (TYPE_SIZE (elttype)));
6385 else
6386 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6388 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6389 && TREE_CODE (type) == ARRAY_TYPE
6390 && TYPE_NONALIASED_COMPONENT (type))
6392 target = copy_rtx (target);
6393 MEM_KEEP_ALIAS_SET_P (target) = 1;
6395 store_constructor_field (target, bitsize, bitpos, mode, value,
6396 cleared, get_alias_set (elttype));
6399 break;
6402 case VECTOR_TYPE:
6404 unsigned HOST_WIDE_INT idx;
6405 constructor_elt *ce;
6406 int i;
6407 int need_to_clear;
6408 int icode = CODE_FOR_nothing;
6409 tree elttype = TREE_TYPE (type);
6410 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6411 machine_mode eltmode = TYPE_MODE (elttype);
6412 HOST_WIDE_INT bitsize;
6413 HOST_WIDE_INT bitpos;
6414 rtvec vector = NULL;
6415 unsigned n_elts;
6416 alias_set_type alias;
6418 gcc_assert (eltmode != BLKmode);
6420 n_elts = TYPE_VECTOR_SUBPARTS (type);
6421 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6423 machine_mode mode = GET_MODE (target);
6425 icode = (int) optab_handler (vec_init_optab, mode);
6426 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6427 if (icode != CODE_FOR_nothing)
6429 tree value;
6431 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6432 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6434 icode = CODE_FOR_nothing;
6435 break;
6438 if (icode != CODE_FOR_nothing)
6440 unsigned int i;
6442 vector = rtvec_alloc (n_elts);
6443 for (i = 0; i < n_elts; i++)
6444 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6448 /* If the constructor has fewer elements than the vector,
6449 clear the whole array first. Similarly if this is static
6450 constructor of a non-BLKmode object. */
6451 if (cleared)
6452 need_to_clear = 0;
6453 else if (REG_P (target) && TREE_STATIC (exp))
6454 need_to_clear = 1;
6455 else
6457 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6458 tree value;
6460 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6462 int n_elts_here = tree_to_uhwi
6463 (int_const_binop (TRUNC_DIV_EXPR,
6464 TYPE_SIZE (TREE_TYPE (value)),
6465 TYPE_SIZE (elttype)));
6467 count += n_elts_here;
6468 if (mostly_zeros_p (value))
6469 zero_count += n_elts_here;
6472 /* Clear the entire vector first if there are any missing elements,
6473 or if the incidence of zero elements is >= 75%. */
6474 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6477 if (need_to_clear && size > 0 && !vector)
6479 if (REG_P (target))
6480 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6481 else
6482 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6483 cleared = 1;
6486 /* Inform later passes that the old value is dead. */
6487 if (!cleared && !vector && REG_P (target))
6488 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6490 if (MEM_P (target))
6491 alias = MEM_ALIAS_SET (target);
6492 else
6493 alias = get_alias_set (elttype);
6495 /* Store each element of the constructor into the corresponding
6496 element of TARGET, determined by counting the elements. */
6497 for (idx = 0, i = 0;
6498 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6499 idx++, i += bitsize / elt_size)
6501 HOST_WIDE_INT eltpos;
6502 tree value = ce->value;
6504 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6505 if (cleared && initializer_zerop (value))
6506 continue;
6508 if (ce->index)
6509 eltpos = tree_to_uhwi (ce->index);
6510 else
6511 eltpos = i;
6513 if (vector)
6515 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6516 elements. */
6517 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6518 RTVEC_ELT (vector, eltpos)
6519 = expand_normal (value);
6521 else
6523 machine_mode value_mode =
6524 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6525 ? TYPE_MODE (TREE_TYPE (value))
6526 : eltmode;
6527 bitpos = eltpos * elt_size;
6528 store_constructor_field (target, bitsize, bitpos, value_mode,
6529 value, cleared, alias);
6533 if (vector)
6534 emit_insn (GEN_FCN (icode)
6535 (target,
6536 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6537 break;
6540 default:
6541 gcc_unreachable ();
6545 /* Store the value of EXP (an expression tree)
6546 into a subfield of TARGET which has mode MODE and occupies
6547 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6548 If MODE is VOIDmode, it means that we are storing into a bit-field.
6550 BITREGION_START is bitpos of the first bitfield in this region.
6551 BITREGION_END is the bitpos of the ending bitfield in this region.
6552 These two fields are 0, if the C++ memory model does not apply,
6553 or we are not interested in keeping track of bitfield regions.
6555 Always return const0_rtx unless we have something particular to
6556 return.
6558 ALIAS_SET is the alias set for the destination. This value will
6559 (in general) be different from that for TARGET, since TARGET is a
6560 reference to the containing structure.
6562 If NONTEMPORAL is true, try generating a nontemporal store. */
6564 static rtx
6565 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6566 unsigned HOST_WIDE_INT bitregion_start,
6567 unsigned HOST_WIDE_INT bitregion_end,
6568 machine_mode mode, tree exp,
6569 alias_set_type alias_set, bool nontemporal)
6571 if (TREE_CODE (exp) == ERROR_MARK)
6572 return const0_rtx;
6574 /* If we have nothing to store, do nothing unless the expression has
6575 side-effects. */
6576 if (bitsize == 0)
6577 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6579 if (GET_CODE (target) == CONCAT)
6581 /* We're storing into a struct containing a single __complex. */
6583 gcc_assert (!bitpos);
6584 return store_expr (exp, target, 0, nontemporal);
6587 /* If the structure is in a register or if the component
6588 is a bit field, we cannot use addressing to access it.
6589 Use bit-field techniques or SUBREG to store in it. */
6591 if (mode == VOIDmode
6592 || (mode != BLKmode && ! direct_store[(int) mode]
6593 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6594 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6595 || REG_P (target)
6596 || GET_CODE (target) == SUBREG
6597 /* If the field isn't aligned enough to store as an ordinary memref,
6598 store it as a bit field. */
6599 || (mode != BLKmode
6600 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6601 || bitpos % GET_MODE_ALIGNMENT (mode))
6602 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6603 || (bitpos % BITS_PER_UNIT != 0)))
6604 || (bitsize >= 0 && mode != BLKmode
6605 && GET_MODE_BITSIZE (mode) > bitsize)
6606 /* If the RHS and field are a constant size and the size of the
6607 RHS isn't the same size as the bitfield, we must use bitfield
6608 operations. */
6609 || (bitsize >= 0
6610 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6611 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6612 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6613 decl we must use bitfield operations. */
6614 || (bitsize >= 0
6615 && TREE_CODE (exp) == MEM_REF
6616 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6617 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6618 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6619 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6621 rtx temp;
6622 gimple nop_def;
6624 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6625 implies a mask operation. If the precision is the same size as
6626 the field we're storing into, that mask is redundant. This is
6627 particularly common with bit field assignments generated by the
6628 C front end. */
6629 nop_def = get_def_for_expr (exp, NOP_EXPR);
6630 if (nop_def)
6632 tree type = TREE_TYPE (exp);
6633 if (INTEGRAL_TYPE_P (type)
6634 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6635 && bitsize == TYPE_PRECISION (type))
6637 tree op = gimple_assign_rhs1 (nop_def);
6638 type = TREE_TYPE (op);
6639 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6640 exp = op;
6644 temp = expand_normal (exp);
6646 /* If BITSIZE is narrower than the size of the type of EXP
6647 we will be narrowing TEMP. Normally, what's wanted are the
6648 low-order bits. However, if EXP's type is a record and this is
6649 big-endian machine, we want the upper BITSIZE bits. */
6650 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6651 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6652 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6653 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6654 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6655 NULL_RTX, 1);
6657 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6658 if (mode != VOIDmode && mode != BLKmode
6659 && mode != TYPE_MODE (TREE_TYPE (exp)))
6660 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6662 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6663 are both BLKmode, both must be in memory and BITPOS must be aligned
6664 on a byte boundary. If so, we simply do a block copy. Likewise for
6665 a BLKmode-like TARGET. */
6666 if (GET_CODE (temp) != PARALLEL
6667 && GET_MODE (temp) == BLKmode
6668 && (GET_MODE (target) == BLKmode
6669 || (MEM_P (target)
6670 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6671 && (bitpos % BITS_PER_UNIT) == 0
6672 && (bitsize % BITS_PER_UNIT) == 0)))
6674 gcc_assert (MEM_P (target) && MEM_P (temp)
6675 && (bitpos % BITS_PER_UNIT) == 0);
6677 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6678 emit_block_move (target, temp,
6679 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6680 / BITS_PER_UNIT),
6681 BLOCK_OP_NORMAL);
6683 return const0_rtx;
6686 /* Handle calls that return values in multiple non-contiguous locations.
6687 The Irix 6 ABI has examples of this. */
6688 if (GET_CODE (temp) == PARALLEL)
6690 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6691 rtx temp_target;
6692 if (mode == BLKmode || mode == VOIDmode)
6693 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6694 temp_target = gen_reg_rtx (mode);
6695 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6696 temp = temp_target;
6698 else if (mode == BLKmode)
6700 /* Handle calls that return BLKmode values in registers. */
6701 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6703 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6704 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6705 temp = temp_target;
6707 else
6709 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6710 rtx temp_target;
6711 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6712 temp_target = gen_reg_rtx (mode);
6713 temp_target
6714 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6715 temp_target, mode, mode);
6716 temp = temp_target;
6720 /* Store the value in the bitfield. */
6721 store_bit_field (target, bitsize, bitpos,
6722 bitregion_start, bitregion_end,
6723 mode, temp);
6725 return const0_rtx;
6727 else
6729 /* Now build a reference to just the desired component. */
6730 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6732 if (to_rtx == target)
6733 to_rtx = copy_rtx (to_rtx);
6735 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6736 set_mem_alias_set (to_rtx, alias_set);
6738 return store_expr (exp, to_rtx, 0, nontemporal);
6742 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6743 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6744 codes and find the ultimate containing object, which we return.
6746 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6747 bit position, and *PUNSIGNEDP to the signedness of the field.
6748 If the position of the field is variable, we store a tree
6749 giving the variable offset (in units) in *POFFSET.
6750 This offset is in addition to the bit position.
6751 If the position is not variable, we store 0 in *POFFSET.
6753 If any of the extraction expressions is volatile,
6754 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6756 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6757 Otherwise, it is a mode that can be used to access the field.
6759 If the field describes a variable-sized object, *PMODE is set to
6760 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6761 this case, but the address of the object can be found.
6763 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6764 look through nodes that serve as markers of a greater alignment than
6765 the one that can be deduced from the expression. These nodes make it
6766 possible for front-ends to prevent temporaries from being created by
6767 the middle-end on alignment considerations. For that purpose, the
6768 normal operating mode at high-level is to always pass FALSE so that
6769 the ultimate containing object is really returned; moreover, the
6770 associated predicate handled_component_p will always return TRUE
6771 on these nodes, thus indicating that they are essentially handled
6772 by get_inner_reference. TRUE should only be passed when the caller
6773 is scanning the expression in order to build another representation
6774 and specifically knows how to handle these nodes; as such, this is
6775 the normal operating mode in the RTL expanders. */
6777 tree
6778 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6779 HOST_WIDE_INT *pbitpos, tree *poffset,
6780 machine_mode *pmode, int *punsignedp,
6781 int *pvolatilep, bool keep_aligning)
6783 tree size_tree = 0;
6784 machine_mode mode = VOIDmode;
6785 bool blkmode_bitfield = false;
6786 tree offset = size_zero_node;
6787 offset_int bit_offset = 0;
6789 /* First get the mode, signedness, and size. We do this from just the
6790 outermost expression. */
6791 *pbitsize = -1;
6792 if (TREE_CODE (exp) == COMPONENT_REF)
6794 tree field = TREE_OPERAND (exp, 1);
6795 size_tree = DECL_SIZE (field);
6796 if (flag_strict_volatile_bitfields > 0
6797 && TREE_THIS_VOLATILE (exp)
6798 && DECL_BIT_FIELD_TYPE (field)
6799 && DECL_MODE (field) != BLKmode)
6800 /* Volatile bitfields should be accessed in the mode of the
6801 field's type, not the mode computed based on the bit
6802 size. */
6803 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6804 else if (!DECL_BIT_FIELD (field))
6805 mode = DECL_MODE (field);
6806 else if (DECL_MODE (field) == BLKmode)
6807 blkmode_bitfield = true;
6809 *punsignedp = DECL_UNSIGNED (field);
6811 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6813 size_tree = TREE_OPERAND (exp, 1);
6814 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6815 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6817 /* For vector types, with the correct size of access, use the mode of
6818 inner type. */
6819 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6820 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6821 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6822 mode = TYPE_MODE (TREE_TYPE (exp));
6824 else
6826 mode = TYPE_MODE (TREE_TYPE (exp));
6827 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6829 if (mode == BLKmode)
6830 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6831 else
6832 *pbitsize = GET_MODE_BITSIZE (mode);
6835 if (size_tree != 0)
6837 if (! tree_fits_uhwi_p (size_tree))
6838 mode = BLKmode, *pbitsize = -1;
6839 else
6840 *pbitsize = tree_to_uhwi (size_tree);
6843 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6844 and find the ultimate containing object. */
6845 while (1)
6847 switch (TREE_CODE (exp))
6849 case BIT_FIELD_REF:
6850 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6851 break;
6853 case COMPONENT_REF:
6855 tree field = TREE_OPERAND (exp, 1);
6856 tree this_offset = component_ref_field_offset (exp);
6858 /* If this field hasn't been filled in yet, don't go past it.
6859 This should only happen when folding expressions made during
6860 type construction. */
6861 if (this_offset == 0)
6862 break;
6864 offset = size_binop (PLUS_EXPR, offset, this_offset);
6865 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6867 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6869 break;
6871 case ARRAY_REF:
6872 case ARRAY_RANGE_REF:
6874 tree index = TREE_OPERAND (exp, 1);
6875 tree low_bound = array_ref_low_bound (exp);
6876 tree unit_size = array_ref_element_size (exp);
6878 /* We assume all arrays have sizes that are a multiple of a byte.
6879 First subtract the lower bound, if any, in the type of the
6880 index, then convert to sizetype and multiply by the size of
6881 the array element. */
6882 if (! integer_zerop (low_bound))
6883 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6884 index, low_bound);
6886 offset = size_binop (PLUS_EXPR, offset,
6887 size_binop (MULT_EXPR,
6888 fold_convert (sizetype, index),
6889 unit_size));
6891 break;
6893 case REALPART_EXPR:
6894 break;
6896 case IMAGPART_EXPR:
6897 bit_offset += *pbitsize;
6898 break;
6900 case VIEW_CONVERT_EXPR:
6901 if (keep_aligning && STRICT_ALIGNMENT
6902 && (TYPE_ALIGN (TREE_TYPE (exp))
6903 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6904 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6905 < BIGGEST_ALIGNMENT)
6906 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6907 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6908 goto done;
6909 break;
6911 case MEM_REF:
6912 /* Hand back the decl for MEM[&decl, off]. */
6913 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6915 tree off = TREE_OPERAND (exp, 1);
6916 if (!integer_zerop (off))
6918 offset_int boff, coff = mem_ref_offset (exp);
6919 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6920 bit_offset += boff;
6922 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6924 goto done;
6926 default:
6927 goto done;
6930 /* If any reference in the chain is volatile, the effect is volatile. */
6931 if (TREE_THIS_VOLATILE (exp))
6932 *pvolatilep = 1;
6934 exp = TREE_OPERAND (exp, 0);
6936 done:
6938 /* If OFFSET is constant, see if we can return the whole thing as a
6939 constant bit position. Make sure to handle overflow during
6940 this conversion. */
6941 if (TREE_CODE (offset) == INTEGER_CST)
6943 offset_int tem = wi::sext (wi::to_offset (offset),
6944 TYPE_PRECISION (sizetype));
6945 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6946 tem += bit_offset;
6947 if (wi::fits_shwi_p (tem))
6949 *pbitpos = tem.to_shwi ();
6950 *poffset = offset = NULL_TREE;
6954 /* Otherwise, split it up. */
6955 if (offset)
6957 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6958 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
6960 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6961 offset_int tem = bit_offset.and_not (mask);
6962 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6963 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6964 bit_offset -= tem;
6965 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6966 offset = size_binop (PLUS_EXPR, offset,
6967 wide_int_to_tree (sizetype, tem));
6970 *pbitpos = bit_offset.to_shwi ();
6971 *poffset = offset;
6974 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6975 if (mode == VOIDmode
6976 && blkmode_bitfield
6977 && (*pbitpos % BITS_PER_UNIT) == 0
6978 && (*pbitsize % BITS_PER_UNIT) == 0)
6979 *pmode = BLKmode;
6980 else
6981 *pmode = mode;
6983 return exp;
6986 /* Return a tree of sizetype representing the size, in bytes, of the element
6987 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6989 tree
6990 array_ref_element_size (tree exp)
6992 tree aligned_size = TREE_OPERAND (exp, 3);
6993 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6994 location_t loc = EXPR_LOCATION (exp);
6996 /* If a size was specified in the ARRAY_REF, it's the size measured
6997 in alignment units of the element type. So multiply by that value. */
6998 if (aligned_size)
7000 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7001 sizetype from another type of the same width and signedness. */
7002 if (TREE_TYPE (aligned_size) != sizetype)
7003 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
7004 return size_binop_loc (loc, MULT_EXPR, aligned_size,
7005 size_int (TYPE_ALIGN_UNIT (elmt_type)));
7008 /* Otherwise, take the size from that of the element type. Substitute
7009 any PLACEHOLDER_EXPR that we have. */
7010 else
7011 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
7014 /* Return a tree representing the lower bound of the array mentioned in
7015 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
7017 tree
7018 array_ref_low_bound (tree exp)
7020 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
7022 /* If a lower bound is specified in EXP, use it. */
7023 if (TREE_OPERAND (exp, 2))
7024 return TREE_OPERAND (exp, 2);
7026 /* Otherwise, if there is a domain type and it has a lower bound, use it,
7027 substituting for a PLACEHOLDER_EXPR as needed. */
7028 if (domain_type && TYPE_MIN_VALUE (domain_type))
7029 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
7031 /* Otherwise, return a zero of the appropriate type. */
7032 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
7035 /* Returns true if REF is an array reference to an array at the end of
7036 a structure. If this is the case, the array may be allocated larger
7037 than its upper bound implies. */
7039 bool
7040 array_at_struct_end_p (tree ref)
7042 if (TREE_CODE (ref) != ARRAY_REF
7043 && TREE_CODE (ref) != ARRAY_RANGE_REF)
7044 return false;
7046 while (handled_component_p (ref))
7048 /* If the reference chain contains a component reference to a
7049 non-union type and there follows another field the reference
7050 is not at the end of a structure. */
7051 if (TREE_CODE (ref) == COMPONENT_REF
7052 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
7054 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
7055 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
7056 nextf = DECL_CHAIN (nextf);
7057 if (nextf)
7058 return false;
7061 ref = TREE_OPERAND (ref, 0);
7064 /* If the reference is based on a declared entity, the size of the array
7065 is constrained by its given domain. */
7066 if (DECL_P (ref))
7067 return false;
7069 return true;
7072 /* Return a tree representing the upper bound of the array mentioned in
7073 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
7075 tree
7076 array_ref_up_bound (tree exp)
7078 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
7080 /* If there is a domain type and it has an upper bound, use it, substituting
7081 for a PLACEHOLDER_EXPR as needed. */
7082 if (domain_type && TYPE_MAX_VALUE (domain_type))
7083 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
7085 /* Otherwise fail. */
7086 return NULL_TREE;
7089 /* Return a tree representing the offset, in bytes, of the field referenced
7090 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
7092 tree
7093 component_ref_field_offset (tree exp)
7095 tree aligned_offset = TREE_OPERAND (exp, 2);
7096 tree field = TREE_OPERAND (exp, 1);
7097 location_t loc = EXPR_LOCATION (exp);
7099 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
7100 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
7101 value. */
7102 if (aligned_offset)
7104 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7105 sizetype from another type of the same width and signedness. */
7106 if (TREE_TYPE (aligned_offset) != sizetype)
7107 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
7108 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
7109 size_int (DECL_OFFSET_ALIGN (field)
7110 / BITS_PER_UNIT));
7113 /* Otherwise, take the offset from that of the field. Substitute
7114 any PLACEHOLDER_EXPR that we have. */
7115 else
7116 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
7119 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7121 static unsigned HOST_WIDE_INT
7122 target_align (const_tree target)
7124 /* We might have a chain of nested references with intermediate misaligning
7125 bitfields components, so need to recurse to find out. */
7127 unsigned HOST_WIDE_INT this_align, outer_align;
7129 switch (TREE_CODE (target))
7131 case BIT_FIELD_REF:
7132 return 1;
7134 case COMPONENT_REF:
7135 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7136 outer_align = target_align (TREE_OPERAND (target, 0));
7137 return MIN (this_align, outer_align);
7139 case ARRAY_REF:
7140 case ARRAY_RANGE_REF:
7141 this_align = TYPE_ALIGN (TREE_TYPE (target));
7142 outer_align = target_align (TREE_OPERAND (target, 0));
7143 return MIN (this_align, outer_align);
7145 CASE_CONVERT:
7146 case NON_LVALUE_EXPR:
7147 case VIEW_CONVERT_EXPR:
7148 this_align = TYPE_ALIGN (TREE_TYPE (target));
7149 outer_align = target_align (TREE_OPERAND (target, 0));
7150 return MAX (this_align, outer_align);
7152 default:
7153 return TYPE_ALIGN (TREE_TYPE (target));
7158 /* Given an rtx VALUE that may contain additions and multiplications, return
7159 an equivalent value that just refers to a register, memory, or constant.
7160 This is done by generating instructions to perform the arithmetic and
7161 returning a pseudo-register containing the value.
7163 The returned value may be a REG, SUBREG, MEM or constant. */
7166 force_operand (rtx value, rtx target)
7168 rtx op1, op2;
7169 /* Use subtarget as the target for operand 0 of a binary operation. */
7170 rtx subtarget = get_subtarget (target);
7171 enum rtx_code code = GET_CODE (value);
7173 /* Check for subreg applied to an expression produced by loop optimizer. */
7174 if (code == SUBREG
7175 && !REG_P (SUBREG_REG (value))
7176 && !MEM_P (SUBREG_REG (value)))
7178 value
7179 = simplify_gen_subreg (GET_MODE (value),
7180 force_reg (GET_MODE (SUBREG_REG (value)),
7181 force_operand (SUBREG_REG (value),
7182 NULL_RTX)),
7183 GET_MODE (SUBREG_REG (value)),
7184 SUBREG_BYTE (value));
7185 code = GET_CODE (value);
7188 /* Check for a PIC address load. */
7189 if ((code == PLUS || code == MINUS)
7190 && XEXP (value, 0) == pic_offset_table_rtx
7191 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7192 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7193 || GET_CODE (XEXP (value, 1)) == CONST))
7195 if (!subtarget)
7196 subtarget = gen_reg_rtx (GET_MODE (value));
7197 emit_move_insn (subtarget, value);
7198 return subtarget;
7201 if (ARITHMETIC_P (value))
7203 op2 = XEXP (value, 1);
7204 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7205 subtarget = 0;
7206 if (code == MINUS && CONST_INT_P (op2))
7208 code = PLUS;
7209 op2 = negate_rtx (GET_MODE (value), op2);
7212 /* Check for an addition with OP2 a constant integer and our first
7213 operand a PLUS of a virtual register and something else. In that
7214 case, we want to emit the sum of the virtual register and the
7215 constant first and then add the other value. This allows virtual
7216 register instantiation to simply modify the constant rather than
7217 creating another one around this addition. */
7218 if (code == PLUS && CONST_INT_P (op2)
7219 && GET_CODE (XEXP (value, 0)) == PLUS
7220 && REG_P (XEXP (XEXP (value, 0), 0))
7221 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7222 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7224 rtx temp = expand_simple_binop (GET_MODE (value), code,
7225 XEXP (XEXP (value, 0), 0), op2,
7226 subtarget, 0, OPTAB_LIB_WIDEN);
7227 return expand_simple_binop (GET_MODE (value), code, temp,
7228 force_operand (XEXP (XEXP (value,
7229 0), 1), 0),
7230 target, 0, OPTAB_LIB_WIDEN);
7233 op1 = force_operand (XEXP (value, 0), subtarget);
7234 op2 = force_operand (op2, NULL_RTX);
7235 switch (code)
7237 case MULT:
7238 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7239 case DIV:
7240 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7241 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7242 target, 1, OPTAB_LIB_WIDEN);
7243 else
7244 return expand_divmod (0,
7245 FLOAT_MODE_P (GET_MODE (value))
7246 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7247 GET_MODE (value), op1, op2, target, 0);
7248 case MOD:
7249 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7250 target, 0);
7251 case UDIV:
7252 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7253 target, 1);
7254 case UMOD:
7255 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7256 target, 1);
7257 case ASHIFTRT:
7258 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7259 target, 0, OPTAB_LIB_WIDEN);
7260 default:
7261 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7262 target, 1, OPTAB_LIB_WIDEN);
7265 if (UNARY_P (value))
7267 if (!target)
7268 target = gen_reg_rtx (GET_MODE (value));
7269 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7270 switch (code)
7272 case ZERO_EXTEND:
7273 case SIGN_EXTEND:
7274 case TRUNCATE:
7275 case FLOAT_EXTEND:
7276 case FLOAT_TRUNCATE:
7277 convert_move (target, op1, code == ZERO_EXTEND);
7278 return target;
7280 case FIX:
7281 case UNSIGNED_FIX:
7282 expand_fix (target, op1, code == UNSIGNED_FIX);
7283 return target;
7285 case FLOAT:
7286 case UNSIGNED_FLOAT:
7287 expand_float (target, op1, code == UNSIGNED_FLOAT);
7288 return target;
7290 default:
7291 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7295 #ifdef INSN_SCHEDULING
7296 /* On machines that have insn scheduling, we want all memory reference to be
7297 explicit, so we need to deal with such paradoxical SUBREGs. */
7298 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7299 value
7300 = simplify_gen_subreg (GET_MODE (value),
7301 force_reg (GET_MODE (SUBREG_REG (value)),
7302 force_operand (SUBREG_REG (value),
7303 NULL_RTX)),
7304 GET_MODE (SUBREG_REG (value)),
7305 SUBREG_BYTE (value));
7306 #endif
7308 return value;
7311 /* Subroutine of expand_expr: return nonzero iff there is no way that
7312 EXP can reference X, which is being modified. TOP_P is nonzero if this
7313 call is going to be used to determine whether we need a temporary
7314 for EXP, as opposed to a recursive call to this function.
7316 It is always safe for this routine to return zero since it merely
7317 searches for optimization opportunities. */
7320 safe_from_p (const_rtx x, tree exp, int top_p)
7322 rtx exp_rtl = 0;
7323 int i, nops;
7325 if (x == 0
7326 /* If EXP has varying size, we MUST use a target since we currently
7327 have no way of allocating temporaries of variable size
7328 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7329 So we assume here that something at a higher level has prevented a
7330 clash. This is somewhat bogus, but the best we can do. Only
7331 do this when X is BLKmode and when we are at the top level. */
7332 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7333 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7334 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7335 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7336 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7337 != INTEGER_CST)
7338 && GET_MODE (x) == BLKmode)
7339 /* If X is in the outgoing argument area, it is always safe. */
7340 || (MEM_P (x)
7341 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7342 || (GET_CODE (XEXP (x, 0)) == PLUS
7343 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7344 return 1;
7346 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7347 find the underlying pseudo. */
7348 if (GET_CODE (x) == SUBREG)
7350 x = SUBREG_REG (x);
7351 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7352 return 0;
7355 /* Now look at our tree code and possibly recurse. */
7356 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7358 case tcc_declaration:
7359 exp_rtl = DECL_RTL_IF_SET (exp);
7360 break;
7362 case tcc_constant:
7363 return 1;
7365 case tcc_exceptional:
7366 if (TREE_CODE (exp) == TREE_LIST)
7368 while (1)
7370 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7371 return 0;
7372 exp = TREE_CHAIN (exp);
7373 if (!exp)
7374 return 1;
7375 if (TREE_CODE (exp) != TREE_LIST)
7376 return safe_from_p (x, exp, 0);
7379 else if (TREE_CODE (exp) == CONSTRUCTOR)
7381 constructor_elt *ce;
7382 unsigned HOST_WIDE_INT idx;
7384 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7385 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7386 || !safe_from_p (x, ce->value, 0))
7387 return 0;
7388 return 1;
7390 else if (TREE_CODE (exp) == ERROR_MARK)
7391 return 1; /* An already-visited SAVE_EXPR? */
7392 else
7393 return 0;
7395 case tcc_statement:
7396 /* The only case we look at here is the DECL_INITIAL inside a
7397 DECL_EXPR. */
7398 return (TREE_CODE (exp) != DECL_EXPR
7399 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7400 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7401 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7403 case tcc_binary:
7404 case tcc_comparison:
7405 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7406 return 0;
7407 /* Fall through. */
7409 case tcc_unary:
7410 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7412 case tcc_expression:
7413 case tcc_reference:
7414 case tcc_vl_exp:
7415 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7416 the expression. If it is set, we conflict iff we are that rtx or
7417 both are in memory. Otherwise, we check all operands of the
7418 expression recursively. */
7420 switch (TREE_CODE (exp))
7422 case ADDR_EXPR:
7423 /* If the operand is static or we are static, we can't conflict.
7424 Likewise if we don't conflict with the operand at all. */
7425 if (staticp (TREE_OPERAND (exp, 0))
7426 || TREE_STATIC (exp)
7427 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7428 return 1;
7430 /* Otherwise, the only way this can conflict is if we are taking
7431 the address of a DECL a that address if part of X, which is
7432 very rare. */
7433 exp = TREE_OPERAND (exp, 0);
7434 if (DECL_P (exp))
7436 if (!DECL_RTL_SET_P (exp)
7437 || !MEM_P (DECL_RTL (exp)))
7438 return 0;
7439 else
7440 exp_rtl = XEXP (DECL_RTL (exp), 0);
7442 break;
7444 case MEM_REF:
7445 if (MEM_P (x)
7446 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7447 get_alias_set (exp)))
7448 return 0;
7449 break;
7451 case CALL_EXPR:
7452 /* Assume that the call will clobber all hard registers and
7453 all of memory. */
7454 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7455 || MEM_P (x))
7456 return 0;
7457 break;
7459 case WITH_CLEANUP_EXPR:
7460 case CLEANUP_POINT_EXPR:
7461 /* Lowered by gimplify.c. */
7462 gcc_unreachable ();
7464 case SAVE_EXPR:
7465 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7467 default:
7468 break;
7471 /* If we have an rtx, we do not need to scan our operands. */
7472 if (exp_rtl)
7473 break;
7475 nops = TREE_OPERAND_LENGTH (exp);
7476 for (i = 0; i < nops; i++)
7477 if (TREE_OPERAND (exp, i) != 0
7478 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7479 return 0;
7481 break;
7483 case tcc_type:
7484 /* Should never get a type here. */
7485 gcc_unreachable ();
7488 /* If we have an rtl, find any enclosed object. Then see if we conflict
7489 with it. */
7490 if (exp_rtl)
7492 if (GET_CODE (exp_rtl) == SUBREG)
7494 exp_rtl = SUBREG_REG (exp_rtl);
7495 if (REG_P (exp_rtl)
7496 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7497 return 0;
7500 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7501 are memory and they conflict. */
7502 return ! (rtx_equal_p (x, exp_rtl)
7503 || (MEM_P (x) && MEM_P (exp_rtl)
7504 && true_dependence (exp_rtl, VOIDmode, x)));
7507 /* If we reach here, it is safe. */
7508 return 1;
7512 /* Return the highest power of two that EXP is known to be a multiple of.
7513 This is used in updating alignment of MEMs in array references. */
7515 unsigned HOST_WIDE_INT
7516 highest_pow2_factor (const_tree exp)
7518 unsigned HOST_WIDE_INT ret;
7519 int trailing_zeros = tree_ctz (exp);
7520 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7521 return BIGGEST_ALIGNMENT;
7522 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7523 if (ret > BIGGEST_ALIGNMENT)
7524 return BIGGEST_ALIGNMENT;
7525 return ret;
7528 /* Similar, except that the alignment requirements of TARGET are
7529 taken into account. Assume it is at least as aligned as its
7530 type, unless it is a COMPONENT_REF in which case the layout of
7531 the structure gives the alignment. */
7533 static unsigned HOST_WIDE_INT
7534 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7536 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7537 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7539 return MAX (factor, talign);
7542 #ifdef HAVE_conditional_move
7543 /* Convert the tree comparison code TCODE to the rtl one where the
7544 signedness is UNSIGNEDP. */
7546 static enum rtx_code
7547 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7549 enum rtx_code code;
7550 switch (tcode)
7552 case EQ_EXPR:
7553 code = EQ;
7554 break;
7555 case NE_EXPR:
7556 code = NE;
7557 break;
7558 case LT_EXPR:
7559 code = unsignedp ? LTU : LT;
7560 break;
7561 case LE_EXPR:
7562 code = unsignedp ? LEU : LE;
7563 break;
7564 case GT_EXPR:
7565 code = unsignedp ? GTU : GT;
7566 break;
7567 case GE_EXPR:
7568 code = unsignedp ? GEU : GE;
7569 break;
7570 case UNORDERED_EXPR:
7571 code = UNORDERED;
7572 break;
7573 case ORDERED_EXPR:
7574 code = ORDERED;
7575 break;
7576 case UNLT_EXPR:
7577 code = UNLT;
7578 break;
7579 case UNLE_EXPR:
7580 code = UNLE;
7581 break;
7582 case UNGT_EXPR:
7583 code = UNGT;
7584 break;
7585 case UNGE_EXPR:
7586 code = UNGE;
7587 break;
7588 case UNEQ_EXPR:
7589 code = UNEQ;
7590 break;
7591 case LTGT_EXPR:
7592 code = LTGT;
7593 break;
7595 default:
7596 gcc_unreachable ();
7598 return code;
7600 #endif
7602 /* Subroutine of expand_expr. Expand the two operands of a binary
7603 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7604 The value may be stored in TARGET if TARGET is nonzero. The
7605 MODIFIER argument is as documented by expand_expr. */
7607 void
7608 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7609 enum expand_modifier modifier)
7611 if (! safe_from_p (target, exp1, 1))
7612 target = 0;
7613 if (operand_equal_p (exp0, exp1, 0))
7615 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7616 *op1 = copy_rtx (*op0);
7618 else
7620 /* If we need to preserve evaluation order, copy exp0 into its own
7621 temporary variable so that it can't be clobbered by exp1. */
7622 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7623 exp0 = save_expr (exp0);
7624 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7625 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7630 /* Return a MEM that contains constant EXP. DEFER is as for
7631 output_constant_def and MODIFIER is as for expand_expr. */
7633 static rtx
7634 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7636 rtx mem;
7638 mem = output_constant_def (exp, defer);
7639 if (modifier != EXPAND_INITIALIZER)
7640 mem = use_anchored_address (mem);
7641 return mem;
7644 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7645 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7647 static rtx
7648 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7649 enum expand_modifier modifier, addr_space_t as)
7651 rtx result, subtarget;
7652 tree inner, offset;
7653 HOST_WIDE_INT bitsize, bitpos;
7654 int volatilep, unsignedp;
7655 machine_mode mode1;
7657 /* If we are taking the address of a constant and are at the top level,
7658 we have to use output_constant_def since we can't call force_const_mem
7659 at top level. */
7660 /* ??? This should be considered a front-end bug. We should not be
7661 generating ADDR_EXPR of something that isn't an LVALUE. The only
7662 exception here is STRING_CST. */
7663 if (CONSTANT_CLASS_P (exp))
7665 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7666 if (modifier < EXPAND_SUM)
7667 result = force_operand (result, target);
7668 return result;
7671 /* Everything must be something allowed by is_gimple_addressable. */
7672 switch (TREE_CODE (exp))
7674 case INDIRECT_REF:
7675 /* This case will happen via recursion for &a->b. */
7676 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7678 case MEM_REF:
7680 tree tem = TREE_OPERAND (exp, 0);
7681 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7682 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7683 return expand_expr (tem, target, tmode, modifier);
7686 case CONST_DECL:
7687 /* Expand the initializer like constants above. */
7688 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7689 0, modifier), 0);
7690 if (modifier < EXPAND_SUM)
7691 result = force_operand (result, target);
7692 return result;
7694 case REALPART_EXPR:
7695 /* The real part of the complex number is always first, therefore
7696 the address is the same as the address of the parent object. */
7697 offset = 0;
7698 bitpos = 0;
7699 inner = TREE_OPERAND (exp, 0);
7700 break;
7702 case IMAGPART_EXPR:
7703 /* The imaginary part of the complex number is always second.
7704 The expression is therefore always offset by the size of the
7705 scalar type. */
7706 offset = 0;
7707 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7708 inner = TREE_OPERAND (exp, 0);
7709 break;
7711 case COMPOUND_LITERAL_EXPR:
7712 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7713 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7714 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7715 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7716 the initializers aren't gimplified. */
7717 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7718 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7719 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7720 target, tmode, modifier, as);
7721 /* FALLTHRU */
7722 default:
7723 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7724 expand_expr, as that can have various side effects; LABEL_DECLs for
7725 example, may not have their DECL_RTL set yet. Expand the rtl of
7726 CONSTRUCTORs too, which should yield a memory reference for the
7727 constructor's contents. Assume language specific tree nodes can
7728 be expanded in some interesting way. */
7729 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7730 if (DECL_P (exp)
7731 || TREE_CODE (exp) == CONSTRUCTOR
7732 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7734 result = expand_expr (exp, target, tmode,
7735 modifier == EXPAND_INITIALIZER
7736 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7738 /* If the DECL isn't in memory, then the DECL wasn't properly
7739 marked TREE_ADDRESSABLE, which will be either a front-end
7740 or a tree optimizer bug. */
7742 if (TREE_ADDRESSABLE (exp)
7743 && ! MEM_P (result)
7744 && ! targetm.calls.allocate_stack_slots_for_args ())
7746 error ("local frame unavailable (naked function?)");
7747 return result;
7749 else
7750 gcc_assert (MEM_P (result));
7751 result = XEXP (result, 0);
7753 /* ??? Is this needed anymore? */
7754 if (DECL_P (exp))
7755 TREE_USED (exp) = 1;
7757 if (modifier != EXPAND_INITIALIZER
7758 && modifier != EXPAND_CONST_ADDRESS
7759 && modifier != EXPAND_SUM)
7760 result = force_operand (result, target);
7761 return result;
7764 /* Pass FALSE as the last argument to get_inner_reference although
7765 we are expanding to RTL. The rationale is that we know how to
7766 handle "aligning nodes" here: we can just bypass them because
7767 they won't change the final object whose address will be returned
7768 (they actually exist only for that purpose). */
7769 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7770 &mode1, &unsignedp, &volatilep, false);
7771 break;
7774 /* We must have made progress. */
7775 gcc_assert (inner != exp);
7777 subtarget = offset || bitpos ? NULL_RTX : target;
7778 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7779 inner alignment, force the inner to be sufficiently aligned. */
7780 if (CONSTANT_CLASS_P (inner)
7781 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7783 inner = copy_node (inner);
7784 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7785 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7786 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7788 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7790 if (offset)
7792 rtx tmp;
7794 if (modifier != EXPAND_NORMAL)
7795 result = force_operand (result, NULL);
7796 tmp = expand_expr (offset, NULL_RTX, tmode,
7797 modifier == EXPAND_INITIALIZER
7798 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7800 /* expand_expr is allowed to return an object in a mode other
7801 than TMODE. If it did, we need to convert. */
7802 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7803 tmp = convert_modes (tmode, GET_MODE (tmp),
7804 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7805 result = convert_memory_address_addr_space (tmode, result, as);
7806 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7808 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7809 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7810 else
7812 subtarget = bitpos ? NULL_RTX : target;
7813 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7814 1, OPTAB_LIB_WIDEN);
7818 if (bitpos)
7820 /* Someone beforehand should have rejected taking the address
7821 of such an object. */
7822 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7824 result = convert_memory_address_addr_space (tmode, result, as);
7825 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7826 if (modifier < EXPAND_SUM)
7827 result = force_operand (result, target);
7830 return result;
7833 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7834 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7836 static rtx
7837 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7838 enum expand_modifier modifier)
7840 addr_space_t as = ADDR_SPACE_GENERIC;
7841 machine_mode address_mode = Pmode;
7842 machine_mode pointer_mode = ptr_mode;
7843 machine_mode rmode;
7844 rtx result;
7846 /* Target mode of VOIDmode says "whatever's natural". */
7847 if (tmode == VOIDmode)
7848 tmode = TYPE_MODE (TREE_TYPE (exp));
7850 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7852 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7853 address_mode = targetm.addr_space.address_mode (as);
7854 pointer_mode = targetm.addr_space.pointer_mode (as);
7857 /* We can get called with some Weird Things if the user does silliness
7858 like "(short) &a". In that case, convert_memory_address won't do
7859 the right thing, so ignore the given target mode. */
7860 if (tmode != address_mode && tmode != pointer_mode)
7861 tmode = address_mode;
7863 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7864 tmode, modifier, as);
7866 /* Despite expand_expr claims concerning ignoring TMODE when not
7867 strictly convenient, stuff breaks if we don't honor it. Note
7868 that combined with the above, we only do this for pointer modes. */
7869 rmode = GET_MODE (result);
7870 if (rmode == VOIDmode)
7871 rmode = tmode;
7872 if (rmode != tmode)
7873 result = convert_memory_address_addr_space (tmode, result, as);
7875 return result;
7878 /* Generate code for computing CONSTRUCTOR EXP.
7879 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7880 is TRUE, instead of creating a temporary variable in memory
7881 NULL is returned and the caller needs to handle it differently. */
7883 static rtx
7884 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7885 bool avoid_temp_mem)
7887 tree type = TREE_TYPE (exp);
7888 machine_mode mode = TYPE_MODE (type);
7890 /* Try to avoid creating a temporary at all. This is possible
7891 if all of the initializer is zero.
7892 FIXME: try to handle all [0..255] initializers we can handle
7893 with memset. */
7894 if (TREE_STATIC (exp)
7895 && !TREE_ADDRESSABLE (exp)
7896 && target != 0 && mode == BLKmode
7897 && all_zeros_p (exp))
7899 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7900 return target;
7903 /* All elts simple constants => refer to a constant in memory. But
7904 if this is a non-BLKmode mode, let it store a field at a time
7905 since that should make a CONST_INT, CONST_WIDE_INT or
7906 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7907 use, it is best to store directly into the target unless the type
7908 is large enough that memcpy will be used. If we are making an
7909 initializer and all operands are constant, put it in memory as
7910 well.
7912 FIXME: Avoid trying to fill vector constructors piece-meal.
7913 Output them with output_constant_def below unless we're sure
7914 they're zeros. This should go away when vector initializers
7915 are treated like VECTOR_CST instead of arrays. */
7916 if ((TREE_STATIC (exp)
7917 && ((mode == BLKmode
7918 && ! (target != 0 && safe_from_p (target, exp, 1)))
7919 || TREE_ADDRESSABLE (exp)
7920 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7921 && (! can_move_by_pieces
7922 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7923 TYPE_ALIGN (type)))
7924 && ! mostly_zeros_p (exp))))
7925 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7926 && TREE_CONSTANT (exp)))
7928 rtx constructor;
7930 if (avoid_temp_mem)
7931 return NULL_RTX;
7933 constructor = expand_expr_constant (exp, 1, modifier);
7935 if (modifier != EXPAND_CONST_ADDRESS
7936 && modifier != EXPAND_INITIALIZER
7937 && modifier != EXPAND_SUM)
7938 constructor = validize_mem (constructor);
7940 return constructor;
7943 /* Handle calls that pass values in multiple non-contiguous
7944 locations. The Irix 6 ABI has examples of this. */
7945 if (target == 0 || ! safe_from_p (target, exp, 1)
7946 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7948 if (avoid_temp_mem)
7949 return NULL_RTX;
7951 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7954 store_constructor (exp, target, 0, int_expr_size (exp));
7955 return target;
7959 /* expand_expr: generate code for computing expression EXP.
7960 An rtx for the computed value is returned. The value is never null.
7961 In the case of a void EXP, const0_rtx is returned.
7963 The value may be stored in TARGET if TARGET is nonzero.
7964 TARGET is just a suggestion; callers must assume that
7965 the rtx returned may not be the same as TARGET.
7967 If TARGET is CONST0_RTX, it means that the value will be ignored.
7969 If TMODE is not VOIDmode, it suggests generating the
7970 result in mode TMODE. But this is done only when convenient.
7971 Otherwise, TMODE is ignored and the value generated in its natural mode.
7972 TMODE is just a suggestion; callers must assume that
7973 the rtx returned may not have mode TMODE.
7975 Note that TARGET may have neither TMODE nor MODE. In that case, it
7976 probably will not be used.
7978 If MODIFIER is EXPAND_SUM then when EXP is an addition
7979 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7980 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7981 products as above, or REG or MEM, or constant.
7982 Ordinarily in such cases we would output mul or add instructions
7983 and then return a pseudo reg containing the sum.
7985 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7986 it also marks a label as absolutely required (it can't be dead).
7987 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7988 This is used for outputting expressions used in initializers.
7990 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7991 with a constant address even if that address is not normally legitimate.
7992 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7994 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7995 a call parameter. Such targets require special care as we haven't yet
7996 marked TARGET so that it's safe from being trashed by libcalls. We
7997 don't want to use TARGET for anything but the final result;
7998 Intermediate values must go elsewhere. Additionally, calls to
7999 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8001 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8002 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8003 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8004 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8005 recursively.
8007 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8008 In this case, we don't adjust a returned MEM rtx that wouldn't be
8009 sufficiently aligned for its mode; instead, it's up to the caller
8010 to deal with it afterwards. This is used to make sure that unaligned
8011 base objects for which out-of-bounds accesses are supported, for
8012 example record types with trailing arrays, aren't realigned behind
8013 the back of the caller.
8014 The normal operating mode is to pass FALSE for this parameter. */
8017 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8018 enum expand_modifier modifier, rtx *alt_rtl,
8019 bool inner_reference_p)
8021 rtx ret;
8023 /* Handle ERROR_MARK before anybody tries to access its type. */
8024 if (TREE_CODE (exp) == ERROR_MARK
8025 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8027 ret = CONST0_RTX (tmode);
8028 return ret ? ret : const0_rtx;
8031 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8032 inner_reference_p);
8033 return ret;
8036 /* Try to expand the conditional expression which is represented by
8037 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
8038 return the rtl reg which repsents the result. Otherwise return
8039 NULL_RTL. */
8041 static rtx
8042 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8043 tree treeop1 ATTRIBUTE_UNUSED,
8044 tree treeop2 ATTRIBUTE_UNUSED)
8046 #ifdef HAVE_conditional_move
8047 rtx insn;
8048 rtx op00, op01, op1, op2;
8049 enum rtx_code comparison_code;
8050 machine_mode comparison_mode;
8051 gimple srcstmt;
8052 rtx temp;
8053 tree type = TREE_TYPE (treeop1);
8054 int unsignedp = TYPE_UNSIGNED (type);
8055 machine_mode mode = TYPE_MODE (type);
8056 machine_mode orig_mode = mode;
8058 /* If we cannot do a conditional move on the mode, try doing it
8059 with the promoted mode. */
8060 if (!can_conditionally_move_p (mode))
8062 mode = promote_mode (type, mode, &unsignedp);
8063 if (!can_conditionally_move_p (mode))
8064 return NULL_RTX;
8065 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8067 else
8068 temp = assign_temp (type, 0, 1);
8070 start_sequence ();
8071 expand_operands (treeop1, treeop2,
8072 temp, &op1, &op2, EXPAND_NORMAL);
8074 if (TREE_CODE (treeop0) == SSA_NAME
8075 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8077 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8078 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8079 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8080 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8081 comparison_mode = TYPE_MODE (type);
8082 unsignedp = TYPE_UNSIGNED (type);
8083 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8085 else if (COMPARISON_CLASS_P (treeop0))
8087 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8088 enum tree_code cmpcode = TREE_CODE (treeop0);
8089 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8090 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8091 unsignedp = TYPE_UNSIGNED (type);
8092 comparison_mode = TYPE_MODE (type);
8093 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8095 else
8097 op00 = expand_normal (treeop0);
8098 op01 = const0_rtx;
8099 comparison_code = NE;
8100 comparison_mode = GET_MODE (op00);
8101 if (comparison_mode == VOIDmode)
8102 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8105 if (GET_MODE (op1) != mode)
8106 op1 = gen_lowpart (mode, op1);
8108 if (GET_MODE (op2) != mode)
8109 op2 = gen_lowpart (mode, op2);
8111 /* Try to emit the conditional move. */
8112 insn = emit_conditional_move (temp, comparison_code,
8113 op00, op01, comparison_mode,
8114 op1, op2, mode,
8115 unsignedp);
8117 /* If we could do the conditional move, emit the sequence,
8118 and return. */
8119 if (insn)
8121 rtx_insn *seq = get_insns ();
8122 end_sequence ();
8123 emit_insn (seq);
8124 return convert_modes (orig_mode, mode, temp, 0);
8127 /* Otherwise discard the sequence and fall back to code with
8128 branches. */
8129 end_sequence ();
8130 #endif
8131 return NULL_RTX;
8135 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8136 enum expand_modifier modifier)
8138 rtx op0, op1, op2, temp;
8139 rtx_code_label *lab;
8140 tree type;
8141 int unsignedp;
8142 machine_mode mode;
8143 enum tree_code code = ops->code;
8144 optab this_optab;
8145 rtx subtarget, original_target;
8146 int ignore;
8147 bool reduce_bit_field;
8148 location_t loc = ops->location;
8149 tree treeop0, treeop1, treeop2;
8150 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8151 ? reduce_to_bit_field_precision ((expr), \
8152 target, \
8153 type) \
8154 : (expr))
8156 type = ops->type;
8157 mode = TYPE_MODE (type);
8158 unsignedp = TYPE_UNSIGNED (type);
8160 treeop0 = ops->op0;
8161 treeop1 = ops->op1;
8162 treeop2 = ops->op2;
8164 /* We should be called only on simple (binary or unary) expressions,
8165 exactly those that are valid in gimple expressions that aren't
8166 GIMPLE_SINGLE_RHS (or invalid). */
8167 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8168 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8169 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8171 ignore = (target == const0_rtx
8172 || ((CONVERT_EXPR_CODE_P (code)
8173 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8174 && TREE_CODE (type) == VOID_TYPE));
8176 /* We should be called only if we need the result. */
8177 gcc_assert (!ignore);
8179 /* An operation in what may be a bit-field type needs the
8180 result to be reduced to the precision of the bit-field type,
8181 which is narrower than that of the type's mode. */
8182 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8183 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8185 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8186 target = 0;
8188 /* Use subtarget as the target for operand 0 of a binary operation. */
8189 subtarget = get_subtarget (target);
8190 original_target = target;
8192 switch (code)
8194 case NON_LVALUE_EXPR:
8195 case PAREN_EXPR:
8196 CASE_CONVERT:
8197 if (treeop0 == error_mark_node)
8198 return const0_rtx;
8200 if (TREE_CODE (type) == UNION_TYPE)
8202 tree valtype = TREE_TYPE (treeop0);
8204 /* If both input and output are BLKmode, this conversion isn't doing
8205 anything except possibly changing memory attribute. */
8206 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8208 rtx result = expand_expr (treeop0, target, tmode,
8209 modifier);
8211 result = copy_rtx (result);
8212 set_mem_attributes (result, type, 0);
8213 return result;
8216 if (target == 0)
8218 if (TYPE_MODE (type) != BLKmode)
8219 target = gen_reg_rtx (TYPE_MODE (type));
8220 else
8221 target = assign_temp (type, 1, 1);
8224 if (MEM_P (target))
8225 /* Store data into beginning of memory target. */
8226 store_expr (treeop0,
8227 adjust_address (target, TYPE_MODE (valtype), 0),
8228 modifier == EXPAND_STACK_PARM,
8229 false);
8231 else
8233 gcc_assert (REG_P (target));
8235 /* Store this field into a union of the proper type. */
8236 store_field (target,
8237 MIN ((int_size_in_bytes (TREE_TYPE
8238 (treeop0))
8239 * BITS_PER_UNIT),
8240 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8241 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8244 /* Return the entire union. */
8245 return target;
8248 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8250 op0 = expand_expr (treeop0, target, VOIDmode,
8251 modifier);
8253 /* If the signedness of the conversion differs and OP0 is
8254 a promoted SUBREG, clear that indication since we now
8255 have to do the proper extension. */
8256 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8257 && GET_CODE (op0) == SUBREG)
8258 SUBREG_PROMOTED_VAR_P (op0) = 0;
8260 return REDUCE_BIT_FIELD (op0);
8263 op0 = expand_expr (treeop0, NULL_RTX, mode,
8264 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8265 if (GET_MODE (op0) == mode)
8268 /* If OP0 is a constant, just convert it into the proper mode. */
8269 else if (CONSTANT_P (op0))
8271 tree inner_type = TREE_TYPE (treeop0);
8272 machine_mode inner_mode = GET_MODE (op0);
8274 if (inner_mode == VOIDmode)
8275 inner_mode = TYPE_MODE (inner_type);
8277 if (modifier == EXPAND_INITIALIZER)
8278 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8279 subreg_lowpart_offset (mode,
8280 inner_mode));
8281 else
8282 op0= convert_modes (mode, inner_mode, op0,
8283 TYPE_UNSIGNED (inner_type));
8286 else if (modifier == EXPAND_INITIALIZER)
8287 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8289 else if (target == 0)
8290 op0 = convert_to_mode (mode, op0,
8291 TYPE_UNSIGNED (TREE_TYPE
8292 (treeop0)));
8293 else
8295 convert_move (target, op0,
8296 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8297 op0 = target;
8300 return REDUCE_BIT_FIELD (op0);
8302 case ADDR_SPACE_CONVERT_EXPR:
8304 tree treeop0_type = TREE_TYPE (treeop0);
8305 addr_space_t as_to;
8306 addr_space_t as_from;
8308 gcc_assert (POINTER_TYPE_P (type));
8309 gcc_assert (POINTER_TYPE_P (treeop0_type));
8311 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8312 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8314 /* Conversions between pointers to the same address space should
8315 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8316 gcc_assert (as_to != as_from);
8318 /* Ask target code to handle conversion between pointers
8319 to overlapping address spaces. */
8320 if (targetm.addr_space.subset_p (as_to, as_from)
8321 || targetm.addr_space.subset_p (as_from, as_to))
8323 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8324 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8325 gcc_assert (op0);
8326 return op0;
8329 /* For disjoint address spaces, converting anything but
8330 a null pointer invokes undefined behaviour. We simply
8331 always return a null pointer here. */
8332 return CONST0_RTX (mode);
8335 case POINTER_PLUS_EXPR:
8336 /* Even though the sizetype mode and the pointer's mode can be different
8337 expand is able to handle this correctly and get the correct result out
8338 of the PLUS_EXPR code. */
8339 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8340 if sizetype precision is smaller than pointer precision. */
8341 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8342 treeop1 = fold_convert_loc (loc, type,
8343 fold_convert_loc (loc, ssizetype,
8344 treeop1));
8345 /* If sizetype precision is larger than pointer precision, truncate the
8346 offset to have matching modes. */
8347 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8348 treeop1 = fold_convert_loc (loc, type, treeop1);
8350 case PLUS_EXPR:
8351 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8352 something else, make sure we add the register to the constant and
8353 then to the other thing. This case can occur during strength
8354 reduction and doing it this way will produce better code if the
8355 frame pointer or argument pointer is eliminated.
8357 fold-const.c will ensure that the constant is always in the inner
8358 PLUS_EXPR, so the only case we need to do anything about is if
8359 sp, ap, or fp is our second argument, in which case we must swap
8360 the innermost first argument and our second argument. */
8362 if (TREE_CODE (treeop0) == PLUS_EXPR
8363 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8364 && TREE_CODE (treeop1) == VAR_DECL
8365 && (DECL_RTL (treeop1) == frame_pointer_rtx
8366 || DECL_RTL (treeop1) == stack_pointer_rtx
8367 || DECL_RTL (treeop1) == arg_pointer_rtx))
8369 gcc_unreachable ();
8372 /* If the result is to be ptr_mode and we are adding an integer to
8373 something, we might be forming a constant. So try to use
8374 plus_constant. If it produces a sum and we can't accept it,
8375 use force_operand. This allows P = &ARR[const] to generate
8376 efficient code on machines where a SYMBOL_REF is not a valid
8377 address.
8379 If this is an EXPAND_SUM call, always return the sum. */
8380 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8381 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8383 if (modifier == EXPAND_STACK_PARM)
8384 target = 0;
8385 if (TREE_CODE (treeop0) == INTEGER_CST
8386 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8387 && TREE_CONSTANT (treeop1))
8389 rtx constant_part;
8390 HOST_WIDE_INT wc;
8391 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8393 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8394 EXPAND_SUM);
8395 /* Use wi::shwi to ensure that the constant is
8396 truncated according to the mode of OP1, then sign extended
8397 to a HOST_WIDE_INT. Using the constant directly can result
8398 in non-canonical RTL in a 64x32 cross compile. */
8399 wc = TREE_INT_CST_LOW (treeop0);
8400 constant_part =
8401 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8402 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8403 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8404 op1 = force_operand (op1, target);
8405 return REDUCE_BIT_FIELD (op1);
8408 else if (TREE_CODE (treeop1) == INTEGER_CST
8409 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8410 && TREE_CONSTANT (treeop0))
8412 rtx constant_part;
8413 HOST_WIDE_INT wc;
8414 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8416 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8417 (modifier == EXPAND_INITIALIZER
8418 ? EXPAND_INITIALIZER : EXPAND_SUM));
8419 if (! CONSTANT_P (op0))
8421 op1 = expand_expr (treeop1, NULL_RTX,
8422 VOIDmode, modifier);
8423 /* Return a PLUS if modifier says it's OK. */
8424 if (modifier == EXPAND_SUM
8425 || modifier == EXPAND_INITIALIZER)
8426 return simplify_gen_binary (PLUS, mode, op0, op1);
8427 goto binop2;
8429 /* Use wi::shwi to ensure that the constant is
8430 truncated according to the mode of OP1, then sign extended
8431 to a HOST_WIDE_INT. Using the constant directly can result
8432 in non-canonical RTL in a 64x32 cross compile. */
8433 wc = TREE_INT_CST_LOW (treeop1);
8434 constant_part
8435 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8436 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8437 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8438 op0 = force_operand (op0, target);
8439 return REDUCE_BIT_FIELD (op0);
8443 /* Use TER to expand pointer addition of a negated value
8444 as pointer subtraction. */
8445 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8446 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8447 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8448 && TREE_CODE (treeop1) == SSA_NAME
8449 && TYPE_MODE (TREE_TYPE (treeop0))
8450 == TYPE_MODE (TREE_TYPE (treeop1)))
8452 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8453 if (def)
8455 treeop1 = gimple_assign_rhs1 (def);
8456 code = MINUS_EXPR;
8457 goto do_minus;
8461 /* No sense saving up arithmetic to be done
8462 if it's all in the wrong mode to form part of an address.
8463 And force_operand won't know whether to sign-extend or
8464 zero-extend. */
8465 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8466 || mode != ptr_mode)
8468 expand_operands (treeop0, treeop1,
8469 subtarget, &op0, &op1, EXPAND_NORMAL);
8470 if (op0 == const0_rtx)
8471 return op1;
8472 if (op1 == const0_rtx)
8473 return op0;
8474 goto binop2;
8477 expand_operands (treeop0, treeop1,
8478 subtarget, &op0, &op1, modifier);
8479 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8481 case MINUS_EXPR:
8482 do_minus:
8483 /* For initializers, we are allowed to return a MINUS of two
8484 symbolic constants. Here we handle all cases when both operands
8485 are constant. */
8486 /* Handle difference of two symbolic constants,
8487 for the sake of an initializer. */
8488 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8489 && really_constant_p (treeop0)
8490 && really_constant_p (treeop1))
8492 expand_operands (treeop0, treeop1,
8493 NULL_RTX, &op0, &op1, modifier);
8495 /* If the last operand is a CONST_INT, use plus_constant of
8496 the negated constant. Else make the MINUS. */
8497 if (CONST_INT_P (op1))
8498 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8499 -INTVAL (op1)));
8500 else
8501 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8504 /* No sense saving up arithmetic to be done
8505 if it's all in the wrong mode to form part of an address.
8506 And force_operand won't know whether to sign-extend or
8507 zero-extend. */
8508 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8509 || mode != ptr_mode)
8510 goto binop;
8512 expand_operands (treeop0, treeop1,
8513 subtarget, &op0, &op1, modifier);
8515 /* Convert A - const to A + (-const). */
8516 if (CONST_INT_P (op1))
8518 op1 = negate_rtx (mode, op1);
8519 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8522 goto binop2;
8524 case WIDEN_MULT_PLUS_EXPR:
8525 case WIDEN_MULT_MINUS_EXPR:
8526 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8527 op2 = expand_normal (treeop2);
8528 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8529 target, unsignedp);
8530 return target;
8532 case WIDEN_MULT_EXPR:
8533 /* If first operand is constant, swap them.
8534 Thus the following special case checks need only
8535 check the second operand. */
8536 if (TREE_CODE (treeop0) == INTEGER_CST)
8538 tree t1 = treeop0;
8539 treeop0 = treeop1;
8540 treeop1 = t1;
8543 /* First, check if we have a multiplication of one signed and one
8544 unsigned operand. */
8545 if (TREE_CODE (treeop1) != INTEGER_CST
8546 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8547 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8549 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8550 this_optab = usmul_widen_optab;
8551 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8552 != CODE_FOR_nothing)
8554 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8555 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8556 EXPAND_NORMAL);
8557 else
8558 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8559 EXPAND_NORMAL);
8560 /* op0 and op1 might still be constant, despite the above
8561 != INTEGER_CST check. Handle it. */
8562 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8564 op0 = convert_modes (innermode, mode, op0, true);
8565 op1 = convert_modes (innermode, mode, op1, false);
8566 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8567 target, unsignedp));
8569 goto binop3;
8572 /* Check for a multiplication with matching signedness. */
8573 else if ((TREE_CODE (treeop1) == INTEGER_CST
8574 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8575 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8576 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8578 tree op0type = TREE_TYPE (treeop0);
8579 machine_mode innermode = TYPE_MODE (op0type);
8580 bool zextend_p = TYPE_UNSIGNED (op0type);
8581 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8582 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8584 if (TREE_CODE (treeop0) != INTEGER_CST)
8586 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8587 != CODE_FOR_nothing)
8589 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8590 EXPAND_NORMAL);
8591 /* op0 and op1 might still be constant, despite the above
8592 != INTEGER_CST check. Handle it. */
8593 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8595 widen_mult_const:
8596 op0 = convert_modes (innermode, mode, op0, zextend_p);
8598 = convert_modes (innermode, mode, op1,
8599 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8600 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8601 target,
8602 unsignedp));
8604 temp = expand_widening_mult (mode, op0, op1, target,
8605 unsignedp, this_optab);
8606 return REDUCE_BIT_FIELD (temp);
8608 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8609 != CODE_FOR_nothing
8610 && innermode == word_mode)
8612 rtx htem, hipart;
8613 op0 = expand_normal (treeop0);
8614 if (TREE_CODE (treeop1) == INTEGER_CST)
8615 op1 = convert_modes (innermode, mode,
8616 expand_normal (treeop1),
8617 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8618 else
8619 op1 = expand_normal (treeop1);
8620 /* op0 and op1 might still be constant, despite the above
8621 != INTEGER_CST check. Handle it. */
8622 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8623 goto widen_mult_const;
8624 temp = expand_binop (mode, other_optab, op0, op1, target,
8625 unsignedp, OPTAB_LIB_WIDEN);
8626 hipart = gen_highpart (innermode, temp);
8627 htem = expand_mult_highpart_adjust (innermode, hipart,
8628 op0, op1, hipart,
8629 zextend_p);
8630 if (htem != hipart)
8631 emit_move_insn (hipart, htem);
8632 return REDUCE_BIT_FIELD (temp);
8636 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8637 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8638 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8639 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8641 case FMA_EXPR:
8643 optab opt = fma_optab;
8644 gimple def0, def2;
8646 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8647 call. */
8648 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8650 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8651 tree call_expr;
8653 gcc_assert (fn != NULL_TREE);
8654 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8655 return expand_builtin (call_expr, target, subtarget, mode, false);
8658 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8659 /* The multiplication is commutative - look at its 2nd operand
8660 if the first isn't fed by a negate. */
8661 if (!def0)
8663 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8664 /* Swap operands if the 2nd operand is fed by a negate. */
8665 if (def0)
8667 tree tem = treeop0;
8668 treeop0 = treeop1;
8669 treeop1 = tem;
8672 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8674 op0 = op2 = NULL;
8676 if (def0 && def2
8677 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8679 opt = fnms_optab;
8680 op0 = expand_normal (gimple_assign_rhs1 (def0));
8681 op2 = expand_normal (gimple_assign_rhs1 (def2));
8683 else if (def0
8684 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8686 opt = fnma_optab;
8687 op0 = expand_normal (gimple_assign_rhs1 (def0));
8689 else if (def2
8690 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8692 opt = fms_optab;
8693 op2 = expand_normal (gimple_assign_rhs1 (def2));
8696 if (op0 == NULL)
8697 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8698 if (op2 == NULL)
8699 op2 = expand_normal (treeop2);
8700 op1 = expand_normal (treeop1);
8702 return expand_ternary_op (TYPE_MODE (type), opt,
8703 op0, op1, op2, target, 0);
8706 case MULT_EXPR:
8707 /* If this is a fixed-point operation, then we cannot use the code
8708 below because "expand_mult" doesn't support sat/no-sat fixed-point
8709 multiplications. */
8710 if (ALL_FIXED_POINT_MODE_P (mode))
8711 goto binop;
8713 /* If first operand is constant, swap them.
8714 Thus the following special case checks need only
8715 check the second operand. */
8716 if (TREE_CODE (treeop0) == INTEGER_CST)
8718 tree t1 = treeop0;
8719 treeop0 = treeop1;
8720 treeop1 = t1;
8723 /* Attempt to return something suitable for generating an
8724 indexed address, for machines that support that. */
8726 if (modifier == EXPAND_SUM && mode == ptr_mode
8727 && tree_fits_shwi_p (treeop1))
8729 tree exp1 = treeop1;
8731 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8732 EXPAND_SUM);
8734 if (!REG_P (op0))
8735 op0 = force_operand (op0, NULL_RTX);
8736 if (!REG_P (op0))
8737 op0 = copy_to_mode_reg (mode, op0);
8739 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8740 gen_int_mode (tree_to_shwi (exp1),
8741 TYPE_MODE (TREE_TYPE (exp1)))));
8744 if (modifier == EXPAND_STACK_PARM)
8745 target = 0;
8747 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8748 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8750 case TRUNC_DIV_EXPR:
8751 case FLOOR_DIV_EXPR:
8752 case CEIL_DIV_EXPR:
8753 case ROUND_DIV_EXPR:
8754 case EXACT_DIV_EXPR:
8755 /* If this is a fixed-point operation, then we cannot use the code
8756 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8757 divisions. */
8758 if (ALL_FIXED_POINT_MODE_P (mode))
8759 goto binop;
8761 if (modifier == EXPAND_STACK_PARM)
8762 target = 0;
8763 /* Possible optimization: compute the dividend with EXPAND_SUM
8764 then if the divisor is constant can optimize the case
8765 where some terms of the dividend have coeffs divisible by it. */
8766 expand_operands (treeop0, treeop1,
8767 subtarget, &op0, &op1, EXPAND_NORMAL);
8768 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8770 case RDIV_EXPR:
8771 goto binop;
8773 case MULT_HIGHPART_EXPR:
8774 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8775 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8776 gcc_assert (temp);
8777 return temp;
8779 case TRUNC_MOD_EXPR:
8780 case FLOOR_MOD_EXPR:
8781 case CEIL_MOD_EXPR:
8782 case ROUND_MOD_EXPR:
8783 if (modifier == EXPAND_STACK_PARM)
8784 target = 0;
8785 expand_operands (treeop0, treeop1,
8786 subtarget, &op0, &op1, EXPAND_NORMAL);
8787 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8789 case FIXED_CONVERT_EXPR:
8790 op0 = expand_normal (treeop0);
8791 if (target == 0 || modifier == EXPAND_STACK_PARM)
8792 target = gen_reg_rtx (mode);
8794 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8795 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8796 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8797 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8798 else
8799 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8800 return target;
8802 case FIX_TRUNC_EXPR:
8803 op0 = expand_normal (treeop0);
8804 if (target == 0 || modifier == EXPAND_STACK_PARM)
8805 target = gen_reg_rtx (mode);
8806 expand_fix (target, op0, unsignedp);
8807 return target;
8809 case FLOAT_EXPR:
8810 op0 = expand_normal (treeop0);
8811 if (target == 0 || modifier == EXPAND_STACK_PARM)
8812 target = gen_reg_rtx (mode);
8813 /* expand_float can't figure out what to do if FROM has VOIDmode.
8814 So give it the correct mode. With -O, cse will optimize this. */
8815 if (GET_MODE (op0) == VOIDmode)
8816 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8817 op0);
8818 expand_float (target, op0,
8819 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8820 return target;
8822 case NEGATE_EXPR:
8823 op0 = expand_expr (treeop0, subtarget,
8824 VOIDmode, EXPAND_NORMAL);
8825 if (modifier == EXPAND_STACK_PARM)
8826 target = 0;
8827 temp = expand_unop (mode,
8828 optab_for_tree_code (NEGATE_EXPR, type,
8829 optab_default),
8830 op0, target, 0);
8831 gcc_assert (temp);
8832 return REDUCE_BIT_FIELD (temp);
8834 case ABS_EXPR:
8835 op0 = expand_expr (treeop0, subtarget,
8836 VOIDmode, EXPAND_NORMAL);
8837 if (modifier == EXPAND_STACK_PARM)
8838 target = 0;
8840 /* ABS_EXPR is not valid for complex arguments. */
8841 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8842 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8844 /* Unsigned abs is simply the operand. Testing here means we don't
8845 risk generating incorrect code below. */
8846 if (TYPE_UNSIGNED (type))
8847 return op0;
8849 return expand_abs (mode, op0, target, unsignedp,
8850 safe_from_p (target, treeop0, 1));
8852 case MAX_EXPR:
8853 case MIN_EXPR:
8854 target = original_target;
8855 if (target == 0
8856 || modifier == EXPAND_STACK_PARM
8857 || (MEM_P (target) && MEM_VOLATILE_P (target))
8858 || GET_MODE (target) != mode
8859 || (REG_P (target)
8860 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8861 target = gen_reg_rtx (mode);
8862 expand_operands (treeop0, treeop1,
8863 target, &op0, &op1, EXPAND_NORMAL);
8865 /* First try to do it with a special MIN or MAX instruction.
8866 If that does not win, use a conditional jump to select the proper
8867 value. */
8868 this_optab = optab_for_tree_code (code, type, optab_default);
8869 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8870 OPTAB_WIDEN);
8871 if (temp != 0)
8872 return temp;
8874 /* At this point, a MEM target is no longer useful; we will get better
8875 code without it. */
8877 if (! REG_P (target))
8878 target = gen_reg_rtx (mode);
8880 /* If op1 was placed in target, swap op0 and op1. */
8881 if (target != op0 && target == op1)
8882 std::swap (op0, op1);
8884 /* We generate better code and avoid problems with op1 mentioning
8885 target by forcing op1 into a pseudo if it isn't a constant. */
8886 if (! CONSTANT_P (op1))
8887 op1 = force_reg (mode, op1);
8890 enum rtx_code comparison_code;
8891 rtx cmpop1 = op1;
8893 if (code == MAX_EXPR)
8894 comparison_code = unsignedp ? GEU : GE;
8895 else
8896 comparison_code = unsignedp ? LEU : LE;
8898 /* Canonicalize to comparisons against 0. */
8899 if (op1 == const1_rtx)
8901 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8902 or (a != 0 ? a : 1) for unsigned.
8903 For MIN we are safe converting (a <= 1 ? a : 1)
8904 into (a <= 0 ? a : 1) */
8905 cmpop1 = const0_rtx;
8906 if (code == MAX_EXPR)
8907 comparison_code = unsignedp ? NE : GT;
8909 if (op1 == constm1_rtx && !unsignedp)
8911 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8912 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8913 cmpop1 = const0_rtx;
8914 if (code == MIN_EXPR)
8915 comparison_code = LT;
8917 #ifdef HAVE_conditional_move
8918 /* Use a conditional move if possible. */
8919 if (can_conditionally_move_p (mode))
8921 rtx insn;
8923 start_sequence ();
8925 /* Try to emit the conditional move. */
8926 insn = emit_conditional_move (target, comparison_code,
8927 op0, cmpop1, mode,
8928 op0, op1, mode,
8929 unsignedp);
8931 /* If we could do the conditional move, emit the sequence,
8932 and return. */
8933 if (insn)
8935 rtx_insn *seq = get_insns ();
8936 end_sequence ();
8937 emit_insn (seq);
8938 return target;
8941 /* Otherwise discard the sequence and fall back to code with
8942 branches. */
8943 end_sequence ();
8945 #endif
8946 if (target != op0)
8947 emit_move_insn (target, op0);
8949 lab = gen_label_rtx ();
8950 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8951 unsignedp, mode, NULL_RTX, NULL, lab,
8952 -1);
8954 emit_move_insn (target, op1);
8955 emit_label (lab);
8956 return target;
8958 case BIT_NOT_EXPR:
8959 op0 = expand_expr (treeop0, subtarget,
8960 VOIDmode, EXPAND_NORMAL);
8961 if (modifier == EXPAND_STACK_PARM)
8962 target = 0;
8963 /* In case we have to reduce the result to bitfield precision
8964 for unsigned bitfield expand this as XOR with a proper constant
8965 instead. */
8966 if (reduce_bit_field && TYPE_UNSIGNED (type))
8968 wide_int mask = wi::mask (TYPE_PRECISION (type),
8969 false, GET_MODE_PRECISION (mode));
8971 temp = expand_binop (mode, xor_optab, op0,
8972 immed_wide_int_const (mask, mode),
8973 target, 1, OPTAB_LIB_WIDEN);
8975 else
8976 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8977 gcc_assert (temp);
8978 return temp;
8980 /* ??? Can optimize bitwise operations with one arg constant.
8981 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8982 and (a bitwise1 b) bitwise2 b (etc)
8983 but that is probably not worth while. */
8985 case BIT_AND_EXPR:
8986 case BIT_IOR_EXPR:
8987 case BIT_XOR_EXPR:
8988 goto binop;
8990 case LROTATE_EXPR:
8991 case RROTATE_EXPR:
8992 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8993 || (GET_MODE_PRECISION (TYPE_MODE (type))
8994 == TYPE_PRECISION (type)));
8995 /* fall through */
8997 case LSHIFT_EXPR:
8998 case RSHIFT_EXPR:
8999 /* If this is a fixed-point operation, then we cannot use the code
9000 below because "expand_shift" doesn't support sat/no-sat fixed-point
9001 shifts. */
9002 if (ALL_FIXED_POINT_MODE_P (mode))
9003 goto binop;
9005 if (! safe_from_p (subtarget, treeop1, 1))
9006 subtarget = 0;
9007 if (modifier == EXPAND_STACK_PARM)
9008 target = 0;
9009 op0 = expand_expr (treeop0, subtarget,
9010 VOIDmode, EXPAND_NORMAL);
9011 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9012 unsignedp);
9013 if (code == LSHIFT_EXPR)
9014 temp = REDUCE_BIT_FIELD (temp);
9015 return temp;
9017 /* Could determine the answer when only additive constants differ. Also,
9018 the addition of one can be handled by changing the condition. */
9019 case LT_EXPR:
9020 case LE_EXPR:
9021 case GT_EXPR:
9022 case GE_EXPR:
9023 case EQ_EXPR:
9024 case NE_EXPR:
9025 case UNORDERED_EXPR:
9026 case ORDERED_EXPR:
9027 case UNLT_EXPR:
9028 case UNLE_EXPR:
9029 case UNGT_EXPR:
9030 case UNGE_EXPR:
9031 case UNEQ_EXPR:
9032 case LTGT_EXPR:
9034 temp = do_store_flag (ops,
9035 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9036 tmode != VOIDmode ? tmode : mode);
9037 if (temp)
9038 return temp;
9040 /* Use a compare and a jump for BLKmode comparisons, or for function
9041 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
9043 if ((target == 0
9044 || modifier == EXPAND_STACK_PARM
9045 || ! safe_from_p (target, treeop0, 1)
9046 || ! safe_from_p (target, treeop1, 1)
9047 /* Make sure we don't have a hard reg (such as function's return
9048 value) live across basic blocks, if not optimizing. */
9049 || (!optimize && REG_P (target)
9050 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9051 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9053 emit_move_insn (target, const0_rtx);
9055 rtx_code_label *lab1 = gen_label_rtx ();
9056 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9058 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9059 emit_move_insn (target, constm1_rtx);
9060 else
9061 emit_move_insn (target, const1_rtx);
9063 emit_label (lab1);
9064 return target;
9066 case COMPLEX_EXPR:
9067 /* Get the rtx code of the operands. */
9068 op0 = expand_normal (treeop0);
9069 op1 = expand_normal (treeop1);
9071 if (!target)
9072 target = gen_reg_rtx (TYPE_MODE (type));
9073 else
9074 /* If target overlaps with op1, then either we need to force
9075 op1 into a pseudo (if target also overlaps with op0),
9076 or write the complex parts in reverse order. */
9077 switch (GET_CODE (target))
9079 case CONCAT:
9080 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9082 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9084 complex_expr_force_op1:
9085 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9086 emit_move_insn (temp, op1);
9087 op1 = temp;
9088 break;
9090 complex_expr_swap_order:
9091 /* Move the imaginary (op1) and real (op0) parts to their
9092 location. */
9093 write_complex_part (target, op1, true);
9094 write_complex_part (target, op0, false);
9096 return target;
9098 break;
9099 case MEM:
9100 temp = adjust_address_nv (target,
9101 GET_MODE_INNER (GET_MODE (target)), 0);
9102 if (reg_overlap_mentioned_p (temp, op1))
9104 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9105 temp = adjust_address_nv (target, imode,
9106 GET_MODE_SIZE (imode));
9107 if (reg_overlap_mentioned_p (temp, op0))
9108 goto complex_expr_force_op1;
9109 goto complex_expr_swap_order;
9111 break;
9112 default:
9113 if (reg_overlap_mentioned_p (target, op1))
9115 if (reg_overlap_mentioned_p (target, op0))
9116 goto complex_expr_force_op1;
9117 goto complex_expr_swap_order;
9119 break;
9122 /* Move the real (op0) and imaginary (op1) parts to their location. */
9123 write_complex_part (target, op0, false);
9124 write_complex_part (target, op1, true);
9126 return target;
9128 case WIDEN_SUM_EXPR:
9130 tree oprnd0 = treeop0;
9131 tree oprnd1 = treeop1;
9133 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9134 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9135 target, unsignedp);
9136 return target;
9139 case REDUC_MAX_EXPR:
9140 case REDUC_MIN_EXPR:
9141 case REDUC_PLUS_EXPR:
9143 op0 = expand_normal (treeop0);
9144 this_optab = optab_for_tree_code (code, type, optab_default);
9145 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9147 if (optab_handler (this_optab, vec_mode) != CODE_FOR_nothing)
9149 struct expand_operand ops[2];
9150 enum insn_code icode = optab_handler (this_optab, vec_mode);
9152 create_output_operand (&ops[0], target, mode);
9153 create_input_operand (&ops[1], op0, vec_mode);
9154 if (maybe_expand_insn (icode, 2, ops))
9156 target = ops[0].value;
9157 if (GET_MODE (target) != mode)
9158 return gen_lowpart (tmode, target);
9159 return target;
9162 /* Fall back to optab with vector result, and then extract scalar. */
9163 this_optab = scalar_reduc_to_vector (this_optab, type);
9164 temp = expand_unop (vec_mode, this_optab, op0, NULL_RTX, unsignedp);
9165 gcc_assert (temp);
9166 /* The tree code produces a scalar result, but (somewhat by convention)
9167 the optab produces a vector with the result in element 0 if
9168 little-endian, or element N-1 if big-endian. So pull the scalar
9169 result out of that element. */
9170 int index = BYTES_BIG_ENDIAN ? GET_MODE_NUNITS (vec_mode) - 1 : 0;
9171 int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (vec_mode));
9172 temp = extract_bit_field (temp, bitsize, bitsize * index, unsignedp,
9173 target, mode, mode);
9174 gcc_assert (temp);
9175 return temp;
9178 case VEC_UNPACK_HI_EXPR:
9179 case VEC_UNPACK_LO_EXPR:
9181 op0 = expand_normal (treeop0);
9182 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9183 target, unsignedp);
9184 gcc_assert (temp);
9185 return temp;
9188 case VEC_UNPACK_FLOAT_HI_EXPR:
9189 case VEC_UNPACK_FLOAT_LO_EXPR:
9191 op0 = expand_normal (treeop0);
9192 /* The signedness is determined from input operand. */
9193 temp = expand_widen_pattern_expr
9194 (ops, op0, NULL_RTX, NULL_RTX,
9195 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9197 gcc_assert (temp);
9198 return temp;
9201 case VEC_WIDEN_MULT_HI_EXPR:
9202 case VEC_WIDEN_MULT_LO_EXPR:
9203 case VEC_WIDEN_MULT_EVEN_EXPR:
9204 case VEC_WIDEN_MULT_ODD_EXPR:
9205 case VEC_WIDEN_LSHIFT_HI_EXPR:
9206 case VEC_WIDEN_LSHIFT_LO_EXPR:
9207 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9208 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9209 target, unsignedp);
9210 gcc_assert (target);
9211 return target;
9213 case VEC_PACK_TRUNC_EXPR:
9214 case VEC_PACK_SAT_EXPR:
9215 case VEC_PACK_FIX_TRUNC_EXPR:
9216 mode = TYPE_MODE (TREE_TYPE (treeop0));
9217 goto binop;
9219 case VEC_PERM_EXPR:
9220 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9221 op2 = expand_normal (treeop2);
9223 /* Careful here: if the target doesn't support integral vector modes,
9224 a constant selection vector could wind up smooshed into a normal
9225 integral constant. */
9226 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9228 tree sel_type = TREE_TYPE (treeop2);
9229 machine_mode vmode
9230 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9231 TYPE_VECTOR_SUBPARTS (sel_type));
9232 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9233 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9234 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9236 else
9237 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9239 temp = expand_vec_perm (mode, op0, op1, op2, target);
9240 gcc_assert (temp);
9241 return temp;
9243 case DOT_PROD_EXPR:
9245 tree oprnd0 = treeop0;
9246 tree oprnd1 = treeop1;
9247 tree oprnd2 = treeop2;
9248 rtx op2;
9250 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9251 op2 = expand_normal (oprnd2);
9252 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9253 target, unsignedp);
9254 return target;
9257 case SAD_EXPR:
9259 tree oprnd0 = treeop0;
9260 tree oprnd1 = treeop1;
9261 tree oprnd2 = treeop2;
9262 rtx op2;
9264 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9265 op2 = expand_normal (oprnd2);
9266 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9267 target, unsignedp);
9268 return target;
9271 case REALIGN_LOAD_EXPR:
9273 tree oprnd0 = treeop0;
9274 tree oprnd1 = treeop1;
9275 tree oprnd2 = treeop2;
9276 rtx op2;
9278 this_optab = optab_for_tree_code (code, type, optab_default);
9279 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9280 op2 = expand_normal (oprnd2);
9281 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9282 target, unsignedp);
9283 gcc_assert (temp);
9284 return temp;
9287 case COND_EXPR:
9289 /* A COND_EXPR with its type being VOID_TYPE represents a
9290 conditional jump and is handled in
9291 expand_gimple_cond_expr. */
9292 gcc_assert (!VOID_TYPE_P (type));
9294 /* Note that COND_EXPRs whose type is a structure or union
9295 are required to be constructed to contain assignments of
9296 a temporary variable, so that we can evaluate them here
9297 for side effect only. If type is void, we must do likewise. */
9299 gcc_assert (!TREE_ADDRESSABLE (type)
9300 && !ignore
9301 && TREE_TYPE (treeop1) != void_type_node
9302 && TREE_TYPE (treeop2) != void_type_node);
9304 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9305 if (temp)
9306 return temp;
9308 /* If we are not to produce a result, we have no target. Otherwise,
9309 if a target was specified use it; it will not be used as an
9310 intermediate target unless it is safe. If no target, use a
9311 temporary. */
9313 if (modifier != EXPAND_STACK_PARM
9314 && original_target
9315 && safe_from_p (original_target, treeop0, 1)
9316 && GET_MODE (original_target) == mode
9317 && !MEM_P (original_target))
9318 temp = original_target;
9319 else
9320 temp = assign_temp (type, 0, 1);
9322 do_pending_stack_adjust ();
9323 NO_DEFER_POP;
9324 rtx_code_label *lab0 = gen_label_rtx ();
9325 rtx_code_label *lab1 = gen_label_rtx ();
9326 jumpifnot (treeop0, lab0, -1);
9327 store_expr (treeop1, temp,
9328 modifier == EXPAND_STACK_PARM,
9329 false);
9331 emit_jump_insn (gen_jump (lab1));
9332 emit_barrier ();
9333 emit_label (lab0);
9334 store_expr (treeop2, temp,
9335 modifier == EXPAND_STACK_PARM,
9336 false);
9338 emit_label (lab1);
9339 OK_DEFER_POP;
9340 return temp;
9343 case VEC_COND_EXPR:
9344 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9345 return target;
9347 default:
9348 gcc_unreachable ();
9351 /* Here to do an ordinary binary operator. */
9352 binop:
9353 expand_operands (treeop0, treeop1,
9354 subtarget, &op0, &op1, EXPAND_NORMAL);
9355 binop2:
9356 this_optab = optab_for_tree_code (code, type, optab_default);
9357 binop3:
9358 if (modifier == EXPAND_STACK_PARM)
9359 target = 0;
9360 temp = expand_binop (mode, this_optab, op0, op1, target,
9361 unsignedp, OPTAB_LIB_WIDEN);
9362 gcc_assert (temp);
9363 /* Bitwise operations do not need bitfield reduction as we expect their
9364 operands being properly truncated. */
9365 if (code == BIT_XOR_EXPR
9366 || code == BIT_AND_EXPR
9367 || code == BIT_IOR_EXPR)
9368 return temp;
9369 return REDUCE_BIT_FIELD (temp);
9371 #undef REDUCE_BIT_FIELD
9374 /* Return TRUE if expression STMT is suitable for replacement.
9375 Never consider memory loads as replaceable, because those don't ever lead
9376 into constant expressions. */
9378 static bool
9379 stmt_is_replaceable_p (gimple stmt)
9381 if (ssa_is_replaceable_p (stmt))
9383 /* Don't move around loads. */
9384 if (!gimple_assign_single_p (stmt)
9385 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9386 return true;
9388 return false;
9392 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9393 enum expand_modifier modifier, rtx *alt_rtl,
9394 bool inner_reference_p)
9396 rtx op0, op1, temp, decl_rtl;
9397 tree type;
9398 int unsignedp;
9399 machine_mode mode;
9400 enum tree_code code = TREE_CODE (exp);
9401 rtx subtarget, original_target;
9402 int ignore;
9403 tree context;
9404 bool reduce_bit_field;
9405 location_t loc = EXPR_LOCATION (exp);
9406 struct separate_ops ops;
9407 tree treeop0, treeop1, treeop2;
9408 tree ssa_name = NULL_TREE;
9409 gimple g;
9411 type = TREE_TYPE (exp);
9412 mode = TYPE_MODE (type);
9413 unsignedp = TYPE_UNSIGNED (type);
9415 treeop0 = treeop1 = treeop2 = NULL_TREE;
9416 if (!VL_EXP_CLASS_P (exp))
9417 switch (TREE_CODE_LENGTH (code))
9419 default:
9420 case 3: treeop2 = TREE_OPERAND (exp, 2);
9421 case 2: treeop1 = TREE_OPERAND (exp, 1);
9422 case 1: treeop0 = TREE_OPERAND (exp, 0);
9423 case 0: break;
9425 ops.code = code;
9426 ops.type = type;
9427 ops.op0 = treeop0;
9428 ops.op1 = treeop1;
9429 ops.op2 = treeop2;
9430 ops.location = loc;
9432 ignore = (target == const0_rtx
9433 || ((CONVERT_EXPR_CODE_P (code)
9434 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9435 && TREE_CODE (type) == VOID_TYPE));
9437 /* An operation in what may be a bit-field type needs the
9438 result to be reduced to the precision of the bit-field type,
9439 which is narrower than that of the type's mode. */
9440 reduce_bit_field = (!ignore
9441 && INTEGRAL_TYPE_P (type)
9442 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9444 /* If we are going to ignore this result, we need only do something
9445 if there is a side-effect somewhere in the expression. If there
9446 is, short-circuit the most common cases here. Note that we must
9447 not call expand_expr with anything but const0_rtx in case this
9448 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9450 if (ignore)
9452 if (! TREE_SIDE_EFFECTS (exp))
9453 return const0_rtx;
9455 /* Ensure we reference a volatile object even if value is ignored, but
9456 don't do this if all we are doing is taking its address. */
9457 if (TREE_THIS_VOLATILE (exp)
9458 && TREE_CODE (exp) != FUNCTION_DECL
9459 && mode != VOIDmode && mode != BLKmode
9460 && modifier != EXPAND_CONST_ADDRESS)
9462 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9463 if (MEM_P (temp))
9464 copy_to_reg (temp);
9465 return const0_rtx;
9468 if (TREE_CODE_CLASS (code) == tcc_unary
9469 || code == BIT_FIELD_REF
9470 || code == COMPONENT_REF
9471 || code == INDIRECT_REF)
9472 return expand_expr (treeop0, const0_rtx, VOIDmode,
9473 modifier);
9475 else if (TREE_CODE_CLASS (code) == tcc_binary
9476 || TREE_CODE_CLASS (code) == tcc_comparison
9477 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9479 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9480 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9481 return const0_rtx;
9484 target = 0;
9487 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9488 target = 0;
9490 /* Use subtarget as the target for operand 0 of a binary operation. */
9491 subtarget = get_subtarget (target);
9492 original_target = target;
9494 switch (code)
9496 case LABEL_DECL:
9498 tree function = decl_function_context (exp);
9500 temp = label_rtx (exp);
9501 temp = gen_rtx_LABEL_REF (Pmode, temp);
9503 if (function != current_function_decl
9504 && function != 0)
9505 LABEL_REF_NONLOCAL_P (temp) = 1;
9507 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9508 return temp;
9511 case SSA_NAME:
9512 /* ??? ivopts calls expander, without any preparation from
9513 out-of-ssa. So fake instructions as if this was an access to the
9514 base variable. This unnecessarily allocates a pseudo, see how we can
9515 reuse it, if partition base vars have it set already. */
9516 if (!currently_expanding_to_rtl)
9518 tree var = SSA_NAME_VAR (exp);
9519 if (var && DECL_RTL_SET_P (var))
9520 return DECL_RTL (var);
9521 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9522 LAST_VIRTUAL_REGISTER + 1);
9525 g = get_gimple_for_ssa_name (exp);
9526 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9527 if (g == NULL
9528 && modifier == EXPAND_INITIALIZER
9529 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9530 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9531 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9532 g = SSA_NAME_DEF_STMT (exp);
9533 if (g)
9535 rtx r;
9536 ops.code = gimple_assign_rhs_code (g);
9537 switch (get_gimple_rhs_class (ops.code))
9539 case GIMPLE_TERNARY_RHS:
9540 ops.op2 = gimple_assign_rhs3 (g);
9541 /* Fallthru */
9542 case GIMPLE_BINARY_RHS:
9543 ops.op1 = gimple_assign_rhs2 (g);
9545 /* Try to expand conditonal compare. */
9546 if (targetm.gen_ccmp_first)
9548 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9549 r = expand_ccmp_expr (g);
9550 if (r)
9551 break;
9553 /* Fallthru */
9554 case GIMPLE_UNARY_RHS:
9555 ops.op0 = gimple_assign_rhs1 (g);
9556 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9557 ops.location = gimple_location (g);
9558 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9559 break;
9560 case GIMPLE_SINGLE_RHS:
9562 location_t saved_loc = curr_insn_location ();
9563 set_curr_insn_location (gimple_location (g));
9564 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9565 tmode, modifier, NULL, inner_reference_p);
9566 set_curr_insn_location (saved_loc);
9567 break;
9569 default:
9570 gcc_unreachable ();
9572 if (REG_P (r) && !REG_EXPR (r))
9573 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9574 return r;
9577 ssa_name = exp;
9578 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9579 exp = SSA_NAME_VAR (ssa_name);
9580 goto expand_decl_rtl;
9582 case PARM_DECL:
9583 case VAR_DECL:
9584 /* If a static var's type was incomplete when the decl was written,
9585 but the type is complete now, lay out the decl now. */
9586 if (DECL_SIZE (exp) == 0
9587 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9588 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9589 layout_decl (exp, 0);
9591 /* ... fall through ... */
9593 case FUNCTION_DECL:
9594 case RESULT_DECL:
9595 decl_rtl = DECL_RTL (exp);
9596 expand_decl_rtl:
9597 gcc_assert (decl_rtl);
9598 decl_rtl = copy_rtx (decl_rtl);
9599 /* Record writes to register variables. */
9600 if (modifier == EXPAND_WRITE
9601 && REG_P (decl_rtl)
9602 && HARD_REGISTER_P (decl_rtl))
9603 add_to_hard_reg_set (&crtl->asm_clobbers,
9604 GET_MODE (decl_rtl), REGNO (decl_rtl));
9606 /* Ensure variable marked as used even if it doesn't go through
9607 a parser. If it hasn't be used yet, write out an external
9608 definition. */
9609 TREE_USED (exp) = 1;
9611 /* Show we haven't gotten RTL for this yet. */
9612 temp = 0;
9614 /* Variables inherited from containing functions should have
9615 been lowered by this point. */
9616 context = decl_function_context (exp);
9617 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9618 || context == current_function_decl
9619 || TREE_STATIC (exp)
9620 || DECL_EXTERNAL (exp)
9621 /* ??? C++ creates functions that are not TREE_STATIC. */
9622 || TREE_CODE (exp) == FUNCTION_DECL);
9624 /* This is the case of an array whose size is to be determined
9625 from its initializer, while the initializer is still being parsed.
9626 ??? We aren't parsing while expanding anymore. */
9628 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9629 temp = validize_mem (decl_rtl);
9631 /* If DECL_RTL is memory, we are in the normal case and the
9632 address is not valid, get the address into a register. */
9634 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9636 if (alt_rtl)
9637 *alt_rtl = decl_rtl;
9638 decl_rtl = use_anchored_address (decl_rtl);
9639 if (modifier != EXPAND_CONST_ADDRESS
9640 && modifier != EXPAND_SUM
9641 && !memory_address_addr_space_p (DECL_MODE (exp),
9642 XEXP (decl_rtl, 0),
9643 MEM_ADDR_SPACE (decl_rtl)))
9644 temp = replace_equiv_address (decl_rtl,
9645 copy_rtx (XEXP (decl_rtl, 0)));
9648 /* If we got something, return it. But first, set the alignment
9649 if the address is a register. */
9650 if (temp != 0)
9652 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9653 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9655 return temp;
9658 /* If the mode of DECL_RTL does not match that of the decl,
9659 there are two cases: we are dealing with a BLKmode value
9660 that is returned in a register, or we are dealing with
9661 a promoted value. In the latter case, return a SUBREG
9662 of the wanted mode, but mark it so that we know that it
9663 was already extended. */
9664 if (REG_P (decl_rtl)
9665 && DECL_MODE (exp) != BLKmode
9666 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9668 machine_mode pmode;
9670 /* Get the signedness to be used for this variable. Ensure we get
9671 the same mode we got when the variable was declared. */
9672 if (code == SSA_NAME
9673 && (g = SSA_NAME_DEF_STMT (ssa_name))
9674 && gimple_code (g) == GIMPLE_CALL
9675 && !gimple_call_internal_p (g))
9676 pmode = promote_function_mode (type, mode, &unsignedp,
9677 gimple_call_fntype (g),
9679 else
9680 pmode = promote_decl_mode (exp, &unsignedp);
9681 gcc_assert (GET_MODE (decl_rtl) == pmode);
9683 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9684 SUBREG_PROMOTED_VAR_P (temp) = 1;
9685 SUBREG_PROMOTED_SET (temp, unsignedp);
9686 return temp;
9689 return decl_rtl;
9691 case INTEGER_CST:
9692 /* Given that TYPE_PRECISION (type) is not always equal to
9693 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9694 the former to the latter according to the signedness of the
9695 type. */
9696 temp = immed_wide_int_const (wide_int::from
9697 (exp,
9698 GET_MODE_PRECISION (TYPE_MODE (type)),
9699 TYPE_SIGN (type)),
9700 TYPE_MODE (type));
9701 return temp;
9703 case VECTOR_CST:
9705 tree tmp = NULL_TREE;
9706 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9707 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9708 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9709 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9710 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9711 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9712 return const_vector_from_tree (exp);
9713 if (GET_MODE_CLASS (mode) == MODE_INT)
9715 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9716 if (type_for_mode)
9717 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9719 if (!tmp)
9721 vec<constructor_elt, va_gc> *v;
9722 unsigned i;
9723 vec_alloc (v, VECTOR_CST_NELTS (exp));
9724 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9725 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9726 tmp = build_constructor (type, v);
9728 return expand_expr (tmp, ignore ? const0_rtx : target,
9729 tmode, modifier);
9732 case CONST_DECL:
9733 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9735 case REAL_CST:
9736 /* If optimized, generate immediate CONST_DOUBLE
9737 which will be turned into memory by reload if necessary.
9739 We used to force a register so that loop.c could see it. But
9740 this does not allow gen_* patterns to perform optimizations with
9741 the constants. It also produces two insns in cases like "x = 1.0;".
9742 On most machines, floating-point constants are not permitted in
9743 many insns, so we'd end up copying it to a register in any case.
9745 Now, we do the copying in expand_binop, if appropriate. */
9746 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9747 TYPE_MODE (TREE_TYPE (exp)));
9749 case FIXED_CST:
9750 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9751 TYPE_MODE (TREE_TYPE (exp)));
9753 case COMPLEX_CST:
9754 /* Handle evaluating a complex constant in a CONCAT target. */
9755 if (original_target && GET_CODE (original_target) == CONCAT)
9757 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9758 rtx rtarg, itarg;
9760 rtarg = XEXP (original_target, 0);
9761 itarg = XEXP (original_target, 1);
9763 /* Move the real and imaginary parts separately. */
9764 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9765 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9767 if (op0 != rtarg)
9768 emit_move_insn (rtarg, op0);
9769 if (op1 != itarg)
9770 emit_move_insn (itarg, op1);
9772 return original_target;
9775 /* ... fall through ... */
9777 case STRING_CST:
9778 temp = expand_expr_constant (exp, 1, modifier);
9780 /* temp contains a constant address.
9781 On RISC machines where a constant address isn't valid,
9782 make some insns to get that address into a register. */
9783 if (modifier != EXPAND_CONST_ADDRESS
9784 && modifier != EXPAND_INITIALIZER
9785 && modifier != EXPAND_SUM
9786 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9787 MEM_ADDR_SPACE (temp)))
9788 return replace_equiv_address (temp,
9789 copy_rtx (XEXP (temp, 0)));
9790 return temp;
9792 case SAVE_EXPR:
9794 tree val = treeop0;
9795 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9796 inner_reference_p);
9798 if (!SAVE_EXPR_RESOLVED_P (exp))
9800 /* We can indeed still hit this case, typically via builtin
9801 expanders calling save_expr immediately before expanding
9802 something. Assume this means that we only have to deal
9803 with non-BLKmode values. */
9804 gcc_assert (GET_MODE (ret) != BLKmode);
9806 val = build_decl (curr_insn_location (),
9807 VAR_DECL, NULL, TREE_TYPE (exp));
9808 DECL_ARTIFICIAL (val) = 1;
9809 DECL_IGNORED_P (val) = 1;
9810 treeop0 = val;
9811 TREE_OPERAND (exp, 0) = treeop0;
9812 SAVE_EXPR_RESOLVED_P (exp) = 1;
9814 if (!CONSTANT_P (ret))
9815 ret = copy_to_reg (ret);
9816 SET_DECL_RTL (val, ret);
9819 return ret;
9823 case CONSTRUCTOR:
9824 /* If we don't need the result, just ensure we evaluate any
9825 subexpressions. */
9826 if (ignore)
9828 unsigned HOST_WIDE_INT idx;
9829 tree value;
9831 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9832 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9834 return const0_rtx;
9837 return expand_constructor (exp, target, modifier, false);
9839 case TARGET_MEM_REF:
9841 addr_space_t as
9842 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9843 enum insn_code icode;
9844 unsigned int align;
9846 op0 = addr_for_mem_ref (exp, as, true);
9847 op0 = memory_address_addr_space (mode, op0, as);
9848 temp = gen_rtx_MEM (mode, op0);
9849 set_mem_attributes (temp, exp, 0);
9850 set_mem_addr_space (temp, as);
9851 align = get_object_alignment (exp);
9852 if (modifier != EXPAND_WRITE
9853 && modifier != EXPAND_MEMORY
9854 && mode != BLKmode
9855 && align < GET_MODE_ALIGNMENT (mode)
9856 /* If the target does not have special handling for unaligned
9857 loads of mode then it can use regular moves for them. */
9858 && ((icode = optab_handler (movmisalign_optab, mode))
9859 != CODE_FOR_nothing))
9861 struct expand_operand ops[2];
9863 /* We've already validated the memory, and we're creating a
9864 new pseudo destination. The predicates really can't fail,
9865 nor can the generator. */
9866 create_output_operand (&ops[0], NULL_RTX, mode);
9867 create_fixed_operand (&ops[1], temp);
9868 expand_insn (icode, 2, ops);
9869 temp = ops[0].value;
9871 return temp;
9874 case MEM_REF:
9876 addr_space_t as
9877 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9878 machine_mode address_mode;
9879 tree base = TREE_OPERAND (exp, 0);
9880 gimple def_stmt;
9881 enum insn_code icode;
9882 unsigned align;
9883 /* Handle expansion of non-aliased memory with non-BLKmode. That
9884 might end up in a register. */
9885 if (mem_ref_refers_to_non_mem_p (exp))
9887 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9888 base = TREE_OPERAND (base, 0);
9889 if (offset == 0
9890 && tree_fits_uhwi_p (TYPE_SIZE (type))
9891 && (GET_MODE_BITSIZE (DECL_MODE (base))
9892 == tree_to_uhwi (TYPE_SIZE (type))))
9893 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9894 target, tmode, modifier);
9895 if (TYPE_MODE (type) == BLKmode)
9897 temp = assign_stack_temp (DECL_MODE (base),
9898 GET_MODE_SIZE (DECL_MODE (base)));
9899 store_expr (base, temp, 0, false);
9900 temp = adjust_address (temp, BLKmode, offset);
9901 set_mem_size (temp, int_size_in_bytes (type));
9902 return temp;
9904 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9905 bitsize_int (offset * BITS_PER_UNIT));
9906 return expand_expr (exp, target, tmode, modifier);
9908 address_mode = targetm.addr_space.address_mode (as);
9909 base = TREE_OPERAND (exp, 0);
9910 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9912 tree mask = gimple_assign_rhs2 (def_stmt);
9913 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9914 gimple_assign_rhs1 (def_stmt), mask);
9915 TREE_OPERAND (exp, 0) = base;
9917 align = get_object_alignment (exp);
9918 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9919 op0 = memory_address_addr_space (mode, op0, as);
9920 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9922 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9923 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9924 op0 = memory_address_addr_space (mode, op0, as);
9926 temp = gen_rtx_MEM (mode, op0);
9927 set_mem_attributes (temp, exp, 0);
9928 set_mem_addr_space (temp, as);
9929 if (TREE_THIS_VOLATILE (exp))
9930 MEM_VOLATILE_P (temp) = 1;
9931 if (modifier != EXPAND_WRITE
9932 && modifier != EXPAND_MEMORY
9933 && !inner_reference_p
9934 && mode != BLKmode
9935 && align < GET_MODE_ALIGNMENT (mode))
9937 if ((icode = optab_handler (movmisalign_optab, mode))
9938 != CODE_FOR_nothing)
9940 struct expand_operand ops[2];
9942 /* We've already validated the memory, and we're creating a
9943 new pseudo destination. The predicates really can't fail,
9944 nor can the generator. */
9945 create_output_operand (&ops[0], NULL_RTX, mode);
9946 create_fixed_operand (&ops[1], temp);
9947 expand_insn (icode, 2, ops);
9948 temp = ops[0].value;
9950 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9951 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9952 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9953 (modifier == EXPAND_STACK_PARM
9954 ? NULL_RTX : target),
9955 mode, mode);
9957 return temp;
9960 case ARRAY_REF:
9963 tree array = treeop0;
9964 tree index = treeop1;
9965 tree init;
9967 /* Fold an expression like: "foo"[2].
9968 This is not done in fold so it won't happen inside &.
9969 Don't fold if this is for wide characters since it's too
9970 difficult to do correctly and this is a very rare case. */
9972 if (modifier != EXPAND_CONST_ADDRESS
9973 && modifier != EXPAND_INITIALIZER
9974 && modifier != EXPAND_MEMORY)
9976 tree t = fold_read_from_constant_string (exp);
9978 if (t)
9979 return expand_expr (t, target, tmode, modifier);
9982 /* If this is a constant index into a constant array,
9983 just get the value from the array. Handle both the cases when
9984 we have an explicit constructor and when our operand is a variable
9985 that was declared const. */
9987 if (modifier != EXPAND_CONST_ADDRESS
9988 && modifier != EXPAND_INITIALIZER
9989 && modifier != EXPAND_MEMORY
9990 && TREE_CODE (array) == CONSTRUCTOR
9991 && ! TREE_SIDE_EFFECTS (array)
9992 && TREE_CODE (index) == INTEGER_CST)
9994 unsigned HOST_WIDE_INT ix;
9995 tree field, value;
9997 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9998 field, value)
9999 if (tree_int_cst_equal (field, index))
10001 if (!TREE_SIDE_EFFECTS (value))
10002 return expand_expr (fold (value), target, tmode, modifier);
10003 break;
10007 else if (optimize >= 1
10008 && modifier != EXPAND_CONST_ADDRESS
10009 && modifier != EXPAND_INITIALIZER
10010 && modifier != EXPAND_MEMORY
10011 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10012 && TREE_CODE (index) == INTEGER_CST
10013 && (TREE_CODE (array) == VAR_DECL
10014 || TREE_CODE (array) == CONST_DECL)
10015 && (init = ctor_for_folding (array)) != error_mark_node)
10017 if (init == NULL_TREE)
10019 tree value = build_zero_cst (type);
10020 if (TREE_CODE (value) == CONSTRUCTOR)
10022 /* If VALUE is a CONSTRUCTOR, this optimization is only
10023 useful if this doesn't store the CONSTRUCTOR into
10024 memory. If it does, it is more efficient to just
10025 load the data from the array directly. */
10026 rtx ret = expand_constructor (value, target,
10027 modifier, true);
10028 if (ret == NULL_RTX)
10029 value = NULL_TREE;
10032 if (value)
10033 return expand_expr (value, target, tmode, modifier);
10035 else if (TREE_CODE (init) == CONSTRUCTOR)
10037 unsigned HOST_WIDE_INT ix;
10038 tree field, value;
10040 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10041 field, value)
10042 if (tree_int_cst_equal (field, index))
10044 if (TREE_SIDE_EFFECTS (value))
10045 break;
10047 if (TREE_CODE (value) == CONSTRUCTOR)
10049 /* If VALUE is a CONSTRUCTOR, this
10050 optimization is only useful if
10051 this doesn't store the CONSTRUCTOR
10052 into memory. If it does, it is more
10053 efficient to just load the data from
10054 the array directly. */
10055 rtx ret = expand_constructor (value, target,
10056 modifier, true);
10057 if (ret == NULL_RTX)
10058 break;
10061 return
10062 expand_expr (fold (value), target, tmode, modifier);
10065 else if (TREE_CODE (init) == STRING_CST)
10067 tree low_bound = array_ref_low_bound (exp);
10068 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10070 /* Optimize the special case of a zero lower bound.
10072 We convert the lower bound to sizetype to avoid problems
10073 with constant folding. E.g. suppose the lower bound is
10074 1 and its mode is QI. Without the conversion
10075 (ARRAY + (INDEX - (unsigned char)1))
10076 becomes
10077 (ARRAY + (-(unsigned char)1) + INDEX)
10078 which becomes
10079 (ARRAY + 255 + INDEX). Oops! */
10080 if (!integer_zerop (low_bound))
10081 index1 = size_diffop_loc (loc, index1,
10082 fold_convert_loc (loc, sizetype,
10083 low_bound));
10085 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10087 tree type = TREE_TYPE (TREE_TYPE (init));
10088 machine_mode mode = TYPE_MODE (type);
10090 if (GET_MODE_CLASS (mode) == MODE_INT
10091 && GET_MODE_SIZE (mode) == 1)
10092 return gen_int_mode (TREE_STRING_POINTER (init)
10093 [TREE_INT_CST_LOW (index1)],
10094 mode);
10099 goto normal_inner_ref;
10101 case COMPONENT_REF:
10102 /* If the operand is a CONSTRUCTOR, we can just extract the
10103 appropriate field if it is present. */
10104 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10106 unsigned HOST_WIDE_INT idx;
10107 tree field, value;
10109 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10110 idx, field, value)
10111 if (field == treeop1
10112 /* We can normally use the value of the field in the
10113 CONSTRUCTOR. However, if this is a bitfield in
10114 an integral mode that we can fit in a HOST_WIDE_INT,
10115 we must mask only the number of bits in the bitfield,
10116 since this is done implicitly by the constructor. If
10117 the bitfield does not meet either of those conditions,
10118 we can't do this optimization. */
10119 && (! DECL_BIT_FIELD (field)
10120 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10121 && (GET_MODE_PRECISION (DECL_MODE (field))
10122 <= HOST_BITS_PER_WIDE_INT))))
10124 if (DECL_BIT_FIELD (field)
10125 && modifier == EXPAND_STACK_PARM)
10126 target = 0;
10127 op0 = expand_expr (value, target, tmode, modifier);
10128 if (DECL_BIT_FIELD (field))
10130 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10131 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10133 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10135 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10136 imode);
10137 op0 = expand_and (imode, op0, op1, target);
10139 else
10141 int count = GET_MODE_PRECISION (imode) - bitsize;
10143 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10144 target, 0);
10145 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10146 target, 0);
10150 return op0;
10153 goto normal_inner_ref;
10155 case BIT_FIELD_REF:
10156 case ARRAY_RANGE_REF:
10157 normal_inner_ref:
10159 machine_mode mode1, mode2;
10160 HOST_WIDE_INT bitsize, bitpos;
10161 tree offset;
10162 int volatilep = 0, must_force_mem;
10163 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10164 &mode1, &unsignedp, &volatilep, true);
10165 rtx orig_op0, memloc;
10166 bool clear_mem_expr = false;
10168 /* If we got back the original object, something is wrong. Perhaps
10169 we are evaluating an expression too early. In any event, don't
10170 infinitely recurse. */
10171 gcc_assert (tem != exp);
10173 /* If TEM's type is a union of variable size, pass TARGET to the inner
10174 computation, since it will need a temporary and TARGET is known
10175 to have to do. This occurs in unchecked conversion in Ada. */
10176 orig_op0 = op0
10177 = expand_expr_real (tem,
10178 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10179 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10180 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10181 != INTEGER_CST)
10182 && modifier != EXPAND_STACK_PARM
10183 ? target : NULL_RTX),
10184 VOIDmode,
10185 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10186 NULL, true);
10188 /* If the field has a mode, we want to access it in the
10189 field's mode, not the computed mode.
10190 If a MEM has VOIDmode (external with incomplete type),
10191 use BLKmode for it instead. */
10192 if (MEM_P (op0))
10194 if (mode1 != VOIDmode)
10195 op0 = adjust_address (op0, mode1, 0);
10196 else if (GET_MODE (op0) == VOIDmode)
10197 op0 = adjust_address (op0, BLKmode, 0);
10200 mode2
10201 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10203 /* If we have either an offset, a BLKmode result, or a reference
10204 outside the underlying object, we must force it to memory.
10205 Such a case can occur in Ada if we have unchecked conversion
10206 of an expression from a scalar type to an aggregate type or
10207 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10208 passed a partially uninitialized object or a view-conversion
10209 to a larger size. */
10210 must_force_mem = (offset
10211 || mode1 == BLKmode
10212 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10214 /* Handle CONCAT first. */
10215 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10217 if (bitpos == 0
10218 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10219 return op0;
10220 if (bitpos == 0
10221 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10222 && bitsize)
10224 op0 = XEXP (op0, 0);
10225 mode2 = GET_MODE (op0);
10227 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10228 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10229 && bitpos
10230 && bitsize)
10232 op0 = XEXP (op0, 1);
10233 bitpos = 0;
10234 mode2 = GET_MODE (op0);
10236 else
10237 /* Otherwise force into memory. */
10238 must_force_mem = 1;
10241 /* If this is a constant, put it in a register if it is a legitimate
10242 constant and we don't need a memory reference. */
10243 if (CONSTANT_P (op0)
10244 && mode2 != BLKmode
10245 && targetm.legitimate_constant_p (mode2, op0)
10246 && !must_force_mem)
10247 op0 = force_reg (mode2, op0);
10249 /* Otherwise, if this is a constant, try to force it to the constant
10250 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10251 is a legitimate constant. */
10252 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10253 op0 = validize_mem (memloc);
10255 /* Otherwise, if this is a constant or the object is not in memory
10256 and need be, put it there. */
10257 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10259 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10260 emit_move_insn (memloc, op0);
10261 op0 = memloc;
10262 clear_mem_expr = true;
10265 if (offset)
10267 machine_mode address_mode;
10268 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10269 EXPAND_SUM);
10271 gcc_assert (MEM_P (op0));
10273 address_mode = get_address_mode (op0);
10274 if (GET_MODE (offset_rtx) != address_mode)
10276 /* We cannot be sure that the RTL in offset_rtx is valid outside
10277 of a memory address context, so force it into a register
10278 before attempting to convert it to the desired mode. */
10279 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10280 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10283 /* See the comment in expand_assignment for the rationale. */
10284 if (mode1 != VOIDmode
10285 && bitpos != 0
10286 && bitsize > 0
10287 && (bitpos % bitsize) == 0
10288 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10289 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10291 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10292 bitpos = 0;
10295 op0 = offset_address (op0, offset_rtx,
10296 highest_pow2_factor (offset));
10299 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10300 record its alignment as BIGGEST_ALIGNMENT. */
10301 if (MEM_P (op0) && bitpos == 0 && offset != 0
10302 && is_aligning_offset (offset, tem))
10303 set_mem_align (op0, BIGGEST_ALIGNMENT);
10305 /* Don't forget about volatility even if this is a bitfield. */
10306 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10308 if (op0 == orig_op0)
10309 op0 = copy_rtx (op0);
10311 MEM_VOLATILE_P (op0) = 1;
10314 /* In cases where an aligned union has an unaligned object
10315 as a field, we might be extracting a BLKmode value from
10316 an integer-mode (e.g., SImode) object. Handle this case
10317 by doing the extract into an object as wide as the field
10318 (which we know to be the width of a basic mode), then
10319 storing into memory, and changing the mode to BLKmode. */
10320 if (mode1 == VOIDmode
10321 || REG_P (op0) || GET_CODE (op0) == SUBREG
10322 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10323 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10324 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10325 && modifier != EXPAND_CONST_ADDRESS
10326 && modifier != EXPAND_INITIALIZER
10327 && modifier != EXPAND_MEMORY)
10328 /* If the bitfield is volatile and the bitsize
10329 is narrower than the access size of the bitfield,
10330 we need to extract bitfields from the access. */
10331 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10332 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10333 && mode1 != BLKmode
10334 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10335 /* If the field isn't aligned enough to fetch as a memref,
10336 fetch it as a bit field. */
10337 || (mode1 != BLKmode
10338 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10339 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10340 || (MEM_P (op0)
10341 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10342 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10343 && modifier != EXPAND_MEMORY
10344 && ((modifier == EXPAND_CONST_ADDRESS
10345 || modifier == EXPAND_INITIALIZER)
10346 ? STRICT_ALIGNMENT
10347 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10348 || (bitpos % BITS_PER_UNIT != 0)))
10349 /* If the type and the field are a constant size and the
10350 size of the type isn't the same size as the bitfield,
10351 we must use bitfield operations. */
10352 || (bitsize >= 0
10353 && TYPE_SIZE (TREE_TYPE (exp))
10354 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10355 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10356 bitsize)))
10358 machine_mode ext_mode = mode;
10360 if (ext_mode == BLKmode
10361 && ! (target != 0 && MEM_P (op0)
10362 && MEM_P (target)
10363 && bitpos % BITS_PER_UNIT == 0))
10364 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10366 if (ext_mode == BLKmode)
10368 if (target == 0)
10369 target = assign_temp (type, 1, 1);
10371 /* ??? Unlike the similar test a few lines below, this one is
10372 very likely obsolete. */
10373 if (bitsize == 0)
10374 return target;
10376 /* In this case, BITPOS must start at a byte boundary and
10377 TARGET, if specified, must be a MEM. */
10378 gcc_assert (MEM_P (op0)
10379 && (!target || MEM_P (target))
10380 && !(bitpos % BITS_PER_UNIT));
10382 emit_block_move (target,
10383 adjust_address (op0, VOIDmode,
10384 bitpos / BITS_PER_UNIT),
10385 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10386 / BITS_PER_UNIT),
10387 (modifier == EXPAND_STACK_PARM
10388 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10390 return target;
10393 /* If we have nothing to extract, the result will be 0 for targets
10394 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10395 return 0 for the sake of consistency, as reading a zero-sized
10396 bitfield is valid in Ada and the value is fully specified. */
10397 if (bitsize == 0)
10398 return const0_rtx;
10400 op0 = validize_mem (op0);
10402 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10403 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10405 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10406 (modifier == EXPAND_STACK_PARM
10407 ? NULL_RTX : target),
10408 ext_mode, ext_mode);
10410 /* If the result is a record type and BITSIZE is narrower than
10411 the mode of OP0, an integral mode, and this is a big endian
10412 machine, we must put the field into the high-order bits. */
10413 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10414 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10415 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10416 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10417 GET_MODE_BITSIZE (GET_MODE (op0))
10418 - bitsize, op0, 1);
10420 /* If the result type is BLKmode, store the data into a temporary
10421 of the appropriate type, but with the mode corresponding to the
10422 mode for the data we have (op0's mode). */
10423 if (mode == BLKmode)
10425 rtx new_rtx
10426 = assign_stack_temp_for_type (ext_mode,
10427 GET_MODE_BITSIZE (ext_mode),
10428 type);
10429 emit_move_insn (new_rtx, op0);
10430 op0 = copy_rtx (new_rtx);
10431 PUT_MODE (op0, BLKmode);
10434 return op0;
10437 /* If the result is BLKmode, use that to access the object
10438 now as well. */
10439 if (mode == BLKmode)
10440 mode1 = BLKmode;
10442 /* Get a reference to just this component. */
10443 if (modifier == EXPAND_CONST_ADDRESS
10444 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10445 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10446 else
10447 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10449 if (op0 == orig_op0)
10450 op0 = copy_rtx (op0);
10452 set_mem_attributes (op0, exp, 0);
10454 if (REG_P (XEXP (op0, 0)))
10455 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10457 /* If op0 is a temporary because the original expressions was forced
10458 to memory, clear MEM_EXPR so that the original expression cannot
10459 be marked as addressable through MEM_EXPR of the temporary. */
10460 if (clear_mem_expr)
10461 set_mem_expr (op0, NULL_TREE);
10463 MEM_VOLATILE_P (op0) |= volatilep;
10464 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10465 || modifier == EXPAND_CONST_ADDRESS
10466 || modifier == EXPAND_INITIALIZER)
10467 return op0;
10469 if (target == 0)
10470 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10472 convert_move (target, op0, unsignedp);
10473 return target;
10476 case OBJ_TYPE_REF:
10477 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10479 case CALL_EXPR:
10480 /* All valid uses of __builtin_va_arg_pack () are removed during
10481 inlining. */
10482 if (CALL_EXPR_VA_ARG_PACK (exp))
10483 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10485 tree fndecl = get_callee_fndecl (exp), attr;
10487 if (fndecl
10488 && (attr = lookup_attribute ("error",
10489 DECL_ATTRIBUTES (fndecl))) != NULL)
10490 error ("%Kcall to %qs declared with attribute error: %s",
10491 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10492 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10493 if (fndecl
10494 && (attr = lookup_attribute ("warning",
10495 DECL_ATTRIBUTES (fndecl))) != NULL)
10496 warning_at (tree_nonartificial_location (exp),
10497 0, "%Kcall to %qs declared with attribute warning: %s",
10498 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10499 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10501 /* Check for a built-in function. */
10502 if (fndecl && DECL_BUILT_IN (fndecl))
10504 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10505 if (CALL_WITH_BOUNDS_P (exp))
10506 return expand_builtin_with_bounds (exp, target, subtarget,
10507 tmode, ignore);
10508 else
10509 return expand_builtin (exp, target, subtarget, tmode, ignore);
10512 return expand_call (exp, target, ignore);
10514 case VIEW_CONVERT_EXPR:
10515 op0 = NULL_RTX;
10517 /* If we are converting to BLKmode, try to avoid an intermediate
10518 temporary by fetching an inner memory reference. */
10519 if (mode == BLKmode
10520 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10521 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10522 && handled_component_p (treeop0))
10524 machine_mode mode1;
10525 HOST_WIDE_INT bitsize, bitpos;
10526 tree offset;
10527 int unsignedp;
10528 int volatilep = 0;
10529 tree tem
10530 = get_inner_reference (treeop0, &bitsize, &bitpos,
10531 &offset, &mode1, &unsignedp, &volatilep,
10532 true);
10533 rtx orig_op0;
10535 /* ??? We should work harder and deal with non-zero offsets. */
10536 if (!offset
10537 && (bitpos % BITS_PER_UNIT) == 0
10538 && bitsize >= 0
10539 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10541 /* See the normal_inner_ref case for the rationale. */
10542 orig_op0
10543 = expand_expr_real (tem,
10544 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10545 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10546 != INTEGER_CST)
10547 && modifier != EXPAND_STACK_PARM
10548 ? target : NULL_RTX),
10549 VOIDmode,
10550 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10551 NULL, true);
10553 if (MEM_P (orig_op0))
10555 op0 = orig_op0;
10557 /* Get a reference to just this component. */
10558 if (modifier == EXPAND_CONST_ADDRESS
10559 || modifier == EXPAND_SUM
10560 || modifier == EXPAND_INITIALIZER)
10561 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10562 else
10563 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10565 if (op0 == orig_op0)
10566 op0 = copy_rtx (op0);
10568 set_mem_attributes (op0, treeop0, 0);
10569 if (REG_P (XEXP (op0, 0)))
10570 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10572 MEM_VOLATILE_P (op0) |= volatilep;
10577 if (!op0)
10578 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10579 NULL, inner_reference_p);
10581 /* If the input and output modes are both the same, we are done. */
10582 if (mode == GET_MODE (op0))
10584 /* If neither mode is BLKmode, and both modes are the same size
10585 then we can use gen_lowpart. */
10586 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10587 && (GET_MODE_PRECISION (mode)
10588 == GET_MODE_PRECISION (GET_MODE (op0)))
10589 && !COMPLEX_MODE_P (GET_MODE (op0)))
10591 if (GET_CODE (op0) == SUBREG)
10592 op0 = force_reg (GET_MODE (op0), op0);
10593 temp = gen_lowpart_common (mode, op0);
10594 if (temp)
10595 op0 = temp;
10596 else
10598 if (!REG_P (op0) && !MEM_P (op0))
10599 op0 = force_reg (GET_MODE (op0), op0);
10600 op0 = gen_lowpart (mode, op0);
10603 /* If both types are integral, convert from one mode to the other. */
10604 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10605 op0 = convert_modes (mode, GET_MODE (op0), op0,
10606 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10607 /* If the output type is a bit-field type, do an extraction. */
10608 else if (reduce_bit_field)
10609 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10610 TYPE_UNSIGNED (type), NULL_RTX,
10611 mode, mode);
10612 /* As a last resort, spill op0 to memory, and reload it in a
10613 different mode. */
10614 else if (!MEM_P (op0))
10616 /* If the operand is not a MEM, force it into memory. Since we
10617 are going to be changing the mode of the MEM, don't call
10618 force_const_mem for constants because we don't allow pool
10619 constants to change mode. */
10620 tree inner_type = TREE_TYPE (treeop0);
10622 gcc_assert (!TREE_ADDRESSABLE (exp));
10624 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10625 target
10626 = assign_stack_temp_for_type
10627 (TYPE_MODE (inner_type),
10628 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10630 emit_move_insn (target, op0);
10631 op0 = target;
10634 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10635 output type is such that the operand is known to be aligned, indicate
10636 that it is. Otherwise, we need only be concerned about alignment for
10637 non-BLKmode results. */
10638 if (MEM_P (op0))
10640 enum insn_code icode;
10642 if (TYPE_ALIGN_OK (type))
10644 /* ??? Copying the MEM without substantially changing it might
10645 run afoul of the code handling volatile memory references in
10646 store_expr, which assumes that TARGET is returned unmodified
10647 if it has been used. */
10648 op0 = copy_rtx (op0);
10649 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10651 else if (modifier != EXPAND_WRITE
10652 && modifier != EXPAND_MEMORY
10653 && !inner_reference_p
10654 && mode != BLKmode
10655 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10657 /* If the target does have special handling for unaligned
10658 loads of mode then use them. */
10659 if ((icode = optab_handler (movmisalign_optab, mode))
10660 != CODE_FOR_nothing)
10662 rtx reg, insn;
10664 op0 = adjust_address (op0, mode, 0);
10665 /* We've already validated the memory, and we're creating a
10666 new pseudo destination. The predicates really can't
10667 fail. */
10668 reg = gen_reg_rtx (mode);
10670 /* Nor can the insn generator. */
10671 insn = GEN_FCN (icode) (reg, op0);
10672 emit_insn (insn);
10673 return reg;
10675 else if (STRICT_ALIGNMENT)
10677 tree inner_type = TREE_TYPE (treeop0);
10678 HOST_WIDE_INT temp_size
10679 = MAX (int_size_in_bytes (inner_type),
10680 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10681 rtx new_rtx
10682 = assign_stack_temp_for_type (mode, temp_size, type);
10683 rtx new_with_op0_mode
10684 = adjust_address (new_rtx, GET_MODE (op0), 0);
10686 gcc_assert (!TREE_ADDRESSABLE (exp));
10688 if (GET_MODE (op0) == BLKmode)
10689 emit_block_move (new_with_op0_mode, op0,
10690 GEN_INT (GET_MODE_SIZE (mode)),
10691 (modifier == EXPAND_STACK_PARM
10692 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10693 else
10694 emit_move_insn (new_with_op0_mode, op0);
10696 op0 = new_rtx;
10700 op0 = adjust_address (op0, mode, 0);
10703 return op0;
10705 case MODIFY_EXPR:
10707 tree lhs = treeop0;
10708 tree rhs = treeop1;
10709 gcc_assert (ignore);
10711 /* Check for |= or &= of a bitfield of size one into another bitfield
10712 of size 1. In this case, (unless we need the result of the
10713 assignment) we can do this more efficiently with a
10714 test followed by an assignment, if necessary.
10716 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10717 things change so we do, this code should be enhanced to
10718 support it. */
10719 if (TREE_CODE (lhs) == COMPONENT_REF
10720 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10721 || TREE_CODE (rhs) == BIT_AND_EXPR)
10722 && TREE_OPERAND (rhs, 0) == lhs
10723 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10724 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10725 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10727 rtx_code_label *label = gen_label_rtx ();
10728 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10729 do_jump (TREE_OPERAND (rhs, 1),
10730 value ? label : 0,
10731 value ? 0 : label, -1);
10732 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10733 false);
10734 do_pending_stack_adjust ();
10735 emit_label (label);
10736 return const0_rtx;
10739 expand_assignment (lhs, rhs, false);
10740 return const0_rtx;
10743 case ADDR_EXPR:
10744 return expand_expr_addr_expr (exp, target, tmode, modifier);
10746 case REALPART_EXPR:
10747 op0 = expand_normal (treeop0);
10748 return read_complex_part (op0, false);
10750 case IMAGPART_EXPR:
10751 op0 = expand_normal (treeop0);
10752 return read_complex_part (op0, true);
10754 case RETURN_EXPR:
10755 case LABEL_EXPR:
10756 case GOTO_EXPR:
10757 case SWITCH_EXPR:
10758 case ASM_EXPR:
10759 /* Expanded in cfgexpand.c. */
10760 gcc_unreachable ();
10762 case TRY_CATCH_EXPR:
10763 case CATCH_EXPR:
10764 case EH_FILTER_EXPR:
10765 case TRY_FINALLY_EXPR:
10766 /* Lowered by tree-eh.c. */
10767 gcc_unreachable ();
10769 case WITH_CLEANUP_EXPR:
10770 case CLEANUP_POINT_EXPR:
10771 case TARGET_EXPR:
10772 case CASE_LABEL_EXPR:
10773 case VA_ARG_EXPR:
10774 case BIND_EXPR:
10775 case INIT_EXPR:
10776 case CONJ_EXPR:
10777 case COMPOUND_EXPR:
10778 case PREINCREMENT_EXPR:
10779 case PREDECREMENT_EXPR:
10780 case POSTINCREMENT_EXPR:
10781 case POSTDECREMENT_EXPR:
10782 case LOOP_EXPR:
10783 case EXIT_EXPR:
10784 case COMPOUND_LITERAL_EXPR:
10785 /* Lowered by gimplify.c. */
10786 gcc_unreachable ();
10788 case FDESC_EXPR:
10789 /* Function descriptors are not valid except for as
10790 initialization constants, and should not be expanded. */
10791 gcc_unreachable ();
10793 case WITH_SIZE_EXPR:
10794 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10795 have pulled out the size to use in whatever context it needed. */
10796 return expand_expr_real (treeop0, original_target, tmode,
10797 modifier, alt_rtl, inner_reference_p);
10799 default:
10800 return expand_expr_real_2 (&ops, target, tmode, modifier);
10804 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10805 signedness of TYPE), possibly returning the result in TARGET. */
10806 static rtx
10807 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10809 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10810 if (target && GET_MODE (target) != GET_MODE (exp))
10811 target = 0;
10812 /* For constant values, reduce using build_int_cst_type. */
10813 if (CONST_INT_P (exp))
10815 HOST_WIDE_INT value = INTVAL (exp);
10816 tree t = build_int_cst_type (type, value);
10817 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10819 else if (TYPE_UNSIGNED (type))
10821 machine_mode mode = GET_MODE (exp);
10822 rtx mask = immed_wide_int_const
10823 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10824 return expand_and (mode, exp, mask, target);
10826 else
10828 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10829 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10830 exp, count, target, 0);
10831 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10832 exp, count, target, 0);
10836 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10837 when applied to the address of EXP produces an address known to be
10838 aligned more than BIGGEST_ALIGNMENT. */
10840 static int
10841 is_aligning_offset (const_tree offset, const_tree exp)
10843 /* Strip off any conversions. */
10844 while (CONVERT_EXPR_P (offset))
10845 offset = TREE_OPERAND (offset, 0);
10847 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10848 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10849 if (TREE_CODE (offset) != BIT_AND_EXPR
10850 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10851 || compare_tree_int (TREE_OPERAND (offset, 1),
10852 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10853 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10854 return 0;
10856 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10857 It must be NEGATE_EXPR. Then strip any more conversions. */
10858 offset = TREE_OPERAND (offset, 0);
10859 while (CONVERT_EXPR_P (offset))
10860 offset = TREE_OPERAND (offset, 0);
10862 if (TREE_CODE (offset) != NEGATE_EXPR)
10863 return 0;
10865 offset = TREE_OPERAND (offset, 0);
10866 while (CONVERT_EXPR_P (offset))
10867 offset = TREE_OPERAND (offset, 0);
10869 /* This must now be the address of EXP. */
10870 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10873 /* Return the tree node if an ARG corresponds to a string constant or zero
10874 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10875 in bytes within the string that ARG is accessing. The type of the
10876 offset will be `sizetype'. */
10878 tree
10879 string_constant (tree arg, tree *ptr_offset)
10881 tree array, offset, lower_bound;
10882 STRIP_NOPS (arg);
10884 if (TREE_CODE (arg) == ADDR_EXPR)
10886 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10888 *ptr_offset = size_zero_node;
10889 return TREE_OPERAND (arg, 0);
10891 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10893 array = TREE_OPERAND (arg, 0);
10894 offset = size_zero_node;
10896 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10898 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10899 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10900 if (TREE_CODE (array) != STRING_CST
10901 && TREE_CODE (array) != VAR_DECL)
10902 return 0;
10904 /* Check if the array has a nonzero lower bound. */
10905 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10906 if (!integer_zerop (lower_bound))
10908 /* If the offset and base aren't both constants, return 0. */
10909 if (TREE_CODE (lower_bound) != INTEGER_CST)
10910 return 0;
10911 if (TREE_CODE (offset) != INTEGER_CST)
10912 return 0;
10913 /* Adjust offset by the lower bound. */
10914 offset = size_diffop (fold_convert (sizetype, offset),
10915 fold_convert (sizetype, lower_bound));
10918 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10920 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10921 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10922 if (TREE_CODE (array) != ADDR_EXPR)
10923 return 0;
10924 array = TREE_OPERAND (array, 0);
10925 if (TREE_CODE (array) != STRING_CST
10926 && TREE_CODE (array) != VAR_DECL)
10927 return 0;
10929 else
10930 return 0;
10932 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10934 tree arg0 = TREE_OPERAND (arg, 0);
10935 tree arg1 = TREE_OPERAND (arg, 1);
10937 STRIP_NOPS (arg0);
10938 STRIP_NOPS (arg1);
10940 if (TREE_CODE (arg0) == ADDR_EXPR
10941 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10942 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10944 array = TREE_OPERAND (arg0, 0);
10945 offset = arg1;
10947 else if (TREE_CODE (arg1) == ADDR_EXPR
10948 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10949 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10951 array = TREE_OPERAND (arg1, 0);
10952 offset = arg0;
10954 else
10955 return 0;
10957 else
10958 return 0;
10960 if (TREE_CODE (array) == STRING_CST)
10962 *ptr_offset = fold_convert (sizetype, offset);
10963 return array;
10965 else if (TREE_CODE (array) == VAR_DECL
10966 || TREE_CODE (array) == CONST_DECL)
10968 int length;
10969 tree init = ctor_for_folding (array);
10971 /* Variables initialized to string literals can be handled too. */
10972 if (init == error_mark_node
10973 || !init
10974 || TREE_CODE (init) != STRING_CST)
10975 return 0;
10977 /* Avoid const char foo[4] = "abcde"; */
10978 if (DECL_SIZE_UNIT (array) == NULL_TREE
10979 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10980 || (length = TREE_STRING_LENGTH (init)) <= 0
10981 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10982 return 0;
10984 /* If variable is bigger than the string literal, OFFSET must be constant
10985 and inside of the bounds of the string literal. */
10986 offset = fold_convert (sizetype, offset);
10987 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10988 && (! tree_fits_uhwi_p (offset)
10989 || compare_tree_int (offset, length) >= 0))
10990 return 0;
10992 *ptr_offset = offset;
10993 return init;
10996 return 0;
10999 /* Generate code to calculate OPS, and exploded expression
11000 using a store-flag instruction and return an rtx for the result.
11001 OPS reflects a comparison.
11003 If TARGET is nonzero, store the result there if convenient.
11005 Return zero if there is no suitable set-flag instruction
11006 available on this machine.
11008 Once expand_expr has been called on the arguments of the comparison,
11009 we are committed to doing the store flag, since it is not safe to
11010 re-evaluate the expression. We emit the store-flag insn by calling
11011 emit_store_flag, but only expand the arguments if we have a reason
11012 to believe that emit_store_flag will be successful. If we think that
11013 it will, but it isn't, we have to simulate the store-flag with a
11014 set/jump/set sequence. */
11016 static rtx
11017 do_store_flag (sepops ops, rtx target, machine_mode mode)
11019 enum rtx_code code;
11020 tree arg0, arg1, type;
11021 tree tem;
11022 machine_mode operand_mode;
11023 int unsignedp;
11024 rtx op0, op1;
11025 rtx subtarget = target;
11026 location_t loc = ops->location;
11028 arg0 = ops->op0;
11029 arg1 = ops->op1;
11031 /* Don't crash if the comparison was erroneous. */
11032 if (arg0 == error_mark_node || arg1 == error_mark_node)
11033 return const0_rtx;
11035 type = TREE_TYPE (arg0);
11036 operand_mode = TYPE_MODE (type);
11037 unsignedp = TYPE_UNSIGNED (type);
11039 /* We won't bother with BLKmode store-flag operations because it would mean
11040 passing a lot of information to emit_store_flag. */
11041 if (operand_mode == BLKmode)
11042 return 0;
11044 /* We won't bother with store-flag operations involving function pointers
11045 when function pointers must be canonicalized before comparisons. */
11046 #ifdef HAVE_canonicalize_funcptr_for_compare
11047 if (HAVE_canonicalize_funcptr_for_compare
11048 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11049 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11050 == FUNCTION_TYPE))
11051 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11052 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11053 == FUNCTION_TYPE))))
11054 return 0;
11055 #endif
11057 STRIP_NOPS (arg0);
11058 STRIP_NOPS (arg1);
11060 /* For vector typed comparisons emit code to generate the desired
11061 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11062 expander for this. */
11063 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11065 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11066 tree if_true = constant_boolean_node (true, ops->type);
11067 tree if_false = constant_boolean_node (false, ops->type);
11068 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
11071 /* Get the rtx comparison code to use. We know that EXP is a comparison
11072 operation of some type. Some comparisons against 1 and -1 can be
11073 converted to comparisons with zero. Do so here so that the tests
11074 below will be aware that we have a comparison with zero. These
11075 tests will not catch constants in the first operand, but constants
11076 are rarely passed as the first operand. */
11078 switch (ops->code)
11080 case EQ_EXPR:
11081 code = EQ;
11082 break;
11083 case NE_EXPR:
11084 code = NE;
11085 break;
11086 case LT_EXPR:
11087 if (integer_onep (arg1))
11088 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11089 else
11090 code = unsignedp ? LTU : LT;
11091 break;
11092 case LE_EXPR:
11093 if (! unsignedp && integer_all_onesp (arg1))
11094 arg1 = integer_zero_node, code = LT;
11095 else
11096 code = unsignedp ? LEU : LE;
11097 break;
11098 case GT_EXPR:
11099 if (! unsignedp && integer_all_onesp (arg1))
11100 arg1 = integer_zero_node, code = GE;
11101 else
11102 code = unsignedp ? GTU : GT;
11103 break;
11104 case GE_EXPR:
11105 if (integer_onep (arg1))
11106 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11107 else
11108 code = unsignedp ? GEU : GE;
11109 break;
11111 case UNORDERED_EXPR:
11112 code = UNORDERED;
11113 break;
11114 case ORDERED_EXPR:
11115 code = ORDERED;
11116 break;
11117 case UNLT_EXPR:
11118 code = UNLT;
11119 break;
11120 case UNLE_EXPR:
11121 code = UNLE;
11122 break;
11123 case UNGT_EXPR:
11124 code = UNGT;
11125 break;
11126 case UNGE_EXPR:
11127 code = UNGE;
11128 break;
11129 case UNEQ_EXPR:
11130 code = UNEQ;
11131 break;
11132 case LTGT_EXPR:
11133 code = LTGT;
11134 break;
11136 default:
11137 gcc_unreachable ();
11140 /* Put a constant second. */
11141 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11142 || TREE_CODE (arg0) == FIXED_CST)
11144 tem = arg0; arg0 = arg1; arg1 = tem;
11145 code = swap_condition (code);
11148 /* If this is an equality or inequality test of a single bit, we can
11149 do this by shifting the bit being tested to the low-order bit and
11150 masking the result with the constant 1. If the condition was EQ,
11151 we xor it with 1. This does not require an scc insn and is faster
11152 than an scc insn even if we have it.
11154 The code to make this transformation was moved into fold_single_bit_test,
11155 so we just call into the folder and expand its result. */
11157 if ((code == NE || code == EQ)
11158 && integer_zerop (arg1)
11159 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11161 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11162 if (srcstmt
11163 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11165 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11166 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11167 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11168 gimple_assign_rhs1 (srcstmt),
11169 gimple_assign_rhs2 (srcstmt));
11170 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11171 if (temp)
11172 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11176 if (! get_subtarget (target)
11177 || GET_MODE (subtarget) != operand_mode)
11178 subtarget = 0;
11180 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11182 if (target == 0)
11183 target = gen_reg_rtx (mode);
11185 /* Try a cstore if possible. */
11186 return emit_store_flag_force (target, code, op0, op1,
11187 operand_mode, unsignedp,
11188 (TYPE_PRECISION (ops->type) == 1
11189 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11193 /* Stubs in case we haven't got a casesi insn. */
11194 #ifndef HAVE_casesi
11195 # define HAVE_casesi 0
11196 # define gen_casesi(a, b, c, d, e) (0)
11197 # define CODE_FOR_casesi CODE_FOR_nothing
11198 #endif
11200 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11201 0 otherwise (i.e. if there is no casesi instruction).
11203 DEFAULT_PROBABILITY is the probability of jumping to the default
11204 label. */
11206 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11207 rtx table_label, rtx default_label, rtx fallback_label,
11208 int default_probability)
11210 struct expand_operand ops[5];
11211 machine_mode index_mode = SImode;
11212 rtx op1, op2, index;
11214 if (! HAVE_casesi)
11215 return 0;
11217 /* Convert the index to SImode. */
11218 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11220 machine_mode omode = TYPE_MODE (index_type);
11221 rtx rangertx = expand_normal (range);
11223 /* We must handle the endpoints in the original mode. */
11224 index_expr = build2 (MINUS_EXPR, index_type,
11225 index_expr, minval);
11226 minval = integer_zero_node;
11227 index = expand_normal (index_expr);
11228 if (default_label)
11229 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11230 omode, 1, default_label,
11231 default_probability);
11232 /* Now we can safely truncate. */
11233 index = convert_to_mode (index_mode, index, 0);
11235 else
11237 if (TYPE_MODE (index_type) != index_mode)
11239 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11240 index_expr = fold_convert (index_type, index_expr);
11243 index = expand_normal (index_expr);
11246 do_pending_stack_adjust ();
11248 op1 = expand_normal (minval);
11249 op2 = expand_normal (range);
11251 create_input_operand (&ops[0], index, index_mode);
11252 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11253 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11254 create_fixed_operand (&ops[3], table_label);
11255 create_fixed_operand (&ops[4], (default_label
11256 ? default_label
11257 : fallback_label));
11258 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11259 return 1;
11262 /* Attempt to generate a tablejump instruction; same concept. */
11263 #ifndef HAVE_tablejump
11264 #define HAVE_tablejump 0
11265 #define gen_tablejump(x, y) (0)
11266 #endif
11268 /* Subroutine of the next function.
11270 INDEX is the value being switched on, with the lowest value
11271 in the table already subtracted.
11272 MODE is its expected mode (needed if INDEX is constant).
11273 RANGE is the length of the jump table.
11274 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11276 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11277 index value is out of range.
11278 DEFAULT_PROBABILITY is the probability of jumping to
11279 the default label. */
11281 static void
11282 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11283 rtx default_label, int default_probability)
11285 rtx temp, vector;
11287 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11288 cfun->cfg->max_jumptable_ents = INTVAL (range);
11290 /* Do an unsigned comparison (in the proper mode) between the index
11291 expression and the value which represents the length of the range.
11292 Since we just finished subtracting the lower bound of the range
11293 from the index expression, this comparison allows us to simultaneously
11294 check that the original index expression value is both greater than
11295 or equal to the minimum value of the range and less than or equal to
11296 the maximum value of the range. */
11298 if (default_label)
11299 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11300 default_label, default_probability);
11303 /* If index is in range, it must fit in Pmode.
11304 Convert to Pmode so we can index with it. */
11305 if (mode != Pmode)
11306 index = convert_to_mode (Pmode, index, 1);
11308 /* Don't let a MEM slip through, because then INDEX that comes
11309 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11310 and break_out_memory_refs will go to work on it and mess it up. */
11311 #ifdef PIC_CASE_VECTOR_ADDRESS
11312 if (flag_pic && !REG_P (index))
11313 index = copy_to_mode_reg (Pmode, index);
11314 #endif
11316 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11317 GET_MODE_SIZE, because this indicates how large insns are. The other
11318 uses should all be Pmode, because they are addresses. This code
11319 could fail if addresses and insns are not the same size. */
11320 index = simplify_gen_binary (MULT, Pmode, index,
11321 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11322 Pmode));
11323 index = simplify_gen_binary (PLUS, Pmode, index,
11324 gen_rtx_LABEL_REF (Pmode, table_label));
11326 #ifdef PIC_CASE_VECTOR_ADDRESS
11327 if (flag_pic)
11328 index = PIC_CASE_VECTOR_ADDRESS (index);
11329 else
11330 #endif
11331 index = memory_address (CASE_VECTOR_MODE, index);
11332 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11333 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11334 convert_move (temp, vector, 0);
11336 emit_jump_insn (gen_tablejump (temp, table_label));
11338 /* If we are generating PIC code or if the table is PC-relative, the
11339 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11340 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11341 emit_barrier ();
11345 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11346 rtx table_label, rtx default_label, int default_probability)
11348 rtx index;
11350 if (! HAVE_tablejump)
11351 return 0;
11353 index_expr = fold_build2 (MINUS_EXPR, index_type,
11354 fold_convert (index_type, index_expr),
11355 fold_convert (index_type, minval));
11356 index = expand_normal (index_expr);
11357 do_pending_stack_adjust ();
11359 do_tablejump (index, TYPE_MODE (index_type),
11360 convert_modes (TYPE_MODE (index_type),
11361 TYPE_MODE (TREE_TYPE (range)),
11362 expand_normal (range),
11363 TYPE_UNSIGNED (TREE_TYPE (range))),
11364 table_label, default_label, default_probability);
11365 return 1;
11368 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11369 static rtx
11370 const_vector_from_tree (tree exp)
11372 rtvec v;
11373 unsigned i;
11374 int units;
11375 tree elt;
11376 machine_mode inner, mode;
11378 mode = TYPE_MODE (TREE_TYPE (exp));
11380 if (initializer_zerop (exp))
11381 return CONST0_RTX (mode);
11383 units = GET_MODE_NUNITS (mode);
11384 inner = GET_MODE_INNER (mode);
11386 v = rtvec_alloc (units);
11388 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11390 elt = VECTOR_CST_ELT (exp, i);
11392 if (TREE_CODE (elt) == REAL_CST)
11393 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11394 inner);
11395 else if (TREE_CODE (elt) == FIXED_CST)
11396 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11397 inner);
11398 else
11399 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11402 return gen_rtx_CONST_VECTOR (mode, v);
11405 /* Build a decl for a personality function given a language prefix. */
11407 tree
11408 build_personality_function (const char *lang)
11410 const char *unwind_and_version;
11411 tree decl, type;
11412 char *name;
11414 switch (targetm_common.except_unwind_info (&global_options))
11416 case UI_NONE:
11417 return NULL;
11418 case UI_SJLJ:
11419 unwind_and_version = "_sj0";
11420 break;
11421 case UI_DWARF2:
11422 case UI_TARGET:
11423 unwind_and_version = "_v0";
11424 break;
11425 case UI_SEH:
11426 unwind_and_version = "_seh0";
11427 break;
11428 default:
11429 gcc_unreachable ();
11432 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11434 type = build_function_type_list (integer_type_node, integer_type_node,
11435 long_long_unsigned_type_node,
11436 ptr_type_node, ptr_type_node, NULL_TREE);
11437 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11438 get_identifier (name), type);
11439 DECL_ARTIFICIAL (decl) = 1;
11440 DECL_EXTERNAL (decl) = 1;
11441 TREE_PUBLIC (decl) = 1;
11443 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11444 are the flags assigned by targetm.encode_section_info. */
11445 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11447 return decl;
11450 /* Extracts the personality function of DECL and returns the corresponding
11451 libfunc. */
11454 get_personality_function (tree decl)
11456 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11457 enum eh_personality_kind pk;
11459 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11460 if (pk == eh_personality_none)
11461 return NULL;
11463 if (!personality
11464 && pk == eh_personality_any)
11465 personality = lang_hooks.eh_personality ();
11467 if (pk == eh_personality_lang)
11468 gcc_assert (personality != NULL_TREE);
11470 return XEXP (DECL_RTL (personality), 0);
11473 /* Returns a tree for the size of EXP in bytes. */
11475 static tree
11476 tree_expr_size (const_tree exp)
11478 if (DECL_P (exp)
11479 && DECL_SIZE_UNIT (exp) != 0)
11480 return DECL_SIZE_UNIT (exp);
11481 else
11482 return size_in_bytes (TREE_TYPE (exp));
11485 /* Return an rtx for the size in bytes of the value of EXP. */
11488 expr_size (tree exp)
11490 tree size;
11492 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11493 size = TREE_OPERAND (exp, 1);
11494 else
11496 size = tree_expr_size (exp);
11497 gcc_assert (size);
11498 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11501 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11504 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11505 if the size can vary or is larger than an integer. */
11507 static HOST_WIDE_INT
11508 int_expr_size (tree exp)
11510 tree size;
11512 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11513 size = TREE_OPERAND (exp, 1);
11514 else
11516 size = tree_expr_size (exp);
11517 gcc_assert (size);
11520 if (size == 0 || !tree_fits_shwi_p (size))
11521 return -1;
11523 return tree_to_shwi (size);
11526 #include "gt-expr.h"