[PR64164] Drop copyrename, use coalescible partition as base when optimizing.
[official-gcc.git] / gcc / expr.c
blobfd9a0763e8ce4445bc2b89218678b38514c7492d
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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "stringpool.h"
31 #include "stor-layout.h"
32 #include "attribs.h"
33 #include "varasm.h"
34 #include "flags.h"
35 #include "regs.h"
36 #include "hard-reg-set.h"
37 #include "except.h"
38 #include "function.h"
39 #include "insn-config.h"
40 #include "insn-attr.h"
41 #include "expmed.h"
42 #include "dojump.h"
43 #include "explow.h"
44 #include "calls.h"
45 #include "emit-rtl.h"
46 #include "stmt.h"
47 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
48 #include "expr.h"
49 #include "insn-codes.h"
50 #include "optabs.h"
51 #include "libfuncs.h"
52 #include "recog.h"
53 #include "reload.h"
54 #include "typeclass.h"
55 #include "toplev.h"
56 #include "langhooks.h"
57 #include "intl.h"
58 #include "tm_p.h"
59 #include "tree-iterator.h"
60 #include "predict.h"
61 #include "dominance.h"
62 #include "cfg.h"
63 #include "basic-block.h"
64 #include "tree-ssa-alias.h"
65 #include "internal-fn.h"
66 #include "gimple-expr.h"
67 #include "is-a.h"
68 #include "gimple.h"
69 #include "gimple-ssa.h"
70 #include "plugin-api.h"
71 #include "ipa-ref.h"
72 #include "cgraph.h"
73 #include "tree-ssanames.h"
74 #include "target.h"
75 #include "common/common-target.h"
76 #include "timevar.h"
77 #include "df.h"
78 #include "diagnostic.h"
79 #include "tree-ssa-live.h"
80 #include "tree-outof-ssa.h"
81 #include "target-globals.h"
82 #include "params.h"
83 #include "tree-ssa-address.h"
84 #include "cfgexpand.h"
85 #include "builtins.h"
86 #include "tree-chkp.h"
87 #include "rtl-chkp.h"
88 #include "ccmp.h"
91 /* If this is nonzero, we do not bother generating VOLATILE
92 around volatile memory references, and we are willing to
93 output indirect addresses. If cse is to follow, we reject
94 indirect addresses so a useful potential cse is generated;
95 if it is used only once, instruction combination will produce
96 the same indirect address eventually. */
97 int cse_not_expected;
99 /* This structure is used by move_by_pieces to describe the move to
100 be performed. */
101 struct move_by_pieces_d
103 rtx to;
104 rtx to_addr;
105 int autinc_to;
106 int explicit_inc_to;
107 rtx from;
108 rtx from_addr;
109 int autinc_from;
110 int explicit_inc_from;
111 unsigned HOST_WIDE_INT len;
112 HOST_WIDE_INT offset;
113 int reverse;
116 /* This structure is used by store_by_pieces to describe the clear to
117 be performed. */
119 struct store_by_pieces_d
121 rtx to;
122 rtx to_addr;
123 int autinc_to;
124 int explicit_inc_to;
125 unsigned HOST_WIDE_INT len;
126 HOST_WIDE_INT offset;
127 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
128 void *constfundata;
129 int reverse;
132 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
133 struct move_by_pieces_d *);
134 static bool block_move_libcall_safe_for_call_parm (void);
135 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
136 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
137 unsigned HOST_WIDE_INT);
138 static tree emit_block_move_libcall_fn (int);
139 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
140 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
141 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
142 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
143 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
144 struct store_by_pieces_d *);
145 static tree clear_storage_libcall_fn (int);
146 static rtx_insn *compress_float_constant (rtx, rtx);
147 static rtx get_subtarget (rtx);
148 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
149 HOST_WIDE_INT, machine_mode,
150 tree, int, alias_set_type);
151 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
152 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
153 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
154 machine_mode, tree, alias_set_type, bool);
156 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
158 static int is_aligning_offset (const_tree, const_tree);
159 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
160 static rtx do_store_flag (sepops, rtx, machine_mode);
161 #ifdef PUSH_ROUNDING
162 static void emit_single_push_insn (machine_mode, rtx, tree);
163 #endif
164 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
165 static rtx const_vector_from_tree (tree);
166 static tree tree_expr_size (const_tree);
167 static HOST_WIDE_INT int_expr_size (tree);
170 /* This is run to set up which modes can be used
171 directly in memory and to initialize the block move optab. It is run
172 at the beginning of compilation and when the target is reinitialized. */
174 void
175 init_expr_target (void)
177 rtx insn, pat;
178 machine_mode mode;
179 int num_clobbers;
180 rtx mem, mem1;
181 rtx reg;
183 /* Try indexing by frame ptr and try by stack ptr.
184 It is known that on the Convex the stack ptr isn't a valid index.
185 With luck, one or the other is valid on any machine. */
186 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
187 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
189 /* A scratch register we can modify in-place below to avoid
190 useless RTL allocations. */
191 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
193 insn = rtx_alloc (INSN);
194 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
195 PATTERN (insn) = pat;
197 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
198 mode = (machine_mode) ((int) mode + 1))
200 int regno;
202 direct_load[(int) mode] = direct_store[(int) mode] = 0;
203 PUT_MODE (mem, mode);
204 PUT_MODE (mem1, mode);
206 /* See if there is some register that can be used in this mode and
207 directly loaded or stored from memory. */
209 if (mode != VOIDmode && mode != BLKmode)
210 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
211 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
212 regno++)
214 if (! HARD_REGNO_MODE_OK (regno, mode))
215 continue;
217 set_mode_and_regno (reg, mode, regno);
219 SET_SRC (pat) = mem;
220 SET_DEST (pat) = reg;
221 if (recog (pat, insn, &num_clobbers) >= 0)
222 direct_load[(int) mode] = 1;
224 SET_SRC (pat) = mem1;
225 SET_DEST (pat) = reg;
226 if (recog (pat, insn, &num_clobbers) >= 0)
227 direct_load[(int) mode] = 1;
229 SET_SRC (pat) = reg;
230 SET_DEST (pat) = mem;
231 if (recog (pat, insn, &num_clobbers) >= 0)
232 direct_store[(int) mode] = 1;
234 SET_SRC (pat) = reg;
235 SET_DEST (pat) = mem1;
236 if (recog (pat, insn, &num_clobbers) >= 0)
237 direct_store[(int) mode] = 1;
241 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
243 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
244 mode = GET_MODE_WIDER_MODE (mode))
246 machine_mode srcmode;
247 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
248 srcmode = GET_MODE_WIDER_MODE (srcmode))
250 enum insn_code ic;
252 ic = can_extend_p (mode, srcmode, 0);
253 if (ic == CODE_FOR_nothing)
254 continue;
256 PUT_MODE (mem, srcmode);
258 if (insn_operand_matches (ic, 1, mem))
259 float_extend_from_mem[mode][srcmode] = true;
264 /* This is run at the start of compiling a function. */
266 void
267 init_expr (void)
269 memset (&crtl->expr, 0, sizeof (crtl->expr));
272 /* Copy data from FROM to TO, where the machine modes are not the same.
273 Both modes may be integer, or both may be floating, or both may be
274 fixed-point.
275 UNSIGNEDP should be nonzero if FROM is an unsigned type.
276 This causes zero-extension instead of sign-extension. */
278 void
279 convert_move (rtx to, rtx from, int unsignedp)
281 machine_mode to_mode = GET_MODE (to);
282 machine_mode from_mode = GET_MODE (from);
283 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
284 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
285 enum insn_code code;
286 rtx libcall;
288 /* rtx code for making an equivalent value. */
289 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
290 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
293 gcc_assert (to_real == from_real);
294 gcc_assert (to_mode != BLKmode);
295 gcc_assert (from_mode != BLKmode);
297 /* If the source and destination are already the same, then there's
298 nothing to do. */
299 if (to == from)
300 return;
302 /* If FROM is a SUBREG that indicates that we have already done at least
303 the required extension, strip it. We don't handle such SUBREGs as
304 TO here. */
306 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
307 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
308 >= GET_MODE_PRECISION (to_mode))
309 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
310 from = gen_lowpart (to_mode, from), from_mode = to_mode;
312 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
314 if (to_mode == from_mode
315 || (from_mode == VOIDmode && CONSTANT_P (from)))
317 emit_move_insn (to, from);
318 return;
321 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
323 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
325 if (VECTOR_MODE_P (to_mode))
326 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
327 else
328 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
330 emit_move_insn (to, from);
331 return;
334 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
336 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
337 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
338 return;
341 if (to_real)
343 rtx value;
344 rtx_insn *insns;
345 convert_optab tab;
347 gcc_assert ((GET_MODE_PRECISION (from_mode)
348 != GET_MODE_PRECISION (to_mode))
349 || (DECIMAL_FLOAT_MODE_P (from_mode)
350 != DECIMAL_FLOAT_MODE_P (to_mode)));
352 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
353 /* Conversion between decimal float and binary float, same size. */
354 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
355 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
356 tab = sext_optab;
357 else
358 tab = trunc_optab;
360 /* Try converting directly if the insn is supported. */
362 code = convert_optab_handler (tab, to_mode, from_mode);
363 if (code != CODE_FOR_nothing)
365 emit_unop_insn (code, to, from,
366 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
367 return;
370 /* Otherwise use a libcall. */
371 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
373 /* Is this conversion implemented yet? */
374 gcc_assert (libcall);
376 start_sequence ();
377 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
378 1, from, from_mode);
379 insns = get_insns ();
380 end_sequence ();
381 emit_libcall_block (insns, to, value,
382 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
383 from)
384 : gen_rtx_FLOAT_EXTEND (to_mode, from));
385 return;
388 /* Handle pointer conversion. */ /* SPEE 900220. */
389 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
391 convert_optab ctab;
393 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
394 ctab = trunc_optab;
395 else if (unsignedp)
396 ctab = zext_optab;
397 else
398 ctab = sext_optab;
400 if (convert_optab_handler (ctab, to_mode, from_mode)
401 != CODE_FOR_nothing)
403 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
404 to, from, UNKNOWN);
405 return;
409 /* Targets are expected to provide conversion insns between PxImode and
410 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
411 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
413 machine_mode full_mode
414 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
416 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
417 != CODE_FOR_nothing);
419 if (full_mode != from_mode)
420 from = convert_to_mode (full_mode, from, unsignedp);
421 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
422 to, from, UNKNOWN);
423 return;
425 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
427 rtx new_from;
428 machine_mode full_mode
429 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
430 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
431 enum insn_code icode;
433 icode = convert_optab_handler (ctab, full_mode, from_mode);
434 gcc_assert (icode != CODE_FOR_nothing);
436 if (to_mode == full_mode)
438 emit_unop_insn (icode, to, from, UNKNOWN);
439 return;
442 new_from = gen_reg_rtx (full_mode);
443 emit_unop_insn (icode, new_from, from, UNKNOWN);
445 /* else proceed to integer conversions below. */
446 from_mode = full_mode;
447 from = new_from;
450 /* Make sure both are fixed-point modes or both are not. */
451 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
452 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
453 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
455 /* If we widen from_mode to to_mode and they are in the same class,
456 we won't saturate the result.
457 Otherwise, always saturate the result to play safe. */
458 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
459 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
460 expand_fixed_convert (to, from, 0, 0);
461 else
462 expand_fixed_convert (to, from, 0, 1);
463 return;
466 /* Now both modes are integers. */
468 /* Handle expanding beyond a word. */
469 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
470 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
472 rtx_insn *insns;
473 rtx lowpart;
474 rtx fill_value;
475 rtx lowfrom;
476 int i;
477 machine_mode lowpart_mode;
478 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
480 /* Try converting directly if the insn is supported. */
481 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
482 != CODE_FOR_nothing)
484 /* If FROM is a SUBREG, put it into a register. Do this
485 so that we always generate the same set of insns for
486 better cse'ing; if an intermediate assignment occurred,
487 we won't be doing the operation directly on the SUBREG. */
488 if (optimize > 0 && GET_CODE (from) == SUBREG)
489 from = force_reg (from_mode, from);
490 emit_unop_insn (code, to, from, equiv_code);
491 return;
493 /* Next, try converting via full word. */
494 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
495 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
496 != CODE_FOR_nothing))
498 rtx word_to = gen_reg_rtx (word_mode);
499 if (REG_P (to))
501 if (reg_overlap_mentioned_p (to, from))
502 from = force_reg (from_mode, from);
503 emit_clobber (to);
505 convert_move (word_to, from, unsignedp);
506 emit_unop_insn (code, to, word_to, equiv_code);
507 return;
510 /* No special multiword conversion insn; do it by hand. */
511 start_sequence ();
513 /* Since we will turn this into a no conflict block, we must ensure the
514 the source does not overlap the target so force it into an isolated
515 register when maybe so. Likewise for any MEM input, since the
516 conversion sequence might require several references to it and we
517 must ensure we're getting the same value every time. */
519 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
520 from = force_reg (from_mode, from);
522 /* Get a copy of FROM widened to a word, if necessary. */
523 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
524 lowpart_mode = word_mode;
525 else
526 lowpart_mode = from_mode;
528 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
530 lowpart = gen_lowpart (lowpart_mode, to);
531 emit_move_insn (lowpart, lowfrom);
533 /* Compute the value to put in each remaining word. */
534 if (unsignedp)
535 fill_value = const0_rtx;
536 else
537 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
538 LT, lowfrom, const0_rtx,
539 lowpart_mode, 0, -1);
541 /* Fill the remaining words. */
542 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
544 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
545 rtx subword = operand_subword (to, index, 1, to_mode);
547 gcc_assert (subword);
549 if (fill_value != subword)
550 emit_move_insn (subword, fill_value);
553 insns = get_insns ();
554 end_sequence ();
556 emit_insn (insns);
557 return;
560 /* Truncating multi-word to a word or less. */
561 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
562 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
564 if (!((MEM_P (from)
565 && ! MEM_VOLATILE_P (from)
566 && direct_load[(int) to_mode]
567 && ! mode_dependent_address_p (XEXP (from, 0),
568 MEM_ADDR_SPACE (from)))
569 || REG_P (from)
570 || GET_CODE (from) == SUBREG))
571 from = force_reg (from_mode, from);
572 convert_move (to, gen_lowpart (word_mode, from), 0);
573 return;
576 /* Now follow all the conversions between integers
577 no more than a word long. */
579 /* For truncation, usually we can just refer to FROM in a narrower mode. */
580 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
581 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
583 if (!((MEM_P (from)
584 && ! MEM_VOLATILE_P (from)
585 && direct_load[(int) to_mode]
586 && ! mode_dependent_address_p (XEXP (from, 0),
587 MEM_ADDR_SPACE (from)))
588 || REG_P (from)
589 || GET_CODE (from) == SUBREG))
590 from = force_reg (from_mode, from);
591 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
592 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
593 from = copy_to_reg (from);
594 emit_move_insn (to, gen_lowpart (to_mode, from));
595 return;
598 /* Handle extension. */
599 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
601 /* Convert directly if that works. */
602 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
603 != CODE_FOR_nothing)
605 emit_unop_insn (code, to, from, equiv_code);
606 return;
608 else
610 machine_mode intermediate;
611 rtx tmp;
612 int shift_amount;
614 /* Search for a mode to convert via. */
615 for (intermediate = from_mode; intermediate != VOIDmode;
616 intermediate = GET_MODE_WIDER_MODE (intermediate))
617 if (((can_extend_p (to_mode, intermediate, unsignedp)
618 != CODE_FOR_nothing)
619 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
620 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
621 && (can_extend_p (intermediate, from_mode, unsignedp)
622 != CODE_FOR_nothing))
624 convert_move (to, convert_to_mode (intermediate, from,
625 unsignedp), unsignedp);
626 return;
629 /* No suitable intermediate mode.
630 Generate what we need with shifts. */
631 shift_amount = (GET_MODE_PRECISION (to_mode)
632 - GET_MODE_PRECISION (from_mode));
633 from = gen_lowpart (to_mode, force_reg (from_mode, from));
634 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
635 to, unsignedp);
636 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
637 to, unsignedp);
638 if (tmp != to)
639 emit_move_insn (to, tmp);
640 return;
644 /* Support special truncate insns for certain modes. */
645 if (convert_optab_handler (trunc_optab, to_mode,
646 from_mode) != CODE_FOR_nothing)
648 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
649 to, from, UNKNOWN);
650 return;
653 /* Handle truncation of volatile memrefs, and so on;
654 the things that couldn't be truncated directly,
655 and for which there was no special instruction.
657 ??? Code above formerly short-circuited this, for most integer
658 mode pairs, with a force_reg in from_mode followed by a recursive
659 call to this routine. Appears always to have been wrong. */
660 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
662 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
663 emit_move_insn (to, temp);
664 return;
667 /* Mode combination is not recognized. */
668 gcc_unreachable ();
671 /* Return an rtx for a value that would result
672 from converting X to mode MODE.
673 Both X and MODE may be floating, or both integer.
674 UNSIGNEDP is nonzero if X is an unsigned value.
675 This can be done by referring to a part of X in place
676 or by copying to a new temporary with conversion. */
679 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
681 return convert_modes (mode, VOIDmode, x, unsignedp);
684 /* Return an rtx for a value that would result
685 from converting X from mode OLDMODE to mode MODE.
686 Both modes may be floating, or both integer.
687 UNSIGNEDP is nonzero if X is an unsigned value.
689 This can be done by referring to a part of X in place
690 or by copying to a new temporary with conversion.
692 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
695 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
697 rtx temp;
699 /* If FROM is a SUBREG that indicates that we have already done at least
700 the required extension, strip it. */
702 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
703 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
704 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
705 x = gen_lowpart (mode, SUBREG_REG (x));
707 if (GET_MODE (x) != VOIDmode)
708 oldmode = GET_MODE (x);
710 if (mode == oldmode)
711 return x;
713 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
715 /* If the caller did not tell us the old mode, then there is not
716 much to do with respect to canonicalization. We have to
717 assume that all the bits are significant. */
718 if (GET_MODE_CLASS (oldmode) != MODE_INT)
719 oldmode = MAX_MODE_INT;
720 wide_int w = wide_int::from (std::make_pair (x, oldmode),
721 GET_MODE_PRECISION (mode),
722 unsignedp ? UNSIGNED : SIGNED);
723 return immed_wide_int_const (w, mode);
726 /* We can do this with a gen_lowpart if both desired and current modes
727 are integer, and this is either a constant integer, a register, or a
728 non-volatile MEM. */
729 if (GET_MODE_CLASS (mode) == MODE_INT
730 && GET_MODE_CLASS (oldmode) == MODE_INT
731 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
732 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
733 || (REG_P (x)
734 && (!HARD_REGISTER_P (x)
735 || HARD_REGNO_MODE_OK (REGNO (x), mode))
736 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
738 return gen_lowpart (mode, x);
740 /* Converting from integer constant into mode is always equivalent to an
741 subreg operation. */
742 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
744 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
745 return simplify_gen_subreg (mode, x, oldmode, 0);
748 temp = gen_reg_rtx (mode);
749 convert_move (temp, x, unsignedp);
750 return temp;
753 /* Return the largest alignment we can use for doing a move (or store)
754 of MAX_PIECES. ALIGN is the largest alignment we could use. */
756 static unsigned int
757 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
759 machine_mode tmode;
761 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
762 if (align >= GET_MODE_ALIGNMENT (tmode))
763 align = GET_MODE_ALIGNMENT (tmode);
764 else
766 machine_mode tmode, xmode;
768 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
769 tmode != VOIDmode;
770 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
771 if (GET_MODE_SIZE (tmode) > max_pieces
772 || SLOW_UNALIGNED_ACCESS (tmode, align))
773 break;
775 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
778 return align;
781 /* Return the widest integer mode no wider than SIZE. If no such mode
782 can be found, return VOIDmode. */
784 static machine_mode
785 widest_int_mode_for_size (unsigned int size)
787 machine_mode tmode, mode = VOIDmode;
789 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
790 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
791 if (GET_MODE_SIZE (tmode) < size)
792 mode = tmode;
794 return mode;
797 /* Determine whether the LEN bytes can be moved by using several move
798 instructions. Return nonzero if a call to move_by_pieces should
799 succeed. */
802 can_move_by_pieces (unsigned HOST_WIDE_INT len,
803 unsigned int align)
805 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
806 optimize_insn_for_speed_p ());
809 /* Generate several move instructions to copy LEN bytes from block FROM to
810 block TO. (These are MEM rtx's with BLKmode).
812 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
813 used to push FROM to the stack.
815 ALIGN is maximum stack alignment we can assume.
817 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
818 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
819 stpcpy. */
822 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
823 unsigned int align, int endp)
825 struct move_by_pieces_d data;
826 machine_mode to_addr_mode;
827 machine_mode from_addr_mode = get_address_mode (from);
828 rtx to_addr, from_addr = XEXP (from, 0);
829 unsigned int max_size = MOVE_MAX_PIECES + 1;
830 enum insn_code icode;
832 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
834 data.offset = 0;
835 data.from_addr = from_addr;
836 if (to)
838 to_addr_mode = get_address_mode (to);
839 to_addr = XEXP (to, 0);
840 data.to = to;
841 data.autinc_to
842 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
843 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
844 data.reverse
845 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
847 else
849 to_addr_mode = VOIDmode;
850 to_addr = NULL_RTX;
851 data.to = NULL_RTX;
852 data.autinc_to = 1;
853 if (STACK_GROWS_DOWNWARD)
854 data.reverse = 1;
855 else
856 data.reverse = 0;
858 data.to_addr = to_addr;
859 data.from = from;
860 data.autinc_from
861 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
862 || GET_CODE (from_addr) == POST_INC
863 || GET_CODE (from_addr) == POST_DEC);
865 data.explicit_inc_from = 0;
866 data.explicit_inc_to = 0;
867 if (data.reverse) data.offset = len;
868 data.len = len;
870 /* If copying requires more than two move insns,
871 copy addresses to registers (to make displacements shorter)
872 and use post-increment if available. */
873 if (!(data.autinc_from && data.autinc_to)
874 && move_by_pieces_ninsns (len, align, max_size) > 2)
876 /* Find the mode of the largest move...
877 MODE might not be used depending on the definitions of the
878 USE_* macros below. */
879 machine_mode mode ATTRIBUTE_UNUSED
880 = widest_int_mode_for_size (max_size);
882 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
884 data.from_addr = copy_to_mode_reg (from_addr_mode,
885 plus_constant (from_addr_mode,
886 from_addr, len));
887 data.autinc_from = 1;
888 data.explicit_inc_from = -1;
890 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
892 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
893 data.autinc_from = 1;
894 data.explicit_inc_from = 1;
896 if (!data.autinc_from && CONSTANT_P (from_addr))
897 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
898 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
900 data.to_addr = copy_to_mode_reg (to_addr_mode,
901 plus_constant (to_addr_mode,
902 to_addr, len));
903 data.autinc_to = 1;
904 data.explicit_inc_to = -1;
906 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
908 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
909 data.autinc_to = 1;
910 data.explicit_inc_to = 1;
912 if (!data.autinc_to && CONSTANT_P (to_addr))
913 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
916 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
918 /* First move what we can in the largest integer mode, then go to
919 successively smaller modes. */
921 while (max_size > 1 && data.len > 0)
923 machine_mode mode = widest_int_mode_for_size (max_size);
925 if (mode == VOIDmode)
926 break;
928 icode = optab_handler (mov_optab, mode);
929 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
930 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
932 max_size = GET_MODE_SIZE (mode);
935 /* The code above should have handled everything. */
936 gcc_assert (!data.len);
938 if (endp)
940 rtx to1;
942 gcc_assert (!data.reverse);
943 if (data.autinc_to)
945 if (endp == 2)
947 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
948 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
949 else
950 data.to_addr = copy_to_mode_reg (to_addr_mode,
951 plus_constant (to_addr_mode,
952 data.to_addr,
953 -1));
955 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
956 data.offset);
958 else
960 if (endp == 2)
961 --data.offset;
962 to1 = adjust_address (data.to, QImode, data.offset);
964 return to1;
966 else
967 return data.to;
970 /* Return number of insns required to move L bytes by pieces.
971 ALIGN (in bits) is maximum alignment we can assume. */
973 unsigned HOST_WIDE_INT
974 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
975 unsigned int max_size)
977 unsigned HOST_WIDE_INT n_insns = 0;
979 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
981 while (max_size > 1 && l > 0)
983 machine_mode mode;
984 enum insn_code icode;
986 mode = widest_int_mode_for_size (max_size);
988 if (mode == VOIDmode)
989 break;
991 icode = optab_handler (mov_optab, mode);
992 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
993 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
995 max_size = GET_MODE_SIZE (mode);
998 gcc_assert (!l);
999 return n_insns;
1002 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1003 with move instructions for mode MODE. GENFUN is the gen_... function
1004 to make a move insn for that mode. DATA has all the other info. */
1006 static void
1007 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1008 struct move_by_pieces_d *data)
1010 unsigned int size = GET_MODE_SIZE (mode);
1011 rtx to1 = NULL_RTX, from1;
1013 while (data->len >= size)
1015 if (data->reverse)
1016 data->offset -= size;
1018 if (data->to)
1020 if (data->autinc_to)
1021 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1022 data->offset);
1023 else
1024 to1 = adjust_address (data->to, mode, data->offset);
1027 if (data->autinc_from)
1028 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1029 data->offset);
1030 else
1031 from1 = adjust_address (data->from, mode, data->offset);
1033 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1034 emit_insn (gen_add2_insn (data->to_addr,
1035 gen_int_mode (-(HOST_WIDE_INT) size,
1036 GET_MODE (data->to_addr))));
1037 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1038 emit_insn (gen_add2_insn (data->from_addr,
1039 gen_int_mode (-(HOST_WIDE_INT) size,
1040 GET_MODE (data->from_addr))));
1042 if (data->to)
1043 emit_insn ((*genfun) (to1, from1));
1044 else
1046 #ifdef PUSH_ROUNDING
1047 emit_single_push_insn (mode, from1, NULL);
1048 #else
1049 gcc_unreachable ();
1050 #endif
1053 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1054 emit_insn (gen_add2_insn (data->to_addr,
1055 gen_int_mode (size,
1056 GET_MODE (data->to_addr))));
1057 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1058 emit_insn (gen_add2_insn (data->from_addr,
1059 gen_int_mode (size,
1060 GET_MODE (data->from_addr))));
1062 if (! data->reverse)
1063 data->offset += size;
1065 data->len -= size;
1069 /* Emit code to move a block Y to a block X. This may be done with
1070 string-move instructions, with multiple scalar move instructions,
1071 or with a library call.
1073 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1074 SIZE is an rtx that says how long they are.
1075 ALIGN is the maximum alignment we can assume they have.
1076 METHOD describes what kind of copy this is, and what mechanisms may be used.
1077 MIN_SIZE is the minimal size of block to move
1078 MAX_SIZE is the maximal size of block to move, if it can not be represented
1079 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1081 Return the address of the new block, if memcpy is called and returns it,
1082 0 otherwise. */
1085 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1086 unsigned int expected_align, HOST_WIDE_INT expected_size,
1087 unsigned HOST_WIDE_INT min_size,
1088 unsigned HOST_WIDE_INT max_size,
1089 unsigned HOST_WIDE_INT probable_max_size)
1091 bool may_use_call;
1092 rtx retval = 0;
1093 unsigned int align;
1095 gcc_assert (size);
1096 if (CONST_INT_P (size)
1097 && INTVAL (size) == 0)
1098 return 0;
1100 switch (method)
1102 case BLOCK_OP_NORMAL:
1103 case BLOCK_OP_TAILCALL:
1104 may_use_call = true;
1105 break;
1107 case BLOCK_OP_CALL_PARM:
1108 may_use_call = block_move_libcall_safe_for_call_parm ();
1110 /* Make inhibit_defer_pop nonzero around the library call
1111 to force it to pop the arguments right away. */
1112 NO_DEFER_POP;
1113 break;
1115 case BLOCK_OP_NO_LIBCALL:
1116 may_use_call = false;
1117 break;
1119 default:
1120 gcc_unreachable ();
1123 gcc_assert (MEM_P (x) && MEM_P (y));
1124 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1125 gcc_assert (align >= BITS_PER_UNIT);
1127 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1128 block copy is more efficient for other large modes, e.g. DCmode. */
1129 x = adjust_address (x, BLKmode, 0);
1130 y = adjust_address (y, BLKmode, 0);
1132 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1133 can be incorrect is coming from __builtin_memcpy. */
1134 if (CONST_INT_P (size))
1136 x = shallow_copy_rtx (x);
1137 y = shallow_copy_rtx (y);
1138 set_mem_size (x, INTVAL (size));
1139 set_mem_size (y, INTVAL (size));
1142 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1143 move_by_pieces (x, y, INTVAL (size), align, 0);
1144 else if (emit_block_move_via_movmem (x, y, size, align,
1145 expected_align, expected_size,
1146 min_size, max_size, probable_max_size))
1148 else if (may_use_call
1149 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1150 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1152 /* Since x and y are passed to a libcall, mark the corresponding
1153 tree EXPR as addressable. */
1154 tree y_expr = MEM_EXPR (y);
1155 tree x_expr = MEM_EXPR (x);
1156 if (y_expr)
1157 mark_addressable (y_expr);
1158 if (x_expr)
1159 mark_addressable (x_expr);
1160 retval = emit_block_move_via_libcall (x, y, size,
1161 method == BLOCK_OP_TAILCALL);
1164 else
1165 emit_block_move_via_loop (x, y, size, align);
1167 if (method == BLOCK_OP_CALL_PARM)
1168 OK_DEFER_POP;
1170 return retval;
1174 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1176 unsigned HOST_WIDE_INT max, min = 0;
1177 if (GET_CODE (size) == CONST_INT)
1178 min = max = UINTVAL (size);
1179 else
1180 max = GET_MODE_MASK (GET_MODE (size));
1181 return emit_block_move_hints (x, y, size, method, 0, -1,
1182 min, max, max);
1185 /* A subroutine of emit_block_move. Returns true if calling the
1186 block move libcall will not clobber any parameters which may have
1187 already been placed on the stack. */
1189 static bool
1190 block_move_libcall_safe_for_call_parm (void)
1192 #if defined (REG_PARM_STACK_SPACE)
1193 tree fn;
1194 #endif
1196 /* If arguments are pushed on the stack, then they're safe. */
1197 if (PUSH_ARGS)
1198 return true;
1200 /* If registers go on the stack anyway, any argument is sure to clobber
1201 an outgoing argument. */
1202 #if defined (REG_PARM_STACK_SPACE)
1203 fn = emit_block_move_libcall_fn (false);
1204 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1205 depend on its argument. */
1206 (void) fn;
1207 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1208 && REG_PARM_STACK_SPACE (fn) != 0)
1209 return false;
1210 #endif
1212 /* If any argument goes in memory, then it might clobber an outgoing
1213 argument. */
1215 CUMULATIVE_ARGS args_so_far_v;
1216 cumulative_args_t args_so_far;
1217 tree fn, arg;
1219 fn = emit_block_move_libcall_fn (false);
1220 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1221 args_so_far = pack_cumulative_args (&args_so_far_v);
1223 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1224 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1226 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1227 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1228 NULL_TREE, true);
1229 if (!tmp || !REG_P (tmp))
1230 return false;
1231 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1232 return false;
1233 targetm.calls.function_arg_advance (args_so_far, mode,
1234 NULL_TREE, true);
1237 return true;
1240 /* A subroutine of emit_block_move. Expand a movmem pattern;
1241 return true if successful. */
1243 static bool
1244 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1245 unsigned int expected_align, HOST_WIDE_INT expected_size,
1246 unsigned HOST_WIDE_INT min_size,
1247 unsigned HOST_WIDE_INT max_size,
1248 unsigned HOST_WIDE_INT probable_max_size)
1250 int save_volatile_ok = volatile_ok;
1251 machine_mode mode;
1253 if (expected_align < align)
1254 expected_align = align;
1255 if (expected_size != -1)
1257 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1258 expected_size = probable_max_size;
1259 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1260 expected_size = min_size;
1263 /* Since this is a move insn, we don't care about volatility. */
1264 volatile_ok = 1;
1266 /* Try the most limited insn first, because there's no point
1267 including more than one in the machine description unless
1268 the more limited one has some advantage. */
1270 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1271 mode = GET_MODE_WIDER_MODE (mode))
1273 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1275 if (code != CODE_FOR_nothing
1276 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1277 here because if SIZE is less than the mode mask, as it is
1278 returned by the macro, it will definitely be less than the
1279 actual mode mask. Since SIZE is within the Pmode address
1280 space, we limit MODE to Pmode. */
1281 && ((CONST_INT_P (size)
1282 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1283 <= (GET_MODE_MASK (mode) >> 1)))
1284 || max_size <= (GET_MODE_MASK (mode) >> 1)
1285 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1287 struct expand_operand ops[9];
1288 unsigned int nops;
1290 /* ??? When called via emit_block_move_for_call, it'd be
1291 nice if there were some way to inform the backend, so
1292 that it doesn't fail the expansion because it thinks
1293 emitting the libcall would be more efficient. */
1294 nops = insn_data[(int) code].n_generator_args;
1295 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1297 create_fixed_operand (&ops[0], x);
1298 create_fixed_operand (&ops[1], y);
1299 /* The check above guarantees that this size conversion is valid. */
1300 create_convert_operand_to (&ops[2], size, mode, true);
1301 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1302 if (nops >= 6)
1304 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1305 create_integer_operand (&ops[5], expected_size);
1307 if (nops >= 8)
1309 create_integer_operand (&ops[6], min_size);
1310 /* If we can not represent the maximal size,
1311 make parameter NULL. */
1312 if ((HOST_WIDE_INT) max_size != -1)
1313 create_integer_operand (&ops[7], max_size);
1314 else
1315 create_fixed_operand (&ops[7], NULL);
1317 if (nops == 9)
1319 /* If we can not represent the maximal size,
1320 make parameter NULL. */
1321 if ((HOST_WIDE_INT) probable_max_size != -1)
1322 create_integer_operand (&ops[8], probable_max_size);
1323 else
1324 create_fixed_operand (&ops[8], NULL);
1326 if (maybe_expand_insn (code, nops, ops))
1328 volatile_ok = save_volatile_ok;
1329 return true;
1334 volatile_ok = save_volatile_ok;
1335 return false;
1338 /* A subroutine of emit_block_move. Expand a call to memcpy.
1339 Return the return value from memcpy, 0 otherwise. */
1342 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1344 rtx dst_addr, src_addr;
1345 tree call_expr, fn, src_tree, dst_tree, size_tree;
1346 machine_mode size_mode;
1347 rtx retval;
1349 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1350 pseudos. We can then place those new pseudos into a VAR_DECL and
1351 use them later. */
1353 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1354 src_addr = copy_addr_to_reg (XEXP (src, 0));
1356 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1357 src_addr = convert_memory_address (ptr_mode, src_addr);
1359 dst_tree = make_tree (ptr_type_node, dst_addr);
1360 src_tree = make_tree (ptr_type_node, src_addr);
1362 size_mode = TYPE_MODE (sizetype);
1364 size = convert_to_mode (size_mode, size, 1);
1365 size = copy_to_mode_reg (size_mode, size);
1367 /* It is incorrect to use the libcall calling conventions to call
1368 memcpy in this context. This could be a user call to memcpy and
1369 the user may wish to examine the return value from memcpy. For
1370 targets where libcalls and normal calls have different conventions
1371 for returning pointers, we could end up generating incorrect code. */
1373 size_tree = make_tree (sizetype, size);
1375 fn = emit_block_move_libcall_fn (true);
1376 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1377 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1379 retval = expand_normal (call_expr);
1381 return retval;
1384 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1385 for the function we use for block copies. */
1387 static GTY(()) tree block_move_fn;
1389 void
1390 init_block_move_fn (const char *asmspec)
1392 if (!block_move_fn)
1394 tree args, fn, attrs, attr_args;
1396 fn = get_identifier ("memcpy");
1397 args = build_function_type_list (ptr_type_node, ptr_type_node,
1398 const_ptr_type_node, sizetype,
1399 NULL_TREE);
1401 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1402 DECL_EXTERNAL (fn) = 1;
1403 TREE_PUBLIC (fn) = 1;
1404 DECL_ARTIFICIAL (fn) = 1;
1405 TREE_NOTHROW (fn) = 1;
1406 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1407 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1409 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1410 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1412 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1414 block_move_fn = fn;
1417 if (asmspec)
1418 set_user_assembler_name (block_move_fn, asmspec);
1421 static tree
1422 emit_block_move_libcall_fn (int for_call)
1424 static bool emitted_extern;
1426 if (!block_move_fn)
1427 init_block_move_fn (NULL);
1429 if (for_call && !emitted_extern)
1431 emitted_extern = true;
1432 make_decl_rtl (block_move_fn);
1435 return block_move_fn;
1438 /* A subroutine of emit_block_move. Copy the data via an explicit
1439 loop. This is used only when libcalls are forbidden. */
1440 /* ??? It'd be nice to copy in hunks larger than QImode. */
1442 static void
1443 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1444 unsigned int align ATTRIBUTE_UNUSED)
1446 rtx_code_label *cmp_label, *top_label;
1447 rtx iter, x_addr, y_addr, tmp;
1448 machine_mode x_addr_mode = get_address_mode (x);
1449 machine_mode y_addr_mode = get_address_mode (y);
1450 machine_mode iter_mode;
1452 iter_mode = GET_MODE (size);
1453 if (iter_mode == VOIDmode)
1454 iter_mode = word_mode;
1456 top_label = gen_label_rtx ();
1457 cmp_label = gen_label_rtx ();
1458 iter = gen_reg_rtx (iter_mode);
1460 emit_move_insn (iter, const0_rtx);
1462 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1463 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1464 do_pending_stack_adjust ();
1466 emit_jump (cmp_label);
1467 emit_label (top_label);
1469 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1470 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1472 if (x_addr_mode != y_addr_mode)
1473 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1474 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1476 x = change_address (x, QImode, x_addr);
1477 y = change_address (y, QImode, y_addr);
1479 emit_move_insn (x, y);
1481 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1482 true, OPTAB_LIB_WIDEN);
1483 if (tmp != iter)
1484 emit_move_insn (iter, tmp);
1486 emit_label (cmp_label);
1488 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1489 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1492 /* Copy all or part of a value X into registers starting at REGNO.
1493 The number of registers to be filled is NREGS. */
1495 void
1496 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1498 int i;
1499 rtx pat;
1500 rtx_insn *last;
1502 if (nregs == 0)
1503 return;
1505 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1506 x = validize_mem (force_const_mem (mode, x));
1508 /* See if the machine can do this with a load multiple insn. */
1509 if (HAVE_load_multiple)
1511 last = get_last_insn ();
1512 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1513 GEN_INT (nregs));
1514 if (pat)
1516 emit_insn (pat);
1517 return;
1519 else
1520 delete_insns_since (last);
1523 for (i = 0; i < nregs; i++)
1524 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1525 operand_subword_force (x, i, mode));
1528 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1529 The number of registers to be filled is NREGS. */
1531 void
1532 move_block_from_reg (int regno, rtx x, int nregs)
1534 int i;
1536 if (nregs == 0)
1537 return;
1539 /* See if the machine can do this with a store multiple insn. */
1540 if (HAVE_store_multiple)
1542 rtx_insn *last = get_last_insn ();
1543 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1544 GEN_INT (nregs));
1545 if (pat)
1547 emit_insn (pat);
1548 return;
1550 else
1551 delete_insns_since (last);
1554 for (i = 0; i < nregs; i++)
1556 rtx tem = operand_subword (x, i, 1, BLKmode);
1558 gcc_assert (tem);
1560 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1564 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1565 ORIG, where ORIG is a non-consecutive group of registers represented by
1566 a PARALLEL. The clone is identical to the original except in that the
1567 original set of registers is replaced by a new set of pseudo registers.
1568 The new set has the same modes as the original set. */
1571 gen_group_rtx (rtx orig)
1573 int i, length;
1574 rtx *tmps;
1576 gcc_assert (GET_CODE (orig) == PARALLEL);
1578 length = XVECLEN (orig, 0);
1579 tmps = XALLOCAVEC (rtx, length);
1581 /* Skip a NULL entry in first slot. */
1582 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1584 if (i)
1585 tmps[0] = 0;
1587 for (; i < length; i++)
1589 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1590 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1592 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1595 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1598 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1599 except that values are placed in TMPS[i], and must later be moved
1600 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1602 static void
1603 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1605 rtx src;
1606 int start, i;
1607 machine_mode m = GET_MODE (orig_src);
1609 gcc_assert (GET_CODE (dst) == PARALLEL);
1611 if (m != VOIDmode
1612 && !SCALAR_INT_MODE_P (m)
1613 && !MEM_P (orig_src)
1614 && GET_CODE (orig_src) != CONCAT)
1616 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1617 if (imode == BLKmode)
1618 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1619 else
1620 src = gen_reg_rtx (imode);
1621 if (imode != BLKmode)
1622 src = gen_lowpart (GET_MODE (orig_src), src);
1623 emit_move_insn (src, orig_src);
1624 /* ...and back again. */
1625 if (imode != BLKmode)
1626 src = gen_lowpart (imode, src);
1627 emit_group_load_1 (tmps, dst, src, type, ssize);
1628 return;
1631 /* Check for a NULL entry, used to indicate that the parameter goes
1632 both on the stack and in registers. */
1633 if (XEXP (XVECEXP (dst, 0, 0), 0))
1634 start = 0;
1635 else
1636 start = 1;
1638 /* Process the pieces. */
1639 for (i = start; i < XVECLEN (dst, 0); i++)
1641 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1642 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1643 unsigned int bytelen = GET_MODE_SIZE (mode);
1644 int shift = 0;
1646 /* Handle trailing fragments that run over the size of the struct. */
1647 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1649 /* Arrange to shift the fragment to where it belongs.
1650 extract_bit_field loads to the lsb of the reg. */
1651 if (
1652 #ifdef BLOCK_REG_PADDING
1653 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1654 == (BYTES_BIG_ENDIAN ? upward : downward)
1655 #else
1656 BYTES_BIG_ENDIAN
1657 #endif
1659 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1660 bytelen = ssize - bytepos;
1661 gcc_assert (bytelen > 0);
1664 /* If we won't be loading directly from memory, protect the real source
1665 from strange tricks we might play; but make sure that the source can
1666 be loaded directly into the destination. */
1667 src = orig_src;
1668 if (!MEM_P (orig_src)
1669 && (!CONSTANT_P (orig_src)
1670 || (GET_MODE (orig_src) != mode
1671 && GET_MODE (orig_src) != VOIDmode)))
1673 if (GET_MODE (orig_src) == VOIDmode)
1674 src = gen_reg_rtx (mode);
1675 else
1676 src = gen_reg_rtx (GET_MODE (orig_src));
1678 emit_move_insn (src, orig_src);
1681 /* Optimize the access just a bit. */
1682 if (MEM_P (src)
1683 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1684 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1685 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1686 && bytelen == GET_MODE_SIZE (mode))
1688 tmps[i] = gen_reg_rtx (mode);
1689 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1691 else if (COMPLEX_MODE_P (mode)
1692 && GET_MODE (src) == mode
1693 && bytelen == GET_MODE_SIZE (mode))
1694 /* Let emit_move_complex do the bulk of the work. */
1695 tmps[i] = src;
1696 else if (GET_CODE (src) == CONCAT)
1698 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1699 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1701 if ((bytepos == 0 && bytelen == slen0)
1702 || (bytepos != 0 && bytepos + bytelen <= slen))
1704 /* The following assumes that the concatenated objects all
1705 have the same size. In this case, a simple calculation
1706 can be used to determine the object and the bit field
1707 to be extracted. */
1708 tmps[i] = XEXP (src, bytepos / slen0);
1709 if (! CONSTANT_P (tmps[i])
1710 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1711 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1712 (bytepos % slen0) * BITS_PER_UNIT,
1713 1, NULL_RTX, mode, mode);
1715 else
1717 rtx mem;
1719 gcc_assert (!bytepos);
1720 mem = assign_stack_temp (GET_MODE (src), slen);
1721 emit_move_insn (mem, src);
1722 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1723 0, 1, NULL_RTX, mode, mode);
1726 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1727 SIMD register, which is currently broken. While we get GCC
1728 to emit proper RTL for these cases, let's dump to memory. */
1729 else if (VECTOR_MODE_P (GET_MODE (dst))
1730 && REG_P (src))
1732 int slen = GET_MODE_SIZE (GET_MODE (src));
1733 rtx mem;
1735 mem = assign_stack_temp (GET_MODE (src), slen);
1736 emit_move_insn (mem, src);
1737 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1739 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1740 && XVECLEN (dst, 0) > 1)
1741 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1742 else if (CONSTANT_P (src))
1744 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1746 if (len == ssize)
1747 tmps[i] = src;
1748 else
1750 rtx first, second;
1752 /* TODO: const_wide_int can have sizes other than this... */
1753 gcc_assert (2 * len == ssize);
1754 split_double (src, &first, &second);
1755 if (i)
1756 tmps[i] = second;
1757 else
1758 tmps[i] = first;
1761 else if (REG_P (src) && GET_MODE (src) == mode)
1762 tmps[i] = src;
1763 else
1764 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1765 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1766 mode, mode);
1768 if (shift)
1769 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1770 shift, tmps[i], 0);
1774 /* Emit code to move a block SRC of type TYPE to a block DST,
1775 where DST is non-consecutive registers represented by a PARALLEL.
1776 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1777 if not known. */
1779 void
1780 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1782 rtx *tmps;
1783 int i;
1785 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1786 emit_group_load_1 (tmps, dst, src, type, ssize);
1788 /* Copy the extracted pieces into the proper (probable) hard regs. */
1789 for (i = 0; i < XVECLEN (dst, 0); i++)
1791 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1792 if (d == NULL)
1793 continue;
1794 emit_move_insn (d, tmps[i]);
1798 /* Similar, but load SRC into new pseudos in a format that looks like
1799 PARALLEL. This can later be fed to emit_group_move to get things
1800 in the right place. */
1803 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1805 rtvec vec;
1806 int i;
1808 vec = rtvec_alloc (XVECLEN (parallel, 0));
1809 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1811 /* Convert the vector to look just like the original PARALLEL, except
1812 with the computed values. */
1813 for (i = 0; i < XVECLEN (parallel, 0); i++)
1815 rtx e = XVECEXP (parallel, 0, i);
1816 rtx d = XEXP (e, 0);
1818 if (d)
1820 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1821 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1823 RTVEC_ELT (vec, i) = e;
1826 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1829 /* Emit code to move a block SRC to block DST, where SRC and DST are
1830 non-consecutive groups of registers, each represented by a PARALLEL. */
1832 void
1833 emit_group_move (rtx dst, rtx src)
1835 int i;
1837 gcc_assert (GET_CODE (src) == PARALLEL
1838 && GET_CODE (dst) == PARALLEL
1839 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1841 /* Skip first entry if NULL. */
1842 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1843 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1844 XEXP (XVECEXP (src, 0, i), 0));
1847 /* Move a group of registers represented by a PARALLEL into pseudos. */
1850 emit_group_move_into_temps (rtx src)
1852 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1853 int i;
1855 for (i = 0; i < XVECLEN (src, 0); i++)
1857 rtx e = XVECEXP (src, 0, i);
1858 rtx d = XEXP (e, 0);
1860 if (d)
1861 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1862 RTVEC_ELT (vec, i) = e;
1865 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1868 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1869 where SRC is non-consecutive registers represented by a PARALLEL.
1870 SSIZE represents the total size of block ORIG_DST, or -1 if not
1871 known. */
1873 void
1874 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1876 rtx *tmps, dst;
1877 int start, finish, i;
1878 machine_mode m = GET_MODE (orig_dst);
1880 gcc_assert (GET_CODE (src) == PARALLEL);
1882 if (!SCALAR_INT_MODE_P (m)
1883 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1885 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1886 if (imode == BLKmode)
1887 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1888 else
1889 dst = gen_reg_rtx (imode);
1890 emit_group_store (dst, src, type, ssize);
1891 if (imode != BLKmode)
1892 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1893 emit_move_insn (orig_dst, dst);
1894 return;
1897 /* Check for a NULL entry, used to indicate that the parameter goes
1898 both on the stack and in registers. */
1899 if (XEXP (XVECEXP (src, 0, 0), 0))
1900 start = 0;
1901 else
1902 start = 1;
1903 finish = XVECLEN (src, 0);
1905 tmps = XALLOCAVEC (rtx, finish);
1907 /* Copy the (probable) hard regs into pseudos. */
1908 for (i = start; i < finish; i++)
1910 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1911 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1913 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1914 emit_move_insn (tmps[i], reg);
1916 else
1917 tmps[i] = reg;
1920 /* If we won't be storing directly into memory, protect the real destination
1921 from strange tricks we might play. */
1922 dst = orig_dst;
1923 if (GET_CODE (dst) == PARALLEL)
1925 rtx temp;
1927 /* We can get a PARALLEL dst if there is a conditional expression in
1928 a return statement. In that case, the dst and src are the same,
1929 so no action is necessary. */
1930 if (rtx_equal_p (dst, src))
1931 return;
1933 /* It is unclear if we can ever reach here, but we may as well handle
1934 it. Allocate a temporary, and split this into a store/load to/from
1935 the temporary. */
1936 temp = assign_stack_temp (GET_MODE (dst), ssize);
1937 emit_group_store (temp, src, type, ssize);
1938 emit_group_load (dst, temp, type, ssize);
1939 return;
1941 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1943 machine_mode outer = GET_MODE (dst);
1944 machine_mode inner;
1945 HOST_WIDE_INT bytepos;
1946 bool done = false;
1947 rtx temp;
1949 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1950 dst = gen_reg_rtx (outer);
1952 /* Make life a bit easier for combine. */
1953 /* If the first element of the vector is the low part
1954 of the destination mode, use a paradoxical subreg to
1955 initialize the destination. */
1956 if (start < finish)
1958 inner = GET_MODE (tmps[start]);
1959 bytepos = subreg_lowpart_offset (inner, outer);
1960 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1962 temp = simplify_gen_subreg (outer, tmps[start],
1963 inner, 0);
1964 if (temp)
1966 emit_move_insn (dst, temp);
1967 done = true;
1968 start++;
1973 /* If the first element wasn't the low part, try the last. */
1974 if (!done
1975 && start < finish - 1)
1977 inner = GET_MODE (tmps[finish - 1]);
1978 bytepos = subreg_lowpart_offset (inner, outer);
1979 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1981 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1982 inner, 0);
1983 if (temp)
1985 emit_move_insn (dst, temp);
1986 done = true;
1987 finish--;
1992 /* Otherwise, simply initialize the result to zero. */
1993 if (!done)
1994 emit_move_insn (dst, CONST0_RTX (outer));
1997 /* Process the pieces. */
1998 for (i = start; i < finish; i++)
2000 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2001 machine_mode mode = GET_MODE (tmps[i]);
2002 unsigned int bytelen = GET_MODE_SIZE (mode);
2003 unsigned int adj_bytelen;
2004 rtx dest = dst;
2006 /* Handle trailing fragments that run over the size of the struct. */
2007 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2008 adj_bytelen = ssize - bytepos;
2009 else
2010 adj_bytelen = bytelen;
2012 if (GET_CODE (dst) == CONCAT)
2014 if (bytepos + adj_bytelen
2015 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2016 dest = XEXP (dst, 0);
2017 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2019 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2020 dest = XEXP (dst, 1);
2022 else
2024 machine_mode dest_mode = GET_MODE (dest);
2025 machine_mode tmp_mode = GET_MODE (tmps[i]);
2027 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2029 if (GET_MODE_ALIGNMENT (dest_mode)
2030 >= GET_MODE_ALIGNMENT (tmp_mode))
2032 dest = assign_stack_temp (dest_mode,
2033 GET_MODE_SIZE (dest_mode));
2034 emit_move_insn (adjust_address (dest,
2035 tmp_mode,
2036 bytepos),
2037 tmps[i]);
2038 dst = dest;
2040 else
2042 dest = assign_stack_temp (tmp_mode,
2043 GET_MODE_SIZE (tmp_mode));
2044 emit_move_insn (dest, tmps[i]);
2045 dst = adjust_address (dest, dest_mode, bytepos);
2047 break;
2051 /* Handle trailing fragments that run over the size of the struct. */
2052 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2054 /* store_bit_field always takes its value from the lsb.
2055 Move the fragment to the lsb if it's not already there. */
2056 if (
2057 #ifdef BLOCK_REG_PADDING
2058 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2059 == (BYTES_BIG_ENDIAN ? upward : downward)
2060 #else
2061 BYTES_BIG_ENDIAN
2062 #endif
2065 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2066 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2067 shift, tmps[i], 0);
2070 /* Make sure not to write past the end of the struct. */
2071 store_bit_field (dest,
2072 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2073 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2074 VOIDmode, tmps[i]);
2077 /* Optimize the access just a bit. */
2078 else if (MEM_P (dest)
2079 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2080 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2081 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2082 && bytelen == GET_MODE_SIZE (mode))
2083 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2085 else
2086 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2087 0, 0, mode, tmps[i]);
2090 /* Copy from the pseudo into the (probable) hard reg. */
2091 if (orig_dst != dst)
2092 emit_move_insn (orig_dst, dst);
2095 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2096 of the value stored in X. */
2099 maybe_emit_group_store (rtx x, tree type)
2101 machine_mode mode = TYPE_MODE (type);
2102 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2103 if (GET_CODE (x) == PARALLEL)
2105 rtx result = gen_reg_rtx (mode);
2106 emit_group_store (result, x, type, int_size_in_bytes (type));
2107 return result;
2109 return x;
2112 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2114 This is used on targets that return BLKmode values in registers. */
2116 void
2117 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2119 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2120 rtx src = NULL, dst = NULL;
2121 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2122 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2123 machine_mode mode = GET_MODE (srcreg);
2124 machine_mode tmode = GET_MODE (target);
2125 machine_mode copy_mode;
2127 /* BLKmode registers created in the back-end shouldn't have survived. */
2128 gcc_assert (mode != BLKmode);
2130 /* If the structure doesn't take up a whole number of words, see whether
2131 SRCREG is padded on the left or on the right. If it's on the left,
2132 set PADDING_CORRECTION to the number of bits to skip.
2134 In most ABIs, the structure will be returned at the least end of
2135 the register, which translates to right padding on little-endian
2136 targets and left padding on big-endian targets. The opposite
2137 holds if the structure is returned at the most significant
2138 end of the register. */
2139 if (bytes % UNITS_PER_WORD != 0
2140 && (targetm.calls.return_in_msb (type)
2141 ? !BYTES_BIG_ENDIAN
2142 : BYTES_BIG_ENDIAN))
2143 padding_correction
2144 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2146 /* We can use a single move if we have an exact mode for the size. */
2147 else if (MEM_P (target)
2148 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2149 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2150 && bytes == GET_MODE_SIZE (mode))
2152 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2153 return;
2156 /* And if we additionally have the same mode for a register. */
2157 else if (REG_P (target)
2158 && GET_MODE (target) == mode
2159 && bytes == GET_MODE_SIZE (mode))
2161 emit_move_insn (target, srcreg);
2162 return;
2165 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2166 into a new pseudo which is a full word. */
2167 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2169 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2170 mode = word_mode;
2173 /* Copy the structure BITSIZE bits at a time. If the target lives in
2174 memory, take care of not reading/writing past its end by selecting
2175 a copy mode suited to BITSIZE. This should always be possible given
2176 how it is computed.
2178 If the target lives in register, make sure not to select a copy mode
2179 larger than the mode of the register.
2181 We could probably emit more efficient code for machines which do not use
2182 strict alignment, but it doesn't seem worth the effort at the current
2183 time. */
2185 copy_mode = word_mode;
2186 if (MEM_P (target))
2188 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2189 if (mem_mode != BLKmode)
2190 copy_mode = mem_mode;
2192 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2193 copy_mode = tmode;
2195 for (bitpos = 0, xbitpos = padding_correction;
2196 bitpos < bytes * BITS_PER_UNIT;
2197 bitpos += bitsize, xbitpos += bitsize)
2199 /* We need a new source operand each time xbitpos is on a
2200 word boundary and when xbitpos == padding_correction
2201 (the first time through). */
2202 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2203 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2205 /* We need a new destination operand each time bitpos is on
2206 a word boundary. */
2207 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2208 dst = target;
2209 else if (bitpos % BITS_PER_WORD == 0)
2210 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2212 /* Use xbitpos for the source extraction (right justified) and
2213 bitpos for the destination store (left justified). */
2214 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2215 extract_bit_field (src, bitsize,
2216 xbitpos % BITS_PER_WORD, 1,
2217 NULL_RTX, copy_mode, copy_mode));
2221 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2222 register if it contains any data, otherwise return null.
2224 This is used on targets that return BLKmode values in registers. */
2227 copy_blkmode_to_reg (machine_mode mode, tree src)
2229 int i, n_regs;
2230 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2231 unsigned int bitsize;
2232 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2233 machine_mode dst_mode;
2235 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2237 x = expand_normal (src);
2239 bytes = int_size_in_bytes (TREE_TYPE (src));
2240 if (bytes == 0)
2241 return NULL_RTX;
2243 /* If the structure doesn't take up a whole number of words, see
2244 whether the register value should be padded on the left or on
2245 the right. Set PADDING_CORRECTION to the number of padding
2246 bits needed on the left side.
2248 In most ABIs, the structure will be returned at the least end of
2249 the register, which translates to right padding on little-endian
2250 targets and left padding on big-endian targets. The opposite
2251 holds if the structure is returned at the most significant
2252 end of the register. */
2253 if (bytes % UNITS_PER_WORD != 0
2254 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2255 ? !BYTES_BIG_ENDIAN
2256 : BYTES_BIG_ENDIAN))
2257 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2258 * BITS_PER_UNIT));
2260 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2261 dst_words = XALLOCAVEC (rtx, n_regs);
2262 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2264 /* Copy the structure BITSIZE bits at a time. */
2265 for (bitpos = 0, xbitpos = padding_correction;
2266 bitpos < bytes * BITS_PER_UNIT;
2267 bitpos += bitsize, xbitpos += bitsize)
2269 /* We need a new destination pseudo each time xbitpos is
2270 on a word boundary and when xbitpos == padding_correction
2271 (the first time through). */
2272 if (xbitpos % BITS_PER_WORD == 0
2273 || xbitpos == padding_correction)
2275 /* Generate an appropriate register. */
2276 dst_word = gen_reg_rtx (word_mode);
2277 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2279 /* Clear the destination before we move anything into it. */
2280 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2283 /* We need a new source operand each time bitpos is on a word
2284 boundary. */
2285 if (bitpos % BITS_PER_WORD == 0)
2286 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2288 /* Use bitpos for the source extraction (left justified) and
2289 xbitpos for the destination store (right justified). */
2290 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2291 0, 0, word_mode,
2292 extract_bit_field (src_word, bitsize,
2293 bitpos % BITS_PER_WORD, 1,
2294 NULL_RTX, word_mode, word_mode));
2297 if (mode == BLKmode)
2299 /* Find the smallest integer mode large enough to hold the
2300 entire structure. */
2301 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2302 mode != VOIDmode;
2303 mode = GET_MODE_WIDER_MODE (mode))
2304 /* Have we found a large enough mode? */
2305 if (GET_MODE_SIZE (mode) >= bytes)
2306 break;
2308 /* A suitable mode should have been found. */
2309 gcc_assert (mode != VOIDmode);
2312 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2313 dst_mode = word_mode;
2314 else
2315 dst_mode = mode;
2316 dst = gen_reg_rtx (dst_mode);
2318 for (i = 0; i < n_regs; i++)
2319 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2321 if (mode != dst_mode)
2322 dst = gen_lowpart (mode, dst);
2324 return dst;
2327 /* Add a USE expression for REG to the (possibly empty) list pointed
2328 to by CALL_FUSAGE. REG must denote a hard register. */
2330 void
2331 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2333 gcc_assert (REG_P (reg));
2335 if (!HARD_REGISTER_P (reg))
2336 return;
2338 *call_fusage
2339 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2342 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2343 to by CALL_FUSAGE. REG must denote a hard register. */
2345 void
2346 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2348 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2350 *call_fusage
2351 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2354 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2355 starting at REGNO. All of these registers must be hard registers. */
2357 void
2358 use_regs (rtx *call_fusage, int regno, int nregs)
2360 int i;
2362 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2364 for (i = 0; i < nregs; i++)
2365 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2368 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2369 PARALLEL REGS. This is for calls that pass values in multiple
2370 non-contiguous locations. The Irix 6 ABI has examples of this. */
2372 void
2373 use_group_regs (rtx *call_fusage, rtx regs)
2375 int i;
2377 for (i = 0; i < XVECLEN (regs, 0); i++)
2379 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2381 /* A NULL entry means the parameter goes both on the stack and in
2382 registers. This can also be a MEM for targets that pass values
2383 partially on the stack and partially in registers. */
2384 if (reg != 0 && REG_P (reg))
2385 use_reg (call_fusage, reg);
2389 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2390 assigment and the code of the expresion on the RHS is CODE. Return
2391 NULL otherwise. */
2393 static gimple
2394 get_def_for_expr (tree name, enum tree_code code)
2396 gimple def_stmt;
2398 if (TREE_CODE (name) != SSA_NAME)
2399 return NULL;
2401 def_stmt = get_gimple_for_ssa_name (name);
2402 if (!def_stmt
2403 || gimple_assign_rhs_code (def_stmt) != code)
2404 return NULL;
2406 return def_stmt;
2409 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2410 assigment and the class of the expresion on the RHS is CLASS. Return
2411 NULL otherwise. */
2413 static gimple
2414 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2416 gimple def_stmt;
2418 if (TREE_CODE (name) != SSA_NAME)
2419 return NULL;
2421 def_stmt = get_gimple_for_ssa_name (name);
2422 if (!def_stmt
2423 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2424 return NULL;
2426 return def_stmt;
2430 /* Determine whether the LEN bytes generated by CONSTFUN can be
2431 stored to memory using several move instructions. CONSTFUNDATA is
2432 a pointer which will be passed as argument in every CONSTFUN call.
2433 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2434 a memset operation and false if it's a copy of a constant string.
2435 Return nonzero if a call to store_by_pieces should succeed. */
2438 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2439 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2440 void *constfundata, unsigned int align, bool memsetp)
2442 unsigned HOST_WIDE_INT l;
2443 unsigned int max_size;
2444 HOST_WIDE_INT offset = 0;
2445 machine_mode mode;
2446 enum insn_code icode;
2447 int reverse;
2448 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2449 rtx cst ATTRIBUTE_UNUSED;
2451 if (len == 0)
2452 return 1;
2454 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2455 memsetp
2456 ? SET_BY_PIECES
2457 : STORE_BY_PIECES,
2458 optimize_insn_for_speed_p ()))
2459 return 0;
2461 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2463 /* We would first store what we can in the largest integer mode, then go to
2464 successively smaller modes. */
2466 for (reverse = 0;
2467 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2468 reverse++)
2470 l = len;
2471 max_size = STORE_MAX_PIECES + 1;
2472 while (max_size > 1 && l > 0)
2474 mode = widest_int_mode_for_size (max_size);
2476 if (mode == VOIDmode)
2477 break;
2479 icode = optab_handler (mov_optab, mode);
2480 if (icode != CODE_FOR_nothing
2481 && align >= GET_MODE_ALIGNMENT (mode))
2483 unsigned int size = GET_MODE_SIZE (mode);
2485 while (l >= size)
2487 if (reverse)
2488 offset -= size;
2490 cst = (*constfun) (constfundata, offset, mode);
2491 if (!targetm.legitimate_constant_p (mode, cst))
2492 return 0;
2494 if (!reverse)
2495 offset += size;
2497 l -= size;
2501 max_size = GET_MODE_SIZE (mode);
2504 /* The code above should have handled everything. */
2505 gcc_assert (!l);
2508 return 1;
2511 /* Generate several move instructions to store LEN bytes generated by
2512 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2513 pointer which will be passed as argument in every CONSTFUN call.
2514 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2515 a memset operation and false if it's a copy of a constant string.
2516 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2517 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2518 stpcpy. */
2521 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2522 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2523 void *constfundata, unsigned int align, bool memsetp, int endp)
2525 machine_mode to_addr_mode = get_address_mode (to);
2526 struct store_by_pieces_d data;
2528 if (len == 0)
2530 gcc_assert (endp != 2);
2531 return to;
2534 gcc_assert (targetm.use_by_pieces_infrastructure_p
2535 (len, align,
2536 memsetp
2537 ? SET_BY_PIECES
2538 : STORE_BY_PIECES,
2539 optimize_insn_for_speed_p ()));
2541 data.constfun = constfun;
2542 data.constfundata = constfundata;
2543 data.len = len;
2544 data.to = to;
2545 store_by_pieces_1 (&data, align);
2546 if (endp)
2548 rtx to1;
2550 gcc_assert (!data.reverse);
2551 if (data.autinc_to)
2553 if (endp == 2)
2555 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2556 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2557 else
2558 data.to_addr = copy_to_mode_reg (to_addr_mode,
2559 plus_constant (to_addr_mode,
2560 data.to_addr,
2561 -1));
2563 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2564 data.offset);
2566 else
2568 if (endp == 2)
2569 --data.offset;
2570 to1 = adjust_address (data.to, QImode, data.offset);
2572 return to1;
2574 else
2575 return data.to;
2578 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2579 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2581 static void
2582 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2584 struct store_by_pieces_d data;
2586 if (len == 0)
2587 return;
2589 data.constfun = clear_by_pieces_1;
2590 data.constfundata = NULL;
2591 data.len = len;
2592 data.to = to;
2593 store_by_pieces_1 (&data, align);
2596 /* Callback routine for clear_by_pieces.
2597 Return const0_rtx unconditionally. */
2599 static rtx
2600 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2601 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2602 machine_mode mode ATTRIBUTE_UNUSED)
2604 return const0_rtx;
2607 /* Subroutine of clear_by_pieces and store_by_pieces.
2608 Generate several move instructions to store LEN bytes of block TO. (A MEM
2609 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2611 static void
2612 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2613 unsigned int align ATTRIBUTE_UNUSED)
2615 machine_mode to_addr_mode = get_address_mode (data->to);
2616 rtx to_addr = XEXP (data->to, 0);
2617 unsigned int max_size = STORE_MAX_PIECES + 1;
2618 enum insn_code icode;
2620 data->offset = 0;
2621 data->to_addr = to_addr;
2622 data->autinc_to
2623 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2624 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2626 data->explicit_inc_to = 0;
2627 data->reverse
2628 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2629 if (data->reverse)
2630 data->offset = data->len;
2632 /* If storing requires more than two move insns,
2633 copy addresses to registers (to make displacements shorter)
2634 and use post-increment if available. */
2635 if (!data->autinc_to
2636 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2638 /* Determine the main mode we'll be using.
2639 MODE might not be used depending on the definitions of the
2640 USE_* macros below. */
2641 machine_mode mode ATTRIBUTE_UNUSED
2642 = widest_int_mode_for_size (max_size);
2644 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2646 data->to_addr = copy_to_mode_reg (to_addr_mode,
2647 plus_constant (to_addr_mode,
2648 to_addr,
2649 data->len));
2650 data->autinc_to = 1;
2651 data->explicit_inc_to = -1;
2654 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2655 && ! data->autinc_to)
2657 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2658 data->autinc_to = 1;
2659 data->explicit_inc_to = 1;
2662 if ( !data->autinc_to && CONSTANT_P (to_addr))
2663 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2666 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2668 /* First store what we can in the largest integer mode, then go to
2669 successively smaller modes. */
2671 while (max_size > 1 && data->len > 0)
2673 machine_mode mode = widest_int_mode_for_size (max_size);
2675 if (mode == VOIDmode)
2676 break;
2678 icode = optab_handler (mov_optab, mode);
2679 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2680 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2682 max_size = GET_MODE_SIZE (mode);
2685 /* The code above should have handled everything. */
2686 gcc_assert (!data->len);
2689 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2690 with move instructions for mode MODE. GENFUN is the gen_... function
2691 to make a move insn for that mode. DATA has all the other info. */
2693 static void
2694 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2695 struct store_by_pieces_d *data)
2697 unsigned int size = GET_MODE_SIZE (mode);
2698 rtx to1, cst;
2700 while (data->len >= size)
2702 if (data->reverse)
2703 data->offset -= size;
2705 if (data->autinc_to)
2706 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2707 data->offset);
2708 else
2709 to1 = adjust_address (data->to, mode, data->offset);
2711 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2712 emit_insn (gen_add2_insn (data->to_addr,
2713 gen_int_mode (-(HOST_WIDE_INT) size,
2714 GET_MODE (data->to_addr))));
2716 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2717 emit_insn ((*genfun) (to1, cst));
2719 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2720 emit_insn (gen_add2_insn (data->to_addr,
2721 gen_int_mode (size,
2722 GET_MODE (data->to_addr))));
2724 if (! data->reverse)
2725 data->offset += size;
2727 data->len -= size;
2731 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2732 its length in bytes. */
2735 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2736 unsigned int expected_align, HOST_WIDE_INT expected_size,
2737 unsigned HOST_WIDE_INT min_size,
2738 unsigned HOST_WIDE_INT max_size,
2739 unsigned HOST_WIDE_INT probable_max_size)
2741 machine_mode mode = GET_MODE (object);
2742 unsigned int align;
2744 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2746 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2747 just move a zero. Otherwise, do this a piece at a time. */
2748 if (mode != BLKmode
2749 && CONST_INT_P (size)
2750 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2752 rtx zero = CONST0_RTX (mode);
2753 if (zero != NULL)
2755 emit_move_insn (object, zero);
2756 return NULL;
2759 if (COMPLEX_MODE_P (mode))
2761 zero = CONST0_RTX (GET_MODE_INNER (mode));
2762 if (zero != NULL)
2764 write_complex_part (object, zero, 0);
2765 write_complex_part (object, zero, 1);
2766 return NULL;
2771 if (size == const0_rtx)
2772 return NULL;
2774 align = MEM_ALIGN (object);
2776 if (CONST_INT_P (size)
2777 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2778 CLEAR_BY_PIECES,
2779 optimize_insn_for_speed_p ()))
2780 clear_by_pieces (object, INTVAL (size), align);
2781 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2782 expected_align, expected_size,
2783 min_size, max_size, probable_max_size))
2785 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2786 return set_storage_via_libcall (object, size, const0_rtx,
2787 method == BLOCK_OP_TAILCALL);
2788 else
2789 gcc_unreachable ();
2791 return NULL;
2795 clear_storage (rtx object, rtx size, enum block_op_methods method)
2797 unsigned HOST_WIDE_INT max, min = 0;
2798 if (GET_CODE (size) == CONST_INT)
2799 min = max = UINTVAL (size);
2800 else
2801 max = GET_MODE_MASK (GET_MODE (size));
2802 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2806 /* A subroutine of clear_storage. Expand a call to memset.
2807 Return the return value of memset, 0 otherwise. */
2810 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2812 tree call_expr, fn, object_tree, size_tree, val_tree;
2813 machine_mode size_mode;
2814 rtx retval;
2816 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2817 place those into new pseudos into a VAR_DECL and use them later. */
2819 object = copy_addr_to_reg (XEXP (object, 0));
2821 size_mode = TYPE_MODE (sizetype);
2822 size = convert_to_mode (size_mode, size, 1);
2823 size = copy_to_mode_reg (size_mode, size);
2825 /* It is incorrect to use the libcall calling conventions to call
2826 memset in this context. This could be a user call to memset and
2827 the user may wish to examine the return value from memset. For
2828 targets where libcalls and normal calls have different conventions
2829 for returning pointers, we could end up generating incorrect code. */
2831 object_tree = make_tree (ptr_type_node, object);
2832 if (!CONST_INT_P (val))
2833 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2834 size_tree = make_tree (sizetype, size);
2835 val_tree = make_tree (integer_type_node, val);
2837 fn = clear_storage_libcall_fn (true);
2838 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2839 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2841 retval = expand_normal (call_expr);
2843 return retval;
2846 /* A subroutine of set_storage_via_libcall. Create the tree node
2847 for the function we use for block clears. */
2849 tree block_clear_fn;
2851 void
2852 init_block_clear_fn (const char *asmspec)
2854 if (!block_clear_fn)
2856 tree fn, args;
2858 fn = get_identifier ("memset");
2859 args = build_function_type_list (ptr_type_node, ptr_type_node,
2860 integer_type_node, sizetype,
2861 NULL_TREE);
2863 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2864 DECL_EXTERNAL (fn) = 1;
2865 TREE_PUBLIC (fn) = 1;
2866 DECL_ARTIFICIAL (fn) = 1;
2867 TREE_NOTHROW (fn) = 1;
2868 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2869 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2871 block_clear_fn = fn;
2874 if (asmspec)
2875 set_user_assembler_name (block_clear_fn, asmspec);
2878 static tree
2879 clear_storage_libcall_fn (int for_call)
2881 static bool emitted_extern;
2883 if (!block_clear_fn)
2884 init_block_clear_fn (NULL);
2886 if (for_call && !emitted_extern)
2888 emitted_extern = true;
2889 make_decl_rtl (block_clear_fn);
2892 return block_clear_fn;
2895 /* Expand a setmem pattern; return true if successful. */
2897 bool
2898 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2899 unsigned int expected_align, HOST_WIDE_INT expected_size,
2900 unsigned HOST_WIDE_INT min_size,
2901 unsigned HOST_WIDE_INT max_size,
2902 unsigned HOST_WIDE_INT probable_max_size)
2904 /* Try the most limited insn first, because there's no point
2905 including more than one in the machine description unless
2906 the more limited one has some advantage. */
2908 machine_mode mode;
2910 if (expected_align < align)
2911 expected_align = align;
2912 if (expected_size != -1)
2914 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2915 expected_size = max_size;
2916 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2917 expected_size = min_size;
2920 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2921 mode = GET_MODE_WIDER_MODE (mode))
2923 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2925 if (code != CODE_FOR_nothing
2926 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2927 here because if SIZE is less than the mode mask, as it is
2928 returned by the macro, it will definitely be less than the
2929 actual mode mask. Since SIZE is within the Pmode address
2930 space, we limit MODE to Pmode. */
2931 && ((CONST_INT_P (size)
2932 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2933 <= (GET_MODE_MASK (mode) >> 1)))
2934 || max_size <= (GET_MODE_MASK (mode) >> 1)
2935 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2937 struct expand_operand ops[9];
2938 unsigned int nops;
2940 nops = insn_data[(int) code].n_generator_args;
2941 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2943 create_fixed_operand (&ops[0], object);
2944 /* The check above guarantees that this size conversion is valid. */
2945 create_convert_operand_to (&ops[1], size, mode, true);
2946 create_convert_operand_from (&ops[2], val, byte_mode, true);
2947 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2948 if (nops >= 6)
2950 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2951 create_integer_operand (&ops[5], expected_size);
2953 if (nops >= 8)
2955 create_integer_operand (&ops[6], min_size);
2956 /* If we can not represent the maximal size,
2957 make parameter NULL. */
2958 if ((HOST_WIDE_INT) max_size != -1)
2959 create_integer_operand (&ops[7], max_size);
2960 else
2961 create_fixed_operand (&ops[7], NULL);
2963 if (nops == 9)
2965 /* If we can not represent the maximal size,
2966 make parameter NULL. */
2967 if ((HOST_WIDE_INT) probable_max_size != -1)
2968 create_integer_operand (&ops[8], probable_max_size);
2969 else
2970 create_fixed_operand (&ops[8], NULL);
2972 if (maybe_expand_insn (code, nops, ops))
2973 return true;
2977 return false;
2981 /* Write to one of the components of the complex value CPLX. Write VAL to
2982 the real part if IMAG_P is false, and the imaginary part if its true. */
2984 void
2985 write_complex_part (rtx cplx, rtx val, bool imag_p)
2987 machine_mode cmode;
2988 machine_mode imode;
2989 unsigned ibitsize;
2991 if (GET_CODE (cplx) == CONCAT)
2993 emit_move_insn (XEXP (cplx, imag_p), val);
2994 return;
2997 cmode = GET_MODE (cplx);
2998 imode = GET_MODE_INNER (cmode);
2999 ibitsize = GET_MODE_BITSIZE (imode);
3001 /* For MEMs simplify_gen_subreg may generate an invalid new address
3002 because, e.g., the original address is considered mode-dependent
3003 by the target, which restricts simplify_subreg from invoking
3004 adjust_address_nv. Instead of preparing fallback support for an
3005 invalid address, we call adjust_address_nv directly. */
3006 if (MEM_P (cplx))
3008 emit_move_insn (adjust_address_nv (cplx, imode,
3009 imag_p ? GET_MODE_SIZE (imode) : 0),
3010 val);
3011 return;
3014 /* If the sub-object is at least word sized, then we know that subregging
3015 will work. This special case is important, since store_bit_field
3016 wants to operate on integer modes, and there's rarely an OImode to
3017 correspond to TCmode. */
3018 if (ibitsize >= BITS_PER_WORD
3019 /* For hard regs we have exact predicates. Assume we can split
3020 the original object if it spans an even number of hard regs.
3021 This special case is important for SCmode on 64-bit platforms
3022 where the natural size of floating-point regs is 32-bit. */
3023 || (REG_P (cplx)
3024 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3025 && REG_NREGS (cplx) % 2 == 0))
3027 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3028 imag_p ? GET_MODE_SIZE (imode) : 0);
3029 if (part)
3031 emit_move_insn (part, val);
3032 return;
3034 else
3035 /* simplify_gen_subreg may fail for sub-word MEMs. */
3036 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3039 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3042 /* Extract one of the components of the complex value CPLX. Extract the
3043 real part if IMAG_P is false, and the imaginary part if it's true. */
3045 static rtx
3046 read_complex_part (rtx cplx, bool imag_p)
3048 machine_mode cmode, imode;
3049 unsigned ibitsize;
3051 if (GET_CODE (cplx) == CONCAT)
3052 return XEXP (cplx, imag_p);
3054 cmode = GET_MODE (cplx);
3055 imode = GET_MODE_INNER (cmode);
3056 ibitsize = GET_MODE_BITSIZE (imode);
3058 /* Special case reads from complex constants that got spilled to memory. */
3059 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3061 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3062 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3064 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3065 if (CONSTANT_CLASS_P (part))
3066 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3070 /* For MEMs simplify_gen_subreg may generate an invalid new address
3071 because, e.g., the original address is considered mode-dependent
3072 by the target, which restricts simplify_subreg from invoking
3073 adjust_address_nv. Instead of preparing fallback support for an
3074 invalid address, we call adjust_address_nv directly. */
3075 if (MEM_P (cplx))
3076 return adjust_address_nv (cplx, imode,
3077 imag_p ? GET_MODE_SIZE (imode) : 0);
3079 /* If the sub-object is at least word sized, then we know that subregging
3080 will work. This special case is important, since extract_bit_field
3081 wants to operate on integer modes, and there's rarely an OImode to
3082 correspond to TCmode. */
3083 if (ibitsize >= BITS_PER_WORD
3084 /* For hard regs we have exact predicates. Assume we can split
3085 the original object if it spans an even number of hard regs.
3086 This special case is important for SCmode on 64-bit platforms
3087 where the natural size of floating-point regs is 32-bit. */
3088 || (REG_P (cplx)
3089 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3090 && REG_NREGS (cplx) % 2 == 0))
3092 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3093 imag_p ? GET_MODE_SIZE (imode) : 0);
3094 if (ret)
3095 return ret;
3096 else
3097 /* simplify_gen_subreg may fail for sub-word MEMs. */
3098 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3101 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3102 true, NULL_RTX, imode, imode);
3105 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3106 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3107 represented in NEW_MODE. If FORCE is true, this will never happen, as
3108 we'll force-create a SUBREG if needed. */
3110 static rtx
3111 emit_move_change_mode (machine_mode new_mode,
3112 machine_mode old_mode, rtx x, bool force)
3114 rtx ret;
3116 if (push_operand (x, GET_MODE (x)))
3118 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3119 MEM_COPY_ATTRIBUTES (ret, x);
3121 else if (MEM_P (x))
3123 /* We don't have to worry about changing the address since the
3124 size in bytes is supposed to be the same. */
3125 if (reload_in_progress)
3127 /* Copy the MEM to change the mode and move any
3128 substitutions from the old MEM to the new one. */
3129 ret = adjust_address_nv (x, new_mode, 0);
3130 copy_replacements (x, ret);
3132 else
3133 ret = adjust_address (x, new_mode, 0);
3135 else
3137 /* Note that we do want simplify_subreg's behavior of validating
3138 that the new mode is ok for a hard register. If we were to use
3139 simplify_gen_subreg, we would create the subreg, but would
3140 probably run into the target not being able to implement it. */
3141 /* Except, of course, when FORCE is true, when this is exactly what
3142 we want. Which is needed for CCmodes on some targets. */
3143 if (force)
3144 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3145 else
3146 ret = simplify_subreg (new_mode, x, old_mode, 0);
3149 return ret;
3152 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3153 an integer mode of the same size as MODE. Returns the instruction
3154 emitted, or NULL if such a move could not be generated. */
3156 static rtx_insn *
3157 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3159 machine_mode imode;
3160 enum insn_code code;
3162 /* There must exist a mode of the exact size we require. */
3163 imode = int_mode_for_mode (mode);
3164 if (imode == BLKmode)
3165 return NULL;
3167 /* The target must support moves in this mode. */
3168 code = optab_handler (mov_optab, imode);
3169 if (code == CODE_FOR_nothing)
3170 return NULL;
3172 x = emit_move_change_mode (imode, mode, x, force);
3173 if (x == NULL_RTX)
3174 return NULL;
3175 y = emit_move_change_mode (imode, mode, y, force);
3176 if (y == NULL_RTX)
3177 return NULL;
3178 return emit_insn (GEN_FCN (code) (x, y));
3181 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3182 Return an equivalent MEM that does not use an auto-increment. */
3185 emit_move_resolve_push (machine_mode mode, rtx x)
3187 enum rtx_code code = GET_CODE (XEXP (x, 0));
3188 HOST_WIDE_INT adjust;
3189 rtx temp;
3191 adjust = GET_MODE_SIZE (mode);
3192 #ifdef PUSH_ROUNDING
3193 adjust = PUSH_ROUNDING (adjust);
3194 #endif
3195 if (code == PRE_DEC || code == POST_DEC)
3196 adjust = -adjust;
3197 else if (code == PRE_MODIFY || code == POST_MODIFY)
3199 rtx expr = XEXP (XEXP (x, 0), 1);
3200 HOST_WIDE_INT val;
3202 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3203 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3204 val = INTVAL (XEXP (expr, 1));
3205 if (GET_CODE (expr) == MINUS)
3206 val = -val;
3207 gcc_assert (adjust == val || adjust == -val);
3208 adjust = val;
3211 /* Do not use anti_adjust_stack, since we don't want to update
3212 stack_pointer_delta. */
3213 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3214 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3215 0, OPTAB_LIB_WIDEN);
3216 if (temp != stack_pointer_rtx)
3217 emit_move_insn (stack_pointer_rtx, temp);
3219 switch (code)
3221 case PRE_INC:
3222 case PRE_DEC:
3223 case PRE_MODIFY:
3224 temp = stack_pointer_rtx;
3225 break;
3226 case POST_INC:
3227 case POST_DEC:
3228 case POST_MODIFY:
3229 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3230 break;
3231 default:
3232 gcc_unreachable ();
3235 return replace_equiv_address (x, temp);
3238 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3239 X is known to satisfy push_operand, and MODE is known to be complex.
3240 Returns the last instruction emitted. */
3242 rtx_insn *
3243 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3245 machine_mode submode = GET_MODE_INNER (mode);
3246 bool imag_first;
3248 #ifdef PUSH_ROUNDING
3249 unsigned int submodesize = GET_MODE_SIZE (submode);
3251 /* In case we output to the stack, but the size is smaller than the
3252 machine can push exactly, we need to use move instructions. */
3253 if (PUSH_ROUNDING (submodesize) != submodesize)
3255 x = emit_move_resolve_push (mode, x);
3256 return emit_move_insn (x, y);
3258 #endif
3260 /* Note that the real part always precedes the imag part in memory
3261 regardless of machine's endianness. */
3262 switch (GET_CODE (XEXP (x, 0)))
3264 case PRE_DEC:
3265 case POST_DEC:
3266 imag_first = true;
3267 break;
3268 case PRE_INC:
3269 case POST_INC:
3270 imag_first = false;
3271 break;
3272 default:
3273 gcc_unreachable ();
3276 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3277 read_complex_part (y, imag_first));
3278 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3279 read_complex_part (y, !imag_first));
3282 /* A subroutine of emit_move_complex. Perform the move from Y to X
3283 via two moves of the parts. Returns the last instruction emitted. */
3285 rtx_insn *
3286 emit_move_complex_parts (rtx x, rtx y)
3288 /* Show the output dies here. This is necessary for SUBREGs
3289 of pseudos since we cannot track their lifetimes correctly;
3290 hard regs shouldn't appear here except as return values. */
3291 if (!reload_completed && !reload_in_progress
3292 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3293 emit_clobber (x);
3295 write_complex_part (x, read_complex_part (y, false), false);
3296 write_complex_part (x, read_complex_part (y, true), true);
3298 return get_last_insn ();
3301 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3302 MODE is known to be complex. Returns the last instruction emitted. */
3304 static rtx_insn *
3305 emit_move_complex (machine_mode mode, rtx x, rtx y)
3307 bool try_int;
3309 /* Need to take special care for pushes, to maintain proper ordering
3310 of the data, and possibly extra padding. */
3311 if (push_operand (x, mode))
3312 return emit_move_complex_push (mode, x, y);
3314 /* See if we can coerce the target into moving both values at once, except
3315 for floating point where we favor moving as parts if this is easy. */
3316 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3317 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3318 && !(REG_P (x)
3319 && HARD_REGISTER_P (x)
3320 && REG_NREGS (x) == 1)
3321 && !(REG_P (y)
3322 && HARD_REGISTER_P (y)
3323 && REG_NREGS (y) == 1))
3324 try_int = false;
3325 /* Not possible if the values are inherently not adjacent. */
3326 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3327 try_int = false;
3328 /* Is possible if both are registers (or subregs of registers). */
3329 else if (register_operand (x, mode) && register_operand (y, mode))
3330 try_int = true;
3331 /* If one of the operands is a memory, and alignment constraints
3332 are friendly enough, we may be able to do combined memory operations.
3333 We do not attempt this if Y is a constant because that combination is
3334 usually better with the by-parts thing below. */
3335 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3336 && (!STRICT_ALIGNMENT
3337 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3338 try_int = true;
3339 else
3340 try_int = false;
3342 if (try_int)
3344 rtx_insn *ret;
3346 /* For memory to memory moves, optimal behavior can be had with the
3347 existing block move logic. */
3348 if (MEM_P (x) && MEM_P (y))
3350 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3351 BLOCK_OP_NO_LIBCALL);
3352 return get_last_insn ();
3355 ret = emit_move_via_integer (mode, x, y, true);
3356 if (ret)
3357 return ret;
3360 return emit_move_complex_parts (x, y);
3363 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3364 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3366 static rtx_insn *
3367 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3369 rtx_insn *ret;
3371 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3372 if (mode != CCmode)
3374 enum insn_code code = optab_handler (mov_optab, CCmode);
3375 if (code != CODE_FOR_nothing)
3377 x = emit_move_change_mode (CCmode, mode, x, true);
3378 y = emit_move_change_mode (CCmode, mode, y, true);
3379 return emit_insn (GEN_FCN (code) (x, y));
3383 /* Otherwise, find the MODE_INT mode of the same width. */
3384 ret = emit_move_via_integer (mode, x, y, false);
3385 gcc_assert (ret != NULL);
3386 return ret;
3389 /* Return true if word I of OP lies entirely in the
3390 undefined bits of a paradoxical subreg. */
3392 static bool
3393 undefined_operand_subword_p (const_rtx op, int i)
3395 machine_mode innermode, innermostmode;
3396 int offset;
3397 if (GET_CODE (op) != SUBREG)
3398 return false;
3399 innermode = GET_MODE (op);
3400 innermostmode = GET_MODE (SUBREG_REG (op));
3401 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3402 /* The SUBREG_BYTE represents offset, as if the value were stored in
3403 memory, except for a paradoxical subreg where we define
3404 SUBREG_BYTE to be 0; undo this exception as in
3405 simplify_subreg. */
3406 if (SUBREG_BYTE (op) == 0
3407 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3409 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3410 if (WORDS_BIG_ENDIAN)
3411 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3412 if (BYTES_BIG_ENDIAN)
3413 offset += difference % UNITS_PER_WORD;
3415 if (offset >= GET_MODE_SIZE (innermostmode)
3416 || offset <= -GET_MODE_SIZE (word_mode))
3417 return true;
3418 return false;
3421 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3422 MODE is any multi-word or full-word mode that lacks a move_insn
3423 pattern. Note that you will get better code if you define such
3424 patterns, even if they must turn into multiple assembler instructions. */
3426 static rtx_insn *
3427 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3429 rtx_insn *last_insn = 0;
3430 rtx_insn *seq;
3431 rtx inner;
3432 bool need_clobber;
3433 int i;
3435 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3437 /* If X is a push on the stack, do the push now and replace
3438 X with a reference to the stack pointer. */
3439 if (push_operand (x, mode))
3440 x = emit_move_resolve_push (mode, x);
3442 /* If we are in reload, see if either operand is a MEM whose address
3443 is scheduled for replacement. */
3444 if (reload_in_progress && MEM_P (x)
3445 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3446 x = replace_equiv_address_nv (x, inner);
3447 if (reload_in_progress && MEM_P (y)
3448 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3449 y = replace_equiv_address_nv (y, inner);
3451 start_sequence ();
3453 need_clobber = false;
3454 for (i = 0;
3455 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3456 i++)
3458 rtx xpart = operand_subword (x, i, 1, mode);
3459 rtx ypart;
3461 /* Do not generate code for a move if it would come entirely
3462 from the undefined bits of a paradoxical subreg. */
3463 if (undefined_operand_subword_p (y, i))
3464 continue;
3466 ypart = operand_subword (y, i, 1, mode);
3468 /* If we can't get a part of Y, put Y into memory if it is a
3469 constant. Otherwise, force it into a register. Then we must
3470 be able to get a part of Y. */
3471 if (ypart == 0 && CONSTANT_P (y))
3473 y = use_anchored_address (force_const_mem (mode, y));
3474 ypart = operand_subword (y, i, 1, mode);
3476 else if (ypart == 0)
3477 ypart = operand_subword_force (y, i, mode);
3479 gcc_assert (xpart && ypart);
3481 need_clobber |= (GET_CODE (xpart) == SUBREG);
3483 last_insn = emit_move_insn (xpart, ypart);
3486 seq = get_insns ();
3487 end_sequence ();
3489 /* Show the output dies here. This is necessary for SUBREGs
3490 of pseudos since we cannot track their lifetimes correctly;
3491 hard regs shouldn't appear here except as return values.
3492 We never want to emit such a clobber after reload. */
3493 if (x != y
3494 && ! (reload_in_progress || reload_completed)
3495 && need_clobber != 0)
3496 emit_clobber (x);
3498 emit_insn (seq);
3500 return last_insn;
3503 /* Low level part of emit_move_insn.
3504 Called just like emit_move_insn, but assumes X and Y
3505 are basically valid. */
3507 rtx_insn *
3508 emit_move_insn_1 (rtx x, rtx y)
3510 machine_mode mode = GET_MODE (x);
3511 enum insn_code code;
3513 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3515 code = optab_handler (mov_optab, mode);
3516 if (code != CODE_FOR_nothing)
3517 return emit_insn (GEN_FCN (code) (x, y));
3519 /* Expand complex moves by moving real part and imag part. */
3520 if (COMPLEX_MODE_P (mode))
3521 return emit_move_complex (mode, x, y);
3523 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3524 || ALL_FIXED_POINT_MODE_P (mode))
3526 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3528 /* If we can't find an integer mode, use multi words. */
3529 if (result)
3530 return result;
3531 else
3532 return emit_move_multi_word (mode, x, y);
3535 if (GET_MODE_CLASS (mode) == MODE_CC)
3536 return emit_move_ccmode (mode, x, y);
3538 /* Try using a move pattern for the corresponding integer mode. This is
3539 only safe when simplify_subreg can convert MODE constants into integer
3540 constants. At present, it can only do this reliably if the value
3541 fits within a HOST_WIDE_INT. */
3542 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3544 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3546 if (ret)
3548 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3549 return ret;
3553 return emit_move_multi_word (mode, x, y);
3556 /* Generate code to copy Y into X.
3557 Both Y and X must have the same mode, except that
3558 Y can be a constant with VOIDmode.
3559 This mode cannot be BLKmode; use emit_block_move for that.
3561 Return the last instruction emitted. */
3563 rtx_insn *
3564 emit_move_insn (rtx x, rtx y)
3566 machine_mode mode = GET_MODE (x);
3567 rtx y_cst = NULL_RTX;
3568 rtx_insn *last_insn;
3569 rtx set;
3571 gcc_assert (mode != BLKmode
3572 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3574 if (CONSTANT_P (y))
3576 if (optimize
3577 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3578 && (last_insn = compress_float_constant (x, y)))
3579 return last_insn;
3581 y_cst = y;
3583 if (!targetm.legitimate_constant_p (mode, y))
3585 y = force_const_mem (mode, y);
3587 /* If the target's cannot_force_const_mem prevented the spill,
3588 assume that the target's move expanders will also take care
3589 of the non-legitimate constant. */
3590 if (!y)
3591 y = y_cst;
3592 else
3593 y = use_anchored_address (y);
3597 /* If X or Y are memory references, verify that their addresses are valid
3598 for the machine. */
3599 if (MEM_P (x)
3600 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3601 MEM_ADDR_SPACE (x))
3602 && ! push_operand (x, GET_MODE (x))))
3603 x = validize_mem (x);
3605 if (MEM_P (y)
3606 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3607 MEM_ADDR_SPACE (y)))
3608 y = validize_mem (y);
3610 gcc_assert (mode != BLKmode);
3612 last_insn = emit_move_insn_1 (x, y);
3614 if (y_cst && REG_P (x)
3615 && (set = single_set (last_insn)) != NULL_RTX
3616 && SET_DEST (set) == x
3617 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3618 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3620 return last_insn;
3623 /* Generate the body of an instruction to copy Y into X.
3624 It may be a list of insns, if one insn isn't enough. */
3626 rtx_insn *
3627 gen_move_insn (rtx x, rtx y)
3629 rtx_insn *seq;
3631 start_sequence ();
3632 emit_move_insn_1 (x, y);
3633 seq = get_insns ();
3634 end_sequence ();
3635 return seq;
3638 /* Same as above, but return rtx (used as a callback, which must have
3639 prototype compatible with other functions returning rtx). */
3642 gen_move_insn_uncast (rtx x, rtx y)
3644 return gen_move_insn (x, y);
3647 /* If Y is representable exactly in a narrower mode, and the target can
3648 perform the extension directly from constant or memory, then emit the
3649 move as an extension. */
3651 static rtx_insn *
3652 compress_float_constant (rtx x, rtx y)
3654 machine_mode dstmode = GET_MODE (x);
3655 machine_mode orig_srcmode = GET_MODE (y);
3656 machine_mode srcmode;
3657 REAL_VALUE_TYPE r;
3658 int oldcost, newcost;
3659 bool speed = optimize_insn_for_speed_p ();
3661 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3663 if (targetm.legitimate_constant_p (dstmode, y))
3664 oldcost = set_src_cost (y, speed);
3665 else
3666 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3668 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3669 srcmode != orig_srcmode;
3670 srcmode = GET_MODE_WIDER_MODE (srcmode))
3672 enum insn_code ic;
3673 rtx trunc_y;
3674 rtx_insn *last_insn;
3676 /* Skip if the target can't extend this way. */
3677 ic = can_extend_p (dstmode, srcmode, 0);
3678 if (ic == CODE_FOR_nothing)
3679 continue;
3681 /* Skip if the narrowed value isn't exact. */
3682 if (! exact_real_truncate (srcmode, &r))
3683 continue;
3685 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3687 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3689 /* Skip if the target needs extra instructions to perform
3690 the extension. */
3691 if (!insn_operand_matches (ic, 1, trunc_y))
3692 continue;
3693 /* This is valid, but may not be cheaper than the original. */
3694 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3695 speed);
3696 if (oldcost < newcost)
3697 continue;
3699 else if (float_extend_from_mem[dstmode][srcmode])
3701 trunc_y = force_const_mem (srcmode, trunc_y);
3702 /* This is valid, but may not be cheaper than the original. */
3703 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3704 speed);
3705 if (oldcost < newcost)
3706 continue;
3707 trunc_y = validize_mem (trunc_y);
3709 else
3710 continue;
3712 /* For CSE's benefit, force the compressed constant pool entry
3713 into a new pseudo. This constant may be used in different modes,
3714 and if not, combine will put things back together for us. */
3715 trunc_y = force_reg (srcmode, trunc_y);
3717 /* If x is a hard register, perform the extension into a pseudo,
3718 so that e.g. stack realignment code is aware of it. */
3719 rtx target = x;
3720 if (REG_P (x) && HARD_REGISTER_P (x))
3721 target = gen_reg_rtx (dstmode);
3723 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3724 last_insn = get_last_insn ();
3726 if (REG_P (target))
3727 set_unique_reg_note (last_insn, REG_EQUAL, y);
3729 if (target != x)
3730 return emit_move_insn (x, target);
3731 return last_insn;
3734 return NULL;
3737 /* Pushing data onto the stack. */
3739 /* Push a block of length SIZE (perhaps variable)
3740 and return an rtx to address the beginning of the block.
3741 The value may be virtual_outgoing_args_rtx.
3743 EXTRA is the number of bytes of padding to push in addition to SIZE.
3744 BELOW nonzero means this padding comes at low addresses;
3745 otherwise, the padding comes at high addresses. */
3748 push_block (rtx size, int extra, int below)
3750 rtx temp;
3752 size = convert_modes (Pmode, ptr_mode, size, 1);
3753 if (CONSTANT_P (size))
3754 anti_adjust_stack (plus_constant (Pmode, size, extra));
3755 else if (REG_P (size) && extra == 0)
3756 anti_adjust_stack (size);
3757 else
3759 temp = copy_to_mode_reg (Pmode, size);
3760 if (extra != 0)
3761 temp = expand_binop (Pmode, add_optab, temp,
3762 gen_int_mode (extra, Pmode),
3763 temp, 0, OPTAB_LIB_WIDEN);
3764 anti_adjust_stack (temp);
3767 if (STACK_GROWS_DOWNWARD)
3769 temp = virtual_outgoing_args_rtx;
3770 if (extra != 0 && below)
3771 temp = plus_constant (Pmode, temp, extra);
3773 else
3775 if (CONST_INT_P (size))
3776 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3777 -INTVAL (size) - (below ? 0 : extra));
3778 else if (extra != 0 && !below)
3779 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3780 negate_rtx (Pmode, plus_constant (Pmode, size,
3781 extra)));
3782 else
3783 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3784 negate_rtx (Pmode, size));
3787 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3790 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3792 static rtx
3793 mem_autoinc_base (rtx mem)
3795 if (MEM_P (mem))
3797 rtx addr = XEXP (mem, 0);
3798 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3799 return XEXP (addr, 0);
3801 return NULL;
3804 /* A utility routine used here, in reload, and in try_split. The insns
3805 after PREV up to and including LAST are known to adjust the stack,
3806 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3807 placing notes as appropriate. PREV may be NULL, indicating the
3808 entire insn sequence prior to LAST should be scanned.
3810 The set of allowed stack pointer modifications is small:
3811 (1) One or more auto-inc style memory references (aka pushes),
3812 (2) One or more addition/subtraction with the SP as destination,
3813 (3) A single move insn with the SP as destination,
3814 (4) A call_pop insn,
3815 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3817 Insns in the sequence that do not modify the SP are ignored,
3818 except for noreturn calls.
3820 The return value is the amount of adjustment that can be trivially
3821 verified, via immediate operand or auto-inc. If the adjustment
3822 cannot be trivially extracted, the return value is INT_MIN. */
3824 HOST_WIDE_INT
3825 find_args_size_adjust (rtx_insn *insn)
3827 rtx dest, set, pat;
3828 int i;
3830 pat = PATTERN (insn);
3831 set = NULL;
3833 /* Look for a call_pop pattern. */
3834 if (CALL_P (insn))
3836 /* We have to allow non-call_pop patterns for the case
3837 of emit_single_push_insn of a TLS address. */
3838 if (GET_CODE (pat) != PARALLEL)
3839 return 0;
3841 /* All call_pop have a stack pointer adjust in the parallel.
3842 The call itself is always first, and the stack adjust is
3843 usually last, so search from the end. */
3844 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3846 set = XVECEXP (pat, 0, i);
3847 if (GET_CODE (set) != SET)
3848 continue;
3849 dest = SET_DEST (set);
3850 if (dest == stack_pointer_rtx)
3851 break;
3853 /* We'd better have found the stack pointer adjust. */
3854 if (i == 0)
3855 return 0;
3856 /* Fall through to process the extracted SET and DEST
3857 as if it was a standalone insn. */
3859 else if (GET_CODE (pat) == SET)
3860 set = pat;
3861 else if ((set = single_set (insn)) != NULL)
3863 else if (GET_CODE (pat) == PARALLEL)
3865 /* ??? Some older ports use a parallel with a stack adjust
3866 and a store for a PUSH_ROUNDING pattern, rather than a
3867 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3868 /* ??? See h8300 and m68k, pushqi1. */
3869 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3871 set = XVECEXP (pat, 0, i);
3872 if (GET_CODE (set) != SET)
3873 continue;
3874 dest = SET_DEST (set);
3875 if (dest == stack_pointer_rtx)
3876 break;
3878 /* We do not expect an auto-inc of the sp in the parallel. */
3879 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3880 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3881 != stack_pointer_rtx);
3883 if (i < 0)
3884 return 0;
3886 else
3887 return 0;
3889 dest = SET_DEST (set);
3891 /* Look for direct modifications of the stack pointer. */
3892 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3894 /* Look for a trivial adjustment, otherwise assume nothing. */
3895 /* Note that the SPU restore_stack_block pattern refers to
3896 the stack pointer in V4SImode. Consider that non-trivial. */
3897 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3898 && GET_CODE (SET_SRC (set)) == PLUS
3899 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3900 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3901 return INTVAL (XEXP (SET_SRC (set), 1));
3902 /* ??? Reload can generate no-op moves, which will be cleaned
3903 up later. Recognize it and continue searching. */
3904 else if (rtx_equal_p (dest, SET_SRC (set)))
3905 return 0;
3906 else
3907 return HOST_WIDE_INT_MIN;
3909 else
3911 rtx mem, addr;
3913 /* Otherwise only think about autoinc patterns. */
3914 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3916 mem = dest;
3917 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3918 != stack_pointer_rtx);
3920 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3921 mem = SET_SRC (set);
3922 else
3923 return 0;
3925 addr = XEXP (mem, 0);
3926 switch (GET_CODE (addr))
3928 case PRE_INC:
3929 case POST_INC:
3930 return GET_MODE_SIZE (GET_MODE (mem));
3931 case PRE_DEC:
3932 case POST_DEC:
3933 return -GET_MODE_SIZE (GET_MODE (mem));
3934 case PRE_MODIFY:
3935 case POST_MODIFY:
3936 addr = XEXP (addr, 1);
3937 gcc_assert (GET_CODE (addr) == PLUS);
3938 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3939 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3940 return INTVAL (XEXP (addr, 1));
3941 default:
3942 gcc_unreachable ();
3948 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3950 int args_size = end_args_size;
3951 bool saw_unknown = false;
3952 rtx_insn *insn;
3954 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3956 HOST_WIDE_INT this_delta;
3958 if (!NONDEBUG_INSN_P (insn))
3959 continue;
3961 this_delta = find_args_size_adjust (insn);
3962 if (this_delta == 0)
3964 if (!CALL_P (insn)
3965 || ACCUMULATE_OUTGOING_ARGS
3966 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3967 continue;
3970 gcc_assert (!saw_unknown);
3971 if (this_delta == HOST_WIDE_INT_MIN)
3972 saw_unknown = true;
3974 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3975 if (STACK_GROWS_DOWNWARD)
3976 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3978 args_size -= this_delta;
3981 return saw_unknown ? INT_MIN : args_size;
3984 #ifdef PUSH_ROUNDING
3985 /* Emit single push insn. */
3987 static void
3988 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
3990 rtx dest_addr;
3991 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3992 rtx dest;
3993 enum insn_code icode;
3995 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3996 /* If there is push pattern, use it. Otherwise try old way of throwing
3997 MEM representing push operation to move expander. */
3998 icode = optab_handler (push_optab, mode);
3999 if (icode != CODE_FOR_nothing)
4001 struct expand_operand ops[1];
4003 create_input_operand (&ops[0], x, mode);
4004 if (maybe_expand_insn (icode, 1, ops))
4005 return;
4007 if (GET_MODE_SIZE (mode) == rounded_size)
4008 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4009 /* If we are to pad downward, adjust the stack pointer first and
4010 then store X into the stack location using an offset. This is
4011 because emit_move_insn does not know how to pad; it does not have
4012 access to type. */
4013 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4015 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4016 HOST_WIDE_INT offset;
4018 emit_move_insn (stack_pointer_rtx,
4019 expand_binop (Pmode,
4020 STACK_GROWS_DOWNWARD ? sub_optab
4021 : add_optab,
4022 stack_pointer_rtx,
4023 gen_int_mode (rounded_size, Pmode),
4024 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4026 offset = (HOST_WIDE_INT) padding_size;
4027 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4028 /* We have already decremented the stack pointer, so get the
4029 previous value. */
4030 offset += (HOST_WIDE_INT) rounded_size;
4032 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4033 /* We have already incremented the stack pointer, so get the
4034 previous value. */
4035 offset -= (HOST_WIDE_INT) rounded_size;
4037 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4038 gen_int_mode (offset, Pmode));
4040 else
4042 if (STACK_GROWS_DOWNWARD)
4043 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4044 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4045 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4046 Pmode));
4047 else
4048 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4049 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4050 gen_int_mode (rounded_size, Pmode));
4052 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4055 dest = gen_rtx_MEM (mode, dest_addr);
4057 if (type != 0)
4059 set_mem_attributes (dest, type, 1);
4061 if (cfun->tail_call_marked)
4062 /* Function incoming arguments may overlap with sibling call
4063 outgoing arguments and we cannot allow reordering of reads
4064 from function arguments with stores to outgoing arguments
4065 of sibling calls. */
4066 set_mem_alias_set (dest, 0);
4068 emit_move_insn (dest, x);
4071 /* Emit and annotate a single push insn. */
4073 static void
4074 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4076 int delta, old_delta = stack_pointer_delta;
4077 rtx_insn *prev = get_last_insn ();
4078 rtx_insn *last;
4080 emit_single_push_insn_1 (mode, x, type);
4082 last = get_last_insn ();
4084 /* Notice the common case where we emitted exactly one insn. */
4085 if (PREV_INSN (last) == prev)
4087 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4088 return;
4091 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4092 gcc_assert (delta == INT_MIN || delta == old_delta);
4094 #endif
4096 /* If reading SIZE bytes from X will end up reading from
4097 Y return the number of bytes that overlap. Return -1
4098 if there is no overlap or -2 if we can't determine
4099 (for example when X and Y have different base registers). */
4101 static int
4102 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4104 rtx tmp = plus_constant (Pmode, x, size);
4105 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4107 if (!CONST_INT_P (sub))
4108 return -2;
4110 HOST_WIDE_INT val = INTVAL (sub);
4112 return IN_RANGE (val, 1, size) ? val : -1;
4115 /* Generate code to push X onto the stack, assuming it has mode MODE and
4116 type TYPE.
4117 MODE is redundant except when X is a CONST_INT (since they don't
4118 carry mode info).
4119 SIZE is an rtx for the size of data to be copied (in bytes),
4120 needed only if X is BLKmode.
4121 Return true if successful. May return false if asked to push a
4122 partial argument during a sibcall optimization (as specified by
4123 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4124 to not overlap.
4126 ALIGN (in bits) is maximum alignment we can assume.
4128 If PARTIAL and REG are both nonzero, then copy that many of the first
4129 bytes of X into registers starting with REG, and push the rest of X.
4130 The amount of space pushed is decreased by PARTIAL bytes.
4131 REG must be a hard register in this case.
4132 If REG is zero but PARTIAL is not, take any all others actions for an
4133 argument partially in registers, but do not actually load any
4134 registers.
4136 EXTRA is the amount in bytes of extra space to leave next to this arg.
4137 This is ignored if an argument block has already been allocated.
4139 On a machine that lacks real push insns, ARGS_ADDR is the address of
4140 the bottom of the argument block for this call. We use indexing off there
4141 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4142 argument block has not been preallocated.
4144 ARGS_SO_FAR is the size of args previously pushed for this call.
4146 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4147 for arguments passed in registers. If nonzero, it will be the number
4148 of bytes required. */
4150 bool
4151 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4152 unsigned int align, int partial, rtx reg, int extra,
4153 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4154 rtx alignment_pad, bool sibcall_p)
4156 rtx xinner;
4157 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4159 /* Decide where to pad the argument: `downward' for below,
4160 `upward' for above, or `none' for don't pad it.
4161 Default is below for small data on big-endian machines; else above. */
4162 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4164 /* Invert direction if stack is post-decrement.
4165 FIXME: why? */
4166 if (STACK_PUSH_CODE == POST_DEC)
4167 if (where_pad != none)
4168 where_pad = (where_pad == downward ? upward : downward);
4170 xinner = x;
4172 int nregs = partial / UNITS_PER_WORD;
4173 rtx *tmp_regs = NULL;
4174 int overlapping = 0;
4176 if (mode == BLKmode
4177 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4179 /* Copy a block into the stack, entirely or partially. */
4181 rtx temp;
4182 int used;
4183 int offset;
4184 int skip;
4186 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4187 used = partial - offset;
4189 if (mode != BLKmode)
4191 /* A value is to be stored in an insufficiently aligned
4192 stack slot; copy via a suitably aligned slot if
4193 necessary. */
4194 size = GEN_INT (GET_MODE_SIZE (mode));
4195 if (!MEM_P (xinner))
4197 temp = assign_temp (type, 1, 1);
4198 emit_move_insn (temp, xinner);
4199 xinner = temp;
4203 gcc_assert (size);
4205 /* USED is now the # of bytes we need not copy to the stack
4206 because registers will take care of them. */
4208 if (partial != 0)
4209 xinner = adjust_address (xinner, BLKmode, used);
4211 /* If the partial register-part of the arg counts in its stack size,
4212 skip the part of stack space corresponding to the registers.
4213 Otherwise, start copying to the beginning of the stack space,
4214 by setting SKIP to 0. */
4215 skip = (reg_parm_stack_space == 0) ? 0 : used;
4217 #ifdef PUSH_ROUNDING
4218 /* Do it with several push insns if that doesn't take lots of insns
4219 and if there is no difficulty with push insns that skip bytes
4220 on the stack for alignment purposes. */
4221 if (args_addr == 0
4222 && PUSH_ARGS
4223 && CONST_INT_P (size)
4224 && skip == 0
4225 && MEM_ALIGN (xinner) >= align
4226 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4227 /* Here we avoid the case of a structure whose weak alignment
4228 forces many pushes of a small amount of data,
4229 and such small pushes do rounding that causes trouble. */
4230 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4231 || align >= BIGGEST_ALIGNMENT
4232 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4233 == (align / BITS_PER_UNIT)))
4234 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4236 /* Push padding now if padding above and stack grows down,
4237 or if padding below and stack grows up.
4238 But if space already allocated, this has already been done. */
4239 if (extra && args_addr == 0
4240 && where_pad != none && where_pad != stack_direction)
4241 anti_adjust_stack (GEN_INT (extra));
4243 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4245 else
4246 #endif /* PUSH_ROUNDING */
4248 rtx target;
4250 /* Otherwise make space on the stack and copy the data
4251 to the address of that space. */
4253 /* Deduct words put into registers from the size we must copy. */
4254 if (partial != 0)
4256 if (CONST_INT_P (size))
4257 size = GEN_INT (INTVAL (size) - used);
4258 else
4259 size = expand_binop (GET_MODE (size), sub_optab, size,
4260 gen_int_mode (used, GET_MODE (size)),
4261 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4264 /* Get the address of the stack space.
4265 In this case, we do not deal with EXTRA separately.
4266 A single stack adjust will do. */
4267 if (! args_addr)
4269 temp = push_block (size, extra, where_pad == downward);
4270 extra = 0;
4272 else if (CONST_INT_P (args_so_far))
4273 temp = memory_address (BLKmode,
4274 plus_constant (Pmode, args_addr,
4275 skip + INTVAL (args_so_far)));
4276 else
4277 temp = memory_address (BLKmode,
4278 plus_constant (Pmode,
4279 gen_rtx_PLUS (Pmode,
4280 args_addr,
4281 args_so_far),
4282 skip));
4284 if (!ACCUMULATE_OUTGOING_ARGS)
4286 /* If the source is referenced relative to the stack pointer,
4287 copy it to another register to stabilize it. We do not need
4288 to do this if we know that we won't be changing sp. */
4290 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4291 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4292 temp = copy_to_reg (temp);
4295 target = gen_rtx_MEM (BLKmode, temp);
4297 /* We do *not* set_mem_attributes here, because incoming arguments
4298 may overlap with sibling call outgoing arguments and we cannot
4299 allow reordering of reads from function arguments with stores
4300 to outgoing arguments of sibling calls. We do, however, want
4301 to record the alignment of the stack slot. */
4302 /* ALIGN may well be better aligned than TYPE, e.g. due to
4303 PARM_BOUNDARY. Assume the caller isn't lying. */
4304 set_mem_align (target, align);
4306 /* If part should go in registers and pushing to that part would
4307 overwrite some of the values that need to go into regs, load the
4308 overlapping values into temporary pseudos to be moved into the hard
4309 regs at the end after the stack pushing has completed.
4310 We cannot load them directly into the hard regs here because
4311 they can be clobbered by the block move expansions.
4312 See PR 65358. */
4314 if (partial > 0 && reg != 0 && mode == BLKmode
4315 && GET_CODE (reg) != PARALLEL)
4317 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4318 if (overlapping > 0)
4320 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4321 overlapping /= UNITS_PER_WORD;
4323 tmp_regs = XALLOCAVEC (rtx, overlapping);
4325 for (int i = 0; i < overlapping; i++)
4326 tmp_regs[i] = gen_reg_rtx (word_mode);
4328 for (int i = 0; i < overlapping; i++)
4329 emit_move_insn (tmp_regs[i],
4330 operand_subword_force (target, i, mode));
4332 else if (overlapping == -1)
4333 overlapping = 0;
4334 /* Could not determine whether there is overlap.
4335 Fail the sibcall. */
4336 else
4338 overlapping = 0;
4339 if (sibcall_p)
4340 return false;
4343 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4346 else if (partial > 0)
4348 /* Scalar partly in registers. */
4350 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4351 int i;
4352 int not_stack;
4353 /* # bytes of start of argument
4354 that we must make space for but need not store. */
4355 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4356 int args_offset = INTVAL (args_so_far);
4357 int skip;
4359 /* Push padding now if padding above and stack grows down,
4360 or if padding below and stack grows up.
4361 But if space already allocated, this has already been done. */
4362 if (extra && args_addr == 0
4363 && where_pad != none && where_pad != stack_direction)
4364 anti_adjust_stack (GEN_INT (extra));
4366 /* If we make space by pushing it, we might as well push
4367 the real data. Otherwise, we can leave OFFSET nonzero
4368 and leave the space uninitialized. */
4369 if (args_addr == 0)
4370 offset = 0;
4372 /* Now NOT_STACK gets the number of words that we don't need to
4373 allocate on the stack. Convert OFFSET to words too. */
4374 not_stack = (partial - offset) / UNITS_PER_WORD;
4375 offset /= UNITS_PER_WORD;
4377 /* If the partial register-part of the arg counts in its stack size,
4378 skip the part of stack space corresponding to the registers.
4379 Otherwise, start copying to the beginning of the stack space,
4380 by setting SKIP to 0. */
4381 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4383 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4384 x = validize_mem (force_const_mem (mode, x));
4386 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4387 SUBREGs of such registers are not allowed. */
4388 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4389 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4390 x = copy_to_reg (x);
4392 /* Loop over all the words allocated on the stack for this arg. */
4393 /* We can do it by words, because any scalar bigger than a word
4394 has a size a multiple of a word. */
4395 for (i = size - 1; i >= not_stack; i--)
4396 if (i >= not_stack + offset)
4397 if (!emit_push_insn (operand_subword_force (x, i, mode),
4398 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4399 0, args_addr,
4400 GEN_INT (args_offset + ((i - not_stack + skip)
4401 * UNITS_PER_WORD)),
4402 reg_parm_stack_space, alignment_pad, sibcall_p))
4403 return false;
4405 else
4407 rtx addr;
4408 rtx dest;
4410 /* Push padding now if padding above and stack grows down,
4411 or if padding below and stack grows up.
4412 But if space already allocated, this has already been done. */
4413 if (extra && args_addr == 0
4414 && where_pad != none && where_pad != stack_direction)
4415 anti_adjust_stack (GEN_INT (extra));
4417 #ifdef PUSH_ROUNDING
4418 if (args_addr == 0 && PUSH_ARGS)
4419 emit_single_push_insn (mode, x, type);
4420 else
4421 #endif
4423 if (CONST_INT_P (args_so_far))
4424 addr
4425 = memory_address (mode,
4426 plus_constant (Pmode, args_addr,
4427 INTVAL (args_so_far)));
4428 else
4429 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4430 args_so_far));
4431 dest = gen_rtx_MEM (mode, addr);
4433 /* We do *not* set_mem_attributes here, because incoming arguments
4434 may overlap with sibling call outgoing arguments and we cannot
4435 allow reordering of reads from function arguments with stores
4436 to outgoing arguments of sibling calls. We do, however, want
4437 to record the alignment of the stack slot. */
4438 /* ALIGN may well be better aligned than TYPE, e.g. due to
4439 PARM_BOUNDARY. Assume the caller isn't lying. */
4440 set_mem_align (dest, align);
4442 emit_move_insn (dest, x);
4446 /* Move the partial arguments into the registers and any overlapping
4447 values that we moved into the pseudos in tmp_regs. */
4448 if (partial > 0 && reg != 0)
4450 /* Handle calls that pass values in multiple non-contiguous locations.
4451 The Irix 6 ABI has examples of this. */
4452 if (GET_CODE (reg) == PARALLEL)
4453 emit_group_load (reg, x, type, -1);
4454 else
4456 gcc_assert (partial % UNITS_PER_WORD == 0);
4457 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4459 for (int i = 0; i < overlapping; i++)
4460 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4461 + nregs - overlapping + i),
4462 tmp_regs[i]);
4467 if (extra && args_addr == 0 && where_pad == stack_direction)
4468 anti_adjust_stack (GEN_INT (extra));
4470 if (alignment_pad && args_addr == 0)
4471 anti_adjust_stack (alignment_pad);
4473 return true;
4476 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4477 operations. */
4479 static rtx
4480 get_subtarget (rtx x)
4482 return (optimize
4483 || x == 0
4484 /* Only registers can be subtargets. */
4485 || !REG_P (x)
4486 /* Don't use hard regs to avoid extending their life. */
4487 || REGNO (x) < FIRST_PSEUDO_REGISTER
4488 ? 0 : x);
4491 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4492 FIELD is a bitfield. Returns true if the optimization was successful,
4493 and there's nothing else to do. */
4495 static bool
4496 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4497 unsigned HOST_WIDE_INT bitpos,
4498 unsigned HOST_WIDE_INT bitregion_start,
4499 unsigned HOST_WIDE_INT bitregion_end,
4500 machine_mode mode1, rtx str_rtx,
4501 tree to, tree src)
4503 machine_mode str_mode = GET_MODE (str_rtx);
4504 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4505 tree op0, op1;
4506 rtx value, result;
4507 optab binop;
4508 gimple srcstmt;
4509 enum tree_code code;
4511 if (mode1 != VOIDmode
4512 || bitsize >= BITS_PER_WORD
4513 || str_bitsize > BITS_PER_WORD
4514 || TREE_SIDE_EFFECTS (to)
4515 || TREE_THIS_VOLATILE (to))
4516 return false;
4518 STRIP_NOPS (src);
4519 if (TREE_CODE (src) != SSA_NAME)
4520 return false;
4521 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4522 return false;
4524 srcstmt = get_gimple_for_ssa_name (src);
4525 if (!srcstmt
4526 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4527 return false;
4529 code = gimple_assign_rhs_code (srcstmt);
4531 op0 = gimple_assign_rhs1 (srcstmt);
4533 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4534 to find its initialization. Hopefully the initialization will
4535 be from a bitfield load. */
4536 if (TREE_CODE (op0) == SSA_NAME)
4538 gimple op0stmt = get_gimple_for_ssa_name (op0);
4540 /* We want to eventually have OP0 be the same as TO, which
4541 should be a bitfield. */
4542 if (!op0stmt
4543 || !is_gimple_assign (op0stmt)
4544 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4545 return false;
4546 op0 = gimple_assign_rhs1 (op0stmt);
4549 op1 = gimple_assign_rhs2 (srcstmt);
4551 if (!operand_equal_p (to, op0, 0))
4552 return false;
4554 if (MEM_P (str_rtx))
4556 unsigned HOST_WIDE_INT offset1;
4558 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4559 str_mode = word_mode;
4560 str_mode = get_best_mode (bitsize, bitpos,
4561 bitregion_start, bitregion_end,
4562 MEM_ALIGN (str_rtx), str_mode, 0);
4563 if (str_mode == VOIDmode)
4564 return false;
4565 str_bitsize = GET_MODE_BITSIZE (str_mode);
4567 offset1 = bitpos;
4568 bitpos %= str_bitsize;
4569 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4570 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4572 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4573 return false;
4575 /* If the bit field covers the whole REG/MEM, store_field
4576 will likely generate better code. */
4577 if (bitsize >= str_bitsize)
4578 return false;
4580 /* We can't handle fields split across multiple entities. */
4581 if (bitpos + bitsize > str_bitsize)
4582 return false;
4584 if (BYTES_BIG_ENDIAN)
4585 bitpos = str_bitsize - bitpos - bitsize;
4587 switch (code)
4589 case PLUS_EXPR:
4590 case MINUS_EXPR:
4591 /* For now, just optimize the case of the topmost bitfield
4592 where we don't need to do any masking and also
4593 1 bit bitfields where xor can be used.
4594 We might win by one instruction for the other bitfields
4595 too if insv/extv instructions aren't used, so that
4596 can be added later. */
4597 if (bitpos + bitsize != str_bitsize
4598 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4599 break;
4601 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4602 value = convert_modes (str_mode,
4603 TYPE_MODE (TREE_TYPE (op1)), value,
4604 TYPE_UNSIGNED (TREE_TYPE (op1)));
4606 /* We may be accessing data outside the field, which means
4607 we can alias adjacent data. */
4608 if (MEM_P (str_rtx))
4610 str_rtx = shallow_copy_rtx (str_rtx);
4611 set_mem_alias_set (str_rtx, 0);
4612 set_mem_expr (str_rtx, 0);
4615 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4616 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4618 value = expand_and (str_mode, value, const1_rtx, NULL);
4619 binop = xor_optab;
4621 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4622 result = expand_binop (str_mode, binop, str_rtx,
4623 value, str_rtx, 1, OPTAB_WIDEN);
4624 if (result != str_rtx)
4625 emit_move_insn (str_rtx, result);
4626 return true;
4628 case BIT_IOR_EXPR:
4629 case BIT_XOR_EXPR:
4630 if (TREE_CODE (op1) != INTEGER_CST)
4631 break;
4632 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4633 value = convert_modes (str_mode,
4634 TYPE_MODE (TREE_TYPE (op1)), value,
4635 TYPE_UNSIGNED (TREE_TYPE (op1)));
4637 /* We may be accessing data outside the field, which means
4638 we can alias adjacent data. */
4639 if (MEM_P (str_rtx))
4641 str_rtx = shallow_copy_rtx (str_rtx);
4642 set_mem_alias_set (str_rtx, 0);
4643 set_mem_expr (str_rtx, 0);
4646 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4647 if (bitpos + bitsize != str_bitsize)
4649 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4650 str_mode);
4651 value = expand_and (str_mode, value, mask, NULL_RTX);
4653 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4654 result = expand_binop (str_mode, binop, str_rtx,
4655 value, str_rtx, 1, OPTAB_WIDEN);
4656 if (result != str_rtx)
4657 emit_move_insn (str_rtx, result);
4658 return true;
4660 default:
4661 break;
4664 return false;
4667 /* In the C++ memory model, consecutive bit fields in a structure are
4668 considered one memory location.
4670 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4671 returns the bit range of consecutive bits in which this COMPONENT_REF
4672 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4673 and *OFFSET may be adjusted in the process.
4675 If the access does not need to be restricted, 0 is returned in both
4676 *BITSTART and *BITEND. */
4678 static void
4679 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4680 unsigned HOST_WIDE_INT *bitend,
4681 tree exp,
4682 HOST_WIDE_INT *bitpos,
4683 tree *offset)
4685 HOST_WIDE_INT bitoffset;
4686 tree field, repr;
4688 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4690 field = TREE_OPERAND (exp, 1);
4691 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4692 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4693 need to limit the range we can access. */
4694 if (!repr)
4696 *bitstart = *bitend = 0;
4697 return;
4700 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4701 part of a larger bit field, then the representative does not serve any
4702 useful purpose. This can occur in Ada. */
4703 if (handled_component_p (TREE_OPERAND (exp, 0)))
4705 machine_mode rmode;
4706 HOST_WIDE_INT rbitsize, rbitpos;
4707 tree roffset;
4708 int unsignedp;
4709 int volatilep = 0;
4710 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4711 &roffset, &rmode, &unsignedp, &volatilep, false);
4712 if ((rbitpos % BITS_PER_UNIT) != 0)
4714 *bitstart = *bitend = 0;
4715 return;
4719 /* Compute the adjustment to bitpos from the offset of the field
4720 relative to the representative. DECL_FIELD_OFFSET of field and
4721 repr are the same by construction if they are not constants,
4722 see finish_bitfield_layout. */
4723 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4724 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4725 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4726 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4727 else
4728 bitoffset = 0;
4729 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4730 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4732 /* If the adjustment is larger than bitpos, we would have a negative bit
4733 position for the lower bound and this may wreak havoc later. Adjust
4734 offset and bitpos to make the lower bound non-negative in that case. */
4735 if (bitoffset > *bitpos)
4737 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4738 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4740 *bitpos += adjust;
4741 if (*offset == NULL_TREE)
4742 *offset = size_int (-adjust / BITS_PER_UNIT);
4743 else
4744 *offset
4745 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4746 *bitstart = 0;
4748 else
4749 *bitstart = *bitpos - bitoffset;
4751 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4754 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4755 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4756 DECL_RTL was not set yet, return NORTL. */
4758 static inline bool
4759 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4761 if (TREE_CODE (addr) != ADDR_EXPR)
4762 return false;
4764 tree base = TREE_OPERAND (addr, 0);
4766 if (!DECL_P (base)
4767 || TREE_ADDRESSABLE (base)
4768 || DECL_MODE (base) == BLKmode)
4769 return false;
4771 if (!DECL_RTL_SET_P (base))
4772 return nortl;
4774 return (!MEM_P (DECL_RTL (base)));
4777 /* Returns true if the MEM_REF REF refers to an object that does not
4778 reside in memory and has non-BLKmode. */
4780 static inline bool
4781 mem_ref_refers_to_non_mem_p (tree ref)
4783 tree base = TREE_OPERAND (ref, 0);
4784 return addr_expr_of_non_mem_decl_p_1 (base, false);
4787 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4788 is true, try generating a nontemporal store. */
4790 void
4791 expand_assignment (tree to, tree from, bool nontemporal)
4793 rtx to_rtx = 0;
4794 rtx result;
4795 machine_mode mode;
4796 unsigned int align;
4797 enum insn_code icode;
4799 /* Don't crash if the lhs of the assignment was erroneous. */
4800 if (TREE_CODE (to) == ERROR_MARK)
4802 expand_normal (from);
4803 return;
4806 /* Optimize away no-op moves without side-effects. */
4807 if (operand_equal_p (to, from, 0))
4808 return;
4810 /* Handle misaligned stores. */
4811 mode = TYPE_MODE (TREE_TYPE (to));
4812 if ((TREE_CODE (to) == MEM_REF
4813 || TREE_CODE (to) == TARGET_MEM_REF)
4814 && mode != BLKmode
4815 && !mem_ref_refers_to_non_mem_p (to)
4816 && ((align = get_object_alignment (to))
4817 < GET_MODE_ALIGNMENT (mode))
4818 && (((icode = optab_handler (movmisalign_optab, mode))
4819 != CODE_FOR_nothing)
4820 || SLOW_UNALIGNED_ACCESS (mode, align)))
4822 rtx reg, mem;
4824 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4825 reg = force_not_mem (reg);
4826 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4828 if (icode != CODE_FOR_nothing)
4830 struct expand_operand ops[2];
4832 create_fixed_operand (&ops[0], mem);
4833 create_input_operand (&ops[1], reg, mode);
4834 /* The movmisalign<mode> pattern cannot fail, else the assignment
4835 would silently be omitted. */
4836 expand_insn (icode, 2, ops);
4838 else
4839 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4840 return;
4843 /* Assignment of a structure component needs special treatment
4844 if the structure component's rtx is not simply a MEM.
4845 Assignment of an array element at a constant index, and assignment of
4846 an array element in an unaligned packed structure field, has the same
4847 problem. Same for (partially) storing into a non-memory object. */
4848 if (handled_component_p (to)
4849 || (TREE_CODE (to) == MEM_REF
4850 && mem_ref_refers_to_non_mem_p (to))
4851 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4853 machine_mode mode1;
4854 HOST_WIDE_INT bitsize, bitpos;
4855 unsigned HOST_WIDE_INT bitregion_start = 0;
4856 unsigned HOST_WIDE_INT bitregion_end = 0;
4857 tree offset;
4858 int unsignedp;
4859 int volatilep = 0;
4860 tree tem;
4862 push_temp_slots ();
4863 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4864 &unsignedp, &volatilep, true);
4866 /* Make sure bitpos is not negative, it can wreak havoc later. */
4867 if (bitpos < 0)
4869 gcc_assert (offset == NULL_TREE);
4870 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4871 ? 3 : exact_log2 (BITS_PER_UNIT)));
4872 bitpos &= BITS_PER_UNIT - 1;
4875 if (TREE_CODE (to) == COMPONENT_REF
4876 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4877 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4878 /* The C++ memory model naturally applies to byte-aligned fields.
4879 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4880 BITSIZE are not byte-aligned, there is no need to limit the range
4881 we can access. This can occur with packed structures in Ada. */
4882 else if (bitsize > 0
4883 && bitsize % BITS_PER_UNIT == 0
4884 && bitpos % BITS_PER_UNIT == 0)
4886 bitregion_start = bitpos;
4887 bitregion_end = bitpos + bitsize - 1;
4890 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4892 /* If the field has a mode, we want to access it in the
4893 field's mode, not the computed mode.
4894 If a MEM has VOIDmode (external with incomplete type),
4895 use BLKmode for it instead. */
4896 if (MEM_P (to_rtx))
4898 if (mode1 != VOIDmode)
4899 to_rtx = adjust_address (to_rtx, mode1, 0);
4900 else if (GET_MODE (to_rtx) == VOIDmode)
4901 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4904 if (offset != 0)
4906 machine_mode address_mode;
4907 rtx offset_rtx;
4909 if (!MEM_P (to_rtx))
4911 /* We can get constant negative offsets into arrays with broken
4912 user code. Translate this to a trap instead of ICEing. */
4913 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4914 expand_builtin_trap ();
4915 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4918 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4919 address_mode = get_address_mode (to_rtx);
4920 if (GET_MODE (offset_rtx) != address_mode)
4922 /* We cannot be sure that the RTL in offset_rtx is valid outside
4923 of a memory address context, so force it into a register
4924 before attempting to convert it to the desired mode. */
4925 offset_rtx = force_operand (offset_rtx, NULL_RTX);
4926 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4929 /* If we have an expression in OFFSET_RTX and a non-zero
4930 byte offset in BITPOS, adding the byte offset before the
4931 OFFSET_RTX results in better intermediate code, which makes
4932 later rtl optimization passes perform better.
4934 We prefer intermediate code like this:
4936 r124:DI=r123:DI+0x18
4937 [r124:DI]=r121:DI
4939 ... instead of ...
4941 r124:DI=r123:DI+0x10
4942 [r124:DI+0x8]=r121:DI
4944 This is only done for aligned data values, as these can
4945 be expected to result in single move instructions. */
4946 if (mode1 != VOIDmode
4947 && bitpos != 0
4948 && bitsize > 0
4949 && (bitpos % bitsize) == 0
4950 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4951 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4953 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4954 bitregion_start = 0;
4955 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4956 bitregion_end -= bitpos;
4957 bitpos = 0;
4960 to_rtx = offset_address (to_rtx, offset_rtx,
4961 highest_pow2_factor_for_target (to,
4962 offset));
4965 /* No action is needed if the target is not a memory and the field
4966 lies completely outside that target. This can occur if the source
4967 code contains an out-of-bounds access to a small array. */
4968 if (!MEM_P (to_rtx)
4969 && GET_MODE (to_rtx) != BLKmode
4970 && (unsigned HOST_WIDE_INT) bitpos
4971 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4973 expand_normal (from);
4974 result = NULL;
4976 /* Handle expand_expr of a complex value returning a CONCAT. */
4977 else if (GET_CODE (to_rtx) == CONCAT)
4979 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4980 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4981 && bitpos == 0
4982 && bitsize == mode_bitsize)
4983 result = store_expr (from, to_rtx, false, nontemporal);
4984 else if (bitsize == mode_bitsize / 2
4985 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4986 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4987 nontemporal);
4988 else if (bitpos + bitsize <= mode_bitsize / 2)
4989 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4990 bitregion_start, bitregion_end,
4991 mode1, from,
4992 get_alias_set (to), nontemporal);
4993 else if (bitpos >= mode_bitsize / 2)
4994 result = store_field (XEXP (to_rtx, 1), bitsize,
4995 bitpos - mode_bitsize / 2,
4996 bitregion_start, bitregion_end,
4997 mode1, from,
4998 get_alias_set (to), nontemporal);
4999 else if (bitpos == 0 && bitsize == mode_bitsize)
5001 rtx from_rtx;
5002 result = expand_normal (from);
5003 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5004 TYPE_MODE (TREE_TYPE (from)), 0);
5005 emit_move_insn (XEXP (to_rtx, 0),
5006 read_complex_part (from_rtx, false));
5007 emit_move_insn (XEXP (to_rtx, 1),
5008 read_complex_part (from_rtx, true));
5010 else
5012 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5013 GET_MODE_SIZE (GET_MODE (to_rtx)));
5014 write_complex_part (temp, XEXP (to_rtx, 0), false);
5015 write_complex_part (temp, XEXP (to_rtx, 1), true);
5016 result = store_field (temp, bitsize, bitpos,
5017 bitregion_start, bitregion_end,
5018 mode1, from,
5019 get_alias_set (to), nontemporal);
5020 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5021 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5024 else
5026 if (MEM_P (to_rtx))
5028 /* If the field is at offset zero, we could have been given the
5029 DECL_RTX of the parent struct. Don't munge it. */
5030 to_rtx = shallow_copy_rtx (to_rtx);
5031 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5032 if (volatilep)
5033 MEM_VOLATILE_P (to_rtx) = 1;
5036 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5037 bitregion_start, bitregion_end,
5038 mode1,
5039 to_rtx, to, from))
5040 result = NULL;
5041 else
5042 result = store_field (to_rtx, bitsize, bitpos,
5043 bitregion_start, bitregion_end,
5044 mode1, from,
5045 get_alias_set (to), nontemporal);
5048 if (result)
5049 preserve_temp_slots (result);
5050 pop_temp_slots ();
5051 return;
5054 /* If the rhs is a function call and its value is not an aggregate,
5055 call the function before we start to compute the lhs.
5056 This is needed for correct code for cases such as
5057 val = setjmp (buf) on machines where reference to val
5058 requires loading up part of an address in a separate insn.
5060 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5061 since it might be a promoted variable where the zero- or sign- extension
5062 needs to be done. Handling this in the normal way is safe because no
5063 computation is done before the call. The same is true for SSA names. */
5064 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5065 && COMPLETE_TYPE_P (TREE_TYPE (from))
5066 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5067 && ! (((TREE_CODE (to) == VAR_DECL
5068 || TREE_CODE (to) == PARM_DECL
5069 || TREE_CODE (to) == RESULT_DECL)
5070 && REG_P (DECL_RTL (to)))
5071 || TREE_CODE (to) == SSA_NAME))
5073 rtx value;
5074 rtx bounds;
5076 push_temp_slots ();
5077 value = expand_normal (from);
5079 /* Split value and bounds to store them separately. */
5080 chkp_split_slot (value, &value, &bounds);
5082 if (to_rtx == 0)
5083 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5085 /* Handle calls that return values in multiple non-contiguous locations.
5086 The Irix 6 ABI has examples of this. */
5087 if (GET_CODE (to_rtx) == PARALLEL)
5089 if (GET_CODE (value) == PARALLEL)
5090 emit_group_move (to_rtx, value);
5091 else
5092 emit_group_load (to_rtx, value, TREE_TYPE (from),
5093 int_size_in_bytes (TREE_TYPE (from)));
5095 else if (GET_CODE (value) == PARALLEL)
5096 emit_group_store (to_rtx, value, TREE_TYPE (from),
5097 int_size_in_bytes (TREE_TYPE (from)));
5098 else if (GET_MODE (to_rtx) == BLKmode)
5100 /* Handle calls that return BLKmode values in registers. */
5101 if (REG_P (value))
5102 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5103 else
5104 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5106 else
5108 if (POINTER_TYPE_P (TREE_TYPE (to)))
5109 value = convert_memory_address_addr_space
5110 (GET_MODE (to_rtx), value,
5111 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5113 emit_move_insn (to_rtx, value);
5116 /* Store bounds if required. */
5117 if (bounds
5118 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5120 gcc_assert (MEM_P (to_rtx));
5121 chkp_emit_bounds_store (bounds, value, to_rtx);
5124 preserve_temp_slots (to_rtx);
5125 pop_temp_slots ();
5126 return;
5129 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5130 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5132 /* Don't move directly into a return register. */
5133 if (TREE_CODE (to) == RESULT_DECL
5134 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5136 rtx temp;
5138 push_temp_slots ();
5140 /* If the source is itself a return value, it still is in a pseudo at
5141 this point so we can move it back to the return register directly. */
5142 if (REG_P (to_rtx)
5143 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5144 && TREE_CODE (from) != CALL_EXPR)
5145 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5146 else
5147 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5149 /* Handle calls that return values in multiple non-contiguous locations.
5150 The Irix 6 ABI has examples of this. */
5151 if (GET_CODE (to_rtx) == PARALLEL)
5153 if (GET_CODE (temp) == PARALLEL)
5154 emit_group_move (to_rtx, temp);
5155 else
5156 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5157 int_size_in_bytes (TREE_TYPE (from)));
5159 else if (temp)
5160 emit_move_insn (to_rtx, temp);
5162 preserve_temp_slots (to_rtx);
5163 pop_temp_slots ();
5164 return;
5167 /* In case we are returning the contents of an object which overlaps
5168 the place the value is being stored, use a safe function when copying
5169 a value through a pointer into a structure value return block. */
5170 if (TREE_CODE (to) == RESULT_DECL
5171 && TREE_CODE (from) == INDIRECT_REF
5172 && ADDR_SPACE_GENERIC_P
5173 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5174 && refs_may_alias_p (to, from)
5175 && cfun->returns_struct
5176 && !cfun->returns_pcc_struct)
5178 rtx from_rtx, size;
5180 push_temp_slots ();
5181 size = expr_size (from);
5182 from_rtx = expand_normal (from);
5184 emit_library_call (memmove_libfunc, LCT_NORMAL,
5185 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5186 XEXP (from_rtx, 0), Pmode,
5187 convert_to_mode (TYPE_MODE (sizetype),
5188 size, TYPE_UNSIGNED (sizetype)),
5189 TYPE_MODE (sizetype));
5191 preserve_temp_slots (to_rtx);
5192 pop_temp_slots ();
5193 return;
5196 /* Compute FROM and store the value in the rtx we got. */
5198 push_temp_slots ();
5199 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5200 preserve_temp_slots (result);
5201 pop_temp_slots ();
5202 return;
5205 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5206 succeeded, false otherwise. */
5208 bool
5209 emit_storent_insn (rtx to, rtx from)
5211 struct expand_operand ops[2];
5212 machine_mode mode = GET_MODE (to);
5213 enum insn_code code = optab_handler (storent_optab, mode);
5215 if (code == CODE_FOR_nothing)
5216 return false;
5218 create_fixed_operand (&ops[0], to);
5219 create_input_operand (&ops[1], from, mode);
5220 return maybe_expand_insn (code, 2, ops);
5223 /* Generate code for computing expression EXP,
5224 and storing the value into TARGET.
5226 If the mode is BLKmode then we may return TARGET itself.
5227 It turns out that in BLKmode it doesn't cause a problem.
5228 because C has no operators that could combine two different
5229 assignments into the same BLKmode object with different values
5230 with no sequence point. Will other languages need this to
5231 be more thorough?
5233 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5234 stack, and block moves may need to be treated specially.
5236 If NONTEMPORAL is true, try using a nontemporal store instruction.
5238 If BTARGET is not NULL then computed bounds of EXP are
5239 associated with BTARGET. */
5242 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5243 bool nontemporal, tree btarget)
5245 rtx temp;
5246 rtx alt_rtl = NULL_RTX;
5247 location_t loc = curr_insn_location ();
5249 if (VOID_TYPE_P (TREE_TYPE (exp)))
5251 /* C++ can generate ?: expressions with a throw expression in one
5252 branch and an rvalue in the other. Here, we resolve attempts to
5253 store the throw expression's nonexistent result. */
5254 gcc_assert (!call_param_p);
5255 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5256 return NULL_RTX;
5258 if (TREE_CODE (exp) == COMPOUND_EXPR)
5260 /* Perform first part of compound expression, then assign from second
5261 part. */
5262 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5263 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5264 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5265 call_param_p, nontemporal, btarget);
5267 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5269 /* For conditional expression, get safe form of the target. Then
5270 test the condition, doing the appropriate assignment on either
5271 side. This avoids the creation of unnecessary temporaries.
5272 For non-BLKmode, it is more efficient not to do this. */
5274 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5276 do_pending_stack_adjust ();
5277 NO_DEFER_POP;
5278 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5279 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5280 nontemporal, btarget);
5281 emit_jump_insn (gen_jump (lab2));
5282 emit_barrier ();
5283 emit_label (lab1);
5284 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5285 nontemporal, btarget);
5286 emit_label (lab2);
5287 OK_DEFER_POP;
5289 return NULL_RTX;
5291 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5292 /* If this is a scalar in a register that is stored in a wider mode
5293 than the declared mode, compute the result into its declared mode
5294 and then convert to the wider mode. Our value is the computed
5295 expression. */
5297 rtx inner_target = 0;
5299 /* We can do the conversion inside EXP, which will often result
5300 in some optimizations. Do the conversion in two steps: first
5301 change the signedness, if needed, then the extend. But don't
5302 do this if the type of EXP is a subtype of something else
5303 since then the conversion might involve more than just
5304 converting modes. */
5305 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5306 && TREE_TYPE (TREE_TYPE (exp)) == 0
5307 && GET_MODE_PRECISION (GET_MODE (target))
5308 == TYPE_PRECISION (TREE_TYPE (exp)))
5310 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5311 TYPE_UNSIGNED (TREE_TYPE (exp))))
5313 /* Some types, e.g. Fortran's logical*4, won't have a signed
5314 version, so use the mode instead. */
5315 tree ntype
5316 = (signed_or_unsigned_type_for
5317 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5318 if (ntype == NULL)
5319 ntype = lang_hooks.types.type_for_mode
5320 (TYPE_MODE (TREE_TYPE (exp)),
5321 SUBREG_PROMOTED_SIGN (target));
5323 exp = fold_convert_loc (loc, ntype, exp);
5326 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5327 (GET_MODE (SUBREG_REG (target)),
5328 SUBREG_PROMOTED_SIGN (target)),
5329 exp);
5331 inner_target = SUBREG_REG (target);
5334 temp = expand_expr (exp, inner_target, VOIDmode,
5335 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5337 /* Handle bounds returned by call. */
5338 if (TREE_CODE (exp) == CALL_EXPR)
5340 rtx bounds;
5341 chkp_split_slot (temp, &temp, &bounds);
5342 if (bounds && btarget)
5344 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5345 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5346 chkp_set_rtl_bounds (btarget, tmp);
5350 /* If TEMP is a VOIDmode constant, use convert_modes to make
5351 sure that we properly convert it. */
5352 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5354 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5355 temp, SUBREG_PROMOTED_SIGN (target));
5356 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5357 GET_MODE (target), temp,
5358 SUBREG_PROMOTED_SIGN (target));
5361 convert_move (SUBREG_REG (target), temp,
5362 SUBREG_PROMOTED_SIGN (target));
5364 return NULL_RTX;
5366 else if ((TREE_CODE (exp) == STRING_CST
5367 || (TREE_CODE (exp) == MEM_REF
5368 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5369 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5370 == STRING_CST
5371 && integer_zerop (TREE_OPERAND (exp, 1))))
5372 && !nontemporal && !call_param_p
5373 && MEM_P (target))
5375 /* Optimize initialization of an array with a STRING_CST. */
5376 HOST_WIDE_INT exp_len, str_copy_len;
5377 rtx dest_mem;
5378 tree str = TREE_CODE (exp) == STRING_CST
5379 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5381 exp_len = int_expr_size (exp);
5382 if (exp_len <= 0)
5383 goto normal_expr;
5385 if (TREE_STRING_LENGTH (str) <= 0)
5386 goto normal_expr;
5388 str_copy_len = strlen (TREE_STRING_POINTER (str));
5389 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5390 goto normal_expr;
5392 str_copy_len = TREE_STRING_LENGTH (str);
5393 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5394 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5396 str_copy_len += STORE_MAX_PIECES - 1;
5397 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5399 str_copy_len = MIN (str_copy_len, exp_len);
5400 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5401 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5402 MEM_ALIGN (target), false))
5403 goto normal_expr;
5405 dest_mem = target;
5407 dest_mem = store_by_pieces (dest_mem,
5408 str_copy_len, builtin_strncpy_read_str,
5409 CONST_CAST (char *,
5410 TREE_STRING_POINTER (str)),
5411 MEM_ALIGN (target), false,
5412 exp_len > str_copy_len ? 1 : 0);
5413 if (exp_len > str_copy_len)
5414 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5415 GEN_INT (exp_len - str_copy_len),
5416 BLOCK_OP_NORMAL);
5417 return NULL_RTX;
5419 else
5421 rtx tmp_target;
5423 normal_expr:
5424 /* If we want to use a nontemporal store, force the value to
5425 register first. */
5426 tmp_target = nontemporal ? NULL_RTX : target;
5427 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5428 (call_param_p
5429 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5430 &alt_rtl, false);
5432 /* Handle bounds returned by call. */
5433 if (TREE_CODE (exp) == CALL_EXPR)
5435 rtx bounds;
5436 chkp_split_slot (temp, &temp, &bounds);
5437 if (bounds && btarget)
5439 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5440 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5441 chkp_set_rtl_bounds (btarget, tmp);
5446 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5447 the same as that of TARGET, adjust the constant. This is needed, for
5448 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5449 only a word-sized value. */
5450 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5451 && TREE_CODE (exp) != ERROR_MARK
5452 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5453 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5454 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5456 /* If value was not generated in the target, store it there.
5457 Convert the value to TARGET's type first if necessary and emit the
5458 pending incrementations that have been queued when expanding EXP.
5459 Note that we cannot emit the whole queue blindly because this will
5460 effectively disable the POST_INC optimization later.
5462 If TEMP and TARGET compare equal according to rtx_equal_p, but
5463 one or both of them are volatile memory refs, we have to distinguish
5464 two cases:
5465 - expand_expr has used TARGET. In this case, we must not generate
5466 another copy. This can be detected by TARGET being equal according
5467 to == .
5468 - expand_expr has not used TARGET - that means that the source just
5469 happens to have the same RTX form. Since temp will have been created
5470 by expand_expr, it will compare unequal according to == .
5471 We must generate a copy in this case, to reach the correct number
5472 of volatile memory references. */
5474 if ((! rtx_equal_p (temp, target)
5475 || (temp != target && (side_effects_p (temp)
5476 || side_effects_p (target))))
5477 && TREE_CODE (exp) != ERROR_MARK
5478 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5479 but TARGET is not valid memory reference, TEMP will differ
5480 from TARGET although it is really the same location. */
5481 && !(alt_rtl
5482 && rtx_equal_p (alt_rtl, target)
5483 && !side_effects_p (alt_rtl)
5484 && !side_effects_p (target))
5485 /* If there's nothing to copy, don't bother. Don't call
5486 expr_size unless necessary, because some front-ends (C++)
5487 expr_size-hook must not be given objects that are not
5488 supposed to be bit-copied or bit-initialized. */
5489 && expr_size (exp) != const0_rtx)
5491 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5493 if (GET_MODE (target) == BLKmode)
5495 /* Handle calls that return BLKmode values in registers. */
5496 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5497 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5498 else
5499 store_bit_field (target,
5500 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5501 0, 0, 0, GET_MODE (temp), temp);
5503 else
5504 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5507 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5509 /* Handle copying a string constant into an array. The string
5510 constant may be shorter than the array. So copy just the string's
5511 actual length, and clear the rest. First get the size of the data
5512 type of the string, which is actually the size of the target. */
5513 rtx size = expr_size (exp);
5515 if (CONST_INT_P (size)
5516 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5517 emit_block_move (target, temp, size,
5518 (call_param_p
5519 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5520 else
5522 machine_mode pointer_mode
5523 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5524 machine_mode address_mode = get_address_mode (target);
5526 /* Compute the size of the data to copy from the string. */
5527 tree copy_size
5528 = size_binop_loc (loc, MIN_EXPR,
5529 make_tree (sizetype, size),
5530 size_int (TREE_STRING_LENGTH (exp)));
5531 rtx copy_size_rtx
5532 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5533 (call_param_p
5534 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5535 rtx_code_label *label = 0;
5537 /* Copy that much. */
5538 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5539 TYPE_UNSIGNED (sizetype));
5540 emit_block_move (target, temp, copy_size_rtx,
5541 (call_param_p
5542 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5544 /* Figure out how much is left in TARGET that we have to clear.
5545 Do all calculations in pointer_mode. */
5546 if (CONST_INT_P (copy_size_rtx))
5548 size = plus_constant (address_mode, size,
5549 -INTVAL (copy_size_rtx));
5550 target = adjust_address (target, BLKmode,
5551 INTVAL (copy_size_rtx));
5553 else
5555 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5556 copy_size_rtx, NULL_RTX, 0,
5557 OPTAB_LIB_WIDEN);
5559 if (GET_MODE (copy_size_rtx) != address_mode)
5560 copy_size_rtx = convert_to_mode (address_mode,
5561 copy_size_rtx,
5562 TYPE_UNSIGNED (sizetype));
5564 target = offset_address (target, copy_size_rtx,
5565 highest_pow2_factor (copy_size));
5566 label = gen_label_rtx ();
5567 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5568 GET_MODE (size), 0, label);
5571 if (size != const0_rtx)
5572 clear_storage (target, size, BLOCK_OP_NORMAL);
5574 if (label)
5575 emit_label (label);
5578 /* Handle calls that return values in multiple non-contiguous locations.
5579 The Irix 6 ABI has examples of this. */
5580 else if (GET_CODE (target) == PARALLEL)
5582 if (GET_CODE (temp) == PARALLEL)
5583 emit_group_move (target, temp);
5584 else
5585 emit_group_load (target, temp, TREE_TYPE (exp),
5586 int_size_in_bytes (TREE_TYPE (exp)));
5588 else if (GET_CODE (temp) == PARALLEL)
5589 emit_group_store (target, temp, TREE_TYPE (exp),
5590 int_size_in_bytes (TREE_TYPE (exp)));
5591 else if (GET_MODE (temp) == BLKmode)
5592 emit_block_move (target, temp, expr_size (exp),
5593 (call_param_p
5594 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5595 /* If we emit a nontemporal store, there is nothing else to do. */
5596 else if (nontemporal && emit_storent_insn (target, temp))
5598 else
5600 temp = force_operand (temp, target);
5601 if (temp != target)
5602 emit_move_insn (target, temp);
5606 return NULL_RTX;
5609 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5611 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5613 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5616 /* Return true if field F of structure TYPE is a flexible array. */
5618 static bool
5619 flexible_array_member_p (const_tree f, const_tree type)
5621 const_tree tf;
5623 tf = TREE_TYPE (f);
5624 return (DECL_CHAIN (f) == NULL
5625 && TREE_CODE (tf) == ARRAY_TYPE
5626 && TYPE_DOMAIN (tf)
5627 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5628 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5629 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5630 && int_size_in_bytes (type) >= 0);
5633 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5634 must have in order for it to completely initialize a value of type TYPE.
5635 Return -1 if the number isn't known.
5637 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5639 static HOST_WIDE_INT
5640 count_type_elements (const_tree type, bool for_ctor_p)
5642 switch (TREE_CODE (type))
5644 case ARRAY_TYPE:
5646 tree nelts;
5648 nelts = array_type_nelts (type);
5649 if (nelts && tree_fits_uhwi_p (nelts))
5651 unsigned HOST_WIDE_INT n;
5653 n = tree_to_uhwi (nelts) + 1;
5654 if (n == 0 || for_ctor_p)
5655 return n;
5656 else
5657 return n * count_type_elements (TREE_TYPE (type), false);
5659 return for_ctor_p ? -1 : 1;
5662 case RECORD_TYPE:
5664 unsigned HOST_WIDE_INT n;
5665 tree f;
5667 n = 0;
5668 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5669 if (TREE_CODE (f) == FIELD_DECL)
5671 if (!for_ctor_p)
5672 n += count_type_elements (TREE_TYPE (f), false);
5673 else if (!flexible_array_member_p (f, type))
5674 /* Don't count flexible arrays, which are not supposed
5675 to be initialized. */
5676 n += 1;
5679 return n;
5682 case UNION_TYPE:
5683 case QUAL_UNION_TYPE:
5685 tree f;
5686 HOST_WIDE_INT n, m;
5688 gcc_assert (!for_ctor_p);
5689 /* Estimate the number of scalars in each field and pick the
5690 maximum. Other estimates would do instead; the idea is simply
5691 to make sure that the estimate is not sensitive to the ordering
5692 of the fields. */
5693 n = 1;
5694 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5695 if (TREE_CODE (f) == FIELD_DECL)
5697 m = count_type_elements (TREE_TYPE (f), false);
5698 /* If the field doesn't span the whole union, add an extra
5699 scalar for the rest. */
5700 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5701 TYPE_SIZE (type)) != 1)
5702 m++;
5703 if (n < m)
5704 n = m;
5706 return n;
5709 case COMPLEX_TYPE:
5710 return 2;
5712 case VECTOR_TYPE:
5713 return TYPE_VECTOR_SUBPARTS (type);
5715 case INTEGER_TYPE:
5716 case REAL_TYPE:
5717 case FIXED_POINT_TYPE:
5718 case ENUMERAL_TYPE:
5719 case BOOLEAN_TYPE:
5720 case POINTER_TYPE:
5721 case OFFSET_TYPE:
5722 case REFERENCE_TYPE:
5723 case NULLPTR_TYPE:
5724 return 1;
5726 case ERROR_MARK:
5727 return 0;
5729 case VOID_TYPE:
5730 case METHOD_TYPE:
5731 case FUNCTION_TYPE:
5732 case LANG_TYPE:
5733 default:
5734 gcc_unreachable ();
5738 /* Helper for categorize_ctor_elements. Identical interface. */
5740 static bool
5741 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5742 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5744 unsigned HOST_WIDE_INT idx;
5745 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5746 tree value, purpose, elt_type;
5748 /* Whether CTOR is a valid constant initializer, in accordance with what
5749 initializer_constant_valid_p does. If inferred from the constructor
5750 elements, true until proven otherwise. */
5751 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5752 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5754 nz_elts = 0;
5755 init_elts = 0;
5756 num_fields = 0;
5757 elt_type = NULL_TREE;
5759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5761 HOST_WIDE_INT mult = 1;
5763 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5765 tree lo_index = TREE_OPERAND (purpose, 0);
5766 tree hi_index = TREE_OPERAND (purpose, 1);
5768 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5769 mult = (tree_to_uhwi (hi_index)
5770 - tree_to_uhwi (lo_index) + 1);
5772 num_fields += mult;
5773 elt_type = TREE_TYPE (value);
5775 switch (TREE_CODE (value))
5777 case CONSTRUCTOR:
5779 HOST_WIDE_INT nz = 0, ic = 0;
5781 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5782 p_complete);
5784 nz_elts += mult * nz;
5785 init_elts += mult * ic;
5787 if (const_from_elts_p && const_p)
5788 const_p = const_elt_p;
5790 break;
5792 case INTEGER_CST:
5793 case REAL_CST:
5794 case FIXED_CST:
5795 if (!initializer_zerop (value))
5796 nz_elts += mult;
5797 init_elts += mult;
5798 break;
5800 case STRING_CST:
5801 nz_elts += mult * TREE_STRING_LENGTH (value);
5802 init_elts += mult * TREE_STRING_LENGTH (value);
5803 break;
5805 case COMPLEX_CST:
5806 if (!initializer_zerop (TREE_REALPART (value)))
5807 nz_elts += mult;
5808 if (!initializer_zerop (TREE_IMAGPART (value)))
5809 nz_elts += mult;
5810 init_elts += mult;
5811 break;
5813 case VECTOR_CST:
5815 unsigned i;
5816 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5818 tree v = VECTOR_CST_ELT (value, i);
5819 if (!initializer_zerop (v))
5820 nz_elts += mult;
5821 init_elts += mult;
5824 break;
5826 default:
5828 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5829 nz_elts += mult * tc;
5830 init_elts += mult * tc;
5832 if (const_from_elts_p && const_p)
5833 const_p = initializer_constant_valid_p (value, elt_type)
5834 != NULL_TREE;
5836 break;
5840 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5841 num_fields, elt_type))
5842 *p_complete = false;
5844 *p_nz_elts += nz_elts;
5845 *p_init_elts += init_elts;
5847 return const_p;
5850 /* Examine CTOR to discover:
5851 * how many scalar fields are set to nonzero values,
5852 and place it in *P_NZ_ELTS;
5853 * how many scalar fields in total are in CTOR,
5854 and place it in *P_ELT_COUNT.
5855 * whether the constructor is complete -- in the sense that every
5856 meaningful byte is explicitly given a value --
5857 and place it in *P_COMPLETE.
5859 Return whether or not CTOR is a valid static constant initializer, the same
5860 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5862 bool
5863 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5864 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5866 *p_nz_elts = 0;
5867 *p_init_elts = 0;
5868 *p_complete = true;
5870 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5873 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5874 of which had type LAST_TYPE. Each element was itself a complete
5875 initializer, in the sense that every meaningful byte was explicitly
5876 given a value. Return true if the same is true for the constructor
5877 as a whole. */
5879 bool
5880 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5881 const_tree last_type)
5883 if (TREE_CODE (type) == UNION_TYPE
5884 || TREE_CODE (type) == QUAL_UNION_TYPE)
5886 if (num_elts == 0)
5887 return false;
5889 gcc_assert (num_elts == 1 && last_type);
5891 /* ??? We could look at each element of the union, and find the
5892 largest element. Which would avoid comparing the size of the
5893 initialized element against any tail padding in the union.
5894 Doesn't seem worth the effort... */
5895 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5898 return count_type_elements (type, true) == num_elts;
5901 /* Return 1 if EXP contains mostly (3/4) zeros. */
5903 static int
5904 mostly_zeros_p (const_tree exp)
5906 if (TREE_CODE (exp) == CONSTRUCTOR)
5908 HOST_WIDE_INT nz_elts, init_elts;
5909 bool complete_p;
5911 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5912 return !complete_p || nz_elts < init_elts / 4;
5915 return initializer_zerop (exp);
5918 /* Return 1 if EXP contains all zeros. */
5920 static int
5921 all_zeros_p (const_tree exp)
5923 if (TREE_CODE (exp) == CONSTRUCTOR)
5925 HOST_WIDE_INT nz_elts, init_elts;
5926 bool complete_p;
5928 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5929 return nz_elts == 0;
5932 return initializer_zerop (exp);
5935 /* Helper function for store_constructor.
5936 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5937 CLEARED is as for store_constructor.
5938 ALIAS_SET is the alias set to use for any stores.
5940 This provides a recursive shortcut back to store_constructor when it isn't
5941 necessary to go through store_field. This is so that we can pass through
5942 the cleared field to let store_constructor know that we may not have to
5943 clear a substructure if the outer structure has already been cleared. */
5945 static void
5946 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5947 HOST_WIDE_INT bitpos, machine_mode mode,
5948 tree exp, int cleared, alias_set_type alias_set)
5950 if (TREE_CODE (exp) == CONSTRUCTOR
5951 /* We can only call store_constructor recursively if the size and
5952 bit position are on a byte boundary. */
5953 && bitpos % BITS_PER_UNIT == 0
5954 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5955 /* If we have a nonzero bitpos for a register target, then we just
5956 let store_field do the bitfield handling. This is unlikely to
5957 generate unnecessary clear instructions anyways. */
5958 && (bitpos == 0 || MEM_P (target)))
5960 if (MEM_P (target))
5961 target
5962 = adjust_address (target,
5963 GET_MODE (target) == BLKmode
5964 || 0 != (bitpos
5965 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5966 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5969 /* Update the alias set, if required. */
5970 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5971 && MEM_ALIAS_SET (target) != 0)
5973 target = copy_rtx (target);
5974 set_mem_alias_set (target, alias_set);
5977 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5979 else
5980 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5984 /* Returns the number of FIELD_DECLs in TYPE. */
5986 static int
5987 fields_length (const_tree type)
5989 tree t = TYPE_FIELDS (type);
5990 int count = 0;
5992 for (; t; t = DECL_CHAIN (t))
5993 if (TREE_CODE (t) == FIELD_DECL)
5994 ++count;
5996 return count;
6000 /* Store the value of constructor EXP into the rtx TARGET.
6001 TARGET is either a REG or a MEM; we know it cannot conflict, since
6002 safe_from_p has been called.
6003 CLEARED is true if TARGET is known to have been zero'd.
6004 SIZE is the number of bytes of TARGET we are allowed to modify: this
6005 may not be the same as the size of EXP if we are assigning to a field
6006 which has been packed to exclude padding bits. */
6008 static void
6009 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
6011 tree type = TREE_TYPE (exp);
6012 #ifdef WORD_REGISTER_OPERATIONS
6013 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6014 #endif
6016 switch (TREE_CODE (type))
6018 case RECORD_TYPE:
6019 case UNION_TYPE:
6020 case QUAL_UNION_TYPE:
6022 unsigned HOST_WIDE_INT idx;
6023 tree field, value;
6025 /* If size is zero or the target is already cleared, do nothing. */
6026 if (size == 0 || cleared)
6027 cleared = 1;
6028 /* We either clear the aggregate or indicate the value is dead. */
6029 else if ((TREE_CODE (type) == UNION_TYPE
6030 || TREE_CODE (type) == QUAL_UNION_TYPE)
6031 && ! CONSTRUCTOR_ELTS (exp))
6032 /* If the constructor is empty, clear the union. */
6034 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6035 cleared = 1;
6038 /* If we are building a static constructor into a register,
6039 set the initial value as zero so we can fold the value into
6040 a constant. But if more than one register is involved,
6041 this probably loses. */
6042 else if (REG_P (target) && TREE_STATIC (exp)
6043 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6045 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6046 cleared = 1;
6049 /* If the constructor has fewer fields than the structure or
6050 if we are initializing the structure to mostly zeros, clear
6051 the whole structure first. Don't do this if TARGET is a
6052 register whose mode size isn't equal to SIZE since
6053 clear_storage can't handle this case. */
6054 else if (size > 0
6055 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
6056 != fields_length (type))
6057 || mostly_zeros_p (exp))
6058 && (!REG_P (target)
6059 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6060 == size)))
6062 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6063 cleared = 1;
6066 if (REG_P (target) && !cleared)
6067 emit_clobber (target);
6069 /* Store each element of the constructor into the
6070 corresponding field of TARGET. */
6071 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6073 machine_mode mode;
6074 HOST_WIDE_INT bitsize;
6075 HOST_WIDE_INT bitpos = 0;
6076 tree offset;
6077 rtx to_rtx = target;
6079 /* Just ignore missing fields. We cleared the whole
6080 structure, above, if any fields are missing. */
6081 if (field == 0)
6082 continue;
6084 if (cleared && initializer_zerop (value))
6085 continue;
6087 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6088 bitsize = tree_to_uhwi (DECL_SIZE (field));
6089 else
6090 bitsize = -1;
6092 mode = DECL_MODE (field);
6093 if (DECL_BIT_FIELD (field))
6094 mode = VOIDmode;
6096 offset = DECL_FIELD_OFFSET (field);
6097 if (tree_fits_shwi_p (offset)
6098 && tree_fits_shwi_p (bit_position (field)))
6100 bitpos = int_bit_position (field);
6101 offset = 0;
6103 else
6104 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6106 if (offset)
6108 machine_mode address_mode;
6109 rtx offset_rtx;
6111 offset
6112 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6113 make_tree (TREE_TYPE (exp),
6114 target));
6116 offset_rtx = expand_normal (offset);
6117 gcc_assert (MEM_P (to_rtx));
6119 address_mode = get_address_mode (to_rtx);
6120 if (GET_MODE (offset_rtx) != address_mode)
6121 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6123 to_rtx = offset_address (to_rtx, offset_rtx,
6124 highest_pow2_factor (offset));
6127 #ifdef WORD_REGISTER_OPERATIONS
6128 /* If this initializes a field that is smaller than a
6129 word, at the start of a word, try to widen it to a full
6130 word. This special case allows us to output C++ member
6131 function initializations in a form that the optimizers
6132 can understand. */
6133 if (REG_P (target)
6134 && bitsize < BITS_PER_WORD
6135 && bitpos % BITS_PER_WORD == 0
6136 && GET_MODE_CLASS (mode) == MODE_INT
6137 && TREE_CODE (value) == INTEGER_CST
6138 && exp_size >= 0
6139 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6141 tree type = TREE_TYPE (value);
6143 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6145 type = lang_hooks.types.type_for_mode
6146 (word_mode, TYPE_UNSIGNED (type));
6147 value = fold_convert (type, value);
6150 if (BYTES_BIG_ENDIAN)
6151 value
6152 = fold_build2 (LSHIFT_EXPR, type, value,
6153 build_int_cst (type,
6154 BITS_PER_WORD - bitsize));
6155 bitsize = BITS_PER_WORD;
6156 mode = word_mode;
6158 #endif
6160 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6161 && DECL_NONADDRESSABLE_P (field))
6163 to_rtx = copy_rtx (to_rtx);
6164 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6167 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6168 value, cleared,
6169 get_alias_set (TREE_TYPE (field)));
6171 break;
6173 case ARRAY_TYPE:
6175 tree value, index;
6176 unsigned HOST_WIDE_INT i;
6177 int need_to_clear;
6178 tree domain;
6179 tree elttype = TREE_TYPE (type);
6180 int const_bounds_p;
6181 HOST_WIDE_INT minelt = 0;
6182 HOST_WIDE_INT maxelt = 0;
6184 domain = TYPE_DOMAIN (type);
6185 const_bounds_p = (TYPE_MIN_VALUE (domain)
6186 && TYPE_MAX_VALUE (domain)
6187 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6188 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6190 /* If we have constant bounds for the range of the type, get them. */
6191 if (const_bounds_p)
6193 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6194 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6197 /* If the constructor has fewer elements than the array, clear
6198 the whole array first. Similarly if this is static
6199 constructor of a non-BLKmode object. */
6200 if (cleared)
6201 need_to_clear = 0;
6202 else if (REG_P (target) && TREE_STATIC (exp))
6203 need_to_clear = 1;
6204 else
6206 unsigned HOST_WIDE_INT idx;
6207 tree index, value;
6208 HOST_WIDE_INT count = 0, zero_count = 0;
6209 need_to_clear = ! const_bounds_p;
6211 /* This loop is a more accurate version of the loop in
6212 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6213 is also needed to check for missing elements. */
6214 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6216 HOST_WIDE_INT this_node_count;
6218 if (need_to_clear)
6219 break;
6221 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6223 tree lo_index = TREE_OPERAND (index, 0);
6224 tree hi_index = TREE_OPERAND (index, 1);
6226 if (! tree_fits_uhwi_p (lo_index)
6227 || ! tree_fits_uhwi_p (hi_index))
6229 need_to_clear = 1;
6230 break;
6233 this_node_count = (tree_to_uhwi (hi_index)
6234 - tree_to_uhwi (lo_index) + 1);
6236 else
6237 this_node_count = 1;
6239 count += this_node_count;
6240 if (mostly_zeros_p (value))
6241 zero_count += this_node_count;
6244 /* Clear the entire array first if there are any missing
6245 elements, or if the incidence of zero elements is >=
6246 75%. */
6247 if (! need_to_clear
6248 && (count < maxelt - minelt + 1
6249 || 4 * zero_count >= 3 * count))
6250 need_to_clear = 1;
6253 if (need_to_clear && size > 0)
6255 if (REG_P (target))
6256 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6257 else
6258 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6259 cleared = 1;
6262 if (!cleared && REG_P (target))
6263 /* Inform later passes that the old value is dead. */
6264 emit_clobber (target);
6266 /* Store each element of the constructor into the
6267 corresponding element of TARGET, determined by counting the
6268 elements. */
6269 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6271 machine_mode mode;
6272 HOST_WIDE_INT bitsize;
6273 HOST_WIDE_INT bitpos;
6274 rtx xtarget = target;
6276 if (cleared && initializer_zerop (value))
6277 continue;
6279 mode = TYPE_MODE (elttype);
6280 if (mode == BLKmode)
6281 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6282 ? tree_to_uhwi (TYPE_SIZE (elttype))
6283 : -1);
6284 else
6285 bitsize = GET_MODE_BITSIZE (mode);
6287 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6289 tree lo_index = TREE_OPERAND (index, 0);
6290 tree hi_index = TREE_OPERAND (index, 1);
6291 rtx index_r, pos_rtx;
6292 HOST_WIDE_INT lo, hi, count;
6293 tree position;
6295 /* If the range is constant and "small", unroll the loop. */
6296 if (const_bounds_p
6297 && tree_fits_shwi_p (lo_index)
6298 && tree_fits_shwi_p (hi_index)
6299 && (lo = tree_to_shwi (lo_index),
6300 hi = tree_to_shwi (hi_index),
6301 count = hi - lo + 1,
6302 (!MEM_P (target)
6303 || count <= 2
6304 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6305 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6306 <= 40 * 8)))))
6308 lo -= minelt; hi -= minelt;
6309 for (; lo <= hi; lo++)
6311 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6313 if (MEM_P (target)
6314 && !MEM_KEEP_ALIAS_SET_P (target)
6315 && TREE_CODE (type) == ARRAY_TYPE
6316 && TYPE_NONALIASED_COMPONENT (type))
6318 target = copy_rtx (target);
6319 MEM_KEEP_ALIAS_SET_P (target) = 1;
6322 store_constructor_field
6323 (target, bitsize, bitpos, mode, value, cleared,
6324 get_alias_set (elttype));
6327 else
6329 rtx_code_label *loop_start = gen_label_rtx ();
6330 rtx_code_label *loop_end = gen_label_rtx ();
6331 tree exit_cond;
6333 expand_normal (hi_index);
6335 index = build_decl (EXPR_LOCATION (exp),
6336 VAR_DECL, NULL_TREE, domain);
6337 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6338 SET_DECL_RTL (index, index_r);
6339 store_expr (lo_index, index_r, 0, false);
6341 /* Build the head of the loop. */
6342 do_pending_stack_adjust ();
6343 emit_label (loop_start);
6345 /* Assign value to element index. */
6346 position =
6347 fold_convert (ssizetype,
6348 fold_build2 (MINUS_EXPR,
6349 TREE_TYPE (index),
6350 index,
6351 TYPE_MIN_VALUE (domain)));
6353 position =
6354 size_binop (MULT_EXPR, position,
6355 fold_convert (ssizetype,
6356 TYPE_SIZE_UNIT (elttype)));
6358 pos_rtx = expand_normal (position);
6359 xtarget = offset_address (target, pos_rtx,
6360 highest_pow2_factor (position));
6361 xtarget = adjust_address (xtarget, mode, 0);
6362 if (TREE_CODE (value) == CONSTRUCTOR)
6363 store_constructor (value, xtarget, cleared,
6364 bitsize / BITS_PER_UNIT);
6365 else
6366 store_expr (value, xtarget, 0, false);
6368 /* Generate a conditional jump to exit the loop. */
6369 exit_cond = build2 (LT_EXPR, integer_type_node,
6370 index, hi_index);
6371 jumpif (exit_cond, loop_end, -1);
6373 /* Update the loop counter, and jump to the head of
6374 the loop. */
6375 expand_assignment (index,
6376 build2 (PLUS_EXPR, TREE_TYPE (index),
6377 index, integer_one_node),
6378 false);
6380 emit_jump (loop_start);
6382 /* Build the end of the loop. */
6383 emit_label (loop_end);
6386 else if ((index != 0 && ! tree_fits_shwi_p (index))
6387 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6389 tree position;
6391 if (index == 0)
6392 index = ssize_int (1);
6394 if (minelt)
6395 index = fold_convert (ssizetype,
6396 fold_build2 (MINUS_EXPR,
6397 TREE_TYPE (index),
6398 index,
6399 TYPE_MIN_VALUE (domain)));
6401 position =
6402 size_binop (MULT_EXPR, index,
6403 fold_convert (ssizetype,
6404 TYPE_SIZE_UNIT (elttype)));
6405 xtarget = offset_address (target,
6406 expand_normal (position),
6407 highest_pow2_factor (position));
6408 xtarget = adjust_address (xtarget, mode, 0);
6409 store_expr (value, xtarget, 0, false);
6411 else
6413 if (index != 0)
6414 bitpos = ((tree_to_shwi (index) - minelt)
6415 * tree_to_uhwi (TYPE_SIZE (elttype)));
6416 else
6417 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6419 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6420 && TREE_CODE (type) == ARRAY_TYPE
6421 && TYPE_NONALIASED_COMPONENT (type))
6423 target = copy_rtx (target);
6424 MEM_KEEP_ALIAS_SET_P (target) = 1;
6426 store_constructor_field (target, bitsize, bitpos, mode, value,
6427 cleared, get_alias_set (elttype));
6430 break;
6433 case VECTOR_TYPE:
6435 unsigned HOST_WIDE_INT idx;
6436 constructor_elt *ce;
6437 int i;
6438 int need_to_clear;
6439 int icode = CODE_FOR_nothing;
6440 tree elttype = TREE_TYPE (type);
6441 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6442 machine_mode eltmode = TYPE_MODE (elttype);
6443 HOST_WIDE_INT bitsize;
6444 HOST_WIDE_INT bitpos;
6445 rtvec vector = NULL;
6446 unsigned n_elts;
6447 alias_set_type alias;
6449 gcc_assert (eltmode != BLKmode);
6451 n_elts = TYPE_VECTOR_SUBPARTS (type);
6452 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6454 machine_mode mode = GET_MODE (target);
6456 icode = (int) optab_handler (vec_init_optab, mode);
6457 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6458 if (icode != CODE_FOR_nothing)
6460 tree value;
6462 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6463 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6465 icode = CODE_FOR_nothing;
6466 break;
6469 if (icode != CODE_FOR_nothing)
6471 unsigned int i;
6473 vector = rtvec_alloc (n_elts);
6474 for (i = 0; i < n_elts; i++)
6475 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6479 /* If the constructor has fewer elements than the vector,
6480 clear the whole array first. Similarly if this is static
6481 constructor of a non-BLKmode object. */
6482 if (cleared)
6483 need_to_clear = 0;
6484 else if (REG_P (target) && TREE_STATIC (exp))
6485 need_to_clear = 1;
6486 else
6488 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6489 tree value;
6491 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6493 int n_elts_here = tree_to_uhwi
6494 (int_const_binop (TRUNC_DIV_EXPR,
6495 TYPE_SIZE (TREE_TYPE (value)),
6496 TYPE_SIZE (elttype)));
6498 count += n_elts_here;
6499 if (mostly_zeros_p (value))
6500 zero_count += n_elts_here;
6503 /* Clear the entire vector first if there are any missing elements,
6504 or if the incidence of zero elements is >= 75%. */
6505 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6508 if (need_to_clear && size > 0 && !vector)
6510 if (REG_P (target))
6511 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6512 else
6513 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6514 cleared = 1;
6517 /* Inform later passes that the old value is dead. */
6518 if (!cleared && !vector && REG_P (target))
6519 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6521 if (MEM_P (target))
6522 alias = MEM_ALIAS_SET (target);
6523 else
6524 alias = get_alias_set (elttype);
6526 /* Store each element of the constructor into the corresponding
6527 element of TARGET, determined by counting the elements. */
6528 for (idx = 0, i = 0;
6529 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6530 idx++, i += bitsize / elt_size)
6532 HOST_WIDE_INT eltpos;
6533 tree value = ce->value;
6535 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6536 if (cleared && initializer_zerop (value))
6537 continue;
6539 if (ce->index)
6540 eltpos = tree_to_uhwi (ce->index);
6541 else
6542 eltpos = i;
6544 if (vector)
6546 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6547 elements. */
6548 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6549 RTVEC_ELT (vector, eltpos)
6550 = expand_normal (value);
6552 else
6554 machine_mode value_mode =
6555 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6556 ? TYPE_MODE (TREE_TYPE (value))
6557 : eltmode;
6558 bitpos = eltpos * elt_size;
6559 store_constructor_field (target, bitsize, bitpos, value_mode,
6560 value, cleared, alias);
6564 if (vector)
6565 emit_insn (GEN_FCN (icode)
6566 (target,
6567 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6568 break;
6571 default:
6572 gcc_unreachable ();
6576 /* Store the value of EXP (an expression tree)
6577 into a subfield of TARGET which has mode MODE and occupies
6578 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6579 If MODE is VOIDmode, it means that we are storing into a bit-field.
6581 BITREGION_START is bitpos of the first bitfield in this region.
6582 BITREGION_END is the bitpos of the ending bitfield in this region.
6583 These two fields are 0, if the C++ memory model does not apply,
6584 or we are not interested in keeping track of bitfield regions.
6586 Always return const0_rtx unless we have something particular to
6587 return.
6589 ALIAS_SET is the alias set for the destination. This value will
6590 (in general) be different from that for TARGET, since TARGET is a
6591 reference to the containing structure.
6593 If NONTEMPORAL is true, try generating a nontemporal store. */
6595 static rtx
6596 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6597 unsigned HOST_WIDE_INT bitregion_start,
6598 unsigned HOST_WIDE_INT bitregion_end,
6599 machine_mode mode, tree exp,
6600 alias_set_type alias_set, bool nontemporal)
6602 if (TREE_CODE (exp) == ERROR_MARK)
6603 return const0_rtx;
6605 /* If we have nothing to store, do nothing unless the expression has
6606 side-effects. */
6607 if (bitsize == 0)
6608 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6610 if (GET_CODE (target) == CONCAT)
6612 /* We're storing into a struct containing a single __complex. */
6614 gcc_assert (!bitpos);
6615 return store_expr (exp, target, 0, nontemporal);
6618 /* If the structure is in a register or if the component
6619 is a bit field, we cannot use addressing to access it.
6620 Use bit-field techniques or SUBREG to store in it. */
6622 if (mode == VOIDmode
6623 || (mode != BLKmode && ! direct_store[(int) mode]
6624 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6625 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6626 || REG_P (target)
6627 || GET_CODE (target) == SUBREG
6628 /* If the field isn't aligned enough to store as an ordinary memref,
6629 store it as a bit field. */
6630 || (mode != BLKmode
6631 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6632 || bitpos % GET_MODE_ALIGNMENT (mode))
6633 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6634 || (bitpos % BITS_PER_UNIT != 0)))
6635 || (bitsize >= 0 && mode != BLKmode
6636 && GET_MODE_BITSIZE (mode) > bitsize)
6637 /* If the RHS and field are a constant size and the size of the
6638 RHS isn't the same size as the bitfield, we must use bitfield
6639 operations. */
6640 || (bitsize >= 0
6641 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6642 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6643 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6644 decl we must use bitfield operations. */
6645 || (bitsize >= 0
6646 && TREE_CODE (exp) == MEM_REF
6647 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6648 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6649 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6650 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6652 rtx temp;
6653 gimple nop_def;
6655 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6656 implies a mask operation. If the precision is the same size as
6657 the field we're storing into, that mask is redundant. This is
6658 particularly common with bit field assignments generated by the
6659 C front end. */
6660 nop_def = get_def_for_expr (exp, NOP_EXPR);
6661 if (nop_def)
6663 tree type = TREE_TYPE (exp);
6664 if (INTEGRAL_TYPE_P (type)
6665 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6666 && bitsize == TYPE_PRECISION (type))
6668 tree op = gimple_assign_rhs1 (nop_def);
6669 type = TREE_TYPE (op);
6670 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6671 exp = op;
6675 temp = expand_normal (exp);
6677 /* If BITSIZE is narrower than the size of the type of EXP
6678 we will be narrowing TEMP. Normally, what's wanted are the
6679 low-order bits. However, if EXP's type is a record and this is
6680 big-endian machine, we want the upper BITSIZE bits. */
6681 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6682 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6683 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6684 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6685 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6686 NULL_RTX, 1);
6688 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6689 if (mode != VOIDmode && mode != BLKmode
6690 && mode != TYPE_MODE (TREE_TYPE (exp)))
6691 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6693 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6694 are both BLKmode, both must be in memory and BITPOS must be aligned
6695 on a byte boundary. If so, we simply do a block copy. Likewise for
6696 a BLKmode-like TARGET. */
6697 if (GET_CODE (temp) != PARALLEL
6698 && GET_MODE (temp) == BLKmode
6699 && (GET_MODE (target) == BLKmode
6700 || (MEM_P (target)
6701 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6702 && (bitpos % BITS_PER_UNIT) == 0
6703 && (bitsize % BITS_PER_UNIT) == 0)))
6705 gcc_assert (MEM_P (target) && MEM_P (temp)
6706 && (bitpos % BITS_PER_UNIT) == 0);
6708 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6709 emit_block_move (target, temp,
6710 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6711 / BITS_PER_UNIT),
6712 BLOCK_OP_NORMAL);
6714 return const0_rtx;
6717 /* Handle calls that return values in multiple non-contiguous locations.
6718 The Irix 6 ABI has examples of this. */
6719 if (GET_CODE (temp) == PARALLEL)
6721 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6722 rtx temp_target;
6723 if (mode == BLKmode || mode == VOIDmode)
6724 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6725 temp_target = gen_reg_rtx (mode);
6726 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6727 temp = temp_target;
6729 else if (mode == BLKmode)
6731 /* Handle calls that return BLKmode values in registers. */
6732 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6734 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6735 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6736 temp = temp_target;
6738 else
6740 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6741 rtx temp_target;
6742 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6743 temp_target = gen_reg_rtx (mode);
6744 temp_target
6745 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6746 temp_target, mode, mode);
6747 temp = temp_target;
6751 /* Store the value in the bitfield. */
6752 store_bit_field (target, bitsize, bitpos,
6753 bitregion_start, bitregion_end,
6754 mode, temp);
6756 return const0_rtx;
6758 else
6760 /* Now build a reference to just the desired component. */
6761 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6763 if (to_rtx == target)
6764 to_rtx = copy_rtx (to_rtx);
6766 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6767 set_mem_alias_set (to_rtx, alias_set);
6769 return store_expr (exp, to_rtx, 0, nontemporal);
6773 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6774 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6775 codes and find the ultimate containing object, which we return.
6777 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6778 bit position, and *PUNSIGNEDP to the signedness of the field.
6779 If the position of the field is variable, we store a tree
6780 giving the variable offset (in units) in *POFFSET.
6781 This offset is in addition to the bit position.
6782 If the position is not variable, we store 0 in *POFFSET.
6784 If any of the extraction expressions is volatile,
6785 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6787 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6788 Otherwise, it is a mode that can be used to access the field.
6790 If the field describes a variable-sized object, *PMODE is set to
6791 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6792 this case, but the address of the object can be found.
6794 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6795 look through nodes that serve as markers of a greater alignment than
6796 the one that can be deduced from the expression. These nodes make it
6797 possible for front-ends to prevent temporaries from being created by
6798 the middle-end on alignment considerations. For that purpose, the
6799 normal operating mode at high-level is to always pass FALSE so that
6800 the ultimate containing object is really returned; moreover, the
6801 associated predicate handled_component_p will always return TRUE
6802 on these nodes, thus indicating that they are essentially handled
6803 by get_inner_reference. TRUE should only be passed when the caller
6804 is scanning the expression in order to build another representation
6805 and specifically knows how to handle these nodes; as such, this is
6806 the normal operating mode in the RTL expanders. */
6808 tree
6809 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6810 HOST_WIDE_INT *pbitpos, tree *poffset,
6811 machine_mode *pmode, int *punsignedp,
6812 int *pvolatilep, bool keep_aligning)
6814 tree size_tree = 0;
6815 machine_mode mode = VOIDmode;
6816 bool blkmode_bitfield = false;
6817 tree offset = size_zero_node;
6818 offset_int bit_offset = 0;
6820 /* First get the mode, signedness, and size. We do this from just the
6821 outermost expression. */
6822 *pbitsize = -1;
6823 if (TREE_CODE (exp) == COMPONENT_REF)
6825 tree field = TREE_OPERAND (exp, 1);
6826 size_tree = DECL_SIZE (field);
6827 if (flag_strict_volatile_bitfields > 0
6828 && TREE_THIS_VOLATILE (exp)
6829 && DECL_BIT_FIELD_TYPE (field)
6830 && DECL_MODE (field) != BLKmode)
6831 /* Volatile bitfields should be accessed in the mode of the
6832 field's type, not the mode computed based on the bit
6833 size. */
6834 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6835 else if (!DECL_BIT_FIELD (field))
6836 mode = DECL_MODE (field);
6837 else if (DECL_MODE (field) == BLKmode)
6838 blkmode_bitfield = true;
6840 *punsignedp = DECL_UNSIGNED (field);
6842 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6844 size_tree = TREE_OPERAND (exp, 1);
6845 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6846 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6848 /* For vector types, with the correct size of access, use the mode of
6849 inner type. */
6850 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6851 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6852 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6853 mode = TYPE_MODE (TREE_TYPE (exp));
6855 else
6857 mode = TYPE_MODE (TREE_TYPE (exp));
6858 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6860 if (mode == BLKmode)
6861 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6862 else
6863 *pbitsize = GET_MODE_BITSIZE (mode);
6866 if (size_tree != 0)
6868 if (! tree_fits_uhwi_p (size_tree))
6869 mode = BLKmode, *pbitsize = -1;
6870 else
6871 *pbitsize = tree_to_uhwi (size_tree);
6874 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6875 and find the ultimate containing object. */
6876 while (1)
6878 switch (TREE_CODE (exp))
6880 case BIT_FIELD_REF:
6881 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6882 break;
6884 case COMPONENT_REF:
6886 tree field = TREE_OPERAND (exp, 1);
6887 tree this_offset = component_ref_field_offset (exp);
6889 /* If this field hasn't been filled in yet, don't go past it.
6890 This should only happen when folding expressions made during
6891 type construction. */
6892 if (this_offset == 0)
6893 break;
6895 offset = size_binop (PLUS_EXPR, offset, this_offset);
6896 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6898 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6900 break;
6902 case ARRAY_REF:
6903 case ARRAY_RANGE_REF:
6905 tree index = TREE_OPERAND (exp, 1);
6906 tree low_bound = array_ref_low_bound (exp);
6907 tree unit_size = array_ref_element_size (exp);
6909 /* We assume all arrays have sizes that are a multiple of a byte.
6910 First subtract the lower bound, if any, in the type of the
6911 index, then convert to sizetype and multiply by the size of
6912 the array element. */
6913 if (! integer_zerop (low_bound))
6914 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6915 index, low_bound);
6917 offset = size_binop (PLUS_EXPR, offset,
6918 size_binop (MULT_EXPR,
6919 fold_convert (sizetype, index),
6920 unit_size));
6922 break;
6924 case REALPART_EXPR:
6925 break;
6927 case IMAGPART_EXPR:
6928 bit_offset += *pbitsize;
6929 break;
6931 case VIEW_CONVERT_EXPR:
6932 if (keep_aligning && STRICT_ALIGNMENT
6933 && (TYPE_ALIGN (TREE_TYPE (exp))
6934 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6935 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6936 < BIGGEST_ALIGNMENT)
6937 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6938 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6939 goto done;
6940 break;
6942 case MEM_REF:
6943 /* Hand back the decl for MEM[&decl, off]. */
6944 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6946 tree off = TREE_OPERAND (exp, 1);
6947 if (!integer_zerop (off))
6949 offset_int boff, coff = mem_ref_offset (exp);
6950 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6951 bit_offset += boff;
6953 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6955 goto done;
6957 default:
6958 goto done;
6961 /* If any reference in the chain is volatile, the effect is volatile. */
6962 if (TREE_THIS_VOLATILE (exp))
6963 *pvolatilep = 1;
6965 exp = TREE_OPERAND (exp, 0);
6967 done:
6969 /* If OFFSET is constant, see if we can return the whole thing as a
6970 constant bit position. Make sure to handle overflow during
6971 this conversion. */
6972 if (TREE_CODE (offset) == INTEGER_CST)
6974 offset_int tem = wi::sext (wi::to_offset (offset),
6975 TYPE_PRECISION (sizetype));
6976 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6977 tem += bit_offset;
6978 if (wi::fits_shwi_p (tem))
6980 *pbitpos = tem.to_shwi ();
6981 *poffset = offset = NULL_TREE;
6985 /* Otherwise, split it up. */
6986 if (offset)
6988 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6989 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
6991 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6992 offset_int tem = bit_offset.and_not (mask);
6993 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6994 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6995 bit_offset -= tem;
6996 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6997 offset = size_binop (PLUS_EXPR, offset,
6998 wide_int_to_tree (sizetype, tem));
7001 *pbitpos = bit_offset.to_shwi ();
7002 *poffset = offset;
7005 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7006 if (mode == VOIDmode
7007 && blkmode_bitfield
7008 && (*pbitpos % BITS_PER_UNIT) == 0
7009 && (*pbitsize % BITS_PER_UNIT) == 0)
7010 *pmode = BLKmode;
7011 else
7012 *pmode = mode;
7014 return exp;
7017 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7019 static unsigned HOST_WIDE_INT
7020 target_align (const_tree target)
7022 /* We might have a chain of nested references with intermediate misaligning
7023 bitfields components, so need to recurse to find out. */
7025 unsigned HOST_WIDE_INT this_align, outer_align;
7027 switch (TREE_CODE (target))
7029 case BIT_FIELD_REF:
7030 return 1;
7032 case COMPONENT_REF:
7033 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7034 outer_align = target_align (TREE_OPERAND (target, 0));
7035 return MIN (this_align, outer_align);
7037 case ARRAY_REF:
7038 case ARRAY_RANGE_REF:
7039 this_align = TYPE_ALIGN (TREE_TYPE (target));
7040 outer_align = target_align (TREE_OPERAND (target, 0));
7041 return MIN (this_align, outer_align);
7043 CASE_CONVERT:
7044 case NON_LVALUE_EXPR:
7045 case VIEW_CONVERT_EXPR:
7046 this_align = TYPE_ALIGN (TREE_TYPE (target));
7047 outer_align = target_align (TREE_OPERAND (target, 0));
7048 return MAX (this_align, outer_align);
7050 default:
7051 return TYPE_ALIGN (TREE_TYPE (target));
7056 /* Given an rtx VALUE that may contain additions and multiplications, return
7057 an equivalent value that just refers to a register, memory, or constant.
7058 This is done by generating instructions to perform the arithmetic and
7059 returning a pseudo-register containing the value.
7061 The returned value may be a REG, SUBREG, MEM or constant. */
7064 force_operand (rtx value, rtx target)
7066 rtx op1, op2;
7067 /* Use subtarget as the target for operand 0 of a binary operation. */
7068 rtx subtarget = get_subtarget (target);
7069 enum rtx_code code = GET_CODE (value);
7071 /* Check for subreg applied to an expression produced by loop optimizer. */
7072 if (code == SUBREG
7073 && !REG_P (SUBREG_REG (value))
7074 && !MEM_P (SUBREG_REG (value)))
7076 value
7077 = simplify_gen_subreg (GET_MODE (value),
7078 force_reg (GET_MODE (SUBREG_REG (value)),
7079 force_operand (SUBREG_REG (value),
7080 NULL_RTX)),
7081 GET_MODE (SUBREG_REG (value)),
7082 SUBREG_BYTE (value));
7083 code = GET_CODE (value);
7086 /* Check for a PIC address load. */
7087 if ((code == PLUS || code == MINUS)
7088 && XEXP (value, 0) == pic_offset_table_rtx
7089 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7090 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7091 || GET_CODE (XEXP (value, 1)) == CONST))
7093 if (!subtarget)
7094 subtarget = gen_reg_rtx (GET_MODE (value));
7095 emit_move_insn (subtarget, value);
7096 return subtarget;
7099 if (ARITHMETIC_P (value))
7101 op2 = XEXP (value, 1);
7102 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7103 subtarget = 0;
7104 if (code == MINUS && CONST_INT_P (op2))
7106 code = PLUS;
7107 op2 = negate_rtx (GET_MODE (value), op2);
7110 /* Check for an addition with OP2 a constant integer and our first
7111 operand a PLUS of a virtual register and something else. In that
7112 case, we want to emit the sum of the virtual register and the
7113 constant first and then add the other value. This allows virtual
7114 register instantiation to simply modify the constant rather than
7115 creating another one around this addition. */
7116 if (code == PLUS && CONST_INT_P (op2)
7117 && GET_CODE (XEXP (value, 0)) == PLUS
7118 && REG_P (XEXP (XEXP (value, 0), 0))
7119 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7120 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7122 rtx temp = expand_simple_binop (GET_MODE (value), code,
7123 XEXP (XEXP (value, 0), 0), op2,
7124 subtarget, 0, OPTAB_LIB_WIDEN);
7125 return expand_simple_binop (GET_MODE (value), code, temp,
7126 force_operand (XEXP (XEXP (value,
7127 0), 1), 0),
7128 target, 0, OPTAB_LIB_WIDEN);
7131 op1 = force_operand (XEXP (value, 0), subtarget);
7132 op2 = force_operand (op2, NULL_RTX);
7133 switch (code)
7135 case MULT:
7136 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7137 case DIV:
7138 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7139 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7140 target, 1, OPTAB_LIB_WIDEN);
7141 else
7142 return expand_divmod (0,
7143 FLOAT_MODE_P (GET_MODE (value))
7144 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7145 GET_MODE (value), op1, op2, target, 0);
7146 case MOD:
7147 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7148 target, 0);
7149 case UDIV:
7150 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7151 target, 1);
7152 case UMOD:
7153 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7154 target, 1);
7155 case ASHIFTRT:
7156 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7157 target, 0, OPTAB_LIB_WIDEN);
7158 default:
7159 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7160 target, 1, OPTAB_LIB_WIDEN);
7163 if (UNARY_P (value))
7165 if (!target)
7166 target = gen_reg_rtx (GET_MODE (value));
7167 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7168 switch (code)
7170 case ZERO_EXTEND:
7171 case SIGN_EXTEND:
7172 case TRUNCATE:
7173 case FLOAT_EXTEND:
7174 case FLOAT_TRUNCATE:
7175 convert_move (target, op1, code == ZERO_EXTEND);
7176 return target;
7178 case FIX:
7179 case UNSIGNED_FIX:
7180 expand_fix (target, op1, code == UNSIGNED_FIX);
7181 return target;
7183 case FLOAT:
7184 case UNSIGNED_FLOAT:
7185 expand_float (target, op1, code == UNSIGNED_FLOAT);
7186 return target;
7188 default:
7189 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7193 #ifdef INSN_SCHEDULING
7194 /* On machines that have insn scheduling, we want all memory reference to be
7195 explicit, so we need to deal with such paradoxical SUBREGs. */
7196 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7197 value
7198 = simplify_gen_subreg (GET_MODE (value),
7199 force_reg (GET_MODE (SUBREG_REG (value)),
7200 force_operand (SUBREG_REG (value),
7201 NULL_RTX)),
7202 GET_MODE (SUBREG_REG (value)),
7203 SUBREG_BYTE (value));
7204 #endif
7206 return value;
7209 /* Subroutine of expand_expr: return nonzero iff there is no way that
7210 EXP can reference X, which is being modified. TOP_P is nonzero if this
7211 call is going to be used to determine whether we need a temporary
7212 for EXP, as opposed to a recursive call to this function.
7214 It is always safe for this routine to return zero since it merely
7215 searches for optimization opportunities. */
7218 safe_from_p (const_rtx x, tree exp, int top_p)
7220 rtx exp_rtl = 0;
7221 int i, nops;
7223 if (x == 0
7224 /* If EXP has varying size, we MUST use a target since we currently
7225 have no way of allocating temporaries of variable size
7226 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7227 So we assume here that something at a higher level has prevented a
7228 clash. This is somewhat bogus, but the best we can do. Only
7229 do this when X is BLKmode and when we are at the top level. */
7230 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7231 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7232 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7233 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7234 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7235 != INTEGER_CST)
7236 && GET_MODE (x) == BLKmode)
7237 /* If X is in the outgoing argument area, it is always safe. */
7238 || (MEM_P (x)
7239 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7240 || (GET_CODE (XEXP (x, 0)) == PLUS
7241 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7242 return 1;
7244 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7245 find the underlying pseudo. */
7246 if (GET_CODE (x) == SUBREG)
7248 x = SUBREG_REG (x);
7249 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7250 return 0;
7253 /* Now look at our tree code and possibly recurse. */
7254 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7256 case tcc_declaration:
7257 exp_rtl = DECL_RTL_IF_SET (exp);
7258 break;
7260 case tcc_constant:
7261 return 1;
7263 case tcc_exceptional:
7264 if (TREE_CODE (exp) == TREE_LIST)
7266 while (1)
7268 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7269 return 0;
7270 exp = TREE_CHAIN (exp);
7271 if (!exp)
7272 return 1;
7273 if (TREE_CODE (exp) != TREE_LIST)
7274 return safe_from_p (x, exp, 0);
7277 else if (TREE_CODE (exp) == CONSTRUCTOR)
7279 constructor_elt *ce;
7280 unsigned HOST_WIDE_INT idx;
7282 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7283 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7284 || !safe_from_p (x, ce->value, 0))
7285 return 0;
7286 return 1;
7288 else if (TREE_CODE (exp) == ERROR_MARK)
7289 return 1; /* An already-visited SAVE_EXPR? */
7290 else
7291 return 0;
7293 case tcc_statement:
7294 /* The only case we look at here is the DECL_INITIAL inside a
7295 DECL_EXPR. */
7296 return (TREE_CODE (exp) != DECL_EXPR
7297 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7298 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7299 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7301 case tcc_binary:
7302 case tcc_comparison:
7303 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7304 return 0;
7305 /* Fall through. */
7307 case tcc_unary:
7308 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7310 case tcc_expression:
7311 case tcc_reference:
7312 case tcc_vl_exp:
7313 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7314 the expression. If it is set, we conflict iff we are that rtx or
7315 both are in memory. Otherwise, we check all operands of the
7316 expression recursively. */
7318 switch (TREE_CODE (exp))
7320 case ADDR_EXPR:
7321 /* If the operand is static or we are static, we can't conflict.
7322 Likewise if we don't conflict with the operand at all. */
7323 if (staticp (TREE_OPERAND (exp, 0))
7324 || TREE_STATIC (exp)
7325 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7326 return 1;
7328 /* Otherwise, the only way this can conflict is if we are taking
7329 the address of a DECL a that address if part of X, which is
7330 very rare. */
7331 exp = TREE_OPERAND (exp, 0);
7332 if (DECL_P (exp))
7334 if (!DECL_RTL_SET_P (exp)
7335 || !MEM_P (DECL_RTL (exp)))
7336 return 0;
7337 else
7338 exp_rtl = XEXP (DECL_RTL (exp), 0);
7340 break;
7342 case MEM_REF:
7343 if (MEM_P (x)
7344 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7345 get_alias_set (exp)))
7346 return 0;
7347 break;
7349 case CALL_EXPR:
7350 /* Assume that the call will clobber all hard registers and
7351 all of memory. */
7352 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7353 || MEM_P (x))
7354 return 0;
7355 break;
7357 case WITH_CLEANUP_EXPR:
7358 case CLEANUP_POINT_EXPR:
7359 /* Lowered by gimplify.c. */
7360 gcc_unreachable ();
7362 case SAVE_EXPR:
7363 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7365 default:
7366 break;
7369 /* If we have an rtx, we do not need to scan our operands. */
7370 if (exp_rtl)
7371 break;
7373 nops = TREE_OPERAND_LENGTH (exp);
7374 for (i = 0; i < nops; i++)
7375 if (TREE_OPERAND (exp, i) != 0
7376 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7377 return 0;
7379 break;
7381 case tcc_type:
7382 /* Should never get a type here. */
7383 gcc_unreachable ();
7386 /* If we have an rtl, find any enclosed object. Then see if we conflict
7387 with it. */
7388 if (exp_rtl)
7390 if (GET_CODE (exp_rtl) == SUBREG)
7392 exp_rtl = SUBREG_REG (exp_rtl);
7393 if (REG_P (exp_rtl)
7394 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7395 return 0;
7398 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7399 are memory and they conflict. */
7400 return ! (rtx_equal_p (x, exp_rtl)
7401 || (MEM_P (x) && MEM_P (exp_rtl)
7402 && true_dependence (exp_rtl, VOIDmode, x)));
7405 /* If we reach here, it is safe. */
7406 return 1;
7410 /* Return the highest power of two that EXP is known to be a multiple of.
7411 This is used in updating alignment of MEMs in array references. */
7413 unsigned HOST_WIDE_INT
7414 highest_pow2_factor (const_tree exp)
7416 unsigned HOST_WIDE_INT ret;
7417 int trailing_zeros = tree_ctz (exp);
7418 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7419 return BIGGEST_ALIGNMENT;
7420 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7421 if (ret > BIGGEST_ALIGNMENT)
7422 return BIGGEST_ALIGNMENT;
7423 return ret;
7426 /* Similar, except that the alignment requirements of TARGET are
7427 taken into account. Assume it is at least as aligned as its
7428 type, unless it is a COMPONENT_REF in which case the layout of
7429 the structure gives the alignment. */
7431 static unsigned HOST_WIDE_INT
7432 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7434 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7435 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7437 return MAX (factor, talign);
7440 /* Convert the tree comparison code TCODE to the rtl one where the
7441 signedness is UNSIGNEDP. */
7443 static enum rtx_code
7444 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7446 enum rtx_code code;
7447 switch (tcode)
7449 case EQ_EXPR:
7450 code = EQ;
7451 break;
7452 case NE_EXPR:
7453 code = NE;
7454 break;
7455 case LT_EXPR:
7456 code = unsignedp ? LTU : LT;
7457 break;
7458 case LE_EXPR:
7459 code = unsignedp ? LEU : LE;
7460 break;
7461 case GT_EXPR:
7462 code = unsignedp ? GTU : GT;
7463 break;
7464 case GE_EXPR:
7465 code = unsignedp ? GEU : GE;
7466 break;
7467 case UNORDERED_EXPR:
7468 code = UNORDERED;
7469 break;
7470 case ORDERED_EXPR:
7471 code = ORDERED;
7472 break;
7473 case UNLT_EXPR:
7474 code = UNLT;
7475 break;
7476 case UNLE_EXPR:
7477 code = UNLE;
7478 break;
7479 case UNGT_EXPR:
7480 code = UNGT;
7481 break;
7482 case UNGE_EXPR:
7483 code = UNGE;
7484 break;
7485 case UNEQ_EXPR:
7486 code = UNEQ;
7487 break;
7488 case LTGT_EXPR:
7489 code = LTGT;
7490 break;
7492 default:
7493 gcc_unreachable ();
7495 return code;
7498 /* Subroutine of expand_expr. Expand the two operands of a binary
7499 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7500 The value may be stored in TARGET if TARGET is nonzero. The
7501 MODIFIER argument is as documented by expand_expr. */
7503 void
7504 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7505 enum expand_modifier modifier)
7507 if (! safe_from_p (target, exp1, 1))
7508 target = 0;
7509 if (operand_equal_p (exp0, exp1, 0))
7511 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7512 *op1 = copy_rtx (*op0);
7514 else
7516 /* If we need to preserve evaluation order, copy exp0 into its own
7517 temporary variable so that it can't be clobbered by exp1. */
7518 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7519 exp0 = save_expr (exp0);
7520 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7521 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7526 /* Return a MEM that contains constant EXP. DEFER is as for
7527 output_constant_def and MODIFIER is as for expand_expr. */
7529 static rtx
7530 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7532 rtx mem;
7534 mem = output_constant_def (exp, defer);
7535 if (modifier != EXPAND_INITIALIZER)
7536 mem = use_anchored_address (mem);
7537 return mem;
7540 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7541 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7543 static rtx
7544 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7545 enum expand_modifier modifier, addr_space_t as)
7547 rtx result, subtarget;
7548 tree inner, offset;
7549 HOST_WIDE_INT bitsize, bitpos;
7550 int volatilep, unsignedp;
7551 machine_mode mode1;
7553 /* If we are taking the address of a constant and are at the top level,
7554 we have to use output_constant_def since we can't call force_const_mem
7555 at top level. */
7556 /* ??? This should be considered a front-end bug. We should not be
7557 generating ADDR_EXPR of something that isn't an LVALUE. The only
7558 exception here is STRING_CST. */
7559 if (CONSTANT_CLASS_P (exp))
7561 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7562 if (modifier < EXPAND_SUM)
7563 result = force_operand (result, target);
7564 return result;
7567 /* Everything must be something allowed by is_gimple_addressable. */
7568 switch (TREE_CODE (exp))
7570 case INDIRECT_REF:
7571 /* This case will happen via recursion for &a->b. */
7572 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7574 case MEM_REF:
7576 tree tem = TREE_OPERAND (exp, 0);
7577 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7578 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7579 return expand_expr (tem, target, tmode, modifier);
7582 case CONST_DECL:
7583 /* Expand the initializer like constants above. */
7584 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7585 0, modifier), 0);
7586 if (modifier < EXPAND_SUM)
7587 result = force_operand (result, target);
7588 return result;
7590 case REALPART_EXPR:
7591 /* The real part of the complex number is always first, therefore
7592 the address is the same as the address of the parent object. */
7593 offset = 0;
7594 bitpos = 0;
7595 inner = TREE_OPERAND (exp, 0);
7596 break;
7598 case IMAGPART_EXPR:
7599 /* The imaginary part of the complex number is always second.
7600 The expression is therefore always offset by the size of the
7601 scalar type. */
7602 offset = 0;
7603 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7604 inner = TREE_OPERAND (exp, 0);
7605 break;
7607 case COMPOUND_LITERAL_EXPR:
7608 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7609 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7610 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7611 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7612 the initializers aren't gimplified. */
7613 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7614 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7615 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7616 target, tmode, modifier, as);
7617 /* FALLTHRU */
7618 default:
7619 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7620 expand_expr, as that can have various side effects; LABEL_DECLs for
7621 example, may not have their DECL_RTL set yet. Expand the rtl of
7622 CONSTRUCTORs too, which should yield a memory reference for the
7623 constructor's contents. Assume language specific tree nodes can
7624 be expanded in some interesting way. */
7625 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7626 if (DECL_P (exp)
7627 || TREE_CODE (exp) == CONSTRUCTOR
7628 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7630 result = expand_expr (exp, target, tmode,
7631 modifier == EXPAND_INITIALIZER
7632 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7634 /* If the DECL isn't in memory, then the DECL wasn't properly
7635 marked TREE_ADDRESSABLE, which will be either a front-end
7636 or a tree optimizer bug. */
7638 if (TREE_ADDRESSABLE (exp)
7639 && ! MEM_P (result)
7640 && ! targetm.calls.allocate_stack_slots_for_args ())
7642 error ("local frame unavailable (naked function?)");
7643 return result;
7645 else
7646 gcc_assert (MEM_P (result));
7647 result = XEXP (result, 0);
7649 /* ??? Is this needed anymore? */
7650 if (DECL_P (exp))
7651 TREE_USED (exp) = 1;
7653 if (modifier != EXPAND_INITIALIZER
7654 && modifier != EXPAND_CONST_ADDRESS
7655 && modifier != EXPAND_SUM)
7656 result = force_operand (result, target);
7657 return result;
7660 /* Pass FALSE as the last argument to get_inner_reference although
7661 we are expanding to RTL. The rationale is that we know how to
7662 handle "aligning nodes" here: we can just bypass them because
7663 they won't change the final object whose address will be returned
7664 (they actually exist only for that purpose). */
7665 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7666 &mode1, &unsignedp, &volatilep, false);
7667 break;
7670 /* We must have made progress. */
7671 gcc_assert (inner != exp);
7673 subtarget = offset || bitpos ? NULL_RTX : target;
7674 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7675 inner alignment, force the inner to be sufficiently aligned. */
7676 if (CONSTANT_CLASS_P (inner)
7677 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7679 inner = copy_node (inner);
7680 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7681 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7682 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7684 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7686 if (offset)
7688 rtx tmp;
7690 if (modifier != EXPAND_NORMAL)
7691 result = force_operand (result, NULL);
7692 tmp = expand_expr (offset, NULL_RTX, tmode,
7693 modifier == EXPAND_INITIALIZER
7694 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7696 /* expand_expr is allowed to return an object in a mode other
7697 than TMODE. If it did, we need to convert. */
7698 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7699 tmp = convert_modes (tmode, GET_MODE (tmp),
7700 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7701 result = convert_memory_address_addr_space (tmode, result, as);
7702 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7704 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7705 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7706 else
7708 subtarget = bitpos ? NULL_RTX : target;
7709 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7710 1, OPTAB_LIB_WIDEN);
7714 if (bitpos)
7716 /* Someone beforehand should have rejected taking the address
7717 of such an object. */
7718 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7720 result = convert_memory_address_addr_space (tmode, result, as);
7721 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7722 if (modifier < EXPAND_SUM)
7723 result = force_operand (result, target);
7726 return result;
7729 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7730 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7732 static rtx
7733 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7734 enum expand_modifier modifier)
7736 addr_space_t as = ADDR_SPACE_GENERIC;
7737 machine_mode address_mode = Pmode;
7738 machine_mode pointer_mode = ptr_mode;
7739 machine_mode rmode;
7740 rtx result;
7742 /* Target mode of VOIDmode says "whatever's natural". */
7743 if (tmode == VOIDmode)
7744 tmode = TYPE_MODE (TREE_TYPE (exp));
7746 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7748 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7749 address_mode = targetm.addr_space.address_mode (as);
7750 pointer_mode = targetm.addr_space.pointer_mode (as);
7753 /* We can get called with some Weird Things if the user does silliness
7754 like "(short) &a". In that case, convert_memory_address won't do
7755 the right thing, so ignore the given target mode. */
7756 if (tmode != address_mode && tmode != pointer_mode)
7757 tmode = address_mode;
7759 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7760 tmode, modifier, as);
7762 /* Despite expand_expr claims concerning ignoring TMODE when not
7763 strictly convenient, stuff breaks if we don't honor it. Note
7764 that combined with the above, we only do this for pointer modes. */
7765 rmode = GET_MODE (result);
7766 if (rmode == VOIDmode)
7767 rmode = tmode;
7768 if (rmode != tmode)
7769 result = convert_memory_address_addr_space (tmode, result, as);
7771 return result;
7774 /* Generate code for computing CONSTRUCTOR EXP.
7775 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7776 is TRUE, instead of creating a temporary variable in memory
7777 NULL is returned and the caller needs to handle it differently. */
7779 static rtx
7780 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7781 bool avoid_temp_mem)
7783 tree type = TREE_TYPE (exp);
7784 machine_mode mode = TYPE_MODE (type);
7786 /* Try to avoid creating a temporary at all. This is possible
7787 if all of the initializer is zero.
7788 FIXME: try to handle all [0..255] initializers we can handle
7789 with memset. */
7790 if (TREE_STATIC (exp)
7791 && !TREE_ADDRESSABLE (exp)
7792 && target != 0 && mode == BLKmode
7793 && all_zeros_p (exp))
7795 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7796 return target;
7799 /* All elts simple constants => refer to a constant in memory. But
7800 if this is a non-BLKmode mode, let it store a field at a time
7801 since that should make a CONST_INT, CONST_WIDE_INT or
7802 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7803 use, it is best to store directly into the target unless the type
7804 is large enough that memcpy will be used. If we are making an
7805 initializer and all operands are constant, put it in memory as
7806 well.
7808 FIXME: Avoid trying to fill vector constructors piece-meal.
7809 Output them with output_constant_def below unless we're sure
7810 they're zeros. This should go away when vector initializers
7811 are treated like VECTOR_CST instead of arrays. */
7812 if ((TREE_STATIC (exp)
7813 && ((mode == BLKmode
7814 && ! (target != 0 && safe_from_p (target, exp, 1)))
7815 || TREE_ADDRESSABLE (exp)
7816 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7817 && (! can_move_by_pieces
7818 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7819 TYPE_ALIGN (type)))
7820 && ! mostly_zeros_p (exp))))
7821 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7822 && TREE_CONSTANT (exp)))
7824 rtx constructor;
7826 if (avoid_temp_mem)
7827 return NULL_RTX;
7829 constructor = expand_expr_constant (exp, 1, modifier);
7831 if (modifier != EXPAND_CONST_ADDRESS
7832 && modifier != EXPAND_INITIALIZER
7833 && modifier != EXPAND_SUM)
7834 constructor = validize_mem (constructor);
7836 return constructor;
7839 /* Handle calls that pass values in multiple non-contiguous
7840 locations. The Irix 6 ABI has examples of this. */
7841 if (target == 0 || ! safe_from_p (target, exp, 1)
7842 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7844 if (avoid_temp_mem)
7845 return NULL_RTX;
7847 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7850 store_constructor (exp, target, 0, int_expr_size (exp));
7851 return target;
7855 /* expand_expr: generate code for computing expression EXP.
7856 An rtx for the computed value is returned. The value is never null.
7857 In the case of a void EXP, const0_rtx is returned.
7859 The value may be stored in TARGET if TARGET is nonzero.
7860 TARGET is just a suggestion; callers must assume that
7861 the rtx returned may not be the same as TARGET.
7863 If TARGET is CONST0_RTX, it means that the value will be ignored.
7865 If TMODE is not VOIDmode, it suggests generating the
7866 result in mode TMODE. But this is done only when convenient.
7867 Otherwise, TMODE is ignored and the value generated in its natural mode.
7868 TMODE is just a suggestion; callers must assume that
7869 the rtx returned may not have mode TMODE.
7871 Note that TARGET may have neither TMODE nor MODE. In that case, it
7872 probably will not be used.
7874 If MODIFIER is EXPAND_SUM then when EXP is an addition
7875 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7876 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7877 products as above, or REG or MEM, or constant.
7878 Ordinarily in such cases we would output mul or add instructions
7879 and then return a pseudo reg containing the sum.
7881 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7882 it also marks a label as absolutely required (it can't be dead).
7883 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7884 This is used for outputting expressions used in initializers.
7886 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7887 with a constant address even if that address is not normally legitimate.
7888 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7890 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7891 a call parameter. Such targets require special care as we haven't yet
7892 marked TARGET so that it's safe from being trashed by libcalls. We
7893 don't want to use TARGET for anything but the final result;
7894 Intermediate values must go elsewhere. Additionally, calls to
7895 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7897 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7898 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7899 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7900 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7901 recursively.
7903 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7904 In this case, we don't adjust a returned MEM rtx that wouldn't be
7905 sufficiently aligned for its mode; instead, it's up to the caller
7906 to deal with it afterwards. This is used to make sure that unaligned
7907 base objects for which out-of-bounds accesses are supported, for
7908 example record types with trailing arrays, aren't realigned behind
7909 the back of the caller.
7910 The normal operating mode is to pass FALSE for this parameter. */
7913 expand_expr_real (tree exp, rtx target, machine_mode tmode,
7914 enum expand_modifier modifier, rtx *alt_rtl,
7915 bool inner_reference_p)
7917 rtx ret;
7919 /* Handle ERROR_MARK before anybody tries to access its type. */
7920 if (TREE_CODE (exp) == ERROR_MARK
7921 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7923 ret = CONST0_RTX (tmode);
7924 return ret ? ret : const0_rtx;
7927 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
7928 inner_reference_p);
7929 return ret;
7932 /* Try to expand the conditional expression which is represented by
7933 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
7934 return the rtl reg which repsents the result. Otherwise return
7935 NULL_RTL. */
7937 static rtx
7938 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7939 tree treeop1 ATTRIBUTE_UNUSED,
7940 tree treeop2 ATTRIBUTE_UNUSED)
7942 rtx insn;
7943 rtx op00, op01, op1, op2;
7944 enum rtx_code comparison_code;
7945 machine_mode comparison_mode;
7946 gimple srcstmt;
7947 rtx temp;
7948 tree type = TREE_TYPE (treeop1);
7949 int unsignedp = TYPE_UNSIGNED (type);
7950 machine_mode mode = TYPE_MODE (type);
7951 machine_mode orig_mode = mode;
7953 /* If we cannot do a conditional move on the mode, try doing it
7954 with the promoted mode. */
7955 if (!can_conditionally_move_p (mode))
7957 mode = promote_mode (type, mode, &unsignedp);
7958 if (!can_conditionally_move_p (mode))
7959 return NULL_RTX;
7960 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
7962 else
7963 temp = assign_temp (type, 0, 1);
7965 start_sequence ();
7966 expand_operands (treeop1, treeop2,
7967 temp, &op1, &op2, EXPAND_NORMAL);
7969 if (TREE_CODE (treeop0) == SSA_NAME
7970 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7972 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7973 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7974 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7975 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7976 comparison_mode = TYPE_MODE (type);
7977 unsignedp = TYPE_UNSIGNED (type);
7978 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7980 else if (COMPARISON_CLASS_P (treeop0))
7982 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7983 enum tree_code cmpcode = TREE_CODE (treeop0);
7984 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
7985 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
7986 unsignedp = TYPE_UNSIGNED (type);
7987 comparison_mode = TYPE_MODE (type);
7988 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7990 else
7992 op00 = expand_normal (treeop0);
7993 op01 = const0_rtx;
7994 comparison_code = NE;
7995 comparison_mode = GET_MODE (op00);
7996 if (comparison_mode == VOIDmode)
7997 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8000 if (GET_MODE (op1) != mode)
8001 op1 = gen_lowpart (mode, op1);
8003 if (GET_MODE (op2) != mode)
8004 op2 = gen_lowpart (mode, op2);
8006 /* Try to emit the conditional move. */
8007 insn = emit_conditional_move (temp, comparison_code,
8008 op00, op01, comparison_mode,
8009 op1, op2, mode,
8010 unsignedp);
8012 /* If we could do the conditional move, emit the sequence,
8013 and return. */
8014 if (insn)
8016 rtx_insn *seq = get_insns ();
8017 end_sequence ();
8018 emit_insn (seq);
8019 return convert_modes (orig_mode, mode, temp, 0);
8022 /* Otherwise discard the sequence and fall back to code with
8023 branches. */
8024 end_sequence ();
8025 return NULL_RTX;
8029 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8030 enum expand_modifier modifier)
8032 rtx op0, op1, op2, temp;
8033 rtx_code_label *lab;
8034 tree type;
8035 int unsignedp;
8036 machine_mode mode;
8037 enum tree_code code = ops->code;
8038 optab this_optab;
8039 rtx subtarget, original_target;
8040 int ignore;
8041 bool reduce_bit_field;
8042 location_t loc = ops->location;
8043 tree treeop0, treeop1, treeop2;
8044 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8045 ? reduce_to_bit_field_precision ((expr), \
8046 target, \
8047 type) \
8048 : (expr))
8050 type = ops->type;
8051 mode = TYPE_MODE (type);
8052 unsignedp = TYPE_UNSIGNED (type);
8054 treeop0 = ops->op0;
8055 treeop1 = ops->op1;
8056 treeop2 = ops->op2;
8058 /* We should be called only on simple (binary or unary) expressions,
8059 exactly those that are valid in gimple expressions that aren't
8060 GIMPLE_SINGLE_RHS (or invalid). */
8061 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8062 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8063 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8065 ignore = (target == const0_rtx
8066 || ((CONVERT_EXPR_CODE_P (code)
8067 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8068 && TREE_CODE (type) == VOID_TYPE));
8070 /* We should be called only if we need the result. */
8071 gcc_assert (!ignore);
8073 /* An operation in what may be a bit-field type needs the
8074 result to be reduced to the precision of the bit-field type,
8075 which is narrower than that of the type's mode. */
8076 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8077 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8079 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8080 target = 0;
8082 /* Use subtarget as the target for operand 0 of a binary operation. */
8083 subtarget = get_subtarget (target);
8084 original_target = target;
8086 switch (code)
8088 case NON_LVALUE_EXPR:
8089 case PAREN_EXPR:
8090 CASE_CONVERT:
8091 if (treeop0 == error_mark_node)
8092 return const0_rtx;
8094 if (TREE_CODE (type) == UNION_TYPE)
8096 tree valtype = TREE_TYPE (treeop0);
8098 /* If both input and output are BLKmode, this conversion isn't doing
8099 anything except possibly changing memory attribute. */
8100 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8102 rtx result = expand_expr (treeop0, target, tmode,
8103 modifier);
8105 result = copy_rtx (result);
8106 set_mem_attributes (result, type, 0);
8107 return result;
8110 if (target == 0)
8112 if (TYPE_MODE (type) != BLKmode)
8113 target = gen_reg_rtx (TYPE_MODE (type));
8114 else
8115 target = assign_temp (type, 1, 1);
8118 if (MEM_P (target))
8119 /* Store data into beginning of memory target. */
8120 store_expr (treeop0,
8121 adjust_address (target, TYPE_MODE (valtype), 0),
8122 modifier == EXPAND_STACK_PARM,
8123 false);
8125 else
8127 gcc_assert (REG_P (target));
8129 /* Store this field into a union of the proper type. */
8130 store_field (target,
8131 MIN ((int_size_in_bytes (TREE_TYPE
8132 (treeop0))
8133 * BITS_PER_UNIT),
8134 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8135 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8138 /* Return the entire union. */
8139 return target;
8142 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8144 op0 = expand_expr (treeop0, target, VOIDmode,
8145 modifier);
8147 /* If the signedness of the conversion differs and OP0 is
8148 a promoted SUBREG, clear that indication since we now
8149 have to do the proper extension. */
8150 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8151 && GET_CODE (op0) == SUBREG)
8152 SUBREG_PROMOTED_VAR_P (op0) = 0;
8154 return REDUCE_BIT_FIELD (op0);
8157 op0 = expand_expr (treeop0, NULL_RTX, mode,
8158 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8159 if (GET_MODE (op0) == mode)
8162 /* If OP0 is a constant, just convert it into the proper mode. */
8163 else if (CONSTANT_P (op0))
8165 tree inner_type = TREE_TYPE (treeop0);
8166 machine_mode inner_mode = GET_MODE (op0);
8168 if (inner_mode == VOIDmode)
8169 inner_mode = TYPE_MODE (inner_type);
8171 if (modifier == EXPAND_INITIALIZER)
8172 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8173 subreg_lowpart_offset (mode,
8174 inner_mode));
8175 else
8176 op0= convert_modes (mode, inner_mode, op0,
8177 TYPE_UNSIGNED (inner_type));
8180 else if (modifier == EXPAND_INITIALIZER)
8181 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8183 else if (target == 0)
8184 op0 = convert_to_mode (mode, op0,
8185 TYPE_UNSIGNED (TREE_TYPE
8186 (treeop0)));
8187 else
8189 convert_move (target, op0,
8190 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8191 op0 = target;
8194 return REDUCE_BIT_FIELD (op0);
8196 case ADDR_SPACE_CONVERT_EXPR:
8198 tree treeop0_type = TREE_TYPE (treeop0);
8199 addr_space_t as_to;
8200 addr_space_t as_from;
8202 gcc_assert (POINTER_TYPE_P (type));
8203 gcc_assert (POINTER_TYPE_P (treeop0_type));
8205 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8206 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8208 /* Conversions between pointers to the same address space should
8209 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8210 gcc_assert (as_to != as_from);
8212 /* Ask target code to handle conversion between pointers
8213 to overlapping address spaces. */
8214 if (targetm.addr_space.subset_p (as_to, as_from)
8215 || targetm.addr_space.subset_p (as_from, as_to))
8217 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8218 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8219 gcc_assert (op0);
8220 return op0;
8223 /* For disjoint address spaces, converting anything but
8224 a null pointer invokes undefined behaviour. We simply
8225 always return a null pointer here. */
8226 return CONST0_RTX (mode);
8229 case POINTER_PLUS_EXPR:
8230 /* Even though the sizetype mode and the pointer's mode can be different
8231 expand is able to handle this correctly and get the correct result out
8232 of the PLUS_EXPR code. */
8233 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8234 if sizetype precision is smaller than pointer precision. */
8235 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8236 treeop1 = fold_convert_loc (loc, type,
8237 fold_convert_loc (loc, ssizetype,
8238 treeop1));
8239 /* If sizetype precision is larger than pointer precision, truncate the
8240 offset to have matching modes. */
8241 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8242 treeop1 = fold_convert_loc (loc, type, treeop1);
8244 case PLUS_EXPR:
8245 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8246 something else, make sure we add the register to the constant and
8247 then to the other thing. This case can occur during strength
8248 reduction and doing it this way will produce better code if the
8249 frame pointer or argument pointer is eliminated.
8251 fold-const.c will ensure that the constant is always in the inner
8252 PLUS_EXPR, so the only case we need to do anything about is if
8253 sp, ap, or fp is our second argument, in which case we must swap
8254 the innermost first argument and our second argument. */
8256 if (TREE_CODE (treeop0) == PLUS_EXPR
8257 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8258 && TREE_CODE (treeop1) == VAR_DECL
8259 && (DECL_RTL (treeop1) == frame_pointer_rtx
8260 || DECL_RTL (treeop1) == stack_pointer_rtx
8261 || DECL_RTL (treeop1) == arg_pointer_rtx))
8263 gcc_unreachable ();
8266 /* If the result is to be ptr_mode and we are adding an integer to
8267 something, we might be forming a constant. So try to use
8268 plus_constant. If it produces a sum and we can't accept it,
8269 use force_operand. This allows P = &ARR[const] to generate
8270 efficient code on machines where a SYMBOL_REF is not a valid
8271 address.
8273 If this is an EXPAND_SUM call, always return the sum. */
8274 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8275 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8277 if (modifier == EXPAND_STACK_PARM)
8278 target = 0;
8279 if (TREE_CODE (treeop0) == INTEGER_CST
8280 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8281 && TREE_CONSTANT (treeop1))
8283 rtx constant_part;
8284 HOST_WIDE_INT wc;
8285 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8287 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8288 EXPAND_SUM);
8289 /* Use wi::shwi to ensure that the constant is
8290 truncated according to the mode of OP1, then sign extended
8291 to a HOST_WIDE_INT. Using the constant directly can result
8292 in non-canonical RTL in a 64x32 cross compile. */
8293 wc = TREE_INT_CST_LOW (treeop0);
8294 constant_part =
8295 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8296 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8297 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8298 op1 = force_operand (op1, target);
8299 return REDUCE_BIT_FIELD (op1);
8302 else if (TREE_CODE (treeop1) == INTEGER_CST
8303 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8304 && TREE_CONSTANT (treeop0))
8306 rtx constant_part;
8307 HOST_WIDE_INT wc;
8308 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8310 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8311 (modifier == EXPAND_INITIALIZER
8312 ? EXPAND_INITIALIZER : EXPAND_SUM));
8313 if (! CONSTANT_P (op0))
8315 op1 = expand_expr (treeop1, NULL_RTX,
8316 VOIDmode, modifier);
8317 /* Return a PLUS if modifier says it's OK. */
8318 if (modifier == EXPAND_SUM
8319 || modifier == EXPAND_INITIALIZER)
8320 return simplify_gen_binary (PLUS, mode, op0, op1);
8321 goto binop2;
8323 /* Use wi::shwi to ensure that the constant is
8324 truncated according to the mode of OP1, then sign extended
8325 to a HOST_WIDE_INT. Using the constant directly can result
8326 in non-canonical RTL in a 64x32 cross compile. */
8327 wc = TREE_INT_CST_LOW (treeop1);
8328 constant_part
8329 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8330 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8331 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8332 op0 = force_operand (op0, target);
8333 return REDUCE_BIT_FIELD (op0);
8337 /* Use TER to expand pointer addition of a negated value
8338 as pointer subtraction. */
8339 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8340 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8341 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8342 && TREE_CODE (treeop1) == SSA_NAME
8343 && TYPE_MODE (TREE_TYPE (treeop0))
8344 == TYPE_MODE (TREE_TYPE (treeop1)))
8346 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8347 if (def)
8349 treeop1 = gimple_assign_rhs1 (def);
8350 code = MINUS_EXPR;
8351 goto do_minus;
8355 /* No sense saving up arithmetic to be done
8356 if it's all in the wrong mode to form part of an address.
8357 And force_operand won't know whether to sign-extend or
8358 zero-extend. */
8359 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8360 || mode != ptr_mode)
8362 expand_operands (treeop0, treeop1,
8363 subtarget, &op0, &op1, EXPAND_NORMAL);
8364 if (op0 == const0_rtx)
8365 return op1;
8366 if (op1 == const0_rtx)
8367 return op0;
8368 goto binop2;
8371 expand_operands (treeop0, treeop1,
8372 subtarget, &op0, &op1, modifier);
8373 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8375 case MINUS_EXPR:
8376 do_minus:
8377 /* For initializers, we are allowed to return a MINUS of two
8378 symbolic constants. Here we handle all cases when both operands
8379 are constant. */
8380 /* Handle difference of two symbolic constants,
8381 for the sake of an initializer. */
8382 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8383 && really_constant_p (treeop0)
8384 && really_constant_p (treeop1))
8386 expand_operands (treeop0, treeop1,
8387 NULL_RTX, &op0, &op1, modifier);
8389 /* If the last operand is a CONST_INT, use plus_constant of
8390 the negated constant. Else make the MINUS. */
8391 if (CONST_INT_P (op1))
8392 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8393 -INTVAL (op1)));
8394 else
8395 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8398 /* No sense saving up arithmetic to be done
8399 if it's all in the wrong mode to form part of an address.
8400 And force_operand won't know whether to sign-extend or
8401 zero-extend. */
8402 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8403 || mode != ptr_mode)
8404 goto binop;
8406 expand_operands (treeop0, treeop1,
8407 subtarget, &op0, &op1, modifier);
8409 /* Convert A - const to A + (-const). */
8410 if (CONST_INT_P (op1))
8412 op1 = negate_rtx (mode, op1);
8413 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8416 goto binop2;
8418 case WIDEN_MULT_PLUS_EXPR:
8419 case WIDEN_MULT_MINUS_EXPR:
8420 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8421 op2 = expand_normal (treeop2);
8422 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8423 target, unsignedp);
8424 return target;
8426 case WIDEN_MULT_EXPR:
8427 /* If first operand is constant, swap them.
8428 Thus the following special case checks need only
8429 check the second operand. */
8430 if (TREE_CODE (treeop0) == INTEGER_CST)
8432 tree t1 = treeop0;
8433 treeop0 = treeop1;
8434 treeop1 = t1;
8437 /* First, check if we have a multiplication of one signed and one
8438 unsigned operand. */
8439 if (TREE_CODE (treeop1) != INTEGER_CST
8440 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8441 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8443 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8444 this_optab = usmul_widen_optab;
8445 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8446 != CODE_FOR_nothing)
8448 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8449 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8450 EXPAND_NORMAL);
8451 else
8452 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8453 EXPAND_NORMAL);
8454 /* op0 and op1 might still be constant, despite the above
8455 != INTEGER_CST check. Handle it. */
8456 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8458 op0 = convert_modes (innermode, mode, op0, true);
8459 op1 = convert_modes (innermode, mode, op1, false);
8460 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8461 target, unsignedp));
8463 goto binop3;
8466 /* Check for a multiplication with matching signedness. */
8467 else if ((TREE_CODE (treeop1) == INTEGER_CST
8468 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8469 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8470 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8472 tree op0type = TREE_TYPE (treeop0);
8473 machine_mode innermode = TYPE_MODE (op0type);
8474 bool zextend_p = TYPE_UNSIGNED (op0type);
8475 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8476 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8478 if (TREE_CODE (treeop0) != INTEGER_CST)
8480 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8481 != CODE_FOR_nothing)
8483 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8484 EXPAND_NORMAL);
8485 /* op0 and op1 might still be constant, despite the above
8486 != INTEGER_CST check. Handle it. */
8487 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8489 widen_mult_const:
8490 op0 = convert_modes (innermode, mode, op0, zextend_p);
8492 = convert_modes (innermode, mode, op1,
8493 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8494 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8495 target,
8496 unsignedp));
8498 temp = expand_widening_mult (mode, op0, op1, target,
8499 unsignedp, this_optab);
8500 return REDUCE_BIT_FIELD (temp);
8502 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8503 != CODE_FOR_nothing
8504 && innermode == word_mode)
8506 rtx htem, hipart;
8507 op0 = expand_normal (treeop0);
8508 if (TREE_CODE (treeop1) == INTEGER_CST)
8509 op1 = convert_modes (innermode, mode,
8510 expand_normal (treeop1),
8511 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8512 else
8513 op1 = expand_normal (treeop1);
8514 /* op0 and op1 might still be constant, despite the above
8515 != INTEGER_CST check. Handle it. */
8516 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8517 goto widen_mult_const;
8518 temp = expand_binop (mode, other_optab, op0, op1, target,
8519 unsignedp, OPTAB_LIB_WIDEN);
8520 hipart = gen_highpart (innermode, temp);
8521 htem = expand_mult_highpart_adjust (innermode, hipart,
8522 op0, op1, hipart,
8523 zextend_p);
8524 if (htem != hipart)
8525 emit_move_insn (hipart, htem);
8526 return REDUCE_BIT_FIELD (temp);
8530 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8531 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8532 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8533 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8535 case FMA_EXPR:
8537 optab opt = fma_optab;
8538 gimple def0, def2;
8540 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8541 call. */
8542 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8544 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8545 tree call_expr;
8547 gcc_assert (fn != NULL_TREE);
8548 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8549 return expand_builtin (call_expr, target, subtarget, mode, false);
8552 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8553 /* The multiplication is commutative - look at its 2nd operand
8554 if the first isn't fed by a negate. */
8555 if (!def0)
8557 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8558 /* Swap operands if the 2nd operand is fed by a negate. */
8559 if (def0)
8561 tree tem = treeop0;
8562 treeop0 = treeop1;
8563 treeop1 = tem;
8566 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8568 op0 = op2 = NULL;
8570 if (def0 && def2
8571 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8573 opt = fnms_optab;
8574 op0 = expand_normal (gimple_assign_rhs1 (def0));
8575 op2 = expand_normal (gimple_assign_rhs1 (def2));
8577 else if (def0
8578 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8580 opt = fnma_optab;
8581 op0 = expand_normal (gimple_assign_rhs1 (def0));
8583 else if (def2
8584 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8586 opt = fms_optab;
8587 op2 = expand_normal (gimple_assign_rhs1 (def2));
8590 if (op0 == NULL)
8591 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8592 if (op2 == NULL)
8593 op2 = expand_normal (treeop2);
8594 op1 = expand_normal (treeop1);
8596 return expand_ternary_op (TYPE_MODE (type), opt,
8597 op0, op1, op2, target, 0);
8600 case MULT_EXPR:
8601 /* If this is a fixed-point operation, then we cannot use the code
8602 below because "expand_mult" doesn't support sat/no-sat fixed-point
8603 multiplications. */
8604 if (ALL_FIXED_POINT_MODE_P (mode))
8605 goto binop;
8607 /* If first operand is constant, swap them.
8608 Thus the following special case checks need only
8609 check the second operand. */
8610 if (TREE_CODE (treeop0) == INTEGER_CST)
8612 tree t1 = treeop0;
8613 treeop0 = treeop1;
8614 treeop1 = t1;
8617 /* Attempt to return something suitable for generating an
8618 indexed address, for machines that support that. */
8620 if (modifier == EXPAND_SUM && mode == ptr_mode
8621 && tree_fits_shwi_p (treeop1))
8623 tree exp1 = treeop1;
8625 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8626 EXPAND_SUM);
8628 if (!REG_P (op0))
8629 op0 = force_operand (op0, NULL_RTX);
8630 if (!REG_P (op0))
8631 op0 = copy_to_mode_reg (mode, op0);
8633 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8634 gen_int_mode (tree_to_shwi (exp1),
8635 TYPE_MODE (TREE_TYPE (exp1)))));
8638 if (modifier == EXPAND_STACK_PARM)
8639 target = 0;
8641 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8642 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8644 case TRUNC_DIV_EXPR:
8645 case FLOOR_DIV_EXPR:
8646 case CEIL_DIV_EXPR:
8647 case ROUND_DIV_EXPR:
8648 case EXACT_DIV_EXPR:
8649 /* If this is a fixed-point operation, then we cannot use the code
8650 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8651 divisions. */
8652 if (ALL_FIXED_POINT_MODE_P (mode))
8653 goto binop;
8655 if (modifier == EXPAND_STACK_PARM)
8656 target = 0;
8657 /* Possible optimization: compute the dividend with EXPAND_SUM
8658 then if the divisor is constant can optimize the case
8659 where some terms of the dividend have coeffs divisible by it. */
8660 expand_operands (treeop0, treeop1,
8661 subtarget, &op0, &op1, EXPAND_NORMAL);
8662 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8664 case RDIV_EXPR:
8665 goto binop;
8667 case MULT_HIGHPART_EXPR:
8668 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8669 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8670 gcc_assert (temp);
8671 return temp;
8673 case TRUNC_MOD_EXPR:
8674 case FLOOR_MOD_EXPR:
8675 case CEIL_MOD_EXPR:
8676 case ROUND_MOD_EXPR:
8677 if (modifier == EXPAND_STACK_PARM)
8678 target = 0;
8679 expand_operands (treeop0, treeop1,
8680 subtarget, &op0, &op1, EXPAND_NORMAL);
8681 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8683 case FIXED_CONVERT_EXPR:
8684 op0 = expand_normal (treeop0);
8685 if (target == 0 || modifier == EXPAND_STACK_PARM)
8686 target = gen_reg_rtx (mode);
8688 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8689 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8690 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8691 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8692 else
8693 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8694 return target;
8696 case FIX_TRUNC_EXPR:
8697 op0 = expand_normal (treeop0);
8698 if (target == 0 || modifier == EXPAND_STACK_PARM)
8699 target = gen_reg_rtx (mode);
8700 expand_fix (target, op0, unsignedp);
8701 return target;
8703 case FLOAT_EXPR:
8704 op0 = expand_normal (treeop0);
8705 if (target == 0 || modifier == EXPAND_STACK_PARM)
8706 target = gen_reg_rtx (mode);
8707 /* expand_float can't figure out what to do if FROM has VOIDmode.
8708 So give it the correct mode. With -O, cse will optimize this. */
8709 if (GET_MODE (op0) == VOIDmode)
8710 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8711 op0);
8712 expand_float (target, op0,
8713 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8714 return target;
8716 case NEGATE_EXPR:
8717 op0 = expand_expr (treeop0, subtarget,
8718 VOIDmode, EXPAND_NORMAL);
8719 if (modifier == EXPAND_STACK_PARM)
8720 target = 0;
8721 temp = expand_unop (mode,
8722 optab_for_tree_code (NEGATE_EXPR, type,
8723 optab_default),
8724 op0, target, 0);
8725 gcc_assert (temp);
8726 return REDUCE_BIT_FIELD (temp);
8728 case ABS_EXPR:
8729 op0 = expand_expr (treeop0, subtarget,
8730 VOIDmode, EXPAND_NORMAL);
8731 if (modifier == EXPAND_STACK_PARM)
8732 target = 0;
8734 /* ABS_EXPR is not valid for complex arguments. */
8735 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8736 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8738 /* Unsigned abs is simply the operand. Testing here means we don't
8739 risk generating incorrect code below. */
8740 if (TYPE_UNSIGNED (type))
8741 return op0;
8743 return expand_abs (mode, op0, target, unsignedp,
8744 safe_from_p (target, treeop0, 1));
8746 case MAX_EXPR:
8747 case MIN_EXPR:
8748 target = original_target;
8749 if (target == 0
8750 || modifier == EXPAND_STACK_PARM
8751 || (MEM_P (target) && MEM_VOLATILE_P (target))
8752 || GET_MODE (target) != mode
8753 || (REG_P (target)
8754 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8755 target = gen_reg_rtx (mode);
8756 expand_operands (treeop0, treeop1,
8757 target, &op0, &op1, EXPAND_NORMAL);
8759 /* First try to do it with a special MIN or MAX instruction.
8760 If that does not win, use a conditional jump to select the proper
8761 value. */
8762 this_optab = optab_for_tree_code (code, type, optab_default);
8763 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8764 OPTAB_WIDEN);
8765 if (temp != 0)
8766 return temp;
8768 /* At this point, a MEM target is no longer useful; we will get better
8769 code without it. */
8771 if (! REG_P (target))
8772 target = gen_reg_rtx (mode);
8774 /* If op1 was placed in target, swap op0 and op1. */
8775 if (target != op0 && target == op1)
8776 std::swap (op0, op1);
8778 /* We generate better code and avoid problems with op1 mentioning
8779 target by forcing op1 into a pseudo if it isn't a constant. */
8780 if (! CONSTANT_P (op1))
8781 op1 = force_reg (mode, op1);
8784 enum rtx_code comparison_code;
8785 rtx cmpop1 = op1;
8787 if (code == MAX_EXPR)
8788 comparison_code = unsignedp ? GEU : GE;
8789 else
8790 comparison_code = unsignedp ? LEU : LE;
8792 /* Canonicalize to comparisons against 0. */
8793 if (op1 == const1_rtx)
8795 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8796 or (a != 0 ? a : 1) for unsigned.
8797 For MIN we are safe converting (a <= 1 ? a : 1)
8798 into (a <= 0 ? a : 1) */
8799 cmpop1 = const0_rtx;
8800 if (code == MAX_EXPR)
8801 comparison_code = unsignedp ? NE : GT;
8803 if (op1 == constm1_rtx && !unsignedp)
8805 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8806 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8807 cmpop1 = const0_rtx;
8808 if (code == MIN_EXPR)
8809 comparison_code = LT;
8812 /* Use a conditional move if possible. */
8813 if (can_conditionally_move_p (mode))
8815 rtx insn;
8817 start_sequence ();
8819 /* Try to emit the conditional move. */
8820 insn = emit_conditional_move (target, comparison_code,
8821 op0, cmpop1, mode,
8822 op0, op1, mode,
8823 unsignedp);
8825 /* If we could do the conditional move, emit the sequence,
8826 and return. */
8827 if (insn)
8829 rtx_insn *seq = get_insns ();
8830 end_sequence ();
8831 emit_insn (seq);
8832 return target;
8835 /* Otherwise discard the sequence and fall back to code with
8836 branches. */
8837 end_sequence ();
8840 if (target != op0)
8841 emit_move_insn (target, op0);
8843 lab = gen_label_rtx ();
8844 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8845 unsignedp, mode, NULL_RTX, NULL, lab,
8846 -1);
8848 emit_move_insn (target, op1);
8849 emit_label (lab);
8850 return target;
8852 case BIT_NOT_EXPR:
8853 op0 = expand_expr (treeop0, subtarget,
8854 VOIDmode, EXPAND_NORMAL);
8855 if (modifier == EXPAND_STACK_PARM)
8856 target = 0;
8857 /* In case we have to reduce the result to bitfield precision
8858 for unsigned bitfield expand this as XOR with a proper constant
8859 instead. */
8860 if (reduce_bit_field && TYPE_UNSIGNED (type))
8862 wide_int mask = wi::mask (TYPE_PRECISION (type),
8863 false, GET_MODE_PRECISION (mode));
8865 temp = expand_binop (mode, xor_optab, op0,
8866 immed_wide_int_const (mask, mode),
8867 target, 1, OPTAB_LIB_WIDEN);
8869 else
8870 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8871 gcc_assert (temp);
8872 return temp;
8874 /* ??? Can optimize bitwise operations with one arg constant.
8875 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8876 and (a bitwise1 b) bitwise2 b (etc)
8877 but that is probably not worth while. */
8879 case BIT_AND_EXPR:
8880 case BIT_IOR_EXPR:
8881 case BIT_XOR_EXPR:
8882 goto binop;
8884 case LROTATE_EXPR:
8885 case RROTATE_EXPR:
8886 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8887 || (GET_MODE_PRECISION (TYPE_MODE (type))
8888 == TYPE_PRECISION (type)));
8889 /* fall through */
8891 case LSHIFT_EXPR:
8892 case RSHIFT_EXPR:
8893 /* If this is a fixed-point operation, then we cannot use the code
8894 below because "expand_shift" doesn't support sat/no-sat fixed-point
8895 shifts. */
8896 if (ALL_FIXED_POINT_MODE_P (mode))
8897 goto binop;
8899 if (! safe_from_p (subtarget, treeop1, 1))
8900 subtarget = 0;
8901 if (modifier == EXPAND_STACK_PARM)
8902 target = 0;
8903 op0 = expand_expr (treeop0, subtarget,
8904 VOIDmode, EXPAND_NORMAL);
8905 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8906 unsignedp);
8907 if (code == LSHIFT_EXPR)
8908 temp = REDUCE_BIT_FIELD (temp);
8909 return temp;
8911 /* Could determine the answer when only additive constants differ. Also,
8912 the addition of one can be handled by changing the condition. */
8913 case LT_EXPR:
8914 case LE_EXPR:
8915 case GT_EXPR:
8916 case GE_EXPR:
8917 case EQ_EXPR:
8918 case NE_EXPR:
8919 case UNORDERED_EXPR:
8920 case ORDERED_EXPR:
8921 case UNLT_EXPR:
8922 case UNLE_EXPR:
8923 case UNGT_EXPR:
8924 case UNGE_EXPR:
8925 case UNEQ_EXPR:
8926 case LTGT_EXPR:
8928 temp = do_store_flag (ops,
8929 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8930 tmode != VOIDmode ? tmode : mode);
8931 if (temp)
8932 return temp;
8934 /* Use a compare and a jump for BLKmode comparisons, or for function
8935 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
8937 if ((target == 0
8938 || modifier == EXPAND_STACK_PARM
8939 || ! safe_from_p (target, treeop0, 1)
8940 || ! safe_from_p (target, treeop1, 1)
8941 /* Make sure we don't have a hard reg (such as function's return
8942 value) live across basic blocks, if not optimizing. */
8943 || (!optimize && REG_P (target)
8944 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8945 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8947 emit_move_insn (target, const0_rtx);
8949 rtx_code_label *lab1 = gen_label_rtx ();
8950 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
8952 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8953 emit_move_insn (target, constm1_rtx);
8954 else
8955 emit_move_insn (target, const1_rtx);
8957 emit_label (lab1);
8958 return target;
8960 case COMPLEX_EXPR:
8961 /* Get the rtx code of the operands. */
8962 op0 = expand_normal (treeop0);
8963 op1 = expand_normal (treeop1);
8965 if (!target)
8966 target = gen_reg_rtx (TYPE_MODE (type));
8967 else
8968 /* If target overlaps with op1, then either we need to force
8969 op1 into a pseudo (if target also overlaps with op0),
8970 or write the complex parts in reverse order. */
8971 switch (GET_CODE (target))
8973 case CONCAT:
8974 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
8976 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
8978 complex_expr_force_op1:
8979 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
8980 emit_move_insn (temp, op1);
8981 op1 = temp;
8982 break;
8984 complex_expr_swap_order:
8985 /* Move the imaginary (op1) and real (op0) parts to their
8986 location. */
8987 write_complex_part (target, op1, true);
8988 write_complex_part (target, op0, false);
8990 return target;
8992 break;
8993 case MEM:
8994 temp = adjust_address_nv (target,
8995 GET_MODE_INNER (GET_MODE (target)), 0);
8996 if (reg_overlap_mentioned_p (temp, op1))
8998 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
8999 temp = adjust_address_nv (target, imode,
9000 GET_MODE_SIZE (imode));
9001 if (reg_overlap_mentioned_p (temp, op0))
9002 goto complex_expr_force_op1;
9003 goto complex_expr_swap_order;
9005 break;
9006 default:
9007 if (reg_overlap_mentioned_p (target, op1))
9009 if (reg_overlap_mentioned_p (target, op0))
9010 goto complex_expr_force_op1;
9011 goto complex_expr_swap_order;
9013 break;
9016 /* Move the real (op0) and imaginary (op1) parts to their location. */
9017 write_complex_part (target, op0, false);
9018 write_complex_part (target, op1, true);
9020 return target;
9022 case WIDEN_SUM_EXPR:
9024 tree oprnd0 = treeop0;
9025 tree oprnd1 = treeop1;
9027 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9028 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9029 target, unsignedp);
9030 return target;
9033 case REDUC_MAX_EXPR:
9034 case REDUC_MIN_EXPR:
9035 case REDUC_PLUS_EXPR:
9037 op0 = expand_normal (treeop0);
9038 this_optab = optab_for_tree_code (code, type, optab_default);
9039 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9041 if (optab_handler (this_optab, vec_mode) != CODE_FOR_nothing)
9043 struct expand_operand ops[2];
9044 enum insn_code icode = optab_handler (this_optab, vec_mode);
9046 create_output_operand (&ops[0], target, mode);
9047 create_input_operand (&ops[1], op0, vec_mode);
9048 if (maybe_expand_insn (icode, 2, ops))
9050 target = ops[0].value;
9051 if (GET_MODE (target) != mode)
9052 return gen_lowpart (tmode, target);
9053 return target;
9056 /* Fall back to optab with vector result, and then extract scalar. */
9057 this_optab = scalar_reduc_to_vector (this_optab, type);
9058 temp = expand_unop (vec_mode, this_optab, op0, NULL_RTX, unsignedp);
9059 gcc_assert (temp);
9060 /* The tree code produces a scalar result, but (somewhat by convention)
9061 the optab produces a vector with the result in element 0 if
9062 little-endian, or element N-1 if big-endian. So pull the scalar
9063 result out of that element. */
9064 int index = BYTES_BIG_ENDIAN ? GET_MODE_NUNITS (vec_mode) - 1 : 0;
9065 int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (vec_mode));
9066 temp = extract_bit_field (temp, bitsize, bitsize * index, unsignedp,
9067 target, mode, mode);
9068 gcc_assert (temp);
9069 return temp;
9072 case VEC_UNPACK_HI_EXPR:
9073 case VEC_UNPACK_LO_EXPR:
9075 op0 = expand_normal (treeop0);
9076 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9077 target, unsignedp);
9078 gcc_assert (temp);
9079 return temp;
9082 case VEC_UNPACK_FLOAT_HI_EXPR:
9083 case VEC_UNPACK_FLOAT_LO_EXPR:
9085 op0 = expand_normal (treeop0);
9086 /* The signedness is determined from input operand. */
9087 temp = expand_widen_pattern_expr
9088 (ops, op0, NULL_RTX, NULL_RTX,
9089 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9091 gcc_assert (temp);
9092 return temp;
9095 case VEC_WIDEN_MULT_HI_EXPR:
9096 case VEC_WIDEN_MULT_LO_EXPR:
9097 case VEC_WIDEN_MULT_EVEN_EXPR:
9098 case VEC_WIDEN_MULT_ODD_EXPR:
9099 case VEC_WIDEN_LSHIFT_HI_EXPR:
9100 case VEC_WIDEN_LSHIFT_LO_EXPR:
9101 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9102 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9103 target, unsignedp);
9104 gcc_assert (target);
9105 return target;
9107 case VEC_PACK_TRUNC_EXPR:
9108 case VEC_PACK_SAT_EXPR:
9109 case VEC_PACK_FIX_TRUNC_EXPR:
9110 mode = TYPE_MODE (TREE_TYPE (treeop0));
9111 goto binop;
9113 case VEC_PERM_EXPR:
9114 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9115 op2 = expand_normal (treeop2);
9117 /* Careful here: if the target doesn't support integral vector modes,
9118 a constant selection vector could wind up smooshed into a normal
9119 integral constant. */
9120 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9122 tree sel_type = TREE_TYPE (treeop2);
9123 machine_mode vmode
9124 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9125 TYPE_VECTOR_SUBPARTS (sel_type));
9126 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9127 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9128 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9130 else
9131 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9133 temp = expand_vec_perm (mode, op0, op1, op2, target);
9134 gcc_assert (temp);
9135 return temp;
9137 case DOT_PROD_EXPR:
9139 tree oprnd0 = treeop0;
9140 tree oprnd1 = treeop1;
9141 tree oprnd2 = treeop2;
9142 rtx op2;
9144 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9145 op2 = expand_normal (oprnd2);
9146 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9147 target, unsignedp);
9148 return target;
9151 case SAD_EXPR:
9153 tree oprnd0 = treeop0;
9154 tree oprnd1 = treeop1;
9155 tree oprnd2 = treeop2;
9156 rtx op2;
9158 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9159 op2 = expand_normal (oprnd2);
9160 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9161 target, unsignedp);
9162 return target;
9165 case REALIGN_LOAD_EXPR:
9167 tree oprnd0 = treeop0;
9168 tree oprnd1 = treeop1;
9169 tree oprnd2 = treeop2;
9170 rtx op2;
9172 this_optab = optab_for_tree_code (code, type, optab_default);
9173 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9174 op2 = expand_normal (oprnd2);
9175 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9176 target, unsignedp);
9177 gcc_assert (temp);
9178 return temp;
9181 case COND_EXPR:
9183 /* A COND_EXPR with its type being VOID_TYPE represents a
9184 conditional jump and is handled in
9185 expand_gimple_cond_expr. */
9186 gcc_assert (!VOID_TYPE_P (type));
9188 /* Note that COND_EXPRs whose type is a structure or union
9189 are required to be constructed to contain assignments of
9190 a temporary variable, so that we can evaluate them here
9191 for side effect only. If type is void, we must do likewise. */
9193 gcc_assert (!TREE_ADDRESSABLE (type)
9194 && !ignore
9195 && TREE_TYPE (treeop1) != void_type_node
9196 && TREE_TYPE (treeop2) != void_type_node);
9198 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9199 if (temp)
9200 return temp;
9202 /* If we are not to produce a result, we have no target. Otherwise,
9203 if a target was specified use it; it will not be used as an
9204 intermediate target unless it is safe. If no target, use a
9205 temporary. */
9207 if (modifier != EXPAND_STACK_PARM
9208 && original_target
9209 && safe_from_p (original_target, treeop0, 1)
9210 && GET_MODE (original_target) == mode
9211 && !MEM_P (original_target))
9212 temp = original_target;
9213 else
9214 temp = assign_temp (type, 0, 1);
9216 do_pending_stack_adjust ();
9217 NO_DEFER_POP;
9218 rtx_code_label *lab0 = gen_label_rtx ();
9219 rtx_code_label *lab1 = gen_label_rtx ();
9220 jumpifnot (treeop0, lab0, -1);
9221 store_expr (treeop1, temp,
9222 modifier == EXPAND_STACK_PARM,
9223 false);
9225 emit_jump_insn (gen_jump (lab1));
9226 emit_barrier ();
9227 emit_label (lab0);
9228 store_expr (treeop2, temp,
9229 modifier == EXPAND_STACK_PARM,
9230 false);
9232 emit_label (lab1);
9233 OK_DEFER_POP;
9234 return temp;
9237 case VEC_COND_EXPR:
9238 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9239 return target;
9241 default:
9242 gcc_unreachable ();
9245 /* Here to do an ordinary binary operator. */
9246 binop:
9247 expand_operands (treeop0, treeop1,
9248 subtarget, &op0, &op1, EXPAND_NORMAL);
9249 binop2:
9250 this_optab = optab_for_tree_code (code, type, optab_default);
9251 binop3:
9252 if (modifier == EXPAND_STACK_PARM)
9253 target = 0;
9254 temp = expand_binop (mode, this_optab, op0, op1, target,
9255 unsignedp, OPTAB_LIB_WIDEN);
9256 gcc_assert (temp);
9257 /* Bitwise operations do not need bitfield reduction as we expect their
9258 operands being properly truncated. */
9259 if (code == BIT_XOR_EXPR
9260 || code == BIT_AND_EXPR
9261 || code == BIT_IOR_EXPR)
9262 return temp;
9263 return REDUCE_BIT_FIELD (temp);
9265 #undef REDUCE_BIT_FIELD
9268 /* Return TRUE if expression STMT is suitable for replacement.
9269 Never consider memory loads as replaceable, because those don't ever lead
9270 into constant expressions. */
9272 static bool
9273 stmt_is_replaceable_p (gimple stmt)
9275 if (ssa_is_replaceable_p (stmt))
9277 /* Don't move around loads. */
9278 if (!gimple_assign_single_p (stmt)
9279 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9280 return true;
9282 return false;
9286 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9287 enum expand_modifier modifier, rtx *alt_rtl,
9288 bool inner_reference_p)
9290 rtx op0, op1, temp, decl_rtl;
9291 tree type;
9292 int unsignedp;
9293 machine_mode mode, dmode;
9294 enum tree_code code = TREE_CODE (exp);
9295 rtx subtarget, original_target;
9296 int ignore;
9297 tree context;
9298 bool reduce_bit_field;
9299 location_t loc = EXPR_LOCATION (exp);
9300 struct separate_ops ops;
9301 tree treeop0, treeop1, treeop2;
9302 tree ssa_name = NULL_TREE;
9303 gimple g;
9305 type = TREE_TYPE (exp);
9306 mode = TYPE_MODE (type);
9307 unsignedp = TYPE_UNSIGNED (type);
9309 treeop0 = treeop1 = treeop2 = NULL_TREE;
9310 if (!VL_EXP_CLASS_P (exp))
9311 switch (TREE_CODE_LENGTH (code))
9313 default:
9314 case 3: treeop2 = TREE_OPERAND (exp, 2);
9315 case 2: treeop1 = TREE_OPERAND (exp, 1);
9316 case 1: treeop0 = TREE_OPERAND (exp, 0);
9317 case 0: break;
9319 ops.code = code;
9320 ops.type = type;
9321 ops.op0 = treeop0;
9322 ops.op1 = treeop1;
9323 ops.op2 = treeop2;
9324 ops.location = loc;
9326 ignore = (target == const0_rtx
9327 || ((CONVERT_EXPR_CODE_P (code)
9328 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9329 && TREE_CODE (type) == VOID_TYPE));
9331 /* An operation in what may be a bit-field type needs the
9332 result to be reduced to the precision of the bit-field type,
9333 which is narrower than that of the type's mode. */
9334 reduce_bit_field = (!ignore
9335 && INTEGRAL_TYPE_P (type)
9336 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9338 /* If we are going to ignore this result, we need only do something
9339 if there is a side-effect somewhere in the expression. If there
9340 is, short-circuit the most common cases here. Note that we must
9341 not call expand_expr with anything but const0_rtx in case this
9342 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9344 if (ignore)
9346 if (! TREE_SIDE_EFFECTS (exp))
9347 return const0_rtx;
9349 /* Ensure we reference a volatile object even if value is ignored, but
9350 don't do this if all we are doing is taking its address. */
9351 if (TREE_THIS_VOLATILE (exp)
9352 && TREE_CODE (exp) != FUNCTION_DECL
9353 && mode != VOIDmode && mode != BLKmode
9354 && modifier != EXPAND_CONST_ADDRESS)
9356 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9357 if (MEM_P (temp))
9358 copy_to_reg (temp);
9359 return const0_rtx;
9362 if (TREE_CODE_CLASS (code) == tcc_unary
9363 || code == BIT_FIELD_REF
9364 || code == COMPONENT_REF
9365 || code == INDIRECT_REF)
9366 return expand_expr (treeop0, const0_rtx, VOIDmode,
9367 modifier);
9369 else if (TREE_CODE_CLASS (code) == tcc_binary
9370 || TREE_CODE_CLASS (code) == tcc_comparison
9371 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9373 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9374 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9375 return const0_rtx;
9378 target = 0;
9381 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9382 target = 0;
9384 /* Use subtarget as the target for operand 0 of a binary operation. */
9385 subtarget = get_subtarget (target);
9386 original_target = target;
9388 switch (code)
9390 case LABEL_DECL:
9392 tree function = decl_function_context (exp);
9394 temp = label_rtx (exp);
9395 temp = gen_rtx_LABEL_REF (Pmode, temp);
9397 if (function != current_function_decl
9398 && function != 0)
9399 LABEL_REF_NONLOCAL_P (temp) = 1;
9401 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9402 return temp;
9405 case SSA_NAME:
9406 /* ??? ivopts calls expander, without any preparation from
9407 out-of-ssa. So fake instructions as if this was an access to the
9408 base variable. This unnecessarily allocates a pseudo, see how we can
9409 reuse it, if partition base vars have it set already. */
9410 if (!currently_expanding_to_rtl)
9412 tree var = SSA_NAME_VAR (exp);
9413 if (var && DECL_RTL_SET_P (var))
9414 return DECL_RTL (var);
9415 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9416 LAST_VIRTUAL_REGISTER + 1);
9419 g = get_gimple_for_ssa_name (exp);
9420 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9421 if (g == NULL
9422 && modifier == EXPAND_INITIALIZER
9423 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9424 && (optimize || !SSA_NAME_VAR (exp)
9425 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9426 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9427 g = SSA_NAME_DEF_STMT (exp);
9428 if (g)
9430 rtx r;
9431 ops.code = gimple_assign_rhs_code (g);
9432 switch (get_gimple_rhs_class (ops.code))
9434 case GIMPLE_TERNARY_RHS:
9435 ops.op2 = gimple_assign_rhs3 (g);
9436 /* Fallthru */
9437 case GIMPLE_BINARY_RHS:
9438 ops.op1 = gimple_assign_rhs2 (g);
9440 /* Try to expand conditonal compare. */
9441 if (targetm.gen_ccmp_first)
9443 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9444 r = expand_ccmp_expr (g);
9445 if (r)
9446 break;
9448 /* Fallthru */
9449 case GIMPLE_UNARY_RHS:
9450 ops.op0 = gimple_assign_rhs1 (g);
9451 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9452 ops.location = gimple_location (g);
9453 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9454 break;
9455 case GIMPLE_SINGLE_RHS:
9457 location_t saved_loc = curr_insn_location ();
9458 set_curr_insn_location (gimple_location (g));
9459 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9460 tmode, modifier, NULL, inner_reference_p);
9461 set_curr_insn_location (saved_loc);
9462 break;
9464 default:
9465 gcc_unreachable ();
9467 if (REG_P (r) && !REG_EXPR (r))
9468 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9469 return r;
9472 ssa_name = exp;
9473 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9474 exp = SSA_NAME_VAR (ssa_name);
9475 goto expand_decl_rtl;
9477 case PARM_DECL:
9478 case VAR_DECL:
9479 /* If a static var's type was incomplete when the decl was written,
9480 but the type is complete now, lay out the decl now. */
9481 if (DECL_SIZE (exp) == 0
9482 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9483 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9484 layout_decl (exp, 0);
9486 /* ... fall through ... */
9488 case FUNCTION_DECL:
9489 case RESULT_DECL:
9490 decl_rtl = DECL_RTL (exp);
9491 expand_decl_rtl:
9492 gcc_assert (decl_rtl);
9493 decl_rtl = copy_rtx (decl_rtl);
9494 /* Record writes to register variables. */
9495 if (modifier == EXPAND_WRITE
9496 && REG_P (decl_rtl)
9497 && HARD_REGISTER_P (decl_rtl))
9498 add_to_hard_reg_set (&crtl->asm_clobbers,
9499 GET_MODE (decl_rtl), REGNO (decl_rtl));
9501 /* Ensure variable marked as used even if it doesn't go through
9502 a parser. If it hasn't be used yet, write out an external
9503 definition. */
9504 if (exp)
9505 TREE_USED (exp) = 1;
9507 /* Show we haven't gotten RTL for this yet. */
9508 temp = 0;
9510 /* Variables inherited from containing functions should have
9511 been lowered by this point. */
9512 if (exp)
9513 context = decl_function_context (exp);
9514 gcc_assert (!exp
9515 || SCOPE_FILE_SCOPE_P (context)
9516 || context == current_function_decl
9517 || TREE_STATIC (exp)
9518 || DECL_EXTERNAL (exp)
9519 /* ??? C++ creates functions that are not TREE_STATIC. */
9520 || TREE_CODE (exp) == FUNCTION_DECL);
9522 /* This is the case of an array whose size is to be determined
9523 from its initializer, while the initializer is still being parsed.
9524 ??? We aren't parsing while expanding anymore. */
9526 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9527 temp = validize_mem (decl_rtl);
9529 /* If DECL_RTL is memory, we are in the normal case and the
9530 address is not valid, get the address into a register. */
9532 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9534 if (alt_rtl)
9535 *alt_rtl = decl_rtl;
9536 decl_rtl = use_anchored_address (decl_rtl);
9537 if (modifier != EXPAND_CONST_ADDRESS
9538 && modifier != EXPAND_SUM
9539 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9540 : GET_MODE (decl_rtl),
9541 XEXP (decl_rtl, 0),
9542 MEM_ADDR_SPACE (decl_rtl)))
9543 temp = replace_equiv_address (decl_rtl,
9544 copy_rtx (XEXP (decl_rtl, 0)));
9547 /* If we got something, return it. But first, set the alignment
9548 if the address is a register. */
9549 if (temp != 0)
9551 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9552 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9554 return temp;
9557 if (exp)
9558 dmode = DECL_MODE (exp);
9559 else
9560 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9562 /* If the mode of DECL_RTL does not match that of the decl,
9563 there are two cases: we are dealing with a BLKmode value
9564 that is returned in a register, or we are dealing with
9565 a promoted value. In the latter case, return a SUBREG
9566 of the wanted mode, but mark it so that we know that it
9567 was already extended. */
9568 if (REG_P (decl_rtl)
9569 && dmode != BLKmode
9570 && GET_MODE (decl_rtl) != dmode)
9572 machine_mode pmode;
9574 /* Get the signedness to be used for this variable. Ensure we get
9575 the same mode we got when the variable was declared. */
9576 if (code != SSA_NAME)
9577 pmode = promote_decl_mode (exp, &unsignedp);
9578 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9579 && gimple_code (g) == GIMPLE_CALL
9580 && !gimple_call_internal_p (g))
9581 pmode = promote_function_mode (type, mode, &unsignedp,
9582 gimple_call_fntype (g),
9584 else
9585 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9586 gcc_assert (GET_MODE (decl_rtl) == pmode);
9588 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9589 SUBREG_PROMOTED_VAR_P (temp) = 1;
9590 SUBREG_PROMOTED_SET (temp, unsignedp);
9591 return temp;
9594 return decl_rtl;
9596 case INTEGER_CST:
9597 /* Given that TYPE_PRECISION (type) is not always equal to
9598 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9599 the former to the latter according to the signedness of the
9600 type. */
9601 temp = immed_wide_int_const (wide_int::from
9602 (exp,
9603 GET_MODE_PRECISION (TYPE_MODE (type)),
9604 TYPE_SIGN (type)),
9605 TYPE_MODE (type));
9606 return temp;
9608 case VECTOR_CST:
9610 tree tmp = NULL_TREE;
9611 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9612 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9613 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9614 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9615 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9616 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9617 return const_vector_from_tree (exp);
9618 if (GET_MODE_CLASS (mode) == MODE_INT)
9620 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9621 if (type_for_mode)
9622 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9624 if (!tmp)
9626 vec<constructor_elt, va_gc> *v;
9627 unsigned i;
9628 vec_alloc (v, VECTOR_CST_NELTS (exp));
9629 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9630 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9631 tmp = build_constructor (type, v);
9633 return expand_expr (tmp, ignore ? const0_rtx : target,
9634 tmode, modifier);
9637 case CONST_DECL:
9638 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9640 case REAL_CST:
9641 /* If optimized, generate immediate CONST_DOUBLE
9642 which will be turned into memory by reload if necessary.
9644 We used to force a register so that loop.c could see it. But
9645 this does not allow gen_* patterns to perform optimizations with
9646 the constants. It also produces two insns in cases like "x = 1.0;".
9647 On most machines, floating-point constants are not permitted in
9648 many insns, so we'd end up copying it to a register in any case.
9650 Now, we do the copying in expand_binop, if appropriate. */
9651 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9652 TYPE_MODE (TREE_TYPE (exp)));
9654 case FIXED_CST:
9655 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9656 TYPE_MODE (TREE_TYPE (exp)));
9658 case COMPLEX_CST:
9659 /* Handle evaluating a complex constant in a CONCAT target. */
9660 if (original_target && GET_CODE (original_target) == CONCAT)
9662 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9663 rtx rtarg, itarg;
9665 rtarg = XEXP (original_target, 0);
9666 itarg = XEXP (original_target, 1);
9668 /* Move the real and imaginary parts separately. */
9669 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9670 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9672 if (op0 != rtarg)
9673 emit_move_insn (rtarg, op0);
9674 if (op1 != itarg)
9675 emit_move_insn (itarg, op1);
9677 return original_target;
9680 /* ... fall through ... */
9682 case STRING_CST:
9683 temp = expand_expr_constant (exp, 1, modifier);
9685 /* temp contains a constant address.
9686 On RISC machines where a constant address isn't valid,
9687 make some insns to get that address into a register. */
9688 if (modifier != EXPAND_CONST_ADDRESS
9689 && modifier != EXPAND_INITIALIZER
9690 && modifier != EXPAND_SUM
9691 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9692 MEM_ADDR_SPACE (temp)))
9693 return replace_equiv_address (temp,
9694 copy_rtx (XEXP (temp, 0)));
9695 return temp;
9697 case SAVE_EXPR:
9699 tree val = treeop0;
9700 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9701 inner_reference_p);
9703 if (!SAVE_EXPR_RESOLVED_P (exp))
9705 /* We can indeed still hit this case, typically via builtin
9706 expanders calling save_expr immediately before expanding
9707 something. Assume this means that we only have to deal
9708 with non-BLKmode values. */
9709 gcc_assert (GET_MODE (ret) != BLKmode);
9711 val = build_decl (curr_insn_location (),
9712 VAR_DECL, NULL, TREE_TYPE (exp));
9713 DECL_ARTIFICIAL (val) = 1;
9714 DECL_IGNORED_P (val) = 1;
9715 treeop0 = val;
9716 TREE_OPERAND (exp, 0) = treeop0;
9717 SAVE_EXPR_RESOLVED_P (exp) = 1;
9719 if (!CONSTANT_P (ret))
9720 ret = copy_to_reg (ret);
9721 SET_DECL_RTL (val, ret);
9724 return ret;
9728 case CONSTRUCTOR:
9729 /* If we don't need the result, just ensure we evaluate any
9730 subexpressions. */
9731 if (ignore)
9733 unsigned HOST_WIDE_INT idx;
9734 tree value;
9736 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9737 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9739 return const0_rtx;
9742 return expand_constructor (exp, target, modifier, false);
9744 case TARGET_MEM_REF:
9746 addr_space_t as
9747 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9748 enum insn_code icode;
9749 unsigned int align;
9751 op0 = addr_for_mem_ref (exp, as, true);
9752 op0 = memory_address_addr_space (mode, op0, as);
9753 temp = gen_rtx_MEM (mode, op0);
9754 set_mem_attributes (temp, exp, 0);
9755 set_mem_addr_space (temp, as);
9756 align = get_object_alignment (exp);
9757 if (modifier != EXPAND_WRITE
9758 && modifier != EXPAND_MEMORY
9759 && mode != BLKmode
9760 && align < GET_MODE_ALIGNMENT (mode)
9761 /* If the target does not have special handling for unaligned
9762 loads of mode then it can use regular moves for them. */
9763 && ((icode = optab_handler (movmisalign_optab, mode))
9764 != CODE_FOR_nothing))
9766 struct expand_operand ops[2];
9768 /* We've already validated the memory, and we're creating a
9769 new pseudo destination. The predicates really can't fail,
9770 nor can the generator. */
9771 create_output_operand (&ops[0], NULL_RTX, mode);
9772 create_fixed_operand (&ops[1], temp);
9773 expand_insn (icode, 2, ops);
9774 temp = ops[0].value;
9776 return temp;
9779 case MEM_REF:
9781 addr_space_t as
9782 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9783 machine_mode address_mode;
9784 tree base = TREE_OPERAND (exp, 0);
9785 gimple def_stmt;
9786 enum insn_code icode;
9787 unsigned align;
9788 /* Handle expansion of non-aliased memory with non-BLKmode. That
9789 might end up in a register. */
9790 if (mem_ref_refers_to_non_mem_p (exp))
9792 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9793 base = TREE_OPERAND (base, 0);
9794 if (offset == 0
9795 && tree_fits_uhwi_p (TYPE_SIZE (type))
9796 && (GET_MODE_BITSIZE (DECL_MODE (base))
9797 == tree_to_uhwi (TYPE_SIZE (type))))
9798 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9799 target, tmode, modifier);
9800 if (TYPE_MODE (type) == BLKmode)
9802 temp = assign_stack_temp (DECL_MODE (base),
9803 GET_MODE_SIZE (DECL_MODE (base)));
9804 store_expr (base, temp, 0, false);
9805 temp = adjust_address (temp, BLKmode, offset);
9806 set_mem_size (temp, int_size_in_bytes (type));
9807 return temp;
9809 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9810 bitsize_int (offset * BITS_PER_UNIT));
9811 return expand_expr (exp, target, tmode, modifier);
9813 address_mode = targetm.addr_space.address_mode (as);
9814 base = TREE_OPERAND (exp, 0);
9815 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9817 tree mask = gimple_assign_rhs2 (def_stmt);
9818 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9819 gimple_assign_rhs1 (def_stmt), mask);
9820 TREE_OPERAND (exp, 0) = base;
9822 align = get_object_alignment (exp);
9823 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9824 op0 = memory_address_addr_space (mode, op0, as);
9825 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9827 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9828 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9829 op0 = memory_address_addr_space (mode, op0, as);
9831 temp = gen_rtx_MEM (mode, op0);
9832 set_mem_attributes (temp, exp, 0);
9833 set_mem_addr_space (temp, as);
9834 if (TREE_THIS_VOLATILE (exp))
9835 MEM_VOLATILE_P (temp) = 1;
9836 if (modifier != EXPAND_WRITE
9837 && modifier != EXPAND_MEMORY
9838 && !inner_reference_p
9839 && mode != BLKmode
9840 && align < GET_MODE_ALIGNMENT (mode))
9842 if ((icode = optab_handler (movmisalign_optab, mode))
9843 != CODE_FOR_nothing)
9845 struct expand_operand ops[2];
9847 /* We've already validated the memory, and we're creating a
9848 new pseudo destination. The predicates really can't fail,
9849 nor can the generator. */
9850 create_output_operand (&ops[0], NULL_RTX, mode);
9851 create_fixed_operand (&ops[1], temp);
9852 expand_insn (icode, 2, ops);
9853 temp = ops[0].value;
9855 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9856 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9857 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9858 (modifier == EXPAND_STACK_PARM
9859 ? NULL_RTX : target),
9860 mode, mode);
9862 return temp;
9865 case ARRAY_REF:
9868 tree array = treeop0;
9869 tree index = treeop1;
9870 tree init;
9872 /* Fold an expression like: "foo"[2].
9873 This is not done in fold so it won't happen inside &.
9874 Don't fold if this is for wide characters since it's too
9875 difficult to do correctly and this is a very rare case. */
9877 if (modifier != EXPAND_CONST_ADDRESS
9878 && modifier != EXPAND_INITIALIZER
9879 && modifier != EXPAND_MEMORY)
9881 tree t = fold_read_from_constant_string (exp);
9883 if (t)
9884 return expand_expr (t, target, tmode, modifier);
9887 /* If this is a constant index into a constant array,
9888 just get the value from the array. Handle both the cases when
9889 we have an explicit constructor and when our operand is a variable
9890 that was declared const. */
9892 if (modifier != EXPAND_CONST_ADDRESS
9893 && modifier != EXPAND_INITIALIZER
9894 && modifier != EXPAND_MEMORY
9895 && TREE_CODE (array) == CONSTRUCTOR
9896 && ! TREE_SIDE_EFFECTS (array)
9897 && TREE_CODE (index) == INTEGER_CST)
9899 unsigned HOST_WIDE_INT ix;
9900 tree field, value;
9902 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9903 field, value)
9904 if (tree_int_cst_equal (field, index))
9906 if (!TREE_SIDE_EFFECTS (value))
9907 return expand_expr (fold (value), target, tmode, modifier);
9908 break;
9912 else if (optimize >= 1
9913 && modifier != EXPAND_CONST_ADDRESS
9914 && modifier != EXPAND_INITIALIZER
9915 && modifier != EXPAND_MEMORY
9916 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9917 && TREE_CODE (index) == INTEGER_CST
9918 && (TREE_CODE (array) == VAR_DECL
9919 || TREE_CODE (array) == CONST_DECL)
9920 && (init = ctor_for_folding (array)) != error_mark_node)
9922 if (init == NULL_TREE)
9924 tree value = build_zero_cst (type);
9925 if (TREE_CODE (value) == CONSTRUCTOR)
9927 /* If VALUE is a CONSTRUCTOR, this optimization is only
9928 useful if this doesn't store the CONSTRUCTOR into
9929 memory. If it does, it is more efficient to just
9930 load the data from the array directly. */
9931 rtx ret = expand_constructor (value, target,
9932 modifier, true);
9933 if (ret == NULL_RTX)
9934 value = NULL_TREE;
9937 if (value)
9938 return expand_expr (value, target, tmode, modifier);
9940 else if (TREE_CODE (init) == CONSTRUCTOR)
9942 unsigned HOST_WIDE_INT ix;
9943 tree field, value;
9945 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9946 field, value)
9947 if (tree_int_cst_equal (field, index))
9949 if (TREE_SIDE_EFFECTS (value))
9950 break;
9952 if (TREE_CODE (value) == CONSTRUCTOR)
9954 /* If VALUE is a CONSTRUCTOR, this
9955 optimization is only useful if
9956 this doesn't store the CONSTRUCTOR
9957 into memory. If it does, it is more
9958 efficient to just load the data from
9959 the array directly. */
9960 rtx ret = expand_constructor (value, target,
9961 modifier, true);
9962 if (ret == NULL_RTX)
9963 break;
9966 return
9967 expand_expr (fold (value), target, tmode, modifier);
9970 else if (TREE_CODE (init) == STRING_CST)
9972 tree low_bound = array_ref_low_bound (exp);
9973 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
9975 /* Optimize the special case of a zero lower bound.
9977 We convert the lower bound to sizetype to avoid problems
9978 with constant folding. E.g. suppose the lower bound is
9979 1 and its mode is QI. Without the conversion
9980 (ARRAY + (INDEX - (unsigned char)1))
9981 becomes
9982 (ARRAY + (-(unsigned char)1) + INDEX)
9983 which becomes
9984 (ARRAY + 255 + INDEX). Oops! */
9985 if (!integer_zerop (low_bound))
9986 index1 = size_diffop_loc (loc, index1,
9987 fold_convert_loc (loc, sizetype,
9988 low_bound));
9990 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
9992 tree type = TREE_TYPE (TREE_TYPE (init));
9993 machine_mode mode = TYPE_MODE (type);
9995 if (GET_MODE_CLASS (mode) == MODE_INT
9996 && GET_MODE_SIZE (mode) == 1)
9997 return gen_int_mode (TREE_STRING_POINTER (init)
9998 [TREE_INT_CST_LOW (index1)],
9999 mode);
10004 goto normal_inner_ref;
10006 case COMPONENT_REF:
10007 /* If the operand is a CONSTRUCTOR, we can just extract the
10008 appropriate field if it is present. */
10009 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10011 unsigned HOST_WIDE_INT idx;
10012 tree field, value;
10014 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10015 idx, field, value)
10016 if (field == treeop1
10017 /* We can normally use the value of the field in the
10018 CONSTRUCTOR. However, if this is a bitfield in
10019 an integral mode that we can fit in a HOST_WIDE_INT,
10020 we must mask only the number of bits in the bitfield,
10021 since this is done implicitly by the constructor. If
10022 the bitfield does not meet either of those conditions,
10023 we can't do this optimization. */
10024 && (! DECL_BIT_FIELD (field)
10025 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10026 && (GET_MODE_PRECISION (DECL_MODE (field))
10027 <= HOST_BITS_PER_WIDE_INT))))
10029 if (DECL_BIT_FIELD (field)
10030 && modifier == EXPAND_STACK_PARM)
10031 target = 0;
10032 op0 = expand_expr (value, target, tmode, modifier);
10033 if (DECL_BIT_FIELD (field))
10035 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10036 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10038 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10040 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10041 imode);
10042 op0 = expand_and (imode, op0, op1, target);
10044 else
10046 int count = GET_MODE_PRECISION (imode) - bitsize;
10048 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10049 target, 0);
10050 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10051 target, 0);
10055 return op0;
10058 goto normal_inner_ref;
10060 case BIT_FIELD_REF:
10061 case ARRAY_RANGE_REF:
10062 normal_inner_ref:
10064 machine_mode mode1, mode2;
10065 HOST_WIDE_INT bitsize, bitpos;
10066 tree offset;
10067 int volatilep = 0, must_force_mem;
10068 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10069 &mode1, &unsignedp, &volatilep, true);
10070 rtx orig_op0, memloc;
10071 bool clear_mem_expr = false;
10073 /* If we got back the original object, something is wrong. Perhaps
10074 we are evaluating an expression too early. In any event, don't
10075 infinitely recurse. */
10076 gcc_assert (tem != exp);
10078 /* If TEM's type is a union of variable size, pass TARGET to the inner
10079 computation, since it will need a temporary and TARGET is known
10080 to have to do. This occurs in unchecked conversion in Ada. */
10081 orig_op0 = op0
10082 = expand_expr_real (tem,
10083 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10084 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10085 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10086 != INTEGER_CST)
10087 && modifier != EXPAND_STACK_PARM
10088 ? target : NULL_RTX),
10089 VOIDmode,
10090 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10091 NULL, true);
10093 /* If the field has a mode, we want to access it in the
10094 field's mode, not the computed mode.
10095 If a MEM has VOIDmode (external with incomplete type),
10096 use BLKmode for it instead. */
10097 if (MEM_P (op0))
10099 if (mode1 != VOIDmode)
10100 op0 = adjust_address (op0, mode1, 0);
10101 else if (GET_MODE (op0) == VOIDmode)
10102 op0 = adjust_address (op0, BLKmode, 0);
10105 mode2
10106 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10108 /* If we have either an offset, a BLKmode result, or a reference
10109 outside the underlying object, we must force it to memory.
10110 Such a case can occur in Ada if we have unchecked conversion
10111 of an expression from a scalar type to an aggregate type or
10112 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10113 passed a partially uninitialized object or a view-conversion
10114 to a larger size. */
10115 must_force_mem = (offset
10116 || mode1 == BLKmode
10117 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10119 /* Handle CONCAT first. */
10120 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10122 if (bitpos == 0
10123 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10124 return op0;
10125 if (bitpos == 0
10126 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10127 && bitsize)
10129 op0 = XEXP (op0, 0);
10130 mode2 = GET_MODE (op0);
10132 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10133 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10134 && bitpos
10135 && bitsize)
10137 op0 = XEXP (op0, 1);
10138 bitpos = 0;
10139 mode2 = GET_MODE (op0);
10141 else
10142 /* Otherwise force into memory. */
10143 must_force_mem = 1;
10146 /* If this is a constant, put it in a register if it is a legitimate
10147 constant and we don't need a memory reference. */
10148 if (CONSTANT_P (op0)
10149 && mode2 != BLKmode
10150 && targetm.legitimate_constant_p (mode2, op0)
10151 && !must_force_mem)
10152 op0 = force_reg (mode2, op0);
10154 /* Otherwise, if this is a constant, try to force it to the constant
10155 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10156 is a legitimate constant. */
10157 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10158 op0 = validize_mem (memloc);
10160 /* Otherwise, if this is a constant or the object is not in memory
10161 and need be, put it there. */
10162 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10164 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10165 emit_move_insn (memloc, op0);
10166 op0 = memloc;
10167 clear_mem_expr = true;
10170 if (offset)
10172 machine_mode address_mode;
10173 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10174 EXPAND_SUM);
10176 gcc_assert (MEM_P (op0));
10178 address_mode = get_address_mode (op0);
10179 if (GET_MODE (offset_rtx) != address_mode)
10181 /* We cannot be sure that the RTL in offset_rtx is valid outside
10182 of a memory address context, so force it into a register
10183 before attempting to convert it to the desired mode. */
10184 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10185 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10188 /* See the comment in expand_assignment for the rationale. */
10189 if (mode1 != VOIDmode
10190 && bitpos != 0
10191 && bitsize > 0
10192 && (bitpos % bitsize) == 0
10193 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10194 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10196 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10197 bitpos = 0;
10200 op0 = offset_address (op0, offset_rtx,
10201 highest_pow2_factor (offset));
10204 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10205 record its alignment as BIGGEST_ALIGNMENT. */
10206 if (MEM_P (op0) && bitpos == 0 && offset != 0
10207 && is_aligning_offset (offset, tem))
10208 set_mem_align (op0, BIGGEST_ALIGNMENT);
10210 /* Don't forget about volatility even if this is a bitfield. */
10211 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10213 if (op0 == orig_op0)
10214 op0 = copy_rtx (op0);
10216 MEM_VOLATILE_P (op0) = 1;
10219 /* In cases where an aligned union has an unaligned object
10220 as a field, we might be extracting a BLKmode value from
10221 an integer-mode (e.g., SImode) object. Handle this case
10222 by doing the extract into an object as wide as the field
10223 (which we know to be the width of a basic mode), then
10224 storing into memory, and changing the mode to BLKmode. */
10225 if (mode1 == VOIDmode
10226 || REG_P (op0) || GET_CODE (op0) == SUBREG
10227 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10228 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10229 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10230 && modifier != EXPAND_CONST_ADDRESS
10231 && modifier != EXPAND_INITIALIZER
10232 && modifier != EXPAND_MEMORY)
10233 /* If the bitfield is volatile and the bitsize
10234 is narrower than the access size of the bitfield,
10235 we need to extract bitfields from the access. */
10236 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10237 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10238 && mode1 != BLKmode
10239 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10240 /* If the field isn't aligned enough to fetch as a memref,
10241 fetch it as a bit field. */
10242 || (mode1 != BLKmode
10243 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10244 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10245 || (MEM_P (op0)
10246 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10247 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10248 && modifier != EXPAND_MEMORY
10249 && ((modifier == EXPAND_CONST_ADDRESS
10250 || modifier == EXPAND_INITIALIZER)
10251 ? STRICT_ALIGNMENT
10252 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10253 || (bitpos % BITS_PER_UNIT != 0)))
10254 /* If the type and the field are a constant size and the
10255 size of the type isn't the same size as the bitfield,
10256 we must use bitfield operations. */
10257 || (bitsize >= 0
10258 && TYPE_SIZE (TREE_TYPE (exp))
10259 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10260 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10261 bitsize)))
10263 machine_mode ext_mode = mode;
10265 if (ext_mode == BLKmode
10266 && ! (target != 0 && MEM_P (op0)
10267 && MEM_P (target)
10268 && bitpos % BITS_PER_UNIT == 0))
10269 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10271 if (ext_mode == BLKmode)
10273 if (target == 0)
10274 target = assign_temp (type, 1, 1);
10276 /* ??? Unlike the similar test a few lines below, this one is
10277 very likely obsolete. */
10278 if (bitsize == 0)
10279 return target;
10281 /* In this case, BITPOS must start at a byte boundary and
10282 TARGET, if specified, must be a MEM. */
10283 gcc_assert (MEM_P (op0)
10284 && (!target || MEM_P (target))
10285 && !(bitpos % BITS_PER_UNIT));
10287 emit_block_move (target,
10288 adjust_address (op0, VOIDmode,
10289 bitpos / BITS_PER_UNIT),
10290 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10291 / BITS_PER_UNIT),
10292 (modifier == EXPAND_STACK_PARM
10293 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10295 return target;
10298 /* If we have nothing to extract, the result will be 0 for targets
10299 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10300 return 0 for the sake of consistency, as reading a zero-sized
10301 bitfield is valid in Ada and the value is fully specified. */
10302 if (bitsize == 0)
10303 return const0_rtx;
10305 op0 = validize_mem (op0);
10307 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10308 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10310 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10311 (modifier == EXPAND_STACK_PARM
10312 ? NULL_RTX : target),
10313 ext_mode, ext_mode);
10315 /* If the result is a record type and BITSIZE is narrower than
10316 the mode of OP0, an integral mode, and this is a big endian
10317 machine, we must put the field into the high-order bits. */
10318 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10319 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10320 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10321 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10322 GET_MODE_BITSIZE (GET_MODE (op0))
10323 - bitsize, op0, 1);
10325 /* If the result type is BLKmode, store the data into a temporary
10326 of the appropriate type, but with the mode corresponding to the
10327 mode for the data we have (op0's mode). */
10328 if (mode == BLKmode)
10330 rtx new_rtx
10331 = assign_stack_temp_for_type (ext_mode,
10332 GET_MODE_BITSIZE (ext_mode),
10333 type);
10334 emit_move_insn (new_rtx, op0);
10335 op0 = copy_rtx (new_rtx);
10336 PUT_MODE (op0, BLKmode);
10339 return op0;
10342 /* If the result is BLKmode, use that to access the object
10343 now as well. */
10344 if (mode == BLKmode)
10345 mode1 = BLKmode;
10347 /* Get a reference to just this component. */
10348 if (modifier == EXPAND_CONST_ADDRESS
10349 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10350 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10351 else
10352 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10354 if (op0 == orig_op0)
10355 op0 = copy_rtx (op0);
10357 set_mem_attributes (op0, exp, 0);
10359 if (REG_P (XEXP (op0, 0)))
10360 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10362 /* If op0 is a temporary because the original expressions was forced
10363 to memory, clear MEM_EXPR so that the original expression cannot
10364 be marked as addressable through MEM_EXPR of the temporary. */
10365 if (clear_mem_expr)
10366 set_mem_expr (op0, NULL_TREE);
10368 MEM_VOLATILE_P (op0) |= volatilep;
10369 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10370 || modifier == EXPAND_CONST_ADDRESS
10371 || modifier == EXPAND_INITIALIZER)
10372 return op0;
10374 if (target == 0)
10375 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10377 convert_move (target, op0, unsignedp);
10378 return target;
10381 case OBJ_TYPE_REF:
10382 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10384 case CALL_EXPR:
10385 /* All valid uses of __builtin_va_arg_pack () are removed during
10386 inlining. */
10387 if (CALL_EXPR_VA_ARG_PACK (exp))
10388 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10390 tree fndecl = get_callee_fndecl (exp), attr;
10392 if (fndecl
10393 && (attr = lookup_attribute ("error",
10394 DECL_ATTRIBUTES (fndecl))) != NULL)
10395 error ("%Kcall to %qs declared with attribute error: %s",
10396 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10397 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10398 if (fndecl
10399 && (attr = lookup_attribute ("warning",
10400 DECL_ATTRIBUTES (fndecl))) != NULL)
10401 warning_at (tree_nonartificial_location (exp),
10402 0, "%Kcall to %qs declared with attribute warning: %s",
10403 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10404 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10406 /* Check for a built-in function. */
10407 if (fndecl && DECL_BUILT_IN (fndecl))
10409 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10410 if (CALL_WITH_BOUNDS_P (exp))
10411 return expand_builtin_with_bounds (exp, target, subtarget,
10412 tmode, ignore);
10413 else
10414 return expand_builtin (exp, target, subtarget, tmode, ignore);
10417 return expand_call (exp, target, ignore);
10419 case VIEW_CONVERT_EXPR:
10420 op0 = NULL_RTX;
10422 /* If we are converting to BLKmode, try to avoid an intermediate
10423 temporary by fetching an inner memory reference. */
10424 if (mode == BLKmode
10425 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10426 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10427 && handled_component_p (treeop0))
10429 machine_mode mode1;
10430 HOST_WIDE_INT bitsize, bitpos;
10431 tree offset;
10432 int unsignedp;
10433 int volatilep = 0;
10434 tree tem
10435 = get_inner_reference (treeop0, &bitsize, &bitpos,
10436 &offset, &mode1, &unsignedp, &volatilep,
10437 true);
10438 rtx orig_op0;
10440 /* ??? We should work harder and deal with non-zero offsets. */
10441 if (!offset
10442 && (bitpos % BITS_PER_UNIT) == 0
10443 && bitsize >= 0
10444 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10446 /* See the normal_inner_ref case for the rationale. */
10447 orig_op0
10448 = expand_expr_real (tem,
10449 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10450 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10451 != INTEGER_CST)
10452 && modifier != EXPAND_STACK_PARM
10453 ? target : NULL_RTX),
10454 VOIDmode,
10455 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10456 NULL, true);
10458 if (MEM_P (orig_op0))
10460 op0 = orig_op0;
10462 /* Get a reference to just this component. */
10463 if (modifier == EXPAND_CONST_ADDRESS
10464 || modifier == EXPAND_SUM
10465 || modifier == EXPAND_INITIALIZER)
10466 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10467 else
10468 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10470 if (op0 == orig_op0)
10471 op0 = copy_rtx (op0);
10473 set_mem_attributes (op0, treeop0, 0);
10474 if (REG_P (XEXP (op0, 0)))
10475 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10477 MEM_VOLATILE_P (op0) |= volatilep;
10482 if (!op0)
10483 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10484 NULL, inner_reference_p);
10486 /* If the input and output modes are both the same, we are done. */
10487 if (mode == GET_MODE (op0))
10489 /* If neither mode is BLKmode, and both modes are the same size
10490 then we can use gen_lowpart. */
10491 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10492 && (GET_MODE_PRECISION (mode)
10493 == GET_MODE_PRECISION (GET_MODE (op0)))
10494 && !COMPLEX_MODE_P (GET_MODE (op0)))
10496 if (GET_CODE (op0) == SUBREG)
10497 op0 = force_reg (GET_MODE (op0), op0);
10498 temp = gen_lowpart_common (mode, op0);
10499 if (temp)
10500 op0 = temp;
10501 else
10503 if (!REG_P (op0) && !MEM_P (op0))
10504 op0 = force_reg (GET_MODE (op0), op0);
10505 op0 = gen_lowpart (mode, op0);
10508 /* If both types are integral, convert from one mode to the other. */
10509 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10510 op0 = convert_modes (mode, GET_MODE (op0), op0,
10511 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10512 /* If the output type is a bit-field type, do an extraction. */
10513 else if (reduce_bit_field)
10514 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10515 TYPE_UNSIGNED (type), NULL_RTX,
10516 mode, mode);
10517 /* As a last resort, spill op0 to memory, and reload it in a
10518 different mode. */
10519 else if (!MEM_P (op0))
10521 /* If the operand is not a MEM, force it into memory. Since we
10522 are going to be changing the mode of the MEM, don't call
10523 force_const_mem for constants because we don't allow pool
10524 constants to change mode. */
10525 tree inner_type = TREE_TYPE (treeop0);
10527 gcc_assert (!TREE_ADDRESSABLE (exp));
10529 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10530 target
10531 = assign_stack_temp_for_type
10532 (TYPE_MODE (inner_type),
10533 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10535 emit_move_insn (target, op0);
10536 op0 = target;
10539 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10540 output type is such that the operand is known to be aligned, indicate
10541 that it is. Otherwise, we need only be concerned about alignment for
10542 non-BLKmode results. */
10543 if (MEM_P (op0))
10545 enum insn_code icode;
10547 if (TYPE_ALIGN_OK (type))
10549 /* ??? Copying the MEM without substantially changing it might
10550 run afoul of the code handling volatile memory references in
10551 store_expr, which assumes that TARGET is returned unmodified
10552 if it has been used. */
10553 op0 = copy_rtx (op0);
10554 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10556 else if (modifier != EXPAND_WRITE
10557 && modifier != EXPAND_MEMORY
10558 && !inner_reference_p
10559 && mode != BLKmode
10560 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10562 /* If the target does have special handling for unaligned
10563 loads of mode then use them. */
10564 if ((icode = optab_handler (movmisalign_optab, mode))
10565 != CODE_FOR_nothing)
10567 rtx reg;
10569 op0 = adjust_address (op0, mode, 0);
10570 /* We've already validated the memory, and we're creating a
10571 new pseudo destination. The predicates really can't
10572 fail. */
10573 reg = gen_reg_rtx (mode);
10575 /* Nor can the insn generator. */
10576 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10577 emit_insn (insn);
10578 return reg;
10580 else if (STRICT_ALIGNMENT)
10582 tree inner_type = TREE_TYPE (treeop0);
10583 HOST_WIDE_INT temp_size
10584 = MAX (int_size_in_bytes (inner_type),
10585 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10586 rtx new_rtx
10587 = assign_stack_temp_for_type (mode, temp_size, type);
10588 rtx new_with_op0_mode
10589 = adjust_address (new_rtx, GET_MODE (op0), 0);
10591 gcc_assert (!TREE_ADDRESSABLE (exp));
10593 if (GET_MODE (op0) == BLKmode)
10594 emit_block_move (new_with_op0_mode, op0,
10595 GEN_INT (GET_MODE_SIZE (mode)),
10596 (modifier == EXPAND_STACK_PARM
10597 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10598 else
10599 emit_move_insn (new_with_op0_mode, op0);
10601 op0 = new_rtx;
10605 op0 = adjust_address (op0, mode, 0);
10608 return op0;
10610 case MODIFY_EXPR:
10612 tree lhs = treeop0;
10613 tree rhs = treeop1;
10614 gcc_assert (ignore);
10616 /* Check for |= or &= of a bitfield of size one into another bitfield
10617 of size 1. In this case, (unless we need the result of the
10618 assignment) we can do this more efficiently with a
10619 test followed by an assignment, if necessary.
10621 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10622 things change so we do, this code should be enhanced to
10623 support it. */
10624 if (TREE_CODE (lhs) == COMPONENT_REF
10625 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10626 || TREE_CODE (rhs) == BIT_AND_EXPR)
10627 && TREE_OPERAND (rhs, 0) == lhs
10628 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10629 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10630 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10632 rtx_code_label *label = gen_label_rtx ();
10633 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10634 do_jump (TREE_OPERAND (rhs, 1),
10635 value ? label : 0,
10636 value ? 0 : label, -1);
10637 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10638 false);
10639 do_pending_stack_adjust ();
10640 emit_label (label);
10641 return const0_rtx;
10644 expand_assignment (lhs, rhs, false);
10645 return const0_rtx;
10648 case ADDR_EXPR:
10649 return expand_expr_addr_expr (exp, target, tmode, modifier);
10651 case REALPART_EXPR:
10652 op0 = expand_normal (treeop0);
10653 return read_complex_part (op0, false);
10655 case IMAGPART_EXPR:
10656 op0 = expand_normal (treeop0);
10657 return read_complex_part (op0, true);
10659 case RETURN_EXPR:
10660 case LABEL_EXPR:
10661 case GOTO_EXPR:
10662 case SWITCH_EXPR:
10663 case ASM_EXPR:
10664 /* Expanded in cfgexpand.c. */
10665 gcc_unreachable ();
10667 case TRY_CATCH_EXPR:
10668 case CATCH_EXPR:
10669 case EH_FILTER_EXPR:
10670 case TRY_FINALLY_EXPR:
10671 /* Lowered by tree-eh.c. */
10672 gcc_unreachable ();
10674 case WITH_CLEANUP_EXPR:
10675 case CLEANUP_POINT_EXPR:
10676 case TARGET_EXPR:
10677 case CASE_LABEL_EXPR:
10678 case VA_ARG_EXPR:
10679 case BIND_EXPR:
10680 case INIT_EXPR:
10681 case CONJ_EXPR:
10682 case COMPOUND_EXPR:
10683 case PREINCREMENT_EXPR:
10684 case PREDECREMENT_EXPR:
10685 case POSTINCREMENT_EXPR:
10686 case POSTDECREMENT_EXPR:
10687 case LOOP_EXPR:
10688 case EXIT_EXPR:
10689 case COMPOUND_LITERAL_EXPR:
10690 /* Lowered by gimplify.c. */
10691 gcc_unreachable ();
10693 case FDESC_EXPR:
10694 /* Function descriptors are not valid except for as
10695 initialization constants, and should not be expanded. */
10696 gcc_unreachable ();
10698 case WITH_SIZE_EXPR:
10699 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10700 have pulled out the size to use in whatever context it needed. */
10701 return expand_expr_real (treeop0, original_target, tmode,
10702 modifier, alt_rtl, inner_reference_p);
10704 default:
10705 return expand_expr_real_2 (&ops, target, tmode, modifier);
10709 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10710 signedness of TYPE), possibly returning the result in TARGET. */
10711 static rtx
10712 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10714 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10715 if (target && GET_MODE (target) != GET_MODE (exp))
10716 target = 0;
10717 /* For constant values, reduce using build_int_cst_type. */
10718 if (CONST_INT_P (exp))
10720 HOST_WIDE_INT value = INTVAL (exp);
10721 tree t = build_int_cst_type (type, value);
10722 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10724 else if (TYPE_UNSIGNED (type))
10726 machine_mode mode = GET_MODE (exp);
10727 rtx mask = immed_wide_int_const
10728 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10729 return expand_and (mode, exp, mask, target);
10731 else
10733 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10734 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10735 exp, count, target, 0);
10736 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10737 exp, count, target, 0);
10741 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10742 when applied to the address of EXP produces an address known to be
10743 aligned more than BIGGEST_ALIGNMENT. */
10745 static int
10746 is_aligning_offset (const_tree offset, const_tree exp)
10748 /* Strip off any conversions. */
10749 while (CONVERT_EXPR_P (offset))
10750 offset = TREE_OPERAND (offset, 0);
10752 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10753 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10754 if (TREE_CODE (offset) != BIT_AND_EXPR
10755 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10756 || compare_tree_int (TREE_OPERAND (offset, 1),
10757 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10758 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10759 return 0;
10761 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10762 It must be NEGATE_EXPR. Then strip any more conversions. */
10763 offset = TREE_OPERAND (offset, 0);
10764 while (CONVERT_EXPR_P (offset))
10765 offset = TREE_OPERAND (offset, 0);
10767 if (TREE_CODE (offset) != NEGATE_EXPR)
10768 return 0;
10770 offset = TREE_OPERAND (offset, 0);
10771 while (CONVERT_EXPR_P (offset))
10772 offset = TREE_OPERAND (offset, 0);
10774 /* This must now be the address of EXP. */
10775 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10778 /* Return the tree node if an ARG corresponds to a string constant or zero
10779 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10780 in bytes within the string that ARG is accessing. The type of the
10781 offset will be `sizetype'. */
10783 tree
10784 string_constant (tree arg, tree *ptr_offset)
10786 tree array, offset, lower_bound;
10787 STRIP_NOPS (arg);
10789 if (TREE_CODE (arg) == ADDR_EXPR)
10791 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10793 *ptr_offset = size_zero_node;
10794 return TREE_OPERAND (arg, 0);
10796 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10798 array = TREE_OPERAND (arg, 0);
10799 offset = size_zero_node;
10801 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10803 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10804 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10805 if (TREE_CODE (array) != STRING_CST
10806 && TREE_CODE (array) != VAR_DECL)
10807 return 0;
10809 /* Check if the array has a nonzero lower bound. */
10810 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10811 if (!integer_zerop (lower_bound))
10813 /* If the offset and base aren't both constants, return 0. */
10814 if (TREE_CODE (lower_bound) != INTEGER_CST)
10815 return 0;
10816 if (TREE_CODE (offset) != INTEGER_CST)
10817 return 0;
10818 /* Adjust offset by the lower bound. */
10819 offset = size_diffop (fold_convert (sizetype, offset),
10820 fold_convert (sizetype, lower_bound));
10823 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10825 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10826 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10827 if (TREE_CODE (array) != ADDR_EXPR)
10828 return 0;
10829 array = TREE_OPERAND (array, 0);
10830 if (TREE_CODE (array) != STRING_CST
10831 && TREE_CODE (array) != VAR_DECL)
10832 return 0;
10834 else
10835 return 0;
10837 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10839 tree arg0 = TREE_OPERAND (arg, 0);
10840 tree arg1 = TREE_OPERAND (arg, 1);
10842 STRIP_NOPS (arg0);
10843 STRIP_NOPS (arg1);
10845 if (TREE_CODE (arg0) == ADDR_EXPR
10846 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10847 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10849 array = TREE_OPERAND (arg0, 0);
10850 offset = arg1;
10852 else if (TREE_CODE (arg1) == ADDR_EXPR
10853 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10854 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10856 array = TREE_OPERAND (arg1, 0);
10857 offset = arg0;
10859 else
10860 return 0;
10862 else
10863 return 0;
10865 if (TREE_CODE (array) == STRING_CST)
10867 *ptr_offset = fold_convert (sizetype, offset);
10868 return array;
10870 else if (TREE_CODE (array) == VAR_DECL
10871 || TREE_CODE (array) == CONST_DECL)
10873 int length;
10874 tree init = ctor_for_folding (array);
10876 /* Variables initialized to string literals can be handled too. */
10877 if (init == error_mark_node
10878 || !init
10879 || TREE_CODE (init) != STRING_CST)
10880 return 0;
10882 /* Avoid const char foo[4] = "abcde"; */
10883 if (DECL_SIZE_UNIT (array) == NULL_TREE
10884 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10885 || (length = TREE_STRING_LENGTH (init)) <= 0
10886 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10887 return 0;
10889 /* If variable is bigger than the string literal, OFFSET must be constant
10890 and inside of the bounds of the string literal. */
10891 offset = fold_convert (sizetype, offset);
10892 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10893 && (! tree_fits_uhwi_p (offset)
10894 || compare_tree_int (offset, length) >= 0))
10895 return 0;
10897 *ptr_offset = offset;
10898 return init;
10901 return 0;
10904 /* Generate code to calculate OPS, and exploded expression
10905 using a store-flag instruction and return an rtx for the result.
10906 OPS reflects a comparison.
10908 If TARGET is nonzero, store the result there if convenient.
10910 Return zero if there is no suitable set-flag instruction
10911 available on this machine.
10913 Once expand_expr has been called on the arguments of the comparison,
10914 we are committed to doing the store flag, since it is not safe to
10915 re-evaluate the expression. We emit the store-flag insn by calling
10916 emit_store_flag, but only expand the arguments if we have a reason
10917 to believe that emit_store_flag will be successful. If we think that
10918 it will, but it isn't, we have to simulate the store-flag with a
10919 set/jump/set sequence. */
10921 static rtx
10922 do_store_flag (sepops ops, rtx target, machine_mode mode)
10924 enum rtx_code code;
10925 tree arg0, arg1, type;
10926 tree tem;
10927 machine_mode operand_mode;
10928 int unsignedp;
10929 rtx op0, op1;
10930 rtx subtarget = target;
10931 location_t loc = ops->location;
10933 arg0 = ops->op0;
10934 arg1 = ops->op1;
10936 /* Don't crash if the comparison was erroneous. */
10937 if (arg0 == error_mark_node || arg1 == error_mark_node)
10938 return const0_rtx;
10940 type = TREE_TYPE (arg0);
10941 operand_mode = TYPE_MODE (type);
10942 unsignedp = TYPE_UNSIGNED (type);
10944 /* We won't bother with BLKmode store-flag operations because it would mean
10945 passing a lot of information to emit_store_flag. */
10946 if (operand_mode == BLKmode)
10947 return 0;
10949 /* We won't bother with store-flag operations involving function pointers
10950 when function pointers must be canonicalized before comparisons. */
10951 #ifdef HAVE_canonicalize_funcptr_for_compare
10952 if (HAVE_canonicalize_funcptr_for_compare
10953 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10954 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10955 == FUNCTION_TYPE))
10956 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10957 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10958 == FUNCTION_TYPE))))
10959 return 0;
10960 #endif
10962 STRIP_NOPS (arg0);
10963 STRIP_NOPS (arg1);
10965 /* For vector typed comparisons emit code to generate the desired
10966 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10967 expander for this. */
10968 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10970 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10971 tree if_true = constant_boolean_node (true, ops->type);
10972 tree if_false = constant_boolean_node (false, ops->type);
10973 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10976 /* Get the rtx comparison code to use. We know that EXP is a comparison
10977 operation of some type. Some comparisons against 1 and -1 can be
10978 converted to comparisons with zero. Do so here so that the tests
10979 below will be aware that we have a comparison with zero. These
10980 tests will not catch constants in the first operand, but constants
10981 are rarely passed as the first operand. */
10983 switch (ops->code)
10985 case EQ_EXPR:
10986 code = EQ;
10987 break;
10988 case NE_EXPR:
10989 code = NE;
10990 break;
10991 case LT_EXPR:
10992 if (integer_onep (arg1))
10993 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10994 else
10995 code = unsignedp ? LTU : LT;
10996 break;
10997 case LE_EXPR:
10998 if (! unsignedp && integer_all_onesp (arg1))
10999 arg1 = integer_zero_node, code = LT;
11000 else
11001 code = unsignedp ? LEU : LE;
11002 break;
11003 case GT_EXPR:
11004 if (! unsignedp && integer_all_onesp (arg1))
11005 arg1 = integer_zero_node, code = GE;
11006 else
11007 code = unsignedp ? GTU : GT;
11008 break;
11009 case GE_EXPR:
11010 if (integer_onep (arg1))
11011 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11012 else
11013 code = unsignedp ? GEU : GE;
11014 break;
11016 case UNORDERED_EXPR:
11017 code = UNORDERED;
11018 break;
11019 case ORDERED_EXPR:
11020 code = ORDERED;
11021 break;
11022 case UNLT_EXPR:
11023 code = UNLT;
11024 break;
11025 case UNLE_EXPR:
11026 code = UNLE;
11027 break;
11028 case UNGT_EXPR:
11029 code = UNGT;
11030 break;
11031 case UNGE_EXPR:
11032 code = UNGE;
11033 break;
11034 case UNEQ_EXPR:
11035 code = UNEQ;
11036 break;
11037 case LTGT_EXPR:
11038 code = LTGT;
11039 break;
11041 default:
11042 gcc_unreachable ();
11045 /* Put a constant second. */
11046 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11047 || TREE_CODE (arg0) == FIXED_CST)
11049 tem = arg0; arg0 = arg1; arg1 = tem;
11050 code = swap_condition (code);
11053 /* If this is an equality or inequality test of a single bit, we can
11054 do this by shifting the bit being tested to the low-order bit and
11055 masking the result with the constant 1. If the condition was EQ,
11056 we xor it with 1. This does not require an scc insn and is faster
11057 than an scc insn even if we have it.
11059 The code to make this transformation was moved into fold_single_bit_test,
11060 so we just call into the folder and expand its result. */
11062 if ((code == NE || code == EQ)
11063 && integer_zerop (arg1)
11064 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11066 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11067 if (srcstmt
11068 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11070 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11071 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11072 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11073 gimple_assign_rhs1 (srcstmt),
11074 gimple_assign_rhs2 (srcstmt));
11075 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11076 if (temp)
11077 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11081 if (! get_subtarget (target)
11082 || GET_MODE (subtarget) != operand_mode)
11083 subtarget = 0;
11085 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11087 if (target == 0)
11088 target = gen_reg_rtx (mode);
11090 /* Try a cstore if possible. */
11091 return emit_store_flag_force (target, code, op0, op1,
11092 operand_mode, unsignedp,
11093 (TYPE_PRECISION (ops->type) == 1
11094 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11098 /* Stubs in case we haven't got a casesi insn. */
11099 #ifndef HAVE_casesi
11100 # define HAVE_casesi 0
11101 # define gen_casesi(a, b, c, d, e) (0)
11102 # define CODE_FOR_casesi CODE_FOR_nothing
11103 #endif
11105 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11106 0 otherwise (i.e. if there is no casesi instruction).
11108 DEFAULT_PROBABILITY is the probability of jumping to the default
11109 label. */
11111 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11112 rtx table_label, rtx default_label, rtx fallback_label,
11113 int default_probability)
11115 struct expand_operand ops[5];
11116 machine_mode index_mode = SImode;
11117 rtx op1, op2, index;
11119 if (! HAVE_casesi)
11120 return 0;
11122 /* Convert the index to SImode. */
11123 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11125 machine_mode omode = TYPE_MODE (index_type);
11126 rtx rangertx = expand_normal (range);
11128 /* We must handle the endpoints in the original mode. */
11129 index_expr = build2 (MINUS_EXPR, index_type,
11130 index_expr, minval);
11131 minval = integer_zero_node;
11132 index = expand_normal (index_expr);
11133 if (default_label)
11134 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11135 omode, 1, default_label,
11136 default_probability);
11137 /* Now we can safely truncate. */
11138 index = convert_to_mode (index_mode, index, 0);
11140 else
11142 if (TYPE_MODE (index_type) != index_mode)
11144 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11145 index_expr = fold_convert (index_type, index_expr);
11148 index = expand_normal (index_expr);
11151 do_pending_stack_adjust ();
11153 op1 = expand_normal (minval);
11154 op2 = expand_normal (range);
11156 create_input_operand (&ops[0], index, index_mode);
11157 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11158 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11159 create_fixed_operand (&ops[3], table_label);
11160 create_fixed_operand (&ops[4], (default_label
11161 ? default_label
11162 : fallback_label));
11163 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11164 return 1;
11167 /* Attempt to generate a tablejump instruction; same concept. */
11168 /* Subroutine of the next function.
11170 INDEX is the value being switched on, with the lowest value
11171 in the table already subtracted.
11172 MODE is its expected mode (needed if INDEX is constant).
11173 RANGE is the length of the jump table.
11174 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11176 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11177 index value is out of range.
11178 DEFAULT_PROBABILITY is the probability of jumping to
11179 the default label. */
11181 static void
11182 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11183 rtx default_label, int default_probability)
11185 rtx temp, vector;
11187 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11188 cfun->cfg->max_jumptable_ents = INTVAL (range);
11190 /* Do an unsigned comparison (in the proper mode) between the index
11191 expression and the value which represents the length of the range.
11192 Since we just finished subtracting the lower bound of the range
11193 from the index expression, this comparison allows us to simultaneously
11194 check that the original index expression value is both greater than
11195 or equal to the minimum value of the range and less than or equal to
11196 the maximum value of the range. */
11198 if (default_label)
11199 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11200 default_label, default_probability);
11203 /* If index is in range, it must fit in Pmode.
11204 Convert to Pmode so we can index with it. */
11205 if (mode != Pmode)
11206 index = convert_to_mode (Pmode, index, 1);
11208 /* Don't let a MEM slip through, because then INDEX that comes
11209 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11210 and break_out_memory_refs will go to work on it and mess it up. */
11211 #ifdef PIC_CASE_VECTOR_ADDRESS
11212 if (flag_pic && !REG_P (index))
11213 index = copy_to_mode_reg (Pmode, index);
11214 #endif
11216 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11217 GET_MODE_SIZE, because this indicates how large insns are. The other
11218 uses should all be Pmode, because they are addresses. This code
11219 could fail if addresses and insns are not the same size. */
11220 index = simplify_gen_binary (MULT, Pmode, index,
11221 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11222 Pmode));
11223 index = simplify_gen_binary (PLUS, Pmode, index,
11224 gen_rtx_LABEL_REF (Pmode, table_label));
11226 #ifdef PIC_CASE_VECTOR_ADDRESS
11227 if (flag_pic)
11228 index = PIC_CASE_VECTOR_ADDRESS (index);
11229 else
11230 #endif
11231 index = memory_address (CASE_VECTOR_MODE, index);
11232 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11233 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11234 convert_move (temp, vector, 0);
11236 emit_jump_insn (gen_tablejump (temp, table_label));
11238 /* If we are generating PIC code or if the table is PC-relative, the
11239 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11240 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11241 emit_barrier ();
11245 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11246 rtx table_label, rtx default_label, int default_probability)
11248 rtx index;
11250 if (! HAVE_tablejump)
11251 return 0;
11253 index_expr = fold_build2 (MINUS_EXPR, index_type,
11254 fold_convert (index_type, index_expr),
11255 fold_convert (index_type, minval));
11256 index = expand_normal (index_expr);
11257 do_pending_stack_adjust ();
11259 do_tablejump (index, TYPE_MODE (index_type),
11260 convert_modes (TYPE_MODE (index_type),
11261 TYPE_MODE (TREE_TYPE (range)),
11262 expand_normal (range),
11263 TYPE_UNSIGNED (TREE_TYPE (range))),
11264 table_label, default_label, default_probability);
11265 return 1;
11268 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11269 static rtx
11270 const_vector_from_tree (tree exp)
11272 rtvec v;
11273 unsigned i;
11274 int units;
11275 tree elt;
11276 machine_mode inner, mode;
11278 mode = TYPE_MODE (TREE_TYPE (exp));
11280 if (initializer_zerop (exp))
11281 return CONST0_RTX (mode);
11283 units = GET_MODE_NUNITS (mode);
11284 inner = GET_MODE_INNER (mode);
11286 v = rtvec_alloc (units);
11288 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11290 elt = VECTOR_CST_ELT (exp, i);
11292 if (TREE_CODE (elt) == REAL_CST)
11293 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11294 inner);
11295 else if (TREE_CODE (elt) == FIXED_CST)
11296 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11297 inner);
11298 else
11299 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11302 return gen_rtx_CONST_VECTOR (mode, v);
11305 /* Build a decl for a personality function given a language prefix. */
11307 tree
11308 build_personality_function (const char *lang)
11310 const char *unwind_and_version;
11311 tree decl, type;
11312 char *name;
11314 switch (targetm_common.except_unwind_info (&global_options))
11316 case UI_NONE:
11317 return NULL;
11318 case UI_SJLJ:
11319 unwind_and_version = "_sj0";
11320 break;
11321 case UI_DWARF2:
11322 case UI_TARGET:
11323 unwind_and_version = "_v0";
11324 break;
11325 case UI_SEH:
11326 unwind_and_version = "_seh0";
11327 break;
11328 default:
11329 gcc_unreachable ();
11332 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11334 type = build_function_type_list (integer_type_node, integer_type_node,
11335 long_long_unsigned_type_node,
11336 ptr_type_node, ptr_type_node, NULL_TREE);
11337 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11338 get_identifier (name), type);
11339 DECL_ARTIFICIAL (decl) = 1;
11340 DECL_EXTERNAL (decl) = 1;
11341 TREE_PUBLIC (decl) = 1;
11343 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11344 are the flags assigned by targetm.encode_section_info. */
11345 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11347 return decl;
11350 /* Extracts the personality function of DECL and returns the corresponding
11351 libfunc. */
11354 get_personality_function (tree decl)
11356 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11357 enum eh_personality_kind pk;
11359 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11360 if (pk == eh_personality_none)
11361 return NULL;
11363 if (!personality
11364 && pk == eh_personality_any)
11365 personality = lang_hooks.eh_personality ();
11367 if (pk == eh_personality_lang)
11368 gcc_assert (personality != NULL_TREE);
11370 return XEXP (DECL_RTL (personality), 0);
11373 /* Returns a tree for the size of EXP in bytes. */
11375 static tree
11376 tree_expr_size (const_tree exp)
11378 if (DECL_P (exp)
11379 && DECL_SIZE_UNIT (exp) != 0)
11380 return DECL_SIZE_UNIT (exp);
11381 else
11382 return size_in_bytes (TREE_TYPE (exp));
11385 /* Return an rtx for the size in bytes of the value of EXP. */
11388 expr_size (tree exp)
11390 tree size;
11392 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11393 size = TREE_OPERAND (exp, 1);
11394 else
11396 size = tree_expr_size (exp);
11397 gcc_assert (size);
11398 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11401 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11404 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11405 if the size can vary or is larger than an integer. */
11407 static HOST_WIDE_INT
11408 int_expr_size (tree exp)
11410 tree size;
11412 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11413 size = TREE_OPERAND (exp, 1);
11414 else
11416 size = tree_expr_size (exp);
11417 gcc_assert (size);
11420 if (size == 0 || !tree_fits_shwi_p (size))
11421 return -1;
11423 return tree_to_shwi (size);
11426 #include "gt-expr.h"