PR middle-end/66633
[official-gcc.git] / gcc / expr.c
blob5c0df44248db9032d7371a5e7750b657c60bc89c
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 "rtl.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "stringpool.h"
30 #include "stor-layout.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "flags.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "except.h"
37 #include "function.h"
38 #include "insn-config.h"
39 #include "insn-attr.h"
40 #include "expmed.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "calls.h"
44 #include "emit-rtl.h"
45 #include "stmt.h"
46 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
47 #include "expr.h"
48 #include "insn-codes.h"
49 #include "optabs.h"
50 #include "libfuncs.h"
51 #include "recog.h"
52 #include "reload.h"
53 #include "typeclass.h"
54 #include "toplev.h"
55 #include "langhooks.h"
56 #include "intl.h"
57 #include "tm_p.h"
58 #include "tree-iterator.h"
59 #include "predict.h"
60 #include "dominance.h"
61 #include "cfg.h"
62 #include "basic-block.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-expr.h"
66 #include "gimple.h"
67 #include "gimple-ssa.h"
68 #include "cgraph.h"
69 #include "tree-ssanames.h"
70 #include "target.h"
71 #include "common/common-target.h"
72 #include "timevar.h"
73 #include "df.h"
74 #include "diagnostic.h"
75 #include "tree-ssa-live.h"
76 #include "tree-outof-ssa.h"
77 #include "target-globals.h"
78 #include "params.h"
79 #include "tree-ssa-address.h"
80 #include "cfgexpand.h"
81 #include "builtins.h"
82 #include "tree-chkp.h"
83 #include "rtl-chkp.h"
84 #include "ccmp.h"
87 /* If this is nonzero, we do not bother generating VOLATILE
88 around volatile memory references, and we are willing to
89 output indirect addresses. If cse is to follow, we reject
90 indirect addresses so a useful potential cse is generated;
91 if it is used only once, instruction combination will produce
92 the same indirect address eventually. */
93 int cse_not_expected;
95 /* This structure is used by move_by_pieces to describe the move to
96 be performed. */
97 struct move_by_pieces_d
99 rtx to;
100 rtx to_addr;
101 int autinc_to;
102 int explicit_inc_to;
103 rtx from;
104 rtx from_addr;
105 int autinc_from;
106 int explicit_inc_from;
107 unsigned HOST_WIDE_INT len;
108 HOST_WIDE_INT offset;
109 int reverse;
112 /* This structure is used by store_by_pieces to describe the clear to
113 be performed. */
115 struct store_by_pieces_d
117 rtx to;
118 rtx to_addr;
119 int autinc_to;
120 int explicit_inc_to;
121 unsigned HOST_WIDE_INT len;
122 HOST_WIDE_INT offset;
123 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
124 void *constfundata;
125 int reverse;
128 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
129 struct move_by_pieces_d *);
130 static bool block_move_libcall_safe_for_call_parm (void);
131 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
132 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
133 unsigned HOST_WIDE_INT);
134 static tree emit_block_move_libcall_fn (int);
135 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
136 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
137 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
138 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
139 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
140 struct store_by_pieces_d *);
141 static tree clear_storage_libcall_fn (int);
142 static rtx_insn *compress_float_constant (rtx, rtx);
143 static rtx get_subtarget (rtx);
144 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
145 HOST_WIDE_INT, machine_mode,
146 tree, int, alias_set_type);
147 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
148 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
149 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
150 machine_mode, tree, alias_set_type, bool);
152 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
154 static int is_aligning_offset (const_tree, const_tree);
155 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
156 static rtx do_store_flag (sepops, rtx, machine_mode);
157 #ifdef PUSH_ROUNDING
158 static void emit_single_push_insn (machine_mode, rtx, tree);
159 #endif
160 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
161 static rtx const_vector_from_tree (tree);
162 static tree tree_expr_size (const_tree);
163 static HOST_WIDE_INT int_expr_size (tree);
166 /* This is run to set up which modes can be used
167 directly in memory and to initialize the block move optab. It is run
168 at the beginning of compilation and when the target is reinitialized. */
170 void
171 init_expr_target (void)
173 rtx insn, pat;
174 machine_mode mode;
175 int num_clobbers;
176 rtx mem, mem1;
177 rtx reg;
179 /* Try indexing by frame ptr and try by stack ptr.
180 It is known that on the Convex the stack ptr isn't a valid index.
181 With luck, one or the other is valid on any machine. */
182 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
183 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
185 /* A scratch register we can modify in-place below to avoid
186 useless RTL allocations. */
187 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
189 insn = rtx_alloc (INSN);
190 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
191 PATTERN (insn) = pat;
193 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
194 mode = (machine_mode) ((int) mode + 1))
196 int regno;
198 direct_load[(int) mode] = direct_store[(int) mode] = 0;
199 PUT_MODE (mem, mode);
200 PUT_MODE (mem1, mode);
202 /* See if there is some register that can be used in this mode and
203 directly loaded or stored from memory. */
205 if (mode != VOIDmode && mode != BLKmode)
206 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
207 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
208 regno++)
210 if (! HARD_REGNO_MODE_OK (regno, mode))
211 continue;
213 set_mode_and_regno (reg, mode, regno);
215 SET_SRC (pat) = mem;
216 SET_DEST (pat) = reg;
217 if (recog (pat, insn, &num_clobbers) >= 0)
218 direct_load[(int) mode] = 1;
220 SET_SRC (pat) = mem1;
221 SET_DEST (pat) = reg;
222 if (recog (pat, insn, &num_clobbers) >= 0)
223 direct_load[(int) mode] = 1;
225 SET_SRC (pat) = reg;
226 SET_DEST (pat) = mem;
227 if (recog (pat, insn, &num_clobbers) >= 0)
228 direct_store[(int) mode] = 1;
230 SET_SRC (pat) = reg;
231 SET_DEST (pat) = mem1;
232 if (recog (pat, insn, &num_clobbers) >= 0)
233 direct_store[(int) mode] = 1;
237 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
239 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
240 mode = GET_MODE_WIDER_MODE (mode))
242 machine_mode srcmode;
243 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
244 srcmode = GET_MODE_WIDER_MODE (srcmode))
246 enum insn_code ic;
248 ic = can_extend_p (mode, srcmode, 0);
249 if (ic == CODE_FOR_nothing)
250 continue;
252 PUT_MODE (mem, srcmode);
254 if (insn_operand_matches (ic, 1, mem))
255 float_extend_from_mem[mode][srcmode] = true;
260 /* This is run at the start of compiling a function. */
262 void
263 init_expr (void)
265 memset (&crtl->expr, 0, sizeof (crtl->expr));
268 /* Copy data from FROM to TO, where the machine modes are not the same.
269 Both modes may be integer, or both may be floating, or both may be
270 fixed-point.
271 UNSIGNEDP should be nonzero if FROM is an unsigned type.
272 This causes zero-extension instead of sign-extension. */
274 void
275 convert_move (rtx to, rtx from, int unsignedp)
277 machine_mode to_mode = GET_MODE (to);
278 machine_mode from_mode = GET_MODE (from);
279 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
280 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
281 enum insn_code code;
282 rtx libcall;
284 /* rtx code for making an equivalent value. */
285 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
286 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
289 gcc_assert (to_real == from_real);
290 gcc_assert (to_mode != BLKmode);
291 gcc_assert (from_mode != BLKmode);
293 /* If the source and destination are already the same, then there's
294 nothing to do. */
295 if (to == from)
296 return;
298 /* If FROM is a SUBREG that indicates that we have already done at least
299 the required extension, strip it. We don't handle such SUBREGs as
300 TO here. */
302 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
303 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
304 >= GET_MODE_PRECISION (to_mode))
305 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
306 from = gen_lowpart (to_mode, from), from_mode = to_mode;
308 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
310 if (to_mode == from_mode
311 || (from_mode == VOIDmode && CONSTANT_P (from)))
313 emit_move_insn (to, from);
314 return;
317 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
319 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
321 if (VECTOR_MODE_P (to_mode))
322 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
323 else
324 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
326 emit_move_insn (to, from);
327 return;
330 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
332 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
333 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
334 return;
337 if (to_real)
339 rtx value;
340 rtx_insn *insns;
341 convert_optab tab;
343 gcc_assert ((GET_MODE_PRECISION (from_mode)
344 != GET_MODE_PRECISION (to_mode))
345 || (DECIMAL_FLOAT_MODE_P (from_mode)
346 != DECIMAL_FLOAT_MODE_P (to_mode)));
348 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
349 /* Conversion between decimal float and binary float, same size. */
350 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
351 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
352 tab = sext_optab;
353 else
354 tab = trunc_optab;
356 /* Try converting directly if the insn is supported. */
358 code = convert_optab_handler (tab, to_mode, from_mode);
359 if (code != CODE_FOR_nothing)
361 emit_unop_insn (code, to, from,
362 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
363 return;
366 /* Otherwise use a libcall. */
367 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
369 /* Is this conversion implemented yet? */
370 gcc_assert (libcall);
372 start_sequence ();
373 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
374 1, from, from_mode);
375 insns = get_insns ();
376 end_sequence ();
377 emit_libcall_block (insns, to, value,
378 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
379 from)
380 : gen_rtx_FLOAT_EXTEND (to_mode, from));
381 return;
384 /* Handle pointer conversion. */ /* SPEE 900220. */
385 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
387 convert_optab ctab;
389 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
390 ctab = trunc_optab;
391 else if (unsignedp)
392 ctab = zext_optab;
393 else
394 ctab = sext_optab;
396 if (convert_optab_handler (ctab, to_mode, from_mode)
397 != CODE_FOR_nothing)
399 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
400 to, from, UNKNOWN);
401 return;
405 /* Targets are expected to provide conversion insns between PxImode and
406 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
407 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
409 machine_mode full_mode
410 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
412 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
413 != CODE_FOR_nothing);
415 if (full_mode != from_mode)
416 from = convert_to_mode (full_mode, from, unsignedp);
417 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
418 to, from, UNKNOWN);
419 return;
421 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
423 rtx new_from;
424 machine_mode full_mode
425 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
426 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
427 enum insn_code icode;
429 icode = convert_optab_handler (ctab, full_mode, from_mode);
430 gcc_assert (icode != CODE_FOR_nothing);
432 if (to_mode == full_mode)
434 emit_unop_insn (icode, to, from, UNKNOWN);
435 return;
438 new_from = gen_reg_rtx (full_mode);
439 emit_unop_insn (icode, new_from, from, UNKNOWN);
441 /* else proceed to integer conversions below. */
442 from_mode = full_mode;
443 from = new_from;
446 /* Make sure both are fixed-point modes or both are not. */
447 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
448 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
449 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
451 /* If we widen from_mode to to_mode and they are in the same class,
452 we won't saturate the result.
453 Otherwise, always saturate the result to play safe. */
454 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
455 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
456 expand_fixed_convert (to, from, 0, 0);
457 else
458 expand_fixed_convert (to, from, 0, 1);
459 return;
462 /* Now both modes are integers. */
464 /* Handle expanding beyond a word. */
465 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
466 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
468 rtx_insn *insns;
469 rtx lowpart;
470 rtx fill_value;
471 rtx lowfrom;
472 int i;
473 machine_mode lowpart_mode;
474 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
476 /* Try converting directly if the insn is supported. */
477 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
478 != CODE_FOR_nothing)
480 /* If FROM is a SUBREG, put it into a register. Do this
481 so that we always generate the same set of insns for
482 better cse'ing; if an intermediate assignment occurred,
483 we won't be doing the operation directly on the SUBREG. */
484 if (optimize > 0 && GET_CODE (from) == SUBREG)
485 from = force_reg (from_mode, from);
486 emit_unop_insn (code, to, from, equiv_code);
487 return;
489 /* Next, try converting via full word. */
490 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
491 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
492 != CODE_FOR_nothing))
494 rtx word_to = gen_reg_rtx (word_mode);
495 if (REG_P (to))
497 if (reg_overlap_mentioned_p (to, from))
498 from = force_reg (from_mode, from);
499 emit_clobber (to);
501 convert_move (word_to, from, unsignedp);
502 emit_unop_insn (code, to, word_to, equiv_code);
503 return;
506 /* No special multiword conversion insn; do it by hand. */
507 start_sequence ();
509 /* Since we will turn this into a no conflict block, we must ensure the
510 the source does not overlap the target so force it into an isolated
511 register when maybe so. Likewise for any MEM input, since the
512 conversion sequence might require several references to it and we
513 must ensure we're getting the same value every time. */
515 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
516 from = force_reg (from_mode, from);
518 /* Get a copy of FROM widened to a word, if necessary. */
519 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
520 lowpart_mode = word_mode;
521 else
522 lowpart_mode = from_mode;
524 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
526 lowpart = gen_lowpart (lowpart_mode, to);
527 emit_move_insn (lowpart, lowfrom);
529 /* Compute the value to put in each remaining word. */
530 if (unsignedp)
531 fill_value = const0_rtx;
532 else
533 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
534 LT, lowfrom, const0_rtx,
535 lowpart_mode, 0, -1);
537 /* Fill the remaining words. */
538 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
540 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
541 rtx subword = operand_subword (to, index, 1, to_mode);
543 gcc_assert (subword);
545 if (fill_value != subword)
546 emit_move_insn (subword, fill_value);
549 insns = get_insns ();
550 end_sequence ();
552 emit_insn (insns);
553 return;
556 /* Truncating multi-word to a word or less. */
557 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
558 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
560 if (!((MEM_P (from)
561 && ! MEM_VOLATILE_P (from)
562 && direct_load[(int) to_mode]
563 && ! mode_dependent_address_p (XEXP (from, 0),
564 MEM_ADDR_SPACE (from)))
565 || REG_P (from)
566 || GET_CODE (from) == SUBREG))
567 from = force_reg (from_mode, from);
568 convert_move (to, gen_lowpart (word_mode, from), 0);
569 return;
572 /* Now follow all the conversions between integers
573 no more than a word long. */
575 /* For truncation, usually we can just refer to FROM in a narrower mode. */
576 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
577 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
579 if (!((MEM_P (from)
580 && ! MEM_VOLATILE_P (from)
581 && direct_load[(int) to_mode]
582 && ! mode_dependent_address_p (XEXP (from, 0),
583 MEM_ADDR_SPACE (from)))
584 || REG_P (from)
585 || GET_CODE (from) == SUBREG))
586 from = force_reg (from_mode, from);
587 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
588 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
589 from = copy_to_reg (from);
590 emit_move_insn (to, gen_lowpart (to_mode, from));
591 return;
594 /* Handle extension. */
595 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
597 /* Convert directly if that works. */
598 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
599 != CODE_FOR_nothing)
601 emit_unop_insn (code, to, from, equiv_code);
602 return;
604 else
606 machine_mode intermediate;
607 rtx tmp;
608 int shift_amount;
610 /* Search for a mode to convert via. */
611 for (intermediate = from_mode; intermediate != VOIDmode;
612 intermediate = GET_MODE_WIDER_MODE (intermediate))
613 if (((can_extend_p (to_mode, intermediate, unsignedp)
614 != CODE_FOR_nothing)
615 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
616 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
617 && (can_extend_p (intermediate, from_mode, unsignedp)
618 != CODE_FOR_nothing))
620 convert_move (to, convert_to_mode (intermediate, from,
621 unsignedp), unsignedp);
622 return;
625 /* No suitable intermediate mode.
626 Generate what we need with shifts. */
627 shift_amount = (GET_MODE_PRECISION (to_mode)
628 - GET_MODE_PRECISION (from_mode));
629 from = gen_lowpart (to_mode, force_reg (from_mode, from));
630 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
631 to, unsignedp);
632 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
633 to, unsignedp);
634 if (tmp != to)
635 emit_move_insn (to, tmp);
636 return;
640 /* Support special truncate insns for certain modes. */
641 if (convert_optab_handler (trunc_optab, to_mode,
642 from_mode) != CODE_FOR_nothing)
644 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
645 to, from, UNKNOWN);
646 return;
649 /* Handle truncation of volatile memrefs, and so on;
650 the things that couldn't be truncated directly,
651 and for which there was no special instruction.
653 ??? Code above formerly short-circuited this, for most integer
654 mode pairs, with a force_reg in from_mode followed by a recursive
655 call to this routine. Appears always to have been wrong. */
656 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
658 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
659 emit_move_insn (to, temp);
660 return;
663 /* Mode combination is not recognized. */
664 gcc_unreachable ();
667 /* Return an rtx for a value that would result
668 from converting X to mode MODE.
669 Both X and MODE may be floating, or both integer.
670 UNSIGNEDP is nonzero if X is an unsigned value.
671 This can be done by referring to a part of X in place
672 or by copying to a new temporary with conversion. */
675 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
677 return convert_modes (mode, VOIDmode, x, unsignedp);
680 /* Return an rtx for a value that would result
681 from converting X from mode OLDMODE to mode MODE.
682 Both modes may be floating, or both integer.
683 UNSIGNEDP is nonzero if X is an unsigned value.
685 This can be done by referring to a part of X in place
686 or by copying to a new temporary with conversion.
688 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
691 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
693 rtx temp;
695 /* If FROM is a SUBREG that indicates that we have already done at least
696 the required extension, strip it. */
698 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
699 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
700 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
701 x = gen_lowpart (mode, SUBREG_REG (x));
703 if (GET_MODE (x) != VOIDmode)
704 oldmode = GET_MODE (x);
706 if (mode == oldmode)
707 return x;
709 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
711 /* If the caller did not tell us the old mode, then there is not
712 much to do with respect to canonicalization. We have to
713 assume that all the bits are significant. */
714 if (GET_MODE_CLASS (oldmode) != MODE_INT)
715 oldmode = MAX_MODE_INT;
716 wide_int w = wide_int::from (std::make_pair (x, oldmode),
717 GET_MODE_PRECISION (mode),
718 unsignedp ? UNSIGNED : SIGNED);
719 return immed_wide_int_const (w, mode);
722 /* We can do this with a gen_lowpart if both desired and current modes
723 are integer, and this is either a constant integer, a register, or a
724 non-volatile MEM. */
725 if (GET_MODE_CLASS (mode) == MODE_INT
726 && GET_MODE_CLASS (oldmode) == MODE_INT
727 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
728 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
729 || (REG_P (x)
730 && (!HARD_REGISTER_P (x)
731 || HARD_REGNO_MODE_OK (REGNO (x), mode))
732 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
734 return gen_lowpart (mode, x);
736 /* Converting from integer constant into mode is always equivalent to an
737 subreg operation. */
738 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
740 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
741 return simplify_gen_subreg (mode, x, oldmode, 0);
744 temp = gen_reg_rtx (mode);
745 convert_move (temp, x, unsignedp);
746 return temp;
749 /* Return the largest alignment we can use for doing a move (or store)
750 of MAX_PIECES. ALIGN is the largest alignment we could use. */
752 static unsigned int
753 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
755 machine_mode tmode;
757 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
758 if (align >= GET_MODE_ALIGNMENT (tmode))
759 align = GET_MODE_ALIGNMENT (tmode);
760 else
762 machine_mode tmode, xmode;
764 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
765 tmode != VOIDmode;
766 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
767 if (GET_MODE_SIZE (tmode) > max_pieces
768 || SLOW_UNALIGNED_ACCESS (tmode, align))
769 break;
771 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
774 return align;
777 /* Return the widest integer mode no wider than SIZE. If no such mode
778 can be found, return VOIDmode. */
780 static machine_mode
781 widest_int_mode_for_size (unsigned int size)
783 machine_mode tmode, mode = VOIDmode;
785 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
786 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
787 if (GET_MODE_SIZE (tmode) < size)
788 mode = tmode;
790 return mode;
793 /* Determine whether the LEN bytes can be moved by using several move
794 instructions. Return nonzero if a call to move_by_pieces should
795 succeed. */
798 can_move_by_pieces (unsigned HOST_WIDE_INT len,
799 unsigned int align)
801 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
802 optimize_insn_for_speed_p ());
805 /* Generate several move instructions to copy LEN bytes from block FROM to
806 block TO. (These are MEM rtx's with BLKmode).
808 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
809 used to push FROM to the stack.
811 ALIGN is maximum stack alignment we can assume.
813 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
814 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
815 stpcpy. */
818 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
819 unsigned int align, int endp)
821 struct move_by_pieces_d data;
822 machine_mode to_addr_mode;
823 machine_mode from_addr_mode = get_address_mode (from);
824 rtx to_addr, from_addr = XEXP (from, 0);
825 unsigned int max_size = MOVE_MAX_PIECES + 1;
826 enum insn_code icode;
828 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
830 data.offset = 0;
831 data.from_addr = from_addr;
832 if (to)
834 to_addr_mode = get_address_mode (to);
835 to_addr = XEXP (to, 0);
836 data.to = to;
837 data.autinc_to
838 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
839 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
840 data.reverse
841 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
843 else
845 to_addr_mode = VOIDmode;
846 to_addr = NULL_RTX;
847 data.to = NULL_RTX;
848 data.autinc_to = 1;
849 if (STACK_GROWS_DOWNWARD)
850 data.reverse = 1;
851 else
852 data.reverse = 0;
854 data.to_addr = to_addr;
855 data.from = from;
856 data.autinc_from
857 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
858 || GET_CODE (from_addr) == POST_INC
859 || GET_CODE (from_addr) == POST_DEC);
861 data.explicit_inc_from = 0;
862 data.explicit_inc_to = 0;
863 if (data.reverse) data.offset = len;
864 data.len = len;
866 /* If copying requires more than two move insns,
867 copy addresses to registers (to make displacements shorter)
868 and use post-increment if available. */
869 if (!(data.autinc_from && data.autinc_to)
870 && move_by_pieces_ninsns (len, align, max_size) > 2)
872 /* Find the mode of the largest move...
873 MODE might not be used depending on the definitions of the
874 USE_* macros below. */
875 machine_mode mode ATTRIBUTE_UNUSED
876 = widest_int_mode_for_size (max_size);
878 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
880 data.from_addr = copy_to_mode_reg (from_addr_mode,
881 plus_constant (from_addr_mode,
882 from_addr, len));
883 data.autinc_from = 1;
884 data.explicit_inc_from = -1;
886 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
888 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
889 data.autinc_from = 1;
890 data.explicit_inc_from = 1;
892 if (!data.autinc_from && CONSTANT_P (from_addr))
893 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
894 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
896 data.to_addr = copy_to_mode_reg (to_addr_mode,
897 plus_constant (to_addr_mode,
898 to_addr, len));
899 data.autinc_to = 1;
900 data.explicit_inc_to = -1;
902 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
904 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
905 data.autinc_to = 1;
906 data.explicit_inc_to = 1;
908 if (!data.autinc_to && CONSTANT_P (to_addr))
909 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
912 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
914 /* First move what we can in the largest integer mode, then go to
915 successively smaller modes. */
917 while (max_size > 1 && data.len > 0)
919 machine_mode mode = widest_int_mode_for_size (max_size);
921 if (mode == VOIDmode)
922 break;
924 icode = optab_handler (mov_optab, mode);
925 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
926 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
928 max_size = GET_MODE_SIZE (mode);
931 /* The code above should have handled everything. */
932 gcc_assert (!data.len);
934 if (endp)
936 rtx to1;
938 gcc_assert (!data.reverse);
939 if (data.autinc_to)
941 if (endp == 2)
943 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
944 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
945 else
946 data.to_addr = copy_to_mode_reg (to_addr_mode,
947 plus_constant (to_addr_mode,
948 data.to_addr,
949 -1));
951 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
952 data.offset);
954 else
956 if (endp == 2)
957 --data.offset;
958 to1 = adjust_address (data.to, QImode, data.offset);
960 return to1;
962 else
963 return data.to;
966 /* Return number of insns required to move L bytes by pieces.
967 ALIGN (in bits) is maximum alignment we can assume. */
969 unsigned HOST_WIDE_INT
970 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
971 unsigned int max_size)
973 unsigned HOST_WIDE_INT n_insns = 0;
975 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
977 while (max_size > 1 && l > 0)
979 machine_mode mode;
980 enum insn_code icode;
982 mode = widest_int_mode_for_size (max_size);
984 if (mode == VOIDmode)
985 break;
987 icode = optab_handler (mov_optab, mode);
988 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
989 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
991 max_size = GET_MODE_SIZE (mode);
994 gcc_assert (!l);
995 return n_insns;
998 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
999 with move instructions for mode MODE. GENFUN is the gen_... function
1000 to make a move insn for that mode. DATA has all the other info. */
1002 static void
1003 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1004 struct move_by_pieces_d *data)
1006 unsigned int size = GET_MODE_SIZE (mode);
1007 rtx to1 = NULL_RTX, from1;
1009 while (data->len >= size)
1011 if (data->reverse)
1012 data->offset -= size;
1014 if (data->to)
1016 if (data->autinc_to)
1017 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1018 data->offset);
1019 else
1020 to1 = adjust_address (data->to, mode, data->offset);
1023 if (data->autinc_from)
1024 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1025 data->offset);
1026 else
1027 from1 = adjust_address (data->from, mode, data->offset);
1029 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1030 emit_insn (gen_add2_insn (data->to_addr,
1031 gen_int_mode (-(HOST_WIDE_INT) size,
1032 GET_MODE (data->to_addr))));
1033 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1034 emit_insn (gen_add2_insn (data->from_addr,
1035 gen_int_mode (-(HOST_WIDE_INT) size,
1036 GET_MODE (data->from_addr))));
1038 if (data->to)
1039 emit_insn ((*genfun) (to1, from1));
1040 else
1042 #ifdef PUSH_ROUNDING
1043 emit_single_push_insn (mode, from1, NULL);
1044 #else
1045 gcc_unreachable ();
1046 #endif
1049 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1050 emit_insn (gen_add2_insn (data->to_addr,
1051 gen_int_mode (size,
1052 GET_MODE (data->to_addr))));
1053 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1054 emit_insn (gen_add2_insn (data->from_addr,
1055 gen_int_mode (size,
1056 GET_MODE (data->from_addr))));
1058 if (! data->reverse)
1059 data->offset += size;
1061 data->len -= size;
1065 /* Emit code to move a block Y to a block X. This may be done with
1066 string-move instructions, with multiple scalar move instructions,
1067 or with a library call.
1069 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1070 SIZE is an rtx that says how long they are.
1071 ALIGN is the maximum alignment we can assume they have.
1072 METHOD describes what kind of copy this is, and what mechanisms may be used.
1073 MIN_SIZE is the minimal size of block to move
1074 MAX_SIZE is the maximal size of block to move, if it can not be represented
1075 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1077 Return the address of the new block, if memcpy is called and returns it,
1078 0 otherwise. */
1081 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1082 unsigned int expected_align, HOST_WIDE_INT expected_size,
1083 unsigned HOST_WIDE_INT min_size,
1084 unsigned HOST_WIDE_INT max_size,
1085 unsigned HOST_WIDE_INT probable_max_size)
1087 bool may_use_call;
1088 rtx retval = 0;
1089 unsigned int align;
1091 gcc_assert (size);
1092 if (CONST_INT_P (size)
1093 && INTVAL (size) == 0)
1094 return 0;
1096 switch (method)
1098 case BLOCK_OP_NORMAL:
1099 case BLOCK_OP_TAILCALL:
1100 may_use_call = true;
1101 break;
1103 case BLOCK_OP_CALL_PARM:
1104 may_use_call = block_move_libcall_safe_for_call_parm ();
1106 /* Make inhibit_defer_pop nonzero around the library call
1107 to force it to pop the arguments right away. */
1108 NO_DEFER_POP;
1109 break;
1111 case BLOCK_OP_NO_LIBCALL:
1112 may_use_call = false;
1113 break;
1115 default:
1116 gcc_unreachable ();
1119 gcc_assert (MEM_P (x) && MEM_P (y));
1120 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1121 gcc_assert (align >= BITS_PER_UNIT);
1123 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1124 block copy is more efficient for other large modes, e.g. DCmode. */
1125 x = adjust_address (x, BLKmode, 0);
1126 y = adjust_address (y, BLKmode, 0);
1128 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1129 can be incorrect is coming from __builtin_memcpy. */
1130 if (CONST_INT_P (size))
1132 x = shallow_copy_rtx (x);
1133 y = shallow_copy_rtx (y);
1134 set_mem_size (x, INTVAL (size));
1135 set_mem_size (y, INTVAL (size));
1138 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1139 move_by_pieces (x, y, INTVAL (size), align, 0);
1140 else if (emit_block_move_via_movmem (x, y, size, align,
1141 expected_align, expected_size,
1142 min_size, max_size, probable_max_size))
1144 else if (may_use_call
1145 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1146 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1148 /* Since x and y are passed to a libcall, mark the corresponding
1149 tree EXPR as addressable. */
1150 tree y_expr = MEM_EXPR (y);
1151 tree x_expr = MEM_EXPR (x);
1152 if (y_expr)
1153 mark_addressable (y_expr);
1154 if (x_expr)
1155 mark_addressable (x_expr);
1156 retval = emit_block_move_via_libcall (x, y, size,
1157 method == BLOCK_OP_TAILCALL);
1160 else
1161 emit_block_move_via_loop (x, y, size, align);
1163 if (method == BLOCK_OP_CALL_PARM)
1164 OK_DEFER_POP;
1166 return retval;
1170 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1172 unsigned HOST_WIDE_INT max, min = 0;
1173 if (GET_CODE (size) == CONST_INT)
1174 min = max = UINTVAL (size);
1175 else
1176 max = GET_MODE_MASK (GET_MODE (size));
1177 return emit_block_move_hints (x, y, size, method, 0, -1,
1178 min, max, max);
1181 /* A subroutine of emit_block_move. Returns true if calling the
1182 block move libcall will not clobber any parameters which may have
1183 already been placed on the stack. */
1185 static bool
1186 block_move_libcall_safe_for_call_parm (void)
1188 #if defined (REG_PARM_STACK_SPACE)
1189 tree fn;
1190 #endif
1192 /* If arguments are pushed on the stack, then they're safe. */
1193 if (PUSH_ARGS)
1194 return true;
1196 /* If registers go on the stack anyway, any argument is sure to clobber
1197 an outgoing argument. */
1198 #if defined (REG_PARM_STACK_SPACE)
1199 fn = emit_block_move_libcall_fn (false);
1200 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1201 depend on its argument. */
1202 (void) fn;
1203 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1204 && REG_PARM_STACK_SPACE (fn) != 0)
1205 return false;
1206 #endif
1208 /* If any argument goes in memory, then it might clobber an outgoing
1209 argument. */
1211 CUMULATIVE_ARGS args_so_far_v;
1212 cumulative_args_t args_so_far;
1213 tree fn, arg;
1215 fn = emit_block_move_libcall_fn (false);
1216 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1217 args_so_far = pack_cumulative_args (&args_so_far_v);
1219 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1220 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1222 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1223 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1224 NULL_TREE, true);
1225 if (!tmp || !REG_P (tmp))
1226 return false;
1227 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1228 return false;
1229 targetm.calls.function_arg_advance (args_so_far, mode,
1230 NULL_TREE, true);
1233 return true;
1236 /* A subroutine of emit_block_move. Expand a movmem pattern;
1237 return true if successful. */
1239 static bool
1240 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1241 unsigned int expected_align, HOST_WIDE_INT expected_size,
1242 unsigned HOST_WIDE_INT min_size,
1243 unsigned HOST_WIDE_INT max_size,
1244 unsigned HOST_WIDE_INT probable_max_size)
1246 int save_volatile_ok = volatile_ok;
1247 machine_mode mode;
1249 if (expected_align < align)
1250 expected_align = align;
1251 if (expected_size != -1)
1253 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1254 expected_size = probable_max_size;
1255 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1256 expected_size = min_size;
1259 /* Since this is a move insn, we don't care about volatility. */
1260 volatile_ok = 1;
1262 /* Try the most limited insn first, because there's no point
1263 including more than one in the machine description unless
1264 the more limited one has some advantage. */
1266 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1267 mode = GET_MODE_WIDER_MODE (mode))
1269 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1271 if (code != CODE_FOR_nothing
1272 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1273 here because if SIZE is less than the mode mask, as it is
1274 returned by the macro, it will definitely be less than the
1275 actual mode mask. Since SIZE is within the Pmode address
1276 space, we limit MODE to Pmode. */
1277 && ((CONST_INT_P (size)
1278 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1279 <= (GET_MODE_MASK (mode) >> 1)))
1280 || max_size <= (GET_MODE_MASK (mode) >> 1)
1281 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1283 struct expand_operand ops[9];
1284 unsigned int nops;
1286 /* ??? When called via emit_block_move_for_call, it'd be
1287 nice if there were some way to inform the backend, so
1288 that it doesn't fail the expansion because it thinks
1289 emitting the libcall would be more efficient. */
1290 nops = insn_data[(int) code].n_generator_args;
1291 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1293 create_fixed_operand (&ops[0], x);
1294 create_fixed_operand (&ops[1], y);
1295 /* The check above guarantees that this size conversion is valid. */
1296 create_convert_operand_to (&ops[2], size, mode, true);
1297 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1298 if (nops >= 6)
1300 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1301 create_integer_operand (&ops[5], expected_size);
1303 if (nops >= 8)
1305 create_integer_operand (&ops[6], min_size);
1306 /* If we can not represent the maximal size,
1307 make parameter NULL. */
1308 if ((HOST_WIDE_INT) max_size != -1)
1309 create_integer_operand (&ops[7], max_size);
1310 else
1311 create_fixed_operand (&ops[7], NULL);
1313 if (nops == 9)
1315 /* If we can not represent the maximal size,
1316 make parameter NULL. */
1317 if ((HOST_WIDE_INT) probable_max_size != -1)
1318 create_integer_operand (&ops[8], probable_max_size);
1319 else
1320 create_fixed_operand (&ops[8], NULL);
1322 if (maybe_expand_insn (code, nops, ops))
1324 volatile_ok = save_volatile_ok;
1325 return true;
1330 volatile_ok = save_volatile_ok;
1331 return false;
1334 /* A subroutine of emit_block_move. Expand a call to memcpy.
1335 Return the return value from memcpy, 0 otherwise. */
1338 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1340 rtx dst_addr, src_addr;
1341 tree call_expr, fn, src_tree, dst_tree, size_tree;
1342 machine_mode size_mode;
1343 rtx retval;
1345 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1346 pseudos. We can then place those new pseudos into a VAR_DECL and
1347 use them later. */
1349 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1350 src_addr = copy_addr_to_reg (XEXP (src, 0));
1352 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1353 src_addr = convert_memory_address (ptr_mode, src_addr);
1355 dst_tree = make_tree (ptr_type_node, dst_addr);
1356 src_tree = make_tree (ptr_type_node, src_addr);
1358 size_mode = TYPE_MODE (sizetype);
1360 size = convert_to_mode (size_mode, size, 1);
1361 size = copy_to_mode_reg (size_mode, size);
1363 /* It is incorrect to use the libcall calling conventions to call
1364 memcpy in this context. This could be a user call to memcpy and
1365 the user may wish to examine the return value from memcpy. For
1366 targets where libcalls and normal calls have different conventions
1367 for returning pointers, we could end up generating incorrect code. */
1369 size_tree = make_tree (sizetype, size);
1371 fn = emit_block_move_libcall_fn (true);
1372 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1373 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1375 retval = expand_normal (call_expr);
1377 return retval;
1380 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1381 for the function we use for block copies. */
1383 static GTY(()) tree block_move_fn;
1385 void
1386 init_block_move_fn (const char *asmspec)
1388 if (!block_move_fn)
1390 tree args, fn, attrs, attr_args;
1392 fn = get_identifier ("memcpy");
1393 args = build_function_type_list (ptr_type_node, ptr_type_node,
1394 const_ptr_type_node, sizetype,
1395 NULL_TREE);
1397 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1398 DECL_EXTERNAL (fn) = 1;
1399 TREE_PUBLIC (fn) = 1;
1400 DECL_ARTIFICIAL (fn) = 1;
1401 TREE_NOTHROW (fn) = 1;
1402 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1403 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1405 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1406 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1408 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1410 block_move_fn = fn;
1413 if (asmspec)
1414 set_user_assembler_name (block_move_fn, asmspec);
1417 static tree
1418 emit_block_move_libcall_fn (int for_call)
1420 static bool emitted_extern;
1422 if (!block_move_fn)
1423 init_block_move_fn (NULL);
1425 if (for_call && !emitted_extern)
1427 emitted_extern = true;
1428 make_decl_rtl (block_move_fn);
1431 return block_move_fn;
1434 /* A subroutine of emit_block_move. Copy the data via an explicit
1435 loop. This is used only when libcalls are forbidden. */
1436 /* ??? It'd be nice to copy in hunks larger than QImode. */
1438 static void
1439 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1440 unsigned int align ATTRIBUTE_UNUSED)
1442 rtx_code_label *cmp_label, *top_label;
1443 rtx iter, x_addr, y_addr, tmp;
1444 machine_mode x_addr_mode = get_address_mode (x);
1445 machine_mode y_addr_mode = get_address_mode (y);
1446 machine_mode iter_mode;
1448 iter_mode = GET_MODE (size);
1449 if (iter_mode == VOIDmode)
1450 iter_mode = word_mode;
1452 top_label = gen_label_rtx ();
1453 cmp_label = gen_label_rtx ();
1454 iter = gen_reg_rtx (iter_mode);
1456 emit_move_insn (iter, const0_rtx);
1458 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1459 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1460 do_pending_stack_adjust ();
1462 emit_jump (cmp_label);
1463 emit_label (top_label);
1465 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1466 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1468 if (x_addr_mode != y_addr_mode)
1469 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1470 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1472 x = change_address (x, QImode, x_addr);
1473 y = change_address (y, QImode, y_addr);
1475 emit_move_insn (x, y);
1477 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1478 true, OPTAB_LIB_WIDEN);
1479 if (tmp != iter)
1480 emit_move_insn (iter, tmp);
1482 emit_label (cmp_label);
1484 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1485 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1488 /* Copy all or part of a value X into registers starting at REGNO.
1489 The number of registers to be filled is NREGS. */
1491 void
1492 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1494 if (nregs == 0)
1495 return;
1497 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1498 x = validize_mem (force_const_mem (mode, x));
1500 /* See if the machine can do this with a load multiple insn. */
1501 if (targetm.have_load_multiple ())
1503 rtx_insn *last = get_last_insn ();
1504 rtx first = gen_rtx_REG (word_mode, regno);
1505 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1506 GEN_INT (nregs)))
1508 emit_insn (pat);
1509 return;
1511 else
1512 delete_insns_since (last);
1515 for (int i = 0; i < nregs; i++)
1516 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1517 operand_subword_force (x, i, mode));
1520 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1521 The number of registers to be filled is NREGS. */
1523 void
1524 move_block_from_reg (int regno, rtx x, int nregs)
1526 if (nregs == 0)
1527 return;
1529 /* See if the machine can do this with a store multiple insn. */
1530 if (targetm.have_store_multiple ())
1532 rtx_insn *last = get_last_insn ();
1533 rtx first = gen_rtx_REG (word_mode, regno);
1534 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
1535 GEN_INT (nregs)))
1537 emit_insn (pat);
1538 return;
1540 else
1541 delete_insns_since (last);
1544 for (int i = 0; i < nregs; i++)
1546 rtx tem = operand_subword (x, i, 1, BLKmode);
1548 gcc_assert (tem);
1550 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1554 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1555 ORIG, where ORIG is a non-consecutive group of registers represented by
1556 a PARALLEL. The clone is identical to the original except in that the
1557 original set of registers is replaced by a new set of pseudo registers.
1558 The new set has the same modes as the original set. */
1561 gen_group_rtx (rtx orig)
1563 int i, length;
1564 rtx *tmps;
1566 gcc_assert (GET_CODE (orig) == PARALLEL);
1568 length = XVECLEN (orig, 0);
1569 tmps = XALLOCAVEC (rtx, length);
1571 /* Skip a NULL entry in first slot. */
1572 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1574 if (i)
1575 tmps[0] = 0;
1577 for (; i < length; i++)
1579 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1580 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1582 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1585 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1588 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1589 except that values are placed in TMPS[i], and must later be moved
1590 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1592 static void
1593 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1595 rtx src;
1596 int start, i;
1597 machine_mode m = GET_MODE (orig_src);
1599 gcc_assert (GET_CODE (dst) == PARALLEL);
1601 if (m != VOIDmode
1602 && !SCALAR_INT_MODE_P (m)
1603 && !MEM_P (orig_src)
1604 && GET_CODE (orig_src) != CONCAT)
1606 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1607 if (imode == BLKmode)
1608 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1609 else
1610 src = gen_reg_rtx (imode);
1611 if (imode != BLKmode)
1612 src = gen_lowpart (GET_MODE (orig_src), src);
1613 emit_move_insn (src, orig_src);
1614 /* ...and back again. */
1615 if (imode != BLKmode)
1616 src = gen_lowpart (imode, src);
1617 emit_group_load_1 (tmps, dst, src, type, ssize);
1618 return;
1621 /* Check for a NULL entry, used to indicate that the parameter goes
1622 both on the stack and in registers. */
1623 if (XEXP (XVECEXP (dst, 0, 0), 0))
1624 start = 0;
1625 else
1626 start = 1;
1628 /* Process the pieces. */
1629 for (i = start; i < XVECLEN (dst, 0); i++)
1631 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1632 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1633 unsigned int bytelen = GET_MODE_SIZE (mode);
1634 int shift = 0;
1636 /* Handle trailing fragments that run over the size of the struct. */
1637 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1639 /* Arrange to shift the fragment to where it belongs.
1640 extract_bit_field loads to the lsb of the reg. */
1641 if (
1642 #ifdef BLOCK_REG_PADDING
1643 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1644 == (BYTES_BIG_ENDIAN ? upward : downward)
1645 #else
1646 BYTES_BIG_ENDIAN
1647 #endif
1649 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1650 bytelen = ssize - bytepos;
1651 gcc_assert (bytelen > 0);
1654 /* If we won't be loading directly from memory, protect the real source
1655 from strange tricks we might play; but make sure that the source can
1656 be loaded directly into the destination. */
1657 src = orig_src;
1658 if (!MEM_P (orig_src)
1659 && (!CONSTANT_P (orig_src)
1660 || (GET_MODE (orig_src) != mode
1661 && GET_MODE (orig_src) != VOIDmode)))
1663 if (GET_MODE (orig_src) == VOIDmode)
1664 src = gen_reg_rtx (mode);
1665 else
1666 src = gen_reg_rtx (GET_MODE (orig_src));
1668 emit_move_insn (src, orig_src);
1671 /* Optimize the access just a bit. */
1672 if (MEM_P (src)
1673 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1674 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1675 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1676 && bytelen == GET_MODE_SIZE (mode))
1678 tmps[i] = gen_reg_rtx (mode);
1679 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1681 else if (COMPLEX_MODE_P (mode)
1682 && GET_MODE (src) == mode
1683 && bytelen == GET_MODE_SIZE (mode))
1684 /* Let emit_move_complex do the bulk of the work. */
1685 tmps[i] = src;
1686 else if (GET_CODE (src) == CONCAT)
1688 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1689 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1691 if ((bytepos == 0 && bytelen == slen0)
1692 || (bytepos != 0 && bytepos + bytelen <= slen))
1694 /* The following assumes that the concatenated objects all
1695 have the same size. In this case, a simple calculation
1696 can be used to determine the object and the bit field
1697 to be extracted. */
1698 tmps[i] = XEXP (src, bytepos / slen0);
1699 if (! CONSTANT_P (tmps[i])
1700 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1701 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1702 (bytepos % slen0) * BITS_PER_UNIT,
1703 1, NULL_RTX, mode, mode);
1705 else
1707 rtx mem;
1709 gcc_assert (!bytepos);
1710 mem = assign_stack_temp (GET_MODE (src), slen);
1711 emit_move_insn (mem, src);
1712 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1713 0, 1, NULL_RTX, mode, mode);
1716 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1717 SIMD register, which is currently broken. While we get GCC
1718 to emit proper RTL for these cases, let's dump to memory. */
1719 else if (VECTOR_MODE_P (GET_MODE (dst))
1720 && REG_P (src))
1722 int slen = GET_MODE_SIZE (GET_MODE (src));
1723 rtx mem;
1725 mem = assign_stack_temp (GET_MODE (src), slen);
1726 emit_move_insn (mem, src);
1727 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1729 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1730 && XVECLEN (dst, 0) > 1)
1731 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1732 else if (CONSTANT_P (src))
1734 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1736 if (len == ssize)
1737 tmps[i] = src;
1738 else
1740 rtx first, second;
1742 /* TODO: const_wide_int can have sizes other than this... */
1743 gcc_assert (2 * len == ssize);
1744 split_double (src, &first, &second);
1745 if (i)
1746 tmps[i] = second;
1747 else
1748 tmps[i] = first;
1751 else if (REG_P (src) && GET_MODE (src) == mode)
1752 tmps[i] = src;
1753 else
1754 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1755 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1756 mode, mode);
1758 if (shift)
1759 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1760 shift, tmps[i], 0);
1764 /* Emit code to move a block SRC of type TYPE to a block DST,
1765 where DST is non-consecutive registers represented by a PARALLEL.
1766 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1767 if not known. */
1769 void
1770 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1772 rtx *tmps;
1773 int i;
1775 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1776 emit_group_load_1 (tmps, dst, src, type, ssize);
1778 /* Copy the extracted pieces into the proper (probable) hard regs. */
1779 for (i = 0; i < XVECLEN (dst, 0); i++)
1781 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1782 if (d == NULL)
1783 continue;
1784 emit_move_insn (d, tmps[i]);
1788 /* Similar, but load SRC into new pseudos in a format that looks like
1789 PARALLEL. This can later be fed to emit_group_move to get things
1790 in the right place. */
1793 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1795 rtvec vec;
1796 int i;
1798 vec = rtvec_alloc (XVECLEN (parallel, 0));
1799 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1801 /* Convert the vector to look just like the original PARALLEL, except
1802 with the computed values. */
1803 for (i = 0; i < XVECLEN (parallel, 0); i++)
1805 rtx e = XVECEXP (parallel, 0, i);
1806 rtx d = XEXP (e, 0);
1808 if (d)
1810 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1811 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1813 RTVEC_ELT (vec, i) = e;
1816 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1819 /* Emit code to move a block SRC to block DST, where SRC and DST are
1820 non-consecutive groups of registers, each represented by a PARALLEL. */
1822 void
1823 emit_group_move (rtx dst, rtx src)
1825 int i;
1827 gcc_assert (GET_CODE (src) == PARALLEL
1828 && GET_CODE (dst) == PARALLEL
1829 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1831 /* Skip first entry if NULL. */
1832 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1833 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1834 XEXP (XVECEXP (src, 0, i), 0));
1837 /* Move a group of registers represented by a PARALLEL into pseudos. */
1840 emit_group_move_into_temps (rtx src)
1842 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1843 int i;
1845 for (i = 0; i < XVECLEN (src, 0); i++)
1847 rtx e = XVECEXP (src, 0, i);
1848 rtx d = XEXP (e, 0);
1850 if (d)
1851 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1852 RTVEC_ELT (vec, i) = e;
1855 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1858 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1859 where SRC is non-consecutive registers represented by a PARALLEL.
1860 SSIZE represents the total size of block ORIG_DST, or -1 if not
1861 known. */
1863 void
1864 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1866 rtx *tmps, dst;
1867 int start, finish, i;
1868 machine_mode m = GET_MODE (orig_dst);
1870 gcc_assert (GET_CODE (src) == PARALLEL);
1872 if (!SCALAR_INT_MODE_P (m)
1873 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1875 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1876 if (imode == BLKmode)
1877 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1878 else
1879 dst = gen_reg_rtx (imode);
1880 emit_group_store (dst, src, type, ssize);
1881 if (imode != BLKmode)
1882 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1883 emit_move_insn (orig_dst, dst);
1884 return;
1887 /* Check for a NULL entry, used to indicate that the parameter goes
1888 both on the stack and in registers. */
1889 if (XEXP (XVECEXP (src, 0, 0), 0))
1890 start = 0;
1891 else
1892 start = 1;
1893 finish = XVECLEN (src, 0);
1895 tmps = XALLOCAVEC (rtx, finish);
1897 /* Copy the (probable) hard regs into pseudos. */
1898 for (i = start; i < finish; i++)
1900 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1901 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1903 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1904 emit_move_insn (tmps[i], reg);
1906 else
1907 tmps[i] = reg;
1910 /* If we won't be storing directly into memory, protect the real destination
1911 from strange tricks we might play. */
1912 dst = orig_dst;
1913 if (GET_CODE (dst) == PARALLEL)
1915 rtx temp;
1917 /* We can get a PARALLEL dst if there is a conditional expression in
1918 a return statement. In that case, the dst and src are the same,
1919 so no action is necessary. */
1920 if (rtx_equal_p (dst, src))
1921 return;
1923 /* It is unclear if we can ever reach here, but we may as well handle
1924 it. Allocate a temporary, and split this into a store/load to/from
1925 the temporary. */
1926 temp = assign_stack_temp (GET_MODE (dst), ssize);
1927 emit_group_store (temp, src, type, ssize);
1928 emit_group_load (dst, temp, type, ssize);
1929 return;
1931 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1933 machine_mode outer = GET_MODE (dst);
1934 machine_mode inner;
1935 HOST_WIDE_INT bytepos;
1936 bool done = false;
1937 rtx temp;
1939 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1940 dst = gen_reg_rtx (outer);
1942 /* Make life a bit easier for combine. */
1943 /* If the first element of the vector is the low part
1944 of the destination mode, use a paradoxical subreg to
1945 initialize the destination. */
1946 if (start < finish)
1948 inner = GET_MODE (tmps[start]);
1949 bytepos = subreg_lowpart_offset (inner, outer);
1950 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1952 temp = simplify_gen_subreg (outer, tmps[start],
1953 inner, 0);
1954 if (temp)
1956 emit_move_insn (dst, temp);
1957 done = true;
1958 start++;
1963 /* If the first element wasn't the low part, try the last. */
1964 if (!done
1965 && start < finish - 1)
1967 inner = GET_MODE (tmps[finish - 1]);
1968 bytepos = subreg_lowpart_offset (inner, outer);
1969 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1971 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1972 inner, 0);
1973 if (temp)
1975 emit_move_insn (dst, temp);
1976 done = true;
1977 finish--;
1982 /* Otherwise, simply initialize the result to zero. */
1983 if (!done)
1984 emit_move_insn (dst, CONST0_RTX (outer));
1987 /* Process the pieces. */
1988 for (i = start; i < finish; i++)
1990 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
1991 machine_mode mode = GET_MODE (tmps[i]);
1992 unsigned int bytelen = GET_MODE_SIZE (mode);
1993 unsigned int adj_bytelen;
1994 rtx dest = dst;
1996 /* Handle trailing fragments that run over the size of the struct. */
1997 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1998 adj_bytelen = ssize - bytepos;
1999 else
2000 adj_bytelen = bytelen;
2002 if (GET_CODE (dst) == CONCAT)
2004 if (bytepos + adj_bytelen
2005 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2006 dest = XEXP (dst, 0);
2007 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2009 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2010 dest = XEXP (dst, 1);
2012 else
2014 machine_mode dest_mode = GET_MODE (dest);
2015 machine_mode tmp_mode = GET_MODE (tmps[i]);
2017 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2019 if (GET_MODE_ALIGNMENT (dest_mode)
2020 >= GET_MODE_ALIGNMENT (tmp_mode))
2022 dest = assign_stack_temp (dest_mode,
2023 GET_MODE_SIZE (dest_mode));
2024 emit_move_insn (adjust_address (dest,
2025 tmp_mode,
2026 bytepos),
2027 tmps[i]);
2028 dst = dest;
2030 else
2032 dest = assign_stack_temp (tmp_mode,
2033 GET_MODE_SIZE (tmp_mode));
2034 emit_move_insn (dest, tmps[i]);
2035 dst = adjust_address (dest, dest_mode, bytepos);
2037 break;
2041 /* Handle trailing fragments that run over the size of the struct. */
2042 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2044 /* store_bit_field always takes its value from the lsb.
2045 Move the fragment to the lsb if it's not already there. */
2046 if (
2047 #ifdef BLOCK_REG_PADDING
2048 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2049 == (BYTES_BIG_ENDIAN ? upward : downward)
2050 #else
2051 BYTES_BIG_ENDIAN
2052 #endif
2055 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2056 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2057 shift, tmps[i], 0);
2060 /* Make sure not to write past the end of the struct. */
2061 store_bit_field (dest,
2062 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2063 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2064 VOIDmode, tmps[i]);
2067 /* Optimize the access just a bit. */
2068 else if (MEM_P (dest)
2069 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2070 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2071 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2072 && bytelen == GET_MODE_SIZE (mode))
2073 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2075 else
2076 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2077 0, 0, mode, tmps[i]);
2080 /* Copy from the pseudo into the (probable) hard reg. */
2081 if (orig_dst != dst)
2082 emit_move_insn (orig_dst, dst);
2085 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2086 of the value stored in X. */
2089 maybe_emit_group_store (rtx x, tree type)
2091 machine_mode mode = TYPE_MODE (type);
2092 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2093 if (GET_CODE (x) == PARALLEL)
2095 rtx result = gen_reg_rtx (mode);
2096 emit_group_store (result, x, type, int_size_in_bytes (type));
2097 return result;
2099 return x;
2102 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2104 This is used on targets that return BLKmode values in registers. */
2106 void
2107 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2109 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2110 rtx src = NULL, dst = NULL;
2111 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2112 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2113 machine_mode mode = GET_MODE (srcreg);
2114 machine_mode tmode = GET_MODE (target);
2115 machine_mode copy_mode;
2117 /* BLKmode registers created in the back-end shouldn't have survived. */
2118 gcc_assert (mode != BLKmode);
2120 /* If the structure doesn't take up a whole number of words, see whether
2121 SRCREG is padded on the left or on the right. If it's on the left,
2122 set PADDING_CORRECTION to the number of bits to skip.
2124 In most ABIs, the structure will be returned at the least end of
2125 the register, which translates to right padding on little-endian
2126 targets and left padding on big-endian targets. The opposite
2127 holds if the structure is returned at the most significant
2128 end of the register. */
2129 if (bytes % UNITS_PER_WORD != 0
2130 && (targetm.calls.return_in_msb (type)
2131 ? !BYTES_BIG_ENDIAN
2132 : BYTES_BIG_ENDIAN))
2133 padding_correction
2134 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2136 /* We can use a single move if we have an exact mode for the size. */
2137 else if (MEM_P (target)
2138 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2139 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2140 && bytes == GET_MODE_SIZE (mode))
2142 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2143 return;
2146 /* And if we additionally have the same mode for a register. */
2147 else if (REG_P (target)
2148 && GET_MODE (target) == mode
2149 && bytes == GET_MODE_SIZE (mode))
2151 emit_move_insn (target, srcreg);
2152 return;
2155 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2156 into a new pseudo which is a full word. */
2157 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2159 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2160 mode = word_mode;
2163 /* Copy the structure BITSIZE bits at a time. If the target lives in
2164 memory, take care of not reading/writing past its end by selecting
2165 a copy mode suited to BITSIZE. This should always be possible given
2166 how it is computed.
2168 If the target lives in register, make sure not to select a copy mode
2169 larger than the mode of the register.
2171 We could probably emit more efficient code for machines which do not use
2172 strict alignment, but it doesn't seem worth the effort at the current
2173 time. */
2175 copy_mode = word_mode;
2176 if (MEM_P (target))
2178 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2179 if (mem_mode != BLKmode)
2180 copy_mode = mem_mode;
2182 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2183 copy_mode = tmode;
2185 for (bitpos = 0, xbitpos = padding_correction;
2186 bitpos < bytes * BITS_PER_UNIT;
2187 bitpos += bitsize, xbitpos += bitsize)
2189 /* We need a new source operand each time xbitpos is on a
2190 word boundary and when xbitpos == padding_correction
2191 (the first time through). */
2192 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2193 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2195 /* We need a new destination operand each time bitpos is on
2196 a word boundary. */
2197 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2198 dst = target;
2199 else if (bitpos % BITS_PER_WORD == 0)
2200 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2202 /* Use xbitpos for the source extraction (right justified) and
2203 bitpos for the destination store (left justified). */
2204 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2205 extract_bit_field (src, bitsize,
2206 xbitpos % BITS_PER_WORD, 1,
2207 NULL_RTX, copy_mode, copy_mode));
2211 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2212 register if it contains any data, otherwise return null.
2214 This is used on targets that return BLKmode values in registers. */
2217 copy_blkmode_to_reg (machine_mode mode, tree src)
2219 int i, n_regs;
2220 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2221 unsigned int bitsize;
2222 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2223 machine_mode dst_mode;
2225 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2227 x = expand_normal (src);
2229 bytes = int_size_in_bytes (TREE_TYPE (src));
2230 if (bytes == 0)
2231 return NULL_RTX;
2233 /* If the structure doesn't take up a whole number of words, see
2234 whether the register value should be padded on the left or on
2235 the right. Set PADDING_CORRECTION to the number of padding
2236 bits needed on the left side.
2238 In most ABIs, the structure will be returned at the least end of
2239 the register, which translates to right padding on little-endian
2240 targets and left padding on big-endian targets. The opposite
2241 holds if the structure is returned at the most significant
2242 end of the register. */
2243 if (bytes % UNITS_PER_WORD != 0
2244 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2245 ? !BYTES_BIG_ENDIAN
2246 : BYTES_BIG_ENDIAN))
2247 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2248 * BITS_PER_UNIT));
2250 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2251 dst_words = XALLOCAVEC (rtx, n_regs);
2252 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2254 /* Copy the structure BITSIZE bits at a time. */
2255 for (bitpos = 0, xbitpos = padding_correction;
2256 bitpos < bytes * BITS_PER_UNIT;
2257 bitpos += bitsize, xbitpos += bitsize)
2259 /* We need a new destination pseudo each time xbitpos is
2260 on a word boundary and when xbitpos == padding_correction
2261 (the first time through). */
2262 if (xbitpos % BITS_PER_WORD == 0
2263 || xbitpos == padding_correction)
2265 /* Generate an appropriate register. */
2266 dst_word = gen_reg_rtx (word_mode);
2267 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2269 /* Clear the destination before we move anything into it. */
2270 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2273 /* We need a new source operand each time bitpos is on a word
2274 boundary. */
2275 if (bitpos % BITS_PER_WORD == 0)
2276 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2278 /* Use bitpos for the source extraction (left justified) and
2279 xbitpos for the destination store (right justified). */
2280 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2281 0, 0, word_mode,
2282 extract_bit_field (src_word, bitsize,
2283 bitpos % BITS_PER_WORD, 1,
2284 NULL_RTX, word_mode, word_mode));
2287 if (mode == BLKmode)
2289 /* Find the smallest integer mode large enough to hold the
2290 entire structure. */
2291 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2292 mode != VOIDmode;
2293 mode = GET_MODE_WIDER_MODE (mode))
2294 /* Have we found a large enough mode? */
2295 if (GET_MODE_SIZE (mode) >= bytes)
2296 break;
2298 /* A suitable mode should have been found. */
2299 gcc_assert (mode != VOIDmode);
2302 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2303 dst_mode = word_mode;
2304 else
2305 dst_mode = mode;
2306 dst = gen_reg_rtx (dst_mode);
2308 for (i = 0; i < n_regs; i++)
2309 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2311 if (mode != dst_mode)
2312 dst = gen_lowpart (mode, dst);
2314 return dst;
2317 /* Add a USE expression for REG to the (possibly empty) list pointed
2318 to by CALL_FUSAGE. REG must denote a hard register. */
2320 void
2321 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2323 gcc_assert (REG_P (reg));
2325 if (!HARD_REGISTER_P (reg))
2326 return;
2328 *call_fusage
2329 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2332 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2333 to by CALL_FUSAGE. REG must denote a hard register. */
2335 void
2336 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2338 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2340 *call_fusage
2341 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2344 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2345 starting at REGNO. All of these registers must be hard registers. */
2347 void
2348 use_regs (rtx *call_fusage, int regno, int nregs)
2350 int i;
2352 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2354 for (i = 0; i < nregs; i++)
2355 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2358 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2359 PARALLEL REGS. This is for calls that pass values in multiple
2360 non-contiguous locations. The Irix 6 ABI has examples of this. */
2362 void
2363 use_group_regs (rtx *call_fusage, rtx regs)
2365 int i;
2367 for (i = 0; i < XVECLEN (regs, 0); i++)
2369 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2371 /* A NULL entry means the parameter goes both on the stack and in
2372 registers. This can also be a MEM for targets that pass values
2373 partially on the stack and partially in registers. */
2374 if (reg != 0 && REG_P (reg))
2375 use_reg (call_fusage, reg);
2379 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2380 assigment and the code of the expresion on the RHS is CODE. Return
2381 NULL otherwise. */
2383 static gimple
2384 get_def_for_expr (tree name, enum tree_code code)
2386 gimple def_stmt;
2388 if (TREE_CODE (name) != SSA_NAME)
2389 return NULL;
2391 def_stmt = get_gimple_for_ssa_name (name);
2392 if (!def_stmt
2393 || gimple_assign_rhs_code (def_stmt) != code)
2394 return NULL;
2396 return def_stmt;
2399 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2400 assigment and the class of the expresion on the RHS is CLASS. Return
2401 NULL otherwise. */
2403 static gimple
2404 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2406 gimple def_stmt;
2408 if (TREE_CODE (name) != SSA_NAME)
2409 return NULL;
2411 def_stmt = get_gimple_for_ssa_name (name);
2412 if (!def_stmt
2413 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2414 return NULL;
2416 return def_stmt;
2420 /* Determine whether the LEN bytes generated by CONSTFUN can be
2421 stored to memory using several move instructions. CONSTFUNDATA is
2422 a pointer which will be passed as argument in every CONSTFUN call.
2423 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2424 a memset operation and false if it's a copy of a constant string.
2425 Return nonzero if a call to store_by_pieces should succeed. */
2428 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2429 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2430 void *constfundata, unsigned int align, bool memsetp)
2432 unsigned HOST_WIDE_INT l;
2433 unsigned int max_size;
2434 HOST_WIDE_INT offset = 0;
2435 machine_mode mode;
2436 enum insn_code icode;
2437 int reverse;
2438 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2439 rtx cst ATTRIBUTE_UNUSED;
2441 if (len == 0)
2442 return 1;
2444 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2445 memsetp
2446 ? SET_BY_PIECES
2447 : STORE_BY_PIECES,
2448 optimize_insn_for_speed_p ()))
2449 return 0;
2451 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2453 /* We would first store what we can in the largest integer mode, then go to
2454 successively smaller modes. */
2456 for (reverse = 0;
2457 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2458 reverse++)
2460 l = len;
2461 max_size = STORE_MAX_PIECES + 1;
2462 while (max_size > 1 && l > 0)
2464 mode = widest_int_mode_for_size (max_size);
2466 if (mode == VOIDmode)
2467 break;
2469 icode = optab_handler (mov_optab, mode);
2470 if (icode != CODE_FOR_nothing
2471 && align >= GET_MODE_ALIGNMENT (mode))
2473 unsigned int size = GET_MODE_SIZE (mode);
2475 while (l >= size)
2477 if (reverse)
2478 offset -= size;
2480 cst = (*constfun) (constfundata, offset, mode);
2481 if (!targetm.legitimate_constant_p (mode, cst))
2482 return 0;
2484 if (!reverse)
2485 offset += size;
2487 l -= size;
2491 max_size = GET_MODE_SIZE (mode);
2494 /* The code above should have handled everything. */
2495 gcc_assert (!l);
2498 return 1;
2501 /* Generate several move instructions to store LEN bytes generated by
2502 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2503 pointer which will be passed as argument in every CONSTFUN call.
2504 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2505 a memset operation and false if it's a copy of a constant string.
2506 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2507 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2508 stpcpy. */
2511 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2512 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2513 void *constfundata, unsigned int align, bool memsetp, int endp)
2515 machine_mode to_addr_mode = get_address_mode (to);
2516 struct store_by_pieces_d data;
2518 if (len == 0)
2520 gcc_assert (endp != 2);
2521 return to;
2524 gcc_assert (targetm.use_by_pieces_infrastructure_p
2525 (len, align,
2526 memsetp
2527 ? SET_BY_PIECES
2528 : STORE_BY_PIECES,
2529 optimize_insn_for_speed_p ()));
2531 data.constfun = constfun;
2532 data.constfundata = constfundata;
2533 data.len = len;
2534 data.to = to;
2535 store_by_pieces_1 (&data, align);
2536 if (endp)
2538 rtx to1;
2540 gcc_assert (!data.reverse);
2541 if (data.autinc_to)
2543 if (endp == 2)
2545 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2546 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2547 else
2548 data.to_addr = copy_to_mode_reg (to_addr_mode,
2549 plus_constant (to_addr_mode,
2550 data.to_addr,
2551 -1));
2553 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2554 data.offset);
2556 else
2558 if (endp == 2)
2559 --data.offset;
2560 to1 = adjust_address (data.to, QImode, data.offset);
2562 return to1;
2564 else
2565 return data.to;
2568 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2569 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2571 static void
2572 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2574 struct store_by_pieces_d data;
2576 if (len == 0)
2577 return;
2579 data.constfun = clear_by_pieces_1;
2580 data.constfundata = NULL;
2581 data.len = len;
2582 data.to = to;
2583 store_by_pieces_1 (&data, align);
2586 /* Callback routine for clear_by_pieces.
2587 Return const0_rtx unconditionally. */
2589 static rtx
2590 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2591 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2592 machine_mode mode ATTRIBUTE_UNUSED)
2594 return const0_rtx;
2597 /* Subroutine of clear_by_pieces and store_by_pieces.
2598 Generate several move instructions to store LEN bytes of block TO. (A MEM
2599 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2601 static void
2602 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2603 unsigned int align ATTRIBUTE_UNUSED)
2605 machine_mode to_addr_mode = get_address_mode (data->to);
2606 rtx to_addr = XEXP (data->to, 0);
2607 unsigned int max_size = STORE_MAX_PIECES + 1;
2608 enum insn_code icode;
2610 data->offset = 0;
2611 data->to_addr = to_addr;
2612 data->autinc_to
2613 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2614 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2616 data->explicit_inc_to = 0;
2617 data->reverse
2618 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2619 if (data->reverse)
2620 data->offset = data->len;
2622 /* If storing requires more than two move insns,
2623 copy addresses to registers (to make displacements shorter)
2624 and use post-increment if available. */
2625 if (!data->autinc_to
2626 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2628 /* Determine the main mode we'll be using.
2629 MODE might not be used depending on the definitions of the
2630 USE_* macros below. */
2631 machine_mode mode ATTRIBUTE_UNUSED
2632 = widest_int_mode_for_size (max_size);
2634 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2636 data->to_addr = copy_to_mode_reg (to_addr_mode,
2637 plus_constant (to_addr_mode,
2638 to_addr,
2639 data->len));
2640 data->autinc_to = 1;
2641 data->explicit_inc_to = -1;
2644 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2645 && ! data->autinc_to)
2647 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2648 data->autinc_to = 1;
2649 data->explicit_inc_to = 1;
2652 if ( !data->autinc_to && CONSTANT_P (to_addr))
2653 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2656 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2658 /* First store what we can in the largest integer mode, then go to
2659 successively smaller modes. */
2661 while (max_size > 1 && data->len > 0)
2663 machine_mode mode = widest_int_mode_for_size (max_size);
2665 if (mode == VOIDmode)
2666 break;
2668 icode = optab_handler (mov_optab, mode);
2669 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2670 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2672 max_size = GET_MODE_SIZE (mode);
2675 /* The code above should have handled everything. */
2676 gcc_assert (!data->len);
2679 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2680 with move instructions for mode MODE. GENFUN is the gen_... function
2681 to make a move insn for that mode. DATA has all the other info. */
2683 static void
2684 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2685 struct store_by_pieces_d *data)
2687 unsigned int size = GET_MODE_SIZE (mode);
2688 rtx to1, cst;
2690 while (data->len >= size)
2692 if (data->reverse)
2693 data->offset -= size;
2695 if (data->autinc_to)
2696 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2697 data->offset);
2698 else
2699 to1 = adjust_address (data->to, mode, data->offset);
2701 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2702 emit_insn (gen_add2_insn (data->to_addr,
2703 gen_int_mode (-(HOST_WIDE_INT) size,
2704 GET_MODE (data->to_addr))));
2706 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2707 emit_insn ((*genfun) (to1, cst));
2709 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2710 emit_insn (gen_add2_insn (data->to_addr,
2711 gen_int_mode (size,
2712 GET_MODE (data->to_addr))));
2714 if (! data->reverse)
2715 data->offset += size;
2717 data->len -= size;
2721 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2722 its length in bytes. */
2725 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2726 unsigned int expected_align, HOST_WIDE_INT expected_size,
2727 unsigned HOST_WIDE_INT min_size,
2728 unsigned HOST_WIDE_INT max_size,
2729 unsigned HOST_WIDE_INT probable_max_size)
2731 machine_mode mode = GET_MODE (object);
2732 unsigned int align;
2734 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2736 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2737 just move a zero. Otherwise, do this a piece at a time. */
2738 if (mode != BLKmode
2739 && CONST_INT_P (size)
2740 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2742 rtx zero = CONST0_RTX (mode);
2743 if (zero != NULL)
2745 emit_move_insn (object, zero);
2746 return NULL;
2749 if (COMPLEX_MODE_P (mode))
2751 zero = CONST0_RTX (GET_MODE_INNER (mode));
2752 if (zero != NULL)
2754 write_complex_part (object, zero, 0);
2755 write_complex_part (object, zero, 1);
2756 return NULL;
2761 if (size == const0_rtx)
2762 return NULL;
2764 align = MEM_ALIGN (object);
2766 if (CONST_INT_P (size)
2767 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2768 CLEAR_BY_PIECES,
2769 optimize_insn_for_speed_p ()))
2770 clear_by_pieces (object, INTVAL (size), align);
2771 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2772 expected_align, expected_size,
2773 min_size, max_size, probable_max_size))
2775 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2776 return set_storage_via_libcall (object, size, const0_rtx,
2777 method == BLOCK_OP_TAILCALL);
2778 else
2779 gcc_unreachable ();
2781 return NULL;
2785 clear_storage (rtx object, rtx size, enum block_op_methods method)
2787 unsigned HOST_WIDE_INT max, min = 0;
2788 if (GET_CODE (size) == CONST_INT)
2789 min = max = UINTVAL (size);
2790 else
2791 max = GET_MODE_MASK (GET_MODE (size));
2792 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2796 /* A subroutine of clear_storage. Expand a call to memset.
2797 Return the return value of memset, 0 otherwise. */
2800 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2802 tree call_expr, fn, object_tree, size_tree, val_tree;
2803 machine_mode size_mode;
2804 rtx retval;
2806 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2807 place those into new pseudos into a VAR_DECL and use them later. */
2809 object = copy_addr_to_reg (XEXP (object, 0));
2811 size_mode = TYPE_MODE (sizetype);
2812 size = convert_to_mode (size_mode, size, 1);
2813 size = copy_to_mode_reg (size_mode, size);
2815 /* It is incorrect to use the libcall calling conventions to call
2816 memset in this context. This could be a user call to memset and
2817 the user may wish to examine the return value from memset. For
2818 targets where libcalls and normal calls have different conventions
2819 for returning pointers, we could end up generating incorrect code. */
2821 object_tree = make_tree (ptr_type_node, object);
2822 if (!CONST_INT_P (val))
2823 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2824 size_tree = make_tree (sizetype, size);
2825 val_tree = make_tree (integer_type_node, val);
2827 fn = clear_storage_libcall_fn (true);
2828 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2829 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2831 retval = expand_normal (call_expr);
2833 return retval;
2836 /* A subroutine of set_storage_via_libcall. Create the tree node
2837 for the function we use for block clears. */
2839 tree block_clear_fn;
2841 void
2842 init_block_clear_fn (const char *asmspec)
2844 if (!block_clear_fn)
2846 tree fn, args;
2848 fn = get_identifier ("memset");
2849 args = build_function_type_list (ptr_type_node, ptr_type_node,
2850 integer_type_node, sizetype,
2851 NULL_TREE);
2853 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2854 DECL_EXTERNAL (fn) = 1;
2855 TREE_PUBLIC (fn) = 1;
2856 DECL_ARTIFICIAL (fn) = 1;
2857 TREE_NOTHROW (fn) = 1;
2858 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2859 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2861 block_clear_fn = fn;
2864 if (asmspec)
2865 set_user_assembler_name (block_clear_fn, asmspec);
2868 static tree
2869 clear_storage_libcall_fn (int for_call)
2871 static bool emitted_extern;
2873 if (!block_clear_fn)
2874 init_block_clear_fn (NULL);
2876 if (for_call && !emitted_extern)
2878 emitted_extern = true;
2879 make_decl_rtl (block_clear_fn);
2882 return block_clear_fn;
2885 /* Expand a setmem pattern; return true if successful. */
2887 bool
2888 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2889 unsigned int expected_align, HOST_WIDE_INT expected_size,
2890 unsigned HOST_WIDE_INT min_size,
2891 unsigned HOST_WIDE_INT max_size,
2892 unsigned HOST_WIDE_INT probable_max_size)
2894 /* Try the most limited insn first, because there's no point
2895 including more than one in the machine description unless
2896 the more limited one has some advantage. */
2898 machine_mode mode;
2900 if (expected_align < align)
2901 expected_align = align;
2902 if (expected_size != -1)
2904 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2905 expected_size = max_size;
2906 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2907 expected_size = min_size;
2910 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2911 mode = GET_MODE_WIDER_MODE (mode))
2913 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2915 if (code != CODE_FOR_nothing
2916 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2917 here because if SIZE is less than the mode mask, as it is
2918 returned by the macro, it will definitely be less than the
2919 actual mode mask. Since SIZE is within the Pmode address
2920 space, we limit MODE to Pmode. */
2921 && ((CONST_INT_P (size)
2922 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2923 <= (GET_MODE_MASK (mode) >> 1)))
2924 || max_size <= (GET_MODE_MASK (mode) >> 1)
2925 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2927 struct expand_operand ops[9];
2928 unsigned int nops;
2930 nops = insn_data[(int) code].n_generator_args;
2931 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2933 create_fixed_operand (&ops[0], object);
2934 /* The check above guarantees that this size conversion is valid. */
2935 create_convert_operand_to (&ops[1], size, mode, true);
2936 create_convert_operand_from (&ops[2], val, byte_mode, true);
2937 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2938 if (nops >= 6)
2940 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2941 create_integer_operand (&ops[5], expected_size);
2943 if (nops >= 8)
2945 create_integer_operand (&ops[6], min_size);
2946 /* If we can not represent the maximal size,
2947 make parameter NULL. */
2948 if ((HOST_WIDE_INT) max_size != -1)
2949 create_integer_operand (&ops[7], max_size);
2950 else
2951 create_fixed_operand (&ops[7], NULL);
2953 if (nops == 9)
2955 /* If we can not represent the maximal size,
2956 make parameter NULL. */
2957 if ((HOST_WIDE_INT) probable_max_size != -1)
2958 create_integer_operand (&ops[8], probable_max_size);
2959 else
2960 create_fixed_operand (&ops[8], NULL);
2962 if (maybe_expand_insn (code, nops, ops))
2963 return true;
2967 return false;
2971 /* Write to one of the components of the complex value CPLX. Write VAL to
2972 the real part if IMAG_P is false, and the imaginary part if its true. */
2974 void
2975 write_complex_part (rtx cplx, rtx val, bool imag_p)
2977 machine_mode cmode;
2978 machine_mode imode;
2979 unsigned ibitsize;
2981 if (GET_CODE (cplx) == CONCAT)
2983 emit_move_insn (XEXP (cplx, imag_p), val);
2984 return;
2987 cmode = GET_MODE (cplx);
2988 imode = GET_MODE_INNER (cmode);
2989 ibitsize = GET_MODE_BITSIZE (imode);
2991 /* For MEMs simplify_gen_subreg may generate an invalid new address
2992 because, e.g., the original address is considered mode-dependent
2993 by the target, which restricts simplify_subreg from invoking
2994 adjust_address_nv. Instead of preparing fallback support for an
2995 invalid address, we call adjust_address_nv directly. */
2996 if (MEM_P (cplx))
2998 emit_move_insn (adjust_address_nv (cplx, imode,
2999 imag_p ? GET_MODE_SIZE (imode) : 0),
3000 val);
3001 return;
3004 /* If the sub-object is at least word sized, then we know that subregging
3005 will work. This special case is important, since store_bit_field
3006 wants to operate on integer modes, and there's rarely an OImode to
3007 correspond to TCmode. */
3008 if (ibitsize >= BITS_PER_WORD
3009 /* For hard regs we have exact predicates. Assume we can split
3010 the original object if it spans an even number of hard regs.
3011 This special case is important for SCmode on 64-bit platforms
3012 where the natural size of floating-point regs is 32-bit. */
3013 || (REG_P (cplx)
3014 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3015 && REG_NREGS (cplx) % 2 == 0))
3017 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3018 imag_p ? GET_MODE_SIZE (imode) : 0);
3019 if (part)
3021 emit_move_insn (part, val);
3022 return;
3024 else
3025 /* simplify_gen_subreg may fail for sub-word MEMs. */
3026 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3029 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3032 /* Extract one of the components of the complex value CPLX. Extract the
3033 real part if IMAG_P is false, and the imaginary part if it's true. */
3035 static rtx
3036 read_complex_part (rtx cplx, bool imag_p)
3038 machine_mode cmode, imode;
3039 unsigned ibitsize;
3041 if (GET_CODE (cplx) == CONCAT)
3042 return XEXP (cplx, imag_p);
3044 cmode = GET_MODE (cplx);
3045 imode = GET_MODE_INNER (cmode);
3046 ibitsize = GET_MODE_BITSIZE (imode);
3048 /* Special case reads from complex constants that got spilled to memory. */
3049 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3051 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3052 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3054 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3055 if (CONSTANT_CLASS_P (part))
3056 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3060 /* For MEMs simplify_gen_subreg may generate an invalid new address
3061 because, e.g., the original address is considered mode-dependent
3062 by the target, which restricts simplify_subreg from invoking
3063 adjust_address_nv. Instead of preparing fallback support for an
3064 invalid address, we call adjust_address_nv directly. */
3065 if (MEM_P (cplx))
3066 return adjust_address_nv (cplx, imode,
3067 imag_p ? GET_MODE_SIZE (imode) : 0);
3069 /* If the sub-object is at least word sized, then we know that subregging
3070 will work. This special case is important, since extract_bit_field
3071 wants to operate on integer modes, and there's rarely an OImode to
3072 correspond to TCmode. */
3073 if (ibitsize >= BITS_PER_WORD
3074 /* For hard regs we have exact predicates. Assume we can split
3075 the original object if it spans an even number of hard regs.
3076 This special case is important for SCmode on 64-bit platforms
3077 where the natural size of floating-point regs is 32-bit. */
3078 || (REG_P (cplx)
3079 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3080 && REG_NREGS (cplx) % 2 == 0))
3082 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3083 imag_p ? GET_MODE_SIZE (imode) : 0);
3084 if (ret)
3085 return ret;
3086 else
3087 /* simplify_gen_subreg may fail for sub-word MEMs. */
3088 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3091 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3092 true, NULL_RTX, imode, imode);
3095 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3096 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3097 represented in NEW_MODE. If FORCE is true, this will never happen, as
3098 we'll force-create a SUBREG if needed. */
3100 static rtx
3101 emit_move_change_mode (machine_mode new_mode,
3102 machine_mode old_mode, rtx x, bool force)
3104 rtx ret;
3106 if (push_operand (x, GET_MODE (x)))
3108 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3109 MEM_COPY_ATTRIBUTES (ret, x);
3111 else if (MEM_P (x))
3113 /* We don't have to worry about changing the address since the
3114 size in bytes is supposed to be the same. */
3115 if (reload_in_progress)
3117 /* Copy the MEM to change the mode and move any
3118 substitutions from the old MEM to the new one. */
3119 ret = adjust_address_nv (x, new_mode, 0);
3120 copy_replacements (x, ret);
3122 else
3123 ret = adjust_address (x, new_mode, 0);
3125 else
3127 /* Note that we do want simplify_subreg's behavior of validating
3128 that the new mode is ok for a hard register. If we were to use
3129 simplify_gen_subreg, we would create the subreg, but would
3130 probably run into the target not being able to implement it. */
3131 /* Except, of course, when FORCE is true, when this is exactly what
3132 we want. Which is needed for CCmodes on some targets. */
3133 if (force)
3134 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3135 else
3136 ret = simplify_subreg (new_mode, x, old_mode, 0);
3139 return ret;
3142 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3143 an integer mode of the same size as MODE. Returns the instruction
3144 emitted, or NULL if such a move could not be generated. */
3146 static rtx_insn *
3147 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3149 machine_mode imode;
3150 enum insn_code code;
3152 /* There must exist a mode of the exact size we require. */
3153 imode = int_mode_for_mode (mode);
3154 if (imode == BLKmode)
3155 return NULL;
3157 /* The target must support moves in this mode. */
3158 code = optab_handler (mov_optab, imode);
3159 if (code == CODE_FOR_nothing)
3160 return NULL;
3162 x = emit_move_change_mode (imode, mode, x, force);
3163 if (x == NULL_RTX)
3164 return NULL;
3165 y = emit_move_change_mode (imode, mode, y, force);
3166 if (y == NULL_RTX)
3167 return NULL;
3168 return emit_insn (GEN_FCN (code) (x, y));
3171 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3172 Return an equivalent MEM that does not use an auto-increment. */
3175 emit_move_resolve_push (machine_mode mode, rtx x)
3177 enum rtx_code code = GET_CODE (XEXP (x, 0));
3178 HOST_WIDE_INT adjust;
3179 rtx temp;
3181 adjust = GET_MODE_SIZE (mode);
3182 #ifdef PUSH_ROUNDING
3183 adjust = PUSH_ROUNDING (adjust);
3184 #endif
3185 if (code == PRE_DEC || code == POST_DEC)
3186 adjust = -adjust;
3187 else if (code == PRE_MODIFY || code == POST_MODIFY)
3189 rtx expr = XEXP (XEXP (x, 0), 1);
3190 HOST_WIDE_INT val;
3192 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3193 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3194 val = INTVAL (XEXP (expr, 1));
3195 if (GET_CODE (expr) == MINUS)
3196 val = -val;
3197 gcc_assert (adjust == val || adjust == -val);
3198 adjust = val;
3201 /* Do not use anti_adjust_stack, since we don't want to update
3202 stack_pointer_delta. */
3203 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3204 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3205 0, OPTAB_LIB_WIDEN);
3206 if (temp != stack_pointer_rtx)
3207 emit_move_insn (stack_pointer_rtx, temp);
3209 switch (code)
3211 case PRE_INC:
3212 case PRE_DEC:
3213 case PRE_MODIFY:
3214 temp = stack_pointer_rtx;
3215 break;
3216 case POST_INC:
3217 case POST_DEC:
3218 case POST_MODIFY:
3219 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3220 break;
3221 default:
3222 gcc_unreachable ();
3225 return replace_equiv_address (x, temp);
3228 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3229 X is known to satisfy push_operand, and MODE is known to be complex.
3230 Returns the last instruction emitted. */
3232 rtx_insn *
3233 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3235 machine_mode submode = GET_MODE_INNER (mode);
3236 bool imag_first;
3238 #ifdef PUSH_ROUNDING
3239 unsigned int submodesize = GET_MODE_SIZE (submode);
3241 /* In case we output to the stack, but the size is smaller than the
3242 machine can push exactly, we need to use move instructions. */
3243 if (PUSH_ROUNDING (submodesize) != submodesize)
3245 x = emit_move_resolve_push (mode, x);
3246 return emit_move_insn (x, y);
3248 #endif
3250 /* Note that the real part always precedes the imag part in memory
3251 regardless of machine's endianness. */
3252 switch (GET_CODE (XEXP (x, 0)))
3254 case PRE_DEC:
3255 case POST_DEC:
3256 imag_first = true;
3257 break;
3258 case PRE_INC:
3259 case POST_INC:
3260 imag_first = false;
3261 break;
3262 default:
3263 gcc_unreachable ();
3266 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3267 read_complex_part (y, imag_first));
3268 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3269 read_complex_part (y, !imag_first));
3272 /* A subroutine of emit_move_complex. Perform the move from Y to X
3273 via two moves of the parts. Returns the last instruction emitted. */
3275 rtx_insn *
3276 emit_move_complex_parts (rtx x, rtx y)
3278 /* Show the output dies here. This is necessary for SUBREGs
3279 of pseudos since we cannot track their lifetimes correctly;
3280 hard regs shouldn't appear here except as return values. */
3281 if (!reload_completed && !reload_in_progress
3282 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3283 emit_clobber (x);
3285 write_complex_part (x, read_complex_part (y, false), false);
3286 write_complex_part (x, read_complex_part (y, true), true);
3288 return get_last_insn ();
3291 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3292 MODE is known to be complex. Returns the last instruction emitted. */
3294 static rtx_insn *
3295 emit_move_complex (machine_mode mode, rtx x, rtx y)
3297 bool try_int;
3299 /* Need to take special care for pushes, to maintain proper ordering
3300 of the data, and possibly extra padding. */
3301 if (push_operand (x, mode))
3302 return emit_move_complex_push (mode, x, y);
3304 /* See if we can coerce the target into moving both values at once, except
3305 for floating point where we favor moving as parts if this is easy. */
3306 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3307 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3308 && !(REG_P (x)
3309 && HARD_REGISTER_P (x)
3310 && REG_NREGS (x) == 1)
3311 && !(REG_P (y)
3312 && HARD_REGISTER_P (y)
3313 && REG_NREGS (y) == 1))
3314 try_int = false;
3315 /* Not possible if the values are inherently not adjacent. */
3316 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3317 try_int = false;
3318 /* Is possible if both are registers (or subregs of registers). */
3319 else if (register_operand (x, mode) && register_operand (y, mode))
3320 try_int = true;
3321 /* If one of the operands is a memory, and alignment constraints
3322 are friendly enough, we may be able to do combined memory operations.
3323 We do not attempt this if Y is a constant because that combination is
3324 usually better with the by-parts thing below. */
3325 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3326 && (!STRICT_ALIGNMENT
3327 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3328 try_int = true;
3329 else
3330 try_int = false;
3332 if (try_int)
3334 rtx_insn *ret;
3336 /* For memory to memory moves, optimal behavior can be had with the
3337 existing block move logic. */
3338 if (MEM_P (x) && MEM_P (y))
3340 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3341 BLOCK_OP_NO_LIBCALL);
3342 return get_last_insn ();
3345 ret = emit_move_via_integer (mode, x, y, true);
3346 if (ret)
3347 return ret;
3350 return emit_move_complex_parts (x, y);
3353 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3354 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3356 static rtx_insn *
3357 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3359 rtx_insn *ret;
3361 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3362 if (mode != CCmode)
3364 enum insn_code code = optab_handler (mov_optab, CCmode);
3365 if (code != CODE_FOR_nothing)
3367 x = emit_move_change_mode (CCmode, mode, x, true);
3368 y = emit_move_change_mode (CCmode, mode, y, true);
3369 return emit_insn (GEN_FCN (code) (x, y));
3373 /* Otherwise, find the MODE_INT mode of the same width. */
3374 ret = emit_move_via_integer (mode, x, y, false);
3375 gcc_assert (ret != NULL);
3376 return ret;
3379 /* Return true if word I of OP lies entirely in the
3380 undefined bits of a paradoxical subreg. */
3382 static bool
3383 undefined_operand_subword_p (const_rtx op, int i)
3385 machine_mode innermode, innermostmode;
3386 int offset;
3387 if (GET_CODE (op) != SUBREG)
3388 return false;
3389 innermode = GET_MODE (op);
3390 innermostmode = GET_MODE (SUBREG_REG (op));
3391 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3392 /* The SUBREG_BYTE represents offset, as if the value were stored in
3393 memory, except for a paradoxical subreg where we define
3394 SUBREG_BYTE to be 0; undo this exception as in
3395 simplify_subreg. */
3396 if (SUBREG_BYTE (op) == 0
3397 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3399 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3400 if (WORDS_BIG_ENDIAN)
3401 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3402 if (BYTES_BIG_ENDIAN)
3403 offset += difference % UNITS_PER_WORD;
3405 if (offset >= GET_MODE_SIZE (innermostmode)
3406 || offset <= -GET_MODE_SIZE (word_mode))
3407 return true;
3408 return false;
3411 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3412 MODE is any multi-word or full-word mode that lacks a move_insn
3413 pattern. Note that you will get better code if you define such
3414 patterns, even if they must turn into multiple assembler instructions. */
3416 static rtx_insn *
3417 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3419 rtx_insn *last_insn = 0;
3420 rtx_insn *seq;
3421 rtx inner;
3422 bool need_clobber;
3423 int i;
3425 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3427 /* If X is a push on the stack, do the push now and replace
3428 X with a reference to the stack pointer. */
3429 if (push_operand (x, mode))
3430 x = emit_move_resolve_push (mode, x);
3432 /* If we are in reload, see if either operand is a MEM whose address
3433 is scheduled for replacement. */
3434 if (reload_in_progress && MEM_P (x)
3435 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3436 x = replace_equiv_address_nv (x, inner);
3437 if (reload_in_progress && MEM_P (y)
3438 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3439 y = replace_equiv_address_nv (y, inner);
3441 start_sequence ();
3443 need_clobber = false;
3444 for (i = 0;
3445 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3446 i++)
3448 rtx xpart = operand_subword (x, i, 1, mode);
3449 rtx ypart;
3451 /* Do not generate code for a move if it would come entirely
3452 from the undefined bits of a paradoxical subreg. */
3453 if (undefined_operand_subword_p (y, i))
3454 continue;
3456 ypart = operand_subword (y, i, 1, mode);
3458 /* If we can't get a part of Y, put Y into memory if it is a
3459 constant. Otherwise, force it into a register. Then we must
3460 be able to get a part of Y. */
3461 if (ypart == 0 && CONSTANT_P (y))
3463 y = use_anchored_address (force_const_mem (mode, y));
3464 ypart = operand_subword (y, i, 1, mode);
3466 else if (ypart == 0)
3467 ypart = operand_subword_force (y, i, mode);
3469 gcc_assert (xpart && ypart);
3471 need_clobber |= (GET_CODE (xpart) == SUBREG);
3473 last_insn = emit_move_insn (xpart, ypart);
3476 seq = get_insns ();
3477 end_sequence ();
3479 /* Show the output dies here. This is necessary for SUBREGs
3480 of pseudos since we cannot track their lifetimes correctly;
3481 hard regs shouldn't appear here except as return values.
3482 We never want to emit such a clobber after reload. */
3483 if (x != y
3484 && ! (reload_in_progress || reload_completed)
3485 && need_clobber != 0)
3486 emit_clobber (x);
3488 emit_insn (seq);
3490 return last_insn;
3493 /* Low level part of emit_move_insn.
3494 Called just like emit_move_insn, but assumes X and Y
3495 are basically valid. */
3497 rtx_insn *
3498 emit_move_insn_1 (rtx x, rtx y)
3500 machine_mode mode = GET_MODE (x);
3501 enum insn_code code;
3503 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3505 code = optab_handler (mov_optab, mode);
3506 if (code != CODE_FOR_nothing)
3507 return emit_insn (GEN_FCN (code) (x, y));
3509 /* Expand complex moves by moving real part and imag part. */
3510 if (COMPLEX_MODE_P (mode))
3511 return emit_move_complex (mode, x, y);
3513 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3514 || ALL_FIXED_POINT_MODE_P (mode))
3516 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3518 /* If we can't find an integer mode, use multi words. */
3519 if (result)
3520 return result;
3521 else
3522 return emit_move_multi_word (mode, x, y);
3525 if (GET_MODE_CLASS (mode) == MODE_CC)
3526 return emit_move_ccmode (mode, x, y);
3528 /* Try using a move pattern for the corresponding integer mode. This is
3529 only safe when simplify_subreg can convert MODE constants into integer
3530 constants. At present, it can only do this reliably if the value
3531 fits within a HOST_WIDE_INT. */
3532 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3534 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3536 if (ret)
3538 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3539 return ret;
3543 return emit_move_multi_word (mode, x, y);
3546 /* Generate code to copy Y into X.
3547 Both Y and X must have the same mode, except that
3548 Y can be a constant with VOIDmode.
3549 This mode cannot be BLKmode; use emit_block_move for that.
3551 Return the last instruction emitted. */
3553 rtx_insn *
3554 emit_move_insn (rtx x, rtx y)
3556 machine_mode mode = GET_MODE (x);
3557 rtx y_cst = NULL_RTX;
3558 rtx_insn *last_insn;
3559 rtx set;
3561 gcc_assert (mode != BLKmode
3562 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3564 if (CONSTANT_P (y))
3566 if (optimize
3567 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3568 && (last_insn = compress_float_constant (x, y)))
3569 return last_insn;
3571 y_cst = y;
3573 if (!targetm.legitimate_constant_p (mode, y))
3575 y = force_const_mem (mode, y);
3577 /* If the target's cannot_force_const_mem prevented the spill,
3578 assume that the target's move expanders will also take care
3579 of the non-legitimate constant. */
3580 if (!y)
3581 y = y_cst;
3582 else
3583 y = use_anchored_address (y);
3587 /* If X or Y are memory references, verify that their addresses are valid
3588 for the machine. */
3589 if (MEM_P (x)
3590 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3591 MEM_ADDR_SPACE (x))
3592 && ! push_operand (x, GET_MODE (x))))
3593 x = validize_mem (x);
3595 if (MEM_P (y)
3596 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3597 MEM_ADDR_SPACE (y)))
3598 y = validize_mem (y);
3600 gcc_assert (mode != BLKmode);
3602 last_insn = emit_move_insn_1 (x, y);
3604 if (y_cst && REG_P (x)
3605 && (set = single_set (last_insn)) != NULL_RTX
3606 && SET_DEST (set) == x
3607 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3608 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3610 return last_insn;
3613 /* Generate the body of an instruction to copy Y into X.
3614 It may be a list of insns, if one insn isn't enough. */
3616 rtx_insn *
3617 gen_move_insn (rtx x, rtx y)
3619 rtx_insn *seq;
3621 start_sequence ();
3622 emit_move_insn_1 (x, y);
3623 seq = get_insns ();
3624 end_sequence ();
3625 return seq;
3628 /* Same as above, but return rtx (used as a callback, which must have
3629 prototype compatible with other functions returning rtx). */
3632 gen_move_insn_uncast (rtx x, rtx y)
3634 return gen_move_insn (x, y);
3637 /* If Y is representable exactly in a narrower mode, and the target can
3638 perform the extension directly from constant or memory, then emit the
3639 move as an extension. */
3641 static rtx_insn *
3642 compress_float_constant (rtx x, rtx y)
3644 machine_mode dstmode = GET_MODE (x);
3645 machine_mode orig_srcmode = GET_MODE (y);
3646 machine_mode srcmode;
3647 REAL_VALUE_TYPE r;
3648 int oldcost, newcost;
3649 bool speed = optimize_insn_for_speed_p ();
3651 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3653 if (targetm.legitimate_constant_p (dstmode, y))
3654 oldcost = set_src_cost (y, speed);
3655 else
3656 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3658 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3659 srcmode != orig_srcmode;
3660 srcmode = GET_MODE_WIDER_MODE (srcmode))
3662 enum insn_code ic;
3663 rtx trunc_y;
3664 rtx_insn *last_insn;
3666 /* Skip if the target can't extend this way. */
3667 ic = can_extend_p (dstmode, srcmode, 0);
3668 if (ic == CODE_FOR_nothing)
3669 continue;
3671 /* Skip if the narrowed value isn't exact. */
3672 if (! exact_real_truncate (srcmode, &r))
3673 continue;
3675 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3677 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3679 /* Skip if the target needs extra instructions to perform
3680 the extension. */
3681 if (!insn_operand_matches (ic, 1, trunc_y))
3682 continue;
3683 /* This is valid, but may not be cheaper than the original. */
3684 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3685 speed);
3686 if (oldcost < newcost)
3687 continue;
3689 else if (float_extend_from_mem[dstmode][srcmode])
3691 trunc_y = force_const_mem (srcmode, trunc_y);
3692 /* This is valid, but may not be cheaper than the original. */
3693 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3694 speed);
3695 if (oldcost < newcost)
3696 continue;
3697 trunc_y = validize_mem (trunc_y);
3699 else
3700 continue;
3702 /* For CSE's benefit, force the compressed constant pool entry
3703 into a new pseudo. This constant may be used in different modes,
3704 and if not, combine will put things back together for us. */
3705 trunc_y = force_reg (srcmode, trunc_y);
3707 /* If x is a hard register, perform the extension into a pseudo,
3708 so that e.g. stack realignment code is aware of it. */
3709 rtx target = x;
3710 if (REG_P (x) && HARD_REGISTER_P (x))
3711 target = gen_reg_rtx (dstmode);
3713 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3714 last_insn = get_last_insn ();
3716 if (REG_P (target))
3717 set_unique_reg_note (last_insn, REG_EQUAL, y);
3719 if (target != x)
3720 return emit_move_insn (x, target);
3721 return last_insn;
3724 return NULL;
3727 /* Pushing data onto the stack. */
3729 /* Push a block of length SIZE (perhaps variable)
3730 and return an rtx to address the beginning of the block.
3731 The value may be virtual_outgoing_args_rtx.
3733 EXTRA is the number of bytes of padding to push in addition to SIZE.
3734 BELOW nonzero means this padding comes at low addresses;
3735 otherwise, the padding comes at high addresses. */
3738 push_block (rtx size, int extra, int below)
3740 rtx temp;
3742 size = convert_modes (Pmode, ptr_mode, size, 1);
3743 if (CONSTANT_P (size))
3744 anti_adjust_stack (plus_constant (Pmode, size, extra));
3745 else if (REG_P (size) && extra == 0)
3746 anti_adjust_stack (size);
3747 else
3749 temp = copy_to_mode_reg (Pmode, size);
3750 if (extra != 0)
3751 temp = expand_binop (Pmode, add_optab, temp,
3752 gen_int_mode (extra, Pmode),
3753 temp, 0, OPTAB_LIB_WIDEN);
3754 anti_adjust_stack (temp);
3757 if (STACK_GROWS_DOWNWARD)
3759 temp = virtual_outgoing_args_rtx;
3760 if (extra != 0 && below)
3761 temp = plus_constant (Pmode, temp, extra);
3763 else
3765 if (CONST_INT_P (size))
3766 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3767 -INTVAL (size) - (below ? 0 : extra));
3768 else if (extra != 0 && !below)
3769 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3770 negate_rtx (Pmode, plus_constant (Pmode, size,
3771 extra)));
3772 else
3773 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3774 negate_rtx (Pmode, size));
3777 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3780 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3782 static rtx
3783 mem_autoinc_base (rtx mem)
3785 if (MEM_P (mem))
3787 rtx addr = XEXP (mem, 0);
3788 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3789 return XEXP (addr, 0);
3791 return NULL;
3794 /* A utility routine used here, in reload, and in try_split. The insns
3795 after PREV up to and including LAST are known to adjust the stack,
3796 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3797 placing notes as appropriate. PREV may be NULL, indicating the
3798 entire insn sequence prior to LAST should be scanned.
3800 The set of allowed stack pointer modifications is small:
3801 (1) One or more auto-inc style memory references (aka pushes),
3802 (2) One or more addition/subtraction with the SP as destination,
3803 (3) A single move insn with the SP as destination,
3804 (4) A call_pop insn,
3805 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3807 Insns in the sequence that do not modify the SP are ignored,
3808 except for noreturn calls.
3810 The return value is the amount of adjustment that can be trivially
3811 verified, via immediate operand or auto-inc. If the adjustment
3812 cannot be trivially extracted, the return value is INT_MIN. */
3814 HOST_WIDE_INT
3815 find_args_size_adjust (rtx_insn *insn)
3817 rtx dest, set, pat;
3818 int i;
3820 pat = PATTERN (insn);
3821 set = NULL;
3823 /* Look for a call_pop pattern. */
3824 if (CALL_P (insn))
3826 /* We have to allow non-call_pop patterns for the case
3827 of emit_single_push_insn of a TLS address. */
3828 if (GET_CODE (pat) != PARALLEL)
3829 return 0;
3831 /* All call_pop have a stack pointer adjust in the parallel.
3832 The call itself is always first, and the stack adjust is
3833 usually last, so search from the end. */
3834 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3836 set = XVECEXP (pat, 0, i);
3837 if (GET_CODE (set) != SET)
3838 continue;
3839 dest = SET_DEST (set);
3840 if (dest == stack_pointer_rtx)
3841 break;
3843 /* We'd better have found the stack pointer adjust. */
3844 if (i == 0)
3845 return 0;
3846 /* Fall through to process the extracted SET and DEST
3847 as if it was a standalone insn. */
3849 else if (GET_CODE (pat) == SET)
3850 set = pat;
3851 else if ((set = single_set (insn)) != NULL)
3853 else if (GET_CODE (pat) == PARALLEL)
3855 /* ??? Some older ports use a parallel with a stack adjust
3856 and a store for a PUSH_ROUNDING pattern, rather than a
3857 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3858 /* ??? See h8300 and m68k, pushqi1. */
3859 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3861 set = XVECEXP (pat, 0, i);
3862 if (GET_CODE (set) != SET)
3863 continue;
3864 dest = SET_DEST (set);
3865 if (dest == stack_pointer_rtx)
3866 break;
3868 /* We do not expect an auto-inc of the sp in the parallel. */
3869 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3870 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3871 != stack_pointer_rtx);
3873 if (i < 0)
3874 return 0;
3876 else
3877 return 0;
3879 dest = SET_DEST (set);
3881 /* Look for direct modifications of the stack pointer. */
3882 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3884 /* Look for a trivial adjustment, otherwise assume nothing. */
3885 /* Note that the SPU restore_stack_block pattern refers to
3886 the stack pointer in V4SImode. Consider that non-trivial. */
3887 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3888 && GET_CODE (SET_SRC (set)) == PLUS
3889 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3890 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3891 return INTVAL (XEXP (SET_SRC (set), 1));
3892 /* ??? Reload can generate no-op moves, which will be cleaned
3893 up later. Recognize it and continue searching. */
3894 else if (rtx_equal_p (dest, SET_SRC (set)))
3895 return 0;
3896 else
3897 return HOST_WIDE_INT_MIN;
3899 else
3901 rtx mem, addr;
3903 /* Otherwise only think about autoinc patterns. */
3904 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3906 mem = dest;
3907 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3908 != stack_pointer_rtx);
3910 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3911 mem = SET_SRC (set);
3912 else
3913 return 0;
3915 addr = XEXP (mem, 0);
3916 switch (GET_CODE (addr))
3918 case PRE_INC:
3919 case POST_INC:
3920 return GET_MODE_SIZE (GET_MODE (mem));
3921 case PRE_DEC:
3922 case POST_DEC:
3923 return -GET_MODE_SIZE (GET_MODE (mem));
3924 case PRE_MODIFY:
3925 case POST_MODIFY:
3926 addr = XEXP (addr, 1);
3927 gcc_assert (GET_CODE (addr) == PLUS);
3928 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3929 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3930 return INTVAL (XEXP (addr, 1));
3931 default:
3932 gcc_unreachable ();
3938 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3940 int args_size = end_args_size;
3941 bool saw_unknown = false;
3942 rtx_insn *insn;
3944 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3946 HOST_WIDE_INT this_delta;
3948 if (!NONDEBUG_INSN_P (insn))
3949 continue;
3951 this_delta = find_args_size_adjust (insn);
3952 if (this_delta == 0)
3954 if (!CALL_P (insn)
3955 || ACCUMULATE_OUTGOING_ARGS
3956 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3957 continue;
3960 gcc_assert (!saw_unknown);
3961 if (this_delta == HOST_WIDE_INT_MIN)
3962 saw_unknown = true;
3964 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3965 if (STACK_GROWS_DOWNWARD)
3966 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3968 args_size -= this_delta;
3971 return saw_unknown ? INT_MIN : args_size;
3974 #ifdef PUSH_ROUNDING
3975 /* Emit single push insn. */
3977 static void
3978 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
3980 rtx dest_addr;
3981 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3982 rtx dest;
3983 enum insn_code icode;
3985 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3986 /* If there is push pattern, use it. Otherwise try old way of throwing
3987 MEM representing push operation to move expander. */
3988 icode = optab_handler (push_optab, mode);
3989 if (icode != CODE_FOR_nothing)
3991 struct expand_operand ops[1];
3993 create_input_operand (&ops[0], x, mode);
3994 if (maybe_expand_insn (icode, 1, ops))
3995 return;
3997 if (GET_MODE_SIZE (mode) == rounded_size)
3998 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
3999 /* If we are to pad downward, adjust the stack pointer first and
4000 then store X into the stack location using an offset. This is
4001 because emit_move_insn does not know how to pad; it does not have
4002 access to type. */
4003 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4005 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4006 HOST_WIDE_INT offset;
4008 emit_move_insn (stack_pointer_rtx,
4009 expand_binop (Pmode,
4010 STACK_GROWS_DOWNWARD ? sub_optab
4011 : add_optab,
4012 stack_pointer_rtx,
4013 gen_int_mode (rounded_size, Pmode),
4014 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4016 offset = (HOST_WIDE_INT) padding_size;
4017 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4018 /* We have already decremented the stack pointer, so get the
4019 previous value. */
4020 offset += (HOST_WIDE_INT) rounded_size;
4022 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4023 /* We have already incremented the stack pointer, so get the
4024 previous value. */
4025 offset -= (HOST_WIDE_INT) rounded_size;
4027 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4028 gen_int_mode (offset, Pmode));
4030 else
4032 if (STACK_GROWS_DOWNWARD)
4033 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4034 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4035 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4036 Pmode));
4037 else
4038 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4039 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4040 gen_int_mode (rounded_size, Pmode));
4042 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4045 dest = gen_rtx_MEM (mode, dest_addr);
4047 if (type != 0)
4049 set_mem_attributes (dest, type, 1);
4051 if (cfun->tail_call_marked)
4052 /* Function incoming arguments may overlap with sibling call
4053 outgoing arguments and we cannot allow reordering of reads
4054 from function arguments with stores to outgoing arguments
4055 of sibling calls. */
4056 set_mem_alias_set (dest, 0);
4058 emit_move_insn (dest, x);
4061 /* Emit and annotate a single push insn. */
4063 static void
4064 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4066 int delta, old_delta = stack_pointer_delta;
4067 rtx_insn *prev = get_last_insn ();
4068 rtx_insn *last;
4070 emit_single_push_insn_1 (mode, x, type);
4072 last = get_last_insn ();
4074 /* Notice the common case where we emitted exactly one insn. */
4075 if (PREV_INSN (last) == prev)
4077 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4078 return;
4081 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4082 gcc_assert (delta == INT_MIN || delta == old_delta);
4084 #endif
4086 /* If reading SIZE bytes from X will end up reading from
4087 Y return the number of bytes that overlap. Return -1
4088 if there is no overlap or -2 if we can't determine
4089 (for example when X and Y have different base registers). */
4091 static int
4092 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4094 rtx tmp = plus_constant (Pmode, x, size);
4095 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4097 if (!CONST_INT_P (sub))
4098 return -2;
4100 HOST_WIDE_INT val = INTVAL (sub);
4102 return IN_RANGE (val, 1, size) ? val : -1;
4105 /* Generate code to push X onto the stack, assuming it has mode MODE and
4106 type TYPE.
4107 MODE is redundant except when X is a CONST_INT (since they don't
4108 carry mode info).
4109 SIZE is an rtx for the size of data to be copied (in bytes),
4110 needed only if X is BLKmode.
4111 Return true if successful. May return false if asked to push a
4112 partial argument during a sibcall optimization (as specified by
4113 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4114 to not overlap.
4116 ALIGN (in bits) is maximum alignment we can assume.
4118 If PARTIAL and REG are both nonzero, then copy that many of the first
4119 bytes of X into registers starting with REG, and push the rest of X.
4120 The amount of space pushed is decreased by PARTIAL bytes.
4121 REG must be a hard register in this case.
4122 If REG is zero but PARTIAL is not, take any all others actions for an
4123 argument partially in registers, but do not actually load any
4124 registers.
4126 EXTRA is the amount in bytes of extra space to leave next to this arg.
4127 This is ignored if an argument block has already been allocated.
4129 On a machine that lacks real push insns, ARGS_ADDR is the address of
4130 the bottom of the argument block for this call. We use indexing off there
4131 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4132 argument block has not been preallocated.
4134 ARGS_SO_FAR is the size of args previously pushed for this call.
4136 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4137 for arguments passed in registers. If nonzero, it will be the number
4138 of bytes required. */
4140 bool
4141 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4142 unsigned int align, int partial, rtx reg, int extra,
4143 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4144 rtx alignment_pad, bool sibcall_p)
4146 rtx xinner;
4147 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4149 /* Decide where to pad the argument: `downward' for below,
4150 `upward' for above, or `none' for don't pad it.
4151 Default is below for small data on big-endian machines; else above. */
4152 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4154 /* Invert direction if stack is post-decrement.
4155 FIXME: why? */
4156 if (STACK_PUSH_CODE == POST_DEC)
4157 if (where_pad != none)
4158 where_pad = (where_pad == downward ? upward : downward);
4160 xinner = x;
4162 int nregs = partial / UNITS_PER_WORD;
4163 rtx *tmp_regs = NULL;
4164 int overlapping = 0;
4166 if (mode == BLKmode
4167 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4169 /* Copy a block into the stack, entirely or partially. */
4171 rtx temp;
4172 int used;
4173 int offset;
4174 int skip;
4176 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4177 used = partial - offset;
4179 if (mode != BLKmode)
4181 /* A value is to be stored in an insufficiently aligned
4182 stack slot; copy via a suitably aligned slot if
4183 necessary. */
4184 size = GEN_INT (GET_MODE_SIZE (mode));
4185 if (!MEM_P (xinner))
4187 temp = assign_temp (type, 1, 1);
4188 emit_move_insn (temp, xinner);
4189 xinner = temp;
4193 gcc_assert (size);
4195 /* USED is now the # of bytes we need not copy to the stack
4196 because registers will take care of them. */
4198 if (partial != 0)
4199 xinner = adjust_address (xinner, BLKmode, used);
4201 /* If the partial register-part of the arg counts in its stack size,
4202 skip the part of stack space corresponding to the registers.
4203 Otherwise, start copying to the beginning of the stack space,
4204 by setting SKIP to 0. */
4205 skip = (reg_parm_stack_space == 0) ? 0 : used;
4207 #ifdef PUSH_ROUNDING
4208 /* Do it with several push insns if that doesn't take lots of insns
4209 and if there is no difficulty with push insns that skip bytes
4210 on the stack for alignment purposes. */
4211 if (args_addr == 0
4212 && PUSH_ARGS
4213 && CONST_INT_P (size)
4214 && skip == 0
4215 && MEM_ALIGN (xinner) >= align
4216 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4217 /* Here we avoid the case of a structure whose weak alignment
4218 forces many pushes of a small amount of data,
4219 and such small pushes do rounding that causes trouble. */
4220 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4221 || align >= BIGGEST_ALIGNMENT
4222 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4223 == (align / BITS_PER_UNIT)))
4224 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4226 /* Push padding now if padding above and stack grows down,
4227 or if padding below and stack grows up.
4228 But if space already allocated, this has already been done. */
4229 if (extra && args_addr == 0
4230 && where_pad != none && where_pad != stack_direction)
4231 anti_adjust_stack (GEN_INT (extra));
4233 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4235 else
4236 #endif /* PUSH_ROUNDING */
4238 rtx target;
4240 /* Otherwise make space on the stack and copy the data
4241 to the address of that space. */
4243 /* Deduct words put into registers from the size we must copy. */
4244 if (partial != 0)
4246 if (CONST_INT_P (size))
4247 size = GEN_INT (INTVAL (size) - used);
4248 else
4249 size = expand_binop (GET_MODE (size), sub_optab, size,
4250 gen_int_mode (used, GET_MODE (size)),
4251 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4254 /* Get the address of the stack space.
4255 In this case, we do not deal with EXTRA separately.
4256 A single stack adjust will do. */
4257 if (! args_addr)
4259 temp = push_block (size, extra, where_pad == downward);
4260 extra = 0;
4262 else if (CONST_INT_P (args_so_far))
4263 temp = memory_address (BLKmode,
4264 plus_constant (Pmode, args_addr,
4265 skip + INTVAL (args_so_far)));
4266 else
4267 temp = memory_address (BLKmode,
4268 plus_constant (Pmode,
4269 gen_rtx_PLUS (Pmode,
4270 args_addr,
4271 args_so_far),
4272 skip));
4274 if (!ACCUMULATE_OUTGOING_ARGS)
4276 /* If the source is referenced relative to the stack pointer,
4277 copy it to another register to stabilize it. We do not need
4278 to do this if we know that we won't be changing sp. */
4280 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4281 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4282 temp = copy_to_reg (temp);
4285 target = gen_rtx_MEM (BLKmode, temp);
4287 /* We do *not* set_mem_attributes here, because incoming arguments
4288 may overlap with sibling call outgoing arguments and we cannot
4289 allow reordering of reads from function arguments with stores
4290 to outgoing arguments of sibling calls. We do, however, want
4291 to record the alignment of the stack slot. */
4292 /* ALIGN may well be better aligned than TYPE, e.g. due to
4293 PARM_BOUNDARY. Assume the caller isn't lying. */
4294 set_mem_align (target, align);
4296 /* If part should go in registers and pushing to that part would
4297 overwrite some of the values that need to go into regs, load the
4298 overlapping values into temporary pseudos to be moved into the hard
4299 regs at the end after the stack pushing has completed.
4300 We cannot load them directly into the hard regs here because
4301 they can be clobbered by the block move expansions.
4302 See PR 65358. */
4304 if (partial > 0 && reg != 0 && mode == BLKmode
4305 && GET_CODE (reg) != PARALLEL)
4307 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4308 if (overlapping > 0)
4310 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4311 overlapping /= UNITS_PER_WORD;
4313 tmp_regs = XALLOCAVEC (rtx, overlapping);
4315 for (int i = 0; i < overlapping; i++)
4316 tmp_regs[i] = gen_reg_rtx (word_mode);
4318 for (int i = 0; i < overlapping; i++)
4319 emit_move_insn (tmp_regs[i],
4320 operand_subword_force (target, i, mode));
4322 else if (overlapping == -1)
4323 overlapping = 0;
4324 /* Could not determine whether there is overlap.
4325 Fail the sibcall. */
4326 else
4328 overlapping = 0;
4329 if (sibcall_p)
4330 return false;
4333 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4336 else if (partial > 0)
4338 /* Scalar partly in registers. */
4340 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4341 int i;
4342 int not_stack;
4343 /* # bytes of start of argument
4344 that we must make space for but need not store. */
4345 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4346 int args_offset = INTVAL (args_so_far);
4347 int skip;
4349 /* Push padding now if padding above and stack grows down,
4350 or if padding below and stack grows up.
4351 But if space already allocated, this has already been done. */
4352 if (extra && args_addr == 0
4353 && where_pad != none && where_pad != stack_direction)
4354 anti_adjust_stack (GEN_INT (extra));
4356 /* If we make space by pushing it, we might as well push
4357 the real data. Otherwise, we can leave OFFSET nonzero
4358 and leave the space uninitialized. */
4359 if (args_addr == 0)
4360 offset = 0;
4362 /* Now NOT_STACK gets the number of words that we don't need to
4363 allocate on the stack. Convert OFFSET to words too. */
4364 not_stack = (partial - offset) / UNITS_PER_WORD;
4365 offset /= UNITS_PER_WORD;
4367 /* If the partial register-part of the arg counts in its stack size,
4368 skip the part of stack space corresponding to the registers.
4369 Otherwise, start copying to the beginning of the stack space,
4370 by setting SKIP to 0. */
4371 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4373 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4374 x = validize_mem (force_const_mem (mode, x));
4376 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4377 SUBREGs of such registers are not allowed. */
4378 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4379 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4380 x = copy_to_reg (x);
4382 /* Loop over all the words allocated on the stack for this arg. */
4383 /* We can do it by words, because any scalar bigger than a word
4384 has a size a multiple of a word. */
4385 for (i = size - 1; i >= not_stack; i--)
4386 if (i >= not_stack + offset)
4387 if (!emit_push_insn (operand_subword_force (x, i, mode),
4388 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4389 0, args_addr,
4390 GEN_INT (args_offset + ((i - not_stack + skip)
4391 * UNITS_PER_WORD)),
4392 reg_parm_stack_space, alignment_pad, sibcall_p))
4393 return false;
4395 else
4397 rtx addr;
4398 rtx dest;
4400 /* Push padding now if padding above and stack grows down,
4401 or if padding below and stack grows up.
4402 But if space already allocated, this has already been done. */
4403 if (extra && args_addr == 0
4404 && where_pad != none && where_pad != stack_direction)
4405 anti_adjust_stack (GEN_INT (extra));
4407 #ifdef PUSH_ROUNDING
4408 if (args_addr == 0 && PUSH_ARGS)
4409 emit_single_push_insn (mode, x, type);
4410 else
4411 #endif
4413 if (CONST_INT_P (args_so_far))
4414 addr
4415 = memory_address (mode,
4416 plus_constant (Pmode, args_addr,
4417 INTVAL (args_so_far)));
4418 else
4419 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4420 args_so_far));
4421 dest = gen_rtx_MEM (mode, addr);
4423 /* We do *not* set_mem_attributes here, because incoming arguments
4424 may overlap with sibling call outgoing arguments and we cannot
4425 allow reordering of reads from function arguments with stores
4426 to outgoing arguments of sibling calls. We do, however, want
4427 to record the alignment of the stack slot. */
4428 /* ALIGN may well be better aligned than TYPE, e.g. due to
4429 PARM_BOUNDARY. Assume the caller isn't lying. */
4430 set_mem_align (dest, align);
4432 emit_move_insn (dest, x);
4436 /* Move the partial arguments into the registers and any overlapping
4437 values that we moved into the pseudos in tmp_regs. */
4438 if (partial > 0 && reg != 0)
4440 /* Handle calls that pass values in multiple non-contiguous locations.
4441 The Irix 6 ABI has examples of this. */
4442 if (GET_CODE (reg) == PARALLEL)
4443 emit_group_load (reg, x, type, -1);
4444 else
4446 gcc_assert (partial % UNITS_PER_WORD == 0);
4447 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4449 for (int i = 0; i < overlapping; i++)
4450 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4451 + nregs - overlapping + i),
4452 tmp_regs[i]);
4457 if (extra && args_addr == 0 && where_pad == stack_direction)
4458 anti_adjust_stack (GEN_INT (extra));
4460 if (alignment_pad && args_addr == 0)
4461 anti_adjust_stack (alignment_pad);
4463 return true;
4466 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4467 operations. */
4469 static rtx
4470 get_subtarget (rtx x)
4472 return (optimize
4473 || x == 0
4474 /* Only registers can be subtargets. */
4475 || !REG_P (x)
4476 /* Don't use hard regs to avoid extending their life. */
4477 || REGNO (x) < FIRST_PSEUDO_REGISTER
4478 ? 0 : x);
4481 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4482 FIELD is a bitfield. Returns true if the optimization was successful,
4483 and there's nothing else to do. */
4485 static bool
4486 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4487 unsigned HOST_WIDE_INT bitpos,
4488 unsigned HOST_WIDE_INT bitregion_start,
4489 unsigned HOST_WIDE_INT bitregion_end,
4490 machine_mode mode1, rtx str_rtx,
4491 tree to, tree src)
4493 machine_mode str_mode = GET_MODE (str_rtx);
4494 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4495 tree op0, op1;
4496 rtx value, result;
4497 optab binop;
4498 gimple srcstmt;
4499 enum tree_code code;
4501 if (mode1 != VOIDmode
4502 || bitsize >= BITS_PER_WORD
4503 || str_bitsize > BITS_PER_WORD
4504 || TREE_SIDE_EFFECTS (to)
4505 || TREE_THIS_VOLATILE (to))
4506 return false;
4508 STRIP_NOPS (src);
4509 if (TREE_CODE (src) != SSA_NAME)
4510 return false;
4511 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4512 return false;
4514 srcstmt = get_gimple_for_ssa_name (src);
4515 if (!srcstmt
4516 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4517 return false;
4519 code = gimple_assign_rhs_code (srcstmt);
4521 op0 = gimple_assign_rhs1 (srcstmt);
4523 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4524 to find its initialization. Hopefully the initialization will
4525 be from a bitfield load. */
4526 if (TREE_CODE (op0) == SSA_NAME)
4528 gimple op0stmt = get_gimple_for_ssa_name (op0);
4530 /* We want to eventually have OP0 be the same as TO, which
4531 should be a bitfield. */
4532 if (!op0stmt
4533 || !is_gimple_assign (op0stmt)
4534 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4535 return false;
4536 op0 = gimple_assign_rhs1 (op0stmt);
4539 op1 = gimple_assign_rhs2 (srcstmt);
4541 if (!operand_equal_p (to, op0, 0))
4542 return false;
4544 if (MEM_P (str_rtx))
4546 unsigned HOST_WIDE_INT offset1;
4548 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4549 str_mode = word_mode;
4550 str_mode = get_best_mode (bitsize, bitpos,
4551 bitregion_start, bitregion_end,
4552 MEM_ALIGN (str_rtx), str_mode, 0);
4553 if (str_mode == VOIDmode)
4554 return false;
4555 str_bitsize = GET_MODE_BITSIZE (str_mode);
4557 offset1 = bitpos;
4558 bitpos %= str_bitsize;
4559 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4560 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4562 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4563 return false;
4565 /* If the bit field covers the whole REG/MEM, store_field
4566 will likely generate better code. */
4567 if (bitsize >= str_bitsize)
4568 return false;
4570 /* We can't handle fields split across multiple entities. */
4571 if (bitpos + bitsize > str_bitsize)
4572 return false;
4574 if (BYTES_BIG_ENDIAN)
4575 bitpos = str_bitsize - bitpos - bitsize;
4577 switch (code)
4579 case PLUS_EXPR:
4580 case MINUS_EXPR:
4581 /* For now, just optimize the case of the topmost bitfield
4582 where we don't need to do any masking and also
4583 1 bit bitfields where xor can be used.
4584 We might win by one instruction for the other bitfields
4585 too if insv/extv instructions aren't used, so that
4586 can be added later. */
4587 if (bitpos + bitsize != str_bitsize
4588 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4589 break;
4591 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4592 value = convert_modes (str_mode,
4593 TYPE_MODE (TREE_TYPE (op1)), value,
4594 TYPE_UNSIGNED (TREE_TYPE (op1)));
4596 /* We may be accessing data outside the field, which means
4597 we can alias adjacent data. */
4598 if (MEM_P (str_rtx))
4600 str_rtx = shallow_copy_rtx (str_rtx);
4601 set_mem_alias_set (str_rtx, 0);
4602 set_mem_expr (str_rtx, 0);
4605 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4606 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4608 value = expand_and (str_mode, value, const1_rtx, NULL);
4609 binop = xor_optab;
4611 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4612 result = expand_binop (str_mode, binop, str_rtx,
4613 value, str_rtx, 1, OPTAB_WIDEN);
4614 if (result != str_rtx)
4615 emit_move_insn (str_rtx, result);
4616 return true;
4618 case BIT_IOR_EXPR:
4619 case BIT_XOR_EXPR:
4620 if (TREE_CODE (op1) != INTEGER_CST)
4621 break;
4622 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4623 value = convert_modes (str_mode,
4624 TYPE_MODE (TREE_TYPE (op1)), value,
4625 TYPE_UNSIGNED (TREE_TYPE (op1)));
4627 /* We may be accessing data outside the field, which means
4628 we can alias adjacent data. */
4629 if (MEM_P (str_rtx))
4631 str_rtx = shallow_copy_rtx (str_rtx);
4632 set_mem_alias_set (str_rtx, 0);
4633 set_mem_expr (str_rtx, 0);
4636 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4637 if (bitpos + bitsize != str_bitsize)
4639 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4640 str_mode);
4641 value = expand_and (str_mode, value, mask, NULL_RTX);
4643 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4644 result = expand_binop (str_mode, binop, str_rtx,
4645 value, str_rtx, 1, OPTAB_WIDEN);
4646 if (result != str_rtx)
4647 emit_move_insn (str_rtx, result);
4648 return true;
4650 default:
4651 break;
4654 return false;
4657 /* In the C++ memory model, consecutive bit fields in a structure are
4658 considered one memory location.
4660 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4661 returns the bit range of consecutive bits in which this COMPONENT_REF
4662 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4663 and *OFFSET may be adjusted in the process.
4665 If the access does not need to be restricted, 0 is returned in both
4666 *BITSTART and *BITEND. */
4668 static void
4669 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4670 unsigned HOST_WIDE_INT *bitend,
4671 tree exp,
4672 HOST_WIDE_INT *bitpos,
4673 tree *offset)
4675 HOST_WIDE_INT bitoffset;
4676 tree field, repr;
4678 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4680 field = TREE_OPERAND (exp, 1);
4681 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4682 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4683 need to limit the range we can access. */
4684 if (!repr)
4686 *bitstart = *bitend = 0;
4687 return;
4690 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4691 part of a larger bit field, then the representative does not serve any
4692 useful purpose. This can occur in Ada. */
4693 if (handled_component_p (TREE_OPERAND (exp, 0)))
4695 machine_mode rmode;
4696 HOST_WIDE_INT rbitsize, rbitpos;
4697 tree roffset;
4698 int unsignedp;
4699 int volatilep = 0;
4700 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4701 &roffset, &rmode, &unsignedp, &volatilep, false);
4702 if ((rbitpos % BITS_PER_UNIT) != 0)
4704 *bitstart = *bitend = 0;
4705 return;
4709 /* Compute the adjustment to bitpos from the offset of the field
4710 relative to the representative. DECL_FIELD_OFFSET of field and
4711 repr are the same by construction if they are not constants,
4712 see finish_bitfield_layout. */
4713 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4714 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4715 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4716 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4717 else
4718 bitoffset = 0;
4719 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4720 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4722 /* If the adjustment is larger than bitpos, we would have a negative bit
4723 position for the lower bound and this may wreak havoc later. Adjust
4724 offset and bitpos to make the lower bound non-negative in that case. */
4725 if (bitoffset > *bitpos)
4727 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4728 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4730 *bitpos += adjust;
4731 if (*offset == NULL_TREE)
4732 *offset = size_int (-adjust / BITS_PER_UNIT);
4733 else
4734 *offset
4735 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4736 *bitstart = 0;
4738 else
4739 *bitstart = *bitpos - bitoffset;
4741 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4744 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4745 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4746 DECL_RTL was not set yet, return NORTL. */
4748 static inline bool
4749 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4751 if (TREE_CODE (addr) != ADDR_EXPR)
4752 return false;
4754 tree base = TREE_OPERAND (addr, 0);
4756 if (!DECL_P (base)
4757 || TREE_ADDRESSABLE (base)
4758 || DECL_MODE (base) == BLKmode)
4759 return false;
4761 if (!DECL_RTL_SET_P (base))
4762 return nortl;
4764 return (!MEM_P (DECL_RTL (base)));
4767 /* Returns true if the MEM_REF REF refers to an object that does not
4768 reside in memory and has non-BLKmode. */
4770 static inline bool
4771 mem_ref_refers_to_non_mem_p (tree ref)
4773 tree base = TREE_OPERAND (ref, 0);
4774 return addr_expr_of_non_mem_decl_p_1 (base, false);
4777 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4778 is true, try generating a nontemporal store. */
4780 void
4781 expand_assignment (tree to, tree from, bool nontemporal)
4783 rtx to_rtx = 0;
4784 rtx result;
4785 machine_mode mode;
4786 unsigned int align;
4787 enum insn_code icode;
4789 /* Don't crash if the lhs of the assignment was erroneous. */
4790 if (TREE_CODE (to) == ERROR_MARK)
4792 expand_normal (from);
4793 return;
4796 /* Optimize away no-op moves without side-effects. */
4797 if (operand_equal_p (to, from, 0))
4798 return;
4800 /* Handle misaligned stores. */
4801 mode = TYPE_MODE (TREE_TYPE (to));
4802 if ((TREE_CODE (to) == MEM_REF
4803 || TREE_CODE (to) == TARGET_MEM_REF)
4804 && mode != BLKmode
4805 && !mem_ref_refers_to_non_mem_p (to)
4806 && ((align = get_object_alignment (to))
4807 < GET_MODE_ALIGNMENT (mode))
4808 && (((icode = optab_handler (movmisalign_optab, mode))
4809 != CODE_FOR_nothing)
4810 || SLOW_UNALIGNED_ACCESS (mode, align)))
4812 rtx reg, mem;
4814 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4815 reg = force_not_mem (reg);
4816 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4818 if (icode != CODE_FOR_nothing)
4820 struct expand_operand ops[2];
4822 create_fixed_operand (&ops[0], mem);
4823 create_input_operand (&ops[1], reg, mode);
4824 /* The movmisalign<mode> pattern cannot fail, else the assignment
4825 would silently be omitted. */
4826 expand_insn (icode, 2, ops);
4828 else
4829 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4830 return;
4833 /* Assignment of a structure component needs special treatment
4834 if the structure component's rtx is not simply a MEM.
4835 Assignment of an array element at a constant index, and assignment of
4836 an array element in an unaligned packed structure field, has the same
4837 problem. Same for (partially) storing into a non-memory object. */
4838 if (handled_component_p (to)
4839 || (TREE_CODE (to) == MEM_REF
4840 && mem_ref_refers_to_non_mem_p (to))
4841 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4843 machine_mode mode1;
4844 HOST_WIDE_INT bitsize, bitpos;
4845 unsigned HOST_WIDE_INT bitregion_start = 0;
4846 unsigned HOST_WIDE_INT bitregion_end = 0;
4847 tree offset;
4848 int unsignedp;
4849 int volatilep = 0;
4850 tree tem;
4852 push_temp_slots ();
4853 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4854 &unsignedp, &volatilep, true);
4856 /* Make sure bitpos is not negative, it can wreak havoc later. */
4857 if (bitpos < 0)
4859 gcc_assert (offset == NULL_TREE);
4860 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4861 ? 3 : exact_log2 (BITS_PER_UNIT)));
4862 bitpos &= BITS_PER_UNIT - 1;
4865 if (TREE_CODE (to) == COMPONENT_REF
4866 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4867 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4868 /* The C++ memory model naturally applies to byte-aligned fields.
4869 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4870 BITSIZE are not byte-aligned, there is no need to limit the range
4871 we can access. This can occur with packed structures in Ada. */
4872 else if (bitsize > 0
4873 && bitsize % BITS_PER_UNIT == 0
4874 && bitpos % BITS_PER_UNIT == 0)
4876 bitregion_start = bitpos;
4877 bitregion_end = bitpos + bitsize - 1;
4880 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4882 /* If the field has a mode, we want to access it in the
4883 field's mode, not the computed mode.
4884 If a MEM has VOIDmode (external with incomplete type),
4885 use BLKmode for it instead. */
4886 if (MEM_P (to_rtx))
4888 if (mode1 != VOIDmode)
4889 to_rtx = adjust_address (to_rtx, mode1, 0);
4890 else if (GET_MODE (to_rtx) == VOIDmode)
4891 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4894 if (offset != 0)
4896 machine_mode address_mode;
4897 rtx offset_rtx;
4899 if (!MEM_P (to_rtx))
4901 /* We can get constant negative offsets into arrays with broken
4902 user code. Translate this to a trap instead of ICEing. */
4903 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4904 expand_builtin_trap ();
4905 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4908 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4909 address_mode = get_address_mode (to_rtx);
4910 if (GET_MODE (offset_rtx) != address_mode)
4912 /* We cannot be sure that the RTL in offset_rtx is valid outside
4913 of a memory address context, so force it into a register
4914 before attempting to convert it to the desired mode. */
4915 offset_rtx = force_operand (offset_rtx, NULL_RTX);
4916 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4919 /* If we have an expression in OFFSET_RTX and a non-zero
4920 byte offset in BITPOS, adding the byte offset before the
4921 OFFSET_RTX results in better intermediate code, which makes
4922 later rtl optimization passes perform better.
4924 We prefer intermediate code like this:
4926 r124:DI=r123:DI+0x18
4927 [r124:DI]=r121:DI
4929 ... instead of ...
4931 r124:DI=r123:DI+0x10
4932 [r124:DI+0x8]=r121:DI
4934 This is only done for aligned data values, as these can
4935 be expected to result in single move instructions. */
4936 if (mode1 != VOIDmode
4937 && bitpos != 0
4938 && bitsize > 0
4939 && (bitpos % bitsize) == 0
4940 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4941 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4943 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4944 bitregion_start = 0;
4945 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4946 bitregion_end -= bitpos;
4947 bitpos = 0;
4950 to_rtx = offset_address (to_rtx, offset_rtx,
4951 highest_pow2_factor_for_target (to,
4952 offset));
4955 /* No action is needed if the target is not a memory and the field
4956 lies completely outside that target. This can occur if the source
4957 code contains an out-of-bounds access to a small array. */
4958 if (!MEM_P (to_rtx)
4959 && GET_MODE (to_rtx) != BLKmode
4960 && (unsigned HOST_WIDE_INT) bitpos
4961 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4963 expand_normal (from);
4964 result = NULL;
4966 /* Handle expand_expr of a complex value returning a CONCAT. */
4967 else if (GET_CODE (to_rtx) == CONCAT)
4969 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4970 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4971 && bitpos == 0
4972 && bitsize == mode_bitsize)
4973 result = store_expr (from, to_rtx, false, nontemporal);
4974 else if (bitsize == mode_bitsize / 2
4975 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4976 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4977 nontemporal);
4978 else if (bitpos + bitsize <= mode_bitsize / 2)
4979 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4980 bitregion_start, bitregion_end,
4981 mode1, from,
4982 get_alias_set (to), nontemporal);
4983 else if (bitpos >= mode_bitsize / 2)
4984 result = store_field (XEXP (to_rtx, 1), bitsize,
4985 bitpos - mode_bitsize / 2,
4986 bitregion_start, bitregion_end,
4987 mode1, from,
4988 get_alias_set (to), nontemporal);
4989 else if (bitpos == 0 && bitsize == mode_bitsize)
4991 rtx from_rtx;
4992 result = expand_normal (from);
4993 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4994 TYPE_MODE (TREE_TYPE (from)), 0);
4995 emit_move_insn (XEXP (to_rtx, 0),
4996 read_complex_part (from_rtx, false));
4997 emit_move_insn (XEXP (to_rtx, 1),
4998 read_complex_part (from_rtx, true));
5000 else
5002 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5003 GET_MODE_SIZE (GET_MODE (to_rtx)));
5004 write_complex_part (temp, XEXP (to_rtx, 0), false);
5005 write_complex_part (temp, XEXP (to_rtx, 1), true);
5006 result = store_field (temp, bitsize, bitpos,
5007 bitregion_start, bitregion_end,
5008 mode1, from,
5009 get_alias_set (to), nontemporal);
5010 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5011 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5014 else
5016 if (MEM_P (to_rtx))
5018 /* If the field is at offset zero, we could have been given the
5019 DECL_RTX of the parent struct. Don't munge it. */
5020 to_rtx = shallow_copy_rtx (to_rtx);
5021 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5022 if (volatilep)
5023 MEM_VOLATILE_P (to_rtx) = 1;
5026 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5027 bitregion_start, bitregion_end,
5028 mode1,
5029 to_rtx, to, from))
5030 result = NULL;
5031 else
5032 result = store_field (to_rtx, bitsize, bitpos,
5033 bitregion_start, bitregion_end,
5034 mode1, from,
5035 get_alias_set (to), nontemporal);
5038 if (result)
5039 preserve_temp_slots (result);
5040 pop_temp_slots ();
5041 return;
5044 /* If the rhs is a function call and its value is not an aggregate,
5045 call the function before we start to compute the lhs.
5046 This is needed for correct code for cases such as
5047 val = setjmp (buf) on machines where reference to val
5048 requires loading up part of an address in a separate insn.
5050 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5051 since it might be a promoted variable where the zero- or sign- extension
5052 needs to be done. Handling this in the normal way is safe because no
5053 computation is done before the call. The same is true for SSA names. */
5054 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5055 && COMPLETE_TYPE_P (TREE_TYPE (from))
5056 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5057 && ! (((TREE_CODE (to) == VAR_DECL
5058 || TREE_CODE (to) == PARM_DECL
5059 || TREE_CODE (to) == RESULT_DECL)
5060 && REG_P (DECL_RTL (to)))
5061 || TREE_CODE (to) == SSA_NAME))
5063 rtx value;
5064 rtx bounds;
5066 push_temp_slots ();
5067 value = expand_normal (from);
5069 /* Split value and bounds to store them separately. */
5070 chkp_split_slot (value, &value, &bounds);
5072 if (to_rtx == 0)
5073 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5075 /* Handle calls that return values in multiple non-contiguous locations.
5076 The Irix 6 ABI has examples of this. */
5077 if (GET_CODE (to_rtx) == PARALLEL)
5079 if (GET_CODE (value) == PARALLEL)
5080 emit_group_move (to_rtx, value);
5081 else
5082 emit_group_load (to_rtx, value, TREE_TYPE (from),
5083 int_size_in_bytes (TREE_TYPE (from)));
5085 else if (GET_CODE (value) == PARALLEL)
5086 emit_group_store (to_rtx, value, TREE_TYPE (from),
5087 int_size_in_bytes (TREE_TYPE (from)));
5088 else if (GET_MODE (to_rtx) == BLKmode)
5090 /* Handle calls that return BLKmode values in registers. */
5091 if (REG_P (value))
5092 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5093 else
5094 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5096 else
5098 if (POINTER_TYPE_P (TREE_TYPE (to)))
5099 value = convert_memory_address_addr_space
5100 (GET_MODE (to_rtx), value,
5101 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5103 emit_move_insn (to_rtx, value);
5106 /* Store bounds if required. */
5107 if (bounds
5108 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5110 gcc_assert (MEM_P (to_rtx));
5111 chkp_emit_bounds_store (bounds, value, to_rtx);
5114 preserve_temp_slots (to_rtx);
5115 pop_temp_slots ();
5116 return;
5119 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5120 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5122 /* Don't move directly into a return register. */
5123 if (TREE_CODE (to) == RESULT_DECL
5124 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5126 rtx temp;
5128 push_temp_slots ();
5130 /* If the source is itself a return value, it still is in a pseudo at
5131 this point so we can move it back to the return register directly. */
5132 if (REG_P (to_rtx)
5133 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5134 && TREE_CODE (from) != CALL_EXPR)
5135 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5136 else
5137 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5139 /* Handle calls that return values in multiple non-contiguous locations.
5140 The Irix 6 ABI has examples of this. */
5141 if (GET_CODE (to_rtx) == PARALLEL)
5143 if (GET_CODE (temp) == PARALLEL)
5144 emit_group_move (to_rtx, temp);
5145 else
5146 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5147 int_size_in_bytes (TREE_TYPE (from)));
5149 else if (temp)
5150 emit_move_insn (to_rtx, temp);
5152 preserve_temp_slots (to_rtx);
5153 pop_temp_slots ();
5154 return;
5157 /* In case we are returning the contents of an object which overlaps
5158 the place the value is being stored, use a safe function when copying
5159 a value through a pointer into a structure value return block. */
5160 if (TREE_CODE (to) == RESULT_DECL
5161 && TREE_CODE (from) == INDIRECT_REF
5162 && ADDR_SPACE_GENERIC_P
5163 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5164 && refs_may_alias_p (to, from)
5165 && cfun->returns_struct
5166 && !cfun->returns_pcc_struct)
5168 rtx from_rtx, size;
5170 push_temp_slots ();
5171 size = expr_size (from);
5172 from_rtx = expand_normal (from);
5174 emit_library_call (memmove_libfunc, LCT_NORMAL,
5175 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5176 XEXP (from_rtx, 0), Pmode,
5177 convert_to_mode (TYPE_MODE (sizetype),
5178 size, TYPE_UNSIGNED (sizetype)),
5179 TYPE_MODE (sizetype));
5181 preserve_temp_slots (to_rtx);
5182 pop_temp_slots ();
5183 return;
5186 /* Compute FROM and store the value in the rtx we got. */
5188 push_temp_slots ();
5189 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5190 preserve_temp_slots (result);
5191 pop_temp_slots ();
5192 return;
5195 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5196 succeeded, false otherwise. */
5198 bool
5199 emit_storent_insn (rtx to, rtx from)
5201 struct expand_operand ops[2];
5202 machine_mode mode = GET_MODE (to);
5203 enum insn_code code = optab_handler (storent_optab, mode);
5205 if (code == CODE_FOR_nothing)
5206 return false;
5208 create_fixed_operand (&ops[0], to);
5209 create_input_operand (&ops[1], from, mode);
5210 return maybe_expand_insn (code, 2, ops);
5213 /* Generate code for computing expression EXP,
5214 and storing the value into TARGET.
5216 If the mode is BLKmode then we may return TARGET itself.
5217 It turns out that in BLKmode it doesn't cause a problem.
5218 because C has no operators that could combine two different
5219 assignments into the same BLKmode object with different values
5220 with no sequence point. Will other languages need this to
5221 be more thorough?
5223 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5224 stack, and block moves may need to be treated specially.
5226 If NONTEMPORAL is true, try using a nontemporal store instruction.
5228 If BTARGET is not NULL then computed bounds of EXP are
5229 associated with BTARGET. */
5232 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5233 bool nontemporal, tree btarget)
5235 rtx temp;
5236 rtx alt_rtl = NULL_RTX;
5237 location_t loc = curr_insn_location ();
5239 if (VOID_TYPE_P (TREE_TYPE (exp)))
5241 /* C++ can generate ?: expressions with a throw expression in one
5242 branch and an rvalue in the other. Here, we resolve attempts to
5243 store the throw expression's nonexistent result. */
5244 gcc_assert (!call_param_p);
5245 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5246 return NULL_RTX;
5248 if (TREE_CODE (exp) == COMPOUND_EXPR)
5250 /* Perform first part of compound expression, then assign from second
5251 part. */
5252 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5253 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5254 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5255 call_param_p, nontemporal, btarget);
5257 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5259 /* For conditional expression, get safe form of the target. Then
5260 test the condition, doing the appropriate assignment on either
5261 side. This avoids the creation of unnecessary temporaries.
5262 For non-BLKmode, it is more efficient not to do this. */
5264 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5266 do_pending_stack_adjust ();
5267 NO_DEFER_POP;
5268 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5269 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5270 nontemporal, btarget);
5271 emit_jump_insn (targetm.gen_jump (lab2));
5272 emit_barrier ();
5273 emit_label (lab1);
5274 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5275 nontemporal, btarget);
5276 emit_label (lab2);
5277 OK_DEFER_POP;
5279 return NULL_RTX;
5281 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5282 /* If this is a scalar in a register that is stored in a wider mode
5283 than the declared mode, compute the result into its declared mode
5284 and then convert to the wider mode. Our value is the computed
5285 expression. */
5287 rtx inner_target = 0;
5289 /* We can do the conversion inside EXP, which will often result
5290 in some optimizations. Do the conversion in two steps: first
5291 change the signedness, if needed, then the extend. But don't
5292 do this if the type of EXP is a subtype of something else
5293 since then the conversion might involve more than just
5294 converting modes. */
5295 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5296 && TREE_TYPE (TREE_TYPE (exp)) == 0
5297 && GET_MODE_PRECISION (GET_MODE (target))
5298 == TYPE_PRECISION (TREE_TYPE (exp)))
5300 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5301 TYPE_UNSIGNED (TREE_TYPE (exp))))
5303 /* Some types, e.g. Fortran's logical*4, won't have a signed
5304 version, so use the mode instead. */
5305 tree ntype
5306 = (signed_or_unsigned_type_for
5307 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5308 if (ntype == NULL)
5309 ntype = lang_hooks.types.type_for_mode
5310 (TYPE_MODE (TREE_TYPE (exp)),
5311 SUBREG_PROMOTED_SIGN (target));
5313 exp = fold_convert_loc (loc, ntype, exp);
5316 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5317 (GET_MODE (SUBREG_REG (target)),
5318 SUBREG_PROMOTED_SIGN (target)),
5319 exp);
5321 inner_target = SUBREG_REG (target);
5324 temp = expand_expr (exp, inner_target, VOIDmode,
5325 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5327 /* Handle bounds returned by call. */
5328 if (TREE_CODE (exp) == CALL_EXPR)
5330 rtx bounds;
5331 chkp_split_slot (temp, &temp, &bounds);
5332 if (bounds && btarget)
5334 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5335 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5336 chkp_set_rtl_bounds (btarget, tmp);
5340 /* If TEMP is a VOIDmode constant, use convert_modes to make
5341 sure that we properly convert it. */
5342 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5344 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5345 temp, SUBREG_PROMOTED_SIGN (target));
5346 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5347 GET_MODE (target), temp,
5348 SUBREG_PROMOTED_SIGN (target));
5351 convert_move (SUBREG_REG (target), temp,
5352 SUBREG_PROMOTED_SIGN (target));
5354 return NULL_RTX;
5356 else if ((TREE_CODE (exp) == STRING_CST
5357 || (TREE_CODE (exp) == MEM_REF
5358 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5359 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5360 == STRING_CST
5361 && integer_zerop (TREE_OPERAND (exp, 1))))
5362 && !nontemporal && !call_param_p
5363 && MEM_P (target))
5365 /* Optimize initialization of an array with a STRING_CST. */
5366 HOST_WIDE_INT exp_len, str_copy_len;
5367 rtx dest_mem;
5368 tree str = TREE_CODE (exp) == STRING_CST
5369 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5371 exp_len = int_expr_size (exp);
5372 if (exp_len <= 0)
5373 goto normal_expr;
5375 if (TREE_STRING_LENGTH (str) <= 0)
5376 goto normal_expr;
5378 str_copy_len = strlen (TREE_STRING_POINTER (str));
5379 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5380 goto normal_expr;
5382 str_copy_len = TREE_STRING_LENGTH (str);
5383 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5384 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5386 str_copy_len += STORE_MAX_PIECES - 1;
5387 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5389 str_copy_len = MIN (str_copy_len, exp_len);
5390 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5391 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5392 MEM_ALIGN (target), false))
5393 goto normal_expr;
5395 dest_mem = target;
5397 dest_mem = store_by_pieces (dest_mem,
5398 str_copy_len, builtin_strncpy_read_str,
5399 CONST_CAST (char *,
5400 TREE_STRING_POINTER (str)),
5401 MEM_ALIGN (target), false,
5402 exp_len > str_copy_len ? 1 : 0);
5403 if (exp_len > str_copy_len)
5404 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5405 GEN_INT (exp_len - str_copy_len),
5406 BLOCK_OP_NORMAL);
5407 return NULL_RTX;
5409 else
5411 rtx tmp_target;
5413 normal_expr:
5414 /* If we want to use a nontemporal store, force the value to
5415 register first. */
5416 tmp_target = nontemporal ? NULL_RTX : target;
5417 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5418 (call_param_p
5419 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5420 &alt_rtl, false);
5422 /* Handle bounds returned by call. */
5423 if (TREE_CODE (exp) == CALL_EXPR)
5425 rtx bounds;
5426 chkp_split_slot (temp, &temp, &bounds);
5427 if (bounds && btarget)
5429 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5430 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5431 chkp_set_rtl_bounds (btarget, tmp);
5436 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5437 the same as that of TARGET, adjust the constant. This is needed, for
5438 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5439 only a word-sized value. */
5440 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5441 && TREE_CODE (exp) != ERROR_MARK
5442 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5443 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5444 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5446 /* If value was not generated in the target, store it there.
5447 Convert the value to TARGET's type first if necessary and emit the
5448 pending incrementations that have been queued when expanding EXP.
5449 Note that we cannot emit the whole queue blindly because this will
5450 effectively disable the POST_INC optimization later.
5452 If TEMP and TARGET compare equal according to rtx_equal_p, but
5453 one or both of them are volatile memory refs, we have to distinguish
5454 two cases:
5455 - expand_expr has used TARGET. In this case, we must not generate
5456 another copy. This can be detected by TARGET being equal according
5457 to == .
5458 - expand_expr has not used TARGET - that means that the source just
5459 happens to have the same RTX form. Since temp will have been created
5460 by expand_expr, it will compare unequal according to == .
5461 We must generate a copy in this case, to reach the correct number
5462 of volatile memory references. */
5464 if ((! rtx_equal_p (temp, target)
5465 || (temp != target && (side_effects_p (temp)
5466 || side_effects_p (target))))
5467 && TREE_CODE (exp) != ERROR_MARK
5468 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5469 but TARGET is not valid memory reference, TEMP will differ
5470 from TARGET although it is really the same location. */
5471 && !(alt_rtl
5472 && rtx_equal_p (alt_rtl, target)
5473 && !side_effects_p (alt_rtl)
5474 && !side_effects_p (target))
5475 /* If there's nothing to copy, don't bother. Don't call
5476 expr_size unless necessary, because some front-ends (C++)
5477 expr_size-hook must not be given objects that are not
5478 supposed to be bit-copied or bit-initialized. */
5479 && expr_size (exp) != const0_rtx)
5481 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5483 if (GET_MODE (target) == BLKmode)
5485 /* Handle calls that return BLKmode values in registers. */
5486 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5487 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5488 else
5489 store_bit_field (target,
5490 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5491 0, 0, 0, GET_MODE (temp), temp);
5493 else
5494 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5497 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5499 /* Handle copying a string constant into an array. The string
5500 constant may be shorter than the array. So copy just the string's
5501 actual length, and clear the rest. First get the size of the data
5502 type of the string, which is actually the size of the target. */
5503 rtx size = expr_size (exp);
5505 if (CONST_INT_P (size)
5506 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5507 emit_block_move (target, temp, size,
5508 (call_param_p
5509 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5510 else
5512 machine_mode pointer_mode
5513 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5514 machine_mode address_mode = get_address_mode (target);
5516 /* Compute the size of the data to copy from the string. */
5517 tree copy_size
5518 = size_binop_loc (loc, MIN_EXPR,
5519 make_tree (sizetype, size),
5520 size_int (TREE_STRING_LENGTH (exp)));
5521 rtx copy_size_rtx
5522 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5523 (call_param_p
5524 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5525 rtx_code_label *label = 0;
5527 /* Copy that much. */
5528 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5529 TYPE_UNSIGNED (sizetype));
5530 emit_block_move (target, temp, copy_size_rtx,
5531 (call_param_p
5532 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5534 /* Figure out how much is left in TARGET that we have to clear.
5535 Do all calculations in pointer_mode. */
5536 if (CONST_INT_P (copy_size_rtx))
5538 size = plus_constant (address_mode, size,
5539 -INTVAL (copy_size_rtx));
5540 target = adjust_address (target, BLKmode,
5541 INTVAL (copy_size_rtx));
5543 else
5545 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5546 copy_size_rtx, NULL_RTX, 0,
5547 OPTAB_LIB_WIDEN);
5549 if (GET_MODE (copy_size_rtx) != address_mode)
5550 copy_size_rtx = convert_to_mode (address_mode,
5551 copy_size_rtx,
5552 TYPE_UNSIGNED (sizetype));
5554 target = offset_address (target, copy_size_rtx,
5555 highest_pow2_factor (copy_size));
5556 label = gen_label_rtx ();
5557 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5558 GET_MODE (size), 0, label);
5561 if (size != const0_rtx)
5562 clear_storage (target, size, BLOCK_OP_NORMAL);
5564 if (label)
5565 emit_label (label);
5568 /* Handle calls that return values in multiple non-contiguous locations.
5569 The Irix 6 ABI has examples of this. */
5570 else if (GET_CODE (target) == PARALLEL)
5572 if (GET_CODE (temp) == PARALLEL)
5573 emit_group_move (target, temp);
5574 else
5575 emit_group_load (target, temp, TREE_TYPE (exp),
5576 int_size_in_bytes (TREE_TYPE (exp)));
5578 else if (GET_CODE (temp) == PARALLEL)
5579 emit_group_store (target, temp, TREE_TYPE (exp),
5580 int_size_in_bytes (TREE_TYPE (exp)));
5581 else if (GET_MODE (temp) == BLKmode)
5582 emit_block_move (target, temp, expr_size (exp),
5583 (call_param_p
5584 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5585 /* If we emit a nontemporal store, there is nothing else to do. */
5586 else if (nontemporal && emit_storent_insn (target, temp))
5588 else
5590 temp = force_operand (temp, target);
5591 if (temp != target)
5592 emit_move_insn (target, temp);
5596 return NULL_RTX;
5599 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5601 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5603 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5606 /* Return true if field F of structure TYPE is a flexible array. */
5608 static bool
5609 flexible_array_member_p (const_tree f, const_tree type)
5611 const_tree tf;
5613 tf = TREE_TYPE (f);
5614 return (DECL_CHAIN (f) == NULL
5615 && TREE_CODE (tf) == ARRAY_TYPE
5616 && TYPE_DOMAIN (tf)
5617 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5618 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5619 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5620 && int_size_in_bytes (type) >= 0);
5623 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5624 must have in order for it to completely initialize a value of type TYPE.
5625 Return -1 if the number isn't known.
5627 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5629 static HOST_WIDE_INT
5630 count_type_elements (const_tree type, bool for_ctor_p)
5632 switch (TREE_CODE (type))
5634 case ARRAY_TYPE:
5636 tree nelts;
5638 nelts = array_type_nelts (type);
5639 if (nelts && tree_fits_uhwi_p (nelts))
5641 unsigned HOST_WIDE_INT n;
5643 n = tree_to_uhwi (nelts) + 1;
5644 if (n == 0 || for_ctor_p)
5645 return n;
5646 else
5647 return n * count_type_elements (TREE_TYPE (type), false);
5649 return for_ctor_p ? -1 : 1;
5652 case RECORD_TYPE:
5654 unsigned HOST_WIDE_INT n;
5655 tree f;
5657 n = 0;
5658 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5659 if (TREE_CODE (f) == FIELD_DECL)
5661 if (!for_ctor_p)
5662 n += count_type_elements (TREE_TYPE (f), false);
5663 else if (!flexible_array_member_p (f, type))
5664 /* Don't count flexible arrays, which are not supposed
5665 to be initialized. */
5666 n += 1;
5669 return n;
5672 case UNION_TYPE:
5673 case QUAL_UNION_TYPE:
5675 tree f;
5676 HOST_WIDE_INT n, m;
5678 gcc_assert (!for_ctor_p);
5679 /* Estimate the number of scalars in each field and pick the
5680 maximum. Other estimates would do instead; the idea is simply
5681 to make sure that the estimate is not sensitive to the ordering
5682 of the fields. */
5683 n = 1;
5684 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5685 if (TREE_CODE (f) == FIELD_DECL)
5687 m = count_type_elements (TREE_TYPE (f), false);
5688 /* If the field doesn't span the whole union, add an extra
5689 scalar for the rest. */
5690 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5691 TYPE_SIZE (type)) != 1)
5692 m++;
5693 if (n < m)
5694 n = m;
5696 return n;
5699 case COMPLEX_TYPE:
5700 return 2;
5702 case VECTOR_TYPE:
5703 return TYPE_VECTOR_SUBPARTS (type);
5705 case INTEGER_TYPE:
5706 case REAL_TYPE:
5707 case FIXED_POINT_TYPE:
5708 case ENUMERAL_TYPE:
5709 case BOOLEAN_TYPE:
5710 case POINTER_TYPE:
5711 case OFFSET_TYPE:
5712 case REFERENCE_TYPE:
5713 case NULLPTR_TYPE:
5714 return 1;
5716 case ERROR_MARK:
5717 return 0;
5719 case VOID_TYPE:
5720 case METHOD_TYPE:
5721 case FUNCTION_TYPE:
5722 case LANG_TYPE:
5723 default:
5724 gcc_unreachable ();
5728 /* Helper for categorize_ctor_elements. Identical interface. */
5730 static bool
5731 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5732 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5734 unsigned HOST_WIDE_INT idx;
5735 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5736 tree value, purpose, elt_type;
5738 /* Whether CTOR is a valid constant initializer, in accordance with what
5739 initializer_constant_valid_p does. If inferred from the constructor
5740 elements, true until proven otherwise. */
5741 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5742 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5744 nz_elts = 0;
5745 init_elts = 0;
5746 num_fields = 0;
5747 elt_type = NULL_TREE;
5749 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5751 HOST_WIDE_INT mult = 1;
5753 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5755 tree lo_index = TREE_OPERAND (purpose, 0);
5756 tree hi_index = TREE_OPERAND (purpose, 1);
5758 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5759 mult = (tree_to_uhwi (hi_index)
5760 - tree_to_uhwi (lo_index) + 1);
5762 num_fields += mult;
5763 elt_type = TREE_TYPE (value);
5765 switch (TREE_CODE (value))
5767 case CONSTRUCTOR:
5769 HOST_WIDE_INT nz = 0, ic = 0;
5771 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5772 p_complete);
5774 nz_elts += mult * nz;
5775 init_elts += mult * ic;
5777 if (const_from_elts_p && const_p)
5778 const_p = const_elt_p;
5780 break;
5782 case INTEGER_CST:
5783 case REAL_CST:
5784 case FIXED_CST:
5785 if (!initializer_zerop (value))
5786 nz_elts += mult;
5787 init_elts += mult;
5788 break;
5790 case STRING_CST:
5791 nz_elts += mult * TREE_STRING_LENGTH (value);
5792 init_elts += mult * TREE_STRING_LENGTH (value);
5793 break;
5795 case COMPLEX_CST:
5796 if (!initializer_zerop (TREE_REALPART (value)))
5797 nz_elts += mult;
5798 if (!initializer_zerop (TREE_IMAGPART (value)))
5799 nz_elts += mult;
5800 init_elts += mult;
5801 break;
5803 case VECTOR_CST:
5805 unsigned i;
5806 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5808 tree v = VECTOR_CST_ELT (value, i);
5809 if (!initializer_zerop (v))
5810 nz_elts += mult;
5811 init_elts += mult;
5814 break;
5816 default:
5818 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5819 nz_elts += mult * tc;
5820 init_elts += mult * tc;
5822 if (const_from_elts_p && const_p)
5823 const_p = initializer_constant_valid_p (value, elt_type)
5824 != NULL_TREE;
5826 break;
5830 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5831 num_fields, elt_type))
5832 *p_complete = false;
5834 *p_nz_elts += nz_elts;
5835 *p_init_elts += init_elts;
5837 return const_p;
5840 /* Examine CTOR to discover:
5841 * how many scalar fields are set to nonzero values,
5842 and place it in *P_NZ_ELTS;
5843 * how many scalar fields in total are in CTOR,
5844 and place it in *P_ELT_COUNT.
5845 * whether the constructor is complete -- in the sense that every
5846 meaningful byte is explicitly given a value --
5847 and place it in *P_COMPLETE.
5849 Return whether or not CTOR is a valid static constant initializer, the same
5850 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5852 bool
5853 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5854 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5856 *p_nz_elts = 0;
5857 *p_init_elts = 0;
5858 *p_complete = true;
5860 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5863 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5864 of which had type LAST_TYPE. Each element was itself a complete
5865 initializer, in the sense that every meaningful byte was explicitly
5866 given a value. Return true if the same is true for the constructor
5867 as a whole. */
5869 bool
5870 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5871 const_tree last_type)
5873 if (TREE_CODE (type) == UNION_TYPE
5874 || TREE_CODE (type) == QUAL_UNION_TYPE)
5876 if (num_elts == 0)
5877 return false;
5879 gcc_assert (num_elts == 1 && last_type);
5881 /* ??? We could look at each element of the union, and find the
5882 largest element. Which would avoid comparing the size of the
5883 initialized element against any tail padding in the union.
5884 Doesn't seem worth the effort... */
5885 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5888 return count_type_elements (type, true) == num_elts;
5891 /* Return 1 if EXP contains mostly (3/4) zeros. */
5893 static int
5894 mostly_zeros_p (const_tree exp)
5896 if (TREE_CODE (exp) == CONSTRUCTOR)
5898 HOST_WIDE_INT nz_elts, init_elts;
5899 bool complete_p;
5901 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5902 return !complete_p || nz_elts < init_elts / 4;
5905 return initializer_zerop (exp);
5908 /* Return 1 if EXP contains all zeros. */
5910 static int
5911 all_zeros_p (const_tree exp)
5913 if (TREE_CODE (exp) == CONSTRUCTOR)
5915 HOST_WIDE_INT nz_elts, init_elts;
5916 bool complete_p;
5918 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5919 return nz_elts == 0;
5922 return initializer_zerop (exp);
5925 /* Helper function for store_constructor.
5926 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5927 CLEARED is as for store_constructor.
5928 ALIAS_SET is the alias set to use for any stores.
5930 This provides a recursive shortcut back to store_constructor when it isn't
5931 necessary to go through store_field. This is so that we can pass through
5932 the cleared field to let store_constructor know that we may not have to
5933 clear a substructure if the outer structure has already been cleared. */
5935 static void
5936 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5937 HOST_WIDE_INT bitpos, machine_mode mode,
5938 tree exp, int cleared, alias_set_type alias_set)
5940 if (TREE_CODE (exp) == CONSTRUCTOR
5941 /* We can only call store_constructor recursively if the size and
5942 bit position are on a byte boundary. */
5943 && bitpos % BITS_PER_UNIT == 0
5944 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5945 /* If we have a nonzero bitpos for a register target, then we just
5946 let store_field do the bitfield handling. This is unlikely to
5947 generate unnecessary clear instructions anyways. */
5948 && (bitpos == 0 || MEM_P (target)))
5950 if (MEM_P (target))
5951 target
5952 = adjust_address (target,
5953 GET_MODE (target) == BLKmode
5954 || 0 != (bitpos
5955 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5956 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5959 /* Update the alias set, if required. */
5960 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5961 && MEM_ALIAS_SET (target) != 0)
5963 target = copy_rtx (target);
5964 set_mem_alias_set (target, alias_set);
5967 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5969 else
5970 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5974 /* Returns the number of FIELD_DECLs in TYPE. */
5976 static int
5977 fields_length (const_tree type)
5979 tree t = TYPE_FIELDS (type);
5980 int count = 0;
5982 for (; t; t = DECL_CHAIN (t))
5983 if (TREE_CODE (t) == FIELD_DECL)
5984 ++count;
5986 return count;
5990 /* Store the value of constructor EXP into the rtx TARGET.
5991 TARGET is either a REG or a MEM; we know it cannot conflict, since
5992 safe_from_p has been called.
5993 CLEARED is true if TARGET is known to have been zero'd.
5994 SIZE is the number of bytes of TARGET we are allowed to modify: this
5995 may not be the same as the size of EXP if we are assigning to a field
5996 which has been packed to exclude padding bits. */
5998 static void
5999 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
6001 tree type = TREE_TYPE (exp);
6002 #ifdef WORD_REGISTER_OPERATIONS
6003 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6004 #endif
6006 switch (TREE_CODE (type))
6008 case RECORD_TYPE:
6009 case UNION_TYPE:
6010 case QUAL_UNION_TYPE:
6012 unsigned HOST_WIDE_INT idx;
6013 tree field, value;
6015 /* If size is zero or the target is already cleared, do nothing. */
6016 if (size == 0 || cleared)
6017 cleared = 1;
6018 /* We either clear the aggregate or indicate the value is dead. */
6019 else if ((TREE_CODE (type) == UNION_TYPE
6020 || TREE_CODE (type) == QUAL_UNION_TYPE)
6021 && ! CONSTRUCTOR_ELTS (exp))
6022 /* If the constructor is empty, clear the union. */
6024 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6025 cleared = 1;
6028 /* If we are building a static constructor into a register,
6029 set the initial value as zero so we can fold the value into
6030 a constant. But if more than one register is involved,
6031 this probably loses. */
6032 else if (REG_P (target) && TREE_STATIC (exp)
6033 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6035 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6036 cleared = 1;
6039 /* If the constructor has fewer fields than the structure or
6040 if we are initializing the structure to mostly zeros, clear
6041 the whole structure first. Don't do this if TARGET is a
6042 register whose mode size isn't equal to SIZE since
6043 clear_storage can't handle this case. */
6044 else if (size > 0
6045 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
6046 != fields_length (type))
6047 || mostly_zeros_p (exp))
6048 && (!REG_P (target)
6049 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6050 == size)))
6052 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6053 cleared = 1;
6056 if (REG_P (target) && !cleared)
6057 emit_clobber (target);
6059 /* Store each element of the constructor into the
6060 corresponding field of TARGET. */
6061 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6063 machine_mode mode;
6064 HOST_WIDE_INT bitsize;
6065 HOST_WIDE_INT bitpos = 0;
6066 tree offset;
6067 rtx to_rtx = target;
6069 /* Just ignore missing fields. We cleared the whole
6070 structure, above, if any fields are missing. */
6071 if (field == 0)
6072 continue;
6074 if (cleared && initializer_zerop (value))
6075 continue;
6077 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6078 bitsize = tree_to_uhwi (DECL_SIZE (field));
6079 else
6080 bitsize = -1;
6082 mode = DECL_MODE (field);
6083 if (DECL_BIT_FIELD (field))
6084 mode = VOIDmode;
6086 offset = DECL_FIELD_OFFSET (field);
6087 if (tree_fits_shwi_p (offset)
6088 && tree_fits_shwi_p (bit_position (field)))
6090 bitpos = int_bit_position (field);
6091 offset = 0;
6093 else
6094 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6096 if (offset)
6098 machine_mode address_mode;
6099 rtx offset_rtx;
6101 offset
6102 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6103 make_tree (TREE_TYPE (exp),
6104 target));
6106 offset_rtx = expand_normal (offset);
6107 gcc_assert (MEM_P (to_rtx));
6109 address_mode = get_address_mode (to_rtx);
6110 if (GET_MODE (offset_rtx) != address_mode)
6111 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6113 to_rtx = offset_address (to_rtx, offset_rtx,
6114 highest_pow2_factor (offset));
6117 #ifdef WORD_REGISTER_OPERATIONS
6118 /* If this initializes a field that is smaller than a
6119 word, at the start of a word, try to widen it to a full
6120 word. This special case allows us to output C++ member
6121 function initializations in a form that the optimizers
6122 can understand. */
6123 if (REG_P (target)
6124 && bitsize < BITS_PER_WORD
6125 && bitpos % BITS_PER_WORD == 0
6126 && GET_MODE_CLASS (mode) == MODE_INT
6127 && TREE_CODE (value) == INTEGER_CST
6128 && exp_size >= 0
6129 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6131 tree type = TREE_TYPE (value);
6133 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6135 type = lang_hooks.types.type_for_mode
6136 (word_mode, TYPE_UNSIGNED (type));
6137 value = fold_convert (type, value);
6140 if (BYTES_BIG_ENDIAN)
6141 value
6142 = fold_build2 (LSHIFT_EXPR, type, value,
6143 build_int_cst (type,
6144 BITS_PER_WORD - bitsize));
6145 bitsize = BITS_PER_WORD;
6146 mode = word_mode;
6148 #endif
6150 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6151 && DECL_NONADDRESSABLE_P (field))
6153 to_rtx = copy_rtx (to_rtx);
6154 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6157 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6158 value, cleared,
6159 get_alias_set (TREE_TYPE (field)));
6161 break;
6163 case ARRAY_TYPE:
6165 tree value, index;
6166 unsigned HOST_WIDE_INT i;
6167 int need_to_clear;
6168 tree domain;
6169 tree elttype = TREE_TYPE (type);
6170 int const_bounds_p;
6171 HOST_WIDE_INT minelt = 0;
6172 HOST_WIDE_INT maxelt = 0;
6174 domain = TYPE_DOMAIN (type);
6175 const_bounds_p = (TYPE_MIN_VALUE (domain)
6176 && TYPE_MAX_VALUE (domain)
6177 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6178 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6180 /* If we have constant bounds for the range of the type, get them. */
6181 if (const_bounds_p)
6183 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6184 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6187 /* If the constructor has fewer elements than the array, clear
6188 the whole array first. Similarly if this is static
6189 constructor of a non-BLKmode object. */
6190 if (cleared)
6191 need_to_clear = 0;
6192 else if (REG_P (target) && TREE_STATIC (exp))
6193 need_to_clear = 1;
6194 else
6196 unsigned HOST_WIDE_INT idx;
6197 tree index, value;
6198 HOST_WIDE_INT count = 0, zero_count = 0;
6199 need_to_clear = ! const_bounds_p;
6201 /* This loop is a more accurate version of the loop in
6202 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6203 is also needed to check for missing elements. */
6204 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6206 HOST_WIDE_INT this_node_count;
6208 if (need_to_clear)
6209 break;
6211 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6213 tree lo_index = TREE_OPERAND (index, 0);
6214 tree hi_index = TREE_OPERAND (index, 1);
6216 if (! tree_fits_uhwi_p (lo_index)
6217 || ! tree_fits_uhwi_p (hi_index))
6219 need_to_clear = 1;
6220 break;
6223 this_node_count = (tree_to_uhwi (hi_index)
6224 - tree_to_uhwi (lo_index) + 1);
6226 else
6227 this_node_count = 1;
6229 count += this_node_count;
6230 if (mostly_zeros_p (value))
6231 zero_count += this_node_count;
6234 /* Clear the entire array first if there are any missing
6235 elements, or if the incidence of zero elements is >=
6236 75%. */
6237 if (! need_to_clear
6238 && (count < maxelt - minelt + 1
6239 || 4 * zero_count >= 3 * count))
6240 need_to_clear = 1;
6243 if (need_to_clear && size > 0)
6245 if (REG_P (target))
6246 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6247 else
6248 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6249 cleared = 1;
6252 if (!cleared && REG_P (target))
6253 /* Inform later passes that the old value is dead. */
6254 emit_clobber (target);
6256 /* Store each element of the constructor into the
6257 corresponding element of TARGET, determined by counting the
6258 elements. */
6259 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6261 machine_mode mode;
6262 HOST_WIDE_INT bitsize;
6263 HOST_WIDE_INT bitpos;
6264 rtx xtarget = target;
6266 if (cleared && initializer_zerop (value))
6267 continue;
6269 mode = TYPE_MODE (elttype);
6270 if (mode == BLKmode)
6271 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6272 ? tree_to_uhwi (TYPE_SIZE (elttype))
6273 : -1);
6274 else
6275 bitsize = GET_MODE_BITSIZE (mode);
6277 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6279 tree lo_index = TREE_OPERAND (index, 0);
6280 tree hi_index = TREE_OPERAND (index, 1);
6281 rtx index_r, pos_rtx;
6282 HOST_WIDE_INT lo, hi, count;
6283 tree position;
6285 /* If the range is constant and "small", unroll the loop. */
6286 if (const_bounds_p
6287 && tree_fits_shwi_p (lo_index)
6288 && tree_fits_shwi_p (hi_index)
6289 && (lo = tree_to_shwi (lo_index),
6290 hi = tree_to_shwi (hi_index),
6291 count = hi - lo + 1,
6292 (!MEM_P (target)
6293 || count <= 2
6294 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6295 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6296 <= 40 * 8)))))
6298 lo -= minelt; hi -= minelt;
6299 for (; lo <= hi; lo++)
6301 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6303 if (MEM_P (target)
6304 && !MEM_KEEP_ALIAS_SET_P (target)
6305 && TREE_CODE (type) == ARRAY_TYPE
6306 && TYPE_NONALIASED_COMPONENT (type))
6308 target = copy_rtx (target);
6309 MEM_KEEP_ALIAS_SET_P (target) = 1;
6312 store_constructor_field
6313 (target, bitsize, bitpos, mode, value, cleared,
6314 get_alias_set (elttype));
6317 else
6319 rtx_code_label *loop_start = gen_label_rtx ();
6320 rtx_code_label *loop_end = gen_label_rtx ();
6321 tree exit_cond;
6323 expand_normal (hi_index);
6325 index = build_decl (EXPR_LOCATION (exp),
6326 VAR_DECL, NULL_TREE, domain);
6327 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6328 SET_DECL_RTL (index, index_r);
6329 store_expr (lo_index, index_r, 0, false);
6331 /* Build the head of the loop. */
6332 do_pending_stack_adjust ();
6333 emit_label (loop_start);
6335 /* Assign value to element index. */
6336 position =
6337 fold_convert (ssizetype,
6338 fold_build2 (MINUS_EXPR,
6339 TREE_TYPE (index),
6340 index,
6341 TYPE_MIN_VALUE (domain)));
6343 position =
6344 size_binop (MULT_EXPR, position,
6345 fold_convert (ssizetype,
6346 TYPE_SIZE_UNIT (elttype)));
6348 pos_rtx = expand_normal (position);
6349 xtarget = offset_address (target, pos_rtx,
6350 highest_pow2_factor (position));
6351 xtarget = adjust_address (xtarget, mode, 0);
6352 if (TREE_CODE (value) == CONSTRUCTOR)
6353 store_constructor (value, xtarget, cleared,
6354 bitsize / BITS_PER_UNIT);
6355 else
6356 store_expr (value, xtarget, 0, false);
6358 /* Generate a conditional jump to exit the loop. */
6359 exit_cond = build2 (LT_EXPR, integer_type_node,
6360 index, hi_index);
6361 jumpif (exit_cond, loop_end, -1);
6363 /* Update the loop counter, and jump to the head of
6364 the loop. */
6365 expand_assignment (index,
6366 build2 (PLUS_EXPR, TREE_TYPE (index),
6367 index, integer_one_node),
6368 false);
6370 emit_jump (loop_start);
6372 /* Build the end of the loop. */
6373 emit_label (loop_end);
6376 else if ((index != 0 && ! tree_fits_shwi_p (index))
6377 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6379 tree position;
6381 if (index == 0)
6382 index = ssize_int (1);
6384 if (minelt)
6385 index = fold_convert (ssizetype,
6386 fold_build2 (MINUS_EXPR,
6387 TREE_TYPE (index),
6388 index,
6389 TYPE_MIN_VALUE (domain)));
6391 position =
6392 size_binop (MULT_EXPR, index,
6393 fold_convert (ssizetype,
6394 TYPE_SIZE_UNIT (elttype)));
6395 xtarget = offset_address (target,
6396 expand_normal (position),
6397 highest_pow2_factor (position));
6398 xtarget = adjust_address (xtarget, mode, 0);
6399 store_expr (value, xtarget, 0, false);
6401 else
6403 if (index != 0)
6404 bitpos = ((tree_to_shwi (index) - minelt)
6405 * tree_to_uhwi (TYPE_SIZE (elttype)));
6406 else
6407 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6409 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6410 && TREE_CODE (type) == ARRAY_TYPE
6411 && TYPE_NONALIASED_COMPONENT (type))
6413 target = copy_rtx (target);
6414 MEM_KEEP_ALIAS_SET_P (target) = 1;
6416 store_constructor_field (target, bitsize, bitpos, mode, value,
6417 cleared, get_alias_set (elttype));
6420 break;
6423 case VECTOR_TYPE:
6425 unsigned HOST_WIDE_INT idx;
6426 constructor_elt *ce;
6427 int i;
6428 int need_to_clear;
6429 int icode = CODE_FOR_nothing;
6430 tree elttype = TREE_TYPE (type);
6431 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6432 machine_mode eltmode = TYPE_MODE (elttype);
6433 HOST_WIDE_INT bitsize;
6434 HOST_WIDE_INT bitpos;
6435 rtvec vector = NULL;
6436 unsigned n_elts;
6437 alias_set_type alias;
6439 gcc_assert (eltmode != BLKmode);
6441 n_elts = TYPE_VECTOR_SUBPARTS (type);
6442 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6444 machine_mode mode = GET_MODE (target);
6446 icode = (int) optab_handler (vec_init_optab, mode);
6447 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6448 if (icode != CODE_FOR_nothing)
6450 tree value;
6452 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6453 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6455 icode = CODE_FOR_nothing;
6456 break;
6459 if (icode != CODE_FOR_nothing)
6461 unsigned int i;
6463 vector = rtvec_alloc (n_elts);
6464 for (i = 0; i < n_elts; i++)
6465 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6469 /* If the constructor has fewer elements than the vector,
6470 clear the whole array first. Similarly if this is static
6471 constructor of a non-BLKmode object. */
6472 if (cleared)
6473 need_to_clear = 0;
6474 else if (REG_P (target) && TREE_STATIC (exp))
6475 need_to_clear = 1;
6476 else
6478 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6479 tree value;
6481 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6483 int n_elts_here = tree_to_uhwi
6484 (int_const_binop (TRUNC_DIV_EXPR,
6485 TYPE_SIZE (TREE_TYPE (value)),
6486 TYPE_SIZE (elttype)));
6488 count += n_elts_here;
6489 if (mostly_zeros_p (value))
6490 zero_count += n_elts_here;
6493 /* Clear the entire vector first if there are any missing elements,
6494 or if the incidence of zero elements is >= 75%. */
6495 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6498 if (need_to_clear && size > 0 && !vector)
6500 if (REG_P (target))
6501 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6502 else
6503 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6504 cleared = 1;
6507 /* Inform later passes that the old value is dead. */
6508 if (!cleared && !vector && REG_P (target))
6509 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6511 if (MEM_P (target))
6512 alias = MEM_ALIAS_SET (target);
6513 else
6514 alias = get_alias_set (elttype);
6516 /* Store each element of the constructor into the corresponding
6517 element of TARGET, determined by counting the elements. */
6518 for (idx = 0, i = 0;
6519 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6520 idx++, i += bitsize / elt_size)
6522 HOST_WIDE_INT eltpos;
6523 tree value = ce->value;
6525 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6526 if (cleared && initializer_zerop (value))
6527 continue;
6529 if (ce->index)
6530 eltpos = tree_to_uhwi (ce->index);
6531 else
6532 eltpos = i;
6534 if (vector)
6536 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6537 elements. */
6538 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6539 RTVEC_ELT (vector, eltpos)
6540 = expand_normal (value);
6542 else
6544 machine_mode value_mode =
6545 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6546 ? TYPE_MODE (TREE_TYPE (value))
6547 : eltmode;
6548 bitpos = eltpos * elt_size;
6549 store_constructor_field (target, bitsize, bitpos, value_mode,
6550 value, cleared, alias);
6554 if (vector)
6555 emit_insn (GEN_FCN (icode)
6556 (target,
6557 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6558 break;
6561 default:
6562 gcc_unreachable ();
6566 /* Store the value of EXP (an expression tree)
6567 into a subfield of TARGET which has mode MODE and occupies
6568 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6569 If MODE is VOIDmode, it means that we are storing into a bit-field.
6571 BITREGION_START is bitpos of the first bitfield in this region.
6572 BITREGION_END is the bitpos of the ending bitfield in this region.
6573 These two fields are 0, if the C++ memory model does not apply,
6574 or we are not interested in keeping track of bitfield regions.
6576 Always return const0_rtx unless we have something particular to
6577 return.
6579 ALIAS_SET is the alias set for the destination. This value will
6580 (in general) be different from that for TARGET, since TARGET is a
6581 reference to the containing structure.
6583 If NONTEMPORAL is true, try generating a nontemporal store. */
6585 static rtx
6586 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6587 unsigned HOST_WIDE_INT bitregion_start,
6588 unsigned HOST_WIDE_INT bitregion_end,
6589 machine_mode mode, tree exp,
6590 alias_set_type alias_set, bool nontemporal)
6592 if (TREE_CODE (exp) == ERROR_MARK)
6593 return const0_rtx;
6595 /* If we have nothing to store, do nothing unless the expression has
6596 side-effects. */
6597 if (bitsize == 0)
6598 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6600 if (GET_CODE (target) == CONCAT)
6602 /* We're storing into a struct containing a single __complex. */
6604 gcc_assert (!bitpos);
6605 return store_expr (exp, target, 0, nontemporal);
6608 /* If the structure is in a register or if the component
6609 is a bit field, we cannot use addressing to access it.
6610 Use bit-field techniques or SUBREG to store in it. */
6612 if (mode == VOIDmode
6613 || (mode != BLKmode && ! direct_store[(int) mode]
6614 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6615 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6616 || REG_P (target)
6617 || GET_CODE (target) == SUBREG
6618 /* If the field isn't aligned enough to store as an ordinary memref,
6619 store it as a bit field. */
6620 || (mode != BLKmode
6621 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6622 || bitpos % GET_MODE_ALIGNMENT (mode))
6623 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6624 || (bitpos % BITS_PER_UNIT != 0)))
6625 || (bitsize >= 0 && mode != BLKmode
6626 && GET_MODE_BITSIZE (mode) > bitsize)
6627 /* If the RHS and field are a constant size and the size of the
6628 RHS isn't the same size as the bitfield, we must use bitfield
6629 operations. */
6630 || (bitsize >= 0
6631 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6632 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6633 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6634 decl we must use bitfield operations. */
6635 || (bitsize >= 0
6636 && TREE_CODE (exp) == MEM_REF
6637 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6638 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6639 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6640 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6642 rtx temp;
6643 gimple nop_def;
6645 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6646 implies a mask operation. If the precision is the same size as
6647 the field we're storing into, that mask is redundant. This is
6648 particularly common with bit field assignments generated by the
6649 C front end. */
6650 nop_def = get_def_for_expr (exp, NOP_EXPR);
6651 if (nop_def)
6653 tree type = TREE_TYPE (exp);
6654 if (INTEGRAL_TYPE_P (type)
6655 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6656 && bitsize == TYPE_PRECISION (type))
6658 tree op = gimple_assign_rhs1 (nop_def);
6659 type = TREE_TYPE (op);
6660 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6661 exp = op;
6665 temp = expand_normal (exp);
6667 /* If BITSIZE is narrower than the size of the type of EXP
6668 we will be narrowing TEMP. Normally, what's wanted are the
6669 low-order bits. However, if EXP's type is a record and this is
6670 big-endian machine, we want the upper BITSIZE bits. */
6671 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6672 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6673 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6674 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6675 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6676 NULL_RTX, 1);
6678 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6679 if (mode != VOIDmode && mode != BLKmode
6680 && mode != TYPE_MODE (TREE_TYPE (exp)))
6681 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6683 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6684 are both BLKmode, both must be in memory and BITPOS must be aligned
6685 on a byte boundary. If so, we simply do a block copy. Likewise for
6686 a BLKmode-like TARGET. */
6687 if (GET_CODE (temp) != PARALLEL
6688 && GET_MODE (temp) == BLKmode
6689 && (GET_MODE (target) == BLKmode
6690 || (MEM_P (target)
6691 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6692 && (bitpos % BITS_PER_UNIT) == 0
6693 && (bitsize % BITS_PER_UNIT) == 0)))
6695 gcc_assert (MEM_P (target) && MEM_P (temp)
6696 && (bitpos % BITS_PER_UNIT) == 0);
6698 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6699 emit_block_move (target, temp,
6700 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6701 / BITS_PER_UNIT),
6702 BLOCK_OP_NORMAL);
6704 return const0_rtx;
6707 /* Handle calls that return values in multiple non-contiguous locations.
6708 The Irix 6 ABI has examples of this. */
6709 if (GET_CODE (temp) == PARALLEL)
6711 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6712 rtx temp_target;
6713 if (mode == BLKmode || mode == VOIDmode)
6714 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6715 temp_target = gen_reg_rtx (mode);
6716 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6717 temp = temp_target;
6719 else if (mode == BLKmode)
6721 /* Handle calls that return BLKmode values in registers. */
6722 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6724 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6725 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6726 temp = temp_target;
6728 else
6730 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6731 rtx temp_target;
6732 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6733 temp_target = gen_reg_rtx (mode);
6734 temp_target
6735 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6736 temp_target, mode, mode);
6737 temp = temp_target;
6741 /* Store the value in the bitfield. */
6742 store_bit_field (target, bitsize, bitpos,
6743 bitregion_start, bitregion_end,
6744 mode, temp);
6746 return const0_rtx;
6748 else
6750 /* Now build a reference to just the desired component. */
6751 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6753 if (to_rtx == target)
6754 to_rtx = copy_rtx (to_rtx);
6756 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6757 set_mem_alias_set (to_rtx, alias_set);
6759 return store_expr (exp, to_rtx, 0, nontemporal);
6763 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6764 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6765 codes and find the ultimate containing object, which we return.
6767 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6768 bit position, and *PUNSIGNEDP to the signedness of the field.
6769 If the position of the field is variable, we store a tree
6770 giving the variable offset (in units) in *POFFSET.
6771 This offset is in addition to the bit position.
6772 If the position is not variable, we store 0 in *POFFSET.
6774 If any of the extraction expressions is volatile,
6775 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6777 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6778 Otherwise, it is a mode that can be used to access the field.
6780 If the field describes a variable-sized object, *PMODE is set to
6781 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6782 this case, but the address of the object can be found.
6784 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6785 look through nodes that serve as markers of a greater alignment than
6786 the one that can be deduced from the expression. These nodes make it
6787 possible for front-ends to prevent temporaries from being created by
6788 the middle-end on alignment considerations. For that purpose, the
6789 normal operating mode at high-level is to always pass FALSE so that
6790 the ultimate containing object is really returned; moreover, the
6791 associated predicate handled_component_p will always return TRUE
6792 on these nodes, thus indicating that they are essentially handled
6793 by get_inner_reference. TRUE should only be passed when the caller
6794 is scanning the expression in order to build another representation
6795 and specifically knows how to handle these nodes; as such, this is
6796 the normal operating mode in the RTL expanders. */
6798 tree
6799 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6800 HOST_WIDE_INT *pbitpos, tree *poffset,
6801 machine_mode *pmode, int *punsignedp,
6802 int *pvolatilep, bool keep_aligning)
6804 tree size_tree = 0;
6805 machine_mode mode = VOIDmode;
6806 bool blkmode_bitfield = false;
6807 tree offset = size_zero_node;
6808 offset_int bit_offset = 0;
6810 /* First get the mode, signedness, and size. We do this from just the
6811 outermost expression. */
6812 *pbitsize = -1;
6813 if (TREE_CODE (exp) == COMPONENT_REF)
6815 tree field = TREE_OPERAND (exp, 1);
6816 size_tree = DECL_SIZE (field);
6817 if (flag_strict_volatile_bitfields > 0
6818 && TREE_THIS_VOLATILE (exp)
6819 && DECL_BIT_FIELD_TYPE (field)
6820 && DECL_MODE (field) != BLKmode)
6821 /* Volatile bitfields should be accessed in the mode of the
6822 field's type, not the mode computed based on the bit
6823 size. */
6824 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6825 else if (!DECL_BIT_FIELD (field))
6826 mode = DECL_MODE (field);
6827 else if (DECL_MODE (field) == BLKmode)
6828 blkmode_bitfield = true;
6830 *punsignedp = DECL_UNSIGNED (field);
6832 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6834 size_tree = TREE_OPERAND (exp, 1);
6835 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6836 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6838 /* For vector types, with the correct size of access, use the mode of
6839 inner type. */
6840 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6841 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6842 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6843 mode = TYPE_MODE (TREE_TYPE (exp));
6845 else
6847 mode = TYPE_MODE (TREE_TYPE (exp));
6848 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6850 if (mode == BLKmode)
6851 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6852 else
6853 *pbitsize = GET_MODE_BITSIZE (mode);
6856 if (size_tree != 0)
6858 if (! tree_fits_uhwi_p (size_tree))
6859 mode = BLKmode, *pbitsize = -1;
6860 else
6861 *pbitsize = tree_to_uhwi (size_tree);
6864 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6865 and find the ultimate containing object. */
6866 while (1)
6868 switch (TREE_CODE (exp))
6870 case BIT_FIELD_REF:
6871 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6872 break;
6874 case COMPONENT_REF:
6876 tree field = TREE_OPERAND (exp, 1);
6877 tree this_offset = component_ref_field_offset (exp);
6879 /* If this field hasn't been filled in yet, don't go past it.
6880 This should only happen when folding expressions made during
6881 type construction. */
6882 if (this_offset == 0)
6883 break;
6885 offset = size_binop (PLUS_EXPR, offset, this_offset);
6886 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6888 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6890 break;
6892 case ARRAY_REF:
6893 case ARRAY_RANGE_REF:
6895 tree index = TREE_OPERAND (exp, 1);
6896 tree low_bound = array_ref_low_bound (exp);
6897 tree unit_size = array_ref_element_size (exp);
6899 /* We assume all arrays have sizes that are a multiple of a byte.
6900 First subtract the lower bound, if any, in the type of the
6901 index, then convert to sizetype and multiply by the size of
6902 the array element. */
6903 if (! integer_zerop (low_bound))
6904 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6905 index, low_bound);
6907 offset = size_binop (PLUS_EXPR, offset,
6908 size_binop (MULT_EXPR,
6909 fold_convert (sizetype, index),
6910 unit_size));
6912 break;
6914 case REALPART_EXPR:
6915 break;
6917 case IMAGPART_EXPR:
6918 bit_offset += *pbitsize;
6919 break;
6921 case VIEW_CONVERT_EXPR:
6922 if (keep_aligning && STRICT_ALIGNMENT
6923 && (TYPE_ALIGN (TREE_TYPE (exp))
6924 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6925 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6926 < BIGGEST_ALIGNMENT)
6927 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6928 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6929 goto done;
6930 break;
6932 case MEM_REF:
6933 /* Hand back the decl for MEM[&decl, off]. */
6934 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6936 tree off = TREE_OPERAND (exp, 1);
6937 if (!integer_zerop (off))
6939 offset_int boff, coff = mem_ref_offset (exp);
6940 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6941 bit_offset += boff;
6943 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6945 goto done;
6947 default:
6948 goto done;
6951 /* If any reference in the chain is volatile, the effect is volatile. */
6952 if (TREE_THIS_VOLATILE (exp))
6953 *pvolatilep = 1;
6955 exp = TREE_OPERAND (exp, 0);
6957 done:
6959 /* If OFFSET is constant, see if we can return the whole thing as a
6960 constant bit position. Make sure to handle overflow during
6961 this conversion. */
6962 if (TREE_CODE (offset) == INTEGER_CST)
6964 offset_int tem = wi::sext (wi::to_offset (offset),
6965 TYPE_PRECISION (sizetype));
6966 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6967 tem += bit_offset;
6968 if (wi::fits_shwi_p (tem))
6970 *pbitpos = tem.to_shwi ();
6971 *poffset = offset = NULL_TREE;
6975 /* Otherwise, split it up. */
6976 if (offset)
6978 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6979 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
6981 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6982 offset_int tem = bit_offset.and_not (mask);
6983 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6984 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6985 bit_offset -= tem;
6986 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6987 offset = size_binop (PLUS_EXPR, offset,
6988 wide_int_to_tree (sizetype, tem));
6991 *pbitpos = bit_offset.to_shwi ();
6992 *poffset = offset;
6995 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6996 if (mode == VOIDmode
6997 && blkmode_bitfield
6998 && (*pbitpos % BITS_PER_UNIT) == 0
6999 && (*pbitsize % BITS_PER_UNIT) == 0)
7000 *pmode = BLKmode;
7001 else
7002 *pmode = mode;
7004 return exp;
7007 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7009 static unsigned HOST_WIDE_INT
7010 target_align (const_tree target)
7012 /* We might have a chain of nested references with intermediate misaligning
7013 bitfields components, so need to recurse to find out. */
7015 unsigned HOST_WIDE_INT this_align, outer_align;
7017 switch (TREE_CODE (target))
7019 case BIT_FIELD_REF:
7020 return 1;
7022 case COMPONENT_REF:
7023 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7024 outer_align = target_align (TREE_OPERAND (target, 0));
7025 return MIN (this_align, outer_align);
7027 case ARRAY_REF:
7028 case ARRAY_RANGE_REF:
7029 this_align = TYPE_ALIGN (TREE_TYPE (target));
7030 outer_align = target_align (TREE_OPERAND (target, 0));
7031 return MIN (this_align, outer_align);
7033 CASE_CONVERT:
7034 case NON_LVALUE_EXPR:
7035 case VIEW_CONVERT_EXPR:
7036 this_align = TYPE_ALIGN (TREE_TYPE (target));
7037 outer_align = target_align (TREE_OPERAND (target, 0));
7038 return MAX (this_align, outer_align);
7040 default:
7041 return TYPE_ALIGN (TREE_TYPE (target));
7046 /* Given an rtx VALUE that may contain additions and multiplications, return
7047 an equivalent value that just refers to a register, memory, or constant.
7048 This is done by generating instructions to perform the arithmetic and
7049 returning a pseudo-register containing the value.
7051 The returned value may be a REG, SUBREG, MEM or constant. */
7054 force_operand (rtx value, rtx target)
7056 rtx op1, op2;
7057 /* Use subtarget as the target for operand 0 of a binary operation. */
7058 rtx subtarget = get_subtarget (target);
7059 enum rtx_code code = GET_CODE (value);
7061 /* Check for subreg applied to an expression produced by loop optimizer. */
7062 if (code == SUBREG
7063 && !REG_P (SUBREG_REG (value))
7064 && !MEM_P (SUBREG_REG (value)))
7066 value
7067 = simplify_gen_subreg (GET_MODE (value),
7068 force_reg (GET_MODE (SUBREG_REG (value)),
7069 force_operand (SUBREG_REG (value),
7070 NULL_RTX)),
7071 GET_MODE (SUBREG_REG (value)),
7072 SUBREG_BYTE (value));
7073 code = GET_CODE (value);
7076 /* Check for a PIC address load. */
7077 if ((code == PLUS || code == MINUS)
7078 && XEXP (value, 0) == pic_offset_table_rtx
7079 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7080 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7081 || GET_CODE (XEXP (value, 1)) == CONST))
7083 if (!subtarget)
7084 subtarget = gen_reg_rtx (GET_MODE (value));
7085 emit_move_insn (subtarget, value);
7086 return subtarget;
7089 if (ARITHMETIC_P (value))
7091 op2 = XEXP (value, 1);
7092 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7093 subtarget = 0;
7094 if (code == MINUS && CONST_INT_P (op2))
7096 code = PLUS;
7097 op2 = negate_rtx (GET_MODE (value), op2);
7100 /* Check for an addition with OP2 a constant integer and our first
7101 operand a PLUS of a virtual register and something else. In that
7102 case, we want to emit the sum of the virtual register and the
7103 constant first and then add the other value. This allows virtual
7104 register instantiation to simply modify the constant rather than
7105 creating another one around this addition. */
7106 if (code == PLUS && CONST_INT_P (op2)
7107 && GET_CODE (XEXP (value, 0)) == PLUS
7108 && REG_P (XEXP (XEXP (value, 0), 0))
7109 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7110 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7112 rtx temp = expand_simple_binop (GET_MODE (value), code,
7113 XEXP (XEXP (value, 0), 0), op2,
7114 subtarget, 0, OPTAB_LIB_WIDEN);
7115 return expand_simple_binop (GET_MODE (value), code, temp,
7116 force_operand (XEXP (XEXP (value,
7117 0), 1), 0),
7118 target, 0, OPTAB_LIB_WIDEN);
7121 op1 = force_operand (XEXP (value, 0), subtarget);
7122 op2 = force_operand (op2, NULL_RTX);
7123 switch (code)
7125 case MULT:
7126 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7127 case DIV:
7128 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7129 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7130 target, 1, OPTAB_LIB_WIDEN);
7131 else
7132 return expand_divmod (0,
7133 FLOAT_MODE_P (GET_MODE (value))
7134 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7135 GET_MODE (value), op1, op2, target, 0);
7136 case MOD:
7137 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7138 target, 0);
7139 case UDIV:
7140 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7141 target, 1);
7142 case UMOD:
7143 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7144 target, 1);
7145 case ASHIFTRT:
7146 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7147 target, 0, OPTAB_LIB_WIDEN);
7148 default:
7149 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7150 target, 1, OPTAB_LIB_WIDEN);
7153 if (UNARY_P (value))
7155 if (!target)
7156 target = gen_reg_rtx (GET_MODE (value));
7157 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7158 switch (code)
7160 case ZERO_EXTEND:
7161 case SIGN_EXTEND:
7162 case TRUNCATE:
7163 case FLOAT_EXTEND:
7164 case FLOAT_TRUNCATE:
7165 convert_move (target, op1, code == ZERO_EXTEND);
7166 return target;
7168 case FIX:
7169 case UNSIGNED_FIX:
7170 expand_fix (target, op1, code == UNSIGNED_FIX);
7171 return target;
7173 case FLOAT:
7174 case UNSIGNED_FLOAT:
7175 expand_float (target, op1, code == UNSIGNED_FLOAT);
7176 return target;
7178 default:
7179 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7183 #ifdef INSN_SCHEDULING
7184 /* On machines that have insn scheduling, we want all memory reference to be
7185 explicit, so we need to deal with such paradoxical SUBREGs. */
7186 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7187 value
7188 = simplify_gen_subreg (GET_MODE (value),
7189 force_reg (GET_MODE (SUBREG_REG (value)),
7190 force_operand (SUBREG_REG (value),
7191 NULL_RTX)),
7192 GET_MODE (SUBREG_REG (value)),
7193 SUBREG_BYTE (value));
7194 #endif
7196 return value;
7199 /* Subroutine of expand_expr: return nonzero iff there is no way that
7200 EXP can reference X, which is being modified. TOP_P is nonzero if this
7201 call is going to be used to determine whether we need a temporary
7202 for EXP, as opposed to a recursive call to this function.
7204 It is always safe for this routine to return zero since it merely
7205 searches for optimization opportunities. */
7208 safe_from_p (const_rtx x, tree exp, int top_p)
7210 rtx exp_rtl = 0;
7211 int i, nops;
7213 if (x == 0
7214 /* If EXP has varying size, we MUST use a target since we currently
7215 have no way of allocating temporaries of variable size
7216 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7217 So we assume here that something at a higher level has prevented a
7218 clash. This is somewhat bogus, but the best we can do. Only
7219 do this when X is BLKmode and when we are at the top level. */
7220 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7221 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7222 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7223 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7224 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7225 != INTEGER_CST)
7226 && GET_MODE (x) == BLKmode)
7227 /* If X is in the outgoing argument area, it is always safe. */
7228 || (MEM_P (x)
7229 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7230 || (GET_CODE (XEXP (x, 0)) == PLUS
7231 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7232 return 1;
7234 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7235 find the underlying pseudo. */
7236 if (GET_CODE (x) == SUBREG)
7238 x = SUBREG_REG (x);
7239 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7240 return 0;
7243 /* Now look at our tree code and possibly recurse. */
7244 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7246 case tcc_declaration:
7247 exp_rtl = DECL_RTL_IF_SET (exp);
7248 break;
7250 case tcc_constant:
7251 return 1;
7253 case tcc_exceptional:
7254 if (TREE_CODE (exp) == TREE_LIST)
7256 while (1)
7258 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7259 return 0;
7260 exp = TREE_CHAIN (exp);
7261 if (!exp)
7262 return 1;
7263 if (TREE_CODE (exp) != TREE_LIST)
7264 return safe_from_p (x, exp, 0);
7267 else if (TREE_CODE (exp) == CONSTRUCTOR)
7269 constructor_elt *ce;
7270 unsigned HOST_WIDE_INT idx;
7272 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7273 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7274 || !safe_from_p (x, ce->value, 0))
7275 return 0;
7276 return 1;
7278 else if (TREE_CODE (exp) == ERROR_MARK)
7279 return 1; /* An already-visited SAVE_EXPR? */
7280 else
7281 return 0;
7283 case tcc_statement:
7284 /* The only case we look at here is the DECL_INITIAL inside a
7285 DECL_EXPR. */
7286 return (TREE_CODE (exp) != DECL_EXPR
7287 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7288 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7289 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7291 case tcc_binary:
7292 case tcc_comparison:
7293 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7294 return 0;
7295 /* Fall through. */
7297 case tcc_unary:
7298 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7300 case tcc_expression:
7301 case tcc_reference:
7302 case tcc_vl_exp:
7303 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7304 the expression. If it is set, we conflict iff we are that rtx or
7305 both are in memory. Otherwise, we check all operands of the
7306 expression recursively. */
7308 switch (TREE_CODE (exp))
7310 case ADDR_EXPR:
7311 /* If the operand is static or we are static, we can't conflict.
7312 Likewise if we don't conflict with the operand at all. */
7313 if (staticp (TREE_OPERAND (exp, 0))
7314 || TREE_STATIC (exp)
7315 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7316 return 1;
7318 /* Otherwise, the only way this can conflict is if we are taking
7319 the address of a DECL a that address if part of X, which is
7320 very rare. */
7321 exp = TREE_OPERAND (exp, 0);
7322 if (DECL_P (exp))
7324 if (!DECL_RTL_SET_P (exp)
7325 || !MEM_P (DECL_RTL (exp)))
7326 return 0;
7327 else
7328 exp_rtl = XEXP (DECL_RTL (exp), 0);
7330 break;
7332 case MEM_REF:
7333 if (MEM_P (x)
7334 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7335 get_alias_set (exp)))
7336 return 0;
7337 break;
7339 case CALL_EXPR:
7340 /* Assume that the call will clobber all hard registers and
7341 all of memory. */
7342 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7343 || MEM_P (x))
7344 return 0;
7345 break;
7347 case WITH_CLEANUP_EXPR:
7348 case CLEANUP_POINT_EXPR:
7349 /* Lowered by gimplify.c. */
7350 gcc_unreachable ();
7352 case SAVE_EXPR:
7353 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7355 default:
7356 break;
7359 /* If we have an rtx, we do not need to scan our operands. */
7360 if (exp_rtl)
7361 break;
7363 nops = TREE_OPERAND_LENGTH (exp);
7364 for (i = 0; i < nops; i++)
7365 if (TREE_OPERAND (exp, i) != 0
7366 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7367 return 0;
7369 break;
7371 case tcc_type:
7372 /* Should never get a type here. */
7373 gcc_unreachable ();
7376 /* If we have an rtl, find any enclosed object. Then see if we conflict
7377 with it. */
7378 if (exp_rtl)
7380 if (GET_CODE (exp_rtl) == SUBREG)
7382 exp_rtl = SUBREG_REG (exp_rtl);
7383 if (REG_P (exp_rtl)
7384 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7385 return 0;
7388 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7389 are memory and they conflict. */
7390 return ! (rtx_equal_p (x, exp_rtl)
7391 || (MEM_P (x) && MEM_P (exp_rtl)
7392 && true_dependence (exp_rtl, VOIDmode, x)));
7395 /* If we reach here, it is safe. */
7396 return 1;
7400 /* Return the highest power of two that EXP is known to be a multiple of.
7401 This is used in updating alignment of MEMs in array references. */
7403 unsigned HOST_WIDE_INT
7404 highest_pow2_factor (const_tree exp)
7406 unsigned HOST_WIDE_INT ret;
7407 int trailing_zeros = tree_ctz (exp);
7408 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7409 return BIGGEST_ALIGNMENT;
7410 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7411 if (ret > BIGGEST_ALIGNMENT)
7412 return BIGGEST_ALIGNMENT;
7413 return ret;
7416 /* Similar, except that the alignment requirements of TARGET are
7417 taken into account. Assume it is at least as aligned as its
7418 type, unless it is a COMPONENT_REF in which case the layout of
7419 the structure gives the alignment. */
7421 static unsigned HOST_WIDE_INT
7422 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7424 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7425 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7427 return MAX (factor, talign);
7430 /* Convert the tree comparison code TCODE to the rtl one where the
7431 signedness is UNSIGNEDP. */
7433 static enum rtx_code
7434 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7436 enum rtx_code code;
7437 switch (tcode)
7439 case EQ_EXPR:
7440 code = EQ;
7441 break;
7442 case NE_EXPR:
7443 code = NE;
7444 break;
7445 case LT_EXPR:
7446 code = unsignedp ? LTU : LT;
7447 break;
7448 case LE_EXPR:
7449 code = unsignedp ? LEU : LE;
7450 break;
7451 case GT_EXPR:
7452 code = unsignedp ? GTU : GT;
7453 break;
7454 case GE_EXPR:
7455 code = unsignedp ? GEU : GE;
7456 break;
7457 case UNORDERED_EXPR:
7458 code = UNORDERED;
7459 break;
7460 case ORDERED_EXPR:
7461 code = ORDERED;
7462 break;
7463 case UNLT_EXPR:
7464 code = UNLT;
7465 break;
7466 case UNLE_EXPR:
7467 code = UNLE;
7468 break;
7469 case UNGT_EXPR:
7470 code = UNGT;
7471 break;
7472 case UNGE_EXPR:
7473 code = UNGE;
7474 break;
7475 case UNEQ_EXPR:
7476 code = UNEQ;
7477 break;
7478 case LTGT_EXPR:
7479 code = LTGT;
7480 break;
7482 default:
7483 gcc_unreachable ();
7485 return code;
7488 /* Subroutine of expand_expr. Expand the two operands of a binary
7489 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7490 The value may be stored in TARGET if TARGET is nonzero. The
7491 MODIFIER argument is as documented by expand_expr. */
7493 void
7494 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7495 enum expand_modifier modifier)
7497 if (! safe_from_p (target, exp1, 1))
7498 target = 0;
7499 if (operand_equal_p (exp0, exp1, 0))
7501 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7502 *op1 = copy_rtx (*op0);
7504 else
7506 /* If we need to preserve evaluation order, copy exp0 into its own
7507 temporary variable so that it can't be clobbered by exp1. */
7508 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7509 exp0 = save_expr (exp0);
7510 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7511 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7516 /* Return a MEM that contains constant EXP. DEFER is as for
7517 output_constant_def and MODIFIER is as for expand_expr. */
7519 static rtx
7520 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7522 rtx mem;
7524 mem = output_constant_def (exp, defer);
7525 if (modifier != EXPAND_INITIALIZER)
7526 mem = use_anchored_address (mem);
7527 return mem;
7530 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7531 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7533 static rtx
7534 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7535 enum expand_modifier modifier, addr_space_t as)
7537 rtx result, subtarget;
7538 tree inner, offset;
7539 HOST_WIDE_INT bitsize, bitpos;
7540 int volatilep, unsignedp;
7541 machine_mode mode1;
7543 /* If we are taking the address of a constant and are at the top level,
7544 we have to use output_constant_def since we can't call force_const_mem
7545 at top level. */
7546 /* ??? This should be considered a front-end bug. We should not be
7547 generating ADDR_EXPR of something that isn't an LVALUE. The only
7548 exception here is STRING_CST. */
7549 if (CONSTANT_CLASS_P (exp))
7551 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7552 if (modifier < EXPAND_SUM)
7553 result = force_operand (result, target);
7554 return result;
7557 /* Everything must be something allowed by is_gimple_addressable. */
7558 switch (TREE_CODE (exp))
7560 case INDIRECT_REF:
7561 /* This case will happen via recursion for &a->b. */
7562 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7564 case MEM_REF:
7566 tree tem = TREE_OPERAND (exp, 0);
7567 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7568 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7569 return expand_expr (tem, target, tmode, modifier);
7572 case CONST_DECL:
7573 /* Expand the initializer like constants above. */
7574 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7575 0, modifier), 0);
7576 if (modifier < EXPAND_SUM)
7577 result = force_operand (result, target);
7578 return result;
7580 case REALPART_EXPR:
7581 /* The real part of the complex number is always first, therefore
7582 the address is the same as the address of the parent object. */
7583 offset = 0;
7584 bitpos = 0;
7585 inner = TREE_OPERAND (exp, 0);
7586 break;
7588 case IMAGPART_EXPR:
7589 /* The imaginary part of the complex number is always second.
7590 The expression is therefore always offset by the size of the
7591 scalar type. */
7592 offset = 0;
7593 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7594 inner = TREE_OPERAND (exp, 0);
7595 break;
7597 case COMPOUND_LITERAL_EXPR:
7598 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7599 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7600 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7601 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7602 the initializers aren't gimplified. */
7603 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7604 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7605 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7606 target, tmode, modifier, as);
7607 /* FALLTHRU */
7608 default:
7609 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7610 expand_expr, as that can have various side effects; LABEL_DECLs for
7611 example, may not have their DECL_RTL set yet. Expand the rtl of
7612 CONSTRUCTORs too, which should yield a memory reference for the
7613 constructor's contents. Assume language specific tree nodes can
7614 be expanded in some interesting way. */
7615 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7616 if (DECL_P (exp)
7617 || TREE_CODE (exp) == CONSTRUCTOR
7618 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7620 result = expand_expr (exp, target, tmode,
7621 modifier == EXPAND_INITIALIZER
7622 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7624 /* If the DECL isn't in memory, then the DECL wasn't properly
7625 marked TREE_ADDRESSABLE, which will be either a front-end
7626 or a tree optimizer bug. */
7628 if (TREE_ADDRESSABLE (exp)
7629 && ! MEM_P (result)
7630 && ! targetm.calls.allocate_stack_slots_for_args ())
7632 error ("local frame unavailable (naked function?)");
7633 return result;
7635 else
7636 gcc_assert (MEM_P (result));
7637 result = XEXP (result, 0);
7639 /* ??? Is this needed anymore? */
7640 if (DECL_P (exp))
7641 TREE_USED (exp) = 1;
7643 if (modifier != EXPAND_INITIALIZER
7644 && modifier != EXPAND_CONST_ADDRESS
7645 && modifier != EXPAND_SUM)
7646 result = force_operand (result, target);
7647 return result;
7650 /* Pass FALSE as the last argument to get_inner_reference although
7651 we are expanding to RTL. The rationale is that we know how to
7652 handle "aligning nodes" here: we can just bypass them because
7653 they won't change the final object whose address will be returned
7654 (they actually exist only for that purpose). */
7655 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7656 &mode1, &unsignedp, &volatilep, false);
7657 break;
7660 /* We must have made progress. */
7661 gcc_assert (inner != exp);
7663 subtarget = offset || bitpos ? NULL_RTX : target;
7664 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7665 inner alignment, force the inner to be sufficiently aligned. */
7666 if (CONSTANT_CLASS_P (inner)
7667 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7669 inner = copy_node (inner);
7670 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7671 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7672 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7674 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7676 if (offset)
7678 rtx tmp;
7680 if (modifier != EXPAND_NORMAL)
7681 result = force_operand (result, NULL);
7682 tmp = expand_expr (offset, NULL_RTX, tmode,
7683 modifier == EXPAND_INITIALIZER
7684 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7686 /* expand_expr is allowed to return an object in a mode other
7687 than TMODE. If it did, we need to convert. */
7688 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7689 tmp = convert_modes (tmode, GET_MODE (tmp),
7690 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7691 result = convert_memory_address_addr_space (tmode, result, as);
7692 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7694 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7695 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7696 else
7698 subtarget = bitpos ? NULL_RTX : target;
7699 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7700 1, OPTAB_LIB_WIDEN);
7704 if (bitpos)
7706 /* Someone beforehand should have rejected taking the address
7707 of such an object. */
7708 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7710 result = convert_memory_address_addr_space (tmode, result, as);
7711 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7712 if (modifier < EXPAND_SUM)
7713 result = force_operand (result, target);
7716 return result;
7719 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7720 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7722 static rtx
7723 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7724 enum expand_modifier modifier)
7726 addr_space_t as = ADDR_SPACE_GENERIC;
7727 machine_mode address_mode = Pmode;
7728 machine_mode pointer_mode = ptr_mode;
7729 machine_mode rmode;
7730 rtx result;
7732 /* Target mode of VOIDmode says "whatever's natural". */
7733 if (tmode == VOIDmode)
7734 tmode = TYPE_MODE (TREE_TYPE (exp));
7736 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7738 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7739 address_mode = targetm.addr_space.address_mode (as);
7740 pointer_mode = targetm.addr_space.pointer_mode (as);
7743 /* We can get called with some Weird Things if the user does silliness
7744 like "(short) &a". In that case, convert_memory_address won't do
7745 the right thing, so ignore the given target mode. */
7746 if (tmode != address_mode && tmode != pointer_mode)
7747 tmode = address_mode;
7749 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7750 tmode, modifier, as);
7752 /* Despite expand_expr claims concerning ignoring TMODE when not
7753 strictly convenient, stuff breaks if we don't honor it. Note
7754 that combined with the above, we only do this for pointer modes. */
7755 rmode = GET_MODE (result);
7756 if (rmode == VOIDmode)
7757 rmode = tmode;
7758 if (rmode != tmode)
7759 result = convert_memory_address_addr_space (tmode, result, as);
7761 return result;
7764 /* Generate code for computing CONSTRUCTOR EXP.
7765 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7766 is TRUE, instead of creating a temporary variable in memory
7767 NULL is returned and the caller needs to handle it differently. */
7769 static rtx
7770 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7771 bool avoid_temp_mem)
7773 tree type = TREE_TYPE (exp);
7774 machine_mode mode = TYPE_MODE (type);
7776 /* Try to avoid creating a temporary at all. This is possible
7777 if all of the initializer is zero.
7778 FIXME: try to handle all [0..255] initializers we can handle
7779 with memset. */
7780 if (TREE_STATIC (exp)
7781 && !TREE_ADDRESSABLE (exp)
7782 && target != 0 && mode == BLKmode
7783 && all_zeros_p (exp))
7785 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7786 return target;
7789 /* All elts simple constants => refer to a constant in memory. But
7790 if this is a non-BLKmode mode, let it store a field at a time
7791 since that should make a CONST_INT, CONST_WIDE_INT or
7792 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7793 use, it is best to store directly into the target unless the type
7794 is large enough that memcpy will be used. If we are making an
7795 initializer and all operands are constant, put it in memory as
7796 well.
7798 FIXME: Avoid trying to fill vector constructors piece-meal.
7799 Output them with output_constant_def below unless we're sure
7800 they're zeros. This should go away when vector initializers
7801 are treated like VECTOR_CST instead of arrays. */
7802 if ((TREE_STATIC (exp)
7803 && ((mode == BLKmode
7804 && ! (target != 0 && safe_from_p (target, exp, 1)))
7805 || TREE_ADDRESSABLE (exp)
7806 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7807 && (! can_move_by_pieces
7808 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7809 TYPE_ALIGN (type)))
7810 && ! mostly_zeros_p (exp))))
7811 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7812 && TREE_CONSTANT (exp)))
7814 rtx constructor;
7816 if (avoid_temp_mem)
7817 return NULL_RTX;
7819 constructor = expand_expr_constant (exp, 1, modifier);
7821 if (modifier != EXPAND_CONST_ADDRESS
7822 && modifier != EXPAND_INITIALIZER
7823 && modifier != EXPAND_SUM)
7824 constructor = validize_mem (constructor);
7826 return constructor;
7829 /* Handle calls that pass values in multiple non-contiguous
7830 locations. The Irix 6 ABI has examples of this. */
7831 if (target == 0 || ! safe_from_p (target, exp, 1)
7832 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7834 if (avoid_temp_mem)
7835 return NULL_RTX;
7837 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7840 store_constructor (exp, target, 0, int_expr_size (exp));
7841 return target;
7845 /* expand_expr: generate code for computing expression EXP.
7846 An rtx for the computed value is returned. The value is never null.
7847 In the case of a void EXP, const0_rtx is returned.
7849 The value may be stored in TARGET if TARGET is nonzero.
7850 TARGET is just a suggestion; callers must assume that
7851 the rtx returned may not be the same as TARGET.
7853 If TARGET is CONST0_RTX, it means that the value will be ignored.
7855 If TMODE is not VOIDmode, it suggests generating the
7856 result in mode TMODE. But this is done only when convenient.
7857 Otherwise, TMODE is ignored and the value generated in its natural mode.
7858 TMODE is just a suggestion; callers must assume that
7859 the rtx returned may not have mode TMODE.
7861 Note that TARGET may have neither TMODE nor MODE. In that case, it
7862 probably will not be used.
7864 If MODIFIER is EXPAND_SUM then when EXP is an addition
7865 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7866 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7867 products as above, or REG or MEM, or constant.
7868 Ordinarily in such cases we would output mul or add instructions
7869 and then return a pseudo reg containing the sum.
7871 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7872 it also marks a label as absolutely required (it can't be dead).
7873 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7874 This is used for outputting expressions used in initializers.
7876 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7877 with a constant address even if that address is not normally legitimate.
7878 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7880 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7881 a call parameter. Such targets require special care as we haven't yet
7882 marked TARGET so that it's safe from being trashed by libcalls. We
7883 don't want to use TARGET for anything but the final result;
7884 Intermediate values must go elsewhere. Additionally, calls to
7885 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7887 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7888 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7889 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7890 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7891 recursively.
7893 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7894 In this case, we don't adjust a returned MEM rtx that wouldn't be
7895 sufficiently aligned for its mode; instead, it's up to the caller
7896 to deal with it afterwards. This is used to make sure that unaligned
7897 base objects for which out-of-bounds accesses are supported, for
7898 example record types with trailing arrays, aren't realigned behind
7899 the back of the caller.
7900 The normal operating mode is to pass FALSE for this parameter. */
7903 expand_expr_real (tree exp, rtx target, machine_mode tmode,
7904 enum expand_modifier modifier, rtx *alt_rtl,
7905 bool inner_reference_p)
7907 rtx ret;
7909 /* Handle ERROR_MARK before anybody tries to access its type. */
7910 if (TREE_CODE (exp) == ERROR_MARK
7911 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7913 ret = CONST0_RTX (tmode);
7914 return ret ? ret : const0_rtx;
7917 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
7918 inner_reference_p);
7919 return ret;
7922 /* Try to expand the conditional expression which is represented by
7923 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
7924 return the rtl reg which repsents the result. Otherwise return
7925 NULL_RTL. */
7927 static rtx
7928 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7929 tree treeop1 ATTRIBUTE_UNUSED,
7930 tree treeop2 ATTRIBUTE_UNUSED)
7932 rtx insn;
7933 rtx op00, op01, op1, op2;
7934 enum rtx_code comparison_code;
7935 machine_mode comparison_mode;
7936 gimple srcstmt;
7937 rtx temp;
7938 tree type = TREE_TYPE (treeop1);
7939 int unsignedp = TYPE_UNSIGNED (type);
7940 machine_mode mode = TYPE_MODE (type);
7941 machine_mode orig_mode = mode;
7943 /* If we cannot do a conditional move on the mode, try doing it
7944 with the promoted mode. */
7945 if (!can_conditionally_move_p (mode))
7947 mode = promote_mode (type, mode, &unsignedp);
7948 if (!can_conditionally_move_p (mode))
7949 return NULL_RTX;
7950 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
7952 else
7953 temp = assign_temp (type, 0, 1);
7955 start_sequence ();
7956 expand_operands (treeop1, treeop2,
7957 temp, &op1, &op2, EXPAND_NORMAL);
7959 if (TREE_CODE (treeop0) == SSA_NAME
7960 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7962 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7963 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7964 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7965 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7966 comparison_mode = TYPE_MODE (type);
7967 unsignedp = TYPE_UNSIGNED (type);
7968 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7970 else if (COMPARISON_CLASS_P (treeop0))
7972 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7973 enum tree_code cmpcode = TREE_CODE (treeop0);
7974 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
7975 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
7976 unsignedp = TYPE_UNSIGNED (type);
7977 comparison_mode = TYPE_MODE (type);
7978 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7980 else
7982 op00 = expand_normal (treeop0);
7983 op01 = const0_rtx;
7984 comparison_code = NE;
7985 comparison_mode = GET_MODE (op00);
7986 if (comparison_mode == VOIDmode)
7987 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
7990 if (GET_MODE (op1) != mode)
7991 op1 = gen_lowpart (mode, op1);
7993 if (GET_MODE (op2) != mode)
7994 op2 = gen_lowpart (mode, op2);
7996 /* Try to emit the conditional move. */
7997 insn = emit_conditional_move (temp, comparison_code,
7998 op00, op01, comparison_mode,
7999 op1, op2, mode,
8000 unsignedp);
8002 /* If we could do the conditional move, emit the sequence,
8003 and return. */
8004 if (insn)
8006 rtx_insn *seq = get_insns ();
8007 end_sequence ();
8008 emit_insn (seq);
8009 return convert_modes (orig_mode, mode, temp, 0);
8012 /* Otherwise discard the sequence and fall back to code with
8013 branches. */
8014 end_sequence ();
8015 return NULL_RTX;
8019 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8020 enum expand_modifier modifier)
8022 rtx op0, op1, op2, temp;
8023 rtx_code_label *lab;
8024 tree type;
8025 int unsignedp;
8026 machine_mode mode;
8027 enum tree_code code = ops->code;
8028 optab this_optab;
8029 rtx subtarget, original_target;
8030 int ignore;
8031 bool reduce_bit_field;
8032 location_t loc = ops->location;
8033 tree treeop0, treeop1, treeop2;
8034 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8035 ? reduce_to_bit_field_precision ((expr), \
8036 target, \
8037 type) \
8038 : (expr))
8040 type = ops->type;
8041 mode = TYPE_MODE (type);
8042 unsignedp = TYPE_UNSIGNED (type);
8044 treeop0 = ops->op0;
8045 treeop1 = ops->op1;
8046 treeop2 = ops->op2;
8048 /* We should be called only on simple (binary or unary) expressions,
8049 exactly those that are valid in gimple expressions that aren't
8050 GIMPLE_SINGLE_RHS (or invalid). */
8051 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8052 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8053 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8055 ignore = (target == const0_rtx
8056 || ((CONVERT_EXPR_CODE_P (code)
8057 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8058 && TREE_CODE (type) == VOID_TYPE));
8060 /* We should be called only if we need the result. */
8061 gcc_assert (!ignore);
8063 /* An operation in what may be a bit-field type needs the
8064 result to be reduced to the precision of the bit-field type,
8065 which is narrower than that of the type's mode. */
8066 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8067 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8069 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8070 target = 0;
8072 /* Use subtarget as the target for operand 0 of a binary operation. */
8073 subtarget = get_subtarget (target);
8074 original_target = target;
8076 switch (code)
8078 case NON_LVALUE_EXPR:
8079 case PAREN_EXPR:
8080 CASE_CONVERT:
8081 if (treeop0 == error_mark_node)
8082 return const0_rtx;
8084 if (TREE_CODE (type) == UNION_TYPE)
8086 tree valtype = TREE_TYPE (treeop0);
8088 /* If both input and output are BLKmode, this conversion isn't doing
8089 anything except possibly changing memory attribute. */
8090 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8092 rtx result = expand_expr (treeop0, target, tmode,
8093 modifier);
8095 result = copy_rtx (result);
8096 set_mem_attributes (result, type, 0);
8097 return result;
8100 if (target == 0)
8102 if (TYPE_MODE (type) != BLKmode)
8103 target = gen_reg_rtx (TYPE_MODE (type));
8104 else
8105 target = assign_temp (type, 1, 1);
8108 if (MEM_P (target))
8109 /* Store data into beginning of memory target. */
8110 store_expr (treeop0,
8111 adjust_address (target, TYPE_MODE (valtype), 0),
8112 modifier == EXPAND_STACK_PARM,
8113 false);
8115 else
8117 gcc_assert (REG_P (target));
8119 /* Store this field into a union of the proper type. */
8120 store_field (target,
8121 MIN ((int_size_in_bytes (TREE_TYPE
8122 (treeop0))
8123 * BITS_PER_UNIT),
8124 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8125 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8128 /* Return the entire union. */
8129 return target;
8132 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8134 op0 = expand_expr (treeop0, target, VOIDmode,
8135 modifier);
8137 /* If the signedness of the conversion differs and OP0 is
8138 a promoted SUBREG, clear that indication since we now
8139 have to do the proper extension. */
8140 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8141 && GET_CODE (op0) == SUBREG)
8142 SUBREG_PROMOTED_VAR_P (op0) = 0;
8144 return REDUCE_BIT_FIELD (op0);
8147 op0 = expand_expr (treeop0, NULL_RTX, mode,
8148 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8149 if (GET_MODE (op0) == mode)
8152 /* If OP0 is a constant, just convert it into the proper mode. */
8153 else if (CONSTANT_P (op0))
8155 tree inner_type = TREE_TYPE (treeop0);
8156 machine_mode inner_mode = GET_MODE (op0);
8158 if (inner_mode == VOIDmode)
8159 inner_mode = TYPE_MODE (inner_type);
8161 if (modifier == EXPAND_INITIALIZER)
8162 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8163 subreg_lowpart_offset (mode,
8164 inner_mode));
8165 else
8166 op0= convert_modes (mode, inner_mode, op0,
8167 TYPE_UNSIGNED (inner_type));
8170 else if (modifier == EXPAND_INITIALIZER)
8171 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8173 else if (target == 0)
8174 op0 = convert_to_mode (mode, op0,
8175 TYPE_UNSIGNED (TREE_TYPE
8176 (treeop0)));
8177 else
8179 convert_move (target, op0,
8180 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8181 op0 = target;
8184 return REDUCE_BIT_FIELD (op0);
8186 case ADDR_SPACE_CONVERT_EXPR:
8188 tree treeop0_type = TREE_TYPE (treeop0);
8189 addr_space_t as_to;
8190 addr_space_t as_from;
8192 gcc_assert (POINTER_TYPE_P (type));
8193 gcc_assert (POINTER_TYPE_P (treeop0_type));
8195 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8196 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8198 /* Conversions between pointers to the same address space should
8199 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8200 gcc_assert (as_to != as_from);
8202 /* Ask target code to handle conversion between pointers
8203 to overlapping address spaces. */
8204 if (targetm.addr_space.subset_p (as_to, as_from)
8205 || targetm.addr_space.subset_p (as_from, as_to))
8207 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8208 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8209 gcc_assert (op0);
8210 return op0;
8213 /* For disjoint address spaces, converting anything but
8214 a null pointer invokes undefined behaviour. We simply
8215 always return a null pointer here. */
8216 return CONST0_RTX (mode);
8219 case POINTER_PLUS_EXPR:
8220 /* Even though the sizetype mode and the pointer's mode can be different
8221 expand is able to handle this correctly and get the correct result out
8222 of the PLUS_EXPR code. */
8223 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8224 if sizetype precision is smaller than pointer precision. */
8225 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8226 treeop1 = fold_convert_loc (loc, type,
8227 fold_convert_loc (loc, ssizetype,
8228 treeop1));
8229 /* If sizetype precision is larger than pointer precision, truncate the
8230 offset to have matching modes. */
8231 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8232 treeop1 = fold_convert_loc (loc, type, treeop1);
8234 case PLUS_EXPR:
8235 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8236 something else, make sure we add the register to the constant and
8237 then to the other thing. This case can occur during strength
8238 reduction and doing it this way will produce better code if the
8239 frame pointer or argument pointer is eliminated.
8241 fold-const.c will ensure that the constant is always in the inner
8242 PLUS_EXPR, so the only case we need to do anything about is if
8243 sp, ap, or fp is our second argument, in which case we must swap
8244 the innermost first argument and our second argument. */
8246 if (TREE_CODE (treeop0) == PLUS_EXPR
8247 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8248 && TREE_CODE (treeop1) == VAR_DECL
8249 && (DECL_RTL (treeop1) == frame_pointer_rtx
8250 || DECL_RTL (treeop1) == stack_pointer_rtx
8251 || DECL_RTL (treeop1) == arg_pointer_rtx))
8253 gcc_unreachable ();
8256 /* If the result is to be ptr_mode and we are adding an integer to
8257 something, we might be forming a constant. So try to use
8258 plus_constant. If it produces a sum and we can't accept it,
8259 use force_operand. This allows P = &ARR[const] to generate
8260 efficient code on machines where a SYMBOL_REF is not a valid
8261 address.
8263 If this is an EXPAND_SUM call, always return the sum. */
8264 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8265 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8267 if (modifier == EXPAND_STACK_PARM)
8268 target = 0;
8269 if (TREE_CODE (treeop0) == INTEGER_CST
8270 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8271 && TREE_CONSTANT (treeop1))
8273 rtx constant_part;
8274 HOST_WIDE_INT wc;
8275 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8277 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8278 EXPAND_SUM);
8279 /* Use wi::shwi to ensure that the constant is
8280 truncated according to the mode of OP1, then sign extended
8281 to a HOST_WIDE_INT. Using the constant directly can result
8282 in non-canonical RTL in a 64x32 cross compile. */
8283 wc = TREE_INT_CST_LOW (treeop0);
8284 constant_part =
8285 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8286 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8287 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8288 op1 = force_operand (op1, target);
8289 return REDUCE_BIT_FIELD (op1);
8292 else if (TREE_CODE (treeop1) == INTEGER_CST
8293 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8294 && TREE_CONSTANT (treeop0))
8296 rtx constant_part;
8297 HOST_WIDE_INT wc;
8298 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8300 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8301 (modifier == EXPAND_INITIALIZER
8302 ? EXPAND_INITIALIZER : EXPAND_SUM));
8303 if (! CONSTANT_P (op0))
8305 op1 = expand_expr (treeop1, NULL_RTX,
8306 VOIDmode, modifier);
8307 /* Return a PLUS if modifier says it's OK. */
8308 if (modifier == EXPAND_SUM
8309 || modifier == EXPAND_INITIALIZER)
8310 return simplify_gen_binary (PLUS, mode, op0, op1);
8311 goto binop2;
8313 /* Use wi::shwi to ensure that the constant is
8314 truncated according to the mode of OP1, then sign extended
8315 to a HOST_WIDE_INT. Using the constant directly can result
8316 in non-canonical RTL in a 64x32 cross compile. */
8317 wc = TREE_INT_CST_LOW (treeop1);
8318 constant_part
8319 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8320 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8321 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8322 op0 = force_operand (op0, target);
8323 return REDUCE_BIT_FIELD (op0);
8327 /* Use TER to expand pointer addition of a negated value
8328 as pointer subtraction. */
8329 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8330 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8331 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8332 && TREE_CODE (treeop1) == SSA_NAME
8333 && TYPE_MODE (TREE_TYPE (treeop0))
8334 == TYPE_MODE (TREE_TYPE (treeop1)))
8336 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8337 if (def)
8339 treeop1 = gimple_assign_rhs1 (def);
8340 code = MINUS_EXPR;
8341 goto do_minus;
8345 /* No sense saving up arithmetic to be done
8346 if it's all in the wrong mode to form part of an address.
8347 And force_operand won't know whether to sign-extend or
8348 zero-extend. */
8349 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8350 || mode != ptr_mode)
8352 expand_operands (treeop0, treeop1,
8353 subtarget, &op0, &op1, EXPAND_NORMAL);
8354 if (op0 == const0_rtx)
8355 return op1;
8356 if (op1 == const0_rtx)
8357 return op0;
8358 goto binop2;
8361 expand_operands (treeop0, treeop1,
8362 subtarget, &op0, &op1, modifier);
8363 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8365 case MINUS_EXPR:
8366 do_minus:
8367 /* For initializers, we are allowed to return a MINUS of two
8368 symbolic constants. Here we handle all cases when both operands
8369 are constant. */
8370 /* Handle difference of two symbolic constants,
8371 for the sake of an initializer. */
8372 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8373 && really_constant_p (treeop0)
8374 && really_constant_p (treeop1))
8376 expand_operands (treeop0, treeop1,
8377 NULL_RTX, &op0, &op1, modifier);
8379 /* If the last operand is a CONST_INT, use plus_constant of
8380 the negated constant. Else make the MINUS. */
8381 if (CONST_INT_P (op1))
8382 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8383 -INTVAL (op1)));
8384 else
8385 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8388 /* No sense saving up arithmetic to be done
8389 if it's all in the wrong mode to form part of an address.
8390 And force_operand won't know whether to sign-extend or
8391 zero-extend. */
8392 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8393 || mode != ptr_mode)
8394 goto binop;
8396 expand_operands (treeop0, treeop1,
8397 subtarget, &op0, &op1, modifier);
8399 /* Convert A - const to A + (-const). */
8400 if (CONST_INT_P (op1))
8402 op1 = negate_rtx (mode, op1);
8403 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8406 goto binop2;
8408 case WIDEN_MULT_PLUS_EXPR:
8409 case WIDEN_MULT_MINUS_EXPR:
8410 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8411 op2 = expand_normal (treeop2);
8412 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8413 target, unsignedp);
8414 return target;
8416 case WIDEN_MULT_EXPR:
8417 /* If first operand is constant, swap them.
8418 Thus the following special case checks need only
8419 check the second operand. */
8420 if (TREE_CODE (treeop0) == INTEGER_CST)
8421 std::swap (treeop0, treeop1);
8423 /* First, check if we have a multiplication of one signed and one
8424 unsigned operand. */
8425 if (TREE_CODE (treeop1) != INTEGER_CST
8426 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8427 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8429 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8430 this_optab = usmul_widen_optab;
8431 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8432 != CODE_FOR_nothing)
8434 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8435 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8436 EXPAND_NORMAL);
8437 else
8438 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8439 EXPAND_NORMAL);
8440 /* op0 and op1 might still be constant, despite the above
8441 != INTEGER_CST check. Handle it. */
8442 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8444 op0 = convert_modes (innermode, mode, op0, true);
8445 op1 = convert_modes (innermode, mode, op1, false);
8446 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8447 target, unsignedp));
8449 goto binop3;
8452 /* Check for a multiplication with matching signedness. */
8453 else if ((TREE_CODE (treeop1) == INTEGER_CST
8454 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8455 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8456 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8458 tree op0type = TREE_TYPE (treeop0);
8459 machine_mode innermode = TYPE_MODE (op0type);
8460 bool zextend_p = TYPE_UNSIGNED (op0type);
8461 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8462 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8464 if (TREE_CODE (treeop0) != INTEGER_CST)
8466 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8467 != CODE_FOR_nothing)
8469 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8470 EXPAND_NORMAL);
8471 /* op0 and op1 might still be constant, despite the above
8472 != INTEGER_CST check. Handle it. */
8473 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8475 widen_mult_const:
8476 op0 = convert_modes (innermode, mode, op0, zextend_p);
8478 = convert_modes (innermode, mode, op1,
8479 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8480 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8481 target,
8482 unsignedp));
8484 temp = expand_widening_mult (mode, op0, op1, target,
8485 unsignedp, this_optab);
8486 return REDUCE_BIT_FIELD (temp);
8488 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8489 != CODE_FOR_nothing
8490 && innermode == word_mode)
8492 rtx htem, hipart;
8493 op0 = expand_normal (treeop0);
8494 if (TREE_CODE (treeop1) == INTEGER_CST)
8495 op1 = convert_modes (innermode, mode,
8496 expand_normal (treeop1),
8497 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8498 else
8499 op1 = expand_normal (treeop1);
8500 /* op0 and op1 might still be constant, despite the above
8501 != INTEGER_CST check. Handle it. */
8502 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8503 goto widen_mult_const;
8504 temp = expand_binop (mode, other_optab, op0, op1, target,
8505 unsignedp, OPTAB_LIB_WIDEN);
8506 hipart = gen_highpart (innermode, temp);
8507 htem = expand_mult_highpart_adjust (innermode, hipart,
8508 op0, op1, hipart,
8509 zextend_p);
8510 if (htem != hipart)
8511 emit_move_insn (hipart, htem);
8512 return REDUCE_BIT_FIELD (temp);
8516 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8517 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8518 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8519 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8521 case FMA_EXPR:
8523 optab opt = fma_optab;
8524 gimple def0, def2;
8526 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8527 call. */
8528 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8530 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8531 tree call_expr;
8533 gcc_assert (fn != NULL_TREE);
8534 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8535 return expand_builtin (call_expr, target, subtarget, mode, false);
8538 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8539 /* The multiplication is commutative - look at its 2nd operand
8540 if the first isn't fed by a negate. */
8541 if (!def0)
8543 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8544 /* Swap operands if the 2nd operand is fed by a negate. */
8545 if (def0)
8546 std::swap (treeop0, treeop1);
8548 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8550 op0 = op2 = NULL;
8552 if (def0 && def2
8553 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8555 opt = fnms_optab;
8556 op0 = expand_normal (gimple_assign_rhs1 (def0));
8557 op2 = expand_normal (gimple_assign_rhs1 (def2));
8559 else if (def0
8560 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8562 opt = fnma_optab;
8563 op0 = expand_normal (gimple_assign_rhs1 (def0));
8565 else if (def2
8566 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8568 opt = fms_optab;
8569 op2 = expand_normal (gimple_assign_rhs1 (def2));
8572 if (op0 == NULL)
8573 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8574 if (op2 == NULL)
8575 op2 = expand_normal (treeop2);
8576 op1 = expand_normal (treeop1);
8578 return expand_ternary_op (TYPE_MODE (type), opt,
8579 op0, op1, op2, target, 0);
8582 case MULT_EXPR:
8583 /* If this is a fixed-point operation, then we cannot use the code
8584 below because "expand_mult" doesn't support sat/no-sat fixed-point
8585 multiplications. */
8586 if (ALL_FIXED_POINT_MODE_P (mode))
8587 goto binop;
8589 /* If first operand is constant, swap them.
8590 Thus the following special case checks need only
8591 check the second operand. */
8592 if (TREE_CODE (treeop0) == INTEGER_CST)
8593 std::swap (treeop0, treeop1);
8595 /* Attempt to return something suitable for generating an
8596 indexed address, for machines that support that. */
8598 if (modifier == EXPAND_SUM && mode == ptr_mode
8599 && tree_fits_shwi_p (treeop1))
8601 tree exp1 = treeop1;
8603 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8604 EXPAND_SUM);
8606 if (!REG_P (op0))
8607 op0 = force_operand (op0, NULL_RTX);
8608 if (!REG_P (op0))
8609 op0 = copy_to_mode_reg (mode, op0);
8611 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8612 gen_int_mode (tree_to_shwi (exp1),
8613 TYPE_MODE (TREE_TYPE (exp1)))));
8616 if (modifier == EXPAND_STACK_PARM)
8617 target = 0;
8619 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8620 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8622 case TRUNC_DIV_EXPR:
8623 case FLOOR_DIV_EXPR:
8624 case CEIL_DIV_EXPR:
8625 case ROUND_DIV_EXPR:
8626 case EXACT_DIV_EXPR:
8627 /* If this is a fixed-point operation, then we cannot use the code
8628 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8629 divisions. */
8630 if (ALL_FIXED_POINT_MODE_P (mode))
8631 goto binop;
8633 if (modifier == EXPAND_STACK_PARM)
8634 target = 0;
8635 /* Possible optimization: compute the dividend with EXPAND_SUM
8636 then if the divisor is constant can optimize the case
8637 where some terms of the dividend have coeffs divisible by it. */
8638 expand_operands (treeop0, treeop1,
8639 subtarget, &op0, &op1, EXPAND_NORMAL);
8640 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8642 case RDIV_EXPR:
8643 goto binop;
8645 case MULT_HIGHPART_EXPR:
8646 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8647 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8648 gcc_assert (temp);
8649 return temp;
8651 case TRUNC_MOD_EXPR:
8652 case FLOOR_MOD_EXPR:
8653 case CEIL_MOD_EXPR:
8654 case ROUND_MOD_EXPR:
8655 if (modifier == EXPAND_STACK_PARM)
8656 target = 0;
8657 expand_operands (treeop0, treeop1,
8658 subtarget, &op0, &op1, EXPAND_NORMAL);
8659 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8661 case FIXED_CONVERT_EXPR:
8662 op0 = expand_normal (treeop0);
8663 if (target == 0 || modifier == EXPAND_STACK_PARM)
8664 target = gen_reg_rtx (mode);
8666 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8667 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8668 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8669 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8670 else
8671 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8672 return target;
8674 case FIX_TRUNC_EXPR:
8675 op0 = expand_normal (treeop0);
8676 if (target == 0 || modifier == EXPAND_STACK_PARM)
8677 target = gen_reg_rtx (mode);
8678 expand_fix (target, op0, unsignedp);
8679 return target;
8681 case FLOAT_EXPR:
8682 op0 = expand_normal (treeop0);
8683 if (target == 0 || modifier == EXPAND_STACK_PARM)
8684 target = gen_reg_rtx (mode);
8685 /* expand_float can't figure out what to do if FROM has VOIDmode.
8686 So give it the correct mode. With -O, cse will optimize this. */
8687 if (GET_MODE (op0) == VOIDmode)
8688 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8689 op0);
8690 expand_float (target, op0,
8691 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8692 return target;
8694 case NEGATE_EXPR:
8695 op0 = expand_expr (treeop0, subtarget,
8696 VOIDmode, EXPAND_NORMAL);
8697 if (modifier == EXPAND_STACK_PARM)
8698 target = 0;
8699 temp = expand_unop (mode,
8700 optab_for_tree_code (NEGATE_EXPR, type,
8701 optab_default),
8702 op0, target, 0);
8703 gcc_assert (temp);
8704 return REDUCE_BIT_FIELD (temp);
8706 case ABS_EXPR:
8707 op0 = expand_expr (treeop0, subtarget,
8708 VOIDmode, EXPAND_NORMAL);
8709 if (modifier == EXPAND_STACK_PARM)
8710 target = 0;
8712 /* ABS_EXPR is not valid for complex arguments. */
8713 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8714 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8716 /* Unsigned abs is simply the operand. Testing here means we don't
8717 risk generating incorrect code below. */
8718 if (TYPE_UNSIGNED (type))
8719 return op0;
8721 return expand_abs (mode, op0, target, unsignedp,
8722 safe_from_p (target, treeop0, 1));
8724 case MAX_EXPR:
8725 case MIN_EXPR:
8726 target = original_target;
8727 if (target == 0
8728 || modifier == EXPAND_STACK_PARM
8729 || (MEM_P (target) && MEM_VOLATILE_P (target))
8730 || GET_MODE (target) != mode
8731 || (REG_P (target)
8732 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8733 target = gen_reg_rtx (mode);
8734 expand_operands (treeop0, treeop1,
8735 target, &op0, &op1, EXPAND_NORMAL);
8737 /* First try to do it with a special MIN or MAX instruction.
8738 If that does not win, use a conditional jump to select the proper
8739 value. */
8740 this_optab = optab_for_tree_code (code, type, optab_default);
8741 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8742 OPTAB_WIDEN);
8743 if (temp != 0)
8744 return temp;
8746 /* At this point, a MEM target is no longer useful; we will get better
8747 code without it. */
8749 if (! REG_P (target))
8750 target = gen_reg_rtx (mode);
8752 /* If op1 was placed in target, swap op0 and op1. */
8753 if (target != op0 && target == op1)
8754 std::swap (op0, op1);
8756 /* We generate better code and avoid problems with op1 mentioning
8757 target by forcing op1 into a pseudo if it isn't a constant. */
8758 if (! CONSTANT_P (op1))
8759 op1 = force_reg (mode, op1);
8762 enum rtx_code comparison_code;
8763 rtx cmpop1 = op1;
8765 if (code == MAX_EXPR)
8766 comparison_code = unsignedp ? GEU : GE;
8767 else
8768 comparison_code = unsignedp ? LEU : LE;
8770 /* Canonicalize to comparisons against 0. */
8771 if (op1 == const1_rtx)
8773 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8774 or (a != 0 ? a : 1) for unsigned.
8775 For MIN we are safe converting (a <= 1 ? a : 1)
8776 into (a <= 0 ? a : 1) */
8777 cmpop1 = const0_rtx;
8778 if (code == MAX_EXPR)
8779 comparison_code = unsignedp ? NE : GT;
8781 if (op1 == constm1_rtx && !unsignedp)
8783 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8784 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8785 cmpop1 = const0_rtx;
8786 if (code == MIN_EXPR)
8787 comparison_code = LT;
8790 /* Use a conditional move if possible. */
8791 if (can_conditionally_move_p (mode))
8793 rtx insn;
8795 start_sequence ();
8797 /* Try to emit the conditional move. */
8798 insn = emit_conditional_move (target, comparison_code,
8799 op0, cmpop1, mode,
8800 op0, op1, mode,
8801 unsignedp);
8803 /* If we could do the conditional move, emit the sequence,
8804 and return. */
8805 if (insn)
8807 rtx_insn *seq = get_insns ();
8808 end_sequence ();
8809 emit_insn (seq);
8810 return target;
8813 /* Otherwise discard the sequence and fall back to code with
8814 branches. */
8815 end_sequence ();
8818 if (target != op0)
8819 emit_move_insn (target, op0);
8821 lab = gen_label_rtx ();
8822 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8823 unsignedp, mode, NULL_RTX, NULL, lab,
8824 -1);
8826 emit_move_insn (target, op1);
8827 emit_label (lab);
8828 return target;
8830 case BIT_NOT_EXPR:
8831 op0 = expand_expr (treeop0, subtarget,
8832 VOIDmode, EXPAND_NORMAL);
8833 if (modifier == EXPAND_STACK_PARM)
8834 target = 0;
8835 /* In case we have to reduce the result to bitfield precision
8836 for unsigned bitfield expand this as XOR with a proper constant
8837 instead. */
8838 if (reduce_bit_field && TYPE_UNSIGNED (type))
8840 wide_int mask = wi::mask (TYPE_PRECISION (type),
8841 false, GET_MODE_PRECISION (mode));
8843 temp = expand_binop (mode, xor_optab, op0,
8844 immed_wide_int_const (mask, mode),
8845 target, 1, OPTAB_LIB_WIDEN);
8847 else
8848 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8849 gcc_assert (temp);
8850 return temp;
8852 /* ??? Can optimize bitwise operations with one arg constant.
8853 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8854 and (a bitwise1 b) bitwise2 b (etc)
8855 but that is probably not worth while. */
8857 case BIT_AND_EXPR:
8858 case BIT_IOR_EXPR:
8859 case BIT_XOR_EXPR:
8860 goto binop;
8862 case LROTATE_EXPR:
8863 case RROTATE_EXPR:
8864 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8865 || (GET_MODE_PRECISION (TYPE_MODE (type))
8866 == TYPE_PRECISION (type)));
8867 /* fall through */
8869 case LSHIFT_EXPR:
8870 case RSHIFT_EXPR:
8871 /* If this is a fixed-point operation, then we cannot use the code
8872 below because "expand_shift" doesn't support sat/no-sat fixed-point
8873 shifts. */
8874 if (ALL_FIXED_POINT_MODE_P (mode))
8875 goto binop;
8877 if (! safe_from_p (subtarget, treeop1, 1))
8878 subtarget = 0;
8879 if (modifier == EXPAND_STACK_PARM)
8880 target = 0;
8881 op0 = expand_expr (treeop0, subtarget,
8882 VOIDmode, EXPAND_NORMAL);
8883 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8884 unsignedp);
8885 if (code == LSHIFT_EXPR)
8886 temp = REDUCE_BIT_FIELD (temp);
8887 return temp;
8889 /* Could determine the answer when only additive constants differ. Also,
8890 the addition of one can be handled by changing the condition. */
8891 case LT_EXPR:
8892 case LE_EXPR:
8893 case GT_EXPR:
8894 case GE_EXPR:
8895 case EQ_EXPR:
8896 case NE_EXPR:
8897 case UNORDERED_EXPR:
8898 case ORDERED_EXPR:
8899 case UNLT_EXPR:
8900 case UNLE_EXPR:
8901 case UNGT_EXPR:
8902 case UNGE_EXPR:
8903 case UNEQ_EXPR:
8904 case LTGT_EXPR:
8906 temp = do_store_flag (ops,
8907 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8908 tmode != VOIDmode ? tmode : mode);
8909 if (temp)
8910 return temp;
8912 /* Use a compare and a jump for BLKmode comparisons, or for function
8913 type comparisons is have_canonicalize_funcptr_for_compare. */
8915 if ((target == 0
8916 || modifier == EXPAND_STACK_PARM
8917 || ! safe_from_p (target, treeop0, 1)
8918 || ! safe_from_p (target, treeop1, 1)
8919 /* Make sure we don't have a hard reg (such as function's return
8920 value) live across basic blocks, if not optimizing. */
8921 || (!optimize && REG_P (target)
8922 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8923 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8925 emit_move_insn (target, const0_rtx);
8927 rtx_code_label *lab1 = gen_label_rtx ();
8928 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
8930 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8931 emit_move_insn (target, constm1_rtx);
8932 else
8933 emit_move_insn (target, const1_rtx);
8935 emit_label (lab1);
8936 return target;
8938 case COMPLEX_EXPR:
8939 /* Get the rtx code of the operands. */
8940 op0 = expand_normal (treeop0);
8941 op1 = expand_normal (treeop1);
8943 if (!target)
8944 target = gen_reg_rtx (TYPE_MODE (type));
8945 else
8946 /* If target overlaps with op1, then either we need to force
8947 op1 into a pseudo (if target also overlaps with op0),
8948 or write the complex parts in reverse order. */
8949 switch (GET_CODE (target))
8951 case CONCAT:
8952 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
8954 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
8956 complex_expr_force_op1:
8957 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
8958 emit_move_insn (temp, op1);
8959 op1 = temp;
8960 break;
8962 complex_expr_swap_order:
8963 /* Move the imaginary (op1) and real (op0) parts to their
8964 location. */
8965 write_complex_part (target, op1, true);
8966 write_complex_part (target, op0, false);
8968 return target;
8970 break;
8971 case MEM:
8972 temp = adjust_address_nv (target,
8973 GET_MODE_INNER (GET_MODE (target)), 0);
8974 if (reg_overlap_mentioned_p (temp, op1))
8976 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
8977 temp = adjust_address_nv (target, imode,
8978 GET_MODE_SIZE (imode));
8979 if (reg_overlap_mentioned_p (temp, op0))
8980 goto complex_expr_force_op1;
8981 goto complex_expr_swap_order;
8983 break;
8984 default:
8985 if (reg_overlap_mentioned_p (target, op1))
8987 if (reg_overlap_mentioned_p (target, op0))
8988 goto complex_expr_force_op1;
8989 goto complex_expr_swap_order;
8991 break;
8994 /* Move the real (op0) and imaginary (op1) parts to their location. */
8995 write_complex_part (target, op0, false);
8996 write_complex_part (target, op1, true);
8998 return target;
9000 case WIDEN_SUM_EXPR:
9002 tree oprnd0 = treeop0;
9003 tree oprnd1 = treeop1;
9005 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9006 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9007 target, unsignedp);
9008 return target;
9011 case REDUC_MAX_EXPR:
9012 case REDUC_MIN_EXPR:
9013 case REDUC_PLUS_EXPR:
9015 op0 = expand_normal (treeop0);
9016 this_optab = optab_for_tree_code (code, type, optab_default);
9017 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9019 if (optab_handler (this_optab, vec_mode) != CODE_FOR_nothing)
9021 struct expand_operand ops[2];
9022 enum insn_code icode = optab_handler (this_optab, vec_mode);
9024 create_output_operand (&ops[0], target, mode);
9025 create_input_operand (&ops[1], op0, vec_mode);
9026 if (maybe_expand_insn (icode, 2, ops))
9028 target = ops[0].value;
9029 if (GET_MODE (target) != mode)
9030 return gen_lowpart (tmode, target);
9031 return target;
9034 /* Fall back to optab with vector result, and then extract scalar. */
9035 this_optab = scalar_reduc_to_vector (this_optab, type);
9036 temp = expand_unop (vec_mode, this_optab, op0, NULL_RTX, unsignedp);
9037 gcc_assert (temp);
9038 /* The tree code produces a scalar result, but (somewhat by convention)
9039 the optab produces a vector with the result in element 0 if
9040 little-endian, or element N-1 if big-endian. So pull the scalar
9041 result out of that element. */
9042 int index = BYTES_BIG_ENDIAN ? GET_MODE_NUNITS (vec_mode) - 1 : 0;
9043 int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (vec_mode));
9044 temp = extract_bit_field (temp, bitsize, bitsize * index, unsignedp,
9045 target, mode, mode);
9046 gcc_assert (temp);
9047 return temp;
9050 case VEC_UNPACK_HI_EXPR:
9051 case VEC_UNPACK_LO_EXPR:
9053 op0 = expand_normal (treeop0);
9054 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9055 target, unsignedp);
9056 gcc_assert (temp);
9057 return temp;
9060 case VEC_UNPACK_FLOAT_HI_EXPR:
9061 case VEC_UNPACK_FLOAT_LO_EXPR:
9063 op0 = expand_normal (treeop0);
9064 /* The signedness is determined from input operand. */
9065 temp = expand_widen_pattern_expr
9066 (ops, op0, NULL_RTX, NULL_RTX,
9067 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9069 gcc_assert (temp);
9070 return temp;
9073 case VEC_WIDEN_MULT_HI_EXPR:
9074 case VEC_WIDEN_MULT_LO_EXPR:
9075 case VEC_WIDEN_MULT_EVEN_EXPR:
9076 case VEC_WIDEN_MULT_ODD_EXPR:
9077 case VEC_WIDEN_LSHIFT_HI_EXPR:
9078 case VEC_WIDEN_LSHIFT_LO_EXPR:
9079 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9080 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9081 target, unsignedp);
9082 gcc_assert (target);
9083 return target;
9085 case VEC_PACK_TRUNC_EXPR:
9086 case VEC_PACK_SAT_EXPR:
9087 case VEC_PACK_FIX_TRUNC_EXPR:
9088 mode = TYPE_MODE (TREE_TYPE (treeop0));
9089 goto binop;
9091 case VEC_PERM_EXPR:
9092 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9093 op2 = expand_normal (treeop2);
9095 /* Careful here: if the target doesn't support integral vector modes,
9096 a constant selection vector could wind up smooshed into a normal
9097 integral constant. */
9098 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9100 tree sel_type = TREE_TYPE (treeop2);
9101 machine_mode vmode
9102 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9103 TYPE_VECTOR_SUBPARTS (sel_type));
9104 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9105 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9106 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9108 else
9109 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9111 temp = expand_vec_perm (mode, op0, op1, op2, target);
9112 gcc_assert (temp);
9113 return temp;
9115 case DOT_PROD_EXPR:
9117 tree oprnd0 = treeop0;
9118 tree oprnd1 = treeop1;
9119 tree oprnd2 = treeop2;
9120 rtx op2;
9122 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9123 op2 = expand_normal (oprnd2);
9124 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9125 target, unsignedp);
9126 return target;
9129 case SAD_EXPR:
9131 tree oprnd0 = treeop0;
9132 tree oprnd1 = treeop1;
9133 tree oprnd2 = treeop2;
9134 rtx op2;
9136 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9137 op2 = expand_normal (oprnd2);
9138 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9139 target, unsignedp);
9140 return target;
9143 case REALIGN_LOAD_EXPR:
9145 tree oprnd0 = treeop0;
9146 tree oprnd1 = treeop1;
9147 tree oprnd2 = treeop2;
9148 rtx op2;
9150 this_optab = optab_for_tree_code (code, type, optab_default);
9151 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9152 op2 = expand_normal (oprnd2);
9153 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9154 target, unsignedp);
9155 gcc_assert (temp);
9156 return temp;
9159 case COND_EXPR:
9161 /* A COND_EXPR with its type being VOID_TYPE represents a
9162 conditional jump and is handled in
9163 expand_gimple_cond_expr. */
9164 gcc_assert (!VOID_TYPE_P (type));
9166 /* Note that COND_EXPRs whose type is a structure or union
9167 are required to be constructed to contain assignments of
9168 a temporary variable, so that we can evaluate them here
9169 for side effect only. If type is void, we must do likewise. */
9171 gcc_assert (!TREE_ADDRESSABLE (type)
9172 && !ignore
9173 && TREE_TYPE (treeop1) != void_type_node
9174 && TREE_TYPE (treeop2) != void_type_node);
9176 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9177 if (temp)
9178 return temp;
9180 /* If we are not to produce a result, we have no target. Otherwise,
9181 if a target was specified use it; it will not be used as an
9182 intermediate target unless it is safe. If no target, use a
9183 temporary. */
9185 if (modifier != EXPAND_STACK_PARM
9186 && original_target
9187 && safe_from_p (original_target, treeop0, 1)
9188 && GET_MODE (original_target) == mode
9189 && !MEM_P (original_target))
9190 temp = original_target;
9191 else
9192 temp = assign_temp (type, 0, 1);
9194 do_pending_stack_adjust ();
9195 NO_DEFER_POP;
9196 rtx_code_label *lab0 = gen_label_rtx ();
9197 rtx_code_label *lab1 = gen_label_rtx ();
9198 jumpifnot (treeop0, lab0, -1);
9199 store_expr (treeop1, temp,
9200 modifier == EXPAND_STACK_PARM,
9201 false);
9203 emit_jump_insn (targetm.gen_jump (lab1));
9204 emit_barrier ();
9205 emit_label (lab0);
9206 store_expr (treeop2, temp,
9207 modifier == EXPAND_STACK_PARM,
9208 false);
9210 emit_label (lab1);
9211 OK_DEFER_POP;
9212 return temp;
9215 case VEC_COND_EXPR:
9216 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9217 return target;
9219 default:
9220 gcc_unreachable ();
9223 /* Here to do an ordinary binary operator. */
9224 binop:
9225 expand_operands (treeop0, treeop1,
9226 subtarget, &op0, &op1, EXPAND_NORMAL);
9227 binop2:
9228 this_optab = optab_for_tree_code (code, type, optab_default);
9229 binop3:
9230 if (modifier == EXPAND_STACK_PARM)
9231 target = 0;
9232 temp = expand_binop (mode, this_optab, op0, op1, target,
9233 unsignedp, OPTAB_LIB_WIDEN);
9234 gcc_assert (temp);
9235 /* Bitwise operations do not need bitfield reduction as we expect their
9236 operands being properly truncated. */
9237 if (code == BIT_XOR_EXPR
9238 || code == BIT_AND_EXPR
9239 || code == BIT_IOR_EXPR)
9240 return temp;
9241 return REDUCE_BIT_FIELD (temp);
9243 #undef REDUCE_BIT_FIELD
9246 /* Return TRUE if expression STMT is suitable for replacement.
9247 Never consider memory loads as replaceable, because those don't ever lead
9248 into constant expressions. */
9250 static bool
9251 stmt_is_replaceable_p (gimple stmt)
9253 if (ssa_is_replaceable_p (stmt))
9255 /* Don't move around loads. */
9256 if (!gimple_assign_single_p (stmt)
9257 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9258 return true;
9260 return false;
9264 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9265 enum expand_modifier modifier, rtx *alt_rtl,
9266 bool inner_reference_p)
9268 rtx op0, op1, temp, decl_rtl;
9269 tree type;
9270 int unsignedp;
9271 machine_mode mode;
9272 enum tree_code code = TREE_CODE (exp);
9273 rtx subtarget, original_target;
9274 int ignore;
9275 tree context;
9276 bool reduce_bit_field;
9277 location_t loc = EXPR_LOCATION (exp);
9278 struct separate_ops ops;
9279 tree treeop0, treeop1, treeop2;
9280 tree ssa_name = NULL_TREE;
9281 gimple g;
9283 type = TREE_TYPE (exp);
9284 mode = TYPE_MODE (type);
9285 unsignedp = TYPE_UNSIGNED (type);
9287 treeop0 = treeop1 = treeop2 = NULL_TREE;
9288 if (!VL_EXP_CLASS_P (exp))
9289 switch (TREE_CODE_LENGTH (code))
9291 default:
9292 case 3: treeop2 = TREE_OPERAND (exp, 2);
9293 case 2: treeop1 = TREE_OPERAND (exp, 1);
9294 case 1: treeop0 = TREE_OPERAND (exp, 0);
9295 case 0: break;
9297 ops.code = code;
9298 ops.type = type;
9299 ops.op0 = treeop0;
9300 ops.op1 = treeop1;
9301 ops.op2 = treeop2;
9302 ops.location = loc;
9304 ignore = (target == const0_rtx
9305 || ((CONVERT_EXPR_CODE_P (code)
9306 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9307 && TREE_CODE (type) == VOID_TYPE));
9309 /* An operation in what may be a bit-field type needs the
9310 result to be reduced to the precision of the bit-field type,
9311 which is narrower than that of the type's mode. */
9312 reduce_bit_field = (!ignore
9313 && INTEGRAL_TYPE_P (type)
9314 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9316 /* If we are going to ignore this result, we need only do something
9317 if there is a side-effect somewhere in the expression. If there
9318 is, short-circuit the most common cases here. Note that we must
9319 not call expand_expr with anything but const0_rtx in case this
9320 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9322 if (ignore)
9324 if (! TREE_SIDE_EFFECTS (exp))
9325 return const0_rtx;
9327 /* Ensure we reference a volatile object even if value is ignored, but
9328 don't do this if all we are doing is taking its address. */
9329 if (TREE_THIS_VOLATILE (exp)
9330 && TREE_CODE (exp) != FUNCTION_DECL
9331 && mode != VOIDmode && mode != BLKmode
9332 && modifier != EXPAND_CONST_ADDRESS)
9334 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9335 if (MEM_P (temp))
9336 copy_to_reg (temp);
9337 return const0_rtx;
9340 if (TREE_CODE_CLASS (code) == tcc_unary
9341 || code == BIT_FIELD_REF
9342 || code == COMPONENT_REF
9343 || code == INDIRECT_REF)
9344 return expand_expr (treeop0, const0_rtx, VOIDmode,
9345 modifier);
9347 else if (TREE_CODE_CLASS (code) == tcc_binary
9348 || TREE_CODE_CLASS (code) == tcc_comparison
9349 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9351 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9352 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9353 return const0_rtx;
9356 target = 0;
9359 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9360 target = 0;
9362 /* Use subtarget as the target for operand 0 of a binary operation. */
9363 subtarget = get_subtarget (target);
9364 original_target = target;
9366 switch (code)
9368 case LABEL_DECL:
9370 tree function = decl_function_context (exp);
9372 temp = label_rtx (exp);
9373 temp = gen_rtx_LABEL_REF (Pmode, temp);
9375 if (function != current_function_decl
9376 && function != 0)
9377 LABEL_REF_NONLOCAL_P (temp) = 1;
9379 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9380 return temp;
9383 case SSA_NAME:
9384 /* ??? ivopts calls expander, without any preparation from
9385 out-of-ssa. So fake instructions as if this was an access to the
9386 base variable. This unnecessarily allocates a pseudo, see how we can
9387 reuse it, if partition base vars have it set already. */
9388 if (!currently_expanding_to_rtl)
9390 tree var = SSA_NAME_VAR (exp);
9391 if (var && DECL_RTL_SET_P (var))
9392 return DECL_RTL (var);
9393 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9394 LAST_VIRTUAL_REGISTER + 1);
9397 g = get_gimple_for_ssa_name (exp);
9398 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9399 if (g == NULL
9400 && modifier == EXPAND_INITIALIZER
9401 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9402 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9403 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9404 g = SSA_NAME_DEF_STMT (exp);
9405 if (g)
9407 rtx r;
9408 ops.code = gimple_assign_rhs_code (g);
9409 switch (get_gimple_rhs_class (ops.code))
9411 case GIMPLE_TERNARY_RHS:
9412 ops.op2 = gimple_assign_rhs3 (g);
9413 /* Fallthru */
9414 case GIMPLE_BINARY_RHS:
9415 ops.op1 = gimple_assign_rhs2 (g);
9417 /* Try to expand conditonal compare. */
9418 if (targetm.gen_ccmp_first)
9420 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9421 r = expand_ccmp_expr (g);
9422 if (r)
9423 break;
9425 /* Fallthru */
9426 case GIMPLE_UNARY_RHS:
9427 ops.op0 = gimple_assign_rhs1 (g);
9428 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9429 ops.location = gimple_location (g);
9430 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9431 break;
9432 case GIMPLE_SINGLE_RHS:
9434 location_t saved_loc = curr_insn_location ();
9435 set_curr_insn_location (gimple_location (g));
9436 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9437 tmode, modifier, NULL, inner_reference_p);
9438 set_curr_insn_location (saved_loc);
9439 break;
9441 default:
9442 gcc_unreachable ();
9444 if (REG_P (r) && !REG_EXPR (r))
9445 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9446 return r;
9449 ssa_name = exp;
9450 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9451 exp = SSA_NAME_VAR (ssa_name);
9452 goto expand_decl_rtl;
9454 case PARM_DECL:
9455 case VAR_DECL:
9456 /* If a static var's type was incomplete when the decl was written,
9457 but the type is complete now, lay out the decl now. */
9458 if (DECL_SIZE (exp) == 0
9459 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9460 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9461 layout_decl (exp, 0);
9463 /* ... fall through ... */
9465 case FUNCTION_DECL:
9466 case RESULT_DECL:
9467 decl_rtl = DECL_RTL (exp);
9468 expand_decl_rtl:
9469 gcc_assert (decl_rtl);
9470 decl_rtl = copy_rtx (decl_rtl);
9471 /* Record writes to register variables. */
9472 if (modifier == EXPAND_WRITE
9473 && REG_P (decl_rtl)
9474 && HARD_REGISTER_P (decl_rtl))
9475 add_to_hard_reg_set (&crtl->asm_clobbers,
9476 GET_MODE (decl_rtl), REGNO (decl_rtl));
9478 /* Ensure variable marked as used even if it doesn't go through
9479 a parser. If it hasn't be used yet, write out an external
9480 definition. */
9481 TREE_USED (exp) = 1;
9483 /* Show we haven't gotten RTL for this yet. */
9484 temp = 0;
9486 /* Variables inherited from containing functions should have
9487 been lowered by this point. */
9488 context = decl_function_context (exp);
9489 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9490 || context == current_function_decl
9491 || TREE_STATIC (exp)
9492 || DECL_EXTERNAL (exp)
9493 /* ??? C++ creates functions that are not TREE_STATIC. */
9494 || TREE_CODE (exp) == FUNCTION_DECL);
9496 /* This is the case of an array whose size is to be determined
9497 from its initializer, while the initializer is still being parsed.
9498 ??? We aren't parsing while expanding anymore. */
9500 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9501 temp = validize_mem (decl_rtl);
9503 /* If DECL_RTL is memory, we are in the normal case and the
9504 address is not valid, get the address into a register. */
9506 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9508 if (alt_rtl)
9509 *alt_rtl = decl_rtl;
9510 decl_rtl = use_anchored_address (decl_rtl);
9511 if (modifier != EXPAND_CONST_ADDRESS
9512 && modifier != EXPAND_SUM
9513 && !memory_address_addr_space_p (DECL_MODE (exp),
9514 XEXP (decl_rtl, 0),
9515 MEM_ADDR_SPACE (decl_rtl)))
9516 temp = replace_equiv_address (decl_rtl,
9517 copy_rtx (XEXP (decl_rtl, 0)));
9520 /* If we got something, return it. But first, set the alignment
9521 if the address is a register. */
9522 if (temp != 0)
9524 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9525 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9527 return temp;
9530 /* If the mode of DECL_RTL does not match that of the decl,
9531 there are two cases: we are dealing with a BLKmode value
9532 that is returned in a register, or we are dealing with
9533 a promoted value. In the latter case, return a SUBREG
9534 of the wanted mode, but mark it so that we know that it
9535 was already extended. */
9536 if (REG_P (decl_rtl)
9537 && DECL_MODE (exp) != BLKmode
9538 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9540 machine_mode pmode;
9542 /* Get the signedness to be used for this variable. Ensure we get
9543 the same mode we got when the variable was declared. */
9544 if (code == SSA_NAME
9545 && (g = SSA_NAME_DEF_STMT (ssa_name))
9546 && gimple_code (g) == GIMPLE_CALL
9547 && !gimple_call_internal_p (g))
9548 pmode = promote_function_mode (type, mode, &unsignedp,
9549 gimple_call_fntype (g),
9551 else
9552 pmode = promote_decl_mode (exp, &unsignedp);
9553 gcc_assert (GET_MODE (decl_rtl) == pmode);
9555 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9556 SUBREG_PROMOTED_VAR_P (temp) = 1;
9557 SUBREG_PROMOTED_SET (temp, unsignedp);
9558 return temp;
9561 return decl_rtl;
9563 case INTEGER_CST:
9564 /* Given that TYPE_PRECISION (type) is not always equal to
9565 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9566 the former to the latter according to the signedness of the
9567 type. */
9568 temp = immed_wide_int_const (wide_int::from
9569 (exp,
9570 GET_MODE_PRECISION (TYPE_MODE (type)),
9571 TYPE_SIGN (type)),
9572 TYPE_MODE (type));
9573 return temp;
9575 case VECTOR_CST:
9577 tree tmp = NULL_TREE;
9578 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9579 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9580 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9581 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9582 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9583 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9584 return const_vector_from_tree (exp);
9585 if (GET_MODE_CLASS (mode) == MODE_INT)
9587 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9588 if (type_for_mode)
9589 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9591 if (!tmp)
9593 vec<constructor_elt, va_gc> *v;
9594 unsigned i;
9595 vec_alloc (v, VECTOR_CST_NELTS (exp));
9596 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9597 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9598 tmp = build_constructor (type, v);
9600 return expand_expr (tmp, ignore ? const0_rtx : target,
9601 tmode, modifier);
9604 case CONST_DECL:
9605 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9607 case REAL_CST:
9608 /* If optimized, generate immediate CONST_DOUBLE
9609 which will be turned into memory by reload if necessary.
9611 We used to force a register so that loop.c could see it. But
9612 this does not allow gen_* patterns to perform optimizations with
9613 the constants. It also produces two insns in cases like "x = 1.0;".
9614 On most machines, floating-point constants are not permitted in
9615 many insns, so we'd end up copying it to a register in any case.
9617 Now, we do the copying in expand_binop, if appropriate. */
9618 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9619 TYPE_MODE (TREE_TYPE (exp)));
9621 case FIXED_CST:
9622 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9623 TYPE_MODE (TREE_TYPE (exp)));
9625 case COMPLEX_CST:
9626 /* Handle evaluating a complex constant in a CONCAT target. */
9627 if (original_target && GET_CODE (original_target) == CONCAT)
9629 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9630 rtx rtarg, itarg;
9632 rtarg = XEXP (original_target, 0);
9633 itarg = XEXP (original_target, 1);
9635 /* Move the real and imaginary parts separately. */
9636 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9637 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9639 if (op0 != rtarg)
9640 emit_move_insn (rtarg, op0);
9641 if (op1 != itarg)
9642 emit_move_insn (itarg, op1);
9644 return original_target;
9647 /* ... fall through ... */
9649 case STRING_CST:
9650 temp = expand_expr_constant (exp, 1, modifier);
9652 /* temp contains a constant address.
9653 On RISC machines where a constant address isn't valid,
9654 make some insns to get that address into a register. */
9655 if (modifier != EXPAND_CONST_ADDRESS
9656 && modifier != EXPAND_INITIALIZER
9657 && modifier != EXPAND_SUM
9658 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9659 MEM_ADDR_SPACE (temp)))
9660 return replace_equiv_address (temp,
9661 copy_rtx (XEXP (temp, 0)));
9662 return temp;
9664 case SAVE_EXPR:
9666 tree val = treeop0;
9667 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9668 inner_reference_p);
9670 if (!SAVE_EXPR_RESOLVED_P (exp))
9672 /* We can indeed still hit this case, typically via builtin
9673 expanders calling save_expr immediately before expanding
9674 something. Assume this means that we only have to deal
9675 with non-BLKmode values. */
9676 gcc_assert (GET_MODE (ret) != BLKmode);
9678 val = build_decl (curr_insn_location (),
9679 VAR_DECL, NULL, TREE_TYPE (exp));
9680 DECL_ARTIFICIAL (val) = 1;
9681 DECL_IGNORED_P (val) = 1;
9682 treeop0 = val;
9683 TREE_OPERAND (exp, 0) = treeop0;
9684 SAVE_EXPR_RESOLVED_P (exp) = 1;
9686 if (!CONSTANT_P (ret))
9687 ret = copy_to_reg (ret);
9688 SET_DECL_RTL (val, ret);
9691 return ret;
9695 case CONSTRUCTOR:
9696 /* If we don't need the result, just ensure we evaluate any
9697 subexpressions. */
9698 if (ignore)
9700 unsigned HOST_WIDE_INT idx;
9701 tree value;
9703 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9704 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9706 return const0_rtx;
9709 return expand_constructor (exp, target, modifier, false);
9711 case TARGET_MEM_REF:
9713 addr_space_t as
9714 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9715 enum insn_code icode;
9716 unsigned int align;
9718 op0 = addr_for_mem_ref (exp, as, true);
9719 op0 = memory_address_addr_space (mode, op0, as);
9720 temp = gen_rtx_MEM (mode, op0);
9721 set_mem_attributes (temp, exp, 0);
9722 set_mem_addr_space (temp, as);
9723 align = get_object_alignment (exp);
9724 if (modifier != EXPAND_WRITE
9725 && modifier != EXPAND_MEMORY
9726 && mode != BLKmode
9727 && align < GET_MODE_ALIGNMENT (mode)
9728 /* If the target does not have special handling for unaligned
9729 loads of mode then it can use regular moves for them. */
9730 && ((icode = optab_handler (movmisalign_optab, mode))
9731 != CODE_FOR_nothing))
9733 struct expand_operand ops[2];
9735 /* We've already validated the memory, and we're creating a
9736 new pseudo destination. The predicates really can't fail,
9737 nor can the generator. */
9738 create_output_operand (&ops[0], NULL_RTX, mode);
9739 create_fixed_operand (&ops[1], temp);
9740 expand_insn (icode, 2, ops);
9741 temp = ops[0].value;
9743 return temp;
9746 case MEM_REF:
9748 addr_space_t as
9749 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9750 machine_mode address_mode;
9751 tree base = TREE_OPERAND (exp, 0);
9752 gimple def_stmt;
9753 enum insn_code icode;
9754 unsigned align;
9755 /* Handle expansion of non-aliased memory with non-BLKmode. That
9756 might end up in a register. */
9757 if (mem_ref_refers_to_non_mem_p (exp))
9759 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9760 base = TREE_OPERAND (base, 0);
9761 if (offset == 0
9762 && tree_fits_uhwi_p (TYPE_SIZE (type))
9763 && (GET_MODE_BITSIZE (DECL_MODE (base))
9764 == tree_to_uhwi (TYPE_SIZE (type))))
9765 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9766 target, tmode, modifier);
9767 if (TYPE_MODE (type) == BLKmode)
9769 temp = assign_stack_temp (DECL_MODE (base),
9770 GET_MODE_SIZE (DECL_MODE (base)));
9771 store_expr (base, temp, 0, false);
9772 temp = adjust_address (temp, BLKmode, offset);
9773 set_mem_size (temp, int_size_in_bytes (type));
9774 return temp;
9776 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9777 bitsize_int (offset * BITS_PER_UNIT));
9778 return expand_expr (exp, target, tmode, modifier);
9780 address_mode = targetm.addr_space.address_mode (as);
9781 base = TREE_OPERAND (exp, 0);
9782 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9784 tree mask = gimple_assign_rhs2 (def_stmt);
9785 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9786 gimple_assign_rhs1 (def_stmt), mask);
9787 TREE_OPERAND (exp, 0) = base;
9789 align = get_object_alignment (exp);
9790 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9791 op0 = memory_address_addr_space (mode, op0, as);
9792 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9794 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9795 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9796 op0 = memory_address_addr_space (mode, op0, as);
9798 temp = gen_rtx_MEM (mode, op0);
9799 set_mem_attributes (temp, exp, 0);
9800 set_mem_addr_space (temp, as);
9801 if (TREE_THIS_VOLATILE (exp))
9802 MEM_VOLATILE_P (temp) = 1;
9803 if (modifier != EXPAND_WRITE
9804 && modifier != EXPAND_MEMORY
9805 && !inner_reference_p
9806 && mode != BLKmode
9807 && align < GET_MODE_ALIGNMENT (mode))
9809 if ((icode = optab_handler (movmisalign_optab, mode))
9810 != CODE_FOR_nothing)
9812 struct expand_operand ops[2];
9814 /* We've already validated the memory, and we're creating a
9815 new pseudo destination. The predicates really can't fail,
9816 nor can the generator. */
9817 create_output_operand (&ops[0], NULL_RTX, mode);
9818 create_fixed_operand (&ops[1], temp);
9819 expand_insn (icode, 2, ops);
9820 temp = ops[0].value;
9822 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9823 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9824 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9825 (modifier == EXPAND_STACK_PARM
9826 ? NULL_RTX : target),
9827 mode, mode);
9829 return temp;
9832 case ARRAY_REF:
9835 tree array = treeop0;
9836 tree index = treeop1;
9837 tree init;
9839 /* Fold an expression like: "foo"[2].
9840 This is not done in fold so it won't happen inside &.
9841 Don't fold if this is for wide characters since it's too
9842 difficult to do correctly and this is a very rare case. */
9844 if (modifier != EXPAND_CONST_ADDRESS
9845 && modifier != EXPAND_INITIALIZER
9846 && modifier != EXPAND_MEMORY)
9848 tree t = fold_read_from_constant_string (exp);
9850 if (t)
9851 return expand_expr (t, target, tmode, modifier);
9854 /* If this is a constant index into a constant array,
9855 just get the value from the array. Handle both the cases when
9856 we have an explicit constructor and when our operand is a variable
9857 that was declared const. */
9859 if (modifier != EXPAND_CONST_ADDRESS
9860 && modifier != EXPAND_INITIALIZER
9861 && modifier != EXPAND_MEMORY
9862 && TREE_CODE (array) == CONSTRUCTOR
9863 && ! TREE_SIDE_EFFECTS (array)
9864 && TREE_CODE (index) == INTEGER_CST)
9866 unsigned HOST_WIDE_INT ix;
9867 tree field, value;
9869 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9870 field, value)
9871 if (tree_int_cst_equal (field, index))
9873 if (!TREE_SIDE_EFFECTS (value))
9874 return expand_expr (fold (value), target, tmode, modifier);
9875 break;
9879 else if (optimize >= 1
9880 && modifier != EXPAND_CONST_ADDRESS
9881 && modifier != EXPAND_INITIALIZER
9882 && modifier != EXPAND_MEMORY
9883 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9884 && TREE_CODE (index) == INTEGER_CST
9885 && (TREE_CODE (array) == VAR_DECL
9886 || TREE_CODE (array) == CONST_DECL)
9887 && (init = ctor_for_folding (array)) != error_mark_node)
9889 if (init == NULL_TREE)
9891 tree value = build_zero_cst (type);
9892 if (TREE_CODE (value) == CONSTRUCTOR)
9894 /* If VALUE is a CONSTRUCTOR, this optimization is only
9895 useful if this doesn't store the CONSTRUCTOR into
9896 memory. If it does, it is more efficient to just
9897 load the data from the array directly. */
9898 rtx ret = expand_constructor (value, target,
9899 modifier, true);
9900 if (ret == NULL_RTX)
9901 value = NULL_TREE;
9904 if (value)
9905 return expand_expr (value, target, tmode, modifier);
9907 else if (TREE_CODE (init) == CONSTRUCTOR)
9909 unsigned HOST_WIDE_INT ix;
9910 tree field, value;
9912 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9913 field, value)
9914 if (tree_int_cst_equal (field, index))
9916 if (TREE_SIDE_EFFECTS (value))
9917 break;
9919 if (TREE_CODE (value) == CONSTRUCTOR)
9921 /* If VALUE is a CONSTRUCTOR, this
9922 optimization is only useful if
9923 this doesn't store the CONSTRUCTOR
9924 into memory. If it does, it is more
9925 efficient to just load the data from
9926 the array directly. */
9927 rtx ret = expand_constructor (value, target,
9928 modifier, true);
9929 if (ret == NULL_RTX)
9930 break;
9933 return
9934 expand_expr (fold (value), target, tmode, modifier);
9937 else if (TREE_CODE (init) == STRING_CST)
9939 tree low_bound = array_ref_low_bound (exp);
9940 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
9942 /* Optimize the special case of a zero lower bound.
9944 We convert the lower bound to sizetype to avoid problems
9945 with constant folding. E.g. suppose the lower bound is
9946 1 and its mode is QI. Without the conversion
9947 (ARRAY + (INDEX - (unsigned char)1))
9948 becomes
9949 (ARRAY + (-(unsigned char)1) + INDEX)
9950 which becomes
9951 (ARRAY + 255 + INDEX). Oops! */
9952 if (!integer_zerop (low_bound))
9953 index1 = size_diffop_loc (loc, index1,
9954 fold_convert_loc (loc, sizetype,
9955 low_bound));
9957 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
9959 tree type = TREE_TYPE (TREE_TYPE (init));
9960 machine_mode mode = TYPE_MODE (type);
9962 if (GET_MODE_CLASS (mode) == MODE_INT
9963 && GET_MODE_SIZE (mode) == 1)
9964 return gen_int_mode (TREE_STRING_POINTER (init)
9965 [TREE_INT_CST_LOW (index1)],
9966 mode);
9971 goto normal_inner_ref;
9973 case COMPONENT_REF:
9974 /* If the operand is a CONSTRUCTOR, we can just extract the
9975 appropriate field if it is present. */
9976 if (TREE_CODE (treeop0) == CONSTRUCTOR)
9978 unsigned HOST_WIDE_INT idx;
9979 tree field, value;
9981 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
9982 idx, field, value)
9983 if (field == treeop1
9984 /* We can normally use the value of the field in the
9985 CONSTRUCTOR. However, if this is a bitfield in
9986 an integral mode that we can fit in a HOST_WIDE_INT,
9987 we must mask only the number of bits in the bitfield,
9988 since this is done implicitly by the constructor. If
9989 the bitfield does not meet either of those conditions,
9990 we can't do this optimization. */
9991 && (! DECL_BIT_FIELD (field)
9992 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
9993 && (GET_MODE_PRECISION (DECL_MODE (field))
9994 <= HOST_BITS_PER_WIDE_INT))))
9996 if (DECL_BIT_FIELD (field)
9997 && modifier == EXPAND_STACK_PARM)
9998 target = 0;
9999 op0 = expand_expr (value, target, tmode, modifier);
10000 if (DECL_BIT_FIELD (field))
10002 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10003 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10005 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10007 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10008 imode);
10009 op0 = expand_and (imode, op0, op1, target);
10011 else
10013 int count = GET_MODE_PRECISION (imode) - bitsize;
10015 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10016 target, 0);
10017 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10018 target, 0);
10022 return op0;
10025 goto normal_inner_ref;
10027 case BIT_FIELD_REF:
10028 case ARRAY_RANGE_REF:
10029 normal_inner_ref:
10031 machine_mode mode1, mode2;
10032 HOST_WIDE_INT bitsize, bitpos;
10033 tree offset;
10034 int volatilep = 0, must_force_mem;
10035 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10036 &mode1, &unsignedp, &volatilep, true);
10037 rtx orig_op0, memloc;
10038 bool clear_mem_expr = false;
10040 /* If we got back the original object, something is wrong. Perhaps
10041 we are evaluating an expression too early. In any event, don't
10042 infinitely recurse. */
10043 gcc_assert (tem != exp);
10045 /* If TEM's type is a union of variable size, pass TARGET to the inner
10046 computation, since it will need a temporary and TARGET is known
10047 to have to do. This occurs in unchecked conversion in Ada. */
10048 orig_op0 = op0
10049 = expand_expr_real (tem,
10050 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10051 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10052 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10053 != INTEGER_CST)
10054 && modifier != EXPAND_STACK_PARM
10055 ? target : NULL_RTX),
10056 VOIDmode,
10057 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10058 NULL, true);
10060 /* If the field has a mode, we want to access it in the
10061 field's mode, not the computed mode.
10062 If a MEM has VOIDmode (external with incomplete type),
10063 use BLKmode for it instead. */
10064 if (MEM_P (op0))
10066 if (mode1 != VOIDmode)
10067 op0 = adjust_address (op0, mode1, 0);
10068 else if (GET_MODE (op0) == VOIDmode)
10069 op0 = adjust_address (op0, BLKmode, 0);
10072 mode2
10073 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10075 /* If we have either an offset, a BLKmode result, or a reference
10076 outside the underlying object, we must force it to memory.
10077 Such a case can occur in Ada if we have unchecked conversion
10078 of an expression from a scalar type to an aggregate type or
10079 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10080 passed a partially uninitialized object or a view-conversion
10081 to a larger size. */
10082 must_force_mem = (offset
10083 || mode1 == BLKmode
10084 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10086 /* Handle CONCAT first. */
10087 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10089 if (bitpos == 0
10090 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10091 return op0;
10092 if (bitpos == 0
10093 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10094 && bitsize)
10096 op0 = XEXP (op0, 0);
10097 mode2 = GET_MODE (op0);
10099 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10100 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10101 && bitpos
10102 && bitsize)
10104 op0 = XEXP (op0, 1);
10105 bitpos = 0;
10106 mode2 = GET_MODE (op0);
10108 else
10109 /* Otherwise force into memory. */
10110 must_force_mem = 1;
10113 /* If this is a constant, put it in a register if it is a legitimate
10114 constant and we don't need a memory reference. */
10115 if (CONSTANT_P (op0)
10116 && mode2 != BLKmode
10117 && targetm.legitimate_constant_p (mode2, op0)
10118 && !must_force_mem)
10119 op0 = force_reg (mode2, op0);
10121 /* Otherwise, if this is a constant, try to force it to the constant
10122 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10123 is a legitimate constant. */
10124 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10125 op0 = validize_mem (memloc);
10127 /* Otherwise, if this is a constant or the object is not in memory
10128 and need be, put it there. */
10129 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10131 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10132 emit_move_insn (memloc, op0);
10133 op0 = memloc;
10134 clear_mem_expr = true;
10137 if (offset)
10139 machine_mode address_mode;
10140 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10141 EXPAND_SUM);
10143 gcc_assert (MEM_P (op0));
10145 address_mode = get_address_mode (op0);
10146 if (GET_MODE (offset_rtx) != address_mode)
10148 /* We cannot be sure that the RTL in offset_rtx is valid outside
10149 of a memory address context, so force it into a register
10150 before attempting to convert it to the desired mode. */
10151 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10152 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10155 /* See the comment in expand_assignment for the rationale. */
10156 if (mode1 != VOIDmode
10157 && bitpos != 0
10158 && bitsize > 0
10159 && (bitpos % bitsize) == 0
10160 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10161 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10163 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10164 bitpos = 0;
10167 op0 = offset_address (op0, offset_rtx,
10168 highest_pow2_factor (offset));
10171 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10172 record its alignment as BIGGEST_ALIGNMENT. */
10173 if (MEM_P (op0) && bitpos == 0 && offset != 0
10174 && is_aligning_offset (offset, tem))
10175 set_mem_align (op0, BIGGEST_ALIGNMENT);
10177 /* Don't forget about volatility even if this is a bitfield. */
10178 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10180 if (op0 == orig_op0)
10181 op0 = copy_rtx (op0);
10183 MEM_VOLATILE_P (op0) = 1;
10186 /* In cases where an aligned union has an unaligned object
10187 as a field, we might be extracting a BLKmode value from
10188 an integer-mode (e.g., SImode) object. Handle this case
10189 by doing the extract into an object as wide as the field
10190 (which we know to be the width of a basic mode), then
10191 storing into memory, and changing the mode to BLKmode. */
10192 if (mode1 == VOIDmode
10193 || REG_P (op0) || GET_CODE (op0) == SUBREG
10194 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10195 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10196 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10197 && modifier != EXPAND_CONST_ADDRESS
10198 && modifier != EXPAND_INITIALIZER
10199 && modifier != EXPAND_MEMORY)
10200 /* If the bitfield is volatile and the bitsize
10201 is narrower than the access size of the bitfield,
10202 we need to extract bitfields from the access. */
10203 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10204 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10205 && mode1 != BLKmode
10206 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10207 /* If the field isn't aligned enough to fetch as a memref,
10208 fetch it as a bit field. */
10209 || (mode1 != BLKmode
10210 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10211 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10212 || (MEM_P (op0)
10213 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10214 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10215 && modifier != EXPAND_MEMORY
10216 && ((modifier == EXPAND_CONST_ADDRESS
10217 || modifier == EXPAND_INITIALIZER)
10218 ? STRICT_ALIGNMENT
10219 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10220 || (bitpos % BITS_PER_UNIT != 0)))
10221 /* If the type and the field are a constant size and the
10222 size of the type isn't the same size as the bitfield,
10223 we must use bitfield operations. */
10224 || (bitsize >= 0
10225 && TYPE_SIZE (TREE_TYPE (exp))
10226 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10227 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10228 bitsize)))
10230 machine_mode ext_mode = mode;
10232 if (ext_mode == BLKmode
10233 && ! (target != 0 && MEM_P (op0)
10234 && MEM_P (target)
10235 && bitpos % BITS_PER_UNIT == 0))
10236 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10238 if (ext_mode == BLKmode)
10240 if (target == 0)
10241 target = assign_temp (type, 1, 1);
10243 /* ??? Unlike the similar test a few lines below, this one is
10244 very likely obsolete. */
10245 if (bitsize == 0)
10246 return target;
10248 /* In this case, BITPOS must start at a byte boundary and
10249 TARGET, if specified, must be a MEM. */
10250 gcc_assert (MEM_P (op0)
10251 && (!target || MEM_P (target))
10252 && !(bitpos % BITS_PER_UNIT));
10254 emit_block_move (target,
10255 adjust_address (op0, VOIDmode,
10256 bitpos / BITS_PER_UNIT),
10257 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10258 / BITS_PER_UNIT),
10259 (modifier == EXPAND_STACK_PARM
10260 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10262 return target;
10265 /* If we have nothing to extract, the result will be 0 for targets
10266 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10267 return 0 for the sake of consistency, as reading a zero-sized
10268 bitfield is valid in Ada and the value is fully specified. */
10269 if (bitsize == 0)
10270 return const0_rtx;
10272 op0 = validize_mem (op0);
10274 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10275 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10277 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10278 (modifier == EXPAND_STACK_PARM
10279 ? NULL_RTX : target),
10280 ext_mode, ext_mode);
10282 /* If the result is a record type and BITSIZE is narrower than
10283 the mode of OP0, an integral mode, and this is a big endian
10284 machine, we must put the field into the high-order bits. */
10285 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10286 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10287 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10288 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10289 GET_MODE_BITSIZE (GET_MODE (op0))
10290 - bitsize, op0, 1);
10292 /* If the result type is BLKmode, store the data into a temporary
10293 of the appropriate type, but with the mode corresponding to the
10294 mode for the data we have (op0's mode). */
10295 if (mode == BLKmode)
10297 rtx new_rtx
10298 = assign_stack_temp_for_type (ext_mode,
10299 GET_MODE_BITSIZE (ext_mode),
10300 type);
10301 emit_move_insn (new_rtx, op0);
10302 op0 = copy_rtx (new_rtx);
10303 PUT_MODE (op0, BLKmode);
10306 return op0;
10309 /* If the result is BLKmode, use that to access the object
10310 now as well. */
10311 if (mode == BLKmode)
10312 mode1 = BLKmode;
10314 /* Get a reference to just this component. */
10315 if (modifier == EXPAND_CONST_ADDRESS
10316 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10317 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10318 else
10319 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10321 if (op0 == orig_op0)
10322 op0 = copy_rtx (op0);
10324 set_mem_attributes (op0, exp, 0);
10326 if (REG_P (XEXP (op0, 0)))
10327 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10329 /* If op0 is a temporary because the original expressions was forced
10330 to memory, clear MEM_EXPR so that the original expression cannot
10331 be marked as addressable through MEM_EXPR of the temporary. */
10332 if (clear_mem_expr)
10333 set_mem_expr (op0, NULL_TREE);
10335 MEM_VOLATILE_P (op0) |= volatilep;
10336 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10337 || modifier == EXPAND_CONST_ADDRESS
10338 || modifier == EXPAND_INITIALIZER)
10339 return op0;
10341 if (target == 0)
10342 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10344 convert_move (target, op0, unsignedp);
10345 return target;
10348 case OBJ_TYPE_REF:
10349 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10351 case CALL_EXPR:
10352 /* All valid uses of __builtin_va_arg_pack () are removed during
10353 inlining. */
10354 if (CALL_EXPR_VA_ARG_PACK (exp))
10355 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10357 tree fndecl = get_callee_fndecl (exp), attr;
10359 if (fndecl
10360 && (attr = lookup_attribute ("error",
10361 DECL_ATTRIBUTES (fndecl))) != NULL)
10362 error ("%Kcall to %qs declared with attribute error: %s",
10363 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10364 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10365 if (fndecl
10366 && (attr = lookup_attribute ("warning",
10367 DECL_ATTRIBUTES (fndecl))) != NULL)
10368 warning_at (tree_nonartificial_location (exp),
10369 0, "%Kcall to %qs declared with attribute warning: %s",
10370 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10371 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10373 /* Check for a built-in function. */
10374 if (fndecl && DECL_BUILT_IN (fndecl))
10376 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10377 if (CALL_WITH_BOUNDS_P (exp))
10378 return expand_builtin_with_bounds (exp, target, subtarget,
10379 tmode, ignore);
10380 else
10381 return expand_builtin (exp, target, subtarget, tmode, ignore);
10384 return expand_call (exp, target, ignore);
10386 case VIEW_CONVERT_EXPR:
10387 op0 = NULL_RTX;
10389 /* If we are converting to BLKmode, try to avoid an intermediate
10390 temporary by fetching an inner memory reference. */
10391 if (mode == BLKmode
10392 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10393 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10394 && handled_component_p (treeop0))
10396 machine_mode mode1;
10397 HOST_WIDE_INT bitsize, bitpos;
10398 tree offset;
10399 int unsignedp;
10400 int volatilep = 0;
10401 tree tem
10402 = get_inner_reference (treeop0, &bitsize, &bitpos,
10403 &offset, &mode1, &unsignedp, &volatilep,
10404 true);
10405 rtx orig_op0;
10407 /* ??? We should work harder and deal with non-zero offsets. */
10408 if (!offset
10409 && (bitpos % BITS_PER_UNIT) == 0
10410 && bitsize >= 0
10411 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10413 /* See the normal_inner_ref case for the rationale. */
10414 orig_op0
10415 = expand_expr_real (tem,
10416 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10417 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10418 != INTEGER_CST)
10419 && modifier != EXPAND_STACK_PARM
10420 ? target : NULL_RTX),
10421 VOIDmode,
10422 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10423 NULL, true);
10425 if (MEM_P (orig_op0))
10427 op0 = orig_op0;
10429 /* Get a reference to just this component. */
10430 if (modifier == EXPAND_CONST_ADDRESS
10431 || modifier == EXPAND_SUM
10432 || modifier == EXPAND_INITIALIZER)
10433 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10434 else
10435 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10437 if (op0 == orig_op0)
10438 op0 = copy_rtx (op0);
10440 set_mem_attributes (op0, treeop0, 0);
10441 if (REG_P (XEXP (op0, 0)))
10442 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10444 MEM_VOLATILE_P (op0) |= volatilep;
10449 if (!op0)
10450 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10451 NULL, inner_reference_p);
10453 /* If the input and output modes are both the same, we are done. */
10454 if (mode == GET_MODE (op0))
10456 /* If neither mode is BLKmode, and both modes are the same size
10457 then we can use gen_lowpart. */
10458 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10459 && (GET_MODE_PRECISION (mode)
10460 == GET_MODE_PRECISION (GET_MODE (op0)))
10461 && !COMPLEX_MODE_P (GET_MODE (op0)))
10463 if (GET_CODE (op0) == SUBREG)
10464 op0 = force_reg (GET_MODE (op0), op0);
10465 temp = gen_lowpart_common (mode, op0);
10466 if (temp)
10467 op0 = temp;
10468 else
10470 if (!REG_P (op0) && !MEM_P (op0))
10471 op0 = force_reg (GET_MODE (op0), op0);
10472 op0 = gen_lowpart (mode, op0);
10475 /* If both types are integral, convert from one mode to the other. */
10476 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10477 op0 = convert_modes (mode, GET_MODE (op0), op0,
10478 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10479 /* If the output type is a bit-field type, do an extraction. */
10480 else if (reduce_bit_field)
10481 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10482 TYPE_UNSIGNED (type), NULL_RTX,
10483 mode, mode);
10484 /* As a last resort, spill op0 to memory, and reload it in a
10485 different mode. */
10486 else if (!MEM_P (op0))
10488 /* If the operand is not a MEM, force it into memory. Since we
10489 are going to be changing the mode of the MEM, don't call
10490 force_const_mem for constants because we don't allow pool
10491 constants to change mode. */
10492 tree inner_type = TREE_TYPE (treeop0);
10494 gcc_assert (!TREE_ADDRESSABLE (exp));
10496 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10497 target
10498 = assign_stack_temp_for_type
10499 (TYPE_MODE (inner_type),
10500 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10502 emit_move_insn (target, op0);
10503 op0 = target;
10506 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10507 output type is such that the operand is known to be aligned, indicate
10508 that it is. Otherwise, we need only be concerned about alignment for
10509 non-BLKmode results. */
10510 if (MEM_P (op0))
10512 enum insn_code icode;
10514 if (TYPE_ALIGN_OK (type))
10516 /* ??? Copying the MEM without substantially changing it might
10517 run afoul of the code handling volatile memory references in
10518 store_expr, which assumes that TARGET is returned unmodified
10519 if it has been used. */
10520 op0 = copy_rtx (op0);
10521 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10523 else if (modifier != EXPAND_WRITE
10524 && modifier != EXPAND_MEMORY
10525 && !inner_reference_p
10526 && mode != BLKmode
10527 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10529 /* If the target does have special handling for unaligned
10530 loads of mode then use them. */
10531 if ((icode = optab_handler (movmisalign_optab, mode))
10532 != CODE_FOR_nothing)
10534 rtx reg;
10536 op0 = adjust_address (op0, mode, 0);
10537 /* We've already validated the memory, and we're creating a
10538 new pseudo destination. The predicates really can't
10539 fail. */
10540 reg = gen_reg_rtx (mode);
10542 /* Nor can the insn generator. */
10543 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10544 emit_insn (insn);
10545 return reg;
10547 else if (STRICT_ALIGNMENT)
10549 tree inner_type = TREE_TYPE (treeop0);
10550 HOST_WIDE_INT temp_size
10551 = MAX (int_size_in_bytes (inner_type),
10552 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10553 rtx new_rtx
10554 = assign_stack_temp_for_type (mode, temp_size, type);
10555 rtx new_with_op0_mode
10556 = adjust_address (new_rtx, GET_MODE (op0), 0);
10558 gcc_assert (!TREE_ADDRESSABLE (exp));
10560 if (GET_MODE (op0) == BLKmode)
10561 emit_block_move (new_with_op0_mode, op0,
10562 GEN_INT (GET_MODE_SIZE (mode)),
10563 (modifier == EXPAND_STACK_PARM
10564 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10565 else
10566 emit_move_insn (new_with_op0_mode, op0);
10568 op0 = new_rtx;
10572 op0 = adjust_address (op0, mode, 0);
10575 return op0;
10577 case MODIFY_EXPR:
10579 tree lhs = treeop0;
10580 tree rhs = treeop1;
10581 gcc_assert (ignore);
10583 /* Check for |= or &= of a bitfield of size one into another bitfield
10584 of size 1. In this case, (unless we need the result of the
10585 assignment) we can do this more efficiently with a
10586 test followed by an assignment, if necessary.
10588 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10589 things change so we do, this code should be enhanced to
10590 support it. */
10591 if (TREE_CODE (lhs) == COMPONENT_REF
10592 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10593 || TREE_CODE (rhs) == BIT_AND_EXPR)
10594 && TREE_OPERAND (rhs, 0) == lhs
10595 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10596 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10597 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10599 rtx_code_label *label = gen_label_rtx ();
10600 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10601 do_jump (TREE_OPERAND (rhs, 1),
10602 value ? label : 0,
10603 value ? 0 : label, -1);
10604 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10605 false);
10606 do_pending_stack_adjust ();
10607 emit_label (label);
10608 return const0_rtx;
10611 expand_assignment (lhs, rhs, false);
10612 return const0_rtx;
10615 case ADDR_EXPR:
10616 return expand_expr_addr_expr (exp, target, tmode, modifier);
10618 case REALPART_EXPR:
10619 op0 = expand_normal (treeop0);
10620 return read_complex_part (op0, false);
10622 case IMAGPART_EXPR:
10623 op0 = expand_normal (treeop0);
10624 return read_complex_part (op0, true);
10626 case RETURN_EXPR:
10627 case LABEL_EXPR:
10628 case GOTO_EXPR:
10629 case SWITCH_EXPR:
10630 case ASM_EXPR:
10631 /* Expanded in cfgexpand.c. */
10632 gcc_unreachable ();
10634 case TRY_CATCH_EXPR:
10635 case CATCH_EXPR:
10636 case EH_FILTER_EXPR:
10637 case TRY_FINALLY_EXPR:
10638 /* Lowered by tree-eh.c. */
10639 gcc_unreachable ();
10641 case WITH_CLEANUP_EXPR:
10642 case CLEANUP_POINT_EXPR:
10643 case TARGET_EXPR:
10644 case CASE_LABEL_EXPR:
10645 case VA_ARG_EXPR:
10646 case BIND_EXPR:
10647 case INIT_EXPR:
10648 case CONJ_EXPR:
10649 case COMPOUND_EXPR:
10650 case PREINCREMENT_EXPR:
10651 case PREDECREMENT_EXPR:
10652 case POSTINCREMENT_EXPR:
10653 case POSTDECREMENT_EXPR:
10654 case LOOP_EXPR:
10655 case EXIT_EXPR:
10656 case COMPOUND_LITERAL_EXPR:
10657 /* Lowered by gimplify.c. */
10658 gcc_unreachable ();
10660 case FDESC_EXPR:
10661 /* Function descriptors are not valid except for as
10662 initialization constants, and should not be expanded. */
10663 gcc_unreachable ();
10665 case WITH_SIZE_EXPR:
10666 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10667 have pulled out the size to use in whatever context it needed. */
10668 return expand_expr_real (treeop0, original_target, tmode,
10669 modifier, alt_rtl, inner_reference_p);
10671 default:
10672 return expand_expr_real_2 (&ops, target, tmode, modifier);
10676 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10677 signedness of TYPE), possibly returning the result in TARGET. */
10678 static rtx
10679 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10681 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10682 if (target && GET_MODE (target) != GET_MODE (exp))
10683 target = 0;
10684 /* For constant values, reduce using build_int_cst_type. */
10685 if (CONST_INT_P (exp))
10687 HOST_WIDE_INT value = INTVAL (exp);
10688 tree t = build_int_cst_type (type, value);
10689 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10691 else if (TYPE_UNSIGNED (type))
10693 machine_mode mode = GET_MODE (exp);
10694 rtx mask = immed_wide_int_const
10695 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10696 return expand_and (mode, exp, mask, target);
10698 else
10700 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10701 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10702 exp, count, target, 0);
10703 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10704 exp, count, target, 0);
10708 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10709 when applied to the address of EXP produces an address known to be
10710 aligned more than BIGGEST_ALIGNMENT. */
10712 static int
10713 is_aligning_offset (const_tree offset, const_tree exp)
10715 /* Strip off any conversions. */
10716 while (CONVERT_EXPR_P (offset))
10717 offset = TREE_OPERAND (offset, 0);
10719 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10720 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10721 if (TREE_CODE (offset) != BIT_AND_EXPR
10722 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10723 || compare_tree_int (TREE_OPERAND (offset, 1),
10724 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10725 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10726 return 0;
10728 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10729 It must be NEGATE_EXPR. Then strip any more conversions. */
10730 offset = TREE_OPERAND (offset, 0);
10731 while (CONVERT_EXPR_P (offset))
10732 offset = TREE_OPERAND (offset, 0);
10734 if (TREE_CODE (offset) != NEGATE_EXPR)
10735 return 0;
10737 offset = TREE_OPERAND (offset, 0);
10738 while (CONVERT_EXPR_P (offset))
10739 offset = TREE_OPERAND (offset, 0);
10741 /* This must now be the address of EXP. */
10742 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10745 /* Return the tree node if an ARG corresponds to a string constant or zero
10746 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10747 in bytes within the string that ARG is accessing. The type of the
10748 offset will be `sizetype'. */
10750 tree
10751 string_constant (tree arg, tree *ptr_offset)
10753 tree array, offset, lower_bound;
10754 STRIP_NOPS (arg);
10756 if (TREE_CODE (arg) == ADDR_EXPR)
10758 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10760 *ptr_offset = size_zero_node;
10761 return TREE_OPERAND (arg, 0);
10763 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10765 array = TREE_OPERAND (arg, 0);
10766 offset = size_zero_node;
10768 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10770 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10771 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10772 if (TREE_CODE (array) != STRING_CST
10773 && TREE_CODE (array) != VAR_DECL)
10774 return 0;
10776 /* Check if the array has a nonzero lower bound. */
10777 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10778 if (!integer_zerop (lower_bound))
10780 /* If the offset and base aren't both constants, return 0. */
10781 if (TREE_CODE (lower_bound) != INTEGER_CST)
10782 return 0;
10783 if (TREE_CODE (offset) != INTEGER_CST)
10784 return 0;
10785 /* Adjust offset by the lower bound. */
10786 offset = size_diffop (fold_convert (sizetype, offset),
10787 fold_convert (sizetype, lower_bound));
10790 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10792 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10793 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10794 if (TREE_CODE (array) != ADDR_EXPR)
10795 return 0;
10796 array = TREE_OPERAND (array, 0);
10797 if (TREE_CODE (array) != STRING_CST
10798 && TREE_CODE (array) != VAR_DECL)
10799 return 0;
10801 else
10802 return 0;
10804 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10806 tree arg0 = TREE_OPERAND (arg, 0);
10807 tree arg1 = TREE_OPERAND (arg, 1);
10809 STRIP_NOPS (arg0);
10810 STRIP_NOPS (arg1);
10812 if (TREE_CODE (arg0) == ADDR_EXPR
10813 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10814 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10816 array = TREE_OPERAND (arg0, 0);
10817 offset = arg1;
10819 else if (TREE_CODE (arg1) == ADDR_EXPR
10820 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10821 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10823 array = TREE_OPERAND (arg1, 0);
10824 offset = arg0;
10826 else
10827 return 0;
10829 else
10830 return 0;
10832 if (TREE_CODE (array) == STRING_CST)
10834 *ptr_offset = fold_convert (sizetype, offset);
10835 return array;
10837 else if (TREE_CODE (array) == VAR_DECL
10838 || TREE_CODE (array) == CONST_DECL)
10840 int length;
10841 tree init = ctor_for_folding (array);
10843 /* Variables initialized to string literals can be handled too. */
10844 if (init == error_mark_node
10845 || !init
10846 || TREE_CODE (init) != STRING_CST)
10847 return 0;
10849 /* Avoid const char foo[4] = "abcde"; */
10850 if (DECL_SIZE_UNIT (array) == NULL_TREE
10851 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10852 || (length = TREE_STRING_LENGTH (init)) <= 0
10853 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10854 return 0;
10856 /* If variable is bigger than the string literal, OFFSET must be constant
10857 and inside of the bounds of the string literal. */
10858 offset = fold_convert (sizetype, offset);
10859 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10860 && (! tree_fits_uhwi_p (offset)
10861 || compare_tree_int (offset, length) >= 0))
10862 return 0;
10864 *ptr_offset = offset;
10865 return init;
10868 return 0;
10871 /* Generate code to calculate OPS, and exploded expression
10872 using a store-flag instruction and return an rtx for the result.
10873 OPS reflects a comparison.
10875 If TARGET is nonzero, store the result there if convenient.
10877 Return zero if there is no suitable set-flag instruction
10878 available on this machine.
10880 Once expand_expr has been called on the arguments of the comparison,
10881 we are committed to doing the store flag, since it is not safe to
10882 re-evaluate the expression. We emit the store-flag insn by calling
10883 emit_store_flag, but only expand the arguments if we have a reason
10884 to believe that emit_store_flag will be successful. If we think that
10885 it will, but it isn't, we have to simulate the store-flag with a
10886 set/jump/set sequence. */
10888 static rtx
10889 do_store_flag (sepops ops, rtx target, machine_mode mode)
10891 enum rtx_code code;
10892 tree arg0, arg1, type;
10893 machine_mode operand_mode;
10894 int unsignedp;
10895 rtx op0, op1;
10896 rtx subtarget = target;
10897 location_t loc = ops->location;
10899 arg0 = ops->op0;
10900 arg1 = ops->op1;
10902 /* Don't crash if the comparison was erroneous. */
10903 if (arg0 == error_mark_node || arg1 == error_mark_node)
10904 return const0_rtx;
10906 type = TREE_TYPE (arg0);
10907 operand_mode = TYPE_MODE (type);
10908 unsignedp = TYPE_UNSIGNED (type);
10910 /* We won't bother with BLKmode store-flag operations because it would mean
10911 passing a lot of information to emit_store_flag. */
10912 if (operand_mode == BLKmode)
10913 return 0;
10915 /* We won't bother with store-flag operations involving function pointers
10916 when function pointers must be canonicalized before comparisons. */
10917 if (targetm.have_canonicalize_funcptr_for_compare ()
10918 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10919 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10920 == FUNCTION_TYPE))
10921 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10922 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10923 == FUNCTION_TYPE))))
10924 return 0;
10926 STRIP_NOPS (arg0);
10927 STRIP_NOPS (arg1);
10929 /* For vector typed comparisons emit code to generate the desired
10930 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10931 expander for this. */
10932 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10934 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10935 tree if_true = constant_boolean_node (true, ops->type);
10936 tree if_false = constant_boolean_node (false, ops->type);
10937 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10940 /* Get the rtx comparison code to use. We know that EXP is a comparison
10941 operation of some type. Some comparisons against 1 and -1 can be
10942 converted to comparisons with zero. Do so here so that the tests
10943 below will be aware that we have a comparison with zero. These
10944 tests will not catch constants in the first operand, but constants
10945 are rarely passed as the first operand. */
10947 switch (ops->code)
10949 case EQ_EXPR:
10950 code = EQ;
10951 break;
10952 case NE_EXPR:
10953 code = NE;
10954 break;
10955 case LT_EXPR:
10956 if (integer_onep (arg1))
10957 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10958 else
10959 code = unsignedp ? LTU : LT;
10960 break;
10961 case LE_EXPR:
10962 if (! unsignedp && integer_all_onesp (arg1))
10963 arg1 = integer_zero_node, code = LT;
10964 else
10965 code = unsignedp ? LEU : LE;
10966 break;
10967 case GT_EXPR:
10968 if (! unsignedp && integer_all_onesp (arg1))
10969 arg1 = integer_zero_node, code = GE;
10970 else
10971 code = unsignedp ? GTU : GT;
10972 break;
10973 case GE_EXPR:
10974 if (integer_onep (arg1))
10975 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
10976 else
10977 code = unsignedp ? GEU : GE;
10978 break;
10980 case UNORDERED_EXPR:
10981 code = UNORDERED;
10982 break;
10983 case ORDERED_EXPR:
10984 code = ORDERED;
10985 break;
10986 case UNLT_EXPR:
10987 code = UNLT;
10988 break;
10989 case UNLE_EXPR:
10990 code = UNLE;
10991 break;
10992 case UNGT_EXPR:
10993 code = UNGT;
10994 break;
10995 case UNGE_EXPR:
10996 code = UNGE;
10997 break;
10998 case UNEQ_EXPR:
10999 code = UNEQ;
11000 break;
11001 case LTGT_EXPR:
11002 code = LTGT;
11003 break;
11005 default:
11006 gcc_unreachable ();
11009 /* Put a constant second. */
11010 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11011 || TREE_CODE (arg0) == FIXED_CST)
11013 std::swap (arg0, arg1);
11014 code = swap_condition (code);
11017 /* If this is an equality or inequality test of a single bit, we can
11018 do this by shifting the bit being tested to the low-order bit and
11019 masking the result with the constant 1. If the condition was EQ,
11020 we xor it with 1. This does not require an scc insn and is faster
11021 than an scc insn even if we have it.
11023 The code to make this transformation was moved into fold_single_bit_test,
11024 so we just call into the folder and expand its result. */
11026 if ((code == NE || code == EQ)
11027 && integer_zerop (arg1)
11028 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11030 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11031 if (srcstmt
11032 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11034 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11035 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11036 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11037 gimple_assign_rhs1 (srcstmt),
11038 gimple_assign_rhs2 (srcstmt));
11039 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11040 if (temp)
11041 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11045 if (! get_subtarget (target)
11046 || GET_MODE (subtarget) != operand_mode)
11047 subtarget = 0;
11049 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11051 if (target == 0)
11052 target = gen_reg_rtx (mode);
11054 /* Try a cstore if possible. */
11055 return emit_store_flag_force (target, code, op0, op1,
11056 operand_mode, unsignedp,
11057 (TYPE_PRECISION (ops->type) == 1
11058 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11062 /* Stubs in case we haven't got a casesi insn. */
11063 #ifndef HAVE_casesi
11064 # define HAVE_casesi 0
11065 # define gen_casesi(a, b, c, d, e) (0)
11066 # define CODE_FOR_casesi CODE_FOR_nothing
11067 #endif
11069 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11070 0 otherwise (i.e. if there is no casesi instruction).
11072 DEFAULT_PROBABILITY is the probability of jumping to the default
11073 label. */
11075 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11076 rtx table_label, rtx default_label, rtx fallback_label,
11077 int default_probability)
11079 struct expand_operand ops[5];
11080 machine_mode index_mode = SImode;
11081 rtx op1, op2, index;
11083 if (! HAVE_casesi)
11084 return 0;
11086 /* Convert the index to SImode. */
11087 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11089 machine_mode omode = TYPE_MODE (index_type);
11090 rtx rangertx = expand_normal (range);
11092 /* We must handle the endpoints in the original mode. */
11093 index_expr = build2 (MINUS_EXPR, index_type,
11094 index_expr, minval);
11095 minval = integer_zero_node;
11096 index = expand_normal (index_expr);
11097 if (default_label)
11098 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11099 omode, 1, default_label,
11100 default_probability);
11101 /* Now we can safely truncate. */
11102 index = convert_to_mode (index_mode, index, 0);
11104 else
11106 if (TYPE_MODE (index_type) != index_mode)
11108 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11109 index_expr = fold_convert (index_type, index_expr);
11112 index = expand_normal (index_expr);
11115 do_pending_stack_adjust ();
11117 op1 = expand_normal (minval);
11118 op2 = expand_normal (range);
11120 create_input_operand (&ops[0], index, index_mode);
11121 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11122 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11123 create_fixed_operand (&ops[3], table_label);
11124 create_fixed_operand (&ops[4], (default_label
11125 ? default_label
11126 : fallback_label));
11127 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11128 return 1;
11131 /* Attempt to generate a tablejump instruction; same concept. */
11132 /* Subroutine of the next function.
11134 INDEX is the value being switched on, with the lowest value
11135 in the table already subtracted.
11136 MODE is its expected mode (needed if INDEX is constant).
11137 RANGE is the length of the jump table.
11138 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11140 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11141 index value is out of range.
11142 DEFAULT_PROBABILITY is the probability of jumping to
11143 the default label. */
11145 static void
11146 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11147 rtx default_label, int default_probability)
11149 rtx temp, vector;
11151 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11152 cfun->cfg->max_jumptable_ents = INTVAL (range);
11154 /* Do an unsigned comparison (in the proper mode) between the index
11155 expression and the value which represents the length of the range.
11156 Since we just finished subtracting the lower bound of the range
11157 from the index expression, this comparison allows us to simultaneously
11158 check that the original index expression value is both greater than
11159 or equal to the minimum value of the range and less than or equal to
11160 the maximum value of the range. */
11162 if (default_label)
11163 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11164 default_label, default_probability);
11167 /* If index is in range, it must fit in Pmode.
11168 Convert to Pmode so we can index with it. */
11169 if (mode != Pmode)
11170 index = convert_to_mode (Pmode, index, 1);
11172 /* Don't let a MEM slip through, because then INDEX that comes
11173 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11174 and break_out_memory_refs will go to work on it and mess it up. */
11175 #ifdef PIC_CASE_VECTOR_ADDRESS
11176 if (flag_pic && !REG_P (index))
11177 index = copy_to_mode_reg (Pmode, index);
11178 #endif
11180 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11181 GET_MODE_SIZE, because this indicates how large insns are. The other
11182 uses should all be Pmode, because they are addresses. This code
11183 could fail if addresses and insns are not the same size. */
11184 index = simplify_gen_binary (MULT, Pmode, index,
11185 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11186 Pmode));
11187 index = simplify_gen_binary (PLUS, Pmode, index,
11188 gen_rtx_LABEL_REF (Pmode, table_label));
11190 #ifdef PIC_CASE_VECTOR_ADDRESS
11191 if (flag_pic)
11192 index = PIC_CASE_VECTOR_ADDRESS (index);
11193 else
11194 #endif
11195 index = memory_address (CASE_VECTOR_MODE, index);
11196 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11197 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11198 convert_move (temp, vector, 0);
11200 emit_jump_insn (gen_tablejump (temp, table_label));
11202 /* If we are generating PIC code or if the table is PC-relative, the
11203 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11204 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11205 emit_barrier ();
11209 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11210 rtx table_label, rtx default_label, int default_probability)
11212 rtx index;
11214 if (! HAVE_tablejump)
11215 return 0;
11217 index_expr = fold_build2 (MINUS_EXPR, index_type,
11218 fold_convert (index_type, index_expr),
11219 fold_convert (index_type, minval));
11220 index = expand_normal (index_expr);
11221 do_pending_stack_adjust ();
11223 do_tablejump (index, TYPE_MODE (index_type),
11224 convert_modes (TYPE_MODE (index_type),
11225 TYPE_MODE (TREE_TYPE (range)),
11226 expand_normal (range),
11227 TYPE_UNSIGNED (TREE_TYPE (range))),
11228 table_label, default_label, default_probability);
11229 return 1;
11232 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11233 static rtx
11234 const_vector_from_tree (tree exp)
11236 rtvec v;
11237 unsigned i;
11238 int units;
11239 tree elt;
11240 machine_mode inner, mode;
11242 mode = TYPE_MODE (TREE_TYPE (exp));
11244 if (initializer_zerop (exp))
11245 return CONST0_RTX (mode);
11247 units = GET_MODE_NUNITS (mode);
11248 inner = GET_MODE_INNER (mode);
11250 v = rtvec_alloc (units);
11252 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11254 elt = VECTOR_CST_ELT (exp, i);
11256 if (TREE_CODE (elt) == REAL_CST)
11257 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11258 inner);
11259 else if (TREE_CODE (elt) == FIXED_CST)
11260 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11261 inner);
11262 else
11263 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11266 return gen_rtx_CONST_VECTOR (mode, v);
11269 /* Build a decl for a personality function given a language prefix. */
11271 tree
11272 build_personality_function (const char *lang)
11274 const char *unwind_and_version;
11275 tree decl, type;
11276 char *name;
11278 switch (targetm_common.except_unwind_info (&global_options))
11280 case UI_NONE:
11281 return NULL;
11282 case UI_SJLJ:
11283 unwind_and_version = "_sj0";
11284 break;
11285 case UI_DWARF2:
11286 case UI_TARGET:
11287 unwind_and_version = "_v0";
11288 break;
11289 case UI_SEH:
11290 unwind_and_version = "_seh0";
11291 break;
11292 default:
11293 gcc_unreachable ();
11296 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11298 type = build_function_type_list (integer_type_node, integer_type_node,
11299 long_long_unsigned_type_node,
11300 ptr_type_node, ptr_type_node, NULL_TREE);
11301 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11302 get_identifier (name), type);
11303 DECL_ARTIFICIAL (decl) = 1;
11304 DECL_EXTERNAL (decl) = 1;
11305 TREE_PUBLIC (decl) = 1;
11307 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11308 are the flags assigned by targetm.encode_section_info. */
11309 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11311 return decl;
11314 /* Extracts the personality function of DECL and returns the corresponding
11315 libfunc. */
11318 get_personality_function (tree decl)
11320 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11321 enum eh_personality_kind pk;
11323 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11324 if (pk == eh_personality_none)
11325 return NULL;
11327 if (!personality
11328 && pk == eh_personality_any)
11329 personality = lang_hooks.eh_personality ();
11331 if (pk == eh_personality_lang)
11332 gcc_assert (personality != NULL_TREE);
11334 return XEXP (DECL_RTL (personality), 0);
11337 /* Returns a tree for the size of EXP in bytes. */
11339 static tree
11340 tree_expr_size (const_tree exp)
11342 if (DECL_P (exp)
11343 && DECL_SIZE_UNIT (exp) != 0)
11344 return DECL_SIZE_UNIT (exp);
11345 else
11346 return size_in_bytes (TREE_TYPE (exp));
11349 /* Return an rtx for the size in bytes of the value of EXP. */
11352 expr_size (tree exp)
11354 tree size;
11356 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11357 size = TREE_OPERAND (exp, 1);
11358 else
11360 size = tree_expr_size (exp);
11361 gcc_assert (size);
11362 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11365 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11368 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11369 if the size can vary or is larger than an integer. */
11371 static HOST_WIDE_INT
11372 int_expr_size (tree exp)
11374 tree size;
11376 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11377 size = TREE_OPERAND (exp, 1);
11378 else
11380 size = tree_expr_size (exp);
11381 gcc_assert (size);
11384 if (size == 0 || !tree_fits_shwi_p (size))
11385 return -1;
11387 return tree_to_shwi (size);
11390 #include "gt-expr.h"