2014-12-20 Martin Uecker <uecker@eecs.berkeley.edu>
[official-gcc.git] / gcc / expr.c
blob3d2ff6ec03ada5b54c66c3a6177a963c807be253
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "attribs.h"
30 #include "varasm.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "hashtab.h"
36 #include "hash-set.h"
37 #include "vec.h"
38 #include "input.h"
39 #include "function.h"
40 #include "insn-config.h"
41 #include "insn-attr.h"
42 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
43 #include "expr.h"
44 #include "insn-codes.h"
45 #include "optabs.h"
46 #include "libfuncs.h"
47 #include "recog.h"
48 #include "reload.h"
49 #include "typeclass.h"
50 #include "toplev.h"
51 #include "langhooks.h"
52 #include "intl.h"
53 #include "tm_p.h"
54 #include "tree-iterator.h"
55 #include "predict.h"
56 #include "dominance.h"
57 #include "cfg.h"
58 #include "basic-block.h"
59 #include "tree-ssa-alias.h"
60 #include "internal-fn.h"
61 #include "gimple-expr.h"
62 #include "is-a.h"
63 #include "gimple.h"
64 #include "gimple-ssa.h"
65 #include "hash-map.h"
66 #include "plugin-api.h"
67 #include "ipa-ref.h"
68 #include "cgraph.h"
69 #include "tree-ssanames.h"
70 #include "target.h"
71 #include "common/common-target.h"
72 #include "timevar.h"
73 #include "df.h"
74 #include "diagnostic.h"
75 #include "tree-ssa-live.h"
76 #include "tree-outof-ssa.h"
77 #include "target-globals.h"
78 #include "params.h"
79 #include "tree-ssa-address.h"
80 #include "cfgexpand.h"
81 #include "builtins.h"
82 #include "tree-chkp.h"
83 #include "rtl-chkp.h"
84 #include "ccmp.h"
86 #ifndef STACK_PUSH_CODE
87 #ifdef STACK_GROWS_DOWNWARD
88 #define STACK_PUSH_CODE PRE_DEC
89 #else
90 #define STACK_PUSH_CODE PRE_INC
91 #endif
92 #endif
95 /* If this is nonzero, we do not bother generating VOLATILE
96 around volatile memory references, and we are willing to
97 output indirect addresses. If cse is to follow, we reject
98 indirect addresses so a useful potential cse is generated;
99 if it is used only once, instruction combination will produce
100 the same indirect address eventually. */
101 int cse_not_expected;
103 /* This structure is used by move_by_pieces to describe the move to
104 be performed. */
105 struct move_by_pieces_d
107 rtx to;
108 rtx to_addr;
109 int autinc_to;
110 int explicit_inc_to;
111 rtx from;
112 rtx from_addr;
113 int autinc_from;
114 int explicit_inc_from;
115 unsigned HOST_WIDE_INT len;
116 HOST_WIDE_INT offset;
117 int reverse;
120 /* This structure is used by store_by_pieces to describe the clear to
121 be performed. */
123 struct store_by_pieces_d
125 rtx to;
126 rtx to_addr;
127 int autinc_to;
128 int explicit_inc_to;
129 unsigned HOST_WIDE_INT len;
130 HOST_WIDE_INT offset;
131 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
132 void *constfundata;
133 int reverse;
136 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
137 struct move_by_pieces_d *);
138 static bool block_move_libcall_safe_for_call_parm (void);
139 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
140 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
141 unsigned HOST_WIDE_INT);
142 static tree emit_block_move_libcall_fn (int);
143 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
144 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
145 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
146 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
147 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
148 struct store_by_pieces_d *);
149 static tree clear_storage_libcall_fn (int);
150 static rtx_insn *compress_float_constant (rtx, rtx);
151 static rtx get_subtarget (rtx);
152 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
153 HOST_WIDE_INT, machine_mode,
154 tree, int, alias_set_type);
155 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
156 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
157 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
158 machine_mode, tree, alias_set_type, bool);
160 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
162 static int is_aligning_offset (const_tree, const_tree);
163 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
164 static rtx do_store_flag (sepops, rtx, machine_mode);
165 #ifdef PUSH_ROUNDING
166 static void emit_single_push_insn (machine_mode, rtx, tree);
167 #endif
168 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
169 static rtx const_vector_from_tree (tree);
172 /* This is run to set up which modes can be used
173 directly in memory and to initialize the block move optab. It is run
174 at the beginning of compilation and when the target is reinitialized. */
176 void
177 init_expr_target (void)
179 rtx insn, pat;
180 machine_mode mode;
181 int num_clobbers;
182 rtx mem, mem1;
183 rtx reg;
185 /* Try indexing by frame ptr and try by stack ptr.
186 It is known that on the Convex the stack ptr isn't a valid index.
187 With luck, one or the other is valid on any machine. */
188 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
189 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
191 /* A scratch register we can modify in-place below to avoid
192 useless RTL allocations. */
193 reg = gen_rtx_REG (VOIDmode, -1);
195 insn = rtx_alloc (INSN);
196 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
197 PATTERN (insn) = pat;
199 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
200 mode = (machine_mode) ((int) mode + 1))
202 int regno;
204 direct_load[(int) mode] = direct_store[(int) mode] = 0;
205 PUT_MODE (mem, mode);
206 PUT_MODE (mem1, mode);
207 PUT_MODE (reg, mode);
209 /* See if there is some register that can be used in this mode and
210 directly loaded or stored from memory. */
212 if (mode != VOIDmode && mode != BLKmode)
213 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
214 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
215 regno++)
217 if (! HARD_REGNO_MODE_OK (regno, mode))
218 continue;
220 SET_REGNO (reg, regno);
222 SET_SRC (pat) = mem;
223 SET_DEST (pat) = reg;
224 if (recog (pat, insn, &num_clobbers) >= 0)
225 direct_load[(int) mode] = 1;
227 SET_SRC (pat) = mem1;
228 SET_DEST (pat) = reg;
229 if (recog (pat, insn, &num_clobbers) >= 0)
230 direct_load[(int) mode] = 1;
232 SET_SRC (pat) = reg;
233 SET_DEST (pat) = mem;
234 if (recog (pat, insn, &num_clobbers) >= 0)
235 direct_store[(int) mode] = 1;
237 SET_SRC (pat) = reg;
238 SET_DEST (pat) = mem1;
239 if (recog (pat, insn, &num_clobbers) >= 0)
240 direct_store[(int) mode] = 1;
244 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
246 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
247 mode = GET_MODE_WIDER_MODE (mode))
249 machine_mode srcmode;
250 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
251 srcmode = GET_MODE_WIDER_MODE (srcmode))
253 enum insn_code ic;
255 ic = can_extend_p (mode, srcmode, 0);
256 if (ic == CODE_FOR_nothing)
257 continue;
259 PUT_MODE (mem, srcmode);
261 if (insn_operand_matches (ic, 1, mem))
262 float_extend_from_mem[mode][srcmode] = true;
267 /* This is run at the start of compiling a function. */
269 void
270 init_expr (void)
272 memset (&crtl->expr, 0, sizeof (crtl->expr));
275 /* Copy data from FROM to TO, where the machine modes are not the same.
276 Both modes may be integer, or both may be floating, or both may be
277 fixed-point.
278 UNSIGNEDP should be nonzero if FROM is an unsigned type.
279 This causes zero-extension instead of sign-extension. */
281 void
282 convert_move (rtx to, rtx from, int unsignedp)
284 machine_mode to_mode = GET_MODE (to);
285 machine_mode from_mode = GET_MODE (from);
286 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
287 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
288 enum insn_code code;
289 rtx libcall;
291 /* rtx code for making an equivalent value. */
292 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
293 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
296 gcc_assert (to_real == from_real);
297 gcc_assert (to_mode != BLKmode);
298 gcc_assert (from_mode != BLKmode);
300 /* If the source and destination are already the same, then there's
301 nothing to do. */
302 if (to == from)
303 return;
305 /* If FROM is a SUBREG that indicates that we have already done at least
306 the required extension, strip it. We don't handle such SUBREGs as
307 TO here. */
309 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
310 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
311 >= GET_MODE_PRECISION (to_mode))
312 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
313 from = gen_lowpart (to_mode, from), from_mode = to_mode;
315 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
317 if (to_mode == from_mode
318 || (from_mode == VOIDmode && CONSTANT_P (from)))
320 emit_move_insn (to, from);
321 return;
324 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
326 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
328 if (VECTOR_MODE_P (to_mode))
329 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
330 else
331 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
333 emit_move_insn (to, from);
334 return;
337 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
339 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
340 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
341 return;
344 if (to_real)
346 rtx value;
347 rtx_insn *insns;
348 convert_optab tab;
350 gcc_assert ((GET_MODE_PRECISION (from_mode)
351 != GET_MODE_PRECISION (to_mode))
352 || (DECIMAL_FLOAT_MODE_P (from_mode)
353 != DECIMAL_FLOAT_MODE_P (to_mode)));
355 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
356 /* Conversion between decimal float and binary float, same size. */
357 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
358 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
359 tab = sext_optab;
360 else
361 tab = trunc_optab;
363 /* Try converting directly if the insn is supported. */
365 code = convert_optab_handler (tab, to_mode, from_mode);
366 if (code != CODE_FOR_nothing)
368 emit_unop_insn (code, to, from,
369 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
370 return;
373 /* Otherwise use a libcall. */
374 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
376 /* Is this conversion implemented yet? */
377 gcc_assert (libcall);
379 start_sequence ();
380 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
381 1, from, from_mode);
382 insns = get_insns ();
383 end_sequence ();
384 emit_libcall_block (insns, to, value,
385 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
386 from)
387 : gen_rtx_FLOAT_EXTEND (to_mode, from));
388 return;
391 /* Handle pointer conversion. */ /* SPEE 900220. */
392 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
394 convert_optab ctab;
396 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
397 ctab = trunc_optab;
398 else if (unsignedp)
399 ctab = zext_optab;
400 else
401 ctab = sext_optab;
403 if (convert_optab_handler (ctab, to_mode, from_mode)
404 != CODE_FOR_nothing)
406 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
407 to, from, UNKNOWN);
408 return;
412 /* Targets are expected to provide conversion insns between PxImode and
413 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
414 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
416 machine_mode full_mode
417 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
419 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
420 != CODE_FOR_nothing);
422 if (full_mode != from_mode)
423 from = convert_to_mode (full_mode, from, unsignedp);
424 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
425 to, from, UNKNOWN);
426 return;
428 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
430 rtx new_from;
431 machine_mode full_mode
432 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
433 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
434 enum insn_code icode;
436 icode = convert_optab_handler (ctab, full_mode, from_mode);
437 gcc_assert (icode != CODE_FOR_nothing);
439 if (to_mode == full_mode)
441 emit_unop_insn (icode, to, from, UNKNOWN);
442 return;
445 new_from = gen_reg_rtx (full_mode);
446 emit_unop_insn (icode, new_from, from, UNKNOWN);
448 /* else proceed to integer conversions below. */
449 from_mode = full_mode;
450 from = new_from;
453 /* Make sure both are fixed-point modes or both are not. */
454 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
455 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
456 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
458 /* If we widen from_mode to to_mode and they are in the same class,
459 we won't saturate the result.
460 Otherwise, always saturate the result to play safe. */
461 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
462 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
463 expand_fixed_convert (to, from, 0, 0);
464 else
465 expand_fixed_convert (to, from, 0, 1);
466 return;
469 /* Now both modes are integers. */
471 /* Handle expanding beyond a word. */
472 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
473 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
475 rtx_insn *insns;
476 rtx lowpart;
477 rtx fill_value;
478 rtx lowfrom;
479 int i;
480 machine_mode lowpart_mode;
481 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
483 /* Try converting directly if the insn is supported. */
484 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
485 != CODE_FOR_nothing)
487 /* If FROM is a SUBREG, put it into a register. Do this
488 so that we always generate the same set of insns for
489 better cse'ing; if an intermediate assignment occurred,
490 we won't be doing the operation directly on the SUBREG. */
491 if (optimize > 0 && GET_CODE (from) == SUBREG)
492 from = force_reg (from_mode, from);
493 emit_unop_insn (code, to, from, equiv_code);
494 return;
496 /* Next, try converting via full word. */
497 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
498 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
499 != CODE_FOR_nothing))
501 rtx word_to = gen_reg_rtx (word_mode);
502 if (REG_P (to))
504 if (reg_overlap_mentioned_p (to, from))
505 from = force_reg (from_mode, from);
506 emit_clobber (to);
508 convert_move (word_to, from, unsignedp);
509 emit_unop_insn (code, to, word_to, equiv_code);
510 return;
513 /* No special multiword conversion insn; do it by hand. */
514 start_sequence ();
516 /* Since we will turn this into a no conflict block, we must ensure the
517 the source does not overlap the target so force it into an isolated
518 register when maybe so. Likewise for any MEM input, since the
519 conversion sequence might require several references to it and we
520 must ensure we're getting the same value every time. */
522 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
523 from = force_reg (from_mode, from);
525 /* Get a copy of FROM widened to a word, if necessary. */
526 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
527 lowpart_mode = word_mode;
528 else
529 lowpart_mode = from_mode;
531 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
533 lowpart = gen_lowpart (lowpart_mode, to);
534 emit_move_insn (lowpart, lowfrom);
536 /* Compute the value to put in each remaining word. */
537 if (unsignedp)
538 fill_value = const0_rtx;
539 else
540 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
541 LT, lowfrom, const0_rtx,
542 lowpart_mode, 0, -1);
544 /* Fill the remaining words. */
545 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
547 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
548 rtx subword = operand_subword (to, index, 1, to_mode);
550 gcc_assert (subword);
552 if (fill_value != subword)
553 emit_move_insn (subword, fill_value);
556 insns = get_insns ();
557 end_sequence ();
559 emit_insn (insns);
560 return;
563 /* Truncating multi-word to a word or less. */
564 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
565 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
567 if (!((MEM_P (from)
568 && ! MEM_VOLATILE_P (from)
569 && direct_load[(int) to_mode]
570 && ! mode_dependent_address_p (XEXP (from, 0),
571 MEM_ADDR_SPACE (from)))
572 || REG_P (from)
573 || GET_CODE (from) == SUBREG))
574 from = force_reg (from_mode, from);
575 convert_move (to, gen_lowpart (word_mode, from), 0);
576 return;
579 /* Now follow all the conversions between integers
580 no more than a word long. */
582 /* For truncation, usually we can just refer to FROM in a narrower mode. */
583 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
584 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
586 if (!((MEM_P (from)
587 && ! MEM_VOLATILE_P (from)
588 && direct_load[(int) to_mode]
589 && ! mode_dependent_address_p (XEXP (from, 0),
590 MEM_ADDR_SPACE (from)))
591 || REG_P (from)
592 || GET_CODE (from) == SUBREG))
593 from = force_reg (from_mode, from);
594 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
595 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
596 from = copy_to_reg (from);
597 emit_move_insn (to, gen_lowpart (to_mode, from));
598 return;
601 /* Handle extension. */
602 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
604 /* Convert directly if that works. */
605 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
606 != CODE_FOR_nothing)
608 emit_unop_insn (code, to, from, equiv_code);
609 return;
611 else
613 machine_mode intermediate;
614 rtx tmp;
615 int shift_amount;
617 /* Search for a mode to convert via. */
618 for (intermediate = from_mode; intermediate != VOIDmode;
619 intermediate = GET_MODE_WIDER_MODE (intermediate))
620 if (((can_extend_p (to_mode, intermediate, unsignedp)
621 != CODE_FOR_nothing)
622 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
623 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
624 && (can_extend_p (intermediate, from_mode, unsignedp)
625 != CODE_FOR_nothing))
627 convert_move (to, convert_to_mode (intermediate, from,
628 unsignedp), unsignedp);
629 return;
632 /* No suitable intermediate mode.
633 Generate what we need with shifts. */
634 shift_amount = (GET_MODE_PRECISION (to_mode)
635 - GET_MODE_PRECISION (from_mode));
636 from = gen_lowpart (to_mode, force_reg (from_mode, from));
637 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
638 to, unsignedp);
639 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
640 to, unsignedp);
641 if (tmp != to)
642 emit_move_insn (to, tmp);
643 return;
647 /* Support special truncate insns for certain modes. */
648 if (convert_optab_handler (trunc_optab, to_mode,
649 from_mode) != CODE_FOR_nothing)
651 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
652 to, from, UNKNOWN);
653 return;
656 /* Handle truncation of volatile memrefs, and so on;
657 the things that couldn't be truncated directly,
658 and for which there was no special instruction.
660 ??? Code above formerly short-circuited this, for most integer
661 mode pairs, with a force_reg in from_mode followed by a recursive
662 call to this routine. Appears always to have been wrong. */
663 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
665 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
666 emit_move_insn (to, temp);
667 return;
670 /* Mode combination is not recognized. */
671 gcc_unreachable ();
674 /* Return an rtx for a value that would result
675 from converting X to mode MODE.
676 Both X and MODE may be floating, or both integer.
677 UNSIGNEDP is nonzero if X is an unsigned value.
678 This can be done by referring to a part of X in place
679 or by copying to a new temporary with conversion. */
682 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
684 return convert_modes (mode, VOIDmode, x, unsignedp);
687 /* Return an rtx for a value that would result
688 from converting X from mode OLDMODE to mode MODE.
689 Both modes may be floating, or both integer.
690 UNSIGNEDP is nonzero if X is an unsigned value.
692 This can be done by referring to a part of X in place
693 or by copying to a new temporary with conversion.
695 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
698 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
700 rtx temp;
702 /* If FROM is a SUBREG that indicates that we have already done at least
703 the required extension, strip it. */
705 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
706 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
707 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
708 x = gen_lowpart (mode, SUBREG_REG (x));
710 if (GET_MODE (x) != VOIDmode)
711 oldmode = GET_MODE (x);
713 if (mode == oldmode)
714 return x;
716 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
718 /* If the caller did not tell us the old mode, then there is not
719 much to do with respect to canonicalization. We have to
720 assume that all the bits are significant. */
721 if (GET_MODE_CLASS (oldmode) != MODE_INT)
722 oldmode = MAX_MODE_INT;
723 wide_int w = wide_int::from (std::make_pair (x, oldmode),
724 GET_MODE_PRECISION (mode),
725 unsignedp ? UNSIGNED : SIGNED);
726 return immed_wide_int_const (w, mode);
729 /* We can do this with a gen_lowpart if both desired and current modes
730 are integer, and this is either a constant integer, a register, or a
731 non-volatile MEM. */
732 if (GET_MODE_CLASS (mode) == MODE_INT
733 && GET_MODE_CLASS (oldmode) == MODE_INT
734 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
735 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
736 || (REG_P (x)
737 && (!HARD_REGISTER_P (x)
738 || HARD_REGNO_MODE_OK (REGNO (x), mode))
739 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
741 return gen_lowpart (mode, x);
743 /* Converting from integer constant into mode is always equivalent to an
744 subreg operation. */
745 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
747 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
748 return simplify_gen_subreg (mode, x, oldmode, 0);
751 temp = gen_reg_rtx (mode);
752 convert_move (temp, x, unsignedp);
753 return temp;
756 /* Return the largest alignment we can use for doing a move (or store)
757 of MAX_PIECES. ALIGN is the largest alignment we could use. */
759 static unsigned int
760 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
762 machine_mode tmode;
764 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
765 if (align >= GET_MODE_ALIGNMENT (tmode))
766 align = GET_MODE_ALIGNMENT (tmode);
767 else
769 machine_mode tmode, xmode;
771 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
772 tmode != VOIDmode;
773 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
774 if (GET_MODE_SIZE (tmode) > max_pieces
775 || SLOW_UNALIGNED_ACCESS (tmode, align))
776 break;
778 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
781 return align;
784 /* Return the widest integer mode no wider than SIZE. If no such mode
785 can be found, return VOIDmode. */
787 static machine_mode
788 widest_int_mode_for_size (unsigned int size)
790 machine_mode tmode, mode = VOIDmode;
792 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
793 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
794 if (GET_MODE_SIZE (tmode) < size)
795 mode = tmode;
797 return mode;
800 /* Determine whether the LEN bytes can be moved by using several move
801 instructions. Return nonzero if a call to move_by_pieces should
802 succeed. */
805 can_move_by_pieces (unsigned HOST_WIDE_INT len,
806 unsigned int align)
808 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
809 optimize_insn_for_speed_p ());
812 /* Generate several move instructions to copy LEN bytes from block FROM to
813 block TO. (These are MEM rtx's with BLKmode).
815 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
816 used to push FROM to the stack.
818 ALIGN is maximum stack alignment we can assume.
820 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
821 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
822 stpcpy. */
825 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
826 unsigned int align, int endp)
828 struct move_by_pieces_d data;
829 machine_mode to_addr_mode;
830 machine_mode from_addr_mode = get_address_mode (from);
831 rtx to_addr, from_addr = XEXP (from, 0);
832 unsigned int max_size = MOVE_MAX_PIECES + 1;
833 enum insn_code icode;
835 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
837 data.offset = 0;
838 data.from_addr = from_addr;
839 if (to)
841 to_addr_mode = get_address_mode (to);
842 to_addr = XEXP (to, 0);
843 data.to = to;
844 data.autinc_to
845 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
846 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
847 data.reverse
848 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
850 else
852 to_addr_mode = VOIDmode;
853 to_addr = NULL_RTX;
854 data.to = NULL_RTX;
855 data.autinc_to = 1;
856 #ifdef STACK_GROWS_DOWNWARD
857 data.reverse = 1;
858 #else
859 data.reverse = 0;
860 #endif
862 data.to_addr = to_addr;
863 data.from = from;
864 data.autinc_from
865 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
866 || GET_CODE (from_addr) == POST_INC
867 || GET_CODE (from_addr) == POST_DEC);
869 data.explicit_inc_from = 0;
870 data.explicit_inc_to = 0;
871 if (data.reverse) data.offset = len;
872 data.len = len;
874 /* If copying requires more than two move insns,
875 copy addresses to registers (to make displacements shorter)
876 and use post-increment if available. */
877 if (!(data.autinc_from && data.autinc_to)
878 && move_by_pieces_ninsns (len, align, max_size) > 2)
880 /* Find the mode of the largest move...
881 MODE might not be used depending on the definitions of the
882 USE_* macros below. */
883 machine_mode mode ATTRIBUTE_UNUSED
884 = widest_int_mode_for_size (max_size);
886 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
888 data.from_addr = copy_to_mode_reg (from_addr_mode,
889 plus_constant (from_addr_mode,
890 from_addr, len));
891 data.autinc_from = 1;
892 data.explicit_inc_from = -1;
894 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
896 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
897 data.autinc_from = 1;
898 data.explicit_inc_from = 1;
900 if (!data.autinc_from && CONSTANT_P (from_addr))
901 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
902 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
904 data.to_addr = copy_to_mode_reg (to_addr_mode,
905 plus_constant (to_addr_mode,
906 to_addr, len));
907 data.autinc_to = 1;
908 data.explicit_inc_to = -1;
910 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
912 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
913 data.autinc_to = 1;
914 data.explicit_inc_to = 1;
916 if (!data.autinc_to && CONSTANT_P (to_addr))
917 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
920 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
922 /* First move what we can in the largest integer mode, then go to
923 successively smaller modes. */
925 while (max_size > 1 && data.len > 0)
927 machine_mode mode = widest_int_mode_for_size (max_size);
929 if (mode == VOIDmode)
930 break;
932 icode = optab_handler (mov_optab, mode);
933 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
934 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
936 max_size = GET_MODE_SIZE (mode);
939 /* The code above should have handled everything. */
940 gcc_assert (!data.len);
942 if (endp)
944 rtx to1;
946 gcc_assert (!data.reverse);
947 if (data.autinc_to)
949 if (endp == 2)
951 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
952 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
953 else
954 data.to_addr = copy_to_mode_reg (to_addr_mode,
955 plus_constant (to_addr_mode,
956 data.to_addr,
957 -1));
959 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
960 data.offset);
962 else
964 if (endp == 2)
965 --data.offset;
966 to1 = adjust_address (data.to, QImode, data.offset);
968 return to1;
970 else
971 return data.to;
974 /* Return number of insns required to move L bytes by pieces.
975 ALIGN (in bits) is maximum alignment we can assume. */
977 unsigned HOST_WIDE_INT
978 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
979 unsigned int max_size)
981 unsigned HOST_WIDE_INT n_insns = 0;
983 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
985 while (max_size > 1 && l > 0)
987 machine_mode mode;
988 enum insn_code icode;
990 mode = widest_int_mode_for_size (max_size);
992 if (mode == VOIDmode)
993 break;
995 icode = optab_handler (mov_optab, mode);
996 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
997 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
999 max_size = GET_MODE_SIZE (mode);
1002 gcc_assert (!l);
1003 return n_insns;
1006 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1007 with move instructions for mode MODE. GENFUN is the gen_... function
1008 to make a move insn for that mode. DATA has all the other info. */
1010 static void
1011 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1012 struct move_by_pieces_d *data)
1014 unsigned int size = GET_MODE_SIZE (mode);
1015 rtx to1 = NULL_RTX, from1;
1017 while (data->len >= size)
1019 if (data->reverse)
1020 data->offset -= size;
1022 if (data->to)
1024 if (data->autinc_to)
1025 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1026 data->offset);
1027 else
1028 to1 = adjust_address (data->to, mode, data->offset);
1031 if (data->autinc_from)
1032 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1033 data->offset);
1034 else
1035 from1 = adjust_address (data->from, mode, data->offset);
1037 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1038 emit_insn (gen_add2_insn (data->to_addr,
1039 gen_int_mode (-(HOST_WIDE_INT) size,
1040 GET_MODE (data->to_addr))));
1041 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1042 emit_insn (gen_add2_insn (data->from_addr,
1043 gen_int_mode (-(HOST_WIDE_INT) size,
1044 GET_MODE (data->from_addr))));
1046 if (data->to)
1047 emit_insn ((*genfun) (to1, from1));
1048 else
1050 #ifdef PUSH_ROUNDING
1051 emit_single_push_insn (mode, from1, NULL);
1052 #else
1053 gcc_unreachable ();
1054 #endif
1057 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1058 emit_insn (gen_add2_insn (data->to_addr,
1059 gen_int_mode (size,
1060 GET_MODE (data->to_addr))));
1061 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1062 emit_insn (gen_add2_insn (data->from_addr,
1063 gen_int_mode (size,
1064 GET_MODE (data->from_addr))));
1066 if (! data->reverse)
1067 data->offset += size;
1069 data->len -= size;
1073 /* Emit code to move a block Y to a block X. This may be done with
1074 string-move instructions, with multiple scalar move instructions,
1075 or with a library call.
1077 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1078 SIZE is an rtx that says how long they are.
1079 ALIGN is the maximum alignment we can assume they have.
1080 METHOD describes what kind of copy this is, and what mechanisms may be used.
1081 MIN_SIZE is the minimal size of block to move
1082 MAX_SIZE is the maximal size of block to move, if it can not be represented
1083 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1085 Return the address of the new block, if memcpy is called and returns it,
1086 0 otherwise. */
1089 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1090 unsigned int expected_align, HOST_WIDE_INT expected_size,
1091 unsigned HOST_WIDE_INT min_size,
1092 unsigned HOST_WIDE_INT max_size,
1093 unsigned HOST_WIDE_INT probable_max_size)
1095 bool may_use_call;
1096 rtx retval = 0;
1097 unsigned int align;
1099 gcc_assert (size);
1100 if (CONST_INT_P (size)
1101 && INTVAL (size) == 0)
1102 return 0;
1104 switch (method)
1106 case BLOCK_OP_NORMAL:
1107 case BLOCK_OP_TAILCALL:
1108 may_use_call = true;
1109 break;
1111 case BLOCK_OP_CALL_PARM:
1112 may_use_call = block_move_libcall_safe_for_call_parm ();
1114 /* Make inhibit_defer_pop nonzero around the library call
1115 to force it to pop the arguments right away. */
1116 NO_DEFER_POP;
1117 break;
1119 case BLOCK_OP_NO_LIBCALL:
1120 may_use_call = false;
1121 break;
1123 default:
1124 gcc_unreachable ();
1127 gcc_assert (MEM_P (x) && MEM_P (y));
1128 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1129 gcc_assert (align >= BITS_PER_UNIT);
1131 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1132 block copy is more efficient for other large modes, e.g. DCmode. */
1133 x = adjust_address (x, BLKmode, 0);
1134 y = adjust_address (y, BLKmode, 0);
1136 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1137 can be incorrect is coming from __builtin_memcpy. */
1138 if (CONST_INT_P (size))
1140 x = shallow_copy_rtx (x);
1141 y = shallow_copy_rtx (y);
1142 set_mem_size (x, INTVAL (size));
1143 set_mem_size (y, INTVAL (size));
1146 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1147 move_by_pieces (x, y, INTVAL (size), align, 0);
1148 else if (emit_block_move_via_movmem (x, y, size, align,
1149 expected_align, expected_size,
1150 min_size, max_size, probable_max_size))
1152 else if (may_use_call
1153 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1154 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1156 /* Since x and y are passed to a libcall, mark the corresponding
1157 tree EXPR as addressable. */
1158 tree y_expr = MEM_EXPR (y);
1159 tree x_expr = MEM_EXPR (x);
1160 if (y_expr)
1161 mark_addressable (y_expr);
1162 if (x_expr)
1163 mark_addressable (x_expr);
1164 retval = emit_block_move_via_libcall (x, y, size,
1165 method == BLOCK_OP_TAILCALL);
1168 else
1169 emit_block_move_via_loop (x, y, size, align);
1171 if (method == BLOCK_OP_CALL_PARM)
1172 OK_DEFER_POP;
1174 return retval;
1178 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1180 unsigned HOST_WIDE_INT max, min = 0;
1181 if (GET_CODE (size) == CONST_INT)
1182 min = max = UINTVAL (size);
1183 else
1184 max = GET_MODE_MASK (GET_MODE (size));
1185 return emit_block_move_hints (x, y, size, method, 0, -1,
1186 min, max, max);
1189 /* A subroutine of emit_block_move. Returns true if calling the
1190 block move libcall will not clobber any parameters which may have
1191 already been placed on the stack. */
1193 static bool
1194 block_move_libcall_safe_for_call_parm (void)
1196 #if defined (REG_PARM_STACK_SPACE)
1197 tree fn;
1198 #endif
1200 /* If arguments are pushed on the stack, then they're safe. */
1201 if (PUSH_ARGS)
1202 return true;
1204 /* If registers go on the stack anyway, any argument is sure to clobber
1205 an outgoing argument. */
1206 #if defined (REG_PARM_STACK_SPACE)
1207 fn = emit_block_move_libcall_fn (false);
1208 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1209 depend on its argument. */
1210 (void) fn;
1211 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1212 && REG_PARM_STACK_SPACE (fn) != 0)
1213 return false;
1214 #endif
1216 /* If any argument goes in memory, then it might clobber an outgoing
1217 argument. */
1219 CUMULATIVE_ARGS args_so_far_v;
1220 cumulative_args_t args_so_far;
1221 tree fn, arg;
1223 fn = emit_block_move_libcall_fn (false);
1224 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1225 args_so_far = pack_cumulative_args (&args_so_far_v);
1227 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1228 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1230 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1231 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1232 NULL_TREE, true);
1233 if (!tmp || !REG_P (tmp))
1234 return false;
1235 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1236 return false;
1237 targetm.calls.function_arg_advance (args_so_far, mode,
1238 NULL_TREE, true);
1241 return true;
1244 /* A subroutine of emit_block_move. Expand a movmem pattern;
1245 return true if successful. */
1247 static bool
1248 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1249 unsigned int expected_align, HOST_WIDE_INT expected_size,
1250 unsigned HOST_WIDE_INT min_size,
1251 unsigned HOST_WIDE_INT max_size,
1252 unsigned HOST_WIDE_INT probable_max_size)
1254 int save_volatile_ok = volatile_ok;
1255 machine_mode mode;
1257 if (expected_align < align)
1258 expected_align = align;
1259 if (expected_size != -1)
1261 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1262 expected_size = probable_max_size;
1263 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1264 expected_size = min_size;
1267 /* Since this is a move insn, we don't care about volatility. */
1268 volatile_ok = 1;
1270 /* Try the most limited insn first, because there's no point
1271 including more than one in the machine description unless
1272 the more limited one has some advantage. */
1274 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1275 mode = GET_MODE_WIDER_MODE (mode))
1277 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1279 if (code != CODE_FOR_nothing
1280 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1281 here because if SIZE is less than the mode mask, as it is
1282 returned by the macro, it will definitely be less than the
1283 actual mode mask. Since SIZE is within the Pmode address
1284 space, we limit MODE to Pmode. */
1285 && ((CONST_INT_P (size)
1286 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1287 <= (GET_MODE_MASK (mode) >> 1)))
1288 || max_size <= (GET_MODE_MASK (mode) >> 1)
1289 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1291 struct expand_operand ops[9];
1292 unsigned int nops;
1294 /* ??? When called via emit_block_move_for_call, it'd be
1295 nice if there were some way to inform the backend, so
1296 that it doesn't fail the expansion because it thinks
1297 emitting the libcall would be more efficient. */
1298 nops = insn_data[(int) code].n_generator_args;
1299 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1301 create_fixed_operand (&ops[0], x);
1302 create_fixed_operand (&ops[1], y);
1303 /* The check above guarantees that this size conversion is valid. */
1304 create_convert_operand_to (&ops[2], size, mode, true);
1305 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1306 if (nops >= 6)
1308 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1309 create_integer_operand (&ops[5], expected_size);
1311 if (nops >= 8)
1313 create_integer_operand (&ops[6], min_size);
1314 /* If we can not represent the maximal size,
1315 make parameter NULL. */
1316 if ((HOST_WIDE_INT) max_size != -1)
1317 create_integer_operand (&ops[7], max_size);
1318 else
1319 create_fixed_operand (&ops[7], NULL);
1321 if (nops == 9)
1323 /* If we can not represent the maximal size,
1324 make parameter NULL. */
1325 if ((HOST_WIDE_INT) probable_max_size != -1)
1326 create_integer_operand (&ops[8], probable_max_size);
1327 else
1328 create_fixed_operand (&ops[8], NULL);
1330 if (maybe_expand_insn (code, nops, ops))
1332 volatile_ok = save_volatile_ok;
1333 return true;
1338 volatile_ok = save_volatile_ok;
1339 return false;
1342 /* A subroutine of emit_block_move. Expand a call to memcpy.
1343 Return the return value from memcpy, 0 otherwise. */
1346 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1348 rtx dst_addr, src_addr;
1349 tree call_expr, fn, src_tree, dst_tree, size_tree;
1350 machine_mode size_mode;
1351 rtx retval;
1353 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1354 pseudos. We can then place those new pseudos into a VAR_DECL and
1355 use them later. */
1357 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1358 src_addr = copy_addr_to_reg (XEXP (src, 0));
1360 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1361 src_addr = convert_memory_address (ptr_mode, src_addr);
1363 dst_tree = make_tree (ptr_type_node, dst_addr);
1364 src_tree = make_tree (ptr_type_node, src_addr);
1366 size_mode = TYPE_MODE (sizetype);
1368 size = convert_to_mode (size_mode, size, 1);
1369 size = copy_to_mode_reg (size_mode, size);
1371 /* It is incorrect to use the libcall calling conventions to call
1372 memcpy in this context. This could be a user call to memcpy and
1373 the user may wish to examine the return value from memcpy. For
1374 targets where libcalls and normal calls have different conventions
1375 for returning pointers, we could end up generating incorrect code. */
1377 size_tree = make_tree (sizetype, size);
1379 fn = emit_block_move_libcall_fn (true);
1380 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1381 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1383 retval = expand_normal (call_expr);
1385 return retval;
1388 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1389 for the function we use for block copies. */
1391 static GTY(()) tree block_move_fn;
1393 void
1394 init_block_move_fn (const char *asmspec)
1396 if (!block_move_fn)
1398 tree args, fn, attrs, attr_args;
1400 fn = get_identifier ("memcpy");
1401 args = build_function_type_list (ptr_type_node, ptr_type_node,
1402 const_ptr_type_node, sizetype,
1403 NULL_TREE);
1405 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1406 DECL_EXTERNAL (fn) = 1;
1407 TREE_PUBLIC (fn) = 1;
1408 DECL_ARTIFICIAL (fn) = 1;
1409 TREE_NOTHROW (fn) = 1;
1410 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1411 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1413 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1414 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1416 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1418 block_move_fn = fn;
1421 if (asmspec)
1422 set_user_assembler_name (block_move_fn, asmspec);
1425 static tree
1426 emit_block_move_libcall_fn (int for_call)
1428 static bool emitted_extern;
1430 if (!block_move_fn)
1431 init_block_move_fn (NULL);
1433 if (for_call && !emitted_extern)
1435 emitted_extern = true;
1436 make_decl_rtl (block_move_fn);
1439 return block_move_fn;
1442 /* A subroutine of emit_block_move. Copy the data via an explicit
1443 loop. This is used only when libcalls are forbidden. */
1444 /* ??? It'd be nice to copy in hunks larger than QImode. */
1446 static void
1447 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1448 unsigned int align ATTRIBUTE_UNUSED)
1450 rtx_code_label *cmp_label, *top_label;
1451 rtx iter, x_addr, y_addr, tmp;
1452 machine_mode x_addr_mode = get_address_mode (x);
1453 machine_mode y_addr_mode = get_address_mode (y);
1454 machine_mode iter_mode;
1456 iter_mode = GET_MODE (size);
1457 if (iter_mode == VOIDmode)
1458 iter_mode = word_mode;
1460 top_label = gen_label_rtx ();
1461 cmp_label = gen_label_rtx ();
1462 iter = gen_reg_rtx (iter_mode);
1464 emit_move_insn (iter, const0_rtx);
1466 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1467 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1468 do_pending_stack_adjust ();
1470 emit_jump (cmp_label);
1471 emit_label (top_label);
1473 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1474 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1476 if (x_addr_mode != y_addr_mode)
1477 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1478 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1480 x = change_address (x, QImode, x_addr);
1481 y = change_address (y, QImode, y_addr);
1483 emit_move_insn (x, y);
1485 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1486 true, OPTAB_LIB_WIDEN);
1487 if (tmp != iter)
1488 emit_move_insn (iter, tmp);
1490 emit_label (cmp_label);
1492 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1493 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1496 /* Copy all or part of a value X into registers starting at REGNO.
1497 The number of registers to be filled is NREGS. */
1499 void
1500 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1502 int i;
1503 #ifdef HAVE_load_multiple
1504 rtx pat;
1505 rtx_insn *last;
1506 #endif
1508 if (nregs == 0)
1509 return;
1511 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1512 x = validize_mem (force_const_mem (mode, x));
1514 /* See if the machine can do this with a load multiple insn. */
1515 #ifdef HAVE_load_multiple
1516 if (HAVE_load_multiple)
1518 last = get_last_insn ();
1519 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1520 GEN_INT (nregs));
1521 if (pat)
1523 emit_insn (pat);
1524 return;
1526 else
1527 delete_insns_since (last);
1529 #endif
1531 for (i = 0; i < nregs; i++)
1532 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1533 operand_subword_force (x, i, mode));
1536 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1537 The number of registers to be filled is NREGS. */
1539 void
1540 move_block_from_reg (int regno, rtx x, int nregs)
1542 int i;
1544 if (nregs == 0)
1545 return;
1547 /* See if the machine can do this with a store multiple insn. */
1548 #ifdef HAVE_store_multiple
1549 if (HAVE_store_multiple)
1551 rtx_insn *last = get_last_insn ();
1552 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1553 GEN_INT (nregs));
1554 if (pat)
1556 emit_insn (pat);
1557 return;
1559 else
1560 delete_insns_since (last);
1562 #endif
1564 for (i = 0; i < nregs; i++)
1566 rtx tem = operand_subword (x, i, 1, BLKmode);
1568 gcc_assert (tem);
1570 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1574 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1575 ORIG, where ORIG is a non-consecutive group of registers represented by
1576 a PARALLEL. The clone is identical to the original except in that the
1577 original set of registers is replaced by a new set of pseudo registers.
1578 The new set has the same modes as the original set. */
1581 gen_group_rtx (rtx orig)
1583 int i, length;
1584 rtx *tmps;
1586 gcc_assert (GET_CODE (orig) == PARALLEL);
1588 length = XVECLEN (orig, 0);
1589 tmps = XALLOCAVEC (rtx, length);
1591 /* Skip a NULL entry in first slot. */
1592 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1594 if (i)
1595 tmps[0] = 0;
1597 for (; i < length; i++)
1599 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1600 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1602 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1605 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1608 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1609 except that values are placed in TMPS[i], and must later be moved
1610 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1612 static void
1613 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1615 rtx src;
1616 int start, i;
1617 machine_mode m = GET_MODE (orig_src);
1619 gcc_assert (GET_CODE (dst) == PARALLEL);
1621 if (m != VOIDmode
1622 && !SCALAR_INT_MODE_P (m)
1623 && !MEM_P (orig_src)
1624 && GET_CODE (orig_src) != CONCAT)
1626 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1627 if (imode == BLKmode)
1628 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1629 else
1630 src = gen_reg_rtx (imode);
1631 if (imode != BLKmode)
1632 src = gen_lowpart (GET_MODE (orig_src), src);
1633 emit_move_insn (src, orig_src);
1634 /* ...and back again. */
1635 if (imode != BLKmode)
1636 src = gen_lowpart (imode, src);
1637 emit_group_load_1 (tmps, dst, src, type, ssize);
1638 return;
1641 /* Check for a NULL entry, used to indicate that the parameter goes
1642 both on the stack and in registers. */
1643 if (XEXP (XVECEXP (dst, 0, 0), 0))
1644 start = 0;
1645 else
1646 start = 1;
1648 /* Process the pieces. */
1649 for (i = start; i < XVECLEN (dst, 0); i++)
1651 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1652 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1653 unsigned int bytelen = GET_MODE_SIZE (mode);
1654 int shift = 0;
1656 /* Handle trailing fragments that run over the size of the struct. */
1657 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1659 /* Arrange to shift the fragment to where it belongs.
1660 extract_bit_field loads to the lsb of the reg. */
1661 if (
1662 #ifdef BLOCK_REG_PADDING
1663 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1664 == (BYTES_BIG_ENDIAN ? upward : downward)
1665 #else
1666 BYTES_BIG_ENDIAN
1667 #endif
1669 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1670 bytelen = ssize - bytepos;
1671 gcc_assert (bytelen > 0);
1674 /* If we won't be loading directly from memory, protect the real source
1675 from strange tricks we might play; but make sure that the source can
1676 be loaded directly into the destination. */
1677 src = orig_src;
1678 if (!MEM_P (orig_src)
1679 && (!CONSTANT_P (orig_src)
1680 || (GET_MODE (orig_src) != mode
1681 && GET_MODE (orig_src) != VOIDmode)))
1683 if (GET_MODE (orig_src) == VOIDmode)
1684 src = gen_reg_rtx (mode);
1685 else
1686 src = gen_reg_rtx (GET_MODE (orig_src));
1688 emit_move_insn (src, orig_src);
1691 /* Optimize the access just a bit. */
1692 if (MEM_P (src)
1693 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1694 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1695 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1696 && bytelen == GET_MODE_SIZE (mode))
1698 tmps[i] = gen_reg_rtx (mode);
1699 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1701 else if (COMPLEX_MODE_P (mode)
1702 && GET_MODE (src) == mode
1703 && bytelen == GET_MODE_SIZE (mode))
1704 /* Let emit_move_complex do the bulk of the work. */
1705 tmps[i] = src;
1706 else if (GET_CODE (src) == CONCAT)
1708 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1709 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1711 if ((bytepos == 0 && bytelen == slen0)
1712 || (bytepos != 0 && bytepos + bytelen <= slen))
1714 /* The following assumes that the concatenated objects all
1715 have the same size. In this case, a simple calculation
1716 can be used to determine the object and the bit field
1717 to be extracted. */
1718 tmps[i] = XEXP (src, bytepos / slen0);
1719 if (! CONSTANT_P (tmps[i])
1720 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1721 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1722 (bytepos % slen0) * BITS_PER_UNIT,
1723 1, NULL_RTX, mode, mode);
1725 else
1727 rtx mem;
1729 gcc_assert (!bytepos);
1730 mem = assign_stack_temp (GET_MODE (src), slen);
1731 emit_move_insn (mem, src);
1732 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1733 0, 1, NULL_RTX, mode, mode);
1736 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1737 SIMD register, which is currently broken. While we get GCC
1738 to emit proper RTL for these cases, let's dump to memory. */
1739 else if (VECTOR_MODE_P (GET_MODE (dst))
1740 && REG_P (src))
1742 int slen = GET_MODE_SIZE (GET_MODE (src));
1743 rtx mem;
1745 mem = assign_stack_temp (GET_MODE (src), slen);
1746 emit_move_insn (mem, src);
1747 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1749 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1750 && XVECLEN (dst, 0) > 1)
1751 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1752 else if (CONSTANT_P (src))
1754 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1756 if (len == ssize)
1757 tmps[i] = src;
1758 else
1760 rtx first, second;
1762 /* TODO: const_wide_int can have sizes other than this... */
1763 gcc_assert (2 * len == ssize);
1764 split_double (src, &first, &second);
1765 if (i)
1766 tmps[i] = second;
1767 else
1768 tmps[i] = first;
1771 else if (REG_P (src) && GET_MODE (src) == mode)
1772 tmps[i] = src;
1773 else
1774 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1775 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1776 mode, mode);
1778 if (shift)
1779 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1780 shift, tmps[i], 0);
1784 /* Emit code to move a block SRC of type TYPE to a block DST,
1785 where DST is non-consecutive registers represented by a PARALLEL.
1786 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1787 if not known. */
1789 void
1790 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1792 rtx *tmps;
1793 int i;
1795 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1796 emit_group_load_1 (tmps, dst, src, type, ssize);
1798 /* Copy the extracted pieces into the proper (probable) hard regs. */
1799 for (i = 0; i < XVECLEN (dst, 0); i++)
1801 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1802 if (d == NULL)
1803 continue;
1804 emit_move_insn (d, tmps[i]);
1808 /* Similar, but load SRC into new pseudos in a format that looks like
1809 PARALLEL. This can later be fed to emit_group_move to get things
1810 in the right place. */
1813 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1815 rtvec vec;
1816 int i;
1818 vec = rtvec_alloc (XVECLEN (parallel, 0));
1819 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1821 /* Convert the vector to look just like the original PARALLEL, except
1822 with the computed values. */
1823 for (i = 0; i < XVECLEN (parallel, 0); i++)
1825 rtx e = XVECEXP (parallel, 0, i);
1826 rtx d = XEXP (e, 0);
1828 if (d)
1830 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1831 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1833 RTVEC_ELT (vec, i) = e;
1836 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1839 /* Emit code to move a block SRC to block DST, where SRC and DST are
1840 non-consecutive groups of registers, each represented by a PARALLEL. */
1842 void
1843 emit_group_move (rtx dst, rtx src)
1845 int i;
1847 gcc_assert (GET_CODE (src) == PARALLEL
1848 && GET_CODE (dst) == PARALLEL
1849 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1851 /* Skip first entry if NULL. */
1852 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1853 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1854 XEXP (XVECEXP (src, 0, i), 0));
1857 /* Move a group of registers represented by a PARALLEL into pseudos. */
1860 emit_group_move_into_temps (rtx src)
1862 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1863 int i;
1865 for (i = 0; i < XVECLEN (src, 0); i++)
1867 rtx e = XVECEXP (src, 0, i);
1868 rtx d = XEXP (e, 0);
1870 if (d)
1871 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1872 RTVEC_ELT (vec, i) = e;
1875 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1878 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1879 where SRC is non-consecutive registers represented by a PARALLEL.
1880 SSIZE represents the total size of block ORIG_DST, or -1 if not
1881 known. */
1883 void
1884 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1886 rtx *tmps, dst;
1887 int start, finish, i;
1888 machine_mode m = GET_MODE (orig_dst);
1890 gcc_assert (GET_CODE (src) == PARALLEL);
1892 if (!SCALAR_INT_MODE_P (m)
1893 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1895 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1896 if (imode == BLKmode)
1897 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1898 else
1899 dst = gen_reg_rtx (imode);
1900 emit_group_store (dst, src, type, ssize);
1901 if (imode != BLKmode)
1902 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1903 emit_move_insn (orig_dst, dst);
1904 return;
1907 /* Check for a NULL entry, used to indicate that the parameter goes
1908 both on the stack and in registers. */
1909 if (XEXP (XVECEXP (src, 0, 0), 0))
1910 start = 0;
1911 else
1912 start = 1;
1913 finish = XVECLEN (src, 0);
1915 tmps = XALLOCAVEC (rtx, finish);
1917 /* Copy the (probable) hard regs into pseudos. */
1918 for (i = start; i < finish; i++)
1920 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1921 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1923 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1924 emit_move_insn (tmps[i], reg);
1926 else
1927 tmps[i] = reg;
1930 /* If we won't be storing directly into memory, protect the real destination
1931 from strange tricks we might play. */
1932 dst = orig_dst;
1933 if (GET_CODE (dst) == PARALLEL)
1935 rtx temp;
1937 /* We can get a PARALLEL dst if there is a conditional expression in
1938 a return statement. In that case, the dst and src are the same,
1939 so no action is necessary. */
1940 if (rtx_equal_p (dst, src))
1941 return;
1943 /* It is unclear if we can ever reach here, but we may as well handle
1944 it. Allocate a temporary, and split this into a store/load to/from
1945 the temporary. */
1946 temp = assign_stack_temp (GET_MODE (dst), ssize);
1947 emit_group_store (temp, src, type, ssize);
1948 emit_group_load (dst, temp, type, ssize);
1949 return;
1951 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1953 machine_mode outer = GET_MODE (dst);
1954 machine_mode inner;
1955 HOST_WIDE_INT bytepos;
1956 bool done = false;
1957 rtx temp;
1959 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1960 dst = gen_reg_rtx (outer);
1962 /* Make life a bit easier for combine. */
1963 /* If the first element of the vector is the low part
1964 of the destination mode, use a paradoxical subreg to
1965 initialize the destination. */
1966 if (start < finish)
1968 inner = GET_MODE (tmps[start]);
1969 bytepos = subreg_lowpart_offset (inner, outer);
1970 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1972 temp = simplify_gen_subreg (outer, tmps[start],
1973 inner, 0);
1974 if (temp)
1976 emit_move_insn (dst, temp);
1977 done = true;
1978 start++;
1983 /* If the first element wasn't the low part, try the last. */
1984 if (!done
1985 && start < finish - 1)
1987 inner = GET_MODE (tmps[finish - 1]);
1988 bytepos = subreg_lowpart_offset (inner, outer);
1989 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1991 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1992 inner, 0);
1993 if (temp)
1995 emit_move_insn (dst, temp);
1996 done = true;
1997 finish--;
2002 /* Otherwise, simply initialize the result to zero. */
2003 if (!done)
2004 emit_move_insn (dst, CONST0_RTX (outer));
2007 /* Process the pieces. */
2008 for (i = start; i < finish; i++)
2010 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2011 machine_mode mode = GET_MODE (tmps[i]);
2012 unsigned int bytelen = GET_MODE_SIZE (mode);
2013 unsigned int adj_bytelen;
2014 rtx dest = dst;
2016 /* Handle trailing fragments that run over the size of the struct. */
2017 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2018 adj_bytelen = ssize - bytepos;
2019 else
2020 adj_bytelen = bytelen;
2022 if (GET_CODE (dst) == CONCAT)
2024 if (bytepos + adj_bytelen
2025 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2026 dest = XEXP (dst, 0);
2027 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2029 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2030 dest = XEXP (dst, 1);
2032 else
2034 machine_mode dest_mode = GET_MODE (dest);
2035 machine_mode tmp_mode = GET_MODE (tmps[i]);
2037 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2039 if (GET_MODE_ALIGNMENT (dest_mode)
2040 >= GET_MODE_ALIGNMENT (tmp_mode))
2042 dest = assign_stack_temp (dest_mode,
2043 GET_MODE_SIZE (dest_mode));
2044 emit_move_insn (adjust_address (dest,
2045 tmp_mode,
2046 bytepos),
2047 tmps[i]);
2048 dst = dest;
2050 else
2052 dest = assign_stack_temp (tmp_mode,
2053 GET_MODE_SIZE (tmp_mode));
2054 emit_move_insn (dest, tmps[i]);
2055 dst = adjust_address (dest, dest_mode, bytepos);
2057 break;
2061 /* Handle trailing fragments that run over the size of the struct. */
2062 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2064 /* store_bit_field always takes its value from the lsb.
2065 Move the fragment to the lsb if it's not already there. */
2066 if (
2067 #ifdef BLOCK_REG_PADDING
2068 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2069 == (BYTES_BIG_ENDIAN ? upward : downward)
2070 #else
2071 BYTES_BIG_ENDIAN
2072 #endif
2075 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2076 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2077 shift, tmps[i], 0);
2080 /* Make sure not to write past the end of the struct. */
2081 store_bit_field (dest,
2082 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2083 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2084 VOIDmode, tmps[i]);
2087 /* Optimize the access just a bit. */
2088 else if (MEM_P (dest)
2089 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2090 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2091 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2092 && bytelen == GET_MODE_SIZE (mode))
2093 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2095 else
2096 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2097 0, 0, mode, tmps[i]);
2100 /* Copy from the pseudo into the (probable) hard reg. */
2101 if (orig_dst != dst)
2102 emit_move_insn (orig_dst, dst);
2105 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2106 of the value stored in X. */
2109 maybe_emit_group_store (rtx x, tree type)
2111 machine_mode mode = TYPE_MODE (type);
2112 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2113 if (GET_CODE (x) == PARALLEL)
2115 rtx result = gen_reg_rtx (mode);
2116 emit_group_store (result, x, type, int_size_in_bytes (type));
2117 return result;
2119 return x;
2122 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2124 This is used on targets that return BLKmode values in registers. */
2126 void
2127 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2129 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2130 rtx src = NULL, dst = NULL;
2131 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2132 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2133 machine_mode mode = GET_MODE (srcreg);
2134 machine_mode tmode = GET_MODE (target);
2135 machine_mode copy_mode;
2137 /* BLKmode registers created in the back-end shouldn't have survived. */
2138 gcc_assert (mode != BLKmode);
2140 /* If the structure doesn't take up a whole number of words, see whether
2141 SRCREG is padded on the left or on the right. If it's on the left,
2142 set PADDING_CORRECTION to the number of bits to skip.
2144 In most ABIs, the structure will be returned at the least end of
2145 the register, which translates to right padding on little-endian
2146 targets and left padding on big-endian targets. The opposite
2147 holds if the structure is returned at the most significant
2148 end of the register. */
2149 if (bytes % UNITS_PER_WORD != 0
2150 && (targetm.calls.return_in_msb (type)
2151 ? !BYTES_BIG_ENDIAN
2152 : BYTES_BIG_ENDIAN))
2153 padding_correction
2154 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2156 /* We can use a single move if we have an exact mode for the size. */
2157 else if (MEM_P (target)
2158 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2159 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2160 && bytes == GET_MODE_SIZE (mode))
2162 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2163 return;
2166 /* And if we additionally have the same mode for a register. */
2167 else if (REG_P (target)
2168 && GET_MODE (target) == mode
2169 && bytes == GET_MODE_SIZE (mode))
2171 emit_move_insn (target, srcreg);
2172 return;
2175 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2176 into a new pseudo which is a full word. */
2177 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2179 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2180 mode = word_mode;
2183 /* Copy the structure BITSIZE bits at a time. If the target lives in
2184 memory, take care of not reading/writing past its end by selecting
2185 a copy mode suited to BITSIZE. This should always be possible given
2186 how it is computed.
2188 If the target lives in register, make sure not to select a copy mode
2189 larger than the mode of the register.
2191 We could probably emit more efficient code for machines which do not use
2192 strict alignment, but it doesn't seem worth the effort at the current
2193 time. */
2195 copy_mode = word_mode;
2196 if (MEM_P (target))
2198 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2199 if (mem_mode != BLKmode)
2200 copy_mode = mem_mode;
2202 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2203 copy_mode = tmode;
2205 for (bitpos = 0, xbitpos = padding_correction;
2206 bitpos < bytes * BITS_PER_UNIT;
2207 bitpos += bitsize, xbitpos += bitsize)
2209 /* We need a new source operand each time xbitpos is on a
2210 word boundary and when xbitpos == padding_correction
2211 (the first time through). */
2212 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2213 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2215 /* We need a new destination operand each time bitpos is on
2216 a word boundary. */
2217 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2218 dst = target;
2219 else if (bitpos % BITS_PER_WORD == 0)
2220 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2222 /* Use xbitpos for the source extraction (right justified) and
2223 bitpos for the destination store (left justified). */
2224 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2225 extract_bit_field (src, bitsize,
2226 xbitpos % BITS_PER_WORD, 1,
2227 NULL_RTX, copy_mode, copy_mode));
2231 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2232 register if it contains any data, otherwise return null.
2234 This is used on targets that return BLKmode values in registers. */
2237 copy_blkmode_to_reg (machine_mode mode, tree src)
2239 int i, n_regs;
2240 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2241 unsigned int bitsize;
2242 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2243 machine_mode dst_mode;
2245 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2247 x = expand_normal (src);
2249 bytes = int_size_in_bytes (TREE_TYPE (src));
2250 if (bytes == 0)
2251 return NULL_RTX;
2253 /* If the structure doesn't take up a whole number of words, see
2254 whether the register value should be padded on the left or on
2255 the right. Set PADDING_CORRECTION to the number of padding
2256 bits needed on the left side.
2258 In most ABIs, the structure will be returned at the least end of
2259 the register, which translates to right padding on little-endian
2260 targets and left padding on big-endian targets. The opposite
2261 holds if the structure is returned at the most significant
2262 end of the register. */
2263 if (bytes % UNITS_PER_WORD != 0
2264 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2265 ? !BYTES_BIG_ENDIAN
2266 : BYTES_BIG_ENDIAN))
2267 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2268 * BITS_PER_UNIT));
2270 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2271 dst_words = XALLOCAVEC (rtx, n_regs);
2272 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2274 /* Copy the structure BITSIZE bits at a time. */
2275 for (bitpos = 0, xbitpos = padding_correction;
2276 bitpos < bytes * BITS_PER_UNIT;
2277 bitpos += bitsize, xbitpos += bitsize)
2279 /* We need a new destination pseudo each time xbitpos is
2280 on a word boundary and when xbitpos == padding_correction
2281 (the first time through). */
2282 if (xbitpos % BITS_PER_WORD == 0
2283 || xbitpos == padding_correction)
2285 /* Generate an appropriate register. */
2286 dst_word = gen_reg_rtx (word_mode);
2287 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2289 /* Clear the destination before we move anything into it. */
2290 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2293 /* We need a new source operand each time bitpos is on a word
2294 boundary. */
2295 if (bitpos % BITS_PER_WORD == 0)
2296 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2298 /* Use bitpos for the source extraction (left justified) and
2299 xbitpos for the destination store (right justified). */
2300 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2301 0, 0, word_mode,
2302 extract_bit_field (src_word, bitsize,
2303 bitpos % BITS_PER_WORD, 1,
2304 NULL_RTX, word_mode, word_mode));
2307 if (mode == BLKmode)
2309 /* Find the smallest integer mode large enough to hold the
2310 entire structure. */
2311 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2312 mode != VOIDmode;
2313 mode = GET_MODE_WIDER_MODE (mode))
2314 /* Have we found a large enough mode? */
2315 if (GET_MODE_SIZE (mode) >= bytes)
2316 break;
2318 /* A suitable mode should have been found. */
2319 gcc_assert (mode != VOIDmode);
2322 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2323 dst_mode = word_mode;
2324 else
2325 dst_mode = mode;
2326 dst = gen_reg_rtx (dst_mode);
2328 for (i = 0; i < n_regs; i++)
2329 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2331 if (mode != dst_mode)
2332 dst = gen_lowpart (mode, dst);
2334 return dst;
2337 /* Add a USE expression for REG to the (possibly empty) list pointed
2338 to by CALL_FUSAGE. REG must denote a hard register. */
2340 void
2341 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2343 gcc_assert (REG_P (reg));
2345 if (!HARD_REGISTER_P (reg))
2346 return;
2348 *call_fusage
2349 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2352 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2353 to by CALL_FUSAGE. REG must denote a hard register. */
2355 void
2356 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2358 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2360 *call_fusage
2361 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2364 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2365 starting at REGNO. All of these registers must be hard registers. */
2367 void
2368 use_regs (rtx *call_fusage, int regno, int nregs)
2370 int i;
2372 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2374 for (i = 0; i < nregs; i++)
2375 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2378 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2379 PARALLEL REGS. This is for calls that pass values in multiple
2380 non-contiguous locations. The Irix 6 ABI has examples of this. */
2382 void
2383 use_group_regs (rtx *call_fusage, rtx regs)
2385 int i;
2387 for (i = 0; i < XVECLEN (regs, 0); i++)
2389 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2391 /* A NULL entry means the parameter goes both on the stack and in
2392 registers. This can also be a MEM for targets that pass values
2393 partially on the stack and partially in registers. */
2394 if (reg != 0 && REG_P (reg))
2395 use_reg (call_fusage, reg);
2399 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2400 assigment and the code of the expresion on the RHS is CODE. Return
2401 NULL otherwise. */
2403 static gimple
2404 get_def_for_expr (tree name, enum tree_code code)
2406 gimple def_stmt;
2408 if (TREE_CODE (name) != SSA_NAME)
2409 return NULL;
2411 def_stmt = get_gimple_for_ssa_name (name);
2412 if (!def_stmt
2413 || gimple_assign_rhs_code (def_stmt) != code)
2414 return NULL;
2416 return def_stmt;
2419 #ifdef HAVE_conditional_move
2420 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2421 assigment and the class of the expresion on the RHS is CLASS. Return
2422 NULL otherwise. */
2424 static gimple
2425 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2427 gimple def_stmt;
2429 if (TREE_CODE (name) != SSA_NAME)
2430 return NULL;
2432 def_stmt = get_gimple_for_ssa_name (name);
2433 if (!def_stmt
2434 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2435 return NULL;
2437 return def_stmt;
2439 #endif
2442 /* Determine whether the LEN bytes generated by CONSTFUN can be
2443 stored to memory using several move instructions. CONSTFUNDATA is
2444 a pointer which will be passed as argument in every CONSTFUN call.
2445 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2446 a memset operation and false if it's a copy of a constant string.
2447 Return nonzero if a call to store_by_pieces should succeed. */
2450 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2451 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2452 void *constfundata, unsigned int align, bool memsetp)
2454 unsigned HOST_WIDE_INT l;
2455 unsigned int max_size;
2456 HOST_WIDE_INT offset = 0;
2457 machine_mode mode;
2458 enum insn_code icode;
2459 int reverse;
2460 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2461 rtx cst ATTRIBUTE_UNUSED;
2463 if (len == 0)
2464 return 1;
2466 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2467 memsetp
2468 ? SET_BY_PIECES
2469 : STORE_BY_PIECES,
2470 optimize_insn_for_speed_p ()))
2471 return 0;
2473 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2475 /* We would first store what we can in the largest integer mode, then go to
2476 successively smaller modes. */
2478 for (reverse = 0;
2479 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2480 reverse++)
2482 l = len;
2483 max_size = STORE_MAX_PIECES + 1;
2484 while (max_size > 1 && l > 0)
2486 mode = widest_int_mode_for_size (max_size);
2488 if (mode == VOIDmode)
2489 break;
2491 icode = optab_handler (mov_optab, mode);
2492 if (icode != CODE_FOR_nothing
2493 && align >= GET_MODE_ALIGNMENT (mode))
2495 unsigned int size = GET_MODE_SIZE (mode);
2497 while (l >= size)
2499 if (reverse)
2500 offset -= size;
2502 cst = (*constfun) (constfundata, offset, mode);
2503 if (!targetm.legitimate_constant_p (mode, cst))
2504 return 0;
2506 if (!reverse)
2507 offset += size;
2509 l -= size;
2513 max_size = GET_MODE_SIZE (mode);
2516 /* The code above should have handled everything. */
2517 gcc_assert (!l);
2520 return 1;
2523 /* Generate several move instructions to store LEN bytes generated by
2524 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2525 pointer which will be passed as argument in every CONSTFUN call.
2526 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2527 a memset operation and false if it's a copy of a constant string.
2528 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2529 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2530 stpcpy. */
2533 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2534 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2535 void *constfundata, unsigned int align, bool memsetp, int endp)
2537 machine_mode to_addr_mode = get_address_mode (to);
2538 struct store_by_pieces_d data;
2540 if (len == 0)
2542 gcc_assert (endp != 2);
2543 return to;
2546 gcc_assert (targetm.use_by_pieces_infrastructure_p
2547 (len, align,
2548 memsetp
2549 ? SET_BY_PIECES
2550 : STORE_BY_PIECES,
2551 optimize_insn_for_speed_p ()));
2553 data.constfun = constfun;
2554 data.constfundata = constfundata;
2555 data.len = len;
2556 data.to = to;
2557 store_by_pieces_1 (&data, align);
2558 if (endp)
2560 rtx to1;
2562 gcc_assert (!data.reverse);
2563 if (data.autinc_to)
2565 if (endp == 2)
2567 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2568 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2569 else
2570 data.to_addr = copy_to_mode_reg (to_addr_mode,
2571 plus_constant (to_addr_mode,
2572 data.to_addr,
2573 -1));
2575 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2576 data.offset);
2578 else
2580 if (endp == 2)
2581 --data.offset;
2582 to1 = adjust_address (data.to, QImode, data.offset);
2584 return to1;
2586 else
2587 return data.to;
2590 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2591 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2593 static void
2594 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2596 struct store_by_pieces_d data;
2598 if (len == 0)
2599 return;
2601 data.constfun = clear_by_pieces_1;
2602 data.constfundata = NULL;
2603 data.len = len;
2604 data.to = to;
2605 store_by_pieces_1 (&data, align);
2608 /* Callback routine for clear_by_pieces.
2609 Return const0_rtx unconditionally. */
2611 static rtx
2612 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2613 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2614 machine_mode mode ATTRIBUTE_UNUSED)
2616 return const0_rtx;
2619 /* Subroutine of clear_by_pieces and store_by_pieces.
2620 Generate several move instructions to store LEN bytes of block TO. (A MEM
2621 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2623 static void
2624 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2625 unsigned int align ATTRIBUTE_UNUSED)
2627 machine_mode to_addr_mode = get_address_mode (data->to);
2628 rtx to_addr = XEXP (data->to, 0);
2629 unsigned int max_size = STORE_MAX_PIECES + 1;
2630 enum insn_code icode;
2632 data->offset = 0;
2633 data->to_addr = to_addr;
2634 data->autinc_to
2635 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2636 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2638 data->explicit_inc_to = 0;
2639 data->reverse
2640 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2641 if (data->reverse)
2642 data->offset = data->len;
2644 /* If storing requires more than two move insns,
2645 copy addresses to registers (to make displacements shorter)
2646 and use post-increment if available. */
2647 if (!data->autinc_to
2648 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2650 /* Determine the main mode we'll be using.
2651 MODE might not be used depending on the definitions of the
2652 USE_* macros below. */
2653 machine_mode mode ATTRIBUTE_UNUSED
2654 = widest_int_mode_for_size (max_size);
2656 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2658 data->to_addr = copy_to_mode_reg (to_addr_mode,
2659 plus_constant (to_addr_mode,
2660 to_addr,
2661 data->len));
2662 data->autinc_to = 1;
2663 data->explicit_inc_to = -1;
2666 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2667 && ! data->autinc_to)
2669 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2670 data->autinc_to = 1;
2671 data->explicit_inc_to = 1;
2674 if ( !data->autinc_to && CONSTANT_P (to_addr))
2675 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2678 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2680 /* First store what we can in the largest integer mode, then go to
2681 successively smaller modes. */
2683 while (max_size > 1 && data->len > 0)
2685 machine_mode mode = widest_int_mode_for_size (max_size);
2687 if (mode == VOIDmode)
2688 break;
2690 icode = optab_handler (mov_optab, mode);
2691 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2692 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2694 max_size = GET_MODE_SIZE (mode);
2697 /* The code above should have handled everything. */
2698 gcc_assert (!data->len);
2701 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2702 with move instructions for mode MODE. GENFUN is the gen_... function
2703 to make a move insn for that mode. DATA has all the other info. */
2705 static void
2706 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2707 struct store_by_pieces_d *data)
2709 unsigned int size = GET_MODE_SIZE (mode);
2710 rtx to1, cst;
2712 while (data->len >= size)
2714 if (data->reverse)
2715 data->offset -= size;
2717 if (data->autinc_to)
2718 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2719 data->offset);
2720 else
2721 to1 = adjust_address (data->to, mode, data->offset);
2723 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2724 emit_insn (gen_add2_insn (data->to_addr,
2725 gen_int_mode (-(HOST_WIDE_INT) size,
2726 GET_MODE (data->to_addr))));
2728 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2729 emit_insn ((*genfun) (to1, cst));
2731 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2732 emit_insn (gen_add2_insn (data->to_addr,
2733 gen_int_mode (size,
2734 GET_MODE (data->to_addr))));
2736 if (! data->reverse)
2737 data->offset += size;
2739 data->len -= size;
2743 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2744 its length in bytes. */
2747 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2748 unsigned int expected_align, HOST_WIDE_INT expected_size,
2749 unsigned HOST_WIDE_INT min_size,
2750 unsigned HOST_WIDE_INT max_size,
2751 unsigned HOST_WIDE_INT probable_max_size)
2753 machine_mode mode = GET_MODE (object);
2754 unsigned int align;
2756 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2758 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2759 just move a zero. Otherwise, do this a piece at a time. */
2760 if (mode != BLKmode
2761 && CONST_INT_P (size)
2762 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2764 rtx zero = CONST0_RTX (mode);
2765 if (zero != NULL)
2767 emit_move_insn (object, zero);
2768 return NULL;
2771 if (COMPLEX_MODE_P (mode))
2773 zero = CONST0_RTX (GET_MODE_INNER (mode));
2774 if (zero != NULL)
2776 write_complex_part (object, zero, 0);
2777 write_complex_part (object, zero, 1);
2778 return NULL;
2783 if (size == const0_rtx)
2784 return NULL;
2786 align = MEM_ALIGN (object);
2788 if (CONST_INT_P (size)
2789 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2790 CLEAR_BY_PIECES,
2791 optimize_insn_for_speed_p ()))
2792 clear_by_pieces (object, INTVAL (size), align);
2793 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2794 expected_align, expected_size,
2795 min_size, max_size, probable_max_size))
2797 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2798 return set_storage_via_libcall (object, size, const0_rtx,
2799 method == BLOCK_OP_TAILCALL);
2800 else
2801 gcc_unreachable ();
2803 return NULL;
2807 clear_storage (rtx object, rtx size, enum block_op_methods method)
2809 unsigned HOST_WIDE_INT max, min = 0;
2810 if (GET_CODE (size) == CONST_INT)
2811 min = max = UINTVAL (size);
2812 else
2813 max = GET_MODE_MASK (GET_MODE (size));
2814 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2818 /* A subroutine of clear_storage. Expand a call to memset.
2819 Return the return value of memset, 0 otherwise. */
2822 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2824 tree call_expr, fn, object_tree, size_tree, val_tree;
2825 machine_mode size_mode;
2826 rtx retval;
2828 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2829 place those into new pseudos into a VAR_DECL and use them later. */
2831 object = copy_addr_to_reg (XEXP (object, 0));
2833 size_mode = TYPE_MODE (sizetype);
2834 size = convert_to_mode (size_mode, size, 1);
2835 size = copy_to_mode_reg (size_mode, size);
2837 /* It is incorrect to use the libcall calling conventions to call
2838 memset in this context. This could be a user call to memset and
2839 the user may wish to examine the return value from memset. For
2840 targets where libcalls and normal calls have different conventions
2841 for returning pointers, we could end up generating incorrect code. */
2843 object_tree = make_tree (ptr_type_node, object);
2844 if (!CONST_INT_P (val))
2845 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2846 size_tree = make_tree (sizetype, size);
2847 val_tree = make_tree (integer_type_node, val);
2849 fn = clear_storage_libcall_fn (true);
2850 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2851 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2853 retval = expand_normal (call_expr);
2855 return retval;
2858 /* A subroutine of set_storage_via_libcall. Create the tree node
2859 for the function we use for block clears. */
2861 tree block_clear_fn;
2863 void
2864 init_block_clear_fn (const char *asmspec)
2866 if (!block_clear_fn)
2868 tree fn, args;
2870 fn = get_identifier ("memset");
2871 args = build_function_type_list (ptr_type_node, ptr_type_node,
2872 integer_type_node, sizetype,
2873 NULL_TREE);
2875 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2876 DECL_EXTERNAL (fn) = 1;
2877 TREE_PUBLIC (fn) = 1;
2878 DECL_ARTIFICIAL (fn) = 1;
2879 TREE_NOTHROW (fn) = 1;
2880 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2881 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2883 block_clear_fn = fn;
2886 if (asmspec)
2887 set_user_assembler_name (block_clear_fn, asmspec);
2890 static tree
2891 clear_storage_libcall_fn (int for_call)
2893 static bool emitted_extern;
2895 if (!block_clear_fn)
2896 init_block_clear_fn (NULL);
2898 if (for_call && !emitted_extern)
2900 emitted_extern = true;
2901 make_decl_rtl (block_clear_fn);
2904 return block_clear_fn;
2907 /* Expand a setmem pattern; return true if successful. */
2909 bool
2910 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2911 unsigned int expected_align, HOST_WIDE_INT expected_size,
2912 unsigned HOST_WIDE_INT min_size,
2913 unsigned HOST_WIDE_INT max_size,
2914 unsigned HOST_WIDE_INT probable_max_size)
2916 /* Try the most limited insn first, because there's no point
2917 including more than one in the machine description unless
2918 the more limited one has some advantage. */
2920 machine_mode mode;
2922 if (expected_align < align)
2923 expected_align = align;
2924 if (expected_size != -1)
2926 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2927 expected_size = max_size;
2928 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2929 expected_size = min_size;
2932 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2933 mode = GET_MODE_WIDER_MODE (mode))
2935 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2937 if (code != CODE_FOR_nothing
2938 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2939 here because if SIZE is less than the mode mask, as it is
2940 returned by the macro, it will definitely be less than the
2941 actual mode mask. Since SIZE is within the Pmode address
2942 space, we limit MODE to Pmode. */
2943 && ((CONST_INT_P (size)
2944 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2945 <= (GET_MODE_MASK (mode) >> 1)))
2946 || max_size <= (GET_MODE_MASK (mode) >> 1)
2947 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2949 struct expand_operand ops[9];
2950 unsigned int nops;
2952 nops = insn_data[(int) code].n_generator_args;
2953 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2955 create_fixed_operand (&ops[0], object);
2956 /* The check above guarantees that this size conversion is valid. */
2957 create_convert_operand_to (&ops[1], size, mode, true);
2958 create_convert_operand_from (&ops[2], val, byte_mode, true);
2959 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2960 if (nops >= 6)
2962 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2963 create_integer_operand (&ops[5], expected_size);
2965 if (nops >= 8)
2967 create_integer_operand (&ops[6], min_size);
2968 /* If we can not represent the maximal size,
2969 make parameter NULL. */
2970 if ((HOST_WIDE_INT) max_size != -1)
2971 create_integer_operand (&ops[7], max_size);
2972 else
2973 create_fixed_operand (&ops[7], NULL);
2975 if (nops == 9)
2977 /* If we can not represent the maximal size,
2978 make parameter NULL. */
2979 if ((HOST_WIDE_INT) probable_max_size != -1)
2980 create_integer_operand (&ops[8], probable_max_size);
2981 else
2982 create_fixed_operand (&ops[8], NULL);
2984 if (maybe_expand_insn (code, nops, ops))
2985 return true;
2989 return false;
2993 /* Write to one of the components of the complex value CPLX. Write VAL to
2994 the real part if IMAG_P is false, and the imaginary part if its true. */
2996 void
2997 write_complex_part (rtx cplx, rtx val, bool imag_p)
2999 machine_mode cmode;
3000 machine_mode imode;
3001 unsigned ibitsize;
3003 if (GET_CODE (cplx) == CONCAT)
3005 emit_move_insn (XEXP (cplx, imag_p), val);
3006 return;
3009 cmode = GET_MODE (cplx);
3010 imode = GET_MODE_INNER (cmode);
3011 ibitsize = GET_MODE_BITSIZE (imode);
3013 /* For MEMs simplify_gen_subreg may generate an invalid new address
3014 because, e.g., the original address is considered mode-dependent
3015 by the target, which restricts simplify_subreg from invoking
3016 adjust_address_nv. Instead of preparing fallback support for an
3017 invalid address, we call adjust_address_nv directly. */
3018 if (MEM_P (cplx))
3020 emit_move_insn (adjust_address_nv (cplx, imode,
3021 imag_p ? GET_MODE_SIZE (imode) : 0),
3022 val);
3023 return;
3026 /* If the sub-object is at least word sized, then we know that subregging
3027 will work. This special case is important, since store_bit_field
3028 wants to operate on integer modes, and there's rarely an OImode to
3029 correspond to TCmode. */
3030 if (ibitsize >= BITS_PER_WORD
3031 /* For hard regs we have exact predicates. Assume we can split
3032 the original object if it spans an even number of hard regs.
3033 This special case is important for SCmode on 64-bit platforms
3034 where the natural size of floating-point regs is 32-bit. */
3035 || (REG_P (cplx)
3036 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3037 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3039 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3040 imag_p ? GET_MODE_SIZE (imode) : 0);
3041 if (part)
3043 emit_move_insn (part, val);
3044 return;
3046 else
3047 /* simplify_gen_subreg may fail for sub-word MEMs. */
3048 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3051 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3054 /* Extract one of the components of the complex value CPLX. Extract the
3055 real part if IMAG_P is false, and the imaginary part if it's true. */
3057 static rtx
3058 read_complex_part (rtx cplx, bool imag_p)
3060 machine_mode cmode, imode;
3061 unsigned ibitsize;
3063 if (GET_CODE (cplx) == CONCAT)
3064 return XEXP (cplx, imag_p);
3066 cmode = GET_MODE (cplx);
3067 imode = GET_MODE_INNER (cmode);
3068 ibitsize = GET_MODE_BITSIZE (imode);
3070 /* Special case reads from complex constants that got spilled to memory. */
3071 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3073 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3074 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3076 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3077 if (CONSTANT_CLASS_P (part))
3078 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3082 /* For MEMs simplify_gen_subreg may generate an invalid new address
3083 because, e.g., the original address is considered mode-dependent
3084 by the target, which restricts simplify_subreg from invoking
3085 adjust_address_nv. Instead of preparing fallback support for an
3086 invalid address, we call adjust_address_nv directly. */
3087 if (MEM_P (cplx))
3088 return adjust_address_nv (cplx, imode,
3089 imag_p ? GET_MODE_SIZE (imode) : 0);
3091 /* If the sub-object is at least word sized, then we know that subregging
3092 will work. This special case is important, since extract_bit_field
3093 wants to operate on integer modes, and there's rarely an OImode to
3094 correspond to TCmode. */
3095 if (ibitsize >= BITS_PER_WORD
3096 /* For hard regs we have exact predicates. Assume we can split
3097 the original object if it spans an even number of hard regs.
3098 This special case is important for SCmode on 64-bit platforms
3099 where the natural size of floating-point regs is 32-bit. */
3100 || (REG_P (cplx)
3101 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3102 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3104 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3105 imag_p ? GET_MODE_SIZE (imode) : 0);
3106 if (ret)
3107 return ret;
3108 else
3109 /* simplify_gen_subreg may fail for sub-word MEMs. */
3110 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3113 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3114 true, NULL_RTX, imode, imode);
3117 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3118 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3119 represented in NEW_MODE. If FORCE is true, this will never happen, as
3120 we'll force-create a SUBREG if needed. */
3122 static rtx
3123 emit_move_change_mode (machine_mode new_mode,
3124 machine_mode old_mode, rtx x, bool force)
3126 rtx ret;
3128 if (push_operand (x, GET_MODE (x)))
3130 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3131 MEM_COPY_ATTRIBUTES (ret, x);
3133 else if (MEM_P (x))
3135 /* We don't have to worry about changing the address since the
3136 size in bytes is supposed to be the same. */
3137 if (reload_in_progress)
3139 /* Copy the MEM to change the mode and move any
3140 substitutions from the old MEM to the new one. */
3141 ret = adjust_address_nv (x, new_mode, 0);
3142 copy_replacements (x, ret);
3144 else
3145 ret = adjust_address (x, new_mode, 0);
3147 else
3149 /* Note that we do want simplify_subreg's behavior of validating
3150 that the new mode is ok for a hard register. If we were to use
3151 simplify_gen_subreg, we would create the subreg, but would
3152 probably run into the target not being able to implement it. */
3153 /* Except, of course, when FORCE is true, when this is exactly what
3154 we want. Which is needed for CCmodes on some targets. */
3155 if (force)
3156 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3157 else
3158 ret = simplify_subreg (new_mode, x, old_mode, 0);
3161 return ret;
3164 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3165 an integer mode of the same size as MODE. Returns the instruction
3166 emitted, or NULL if such a move could not be generated. */
3168 static rtx_insn *
3169 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3171 machine_mode imode;
3172 enum insn_code code;
3174 /* There must exist a mode of the exact size we require. */
3175 imode = int_mode_for_mode (mode);
3176 if (imode == BLKmode)
3177 return NULL;
3179 /* The target must support moves in this mode. */
3180 code = optab_handler (mov_optab, imode);
3181 if (code == CODE_FOR_nothing)
3182 return NULL;
3184 x = emit_move_change_mode (imode, mode, x, force);
3185 if (x == NULL_RTX)
3186 return NULL;
3187 y = emit_move_change_mode (imode, mode, y, force);
3188 if (y == NULL_RTX)
3189 return NULL;
3190 return emit_insn (GEN_FCN (code) (x, y));
3193 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3194 Return an equivalent MEM that does not use an auto-increment. */
3197 emit_move_resolve_push (machine_mode mode, rtx x)
3199 enum rtx_code code = GET_CODE (XEXP (x, 0));
3200 HOST_WIDE_INT adjust;
3201 rtx temp;
3203 adjust = GET_MODE_SIZE (mode);
3204 #ifdef PUSH_ROUNDING
3205 adjust = PUSH_ROUNDING (adjust);
3206 #endif
3207 if (code == PRE_DEC || code == POST_DEC)
3208 adjust = -adjust;
3209 else if (code == PRE_MODIFY || code == POST_MODIFY)
3211 rtx expr = XEXP (XEXP (x, 0), 1);
3212 HOST_WIDE_INT val;
3214 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3215 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3216 val = INTVAL (XEXP (expr, 1));
3217 if (GET_CODE (expr) == MINUS)
3218 val = -val;
3219 gcc_assert (adjust == val || adjust == -val);
3220 adjust = val;
3223 /* Do not use anti_adjust_stack, since we don't want to update
3224 stack_pointer_delta. */
3225 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3226 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3227 0, OPTAB_LIB_WIDEN);
3228 if (temp != stack_pointer_rtx)
3229 emit_move_insn (stack_pointer_rtx, temp);
3231 switch (code)
3233 case PRE_INC:
3234 case PRE_DEC:
3235 case PRE_MODIFY:
3236 temp = stack_pointer_rtx;
3237 break;
3238 case POST_INC:
3239 case POST_DEC:
3240 case POST_MODIFY:
3241 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3242 break;
3243 default:
3244 gcc_unreachable ();
3247 return replace_equiv_address (x, temp);
3250 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3251 X is known to satisfy push_operand, and MODE is known to be complex.
3252 Returns the last instruction emitted. */
3254 rtx_insn *
3255 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3257 machine_mode submode = GET_MODE_INNER (mode);
3258 bool imag_first;
3260 #ifdef PUSH_ROUNDING
3261 unsigned int submodesize = GET_MODE_SIZE (submode);
3263 /* In case we output to the stack, but the size is smaller than the
3264 machine can push exactly, we need to use move instructions. */
3265 if (PUSH_ROUNDING (submodesize) != submodesize)
3267 x = emit_move_resolve_push (mode, x);
3268 return emit_move_insn (x, y);
3270 #endif
3272 /* Note that the real part always precedes the imag part in memory
3273 regardless of machine's endianness. */
3274 switch (GET_CODE (XEXP (x, 0)))
3276 case PRE_DEC:
3277 case POST_DEC:
3278 imag_first = true;
3279 break;
3280 case PRE_INC:
3281 case POST_INC:
3282 imag_first = false;
3283 break;
3284 default:
3285 gcc_unreachable ();
3288 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3289 read_complex_part (y, imag_first));
3290 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3291 read_complex_part (y, !imag_first));
3294 /* A subroutine of emit_move_complex. Perform the move from Y to X
3295 via two moves of the parts. Returns the last instruction emitted. */
3297 rtx_insn *
3298 emit_move_complex_parts (rtx x, rtx y)
3300 /* Show the output dies here. This is necessary for SUBREGs
3301 of pseudos since we cannot track their lifetimes correctly;
3302 hard regs shouldn't appear here except as return values. */
3303 if (!reload_completed && !reload_in_progress
3304 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3305 emit_clobber (x);
3307 write_complex_part (x, read_complex_part (y, false), false);
3308 write_complex_part (x, read_complex_part (y, true), true);
3310 return get_last_insn ();
3313 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3314 MODE is known to be complex. Returns the last instruction emitted. */
3316 static rtx_insn *
3317 emit_move_complex (machine_mode mode, rtx x, rtx y)
3319 bool try_int;
3321 /* Need to take special care for pushes, to maintain proper ordering
3322 of the data, and possibly extra padding. */
3323 if (push_operand (x, mode))
3324 return emit_move_complex_push (mode, x, y);
3326 /* See if we can coerce the target into moving both values at once, except
3327 for floating point where we favor moving as parts if this is easy. */
3328 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3329 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3330 && !(REG_P (x)
3331 && HARD_REGISTER_P (x)
3332 && hard_regno_nregs[REGNO (x)][mode] == 1)
3333 && !(REG_P (y)
3334 && HARD_REGISTER_P (y)
3335 && hard_regno_nregs[REGNO (y)][mode] == 1))
3336 try_int = false;
3337 /* Not possible if the values are inherently not adjacent. */
3338 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3339 try_int = false;
3340 /* Is possible if both are registers (or subregs of registers). */
3341 else if (register_operand (x, mode) && register_operand (y, mode))
3342 try_int = true;
3343 /* If one of the operands is a memory, and alignment constraints
3344 are friendly enough, we may be able to do combined memory operations.
3345 We do not attempt this if Y is a constant because that combination is
3346 usually better with the by-parts thing below. */
3347 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3348 && (!STRICT_ALIGNMENT
3349 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3350 try_int = true;
3351 else
3352 try_int = false;
3354 if (try_int)
3356 rtx_insn *ret;
3358 /* For memory to memory moves, optimal behavior can be had with the
3359 existing block move logic. */
3360 if (MEM_P (x) && MEM_P (y))
3362 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3363 BLOCK_OP_NO_LIBCALL);
3364 return get_last_insn ();
3367 ret = emit_move_via_integer (mode, x, y, true);
3368 if (ret)
3369 return ret;
3372 return emit_move_complex_parts (x, y);
3375 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3376 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3378 static rtx_insn *
3379 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3381 rtx_insn *ret;
3383 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3384 if (mode != CCmode)
3386 enum insn_code code = optab_handler (mov_optab, CCmode);
3387 if (code != CODE_FOR_nothing)
3389 x = emit_move_change_mode (CCmode, mode, x, true);
3390 y = emit_move_change_mode (CCmode, mode, y, true);
3391 return emit_insn (GEN_FCN (code) (x, y));
3395 /* Otherwise, find the MODE_INT mode of the same width. */
3396 ret = emit_move_via_integer (mode, x, y, false);
3397 gcc_assert (ret != NULL);
3398 return ret;
3401 /* Return true if word I of OP lies entirely in the
3402 undefined bits of a paradoxical subreg. */
3404 static bool
3405 undefined_operand_subword_p (const_rtx op, int i)
3407 machine_mode innermode, innermostmode;
3408 int offset;
3409 if (GET_CODE (op) != SUBREG)
3410 return false;
3411 innermode = GET_MODE (op);
3412 innermostmode = GET_MODE (SUBREG_REG (op));
3413 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3414 /* The SUBREG_BYTE represents offset, as if the value were stored in
3415 memory, except for a paradoxical subreg where we define
3416 SUBREG_BYTE to be 0; undo this exception as in
3417 simplify_subreg. */
3418 if (SUBREG_BYTE (op) == 0
3419 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3421 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3422 if (WORDS_BIG_ENDIAN)
3423 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3424 if (BYTES_BIG_ENDIAN)
3425 offset += difference % UNITS_PER_WORD;
3427 if (offset >= GET_MODE_SIZE (innermostmode)
3428 || offset <= -GET_MODE_SIZE (word_mode))
3429 return true;
3430 return false;
3433 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3434 MODE is any multi-word or full-word mode that lacks a move_insn
3435 pattern. Note that you will get better code if you define such
3436 patterns, even if they must turn into multiple assembler instructions. */
3438 static rtx_insn *
3439 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3441 rtx_insn *last_insn = 0;
3442 rtx_insn *seq;
3443 rtx inner;
3444 bool need_clobber;
3445 int i;
3447 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3449 /* If X is a push on the stack, do the push now and replace
3450 X with a reference to the stack pointer. */
3451 if (push_operand (x, mode))
3452 x = emit_move_resolve_push (mode, x);
3454 /* If we are in reload, see if either operand is a MEM whose address
3455 is scheduled for replacement. */
3456 if (reload_in_progress && MEM_P (x)
3457 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3458 x = replace_equiv_address_nv (x, inner);
3459 if (reload_in_progress && MEM_P (y)
3460 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3461 y = replace_equiv_address_nv (y, inner);
3463 start_sequence ();
3465 need_clobber = false;
3466 for (i = 0;
3467 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3468 i++)
3470 rtx xpart = operand_subword (x, i, 1, mode);
3471 rtx ypart;
3473 /* Do not generate code for a move if it would come entirely
3474 from the undefined bits of a paradoxical subreg. */
3475 if (undefined_operand_subword_p (y, i))
3476 continue;
3478 ypart = operand_subword (y, i, 1, mode);
3480 /* If we can't get a part of Y, put Y into memory if it is a
3481 constant. Otherwise, force it into a register. Then we must
3482 be able to get a part of Y. */
3483 if (ypart == 0 && CONSTANT_P (y))
3485 y = use_anchored_address (force_const_mem (mode, y));
3486 ypart = operand_subword (y, i, 1, mode);
3488 else if (ypart == 0)
3489 ypart = operand_subword_force (y, i, mode);
3491 gcc_assert (xpart && ypart);
3493 need_clobber |= (GET_CODE (xpart) == SUBREG);
3495 last_insn = emit_move_insn (xpart, ypart);
3498 seq = get_insns ();
3499 end_sequence ();
3501 /* Show the output dies here. This is necessary for SUBREGs
3502 of pseudos since we cannot track their lifetimes correctly;
3503 hard regs shouldn't appear here except as return values.
3504 We never want to emit such a clobber after reload. */
3505 if (x != y
3506 && ! (reload_in_progress || reload_completed)
3507 && need_clobber != 0)
3508 emit_clobber (x);
3510 emit_insn (seq);
3512 return last_insn;
3515 /* Low level part of emit_move_insn.
3516 Called just like emit_move_insn, but assumes X and Y
3517 are basically valid. */
3519 rtx_insn *
3520 emit_move_insn_1 (rtx x, rtx y)
3522 machine_mode mode = GET_MODE (x);
3523 enum insn_code code;
3525 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3527 code = optab_handler (mov_optab, mode);
3528 if (code != CODE_FOR_nothing)
3529 return emit_insn (GEN_FCN (code) (x, y));
3531 /* Expand complex moves by moving real part and imag part. */
3532 if (COMPLEX_MODE_P (mode))
3533 return emit_move_complex (mode, x, y);
3535 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3536 || ALL_FIXED_POINT_MODE_P (mode))
3538 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3540 /* If we can't find an integer mode, use multi words. */
3541 if (result)
3542 return result;
3543 else
3544 return emit_move_multi_word (mode, x, y);
3547 if (GET_MODE_CLASS (mode) == MODE_CC)
3548 return emit_move_ccmode (mode, x, y);
3550 /* Try using a move pattern for the corresponding integer mode. This is
3551 only safe when simplify_subreg can convert MODE constants into integer
3552 constants. At present, it can only do this reliably if the value
3553 fits within a HOST_WIDE_INT. */
3554 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3556 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3558 if (ret)
3560 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3561 return ret;
3565 return emit_move_multi_word (mode, x, y);
3568 /* Generate code to copy Y into X.
3569 Both Y and X must have the same mode, except that
3570 Y can be a constant with VOIDmode.
3571 This mode cannot be BLKmode; use emit_block_move for that.
3573 Return the last instruction emitted. */
3575 rtx_insn *
3576 emit_move_insn (rtx x, rtx y)
3578 machine_mode mode = GET_MODE (x);
3579 rtx y_cst = NULL_RTX;
3580 rtx_insn *last_insn;
3581 rtx set;
3583 gcc_assert (mode != BLKmode
3584 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3586 if (CONSTANT_P (y))
3588 if (optimize
3589 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3590 && (last_insn = compress_float_constant (x, y)))
3591 return last_insn;
3593 y_cst = y;
3595 if (!targetm.legitimate_constant_p (mode, y))
3597 y = force_const_mem (mode, y);
3599 /* If the target's cannot_force_const_mem prevented the spill,
3600 assume that the target's move expanders will also take care
3601 of the non-legitimate constant. */
3602 if (!y)
3603 y = y_cst;
3604 else
3605 y = use_anchored_address (y);
3609 /* If X or Y are memory references, verify that their addresses are valid
3610 for the machine. */
3611 if (MEM_P (x)
3612 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3613 MEM_ADDR_SPACE (x))
3614 && ! push_operand (x, GET_MODE (x))))
3615 x = validize_mem (x);
3617 if (MEM_P (y)
3618 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3619 MEM_ADDR_SPACE (y)))
3620 y = validize_mem (y);
3622 gcc_assert (mode != BLKmode);
3624 last_insn = emit_move_insn_1 (x, y);
3626 if (y_cst && REG_P (x)
3627 && (set = single_set (last_insn)) != NULL_RTX
3628 && SET_DEST (set) == x
3629 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3630 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3632 return last_insn;
3635 /* Generate the body of an instruction to copy Y into X.
3636 It may be a list of insns, if one insn isn't enough. */
3639 gen_move_insn (rtx x, rtx y)
3641 rtx_insn *seq;
3643 start_sequence ();
3644 emit_move_insn_1 (x, y);
3645 seq = get_insns ();
3646 end_sequence ();
3647 return seq;
3650 /* If Y is representable exactly in a narrower mode, and the target can
3651 perform the extension directly from constant or memory, then emit the
3652 move as an extension. */
3654 static rtx_insn *
3655 compress_float_constant (rtx x, rtx y)
3657 machine_mode dstmode = GET_MODE (x);
3658 machine_mode orig_srcmode = GET_MODE (y);
3659 machine_mode srcmode;
3660 REAL_VALUE_TYPE r;
3661 int oldcost, newcost;
3662 bool speed = optimize_insn_for_speed_p ();
3664 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3666 if (targetm.legitimate_constant_p (dstmode, y))
3667 oldcost = set_src_cost (y, speed);
3668 else
3669 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3671 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3672 srcmode != orig_srcmode;
3673 srcmode = GET_MODE_WIDER_MODE (srcmode))
3675 enum insn_code ic;
3676 rtx trunc_y;
3677 rtx_insn *last_insn;
3679 /* Skip if the target can't extend this way. */
3680 ic = can_extend_p (dstmode, srcmode, 0);
3681 if (ic == CODE_FOR_nothing)
3682 continue;
3684 /* Skip if the narrowed value isn't exact. */
3685 if (! exact_real_truncate (srcmode, &r))
3686 continue;
3688 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3690 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3692 /* Skip if the target needs extra instructions to perform
3693 the extension. */
3694 if (!insn_operand_matches (ic, 1, trunc_y))
3695 continue;
3696 /* This is valid, but may not be cheaper than the original. */
3697 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3698 speed);
3699 if (oldcost < newcost)
3700 continue;
3702 else if (float_extend_from_mem[dstmode][srcmode])
3704 trunc_y = force_const_mem (srcmode, trunc_y);
3705 /* This is valid, but may not be cheaper than the original. */
3706 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3707 speed);
3708 if (oldcost < newcost)
3709 continue;
3710 trunc_y = validize_mem (trunc_y);
3712 else
3713 continue;
3715 /* For CSE's benefit, force the compressed constant pool entry
3716 into a new pseudo. This constant may be used in different modes,
3717 and if not, combine will put things back together for us. */
3718 trunc_y = force_reg (srcmode, trunc_y);
3720 /* If x is a hard register, perform the extension into a pseudo,
3721 so that e.g. stack realignment code is aware of it. */
3722 rtx target = x;
3723 if (REG_P (x) && HARD_REGISTER_P (x))
3724 target = gen_reg_rtx (dstmode);
3726 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3727 last_insn = get_last_insn ();
3729 if (REG_P (target))
3730 set_unique_reg_note (last_insn, REG_EQUAL, y);
3732 if (target != x)
3733 return emit_move_insn (x, target);
3734 return last_insn;
3737 return NULL;
3740 /* Pushing data onto the stack. */
3742 /* Push a block of length SIZE (perhaps variable)
3743 and return an rtx to address the beginning of the block.
3744 The value may be virtual_outgoing_args_rtx.
3746 EXTRA is the number of bytes of padding to push in addition to SIZE.
3747 BELOW nonzero means this padding comes at low addresses;
3748 otherwise, the padding comes at high addresses. */
3751 push_block (rtx size, int extra, int below)
3753 rtx temp;
3755 size = convert_modes (Pmode, ptr_mode, size, 1);
3756 if (CONSTANT_P (size))
3757 anti_adjust_stack (plus_constant (Pmode, size, extra));
3758 else if (REG_P (size) && extra == 0)
3759 anti_adjust_stack (size);
3760 else
3762 temp = copy_to_mode_reg (Pmode, size);
3763 if (extra != 0)
3764 temp = expand_binop (Pmode, add_optab, temp,
3765 gen_int_mode (extra, Pmode),
3766 temp, 0, OPTAB_LIB_WIDEN);
3767 anti_adjust_stack (temp);
3770 #ifndef STACK_GROWS_DOWNWARD
3771 if (0)
3772 #else
3773 if (1)
3774 #endif
3776 temp = virtual_outgoing_args_rtx;
3777 if (extra != 0 && below)
3778 temp = plus_constant (Pmode, temp, extra);
3780 else
3782 if (CONST_INT_P (size))
3783 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3784 -INTVAL (size) - (below ? 0 : extra));
3785 else if (extra != 0 && !below)
3786 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3787 negate_rtx (Pmode, plus_constant (Pmode, size,
3788 extra)));
3789 else
3790 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3791 negate_rtx (Pmode, size));
3794 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3797 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3799 static rtx
3800 mem_autoinc_base (rtx mem)
3802 if (MEM_P (mem))
3804 rtx addr = XEXP (mem, 0);
3805 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3806 return XEXP (addr, 0);
3808 return NULL;
3811 /* A utility routine used here, in reload, and in try_split. The insns
3812 after PREV up to and including LAST are known to adjust the stack,
3813 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3814 placing notes as appropriate. PREV may be NULL, indicating the
3815 entire insn sequence prior to LAST should be scanned.
3817 The set of allowed stack pointer modifications is small:
3818 (1) One or more auto-inc style memory references (aka pushes),
3819 (2) One or more addition/subtraction with the SP as destination,
3820 (3) A single move insn with the SP as destination,
3821 (4) A call_pop insn,
3822 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3824 Insns in the sequence that do not modify the SP are ignored,
3825 except for noreturn calls.
3827 The return value is the amount of adjustment that can be trivially
3828 verified, via immediate operand or auto-inc. If the adjustment
3829 cannot be trivially extracted, the return value is INT_MIN. */
3831 HOST_WIDE_INT
3832 find_args_size_adjust (rtx_insn *insn)
3834 rtx dest, set, pat;
3835 int i;
3837 pat = PATTERN (insn);
3838 set = NULL;
3840 /* Look for a call_pop pattern. */
3841 if (CALL_P (insn))
3843 /* We have to allow non-call_pop patterns for the case
3844 of emit_single_push_insn of a TLS address. */
3845 if (GET_CODE (pat) != PARALLEL)
3846 return 0;
3848 /* All call_pop have a stack pointer adjust in the parallel.
3849 The call itself is always first, and the stack adjust is
3850 usually last, so search from the end. */
3851 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3853 set = XVECEXP (pat, 0, i);
3854 if (GET_CODE (set) != SET)
3855 continue;
3856 dest = SET_DEST (set);
3857 if (dest == stack_pointer_rtx)
3858 break;
3860 /* We'd better have found the stack pointer adjust. */
3861 if (i == 0)
3862 return 0;
3863 /* Fall through to process the extracted SET and DEST
3864 as if it was a standalone insn. */
3866 else if (GET_CODE (pat) == SET)
3867 set = pat;
3868 else if ((set = single_set (insn)) != NULL)
3870 else if (GET_CODE (pat) == PARALLEL)
3872 /* ??? Some older ports use a parallel with a stack adjust
3873 and a store for a PUSH_ROUNDING pattern, rather than a
3874 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3875 /* ??? See h8300 and m68k, pushqi1. */
3876 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3878 set = XVECEXP (pat, 0, i);
3879 if (GET_CODE (set) != SET)
3880 continue;
3881 dest = SET_DEST (set);
3882 if (dest == stack_pointer_rtx)
3883 break;
3885 /* We do not expect an auto-inc of the sp in the parallel. */
3886 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3887 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3888 != stack_pointer_rtx);
3890 if (i < 0)
3891 return 0;
3893 else
3894 return 0;
3896 dest = SET_DEST (set);
3898 /* Look for direct modifications of the stack pointer. */
3899 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3901 /* Look for a trivial adjustment, otherwise assume nothing. */
3902 /* Note that the SPU restore_stack_block pattern refers to
3903 the stack pointer in V4SImode. Consider that non-trivial. */
3904 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3905 && GET_CODE (SET_SRC (set)) == PLUS
3906 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3907 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3908 return INTVAL (XEXP (SET_SRC (set), 1));
3909 /* ??? Reload can generate no-op moves, which will be cleaned
3910 up later. Recognize it and continue searching. */
3911 else if (rtx_equal_p (dest, SET_SRC (set)))
3912 return 0;
3913 else
3914 return HOST_WIDE_INT_MIN;
3916 else
3918 rtx mem, addr;
3920 /* Otherwise only think about autoinc patterns. */
3921 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3923 mem = dest;
3924 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3925 != stack_pointer_rtx);
3927 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3928 mem = SET_SRC (set);
3929 else
3930 return 0;
3932 addr = XEXP (mem, 0);
3933 switch (GET_CODE (addr))
3935 case PRE_INC:
3936 case POST_INC:
3937 return GET_MODE_SIZE (GET_MODE (mem));
3938 case PRE_DEC:
3939 case POST_DEC:
3940 return -GET_MODE_SIZE (GET_MODE (mem));
3941 case PRE_MODIFY:
3942 case POST_MODIFY:
3943 addr = XEXP (addr, 1);
3944 gcc_assert (GET_CODE (addr) == PLUS);
3945 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3946 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3947 return INTVAL (XEXP (addr, 1));
3948 default:
3949 gcc_unreachable ();
3955 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3957 int args_size = end_args_size;
3958 bool saw_unknown = false;
3959 rtx_insn *insn;
3961 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3963 HOST_WIDE_INT this_delta;
3965 if (!NONDEBUG_INSN_P (insn))
3966 continue;
3968 this_delta = find_args_size_adjust (insn);
3969 if (this_delta == 0)
3971 if (!CALL_P (insn)
3972 || ACCUMULATE_OUTGOING_ARGS
3973 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3974 continue;
3977 gcc_assert (!saw_unknown);
3978 if (this_delta == HOST_WIDE_INT_MIN)
3979 saw_unknown = true;
3981 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3982 #ifdef STACK_GROWS_DOWNWARD
3983 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3984 #endif
3985 args_size -= this_delta;
3988 return saw_unknown ? INT_MIN : args_size;
3991 #ifdef PUSH_ROUNDING
3992 /* Emit single push insn. */
3994 static void
3995 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
3997 rtx dest_addr;
3998 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3999 rtx dest;
4000 enum insn_code icode;
4002 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4003 /* If there is push pattern, use it. Otherwise try old way of throwing
4004 MEM representing push operation to move expander. */
4005 icode = optab_handler (push_optab, mode);
4006 if (icode != CODE_FOR_nothing)
4008 struct expand_operand ops[1];
4010 create_input_operand (&ops[0], x, mode);
4011 if (maybe_expand_insn (icode, 1, ops))
4012 return;
4014 if (GET_MODE_SIZE (mode) == rounded_size)
4015 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4016 /* If we are to pad downward, adjust the stack pointer first and
4017 then store X into the stack location using an offset. This is
4018 because emit_move_insn does not know how to pad; it does not have
4019 access to type. */
4020 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4022 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4023 HOST_WIDE_INT offset;
4025 emit_move_insn (stack_pointer_rtx,
4026 expand_binop (Pmode,
4027 #ifdef STACK_GROWS_DOWNWARD
4028 sub_optab,
4029 #else
4030 add_optab,
4031 #endif
4032 stack_pointer_rtx,
4033 gen_int_mode (rounded_size, Pmode),
4034 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4036 offset = (HOST_WIDE_INT) padding_size;
4037 #ifdef STACK_GROWS_DOWNWARD
4038 if (STACK_PUSH_CODE == POST_DEC)
4039 /* We have already decremented the stack pointer, so get the
4040 previous value. */
4041 offset += (HOST_WIDE_INT) rounded_size;
4042 #else
4043 if (STACK_PUSH_CODE == POST_INC)
4044 /* We have already incremented the stack pointer, so get the
4045 previous value. */
4046 offset -= (HOST_WIDE_INT) rounded_size;
4047 #endif
4048 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4049 gen_int_mode (offset, Pmode));
4051 else
4053 #ifdef STACK_GROWS_DOWNWARD
4054 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4055 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4056 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4057 Pmode));
4058 #else
4059 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4060 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4061 gen_int_mode (rounded_size, Pmode));
4062 #endif
4063 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4066 dest = gen_rtx_MEM (mode, dest_addr);
4068 if (type != 0)
4070 set_mem_attributes (dest, type, 1);
4072 if (cfun->tail_call_marked)
4073 /* Function incoming arguments may overlap with sibling call
4074 outgoing arguments and we cannot allow reordering of reads
4075 from function arguments with stores to outgoing arguments
4076 of sibling calls. */
4077 set_mem_alias_set (dest, 0);
4079 emit_move_insn (dest, x);
4082 /* Emit and annotate a single push insn. */
4084 static void
4085 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4087 int delta, old_delta = stack_pointer_delta;
4088 rtx_insn *prev = get_last_insn ();
4089 rtx_insn *last;
4091 emit_single_push_insn_1 (mode, x, type);
4093 last = get_last_insn ();
4095 /* Notice the common case where we emitted exactly one insn. */
4096 if (PREV_INSN (last) == prev)
4098 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4099 return;
4102 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4103 gcc_assert (delta == INT_MIN || delta == old_delta);
4105 #endif
4107 /* Generate code to push X onto the stack, assuming it has mode MODE and
4108 type TYPE.
4109 MODE is redundant except when X is a CONST_INT (since they don't
4110 carry mode info).
4111 SIZE is an rtx for the size of data to be copied (in bytes),
4112 needed only if X is BLKmode.
4114 ALIGN (in bits) is maximum alignment we can assume.
4116 If PARTIAL and REG are both nonzero, then copy that many of the first
4117 bytes of X into registers starting with REG, and push the rest of X.
4118 The amount of space pushed is decreased by PARTIAL bytes.
4119 REG must be a hard register in this case.
4120 If REG is zero but PARTIAL is not, take any all others actions for an
4121 argument partially in registers, but do not actually load any
4122 registers.
4124 EXTRA is the amount in bytes of extra space to leave next to this arg.
4125 This is ignored if an argument block has already been allocated.
4127 On a machine that lacks real push insns, ARGS_ADDR is the address of
4128 the bottom of the argument block for this call. We use indexing off there
4129 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4130 argument block has not been preallocated.
4132 ARGS_SO_FAR is the size of args previously pushed for this call.
4134 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4135 for arguments passed in registers. If nonzero, it will be the number
4136 of bytes required. */
4138 void
4139 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4140 unsigned int align, int partial, rtx reg, int extra,
4141 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4142 rtx alignment_pad)
4144 rtx xinner;
4145 enum direction stack_direction
4146 #ifdef STACK_GROWS_DOWNWARD
4147 = downward;
4148 #else
4149 = upward;
4150 #endif
4152 /* Decide where to pad the argument: `downward' for below,
4153 `upward' for above, or `none' for don't pad it.
4154 Default is below for small data on big-endian machines; else above. */
4155 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4157 /* Invert direction if stack is post-decrement.
4158 FIXME: why? */
4159 if (STACK_PUSH_CODE == POST_DEC)
4160 if (where_pad != none)
4161 where_pad = (where_pad == downward ? upward : downward);
4163 xinner = x;
4165 if (mode == BLKmode
4166 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4168 /* Copy a block into the stack, entirely or partially. */
4170 rtx temp;
4171 int used;
4172 int offset;
4173 int skip;
4175 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4176 used = partial - offset;
4178 if (mode != BLKmode)
4180 /* A value is to be stored in an insufficiently aligned
4181 stack slot; copy via a suitably aligned slot if
4182 necessary. */
4183 size = GEN_INT (GET_MODE_SIZE (mode));
4184 if (!MEM_P (xinner))
4186 temp = assign_temp (type, 1, 1);
4187 emit_move_insn (temp, xinner);
4188 xinner = temp;
4192 gcc_assert (size);
4194 /* USED is now the # of bytes we need not copy to the stack
4195 because registers will take care of them. */
4197 if (partial != 0)
4198 xinner = adjust_address (xinner, BLKmode, used);
4200 /* If the partial register-part of the arg counts in its stack size,
4201 skip the part of stack space corresponding to the registers.
4202 Otherwise, start copying to the beginning of the stack space,
4203 by setting SKIP to 0. */
4204 skip = (reg_parm_stack_space == 0) ? 0 : used;
4206 #ifdef PUSH_ROUNDING
4207 /* Do it with several push insns if that doesn't take lots of insns
4208 and if there is no difficulty with push insns that skip bytes
4209 on the stack for alignment purposes. */
4210 if (args_addr == 0
4211 && PUSH_ARGS
4212 && CONST_INT_P (size)
4213 && skip == 0
4214 && MEM_ALIGN (xinner) >= align
4215 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4216 /* Here we avoid the case of a structure whose weak alignment
4217 forces many pushes of a small amount of data,
4218 and such small pushes do rounding that causes trouble. */
4219 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4220 || align >= BIGGEST_ALIGNMENT
4221 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4222 == (align / BITS_PER_UNIT)))
4223 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4225 /* Push padding now if padding above and stack grows down,
4226 or if padding below and stack grows up.
4227 But if space already allocated, this has already been done. */
4228 if (extra && args_addr == 0
4229 && where_pad != none && where_pad != stack_direction)
4230 anti_adjust_stack (GEN_INT (extra));
4232 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4234 else
4235 #endif /* PUSH_ROUNDING */
4237 rtx target;
4239 /* Otherwise make space on the stack and copy the data
4240 to the address of that space. */
4242 /* Deduct words put into registers from the size we must copy. */
4243 if (partial != 0)
4245 if (CONST_INT_P (size))
4246 size = GEN_INT (INTVAL (size) - used);
4247 else
4248 size = expand_binop (GET_MODE (size), sub_optab, size,
4249 gen_int_mode (used, GET_MODE (size)),
4250 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4253 /* Get the address of the stack space.
4254 In this case, we do not deal with EXTRA separately.
4255 A single stack adjust will do. */
4256 if (! args_addr)
4258 temp = push_block (size, extra, where_pad == downward);
4259 extra = 0;
4261 else if (CONST_INT_P (args_so_far))
4262 temp = memory_address (BLKmode,
4263 plus_constant (Pmode, args_addr,
4264 skip + INTVAL (args_so_far)));
4265 else
4266 temp = memory_address (BLKmode,
4267 plus_constant (Pmode,
4268 gen_rtx_PLUS (Pmode,
4269 args_addr,
4270 args_so_far),
4271 skip));
4273 if (!ACCUMULATE_OUTGOING_ARGS)
4275 /* If the source is referenced relative to the stack pointer,
4276 copy it to another register to stabilize it. We do not need
4277 to do this if we know that we won't be changing sp. */
4279 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4280 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4281 temp = copy_to_reg (temp);
4284 target = gen_rtx_MEM (BLKmode, temp);
4286 /* We do *not* set_mem_attributes here, because incoming arguments
4287 may overlap with sibling call outgoing arguments and we cannot
4288 allow reordering of reads from function arguments with stores
4289 to outgoing arguments of sibling calls. We do, however, want
4290 to record the alignment of the stack slot. */
4291 /* ALIGN may well be better aligned than TYPE, e.g. due to
4292 PARM_BOUNDARY. Assume the caller isn't lying. */
4293 set_mem_align (target, align);
4295 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4298 else if (partial > 0)
4300 /* Scalar partly in registers. */
4302 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4303 int i;
4304 int not_stack;
4305 /* # bytes of start of argument
4306 that we must make space for but need not store. */
4307 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4308 int args_offset = INTVAL (args_so_far);
4309 int skip;
4311 /* Push padding now if padding above and stack grows down,
4312 or if padding below and stack grows up.
4313 But if space already allocated, this has already been done. */
4314 if (extra && args_addr == 0
4315 && where_pad != none && where_pad != stack_direction)
4316 anti_adjust_stack (GEN_INT (extra));
4318 /* If we make space by pushing it, we might as well push
4319 the real data. Otherwise, we can leave OFFSET nonzero
4320 and leave the space uninitialized. */
4321 if (args_addr == 0)
4322 offset = 0;
4324 /* Now NOT_STACK gets the number of words that we don't need to
4325 allocate on the stack. Convert OFFSET to words too. */
4326 not_stack = (partial - offset) / UNITS_PER_WORD;
4327 offset /= UNITS_PER_WORD;
4329 /* If the partial register-part of the arg counts in its stack size,
4330 skip the part of stack space corresponding to the registers.
4331 Otherwise, start copying to the beginning of the stack space,
4332 by setting SKIP to 0. */
4333 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4335 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4336 x = validize_mem (force_const_mem (mode, x));
4338 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4339 SUBREGs of such registers are not allowed. */
4340 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4341 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4342 x = copy_to_reg (x);
4344 /* Loop over all the words allocated on the stack for this arg. */
4345 /* We can do it by words, because any scalar bigger than a word
4346 has a size a multiple of a word. */
4347 for (i = size - 1; i >= not_stack; i--)
4348 if (i >= not_stack + offset)
4349 emit_push_insn (operand_subword_force (x, i, mode),
4350 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4351 0, args_addr,
4352 GEN_INT (args_offset + ((i - not_stack + skip)
4353 * UNITS_PER_WORD)),
4354 reg_parm_stack_space, alignment_pad);
4356 else
4358 rtx addr;
4359 rtx dest;
4361 /* Push padding now if padding above and stack grows down,
4362 or if padding below and stack grows up.
4363 But if space already allocated, this has already been done. */
4364 if (extra && args_addr == 0
4365 && where_pad != none && where_pad != stack_direction)
4366 anti_adjust_stack (GEN_INT (extra));
4368 #ifdef PUSH_ROUNDING
4369 if (args_addr == 0 && PUSH_ARGS)
4370 emit_single_push_insn (mode, x, type);
4371 else
4372 #endif
4374 if (CONST_INT_P (args_so_far))
4375 addr
4376 = memory_address (mode,
4377 plus_constant (Pmode, args_addr,
4378 INTVAL (args_so_far)));
4379 else
4380 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4381 args_so_far));
4382 dest = gen_rtx_MEM (mode, addr);
4384 /* We do *not* set_mem_attributes here, because incoming arguments
4385 may overlap with sibling call outgoing arguments and we cannot
4386 allow reordering of reads from function arguments with stores
4387 to outgoing arguments of sibling calls. We do, however, want
4388 to record the alignment of the stack slot. */
4389 /* ALIGN may well be better aligned than TYPE, e.g. due to
4390 PARM_BOUNDARY. Assume the caller isn't lying. */
4391 set_mem_align (dest, align);
4393 emit_move_insn (dest, x);
4397 /* If part should go in registers, copy that part
4398 into the appropriate registers. Do this now, at the end,
4399 since mem-to-mem copies above may do function calls. */
4400 if (partial > 0 && reg != 0)
4402 /* Handle calls that pass values in multiple non-contiguous locations.
4403 The Irix 6 ABI has examples of this. */
4404 if (GET_CODE (reg) == PARALLEL)
4405 emit_group_load (reg, x, type, -1);
4406 else
4408 gcc_assert (partial % UNITS_PER_WORD == 0);
4409 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4413 if (extra && args_addr == 0 && where_pad == stack_direction)
4414 anti_adjust_stack (GEN_INT (extra));
4416 if (alignment_pad && args_addr == 0)
4417 anti_adjust_stack (alignment_pad);
4420 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4421 operations. */
4423 static rtx
4424 get_subtarget (rtx x)
4426 return (optimize
4427 || x == 0
4428 /* Only registers can be subtargets. */
4429 || !REG_P (x)
4430 /* Don't use hard regs to avoid extending their life. */
4431 || REGNO (x) < FIRST_PSEUDO_REGISTER
4432 ? 0 : x);
4435 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4436 FIELD is a bitfield. Returns true if the optimization was successful,
4437 and there's nothing else to do. */
4439 static bool
4440 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4441 unsigned HOST_WIDE_INT bitpos,
4442 unsigned HOST_WIDE_INT bitregion_start,
4443 unsigned HOST_WIDE_INT bitregion_end,
4444 machine_mode mode1, rtx str_rtx,
4445 tree to, tree src)
4447 machine_mode str_mode = GET_MODE (str_rtx);
4448 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4449 tree op0, op1;
4450 rtx value, result;
4451 optab binop;
4452 gimple srcstmt;
4453 enum tree_code code;
4455 if (mode1 != VOIDmode
4456 || bitsize >= BITS_PER_WORD
4457 || str_bitsize > BITS_PER_WORD
4458 || TREE_SIDE_EFFECTS (to)
4459 || TREE_THIS_VOLATILE (to))
4460 return false;
4462 STRIP_NOPS (src);
4463 if (TREE_CODE (src) != SSA_NAME)
4464 return false;
4465 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4466 return false;
4468 srcstmt = get_gimple_for_ssa_name (src);
4469 if (!srcstmt
4470 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4471 return false;
4473 code = gimple_assign_rhs_code (srcstmt);
4475 op0 = gimple_assign_rhs1 (srcstmt);
4477 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4478 to find its initialization. Hopefully the initialization will
4479 be from a bitfield load. */
4480 if (TREE_CODE (op0) == SSA_NAME)
4482 gimple op0stmt = get_gimple_for_ssa_name (op0);
4484 /* We want to eventually have OP0 be the same as TO, which
4485 should be a bitfield. */
4486 if (!op0stmt
4487 || !is_gimple_assign (op0stmt)
4488 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4489 return false;
4490 op0 = gimple_assign_rhs1 (op0stmt);
4493 op1 = gimple_assign_rhs2 (srcstmt);
4495 if (!operand_equal_p (to, op0, 0))
4496 return false;
4498 if (MEM_P (str_rtx))
4500 unsigned HOST_WIDE_INT offset1;
4502 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4503 str_mode = word_mode;
4504 str_mode = get_best_mode (bitsize, bitpos,
4505 bitregion_start, bitregion_end,
4506 MEM_ALIGN (str_rtx), str_mode, 0);
4507 if (str_mode == VOIDmode)
4508 return false;
4509 str_bitsize = GET_MODE_BITSIZE (str_mode);
4511 offset1 = bitpos;
4512 bitpos %= str_bitsize;
4513 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4514 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4516 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4517 return false;
4519 /* If the bit field covers the whole REG/MEM, store_field
4520 will likely generate better code. */
4521 if (bitsize >= str_bitsize)
4522 return false;
4524 /* We can't handle fields split across multiple entities. */
4525 if (bitpos + bitsize > str_bitsize)
4526 return false;
4528 if (BYTES_BIG_ENDIAN)
4529 bitpos = str_bitsize - bitpos - bitsize;
4531 switch (code)
4533 case PLUS_EXPR:
4534 case MINUS_EXPR:
4535 /* For now, just optimize the case of the topmost bitfield
4536 where we don't need to do any masking and also
4537 1 bit bitfields where xor can be used.
4538 We might win by one instruction for the other bitfields
4539 too if insv/extv instructions aren't used, so that
4540 can be added later. */
4541 if (bitpos + bitsize != str_bitsize
4542 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4543 break;
4545 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4546 value = convert_modes (str_mode,
4547 TYPE_MODE (TREE_TYPE (op1)), value,
4548 TYPE_UNSIGNED (TREE_TYPE (op1)));
4550 /* We may be accessing data outside the field, which means
4551 we can alias adjacent data. */
4552 if (MEM_P (str_rtx))
4554 str_rtx = shallow_copy_rtx (str_rtx);
4555 set_mem_alias_set (str_rtx, 0);
4556 set_mem_expr (str_rtx, 0);
4559 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4560 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4562 value = expand_and (str_mode, value, const1_rtx, NULL);
4563 binop = xor_optab;
4565 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4566 result = expand_binop (str_mode, binop, str_rtx,
4567 value, str_rtx, 1, OPTAB_WIDEN);
4568 if (result != str_rtx)
4569 emit_move_insn (str_rtx, result);
4570 return true;
4572 case BIT_IOR_EXPR:
4573 case BIT_XOR_EXPR:
4574 if (TREE_CODE (op1) != INTEGER_CST)
4575 break;
4576 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4577 value = convert_modes (str_mode,
4578 TYPE_MODE (TREE_TYPE (op1)), value,
4579 TYPE_UNSIGNED (TREE_TYPE (op1)));
4581 /* We may be accessing data outside the field, which means
4582 we can alias adjacent data. */
4583 if (MEM_P (str_rtx))
4585 str_rtx = shallow_copy_rtx (str_rtx);
4586 set_mem_alias_set (str_rtx, 0);
4587 set_mem_expr (str_rtx, 0);
4590 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4591 if (bitpos + bitsize != str_bitsize)
4593 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4594 str_mode);
4595 value = expand_and (str_mode, value, mask, NULL_RTX);
4597 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4598 result = expand_binop (str_mode, binop, str_rtx,
4599 value, str_rtx, 1, OPTAB_WIDEN);
4600 if (result != str_rtx)
4601 emit_move_insn (str_rtx, result);
4602 return true;
4604 default:
4605 break;
4608 return false;
4611 /* In the C++ memory model, consecutive bit fields in a structure are
4612 considered one memory location.
4614 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4615 returns the bit range of consecutive bits in which this COMPONENT_REF
4616 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4617 and *OFFSET may be adjusted in the process.
4619 If the access does not need to be restricted, 0 is returned in both
4620 *BITSTART and *BITEND. */
4622 static void
4623 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4624 unsigned HOST_WIDE_INT *bitend,
4625 tree exp,
4626 HOST_WIDE_INT *bitpos,
4627 tree *offset)
4629 HOST_WIDE_INT bitoffset;
4630 tree field, repr;
4632 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4634 field = TREE_OPERAND (exp, 1);
4635 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4636 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4637 need to limit the range we can access. */
4638 if (!repr)
4640 *bitstart = *bitend = 0;
4641 return;
4644 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4645 part of a larger bit field, then the representative does not serve any
4646 useful purpose. This can occur in Ada. */
4647 if (handled_component_p (TREE_OPERAND (exp, 0)))
4649 machine_mode rmode;
4650 HOST_WIDE_INT rbitsize, rbitpos;
4651 tree roffset;
4652 int unsignedp;
4653 int volatilep = 0;
4654 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4655 &roffset, &rmode, &unsignedp, &volatilep, false);
4656 if ((rbitpos % BITS_PER_UNIT) != 0)
4658 *bitstart = *bitend = 0;
4659 return;
4663 /* Compute the adjustment to bitpos from the offset of the field
4664 relative to the representative. DECL_FIELD_OFFSET of field and
4665 repr are the same by construction if they are not constants,
4666 see finish_bitfield_layout. */
4667 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4668 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4669 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4670 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4671 else
4672 bitoffset = 0;
4673 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4674 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4676 /* If the adjustment is larger than bitpos, we would have a negative bit
4677 position for the lower bound and this may wreak havoc later. Adjust
4678 offset and bitpos to make the lower bound non-negative in that case. */
4679 if (bitoffset > *bitpos)
4681 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4682 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4684 *bitpos += adjust;
4685 if (*offset == NULL_TREE)
4686 *offset = size_int (-adjust / BITS_PER_UNIT);
4687 else
4688 *offset
4689 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4690 *bitstart = 0;
4692 else
4693 *bitstart = *bitpos - bitoffset;
4695 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4698 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4699 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4700 DECL_RTL was not set yet, return NORTL. */
4702 static inline bool
4703 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4705 if (TREE_CODE (addr) != ADDR_EXPR)
4706 return false;
4708 tree base = TREE_OPERAND (addr, 0);
4710 if (!DECL_P (base)
4711 || TREE_ADDRESSABLE (base)
4712 || DECL_MODE (base) == BLKmode)
4713 return false;
4715 if (!DECL_RTL_SET_P (base))
4716 return nortl;
4718 return (!MEM_P (DECL_RTL (base)));
4721 /* Returns true if the MEM_REF REF refers to an object that does not
4722 reside in memory and has non-BLKmode. */
4724 static inline bool
4725 mem_ref_refers_to_non_mem_p (tree ref)
4727 tree base = TREE_OPERAND (ref, 0);
4728 return addr_expr_of_non_mem_decl_p_1 (base, false);
4731 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4732 is true, try generating a nontemporal store. */
4734 void
4735 expand_assignment (tree to, tree from, bool nontemporal)
4737 rtx to_rtx = 0;
4738 rtx result;
4739 machine_mode mode;
4740 unsigned int align;
4741 enum insn_code icode;
4743 /* Don't crash if the lhs of the assignment was erroneous. */
4744 if (TREE_CODE (to) == ERROR_MARK)
4746 expand_normal (from);
4747 return;
4750 /* Optimize away no-op moves without side-effects. */
4751 if (operand_equal_p (to, from, 0))
4752 return;
4754 /* Handle misaligned stores. */
4755 mode = TYPE_MODE (TREE_TYPE (to));
4756 if ((TREE_CODE (to) == MEM_REF
4757 || TREE_CODE (to) == TARGET_MEM_REF)
4758 && mode != BLKmode
4759 && !mem_ref_refers_to_non_mem_p (to)
4760 && ((align = get_object_alignment (to))
4761 < GET_MODE_ALIGNMENT (mode))
4762 && (((icode = optab_handler (movmisalign_optab, mode))
4763 != CODE_FOR_nothing)
4764 || SLOW_UNALIGNED_ACCESS (mode, align)))
4766 rtx reg, mem;
4768 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4769 reg = force_not_mem (reg);
4770 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4772 if (icode != CODE_FOR_nothing)
4774 struct expand_operand ops[2];
4776 create_fixed_operand (&ops[0], mem);
4777 create_input_operand (&ops[1], reg, mode);
4778 /* The movmisalign<mode> pattern cannot fail, else the assignment
4779 would silently be omitted. */
4780 expand_insn (icode, 2, ops);
4782 else
4783 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4784 return;
4787 /* Assignment of a structure component needs special treatment
4788 if the structure component's rtx is not simply a MEM.
4789 Assignment of an array element at a constant index, and assignment of
4790 an array element in an unaligned packed structure field, has the same
4791 problem. Same for (partially) storing into a non-memory object. */
4792 if (handled_component_p (to)
4793 || (TREE_CODE (to) == MEM_REF
4794 && mem_ref_refers_to_non_mem_p (to))
4795 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4797 machine_mode mode1;
4798 HOST_WIDE_INT bitsize, bitpos;
4799 unsigned HOST_WIDE_INT bitregion_start = 0;
4800 unsigned HOST_WIDE_INT bitregion_end = 0;
4801 tree offset;
4802 int unsignedp;
4803 int volatilep = 0;
4804 tree tem;
4806 push_temp_slots ();
4807 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4808 &unsignedp, &volatilep, true);
4810 /* Make sure bitpos is not negative, it can wreak havoc later. */
4811 if (bitpos < 0)
4813 gcc_assert (offset == NULL_TREE);
4814 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4815 ? 3 : exact_log2 (BITS_PER_UNIT)));
4816 bitpos &= BITS_PER_UNIT - 1;
4819 if (TREE_CODE (to) == COMPONENT_REF
4820 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4821 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4822 /* The C++ memory model naturally applies to byte-aligned fields.
4823 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4824 BITSIZE are not byte-aligned, there is no need to limit the range
4825 we can access. This can occur with packed structures in Ada. */
4826 else if (bitsize > 0
4827 && bitsize % BITS_PER_UNIT == 0
4828 && bitpos % BITS_PER_UNIT == 0)
4830 bitregion_start = bitpos;
4831 bitregion_end = bitpos + bitsize - 1;
4834 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4836 /* If the field has a mode, we want to access it in the
4837 field's mode, not the computed mode.
4838 If a MEM has VOIDmode (external with incomplete type),
4839 use BLKmode for it instead. */
4840 if (MEM_P (to_rtx))
4842 if (mode1 != VOIDmode)
4843 to_rtx = adjust_address (to_rtx, mode1, 0);
4844 else if (GET_MODE (to_rtx) == VOIDmode)
4845 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4848 if (offset != 0)
4850 machine_mode address_mode;
4851 rtx offset_rtx;
4853 if (!MEM_P (to_rtx))
4855 /* We can get constant negative offsets into arrays with broken
4856 user code. Translate this to a trap instead of ICEing. */
4857 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4858 expand_builtin_trap ();
4859 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4862 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4863 address_mode = get_address_mode (to_rtx);
4864 if (GET_MODE (offset_rtx) != address_mode)
4865 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4867 /* If we have an expression in OFFSET_RTX and a non-zero
4868 byte offset in BITPOS, adding the byte offset before the
4869 OFFSET_RTX results in better intermediate code, which makes
4870 later rtl optimization passes perform better.
4872 We prefer intermediate code like this:
4874 r124:DI=r123:DI+0x18
4875 [r124:DI]=r121:DI
4877 ... instead of ...
4879 r124:DI=r123:DI+0x10
4880 [r124:DI+0x8]=r121:DI
4882 This is only done for aligned data values, as these can
4883 be expected to result in single move instructions. */
4884 if (mode1 != VOIDmode
4885 && bitpos != 0
4886 && bitsize > 0
4887 && (bitpos % bitsize) == 0
4888 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4889 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4891 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4892 bitregion_start = 0;
4893 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4894 bitregion_end -= bitpos;
4895 bitpos = 0;
4898 to_rtx = offset_address (to_rtx, offset_rtx,
4899 highest_pow2_factor_for_target (to,
4900 offset));
4903 /* No action is needed if the target is not a memory and the field
4904 lies completely outside that target. This can occur if the source
4905 code contains an out-of-bounds access to a small array. */
4906 if (!MEM_P (to_rtx)
4907 && GET_MODE (to_rtx) != BLKmode
4908 && (unsigned HOST_WIDE_INT) bitpos
4909 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4911 expand_normal (from);
4912 result = NULL;
4914 /* Handle expand_expr of a complex value returning a CONCAT. */
4915 else if (GET_CODE (to_rtx) == CONCAT)
4917 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4918 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4919 && bitpos == 0
4920 && bitsize == mode_bitsize)
4921 result = store_expr (from, to_rtx, false, nontemporal);
4922 else if (bitsize == mode_bitsize / 2
4923 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4924 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4925 nontemporal);
4926 else if (bitpos + bitsize <= mode_bitsize / 2)
4927 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4928 bitregion_start, bitregion_end,
4929 mode1, from,
4930 get_alias_set (to), nontemporal);
4931 else if (bitpos >= mode_bitsize / 2)
4932 result = store_field (XEXP (to_rtx, 1), bitsize,
4933 bitpos - mode_bitsize / 2,
4934 bitregion_start, bitregion_end,
4935 mode1, from,
4936 get_alias_set (to), nontemporal);
4937 else if (bitpos == 0 && bitsize == mode_bitsize)
4939 rtx from_rtx;
4940 result = expand_normal (from);
4941 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4942 TYPE_MODE (TREE_TYPE (from)), 0);
4943 emit_move_insn (XEXP (to_rtx, 0),
4944 read_complex_part (from_rtx, false));
4945 emit_move_insn (XEXP (to_rtx, 1),
4946 read_complex_part (from_rtx, true));
4948 else
4950 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4951 GET_MODE_SIZE (GET_MODE (to_rtx)));
4952 write_complex_part (temp, XEXP (to_rtx, 0), false);
4953 write_complex_part (temp, XEXP (to_rtx, 1), true);
4954 result = store_field (temp, bitsize, bitpos,
4955 bitregion_start, bitregion_end,
4956 mode1, from,
4957 get_alias_set (to), nontemporal);
4958 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4959 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4962 else
4964 if (MEM_P (to_rtx))
4966 /* If the field is at offset zero, we could have been given the
4967 DECL_RTX of the parent struct. Don't munge it. */
4968 to_rtx = shallow_copy_rtx (to_rtx);
4969 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4970 if (volatilep)
4971 MEM_VOLATILE_P (to_rtx) = 1;
4974 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4975 bitregion_start, bitregion_end,
4976 mode1,
4977 to_rtx, to, from))
4978 result = NULL;
4979 else
4980 result = store_field (to_rtx, bitsize, bitpos,
4981 bitregion_start, bitregion_end,
4982 mode1, from,
4983 get_alias_set (to), nontemporal);
4986 if (result)
4987 preserve_temp_slots (result);
4988 pop_temp_slots ();
4989 return;
4992 /* If the rhs is a function call and its value is not an aggregate,
4993 call the function before we start to compute the lhs.
4994 This is needed for correct code for cases such as
4995 val = setjmp (buf) on machines where reference to val
4996 requires loading up part of an address in a separate insn.
4998 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4999 since it might be a promoted variable where the zero- or sign- extension
5000 needs to be done. Handling this in the normal way is safe because no
5001 computation is done before the call. The same is true for SSA names. */
5002 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5003 && COMPLETE_TYPE_P (TREE_TYPE (from))
5004 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5005 && ! (((TREE_CODE (to) == VAR_DECL
5006 || TREE_CODE (to) == PARM_DECL
5007 || TREE_CODE (to) == RESULT_DECL)
5008 && REG_P (DECL_RTL (to)))
5009 || TREE_CODE (to) == SSA_NAME))
5011 rtx value;
5012 rtx bounds;
5014 push_temp_slots ();
5015 value = expand_normal (from);
5017 /* Split value and bounds to store them separately. */
5018 chkp_split_slot (value, &value, &bounds);
5020 if (to_rtx == 0)
5021 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5023 /* Handle calls that return values in multiple non-contiguous locations.
5024 The Irix 6 ABI has examples of this. */
5025 if (GET_CODE (to_rtx) == PARALLEL)
5027 if (GET_CODE (value) == PARALLEL)
5028 emit_group_move (to_rtx, value);
5029 else
5030 emit_group_load (to_rtx, value, TREE_TYPE (from),
5031 int_size_in_bytes (TREE_TYPE (from)));
5033 else if (GET_CODE (value) == PARALLEL)
5034 emit_group_store (to_rtx, value, TREE_TYPE (from),
5035 int_size_in_bytes (TREE_TYPE (from)));
5036 else if (GET_MODE (to_rtx) == BLKmode)
5038 /* Handle calls that return BLKmode values in registers. */
5039 if (REG_P (value))
5040 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5041 else
5042 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5044 else
5046 if (POINTER_TYPE_P (TREE_TYPE (to)))
5047 value = convert_memory_address_addr_space
5048 (GET_MODE (to_rtx), value,
5049 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5051 emit_move_insn (to_rtx, value);
5054 /* Store bounds if required. */
5055 if (bounds
5056 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5058 gcc_assert (MEM_P (to_rtx));
5059 chkp_emit_bounds_store (bounds, value, to_rtx);
5062 preserve_temp_slots (to_rtx);
5063 pop_temp_slots ();
5064 return;
5067 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5068 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5070 /* Don't move directly into a return register. */
5071 if (TREE_CODE (to) == RESULT_DECL
5072 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5074 rtx temp;
5076 push_temp_slots ();
5078 /* If the source is itself a return value, it still is in a pseudo at
5079 this point so we can move it back to the return register directly. */
5080 if (REG_P (to_rtx)
5081 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5082 && TREE_CODE (from) != CALL_EXPR)
5083 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5084 else
5085 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5087 /* Handle calls that return values in multiple non-contiguous locations.
5088 The Irix 6 ABI has examples of this. */
5089 if (GET_CODE (to_rtx) == PARALLEL)
5091 if (GET_CODE (temp) == PARALLEL)
5092 emit_group_move (to_rtx, temp);
5093 else
5094 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5095 int_size_in_bytes (TREE_TYPE (from)));
5097 else if (temp)
5098 emit_move_insn (to_rtx, temp);
5100 preserve_temp_slots (to_rtx);
5101 pop_temp_slots ();
5102 return;
5105 /* In case we are returning the contents of an object which overlaps
5106 the place the value is being stored, use a safe function when copying
5107 a value through a pointer into a structure value return block. */
5108 if (TREE_CODE (to) == RESULT_DECL
5109 && TREE_CODE (from) == INDIRECT_REF
5110 && ADDR_SPACE_GENERIC_P
5111 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5112 && refs_may_alias_p (to, from)
5113 && cfun->returns_struct
5114 && !cfun->returns_pcc_struct)
5116 rtx from_rtx, size;
5118 push_temp_slots ();
5119 size = expr_size (from);
5120 from_rtx = expand_normal (from);
5122 emit_library_call (memmove_libfunc, LCT_NORMAL,
5123 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5124 XEXP (from_rtx, 0), Pmode,
5125 convert_to_mode (TYPE_MODE (sizetype),
5126 size, TYPE_UNSIGNED (sizetype)),
5127 TYPE_MODE (sizetype));
5129 preserve_temp_slots (to_rtx);
5130 pop_temp_slots ();
5131 return;
5134 /* Compute FROM and store the value in the rtx we got. */
5136 push_temp_slots ();
5137 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5138 preserve_temp_slots (result);
5139 pop_temp_slots ();
5140 return;
5143 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5144 succeeded, false otherwise. */
5146 bool
5147 emit_storent_insn (rtx to, rtx from)
5149 struct expand_operand ops[2];
5150 machine_mode mode = GET_MODE (to);
5151 enum insn_code code = optab_handler (storent_optab, mode);
5153 if (code == CODE_FOR_nothing)
5154 return false;
5156 create_fixed_operand (&ops[0], to);
5157 create_input_operand (&ops[1], from, mode);
5158 return maybe_expand_insn (code, 2, ops);
5161 /* Generate code for computing expression EXP,
5162 and storing the value into TARGET.
5164 If the mode is BLKmode then we may return TARGET itself.
5165 It turns out that in BLKmode it doesn't cause a problem.
5166 because C has no operators that could combine two different
5167 assignments into the same BLKmode object with different values
5168 with no sequence point. Will other languages need this to
5169 be more thorough?
5171 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5172 stack, and block moves may need to be treated specially.
5174 If NONTEMPORAL is true, try using a nontemporal store instruction.
5176 If BTARGET is not NULL then computed bounds of EXP are
5177 associated with BTARGET. */
5180 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5181 bool nontemporal, tree btarget)
5183 rtx temp;
5184 rtx alt_rtl = NULL_RTX;
5185 location_t loc = curr_insn_location ();
5187 if (VOID_TYPE_P (TREE_TYPE (exp)))
5189 /* C++ can generate ?: expressions with a throw expression in one
5190 branch and an rvalue in the other. Here, we resolve attempts to
5191 store the throw expression's nonexistent result. */
5192 gcc_assert (!call_param_p);
5193 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5194 return NULL_RTX;
5196 if (TREE_CODE (exp) == COMPOUND_EXPR)
5198 /* Perform first part of compound expression, then assign from second
5199 part. */
5200 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5201 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5202 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5203 call_param_p, nontemporal, btarget);
5205 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5207 /* For conditional expression, get safe form of the target. Then
5208 test the condition, doing the appropriate assignment on either
5209 side. This avoids the creation of unnecessary temporaries.
5210 For non-BLKmode, it is more efficient not to do this. */
5212 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5214 do_pending_stack_adjust ();
5215 NO_DEFER_POP;
5216 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5217 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5218 nontemporal, btarget);
5219 emit_jump_insn (gen_jump (lab2));
5220 emit_barrier ();
5221 emit_label (lab1);
5222 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5223 nontemporal, btarget);
5224 emit_label (lab2);
5225 OK_DEFER_POP;
5227 return NULL_RTX;
5229 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5230 /* If this is a scalar in a register that is stored in a wider mode
5231 than the declared mode, compute the result into its declared mode
5232 and then convert to the wider mode. Our value is the computed
5233 expression. */
5235 rtx inner_target = 0;
5237 /* We can do the conversion inside EXP, which will often result
5238 in some optimizations. Do the conversion in two steps: first
5239 change the signedness, if needed, then the extend. But don't
5240 do this if the type of EXP is a subtype of something else
5241 since then the conversion might involve more than just
5242 converting modes. */
5243 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5244 && TREE_TYPE (TREE_TYPE (exp)) == 0
5245 && GET_MODE_PRECISION (GET_MODE (target))
5246 == TYPE_PRECISION (TREE_TYPE (exp)))
5248 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5249 TYPE_UNSIGNED (TREE_TYPE (exp))))
5251 /* Some types, e.g. Fortran's logical*4, won't have a signed
5252 version, so use the mode instead. */
5253 tree ntype
5254 = (signed_or_unsigned_type_for
5255 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5256 if (ntype == NULL)
5257 ntype = lang_hooks.types.type_for_mode
5258 (TYPE_MODE (TREE_TYPE (exp)),
5259 SUBREG_PROMOTED_SIGN (target));
5261 exp = fold_convert_loc (loc, ntype, exp);
5264 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5265 (GET_MODE (SUBREG_REG (target)),
5266 SUBREG_PROMOTED_SIGN (target)),
5267 exp);
5269 inner_target = SUBREG_REG (target);
5272 temp = expand_expr (exp, inner_target, VOIDmode,
5273 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5275 /* Handle bounds returned by call. */
5276 if (TREE_CODE (exp) == CALL_EXPR)
5278 rtx bounds;
5279 chkp_split_slot (temp, &temp, &bounds);
5280 if (bounds && btarget)
5282 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5283 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5284 chkp_set_rtl_bounds (btarget, tmp);
5288 /* If TEMP is a VOIDmode constant, use convert_modes to make
5289 sure that we properly convert it. */
5290 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5292 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5293 temp, SUBREG_PROMOTED_SIGN (target));
5294 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5295 GET_MODE (target), temp,
5296 SUBREG_PROMOTED_SIGN (target));
5299 convert_move (SUBREG_REG (target), temp,
5300 SUBREG_PROMOTED_SIGN (target));
5302 return NULL_RTX;
5304 else if ((TREE_CODE (exp) == STRING_CST
5305 || (TREE_CODE (exp) == MEM_REF
5306 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5307 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5308 == STRING_CST
5309 && integer_zerop (TREE_OPERAND (exp, 1))))
5310 && !nontemporal && !call_param_p
5311 && MEM_P (target))
5313 /* Optimize initialization of an array with a STRING_CST. */
5314 HOST_WIDE_INT exp_len, str_copy_len;
5315 rtx dest_mem;
5316 tree str = TREE_CODE (exp) == STRING_CST
5317 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5319 exp_len = int_expr_size (exp);
5320 if (exp_len <= 0)
5321 goto normal_expr;
5323 if (TREE_STRING_LENGTH (str) <= 0)
5324 goto normal_expr;
5326 str_copy_len = strlen (TREE_STRING_POINTER (str));
5327 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5328 goto normal_expr;
5330 str_copy_len = TREE_STRING_LENGTH (str);
5331 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5332 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5334 str_copy_len += STORE_MAX_PIECES - 1;
5335 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5337 str_copy_len = MIN (str_copy_len, exp_len);
5338 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5339 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5340 MEM_ALIGN (target), false))
5341 goto normal_expr;
5343 dest_mem = target;
5345 dest_mem = store_by_pieces (dest_mem,
5346 str_copy_len, builtin_strncpy_read_str,
5347 CONST_CAST (char *,
5348 TREE_STRING_POINTER (str)),
5349 MEM_ALIGN (target), false,
5350 exp_len > str_copy_len ? 1 : 0);
5351 if (exp_len > str_copy_len)
5352 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5353 GEN_INT (exp_len - str_copy_len),
5354 BLOCK_OP_NORMAL);
5355 return NULL_RTX;
5357 else
5359 rtx tmp_target;
5361 normal_expr:
5362 /* If we want to use a nontemporal store, force the value to
5363 register first. */
5364 tmp_target = nontemporal ? NULL_RTX : target;
5365 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5366 (call_param_p
5367 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5368 &alt_rtl, false);
5370 /* Handle bounds returned by call. */
5371 if (TREE_CODE (exp) == CALL_EXPR)
5373 rtx bounds;
5374 chkp_split_slot (temp, &temp, &bounds);
5375 if (bounds && btarget)
5377 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5378 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5379 chkp_set_rtl_bounds (btarget, tmp);
5384 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5385 the same as that of TARGET, adjust the constant. This is needed, for
5386 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5387 only a word-sized value. */
5388 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5389 && TREE_CODE (exp) != ERROR_MARK
5390 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5391 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5392 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5394 /* If value was not generated in the target, store it there.
5395 Convert the value to TARGET's type first if necessary and emit the
5396 pending incrementations that have been queued when expanding EXP.
5397 Note that we cannot emit the whole queue blindly because this will
5398 effectively disable the POST_INC optimization later.
5400 If TEMP and TARGET compare equal according to rtx_equal_p, but
5401 one or both of them are volatile memory refs, we have to distinguish
5402 two cases:
5403 - expand_expr has used TARGET. In this case, we must not generate
5404 another copy. This can be detected by TARGET being equal according
5405 to == .
5406 - expand_expr has not used TARGET - that means that the source just
5407 happens to have the same RTX form. Since temp will have been created
5408 by expand_expr, it will compare unequal according to == .
5409 We must generate a copy in this case, to reach the correct number
5410 of volatile memory references. */
5412 if ((! rtx_equal_p (temp, target)
5413 || (temp != target && (side_effects_p (temp)
5414 || side_effects_p (target))))
5415 && TREE_CODE (exp) != ERROR_MARK
5416 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5417 but TARGET is not valid memory reference, TEMP will differ
5418 from TARGET although it is really the same location. */
5419 && !(alt_rtl
5420 && rtx_equal_p (alt_rtl, target)
5421 && !side_effects_p (alt_rtl)
5422 && !side_effects_p (target))
5423 /* If there's nothing to copy, don't bother. Don't call
5424 expr_size unless necessary, because some front-ends (C++)
5425 expr_size-hook must not be given objects that are not
5426 supposed to be bit-copied or bit-initialized. */
5427 && expr_size (exp) != const0_rtx)
5429 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5431 if (GET_MODE (target) == BLKmode)
5433 /* Handle calls that return BLKmode values in registers. */
5434 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5435 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5436 else
5437 store_bit_field (target,
5438 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5439 0, 0, 0, GET_MODE (temp), temp);
5441 else
5442 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5445 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5447 /* Handle copying a string constant into an array. The string
5448 constant may be shorter than the array. So copy just the string's
5449 actual length, and clear the rest. First get the size of the data
5450 type of the string, which is actually the size of the target. */
5451 rtx size = expr_size (exp);
5453 if (CONST_INT_P (size)
5454 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5455 emit_block_move (target, temp, size,
5456 (call_param_p
5457 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5458 else
5460 machine_mode pointer_mode
5461 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5462 machine_mode address_mode = get_address_mode (target);
5464 /* Compute the size of the data to copy from the string. */
5465 tree copy_size
5466 = size_binop_loc (loc, MIN_EXPR,
5467 make_tree (sizetype, size),
5468 size_int (TREE_STRING_LENGTH (exp)));
5469 rtx copy_size_rtx
5470 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5471 (call_param_p
5472 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5473 rtx_code_label *label = 0;
5475 /* Copy that much. */
5476 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5477 TYPE_UNSIGNED (sizetype));
5478 emit_block_move (target, temp, copy_size_rtx,
5479 (call_param_p
5480 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5482 /* Figure out how much is left in TARGET that we have to clear.
5483 Do all calculations in pointer_mode. */
5484 if (CONST_INT_P (copy_size_rtx))
5486 size = plus_constant (address_mode, size,
5487 -INTVAL (copy_size_rtx));
5488 target = adjust_address (target, BLKmode,
5489 INTVAL (copy_size_rtx));
5491 else
5493 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5494 copy_size_rtx, NULL_RTX, 0,
5495 OPTAB_LIB_WIDEN);
5497 if (GET_MODE (copy_size_rtx) != address_mode)
5498 copy_size_rtx = convert_to_mode (address_mode,
5499 copy_size_rtx,
5500 TYPE_UNSIGNED (sizetype));
5502 target = offset_address (target, copy_size_rtx,
5503 highest_pow2_factor (copy_size));
5504 label = gen_label_rtx ();
5505 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5506 GET_MODE (size), 0, label);
5509 if (size != const0_rtx)
5510 clear_storage (target, size, BLOCK_OP_NORMAL);
5512 if (label)
5513 emit_label (label);
5516 /* Handle calls that return values in multiple non-contiguous locations.
5517 The Irix 6 ABI has examples of this. */
5518 else if (GET_CODE (target) == PARALLEL)
5520 if (GET_CODE (temp) == PARALLEL)
5521 emit_group_move (target, temp);
5522 else
5523 emit_group_load (target, temp, TREE_TYPE (exp),
5524 int_size_in_bytes (TREE_TYPE (exp)));
5526 else if (GET_CODE (temp) == PARALLEL)
5527 emit_group_store (target, temp, TREE_TYPE (exp),
5528 int_size_in_bytes (TREE_TYPE (exp)));
5529 else if (GET_MODE (temp) == BLKmode)
5530 emit_block_move (target, temp, expr_size (exp),
5531 (call_param_p
5532 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5533 /* If we emit a nontemporal store, there is nothing else to do. */
5534 else if (nontemporal && emit_storent_insn (target, temp))
5536 else
5538 temp = force_operand (temp, target);
5539 if (temp != target)
5540 emit_move_insn (target, temp);
5544 return NULL_RTX;
5547 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5549 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5551 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5554 /* Return true if field F of structure TYPE is a flexible array. */
5556 static bool
5557 flexible_array_member_p (const_tree f, const_tree type)
5559 const_tree tf;
5561 tf = TREE_TYPE (f);
5562 return (DECL_CHAIN (f) == NULL
5563 && TREE_CODE (tf) == ARRAY_TYPE
5564 && TYPE_DOMAIN (tf)
5565 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5566 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5567 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5568 && int_size_in_bytes (type) >= 0);
5571 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5572 must have in order for it to completely initialize a value of type TYPE.
5573 Return -1 if the number isn't known.
5575 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5577 static HOST_WIDE_INT
5578 count_type_elements (const_tree type, bool for_ctor_p)
5580 switch (TREE_CODE (type))
5582 case ARRAY_TYPE:
5584 tree nelts;
5586 nelts = array_type_nelts (type);
5587 if (nelts && tree_fits_uhwi_p (nelts))
5589 unsigned HOST_WIDE_INT n;
5591 n = tree_to_uhwi (nelts) + 1;
5592 if (n == 0 || for_ctor_p)
5593 return n;
5594 else
5595 return n * count_type_elements (TREE_TYPE (type), false);
5597 return for_ctor_p ? -1 : 1;
5600 case RECORD_TYPE:
5602 unsigned HOST_WIDE_INT n;
5603 tree f;
5605 n = 0;
5606 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5607 if (TREE_CODE (f) == FIELD_DECL)
5609 if (!for_ctor_p)
5610 n += count_type_elements (TREE_TYPE (f), false);
5611 else if (!flexible_array_member_p (f, type))
5612 /* Don't count flexible arrays, which are not supposed
5613 to be initialized. */
5614 n += 1;
5617 return n;
5620 case UNION_TYPE:
5621 case QUAL_UNION_TYPE:
5623 tree f;
5624 HOST_WIDE_INT n, m;
5626 gcc_assert (!for_ctor_p);
5627 /* Estimate the number of scalars in each field and pick the
5628 maximum. Other estimates would do instead; the idea is simply
5629 to make sure that the estimate is not sensitive to the ordering
5630 of the fields. */
5631 n = 1;
5632 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5633 if (TREE_CODE (f) == FIELD_DECL)
5635 m = count_type_elements (TREE_TYPE (f), false);
5636 /* If the field doesn't span the whole union, add an extra
5637 scalar for the rest. */
5638 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5639 TYPE_SIZE (type)) != 1)
5640 m++;
5641 if (n < m)
5642 n = m;
5644 return n;
5647 case COMPLEX_TYPE:
5648 return 2;
5650 case VECTOR_TYPE:
5651 return TYPE_VECTOR_SUBPARTS (type);
5653 case INTEGER_TYPE:
5654 case REAL_TYPE:
5655 case FIXED_POINT_TYPE:
5656 case ENUMERAL_TYPE:
5657 case BOOLEAN_TYPE:
5658 case POINTER_TYPE:
5659 case OFFSET_TYPE:
5660 case REFERENCE_TYPE:
5661 case NULLPTR_TYPE:
5662 return 1;
5664 case ERROR_MARK:
5665 return 0;
5667 case VOID_TYPE:
5668 case METHOD_TYPE:
5669 case FUNCTION_TYPE:
5670 case LANG_TYPE:
5671 default:
5672 gcc_unreachable ();
5676 /* Helper for categorize_ctor_elements. Identical interface. */
5678 static bool
5679 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5680 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5682 unsigned HOST_WIDE_INT idx;
5683 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5684 tree value, purpose, elt_type;
5686 /* Whether CTOR is a valid constant initializer, in accordance with what
5687 initializer_constant_valid_p does. If inferred from the constructor
5688 elements, true until proven otherwise. */
5689 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5690 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5692 nz_elts = 0;
5693 init_elts = 0;
5694 num_fields = 0;
5695 elt_type = NULL_TREE;
5697 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5699 HOST_WIDE_INT mult = 1;
5701 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5703 tree lo_index = TREE_OPERAND (purpose, 0);
5704 tree hi_index = TREE_OPERAND (purpose, 1);
5706 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5707 mult = (tree_to_uhwi (hi_index)
5708 - tree_to_uhwi (lo_index) + 1);
5710 num_fields += mult;
5711 elt_type = TREE_TYPE (value);
5713 switch (TREE_CODE (value))
5715 case CONSTRUCTOR:
5717 HOST_WIDE_INT nz = 0, ic = 0;
5719 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5720 p_complete);
5722 nz_elts += mult * nz;
5723 init_elts += mult * ic;
5725 if (const_from_elts_p && const_p)
5726 const_p = const_elt_p;
5728 break;
5730 case INTEGER_CST:
5731 case REAL_CST:
5732 case FIXED_CST:
5733 if (!initializer_zerop (value))
5734 nz_elts += mult;
5735 init_elts += mult;
5736 break;
5738 case STRING_CST:
5739 nz_elts += mult * TREE_STRING_LENGTH (value);
5740 init_elts += mult * TREE_STRING_LENGTH (value);
5741 break;
5743 case COMPLEX_CST:
5744 if (!initializer_zerop (TREE_REALPART (value)))
5745 nz_elts += mult;
5746 if (!initializer_zerop (TREE_IMAGPART (value)))
5747 nz_elts += mult;
5748 init_elts += mult;
5749 break;
5751 case VECTOR_CST:
5753 unsigned i;
5754 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5756 tree v = VECTOR_CST_ELT (value, i);
5757 if (!initializer_zerop (v))
5758 nz_elts += mult;
5759 init_elts += mult;
5762 break;
5764 default:
5766 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5767 nz_elts += mult * tc;
5768 init_elts += mult * tc;
5770 if (const_from_elts_p && const_p)
5771 const_p = initializer_constant_valid_p (value, elt_type)
5772 != NULL_TREE;
5774 break;
5778 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5779 num_fields, elt_type))
5780 *p_complete = false;
5782 *p_nz_elts += nz_elts;
5783 *p_init_elts += init_elts;
5785 return const_p;
5788 /* Examine CTOR to discover:
5789 * how many scalar fields are set to nonzero values,
5790 and place it in *P_NZ_ELTS;
5791 * how many scalar fields in total are in CTOR,
5792 and place it in *P_ELT_COUNT.
5793 * whether the constructor is complete -- in the sense that every
5794 meaningful byte is explicitly given a value --
5795 and place it in *P_COMPLETE.
5797 Return whether or not CTOR is a valid static constant initializer, the same
5798 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5800 bool
5801 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5802 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5804 *p_nz_elts = 0;
5805 *p_init_elts = 0;
5806 *p_complete = true;
5808 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5811 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5812 of which had type LAST_TYPE. Each element was itself a complete
5813 initializer, in the sense that every meaningful byte was explicitly
5814 given a value. Return true if the same is true for the constructor
5815 as a whole. */
5817 bool
5818 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5819 const_tree last_type)
5821 if (TREE_CODE (type) == UNION_TYPE
5822 || TREE_CODE (type) == QUAL_UNION_TYPE)
5824 if (num_elts == 0)
5825 return false;
5827 gcc_assert (num_elts == 1 && last_type);
5829 /* ??? We could look at each element of the union, and find the
5830 largest element. Which would avoid comparing the size of the
5831 initialized element against any tail padding in the union.
5832 Doesn't seem worth the effort... */
5833 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5836 return count_type_elements (type, true) == num_elts;
5839 /* Return 1 if EXP contains mostly (3/4) zeros. */
5841 static int
5842 mostly_zeros_p (const_tree exp)
5844 if (TREE_CODE (exp) == CONSTRUCTOR)
5846 HOST_WIDE_INT nz_elts, init_elts;
5847 bool complete_p;
5849 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5850 return !complete_p || nz_elts < init_elts / 4;
5853 return initializer_zerop (exp);
5856 /* Return 1 if EXP contains all zeros. */
5858 static int
5859 all_zeros_p (const_tree exp)
5861 if (TREE_CODE (exp) == CONSTRUCTOR)
5863 HOST_WIDE_INT nz_elts, init_elts;
5864 bool complete_p;
5866 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5867 return nz_elts == 0;
5870 return initializer_zerop (exp);
5873 /* Helper function for store_constructor.
5874 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5875 CLEARED is as for store_constructor.
5876 ALIAS_SET is the alias set to use for any stores.
5878 This provides a recursive shortcut back to store_constructor when it isn't
5879 necessary to go through store_field. This is so that we can pass through
5880 the cleared field to let store_constructor know that we may not have to
5881 clear a substructure if the outer structure has already been cleared. */
5883 static void
5884 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5885 HOST_WIDE_INT bitpos, machine_mode mode,
5886 tree exp, int cleared, alias_set_type alias_set)
5888 if (TREE_CODE (exp) == CONSTRUCTOR
5889 /* We can only call store_constructor recursively if the size and
5890 bit position are on a byte boundary. */
5891 && bitpos % BITS_PER_UNIT == 0
5892 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5893 /* If we have a nonzero bitpos for a register target, then we just
5894 let store_field do the bitfield handling. This is unlikely to
5895 generate unnecessary clear instructions anyways. */
5896 && (bitpos == 0 || MEM_P (target)))
5898 if (MEM_P (target))
5899 target
5900 = adjust_address (target,
5901 GET_MODE (target) == BLKmode
5902 || 0 != (bitpos
5903 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5904 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5907 /* Update the alias set, if required. */
5908 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5909 && MEM_ALIAS_SET (target) != 0)
5911 target = copy_rtx (target);
5912 set_mem_alias_set (target, alias_set);
5915 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5917 else
5918 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5922 /* Returns the number of FIELD_DECLs in TYPE. */
5924 static int
5925 fields_length (const_tree type)
5927 tree t = TYPE_FIELDS (type);
5928 int count = 0;
5930 for (; t; t = DECL_CHAIN (t))
5931 if (TREE_CODE (t) == FIELD_DECL)
5932 ++count;
5934 return count;
5938 /* Store the value of constructor EXP into the rtx TARGET.
5939 TARGET is either a REG or a MEM; we know it cannot conflict, since
5940 safe_from_p has been called.
5941 CLEARED is true if TARGET is known to have been zero'd.
5942 SIZE is the number of bytes of TARGET we are allowed to modify: this
5943 may not be the same as the size of EXP if we are assigning to a field
5944 which has been packed to exclude padding bits. */
5946 static void
5947 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5949 tree type = TREE_TYPE (exp);
5950 #ifdef WORD_REGISTER_OPERATIONS
5951 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5952 #endif
5954 switch (TREE_CODE (type))
5956 case RECORD_TYPE:
5957 case UNION_TYPE:
5958 case QUAL_UNION_TYPE:
5960 unsigned HOST_WIDE_INT idx;
5961 tree field, value;
5963 /* If size is zero or the target is already cleared, do nothing. */
5964 if (size == 0 || cleared)
5965 cleared = 1;
5966 /* We either clear the aggregate or indicate the value is dead. */
5967 else if ((TREE_CODE (type) == UNION_TYPE
5968 || TREE_CODE (type) == QUAL_UNION_TYPE)
5969 && ! CONSTRUCTOR_ELTS (exp))
5970 /* If the constructor is empty, clear the union. */
5972 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5973 cleared = 1;
5976 /* If we are building a static constructor into a register,
5977 set the initial value as zero so we can fold the value into
5978 a constant. But if more than one register is involved,
5979 this probably loses. */
5980 else if (REG_P (target) && TREE_STATIC (exp)
5981 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5983 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5984 cleared = 1;
5987 /* If the constructor has fewer fields than the structure or
5988 if we are initializing the structure to mostly zeros, clear
5989 the whole structure first. Don't do this if TARGET is a
5990 register whose mode size isn't equal to SIZE since
5991 clear_storage can't handle this case. */
5992 else if (size > 0
5993 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
5994 != fields_length (type))
5995 || mostly_zeros_p (exp))
5996 && (!REG_P (target)
5997 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5998 == size)))
6000 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6001 cleared = 1;
6004 if (REG_P (target) && !cleared)
6005 emit_clobber (target);
6007 /* Store each element of the constructor into the
6008 corresponding field of TARGET. */
6009 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6011 machine_mode mode;
6012 HOST_WIDE_INT bitsize;
6013 HOST_WIDE_INT bitpos = 0;
6014 tree offset;
6015 rtx to_rtx = target;
6017 /* Just ignore missing fields. We cleared the whole
6018 structure, above, if any fields are missing. */
6019 if (field == 0)
6020 continue;
6022 if (cleared && initializer_zerop (value))
6023 continue;
6025 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6026 bitsize = tree_to_uhwi (DECL_SIZE (field));
6027 else
6028 bitsize = -1;
6030 mode = DECL_MODE (field);
6031 if (DECL_BIT_FIELD (field))
6032 mode = VOIDmode;
6034 offset = DECL_FIELD_OFFSET (field);
6035 if (tree_fits_shwi_p (offset)
6036 && tree_fits_shwi_p (bit_position (field)))
6038 bitpos = int_bit_position (field);
6039 offset = 0;
6041 else
6042 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6044 if (offset)
6046 machine_mode address_mode;
6047 rtx offset_rtx;
6049 offset
6050 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6051 make_tree (TREE_TYPE (exp),
6052 target));
6054 offset_rtx = expand_normal (offset);
6055 gcc_assert (MEM_P (to_rtx));
6057 address_mode = get_address_mode (to_rtx);
6058 if (GET_MODE (offset_rtx) != address_mode)
6059 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6061 to_rtx = offset_address (to_rtx, offset_rtx,
6062 highest_pow2_factor (offset));
6065 #ifdef WORD_REGISTER_OPERATIONS
6066 /* If this initializes a field that is smaller than a
6067 word, at the start of a word, try to widen it to a full
6068 word. This special case allows us to output C++ member
6069 function initializations in a form that the optimizers
6070 can understand. */
6071 if (REG_P (target)
6072 && bitsize < BITS_PER_WORD
6073 && bitpos % BITS_PER_WORD == 0
6074 && GET_MODE_CLASS (mode) == MODE_INT
6075 && TREE_CODE (value) == INTEGER_CST
6076 && exp_size >= 0
6077 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6079 tree type = TREE_TYPE (value);
6081 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6083 type = lang_hooks.types.type_for_mode
6084 (word_mode, TYPE_UNSIGNED (type));
6085 value = fold_convert (type, value);
6088 if (BYTES_BIG_ENDIAN)
6089 value
6090 = fold_build2 (LSHIFT_EXPR, type, value,
6091 build_int_cst (type,
6092 BITS_PER_WORD - bitsize));
6093 bitsize = BITS_PER_WORD;
6094 mode = word_mode;
6096 #endif
6098 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6099 && DECL_NONADDRESSABLE_P (field))
6101 to_rtx = copy_rtx (to_rtx);
6102 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6105 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6106 value, cleared,
6107 get_alias_set (TREE_TYPE (field)));
6109 break;
6111 case ARRAY_TYPE:
6113 tree value, index;
6114 unsigned HOST_WIDE_INT i;
6115 int need_to_clear;
6116 tree domain;
6117 tree elttype = TREE_TYPE (type);
6118 int const_bounds_p;
6119 HOST_WIDE_INT minelt = 0;
6120 HOST_WIDE_INT maxelt = 0;
6122 domain = TYPE_DOMAIN (type);
6123 const_bounds_p = (TYPE_MIN_VALUE (domain)
6124 && TYPE_MAX_VALUE (domain)
6125 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6126 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6128 /* If we have constant bounds for the range of the type, get them. */
6129 if (const_bounds_p)
6131 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6132 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6135 /* If the constructor has fewer elements than the array, clear
6136 the whole array first. Similarly if this is static
6137 constructor of a non-BLKmode object. */
6138 if (cleared)
6139 need_to_clear = 0;
6140 else if (REG_P (target) && TREE_STATIC (exp))
6141 need_to_clear = 1;
6142 else
6144 unsigned HOST_WIDE_INT idx;
6145 tree index, value;
6146 HOST_WIDE_INT count = 0, zero_count = 0;
6147 need_to_clear = ! const_bounds_p;
6149 /* This loop is a more accurate version of the loop in
6150 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6151 is also needed to check for missing elements. */
6152 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6154 HOST_WIDE_INT this_node_count;
6156 if (need_to_clear)
6157 break;
6159 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6161 tree lo_index = TREE_OPERAND (index, 0);
6162 tree hi_index = TREE_OPERAND (index, 1);
6164 if (! tree_fits_uhwi_p (lo_index)
6165 || ! tree_fits_uhwi_p (hi_index))
6167 need_to_clear = 1;
6168 break;
6171 this_node_count = (tree_to_uhwi (hi_index)
6172 - tree_to_uhwi (lo_index) + 1);
6174 else
6175 this_node_count = 1;
6177 count += this_node_count;
6178 if (mostly_zeros_p (value))
6179 zero_count += this_node_count;
6182 /* Clear the entire array first if there are any missing
6183 elements, or if the incidence of zero elements is >=
6184 75%. */
6185 if (! need_to_clear
6186 && (count < maxelt - minelt + 1
6187 || 4 * zero_count >= 3 * count))
6188 need_to_clear = 1;
6191 if (need_to_clear && size > 0)
6193 if (REG_P (target))
6194 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6195 else
6196 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6197 cleared = 1;
6200 if (!cleared && REG_P (target))
6201 /* Inform later passes that the old value is dead. */
6202 emit_clobber (target);
6204 /* Store each element of the constructor into the
6205 corresponding element of TARGET, determined by counting the
6206 elements. */
6207 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6209 machine_mode mode;
6210 HOST_WIDE_INT bitsize;
6211 HOST_WIDE_INT bitpos;
6212 rtx xtarget = target;
6214 if (cleared && initializer_zerop (value))
6215 continue;
6217 mode = TYPE_MODE (elttype);
6218 if (mode == BLKmode)
6219 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6220 ? tree_to_uhwi (TYPE_SIZE (elttype))
6221 : -1);
6222 else
6223 bitsize = GET_MODE_BITSIZE (mode);
6225 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6227 tree lo_index = TREE_OPERAND (index, 0);
6228 tree hi_index = TREE_OPERAND (index, 1);
6229 rtx index_r, pos_rtx;
6230 HOST_WIDE_INT lo, hi, count;
6231 tree position;
6233 /* If the range is constant and "small", unroll the loop. */
6234 if (const_bounds_p
6235 && tree_fits_shwi_p (lo_index)
6236 && tree_fits_shwi_p (hi_index)
6237 && (lo = tree_to_shwi (lo_index),
6238 hi = tree_to_shwi (hi_index),
6239 count = hi - lo + 1,
6240 (!MEM_P (target)
6241 || count <= 2
6242 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6243 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6244 <= 40 * 8)))))
6246 lo -= minelt; hi -= minelt;
6247 for (; lo <= hi; lo++)
6249 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6251 if (MEM_P (target)
6252 && !MEM_KEEP_ALIAS_SET_P (target)
6253 && TREE_CODE (type) == ARRAY_TYPE
6254 && TYPE_NONALIASED_COMPONENT (type))
6256 target = copy_rtx (target);
6257 MEM_KEEP_ALIAS_SET_P (target) = 1;
6260 store_constructor_field
6261 (target, bitsize, bitpos, mode, value, cleared,
6262 get_alias_set (elttype));
6265 else
6267 rtx_code_label *loop_start = gen_label_rtx ();
6268 rtx_code_label *loop_end = gen_label_rtx ();
6269 tree exit_cond;
6271 expand_normal (hi_index);
6273 index = build_decl (EXPR_LOCATION (exp),
6274 VAR_DECL, NULL_TREE, domain);
6275 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6276 SET_DECL_RTL (index, index_r);
6277 store_expr (lo_index, index_r, 0, false);
6279 /* Build the head of the loop. */
6280 do_pending_stack_adjust ();
6281 emit_label (loop_start);
6283 /* Assign value to element index. */
6284 position =
6285 fold_convert (ssizetype,
6286 fold_build2 (MINUS_EXPR,
6287 TREE_TYPE (index),
6288 index,
6289 TYPE_MIN_VALUE (domain)));
6291 position =
6292 size_binop (MULT_EXPR, position,
6293 fold_convert (ssizetype,
6294 TYPE_SIZE_UNIT (elttype)));
6296 pos_rtx = expand_normal (position);
6297 xtarget = offset_address (target, pos_rtx,
6298 highest_pow2_factor (position));
6299 xtarget = adjust_address (xtarget, mode, 0);
6300 if (TREE_CODE (value) == CONSTRUCTOR)
6301 store_constructor (value, xtarget, cleared,
6302 bitsize / BITS_PER_UNIT);
6303 else
6304 store_expr (value, xtarget, 0, false);
6306 /* Generate a conditional jump to exit the loop. */
6307 exit_cond = build2 (LT_EXPR, integer_type_node,
6308 index, hi_index);
6309 jumpif (exit_cond, loop_end, -1);
6311 /* Update the loop counter, and jump to the head of
6312 the loop. */
6313 expand_assignment (index,
6314 build2 (PLUS_EXPR, TREE_TYPE (index),
6315 index, integer_one_node),
6316 false);
6318 emit_jump (loop_start);
6320 /* Build the end of the loop. */
6321 emit_label (loop_end);
6324 else if ((index != 0 && ! tree_fits_shwi_p (index))
6325 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6327 tree position;
6329 if (index == 0)
6330 index = ssize_int (1);
6332 if (minelt)
6333 index = fold_convert (ssizetype,
6334 fold_build2 (MINUS_EXPR,
6335 TREE_TYPE (index),
6336 index,
6337 TYPE_MIN_VALUE (domain)));
6339 position =
6340 size_binop (MULT_EXPR, index,
6341 fold_convert (ssizetype,
6342 TYPE_SIZE_UNIT (elttype)));
6343 xtarget = offset_address (target,
6344 expand_normal (position),
6345 highest_pow2_factor (position));
6346 xtarget = adjust_address (xtarget, mode, 0);
6347 store_expr (value, xtarget, 0, false);
6349 else
6351 if (index != 0)
6352 bitpos = ((tree_to_shwi (index) - minelt)
6353 * tree_to_uhwi (TYPE_SIZE (elttype)));
6354 else
6355 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6357 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6358 && TREE_CODE (type) == ARRAY_TYPE
6359 && TYPE_NONALIASED_COMPONENT (type))
6361 target = copy_rtx (target);
6362 MEM_KEEP_ALIAS_SET_P (target) = 1;
6364 store_constructor_field (target, bitsize, bitpos, mode, value,
6365 cleared, get_alias_set (elttype));
6368 break;
6371 case VECTOR_TYPE:
6373 unsigned HOST_WIDE_INT idx;
6374 constructor_elt *ce;
6375 int i;
6376 int need_to_clear;
6377 int icode = CODE_FOR_nothing;
6378 tree elttype = TREE_TYPE (type);
6379 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6380 machine_mode eltmode = TYPE_MODE (elttype);
6381 HOST_WIDE_INT bitsize;
6382 HOST_WIDE_INT bitpos;
6383 rtvec vector = NULL;
6384 unsigned n_elts;
6385 alias_set_type alias;
6387 gcc_assert (eltmode != BLKmode);
6389 n_elts = TYPE_VECTOR_SUBPARTS (type);
6390 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6392 machine_mode mode = GET_MODE (target);
6394 icode = (int) optab_handler (vec_init_optab, mode);
6395 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6396 if (icode != CODE_FOR_nothing)
6398 tree value;
6400 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6401 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6403 icode = CODE_FOR_nothing;
6404 break;
6407 if (icode != CODE_FOR_nothing)
6409 unsigned int i;
6411 vector = rtvec_alloc (n_elts);
6412 for (i = 0; i < n_elts; i++)
6413 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6417 /* If the constructor has fewer elements than the vector,
6418 clear the whole array first. Similarly if this is static
6419 constructor of a non-BLKmode object. */
6420 if (cleared)
6421 need_to_clear = 0;
6422 else if (REG_P (target) && TREE_STATIC (exp))
6423 need_to_clear = 1;
6424 else
6426 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6427 tree value;
6429 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6431 int n_elts_here = tree_to_uhwi
6432 (int_const_binop (TRUNC_DIV_EXPR,
6433 TYPE_SIZE (TREE_TYPE (value)),
6434 TYPE_SIZE (elttype)));
6436 count += n_elts_here;
6437 if (mostly_zeros_p (value))
6438 zero_count += n_elts_here;
6441 /* Clear the entire vector first if there are any missing elements,
6442 or if the incidence of zero elements is >= 75%. */
6443 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6446 if (need_to_clear && size > 0 && !vector)
6448 if (REG_P (target))
6449 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6450 else
6451 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6452 cleared = 1;
6455 /* Inform later passes that the old value is dead. */
6456 if (!cleared && !vector && REG_P (target))
6457 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6459 if (MEM_P (target))
6460 alias = MEM_ALIAS_SET (target);
6461 else
6462 alias = get_alias_set (elttype);
6464 /* Store each element of the constructor into the corresponding
6465 element of TARGET, determined by counting the elements. */
6466 for (idx = 0, i = 0;
6467 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6468 idx++, i += bitsize / elt_size)
6470 HOST_WIDE_INT eltpos;
6471 tree value = ce->value;
6473 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6474 if (cleared && initializer_zerop (value))
6475 continue;
6477 if (ce->index)
6478 eltpos = tree_to_uhwi (ce->index);
6479 else
6480 eltpos = i;
6482 if (vector)
6484 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6485 elements. */
6486 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6487 RTVEC_ELT (vector, eltpos)
6488 = expand_normal (value);
6490 else
6492 machine_mode value_mode =
6493 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6494 ? TYPE_MODE (TREE_TYPE (value))
6495 : eltmode;
6496 bitpos = eltpos * elt_size;
6497 store_constructor_field (target, bitsize, bitpos, value_mode,
6498 value, cleared, alias);
6502 if (vector)
6503 emit_insn (GEN_FCN (icode)
6504 (target,
6505 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6506 break;
6509 default:
6510 gcc_unreachable ();
6514 /* Store the value of EXP (an expression tree)
6515 into a subfield of TARGET which has mode MODE and occupies
6516 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6517 If MODE is VOIDmode, it means that we are storing into a bit-field.
6519 BITREGION_START is bitpos of the first bitfield in this region.
6520 BITREGION_END is the bitpos of the ending bitfield in this region.
6521 These two fields are 0, if the C++ memory model does not apply,
6522 or we are not interested in keeping track of bitfield regions.
6524 Always return const0_rtx unless we have something particular to
6525 return.
6527 ALIAS_SET is the alias set for the destination. This value will
6528 (in general) be different from that for TARGET, since TARGET is a
6529 reference to the containing structure.
6531 If NONTEMPORAL is true, try generating a nontemporal store. */
6533 static rtx
6534 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6535 unsigned HOST_WIDE_INT bitregion_start,
6536 unsigned HOST_WIDE_INT bitregion_end,
6537 machine_mode mode, tree exp,
6538 alias_set_type alias_set, bool nontemporal)
6540 if (TREE_CODE (exp) == ERROR_MARK)
6541 return const0_rtx;
6543 /* If we have nothing to store, do nothing unless the expression has
6544 side-effects. */
6545 if (bitsize == 0)
6546 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6548 if (GET_CODE (target) == CONCAT)
6550 /* We're storing into a struct containing a single __complex. */
6552 gcc_assert (!bitpos);
6553 return store_expr (exp, target, 0, nontemporal);
6556 /* If the structure is in a register or if the component
6557 is a bit field, we cannot use addressing to access it.
6558 Use bit-field techniques or SUBREG to store in it. */
6560 if (mode == VOIDmode
6561 || (mode != BLKmode && ! direct_store[(int) mode]
6562 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6563 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6564 || REG_P (target)
6565 || GET_CODE (target) == SUBREG
6566 /* If the field isn't aligned enough to store as an ordinary memref,
6567 store it as a bit field. */
6568 || (mode != BLKmode
6569 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6570 || bitpos % GET_MODE_ALIGNMENT (mode))
6571 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6572 || (bitpos % BITS_PER_UNIT != 0)))
6573 || (bitsize >= 0 && mode != BLKmode
6574 && GET_MODE_BITSIZE (mode) > bitsize)
6575 /* If the RHS and field are a constant size and the size of the
6576 RHS isn't the same size as the bitfield, we must use bitfield
6577 operations. */
6578 || (bitsize >= 0
6579 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6580 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6581 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6582 decl we must use bitfield operations. */
6583 || (bitsize >= 0
6584 && TREE_CODE (exp) == MEM_REF
6585 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6586 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6587 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6588 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6590 rtx temp;
6591 gimple nop_def;
6593 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6594 implies a mask operation. If the precision is the same size as
6595 the field we're storing into, that mask is redundant. This is
6596 particularly common with bit field assignments generated by the
6597 C front end. */
6598 nop_def = get_def_for_expr (exp, NOP_EXPR);
6599 if (nop_def)
6601 tree type = TREE_TYPE (exp);
6602 if (INTEGRAL_TYPE_P (type)
6603 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6604 && bitsize == TYPE_PRECISION (type))
6606 tree op = gimple_assign_rhs1 (nop_def);
6607 type = TREE_TYPE (op);
6608 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6609 exp = op;
6613 temp = expand_normal (exp);
6615 /* If BITSIZE is narrower than the size of the type of EXP
6616 we will be narrowing TEMP. Normally, what's wanted are the
6617 low-order bits. However, if EXP's type is a record and this is
6618 big-endian machine, we want the upper BITSIZE bits. */
6619 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6620 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6621 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6622 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6623 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6624 NULL_RTX, 1);
6626 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6627 if (mode != VOIDmode && mode != BLKmode
6628 && mode != TYPE_MODE (TREE_TYPE (exp)))
6629 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6631 /* If the modes of TEMP and TARGET are both BLKmode, both
6632 must be in memory and BITPOS must be aligned on a byte
6633 boundary. If so, we simply do a block copy. Likewise
6634 for a BLKmode-like TARGET. */
6635 if (GET_MODE (temp) == BLKmode
6636 && (GET_MODE (target) == BLKmode
6637 || (MEM_P (target)
6638 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6639 && (bitpos % BITS_PER_UNIT) == 0
6640 && (bitsize % BITS_PER_UNIT) == 0)))
6642 gcc_assert (MEM_P (target) && MEM_P (temp)
6643 && (bitpos % BITS_PER_UNIT) == 0);
6645 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6646 emit_block_move (target, temp,
6647 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6648 / BITS_PER_UNIT),
6649 BLOCK_OP_NORMAL);
6651 return const0_rtx;
6654 /* Handle calls that return values in multiple non-contiguous locations.
6655 The Irix 6 ABI has examples of this. */
6656 if (GET_CODE (temp) == PARALLEL)
6658 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6659 rtx temp_target;
6660 if (mode == BLKmode || mode == VOIDmode)
6661 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6662 temp_target = gen_reg_rtx (mode);
6663 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6664 temp = temp_target;
6666 else if (mode == BLKmode)
6668 /* Handle calls that return BLKmode values in registers. */
6669 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6671 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6672 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6673 temp = temp_target;
6675 else
6677 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6678 rtx temp_target;
6679 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6680 temp_target = gen_reg_rtx (mode);
6681 temp_target
6682 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6683 temp_target, mode, mode);
6684 temp = temp_target;
6688 /* Store the value in the bitfield. */
6689 store_bit_field (target, bitsize, bitpos,
6690 bitregion_start, bitregion_end,
6691 mode, temp);
6693 return const0_rtx;
6695 else
6697 /* Now build a reference to just the desired component. */
6698 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6700 if (to_rtx == target)
6701 to_rtx = copy_rtx (to_rtx);
6703 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6704 set_mem_alias_set (to_rtx, alias_set);
6706 return store_expr (exp, to_rtx, 0, nontemporal);
6710 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6711 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6712 codes and find the ultimate containing object, which we return.
6714 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6715 bit position, and *PUNSIGNEDP to the signedness of the field.
6716 If the position of the field is variable, we store a tree
6717 giving the variable offset (in units) in *POFFSET.
6718 This offset is in addition to the bit position.
6719 If the position is not variable, we store 0 in *POFFSET.
6721 If any of the extraction expressions is volatile,
6722 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6724 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6725 Otherwise, it is a mode that can be used to access the field.
6727 If the field describes a variable-sized object, *PMODE is set to
6728 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6729 this case, but the address of the object can be found.
6731 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6732 look through nodes that serve as markers of a greater alignment than
6733 the one that can be deduced from the expression. These nodes make it
6734 possible for front-ends to prevent temporaries from being created by
6735 the middle-end on alignment considerations. For that purpose, the
6736 normal operating mode at high-level is to always pass FALSE so that
6737 the ultimate containing object is really returned; moreover, the
6738 associated predicate handled_component_p will always return TRUE
6739 on these nodes, thus indicating that they are essentially handled
6740 by get_inner_reference. TRUE should only be passed when the caller
6741 is scanning the expression in order to build another representation
6742 and specifically knows how to handle these nodes; as such, this is
6743 the normal operating mode in the RTL expanders. */
6745 tree
6746 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6747 HOST_WIDE_INT *pbitpos, tree *poffset,
6748 machine_mode *pmode, int *punsignedp,
6749 int *pvolatilep, bool keep_aligning)
6751 tree size_tree = 0;
6752 machine_mode mode = VOIDmode;
6753 bool blkmode_bitfield = false;
6754 tree offset = size_zero_node;
6755 offset_int bit_offset = 0;
6757 /* First get the mode, signedness, and size. We do this from just the
6758 outermost expression. */
6759 *pbitsize = -1;
6760 if (TREE_CODE (exp) == COMPONENT_REF)
6762 tree field = TREE_OPERAND (exp, 1);
6763 size_tree = DECL_SIZE (field);
6764 if (flag_strict_volatile_bitfields > 0
6765 && TREE_THIS_VOLATILE (exp)
6766 && DECL_BIT_FIELD_TYPE (field)
6767 && DECL_MODE (field) != BLKmode)
6768 /* Volatile bitfields should be accessed in the mode of the
6769 field's type, not the mode computed based on the bit
6770 size. */
6771 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6772 else if (!DECL_BIT_FIELD (field))
6773 mode = DECL_MODE (field);
6774 else if (DECL_MODE (field) == BLKmode)
6775 blkmode_bitfield = true;
6777 *punsignedp = DECL_UNSIGNED (field);
6779 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6781 size_tree = TREE_OPERAND (exp, 1);
6782 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6783 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6785 /* For vector types, with the correct size of access, use the mode of
6786 inner type. */
6787 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6788 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6789 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6790 mode = TYPE_MODE (TREE_TYPE (exp));
6792 else
6794 mode = TYPE_MODE (TREE_TYPE (exp));
6795 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6797 if (mode == BLKmode)
6798 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6799 else
6800 *pbitsize = GET_MODE_BITSIZE (mode);
6803 if (size_tree != 0)
6805 if (! tree_fits_uhwi_p (size_tree))
6806 mode = BLKmode, *pbitsize = -1;
6807 else
6808 *pbitsize = tree_to_uhwi (size_tree);
6811 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6812 and find the ultimate containing object. */
6813 while (1)
6815 switch (TREE_CODE (exp))
6817 case BIT_FIELD_REF:
6818 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6819 break;
6821 case COMPONENT_REF:
6823 tree field = TREE_OPERAND (exp, 1);
6824 tree this_offset = component_ref_field_offset (exp);
6826 /* If this field hasn't been filled in yet, don't go past it.
6827 This should only happen when folding expressions made during
6828 type construction. */
6829 if (this_offset == 0)
6830 break;
6832 offset = size_binop (PLUS_EXPR, offset, this_offset);
6833 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6835 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6837 break;
6839 case ARRAY_REF:
6840 case ARRAY_RANGE_REF:
6842 tree index = TREE_OPERAND (exp, 1);
6843 tree low_bound = array_ref_low_bound (exp);
6844 tree unit_size = array_ref_element_size (exp);
6846 /* We assume all arrays have sizes that are a multiple of a byte.
6847 First subtract the lower bound, if any, in the type of the
6848 index, then convert to sizetype and multiply by the size of
6849 the array element. */
6850 if (! integer_zerop (low_bound))
6851 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6852 index, low_bound);
6854 offset = size_binop (PLUS_EXPR, offset,
6855 size_binop (MULT_EXPR,
6856 fold_convert (sizetype, index),
6857 unit_size));
6859 break;
6861 case REALPART_EXPR:
6862 break;
6864 case IMAGPART_EXPR:
6865 bit_offset += *pbitsize;
6866 break;
6868 case VIEW_CONVERT_EXPR:
6869 if (keep_aligning && STRICT_ALIGNMENT
6870 && (TYPE_ALIGN (TREE_TYPE (exp))
6871 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6872 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6873 < BIGGEST_ALIGNMENT)
6874 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6875 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6876 goto done;
6877 break;
6879 case MEM_REF:
6880 /* Hand back the decl for MEM[&decl, off]. */
6881 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6883 tree off = TREE_OPERAND (exp, 1);
6884 if (!integer_zerop (off))
6886 offset_int boff, coff = mem_ref_offset (exp);
6887 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6888 bit_offset += boff;
6890 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6892 goto done;
6894 default:
6895 goto done;
6898 /* If any reference in the chain is volatile, the effect is volatile. */
6899 if (TREE_THIS_VOLATILE (exp))
6900 *pvolatilep = 1;
6902 exp = TREE_OPERAND (exp, 0);
6904 done:
6906 /* If OFFSET is constant, see if we can return the whole thing as a
6907 constant bit position. Make sure to handle overflow during
6908 this conversion. */
6909 if (TREE_CODE (offset) == INTEGER_CST)
6911 offset_int tem = wi::sext (wi::to_offset (offset),
6912 TYPE_PRECISION (sizetype));
6913 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6914 tem += bit_offset;
6915 if (wi::fits_shwi_p (tem))
6917 *pbitpos = tem.to_shwi ();
6918 *poffset = offset = NULL_TREE;
6922 /* Otherwise, split it up. */
6923 if (offset)
6925 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6926 if (wi::neg_p (bit_offset))
6928 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6929 offset_int tem = bit_offset.and_not (mask);
6930 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6931 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6932 bit_offset -= tem;
6933 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6934 offset = size_binop (PLUS_EXPR, offset,
6935 wide_int_to_tree (sizetype, tem));
6938 *pbitpos = bit_offset.to_shwi ();
6939 *poffset = offset;
6942 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6943 if (mode == VOIDmode
6944 && blkmode_bitfield
6945 && (*pbitpos % BITS_PER_UNIT) == 0
6946 && (*pbitsize % BITS_PER_UNIT) == 0)
6947 *pmode = BLKmode;
6948 else
6949 *pmode = mode;
6951 return exp;
6954 /* Return a tree of sizetype representing the size, in bytes, of the element
6955 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6957 tree
6958 array_ref_element_size (tree exp)
6960 tree aligned_size = TREE_OPERAND (exp, 3);
6961 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6962 location_t loc = EXPR_LOCATION (exp);
6964 /* If a size was specified in the ARRAY_REF, it's the size measured
6965 in alignment units of the element type. So multiply by that value. */
6966 if (aligned_size)
6968 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6969 sizetype from another type of the same width and signedness. */
6970 if (TREE_TYPE (aligned_size) != sizetype)
6971 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6972 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6973 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6976 /* Otherwise, take the size from that of the element type. Substitute
6977 any PLACEHOLDER_EXPR that we have. */
6978 else
6979 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6982 /* Return a tree representing the lower bound of the array mentioned in
6983 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6985 tree
6986 array_ref_low_bound (tree exp)
6988 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6990 /* If a lower bound is specified in EXP, use it. */
6991 if (TREE_OPERAND (exp, 2))
6992 return TREE_OPERAND (exp, 2);
6994 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6995 substituting for a PLACEHOLDER_EXPR as needed. */
6996 if (domain_type && TYPE_MIN_VALUE (domain_type))
6997 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6999 /* Otherwise, return a zero of the appropriate type. */
7000 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
7003 /* Returns true if REF is an array reference to an array at the end of
7004 a structure. If this is the case, the array may be allocated larger
7005 than its upper bound implies. */
7007 bool
7008 array_at_struct_end_p (tree ref)
7010 if (TREE_CODE (ref) != ARRAY_REF
7011 && TREE_CODE (ref) != ARRAY_RANGE_REF)
7012 return false;
7014 while (handled_component_p (ref))
7016 /* If the reference chain contains a component reference to a
7017 non-union type and there follows another field the reference
7018 is not at the end of a structure. */
7019 if (TREE_CODE (ref) == COMPONENT_REF
7020 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
7022 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
7023 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
7024 nextf = DECL_CHAIN (nextf);
7025 if (nextf)
7026 return false;
7029 ref = TREE_OPERAND (ref, 0);
7032 /* If the reference is based on a declared entity, the size of the array
7033 is constrained by its given domain. */
7034 if (DECL_P (ref))
7035 return false;
7037 return true;
7040 /* Return a tree representing the upper bound of the array mentioned in
7041 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
7043 tree
7044 array_ref_up_bound (tree exp)
7046 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
7048 /* If there is a domain type and it has an upper bound, use it, substituting
7049 for a PLACEHOLDER_EXPR as needed. */
7050 if (domain_type && TYPE_MAX_VALUE (domain_type))
7051 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
7053 /* Otherwise fail. */
7054 return NULL_TREE;
7057 /* Return a tree representing the offset, in bytes, of the field referenced
7058 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
7060 tree
7061 component_ref_field_offset (tree exp)
7063 tree aligned_offset = TREE_OPERAND (exp, 2);
7064 tree field = TREE_OPERAND (exp, 1);
7065 location_t loc = EXPR_LOCATION (exp);
7067 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
7068 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
7069 value. */
7070 if (aligned_offset)
7072 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7073 sizetype from another type of the same width and signedness. */
7074 if (TREE_TYPE (aligned_offset) != sizetype)
7075 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
7076 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
7077 size_int (DECL_OFFSET_ALIGN (field)
7078 / BITS_PER_UNIT));
7081 /* Otherwise, take the offset from that of the field. Substitute
7082 any PLACEHOLDER_EXPR that we have. */
7083 else
7084 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
7087 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7089 static unsigned HOST_WIDE_INT
7090 target_align (const_tree target)
7092 /* We might have a chain of nested references with intermediate misaligning
7093 bitfields components, so need to recurse to find out. */
7095 unsigned HOST_WIDE_INT this_align, outer_align;
7097 switch (TREE_CODE (target))
7099 case BIT_FIELD_REF:
7100 return 1;
7102 case COMPONENT_REF:
7103 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7104 outer_align = target_align (TREE_OPERAND (target, 0));
7105 return MIN (this_align, outer_align);
7107 case ARRAY_REF:
7108 case ARRAY_RANGE_REF:
7109 this_align = TYPE_ALIGN (TREE_TYPE (target));
7110 outer_align = target_align (TREE_OPERAND (target, 0));
7111 return MIN (this_align, outer_align);
7113 CASE_CONVERT:
7114 case NON_LVALUE_EXPR:
7115 case VIEW_CONVERT_EXPR:
7116 this_align = TYPE_ALIGN (TREE_TYPE (target));
7117 outer_align = target_align (TREE_OPERAND (target, 0));
7118 return MAX (this_align, outer_align);
7120 default:
7121 return TYPE_ALIGN (TREE_TYPE (target));
7126 /* Given an rtx VALUE that may contain additions and multiplications, return
7127 an equivalent value that just refers to a register, memory, or constant.
7128 This is done by generating instructions to perform the arithmetic and
7129 returning a pseudo-register containing the value.
7131 The returned value may be a REG, SUBREG, MEM or constant. */
7134 force_operand (rtx value, rtx target)
7136 rtx op1, op2;
7137 /* Use subtarget as the target for operand 0 of a binary operation. */
7138 rtx subtarget = get_subtarget (target);
7139 enum rtx_code code = GET_CODE (value);
7141 /* Check for subreg applied to an expression produced by loop optimizer. */
7142 if (code == SUBREG
7143 && !REG_P (SUBREG_REG (value))
7144 && !MEM_P (SUBREG_REG (value)))
7146 value
7147 = simplify_gen_subreg (GET_MODE (value),
7148 force_reg (GET_MODE (SUBREG_REG (value)),
7149 force_operand (SUBREG_REG (value),
7150 NULL_RTX)),
7151 GET_MODE (SUBREG_REG (value)),
7152 SUBREG_BYTE (value));
7153 code = GET_CODE (value);
7156 /* Check for a PIC address load. */
7157 if ((code == PLUS || code == MINUS)
7158 && XEXP (value, 0) == pic_offset_table_rtx
7159 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7160 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7161 || GET_CODE (XEXP (value, 1)) == CONST))
7163 if (!subtarget)
7164 subtarget = gen_reg_rtx (GET_MODE (value));
7165 emit_move_insn (subtarget, value);
7166 return subtarget;
7169 if (ARITHMETIC_P (value))
7171 op2 = XEXP (value, 1);
7172 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7173 subtarget = 0;
7174 if (code == MINUS && CONST_INT_P (op2))
7176 code = PLUS;
7177 op2 = negate_rtx (GET_MODE (value), op2);
7180 /* Check for an addition with OP2 a constant integer and our first
7181 operand a PLUS of a virtual register and something else. In that
7182 case, we want to emit the sum of the virtual register and the
7183 constant first and then add the other value. This allows virtual
7184 register instantiation to simply modify the constant rather than
7185 creating another one around this addition. */
7186 if (code == PLUS && CONST_INT_P (op2)
7187 && GET_CODE (XEXP (value, 0)) == PLUS
7188 && REG_P (XEXP (XEXP (value, 0), 0))
7189 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7190 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7192 rtx temp = expand_simple_binop (GET_MODE (value), code,
7193 XEXP (XEXP (value, 0), 0), op2,
7194 subtarget, 0, OPTAB_LIB_WIDEN);
7195 return expand_simple_binop (GET_MODE (value), code, temp,
7196 force_operand (XEXP (XEXP (value,
7197 0), 1), 0),
7198 target, 0, OPTAB_LIB_WIDEN);
7201 op1 = force_operand (XEXP (value, 0), subtarget);
7202 op2 = force_operand (op2, NULL_RTX);
7203 switch (code)
7205 case MULT:
7206 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7207 case DIV:
7208 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7209 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7210 target, 1, OPTAB_LIB_WIDEN);
7211 else
7212 return expand_divmod (0,
7213 FLOAT_MODE_P (GET_MODE (value))
7214 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7215 GET_MODE (value), op1, op2, target, 0);
7216 case MOD:
7217 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7218 target, 0);
7219 case UDIV:
7220 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7221 target, 1);
7222 case UMOD:
7223 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7224 target, 1);
7225 case ASHIFTRT:
7226 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7227 target, 0, OPTAB_LIB_WIDEN);
7228 default:
7229 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7230 target, 1, OPTAB_LIB_WIDEN);
7233 if (UNARY_P (value))
7235 if (!target)
7236 target = gen_reg_rtx (GET_MODE (value));
7237 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7238 switch (code)
7240 case ZERO_EXTEND:
7241 case SIGN_EXTEND:
7242 case TRUNCATE:
7243 case FLOAT_EXTEND:
7244 case FLOAT_TRUNCATE:
7245 convert_move (target, op1, code == ZERO_EXTEND);
7246 return target;
7248 case FIX:
7249 case UNSIGNED_FIX:
7250 expand_fix (target, op1, code == UNSIGNED_FIX);
7251 return target;
7253 case FLOAT:
7254 case UNSIGNED_FLOAT:
7255 expand_float (target, op1, code == UNSIGNED_FLOAT);
7256 return target;
7258 default:
7259 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7263 #ifdef INSN_SCHEDULING
7264 /* On machines that have insn scheduling, we want all memory reference to be
7265 explicit, so we need to deal with such paradoxical SUBREGs. */
7266 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7267 value
7268 = simplify_gen_subreg (GET_MODE (value),
7269 force_reg (GET_MODE (SUBREG_REG (value)),
7270 force_operand (SUBREG_REG (value),
7271 NULL_RTX)),
7272 GET_MODE (SUBREG_REG (value)),
7273 SUBREG_BYTE (value));
7274 #endif
7276 return value;
7279 /* Subroutine of expand_expr: return nonzero iff there is no way that
7280 EXP can reference X, which is being modified. TOP_P is nonzero if this
7281 call is going to be used to determine whether we need a temporary
7282 for EXP, as opposed to a recursive call to this function.
7284 It is always safe for this routine to return zero since it merely
7285 searches for optimization opportunities. */
7288 safe_from_p (const_rtx x, tree exp, int top_p)
7290 rtx exp_rtl = 0;
7291 int i, nops;
7293 if (x == 0
7294 /* If EXP has varying size, we MUST use a target since we currently
7295 have no way of allocating temporaries of variable size
7296 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7297 So we assume here that something at a higher level has prevented a
7298 clash. This is somewhat bogus, but the best we can do. Only
7299 do this when X is BLKmode and when we are at the top level. */
7300 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7301 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7302 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7303 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7304 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7305 != INTEGER_CST)
7306 && GET_MODE (x) == BLKmode)
7307 /* If X is in the outgoing argument area, it is always safe. */
7308 || (MEM_P (x)
7309 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7310 || (GET_CODE (XEXP (x, 0)) == PLUS
7311 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7312 return 1;
7314 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7315 find the underlying pseudo. */
7316 if (GET_CODE (x) == SUBREG)
7318 x = SUBREG_REG (x);
7319 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7320 return 0;
7323 /* Now look at our tree code and possibly recurse. */
7324 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7326 case tcc_declaration:
7327 exp_rtl = DECL_RTL_IF_SET (exp);
7328 break;
7330 case tcc_constant:
7331 return 1;
7333 case tcc_exceptional:
7334 if (TREE_CODE (exp) == TREE_LIST)
7336 while (1)
7338 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7339 return 0;
7340 exp = TREE_CHAIN (exp);
7341 if (!exp)
7342 return 1;
7343 if (TREE_CODE (exp) != TREE_LIST)
7344 return safe_from_p (x, exp, 0);
7347 else if (TREE_CODE (exp) == CONSTRUCTOR)
7349 constructor_elt *ce;
7350 unsigned HOST_WIDE_INT idx;
7352 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7353 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7354 || !safe_from_p (x, ce->value, 0))
7355 return 0;
7356 return 1;
7358 else if (TREE_CODE (exp) == ERROR_MARK)
7359 return 1; /* An already-visited SAVE_EXPR? */
7360 else
7361 return 0;
7363 case tcc_statement:
7364 /* The only case we look at here is the DECL_INITIAL inside a
7365 DECL_EXPR. */
7366 return (TREE_CODE (exp) != DECL_EXPR
7367 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7368 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7369 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7371 case tcc_binary:
7372 case tcc_comparison:
7373 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7374 return 0;
7375 /* Fall through. */
7377 case tcc_unary:
7378 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7380 case tcc_expression:
7381 case tcc_reference:
7382 case tcc_vl_exp:
7383 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7384 the expression. If it is set, we conflict iff we are that rtx or
7385 both are in memory. Otherwise, we check all operands of the
7386 expression recursively. */
7388 switch (TREE_CODE (exp))
7390 case ADDR_EXPR:
7391 /* If the operand is static or we are static, we can't conflict.
7392 Likewise if we don't conflict with the operand at all. */
7393 if (staticp (TREE_OPERAND (exp, 0))
7394 || TREE_STATIC (exp)
7395 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7396 return 1;
7398 /* Otherwise, the only way this can conflict is if we are taking
7399 the address of a DECL a that address if part of X, which is
7400 very rare. */
7401 exp = TREE_OPERAND (exp, 0);
7402 if (DECL_P (exp))
7404 if (!DECL_RTL_SET_P (exp)
7405 || !MEM_P (DECL_RTL (exp)))
7406 return 0;
7407 else
7408 exp_rtl = XEXP (DECL_RTL (exp), 0);
7410 break;
7412 case MEM_REF:
7413 if (MEM_P (x)
7414 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7415 get_alias_set (exp)))
7416 return 0;
7417 break;
7419 case CALL_EXPR:
7420 /* Assume that the call will clobber all hard registers and
7421 all of memory. */
7422 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7423 || MEM_P (x))
7424 return 0;
7425 break;
7427 case WITH_CLEANUP_EXPR:
7428 case CLEANUP_POINT_EXPR:
7429 /* Lowered by gimplify.c. */
7430 gcc_unreachable ();
7432 case SAVE_EXPR:
7433 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7435 default:
7436 break;
7439 /* If we have an rtx, we do not need to scan our operands. */
7440 if (exp_rtl)
7441 break;
7443 nops = TREE_OPERAND_LENGTH (exp);
7444 for (i = 0; i < nops; i++)
7445 if (TREE_OPERAND (exp, i) != 0
7446 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7447 return 0;
7449 break;
7451 case tcc_type:
7452 /* Should never get a type here. */
7453 gcc_unreachable ();
7456 /* If we have an rtl, find any enclosed object. Then see if we conflict
7457 with it. */
7458 if (exp_rtl)
7460 if (GET_CODE (exp_rtl) == SUBREG)
7462 exp_rtl = SUBREG_REG (exp_rtl);
7463 if (REG_P (exp_rtl)
7464 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7465 return 0;
7468 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7469 are memory and they conflict. */
7470 return ! (rtx_equal_p (x, exp_rtl)
7471 || (MEM_P (x) && MEM_P (exp_rtl)
7472 && true_dependence (exp_rtl, VOIDmode, x)));
7475 /* If we reach here, it is safe. */
7476 return 1;
7480 /* Return the highest power of two that EXP is known to be a multiple of.
7481 This is used in updating alignment of MEMs in array references. */
7483 unsigned HOST_WIDE_INT
7484 highest_pow2_factor (const_tree exp)
7486 unsigned HOST_WIDE_INT ret;
7487 int trailing_zeros = tree_ctz (exp);
7488 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7489 return BIGGEST_ALIGNMENT;
7490 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7491 if (ret > BIGGEST_ALIGNMENT)
7492 return BIGGEST_ALIGNMENT;
7493 return ret;
7496 /* Similar, except that the alignment requirements of TARGET are
7497 taken into account. Assume it is at least as aligned as its
7498 type, unless it is a COMPONENT_REF in which case the layout of
7499 the structure gives the alignment. */
7501 static unsigned HOST_WIDE_INT
7502 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7504 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7505 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7507 return MAX (factor, talign);
7510 #ifdef HAVE_conditional_move
7511 /* Convert the tree comparison code TCODE to the rtl one where the
7512 signedness is UNSIGNEDP. */
7514 static enum rtx_code
7515 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7517 enum rtx_code code;
7518 switch (tcode)
7520 case EQ_EXPR:
7521 code = EQ;
7522 break;
7523 case NE_EXPR:
7524 code = NE;
7525 break;
7526 case LT_EXPR:
7527 code = unsignedp ? LTU : LT;
7528 break;
7529 case LE_EXPR:
7530 code = unsignedp ? LEU : LE;
7531 break;
7532 case GT_EXPR:
7533 code = unsignedp ? GTU : GT;
7534 break;
7535 case GE_EXPR:
7536 code = unsignedp ? GEU : GE;
7537 break;
7538 case UNORDERED_EXPR:
7539 code = UNORDERED;
7540 break;
7541 case ORDERED_EXPR:
7542 code = ORDERED;
7543 break;
7544 case UNLT_EXPR:
7545 code = UNLT;
7546 break;
7547 case UNLE_EXPR:
7548 code = UNLE;
7549 break;
7550 case UNGT_EXPR:
7551 code = UNGT;
7552 break;
7553 case UNGE_EXPR:
7554 code = UNGE;
7555 break;
7556 case UNEQ_EXPR:
7557 code = UNEQ;
7558 break;
7559 case LTGT_EXPR:
7560 code = LTGT;
7561 break;
7563 default:
7564 gcc_unreachable ();
7566 return code;
7568 #endif
7570 /* Subroutine of expand_expr. Expand the two operands of a binary
7571 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7572 The value may be stored in TARGET if TARGET is nonzero. The
7573 MODIFIER argument is as documented by expand_expr. */
7575 void
7576 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7577 enum expand_modifier modifier)
7579 if (! safe_from_p (target, exp1, 1))
7580 target = 0;
7581 if (operand_equal_p (exp0, exp1, 0))
7583 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7584 *op1 = copy_rtx (*op0);
7586 else
7588 /* If we need to preserve evaluation order, copy exp0 into its own
7589 temporary variable so that it can't be clobbered by exp1. */
7590 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7591 exp0 = save_expr (exp0);
7592 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7593 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7598 /* Return a MEM that contains constant EXP. DEFER is as for
7599 output_constant_def and MODIFIER is as for expand_expr. */
7601 static rtx
7602 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7604 rtx mem;
7606 mem = output_constant_def (exp, defer);
7607 if (modifier != EXPAND_INITIALIZER)
7608 mem = use_anchored_address (mem);
7609 return mem;
7612 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7613 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7615 static rtx
7616 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7617 enum expand_modifier modifier, addr_space_t as)
7619 rtx result, subtarget;
7620 tree inner, offset;
7621 HOST_WIDE_INT bitsize, bitpos;
7622 int volatilep, unsignedp;
7623 machine_mode mode1;
7625 /* If we are taking the address of a constant and are at the top level,
7626 we have to use output_constant_def since we can't call force_const_mem
7627 at top level. */
7628 /* ??? This should be considered a front-end bug. We should not be
7629 generating ADDR_EXPR of something that isn't an LVALUE. The only
7630 exception here is STRING_CST. */
7631 if (CONSTANT_CLASS_P (exp))
7633 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7634 if (modifier < EXPAND_SUM)
7635 result = force_operand (result, target);
7636 return result;
7639 /* Everything must be something allowed by is_gimple_addressable. */
7640 switch (TREE_CODE (exp))
7642 case INDIRECT_REF:
7643 /* This case will happen via recursion for &a->b. */
7644 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7646 case MEM_REF:
7648 tree tem = TREE_OPERAND (exp, 0);
7649 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7650 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7651 return expand_expr (tem, target, tmode, modifier);
7654 case CONST_DECL:
7655 /* Expand the initializer like constants above. */
7656 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7657 0, modifier), 0);
7658 if (modifier < EXPAND_SUM)
7659 result = force_operand (result, target);
7660 return result;
7662 case REALPART_EXPR:
7663 /* The real part of the complex number is always first, therefore
7664 the address is the same as the address of the parent object. */
7665 offset = 0;
7666 bitpos = 0;
7667 inner = TREE_OPERAND (exp, 0);
7668 break;
7670 case IMAGPART_EXPR:
7671 /* The imaginary part of the complex number is always second.
7672 The expression is therefore always offset by the size of the
7673 scalar type. */
7674 offset = 0;
7675 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7676 inner = TREE_OPERAND (exp, 0);
7677 break;
7679 case COMPOUND_LITERAL_EXPR:
7680 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7681 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7682 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7683 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7684 the initializers aren't gimplified. */
7685 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7686 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7687 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7688 target, tmode, modifier, as);
7689 /* FALLTHRU */
7690 default:
7691 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7692 expand_expr, as that can have various side effects; LABEL_DECLs for
7693 example, may not have their DECL_RTL set yet. Expand the rtl of
7694 CONSTRUCTORs too, which should yield a memory reference for the
7695 constructor's contents. Assume language specific tree nodes can
7696 be expanded in some interesting way. */
7697 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7698 if (DECL_P (exp)
7699 || TREE_CODE (exp) == CONSTRUCTOR
7700 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7702 result = expand_expr (exp, target, tmode,
7703 modifier == EXPAND_INITIALIZER
7704 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7706 /* If the DECL isn't in memory, then the DECL wasn't properly
7707 marked TREE_ADDRESSABLE, which will be either a front-end
7708 or a tree optimizer bug. */
7710 if (TREE_ADDRESSABLE (exp)
7711 && ! MEM_P (result)
7712 && ! targetm.calls.allocate_stack_slots_for_args ())
7714 error ("local frame unavailable (naked function?)");
7715 return result;
7717 else
7718 gcc_assert (MEM_P (result));
7719 result = XEXP (result, 0);
7721 /* ??? Is this needed anymore? */
7722 if (DECL_P (exp))
7723 TREE_USED (exp) = 1;
7725 if (modifier != EXPAND_INITIALIZER
7726 && modifier != EXPAND_CONST_ADDRESS
7727 && modifier != EXPAND_SUM)
7728 result = force_operand (result, target);
7729 return result;
7732 /* Pass FALSE as the last argument to get_inner_reference although
7733 we are expanding to RTL. The rationale is that we know how to
7734 handle "aligning nodes" here: we can just bypass them because
7735 they won't change the final object whose address will be returned
7736 (they actually exist only for that purpose). */
7737 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7738 &mode1, &unsignedp, &volatilep, false);
7739 break;
7742 /* We must have made progress. */
7743 gcc_assert (inner != exp);
7745 subtarget = offset || bitpos ? NULL_RTX : target;
7746 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7747 inner alignment, force the inner to be sufficiently aligned. */
7748 if (CONSTANT_CLASS_P (inner)
7749 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7751 inner = copy_node (inner);
7752 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7753 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7754 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7756 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7758 if (offset)
7760 rtx tmp;
7762 if (modifier != EXPAND_NORMAL)
7763 result = force_operand (result, NULL);
7764 tmp = expand_expr (offset, NULL_RTX, tmode,
7765 modifier == EXPAND_INITIALIZER
7766 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7768 /* expand_expr is allowed to return an object in a mode other
7769 than TMODE. If it did, we need to convert. */
7770 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7771 tmp = convert_modes (tmode, GET_MODE (tmp),
7772 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7773 result = convert_memory_address_addr_space (tmode, result, as);
7774 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7776 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7777 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7778 else
7780 subtarget = bitpos ? NULL_RTX : target;
7781 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7782 1, OPTAB_LIB_WIDEN);
7786 if (bitpos)
7788 /* Someone beforehand should have rejected taking the address
7789 of such an object. */
7790 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7792 result = convert_memory_address_addr_space (tmode, result, as);
7793 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7794 if (modifier < EXPAND_SUM)
7795 result = force_operand (result, target);
7798 return result;
7801 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7802 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7804 static rtx
7805 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7806 enum expand_modifier modifier)
7808 addr_space_t as = ADDR_SPACE_GENERIC;
7809 machine_mode address_mode = Pmode;
7810 machine_mode pointer_mode = ptr_mode;
7811 machine_mode rmode;
7812 rtx result;
7814 /* Target mode of VOIDmode says "whatever's natural". */
7815 if (tmode == VOIDmode)
7816 tmode = TYPE_MODE (TREE_TYPE (exp));
7818 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7820 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7821 address_mode = targetm.addr_space.address_mode (as);
7822 pointer_mode = targetm.addr_space.pointer_mode (as);
7825 /* We can get called with some Weird Things if the user does silliness
7826 like "(short) &a". In that case, convert_memory_address won't do
7827 the right thing, so ignore the given target mode. */
7828 if (tmode != address_mode && tmode != pointer_mode)
7829 tmode = address_mode;
7831 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7832 tmode, modifier, as);
7834 /* Despite expand_expr claims concerning ignoring TMODE when not
7835 strictly convenient, stuff breaks if we don't honor it. Note
7836 that combined with the above, we only do this for pointer modes. */
7837 rmode = GET_MODE (result);
7838 if (rmode == VOIDmode)
7839 rmode = tmode;
7840 if (rmode != tmode)
7841 result = convert_memory_address_addr_space (tmode, result, as);
7843 return result;
7846 /* Generate code for computing CONSTRUCTOR EXP.
7847 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7848 is TRUE, instead of creating a temporary variable in memory
7849 NULL is returned and the caller needs to handle it differently. */
7851 static rtx
7852 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7853 bool avoid_temp_mem)
7855 tree type = TREE_TYPE (exp);
7856 machine_mode mode = TYPE_MODE (type);
7858 /* Try to avoid creating a temporary at all. This is possible
7859 if all of the initializer is zero.
7860 FIXME: try to handle all [0..255] initializers we can handle
7861 with memset. */
7862 if (TREE_STATIC (exp)
7863 && !TREE_ADDRESSABLE (exp)
7864 && target != 0 && mode == BLKmode
7865 && all_zeros_p (exp))
7867 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7868 return target;
7871 /* All elts simple constants => refer to a constant in memory. But
7872 if this is a non-BLKmode mode, let it store a field at a time
7873 since that should make a CONST_INT, CONST_WIDE_INT or
7874 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7875 use, it is best to store directly into the target unless the type
7876 is large enough that memcpy will be used. If we are making an
7877 initializer and all operands are constant, put it in memory as
7878 well.
7880 FIXME: Avoid trying to fill vector constructors piece-meal.
7881 Output them with output_constant_def below unless we're sure
7882 they're zeros. This should go away when vector initializers
7883 are treated like VECTOR_CST instead of arrays. */
7884 if ((TREE_STATIC (exp)
7885 && ((mode == BLKmode
7886 && ! (target != 0 && safe_from_p (target, exp, 1)))
7887 || TREE_ADDRESSABLE (exp)
7888 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7889 && (! can_move_by_pieces
7890 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7891 TYPE_ALIGN (type)))
7892 && ! mostly_zeros_p (exp))))
7893 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7894 && TREE_CONSTANT (exp)))
7896 rtx constructor;
7898 if (avoid_temp_mem)
7899 return NULL_RTX;
7901 constructor = expand_expr_constant (exp, 1, modifier);
7903 if (modifier != EXPAND_CONST_ADDRESS
7904 && modifier != EXPAND_INITIALIZER
7905 && modifier != EXPAND_SUM)
7906 constructor = validize_mem (constructor);
7908 return constructor;
7911 /* Handle calls that pass values in multiple non-contiguous
7912 locations. The Irix 6 ABI has examples of this. */
7913 if (target == 0 || ! safe_from_p (target, exp, 1)
7914 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7916 if (avoid_temp_mem)
7917 return NULL_RTX;
7919 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7922 store_constructor (exp, target, 0, int_expr_size (exp));
7923 return target;
7927 /* expand_expr: generate code for computing expression EXP.
7928 An rtx for the computed value is returned. The value is never null.
7929 In the case of a void EXP, const0_rtx is returned.
7931 The value may be stored in TARGET if TARGET is nonzero.
7932 TARGET is just a suggestion; callers must assume that
7933 the rtx returned may not be the same as TARGET.
7935 If TARGET is CONST0_RTX, it means that the value will be ignored.
7937 If TMODE is not VOIDmode, it suggests generating the
7938 result in mode TMODE. But this is done only when convenient.
7939 Otherwise, TMODE is ignored and the value generated in its natural mode.
7940 TMODE is just a suggestion; callers must assume that
7941 the rtx returned may not have mode TMODE.
7943 Note that TARGET may have neither TMODE nor MODE. In that case, it
7944 probably will not be used.
7946 If MODIFIER is EXPAND_SUM then when EXP is an addition
7947 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7948 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7949 products as above, or REG or MEM, or constant.
7950 Ordinarily in such cases we would output mul or add instructions
7951 and then return a pseudo reg containing the sum.
7953 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7954 it also marks a label as absolutely required (it can't be dead).
7955 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7956 This is used for outputting expressions used in initializers.
7958 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7959 with a constant address even if that address is not normally legitimate.
7960 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7962 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7963 a call parameter. Such targets require special care as we haven't yet
7964 marked TARGET so that it's safe from being trashed by libcalls. We
7965 don't want to use TARGET for anything but the final result;
7966 Intermediate values must go elsewhere. Additionally, calls to
7967 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7969 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7970 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7971 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7972 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7973 recursively.
7975 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7976 In this case, we don't adjust a returned MEM rtx that wouldn't be
7977 sufficiently aligned for its mode; instead, it's up to the caller
7978 to deal with it afterwards. This is used to make sure that unaligned
7979 base objects for which out-of-bounds accesses are supported, for
7980 example record types with trailing arrays, aren't realigned behind
7981 the back of the caller.
7982 The normal operating mode is to pass FALSE for this parameter. */
7985 expand_expr_real (tree exp, rtx target, machine_mode tmode,
7986 enum expand_modifier modifier, rtx *alt_rtl,
7987 bool inner_reference_p)
7989 rtx ret;
7991 /* Handle ERROR_MARK before anybody tries to access its type. */
7992 if (TREE_CODE (exp) == ERROR_MARK
7993 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7995 ret = CONST0_RTX (tmode);
7996 return ret ? ret : const0_rtx;
7999 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8000 inner_reference_p);
8001 return ret;
8004 /* Try to expand the conditional expression which is represented by
8005 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
8006 return the rtl reg which repsents the result. Otherwise return
8007 NULL_RTL. */
8009 static rtx
8010 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8011 tree treeop1 ATTRIBUTE_UNUSED,
8012 tree treeop2 ATTRIBUTE_UNUSED)
8014 #ifdef HAVE_conditional_move
8015 rtx insn;
8016 rtx op00, op01, op1, op2;
8017 enum rtx_code comparison_code;
8018 machine_mode comparison_mode;
8019 gimple srcstmt;
8020 rtx temp;
8021 tree type = TREE_TYPE (treeop1);
8022 int unsignedp = TYPE_UNSIGNED (type);
8023 machine_mode mode = TYPE_MODE (type);
8024 machine_mode orig_mode = mode;
8026 /* If we cannot do a conditional move on the mode, try doing it
8027 with the promoted mode. */
8028 if (!can_conditionally_move_p (mode))
8030 mode = promote_mode (type, mode, &unsignedp);
8031 if (!can_conditionally_move_p (mode))
8032 return NULL_RTX;
8033 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8035 else
8036 temp = assign_temp (type, 0, 1);
8038 start_sequence ();
8039 expand_operands (treeop1, treeop2,
8040 temp, &op1, &op2, EXPAND_NORMAL);
8042 if (TREE_CODE (treeop0) == SSA_NAME
8043 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8045 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8046 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8047 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8048 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8049 comparison_mode = TYPE_MODE (type);
8050 unsignedp = TYPE_UNSIGNED (type);
8051 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8053 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
8055 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8056 enum tree_code cmpcode = TREE_CODE (treeop0);
8057 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8058 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8059 unsignedp = TYPE_UNSIGNED (type);
8060 comparison_mode = TYPE_MODE (type);
8061 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8063 else
8065 op00 = expand_normal (treeop0);
8066 op01 = const0_rtx;
8067 comparison_code = NE;
8068 comparison_mode = GET_MODE (op00);
8069 if (comparison_mode == VOIDmode)
8070 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8073 if (GET_MODE (op1) != mode)
8074 op1 = gen_lowpart (mode, op1);
8076 if (GET_MODE (op2) != mode)
8077 op2 = gen_lowpart (mode, op2);
8079 /* Try to emit the conditional move. */
8080 insn = emit_conditional_move (temp, comparison_code,
8081 op00, op01, comparison_mode,
8082 op1, op2, mode,
8083 unsignedp);
8085 /* If we could do the conditional move, emit the sequence,
8086 and return. */
8087 if (insn)
8089 rtx_insn *seq = get_insns ();
8090 end_sequence ();
8091 emit_insn (seq);
8092 return convert_modes (orig_mode, mode, temp, 0);
8095 /* Otherwise discard the sequence and fall back to code with
8096 branches. */
8097 end_sequence ();
8098 #endif
8099 return NULL_RTX;
8103 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8104 enum expand_modifier modifier)
8106 rtx op0, op1, op2, temp;
8107 tree type;
8108 int unsignedp;
8109 machine_mode mode;
8110 enum tree_code code = ops->code;
8111 optab this_optab;
8112 rtx subtarget, original_target;
8113 int ignore;
8114 bool reduce_bit_field;
8115 location_t loc = ops->location;
8116 tree treeop0, treeop1, treeop2;
8117 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8118 ? reduce_to_bit_field_precision ((expr), \
8119 target, \
8120 type) \
8121 : (expr))
8123 type = ops->type;
8124 mode = TYPE_MODE (type);
8125 unsignedp = TYPE_UNSIGNED (type);
8127 treeop0 = ops->op0;
8128 treeop1 = ops->op1;
8129 treeop2 = ops->op2;
8131 /* We should be called only on simple (binary or unary) expressions,
8132 exactly those that are valid in gimple expressions that aren't
8133 GIMPLE_SINGLE_RHS (or invalid). */
8134 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8135 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8136 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8138 ignore = (target == const0_rtx
8139 || ((CONVERT_EXPR_CODE_P (code)
8140 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8141 && TREE_CODE (type) == VOID_TYPE));
8143 /* We should be called only if we need the result. */
8144 gcc_assert (!ignore);
8146 /* An operation in what may be a bit-field type needs the
8147 result to be reduced to the precision of the bit-field type,
8148 which is narrower than that of the type's mode. */
8149 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8150 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8152 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8153 target = 0;
8155 /* Use subtarget as the target for operand 0 of a binary operation. */
8156 subtarget = get_subtarget (target);
8157 original_target = target;
8159 switch (code)
8161 case NON_LVALUE_EXPR:
8162 case PAREN_EXPR:
8163 CASE_CONVERT:
8164 if (treeop0 == error_mark_node)
8165 return const0_rtx;
8167 if (TREE_CODE (type) == UNION_TYPE)
8169 tree valtype = TREE_TYPE (treeop0);
8171 /* If both input and output are BLKmode, this conversion isn't doing
8172 anything except possibly changing memory attribute. */
8173 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8175 rtx result = expand_expr (treeop0, target, tmode,
8176 modifier);
8178 result = copy_rtx (result);
8179 set_mem_attributes (result, type, 0);
8180 return result;
8183 if (target == 0)
8185 if (TYPE_MODE (type) != BLKmode)
8186 target = gen_reg_rtx (TYPE_MODE (type));
8187 else
8188 target = assign_temp (type, 1, 1);
8191 if (MEM_P (target))
8192 /* Store data into beginning of memory target. */
8193 store_expr (treeop0,
8194 adjust_address (target, TYPE_MODE (valtype), 0),
8195 modifier == EXPAND_STACK_PARM,
8196 false);
8198 else
8200 gcc_assert (REG_P (target));
8202 /* Store this field into a union of the proper type. */
8203 store_field (target,
8204 MIN ((int_size_in_bytes (TREE_TYPE
8205 (treeop0))
8206 * BITS_PER_UNIT),
8207 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8208 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8211 /* Return the entire union. */
8212 return target;
8215 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8217 op0 = expand_expr (treeop0, target, VOIDmode,
8218 modifier);
8220 /* If the signedness of the conversion differs and OP0 is
8221 a promoted SUBREG, clear that indication since we now
8222 have to do the proper extension. */
8223 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8224 && GET_CODE (op0) == SUBREG)
8225 SUBREG_PROMOTED_VAR_P (op0) = 0;
8227 return REDUCE_BIT_FIELD (op0);
8230 op0 = expand_expr (treeop0, NULL_RTX, mode,
8231 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8232 if (GET_MODE (op0) == mode)
8235 /* If OP0 is a constant, just convert it into the proper mode. */
8236 else if (CONSTANT_P (op0))
8238 tree inner_type = TREE_TYPE (treeop0);
8239 machine_mode inner_mode = GET_MODE (op0);
8241 if (inner_mode == VOIDmode)
8242 inner_mode = TYPE_MODE (inner_type);
8244 if (modifier == EXPAND_INITIALIZER)
8245 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8246 subreg_lowpart_offset (mode,
8247 inner_mode));
8248 else
8249 op0= convert_modes (mode, inner_mode, op0,
8250 TYPE_UNSIGNED (inner_type));
8253 else if (modifier == EXPAND_INITIALIZER)
8254 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8256 else if (target == 0)
8257 op0 = convert_to_mode (mode, op0,
8258 TYPE_UNSIGNED (TREE_TYPE
8259 (treeop0)));
8260 else
8262 convert_move (target, op0,
8263 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8264 op0 = target;
8267 return REDUCE_BIT_FIELD (op0);
8269 case ADDR_SPACE_CONVERT_EXPR:
8271 tree treeop0_type = TREE_TYPE (treeop0);
8272 addr_space_t as_to;
8273 addr_space_t as_from;
8275 gcc_assert (POINTER_TYPE_P (type));
8276 gcc_assert (POINTER_TYPE_P (treeop0_type));
8278 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8279 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8281 /* Conversions between pointers to the same address space should
8282 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8283 gcc_assert (as_to != as_from);
8285 /* Ask target code to handle conversion between pointers
8286 to overlapping address spaces. */
8287 if (targetm.addr_space.subset_p (as_to, as_from)
8288 || targetm.addr_space.subset_p (as_from, as_to))
8290 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8291 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8292 gcc_assert (op0);
8293 return op0;
8296 /* For disjoint address spaces, converting anything but
8297 a null pointer invokes undefined behaviour. We simply
8298 always return a null pointer here. */
8299 return CONST0_RTX (mode);
8302 case POINTER_PLUS_EXPR:
8303 /* Even though the sizetype mode and the pointer's mode can be different
8304 expand is able to handle this correctly and get the correct result out
8305 of the PLUS_EXPR code. */
8306 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8307 if sizetype precision is smaller than pointer precision. */
8308 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8309 treeop1 = fold_convert_loc (loc, type,
8310 fold_convert_loc (loc, ssizetype,
8311 treeop1));
8312 /* If sizetype precision is larger than pointer precision, truncate the
8313 offset to have matching modes. */
8314 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8315 treeop1 = fold_convert_loc (loc, type, treeop1);
8317 case PLUS_EXPR:
8318 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8319 something else, make sure we add the register to the constant and
8320 then to the other thing. This case can occur during strength
8321 reduction and doing it this way will produce better code if the
8322 frame pointer or argument pointer is eliminated.
8324 fold-const.c will ensure that the constant is always in the inner
8325 PLUS_EXPR, so the only case we need to do anything about is if
8326 sp, ap, or fp is our second argument, in which case we must swap
8327 the innermost first argument and our second argument. */
8329 if (TREE_CODE (treeop0) == PLUS_EXPR
8330 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8331 && TREE_CODE (treeop1) == VAR_DECL
8332 && (DECL_RTL (treeop1) == frame_pointer_rtx
8333 || DECL_RTL (treeop1) == stack_pointer_rtx
8334 || DECL_RTL (treeop1) == arg_pointer_rtx))
8336 gcc_unreachable ();
8339 /* If the result is to be ptr_mode and we are adding an integer to
8340 something, we might be forming a constant. So try to use
8341 plus_constant. If it produces a sum and we can't accept it,
8342 use force_operand. This allows P = &ARR[const] to generate
8343 efficient code on machines where a SYMBOL_REF is not a valid
8344 address.
8346 If this is an EXPAND_SUM call, always return the sum. */
8347 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8348 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8350 if (modifier == EXPAND_STACK_PARM)
8351 target = 0;
8352 if (TREE_CODE (treeop0) == INTEGER_CST
8353 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8354 && TREE_CONSTANT (treeop1))
8356 rtx constant_part;
8357 HOST_WIDE_INT wc;
8358 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8360 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8361 EXPAND_SUM);
8362 /* Use wi::shwi to ensure that the constant is
8363 truncated according to the mode of OP1, then sign extended
8364 to a HOST_WIDE_INT. Using the constant directly can result
8365 in non-canonical RTL in a 64x32 cross compile. */
8366 wc = TREE_INT_CST_LOW (treeop0);
8367 constant_part =
8368 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8369 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8370 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8371 op1 = force_operand (op1, target);
8372 return REDUCE_BIT_FIELD (op1);
8375 else if (TREE_CODE (treeop1) == INTEGER_CST
8376 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8377 && TREE_CONSTANT (treeop0))
8379 rtx constant_part;
8380 HOST_WIDE_INT wc;
8381 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8383 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8384 (modifier == EXPAND_INITIALIZER
8385 ? EXPAND_INITIALIZER : EXPAND_SUM));
8386 if (! CONSTANT_P (op0))
8388 op1 = expand_expr (treeop1, NULL_RTX,
8389 VOIDmode, modifier);
8390 /* Return a PLUS if modifier says it's OK. */
8391 if (modifier == EXPAND_SUM
8392 || modifier == EXPAND_INITIALIZER)
8393 return simplify_gen_binary (PLUS, mode, op0, op1);
8394 goto binop2;
8396 /* Use wi::shwi to ensure that the constant is
8397 truncated according to the mode of OP1, then sign extended
8398 to a HOST_WIDE_INT. Using the constant directly can result
8399 in non-canonical RTL in a 64x32 cross compile. */
8400 wc = TREE_INT_CST_LOW (treeop1);
8401 constant_part
8402 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8403 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8404 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8405 op0 = force_operand (op0, target);
8406 return REDUCE_BIT_FIELD (op0);
8410 /* Use TER to expand pointer addition of a negated value
8411 as pointer subtraction. */
8412 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8413 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8414 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8415 && TREE_CODE (treeop1) == SSA_NAME
8416 && TYPE_MODE (TREE_TYPE (treeop0))
8417 == TYPE_MODE (TREE_TYPE (treeop1)))
8419 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8420 if (def)
8422 treeop1 = gimple_assign_rhs1 (def);
8423 code = MINUS_EXPR;
8424 goto do_minus;
8428 /* No sense saving up arithmetic to be done
8429 if it's all in the wrong mode to form part of an address.
8430 And force_operand won't know whether to sign-extend or
8431 zero-extend. */
8432 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8433 || mode != ptr_mode)
8435 expand_operands (treeop0, treeop1,
8436 subtarget, &op0, &op1, EXPAND_NORMAL);
8437 if (op0 == const0_rtx)
8438 return op1;
8439 if (op1 == const0_rtx)
8440 return op0;
8441 goto binop2;
8444 expand_operands (treeop0, treeop1,
8445 subtarget, &op0, &op1, modifier);
8446 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8448 case MINUS_EXPR:
8449 do_minus:
8450 /* For initializers, we are allowed to return a MINUS of two
8451 symbolic constants. Here we handle all cases when both operands
8452 are constant. */
8453 /* Handle difference of two symbolic constants,
8454 for the sake of an initializer. */
8455 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8456 && really_constant_p (treeop0)
8457 && really_constant_p (treeop1))
8459 expand_operands (treeop0, treeop1,
8460 NULL_RTX, &op0, &op1, modifier);
8462 /* If the last operand is a CONST_INT, use plus_constant of
8463 the negated constant. Else make the MINUS. */
8464 if (CONST_INT_P (op1))
8465 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8466 -INTVAL (op1)));
8467 else
8468 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8471 /* No sense saving up arithmetic to be done
8472 if it's all in the wrong mode to form part of an address.
8473 And force_operand won't know whether to sign-extend or
8474 zero-extend. */
8475 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8476 || mode != ptr_mode)
8477 goto binop;
8479 expand_operands (treeop0, treeop1,
8480 subtarget, &op0, &op1, modifier);
8482 /* Convert A - const to A + (-const). */
8483 if (CONST_INT_P (op1))
8485 op1 = negate_rtx (mode, op1);
8486 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8489 goto binop2;
8491 case WIDEN_MULT_PLUS_EXPR:
8492 case WIDEN_MULT_MINUS_EXPR:
8493 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8494 op2 = expand_normal (treeop2);
8495 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8496 target, unsignedp);
8497 return target;
8499 case WIDEN_MULT_EXPR:
8500 /* If first operand is constant, swap them.
8501 Thus the following special case checks need only
8502 check the second operand. */
8503 if (TREE_CODE (treeop0) == INTEGER_CST)
8505 tree t1 = treeop0;
8506 treeop0 = treeop1;
8507 treeop1 = t1;
8510 /* First, check if we have a multiplication of one signed and one
8511 unsigned operand. */
8512 if (TREE_CODE (treeop1) != INTEGER_CST
8513 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8514 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8516 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8517 this_optab = usmul_widen_optab;
8518 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8519 != CODE_FOR_nothing)
8521 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8522 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8523 EXPAND_NORMAL);
8524 else
8525 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8526 EXPAND_NORMAL);
8527 /* op0 and op1 might still be constant, despite the above
8528 != INTEGER_CST check. Handle it. */
8529 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8531 op0 = convert_modes (innermode, mode, op0, true);
8532 op1 = convert_modes (innermode, mode, op1, false);
8533 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8534 target, unsignedp));
8536 goto binop3;
8539 /* Check for a multiplication with matching signedness. */
8540 else if ((TREE_CODE (treeop1) == INTEGER_CST
8541 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8542 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8543 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8545 tree op0type = TREE_TYPE (treeop0);
8546 machine_mode innermode = TYPE_MODE (op0type);
8547 bool zextend_p = TYPE_UNSIGNED (op0type);
8548 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8549 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8551 if (TREE_CODE (treeop0) != INTEGER_CST)
8553 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8554 != CODE_FOR_nothing)
8556 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8557 EXPAND_NORMAL);
8558 /* op0 and op1 might still be constant, despite the above
8559 != INTEGER_CST check. Handle it. */
8560 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8562 widen_mult_const:
8563 op0 = convert_modes (innermode, mode, op0, zextend_p);
8565 = convert_modes (innermode, mode, op1,
8566 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8567 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8568 target,
8569 unsignedp));
8571 temp = expand_widening_mult (mode, op0, op1, target,
8572 unsignedp, this_optab);
8573 return REDUCE_BIT_FIELD (temp);
8575 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8576 != CODE_FOR_nothing
8577 && innermode == word_mode)
8579 rtx htem, hipart;
8580 op0 = expand_normal (treeop0);
8581 if (TREE_CODE (treeop1) == INTEGER_CST)
8582 op1 = convert_modes (innermode, mode,
8583 expand_normal (treeop1),
8584 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8585 else
8586 op1 = expand_normal (treeop1);
8587 /* op0 and op1 might still be constant, despite the above
8588 != INTEGER_CST check. Handle it. */
8589 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8590 goto widen_mult_const;
8591 temp = expand_binop (mode, other_optab, op0, op1, target,
8592 unsignedp, OPTAB_LIB_WIDEN);
8593 hipart = gen_highpart (innermode, temp);
8594 htem = expand_mult_highpart_adjust (innermode, hipart,
8595 op0, op1, hipart,
8596 zextend_p);
8597 if (htem != hipart)
8598 emit_move_insn (hipart, htem);
8599 return REDUCE_BIT_FIELD (temp);
8603 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8604 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8605 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8606 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8608 case FMA_EXPR:
8610 optab opt = fma_optab;
8611 gimple def0, def2;
8613 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8614 call. */
8615 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8617 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8618 tree call_expr;
8620 gcc_assert (fn != NULL_TREE);
8621 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8622 return expand_builtin (call_expr, target, subtarget, mode, false);
8625 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8626 /* The multiplication is commutative - look at its 2nd operand
8627 if the first isn't fed by a negate. */
8628 if (!def0)
8630 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8631 /* Swap operands if the 2nd operand is fed by a negate. */
8632 if (def0)
8634 tree tem = treeop0;
8635 treeop0 = treeop1;
8636 treeop1 = tem;
8639 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8641 op0 = op2 = NULL;
8643 if (def0 && def2
8644 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8646 opt = fnms_optab;
8647 op0 = expand_normal (gimple_assign_rhs1 (def0));
8648 op2 = expand_normal (gimple_assign_rhs1 (def2));
8650 else if (def0
8651 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8653 opt = fnma_optab;
8654 op0 = expand_normal (gimple_assign_rhs1 (def0));
8656 else if (def2
8657 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8659 opt = fms_optab;
8660 op2 = expand_normal (gimple_assign_rhs1 (def2));
8663 if (op0 == NULL)
8664 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8665 if (op2 == NULL)
8666 op2 = expand_normal (treeop2);
8667 op1 = expand_normal (treeop1);
8669 return expand_ternary_op (TYPE_MODE (type), opt,
8670 op0, op1, op2, target, 0);
8673 case MULT_EXPR:
8674 /* If this is a fixed-point operation, then we cannot use the code
8675 below because "expand_mult" doesn't support sat/no-sat fixed-point
8676 multiplications. */
8677 if (ALL_FIXED_POINT_MODE_P (mode))
8678 goto binop;
8680 /* If first operand is constant, swap them.
8681 Thus the following special case checks need only
8682 check the second operand. */
8683 if (TREE_CODE (treeop0) == INTEGER_CST)
8685 tree t1 = treeop0;
8686 treeop0 = treeop1;
8687 treeop1 = t1;
8690 /* Attempt to return something suitable for generating an
8691 indexed address, for machines that support that. */
8693 if (modifier == EXPAND_SUM && mode == ptr_mode
8694 && tree_fits_shwi_p (treeop1))
8696 tree exp1 = treeop1;
8698 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8699 EXPAND_SUM);
8701 if (!REG_P (op0))
8702 op0 = force_operand (op0, NULL_RTX);
8703 if (!REG_P (op0))
8704 op0 = copy_to_mode_reg (mode, op0);
8706 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8707 gen_int_mode (tree_to_shwi (exp1),
8708 TYPE_MODE (TREE_TYPE (exp1)))));
8711 if (modifier == EXPAND_STACK_PARM)
8712 target = 0;
8714 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8715 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8717 case TRUNC_DIV_EXPR:
8718 case FLOOR_DIV_EXPR:
8719 case CEIL_DIV_EXPR:
8720 case ROUND_DIV_EXPR:
8721 case EXACT_DIV_EXPR:
8722 /* If this is a fixed-point operation, then we cannot use the code
8723 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8724 divisions. */
8725 if (ALL_FIXED_POINT_MODE_P (mode))
8726 goto binop;
8728 if (modifier == EXPAND_STACK_PARM)
8729 target = 0;
8730 /* Possible optimization: compute the dividend with EXPAND_SUM
8731 then if the divisor is constant can optimize the case
8732 where some terms of the dividend have coeffs divisible by it. */
8733 expand_operands (treeop0, treeop1,
8734 subtarget, &op0, &op1, EXPAND_NORMAL);
8735 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8737 case RDIV_EXPR:
8738 goto binop;
8740 case MULT_HIGHPART_EXPR:
8741 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8742 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8743 gcc_assert (temp);
8744 return temp;
8746 case TRUNC_MOD_EXPR:
8747 case FLOOR_MOD_EXPR:
8748 case CEIL_MOD_EXPR:
8749 case ROUND_MOD_EXPR:
8750 if (modifier == EXPAND_STACK_PARM)
8751 target = 0;
8752 expand_operands (treeop0, treeop1,
8753 subtarget, &op0, &op1, EXPAND_NORMAL);
8754 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8756 case FIXED_CONVERT_EXPR:
8757 op0 = expand_normal (treeop0);
8758 if (target == 0 || modifier == EXPAND_STACK_PARM)
8759 target = gen_reg_rtx (mode);
8761 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8762 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8763 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8764 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8765 else
8766 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8767 return target;
8769 case FIX_TRUNC_EXPR:
8770 op0 = expand_normal (treeop0);
8771 if (target == 0 || modifier == EXPAND_STACK_PARM)
8772 target = gen_reg_rtx (mode);
8773 expand_fix (target, op0, unsignedp);
8774 return target;
8776 case FLOAT_EXPR:
8777 op0 = expand_normal (treeop0);
8778 if (target == 0 || modifier == EXPAND_STACK_PARM)
8779 target = gen_reg_rtx (mode);
8780 /* expand_float can't figure out what to do if FROM has VOIDmode.
8781 So give it the correct mode. With -O, cse will optimize this. */
8782 if (GET_MODE (op0) == VOIDmode)
8783 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8784 op0);
8785 expand_float (target, op0,
8786 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8787 return target;
8789 case NEGATE_EXPR:
8790 op0 = expand_expr (treeop0, subtarget,
8791 VOIDmode, EXPAND_NORMAL);
8792 if (modifier == EXPAND_STACK_PARM)
8793 target = 0;
8794 temp = expand_unop (mode,
8795 optab_for_tree_code (NEGATE_EXPR, type,
8796 optab_default),
8797 op0, target, 0);
8798 gcc_assert (temp);
8799 return REDUCE_BIT_FIELD (temp);
8801 case ABS_EXPR:
8802 op0 = expand_expr (treeop0, subtarget,
8803 VOIDmode, EXPAND_NORMAL);
8804 if (modifier == EXPAND_STACK_PARM)
8805 target = 0;
8807 /* ABS_EXPR is not valid for complex arguments. */
8808 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8809 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8811 /* Unsigned abs is simply the operand. Testing here means we don't
8812 risk generating incorrect code below. */
8813 if (TYPE_UNSIGNED (type))
8814 return op0;
8816 return expand_abs (mode, op0, target, unsignedp,
8817 safe_from_p (target, treeop0, 1));
8819 case MAX_EXPR:
8820 case MIN_EXPR:
8821 target = original_target;
8822 if (target == 0
8823 || modifier == EXPAND_STACK_PARM
8824 || (MEM_P (target) && MEM_VOLATILE_P (target))
8825 || GET_MODE (target) != mode
8826 || (REG_P (target)
8827 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8828 target = gen_reg_rtx (mode);
8829 expand_operands (treeop0, treeop1,
8830 target, &op0, &op1, EXPAND_NORMAL);
8832 /* First try to do it with a special MIN or MAX instruction.
8833 If that does not win, use a conditional jump to select the proper
8834 value. */
8835 this_optab = optab_for_tree_code (code, type, optab_default);
8836 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8837 OPTAB_WIDEN);
8838 if (temp != 0)
8839 return temp;
8841 /* At this point, a MEM target is no longer useful; we will get better
8842 code without it. */
8844 if (! REG_P (target))
8845 target = gen_reg_rtx (mode);
8847 /* If op1 was placed in target, swap op0 and op1. */
8848 if (target != op0 && target == op1)
8850 temp = op0;
8851 op0 = op1;
8852 op1 = temp;
8855 /* We generate better code and avoid problems with op1 mentioning
8856 target by forcing op1 into a pseudo if it isn't a constant. */
8857 if (! CONSTANT_P (op1))
8858 op1 = force_reg (mode, op1);
8861 enum rtx_code comparison_code;
8862 rtx cmpop1 = op1;
8864 if (code == MAX_EXPR)
8865 comparison_code = unsignedp ? GEU : GE;
8866 else
8867 comparison_code = unsignedp ? LEU : LE;
8869 /* Canonicalize to comparisons against 0. */
8870 if (op1 == const1_rtx)
8872 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8873 or (a != 0 ? a : 1) for unsigned.
8874 For MIN we are safe converting (a <= 1 ? a : 1)
8875 into (a <= 0 ? a : 1) */
8876 cmpop1 = const0_rtx;
8877 if (code == MAX_EXPR)
8878 comparison_code = unsignedp ? NE : GT;
8880 if (op1 == constm1_rtx && !unsignedp)
8882 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8883 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8884 cmpop1 = const0_rtx;
8885 if (code == MIN_EXPR)
8886 comparison_code = LT;
8888 #ifdef HAVE_conditional_move
8889 /* Use a conditional move if possible. */
8890 if (can_conditionally_move_p (mode))
8892 rtx insn;
8894 start_sequence ();
8896 /* Try to emit the conditional move. */
8897 insn = emit_conditional_move (target, comparison_code,
8898 op0, cmpop1, mode,
8899 op0, op1, mode,
8900 unsignedp);
8902 /* If we could do the conditional move, emit the sequence,
8903 and return. */
8904 if (insn)
8906 rtx_insn *seq = get_insns ();
8907 end_sequence ();
8908 emit_insn (seq);
8909 return target;
8912 /* Otherwise discard the sequence and fall back to code with
8913 branches. */
8914 end_sequence ();
8916 #endif
8917 if (target != op0)
8918 emit_move_insn (target, op0);
8920 temp = gen_label_rtx ();
8921 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8922 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8923 -1);
8925 emit_move_insn (target, op1);
8926 emit_label (temp);
8927 return target;
8929 case BIT_NOT_EXPR:
8930 op0 = expand_expr (treeop0, subtarget,
8931 VOIDmode, EXPAND_NORMAL);
8932 if (modifier == EXPAND_STACK_PARM)
8933 target = 0;
8934 /* In case we have to reduce the result to bitfield precision
8935 for unsigned bitfield expand this as XOR with a proper constant
8936 instead. */
8937 if (reduce_bit_field && TYPE_UNSIGNED (type))
8939 wide_int mask = wi::mask (TYPE_PRECISION (type),
8940 false, GET_MODE_PRECISION (mode));
8942 temp = expand_binop (mode, xor_optab, op0,
8943 immed_wide_int_const (mask, mode),
8944 target, 1, OPTAB_LIB_WIDEN);
8946 else
8947 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8948 gcc_assert (temp);
8949 return temp;
8951 /* ??? Can optimize bitwise operations with one arg constant.
8952 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8953 and (a bitwise1 b) bitwise2 b (etc)
8954 but that is probably not worth while. */
8956 case BIT_AND_EXPR:
8957 case BIT_IOR_EXPR:
8958 case BIT_XOR_EXPR:
8959 goto binop;
8961 case LROTATE_EXPR:
8962 case RROTATE_EXPR:
8963 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8964 || (GET_MODE_PRECISION (TYPE_MODE (type))
8965 == TYPE_PRECISION (type)));
8966 /* fall through */
8968 case LSHIFT_EXPR:
8969 case RSHIFT_EXPR:
8970 /* If this is a fixed-point operation, then we cannot use the code
8971 below because "expand_shift" doesn't support sat/no-sat fixed-point
8972 shifts. */
8973 if (ALL_FIXED_POINT_MODE_P (mode))
8974 goto binop;
8976 if (! safe_from_p (subtarget, treeop1, 1))
8977 subtarget = 0;
8978 if (modifier == EXPAND_STACK_PARM)
8979 target = 0;
8980 op0 = expand_expr (treeop0, subtarget,
8981 VOIDmode, EXPAND_NORMAL);
8982 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8983 unsignedp);
8984 if (code == LSHIFT_EXPR)
8985 temp = REDUCE_BIT_FIELD (temp);
8986 return temp;
8988 /* Could determine the answer when only additive constants differ. Also,
8989 the addition of one can be handled by changing the condition. */
8990 case LT_EXPR:
8991 case LE_EXPR:
8992 case GT_EXPR:
8993 case GE_EXPR:
8994 case EQ_EXPR:
8995 case NE_EXPR:
8996 case UNORDERED_EXPR:
8997 case ORDERED_EXPR:
8998 case UNLT_EXPR:
8999 case UNLE_EXPR:
9000 case UNGT_EXPR:
9001 case UNGE_EXPR:
9002 case UNEQ_EXPR:
9003 case LTGT_EXPR:
9004 temp = do_store_flag (ops,
9005 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9006 tmode != VOIDmode ? tmode : mode);
9007 if (temp)
9008 return temp;
9010 /* Use a compare and a jump for BLKmode comparisons, or for function
9011 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
9013 if ((target == 0
9014 || modifier == EXPAND_STACK_PARM
9015 || ! safe_from_p (target, treeop0, 1)
9016 || ! safe_from_p (target, treeop1, 1)
9017 /* Make sure we don't have a hard reg (such as function's return
9018 value) live across basic blocks, if not optimizing. */
9019 || (!optimize && REG_P (target)
9020 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9021 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9023 emit_move_insn (target, const0_rtx);
9025 op1 = gen_label_rtx ();
9026 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
9028 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9029 emit_move_insn (target, constm1_rtx);
9030 else
9031 emit_move_insn (target, const1_rtx);
9033 emit_label (op1);
9034 return target;
9036 case COMPLEX_EXPR:
9037 /* Get the rtx code of the operands. */
9038 op0 = expand_normal (treeop0);
9039 op1 = expand_normal (treeop1);
9041 if (!target)
9042 target = gen_reg_rtx (TYPE_MODE (type));
9043 else
9044 /* If target overlaps with op1, then either we need to force
9045 op1 into a pseudo (if target also overlaps with op0),
9046 or write the complex parts in reverse order. */
9047 switch (GET_CODE (target))
9049 case CONCAT:
9050 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9052 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9054 complex_expr_force_op1:
9055 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9056 emit_move_insn (temp, op1);
9057 op1 = temp;
9058 break;
9060 complex_expr_swap_order:
9061 /* Move the imaginary (op1) and real (op0) parts to their
9062 location. */
9063 write_complex_part (target, op1, true);
9064 write_complex_part (target, op0, false);
9066 return target;
9068 break;
9069 case MEM:
9070 temp = adjust_address_nv (target,
9071 GET_MODE_INNER (GET_MODE (target)), 0);
9072 if (reg_overlap_mentioned_p (temp, op1))
9074 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9075 temp = adjust_address_nv (target, imode,
9076 GET_MODE_SIZE (imode));
9077 if (reg_overlap_mentioned_p (temp, op0))
9078 goto complex_expr_force_op1;
9079 goto complex_expr_swap_order;
9081 break;
9082 default:
9083 if (reg_overlap_mentioned_p (target, op1))
9085 if (reg_overlap_mentioned_p (target, op0))
9086 goto complex_expr_force_op1;
9087 goto complex_expr_swap_order;
9089 break;
9092 /* Move the real (op0) and imaginary (op1) parts to their location. */
9093 write_complex_part (target, op0, false);
9094 write_complex_part (target, op1, true);
9096 return target;
9098 case WIDEN_SUM_EXPR:
9100 tree oprnd0 = treeop0;
9101 tree oprnd1 = treeop1;
9103 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9104 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9105 target, unsignedp);
9106 return target;
9109 case REDUC_MAX_EXPR:
9110 case REDUC_MIN_EXPR:
9111 case REDUC_PLUS_EXPR:
9113 op0 = expand_normal (treeop0);
9114 this_optab = optab_for_tree_code (code, type, optab_default);
9115 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9117 if (optab_handler (this_optab, vec_mode) != CODE_FOR_nothing)
9119 struct expand_operand ops[2];
9120 enum insn_code icode = optab_handler (this_optab, vec_mode);
9122 create_output_operand (&ops[0], target, mode);
9123 create_input_operand (&ops[1], op0, vec_mode);
9124 if (maybe_expand_insn (icode, 2, ops))
9126 target = ops[0].value;
9127 if (GET_MODE (target) != mode)
9128 return gen_lowpart (tmode, target);
9129 return target;
9132 /* Fall back to optab with vector result, and then extract scalar. */
9133 this_optab = scalar_reduc_to_vector (this_optab, type);
9134 temp = expand_unop (vec_mode, this_optab, op0, NULL_RTX, unsignedp);
9135 gcc_assert (temp);
9136 /* The tree code produces a scalar result, but (somewhat by convention)
9137 the optab produces a vector with the result in element 0 if
9138 little-endian, or element N-1 if big-endian. So pull the scalar
9139 result out of that element. */
9140 int index = BYTES_BIG_ENDIAN ? GET_MODE_NUNITS (vec_mode) - 1 : 0;
9141 int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (vec_mode));
9142 temp = extract_bit_field (temp, bitsize, bitsize * index, unsignedp,
9143 target, mode, mode);
9144 gcc_assert (temp);
9145 return temp;
9148 case VEC_UNPACK_HI_EXPR:
9149 case VEC_UNPACK_LO_EXPR:
9151 op0 = expand_normal (treeop0);
9152 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9153 target, unsignedp);
9154 gcc_assert (temp);
9155 return temp;
9158 case VEC_UNPACK_FLOAT_HI_EXPR:
9159 case VEC_UNPACK_FLOAT_LO_EXPR:
9161 op0 = expand_normal (treeop0);
9162 /* The signedness is determined from input operand. */
9163 temp = expand_widen_pattern_expr
9164 (ops, op0, NULL_RTX, NULL_RTX,
9165 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9167 gcc_assert (temp);
9168 return temp;
9171 case VEC_WIDEN_MULT_HI_EXPR:
9172 case VEC_WIDEN_MULT_LO_EXPR:
9173 case VEC_WIDEN_MULT_EVEN_EXPR:
9174 case VEC_WIDEN_MULT_ODD_EXPR:
9175 case VEC_WIDEN_LSHIFT_HI_EXPR:
9176 case VEC_WIDEN_LSHIFT_LO_EXPR:
9177 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9178 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9179 target, unsignedp);
9180 gcc_assert (target);
9181 return target;
9183 case VEC_PACK_TRUNC_EXPR:
9184 case VEC_PACK_SAT_EXPR:
9185 case VEC_PACK_FIX_TRUNC_EXPR:
9186 mode = TYPE_MODE (TREE_TYPE (treeop0));
9187 goto binop;
9189 case VEC_PERM_EXPR:
9190 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9191 op2 = expand_normal (treeop2);
9193 /* Careful here: if the target doesn't support integral vector modes,
9194 a constant selection vector could wind up smooshed into a normal
9195 integral constant. */
9196 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9198 tree sel_type = TREE_TYPE (treeop2);
9199 machine_mode vmode
9200 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9201 TYPE_VECTOR_SUBPARTS (sel_type));
9202 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9203 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9204 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9206 else
9207 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9209 temp = expand_vec_perm (mode, op0, op1, op2, target);
9210 gcc_assert (temp);
9211 return temp;
9213 case DOT_PROD_EXPR:
9215 tree oprnd0 = treeop0;
9216 tree oprnd1 = treeop1;
9217 tree oprnd2 = treeop2;
9218 rtx op2;
9220 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9221 op2 = expand_normal (oprnd2);
9222 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9223 target, unsignedp);
9224 return target;
9227 case SAD_EXPR:
9229 tree oprnd0 = treeop0;
9230 tree oprnd1 = treeop1;
9231 tree oprnd2 = treeop2;
9232 rtx op2;
9234 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9235 op2 = expand_normal (oprnd2);
9236 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9237 target, unsignedp);
9238 return target;
9241 case REALIGN_LOAD_EXPR:
9243 tree oprnd0 = treeop0;
9244 tree oprnd1 = treeop1;
9245 tree oprnd2 = treeop2;
9246 rtx op2;
9248 this_optab = optab_for_tree_code (code, type, optab_default);
9249 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9250 op2 = expand_normal (oprnd2);
9251 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9252 target, unsignedp);
9253 gcc_assert (temp);
9254 return temp;
9257 case COND_EXPR:
9258 /* A COND_EXPR with its type being VOID_TYPE represents a
9259 conditional jump and is handled in
9260 expand_gimple_cond_expr. */
9261 gcc_assert (!VOID_TYPE_P (type));
9263 /* Note that COND_EXPRs whose type is a structure or union
9264 are required to be constructed to contain assignments of
9265 a temporary variable, so that we can evaluate them here
9266 for side effect only. If type is void, we must do likewise. */
9268 gcc_assert (!TREE_ADDRESSABLE (type)
9269 && !ignore
9270 && TREE_TYPE (treeop1) != void_type_node
9271 && TREE_TYPE (treeop2) != void_type_node);
9273 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9274 if (temp)
9275 return temp;
9277 /* If we are not to produce a result, we have no target. Otherwise,
9278 if a target was specified use it; it will not be used as an
9279 intermediate target unless it is safe. If no target, use a
9280 temporary. */
9282 if (modifier != EXPAND_STACK_PARM
9283 && original_target
9284 && safe_from_p (original_target, treeop0, 1)
9285 && GET_MODE (original_target) == mode
9286 && !MEM_P (original_target))
9287 temp = original_target;
9288 else
9289 temp = assign_temp (type, 0, 1);
9291 do_pending_stack_adjust ();
9292 NO_DEFER_POP;
9293 op0 = gen_label_rtx ();
9294 op1 = gen_label_rtx ();
9295 jumpifnot (treeop0, op0, -1);
9296 store_expr (treeop1, temp,
9297 modifier == EXPAND_STACK_PARM,
9298 false);
9300 emit_jump_insn (gen_jump (op1));
9301 emit_barrier ();
9302 emit_label (op0);
9303 store_expr (treeop2, temp,
9304 modifier == EXPAND_STACK_PARM,
9305 false);
9307 emit_label (op1);
9308 OK_DEFER_POP;
9309 return temp;
9311 case VEC_COND_EXPR:
9312 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9313 return target;
9315 default:
9316 gcc_unreachable ();
9319 /* Here to do an ordinary binary operator. */
9320 binop:
9321 expand_operands (treeop0, treeop1,
9322 subtarget, &op0, &op1, EXPAND_NORMAL);
9323 binop2:
9324 this_optab = optab_for_tree_code (code, type, optab_default);
9325 binop3:
9326 if (modifier == EXPAND_STACK_PARM)
9327 target = 0;
9328 temp = expand_binop (mode, this_optab, op0, op1, target,
9329 unsignedp, OPTAB_LIB_WIDEN);
9330 gcc_assert (temp);
9331 /* Bitwise operations do not need bitfield reduction as we expect their
9332 operands being properly truncated. */
9333 if (code == BIT_XOR_EXPR
9334 || code == BIT_AND_EXPR
9335 || code == BIT_IOR_EXPR)
9336 return temp;
9337 return REDUCE_BIT_FIELD (temp);
9339 #undef REDUCE_BIT_FIELD
9342 /* Return TRUE if expression STMT is suitable for replacement.
9343 Never consider memory loads as replaceable, because those don't ever lead
9344 into constant expressions. */
9346 static bool
9347 stmt_is_replaceable_p (gimple stmt)
9349 if (ssa_is_replaceable_p (stmt))
9351 /* Don't move around loads. */
9352 if (!gimple_assign_single_p (stmt)
9353 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9354 return true;
9356 return false;
9360 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9361 enum expand_modifier modifier, rtx *alt_rtl,
9362 bool inner_reference_p)
9364 rtx op0, op1, temp, decl_rtl;
9365 tree type;
9366 int unsignedp;
9367 machine_mode mode;
9368 enum tree_code code = TREE_CODE (exp);
9369 rtx subtarget, original_target;
9370 int ignore;
9371 tree context;
9372 bool reduce_bit_field;
9373 location_t loc = EXPR_LOCATION (exp);
9374 struct separate_ops ops;
9375 tree treeop0, treeop1, treeop2;
9376 tree ssa_name = NULL_TREE;
9377 gimple g;
9379 type = TREE_TYPE (exp);
9380 mode = TYPE_MODE (type);
9381 unsignedp = TYPE_UNSIGNED (type);
9383 treeop0 = treeop1 = treeop2 = NULL_TREE;
9384 if (!VL_EXP_CLASS_P (exp))
9385 switch (TREE_CODE_LENGTH (code))
9387 default:
9388 case 3: treeop2 = TREE_OPERAND (exp, 2);
9389 case 2: treeop1 = TREE_OPERAND (exp, 1);
9390 case 1: treeop0 = TREE_OPERAND (exp, 0);
9391 case 0: break;
9393 ops.code = code;
9394 ops.type = type;
9395 ops.op0 = treeop0;
9396 ops.op1 = treeop1;
9397 ops.op2 = treeop2;
9398 ops.location = loc;
9400 ignore = (target == const0_rtx
9401 || ((CONVERT_EXPR_CODE_P (code)
9402 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9403 && TREE_CODE (type) == VOID_TYPE));
9405 /* An operation in what may be a bit-field type needs the
9406 result to be reduced to the precision of the bit-field type,
9407 which is narrower than that of the type's mode. */
9408 reduce_bit_field = (!ignore
9409 && INTEGRAL_TYPE_P (type)
9410 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9412 /* If we are going to ignore this result, we need only do something
9413 if there is a side-effect somewhere in the expression. If there
9414 is, short-circuit the most common cases here. Note that we must
9415 not call expand_expr with anything but const0_rtx in case this
9416 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9418 if (ignore)
9420 if (! TREE_SIDE_EFFECTS (exp))
9421 return const0_rtx;
9423 /* Ensure we reference a volatile object even if value is ignored, but
9424 don't do this if all we are doing is taking its address. */
9425 if (TREE_THIS_VOLATILE (exp)
9426 && TREE_CODE (exp) != FUNCTION_DECL
9427 && mode != VOIDmode && mode != BLKmode
9428 && modifier != EXPAND_CONST_ADDRESS)
9430 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9431 if (MEM_P (temp))
9432 copy_to_reg (temp);
9433 return const0_rtx;
9436 if (TREE_CODE_CLASS (code) == tcc_unary
9437 || code == BIT_FIELD_REF
9438 || code == COMPONENT_REF
9439 || code == INDIRECT_REF)
9440 return expand_expr (treeop0, const0_rtx, VOIDmode,
9441 modifier);
9443 else if (TREE_CODE_CLASS (code) == tcc_binary
9444 || TREE_CODE_CLASS (code) == tcc_comparison
9445 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9447 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9448 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9449 return const0_rtx;
9452 target = 0;
9455 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9456 target = 0;
9458 /* Use subtarget as the target for operand 0 of a binary operation. */
9459 subtarget = get_subtarget (target);
9460 original_target = target;
9462 switch (code)
9464 case LABEL_DECL:
9466 tree function = decl_function_context (exp);
9468 temp = label_rtx (exp);
9469 temp = gen_rtx_LABEL_REF (Pmode, temp);
9471 if (function != current_function_decl
9472 && function != 0)
9473 LABEL_REF_NONLOCAL_P (temp) = 1;
9475 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9476 return temp;
9479 case SSA_NAME:
9480 /* ??? ivopts calls expander, without any preparation from
9481 out-of-ssa. So fake instructions as if this was an access to the
9482 base variable. This unnecessarily allocates a pseudo, see how we can
9483 reuse it, if partition base vars have it set already. */
9484 if (!currently_expanding_to_rtl)
9486 tree var = SSA_NAME_VAR (exp);
9487 if (var && DECL_RTL_SET_P (var))
9488 return DECL_RTL (var);
9489 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9490 LAST_VIRTUAL_REGISTER + 1);
9493 g = get_gimple_for_ssa_name (exp);
9494 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9495 if (g == NULL
9496 && modifier == EXPAND_INITIALIZER
9497 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9498 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9499 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9500 g = SSA_NAME_DEF_STMT (exp);
9501 if (g)
9503 rtx r;
9504 ops.code = gimple_assign_rhs_code (g);
9505 switch (get_gimple_rhs_class (ops.code))
9507 case GIMPLE_TERNARY_RHS:
9508 ops.op2 = gimple_assign_rhs3 (g);
9509 /* Fallthru */
9510 case GIMPLE_BINARY_RHS:
9511 ops.op1 = gimple_assign_rhs2 (g);
9513 /* Try to expand conditonal compare. */
9514 if (targetm.gen_ccmp_first)
9516 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9517 r = expand_ccmp_expr (g);
9518 if (r)
9519 break;
9521 /* Fallthru */
9522 case GIMPLE_UNARY_RHS:
9523 ops.op0 = gimple_assign_rhs1 (g);
9524 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9525 ops.location = gimple_location (g);
9526 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9527 break;
9528 case GIMPLE_SINGLE_RHS:
9530 location_t saved_loc = curr_insn_location ();
9531 set_curr_insn_location (gimple_location (g));
9532 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9533 tmode, modifier, NULL, inner_reference_p);
9534 set_curr_insn_location (saved_loc);
9535 break;
9537 default:
9538 gcc_unreachable ();
9540 if (REG_P (r) && !REG_EXPR (r))
9541 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9542 return r;
9545 ssa_name = exp;
9546 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9547 exp = SSA_NAME_VAR (ssa_name);
9548 goto expand_decl_rtl;
9550 case PARM_DECL:
9551 case VAR_DECL:
9552 /* If a static var's type was incomplete when the decl was written,
9553 but the type is complete now, lay out the decl now. */
9554 if (DECL_SIZE (exp) == 0
9555 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9556 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9557 layout_decl (exp, 0);
9559 /* ... fall through ... */
9561 case FUNCTION_DECL:
9562 case RESULT_DECL:
9563 decl_rtl = DECL_RTL (exp);
9564 expand_decl_rtl:
9565 gcc_assert (decl_rtl);
9566 decl_rtl = copy_rtx (decl_rtl);
9567 /* Record writes to register variables. */
9568 if (modifier == EXPAND_WRITE
9569 && REG_P (decl_rtl)
9570 && HARD_REGISTER_P (decl_rtl))
9571 add_to_hard_reg_set (&crtl->asm_clobbers,
9572 GET_MODE (decl_rtl), REGNO (decl_rtl));
9574 /* Ensure variable marked as used even if it doesn't go through
9575 a parser. If it hasn't be used yet, write out an external
9576 definition. */
9577 TREE_USED (exp) = 1;
9579 /* Show we haven't gotten RTL for this yet. */
9580 temp = 0;
9582 /* Variables inherited from containing functions should have
9583 been lowered by this point. */
9584 context = decl_function_context (exp);
9585 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9586 || context == current_function_decl
9587 || TREE_STATIC (exp)
9588 || DECL_EXTERNAL (exp)
9589 /* ??? C++ creates functions that are not TREE_STATIC. */
9590 || TREE_CODE (exp) == FUNCTION_DECL);
9592 /* This is the case of an array whose size is to be determined
9593 from its initializer, while the initializer is still being parsed.
9594 ??? We aren't parsing while expanding anymore. */
9596 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9597 temp = validize_mem (decl_rtl);
9599 /* If DECL_RTL is memory, we are in the normal case and the
9600 address is not valid, get the address into a register. */
9602 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9604 if (alt_rtl)
9605 *alt_rtl = decl_rtl;
9606 decl_rtl = use_anchored_address (decl_rtl);
9607 if (modifier != EXPAND_CONST_ADDRESS
9608 && modifier != EXPAND_SUM
9609 && !memory_address_addr_space_p (DECL_MODE (exp),
9610 XEXP (decl_rtl, 0),
9611 MEM_ADDR_SPACE (decl_rtl)))
9612 temp = replace_equiv_address (decl_rtl,
9613 copy_rtx (XEXP (decl_rtl, 0)));
9616 /* If we got something, return it. But first, set the alignment
9617 if the address is a register. */
9618 if (temp != 0)
9620 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9621 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9623 return temp;
9626 /* If the mode of DECL_RTL does not match that of the decl,
9627 there are two cases: we are dealing with a BLKmode value
9628 that is returned in a register, or we are dealing with
9629 a promoted value. In the latter case, return a SUBREG
9630 of the wanted mode, but mark it so that we know that it
9631 was already extended. */
9632 if (REG_P (decl_rtl)
9633 && DECL_MODE (exp) != BLKmode
9634 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9636 machine_mode pmode;
9638 /* Get the signedness to be used for this variable. Ensure we get
9639 the same mode we got when the variable was declared. */
9640 if (code == SSA_NAME
9641 && (g = SSA_NAME_DEF_STMT (ssa_name))
9642 && gimple_code (g) == GIMPLE_CALL
9643 && !gimple_call_internal_p (g))
9644 pmode = promote_function_mode (type, mode, &unsignedp,
9645 gimple_call_fntype (g),
9647 else
9648 pmode = promote_decl_mode (exp, &unsignedp);
9649 gcc_assert (GET_MODE (decl_rtl) == pmode);
9651 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9652 SUBREG_PROMOTED_VAR_P (temp) = 1;
9653 SUBREG_PROMOTED_SET (temp, unsignedp);
9654 return temp;
9657 return decl_rtl;
9659 case INTEGER_CST:
9660 /* Given that TYPE_PRECISION (type) is not always equal to
9661 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9662 the former to the latter according to the signedness of the
9663 type. */
9664 temp = immed_wide_int_const (wide_int::from
9665 (exp,
9666 GET_MODE_PRECISION (TYPE_MODE (type)),
9667 TYPE_SIGN (type)),
9668 TYPE_MODE (type));
9669 return temp;
9671 case VECTOR_CST:
9673 tree tmp = NULL_TREE;
9674 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9675 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9676 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9677 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9678 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9679 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9680 return const_vector_from_tree (exp);
9681 if (GET_MODE_CLASS (mode) == MODE_INT)
9683 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9684 if (type_for_mode)
9685 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9687 if (!tmp)
9689 vec<constructor_elt, va_gc> *v;
9690 unsigned i;
9691 vec_alloc (v, VECTOR_CST_NELTS (exp));
9692 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9693 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9694 tmp = build_constructor (type, v);
9696 return expand_expr (tmp, ignore ? const0_rtx : target,
9697 tmode, modifier);
9700 case CONST_DECL:
9701 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9703 case REAL_CST:
9704 /* If optimized, generate immediate CONST_DOUBLE
9705 which will be turned into memory by reload if necessary.
9707 We used to force a register so that loop.c could see it. But
9708 this does not allow gen_* patterns to perform optimizations with
9709 the constants. It also produces two insns in cases like "x = 1.0;".
9710 On most machines, floating-point constants are not permitted in
9711 many insns, so we'd end up copying it to a register in any case.
9713 Now, we do the copying in expand_binop, if appropriate. */
9714 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9715 TYPE_MODE (TREE_TYPE (exp)));
9717 case FIXED_CST:
9718 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9719 TYPE_MODE (TREE_TYPE (exp)));
9721 case COMPLEX_CST:
9722 /* Handle evaluating a complex constant in a CONCAT target. */
9723 if (original_target && GET_CODE (original_target) == CONCAT)
9725 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9726 rtx rtarg, itarg;
9728 rtarg = XEXP (original_target, 0);
9729 itarg = XEXP (original_target, 1);
9731 /* Move the real and imaginary parts separately. */
9732 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9733 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9735 if (op0 != rtarg)
9736 emit_move_insn (rtarg, op0);
9737 if (op1 != itarg)
9738 emit_move_insn (itarg, op1);
9740 return original_target;
9743 /* ... fall through ... */
9745 case STRING_CST:
9746 temp = expand_expr_constant (exp, 1, modifier);
9748 /* temp contains a constant address.
9749 On RISC machines where a constant address isn't valid,
9750 make some insns to get that address into a register. */
9751 if (modifier != EXPAND_CONST_ADDRESS
9752 && modifier != EXPAND_INITIALIZER
9753 && modifier != EXPAND_SUM
9754 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9755 MEM_ADDR_SPACE (temp)))
9756 return replace_equiv_address (temp,
9757 copy_rtx (XEXP (temp, 0)));
9758 return temp;
9760 case SAVE_EXPR:
9762 tree val = treeop0;
9763 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9764 inner_reference_p);
9766 if (!SAVE_EXPR_RESOLVED_P (exp))
9768 /* We can indeed still hit this case, typically via builtin
9769 expanders calling save_expr immediately before expanding
9770 something. Assume this means that we only have to deal
9771 with non-BLKmode values. */
9772 gcc_assert (GET_MODE (ret) != BLKmode);
9774 val = build_decl (curr_insn_location (),
9775 VAR_DECL, NULL, TREE_TYPE (exp));
9776 DECL_ARTIFICIAL (val) = 1;
9777 DECL_IGNORED_P (val) = 1;
9778 treeop0 = val;
9779 TREE_OPERAND (exp, 0) = treeop0;
9780 SAVE_EXPR_RESOLVED_P (exp) = 1;
9782 if (!CONSTANT_P (ret))
9783 ret = copy_to_reg (ret);
9784 SET_DECL_RTL (val, ret);
9787 return ret;
9791 case CONSTRUCTOR:
9792 /* If we don't need the result, just ensure we evaluate any
9793 subexpressions. */
9794 if (ignore)
9796 unsigned HOST_WIDE_INT idx;
9797 tree value;
9799 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9800 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9802 return const0_rtx;
9805 return expand_constructor (exp, target, modifier, false);
9807 case TARGET_MEM_REF:
9809 addr_space_t as
9810 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9811 enum insn_code icode;
9812 unsigned int align;
9814 op0 = addr_for_mem_ref (exp, as, true);
9815 op0 = memory_address_addr_space (mode, op0, as);
9816 temp = gen_rtx_MEM (mode, op0);
9817 set_mem_attributes (temp, exp, 0);
9818 set_mem_addr_space (temp, as);
9819 align = get_object_alignment (exp);
9820 if (modifier != EXPAND_WRITE
9821 && modifier != EXPAND_MEMORY
9822 && mode != BLKmode
9823 && align < GET_MODE_ALIGNMENT (mode)
9824 /* If the target does not have special handling for unaligned
9825 loads of mode then it can use regular moves for them. */
9826 && ((icode = optab_handler (movmisalign_optab, mode))
9827 != CODE_FOR_nothing))
9829 struct expand_operand ops[2];
9831 /* We've already validated the memory, and we're creating a
9832 new pseudo destination. The predicates really can't fail,
9833 nor can the generator. */
9834 create_output_operand (&ops[0], NULL_RTX, mode);
9835 create_fixed_operand (&ops[1], temp);
9836 expand_insn (icode, 2, ops);
9837 temp = ops[0].value;
9839 return temp;
9842 case MEM_REF:
9844 addr_space_t as
9845 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9846 machine_mode address_mode;
9847 tree base = TREE_OPERAND (exp, 0);
9848 gimple def_stmt;
9849 enum insn_code icode;
9850 unsigned align;
9851 /* Handle expansion of non-aliased memory with non-BLKmode. That
9852 might end up in a register. */
9853 if (mem_ref_refers_to_non_mem_p (exp))
9855 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9856 base = TREE_OPERAND (base, 0);
9857 if (offset == 0
9858 && tree_fits_uhwi_p (TYPE_SIZE (type))
9859 && (GET_MODE_BITSIZE (DECL_MODE (base))
9860 == tree_to_uhwi (TYPE_SIZE (type))))
9861 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9862 target, tmode, modifier);
9863 if (TYPE_MODE (type) == BLKmode)
9865 temp = assign_stack_temp (DECL_MODE (base),
9866 GET_MODE_SIZE (DECL_MODE (base)));
9867 store_expr (base, temp, 0, false);
9868 temp = adjust_address (temp, BLKmode, offset);
9869 set_mem_size (temp, int_size_in_bytes (type));
9870 return temp;
9872 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9873 bitsize_int (offset * BITS_PER_UNIT));
9874 return expand_expr (exp, target, tmode, modifier);
9876 address_mode = targetm.addr_space.address_mode (as);
9877 base = TREE_OPERAND (exp, 0);
9878 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9880 tree mask = gimple_assign_rhs2 (def_stmt);
9881 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9882 gimple_assign_rhs1 (def_stmt), mask);
9883 TREE_OPERAND (exp, 0) = base;
9885 align = get_object_alignment (exp);
9886 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9887 op0 = memory_address_addr_space (mode, op0, as);
9888 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9890 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9891 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9892 op0 = memory_address_addr_space (mode, op0, as);
9894 temp = gen_rtx_MEM (mode, op0);
9895 set_mem_attributes (temp, exp, 0);
9896 set_mem_addr_space (temp, as);
9897 if (TREE_THIS_VOLATILE (exp))
9898 MEM_VOLATILE_P (temp) = 1;
9899 if (modifier != EXPAND_WRITE
9900 && modifier != EXPAND_MEMORY
9901 && !inner_reference_p
9902 && mode != BLKmode
9903 && align < GET_MODE_ALIGNMENT (mode))
9905 if ((icode = optab_handler (movmisalign_optab, mode))
9906 != CODE_FOR_nothing)
9908 struct expand_operand ops[2];
9910 /* We've already validated the memory, and we're creating a
9911 new pseudo destination. The predicates really can't fail,
9912 nor can the generator. */
9913 create_output_operand (&ops[0], NULL_RTX, mode);
9914 create_fixed_operand (&ops[1], temp);
9915 expand_insn (icode, 2, ops);
9916 temp = ops[0].value;
9918 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9919 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9920 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9921 (modifier == EXPAND_STACK_PARM
9922 ? NULL_RTX : target),
9923 mode, mode);
9925 return temp;
9928 case ARRAY_REF:
9931 tree array = treeop0;
9932 tree index = treeop1;
9933 tree init;
9935 /* Fold an expression like: "foo"[2].
9936 This is not done in fold so it won't happen inside &.
9937 Don't fold if this is for wide characters since it's too
9938 difficult to do correctly and this is a very rare case. */
9940 if (modifier != EXPAND_CONST_ADDRESS
9941 && modifier != EXPAND_INITIALIZER
9942 && modifier != EXPAND_MEMORY)
9944 tree t = fold_read_from_constant_string (exp);
9946 if (t)
9947 return expand_expr (t, target, tmode, modifier);
9950 /* If this is a constant index into a constant array,
9951 just get the value from the array. Handle both the cases when
9952 we have an explicit constructor and when our operand is a variable
9953 that was declared const. */
9955 if (modifier != EXPAND_CONST_ADDRESS
9956 && modifier != EXPAND_INITIALIZER
9957 && modifier != EXPAND_MEMORY
9958 && TREE_CODE (array) == CONSTRUCTOR
9959 && ! TREE_SIDE_EFFECTS (array)
9960 && TREE_CODE (index) == INTEGER_CST)
9962 unsigned HOST_WIDE_INT ix;
9963 tree field, value;
9965 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9966 field, value)
9967 if (tree_int_cst_equal (field, index))
9969 if (!TREE_SIDE_EFFECTS (value))
9970 return expand_expr (fold (value), target, tmode, modifier);
9971 break;
9975 else if (optimize >= 1
9976 && modifier != EXPAND_CONST_ADDRESS
9977 && modifier != EXPAND_INITIALIZER
9978 && modifier != EXPAND_MEMORY
9979 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9980 && TREE_CODE (index) == INTEGER_CST
9981 && (TREE_CODE (array) == VAR_DECL
9982 || TREE_CODE (array) == CONST_DECL)
9983 && (init = ctor_for_folding (array)) != error_mark_node)
9985 if (init == NULL_TREE)
9987 tree value = build_zero_cst (type);
9988 if (TREE_CODE (value) == CONSTRUCTOR)
9990 /* If VALUE is a CONSTRUCTOR, this optimization is only
9991 useful if this doesn't store the CONSTRUCTOR into
9992 memory. If it does, it is more efficient to just
9993 load the data from the array directly. */
9994 rtx ret = expand_constructor (value, target,
9995 modifier, true);
9996 if (ret == NULL_RTX)
9997 value = NULL_TREE;
10000 if (value)
10001 return expand_expr (value, target, tmode, modifier);
10003 else if (TREE_CODE (init) == CONSTRUCTOR)
10005 unsigned HOST_WIDE_INT ix;
10006 tree field, value;
10008 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10009 field, value)
10010 if (tree_int_cst_equal (field, index))
10012 if (TREE_SIDE_EFFECTS (value))
10013 break;
10015 if (TREE_CODE (value) == CONSTRUCTOR)
10017 /* If VALUE is a CONSTRUCTOR, this
10018 optimization is only useful if
10019 this doesn't store the CONSTRUCTOR
10020 into memory. If it does, it is more
10021 efficient to just load the data from
10022 the array directly. */
10023 rtx ret = expand_constructor (value, target,
10024 modifier, true);
10025 if (ret == NULL_RTX)
10026 break;
10029 return
10030 expand_expr (fold (value), target, tmode, modifier);
10033 else if (TREE_CODE (init) == STRING_CST)
10035 tree low_bound = array_ref_low_bound (exp);
10036 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10038 /* Optimize the special case of a zero lower bound.
10040 We convert the lower bound to sizetype to avoid problems
10041 with constant folding. E.g. suppose the lower bound is
10042 1 and its mode is QI. Without the conversion
10043 (ARRAY + (INDEX - (unsigned char)1))
10044 becomes
10045 (ARRAY + (-(unsigned char)1) + INDEX)
10046 which becomes
10047 (ARRAY + 255 + INDEX). Oops! */
10048 if (!integer_zerop (low_bound))
10049 index1 = size_diffop_loc (loc, index1,
10050 fold_convert_loc (loc, sizetype,
10051 low_bound));
10053 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10055 tree type = TREE_TYPE (TREE_TYPE (init));
10056 machine_mode mode = TYPE_MODE (type);
10058 if (GET_MODE_CLASS (mode) == MODE_INT
10059 && GET_MODE_SIZE (mode) == 1)
10060 return gen_int_mode (TREE_STRING_POINTER (init)
10061 [TREE_INT_CST_LOW (index1)],
10062 mode);
10067 goto normal_inner_ref;
10069 case COMPONENT_REF:
10070 /* If the operand is a CONSTRUCTOR, we can just extract the
10071 appropriate field if it is present. */
10072 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10074 unsigned HOST_WIDE_INT idx;
10075 tree field, value;
10077 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10078 idx, field, value)
10079 if (field == treeop1
10080 /* We can normally use the value of the field in the
10081 CONSTRUCTOR. However, if this is a bitfield in
10082 an integral mode that we can fit in a HOST_WIDE_INT,
10083 we must mask only the number of bits in the bitfield,
10084 since this is done implicitly by the constructor. If
10085 the bitfield does not meet either of those conditions,
10086 we can't do this optimization. */
10087 && (! DECL_BIT_FIELD (field)
10088 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10089 && (GET_MODE_PRECISION (DECL_MODE (field))
10090 <= HOST_BITS_PER_WIDE_INT))))
10092 if (DECL_BIT_FIELD (field)
10093 && modifier == EXPAND_STACK_PARM)
10094 target = 0;
10095 op0 = expand_expr (value, target, tmode, modifier);
10096 if (DECL_BIT_FIELD (field))
10098 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10099 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10101 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10103 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10104 imode);
10105 op0 = expand_and (imode, op0, op1, target);
10107 else
10109 int count = GET_MODE_PRECISION (imode) - bitsize;
10111 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10112 target, 0);
10113 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10114 target, 0);
10118 return op0;
10121 goto normal_inner_ref;
10123 case BIT_FIELD_REF:
10124 case ARRAY_RANGE_REF:
10125 normal_inner_ref:
10127 machine_mode mode1, mode2;
10128 HOST_WIDE_INT bitsize, bitpos;
10129 tree offset;
10130 int volatilep = 0, must_force_mem;
10131 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10132 &mode1, &unsignedp, &volatilep, true);
10133 rtx orig_op0, memloc;
10134 bool mem_attrs_from_type = false;
10136 /* If we got back the original object, something is wrong. Perhaps
10137 we are evaluating an expression too early. In any event, don't
10138 infinitely recurse. */
10139 gcc_assert (tem != exp);
10141 /* If TEM's type is a union of variable size, pass TARGET to the inner
10142 computation, since it will need a temporary and TARGET is known
10143 to have to do. This occurs in unchecked conversion in Ada. */
10144 orig_op0 = op0
10145 = expand_expr_real (tem,
10146 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10147 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10148 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10149 != INTEGER_CST)
10150 && modifier != EXPAND_STACK_PARM
10151 ? target : NULL_RTX),
10152 VOIDmode,
10153 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10154 NULL, true);
10156 /* If the field has a mode, we want to access it in the
10157 field's mode, not the computed mode.
10158 If a MEM has VOIDmode (external with incomplete type),
10159 use BLKmode for it instead. */
10160 if (MEM_P (op0))
10162 if (mode1 != VOIDmode)
10163 op0 = adjust_address (op0, mode1, 0);
10164 else if (GET_MODE (op0) == VOIDmode)
10165 op0 = adjust_address (op0, BLKmode, 0);
10168 mode2
10169 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10171 /* If we have either an offset, a BLKmode result, or a reference
10172 outside the underlying object, we must force it to memory.
10173 Such a case can occur in Ada if we have unchecked conversion
10174 of an expression from a scalar type to an aggregate type or
10175 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10176 passed a partially uninitialized object or a view-conversion
10177 to a larger size. */
10178 must_force_mem = (offset
10179 || mode1 == BLKmode
10180 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10182 /* Handle CONCAT first. */
10183 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10185 if (bitpos == 0
10186 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10187 return op0;
10188 if (bitpos == 0
10189 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10190 && bitsize)
10192 op0 = XEXP (op0, 0);
10193 mode2 = GET_MODE (op0);
10195 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10196 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10197 && bitpos
10198 && bitsize)
10200 op0 = XEXP (op0, 1);
10201 bitpos = 0;
10202 mode2 = GET_MODE (op0);
10204 else
10205 /* Otherwise force into memory. */
10206 must_force_mem = 1;
10209 /* If this is a constant, put it in a register if it is a legitimate
10210 constant and we don't need a memory reference. */
10211 if (CONSTANT_P (op0)
10212 && mode2 != BLKmode
10213 && targetm.legitimate_constant_p (mode2, op0)
10214 && !must_force_mem)
10215 op0 = force_reg (mode2, op0);
10217 /* Otherwise, if this is a constant, try to force it to the constant
10218 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10219 is a legitimate constant. */
10220 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10221 op0 = validize_mem (memloc);
10223 /* Otherwise, if this is a constant or the object is not in memory
10224 and need be, put it there. */
10225 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10227 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10228 emit_move_insn (memloc, op0);
10229 op0 = memloc;
10230 mem_attrs_from_type = true;
10233 if (offset)
10235 machine_mode address_mode;
10236 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10237 EXPAND_SUM);
10239 gcc_assert (MEM_P (op0));
10241 address_mode = get_address_mode (op0);
10242 if (GET_MODE (offset_rtx) != address_mode)
10243 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10245 /* See the comment in expand_assignment for the rationale. */
10246 if (mode1 != VOIDmode
10247 && bitpos != 0
10248 && bitsize > 0
10249 && (bitpos % bitsize) == 0
10250 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10251 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10253 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10254 bitpos = 0;
10257 op0 = offset_address (op0, offset_rtx,
10258 highest_pow2_factor (offset));
10261 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10262 record its alignment as BIGGEST_ALIGNMENT. */
10263 if (MEM_P (op0) && bitpos == 0 && offset != 0
10264 && is_aligning_offset (offset, tem))
10265 set_mem_align (op0, BIGGEST_ALIGNMENT);
10267 /* Don't forget about volatility even if this is a bitfield. */
10268 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10270 if (op0 == orig_op0)
10271 op0 = copy_rtx (op0);
10273 MEM_VOLATILE_P (op0) = 1;
10276 /* In cases where an aligned union has an unaligned object
10277 as a field, we might be extracting a BLKmode value from
10278 an integer-mode (e.g., SImode) object. Handle this case
10279 by doing the extract into an object as wide as the field
10280 (which we know to be the width of a basic mode), then
10281 storing into memory, and changing the mode to BLKmode. */
10282 if (mode1 == VOIDmode
10283 || REG_P (op0) || GET_CODE (op0) == SUBREG
10284 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10285 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10286 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10287 && modifier != EXPAND_CONST_ADDRESS
10288 && modifier != EXPAND_INITIALIZER
10289 && modifier != EXPAND_MEMORY)
10290 /* If the bitfield is volatile and the bitsize
10291 is narrower than the access size of the bitfield,
10292 we need to extract bitfields from the access. */
10293 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10294 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10295 && mode1 != BLKmode
10296 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10297 /* If the field isn't aligned enough to fetch as a memref,
10298 fetch it as a bit field. */
10299 || (mode1 != BLKmode
10300 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10301 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10302 || (MEM_P (op0)
10303 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10304 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10305 && modifier != EXPAND_MEMORY
10306 && ((modifier == EXPAND_CONST_ADDRESS
10307 || modifier == EXPAND_INITIALIZER)
10308 ? STRICT_ALIGNMENT
10309 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10310 || (bitpos % BITS_PER_UNIT != 0)))
10311 /* If the type and the field are a constant size and the
10312 size of the type isn't the same size as the bitfield,
10313 we must use bitfield operations. */
10314 || (bitsize >= 0
10315 && TYPE_SIZE (TREE_TYPE (exp))
10316 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10317 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10318 bitsize)))
10320 machine_mode ext_mode = mode;
10322 if (ext_mode == BLKmode
10323 && ! (target != 0 && MEM_P (op0)
10324 && MEM_P (target)
10325 && bitpos % BITS_PER_UNIT == 0))
10326 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10328 if (ext_mode == BLKmode)
10330 if (target == 0)
10331 target = assign_temp (type, 1, 1);
10333 /* ??? Unlike the similar test a few lines below, this one is
10334 very likely obsolete. */
10335 if (bitsize == 0)
10336 return target;
10338 /* In this case, BITPOS must start at a byte boundary and
10339 TARGET, if specified, must be a MEM. */
10340 gcc_assert (MEM_P (op0)
10341 && (!target || MEM_P (target))
10342 && !(bitpos % BITS_PER_UNIT));
10344 emit_block_move (target,
10345 adjust_address (op0, VOIDmode,
10346 bitpos / BITS_PER_UNIT),
10347 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10348 / BITS_PER_UNIT),
10349 (modifier == EXPAND_STACK_PARM
10350 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10352 return target;
10355 /* If we have nothing to extract, the result will be 0 for targets
10356 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10357 return 0 for the sake of consistency, as reading a zero-sized
10358 bitfield is valid in Ada and the value is fully specified. */
10359 if (bitsize == 0)
10360 return const0_rtx;
10362 op0 = validize_mem (op0);
10364 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10365 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10367 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10368 (modifier == EXPAND_STACK_PARM
10369 ? NULL_RTX : target),
10370 ext_mode, ext_mode);
10372 /* If the result is a record type and BITSIZE is narrower than
10373 the mode of OP0, an integral mode, and this is a big endian
10374 machine, we must put the field into the high-order bits. */
10375 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10376 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10377 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10378 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10379 GET_MODE_BITSIZE (GET_MODE (op0))
10380 - bitsize, op0, 1);
10382 /* If the result type is BLKmode, store the data into a temporary
10383 of the appropriate type, but with the mode corresponding to the
10384 mode for the data we have (op0's mode). */
10385 if (mode == BLKmode)
10387 rtx new_rtx
10388 = assign_stack_temp_for_type (ext_mode,
10389 GET_MODE_BITSIZE (ext_mode),
10390 type);
10391 emit_move_insn (new_rtx, op0);
10392 op0 = copy_rtx (new_rtx);
10393 PUT_MODE (op0, BLKmode);
10396 return op0;
10399 /* If the result is BLKmode, use that to access the object
10400 now as well. */
10401 if (mode == BLKmode)
10402 mode1 = BLKmode;
10404 /* Get a reference to just this component. */
10405 if (modifier == EXPAND_CONST_ADDRESS
10406 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10407 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10408 else
10409 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10411 if (op0 == orig_op0)
10412 op0 = copy_rtx (op0);
10414 /* If op0 is a temporary because of forcing to memory, pass only the
10415 type to set_mem_attributes so that the original expression is never
10416 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10417 if (mem_attrs_from_type)
10418 set_mem_attributes (op0, type, 0);
10419 else
10420 set_mem_attributes (op0, exp, 0);
10422 if (REG_P (XEXP (op0, 0)))
10423 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10425 MEM_VOLATILE_P (op0) |= volatilep;
10426 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10427 || modifier == EXPAND_CONST_ADDRESS
10428 || modifier == EXPAND_INITIALIZER)
10429 return op0;
10431 if (target == 0)
10432 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10434 convert_move (target, op0, unsignedp);
10435 return target;
10438 case OBJ_TYPE_REF:
10439 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10441 case CALL_EXPR:
10442 /* All valid uses of __builtin_va_arg_pack () are removed during
10443 inlining. */
10444 if (CALL_EXPR_VA_ARG_PACK (exp))
10445 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10447 tree fndecl = get_callee_fndecl (exp), attr;
10449 if (fndecl
10450 && (attr = lookup_attribute ("error",
10451 DECL_ATTRIBUTES (fndecl))) != NULL)
10452 error ("%Kcall to %qs declared with attribute error: %s",
10453 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10454 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10455 if (fndecl
10456 && (attr = lookup_attribute ("warning",
10457 DECL_ATTRIBUTES (fndecl))) != NULL)
10458 warning_at (tree_nonartificial_location (exp),
10459 0, "%Kcall to %qs declared with attribute warning: %s",
10460 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10461 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10463 /* Check for a built-in function. */
10464 if (fndecl && DECL_BUILT_IN (fndecl))
10466 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10467 if (CALL_WITH_BOUNDS_P (exp))
10468 return expand_builtin_with_bounds (exp, target, subtarget,
10469 tmode, ignore);
10470 else
10471 return expand_builtin (exp, target, subtarget, tmode, ignore);
10474 return expand_call (exp, target, ignore);
10476 case VIEW_CONVERT_EXPR:
10477 op0 = NULL_RTX;
10479 /* If we are converting to BLKmode, try to avoid an intermediate
10480 temporary by fetching an inner memory reference. */
10481 if (mode == BLKmode
10482 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10483 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10484 && handled_component_p (treeop0))
10486 machine_mode mode1;
10487 HOST_WIDE_INT bitsize, bitpos;
10488 tree offset;
10489 int unsignedp;
10490 int volatilep = 0;
10491 tree tem
10492 = get_inner_reference (treeop0, &bitsize, &bitpos,
10493 &offset, &mode1, &unsignedp, &volatilep,
10494 true);
10495 rtx orig_op0;
10497 /* ??? We should work harder and deal with non-zero offsets. */
10498 if (!offset
10499 && (bitpos % BITS_PER_UNIT) == 0
10500 && bitsize >= 0
10501 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10503 /* See the normal_inner_ref case for the rationale. */
10504 orig_op0
10505 = expand_expr_real (tem,
10506 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10507 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10508 != INTEGER_CST)
10509 && modifier != EXPAND_STACK_PARM
10510 ? target : NULL_RTX),
10511 VOIDmode,
10512 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10513 NULL, true);
10515 if (MEM_P (orig_op0))
10517 op0 = orig_op0;
10519 /* Get a reference to just this component. */
10520 if (modifier == EXPAND_CONST_ADDRESS
10521 || modifier == EXPAND_SUM
10522 || modifier == EXPAND_INITIALIZER)
10523 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10524 else
10525 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10527 if (op0 == orig_op0)
10528 op0 = copy_rtx (op0);
10530 set_mem_attributes (op0, treeop0, 0);
10531 if (REG_P (XEXP (op0, 0)))
10532 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10534 MEM_VOLATILE_P (op0) |= volatilep;
10539 if (!op0)
10540 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10541 NULL, inner_reference_p);
10543 /* If the input and output modes are both the same, we are done. */
10544 if (mode == GET_MODE (op0))
10546 /* If neither mode is BLKmode, and both modes are the same size
10547 then we can use gen_lowpart. */
10548 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10549 && (GET_MODE_PRECISION (mode)
10550 == GET_MODE_PRECISION (GET_MODE (op0)))
10551 && !COMPLEX_MODE_P (GET_MODE (op0)))
10553 if (GET_CODE (op0) == SUBREG)
10554 op0 = force_reg (GET_MODE (op0), op0);
10555 temp = gen_lowpart_common (mode, op0);
10556 if (temp)
10557 op0 = temp;
10558 else
10560 if (!REG_P (op0) && !MEM_P (op0))
10561 op0 = force_reg (GET_MODE (op0), op0);
10562 op0 = gen_lowpart (mode, op0);
10565 /* If both types are integral, convert from one mode to the other. */
10566 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10567 op0 = convert_modes (mode, GET_MODE (op0), op0,
10568 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10569 /* If the output type is a bit-field type, do an extraction. */
10570 else if (reduce_bit_field)
10571 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10572 TYPE_UNSIGNED (type), NULL_RTX,
10573 mode, mode);
10574 /* As a last resort, spill op0 to memory, and reload it in a
10575 different mode. */
10576 else if (!MEM_P (op0))
10578 /* If the operand is not a MEM, force it into memory. Since we
10579 are going to be changing the mode of the MEM, don't call
10580 force_const_mem for constants because we don't allow pool
10581 constants to change mode. */
10582 tree inner_type = TREE_TYPE (treeop0);
10584 gcc_assert (!TREE_ADDRESSABLE (exp));
10586 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10587 target
10588 = assign_stack_temp_for_type
10589 (TYPE_MODE (inner_type),
10590 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10592 emit_move_insn (target, op0);
10593 op0 = target;
10596 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10597 output type is such that the operand is known to be aligned, indicate
10598 that it is. Otherwise, we need only be concerned about alignment for
10599 non-BLKmode results. */
10600 if (MEM_P (op0))
10602 enum insn_code icode;
10604 if (TYPE_ALIGN_OK (type))
10606 /* ??? Copying the MEM without substantially changing it might
10607 run afoul of the code handling volatile memory references in
10608 store_expr, which assumes that TARGET is returned unmodified
10609 if it has been used. */
10610 op0 = copy_rtx (op0);
10611 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10613 else if (modifier != EXPAND_WRITE
10614 && modifier != EXPAND_MEMORY
10615 && !inner_reference_p
10616 && mode != BLKmode
10617 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10619 /* If the target does have special handling for unaligned
10620 loads of mode then use them. */
10621 if ((icode = optab_handler (movmisalign_optab, mode))
10622 != CODE_FOR_nothing)
10624 rtx reg, insn;
10626 op0 = adjust_address (op0, mode, 0);
10627 /* We've already validated the memory, and we're creating a
10628 new pseudo destination. The predicates really can't
10629 fail. */
10630 reg = gen_reg_rtx (mode);
10632 /* Nor can the insn generator. */
10633 insn = GEN_FCN (icode) (reg, op0);
10634 emit_insn (insn);
10635 return reg;
10637 else if (STRICT_ALIGNMENT)
10639 tree inner_type = TREE_TYPE (treeop0);
10640 HOST_WIDE_INT temp_size
10641 = MAX (int_size_in_bytes (inner_type),
10642 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10643 rtx new_rtx
10644 = assign_stack_temp_for_type (mode, temp_size, type);
10645 rtx new_with_op0_mode
10646 = adjust_address (new_rtx, GET_MODE (op0), 0);
10648 gcc_assert (!TREE_ADDRESSABLE (exp));
10650 if (GET_MODE (op0) == BLKmode)
10651 emit_block_move (new_with_op0_mode, op0,
10652 GEN_INT (GET_MODE_SIZE (mode)),
10653 (modifier == EXPAND_STACK_PARM
10654 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10655 else
10656 emit_move_insn (new_with_op0_mode, op0);
10658 op0 = new_rtx;
10662 op0 = adjust_address (op0, mode, 0);
10665 return op0;
10667 case MODIFY_EXPR:
10669 tree lhs = treeop0;
10670 tree rhs = treeop1;
10671 gcc_assert (ignore);
10673 /* Check for |= or &= of a bitfield of size one into another bitfield
10674 of size 1. In this case, (unless we need the result of the
10675 assignment) we can do this more efficiently with a
10676 test followed by an assignment, if necessary.
10678 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10679 things change so we do, this code should be enhanced to
10680 support it. */
10681 if (TREE_CODE (lhs) == COMPONENT_REF
10682 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10683 || TREE_CODE (rhs) == BIT_AND_EXPR)
10684 && TREE_OPERAND (rhs, 0) == lhs
10685 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10686 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10687 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10689 rtx_code_label *label = gen_label_rtx ();
10690 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10691 do_jump (TREE_OPERAND (rhs, 1),
10692 value ? label : 0,
10693 value ? 0 : label, -1);
10694 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10695 false);
10696 do_pending_stack_adjust ();
10697 emit_label (label);
10698 return const0_rtx;
10701 expand_assignment (lhs, rhs, false);
10702 return const0_rtx;
10705 case ADDR_EXPR:
10706 return expand_expr_addr_expr (exp, target, tmode, modifier);
10708 case REALPART_EXPR:
10709 op0 = expand_normal (treeop0);
10710 return read_complex_part (op0, false);
10712 case IMAGPART_EXPR:
10713 op0 = expand_normal (treeop0);
10714 return read_complex_part (op0, true);
10716 case RETURN_EXPR:
10717 case LABEL_EXPR:
10718 case GOTO_EXPR:
10719 case SWITCH_EXPR:
10720 case ASM_EXPR:
10721 /* Expanded in cfgexpand.c. */
10722 gcc_unreachable ();
10724 case TRY_CATCH_EXPR:
10725 case CATCH_EXPR:
10726 case EH_FILTER_EXPR:
10727 case TRY_FINALLY_EXPR:
10728 /* Lowered by tree-eh.c. */
10729 gcc_unreachable ();
10731 case WITH_CLEANUP_EXPR:
10732 case CLEANUP_POINT_EXPR:
10733 case TARGET_EXPR:
10734 case CASE_LABEL_EXPR:
10735 case VA_ARG_EXPR:
10736 case BIND_EXPR:
10737 case INIT_EXPR:
10738 case CONJ_EXPR:
10739 case COMPOUND_EXPR:
10740 case PREINCREMENT_EXPR:
10741 case PREDECREMENT_EXPR:
10742 case POSTINCREMENT_EXPR:
10743 case POSTDECREMENT_EXPR:
10744 case LOOP_EXPR:
10745 case EXIT_EXPR:
10746 case COMPOUND_LITERAL_EXPR:
10747 /* Lowered by gimplify.c. */
10748 gcc_unreachable ();
10750 case FDESC_EXPR:
10751 /* Function descriptors are not valid except for as
10752 initialization constants, and should not be expanded. */
10753 gcc_unreachable ();
10755 case WITH_SIZE_EXPR:
10756 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10757 have pulled out the size to use in whatever context it needed. */
10758 return expand_expr_real (treeop0, original_target, tmode,
10759 modifier, alt_rtl, inner_reference_p);
10761 default:
10762 return expand_expr_real_2 (&ops, target, tmode, modifier);
10766 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10767 signedness of TYPE), possibly returning the result in TARGET. */
10768 static rtx
10769 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10771 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10772 if (target && GET_MODE (target) != GET_MODE (exp))
10773 target = 0;
10774 /* For constant values, reduce using build_int_cst_type. */
10775 if (CONST_INT_P (exp))
10777 HOST_WIDE_INT value = INTVAL (exp);
10778 tree t = build_int_cst_type (type, value);
10779 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10781 else if (TYPE_UNSIGNED (type))
10783 machine_mode mode = GET_MODE (exp);
10784 rtx mask = immed_wide_int_const
10785 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10786 return expand_and (mode, exp, mask, target);
10788 else
10790 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10791 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10792 exp, count, target, 0);
10793 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10794 exp, count, target, 0);
10798 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10799 when applied to the address of EXP produces an address known to be
10800 aligned more than BIGGEST_ALIGNMENT. */
10802 static int
10803 is_aligning_offset (const_tree offset, const_tree exp)
10805 /* Strip off any conversions. */
10806 while (CONVERT_EXPR_P (offset))
10807 offset = TREE_OPERAND (offset, 0);
10809 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10810 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10811 if (TREE_CODE (offset) != BIT_AND_EXPR
10812 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10813 || compare_tree_int (TREE_OPERAND (offset, 1),
10814 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10815 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10816 return 0;
10818 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10819 It must be NEGATE_EXPR. Then strip any more conversions. */
10820 offset = TREE_OPERAND (offset, 0);
10821 while (CONVERT_EXPR_P (offset))
10822 offset = TREE_OPERAND (offset, 0);
10824 if (TREE_CODE (offset) != NEGATE_EXPR)
10825 return 0;
10827 offset = TREE_OPERAND (offset, 0);
10828 while (CONVERT_EXPR_P (offset))
10829 offset = TREE_OPERAND (offset, 0);
10831 /* This must now be the address of EXP. */
10832 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10835 /* Return the tree node if an ARG corresponds to a string constant or zero
10836 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10837 in bytes within the string that ARG is accessing. The type of the
10838 offset will be `sizetype'. */
10840 tree
10841 string_constant (tree arg, tree *ptr_offset)
10843 tree array, offset, lower_bound;
10844 STRIP_NOPS (arg);
10846 if (TREE_CODE (arg) == ADDR_EXPR)
10848 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10850 *ptr_offset = size_zero_node;
10851 return TREE_OPERAND (arg, 0);
10853 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10855 array = TREE_OPERAND (arg, 0);
10856 offset = size_zero_node;
10858 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10860 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10861 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10862 if (TREE_CODE (array) != STRING_CST
10863 && TREE_CODE (array) != VAR_DECL)
10864 return 0;
10866 /* Check if the array has a nonzero lower bound. */
10867 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10868 if (!integer_zerop (lower_bound))
10870 /* If the offset and base aren't both constants, return 0. */
10871 if (TREE_CODE (lower_bound) != INTEGER_CST)
10872 return 0;
10873 if (TREE_CODE (offset) != INTEGER_CST)
10874 return 0;
10875 /* Adjust offset by the lower bound. */
10876 offset = size_diffop (fold_convert (sizetype, offset),
10877 fold_convert (sizetype, lower_bound));
10880 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10882 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10883 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10884 if (TREE_CODE (array) != ADDR_EXPR)
10885 return 0;
10886 array = TREE_OPERAND (array, 0);
10887 if (TREE_CODE (array) != STRING_CST
10888 && TREE_CODE (array) != VAR_DECL)
10889 return 0;
10891 else
10892 return 0;
10894 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10896 tree arg0 = TREE_OPERAND (arg, 0);
10897 tree arg1 = TREE_OPERAND (arg, 1);
10899 STRIP_NOPS (arg0);
10900 STRIP_NOPS (arg1);
10902 if (TREE_CODE (arg0) == ADDR_EXPR
10903 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10904 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10906 array = TREE_OPERAND (arg0, 0);
10907 offset = arg1;
10909 else if (TREE_CODE (arg1) == ADDR_EXPR
10910 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10911 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10913 array = TREE_OPERAND (arg1, 0);
10914 offset = arg0;
10916 else
10917 return 0;
10919 else
10920 return 0;
10922 if (TREE_CODE (array) == STRING_CST)
10924 *ptr_offset = fold_convert (sizetype, offset);
10925 return array;
10927 else if (TREE_CODE (array) == VAR_DECL
10928 || TREE_CODE (array) == CONST_DECL)
10930 int length;
10931 tree init = ctor_for_folding (array);
10933 /* Variables initialized to string literals can be handled too. */
10934 if (init == error_mark_node
10935 || !init
10936 || TREE_CODE (init) != STRING_CST)
10937 return 0;
10939 /* Avoid const char foo[4] = "abcde"; */
10940 if (DECL_SIZE_UNIT (array) == NULL_TREE
10941 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10942 || (length = TREE_STRING_LENGTH (init)) <= 0
10943 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10944 return 0;
10946 /* If variable is bigger than the string literal, OFFSET must be constant
10947 and inside of the bounds of the string literal. */
10948 offset = fold_convert (sizetype, offset);
10949 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10950 && (! tree_fits_uhwi_p (offset)
10951 || compare_tree_int (offset, length) >= 0))
10952 return 0;
10954 *ptr_offset = offset;
10955 return init;
10958 return 0;
10961 /* Generate code to calculate OPS, and exploded expression
10962 using a store-flag instruction and return an rtx for the result.
10963 OPS reflects a comparison.
10965 If TARGET is nonzero, store the result there if convenient.
10967 Return zero if there is no suitable set-flag instruction
10968 available on this machine.
10970 Once expand_expr has been called on the arguments of the comparison,
10971 we are committed to doing the store flag, since it is not safe to
10972 re-evaluate the expression. We emit the store-flag insn by calling
10973 emit_store_flag, but only expand the arguments if we have a reason
10974 to believe that emit_store_flag will be successful. If we think that
10975 it will, but it isn't, we have to simulate the store-flag with a
10976 set/jump/set sequence. */
10978 static rtx
10979 do_store_flag (sepops ops, rtx target, machine_mode mode)
10981 enum rtx_code code;
10982 tree arg0, arg1, type;
10983 tree tem;
10984 machine_mode operand_mode;
10985 int unsignedp;
10986 rtx op0, op1;
10987 rtx subtarget = target;
10988 location_t loc = ops->location;
10990 arg0 = ops->op0;
10991 arg1 = ops->op1;
10993 /* Don't crash if the comparison was erroneous. */
10994 if (arg0 == error_mark_node || arg1 == error_mark_node)
10995 return const0_rtx;
10997 type = TREE_TYPE (arg0);
10998 operand_mode = TYPE_MODE (type);
10999 unsignedp = TYPE_UNSIGNED (type);
11001 /* We won't bother with BLKmode store-flag operations because it would mean
11002 passing a lot of information to emit_store_flag. */
11003 if (operand_mode == BLKmode)
11004 return 0;
11006 /* We won't bother with store-flag operations involving function pointers
11007 when function pointers must be canonicalized before comparisons. */
11008 #ifdef HAVE_canonicalize_funcptr_for_compare
11009 if (HAVE_canonicalize_funcptr_for_compare
11010 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11011 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11012 == FUNCTION_TYPE))
11013 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11014 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11015 == FUNCTION_TYPE))))
11016 return 0;
11017 #endif
11019 STRIP_NOPS (arg0);
11020 STRIP_NOPS (arg1);
11022 /* For vector typed comparisons emit code to generate the desired
11023 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11024 expander for this. */
11025 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11027 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11028 tree if_true = constant_boolean_node (true, ops->type);
11029 tree if_false = constant_boolean_node (false, ops->type);
11030 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
11033 /* Get the rtx comparison code to use. We know that EXP is a comparison
11034 operation of some type. Some comparisons against 1 and -1 can be
11035 converted to comparisons with zero. Do so here so that the tests
11036 below will be aware that we have a comparison with zero. These
11037 tests will not catch constants in the first operand, but constants
11038 are rarely passed as the first operand. */
11040 switch (ops->code)
11042 case EQ_EXPR:
11043 code = EQ;
11044 break;
11045 case NE_EXPR:
11046 code = NE;
11047 break;
11048 case LT_EXPR:
11049 if (integer_onep (arg1))
11050 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11051 else
11052 code = unsignedp ? LTU : LT;
11053 break;
11054 case LE_EXPR:
11055 if (! unsignedp && integer_all_onesp (arg1))
11056 arg1 = integer_zero_node, code = LT;
11057 else
11058 code = unsignedp ? LEU : LE;
11059 break;
11060 case GT_EXPR:
11061 if (! unsignedp && integer_all_onesp (arg1))
11062 arg1 = integer_zero_node, code = GE;
11063 else
11064 code = unsignedp ? GTU : GT;
11065 break;
11066 case GE_EXPR:
11067 if (integer_onep (arg1))
11068 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11069 else
11070 code = unsignedp ? GEU : GE;
11071 break;
11073 case UNORDERED_EXPR:
11074 code = UNORDERED;
11075 break;
11076 case ORDERED_EXPR:
11077 code = ORDERED;
11078 break;
11079 case UNLT_EXPR:
11080 code = UNLT;
11081 break;
11082 case UNLE_EXPR:
11083 code = UNLE;
11084 break;
11085 case UNGT_EXPR:
11086 code = UNGT;
11087 break;
11088 case UNGE_EXPR:
11089 code = UNGE;
11090 break;
11091 case UNEQ_EXPR:
11092 code = UNEQ;
11093 break;
11094 case LTGT_EXPR:
11095 code = LTGT;
11096 break;
11098 default:
11099 gcc_unreachable ();
11102 /* Put a constant second. */
11103 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11104 || TREE_CODE (arg0) == FIXED_CST)
11106 tem = arg0; arg0 = arg1; arg1 = tem;
11107 code = swap_condition (code);
11110 /* If this is an equality or inequality test of a single bit, we can
11111 do this by shifting the bit being tested to the low-order bit and
11112 masking the result with the constant 1. If the condition was EQ,
11113 we xor it with 1. This does not require an scc insn and is faster
11114 than an scc insn even if we have it.
11116 The code to make this transformation was moved into fold_single_bit_test,
11117 so we just call into the folder and expand its result. */
11119 if ((code == NE || code == EQ)
11120 && integer_zerop (arg1)
11121 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11123 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11124 if (srcstmt
11125 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11127 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11128 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11129 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11130 gimple_assign_rhs1 (srcstmt),
11131 gimple_assign_rhs2 (srcstmt));
11132 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11133 if (temp)
11134 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11138 if (! get_subtarget (target)
11139 || GET_MODE (subtarget) != operand_mode)
11140 subtarget = 0;
11142 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11144 if (target == 0)
11145 target = gen_reg_rtx (mode);
11147 /* Try a cstore if possible. */
11148 return emit_store_flag_force (target, code, op0, op1,
11149 operand_mode, unsignedp,
11150 (TYPE_PRECISION (ops->type) == 1
11151 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11155 /* Stubs in case we haven't got a casesi insn. */
11156 #ifndef HAVE_casesi
11157 # define HAVE_casesi 0
11158 # define gen_casesi(a, b, c, d, e) (0)
11159 # define CODE_FOR_casesi CODE_FOR_nothing
11160 #endif
11162 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11163 0 otherwise (i.e. if there is no casesi instruction).
11165 DEFAULT_PROBABILITY is the probability of jumping to the default
11166 label. */
11168 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11169 rtx table_label, rtx default_label, rtx fallback_label,
11170 int default_probability)
11172 struct expand_operand ops[5];
11173 machine_mode index_mode = SImode;
11174 rtx op1, op2, index;
11176 if (! HAVE_casesi)
11177 return 0;
11179 /* Convert the index to SImode. */
11180 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11182 machine_mode omode = TYPE_MODE (index_type);
11183 rtx rangertx = expand_normal (range);
11185 /* We must handle the endpoints in the original mode. */
11186 index_expr = build2 (MINUS_EXPR, index_type,
11187 index_expr, minval);
11188 minval = integer_zero_node;
11189 index = expand_normal (index_expr);
11190 if (default_label)
11191 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11192 omode, 1, default_label,
11193 default_probability);
11194 /* Now we can safely truncate. */
11195 index = convert_to_mode (index_mode, index, 0);
11197 else
11199 if (TYPE_MODE (index_type) != index_mode)
11201 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11202 index_expr = fold_convert (index_type, index_expr);
11205 index = expand_normal (index_expr);
11208 do_pending_stack_adjust ();
11210 op1 = expand_normal (minval);
11211 op2 = expand_normal (range);
11213 create_input_operand (&ops[0], index, index_mode);
11214 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11215 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11216 create_fixed_operand (&ops[3], table_label);
11217 create_fixed_operand (&ops[4], (default_label
11218 ? default_label
11219 : fallback_label));
11220 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11221 return 1;
11224 /* Attempt to generate a tablejump instruction; same concept. */
11225 #ifndef HAVE_tablejump
11226 #define HAVE_tablejump 0
11227 #define gen_tablejump(x, y) (0)
11228 #endif
11230 /* Subroutine of the next function.
11232 INDEX is the value being switched on, with the lowest value
11233 in the table already subtracted.
11234 MODE is its expected mode (needed if INDEX is constant).
11235 RANGE is the length of the jump table.
11236 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11238 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11239 index value is out of range.
11240 DEFAULT_PROBABILITY is the probability of jumping to
11241 the default label. */
11243 static void
11244 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11245 rtx default_label, int default_probability)
11247 rtx temp, vector;
11249 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11250 cfun->cfg->max_jumptable_ents = INTVAL (range);
11252 /* Do an unsigned comparison (in the proper mode) between the index
11253 expression and the value which represents the length of the range.
11254 Since we just finished subtracting the lower bound of the range
11255 from the index expression, this comparison allows us to simultaneously
11256 check that the original index expression value is both greater than
11257 or equal to the minimum value of the range and less than or equal to
11258 the maximum value of the range. */
11260 if (default_label)
11261 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11262 default_label, default_probability);
11265 /* If index is in range, it must fit in Pmode.
11266 Convert to Pmode so we can index with it. */
11267 if (mode != Pmode)
11268 index = convert_to_mode (Pmode, index, 1);
11270 /* Don't let a MEM slip through, because then INDEX that comes
11271 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11272 and break_out_memory_refs will go to work on it and mess it up. */
11273 #ifdef PIC_CASE_VECTOR_ADDRESS
11274 if (flag_pic && !REG_P (index))
11275 index = copy_to_mode_reg (Pmode, index);
11276 #endif
11278 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11279 GET_MODE_SIZE, because this indicates how large insns are. The other
11280 uses should all be Pmode, because they are addresses. This code
11281 could fail if addresses and insns are not the same size. */
11282 index = simplify_gen_binary (MULT, Pmode, index,
11283 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11284 Pmode));
11285 index = simplify_gen_binary (PLUS, Pmode, index,
11286 gen_rtx_LABEL_REF (Pmode, table_label));
11288 #ifdef PIC_CASE_VECTOR_ADDRESS
11289 if (flag_pic)
11290 index = PIC_CASE_VECTOR_ADDRESS (index);
11291 else
11292 #endif
11293 index = memory_address (CASE_VECTOR_MODE, index);
11294 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11295 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11296 convert_move (temp, vector, 0);
11298 emit_jump_insn (gen_tablejump (temp, table_label));
11300 /* If we are generating PIC code or if the table is PC-relative, the
11301 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11302 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11303 emit_barrier ();
11307 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11308 rtx table_label, rtx default_label, int default_probability)
11310 rtx index;
11312 if (! HAVE_tablejump)
11313 return 0;
11315 index_expr = fold_build2 (MINUS_EXPR, index_type,
11316 fold_convert (index_type, index_expr),
11317 fold_convert (index_type, minval));
11318 index = expand_normal (index_expr);
11319 do_pending_stack_adjust ();
11321 do_tablejump (index, TYPE_MODE (index_type),
11322 convert_modes (TYPE_MODE (index_type),
11323 TYPE_MODE (TREE_TYPE (range)),
11324 expand_normal (range),
11325 TYPE_UNSIGNED (TREE_TYPE (range))),
11326 table_label, default_label, default_probability);
11327 return 1;
11330 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11331 static rtx
11332 const_vector_from_tree (tree exp)
11334 rtvec v;
11335 unsigned i;
11336 int units;
11337 tree elt;
11338 machine_mode inner, mode;
11340 mode = TYPE_MODE (TREE_TYPE (exp));
11342 if (initializer_zerop (exp))
11343 return CONST0_RTX (mode);
11345 units = GET_MODE_NUNITS (mode);
11346 inner = GET_MODE_INNER (mode);
11348 v = rtvec_alloc (units);
11350 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11352 elt = VECTOR_CST_ELT (exp, i);
11354 if (TREE_CODE (elt) == REAL_CST)
11355 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11356 inner);
11357 else if (TREE_CODE (elt) == FIXED_CST)
11358 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11359 inner);
11360 else
11361 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11364 return gen_rtx_CONST_VECTOR (mode, v);
11367 /* Build a decl for a personality function given a language prefix. */
11369 tree
11370 build_personality_function (const char *lang)
11372 const char *unwind_and_version;
11373 tree decl, type;
11374 char *name;
11376 switch (targetm_common.except_unwind_info (&global_options))
11378 case UI_NONE:
11379 return NULL;
11380 case UI_SJLJ:
11381 unwind_and_version = "_sj0";
11382 break;
11383 case UI_DWARF2:
11384 case UI_TARGET:
11385 unwind_and_version = "_v0";
11386 break;
11387 case UI_SEH:
11388 unwind_and_version = "_seh0";
11389 break;
11390 default:
11391 gcc_unreachable ();
11394 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11396 type = build_function_type_list (integer_type_node, integer_type_node,
11397 long_long_unsigned_type_node,
11398 ptr_type_node, ptr_type_node, NULL_TREE);
11399 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11400 get_identifier (name), type);
11401 DECL_ARTIFICIAL (decl) = 1;
11402 DECL_EXTERNAL (decl) = 1;
11403 TREE_PUBLIC (decl) = 1;
11405 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11406 are the flags assigned by targetm.encode_section_info. */
11407 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11409 return decl;
11412 /* Extracts the personality function of DECL and returns the corresponding
11413 libfunc. */
11416 get_personality_function (tree decl)
11418 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11419 enum eh_personality_kind pk;
11421 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11422 if (pk == eh_personality_none)
11423 return NULL;
11425 if (!personality
11426 && pk == eh_personality_any)
11427 personality = lang_hooks.eh_personality ();
11429 if (pk == eh_personality_lang)
11430 gcc_assert (personality != NULL_TREE);
11432 return XEXP (DECL_RTL (personality), 0);
11435 #include "gt-expr.h"