2015-05-22 Pascal Obry <obry@adacore.com>
[official-gcc.git] / gcc / expr.c
blob3605e9942897980d63a65ab1de1fbcf1a6beac68
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "hash-set.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stringpool.h"
37 #include "stor-layout.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "flags.h"
41 #include "regs.h"
42 #include "hard-reg-set.h"
43 #include "except.h"
44 #include "function.h"
45 #include "insn-config.h"
46 #include "insn-attr.h"
47 #include "hashtab.h"
48 #include "statistics.h"
49 #include "real.h"
50 #include "fixed-value.h"
51 #include "expmed.h"
52 #include "dojump.h"
53 #include "explow.h"
54 #include "calls.h"
55 #include "emit-rtl.h"
56 #include "stmt.h"
57 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
58 #include "expr.h"
59 #include "insn-codes.h"
60 #include "optabs.h"
61 #include "libfuncs.h"
62 #include "recog.h"
63 #include "reload.h"
64 #include "typeclass.h"
65 #include "toplev.h"
66 #include "langhooks.h"
67 #include "intl.h"
68 #include "tm_p.h"
69 #include "tree-iterator.h"
70 #include "predict.h"
71 #include "dominance.h"
72 #include "cfg.h"
73 #include "basic-block.h"
74 #include "tree-ssa-alias.h"
75 #include "internal-fn.h"
76 #include "gimple-expr.h"
77 #include "is-a.h"
78 #include "gimple.h"
79 #include "gimple-ssa.h"
80 #include "hash-map.h"
81 #include "plugin-api.h"
82 #include "ipa-ref.h"
83 #include "cgraph.h"
84 #include "tree-ssanames.h"
85 #include "target.h"
86 #include "common/common-target.h"
87 #include "timevar.h"
88 #include "df.h"
89 #include "diagnostic.h"
90 #include "tree-ssa-live.h"
91 #include "tree-outof-ssa.h"
92 #include "target-globals.h"
93 #include "params.h"
94 #include "tree-ssa-address.h"
95 #include "cfgexpand.h"
96 #include "builtins.h"
97 #include "tree-chkp.h"
98 #include "rtl-chkp.h"
99 #include "ccmp.h"
102 /* If this is nonzero, we do not bother generating VOLATILE
103 around volatile memory references, and we are willing to
104 output indirect addresses. If cse is to follow, we reject
105 indirect addresses so a useful potential cse is generated;
106 if it is used only once, instruction combination will produce
107 the same indirect address eventually. */
108 int cse_not_expected;
110 /* This structure is used by move_by_pieces to describe the move to
111 be performed. */
112 struct move_by_pieces_d
114 rtx to;
115 rtx to_addr;
116 int autinc_to;
117 int explicit_inc_to;
118 rtx from;
119 rtx from_addr;
120 int autinc_from;
121 int explicit_inc_from;
122 unsigned HOST_WIDE_INT len;
123 HOST_WIDE_INT offset;
124 int reverse;
127 /* This structure is used by store_by_pieces to describe the clear to
128 be performed. */
130 struct store_by_pieces_d
132 rtx to;
133 rtx to_addr;
134 int autinc_to;
135 int explicit_inc_to;
136 unsigned HOST_WIDE_INT len;
137 HOST_WIDE_INT offset;
138 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode);
139 void *constfundata;
140 int reverse;
143 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
144 struct move_by_pieces_d *);
145 static bool block_move_libcall_safe_for_call_parm (void);
146 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
147 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
148 unsigned HOST_WIDE_INT);
149 static tree emit_block_move_libcall_fn (int);
150 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
151 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode);
152 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
153 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
154 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
155 struct store_by_pieces_d *);
156 static tree clear_storage_libcall_fn (int);
157 static rtx_insn *compress_float_constant (rtx, rtx);
158 static rtx get_subtarget (rtx);
159 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
160 HOST_WIDE_INT, machine_mode,
161 tree, int, alias_set_type);
162 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
163 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
164 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
165 machine_mode, tree, alias_set_type, bool);
167 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
169 static int is_aligning_offset (const_tree, const_tree);
170 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
171 static rtx do_store_flag (sepops, rtx, machine_mode);
172 #ifdef PUSH_ROUNDING
173 static void emit_single_push_insn (machine_mode, rtx, tree);
174 #endif
175 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
176 static rtx const_vector_from_tree (tree);
177 static tree tree_expr_size (const_tree);
178 static HOST_WIDE_INT int_expr_size (tree);
181 /* This is run to set up which modes can be used
182 directly in memory and to initialize the block move optab. It is run
183 at the beginning of compilation and when the target is reinitialized. */
185 void
186 init_expr_target (void)
188 rtx insn, pat;
189 machine_mode mode;
190 int num_clobbers;
191 rtx mem, mem1;
192 rtx reg;
194 /* Try indexing by frame ptr and try by stack ptr.
195 It is known that on the Convex the stack ptr isn't a valid index.
196 With luck, one or the other is valid on any machine. */
197 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
198 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
200 /* A scratch register we can modify in-place below to avoid
201 useless RTL allocations. */
202 reg = gen_rtx_REG (word_mode, FIRST_PSEUDO_REGISTER);
204 insn = rtx_alloc (INSN);
205 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
206 PATTERN (insn) = pat;
208 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
209 mode = (machine_mode) ((int) mode + 1))
211 int regno;
213 direct_load[(int) mode] = direct_store[(int) mode] = 0;
214 PUT_MODE (mem, mode);
215 PUT_MODE (mem1, mode);
217 /* See if there is some register that can be used in this mode and
218 directly loaded or stored from memory. */
220 if (mode != VOIDmode && mode != BLKmode)
221 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
222 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
223 regno++)
225 if (! HARD_REGNO_MODE_OK (regno, mode))
226 continue;
228 set_mode_and_regno (reg, mode, regno);
230 SET_SRC (pat) = mem;
231 SET_DEST (pat) = reg;
232 if (recog (pat, insn, &num_clobbers) >= 0)
233 direct_load[(int) mode] = 1;
235 SET_SRC (pat) = mem1;
236 SET_DEST (pat) = reg;
237 if (recog (pat, insn, &num_clobbers) >= 0)
238 direct_load[(int) mode] = 1;
240 SET_SRC (pat) = reg;
241 SET_DEST (pat) = mem;
242 if (recog (pat, insn, &num_clobbers) >= 0)
243 direct_store[(int) mode] = 1;
245 SET_SRC (pat) = reg;
246 SET_DEST (pat) = mem1;
247 if (recog (pat, insn, &num_clobbers) >= 0)
248 direct_store[(int) mode] = 1;
252 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER));
254 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
255 mode = GET_MODE_WIDER_MODE (mode))
257 machine_mode srcmode;
258 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
259 srcmode = GET_MODE_WIDER_MODE (srcmode))
261 enum insn_code ic;
263 ic = can_extend_p (mode, srcmode, 0);
264 if (ic == CODE_FOR_nothing)
265 continue;
267 PUT_MODE (mem, srcmode);
269 if (insn_operand_matches (ic, 1, mem))
270 float_extend_from_mem[mode][srcmode] = true;
275 /* This is run at the start of compiling a function. */
277 void
278 init_expr (void)
280 memset (&crtl->expr, 0, sizeof (crtl->expr));
283 /* Copy data from FROM to TO, where the machine modes are not the same.
284 Both modes may be integer, or both may be floating, or both may be
285 fixed-point.
286 UNSIGNEDP should be nonzero if FROM is an unsigned type.
287 This causes zero-extension instead of sign-extension. */
289 void
290 convert_move (rtx to, rtx from, int unsignedp)
292 machine_mode to_mode = GET_MODE (to);
293 machine_mode from_mode = GET_MODE (from);
294 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
295 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
296 enum insn_code code;
297 rtx libcall;
299 /* rtx code for making an equivalent value. */
300 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
301 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
304 gcc_assert (to_real == from_real);
305 gcc_assert (to_mode != BLKmode);
306 gcc_assert (from_mode != BLKmode);
308 /* If the source and destination are already the same, then there's
309 nothing to do. */
310 if (to == from)
311 return;
313 /* If FROM is a SUBREG that indicates that we have already done at least
314 the required extension, strip it. We don't handle such SUBREGs as
315 TO here. */
317 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
318 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
319 >= GET_MODE_PRECISION (to_mode))
320 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
321 from = gen_lowpart (to_mode, from), from_mode = to_mode;
323 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
325 if (to_mode == from_mode
326 || (from_mode == VOIDmode && CONSTANT_P (from)))
328 emit_move_insn (to, from);
329 return;
332 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
334 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
336 if (VECTOR_MODE_P (to_mode))
337 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
338 else
339 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
341 emit_move_insn (to, from);
342 return;
345 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
347 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
348 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
349 return;
352 if (to_real)
354 rtx value;
355 rtx_insn *insns;
356 convert_optab tab;
358 gcc_assert ((GET_MODE_PRECISION (from_mode)
359 != GET_MODE_PRECISION (to_mode))
360 || (DECIMAL_FLOAT_MODE_P (from_mode)
361 != DECIMAL_FLOAT_MODE_P (to_mode)));
363 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
364 /* Conversion between decimal float and binary float, same size. */
365 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
366 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
367 tab = sext_optab;
368 else
369 tab = trunc_optab;
371 /* Try converting directly if the insn is supported. */
373 code = convert_optab_handler (tab, to_mode, from_mode);
374 if (code != CODE_FOR_nothing)
376 emit_unop_insn (code, to, from,
377 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
378 return;
381 /* Otherwise use a libcall. */
382 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
384 /* Is this conversion implemented yet? */
385 gcc_assert (libcall);
387 start_sequence ();
388 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
389 1, from, from_mode);
390 insns = get_insns ();
391 end_sequence ();
392 emit_libcall_block (insns, to, value,
393 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
394 from)
395 : gen_rtx_FLOAT_EXTEND (to_mode, from));
396 return;
399 /* Handle pointer conversion. */ /* SPEE 900220. */
400 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
402 convert_optab ctab;
404 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
405 ctab = trunc_optab;
406 else if (unsignedp)
407 ctab = zext_optab;
408 else
409 ctab = sext_optab;
411 if (convert_optab_handler (ctab, to_mode, from_mode)
412 != CODE_FOR_nothing)
414 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
415 to, from, UNKNOWN);
416 return;
420 /* Targets are expected to provide conversion insns between PxImode and
421 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
422 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
424 machine_mode full_mode
425 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
427 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
428 != CODE_FOR_nothing);
430 if (full_mode != from_mode)
431 from = convert_to_mode (full_mode, from, unsignedp);
432 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
433 to, from, UNKNOWN);
434 return;
436 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
438 rtx new_from;
439 machine_mode full_mode
440 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
441 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
442 enum insn_code icode;
444 icode = convert_optab_handler (ctab, full_mode, from_mode);
445 gcc_assert (icode != CODE_FOR_nothing);
447 if (to_mode == full_mode)
449 emit_unop_insn (icode, to, from, UNKNOWN);
450 return;
453 new_from = gen_reg_rtx (full_mode);
454 emit_unop_insn (icode, new_from, from, UNKNOWN);
456 /* else proceed to integer conversions below. */
457 from_mode = full_mode;
458 from = new_from;
461 /* Make sure both are fixed-point modes or both are not. */
462 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
463 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
464 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
466 /* If we widen from_mode to to_mode and they are in the same class,
467 we won't saturate the result.
468 Otherwise, always saturate the result to play safe. */
469 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
470 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
471 expand_fixed_convert (to, from, 0, 0);
472 else
473 expand_fixed_convert (to, from, 0, 1);
474 return;
477 /* Now both modes are integers. */
479 /* Handle expanding beyond a word. */
480 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
481 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
483 rtx_insn *insns;
484 rtx lowpart;
485 rtx fill_value;
486 rtx lowfrom;
487 int i;
488 machine_mode lowpart_mode;
489 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
491 /* Try converting directly if the insn is supported. */
492 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
493 != CODE_FOR_nothing)
495 /* If FROM is a SUBREG, put it into a register. Do this
496 so that we always generate the same set of insns for
497 better cse'ing; if an intermediate assignment occurred,
498 we won't be doing the operation directly on the SUBREG. */
499 if (optimize > 0 && GET_CODE (from) == SUBREG)
500 from = force_reg (from_mode, from);
501 emit_unop_insn (code, to, from, equiv_code);
502 return;
504 /* Next, try converting via full word. */
505 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
506 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
507 != CODE_FOR_nothing))
509 rtx word_to = gen_reg_rtx (word_mode);
510 if (REG_P (to))
512 if (reg_overlap_mentioned_p (to, from))
513 from = force_reg (from_mode, from);
514 emit_clobber (to);
516 convert_move (word_to, from, unsignedp);
517 emit_unop_insn (code, to, word_to, equiv_code);
518 return;
521 /* No special multiword conversion insn; do it by hand. */
522 start_sequence ();
524 /* Since we will turn this into a no conflict block, we must ensure the
525 the source does not overlap the target so force it into an isolated
526 register when maybe so. Likewise for any MEM input, since the
527 conversion sequence might require several references to it and we
528 must ensure we're getting the same value every time. */
530 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
531 from = force_reg (from_mode, from);
533 /* Get a copy of FROM widened to a word, if necessary. */
534 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
535 lowpart_mode = word_mode;
536 else
537 lowpart_mode = from_mode;
539 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
541 lowpart = gen_lowpart (lowpart_mode, to);
542 emit_move_insn (lowpart, lowfrom);
544 /* Compute the value to put in each remaining word. */
545 if (unsignedp)
546 fill_value = const0_rtx;
547 else
548 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
549 LT, lowfrom, const0_rtx,
550 lowpart_mode, 0, -1);
552 /* Fill the remaining words. */
553 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
555 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
556 rtx subword = operand_subword (to, index, 1, to_mode);
558 gcc_assert (subword);
560 if (fill_value != subword)
561 emit_move_insn (subword, fill_value);
564 insns = get_insns ();
565 end_sequence ();
567 emit_insn (insns);
568 return;
571 /* Truncating multi-word to a word or less. */
572 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
573 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
575 if (!((MEM_P (from)
576 && ! MEM_VOLATILE_P (from)
577 && direct_load[(int) to_mode]
578 && ! mode_dependent_address_p (XEXP (from, 0),
579 MEM_ADDR_SPACE (from)))
580 || REG_P (from)
581 || GET_CODE (from) == SUBREG))
582 from = force_reg (from_mode, from);
583 convert_move (to, gen_lowpart (word_mode, from), 0);
584 return;
587 /* Now follow all the conversions between integers
588 no more than a word long. */
590 /* For truncation, usually we can just refer to FROM in a narrower mode. */
591 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
592 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
594 if (!((MEM_P (from)
595 && ! MEM_VOLATILE_P (from)
596 && direct_load[(int) to_mode]
597 && ! mode_dependent_address_p (XEXP (from, 0),
598 MEM_ADDR_SPACE (from)))
599 || REG_P (from)
600 || GET_CODE (from) == SUBREG))
601 from = force_reg (from_mode, from);
602 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
603 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
604 from = copy_to_reg (from);
605 emit_move_insn (to, gen_lowpart (to_mode, from));
606 return;
609 /* Handle extension. */
610 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
612 /* Convert directly if that works. */
613 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
614 != CODE_FOR_nothing)
616 emit_unop_insn (code, to, from, equiv_code);
617 return;
619 else
621 machine_mode intermediate;
622 rtx tmp;
623 int shift_amount;
625 /* Search for a mode to convert via. */
626 for (intermediate = from_mode; intermediate != VOIDmode;
627 intermediate = GET_MODE_WIDER_MODE (intermediate))
628 if (((can_extend_p (to_mode, intermediate, unsignedp)
629 != CODE_FOR_nothing)
630 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
631 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
632 && (can_extend_p (intermediate, from_mode, unsignedp)
633 != CODE_FOR_nothing))
635 convert_move (to, convert_to_mode (intermediate, from,
636 unsignedp), unsignedp);
637 return;
640 /* No suitable intermediate mode.
641 Generate what we need with shifts. */
642 shift_amount = (GET_MODE_PRECISION (to_mode)
643 - GET_MODE_PRECISION (from_mode));
644 from = gen_lowpart (to_mode, force_reg (from_mode, from));
645 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
646 to, unsignedp);
647 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
648 to, unsignedp);
649 if (tmp != to)
650 emit_move_insn (to, tmp);
651 return;
655 /* Support special truncate insns for certain modes. */
656 if (convert_optab_handler (trunc_optab, to_mode,
657 from_mode) != CODE_FOR_nothing)
659 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
660 to, from, UNKNOWN);
661 return;
664 /* Handle truncation of volatile memrefs, and so on;
665 the things that couldn't be truncated directly,
666 and for which there was no special instruction.
668 ??? Code above formerly short-circuited this, for most integer
669 mode pairs, with a force_reg in from_mode followed by a recursive
670 call to this routine. Appears always to have been wrong. */
671 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
673 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
674 emit_move_insn (to, temp);
675 return;
678 /* Mode combination is not recognized. */
679 gcc_unreachable ();
682 /* Return an rtx for a value that would result
683 from converting X to mode MODE.
684 Both X and MODE may be floating, or both integer.
685 UNSIGNEDP is nonzero if X is an unsigned value.
686 This can be done by referring to a part of X in place
687 or by copying to a new temporary with conversion. */
690 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
692 return convert_modes (mode, VOIDmode, x, unsignedp);
695 /* Return an rtx for a value that would result
696 from converting X from mode OLDMODE to mode MODE.
697 Both modes may be floating, or both integer.
698 UNSIGNEDP is nonzero if X is an unsigned value.
700 This can be done by referring to a part of X in place
701 or by copying to a new temporary with conversion.
703 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
706 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
708 rtx temp;
710 /* If FROM is a SUBREG that indicates that we have already done at least
711 the required extension, strip it. */
713 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
714 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
715 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
716 x = gen_lowpart (mode, SUBREG_REG (x));
718 if (GET_MODE (x) != VOIDmode)
719 oldmode = GET_MODE (x);
721 if (mode == oldmode)
722 return x;
724 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
726 /* If the caller did not tell us the old mode, then there is not
727 much to do with respect to canonicalization. We have to
728 assume that all the bits are significant. */
729 if (GET_MODE_CLASS (oldmode) != MODE_INT)
730 oldmode = MAX_MODE_INT;
731 wide_int w = wide_int::from (std::make_pair (x, oldmode),
732 GET_MODE_PRECISION (mode),
733 unsignedp ? UNSIGNED : SIGNED);
734 return immed_wide_int_const (w, mode);
737 /* We can do this with a gen_lowpart if both desired and current modes
738 are integer, and this is either a constant integer, a register, or a
739 non-volatile MEM. */
740 if (GET_MODE_CLASS (mode) == MODE_INT
741 && GET_MODE_CLASS (oldmode) == MODE_INT
742 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
743 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
744 || (REG_P (x)
745 && (!HARD_REGISTER_P (x)
746 || HARD_REGNO_MODE_OK (REGNO (x), mode))
747 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
749 return gen_lowpart (mode, x);
751 /* Converting from integer constant into mode is always equivalent to an
752 subreg operation. */
753 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
755 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
756 return simplify_gen_subreg (mode, x, oldmode, 0);
759 temp = gen_reg_rtx (mode);
760 convert_move (temp, x, unsignedp);
761 return temp;
764 /* Return the largest alignment we can use for doing a move (or store)
765 of MAX_PIECES. ALIGN is the largest alignment we could use. */
767 static unsigned int
768 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
770 machine_mode tmode;
772 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
773 if (align >= GET_MODE_ALIGNMENT (tmode))
774 align = GET_MODE_ALIGNMENT (tmode);
775 else
777 machine_mode tmode, xmode;
779 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
780 tmode != VOIDmode;
781 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
782 if (GET_MODE_SIZE (tmode) > max_pieces
783 || SLOW_UNALIGNED_ACCESS (tmode, align))
784 break;
786 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
789 return align;
792 /* Return the widest integer mode no wider than SIZE. If no such mode
793 can be found, return VOIDmode. */
795 static machine_mode
796 widest_int_mode_for_size (unsigned int size)
798 machine_mode tmode, mode = VOIDmode;
800 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
801 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
802 if (GET_MODE_SIZE (tmode) < size)
803 mode = tmode;
805 return mode;
808 /* Determine whether the LEN bytes can be moved by using several move
809 instructions. Return nonzero if a call to move_by_pieces should
810 succeed. */
813 can_move_by_pieces (unsigned HOST_WIDE_INT len,
814 unsigned int align)
816 return targetm.use_by_pieces_infrastructure_p (len, align, MOVE_BY_PIECES,
817 optimize_insn_for_speed_p ());
820 /* Generate several move instructions to copy LEN bytes from block FROM to
821 block TO. (These are MEM rtx's with BLKmode).
823 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
824 used to push FROM to the stack.
826 ALIGN is maximum stack alignment we can assume.
828 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
829 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
830 stpcpy. */
833 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
834 unsigned int align, int endp)
836 struct move_by_pieces_d data;
837 machine_mode to_addr_mode;
838 machine_mode from_addr_mode = get_address_mode (from);
839 rtx to_addr, from_addr = XEXP (from, 0);
840 unsigned int max_size = MOVE_MAX_PIECES + 1;
841 enum insn_code icode;
843 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
845 data.offset = 0;
846 data.from_addr = from_addr;
847 if (to)
849 to_addr_mode = get_address_mode (to);
850 to_addr = XEXP (to, 0);
851 data.to = to;
852 data.autinc_to
853 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
854 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
855 data.reverse
856 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
858 else
860 to_addr_mode = VOIDmode;
861 to_addr = NULL_RTX;
862 data.to = NULL_RTX;
863 data.autinc_to = 1;
864 if (STACK_GROWS_DOWNWARD)
865 data.reverse = 1;
866 else
867 data.reverse = 0;
869 data.to_addr = to_addr;
870 data.from = from;
871 data.autinc_from
872 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
873 || GET_CODE (from_addr) == POST_INC
874 || GET_CODE (from_addr) == POST_DEC);
876 data.explicit_inc_from = 0;
877 data.explicit_inc_to = 0;
878 if (data.reverse) data.offset = len;
879 data.len = len;
881 /* If copying requires more than two move insns,
882 copy addresses to registers (to make displacements shorter)
883 and use post-increment if available. */
884 if (!(data.autinc_from && data.autinc_to)
885 && move_by_pieces_ninsns (len, align, max_size) > 2)
887 /* Find the mode of the largest move...
888 MODE might not be used depending on the definitions of the
889 USE_* macros below. */
890 machine_mode mode ATTRIBUTE_UNUSED
891 = widest_int_mode_for_size (max_size);
893 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
895 data.from_addr = copy_to_mode_reg (from_addr_mode,
896 plus_constant (from_addr_mode,
897 from_addr, len));
898 data.autinc_from = 1;
899 data.explicit_inc_from = -1;
901 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
903 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
904 data.autinc_from = 1;
905 data.explicit_inc_from = 1;
907 if (!data.autinc_from && CONSTANT_P (from_addr))
908 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
909 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
911 data.to_addr = copy_to_mode_reg (to_addr_mode,
912 plus_constant (to_addr_mode,
913 to_addr, len));
914 data.autinc_to = 1;
915 data.explicit_inc_to = -1;
917 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
919 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
920 data.autinc_to = 1;
921 data.explicit_inc_to = 1;
923 if (!data.autinc_to && CONSTANT_P (to_addr))
924 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
927 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
929 /* First move what we can in the largest integer mode, then go to
930 successively smaller modes. */
932 while (max_size > 1 && data.len > 0)
934 machine_mode mode = widest_int_mode_for_size (max_size);
936 if (mode == VOIDmode)
937 break;
939 icode = optab_handler (mov_optab, mode);
940 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
941 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
943 max_size = GET_MODE_SIZE (mode);
946 /* The code above should have handled everything. */
947 gcc_assert (!data.len);
949 if (endp)
951 rtx to1;
953 gcc_assert (!data.reverse);
954 if (data.autinc_to)
956 if (endp == 2)
958 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
959 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
960 else
961 data.to_addr = copy_to_mode_reg (to_addr_mode,
962 plus_constant (to_addr_mode,
963 data.to_addr,
964 -1));
966 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
967 data.offset);
969 else
971 if (endp == 2)
972 --data.offset;
973 to1 = adjust_address (data.to, QImode, data.offset);
975 return to1;
977 else
978 return data.to;
981 /* Return number of insns required to move L bytes by pieces.
982 ALIGN (in bits) is maximum alignment we can assume. */
984 unsigned HOST_WIDE_INT
985 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
986 unsigned int max_size)
988 unsigned HOST_WIDE_INT n_insns = 0;
990 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
992 while (max_size > 1 && l > 0)
994 machine_mode mode;
995 enum insn_code icode;
997 mode = widest_int_mode_for_size (max_size);
999 if (mode == VOIDmode)
1000 break;
1002 icode = optab_handler (mov_optab, mode);
1003 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1004 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1006 max_size = GET_MODE_SIZE (mode);
1009 gcc_assert (!l);
1010 return n_insns;
1013 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1014 with move instructions for mode MODE. GENFUN is the gen_... function
1015 to make a move insn for that mode. DATA has all the other info. */
1017 static void
1018 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1019 struct move_by_pieces_d *data)
1021 unsigned int size = GET_MODE_SIZE (mode);
1022 rtx to1 = NULL_RTX, from1;
1024 while (data->len >= size)
1026 if (data->reverse)
1027 data->offset -= size;
1029 if (data->to)
1031 if (data->autinc_to)
1032 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1033 data->offset);
1034 else
1035 to1 = adjust_address (data->to, mode, data->offset);
1038 if (data->autinc_from)
1039 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1040 data->offset);
1041 else
1042 from1 = adjust_address (data->from, mode, data->offset);
1044 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1045 emit_insn (gen_add2_insn (data->to_addr,
1046 gen_int_mode (-(HOST_WIDE_INT) size,
1047 GET_MODE (data->to_addr))));
1048 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1049 emit_insn (gen_add2_insn (data->from_addr,
1050 gen_int_mode (-(HOST_WIDE_INT) size,
1051 GET_MODE (data->from_addr))));
1053 if (data->to)
1054 emit_insn ((*genfun) (to1, from1));
1055 else
1057 #ifdef PUSH_ROUNDING
1058 emit_single_push_insn (mode, from1, NULL);
1059 #else
1060 gcc_unreachable ();
1061 #endif
1064 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1065 emit_insn (gen_add2_insn (data->to_addr,
1066 gen_int_mode (size,
1067 GET_MODE (data->to_addr))));
1068 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1069 emit_insn (gen_add2_insn (data->from_addr,
1070 gen_int_mode (size,
1071 GET_MODE (data->from_addr))));
1073 if (! data->reverse)
1074 data->offset += size;
1076 data->len -= size;
1080 /* Emit code to move a block Y to a block X. This may be done with
1081 string-move instructions, with multiple scalar move instructions,
1082 or with a library call.
1084 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1085 SIZE is an rtx that says how long they are.
1086 ALIGN is the maximum alignment we can assume they have.
1087 METHOD describes what kind of copy this is, and what mechanisms may be used.
1088 MIN_SIZE is the minimal size of block to move
1089 MAX_SIZE is the maximal size of block to move, if it can not be represented
1090 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1092 Return the address of the new block, if memcpy is called and returns it,
1093 0 otherwise. */
1096 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1097 unsigned int expected_align, HOST_WIDE_INT expected_size,
1098 unsigned HOST_WIDE_INT min_size,
1099 unsigned HOST_WIDE_INT max_size,
1100 unsigned HOST_WIDE_INT probable_max_size)
1102 bool may_use_call;
1103 rtx retval = 0;
1104 unsigned int align;
1106 gcc_assert (size);
1107 if (CONST_INT_P (size)
1108 && INTVAL (size) == 0)
1109 return 0;
1111 switch (method)
1113 case BLOCK_OP_NORMAL:
1114 case BLOCK_OP_TAILCALL:
1115 may_use_call = true;
1116 break;
1118 case BLOCK_OP_CALL_PARM:
1119 may_use_call = block_move_libcall_safe_for_call_parm ();
1121 /* Make inhibit_defer_pop nonzero around the library call
1122 to force it to pop the arguments right away. */
1123 NO_DEFER_POP;
1124 break;
1126 case BLOCK_OP_NO_LIBCALL:
1127 may_use_call = false;
1128 break;
1130 default:
1131 gcc_unreachable ();
1134 gcc_assert (MEM_P (x) && MEM_P (y));
1135 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1136 gcc_assert (align >= BITS_PER_UNIT);
1138 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1139 block copy is more efficient for other large modes, e.g. DCmode. */
1140 x = adjust_address (x, BLKmode, 0);
1141 y = adjust_address (y, BLKmode, 0);
1143 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1144 can be incorrect is coming from __builtin_memcpy. */
1145 if (CONST_INT_P (size))
1147 x = shallow_copy_rtx (x);
1148 y = shallow_copy_rtx (y);
1149 set_mem_size (x, INTVAL (size));
1150 set_mem_size (y, INTVAL (size));
1153 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1154 move_by_pieces (x, y, INTVAL (size), align, 0);
1155 else if (emit_block_move_via_movmem (x, y, size, align,
1156 expected_align, expected_size,
1157 min_size, max_size, probable_max_size))
1159 else if (may_use_call
1160 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1161 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1163 /* Since x and y are passed to a libcall, mark the corresponding
1164 tree EXPR as addressable. */
1165 tree y_expr = MEM_EXPR (y);
1166 tree x_expr = MEM_EXPR (x);
1167 if (y_expr)
1168 mark_addressable (y_expr);
1169 if (x_expr)
1170 mark_addressable (x_expr);
1171 retval = emit_block_move_via_libcall (x, y, size,
1172 method == BLOCK_OP_TAILCALL);
1175 else
1176 emit_block_move_via_loop (x, y, size, align);
1178 if (method == BLOCK_OP_CALL_PARM)
1179 OK_DEFER_POP;
1181 return retval;
1185 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1187 unsigned HOST_WIDE_INT max, min = 0;
1188 if (GET_CODE (size) == CONST_INT)
1189 min = max = UINTVAL (size);
1190 else
1191 max = GET_MODE_MASK (GET_MODE (size));
1192 return emit_block_move_hints (x, y, size, method, 0, -1,
1193 min, max, max);
1196 /* A subroutine of emit_block_move. Returns true if calling the
1197 block move libcall will not clobber any parameters which may have
1198 already been placed on the stack. */
1200 static bool
1201 block_move_libcall_safe_for_call_parm (void)
1203 #if defined (REG_PARM_STACK_SPACE)
1204 tree fn;
1205 #endif
1207 /* If arguments are pushed on the stack, then they're safe. */
1208 if (PUSH_ARGS)
1209 return true;
1211 /* If registers go on the stack anyway, any argument is sure to clobber
1212 an outgoing argument. */
1213 #if defined (REG_PARM_STACK_SPACE)
1214 fn = emit_block_move_libcall_fn (false);
1215 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1216 depend on its argument. */
1217 (void) fn;
1218 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1219 && REG_PARM_STACK_SPACE (fn) != 0)
1220 return false;
1221 #endif
1223 /* If any argument goes in memory, then it might clobber an outgoing
1224 argument. */
1226 CUMULATIVE_ARGS args_so_far_v;
1227 cumulative_args_t args_so_far;
1228 tree fn, arg;
1230 fn = emit_block_move_libcall_fn (false);
1231 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1232 args_so_far = pack_cumulative_args (&args_so_far_v);
1234 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1235 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1237 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1238 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1239 NULL_TREE, true);
1240 if (!tmp || !REG_P (tmp))
1241 return false;
1242 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1243 return false;
1244 targetm.calls.function_arg_advance (args_so_far, mode,
1245 NULL_TREE, true);
1248 return true;
1251 /* A subroutine of emit_block_move. Expand a movmem pattern;
1252 return true if successful. */
1254 static bool
1255 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1256 unsigned int expected_align, HOST_WIDE_INT expected_size,
1257 unsigned HOST_WIDE_INT min_size,
1258 unsigned HOST_WIDE_INT max_size,
1259 unsigned HOST_WIDE_INT probable_max_size)
1261 int save_volatile_ok = volatile_ok;
1262 machine_mode mode;
1264 if (expected_align < align)
1265 expected_align = align;
1266 if (expected_size != -1)
1268 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1269 expected_size = probable_max_size;
1270 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1271 expected_size = min_size;
1274 /* Since this is a move insn, we don't care about volatility. */
1275 volatile_ok = 1;
1277 /* Try the most limited insn first, because there's no point
1278 including more than one in the machine description unless
1279 the more limited one has some advantage. */
1281 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1282 mode = GET_MODE_WIDER_MODE (mode))
1284 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1286 if (code != CODE_FOR_nothing
1287 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1288 here because if SIZE is less than the mode mask, as it is
1289 returned by the macro, it will definitely be less than the
1290 actual mode mask. Since SIZE is within the Pmode address
1291 space, we limit MODE to Pmode. */
1292 && ((CONST_INT_P (size)
1293 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1294 <= (GET_MODE_MASK (mode) >> 1)))
1295 || max_size <= (GET_MODE_MASK (mode) >> 1)
1296 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1298 struct expand_operand ops[9];
1299 unsigned int nops;
1301 /* ??? When called via emit_block_move_for_call, it'd be
1302 nice if there were some way to inform the backend, so
1303 that it doesn't fail the expansion because it thinks
1304 emitting the libcall would be more efficient. */
1305 nops = insn_data[(int) code].n_generator_args;
1306 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1308 create_fixed_operand (&ops[0], x);
1309 create_fixed_operand (&ops[1], y);
1310 /* The check above guarantees that this size conversion is valid. */
1311 create_convert_operand_to (&ops[2], size, mode, true);
1312 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1313 if (nops >= 6)
1315 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1316 create_integer_operand (&ops[5], expected_size);
1318 if (nops >= 8)
1320 create_integer_operand (&ops[6], min_size);
1321 /* If we can not represent the maximal size,
1322 make parameter NULL. */
1323 if ((HOST_WIDE_INT) max_size != -1)
1324 create_integer_operand (&ops[7], max_size);
1325 else
1326 create_fixed_operand (&ops[7], NULL);
1328 if (nops == 9)
1330 /* If we can not represent the maximal size,
1331 make parameter NULL. */
1332 if ((HOST_WIDE_INT) probable_max_size != -1)
1333 create_integer_operand (&ops[8], probable_max_size);
1334 else
1335 create_fixed_operand (&ops[8], NULL);
1337 if (maybe_expand_insn (code, nops, ops))
1339 volatile_ok = save_volatile_ok;
1340 return true;
1345 volatile_ok = save_volatile_ok;
1346 return false;
1349 /* A subroutine of emit_block_move. Expand a call to memcpy.
1350 Return the return value from memcpy, 0 otherwise. */
1353 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1355 rtx dst_addr, src_addr;
1356 tree call_expr, fn, src_tree, dst_tree, size_tree;
1357 machine_mode size_mode;
1358 rtx retval;
1360 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1361 pseudos. We can then place those new pseudos into a VAR_DECL and
1362 use them later. */
1364 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1365 src_addr = copy_addr_to_reg (XEXP (src, 0));
1367 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1368 src_addr = convert_memory_address (ptr_mode, src_addr);
1370 dst_tree = make_tree (ptr_type_node, dst_addr);
1371 src_tree = make_tree (ptr_type_node, src_addr);
1373 size_mode = TYPE_MODE (sizetype);
1375 size = convert_to_mode (size_mode, size, 1);
1376 size = copy_to_mode_reg (size_mode, size);
1378 /* It is incorrect to use the libcall calling conventions to call
1379 memcpy in this context. This could be a user call to memcpy and
1380 the user may wish to examine the return value from memcpy. For
1381 targets where libcalls and normal calls have different conventions
1382 for returning pointers, we could end up generating incorrect code. */
1384 size_tree = make_tree (sizetype, size);
1386 fn = emit_block_move_libcall_fn (true);
1387 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1388 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1390 retval = expand_normal (call_expr);
1392 return retval;
1395 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1396 for the function we use for block copies. */
1398 static GTY(()) tree block_move_fn;
1400 void
1401 init_block_move_fn (const char *asmspec)
1403 if (!block_move_fn)
1405 tree args, fn, attrs, attr_args;
1407 fn = get_identifier ("memcpy");
1408 args = build_function_type_list (ptr_type_node, ptr_type_node,
1409 const_ptr_type_node, sizetype,
1410 NULL_TREE);
1412 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1413 DECL_EXTERNAL (fn) = 1;
1414 TREE_PUBLIC (fn) = 1;
1415 DECL_ARTIFICIAL (fn) = 1;
1416 TREE_NOTHROW (fn) = 1;
1417 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1418 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1420 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1421 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1423 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1425 block_move_fn = fn;
1428 if (asmspec)
1429 set_user_assembler_name (block_move_fn, asmspec);
1432 static tree
1433 emit_block_move_libcall_fn (int for_call)
1435 static bool emitted_extern;
1437 if (!block_move_fn)
1438 init_block_move_fn (NULL);
1440 if (for_call && !emitted_extern)
1442 emitted_extern = true;
1443 make_decl_rtl (block_move_fn);
1446 return block_move_fn;
1449 /* A subroutine of emit_block_move. Copy the data via an explicit
1450 loop. This is used only when libcalls are forbidden. */
1451 /* ??? It'd be nice to copy in hunks larger than QImode. */
1453 static void
1454 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1455 unsigned int align ATTRIBUTE_UNUSED)
1457 rtx_code_label *cmp_label, *top_label;
1458 rtx iter, x_addr, y_addr, tmp;
1459 machine_mode x_addr_mode = get_address_mode (x);
1460 machine_mode y_addr_mode = get_address_mode (y);
1461 machine_mode iter_mode;
1463 iter_mode = GET_MODE (size);
1464 if (iter_mode == VOIDmode)
1465 iter_mode = word_mode;
1467 top_label = gen_label_rtx ();
1468 cmp_label = gen_label_rtx ();
1469 iter = gen_reg_rtx (iter_mode);
1471 emit_move_insn (iter, const0_rtx);
1473 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1474 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1475 do_pending_stack_adjust ();
1477 emit_jump (cmp_label);
1478 emit_label (top_label);
1480 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1481 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1483 if (x_addr_mode != y_addr_mode)
1484 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1485 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1487 x = change_address (x, QImode, x_addr);
1488 y = change_address (y, QImode, y_addr);
1490 emit_move_insn (x, y);
1492 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1493 true, OPTAB_LIB_WIDEN);
1494 if (tmp != iter)
1495 emit_move_insn (iter, tmp);
1497 emit_label (cmp_label);
1499 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1500 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1503 /* Copy all or part of a value X into registers starting at REGNO.
1504 The number of registers to be filled is NREGS. */
1506 void
1507 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1509 int i;
1510 #ifdef HAVE_load_multiple
1511 rtx pat;
1512 rtx_insn *last;
1513 #endif
1515 if (nregs == 0)
1516 return;
1518 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1519 x = validize_mem (force_const_mem (mode, x));
1521 /* See if the machine can do this with a load multiple insn. */
1522 #ifdef HAVE_load_multiple
1523 if (HAVE_load_multiple)
1525 last = get_last_insn ();
1526 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1527 GEN_INT (nregs));
1528 if (pat)
1530 emit_insn (pat);
1531 return;
1533 else
1534 delete_insns_since (last);
1536 #endif
1538 for (i = 0; i < nregs; i++)
1539 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1540 operand_subword_force (x, i, mode));
1543 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1544 The number of registers to be filled is NREGS. */
1546 void
1547 move_block_from_reg (int regno, rtx x, int nregs)
1549 int i;
1551 if (nregs == 0)
1552 return;
1554 /* See if the machine can do this with a store multiple insn. */
1555 #ifdef HAVE_store_multiple
1556 if (HAVE_store_multiple)
1558 rtx_insn *last = get_last_insn ();
1559 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1560 GEN_INT (nregs));
1561 if (pat)
1563 emit_insn (pat);
1564 return;
1566 else
1567 delete_insns_since (last);
1569 #endif
1571 for (i = 0; i < nregs; i++)
1573 rtx tem = operand_subword (x, i, 1, BLKmode);
1575 gcc_assert (tem);
1577 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1581 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1582 ORIG, where ORIG is a non-consecutive group of registers represented by
1583 a PARALLEL. The clone is identical to the original except in that the
1584 original set of registers is replaced by a new set of pseudo registers.
1585 The new set has the same modes as the original set. */
1588 gen_group_rtx (rtx orig)
1590 int i, length;
1591 rtx *tmps;
1593 gcc_assert (GET_CODE (orig) == PARALLEL);
1595 length = XVECLEN (orig, 0);
1596 tmps = XALLOCAVEC (rtx, length);
1598 /* Skip a NULL entry in first slot. */
1599 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1601 if (i)
1602 tmps[0] = 0;
1604 for (; i < length; i++)
1606 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1607 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1609 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1612 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1615 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1616 except that values are placed in TMPS[i], and must later be moved
1617 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1619 static void
1620 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1622 rtx src;
1623 int start, i;
1624 machine_mode m = GET_MODE (orig_src);
1626 gcc_assert (GET_CODE (dst) == PARALLEL);
1628 if (m != VOIDmode
1629 && !SCALAR_INT_MODE_P (m)
1630 && !MEM_P (orig_src)
1631 && GET_CODE (orig_src) != CONCAT)
1633 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1634 if (imode == BLKmode)
1635 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1636 else
1637 src = gen_reg_rtx (imode);
1638 if (imode != BLKmode)
1639 src = gen_lowpart (GET_MODE (orig_src), src);
1640 emit_move_insn (src, orig_src);
1641 /* ...and back again. */
1642 if (imode != BLKmode)
1643 src = gen_lowpart (imode, src);
1644 emit_group_load_1 (tmps, dst, src, type, ssize);
1645 return;
1648 /* Check for a NULL entry, used to indicate that the parameter goes
1649 both on the stack and in registers. */
1650 if (XEXP (XVECEXP (dst, 0, 0), 0))
1651 start = 0;
1652 else
1653 start = 1;
1655 /* Process the pieces. */
1656 for (i = start; i < XVECLEN (dst, 0); i++)
1658 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1659 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1660 unsigned int bytelen = GET_MODE_SIZE (mode);
1661 int shift = 0;
1663 /* Handle trailing fragments that run over the size of the struct. */
1664 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1666 /* Arrange to shift the fragment to where it belongs.
1667 extract_bit_field loads to the lsb of the reg. */
1668 if (
1669 #ifdef BLOCK_REG_PADDING
1670 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1671 == (BYTES_BIG_ENDIAN ? upward : downward)
1672 #else
1673 BYTES_BIG_ENDIAN
1674 #endif
1676 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1677 bytelen = ssize - bytepos;
1678 gcc_assert (bytelen > 0);
1681 /* If we won't be loading directly from memory, protect the real source
1682 from strange tricks we might play; but make sure that the source can
1683 be loaded directly into the destination. */
1684 src = orig_src;
1685 if (!MEM_P (orig_src)
1686 && (!CONSTANT_P (orig_src)
1687 || (GET_MODE (orig_src) != mode
1688 && GET_MODE (orig_src) != VOIDmode)))
1690 if (GET_MODE (orig_src) == VOIDmode)
1691 src = gen_reg_rtx (mode);
1692 else
1693 src = gen_reg_rtx (GET_MODE (orig_src));
1695 emit_move_insn (src, orig_src);
1698 /* Optimize the access just a bit. */
1699 if (MEM_P (src)
1700 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1701 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1702 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1703 && bytelen == GET_MODE_SIZE (mode))
1705 tmps[i] = gen_reg_rtx (mode);
1706 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1708 else if (COMPLEX_MODE_P (mode)
1709 && GET_MODE (src) == mode
1710 && bytelen == GET_MODE_SIZE (mode))
1711 /* Let emit_move_complex do the bulk of the work. */
1712 tmps[i] = src;
1713 else if (GET_CODE (src) == CONCAT)
1715 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1716 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1718 if ((bytepos == 0 && bytelen == slen0)
1719 || (bytepos != 0 && bytepos + bytelen <= slen))
1721 /* The following assumes that the concatenated objects all
1722 have the same size. In this case, a simple calculation
1723 can be used to determine the object and the bit field
1724 to be extracted. */
1725 tmps[i] = XEXP (src, bytepos / slen0);
1726 if (! CONSTANT_P (tmps[i])
1727 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1728 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1729 (bytepos % slen0) * BITS_PER_UNIT,
1730 1, NULL_RTX, mode, mode);
1732 else
1734 rtx mem;
1736 gcc_assert (!bytepos);
1737 mem = assign_stack_temp (GET_MODE (src), slen);
1738 emit_move_insn (mem, src);
1739 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1740 0, 1, NULL_RTX, mode, mode);
1743 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1744 SIMD register, which is currently broken. While we get GCC
1745 to emit proper RTL for these cases, let's dump to memory. */
1746 else if (VECTOR_MODE_P (GET_MODE (dst))
1747 && REG_P (src))
1749 int slen = GET_MODE_SIZE (GET_MODE (src));
1750 rtx mem;
1752 mem = assign_stack_temp (GET_MODE (src), slen);
1753 emit_move_insn (mem, src);
1754 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1756 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1757 && XVECLEN (dst, 0) > 1)
1758 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1759 else if (CONSTANT_P (src))
1761 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1763 if (len == ssize)
1764 tmps[i] = src;
1765 else
1767 rtx first, second;
1769 /* TODO: const_wide_int can have sizes other than this... */
1770 gcc_assert (2 * len == ssize);
1771 split_double (src, &first, &second);
1772 if (i)
1773 tmps[i] = second;
1774 else
1775 tmps[i] = first;
1778 else if (REG_P (src) && GET_MODE (src) == mode)
1779 tmps[i] = src;
1780 else
1781 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1782 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1783 mode, mode);
1785 if (shift)
1786 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1787 shift, tmps[i], 0);
1791 /* Emit code to move a block SRC of type TYPE to a block DST,
1792 where DST is non-consecutive registers represented by a PARALLEL.
1793 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1794 if not known. */
1796 void
1797 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1799 rtx *tmps;
1800 int i;
1802 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1803 emit_group_load_1 (tmps, dst, src, type, ssize);
1805 /* Copy the extracted pieces into the proper (probable) hard regs. */
1806 for (i = 0; i < XVECLEN (dst, 0); i++)
1808 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1809 if (d == NULL)
1810 continue;
1811 emit_move_insn (d, tmps[i]);
1815 /* Similar, but load SRC into new pseudos in a format that looks like
1816 PARALLEL. This can later be fed to emit_group_move to get things
1817 in the right place. */
1820 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1822 rtvec vec;
1823 int i;
1825 vec = rtvec_alloc (XVECLEN (parallel, 0));
1826 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1828 /* Convert the vector to look just like the original PARALLEL, except
1829 with the computed values. */
1830 for (i = 0; i < XVECLEN (parallel, 0); i++)
1832 rtx e = XVECEXP (parallel, 0, i);
1833 rtx d = XEXP (e, 0);
1835 if (d)
1837 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1838 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1840 RTVEC_ELT (vec, i) = e;
1843 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1846 /* Emit code to move a block SRC to block DST, where SRC and DST are
1847 non-consecutive groups of registers, each represented by a PARALLEL. */
1849 void
1850 emit_group_move (rtx dst, rtx src)
1852 int i;
1854 gcc_assert (GET_CODE (src) == PARALLEL
1855 && GET_CODE (dst) == PARALLEL
1856 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1858 /* Skip first entry if NULL. */
1859 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1860 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1861 XEXP (XVECEXP (src, 0, i), 0));
1864 /* Move a group of registers represented by a PARALLEL into pseudos. */
1867 emit_group_move_into_temps (rtx src)
1869 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1870 int i;
1872 for (i = 0; i < XVECLEN (src, 0); i++)
1874 rtx e = XVECEXP (src, 0, i);
1875 rtx d = XEXP (e, 0);
1877 if (d)
1878 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1879 RTVEC_ELT (vec, i) = e;
1882 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1885 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1886 where SRC is non-consecutive registers represented by a PARALLEL.
1887 SSIZE represents the total size of block ORIG_DST, or -1 if not
1888 known. */
1890 void
1891 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1893 rtx *tmps, dst;
1894 int start, finish, i;
1895 machine_mode m = GET_MODE (orig_dst);
1897 gcc_assert (GET_CODE (src) == PARALLEL);
1899 if (!SCALAR_INT_MODE_P (m)
1900 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1902 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1903 if (imode == BLKmode)
1904 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1905 else
1906 dst = gen_reg_rtx (imode);
1907 emit_group_store (dst, src, type, ssize);
1908 if (imode != BLKmode)
1909 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1910 emit_move_insn (orig_dst, dst);
1911 return;
1914 /* Check for a NULL entry, used to indicate that the parameter goes
1915 both on the stack and in registers. */
1916 if (XEXP (XVECEXP (src, 0, 0), 0))
1917 start = 0;
1918 else
1919 start = 1;
1920 finish = XVECLEN (src, 0);
1922 tmps = XALLOCAVEC (rtx, finish);
1924 /* Copy the (probable) hard regs into pseudos. */
1925 for (i = start; i < finish; i++)
1927 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1928 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1930 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1931 emit_move_insn (tmps[i], reg);
1933 else
1934 tmps[i] = reg;
1937 /* If we won't be storing directly into memory, protect the real destination
1938 from strange tricks we might play. */
1939 dst = orig_dst;
1940 if (GET_CODE (dst) == PARALLEL)
1942 rtx temp;
1944 /* We can get a PARALLEL dst if there is a conditional expression in
1945 a return statement. In that case, the dst and src are the same,
1946 so no action is necessary. */
1947 if (rtx_equal_p (dst, src))
1948 return;
1950 /* It is unclear if we can ever reach here, but we may as well handle
1951 it. Allocate a temporary, and split this into a store/load to/from
1952 the temporary. */
1953 temp = assign_stack_temp (GET_MODE (dst), ssize);
1954 emit_group_store (temp, src, type, ssize);
1955 emit_group_load (dst, temp, type, ssize);
1956 return;
1958 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1960 machine_mode outer = GET_MODE (dst);
1961 machine_mode inner;
1962 HOST_WIDE_INT bytepos;
1963 bool done = false;
1964 rtx temp;
1966 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1967 dst = gen_reg_rtx (outer);
1969 /* Make life a bit easier for combine. */
1970 /* If the first element of the vector is the low part
1971 of the destination mode, use a paradoxical subreg to
1972 initialize the destination. */
1973 if (start < finish)
1975 inner = GET_MODE (tmps[start]);
1976 bytepos = subreg_lowpart_offset (inner, outer);
1977 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1979 temp = simplify_gen_subreg (outer, tmps[start],
1980 inner, 0);
1981 if (temp)
1983 emit_move_insn (dst, temp);
1984 done = true;
1985 start++;
1990 /* If the first element wasn't the low part, try the last. */
1991 if (!done
1992 && start < finish - 1)
1994 inner = GET_MODE (tmps[finish - 1]);
1995 bytepos = subreg_lowpart_offset (inner, outer);
1996 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1998 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1999 inner, 0);
2000 if (temp)
2002 emit_move_insn (dst, temp);
2003 done = true;
2004 finish--;
2009 /* Otherwise, simply initialize the result to zero. */
2010 if (!done)
2011 emit_move_insn (dst, CONST0_RTX (outer));
2014 /* Process the pieces. */
2015 for (i = start; i < finish; i++)
2017 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2018 machine_mode mode = GET_MODE (tmps[i]);
2019 unsigned int bytelen = GET_MODE_SIZE (mode);
2020 unsigned int adj_bytelen;
2021 rtx dest = dst;
2023 /* Handle trailing fragments that run over the size of the struct. */
2024 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2025 adj_bytelen = ssize - bytepos;
2026 else
2027 adj_bytelen = bytelen;
2029 if (GET_CODE (dst) == CONCAT)
2031 if (bytepos + adj_bytelen
2032 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2033 dest = XEXP (dst, 0);
2034 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2036 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2037 dest = XEXP (dst, 1);
2039 else
2041 machine_mode dest_mode = GET_MODE (dest);
2042 machine_mode tmp_mode = GET_MODE (tmps[i]);
2044 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2046 if (GET_MODE_ALIGNMENT (dest_mode)
2047 >= GET_MODE_ALIGNMENT (tmp_mode))
2049 dest = assign_stack_temp (dest_mode,
2050 GET_MODE_SIZE (dest_mode));
2051 emit_move_insn (adjust_address (dest,
2052 tmp_mode,
2053 bytepos),
2054 tmps[i]);
2055 dst = dest;
2057 else
2059 dest = assign_stack_temp (tmp_mode,
2060 GET_MODE_SIZE (tmp_mode));
2061 emit_move_insn (dest, tmps[i]);
2062 dst = adjust_address (dest, dest_mode, bytepos);
2064 break;
2068 /* Handle trailing fragments that run over the size of the struct. */
2069 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2071 /* store_bit_field always takes its value from the lsb.
2072 Move the fragment to the lsb if it's not already there. */
2073 if (
2074 #ifdef BLOCK_REG_PADDING
2075 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2076 == (BYTES_BIG_ENDIAN ? upward : downward)
2077 #else
2078 BYTES_BIG_ENDIAN
2079 #endif
2082 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2083 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2084 shift, tmps[i], 0);
2087 /* Make sure not to write past the end of the struct. */
2088 store_bit_field (dest,
2089 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2090 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2091 VOIDmode, tmps[i]);
2094 /* Optimize the access just a bit. */
2095 else if (MEM_P (dest)
2096 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2097 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2098 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2099 && bytelen == GET_MODE_SIZE (mode))
2100 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2102 else
2103 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2104 0, 0, mode, tmps[i]);
2107 /* Copy from the pseudo into the (probable) hard reg. */
2108 if (orig_dst != dst)
2109 emit_move_insn (orig_dst, dst);
2112 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2113 of the value stored in X. */
2116 maybe_emit_group_store (rtx x, tree type)
2118 machine_mode mode = TYPE_MODE (type);
2119 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2120 if (GET_CODE (x) == PARALLEL)
2122 rtx result = gen_reg_rtx (mode);
2123 emit_group_store (result, x, type, int_size_in_bytes (type));
2124 return result;
2126 return x;
2129 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2131 This is used on targets that return BLKmode values in registers. */
2133 void
2134 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2136 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2137 rtx src = NULL, dst = NULL;
2138 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2139 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2140 machine_mode mode = GET_MODE (srcreg);
2141 machine_mode tmode = GET_MODE (target);
2142 machine_mode copy_mode;
2144 /* BLKmode registers created in the back-end shouldn't have survived. */
2145 gcc_assert (mode != BLKmode);
2147 /* If the structure doesn't take up a whole number of words, see whether
2148 SRCREG is padded on the left or on the right. If it's on the left,
2149 set PADDING_CORRECTION to the number of bits to skip.
2151 In most ABIs, the structure will be returned at the least end of
2152 the register, which translates to right padding on little-endian
2153 targets and left padding on big-endian targets. The opposite
2154 holds if the structure is returned at the most significant
2155 end of the register. */
2156 if (bytes % UNITS_PER_WORD != 0
2157 && (targetm.calls.return_in_msb (type)
2158 ? !BYTES_BIG_ENDIAN
2159 : BYTES_BIG_ENDIAN))
2160 padding_correction
2161 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2163 /* We can use a single move if we have an exact mode for the size. */
2164 else if (MEM_P (target)
2165 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2166 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2167 && bytes == GET_MODE_SIZE (mode))
2169 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2170 return;
2173 /* And if we additionally have the same mode for a register. */
2174 else if (REG_P (target)
2175 && GET_MODE (target) == mode
2176 && bytes == GET_MODE_SIZE (mode))
2178 emit_move_insn (target, srcreg);
2179 return;
2182 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2183 into a new pseudo which is a full word. */
2184 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2186 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2187 mode = word_mode;
2190 /* Copy the structure BITSIZE bits at a time. If the target lives in
2191 memory, take care of not reading/writing past its end by selecting
2192 a copy mode suited to BITSIZE. This should always be possible given
2193 how it is computed.
2195 If the target lives in register, make sure not to select a copy mode
2196 larger than the mode of the register.
2198 We could probably emit more efficient code for machines which do not use
2199 strict alignment, but it doesn't seem worth the effort at the current
2200 time. */
2202 copy_mode = word_mode;
2203 if (MEM_P (target))
2205 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2206 if (mem_mode != BLKmode)
2207 copy_mode = mem_mode;
2209 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2210 copy_mode = tmode;
2212 for (bitpos = 0, xbitpos = padding_correction;
2213 bitpos < bytes * BITS_PER_UNIT;
2214 bitpos += bitsize, xbitpos += bitsize)
2216 /* We need a new source operand each time xbitpos is on a
2217 word boundary and when xbitpos == padding_correction
2218 (the first time through). */
2219 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2220 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2222 /* We need a new destination operand each time bitpos is on
2223 a word boundary. */
2224 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2225 dst = target;
2226 else if (bitpos % BITS_PER_WORD == 0)
2227 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2229 /* Use xbitpos for the source extraction (right justified) and
2230 bitpos for the destination store (left justified). */
2231 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2232 extract_bit_field (src, bitsize,
2233 xbitpos % BITS_PER_WORD, 1,
2234 NULL_RTX, copy_mode, copy_mode));
2238 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2239 register if it contains any data, otherwise return null.
2241 This is used on targets that return BLKmode values in registers. */
2244 copy_blkmode_to_reg (machine_mode mode, tree src)
2246 int i, n_regs;
2247 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2248 unsigned int bitsize;
2249 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2250 machine_mode dst_mode;
2252 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2254 x = expand_normal (src);
2256 bytes = int_size_in_bytes (TREE_TYPE (src));
2257 if (bytes == 0)
2258 return NULL_RTX;
2260 /* If the structure doesn't take up a whole number of words, see
2261 whether the register value should be padded on the left or on
2262 the right. Set PADDING_CORRECTION to the number of padding
2263 bits needed on the left side.
2265 In most ABIs, the structure will be returned at the least end of
2266 the register, which translates to right padding on little-endian
2267 targets and left padding on big-endian targets. The opposite
2268 holds if the structure is returned at the most significant
2269 end of the register. */
2270 if (bytes % UNITS_PER_WORD != 0
2271 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2272 ? !BYTES_BIG_ENDIAN
2273 : BYTES_BIG_ENDIAN))
2274 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2275 * BITS_PER_UNIT));
2277 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2278 dst_words = XALLOCAVEC (rtx, n_regs);
2279 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2281 /* Copy the structure BITSIZE bits at a time. */
2282 for (bitpos = 0, xbitpos = padding_correction;
2283 bitpos < bytes * BITS_PER_UNIT;
2284 bitpos += bitsize, xbitpos += bitsize)
2286 /* We need a new destination pseudo each time xbitpos is
2287 on a word boundary and when xbitpos == padding_correction
2288 (the first time through). */
2289 if (xbitpos % BITS_PER_WORD == 0
2290 || xbitpos == padding_correction)
2292 /* Generate an appropriate register. */
2293 dst_word = gen_reg_rtx (word_mode);
2294 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2296 /* Clear the destination before we move anything into it. */
2297 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2300 /* We need a new source operand each time bitpos is on a word
2301 boundary. */
2302 if (bitpos % BITS_PER_WORD == 0)
2303 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2305 /* Use bitpos for the source extraction (left justified) and
2306 xbitpos for the destination store (right justified). */
2307 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2308 0, 0, word_mode,
2309 extract_bit_field (src_word, bitsize,
2310 bitpos % BITS_PER_WORD, 1,
2311 NULL_RTX, word_mode, word_mode));
2314 if (mode == BLKmode)
2316 /* Find the smallest integer mode large enough to hold the
2317 entire structure. */
2318 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2319 mode != VOIDmode;
2320 mode = GET_MODE_WIDER_MODE (mode))
2321 /* Have we found a large enough mode? */
2322 if (GET_MODE_SIZE (mode) >= bytes)
2323 break;
2325 /* A suitable mode should have been found. */
2326 gcc_assert (mode != VOIDmode);
2329 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2330 dst_mode = word_mode;
2331 else
2332 dst_mode = mode;
2333 dst = gen_reg_rtx (dst_mode);
2335 for (i = 0; i < n_regs; i++)
2336 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2338 if (mode != dst_mode)
2339 dst = gen_lowpart (mode, dst);
2341 return dst;
2344 /* Add a USE expression for REG to the (possibly empty) list pointed
2345 to by CALL_FUSAGE. REG must denote a hard register. */
2347 void
2348 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2350 gcc_assert (REG_P (reg));
2352 if (!HARD_REGISTER_P (reg))
2353 return;
2355 *call_fusage
2356 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2359 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2360 to by CALL_FUSAGE. REG must denote a hard register. */
2362 void
2363 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2365 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2367 *call_fusage
2368 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2371 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2372 starting at REGNO. All of these registers must be hard registers. */
2374 void
2375 use_regs (rtx *call_fusage, int regno, int nregs)
2377 int i;
2379 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2381 for (i = 0; i < nregs; i++)
2382 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2385 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2386 PARALLEL REGS. This is for calls that pass values in multiple
2387 non-contiguous locations. The Irix 6 ABI has examples of this. */
2389 void
2390 use_group_regs (rtx *call_fusage, rtx regs)
2392 int i;
2394 for (i = 0; i < XVECLEN (regs, 0); i++)
2396 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2398 /* A NULL entry means the parameter goes both on the stack and in
2399 registers. This can also be a MEM for targets that pass values
2400 partially on the stack and partially in registers. */
2401 if (reg != 0 && REG_P (reg))
2402 use_reg (call_fusage, reg);
2406 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2407 assigment and the code of the expresion on the RHS is CODE. Return
2408 NULL otherwise. */
2410 static gimple
2411 get_def_for_expr (tree name, enum tree_code code)
2413 gimple def_stmt;
2415 if (TREE_CODE (name) != SSA_NAME)
2416 return NULL;
2418 def_stmt = get_gimple_for_ssa_name (name);
2419 if (!def_stmt
2420 || gimple_assign_rhs_code (def_stmt) != code)
2421 return NULL;
2423 return def_stmt;
2426 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2427 assigment and the class of the expresion on the RHS is CLASS. Return
2428 NULL otherwise. */
2430 static gimple
2431 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2433 gimple def_stmt;
2435 if (TREE_CODE (name) != SSA_NAME)
2436 return NULL;
2438 def_stmt = get_gimple_for_ssa_name (name);
2439 if (!def_stmt
2440 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2441 return NULL;
2443 return def_stmt;
2447 /* Determine whether the LEN bytes generated by CONSTFUN can be
2448 stored to memory using several move instructions. CONSTFUNDATA is
2449 a pointer which will be passed as argument in every CONSTFUN call.
2450 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2451 a memset operation and false if it's a copy of a constant string.
2452 Return nonzero if a call to store_by_pieces should succeed. */
2455 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2456 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2457 void *constfundata, unsigned int align, bool memsetp)
2459 unsigned HOST_WIDE_INT l;
2460 unsigned int max_size;
2461 HOST_WIDE_INT offset = 0;
2462 machine_mode mode;
2463 enum insn_code icode;
2464 int reverse;
2465 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2466 rtx cst ATTRIBUTE_UNUSED;
2468 if (len == 0)
2469 return 1;
2471 if (!targetm.use_by_pieces_infrastructure_p (len, align,
2472 memsetp
2473 ? SET_BY_PIECES
2474 : STORE_BY_PIECES,
2475 optimize_insn_for_speed_p ()))
2476 return 0;
2478 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2480 /* We would first store what we can in the largest integer mode, then go to
2481 successively smaller modes. */
2483 for (reverse = 0;
2484 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2485 reverse++)
2487 l = len;
2488 max_size = STORE_MAX_PIECES + 1;
2489 while (max_size > 1 && l > 0)
2491 mode = widest_int_mode_for_size (max_size);
2493 if (mode == VOIDmode)
2494 break;
2496 icode = optab_handler (mov_optab, mode);
2497 if (icode != CODE_FOR_nothing
2498 && align >= GET_MODE_ALIGNMENT (mode))
2500 unsigned int size = GET_MODE_SIZE (mode);
2502 while (l >= size)
2504 if (reverse)
2505 offset -= size;
2507 cst = (*constfun) (constfundata, offset, mode);
2508 if (!targetm.legitimate_constant_p (mode, cst))
2509 return 0;
2511 if (!reverse)
2512 offset += size;
2514 l -= size;
2518 max_size = GET_MODE_SIZE (mode);
2521 /* The code above should have handled everything. */
2522 gcc_assert (!l);
2525 return 1;
2528 /* Generate several move instructions to store LEN bytes generated by
2529 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2530 pointer which will be passed as argument in every CONSTFUN call.
2531 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2532 a memset operation and false if it's a copy of a constant string.
2533 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2534 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2535 stpcpy. */
2538 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2539 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
2540 void *constfundata, unsigned int align, bool memsetp, int endp)
2542 machine_mode to_addr_mode = get_address_mode (to);
2543 struct store_by_pieces_d data;
2545 if (len == 0)
2547 gcc_assert (endp != 2);
2548 return to;
2551 gcc_assert (targetm.use_by_pieces_infrastructure_p
2552 (len, align,
2553 memsetp
2554 ? SET_BY_PIECES
2555 : STORE_BY_PIECES,
2556 optimize_insn_for_speed_p ()));
2558 data.constfun = constfun;
2559 data.constfundata = constfundata;
2560 data.len = len;
2561 data.to = to;
2562 store_by_pieces_1 (&data, align);
2563 if (endp)
2565 rtx to1;
2567 gcc_assert (!data.reverse);
2568 if (data.autinc_to)
2570 if (endp == 2)
2572 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2573 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2574 else
2575 data.to_addr = copy_to_mode_reg (to_addr_mode,
2576 plus_constant (to_addr_mode,
2577 data.to_addr,
2578 -1));
2580 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2581 data.offset);
2583 else
2585 if (endp == 2)
2586 --data.offset;
2587 to1 = adjust_address (data.to, QImode, data.offset);
2589 return to1;
2591 else
2592 return data.to;
2595 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2596 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2598 static void
2599 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2601 struct store_by_pieces_d data;
2603 if (len == 0)
2604 return;
2606 data.constfun = clear_by_pieces_1;
2607 data.constfundata = NULL;
2608 data.len = len;
2609 data.to = to;
2610 store_by_pieces_1 (&data, align);
2613 /* Callback routine for clear_by_pieces.
2614 Return const0_rtx unconditionally. */
2616 static rtx
2617 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2618 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2619 machine_mode mode ATTRIBUTE_UNUSED)
2621 return const0_rtx;
2624 /* Subroutine of clear_by_pieces and store_by_pieces.
2625 Generate several move instructions to store LEN bytes of block TO. (A MEM
2626 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2628 static void
2629 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2630 unsigned int align ATTRIBUTE_UNUSED)
2632 machine_mode to_addr_mode = get_address_mode (data->to);
2633 rtx to_addr = XEXP (data->to, 0);
2634 unsigned int max_size = STORE_MAX_PIECES + 1;
2635 enum insn_code icode;
2637 data->offset = 0;
2638 data->to_addr = to_addr;
2639 data->autinc_to
2640 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2641 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2643 data->explicit_inc_to = 0;
2644 data->reverse
2645 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2646 if (data->reverse)
2647 data->offset = data->len;
2649 /* If storing requires more than two move insns,
2650 copy addresses to registers (to make displacements shorter)
2651 and use post-increment if available. */
2652 if (!data->autinc_to
2653 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2655 /* Determine the main mode we'll be using.
2656 MODE might not be used depending on the definitions of the
2657 USE_* macros below. */
2658 machine_mode mode ATTRIBUTE_UNUSED
2659 = widest_int_mode_for_size (max_size);
2661 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2663 data->to_addr = copy_to_mode_reg (to_addr_mode,
2664 plus_constant (to_addr_mode,
2665 to_addr,
2666 data->len));
2667 data->autinc_to = 1;
2668 data->explicit_inc_to = -1;
2671 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2672 && ! data->autinc_to)
2674 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2675 data->autinc_to = 1;
2676 data->explicit_inc_to = 1;
2679 if ( !data->autinc_to && CONSTANT_P (to_addr))
2680 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2683 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2685 /* First store what we can in the largest integer mode, then go to
2686 successively smaller modes. */
2688 while (max_size > 1 && data->len > 0)
2690 machine_mode mode = widest_int_mode_for_size (max_size);
2692 if (mode == VOIDmode)
2693 break;
2695 icode = optab_handler (mov_optab, mode);
2696 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2697 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2699 max_size = GET_MODE_SIZE (mode);
2702 /* The code above should have handled everything. */
2703 gcc_assert (!data->len);
2706 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2707 with move instructions for mode MODE. GENFUN is the gen_... function
2708 to make a move insn for that mode. DATA has all the other info. */
2710 static void
2711 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2712 struct store_by_pieces_d *data)
2714 unsigned int size = GET_MODE_SIZE (mode);
2715 rtx to1, cst;
2717 while (data->len >= size)
2719 if (data->reverse)
2720 data->offset -= size;
2722 if (data->autinc_to)
2723 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2724 data->offset);
2725 else
2726 to1 = adjust_address (data->to, mode, data->offset);
2728 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2729 emit_insn (gen_add2_insn (data->to_addr,
2730 gen_int_mode (-(HOST_WIDE_INT) size,
2731 GET_MODE (data->to_addr))));
2733 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2734 emit_insn ((*genfun) (to1, cst));
2736 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2737 emit_insn (gen_add2_insn (data->to_addr,
2738 gen_int_mode (size,
2739 GET_MODE (data->to_addr))));
2741 if (! data->reverse)
2742 data->offset += size;
2744 data->len -= size;
2748 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2749 its length in bytes. */
2752 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2753 unsigned int expected_align, HOST_WIDE_INT expected_size,
2754 unsigned HOST_WIDE_INT min_size,
2755 unsigned HOST_WIDE_INT max_size,
2756 unsigned HOST_WIDE_INT probable_max_size)
2758 machine_mode mode = GET_MODE (object);
2759 unsigned int align;
2761 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2763 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2764 just move a zero. Otherwise, do this a piece at a time. */
2765 if (mode != BLKmode
2766 && CONST_INT_P (size)
2767 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2769 rtx zero = CONST0_RTX (mode);
2770 if (zero != NULL)
2772 emit_move_insn (object, zero);
2773 return NULL;
2776 if (COMPLEX_MODE_P (mode))
2778 zero = CONST0_RTX (GET_MODE_INNER (mode));
2779 if (zero != NULL)
2781 write_complex_part (object, zero, 0);
2782 write_complex_part (object, zero, 1);
2783 return NULL;
2788 if (size == const0_rtx)
2789 return NULL;
2791 align = MEM_ALIGN (object);
2793 if (CONST_INT_P (size)
2794 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2795 CLEAR_BY_PIECES,
2796 optimize_insn_for_speed_p ()))
2797 clear_by_pieces (object, INTVAL (size), align);
2798 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2799 expected_align, expected_size,
2800 min_size, max_size, probable_max_size))
2802 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2803 return set_storage_via_libcall (object, size, const0_rtx,
2804 method == BLOCK_OP_TAILCALL);
2805 else
2806 gcc_unreachable ();
2808 return NULL;
2812 clear_storage (rtx object, rtx size, enum block_op_methods method)
2814 unsigned HOST_WIDE_INT max, min = 0;
2815 if (GET_CODE (size) == CONST_INT)
2816 min = max = UINTVAL (size);
2817 else
2818 max = GET_MODE_MASK (GET_MODE (size));
2819 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2823 /* A subroutine of clear_storage. Expand a call to memset.
2824 Return the return value of memset, 0 otherwise. */
2827 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2829 tree call_expr, fn, object_tree, size_tree, val_tree;
2830 machine_mode size_mode;
2831 rtx retval;
2833 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2834 place those into new pseudos into a VAR_DECL and use them later. */
2836 object = copy_addr_to_reg (XEXP (object, 0));
2838 size_mode = TYPE_MODE (sizetype);
2839 size = convert_to_mode (size_mode, size, 1);
2840 size = copy_to_mode_reg (size_mode, size);
2842 /* It is incorrect to use the libcall calling conventions to call
2843 memset in this context. This could be a user call to memset and
2844 the user may wish to examine the return value from memset. For
2845 targets where libcalls and normal calls have different conventions
2846 for returning pointers, we could end up generating incorrect code. */
2848 object_tree = make_tree (ptr_type_node, object);
2849 if (!CONST_INT_P (val))
2850 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2851 size_tree = make_tree (sizetype, size);
2852 val_tree = make_tree (integer_type_node, val);
2854 fn = clear_storage_libcall_fn (true);
2855 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2856 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2858 retval = expand_normal (call_expr);
2860 return retval;
2863 /* A subroutine of set_storage_via_libcall. Create the tree node
2864 for the function we use for block clears. */
2866 tree block_clear_fn;
2868 void
2869 init_block_clear_fn (const char *asmspec)
2871 if (!block_clear_fn)
2873 tree fn, args;
2875 fn = get_identifier ("memset");
2876 args = build_function_type_list (ptr_type_node, ptr_type_node,
2877 integer_type_node, sizetype,
2878 NULL_TREE);
2880 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2881 DECL_EXTERNAL (fn) = 1;
2882 TREE_PUBLIC (fn) = 1;
2883 DECL_ARTIFICIAL (fn) = 1;
2884 TREE_NOTHROW (fn) = 1;
2885 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2886 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2888 block_clear_fn = fn;
2891 if (asmspec)
2892 set_user_assembler_name (block_clear_fn, asmspec);
2895 static tree
2896 clear_storage_libcall_fn (int for_call)
2898 static bool emitted_extern;
2900 if (!block_clear_fn)
2901 init_block_clear_fn (NULL);
2903 if (for_call && !emitted_extern)
2905 emitted_extern = true;
2906 make_decl_rtl (block_clear_fn);
2909 return block_clear_fn;
2912 /* Expand a setmem pattern; return true if successful. */
2914 bool
2915 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2916 unsigned int expected_align, HOST_WIDE_INT expected_size,
2917 unsigned HOST_WIDE_INT min_size,
2918 unsigned HOST_WIDE_INT max_size,
2919 unsigned HOST_WIDE_INT probable_max_size)
2921 /* Try the most limited insn first, because there's no point
2922 including more than one in the machine description unless
2923 the more limited one has some advantage. */
2925 machine_mode mode;
2927 if (expected_align < align)
2928 expected_align = align;
2929 if (expected_size != -1)
2931 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2932 expected_size = max_size;
2933 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2934 expected_size = min_size;
2937 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2938 mode = GET_MODE_WIDER_MODE (mode))
2940 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2942 if (code != CODE_FOR_nothing
2943 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2944 here because if SIZE is less than the mode mask, as it is
2945 returned by the macro, it will definitely be less than the
2946 actual mode mask. Since SIZE is within the Pmode address
2947 space, we limit MODE to Pmode. */
2948 && ((CONST_INT_P (size)
2949 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2950 <= (GET_MODE_MASK (mode) >> 1)))
2951 || max_size <= (GET_MODE_MASK (mode) >> 1)
2952 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2954 struct expand_operand ops[9];
2955 unsigned int nops;
2957 nops = insn_data[(int) code].n_generator_args;
2958 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2960 create_fixed_operand (&ops[0], object);
2961 /* The check above guarantees that this size conversion is valid. */
2962 create_convert_operand_to (&ops[1], size, mode, true);
2963 create_convert_operand_from (&ops[2], val, byte_mode, true);
2964 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2965 if (nops >= 6)
2967 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2968 create_integer_operand (&ops[5], expected_size);
2970 if (nops >= 8)
2972 create_integer_operand (&ops[6], min_size);
2973 /* If we can not represent the maximal size,
2974 make parameter NULL. */
2975 if ((HOST_WIDE_INT) max_size != -1)
2976 create_integer_operand (&ops[7], max_size);
2977 else
2978 create_fixed_operand (&ops[7], NULL);
2980 if (nops == 9)
2982 /* If we can not represent the maximal size,
2983 make parameter NULL. */
2984 if ((HOST_WIDE_INT) probable_max_size != -1)
2985 create_integer_operand (&ops[8], probable_max_size);
2986 else
2987 create_fixed_operand (&ops[8], NULL);
2989 if (maybe_expand_insn (code, nops, ops))
2990 return true;
2994 return false;
2998 /* Write to one of the components of the complex value CPLX. Write VAL to
2999 the real part if IMAG_P is false, and the imaginary part if its true. */
3001 void
3002 write_complex_part (rtx cplx, rtx val, bool imag_p)
3004 machine_mode cmode;
3005 machine_mode imode;
3006 unsigned ibitsize;
3008 if (GET_CODE (cplx) == CONCAT)
3010 emit_move_insn (XEXP (cplx, imag_p), val);
3011 return;
3014 cmode = GET_MODE (cplx);
3015 imode = GET_MODE_INNER (cmode);
3016 ibitsize = GET_MODE_BITSIZE (imode);
3018 /* For MEMs simplify_gen_subreg may generate an invalid new address
3019 because, e.g., the original address is considered mode-dependent
3020 by the target, which restricts simplify_subreg from invoking
3021 adjust_address_nv. Instead of preparing fallback support for an
3022 invalid address, we call adjust_address_nv directly. */
3023 if (MEM_P (cplx))
3025 emit_move_insn (adjust_address_nv (cplx, imode,
3026 imag_p ? GET_MODE_SIZE (imode) : 0),
3027 val);
3028 return;
3031 /* If the sub-object is at least word sized, then we know that subregging
3032 will work. This special case is important, since store_bit_field
3033 wants to operate on integer modes, and there's rarely an OImode to
3034 correspond to TCmode. */
3035 if (ibitsize >= BITS_PER_WORD
3036 /* For hard regs we have exact predicates. Assume we can split
3037 the original object if it spans an even number of hard regs.
3038 This special case is important for SCmode on 64-bit platforms
3039 where the natural size of floating-point regs is 32-bit. */
3040 || (REG_P (cplx)
3041 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3042 && REG_NREGS (cplx) % 2 == 0))
3044 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3045 imag_p ? GET_MODE_SIZE (imode) : 0);
3046 if (part)
3048 emit_move_insn (part, val);
3049 return;
3051 else
3052 /* simplify_gen_subreg may fail for sub-word MEMs. */
3053 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3056 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3059 /* Extract one of the components of the complex value CPLX. Extract the
3060 real part if IMAG_P is false, and the imaginary part if it's true. */
3062 static rtx
3063 read_complex_part (rtx cplx, bool imag_p)
3065 machine_mode cmode, imode;
3066 unsigned ibitsize;
3068 if (GET_CODE (cplx) == CONCAT)
3069 return XEXP (cplx, imag_p);
3071 cmode = GET_MODE (cplx);
3072 imode = GET_MODE_INNER (cmode);
3073 ibitsize = GET_MODE_BITSIZE (imode);
3075 /* Special case reads from complex constants that got spilled to memory. */
3076 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3078 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3079 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3081 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3082 if (CONSTANT_CLASS_P (part))
3083 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3087 /* For MEMs simplify_gen_subreg may generate an invalid new address
3088 because, e.g., the original address is considered mode-dependent
3089 by the target, which restricts simplify_subreg from invoking
3090 adjust_address_nv. Instead of preparing fallback support for an
3091 invalid address, we call adjust_address_nv directly. */
3092 if (MEM_P (cplx))
3093 return adjust_address_nv (cplx, imode,
3094 imag_p ? GET_MODE_SIZE (imode) : 0);
3096 /* If the sub-object is at least word sized, then we know that subregging
3097 will work. This special case is important, since extract_bit_field
3098 wants to operate on integer modes, and there's rarely an OImode to
3099 correspond to TCmode. */
3100 if (ibitsize >= BITS_PER_WORD
3101 /* For hard regs we have exact predicates. Assume we can split
3102 the original object if it spans an even number of hard regs.
3103 This special case is important for SCmode on 64-bit platforms
3104 where the natural size of floating-point regs is 32-bit. */
3105 || (REG_P (cplx)
3106 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3107 && REG_NREGS (cplx) % 2 == 0))
3109 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3110 imag_p ? GET_MODE_SIZE (imode) : 0);
3111 if (ret)
3112 return ret;
3113 else
3114 /* simplify_gen_subreg may fail for sub-word MEMs. */
3115 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3118 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3119 true, NULL_RTX, imode, imode);
3122 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3123 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3124 represented in NEW_MODE. If FORCE is true, this will never happen, as
3125 we'll force-create a SUBREG if needed. */
3127 static rtx
3128 emit_move_change_mode (machine_mode new_mode,
3129 machine_mode old_mode, rtx x, bool force)
3131 rtx ret;
3133 if (push_operand (x, GET_MODE (x)))
3135 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3136 MEM_COPY_ATTRIBUTES (ret, x);
3138 else if (MEM_P (x))
3140 /* We don't have to worry about changing the address since the
3141 size in bytes is supposed to be the same. */
3142 if (reload_in_progress)
3144 /* Copy the MEM to change the mode and move any
3145 substitutions from the old MEM to the new one. */
3146 ret = adjust_address_nv (x, new_mode, 0);
3147 copy_replacements (x, ret);
3149 else
3150 ret = adjust_address (x, new_mode, 0);
3152 else
3154 /* Note that we do want simplify_subreg's behavior of validating
3155 that the new mode is ok for a hard register. If we were to use
3156 simplify_gen_subreg, we would create the subreg, but would
3157 probably run into the target not being able to implement it. */
3158 /* Except, of course, when FORCE is true, when this is exactly what
3159 we want. Which is needed for CCmodes on some targets. */
3160 if (force)
3161 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3162 else
3163 ret = simplify_subreg (new_mode, x, old_mode, 0);
3166 return ret;
3169 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3170 an integer mode of the same size as MODE. Returns the instruction
3171 emitted, or NULL if such a move could not be generated. */
3173 static rtx_insn *
3174 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3176 machine_mode imode;
3177 enum insn_code code;
3179 /* There must exist a mode of the exact size we require. */
3180 imode = int_mode_for_mode (mode);
3181 if (imode == BLKmode)
3182 return NULL;
3184 /* The target must support moves in this mode. */
3185 code = optab_handler (mov_optab, imode);
3186 if (code == CODE_FOR_nothing)
3187 return NULL;
3189 x = emit_move_change_mode (imode, mode, x, force);
3190 if (x == NULL_RTX)
3191 return NULL;
3192 y = emit_move_change_mode (imode, mode, y, force);
3193 if (y == NULL_RTX)
3194 return NULL;
3195 return emit_insn (GEN_FCN (code) (x, y));
3198 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3199 Return an equivalent MEM that does not use an auto-increment. */
3202 emit_move_resolve_push (machine_mode mode, rtx x)
3204 enum rtx_code code = GET_CODE (XEXP (x, 0));
3205 HOST_WIDE_INT adjust;
3206 rtx temp;
3208 adjust = GET_MODE_SIZE (mode);
3209 #ifdef PUSH_ROUNDING
3210 adjust = PUSH_ROUNDING (adjust);
3211 #endif
3212 if (code == PRE_DEC || code == POST_DEC)
3213 adjust = -adjust;
3214 else if (code == PRE_MODIFY || code == POST_MODIFY)
3216 rtx expr = XEXP (XEXP (x, 0), 1);
3217 HOST_WIDE_INT val;
3219 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3220 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3221 val = INTVAL (XEXP (expr, 1));
3222 if (GET_CODE (expr) == MINUS)
3223 val = -val;
3224 gcc_assert (adjust == val || adjust == -val);
3225 adjust = val;
3228 /* Do not use anti_adjust_stack, since we don't want to update
3229 stack_pointer_delta. */
3230 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3231 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3232 0, OPTAB_LIB_WIDEN);
3233 if (temp != stack_pointer_rtx)
3234 emit_move_insn (stack_pointer_rtx, temp);
3236 switch (code)
3238 case PRE_INC:
3239 case PRE_DEC:
3240 case PRE_MODIFY:
3241 temp = stack_pointer_rtx;
3242 break;
3243 case POST_INC:
3244 case POST_DEC:
3245 case POST_MODIFY:
3246 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3247 break;
3248 default:
3249 gcc_unreachable ();
3252 return replace_equiv_address (x, temp);
3255 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3256 X is known to satisfy push_operand, and MODE is known to be complex.
3257 Returns the last instruction emitted. */
3259 rtx_insn *
3260 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3262 machine_mode submode = GET_MODE_INNER (mode);
3263 bool imag_first;
3265 #ifdef PUSH_ROUNDING
3266 unsigned int submodesize = GET_MODE_SIZE (submode);
3268 /* In case we output to the stack, but the size is smaller than the
3269 machine can push exactly, we need to use move instructions. */
3270 if (PUSH_ROUNDING (submodesize) != submodesize)
3272 x = emit_move_resolve_push (mode, x);
3273 return emit_move_insn (x, y);
3275 #endif
3277 /* Note that the real part always precedes the imag part in memory
3278 regardless of machine's endianness. */
3279 switch (GET_CODE (XEXP (x, 0)))
3281 case PRE_DEC:
3282 case POST_DEC:
3283 imag_first = true;
3284 break;
3285 case PRE_INC:
3286 case POST_INC:
3287 imag_first = false;
3288 break;
3289 default:
3290 gcc_unreachable ();
3293 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3294 read_complex_part (y, imag_first));
3295 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3296 read_complex_part (y, !imag_first));
3299 /* A subroutine of emit_move_complex. Perform the move from Y to X
3300 via two moves of the parts. Returns the last instruction emitted. */
3302 rtx_insn *
3303 emit_move_complex_parts (rtx x, rtx y)
3305 /* Show the output dies here. This is necessary for SUBREGs
3306 of pseudos since we cannot track their lifetimes correctly;
3307 hard regs shouldn't appear here except as return values. */
3308 if (!reload_completed && !reload_in_progress
3309 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3310 emit_clobber (x);
3312 write_complex_part (x, read_complex_part (y, false), false);
3313 write_complex_part (x, read_complex_part (y, true), true);
3315 return get_last_insn ();
3318 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3319 MODE is known to be complex. Returns the last instruction emitted. */
3321 static rtx_insn *
3322 emit_move_complex (machine_mode mode, rtx x, rtx y)
3324 bool try_int;
3326 /* Need to take special care for pushes, to maintain proper ordering
3327 of the data, and possibly extra padding. */
3328 if (push_operand (x, mode))
3329 return emit_move_complex_push (mode, x, y);
3331 /* See if we can coerce the target into moving both values at once, except
3332 for floating point where we favor moving as parts if this is easy. */
3333 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3334 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3335 && !(REG_P (x)
3336 && HARD_REGISTER_P (x)
3337 && REG_NREGS (x) == 1)
3338 && !(REG_P (y)
3339 && HARD_REGISTER_P (y)
3340 && REG_NREGS (y) == 1))
3341 try_int = false;
3342 /* Not possible if the values are inherently not adjacent. */
3343 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3344 try_int = false;
3345 /* Is possible if both are registers (or subregs of registers). */
3346 else if (register_operand (x, mode) && register_operand (y, mode))
3347 try_int = true;
3348 /* If one of the operands is a memory, and alignment constraints
3349 are friendly enough, we may be able to do combined memory operations.
3350 We do not attempt this if Y is a constant because that combination is
3351 usually better with the by-parts thing below. */
3352 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3353 && (!STRICT_ALIGNMENT
3354 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3355 try_int = true;
3356 else
3357 try_int = false;
3359 if (try_int)
3361 rtx_insn *ret;
3363 /* For memory to memory moves, optimal behavior can be had with the
3364 existing block move logic. */
3365 if (MEM_P (x) && MEM_P (y))
3367 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3368 BLOCK_OP_NO_LIBCALL);
3369 return get_last_insn ();
3372 ret = emit_move_via_integer (mode, x, y, true);
3373 if (ret)
3374 return ret;
3377 return emit_move_complex_parts (x, y);
3380 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3381 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3383 static rtx_insn *
3384 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3386 rtx_insn *ret;
3388 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3389 if (mode != CCmode)
3391 enum insn_code code = optab_handler (mov_optab, CCmode);
3392 if (code != CODE_FOR_nothing)
3394 x = emit_move_change_mode (CCmode, mode, x, true);
3395 y = emit_move_change_mode (CCmode, mode, y, true);
3396 return emit_insn (GEN_FCN (code) (x, y));
3400 /* Otherwise, find the MODE_INT mode of the same width. */
3401 ret = emit_move_via_integer (mode, x, y, false);
3402 gcc_assert (ret != NULL);
3403 return ret;
3406 /* Return true if word I of OP lies entirely in the
3407 undefined bits of a paradoxical subreg. */
3409 static bool
3410 undefined_operand_subword_p (const_rtx op, int i)
3412 machine_mode innermode, innermostmode;
3413 int offset;
3414 if (GET_CODE (op) != SUBREG)
3415 return false;
3416 innermode = GET_MODE (op);
3417 innermostmode = GET_MODE (SUBREG_REG (op));
3418 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3419 /* The SUBREG_BYTE represents offset, as if the value were stored in
3420 memory, except for a paradoxical subreg where we define
3421 SUBREG_BYTE to be 0; undo this exception as in
3422 simplify_subreg. */
3423 if (SUBREG_BYTE (op) == 0
3424 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3426 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3427 if (WORDS_BIG_ENDIAN)
3428 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3429 if (BYTES_BIG_ENDIAN)
3430 offset += difference % UNITS_PER_WORD;
3432 if (offset >= GET_MODE_SIZE (innermostmode)
3433 || offset <= -GET_MODE_SIZE (word_mode))
3434 return true;
3435 return false;
3438 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3439 MODE is any multi-word or full-word mode that lacks a move_insn
3440 pattern. Note that you will get better code if you define such
3441 patterns, even if they must turn into multiple assembler instructions. */
3443 static rtx_insn *
3444 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3446 rtx_insn *last_insn = 0;
3447 rtx_insn *seq;
3448 rtx inner;
3449 bool need_clobber;
3450 int i;
3452 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3454 /* If X is a push on the stack, do the push now and replace
3455 X with a reference to the stack pointer. */
3456 if (push_operand (x, mode))
3457 x = emit_move_resolve_push (mode, x);
3459 /* If we are in reload, see if either operand is a MEM whose address
3460 is scheduled for replacement. */
3461 if (reload_in_progress && MEM_P (x)
3462 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3463 x = replace_equiv_address_nv (x, inner);
3464 if (reload_in_progress && MEM_P (y)
3465 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3466 y = replace_equiv_address_nv (y, inner);
3468 start_sequence ();
3470 need_clobber = false;
3471 for (i = 0;
3472 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3473 i++)
3475 rtx xpart = operand_subword (x, i, 1, mode);
3476 rtx ypart;
3478 /* Do not generate code for a move if it would come entirely
3479 from the undefined bits of a paradoxical subreg. */
3480 if (undefined_operand_subword_p (y, i))
3481 continue;
3483 ypart = operand_subword (y, i, 1, mode);
3485 /* If we can't get a part of Y, put Y into memory if it is a
3486 constant. Otherwise, force it into a register. Then we must
3487 be able to get a part of Y. */
3488 if (ypart == 0 && CONSTANT_P (y))
3490 y = use_anchored_address (force_const_mem (mode, y));
3491 ypart = operand_subword (y, i, 1, mode);
3493 else if (ypart == 0)
3494 ypart = operand_subword_force (y, i, mode);
3496 gcc_assert (xpart && ypart);
3498 need_clobber |= (GET_CODE (xpart) == SUBREG);
3500 last_insn = emit_move_insn (xpart, ypart);
3503 seq = get_insns ();
3504 end_sequence ();
3506 /* Show the output dies here. This is necessary for SUBREGs
3507 of pseudos since we cannot track their lifetimes correctly;
3508 hard regs shouldn't appear here except as return values.
3509 We never want to emit such a clobber after reload. */
3510 if (x != y
3511 && ! (reload_in_progress || reload_completed)
3512 && need_clobber != 0)
3513 emit_clobber (x);
3515 emit_insn (seq);
3517 return last_insn;
3520 /* Low level part of emit_move_insn.
3521 Called just like emit_move_insn, but assumes X and Y
3522 are basically valid. */
3524 rtx_insn *
3525 emit_move_insn_1 (rtx x, rtx y)
3527 machine_mode mode = GET_MODE (x);
3528 enum insn_code code;
3530 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3532 code = optab_handler (mov_optab, mode);
3533 if (code != CODE_FOR_nothing)
3534 return emit_insn (GEN_FCN (code) (x, y));
3536 /* Expand complex moves by moving real part and imag part. */
3537 if (COMPLEX_MODE_P (mode))
3538 return emit_move_complex (mode, x, y);
3540 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3541 || ALL_FIXED_POINT_MODE_P (mode))
3543 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3545 /* If we can't find an integer mode, use multi words. */
3546 if (result)
3547 return result;
3548 else
3549 return emit_move_multi_word (mode, x, y);
3552 if (GET_MODE_CLASS (mode) == MODE_CC)
3553 return emit_move_ccmode (mode, x, y);
3555 /* Try using a move pattern for the corresponding integer mode. This is
3556 only safe when simplify_subreg can convert MODE constants into integer
3557 constants. At present, it can only do this reliably if the value
3558 fits within a HOST_WIDE_INT. */
3559 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3561 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3563 if (ret)
3565 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3566 return ret;
3570 return emit_move_multi_word (mode, x, y);
3573 /* Generate code to copy Y into X.
3574 Both Y and X must have the same mode, except that
3575 Y can be a constant with VOIDmode.
3576 This mode cannot be BLKmode; use emit_block_move for that.
3578 Return the last instruction emitted. */
3580 rtx_insn *
3581 emit_move_insn (rtx x, rtx y)
3583 machine_mode mode = GET_MODE (x);
3584 rtx y_cst = NULL_RTX;
3585 rtx_insn *last_insn;
3586 rtx set;
3588 gcc_assert (mode != BLKmode
3589 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3591 if (CONSTANT_P (y))
3593 if (optimize
3594 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3595 && (last_insn = compress_float_constant (x, y)))
3596 return last_insn;
3598 y_cst = y;
3600 if (!targetm.legitimate_constant_p (mode, y))
3602 y = force_const_mem (mode, y);
3604 /* If the target's cannot_force_const_mem prevented the spill,
3605 assume that the target's move expanders will also take care
3606 of the non-legitimate constant. */
3607 if (!y)
3608 y = y_cst;
3609 else
3610 y = use_anchored_address (y);
3614 /* If X or Y are memory references, verify that their addresses are valid
3615 for the machine. */
3616 if (MEM_P (x)
3617 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3618 MEM_ADDR_SPACE (x))
3619 && ! push_operand (x, GET_MODE (x))))
3620 x = validize_mem (x);
3622 if (MEM_P (y)
3623 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3624 MEM_ADDR_SPACE (y)))
3625 y = validize_mem (y);
3627 gcc_assert (mode != BLKmode);
3629 last_insn = emit_move_insn_1 (x, y);
3631 if (y_cst && REG_P (x)
3632 && (set = single_set (last_insn)) != NULL_RTX
3633 && SET_DEST (set) == x
3634 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3635 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3637 return last_insn;
3640 /* Generate the body of an instruction to copy Y into X.
3641 It may be a list of insns, if one insn isn't enough. */
3643 rtx_insn *
3644 gen_move_insn (rtx x, rtx y)
3646 rtx_insn *seq;
3648 start_sequence ();
3649 emit_move_insn_1 (x, y);
3650 seq = get_insns ();
3651 end_sequence ();
3652 return seq;
3655 /* Same as above, but return rtx (used as a callback, which must have
3656 prototype compatible with other functions returning rtx). */
3659 gen_move_insn_uncast (rtx x, rtx y)
3661 return gen_move_insn (x, y);
3664 /* If Y is representable exactly in a narrower mode, and the target can
3665 perform the extension directly from constant or memory, then emit the
3666 move as an extension. */
3668 static rtx_insn *
3669 compress_float_constant (rtx x, rtx y)
3671 machine_mode dstmode = GET_MODE (x);
3672 machine_mode orig_srcmode = GET_MODE (y);
3673 machine_mode srcmode;
3674 REAL_VALUE_TYPE r;
3675 int oldcost, newcost;
3676 bool speed = optimize_insn_for_speed_p ();
3678 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3680 if (targetm.legitimate_constant_p (dstmode, y))
3681 oldcost = set_src_cost (y, speed);
3682 else
3683 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3685 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3686 srcmode != orig_srcmode;
3687 srcmode = GET_MODE_WIDER_MODE (srcmode))
3689 enum insn_code ic;
3690 rtx trunc_y;
3691 rtx_insn *last_insn;
3693 /* Skip if the target can't extend this way. */
3694 ic = can_extend_p (dstmode, srcmode, 0);
3695 if (ic == CODE_FOR_nothing)
3696 continue;
3698 /* Skip if the narrowed value isn't exact. */
3699 if (! exact_real_truncate (srcmode, &r))
3700 continue;
3702 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3704 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3706 /* Skip if the target needs extra instructions to perform
3707 the extension. */
3708 if (!insn_operand_matches (ic, 1, trunc_y))
3709 continue;
3710 /* This is valid, but may not be cheaper than the original. */
3711 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3712 speed);
3713 if (oldcost < newcost)
3714 continue;
3716 else if (float_extend_from_mem[dstmode][srcmode])
3718 trunc_y = force_const_mem (srcmode, trunc_y);
3719 /* This is valid, but may not be cheaper than the original. */
3720 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3721 speed);
3722 if (oldcost < newcost)
3723 continue;
3724 trunc_y = validize_mem (trunc_y);
3726 else
3727 continue;
3729 /* For CSE's benefit, force the compressed constant pool entry
3730 into a new pseudo. This constant may be used in different modes,
3731 and if not, combine will put things back together for us. */
3732 trunc_y = force_reg (srcmode, trunc_y);
3734 /* If x is a hard register, perform the extension into a pseudo,
3735 so that e.g. stack realignment code is aware of it. */
3736 rtx target = x;
3737 if (REG_P (x) && HARD_REGISTER_P (x))
3738 target = gen_reg_rtx (dstmode);
3740 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3741 last_insn = get_last_insn ();
3743 if (REG_P (target))
3744 set_unique_reg_note (last_insn, REG_EQUAL, y);
3746 if (target != x)
3747 return emit_move_insn (x, target);
3748 return last_insn;
3751 return NULL;
3754 /* Pushing data onto the stack. */
3756 /* Push a block of length SIZE (perhaps variable)
3757 and return an rtx to address the beginning of the block.
3758 The value may be virtual_outgoing_args_rtx.
3760 EXTRA is the number of bytes of padding to push in addition to SIZE.
3761 BELOW nonzero means this padding comes at low addresses;
3762 otherwise, the padding comes at high addresses. */
3765 push_block (rtx size, int extra, int below)
3767 rtx temp;
3769 size = convert_modes (Pmode, ptr_mode, size, 1);
3770 if (CONSTANT_P (size))
3771 anti_adjust_stack (plus_constant (Pmode, size, extra));
3772 else if (REG_P (size) && extra == 0)
3773 anti_adjust_stack (size);
3774 else
3776 temp = copy_to_mode_reg (Pmode, size);
3777 if (extra != 0)
3778 temp = expand_binop (Pmode, add_optab, temp,
3779 gen_int_mode (extra, Pmode),
3780 temp, 0, OPTAB_LIB_WIDEN);
3781 anti_adjust_stack (temp);
3784 if (STACK_GROWS_DOWNWARD)
3786 temp = virtual_outgoing_args_rtx;
3787 if (extra != 0 && below)
3788 temp = plus_constant (Pmode, temp, extra);
3790 else
3792 if (CONST_INT_P (size))
3793 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3794 -INTVAL (size) - (below ? 0 : extra));
3795 else if (extra != 0 && !below)
3796 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3797 negate_rtx (Pmode, plus_constant (Pmode, size,
3798 extra)));
3799 else
3800 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3801 negate_rtx (Pmode, size));
3804 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3807 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3809 static rtx
3810 mem_autoinc_base (rtx mem)
3812 if (MEM_P (mem))
3814 rtx addr = XEXP (mem, 0);
3815 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3816 return XEXP (addr, 0);
3818 return NULL;
3821 /* A utility routine used here, in reload, and in try_split. The insns
3822 after PREV up to and including LAST are known to adjust the stack,
3823 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3824 placing notes as appropriate. PREV may be NULL, indicating the
3825 entire insn sequence prior to LAST should be scanned.
3827 The set of allowed stack pointer modifications is small:
3828 (1) One or more auto-inc style memory references (aka pushes),
3829 (2) One or more addition/subtraction with the SP as destination,
3830 (3) A single move insn with the SP as destination,
3831 (4) A call_pop insn,
3832 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3834 Insns in the sequence that do not modify the SP are ignored,
3835 except for noreturn calls.
3837 The return value is the amount of adjustment that can be trivially
3838 verified, via immediate operand or auto-inc. If the adjustment
3839 cannot be trivially extracted, the return value is INT_MIN. */
3841 HOST_WIDE_INT
3842 find_args_size_adjust (rtx_insn *insn)
3844 rtx dest, set, pat;
3845 int i;
3847 pat = PATTERN (insn);
3848 set = NULL;
3850 /* Look for a call_pop pattern. */
3851 if (CALL_P (insn))
3853 /* We have to allow non-call_pop patterns for the case
3854 of emit_single_push_insn of a TLS address. */
3855 if (GET_CODE (pat) != PARALLEL)
3856 return 0;
3858 /* All call_pop have a stack pointer adjust in the parallel.
3859 The call itself is always first, and the stack adjust is
3860 usually last, so search from the end. */
3861 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3863 set = XVECEXP (pat, 0, i);
3864 if (GET_CODE (set) != SET)
3865 continue;
3866 dest = SET_DEST (set);
3867 if (dest == stack_pointer_rtx)
3868 break;
3870 /* We'd better have found the stack pointer adjust. */
3871 if (i == 0)
3872 return 0;
3873 /* Fall through to process the extracted SET and DEST
3874 as if it was a standalone insn. */
3876 else if (GET_CODE (pat) == SET)
3877 set = pat;
3878 else if ((set = single_set (insn)) != NULL)
3880 else if (GET_CODE (pat) == PARALLEL)
3882 /* ??? Some older ports use a parallel with a stack adjust
3883 and a store for a PUSH_ROUNDING pattern, rather than a
3884 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3885 /* ??? See h8300 and m68k, pushqi1. */
3886 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3888 set = XVECEXP (pat, 0, i);
3889 if (GET_CODE (set) != SET)
3890 continue;
3891 dest = SET_DEST (set);
3892 if (dest == stack_pointer_rtx)
3893 break;
3895 /* We do not expect an auto-inc of the sp in the parallel. */
3896 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3897 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3898 != stack_pointer_rtx);
3900 if (i < 0)
3901 return 0;
3903 else
3904 return 0;
3906 dest = SET_DEST (set);
3908 /* Look for direct modifications of the stack pointer. */
3909 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3911 /* Look for a trivial adjustment, otherwise assume nothing. */
3912 /* Note that the SPU restore_stack_block pattern refers to
3913 the stack pointer in V4SImode. Consider that non-trivial. */
3914 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3915 && GET_CODE (SET_SRC (set)) == PLUS
3916 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3917 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3918 return INTVAL (XEXP (SET_SRC (set), 1));
3919 /* ??? Reload can generate no-op moves, which will be cleaned
3920 up later. Recognize it and continue searching. */
3921 else if (rtx_equal_p (dest, SET_SRC (set)))
3922 return 0;
3923 else
3924 return HOST_WIDE_INT_MIN;
3926 else
3928 rtx mem, addr;
3930 /* Otherwise only think about autoinc patterns. */
3931 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3933 mem = dest;
3934 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3935 != stack_pointer_rtx);
3937 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3938 mem = SET_SRC (set);
3939 else
3940 return 0;
3942 addr = XEXP (mem, 0);
3943 switch (GET_CODE (addr))
3945 case PRE_INC:
3946 case POST_INC:
3947 return GET_MODE_SIZE (GET_MODE (mem));
3948 case PRE_DEC:
3949 case POST_DEC:
3950 return -GET_MODE_SIZE (GET_MODE (mem));
3951 case PRE_MODIFY:
3952 case POST_MODIFY:
3953 addr = XEXP (addr, 1);
3954 gcc_assert (GET_CODE (addr) == PLUS);
3955 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3956 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3957 return INTVAL (XEXP (addr, 1));
3958 default:
3959 gcc_unreachable ();
3965 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
3967 int args_size = end_args_size;
3968 bool saw_unknown = false;
3969 rtx_insn *insn;
3971 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3973 HOST_WIDE_INT this_delta;
3975 if (!NONDEBUG_INSN_P (insn))
3976 continue;
3978 this_delta = find_args_size_adjust (insn);
3979 if (this_delta == 0)
3981 if (!CALL_P (insn)
3982 || ACCUMULATE_OUTGOING_ARGS
3983 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3984 continue;
3987 gcc_assert (!saw_unknown);
3988 if (this_delta == HOST_WIDE_INT_MIN)
3989 saw_unknown = true;
3991 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3992 if (STACK_GROWS_DOWNWARD)
3993 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3995 args_size -= this_delta;
3998 return saw_unknown ? INT_MIN : args_size;
4001 #ifdef PUSH_ROUNDING
4002 /* Emit single push insn. */
4004 static void
4005 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4007 rtx dest_addr;
4008 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4009 rtx dest;
4010 enum insn_code icode;
4012 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4013 /* If there is push pattern, use it. Otherwise try old way of throwing
4014 MEM representing push operation to move expander. */
4015 icode = optab_handler (push_optab, mode);
4016 if (icode != CODE_FOR_nothing)
4018 struct expand_operand ops[1];
4020 create_input_operand (&ops[0], x, mode);
4021 if (maybe_expand_insn (icode, 1, ops))
4022 return;
4024 if (GET_MODE_SIZE (mode) == rounded_size)
4025 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4026 /* If we are to pad downward, adjust the stack pointer first and
4027 then store X into the stack location using an offset. This is
4028 because emit_move_insn does not know how to pad; it does not have
4029 access to type. */
4030 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4032 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4033 HOST_WIDE_INT offset;
4035 emit_move_insn (stack_pointer_rtx,
4036 expand_binop (Pmode,
4037 STACK_GROWS_DOWNWARD ? sub_optab
4038 : add_optab,
4039 stack_pointer_rtx,
4040 gen_int_mode (rounded_size, Pmode),
4041 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4043 offset = (HOST_WIDE_INT) padding_size;
4044 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4045 /* We have already decremented the stack pointer, so get the
4046 previous value. */
4047 offset += (HOST_WIDE_INT) rounded_size;
4049 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4050 /* We have already incremented the stack pointer, so get the
4051 previous value. */
4052 offset -= (HOST_WIDE_INT) rounded_size;
4054 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4055 gen_int_mode (offset, Pmode));
4057 else
4059 if (STACK_GROWS_DOWNWARD)
4060 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4061 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4062 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4063 Pmode));
4064 else
4065 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4066 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4067 gen_int_mode (rounded_size, Pmode));
4069 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4072 dest = gen_rtx_MEM (mode, dest_addr);
4074 if (type != 0)
4076 set_mem_attributes (dest, type, 1);
4078 if (cfun->tail_call_marked)
4079 /* Function incoming arguments may overlap with sibling call
4080 outgoing arguments and we cannot allow reordering of reads
4081 from function arguments with stores to outgoing arguments
4082 of sibling calls. */
4083 set_mem_alias_set (dest, 0);
4085 emit_move_insn (dest, x);
4088 /* Emit and annotate a single push insn. */
4090 static void
4091 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4093 int delta, old_delta = stack_pointer_delta;
4094 rtx_insn *prev = get_last_insn ();
4095 rtx_insn *last;
4097 emit_single_push_insn_1 (mode, x, type);
4099 last = get_last_insn ();
4101 /* Notice the common case where we emitted exactly one insn. */
4102 if (PREV_INSN (last) == prev)
4104 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4105 return;
4108 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4109 gcc_assert (delta == INT_MIN || delta == old_delta);
4111 #endif
4113 /* Generate code to push X onto the stack, assuming it has mode MODE and
4114 type TYPE.
4115 MODE is redundant except when X is a CONST_INT (since they don't
4116 carry mode info).
4117 SIZE is an rtx for the size of data to be copied (in bytes),
4118 needed only if X is BLKmode.
4120 ALIGN (in bits) is maximum alignment we can assume.
4122 If PARTIAL and REG are both nonzero, then copy that many of the first
4123 bytes of X into registers starting with REG, and push the rest of X.
4124 The amount of space pushed is decreased by PARTIAL bytes.
4125 REG must be a hard register in this case.
4126 If REG is zero but PARTIAL is not, take any all others actions for an
4127 argument partially in registers, but do not actually load any
4128 registers.
4130 EXTRA is the amount in bytes of extra space to leave next to this arg.
4131 This is ignored if an argument block has already been allocated.
4133 On a machine that lacks real push insns, ARGS_ADDR is the address of
4134 the bottom of the argument block for this call. We use indexing off there
4135 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4136 argument block has not been preallocated.
4138 ARGS_SO_FAR is the size of args previously pushed for this call.
4140 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4141 for arguments passed in registers. If nonzero, it will be the number
4142 of bytes required. */
4144 void
4145 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4146 unsigned int align, int partial, rtx reg, int extra,
4147 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4148 rtx alignment_pad)
4150 rtx xinner;
4151 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4153 /* Decide where to pad the argument: `downward' for below,
4154 `upward' for above, or `none' for don't pad it.
4155 Default is below for small data on big-endian machines; else above. */
4156 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4158 /* Invert direction if stack is post-decrement.
4159 FIXME: why? */
4160 if (STACK_PUSH_CODE == POST_DEC)
4161 if (where_pad != none)
4162 where_pad = (where_pad == downward ? upward : downward);
4164 xinner = x;
4166 if (mode == BLKmode
4167 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4169 /* Copy a block into the stack, entirely or partially. */
4171 rtx temp;
4172 int used;
4173 int offset;
4174 int skip;
4176 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4177 used = partial - offset;
4179 if (mode != BLKmode)
4181 /* A value is to be stored in an insufficiently aligned
4182 stack slot; copy via a suitably aligned slot if
4183 necessary. */
4184 size = GEN_INT (GET_MODE_SIZE (mode));
4185 if (!MEM_P (xinner))
4187 temp = assign_temp (type, 1, 1);
4188 emit_move_insn (temp, xinner);
4189 xinner = temp;
4193 gcc_assert (size);
4195 /* USED is now the # of bytes we need not copy to the stack
4196 because registers will take care of them. */
4198 if (partial != 0)
4199 xinner = adjust_address (xinner, BLKmode, used);
4201 /* If the partial register-part of the arg counts in its stack size,
4202 skip the part of stack space corresponding to the registers.
4203 Otherwise, start copying to the beginning of the stack space,
4204 by setting SKIP to 0. */
4205 skip = (reg_parm_stack_space == 0) ? 0 : used;
4207 #ifdef PUSH_ROUNDING
4208 /* Do it with several push insns if that doesn't take lots of insns
4209 and if there is no difficulty with push insns that skip bytes
4210 on the stack for alignment purposes. */
4211 if (args_addr == 0
4212 && PUSH_ARGS
4213 && CONST_INT_P (size)
4214 && skip == 0
4215 && MEM_ALIGN (xinner) >= align
4216 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4217 /* Here we avoid the case of a structure whose weak alignment
4218 forces many pushes of a small amount of data,
4219 and such small pushes do rounding that causes trouble. */
4220 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4221 || align >= BIGGEST_ALIGNMENT
4222 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4223 == (align / BITS_PER_UNIT)))
4224 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4226 /* Push padding now if padding above and stack grows down,
4227 or if padding below and stack grows up.
4228 But if space already allocated, this has already been done. */
4229 if (extra && args_addr == 0
4230 && where_pad != none && where_pad != stack_direction)
4231 anti_adjust_stack (GEN_INT (extra));
4233 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4235 else
4236 #endif /* PUSH_ROUNDING */
4238 rtx target;
4240 /* Otherwise make space on the stack and copy the data
4241 to the address of that space. */
4243 /* Deduct words put into registers from the size we must copy. */
4244 if (partial != 0)
4246 if (CONST_INT_P (size))
4247 size = GEN_INT (INTVAL (size) - used);
4248 else
4249 size = expand_binop (GET_MODE (size), sub_optab, size,
4250 gen_int_mode (used, GET_MODE (size)),
4251 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4254 /* Get the address of the stack space.
4255 In this case, we do not deal with EXTRA separately.
4256 A single stack adjust will do. */
4257 if (! args_addr)
4259 temp = push_block (size, extra, where_pad == downward);
4260 extra = 0;
4262 else if (CONST_INT_P (args_so_far))
4263 temp = memory_address (BLKmode,
4264 plus_constant (Pmode, args_addr,
4265 skip + INTVAL (args_so_far)));
4266 else
4267 temp = memory_address (BLKmode,
4268 plus_constant (Pmode,
4269 gen_rtx_PLUS (Pmode,
4270 args_addr,
4271 args_so_far),
4272 skip));
4274 if (!ACCUMULATE_OUTGOING_ARGS)
4276 /* If the source is referenced relative to the stack pointer,
4277 copy it to another register to stabilize it. We do not need
4278 to do this if we know that we won't be changing sp. */
4280 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4281 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4282 temp = copy_to_reg (temp);
4285 target = gen_rtx_MEM (BLKmode, temp);
4287 /* We do *not* set_mem_attributes here, because incoming arguments
4288 may overlap with sibling call outgoing arguments and we cannot
4289 allow reordering of reads from function arguments with stores
4290 to outgoing arguments of sibling calls. We do, however, want
4291 to record the alignment of the stack slot. */
4292 /* ALIGN may well be better aligned than TYPE, e.g. due to
4293 PARM_BOUNDARY. Assume the caller isn't lying. */
4294 set_mem_align (target, align);
4296 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4299 else if (partial > 0)
4301 /* Scalar partly in registers. */
4303 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4304 int i;
4305 int not_stack;
4306 /* # bytes of start of argument
4307 that we must make space for but need not store. */
4308 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4309 int args_offset = INTVAL (args_so_far);
4310 int skip;
4312 /* Push padding now if padding above and stack grows down,
4313 or if padding below and stack grows up.
4314 But if space already allocated, this has already been done. */
4315 if (extra && args_addr == 0
4316 && where_pad != none && where_pad != stack_direction)
4317 anti_adjust_stack (GEN_INT (extra));
4319 /* If we make space by pushing it, we might as well push
4320 the real data. Otherwise, we can leave OFFSET nonzero
4321 and leave the space uninitialized. */
4322 if (args_addr == 0)
4323 offset = 0;
4325 /* Now NOT_STACK gets the number of words that we don't need to
4326 allocate on the stack. Convert OFFSET to words too. */
4327 not_stack = (partial - offset) / UNITS_PER_WORD;
4328 offset /= UNITS_PER_WORD;
4330 /* If the partial register-part of the arg counts in its stack size,
4331 skip the part of stack space corresponding to the registers.
4332 Otherwise, start copying to the beginning of the stack space,
4333 by setting SKIP to 0. */
4334 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4336 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4337 x = validize_mem (force_const_mem (mode, x));
4339 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4340 SUBREGs of such registers are not allowed. */
4341 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4342 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4343 x = copy_to_reg (x);
4345 /* Loop over all the words allocated on the stack for this arg. */
4346 /* We can do it by words, because any scalar bigger than a word
4347 has a size a multiple of a word. */
4348 for (i = size - 1; i >= not_stack; i--)
4349 if (i >= not_stack + offset)
4350 emit_push_insn (operand_subword_force (x, i, mode),
4351 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4352 0, args_addr,
4353 GEN_INT (args_offset + ((i - not_stack + skip)
4354 * UNITS_PER_WORD)),
4355 reg_parm_stack_space, alignment_pad);
4357 else
4359 rtx addr;
4360 rtx dest;
4362 /* Push padding now if padding above and stack grows down,
4363 or if padding below and stack grows up.
4364 But if space already allocated, this has already been done. */
4365 if (extra && args_addr == 0
4366 && where_pad != none && where_pad != stack_direction)
4367 anti_adjust_stack (GEN_INT (extra));
4369 #ifdef PUSH_ROUNDING
4370 if (args_addr == 0 && PUSH_ARGS)
4371 emit_single_push_insn (mode, x, type);
4372 else
4373 #endif
4375 if (CONST_INT_P (args_so_far))
4376 addr
4377 = memory_address (mode,
4378 plus_constant (Pmode, args_addr,
4379 INTVAL (args_so_far)));
4380 else
4381 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4382 args_so_far));
4383 dest = gen_rtx_MEM (mode, addr);
4385 /* We do *not* set_mem_attributes here, because incoming arguments
4386 may overlap with sibling call outgoing arguments and we cannot
4387 allow reordering of reads from function arguments with stores
4388 to outgoing arguments of sibling calls. We do, however, want
4389 to record the alignment of the stack slot. */
4390 /* ALIGN may well be better aligned than TYPE, e.g. due to
4391 PARM_BOUNDARY. Assume the caller isn't lying. */
4392 set_mem_align (dest, align);
4394 emit_move_insn (dest, x);
4398 /* If part should go in registers, copy that part
4399 into the appropriate registers. Do this now, at the end,
4400 since mem-to-mem copies above may do function calls. */
4401 if (partial > 0 && reg != 0)
4403 /* Handle calls that pass values in multiple non-contiguous locations.
4404 The Irix 6 ABI has examples of this. */
4405 if (GET_CODE (reg) == PARALLEL)
4406 emit_group_load (reg, x, type, -1);
4407 else
4409 gcc_assert (partial % UNITS_PER_WORD == 0);
4410 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4414 if (extra && args_addr == 0 && where_pad == stack_direction)
4415 anti_adjust_stack (GEN_INT (extra));
4417 if (alignment_pad && args_addr == 0)
4418 anti_adjust_stack (alignment_pad);
4421 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4422 operations. */
4424 static rtx
4425 get_subtarget (rtx x)
4427 return (optimize
4428 || x == 0
4429 /* Only registers can be subtargets. */
4430 || !REG_P (x)
4431 /* Don't use hard regs to avoid extending their life. */
4432 || REGNO (x) < FIRST_PSEUDO_REGISTER
4433 ? 0 : x);
4436 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4437 FIELD is a bitfield. Returns true if the optimization was successful,
4438 and there's nothing else to do. */
4440 static bool
4441 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4442 unsigned HOST_WIDE_INT bitpos,
4443 unsigned HOST_WIDE_INT bitregion_start,
4444 unsigned HOST_WIDE_INT bitregion_end,
4445 machine_mode mode1, rtx str_rtx,
4446 tree to, tree src)
4448 machine_mode str_mode = GET_MODE (str_rtx);
4449 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4450 tree op0, op1;
4451 rtx value, result;
4452 optab binop;
4453 gimple srcstmt;
4454 enum tree_code code;
4456 if (mode1 != VOIDmode
4457 || bitsize >= BITS_PER_WORD
4458 || str_bitsize > BITS_PER_WORD
4459 || TREE_SIDE_EFFECTS (to)
4460 || TREE_THIS_VOLATILE (to))
4461 return false;
4463 STRIP_NOPS (src);
4464 if (TREE_CODE (src) != SSA_NAME)
4465 return false;
4466 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4467 return false;
4469 srcstmt = get_gimple_for_ssa_name (src);
4470 if (!srcstmt
4471 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4472 return false;
4474 code = gimple_assign_rhs_code (srcstmt);
4476 op0 = gimple_assign_rhs1 (srcstmt);
4478 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4479 to find its initialization. Hopefully the initialization will
4480 be from a bitfield load. */
4481 if (TREE_CODE (op0) == SSA_NAME)
4483 gimple op0stmt = get_gimple_for_ssa_name (op0);
4485 /* We want to eventually have OP0 be the same as TO, which
4486 should be a bitfield. */
4487 if (!op0stmt
4488 || !is_gimple_assign (op0stmt)
4489 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4490 return false;
4491 op0 = gimple_assign_rhs1 (op0stmt);
4494 op1 = gimple_assign_rhs2 (srcstmt);
4496 if (!operand_equal_p (to, op0, 0))
4497 return false;
4499 if (MEM_P (str_rtx))
4501 unsigned HOST_WIDE_INT offset1;
4503 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4504 str_mode = word_mode;
4505 str_mode = get_best_mode (bitsize, bitpos,
4506 bitregion_start, bitregion_end,
4507 MEM_ALIGN (str_rtx), str_mode, 0);
4508 if (str_mode == VOIDmode)
4509 return false;
4510 str_bitsize = GET_MODE_BITSIZE (str_mode);
4512 offset1 = bitpos;
4513 bitpos %= str_bitsize;
4514 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4515 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4517 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4518 return false;
4520 /* If the bit field covers the whole REG/MEM, store_field
4521 will likely generate better code. */
4522 if (bitsize >= str_bitsize)
4523 return false;
4525 /* We can't handle fields split across multiple entities. */
4526 if (bitpos + bitsize > str_bitsize)
4527 return false;
4529 if (BYTES_BIG_ENDIAN)
4530 bitpos = str_bitsize - bitpos - bitsize;
4532 switch (code)
4534 case PLUS_EXPR:
4535 case MINUS_EXPR:
4536 /* For now, just optimize the case of the topmost bitfield
4537 where we don't need to do any masking and also
4538 1 bit bitfields where xor can be used.
4539 We might win by one instruction for the other bitfields
4540 too if insv/extv instructions aren't used, so that
4541 can be added later. */
4542 if (bitpos + bitsize != str_bitsize
4543 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4544 break;
4546 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4547 value = convert_modes (str_mode,
4548 TYPE_MODE (TREE_TYPE (op1)), value,
4549 TYPE_UNSIGNED (TREE_TYPE (op1)));
4551 /* We may be accessing data outside the field, which means
4552 we can alias adjacent data. */
4553 if (MEM_P (str_rtx))
4555 str_rtx = shallow_copy_rtx (str_rtx);
4556 set_mem_alias_set (str_rtx, 0);
4557 set_mem_expr (str_rtx, 0);
4560 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4561 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4563 value = expand_and (str_mode, value, const1_rtx, NULL);
4564 binop = xor_optab;
4566 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4567 result = expand_binop (str_mode, binop, str_rtx,
4568 value, str_rtx, 1, OPTAB_WIDEN);
4569 if (result != str_rtx)
4570 emit_move_insn (str_rtx, result);
4571 return true;
4573 case BIT_IOR_EXPR:
4574 case BIT_XOR_EXPR:
4575 if (TREE_CODE (op1) != INTEGER_CST)
4576 break;
4577 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4578 value = convert_modes (str_mode,
4579 TYPE_MODE (TREE_TYPE (op1)), value,
4580 TYPE_UNSIGNED (TREE_TYPE (op1)));
4582 /* We may be accessing data outside the field, which means
4583 we can alias adjacent data. */
4584 if (MEM_P (str_rtx))
4586 str_rtx = shallow_copy_rtx (str_rtx);
4587 set_mem_alias_set (str_rtx, 0);
4588 set_mem_expr (str_rtx, 0);
4591 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4592 if (bitpos + bitsize != str_bitsize)
4594 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4595 str_mode);
4596 value = expand_and (str_mode, value, mask, NULL_RTX);
4598 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4599 result = expand_binop (str_mode, binop, str_rtx,
4600 value, str_rtx, 1, OPTAB_WIDEN);
4601 if (result != str_rtx)
4602 emit_move_insn (str_rtx, result);
4603 return true;
4605 default:
4606 break;
4609 return false;
4612 /* In the C++ memory model, consecutive bit fields in a structure are
4613 considered one memory location.
4615 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4616 returns the bit range of consecutive bits in which this COMPONENT_REF
4617 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4618 and *OFFSET may be adjusted in the process.
4620 If the access does not need to be restricted, 0 is returned in both
4621 *BITSTART and *BITEND. */
4623 static void
4624 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4625 unsigned HOST_WIDE_INT *bitend,
4626 tree exp,
4627 HOST_WIDE_INT *bitpos,
4628 tree *offset)
4630 HOST_WIDE_INT bitoffset;
4631 tree field, repr;
4633 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4635 field = TREE_OPERAND (exp, 1);
4636 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4637 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4638 need to limit the range we can access. */
4639 if (!repr)
4641 *bitstart = *bitend = 0;
4642 return;
4645 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4646 part of a larger bit field, then the representative does not serve any
4647 useful purpose. This can occur in Ada. */
4648 if (handled_component_p (TREE_OPERAND (exp, 0)))
4650 machine_mode rmode;
4651 HOST_WIDE_INT rbitsize, rbitpos;
4652 tree roffset;
4653 int unsignedp;
4654 int volatilep = 0;
4655 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4656 &roffset, &rmode, &unsignedp, &volatilep, false);
4657 if ((rbitpos % BITS_PER_UNIT) != 0)
4659 *bitstart = *bitend = 0;
4660 return;
4664 /* Compute the adjustment to bitpos from the offset of the field
4665 relative to the representative. DECL_FIELD_OFFSET of field and
4666 repr are the same by construction if they are not constants,
4667 see finish_bitfield_layout. */
4668 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4669 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4670 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4671 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4672 else
4673 bitoffset = 0;
4674 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4675 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4677 /* If the adjustment is larger than bitpos, we would have a negative bit
4678 position for the lower bound and this may wreak havoc later. Adjust
4679 offset and bitpos to make the lower bound non-negative in that case. */
4680 if (bitoffset > *bitpos)
4682 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4683 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4685 *bitpos += adjust;
4686 if (*offset == NULL_TREE)
4687 *offset = size_int (-adjust / BITS_PER_UNIT);
4688 else
4689 *offset
4690 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4691 *bitstart = 0;
4693 else
4694 *bitstart = *bitpos - bitoffset;
4696 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4699 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4700 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4701 DECL_RTL was not set yet, return NORTL. */
4703 static inline bool
4704 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4706 if (TREE_CODE (addr) != ADDR_EXPR)
4707 return false;
4709 tree base = TREE_OPERAND (addr, 0);
4711 if (!DECL_P (base)
4712 || TREE_ADDRESSABLE (base)
4713 || DECL_MODE (base) == BLKmode)
4714 return false;
4716 if (!DECL_RTL_SET_P (base))
4717 return nortl;
4719 return (!MEM_P (DECL_RTL (base)));
4722 /* Returns true if the MEM_REF REF refers to an object that does not
4723 reside in memory and has non-BLKmode. */
4725 static inline bool
4726 mem_ref_refers_to_non_mem_p (tree ref)
4728 tree base = TREE_OPERAND (ref, 0);
4729 return addr_expr_of_non_mem_decl_p_1 (base, false);
4732 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4733 is true, try generating a nontemporal store. */
4735 void
4736 expand_assignment (tree to, tree from, bool nontemporal)
4738 rtx to_rtx = 0;
4739 rtx result;
4740 machine_mode mode;
4741 unsigned int align;
4742 enum insn_code icode;
4744 /* Don't crash if the lhs of the assignment was erroneous. */
4745 if (TREE_CODE (to) == ERROR_MARK)
4747 expand_normal (from);
4748 return;
4751 /* Optimize away no-op moves without side-effects. */
4752 if (operand_equal_p (to, from, 0))
4753 return;
4755 /* Handle misaligned stores. */
4756 mode = TYPE_MODE (TREE_TYPE (to));
4757 if ((TREE_CODE (to) == MEM_REF
4758 || TREE_CODE (to) == TARGET_MEM_REF)
4759 && mode != BLKmode
4760 && !mem_ref_refers_to_non_mem_p (to)
4761 && ((align = get_object_alignment (to))
4762 < GET_MODE_ALIGNMENT (mode))
4763 && (((icode = optab_handler (movmisalign_optab, mode))
4764 != CODE_FOR_nothing)
4765 || SLOW_UNALIGNED_ACCESS (mode, align)))
4767 rtx reg, mem;
4769 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4770 reg = force_not_mem (reg);
4771 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4773 if (icode != CODE_FOR_nothing)
4775 struct expand_operand ops[2];
4777 create_fixed_operand (&ops[0], mem);
4778 create_input_operand (&ops[1], reg, mode);
4779 /* The movmisalign<mode> pattern cannot fail, else the assignment
4780 would silently be omitted. */
4781 expand_insn (icode, 2, ops);
4783 else
4784 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4785 return;
4788 /* Assignment of a structure component needs special treatment
4789 if the structure component's rtx is not simply a MEM.
4790 Assignment of an array element at a constant index, and assignment of
4791 an array element in an unaligned packed structure field, has the same
4792 problem. Same for (partially) storing into a non-memory object. */
4793 if (handled_component_p (to)
4794 || (TREE_CODE (to) == MEM_REF
4795 && mem_ref_refers_to_non_mem_p (to))
4796 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4798 machine_mode mode1;
4799 HOST_WIDE_INT bitsize, bitpos;
4800 unsigned HOST_WIDE_INT bitregion_start = 0;
4801 unsigned HOST_WIDE_INT bitregion_end = 0;
4802 tree offset;
4803 int unsignedp;
4804 int volatilep = 0;
4805 tree tem;
4807 push_temp_slots ();
4808 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4809 &unsignedp, &volatilep, true);
4811 /* Make sure bitpos is not negative, it can wreak havoc later. */
4812 if (bitpos < 0)
4814 gcc_assert (offset == NULL_TREE);
4815 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4816 ? 3 : exact_log2 (BITS_PER_UNIT)));
4817 bitpos &= BITS_PER_UNIT - 1;
4820 if (TREE_CODE (to) == COMPONENT_REF
4821 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4822 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4823 /* The C++ memory model naturally applies to byte-aligned fields.
4824 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4825 BITSIZE are not byte-aligned, there is no need to limit the range
4826 we can access. This can occur with packed structures in Ada. */
4827 else if (bitsize > 0
4828 && bitsize % BITS_PER_UNIT == 0
4829 && bitpos % BITS_PER_UNIT == 0)
4831 bitregion_start = bitpos;
4832 bitregion_end = bitpos + bitsize - 1;
4835 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4837 /* If the field has a mode, we want to access it in the
4838 field's mode, not the computed mode.
4839 If a MEM has VOIDmode (external with incomplete type),
4840 use BLKmode for it instead. */
4841 if (MEM_P (to_rtx))
4843 if (mode1 != VOIDmode)
4844 to_rtx = adjust_address (to_rtx, mode1, 0);
4845 else if (GET_MODE (to_rtx) == VOIDmode)
4846 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4849 if (offset != 0)
4851 machine_mode address_mode;
4852 rtx offset_rtx;
4854 if (!MEM_P (to_rtx))
4856 /* We can get constant negative offsets into arrays with broken
4857 user code. Translate this to a trap instead of ICEing. */
4858 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4859 expand_builtin_trap ();
4860 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4863 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4864 address_mode = get_address_mode (to_rtx);
4865 if (GET_MODE (offset_rtx) != address_mode)
4867 /* We cannot be sure that the RTL in offset_rtx is valid outside
4868 of a memory address context, so force it into a register
4869 before attempting to convert it to the desired mode. */
4870 offset_rtx = force_operand (offset_rtx, NULL_RTX);
4871 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4874 /* If we have an expression in OFFSET_RTX and a non-zero
4875 byte offset in BITPOS, adding the byte offset before the
4876 OFFSET_RTX results in better intermediate code, which makes
4877 later rtl optimization passes perform better.
4879 We prefer intermediate code like this:
4881 r124:DI=r123:DI+0x18
4882 [r124:DI]=r121:DI
4884 ... instead of ...
4886 r124:DI=r123:DI+0x10
4887 [r124:DI+0x8]=r121:DI
4889 This is only done for aligned data values, as these can
4890 be expected to result in single move instructions. */
4891 if (mode1 != VOIDmode
4892 && bitpos != 0
4893 && bitsize > 0
4894 && (bitpos % bitsize) == 0
4895 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4896 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4898 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4899 bitregion_start = 0;
4900 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4901 bitregion_end -= bitpos;
4902 bitpos = 0;
4905 to_rtx = offset_address (to_rtx, offset_rtx,
4906 highest_pow2_factor_for_target (to,
4907 offset));
4910 /* No action is needed if the target is not a memory and the field
4911 lies completely outside that target. This can occur if the source
4912 code contains an out-of-bounds access to a small array. */
4913 if (!MEM_P (to_rtx)
4914 && GET_MODE (to_rtx) != BLKmode
4915 && (unsigned HOST_WIDE_INT) bitpos
4916 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4918 expand_normal (from);
4919 result = NULL;
4921 /* Handle expand_expr of a complex value returning a CONCAT. */
4922 else if (GET_CODE (to_rtx) == CONCAT)
4924 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4925 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4926 && bitpos == 0
4927 && bitsize == mode_bitsize)
4928 result = store_expr (from, to_rtx, false, nontemporal);
4929 else if (bitsize == mode_bitsize / 2
4930 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4931 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4932 nontemporal);
4933 else if (bitpos + bitsize <= mode_bitsize / 2)
4934 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4935 bitregion_start, bitregion_end,
4936 mode1, from,
4937 get_alias_set (to), nontemporal);
4938 else if (bitpos >= mode_bitsize / 2)
4939 result = store_field (XEXP (to_rtx, 1), bitsize,
4940 bitpos - mode_bitsize / 2,
4941 bitregion_start, bitregion_end,
4942 mode1, from,
4943 get_alias_set (to), nontemporal);
4944 else if (bitpos == 0 && bitsize == mode_bitsize)
4946 rtx from_rtx;
4947 result = expand_normal (from);
4948 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4949 TYPE_MODE (TREE_TYPE (from)), 0);
4950 emit_move_insn (XEXP (to_rtx, 0),
4951 read_complex_part (from_rtx, false));
4952 emit_move_insn (XEXP (to_rtx, 1),
4953 read_complex_part (from_rtx, true));
4955 else
4957 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4958 GET_MODE_SIZE (GET_MODE (to_rtx)));
4959 write_complex_part (temp, XEXP (to_rtx, 0), false);
4960 write_complex_part (temp, XEXP (to_rtx, 1), true);
4961 result = store_field (temp, bitsize, bitpos,
4962 bitregion_start, bitregion_end,
4963 mode1, from,
4964 get_alias_set (to), nontemporal);
4965 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4966 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4969 else
4971 if (MEM_P (to_rtx))
4973 /* If the field is at offset zero, we could have been given the
4974 DECL_RTX of the parent struct. Don't munge it. */
4975 to_rtx = shallow_copy_rtx (to_rtx);
4976 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4977 if (volatilep)
4978 MEM_VOLATILE_P (to_rtx) = 1;
4981 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4982 bitregion_start, bitregion_end,
4983 mode1,
4984 to_rtx, to, from))
4985 result = NULL;
4986 else
4987 result = store_field (to_rtx, bitsize, bitpos,
4988 bitregion_start, bitregion_end,
4989 mode1, from,
4990 get_alias_set (to), nontemporal);
4993 if (result)
4994 preserve_temp_slots (result);
4995 pop_temp_slots ();
4996 return;
4999 /* If the rhs is a function call and its value is not an aggregate,
5000 call the function before we start to compute the lhs.
5001 This is needed for correct code for cases such as
5002 val = setjmp (buf) on machines where reference to val
5003 requires loading up part of an address in a separate insn.
5005 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5006 since it might be a promoted variable where the zero- or sign- extension
5007 needs to be done. Handling this in the normal way is safe because no
5008 computation is done before the call. The same is true for SSA names. */
5009 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5010 && COMPLETE_TYPE_P (TREE_TYPE (from))
5011 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5012 && ! (((TREE_CODE (to) == VAR_DECL
5013 || TREE_CODE (to) == PARM_DECL
5014 || TREE_CODE (to) == RESULT_DECL)
5015 && REG_P (DECL_RTL (to)))
5016 || TREE_CODE (to) == SSA_NAME))
5018 rtx value;
5019 rtx bounds;
5021 push_temp_slots ();
5022 value = expand_normal (from);
5024 /* Split value and bounds to store them separately. */
5025 chkp_split_slot (value, &value, &bounds);
5027 if (to_rtx == 0)
5028 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5030 /* Handle calls that return values in multiple non-contiguous locations.
5031 The Irix 6 ABI has examples of this. */
5032 if (GET_CODE (to_rtx) == PARALLEL)
5034 if (GET_CODE (value) == PARALLEL)
5035 emit_group_move (to_rtx, value);
5036 else
5037 emit_group_load (to_rtx, value, TREE_TYPE (from),
5038 int_size_in_bytes (TREE_TYPE (from)));
5040 else if (GET_CODE (value) == PARALLEL)
5041 emit_group_store (to_rtx, value, TREE_TYPE (from),
5042 int_size_in_bytes (TREE_TYPE (from)));
5043 else if (GET_MODE (to_rtx) == BLKmode)
5045 /* Handle calls that return BLKmode values in registers. */
5046 if (REG_P (value))
5047 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5048 else
5049 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5051 else
5053 if (POINTER_TYPE_P (TREE_TYPE (to)))
5054 value = convert_memory_address_addr_space
5055 (GET_MODE (to_rtx), value,
5056 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5058 emit_move_insn (to_rtx, value);
5061 /* Store bounds if required. */
5062 if (bounds
5063 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5065 gcc_assert (MEM_P (to_rtx));
5066 chkp_emit_bounds_store (bounds, value, to_rtx);
5069 preserve_temp_slots (to_rtx);
5070 pop_temp_slots ();
5071 return;
5074 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5075 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5077 /* Don't move directly into a return register. */
5078 if (TREE_CODE (to) == RESULT_DECL
5079 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5081 rtx temp;
5083 push_temp_slots ();
5085 /* If the source is itself a return value, it still is in a pseudo at
5086 this point so we can move it back to the return register directly. */
5087 if (REG_P (to_rtx)
5088 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5089 && TREE_CODE (from) != CALL_EXPR)
5090 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5091 else
5092 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5094 /* Handle calls that return values in multiple non-contiguous locations.
5095 The Irix 6 ABI has examples of this. */
5096 if (GET_CODE (to_rtx) == PARALLEL)
5098 if (GET_CODE (temp) == PARALLEL)
5099 emit_group_move (to_rtx, temp);
5100 else
5101 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5102 int_size_in_bytes (TREE_TYPE (from)));
5104 else if (temp)
5105 emit_move_insn (to_rtx, temp);
5107 preserve_temp_slots (to_rtx);
5108 pop_temp_slots ();
5109 return;
5112 /* In case we are returning the contents of an object which overlaps
5113 the place the value is being stored, use a safe function when copying
5114 a value through a pointer into a structure value return block. */
5115 if (TREE_CODE (to) == RESULT_DECL
5116 && TREE_CODE (from) == INDIRECT_REF
5117 && ADDR_SPACE_GENERIC_P
5118 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5119 && refs_may_alias_p (to, from)
5120 && cfun->returns_struct
5121 && !cfun->returns_pcc_struct)
5123 rtx from_rtx, size;
5125 push_temp_slots ();
5126 size = expr_size (from);
5127 from_rtx = expand_normal (from);
5129 emit_library_call (memmove_libfunc, LCT_NORMAL,
5130 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5131 XEXP (from_rtx, 0), Pmode,
5132 convert_to_mode (TYPE_MODE (sizetype),
5133 size, TYPE_UNSIGNED (sizetype)),
5134 TYPE_MODE (sizetype));
5136 preserve_temp_slots (to_rtx);
5137 pop_temp_slots ();
5138 return;
5141 /* Compute FROM and store the value in the rtx we got. */
5143 push_temp_slots ();
5144 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, to);
5145 preserve_temp_slots (result);
5146 pop_temp_slots ();
5147 return;
5150 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5151 succeeded, false otherwise. */
5153 bool
5154 emit_storent_insn (rtx to, rtx from)
5156 struct expand_operand ops[2];
5157 machine_mode mode = GET_MODE (to);
5158 enum insn_code code = optab_handler (storent_optab, mode);
5160 if (code == CODE_FOR_nothing)
5161 return false;
5163 create_fixed_operand (&ops[0], to);
5164 create_input_operand (&ops[1], from, mode);
5165 return maybe_expand_insn (code, 2, ops);
5168 /* Generate code for computing expression EXP,
5169 and storing the value into TARGET.
5171 If the mode is BLKmode then we may return TARGET itself.
5172 It turns out that in BLKmode it doesn't cause a problem.
5173 because C has no operators that could combine two different
5174 assignments into the same BLKmode object with different values
5175 with no sequence point. Will other languages need this to
5176 be more thorough?
5178 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5179 stack, and block moves may need to be treated specially.
5181 If NONTEMPORAL is true, try using a nontemporal store instruction.
5183 If BTARGET is not NULL then computed bounds of EXP are
5184 associated with BTARGET. */
5187 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5188 bool nontemporal, tree btarget)
5190 rtx temp;
5191 rtx alt_rtl = NULL_RTX;
5192 location_t loc = curr_insn_location ();
5194 if (VOID_TYPE_P (TREE_TYPE (exp)))
5196 /* C++ can generate ?: expressions with a throw expression in one
5197 branch and an rvalue in the other. Here, we resolve attempts to
5198 store the throw expression's nonexistent result. */
5199 gcc_assert (!call_param_p);
5200 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5201 return NULL_RTX;
5203 if (TREE_CODE (exp) == COMPOUND_EXPR)
5205 /* Perform first part of compound expression, then assign from second
5206 part. */
5207 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5208 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5209 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5210 call_param_p, nontemporal, btarget);
5212 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5214 /* For conditional expression, get safe form of the target. Then
5215 test the condition, doing the appropriate assignment on either
5216 side. This avoids the creation of unnecessary temporaries.
5217 For non-BLKmode, it is more efficient not to do this. */
5219 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5221 do_pending_stack_adjust ();
5222 NO_DEFER_POP;
5223 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5224 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5225 nontemporal, btarget);
5226 emit_jump_insn (gen_jump (lab2));
5227 emit_barrier ();
5228 emit_label (lab1);
5229 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5230 nontemporal, btarget);
5231 emit_label (lab2);
5232 OK_DEFER_POP;
5234 return NULL_RTX;
5236 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5237 /* If this is a scalar in a register that is stored in a wider mode
5238 than the declared mode, compute the result into its declared mode
5239 and then convert to the wider mode. Our value is the computed
5240 expression. */
5242 rtx inner_target = 0;
5244 /* We can do the conversion inside EXP, which will often result
5245 in some optimizations. Do the conversion in two steps: first
5246 change the signedness, if needed, then the extend. But don't
5247 do this if the type of EXP is a subtype of something else
5248 since then the conversion might involve more than just
5249 converting modes. */
5250 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5251 && TREE_TYPE (TREE_TYPE (exp)) == 0
5252 && GET_MODE_PRECISION (GET_MODE (target))
5253 == TYPE_PRECISION (TREE_TYPE (exp)))
5255 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5256 TYPE_UNSIGNED (TREE_TYPE (exp))))
5258 /* Some types, e.g. Fortran's logical*4, won't have a signed
5259 version, so use the mode instead. */
5260 tree ntype
5261 = (signed_or_unsigned_type_for
5262 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5263 if (ntype == NULL)
5264 ntype = lang_hooks.types.type_for_mode
5265 (TYPE_MODE (TREE_TYPE (exp)),
5266 SUBREG_PROMOTED_SIGN (target));
5268 exp = fold_convert_loc (loc, ntype, exp);
5271 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5272 (GET_MODE (SUBREG_REG (target)),
5273 SUBREG_PROMOTED_SIGN (target)),
5274 exp);
5276 inner_target = SUBREG_REG (target);
5279 temp = expand_expr (exp, inner_target, VOIDmode,
5280 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5282 /* Handle bounds returned by call. */
5283 if (TREE_CODE (exp) == CALL_EXPR)
5285 rtx bounds;
5286 chkp_split_slot (temp, &temp, &bounds);
5287 if (bounds && btarget)
5289 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5290 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5291 chkp_set_rtl_bounds (btarget, tmp);
5295 /* If TEMP is a VOIDmode constant, use convert_modes to make
5296 sure that we properly convert it. */
5297 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5299 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5300 temp, SUBREG_PROMOTED_SIGN (target));
5301 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5302 GET_MODE (target), temp,
5303 SUBREG_PROMOTED_SIGN (target));
5306 convert_move (SUBREG_REG (target), temp,
5307 SUBREG_PROMOTED_SIGN (target));
5309 return NULL_RTX;
5311 else if ((TREE_CODE (exp) == STRING_CST
5312 || (TREE_CODE (exp) == MEM_REF
5313 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5314 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5315 == STRING_CST
5316 && integer_zerop (TREE_OPERAND (exp, 1))))
5317 && !nontemporal && !call_param_p
5318 && MEM_P (target))
5320 /* Optimize initialization of an array with a STRING_CST. */
5321 HOST_WIDE_INT exp_len, str_copy_len;
5322 rtx dest_mem;
5323 tree str = TREE_CODE (exp) == STRING_CST
5324 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5326 exp_len = int_expr_size (exp);
5327 if (exp_len <= 0)
5328 goto normal_expr;
5330 if (TREE_STRING_LENGTH (str) <= 0)
5331 goto normal_expr;
5333 str_copy_len = strlen (TREE_STRING_POINTER (str));
5334 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5335 goto normal_expr;
5337 str_copy_len = TREE_STRING_LENGTH (str);
5338 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5339 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5341 str_copy_len += STORE_MAX_PIECES - 1;
5342 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5344 str_copy_len = MIN (str_copy_len, exp_len);
5345 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5346 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5347 MEM_ALIGN (target), false))
5348 goto normal_expr;
5350 dest_mem = target;
5352 dest_mem = store_by_pieces (dest_mem,
5353 str_copy_len, builtin_strncpy_read_str,
5354 CONST_CAST (char *,
5355 TREE_STRING_POINTER (str)),
5356 MEM_ALIGN (target), false,
5357 exp_len > str_copy_len ? 1 : 0);
5358 if (exp_len > str_copy_len)
5359 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5360 GEN_INT (exp_len - str_copy_len),
5361 BLOCK_OP_NORMAL);
5362 return NULL_RTX;
5364 else
5366 rtx tmp_target;
5368 normal_expr:
5369 /* If we want to use a nontemporal store, force the value to
5370 register first. */
5371 tmp_target = nontemporal ? NULL_RTX : target;
5372 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5373 (call_param_p
5374 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5375 &alt_rtl, false);
5377 /* Handle bounds returned by call. */
5378 if (TREE_CODE (exp) == CALL_EXPR)
5380 rtx bounds;
5381 chkp_split_slot (temp, &temp, &bounds);
5382 if (bounds && btarget)
5384 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5385 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5386 chkp_set_rtl_bounds (btarget, tmp);
5391 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5392 the same as that of TARGET, adjust the constant. This is needed, for
5393 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5394 only a word-sized value. */
5395 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5396 && TREE_CODE (exp) != ERROR_MARK
5397 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5398 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5399 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5401 /* If value was not generated in the target, store it there.
5402 Convert the value to TARGET's type first if necessary and emit the
5403 pending incrementations that have been queued when expanding EXP.
5404 Note that we cannot emit the whole queue blindly because this will
5405 effectively disable the POST_INC optimization later.
5407 If TEMP and TARGET compare equal according to rtx_equal_p, but
5408 one or both of them are volatile memory refs, we have to distinguish
5409 two cases:
5410 - expand_expr has used TARGET. In this case, we must not generate
5411 another copy. This can be detected by TARGET being equal according
5412 to == .
5413 - expand_expr has not used TARGET - that means that the source just
5414 happens to have the same RTX form. Since temp will have been created
5415 by expand_expr, it will compare unequal according to == .
5416 We must generate a copy in this case, to reach the correct number
5417 of volatile memory references. */
5419 if ((! rtx_equal_p (temp, target)
5420 || (temp != target && (side_effects_p (temp)
5421 || side_effects_p (target))))
5422 && TREE_CODE (exp) != ERROR_MARK
5423 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5424 but TARGET is not valid memory reference, TEMP will differ
5425 from TARGET although it is really the same location. */
5426 && !(alt_rtl
5427 && rtx_equal_p (alt_rtl, target)
5428 && !side_effects_p (alt_rtl)
5429 && !side_effects_p (target))
5430 /* If there's nothing to copy, don't bother. Don't call
5431 expr_size unless necessary, because some front-ends (C++)
5432 expr_size-hook must not be given objects that are not
5433 supposed to be bit-copied or bit-initialized. */
5434 && expr_size (exp) != const0_rtx)
5436 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5438 if (GET_MODE (target) == BLKmode)
5440 /* Handle calls that return BLKmode values in registers. */
5441 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5442 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5443 else
5444 store_bit_field (target,
5445 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5446 0, 0, 0, GET_MODE (temp), temp);
5448 else
5449 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5452 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5454 /* Handle copying a string constant into an array. The string
5455 constant may be shorter than the array. So copy just the string's
5456 actual length, and clear the rest. First get the size of the data
5457 type of the string, which is actually the size of the target. */
5458 rtx size = expr_size (exp);
5460 if (CONST_INT_P (size)
5461 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5462 emit_block_move (target, temp, size,
5463 (call_param_p
5464 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5465 else
5467 machine_mode pointer_mode
5468 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5469 machine_mode address_mode = get_address_mode (target);
5471 /* Compute the size of the data to copy from the string. */
5472 tree copy_size
5473 = size_binop_loc (loc, MIN_EXPR,
5474 make_tree (sizetype, size),
5475 size_int (TREE_STRING_LENGTH (exp)));
5476 rtx copy_size_rtx
5477 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5478 (call_param_p
5479 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5480 rtx_code_label *label = 0;
5482 /* Copy that much. */
5483 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5484 TYPE_UNSIGNED (sizetype));
5485 emit_block_move (target, temp, copy_size_rtx,
5486 (call_param_p
5487 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5489 /* Figure out how much is left in TARGET that we have to clear.
5490 Do all calculations in pointer_mode. */
5491 if (CONST_INT_P (copy_size_rtx))
5493 size = plus_constant (address_mode, size,
5494 -INTVAL (copy_size_rtx));
5495 target = adjust_address (target, BLKmode,
5496 INTVAL (copy_size_rtx));
5498 else
5500 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5501 copy_size_rtx, NULL_RTX, 0,
5502 OPTAB_LIB_WIDEN);
5504 if (GET_MODE (copy_size_rtx) != address_mode)
5505 copy_size_rtx = convert_to_mode (address_mode,
5506 copy_size_rtx,
5507 TYPE_UNSIGNED (sizetype));
5509 target = offset_address (target, copy_size_rtx,
5510 highest_pow2_factor (copy_size));
5511 label = gen_label_rtx ();
5512 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5513 GET_MODE (size), 0, label);
5516 if (size != const0_rtx)
5517 clear_storage (target, size, BLOCK_OP_NORMAL);
5519 if (label)
5520 emit_label (label);
5523 /* Handle calls that return values in multiple non-contiguous locations.
5524 The Irix 6 ABI has examples of this. */
5525 else if (GET_CODE (target) == PARALLEL)
5527 if (GET_CODE (temp) == PARALLEL)
5528 emit_group_move (target, temp);
5529 else
5530 emit_group_load (target, temp, TREE_TYPE (exp),
5531 int_size_in_bytes (TREE_TYPE (exp)));
5533 else if (GET_CODE (temp) == PARALLEL)
5534 emit_group_store (target, temp, TREE_TYPE (exp),
5535 int_size_in_bytes (TREE_TYPE (exp)));
5536 else if (GET_MODE (temp) == BLKmode)
5537 emit_block_move (target, temp, expr_size (exp),
5538 (call_param_p
5539 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5540 /* If we emit a nontemporal store, there is nothing else to do. */
5541 else if (nontemporal && emit_storent_insn (target, temp))
5543 else
5545 temp = force_operand (temp, target);
5546 if (temp != target)
5547 emit_move_insn (target, temp);
5551 return NULL_RTX;
5554 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5556 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5558 return store_expr_with_bounds (exp, target, call_param_p, nontemporal, NULL);
5561 /* Return true if field F of structure TYPE is a flexible array. */
5563 static bool
5564 flexible_array_member_p (const_tree f, const_tree type)
5566 const_tree tf;
5568 tf = TREE_TYPE (f);
5569 return (DECL_CHAIN (f) == NULL
5570 && TREE_CODE (tf) == ARRAY_TYPE
5571 && TYPE_DOMAIN (tf)
5572 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5573 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5574 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5575 && int_size_in_bytes (type) >= 0);
5578 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5579 must have in order for it to completely initialize a value of type TYPE.
5580 Return -1 if the number isn't known.
5582 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5584 static HOST_WIDE_INT
5585 count_type_elements (const_tree type, bool for_ctor_p)
5587 switch (TREE_CODE (type))
5589 case ARRAY_TYPE:
5591 tree nelts;
5593 nelts = array_type_nelts (type);
5594 if (nelts && tree_fits_uhwi_p (nelts))
5596 unsigned HOST_WIDE_INT n;
5598 n = tree_to_uhwi (nelts) + 1;
5599 if (n == 0 || for_ctor_p)
5600 return n;
5601 else
5602 return n * count_type_elements (TREE_TYPE (type), false);
5604 return for_ctor_p ? -1 : 1;
5607 case RECORD_TYPE:
5609 unsigned HOST_WIDE_INT n;
5610 tree f;
5612 n = 0;
5613 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5614 if (TREE_CODE (f) == FIELD_DECL)
5616 if (!for_ctor_p)
5617 n += count_type_elements (TREE_TYPE (f), false);
5618 else if (!flexible_array_member_p (f, type))
5619 /* Don't count flexible arrays, which are not supposed
5620 to be initialized. */
5621 n += 1;
5624 return n;
5627 case UNION_TYPE:
5628 case QUAL_UNION_TYPE:
5630 tree f;
5631 HOST_WIDE_INT n, m;
5633 gcc_assert (!for_ctor_p);
5634 /* Estimate the number of scalars in each field and pick the
5635 maximum. Other estimates would do instead; the idea is simply
5636 to make sure that the estimate is not sensitive to the ordering
5637 of the fields. */
5638 n = 1;
5639 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5640 if (TREE_CODE (f) == FIELD_DECL)
5642 m = count_type_elements (TREE_TYPE (f), false);
5643 /* If the field doesn't span the whole union, add an extra
5644 scalar for the rest. */
5645 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5646 TYPE_SIZE (type)) != 1)
5647 m++;
5648 if (n < m)
5649 n = m;
5651 return n;
5654 case COMPLEX_TYPE:
5655 return 2;
5657 case VECTOR_TYPE:
5658 return TYPE_VECTOR_SUBPARTS (type);
5660 case INTEGER_TYPE:
5661 case REAL_TYPE:
5662 case FIXED_POINT_TYPE:
5663 case ENUMERAL_TYPE:
5664 case BOOLEAN_TYPE:
5665 case POINTER_TYPE:
5666 case OFFSET_TYPE:
5667 case REFERENCE_TYPE:
5668 case NULLPTR_TYPE:
5669 return 1;
5671 case ERROR_MARK:
5672 return 0;
5674 case VOID_TYPE:
5675 case METHOD_TYPE:
5676 case FUNCTION_TYPE:
5677 case LANG_TYPE:
5678 default:
5679 gcc_unreachable ();
5683 /* Helper for categorize_ctor_elements. Identical interface. */
5685 static bool
5686 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5687 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5689 unsigned HOST_WIDE_INT idx;
5690 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5691 tree value, purpose, elt_type;
5693 /* Whether CTOR is a valid constant initializer, in accordance with what
5694 initializer_constant_valid_p does. If inferred from the constructor
5695 elements, true until proven otherwise. */
5696 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5697 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5699 nz_elts = 0;
5700 init_elts = 0;
5701 num_fields = 0;
5702 elt_type = NULL_TREE;
5704 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5706 HOST_WIDE_INT mult = 1;
5708 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5710 tree lo_index = TREE_OPERAND (purpose, 0);
5711 tree hi_index = TREE_OPERAND (purpose, 1);
5713 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5714 mult = (tree_to_uhwi (hi_index)
5715 - tree_to_uhwi (lo_index) + 1);
5717 num_fields += mult;
5718 elt_type = TREE_TYPE (value);
5720 switch (TREE_CODE (value))
5722 case CONSTRUCTOR:
5724 HOST_WIDE_INT nz = 0, ic = 0;
5726 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5727 p_complete);
5729 nz_elts += mult * nz;
5730 init_elts += mult * ic;
5732 if (const_from_elts_p && const_p)
5733 const_p = const_elt_p;
5735 break;
5737 case INTEGER_CST:
5738 case REAL_CST:
5739 case FIXED_CST:
5740 if (!initializer_zerop (value))
5741 nz_elts += mult;
5742 init_elts += mult;
5743 break;
5745 case STRING_CST:
5746 nz_elts += mult * TREE_STRING_LENGTH (value);
5747 init_elts += mult * TREE_STRING_LENGTH (value);
5748 break;
5750 case COMPLEX_CST:
5751 if (!initializer_zerop (TREE_REALPART (value)))
5752 nz_elts += mult;
5753 if (!initializer_zerop (TREE_IMAGPART (value)))
5754 nz_elts += mult;
5755 init_elts += mult;
5756 break;
5758 case VECTOR_CST:
5760 unsigned i;
5761 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5763 tree v = VECTOR_CST_ELT (value, i);
5764 if (!initializer_zerop (v))
5765 nz_elts += mult;
5766 init_elts += mult;
5769 break;
5771 default:
5773 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5774 nz_elts += mult * tc;
5775 init_elts += mult * tc;
5777 if (const_from_elts_p && const_p)
5778 const_p = initializer_constant_valid_p (value, elt_type)
5779 != NULL_TREE;
5781 break;
5785 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5786 num_fields, elt_type))
5787 *p_complete = false;
5789 *p_nz_elts += nz_elts;
5790 *p_init_elts += init_elts;
5792 return const_p;
5795 /* Examine CTOR to discover:
5796 * how many scalar fields are set to nonzero values,
5797 and place it in *P_NZ_ELTS;
5798 * how many scalar fields in total are in CTOR,
5799 and place it in *P_ELT_COUNT.
5800 * whether the constructor is complete -- in the sense that every
5801 meaningful byte is explicitly given a value --
5802 and place it in *P_COMPLETE.
5804 Return whether or not CTOR is a valid static constant initializer, the same
5805 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5807 bool
5808 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5809 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5811 *p_nz_elts = 0;
5812 *p_init_elts = 0;
5813 *p_complete = true;
5815 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5818 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5819 of which had type LAST_TYPE. Each element was itself a complete
5820 initializer, in the sense that every meaningful byte was explicitly
5821 given a value. Return true if the same is true for the constructor
5822 as a whole. */
5824 bool
5825 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5826 const_tree last_type)
5828 if (TREE_CODE (type) == UNION_TYPE
5829 || TREE_CODE (type) == QUAL_UNION_TYPE)
5831 if (num_elts == 0)
5832 return false;
5834 gcc_assert (num_elts == 1 && last_type);
5836 /* ??? We could look at each element of the union, and find the
5837 largest element. Which would avoid comparing the size of the
5838 initialized element against any tail padding in the union.
5839 Doesn't seem worth the effort... */
5840 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5843 return count_type_elements (type, true) == num_elts;
5846 /* Return 1 if EXP contains mostly (3/4) zeros. */
5848 static int
5849 mostly_zeros_p (const_tree exp)
5851 if (TREE_CODE (exp) == CONSTRUCTOR)
5853 HOST_WIDE_INT nz_elts, init_elts;
5854 bool complete_p;
5856 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5857 return !complete_p || nz_elts < init_elts / 4;
5860 return initializer_zerop (exp);
5863 /* Return 1 if EXP contains all zeros. */
5865 static int
5866 all_zeros_p (const_tree exp)
5868 if (TREE_CODE (exp) == CONSTRUCTOR)
5870 HOST_WIDE_INT nz_elts, init_elts;
5871 bool complete_p;
5873 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5874 return nz_elts == 0;
5877 return initializer_zerop (exp);
5880 /* Helper function for store_constructor.
5881 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5882 CLEARED is as for store_constructor.
5883 ALIAS_SET is the alias set to use for any stores.
5885 This provides a recursive shortcut back to store_constructor when it isn't
5886 necessary to go through store_field. This is so that we can pass through
5887 the cleared field to let store_constructor know that we may not have to
5888 clear a substructure if the outer structure has already been cleared. */
5890 static void
5891 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5892 HOST_WIDE_INT bitpos, machine_mode mode,
5893 tree exp, int cleared, alias_set_type alias_set)
5895 if (TREE_CODE (exp) == CONSTRUCTOR
5896 /* We can only call store_constructor recursively if the size and
5897 bit position are on a byte boundary. */
5898 && bitpos % BITS_PER_UNIT == 0
5899 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5900 /* If we have a nonzero bitpos for a register target, then we just
5901 let store_field do the bitfield handling. This is unlikely to
5902 generate unnecessary clear instructions anyways. */
5903 && (bitpos == 0 || MEM_P (target)))
5905 if (MEM_P (target))
5906 target
5907 = adjust_address (target,
5908 GET_MODE (target) == BLKmode
5909 || 0 != (bitpos
5910 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5911 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5914 /* Update the alias set, if required. */
5915 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5916 && MEM_ALIAS_SET (target) != 0)
5918 target = copy_rtx (target);
5919 set_mem_alias_set (target, alias_set);
5922 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5924 else
5925 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5929 /* Returns the number of FIELD_DECLs in TYPE. */
5931 static int
5932 fields_length (const_tree type)
5934 tree t = TYPE_FIELDS (type);
5935 int count = 0;
5937 for (; t; t = DECL_CHAIN (t))
5938 if (TREE_CODE (t) == FIELD_DECL)
5939 ++count;
5941 return count;
5945 /* Store the value of constructor EXP into the rtx TARGET.
5946 TARGET is either a REG or a MEM; we know it cannot conflict, since
5947 safe_from_p has been called.
5948 CLEARED is true if TARGET is known to have been zero'd.
5949 SIZE is the number of bytes of TARGET we are allowed to modify: this
5950 may not be the same as the size of EXP if we are assigning to a field
5951 which has been packed to exclude padding bits. */
5953 static void
5954 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5956 tree type = TREE_TYPE (exp);
5957 #ifdef WORD_REGISTER_OPERATIONS
5958 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5959 #endif
5961 switch (TREE_CODE (type))
5963 case RECORD_TYPE:
5964 case UNION_TYPE:
5965 case QUAL_UNION_TYPE:
5967 unsigned HOST_WIDE_INT idx;
5968 tree field, value;
5970 /* If size is zero or the target is already cleared, do nothing. */
5971 if (size == 0 || cleared)
5972 cleared = 1;
5973 /* We either clear the aggregate or indicate the value is dead. */
5974 else if ((TREE_CODE (type) == UNION_TYPE
5975 || TREE_CODE (type) == QUAL_UNION_TYPE)
5976 && ! CONSTRUCTOR_ELTS (exp))
5977 /* If the constructor is empty, clear the union. */
5979 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5980 cleared = 1;
5983 /* If we are building a static constructor into a register,
5984 set the initial value as zero so we can fold the value into
5985 a constant. But if more than one register is involved,
5986 this probably loses. */
5987 else if (REG_P (target) && TREE_STATIC (exp)
5988 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5990 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5991 cleared = 1;
5994 /* If the constructor has fewer fields than the structure or
5995 if we are initializing the structure to mostly zeros, clear
5996 the whole structure first. Don't do this if TARGET is a
5997 register whose mode size isn't equal to SIZE since
5998 clear_storage can't handle this case. */
5999 else if (size > 0
6000 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
6001 != fields_length (type))
6002 || mostly_zeros_p (exp))
6003 && (!REG_P (target)
6004 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6005 == size)))
6007 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6008 cleared = 1;
6011 if (REG_P (target) && !cleared)
6012 emit_clobber (target);
6014 /* Store each element of the constructor into the
6015 corresponding field of TARGET. */
6016 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6018 machine_mode mode;
6019 HOST_WIDE_INT bitsize;
6020 HOST_WIDE_INT bitpos = 0;
6021 tree offset;
6022 rtx to_rtx = target;
6024 /* Just ignore missing fields. We cleared the whole
6025 structure, above, if any fields are missing. */
6026 if (field == 0)
6027 continue;
6029 if (cleared && initializer_zerop (value))
6030 continue;
6032 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6033 bitsize = tree_to_uhwi (DECL_SIZE (field));
6034 else
6035 bitsize = -1;
6037 mode = DECL_MODE (field);
6038 if (DECL_BIT_FIELD (field))
6039 mode = VOIDmode;
6041 offset = DECL_FIELD_OFFSET (field);
6042 if (tree_fits_shwi_p (offset)
6043 && tree_fits_shwi_p (bit_position (field)))
6045 bitpos = int_bit_position (field);
6046 offset = 0;
6048 else
6049 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
6051 if (offset)
6053 machine_mode address_mode;
6054 rtx offset_rtx;
6056 offset
6057 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6058 make_tree (TREE_TYPE (exp),
6059 target));
6061 offset_rtx = expand_normal (offset);
6062 gcc_assert (MEM_P (to_rtx));
6064 address_mode = get_address_mode (to_rtx);
6065 if (GET_MODE (offset_rtx) != address_mode)
6066 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6068 to_rtx = offset_address (to_rtx, offset_rtx,
6069 highest_pow2_factor (offset));
6072 #ifdef WORD_REGISTER_OPERATIONS
6073 /* If this initializes a field that is smaller than a
6074 word, at the start of a word, try to widen it to a full
6075 word. This special case allows us to output C++ member
6076 function initializations in a form that the optimizers
6077 can understand. */
6078 if (REG_P (target)
6079 && bitsize < BITS_PER_WORD
6080 && bitpos % BITS_PER_WORD == 0
6081 && GET_MODE_CLASS (mode) == MODE_INT
6082 && TREE_CODE (value) == INTEGER_CST
6083 && exp_size >= 0
6084 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6086 tree type = TREE_TYPE (value);
6088 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6090 type = lang_hooks.types.type_for_mode
6091 (word_mode, TYPE_UNSIGNED (type));
6092 value = fold_convert (type, value);
6095 if (BYTES_BIG_ENDIAN)
6096 value
6097 = fold_build2 (LSHIFT_EXPR, type, value,
6098 build_int_cst (type,
6099 BITS_PER_WORD - bitsize));
6100 bitsize = BITS_PER_WORD;
6101 mode = word_mode;
6103 #endif
6105 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6106 && DECL_NONADDRESSABLE_P (field))
6108 to_rtx = copy_rtx (to_rtx);
6109 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6112 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6113 value, cleared,
6114 get_alias_set (TREE_TYPE (field)));
6116 break;
6118 case ARRAY_TYPE:
6120 tree value, index;
6121 unsigned HOST_WIDE_INT i;
6122 int need_to_clear;
6123 tree domain;
6124 tree elttype = TREE_TYPE (type);
6125 int const_bounds_p;
6126 HOST_WIDE_INT minelt = 0;
6127 HOST_WIDE_INT maxelt = 0;
6129 domain = TYPE_DOMAIN (type);
6130 const_bounds_p = (TYPE_MIN_VALUE (domain)
6131 && TYPE_MAX_VALUE (domain)
6132 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6133 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6135 /* If we have constant bounds for the range of the type, get them. */
6136 if (const_bounds_p)
6138 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6139 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6142 /* If the constructor has fewer elements than the array, clear
6143 the whole array first. Similarly if this is static
6144 constructor of a non-BLKmode object. */
6145 if (cleared)
6146 need_to_clear = 0;
6147 else if (REG_P (target) && TREE_STATIC (exp))
6148 need_to_clear = 1;
6149 else
6151 unsigned HOST_WIDE_INT idx;
6152 tree index, value;
6153 HOST_WIDE_INT count = 0, zero_count = 0;
6154 need_to_clear = ! const_bounds_p;
6156 /* This loop is a more accurate version of the loop in
6157 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6158 is also needed to check for missing elements. */
6159 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6161 HOST_WIDE_INT this_node_count;
6163 if (need_to_clear)
6164 break;
6166 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6168 tree lo_index = TREE_OPERAND (index, 0);
6169 tree hi_index = TREE_OPERAND (index, 1);
6171 if (! tree_fits_uhwi_p (lo_index)
6172 || ! tree_fits_uhwi_p (hi_index))
6174 need_to_clear = 1;
6175 break;
6178 this_node_count = (tree_to_uhwi (hi_index)
6179 - tree_to_uhwi (lo_index) + 1);
6181 else
6182 this_node_count = 1;
6184 count += this_node_count;
6185 if (mostly_zeros_p (value))
6186 zero_count += this_node_count;
6189 /* Clear the entire array first if there are any missing
6190 elements, or if the incidence of zero elements is >=
6191 75%. */
6192 if (! need_to_clear
6193 && (count < maxelt - minelt + 1
6194 || 4 * zero_count >= 3 * count))
6195 need_to_clear = 1;
6198 if (need_to_clear && size > 0)
6200 if (REG_P (target))
6201 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6202 else
6203 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6204 cleared = 1;
6207 if (!cleared && REG_P (target))
6208 /* Inform later passes that the old value is dead. */
6209 emit_clobber (target);
6211 /* Store each element of the constructor into the
6212 corresponding element of TARGET, determined by counting the
6213 elements. */
6214 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6216 machine_mode mode;
6217 HOST_WIDE_INT bitsize;
6218 HOST_WIDE_INT bitpos;
6219 rtx xtarget = target;
6221 if (cleared && initializer_zerop (value))
6222 continue;
6224 mode = TYPE_MODE (elttype);
6225 if (mode == BLKmode)
6226 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6227 ? tree_to_uhwi (TYPE_SIZE (elttype))
6228 : -1);
6229 else
6230 bitsize = GET_MODE_BITSIZE (mode);
6232 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6234 tree lo_index = TREE_OPERAND (index, 0);
6235 tree hi_index = TREE_OPERAND (index, 1);
6236 rtx index_r, pos_rtx;
6237 HOST_WIDE_INT lo, hi, count;
6238 tree position;
6240 /* If the range is constant and "small", unroll the loop. */
6241 if (const_bounds_p
6242 && tree_fits_shwi_p (lo_index)
6243 && tree_fits_shwi_p (hi_index)
6244 && (lo = tree_to_shwi (lo_index),
6245 hi = tree_to_shwi (hi_index),
6246 count = hi - lo + 1,
6247 (!MEM_P (target)
6248 || count <= 2
6249 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6250 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6251 <= 40 * 8)))))
6253 lo -= minelt; hi -= minelt;
6254 for (; lo <= hi; lo++)
6256 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6258 if (MEM_P (target)
6259 && !MEM_KEEP_ALIAS_SET_P (target)
6260 && TREE_CODE (type) == ARRAY_TYPE
6261 && TYPE_NONALIASED_COMPONENT (type))
6263 target = copy_rtx (target);
6264 MEM_KEEP_ALIAS_SET_P (target) = 1;
6267 store_constructor_field
6268 (target, bitsize, bitpos, mode, value, cleared,
6269 get_alias_set (elttype));
6272 else
6274 rtx_code_label *loop_start = gen_label_rtx ();
6275 rtx_code_label *loop_end = gen_label_rtx ();
6276 tree exit_cond;
6278 expand_normal (hi_index);
6280 index = build_decl (EXPR_LOCATION (exp),
6281 VAR_DECL, NULL_TREE, domain);
6282 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6283 SET_DECL_RTL (index, index_r);
6284 store_expr (lo_index, index_r, 0, false);
6286 /* Build the head of the loop. */
6287 do_pending_stack_adjust ();
6288 emit_label (loop_start);
6290 /* Assign value to element index. */
6291 position =
6292 fold_convert (ssizetype,
6293 fold_build2 (MINUS_EXPR,
6294 TREE_TYPE (index),
6295 index,
6296 TYPE_MIN_VALUE (domain)));
6298 position =
6299 size_binop (MULT_EXPR, position,
6300 fold_convert (ssizetype,
6301 TYPE_SIZE_UNIT (elttype)));
6303 pos_rtx = expand_normal (position);
6304 xtarget = offset_address (target, pos_rtx,
6305 highest_pow2_factor (position));
6306 xtarget = adjust_address (xtarget, mode, 0);
6307 if (TREE_CODE (value) == CONSTRUCTOR)
6308 store_constructor (value, xtarget, cleared,
6309 bitsize / BITS_PER_UNIT);
6310 else
6311 store_expr (value, xtarget, 0, false);
6313 /* Generate a conditional jump to exit the loop. */
6314 exit_cond = build2 (LT_EXPR, integer_type_node,
6315 index, hi_index);
6316 jumpif (exit_cond, loop_end, -1);
6318 /* Update the loop counter, and jump to the head of
6319 the loop. */
6320 expand_assignment (index,
6321 build2 (PLUS_EXPR, TREE_TYPE (index),
6322 index, integer_one_node),
6323 false);
6325 emit_jump (loop_start);
6327 /* Build the end of the loop. */
6328 emit_label (loop_end);
6331 else if ((index != 0 && ! tree_fits_shwi_p (index))
6332 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6334 tree position;
6336 if (index == 0)
6337 index = ssize_int (1);
6339 if (minelt)
6340 index = fold_convert (ssizetype,
6341 fold_build2 (MINUS_EXPR,
6342 TREE_TYPE (index),
6343 index,
6344 TYPE_MIN_VALUE (domain)));
6346 position =
6347 size_binop (MULT_EXPR, index,
6348 fold_convert (ssizetype,
6349 TYPE_SIZE_UNIT (elttype)));
6350 xtarget = offset_address (target,
6351 expand_normal (position),
6352 highest_pow2_factor (position));
6353 xtarget = adjust_address (xtarget, mode, 0);
6354 store_expr (value, xtarget, 0, false);
6356 else
6358 if (index != 0)
6359 bitpos = ((tree_to_shwi (index) - minelt)
6360 * tree_to_uhwi (TYPE_SIZE (elttype)));
6361 else
6362 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6364 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6365 && TREE_CODE (type) == ARRAY_TYPE
6366 && TYPE_NONALIASED_COMPONENT (type))
6368 target = copy_rtx (target);
6369 MEM_KEEP_ALIAS_SET_P (target) = 1;
6371 store_constructor_field (target, bitsize, bitpos, mode, value,
6372 cleared, get_alias_set (elttype));
6375 break;
6378 case VECTOR_TYPE:
6380 unsigned HOST_WIDE_INT idx;
6381 constructor_elt *ce;
6382 int i;
6383 int need_to_clear;
6384 int icode = CODE_FOR_nothing;
6385 tree elttype = TREE_TYPE (type);
6386 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6387 machine_mode eltmode = TYPE_MODE (elttype);
6388 HOST_WIDE_INT bitsize;
6389 HOST_WIDE_INT bitpos;
6390 rtvec vector = NULL;
6391 unsigned n_elts;
6392 alias_set_type alias;
6394 gcc_assert (eltmode != BLKmode);
6396 n_elts = TYPE_VECTOR_SUBPARTS (type);
6397 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6399 machine_mode mode = GET_MODE (target);
6401 icode = (int) optab_handler (vec_init_optab, mode);
6402 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6403 if (icode != CODE_FOR_nothing)
6405 tree value;
6407 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6408 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6410 icode = CODE_FOR_nothing;
6411 break;
6414 if (icode != CODE_FOR_nothing)
6416 unsigned int i;
6418 vector = rtvec_alloc (n_elts);
6419 for (i = 0; i < n_elts; i++)
6420 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6424 /* If the constructor has fewer elements than the vector,
6425 clear the whole array first. Similarly if this is static
6426 constructor of a non-BLKmode object. */
6427 if (cleared)
6428 need_to_clear = 0;
6429 else if (REG_P (target) && TREE_STATIC (exp))
6430 need_to_clear = 1;
6431 else
6433 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6434 tree value;
6436 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6438 int n_elts_here = tree_to_uhwi
6439 (int_const_binop (TRUNC_DIV_EXPR,
6440 TYPE_SIZE (TREE_TYPE (value)),
6441 TYPE_SIZE (elttype)));
6443 count += n_elts_here;
6444 if (mostly_zeros_p (value))
6445 zero_count += n_elts_here;
6448 /* Clear the entire vector first if there are any missing elements,
6449 or if the incidence of zero elements is >= 75%. */
6450 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6453 if (need_to_clear && size > 0 && !vector)
6455 if (REG_P (target))
6456 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6457 else
6458 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6459 cleared = 1;
6462 /* Inform later passes that the old value is dead. */
6463 if (!cleared && !vector && REG_P (target))
6464 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6466 if (MEM_P (target))
6467 alias = MEM_ALIAS_SET (target);
6468 else
6469 alias = get_alias_set (elttype);
6471 /* Store each element of the constructor into the corresponding
6472 element of TARGET, determined by counting the elements. */
6473 for (idx = 0, i = 0;
6474 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6475 idx++, i += bitsize / elt_size)
6477 HOST_WIDE_INT eltpos;
6478 tree value = ce->value;
6480 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6481 if (cleared && initializer_zerop (value))
6482 continue;
6484 if (ce->index)
6485 eltpos = tree_to_uhwi (ce->index);
6486 else
6487 eltpos = i;
6489 if (vector)
6491 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6492 elements. */
6493 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6494 RTVEC_ELT (vector, eltpos)
6495 = expand_normal (value);
6497 else
6499 machine_mode value_mode =
6500 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6501 ? TYPE_MODE (TREE_TYPE (value))
6502 : eltmode;
6503 bitpos = eltpos * elt_size;
6504 store_constructor_field (target, bitsize, bitpos, value_mode,
6505 value, cleared, alias);
6509 if (vector)
6510 emit_insn (GEN_FCN (icode)
6511 (target,
6512 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6513 break;
6516 default:
6517 gcc_unreachable ();
6521 /* Store the value of EXP (an expression tree)
6522 into a subfield of TARGET which has mode MODE and occupies
6523 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6524 If MODE is VOIDmode, it means that we are storing into a bit-field.
6526 BITREGION_START is bitpos of the first bitfield in this region.
6527 BITREGION_END is the bitpos of the ending bitfield in this region.
6528 These two fields are 0, if the C++ memory model does not apply,
6529 or we are not interested in keeping track of bitfield regions.
6531 Always return const0_rtx unless we have something particular to
6532 return.
6534 ALIAS_SET is the alias set for the destination. This value will
6535 (in general) be different from that for TARGET, since TARGET is a
6536 reference to the containing structure.
6538 If NONTEMPORAL is true, try generating a nontemporal store. */
6540 static rtx
6541 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6542 unsigned HOST_WIDE_INT bitregion_start,
6543 unsigned HOST_WIDE_INT bitregion_end,
6544 machine_mode mode, tree exp,
6545 alias_set_type alias_set, bool nontemporal)
6547 if (TREE_CODE (exp) == ERROR_MARK)
6548 return const0_rtx;
6550 /* If we have nothing to store, do nothing unless the expression has
6551 side-effects. */
6552 if (bitsize == 0)
6553 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6555 if (GET_CODE (target) == CONCAT)
6557 /* We're storing into a struct containing a single __complex. */
6559 gcc_assert (!bitpos);
6560 return store_expr (exp, target, 0, nontemporal);
6563 /* If the structure is in a register or if the component
6564 is a bit field, we cannot use addressing to access it.
6565 Use bit-field techniques or SUBREG to store in it. */
6567 if (mode == VOIDmode
6568 || (mode != BLKmode && ! direct_store[(int) mode]
6569 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6570 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6571 || REG_P (target)
6572 || GET_CODE (target) == SUBREG
6573 /* If the field isn't aligned enough to store as an ordinary memref,
6574 store it as a bit field. */
6575 || (mode != BLKmode
6576 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6577 || bitpos % GET_MODE_ALIGNMENT (mode))
6578 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6579 || (bitpos % BITS_PER_UNIT != 0)))
6580 || (bitsize >= 0 && mode != BLKmode
6581 && GET_MODE_BITSIZE (mode) > bitsize)
6582 /* If the RHS and field are a constant size and the size of the
6583 RHS isn't the same size as the bitfield, we must use bitfield
6584 operations. */
6585 || (bitsize >= 0
6586 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6587 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6588 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6589 decl we must use bitfield operations. */
6590 || (bitsize >= 0
6591 && TREE_CODE (exp) == MEM_REF
6592 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6593 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6594 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6595 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6597 rtx temp;
6598 gimple nop_def;
6600 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6601 implies a mask operation. If the precision is the same size as
6602 the field we're storing into, that mask is redundant. This is
6603 particularly common with bit field assignments generated by the
6604 C front end. */
6605 nop_def = get_def_for_expr (exp, NOP_EXPR);
6606 if (nop_def)
6608 tree type = TREE_TYPE (exp);
6609 if (INTEGRAL_TYPE_P (type)
6610 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6611 && bitsize == TYPE_PRECISION (type))
6613 tree op = gimple_assign_rhs1 (nop_def);
6614 type = TREE_TYPE (op);
6615 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6616 exp = op;
6620 temp = expand_normal (exp);
6622 /* If BITSIZE is narrower than the size of the type of EXP
6623 we will be narrowing TEMP. Normally, what's wanted are the
6624 low-order bits. However, if EXP's type is a record and this is
6625 big-endian machine, we want the upper BITSIZE bits. */
6626 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6627 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6628 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6629 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6630 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6631 NULL_RTX, 1);
6633 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6634 if (mode != VOIDmode && mode != BLKmode
6635 && mode != TYPE_MODE (TREE_TYPE (exp)))
6636 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6638 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6639 are both BLKmode, both must be in memory and BITPOS must be aligned
6640 on a byte boundary. If so, we simply do a block copy. Likewise for
6641 a BLKmode-like TARGET. */
6642 if (GET_CODE (temp) != PARALLEL
6643 && GET_MODE (temp) == BLKmode
6644 && (GET_MODE (target) == BLKmode
6645 || (MEM_P (target)
6646 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6647 && (bitpos % BITS_PER_UNIT) == 0
6648 && (bitsize % BITS_PER_UNIT) == 0)))
6650 gcc_assert (MEM_P (target) && MEM_P (temp)
6651 && (bitpos % BITS_PER_UNIT) == 0);
6653 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6654 emit_block_move (target, temp,
6655 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6656 / BITS_PER_UNIT),
6657 BLOCK_OP_NORMAL);
6659 return const0_rtx;
6662 /* Handle calls that return values in multiple non-contiguous locations.
6663 The Irix 6 ABI has examples of this. */
6664 if (GET_CODE (temp) == PARALLEL)
6666 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6667 rtx temp_target;
6668 if (mode == BLKmode || mode == VOIDmode)
6669 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6670 temp_target = gen_reg_rtx (mode);
6671 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6672 temp = temp_target;
6674 else if (mode == BLKmode)
6676 /* Handle calls that return BLKmode values in registers. */
6677 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6679 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6680 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6681 temp = temp_target;
6683 else
6685 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6686 rtx temp_target;
6687 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6688 temp_target = gen_reg_rtx (mode);
6689 temp_target
6690 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6691 temp_target, mode, mode);
6692 temp = temp_target;
6696 /* Store the value in the bitfield. */
6697 store_bit_field (target, bitsize, bitpos,
6698 bitregion_start, bitregion_end,
6699 mode, temp);
6701 return const0_rtx;
6703 else
6705 /* Now build a reference to just the desired component. */
6706 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6708 if (to_rtx == target)
6709 to_rtx = copy_rtx (to_rtx);
6711 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6712 set_mem_alias_set (to_rtx, alias_set);
6714 return store_expr (exp, to_rtx, 0, nontemporal);
6718 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6719 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6720 codes and find the ultimate containing object, which we return.
6722 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6723 bit position, and *PUNSIGNEDP to the signedness of the field.
6724 If the position of the field is variable, we store a tree
6725 giving the variable offset (in units) in *POFFSET.
6726 This offset is in addition to the bit position.
6727 If the position is not variable, we store 0 in *POFFSET.
6729 If any of the extraction expressions is volatile,
6730 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6732 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6733 Otherwise, it is a mode that can be used to access the field.
6735 If the field describes a variable-sized object, *PMODE is set to
6736 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6737 this case, but the address of the object can be found.
6739 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6740 look through nodes that serve as markers of a greater alignment than
6741 the one that can be deduced from the expression. These nodes make it
6742 possible for front-ends to prevent temporaries from being created by
6743 the middle-end on alignment considerations. For that purpose, the
6744 normal operating mode at high-level is to always pass FALSE so that
6745 the ultimate containing object is really returned; moreover, the
6746 associated predicate handled_component_p will always return TRUE
6747 on these nodes, thus indicating that they are essentially handled
6748 by get_inner_reference. TRUE should only be passed when the caller
6749 is scanning the expression in order to build another representation
6750 and specifically knows how to handle these nodes; as such, this is
6751 the normal operating mode in the RTL expanders. */
6753 tree
6754 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6755 HOST_WIDE_INT *pbitpos, tree *poffset,
6756 machine_mode *pmode, int *punsignedp,
6757 int *pvolatilep, bool keep_aligning)
6759 tree size_tree = 0;
6760 machine_mode mode = VOIDmode;
6761 bool blkmode_bitfield = false;
6762 tree offset = size_zero_node;
6763 offset_int bit_offset = 0;
6765 /* First get the mode, signedness, and size. We do this from just the
6766 outermost expression. */
6767 *pbitsize = -1;
6768 if (TREE_CODE (exp) == COMPONENT_REF)
6770 tree field = TREE_OPERAND (exp, 1);
6771 size_tree = DECL_SIZE (field);
6772 if (flag_strict_volatile_bitfields > 0
6773 && TREE_THIS_VOLATILE (exp)
6774 && DECL_BIT_FIELD_TYPE (field)
6775 && DECL_MODE (field) != BLKmode)
6776 /* Volatile bitfields should be accessed in the mode of the
6777 field's type, not the mode computed based on the bit
6778 size. */
6779 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6780 else if (!DECL_BIT_FIELD (field))
6781 mode = DECL_MODE (field);
6782 else if (DECL_MODE (field) == BLKmode)
6783 blkmode_bitfield = true;
6785 *punsignedp = DECL_UNSIGNED (field);
6787 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6789 size_tree = TREE_OPERAND (exp, 1);
6790 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6791 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6793 /* For vector types, with the correct size of access, use the mode of
6794 inner type. */
6795 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6796 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6797 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6798 mode = TYPE_MODE (TREE_TYPE (exp));
6800 else
6802 mode = TYPE_MODE (TREE_TYPE (exp));
6803 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6805 if (mode == BLKmode)
6806 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6807 else
6808 *pbitsize = GET_MODE_BITSIZE (mode);
6811 if (size_tree != 0)
6813 if (! tree_fits_uhwi_p (size_tree))
6814 mode = BLKmode, *pbitsize = -1;
6815 else
6816 *pbitsize = tree_to_uhwi (size_tree);
6819 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6820 and find the ultimate containing object. */
6821 while (1)
6823 switch (TREE_CODE (exp))
6825 case BIT_FIELD_REF:
6826 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6827 break;
6829 case COMPONENT_REF:
6831 tree field = TREE_OPERAND (exp, 1);
6832 tree this_offset = component_ref_field_offset (exp);
6834 /* If this field hasn't been filled in yet, don't go past it.
6835 This should only happen when folding expressions made during
6836 type construction. */
6837 if (this_offset == 0)
6838 break;
6840 offset = size_binop (PLUS_EXPR, offset, this_offset);
6841 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6843 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6845 break;
6847 case ARRAY_REF:
6848 case ARRAY_RANGE_REF:
6850 tree index = TREE_OPERAND (exp, 1);
6851 tree low_bound = array_ref_low_bound (exp);
6852 tree unit_size = array_ref_element_size (exp);
6854 /* We assume all arrays have sizes that are a multiple of a byte.
6855 First subtract the lower bound, if any, in the type of the
6856 index, then convert to sizetype and multiply by the size of
6857 the array element. */
6858 if (! integer_zerop (low_bound))
6859 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6860 index, low_bound);
6862 offset = size_binop (PLUS_EXPR, offset,
6863 size_binop (MULT_EXPR,
6864 fold_convert (sizetype, index),
6865 unit_size));
6867 break;
6869 case REALPART_EXPR:
6870 break;
6872 case IMAGPART_EXPR:
6873 bit_offset += *pbitsize;
6874 break;
6876 case VIEW_CONVERT_EXPR:
6877 if (keep_aligning && STRICT_ALIGNMENT
6878 && (TYPE_ALIGN (TREE_TYPE (exp))
6879 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6880 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6881 < BIGGEST_ALIGNMENT)
6882 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6883 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6884 goto done;
6885 break;
6887 case MEM_REF:
6888 /* Hand back the decl for MEM[&decl, off]. */
6889 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6891 tree off = TREE_OPERAND (exp, 1);
6892 if (!integer_zerop (off))
6894 offset_int boff, coff = mem_ref_offset (exp);
6895 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6896 bit_offset += boff;
6898 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6900 goto done;
6902 default:
6903 goto done;
6906 /* If any reference in the chain is volatile, the effect is volatile. */
6907 if (TREE_THIS_VOLATILE (exp))
6908 *pvolatilep = 1;
6910 exp = TREE_OPERAND (exp, 0);
6912 done:
6914 /* If OFFSET is constant, see if we can return the whole thing as a
6915 constant bit position. Make sure to handle overflow during
6916 this conversion. */
6917 if (TREE_CODE (offset) == INTEGER_CST)
6919 offset_int tem = wi::sext (wi::to_offset (offset),
6920 TYPE_PRECISION (sizetype));
6921 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6922 tem += bit_offset;
6923 if (wi::fits_shwi_p (tem))
6925 *pbitpos = tem.to_shwi ();
6926 *poffset = offset = NULL_TREE;
6930 /* Otherwise, split it up. */
6931 if (offset)
6933 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6934 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
6936 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6937 offset_int tem = bit_offset.and_not (mask);
6938 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6939 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6940 bit_offset -= tem;
6941 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6942 offset = size_binop (PLUS_EXPR, offset,
6943 wide_int_to_tree (sizetype, tem));
6946 *pbitpos = bit_offset.to_shwi ();
6947 *poffset = offset;
6950 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6951 if (mode == VOIDmode
6952 && blkmode_bitfield
6953 && (*pbitpos % BITS_PER_UNIT) == 0
6954 && (*pbitsize % BITS_PER_UNIT) == 0)
6955 *pmode = BLKmode;
6956 else
6957 *pmode = mode;
6959 return exp;
6962 /* Return a tree of sizetype representing the size, in bytes, of the element
6963 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6965 tree
6966 array_ref_element_size (tree exp)
6968 tree aligned_size = TREE_OPERAND (exp, 3);
6969 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6970 location_t loc = EXPR_LOCATION (exp);
6972 /* If a size was specified in the ARRAY_REF, it's the size measured
6973 in alignment units of the element type. So multiply by that value. */
6974 if (aligned_size)
6976 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6977 sizetype from another type of the same width and signedness. */
6978 if (TREE_TYPE (aligned_size) != sizetype)
6979 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6980 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6981 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6984 /* Otherwise, take the size from that of the element type. Substitute
6985 any PLACEHOLDER_EXPR that we have. */
6986 else
6987 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6990 /* Return a tree representing the lower bound of the array mentioned in
6991 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6993 tree
6994 array_ref_low_bound (tree exp)
6996 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6998 /* If a lower bound is specified in EXP, use it. */
6999 if (TREE_OPERAND (exp, 2))
7000 return TREE_OPERAND (exp, 2);
7002 /* Otherwise, if there is a domain type and it has a lower bound, use it,
7003 substituting for a PLACEHOLDER_EXPR as needed. */
7004 if (domain_type && TYPE_MIN_VALUE (domain_type))
7005 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
7007 /* Otherwise, return a zero of the appropriate type. */
7008 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
7011 /* Returns true if REF is an array reference to an array at the end of
7012 a structure. If this is the case, the array may be allocated larger
7013 than its upper bound implies. */
7015 bool
7016 array_at_struct_end_p (tree ref)
7018 if (TREE_CODE (ref) != ARRAY_REF
7019 && TREE_CODE (ref) != ARRAY_RANGE_REF)
7020 return false;
7022 while (handled_component_p (ref))
7024 /* If the reference chain contains a component reference to a
7025 non-union type and there follows another field the reference
7026 is not at the end of a structure. */
7027 if (TREE_CODE (ref) == COMPONENT_REF
7028 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
7030 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
7031 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
7032 nextf = DECL_CHAIN (nextf);
7033 if (nextf)
7034 return false;
7037 ref = TREE_OPERAND (ref, 0);
7040 /* If the reference is based on a declared entity, the size of the array
7041 is constrained by its given domain. */
7042 if (DECL_P (ref))
7043 return false;
7045 return true;
7048 /* Return a tree representing the upper bound of the array mentioned in
7049 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
7051 tree
7052 array_ref_up_bound (tree exp)
7054 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
7056 /* If there is a domain type and it has an upper bound, use it, substituting
7057 for a PLACEHOLDER_EXPR as needed. */
7058 if (domain_type && TYPE_MAX_VALUE (domain_type))
7059 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
7061 /* Otherwise fail. */
7062 return NULL_TREE;
7065 /* Return a tree representing the offset, in bytes, of the field referenced
7066 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
7068 tree
7069 component_ref_field_offset (tree exp)
7071 tree aligned_offset = TREE_OPERAND (exp, 2);
7072 tree field = TREE_OPERAND (exp, 1);
7073 location_t loc = EXPR_LOCATION (exp);
7075 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
7076 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
7077 value. */
7078 if (aligned_offset)
7080 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7081 sizetype from another type of the same width and signedness. */
7082 if (TREE_TYPE (aligned_offset) != sizetype)
7083 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
7084 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
7085 size_int (DECL_OFFSET_ALIGN (field)
7086 / BITS_PER_UNIT));
7089 /* Otherwise, take the offset from that of the field. Substitute
7090 any PLACEHOLDER_EXPR that we have. */
7091 else
7092 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
7095 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7097 static unsigned HOST_WIDE_INT
7098 target_align (const_tree target)
7100 /* We might have a chain of nested references with intermediate misaligning
7101 bitfields components, so need to recurse to find out. */
7103 unsigned HOST_WIDE_INT this_align, outer_align;
7105 switch (TREE_CODE (target))
7107 case BIT_FIELD_REF:
7108 return 1;
7110 case COMPONENT_REF:
7111 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7112 outer_align = target_align (TREE_OPERAND (target, 0));
7113 return MIN (this_align, outer_align);
7115 case ARRAY_REF:
7116 case ARRAY_RANGE_REF:
7117 this_align = TYPE_ALIGN (TREE_TYPE (target));
7118 outer_align = target_align (TREE_OPERAND (target, 0));
7119 return MIN (this_align, outer_align);
7121 CASE_CONVERT:
7122 case NON_LVALUE_EXPR:
7123 case VIEW_CONVERT_EXPR:
7124 this_align = TYPE_ALIGN (TREE_TYPE (target));
7125 outer_align = target_align (TREE_OPERAND (target, 0));
7126 return MAX (this_align, outer_align);
7128 default:
7129 return TYPE_ALIGN (TREE_TYPE (target));
7134 /* Given an rtx VALUE that may contain additions and multiplications, return
7135 an equivalent value that just refers to a register, memory, or constant.
7136 This is done by generating instructions to perform the arithmetic and
7137 returning a pseudo-register containing the value.
7139 The returned value may be a REG, SUBREG, MEM or constant. */
7142 force_operand (rtx value, rtx target)
7144 rtx op1, op2;
7145 /* Use subtarget as the target for operand 0 of a binary operation. */
7146 rtx subtarget = get_subtarget (target);
7147 enum rtx_code code = GET_CODE (value);
7149 /* Check for subreg applied to an expression produced by loop optimizer. */
7150 if (code == SUBREG
7151 && !REG_P (SUBREG_REG (value))
7152 && !MEM_P (SUBREG_REG (value)))
7154 value
7155 = simplify_gen_subreg (GET_MODE (value),
7156 force_reg (GET_MODE (SUBREG_REG (value)),
7157 force_operand (SUBREG_REG (value),
7158 NULL_RTX)),
7159 GET_MODE (SUBREG_REG (value)),
7160 SUBREG_BYTE (value));
7161 code = GET_CODE (value);
7164 /* Check for a PIC address load. */
7165 if ((code == PLUS || code == MINUS)
7166 && XEXP (value, 0) == pic_offset_table_rtx
7167 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7168 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7169 || GET_CODE (XEXP (value, 1)) == CONST))
7171 if (!subtarget)
7172 subtarget = gen_reg_rtx (GET_MODE (value));
7173 emit_move_insn (subtarget, value);
7174 return subtarget;
7177 if (ARITHMETIC_P (value))
7179 op2 = XEXP (value, 1);
7180 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7181 subtarget = 0;
7182 if (code == MINUS && CONST_INT_P (op2))
7184 code = PLUS;
7185 op2 = negate_rtx (GET_MODE (value), op2);
7188 /* Check for an addition with OP2 a constant integer and our first
7189 operand a PLUS of a virtual register and something else. In that
7190 case, we want to emit the sum of the virtual register and the
7191 constant first and then add the other value. This allows virtual
7192 register instantiation to simply modify the constant rather than
7193 creating another one around this addition. */
7194 if (code == PLUS && CONST_INT_P (op2)
7195 && GET_CODE (XEXP (value, 0)) == PLUS
7196 && REG_P (XEXP (XEXP (value, 0), 0))
7197 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7198 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7200 rtx temp = expand_simple_binop (GET_MODE (value), code,
7201 XEXP (XEXP (value, 0), 0), op2,
7202 subtarget, 0, OPTAB_LIB_WIDEN);
7203 return expand_simple_binop (GET_MODE (value), code, temp,
7204 force_operand (XEXP (XEXP (value,
7205 0), 1), 0),
7206 target, 0, OPTAB_LIB_WIDEN);
7209 op1 = force_operand (XEXP (value, 0), subtarget);
7210 op2 = force_operand (op2, NULL_RTX);
7211 switch (code)
7213 case MULT:
7214 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7215 case DIV:
7216 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7217 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7218 target, 1, OPTAB_LIB_WIDEN);
7219 else
7220 return expand_divmod (0,
7221 FLOAT_MODE_P (GET_MODE (value))
7222 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7223 GET_MODE (value), op1, op2, target, 0);
7224 case MOD:
7225 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7226 target, 0);
7227 case UDIV:
7228 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7229 target, 1);
7230 case UMOD:
7231 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7232 target, 1);
7233 case ASHIFTRT:
7234 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7235 target, 0, OPTAB_LIB_WIDEN);
7236 default:
7237 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7238 target, 1, OPTAB_LIB_WIDEN);
7241 if (UNARY_P (value))
7243 if (!target)
7244 target = gen_reg_rtx (GET_MODE (value));
7245 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7246 switch (code)
7248 case ZERO_EXTEND:
7249 case SIGN_EXTEND:
7250 case TRUNCATE:
7251 case FLOAT_EXTEND:
7252 case FLOAT_TRUNCATE:
7253 convert_move (target, op1, code == ZERO_EXTEND);
7254 return target;
7256 case FIX:
7257 case UNSIGNED_FIX:
7258 expand_fix (target, op1, code == UNSIGNED_FIX);
7259 return target;
7261 case FLOAT:
7262 case UNSIGNED_FLOAT:
7263 expand_float (target, op1, code == UNSIGNED_FLOAT);
7264 return target;
7266 default:
7267 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7271 #ifdef INSN_SCHEDULING
7272 /* On machines that have insn scheduling, we want all memory reference to be
7273 explicit, so we need to deal with such paradoxical SUBREGs. */
7274 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7275 value
7276 = simplify_gen_subreg (GET_MODE (value),
7277 force_reg (GET_MODE (SUBREG_REG (value)),
7278 force_operand (SUBREG_REG (value),
7279 NULL_RTX)),
7280 GET_MODE (SUBREG_REG (value)),
7281 SUBREG_BYTE (value));
7282 #endif
7284 return value;
7287 /* Subroutine of expand_expr: return nonzero iff there is no way that
7288 EXP can reference X, which is being modified. TOP_P is nonzero if this
7289 call is going to be used to determine whether we need a temporary
7290 for EXP, as opposed to a recursive call to this function.
7292 It is always safe for this routine to return zero since it merely
7293 searches for optimization opportunities. */
7296 safe_from_p (const_rtx x, tree exp, int top_p)
7298 rtx exp_rtl = 0;
7299 int i, nops;
7301 if (x == 0
7302 /* If EXP has varying size, we MUST use a target since we currently
7303 have no way of allocating temporaries of variable size
7304 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7305 So we assume here that something at a higher level has prevented a
7306 clash. This is somewhat bogus, but the best we can do. Only
7307 do this when X is BLKmode and when we are at the top level. */
7308 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7309 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7310 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7311 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7312 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7313 != INTEGER_CST)
7314 && GET_MODE (x) == BLKmode)
7315 /* If X is in the outgoing argument area, it is always safe. */
7316 || (MEM_P (x)
7317 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7318 || (GET_CODE (XEXP (x, 0)) == PLUS
7319 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7320 return 1;
7322 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7323 find the underlying pseudo. */
7324 if (GET_CODE (x) == SUBREG)
7326 x = SUBREG_REG (x);
7327 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7328 return 0;
7331 /* Now look at our tree code and possibly recurse. */
7332 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7334 case tcc_declaration:
7335 exp_rtl = DECL_RTL_IF_SET (exp);
7336 break;
7338 case tcc_constant:
7339 return 1;
7341 case tcc_exceptional:
7342 if (TREE_CODE (exp) == TREE_LIST)
7344 while (1)
7346 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7347 return 0;
7348 exp = TREE_CHAIN (exp);
7349 if (!exp)
7350 return 1;
7351 if (TREE_CODE (exp) != TREE_LIST)
7352 return safe_from_p (x, exp, 0);
7355 else if (TREE_CODE (exp) == CONSTRUCTOR)
7357 constructor_elt *ce;
7358 unsigned HOST_WIDE_INT idx;
7360 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7361 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7362 || !safe_from_p (x, ce->value, 0))
7363 return 0;
7364 return 1;
7366 else if (TREE_CODE (exp) == ERROR_MARK)
7367 return 1; /* An already-visited SAVE_EXPR? */
7368 else
7369 return 0;
7371 case tcc_statement:
7372 /* The only case we look at here is the DECL_INITIAL inside a
7373 DECL_EXPR. */
7374 return (TREE_CODE (exp) != DECL_EXPR
7375 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7376 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7377 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7379 case tcc_binary:
7380 case tcc_comparison:
7381 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7382 return 0;
7383 /* Fall through. */
7385 case tcc_unary:
7386 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7388 case tcc_expression:
7389 case tcc_reference:
7390 case tcc_vl_exp:
7391 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7392 the expression. If it is set, we conflict iff we are that rtx or
7393 both are in memory. Otherwise, we check all operands of the
7394 expression recursively. */
7396 switch (TREE_CODE (exp))
7398 case ADDR_EXPR:
7399 /* If the operand is static or we are static, we can't conflict.
7400 Likewise if we don't conflict with the operand at all. */
7401 if (staticp (TREE_OPERAND (exp, 0))
7402 || TREE_STATIC (exp)
7403 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7404 return 1;
7406 /* Otherwise, the only way this can conflict is if we are taking
7407 the address of a DECL a that address if part of X, which is
7408 very rare. */
7409 exp = TREE_OPERAND (exp, 0);
7410 if (DECL_P (exp))
7412 if (!DECL_RTL_SET_P (exp)
7413 || !MEM_P (DECL_RTL (exp)))
7414 return 0;
7415 else
7416 exp_rtl = XEXP (DECL_RTL (exp), 0);
7418 break;
7420 case MEM_REF:
7421 if (MEM_P (x)
7422 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7423 get_alias_set (exp)))
7424 return 0;
7425 break;
7427 case CALL_EXPR:
7428 /* Assume that the call will clobber all hard registers and
7429 all of memory. */
7430 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7431 || MEM_P (x))
7432 return 0;
7433 break;
7435 case WITH_CLEANUP_EXPR:
7436 case CLEANUP_POINT_EXPR:
7437 /* Lowered by gimplify.c. */
7438 gcc_unreachable ();
7440 case SAVE_EXPR:
7441 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7443 default:
7444 break;
7447 /* If we have an rtx, we do not need to scan our operands. */
7448 if (exp_rtl)
7449 break;
7451 nops = TREE_OPERAND_LENGTH (exp);
7452 for (i = 0; i < nops; i++)
7453 if (TREE_OPERAND (exp, i) != 0
7454 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7455 return 0;
7457 break;
7459 case tcc_type:
7460 /* Should never get a type here. */
7461 gcc_unreachable ();
7464 /* If we have an rtl, find any enclosed object. Then see if we conflict
7465 with it. */
7466 if (exp_rtl)
7468 if (GET_CODE (exp_rtl) == SUBREG)
7470 exp_rtl = SUBREG_REG (exp_rtl);
7471 if (REG_P (exp_rtl)
7472 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7473 return 0;
7476 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7477 are memory and they conflict. */
7478 return ! (rtx_equal_p (x, exp_rtl)
7479 || (MEM_P (x) && MEM_P (exp_rtl)
7480 && true_dependence (exp_rtl, VOIDmode, x)));
7483 /* If we reach here, it is safe. */
7484 return 1;
7488 /* Return the highest power of two that EXP is known to be a multiple of.
7489 This is used in updating alignment of MEMs in array references. */
7491 unsigned HOST_WIDE_INT
7492 highest_pow2_factor (const_tree exp)
7494 unsigned HOST_WIDE_INT ret;
7495 int trailing_zeros = tree_ctz (exp);
7496 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7497 return BIGGEST_ALIGNMENT;
7498 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7499 if (ret > BIGGEST_ALIGNMENT)
7500 return BIGGEST_ALIGNMENT;
7501 return ret;
7504 /* Similar, except that the alignment requirements of TARGET are
7505 taken into account. Assume it is at least as aligned as its
7506 type, unless it is a COMPONENT_REF in which case the layout of
7507 the structure gives the alignment. */
7509 static unsigned HOST_WIDE_INT
7510 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7512 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7513 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7515 return MAX (factor, talign);
7518 /* Convert the tree comparison code TCODE to the rtl one where the
7519 signedness is UNSIGNEDP. */
7521 static enum rtx_code
7522 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7524 enum rtx_code code;
7525 switch (tcode)
7527 case EQ_EXPR:
7528 code = EQ;
7529 break;
7530 case NE_EXPR:
7531 code = NE;
7532 break;
7533 case LT_EXPR:
7534 code = unsignedp ? LTU : LT;
7535 break;
7536 case LE_EXPR:
7537 code = unsignedp ? LEU : LE;
7538 break;
7539 case GT_EXPR:
7540 code = unsignedp ? GTU : GT;
7541 break;
7542 case GE_EXPR:
7543 code = unsignedp ? GEU : GE;
7544 break;
7545 case UNORDERED_EXPR:
7546 code = UNORDERED;
7547 break;
7548 case ORDERED_EXPR:
7549 code = ORDERED;
7550 break;
7551 case UNLT_EXPR:
7552 code = UNLT;
7553 break;
7554 case UNLE_EXPR:
7555 code = UNLE;
7556 break;
7557 case UNGT_EXPR:
7558 code = UNGT;
7559 break;
7560 case UNGE_EXPR:
7561 code = UNGE;
7562 break;
7563 case UNEQ_EXPR:
7564 code = UNEQ;
7565 break;
7566 case LTGT_EXPR:
7567 code = LTGT;
7568 break;
7570 default:
7571 gcc_unreachable ();
7573 return code;
7576 /* Subroutine of expand_expr. Expand the two operands of a binary
7577 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7578 The value may be stored in TARGET if TARGET is nonzero. The
7579 MODIFIER argument is as documented by expand_expr. */
7581 void
7582 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7583 enum expand_modifier modifier)
7585 if (! safe_from_p (target, exp1, 1))
7586 target = 0;
7587 if (operand_equal_p (exp0, exp1, 0))
7589 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7590 *op1 = copy_rtx (*op0);
7592 else
7594 /* If we need to preserve evaluation order, copy exp0 into its own
7595 temporary variable so that it can't be clobbered by exp1. */
7596 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7597 exp0 = save_expr (exp0);
7598 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7599 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7604 /* Return a MEM that contains constant EXP. DEFER is as for
7605 output_constant_def and MODIFIER is as for expand_expr. */
7607 static rtx
7608 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7610 rtx mem;
7612 mem = output_constant_def (exp, defer);
7613 if (modifier != EXPAND_INITIALIZER)
7614 mem = use_anchored_address (mem);
7615 return mem;
7618 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7619 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7621 static rtx
7622 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7623 enum expand_modifier modifier, addr_space_t as)
7625 rtx result, subtarget;
7626 tree inner, offset;
7627 HOST_WIDE_INT bitsize, bitpos;
7628 int volatilep, unsignedp;
7629 machine_mode mode1;
7631 /* If we are taking the address of a constant and are at the top level,
7632 we have to use output_constant_def since we can't call force_const_mem
7633 at top level. */
7634 /* ??? This should be considered a front-end bug. We should not be
7635 generating ADDR_EXPR of something that isn't an LVALUE. The only
7636 exception here is STRING_CST. */
7637 if (CONSTANT_CLASS_P (exp))
7639 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7640 if (modifier < EXPAND_SUM)
7641 result = force_operand (result, target);
7642 return result;
7645 /* Everything must be something allowed by is_gimple_addressable. */
7646 switch (TREE_CODE (exp))
7648 case INDIRECT_REF:
7649 /* This case will happen via recursion for &a->b. */
7650 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7652 case MEM_REF:
7654 tree tem = TREE_OPERAND (exp, 0);
7655 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7656 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7657 return expand_expr (tem, target, tmode, modifier);
7660 case CONST_DECL:
7661 /* Expand the initializer like constants above. */
7662 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7663 0, modifier), 0);
7664 if (modifier < EXPAND_SUM)
7665 result = force_operand (result, target);
7666 return result;
7668 case REALPART_EXPR:
7669 /* The real part of the complex number is always first, therefore
7670 the address is the same as the address of the parent object. */
7671 offset = 0;
7672 bitpos = 0;
7673 inner = TREE_OPERAND (exp, 0);
7674 break;
7676 case IMAGPART_EXPR:
7677 /* The imaginary part of the complex number is always second.
7678 The expression is therefore always offset by the size of the
7679 scalar type. */
7680 offset = 0;
7681 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7682 inner = TREE_OPERAND (exp, 0);
7683 break;
7685 case COMPOUND_LITERAL_EXPR:
7686 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7687 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7688 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7689 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7690 the initializers aren't gimplified. */
7691 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7692 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7693 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7694 target, tmode, modifier, as);
7695 /* FALLTHRU */
7696 default:
7697 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7698 expand_expr, as that can have various side effects; LABEL_DECLs for
7699 example, may not have their DECL_RTL set yet. Expand the rtl of
7700 CONSTRUCTORs too, which should yield a memory reference for the
7701 constructor's contents. Assume language specific tree nodes can
7702 be expanded in some interesting way. */
7703 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7704 if (DECL_P (exp)
7705 || TREE_CODE (exp) == CONSTRUCTOR
7706 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7708 result = expand_expr (exp, target, tmode,
7709 modifier == EXPAND_INITIALIZER
7710 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7712 /* If the DECL isn't in memory, then the DECL wasn't properly
7713 marked TREE_ADDRESSABLE, which will be either a front-end
7714 or a tree optimizer bug. */
7716 if (TREE_ADDRESSABLE (exp)
7717 && ! MEM_P (result)
7718 && ! targetm.calls.allocate_stack_slots_for_args ())
7720 error ("local frame unavailable (naked function?)");
7721 return result;
7723 else
7724 gcc_assert (MEM_P (result));
7725 result = XEXP (result, 0);
7727 /* ??? Is this needed anymore? */
7728 if (DECL_P (exp))
7729 TREE_USED (exp) = 1;
7731 if (modifier != EXPAND_INITIALIZER
7732 && modifier != EXPAND_CONST_ADDRESS
7733 && modifier != EXPAND_SUM)
7734 result = force_operand (result, target);
7735 return result;
7738 /* Pass FALSE as the last argument to get_inner_reference although
7739 we are expanding to RTL. The rationale is that we know how to
7740 handle "aligning nodes" here: we can just bypass them because
7741 they won't change the final object whose address will be returned
7742 (they actually exist only for that purpose). */
7743 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7744 &mode1, &unsignedp, &volatilep, false);
7745 break;
7748 /* We must have made progress. */
7749 gcc_assert (inner != exp);
7751 subtarget = offset || bitpos ? NULL_RTX : target;
7752 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7753 inner alignment, force the inner to be sufficiently aligned. */
7754 if (CONSTANT_CLASS_P (inner)
7755 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7757 inner = copy_node (inner);
7758 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7759 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7760 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7762 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7764 if (offset)
7766 rtx tmp;
7768 if (modifier != EXPAND_NORMAL)
7769 result = force_operand (result, NULL);
7770 tmp = expand_expr (offset, NULL_RTX, tmode,
7771 modifier == EXPAND_INITIALIZER
7772 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7774 /* expand_expr is allowed to return an object in a mode other
7775 than TMODE. If it did, we need to convert. */
7776 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7777 tmp = convert_modes (tmode, GET_MODE (tmp),
7778 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7779 result = convert_memory_address_addr_space (tmode, result, as);
7780 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7782 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7783 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7784 else
7786 subtarget = bitpos ? NULL_RTX : target;
7787 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7788 1, OPTAB_LIB_WIDEN);
7792 if (bitpos)
7794 /* Someone beforehand should have rejected taking the address
7795 of such an object. */
7796 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7798 result = convert_memory_address_addr_space (tmode, result, as);
7799 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7800 if (modifier < EXPAND_SUM)
7801 result = force_operand (result, target);
7804 return result;
7807 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7808 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7810 static rtx
7811 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7812 enum expand_modifier modifier)
7814 addr_space_t as = ADDR_SPACE_GENERIC;
7815 machine_mode address_mode = Pmode;
7816 machine_mode pointer_mode = ptr_mode;
7817 machine_mode rmode;
7818 rtx result;
7820 /* Target mode of VOIDmode says "whatever's natural". */
7821 if (tmode == VOIDmode)
7822 tmode = TYPE_MODE (TREE_TYPE (exp));
7824 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7826 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7827 address_mode = targetm.addr_space.address_mode (as);
7828 pointer_mode = targetm.addr_space.pointer_mode (as);
7831 /* We can get called with some Weird Things if the user does silliness
7832 like "(short) &a". In that case, convert_memory_address won't do
7833 the right thing, so ignore the given target mode. */
7834 if (tmode != address_mode && tmode != pointer_mode)
7835 tmode = address_mode;
7837 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7838 tmode, modifier, as);
7840 /* Despite expand_expr claims concerning ignoring TMODE when not
7841 strictly convenient, stuff breaks if we don't honor it. Note
7842 that combined with the above, we only do this for pointer modes. */
7843 rmode = GET_MODE (result);
7844 if (rmode == VOIDmode)
7845 rmode = tmode;
7846 if (rmode != tmode)
7847 result = convert_memory_address_addr_space (tmode, result, as);
7849 return result;
7852 /* Generate code for computing CONSTRUCTOR EXP.
7853 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7854 is TRUE, instead of creating a temporary variable in memory
7855 NULL is returned and the caller needs to handle it differently. */
7857 static rtx
7858 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7859 bool avoid_temp_mem)
7861 tree type = TREE_TYPE (exp);
7862 machine_mode mode = TYPE_MODE (type);
7864 /* Try to avoid creating a temporary at all. This is possible
7865 if all of the initializer is zero.
7866 FIXME: try to handle all [0..255] initializers we can handle
7867 with memset. */
7868 if (TREE_STATIC (exp)
7869 && !TREE_ADDRESSABLE (exp)
7870 && target != 0 && mode == BLKmode
7871 && all_zeros_p (exp))
7873 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7874 return target;
7877 /* All elts simple constants => refer to a constant in memory. But
7878 if this is a non-BLKmode mode, let it store a field at a time
7879 since that should make a CONST_INT, CONST_WIDE_INT or
7880 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7881 use, it is best to store directly into the target unless the type
7882 is large enough that memcpy will be used. If we are making an
7883 initializer and all operands are constant, put it in memory as
7884 well.
7886 FIXME: Avoid trying to fill vector constructors piece-meal.
7887 Output them with output_constant_def below unless we're sure
7888 they're zeros. This should go away when vector initializers
7889 are treated like VECTOR_CST instead of arrays. */
7890 if ((TREE_STATIC (exp)
7891 && ((mode == BLKmode
7892 && ! (target != 0 && safe_from_p (target, exp, 1)))
7893 || TREE_ADDRESSABLE (exp)
7894 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7895 && (! can_move_by_pieces
7896 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7897 TYPE_ALIGN (type)))
7898 && ! mostly_zeros_p (exp))))
7899 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7900 && TREE_CONSTANT (exp)))
7902 rtx constructor;
7904 if (avoid_temp_mem)
7905 return NULL_RTX;
7907 constructor = expand_expr_constant (exp, 1, modifier);
7909 if (modifier != EXPAND_CONST_ADDRESS
7910 && modifier != EXPAND_INITIALIZER
7911 && modifier != EXPAND_SUM)
7912 constructor = validize_mem (constructor);
7914 return constructor;
7917 /* Handle calls that pass values in multiple non-contiguous
7918 locations. The Irix 6 ABI has examples of this. */
7919 if (target == 0 || ! safe_from_p (target, exp, 1)
7920 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7922 if (avoid_temp_mem)
7923 return NULL_RTX;
7925 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7928 store_constructor (exp, target, 0, int_expr_size (exp));
7929 return target;
7933 /* expand_expr: generate code for computing expression EXP.
7934 An rtx for the computed value is returned. The value is never null.
7935 In the case of a void EXP, const0_rtx is returned.
7937 The value may be stored in TARGET if TARGET is nonzero.
7938 TARGET is just a suggestion; callers must assume that
7939 the rtx returned may not be the same as TARGET.
7941 If TARGET is CONST0_RTX, it means that the value will be ignored.
7943 If TMODE is not VOIDmode, it suggests generating the
7944 result in mode TMODE. But this is done only when convenient.
7945 Otherwise, TMODE is ignored and the value generated in its natural mode.
7946 TMODE is just a suggestion; callers must assume that
7947 the rtx returned may not have mode TMODE.
7949 Note that TARGET may have neither TMODE nor MODE. In that case, it
7950 probably will not be used.
7952 If MODIFIER is EXPAND_SUM then when EXP is an addition
7953 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7954 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7955 products as above, or REG or MEM, or constant.
7956 Ordinarily in such cases we would output mul or add instructions
7957 and then return a pseudo reg containing the sum.
7959 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7960 it also marks a label as absolutely required (it can't be dead).
7961 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7962 This is used for outputting expressions used in initializers.
7964 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7965 with a constant address even if that address is not normally legitimate.
7966 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7968 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7969 a call parameter. Such targets require special care as we haven't yet
7970 marked TARGET so that it's safe from being trashed by libcalls. We
7971 don't want to use TARGET for anything but the final result;
7972 Intermediate values must go elsewhere. Additionally, calls to
7973 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7975 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7976 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7977 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7978 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7979 recursively.
7981 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7982 In this case, we don't adjust a returned MEM rtx that wouldn't be
7983 sufficiently aligned for its mode; instead, it's up to the caller
7984 to deal with it afterwards. This is used to make sure that unaligned
7985 base objects for which out-of-bounds accesses are supported, for
7986 example record types with trailing arrays, aren't realigned behind
7987 the back of the caller.
7988 The normal operating mode is to pass FALSE for this parameter. */
7991 expand_expr_real (tree exp, rtx target, machine_mode tmode,
7992 enum expand_modifier modifier, rtx *alt_rtl,
7993 bool inner_reference_p)
7995 rtx ret;
7997 /* Handle ERROR_MARK before anybody tries to access its type. */
7998 if (TREE_CODE (exp) == ERROR_MARK
7999 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8001 ret = CONST0_RTX (tmode);
8002 return ret ? ret : const0_rtx;
8005 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8006 inner_reference_p);
8007 return ret;
8010 /* Try to expand the conditional expression which is represented by
8011 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
8012 return the rtl reg which repsents the result. Otherwise return
8013 NULL_RTL. */
8015 static rtx
8016 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8017 tree treeop1 ATTRIBUTE_UNUSED,
8018 tree treeop2 ATTRIBUTE_UNUSED)
8020 rtx insn;
8021 rtx op00, op01, op1, op2;
8022 enum rtx_code comparison_code;
8023 machine_mode comparison_mode;
8024 gimple srcstmt;
8025 rtx temp;
8026 tree type = TREE_TYPE (treeop1);
8027 int unsignedp = TYPE_UNSIGNED (type);
8028 machine_mode mode = TYPE_MODE (type);
8029 machine_mode orig_mode = mode;
8031 /* If we cannot do a conditional move on the mode, try doing it
8032 with the promoted mode. */
8033 if (!can_conditionally_move_p (mode))
8035 mode = promote_mode (type, mode, &unsignedp);
8036 if (!can_conditionally_move_p (mode))
8037 return NULL_RTX;
8038 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8040 else
8041 temp = assign_temp (type, 0, 1);
8043 start_sequence ();
8044 expand_operands (treeop1, treeop2,
8045 temp, &op1, &op2, EXPAND_NORMAL);
8047 if (TREE_CODE (treeop0) == SSA_NAME
8048 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8050 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8051 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8052 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8053 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8054 comparison_mode = TYPE_MODE (type);
8055 unsignedp = TYPE_UNSIGNED (type);
8056 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8058 else if (COMPARISON_CLASS_P (treeop0))
8060 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8061 enum tree_code cmpcode = TREE_CODE (treeop0);
8062 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8063 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8064 unsignedp = TYPE_UNSIGNED (type);
8065 comparison_mode = TYPE_MODE (type);
8066 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8068 else
8070 op00 = expand_normal (treeop0);
8071 op01 = const0_rtx;
8072 comparison_code = NE;
8073 comparison_mode = GET_MODE (op00);
8074 if (comparison_mode == VOIDmode)
8075 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8078 if (GET_MODE (op1) != mode)
8079 op1 = gen_lowpart (mode, op1);
8081 if (GET_MODE (op2) != mode)
8082 op2 = gen_lowpart (mode, op2);
8084 /* Try to emit the conditional move. */
8085 insn = emit_conditional_move (temp, comparison_code,
8086 op00, op01, comparison_mode,
8087 op1, op2, mode,
8088 unsignedp);
8090 /* If we could do the conditional move, emit the sequence,
8091 and return. */
8092 if (insn)
8094 rtx_insn *seq = get_insns ();
8095 end_sequence ();
8096 emit_insn (seq);
8097 return convert_modes (orig_mode, mode, temp, 0);
8100 /* Otherwise discard the sequence and fall back to code with
8101 branches. */
8102 end_sequence ();
8103 return NULL_RTX;
8107 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8108 enum expand_modifier modifier)
8110 rtx op0, op1, op2, temp;
8111 rtx_code_label *lab;
8112 tree type;
8113 int unsignedp;
8114 machine_mode mode;
8115 enum tree_code code = ops->code;
8116 optab this_optab;
8117 rtx subtarget, original_target;
8118 int ignore;
8119 bool reduce_bit_field;
8120 location_t loc = ops->location;
8121 tree treeop0, treeop1, treeop2;
8122 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8123 ? reduce_to_bit_field_precision ((expr), \
8124 target, \
8125 type) \
8126 : (expr))
8128 type = ops->type;
8129 mode = TYPE_MODE (type);
8130 unsignedp = TYPE_UNSIGNED (type);
8132 treeop0 = ops->op0;
8133 treeop1 = ops->op1;
8134 treeop2 = ops->op2;
8136 /* We should be called only on simple (binary or unary) expressions,
8137 exactly those that are valid in gimple expressions that aren't
8138 GIMPLE_SINGLE_RHS (or invalid). */
8139 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8140 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8141 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8143 ignore = (target == const0_rtx
8144 || ((CONVERT_EXPR_CODE_P (code)
8145 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8146 && TREE_CODE (type) == VOID_TYPE));
8148 /* We should be called only if we need the result. */
8149 gcc_assert (!ignore);
8151 /* An operation in what may be a bit-field type needs the
8152 result to be reduced to the precision of the bit-field type,
8153 which is narrower than that of the type's mode. */
8154 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8155 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8157 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8158 target = 0;
8160 /* Use subtarget as the target for operand 0 of a binary operation. */
8161 subtarget = get_subtarget (target);
8162 original_target = target;
8164 switch (code)
8166 case NON_LVALUE_EXPR:
8167 case PAREN_EXPR:
8168 CASE_CONVERT:
8169 if (treeop0 == error_mark_node)
8170 return const0_rtx;
8172 if (TREE_CODE (type) == UNION_TYPE)
8174 tree valtype = TREE_TYPE (treeop0);
8176 /* If both input and output are BLKmode, this conversion isn't doing
8177 anything except possibly changing memory attribute. */
8178 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8180 rtx result = expand_expr (treeop0, target, tmode,
8181 modifier);
8183 result = copy_rtx (result);
8184 set_mem_attributes (result, type, 0);
8185 return result;
8188 if (target == 0)
8190 if (TYPE_MODE (type) != BLKmode)
8191 target = gen_reg_rtx (TYPE_MODE (type));
8192 else
8193 target = assign_temp (type, 1, 1);
8196 if (MEM_P (target))
8197 /* Store data into beginning of memory target. */
8198 store_expr (treeop0,
8199 adjust_address (target, TYPE_MODE (valtype), 0),
8200 modifier == EXPAND_STACK_PARM,
8201 false);
8203 else
8205 gcc_assert (REG_P (target));
8207 /* Store this field into a union of the proper type. */
8208 store_field (target,
8209 MIN ((int_size_in_bytes (TREE_TYPE
8210 (treeop0))
8211 * BITS_PER_UNIT),
8212 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8213 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8216 /* Return the entire union. */
8217 return target;
8220 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8222 op0 = expand_expr (treeop0, target, VOIDmode,
8223 modifier);
8225 /* If the signedness of the conversion differs and OP0 is
8226 a promoted SUBREG, clear that indication since we now
8227 have to do the proper extension. */
8228 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8229 && GET_CODE (op0) == SUBREG)
8230 SUBREG_PROMOTED_VAR_P (op0) = 0;
8232 return REDUCE_BIT_FIELD (op0);
8235 op0 = expand_expr (treeop0, NULL_RTX, mode,
8236 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8237 if (GET_MODE (op0) == mode)
8240 /* If OP0 is a constant, just convert it into the proper mode. */
8241 else if (CONSTANT_P (op0))
8243 tree inner_type = TREE_TYPE (treeop0);
8244 machine_mode inner_mode = GET_MODE (op0);
8246 if (inner_mode == VOIDmode)
8247 inner_mode = TYPE_MODE (inner_type);
8249 if (modifier == EXPAND_INITIALIZER)
8250 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8251 subreg_lowpart_offset (mode,
8252 inner_mode));
8253 else
8254 op0= convert_modes (mode, inner_mode, op0,
8255 TYPE_UNSIGNED (inner_type));
8258 else if (modifier == EXPAND_INITIALIZER)
8259 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8261 else if (target == 0)
8262 op0 = convert_to_mode (mode, op0,
8263 TYPE_UNSIGNED (TREE_TYPE
8264 (treeop0)));
8265 else
8267 convert_move (target, op0,
8268 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8269 op0 = target;
8272 return REDUCE_BIT_FIELD (op0);
8274 case ADDR_SPACE_CONVERT_EXPR:
8276 tree treeop0_type = TREE_TYPE (treeop0);
8277 addr_space_t as_to;
8278 addr_space_t as_from;
8280 gcc_assert (POINTER_TYPE_P (type));
8281 gcc_assert (POINTER_TYPE_P (treeop0_type));
8283 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8284 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8286 /* Conversions between pointers to the same address space should
8287 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8288 gcc_assert (as_to != as_from);
8290 /* Ask target code to handle conversion between pointers
8291 to overlapping address spaces. */
8292 if (targetm.addr_space.subset_p (as_to, as_from)
8293 || targetm.addr_space.subset_p (as_from, as_to))
8295 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8296 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8297 gcc_assert (op0);
8298 return op0;
8301 /* For disjoint address spaces, converting anything but
8302 a null pointer invokes undefined behaviour. We simply
8303 always return a null pointer here. */
8304 return CONST0_RTX (mode);
8307 case POINTER_PLUS_EXPR:
8308 /* Even though the sizetype mode and the pointer's mode can be different
8309 expand is able to handle this correctly and get the correct result out
8310 of the PLUS_EXPR code. */
8311 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8312 if sizetype precision is smaller than pointer precision. */
8313 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8314 treeop1 = fold_convert_loc (loc, type,
8315 fold_convert_loc (loc, ssizetype,
8316 treeop1));
8317 /* If sizetype precision is larger than pointer precision, truncate the
8318 offset to have matching modes. */
8319 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8320 treeop1 = fold_convert_loc (loc, type, treeop1);
8322 case PLUS_EXPR:
8323 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8324 something else, make sure we add the register to the constant and
8325 then to the other thing. This case can occur during strength
8326 reduction and doing it this way will produce better code if the
8327 frame pointer or argument pointer is eliminated.
8329 fold-const.c will ensure that the constant is always in the inner
8330 PLUS_EXPR, so the only case we need to do anything about is if
8331 sp, ap, or fp is our second argument, in which case we must swap
8332 the innermost first argument and our second argument. */
8334 if (TREE_CODE (treeop0) == PLUS_EXPR
8335 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8336 && TREE_CODE (treeop1) == VAR_DECL
8337 && (DECL_RTL (treeop1) == frame_pointer_rtx
8338 || DECL_RTL (treeop1) == stack_pointer_rtx
8339 || DECL_RTL (treeop1) == arg_pointer_rtx))
8341 gcc_unreachable ();
8344 /* If the result is to be ptr_mode and we are adding an integer to
8345 something, we might be forming a constant. So try to use
8346 plus_constant. If it produces a sum and we can't accept it,
8347 use force_operand. This allows P = &ARR[const] to generate
8348 efficient code on machines where a SYMBOL_REF is not a valid
8349 address.
8351 If this is an EXPAND_SUM call, always return the sum. */
8352 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8353 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8355 if (modifier == EXPAND_STACK_PARM)
8356 target = 0;
8357 if (TREE_CODE (treeop0) == INTEGER_CST
8358 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8359 && TREE_CONSTANT (treeop1))
8361 rtx constant_part;
8362 HOST_WIDE_INT wc;
8363 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8365 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8366 EXPAND_SUM);
8367 /* Use wi::shwi to ensure that the constant is
8368 truncated according to the mode of OP1, then sign extended
8369 to a HOST_WIDE_INT. Using the constant directly can result
8370 in non-canonical RTL in a 64x32 cross compile. */
8371 wc = TREE_INT_CST_LOW (treeop0);
8372 constant_part =
8373 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8374 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8375 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8376 op1 = force_operand (op1, target);
8377 return REDUCE_BIT_FIELD (op1);
8380 else if (TREE_CODE (treeop1) == INTEGER_CST
8381 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8382 && TREE_CONSTANT (treeop0))
8384 rtx constant_part;
8385 HOST_WIDE_INT wc;
8386 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8388 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8389 (modifier == EXPAND_INITIALIZER
8390 ? EXPAND_INITIALIZER : EXPAND_SUM));
8391 if (! CONSTANT_P (op0))
8393 op1 = expand_expr (treeop1, NULL_RTX,
8394 VOIDmode, modifier);
8395 /* Return a PLUS if modifier says it's OK. */
8396 if (modifier == EXPAND_SUM
8397 || modifier == EXPAND_INITIALIZER)
8398 return simplify_gen_binary (PLUS, mode, op0, op1);
8399 goto binop2;
8401 /* Use wi::shwi to ensure that the constant is
8402 truncated according to the mode of OP1, then sign extended
8403 to a HOST_WIDE_INT. Using the constant directly can result
8404 in non-canonical RTL in a 64x32 cross compile. */
8405 wc = TREE_INT_CST_LOW (treeop1);
8406 constant_part
8407 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8408 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8409 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8410 op0 = force_operand (op0, target);
8411 return REDUCE_BIT_FIELD (op0);
8415 /* Use TER to expand pointer addition of a negated value
8416 as pointer subtraction. */
8417 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8418 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8419 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8420 && TREE_CODE (treeop1) == SSA_NAME
8421 && TYPE_MODE (TREE_TYPE (treeop0))
8422 == TYPE_MODE (TREE_TYPE (treeop1)))
8424 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8425 if (def)
8427 treeop1 = gimple_assign_rhs1 (def);
8428 code = MINUS_EXPR;
8429 goto do_minus;
8433 /* No sense saving up arithmetic to be done
8434 if it's all in the wrong mode to form part of an address.
8435 And force_operand won't know whether to sign-extend or
8436 zero-extend. */
8437 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8438 || mode != ptr_mode)
8440 expand_operands (treeop0, treeop1,
8441 subtarget, &op0, &op1, EXPAND_NORMAL);
8442 if (op0 == const0_rtx)
8443 return op1;
8444 if (op1 == const0_rtx)
8445 return op0;
8446 goto binop2;
8449 expand_operands (treeop0, treeop1,
8450 subtarget, &op0, &op1, modifier);
8451 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8453 case MINUS_EXPR:
8454 do_minus:
8455 /* For initializers, we are allowed to return a MINUS of two
8456 symbolic constants. Here we handle all cases when both operands
8457 are constant. */
8458 /* Handle difference of two symbolic constants,
8459 for the sake of an initializer. */
8460 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8461 && really_constant_p (treeop0)
8462 && really_constant_p (treeop1))
8464 expand_operands (treeop0, treeop1,
8465 NULL_RTX, &op0, &op1, modifier);
8467 /* If the last operand is a CONST_INT, use plus_constant of
8468 the negated constant. Else make the MINUS. */
8469 if (CONST_INT_P (op1))
8470 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8471 -INTVAL (op1)));
8472 else
8473 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8476 /* No sense saving up arithmetic to be done
8477 if it's all in the wrong mode to form part of an address.
8478 And force_operand won't know whether to sign-extend or
8479 zero-extend. */
8480 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8481 || mode != ptr_mode)
8482 goto binop;
8484 expand_operands (treeop0, treeop1,
8485 subtarget, &op0, &op1, modifier);
8487 /* Convert A - const to A + (-const). */
8488 if (CONST_INT_P (op1))
8490 op1 = negate_rtx (mode, op1);
8491 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8494 goto binop2;
8496 case WIDEN_MULT_PLUS_EXPR:
8497 case WIDEN_MULT_MINUS_EXPR:
8498 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8499 op2 = expand_normal (treeop2);
8500 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8501 target, unsignedp);
8502 return target;
8504 case WIDEN_MULT_EXPR:
8505 /* If first operand is constant, swap them.
8506 Thus the following special case checks need only
8507 check the second operand. */
8508 if (TREE_CODE (treeop0) == INTEGER_CST)
8510 tree t1 = treeop0;
8511 treeop0 = treeop1;
8512 treeop1 = t1;
8515 /* First, check if we have a multiplication of one signed and one
8516 unsigned operand. */
8517 if (TREE_CODE (treeop1) != INTEGER_CST
8518 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8519 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8521 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8522 this_optab = usmul_widen_optab;
8523 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8524 != CODE_FOR_nothing)
8526 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8527 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8528 EXPAND_NORMAL);
8529 else
8530 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8531 EXPAND_NORMAL);
8532 /* op0 and op1 might still be constant, despite the above
8533 != INTEGER_CST check. Handle it. */
8534 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8536 op0 = convert_modes (innermode, mode, op0, true);
8537 op1 = convert_modes (innermode, mode, op1, false);
8538 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8539 target, unsignedp));
8541 goto binop3;
8544 /* Check for a multiplication with matching signedness. */
8545 else if ((TREE_CODE (treeop1) == INTEGER_CST
8546 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8547 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8548 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8550 tree op0type = TREE_TYPE (treeop0);
8551 machine_mode innermode = TYPE_MODE (op0type);
8552 bool zextend_p = TYPE_UNSIGNED (op0type);
8553 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8554 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8556 if (TREE_CODE (treeop0) != INTEGER_CST)
8558 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8559 != CODE_FOR_nothing)
8561 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8562 EXPAND_NORMAL);
8563 /* op0 and op1 might still be constant, despite the above
8564 != INTEGER_CST check. Handle it. */
8565 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8567 widen_mult_const:
8568 op0 = convert_modes (innermode, mode, op0, zextend_p);
8570 = convert_modes (innermode, mode, op1,
8571 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8572 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8573 target,
8574 unsignedp));
8576 temp = expand_widening_mult (mode, op0, op1, target,
8577 unsignedp, this_optab);
8578 return REDUCE_BIT_FIELD (temp);
8580 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8581 != CODE_FOR_nothing
8582 && innermode == word_mode)
8584 rtx htem, hipart;
8585 op0 = expand_normal (treeop0);
8586 if (TREE_CODE (treeop1) == INTEGER_CST)
8587 op1 = convert_modes (innermode, mode,
8588 expand_normal (treeop1),
8589 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8590 else
8591 op1 = expand_normal (treeop1);
8592 /* op0 and op1 might still be constant, despite the above
8593 != INTEGER_CST check. Handle it. */
8594 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8595 goto widen_mult_const;
8596 temp = expand_binop (mode, other_optab, op0, op1, target,
8597 unsignedp, OPTAB_LIB_WIDEN);
8598 hipart = gen_highpart (innermode, temp);
8599 htem = expand_mult_highpart_adjust (innermode, hipart,
8600 op0, op1, hipart,
8601 zextend_p);
8602 if (htem != hipart)
8603 emit_move_insn (hipart, htem);
8604 return REDUCE_BIT_FIELD (temp);
8608 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8609 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8610 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8611 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8613 case FMA_EXPR:
8615 optab opt = fma_optab;
8616 gimple def0, def2;
8618 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8619 call. */
8620 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8622 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8623 tree call_expr;
8625 gcc_assert (fn != NULL_TREE);
8626 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8627 return expand_builtin (call_expr, target, subtarget, mode, false);
8630 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8631 /* The multiplication is commutative - look at its 2nd operand
8632 if the first isn't fed by a negate. */
8633 if (!def0)
8635 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8636 /* Swap operands if the 2nd operand is fed by a negate. */
8637 if (def0)
8639 tree tem = treeop0;
8640 treeop0 = treeop1;
8641 treeop1 = tem;
8644 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8646 op0 = op2 = NULL;
8648 if (def0 && def2
8649 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8651 opt = fnms_optab;
8652 op0 = expand_normal (gimple_assign_rhs1 (def0));
8653 op2 = expand_normal (gimple_assign_rhs1 (def2));
8655 else if (def0
8656 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8658 opt = fnma_optab;
8659 op0 = expand_normal (gimple_assign_rhs1 (def0));
8661 else if (def2
8662 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8664 opt = fms_optab;
8665 op2 = expand_normal (gimple_assign_rhs1 (def2));
8668 if (op0 == NULL)
8669 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8670 if (op2 == NULL)
8671 op2 = expand_normal (treeop2);
8672 op1 = expand_normal (treeop1);
8674 return expand_ternary_op (TYPE_MODE (type), opt,
8675 op0, op1, op2, target, 0);
8678 case MULT_EXPR:
8679 /* If this is a fixed-point operation, then we cannot use the code
8680 below because "expand_mult" doesn't support sat/no-sat fixed-point
8681 multiplications. */
8682 if (ALL_FIXED_POINT_MODE_P (mode))
8683 goto binop;
8685 /* If first operand is constant, swap them.
8686 Thus the following special case checks need only
8687 check the second operand. */
8688 if (TREE_CODE (treeop0) == INTEGER_CST)
8690 tree t1 = treeop0;
8691 treeop0 = treeop1;
8692 treeop1 = t1;
8695 /* Attempt to return something suitable for generating an
8696 indexed address, for machines that support that. */
8698 if (modifier == EXPAND_SUM && mode == ptr_mode
8699 && tree_fits_shwi_p (treeop1))
8701 tree exp1 = treeop1;
8703 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8704 EXPAND_SUM);
8706 if (!REG_P (op0))
8707 op0 = force_operand (op0, NULL_RTX);
8708 if (!REG_P (op0))
8709 op0 = copy_to_mode_reg (mode, op0);
8711 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8712 gen_int_mode (tree_to_shwi (exp1),
8713 TYPE_MODE (TREE_TYPE (exp1)))));
8716 if (modifier == EXPAND_STACK_PARM)
8717 target = 0;
8719 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8720 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8722 case TRUNC_DIV_EXPR:
8723 case FLOOR_DIV_EXPR:
8724 case CEIL_DIV_EXPR:
8725 case ROUND_DIV_EXPR:
8726 case EXACT_DIV_EXPR:
8727 /* If this is a fixed-point operation, then we cannot use the code
8728 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8729 divisions. */
8730 if (ALL_FIXED_POINT_MODE_P (mode))
8731 goto binop;
8733 if (modifier == EXPAND_STACK_PARM)
8734 target = 0;
8735 /* Possible optimization: compute the dividend with EXPAND_SUM
8736 then if the divisor is constant can optimize the case
8737 where some terms of the dividend have coeffs divisible by it. */
8738 expand_operands (treeop0, treeop1,
8739 subtarget, &op0, &op1, EXPAND_NORMAL);
8740 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8742 case RDIV_EXPR:
8743 goto binop;
8745 case MULT_HIGHPART_EXPR:
8746 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8747 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8748 gcc_assert (temp);
8749 return temp;
8751 case TRUNC_MOD_EXPR:
8752 case FLOOR_MOD_EXPR:
8753 case CEIL_MOD_EXPR:
8754 case ROUND_MOD_EXPR:
8755 if (modifier == EXPAND_STACK_PARM)
8756 target = 0;
8757 expand_operands (treeop0, treeop1,
8758 subtarget, &op0, &op1, EXPAND_NORMAL);
8759 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8761 case FIXED_CONVERT_EXPR:
8762 op0 = expand_normal (treeop0);
8763 if (target == 0 || modifier == EXPAND_STACK_PARM)
8764 target = gen_reg_rtx (mode);
8766 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8767 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8768 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8769 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8770 else
8771 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8772 return target;
8774 case FIX_TRUNC_EXPR:
8775 op0 = expand_normal (treeop0);
8776 if (target == 0 || modifier == EXPAND_STACK_PARM)
8777 target = gen_reg_rtx (mode);
8778 expand_fix (target, op0, unsignedp);
8779 return target;
8781 case FLOAT_EXPR:
8782 op0 = expand_normal (treeop0);
8783 if (target == 0 || modifier == EXPAND_STACK_PARM)
8784 target = gen_reg_rtx (mode);
8785 /* expand_float can't figure out what to do if FROM has VOIDmode.
8786 So give it the correct mode. With -O, cse will optimize this. */
8787 if (GET_MODE (op0) == VOIDmode)
8788 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8789 op0);
8790 expand_float (target, op0,
8791 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8792 return target;
8794 case NEGATE_EXPR:
8795 op0 = expand_expr (treeop0, subtarget,
8796 VOIDmode, EXPAND_NORMAL);
8797 if (modifier == EXPAND_STACK_PARM)
8798 target = 0;
8799 temp = expand_unop (mode,
8800 optab_for_tree_code (NEGATE_EXPR, type,
8801 optab_default),
8802 op0, target, 0);
8803 gcc_assert (temp);
8804 return REDUCE_BIT_FIELD (temp);
8806 case ABS_EXPR:
8807 op0 = expand_expr (treeop0, subtarget,
8808 VOIDmode, EXPAND_NORMAL);
8809 if (modifier == EXPAND_STACK_PARM)
8810 target = 0;
8812 /* ABS_EXPR is not valid for complex arguments. */
8813 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8814 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8816 /* Unsigned abs is simply the operand. Testing here means we don't
8817 risk generating incorrect code below. */
8818 if (TYPE_UNSIGNED (type))
8819 return op0;
8821 return expand_abs (mode, op0, target, unsignedp,
8822 safe_from_p (target, treeop0, 1));
8824 case MAX_EXPR:
8825 case MIN_EXPR:
8826 target = original_target;
8827 if (target == 0
8828 || modifier == EXPAND_STACK_PARM
8829 || (MEM_P (target) && MEM_VOLATILE_P (target))
8830 || GET_MODE (target) != mode
8831 || (REG_P (target)
8832 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8833 target = gen_reg_rtx (mode);
8834 expand_operands (treeop0, treeop1,
8835 target, &op0, &op1, EXPAND_NORMAL);
8837 /* First try to do it with a special MIN or MAX instruction.
8838 If that does not win, use a conditional jump to select the proper
8839 value. */
8840 this_optab = optab_for_tree_code (code, type, optab_default);
8841 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8842 OPTAB_WIDEN);
8843 if (temp != 0)
8844 return temp;
8846 /* At this point, a MEM target is no longer useful; we will get better
8847 code without it. */
8849 if (! REG_P (target))
8850 target = gen_reg_rtx (mode);
8852 /* If op1 was placed in target, swap op0 and op1. */
8853 if (target != op0 && target == op1)
8854 std::swap (op0, op1);
8856 /* We generate better code and avoid problems with op1 mentioning
8857 target by forcing op1 into a pseudo if it isn't a constant. */
8858 if (! CONSTANT_P (op1))
8859 op1 = force_reg (mode, op1);
8862 enum rtx_code comparison_code;
8863 rtx cmpop1 = op1;
8865 if (code == MAX_EXPR)
8866 comparison_code = unsignedp ? GEU : GE;
8867 else
8868 comparison_code = unsignedp ? LEU : LE;
8870 /* Canonicalize to comparisons against 0. */
8871 if (op1 == const1_rtx)
8873 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8874 or (a != 0 ? a : 1) for unsigned.
8875 For MIN we are safe converting (a <= 1 ? a : 1)
8876 into (a <= 0 ? a : 1) */
8877 cmpop1 = const0_rtx;
8878 if (code == MAX_EXPR)
8879 comparison_code = unsignedp ? NE : GT;
8881 if (op1 == constm1_rtx && !unsignedp)
8883 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8884 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8885 cmpop1 = const0_rtx;
8886 if (code == MIN_EXPR)
8887 comparison_code = LT;
8890 /* Use a conditional move if possible. */
8891 if (can_conditionally_move_p (mode))
8893 rtx insn;
8895 start_sequence ();
8897 /* Try to emit the conditional move. */
8898 insn = emit_conditional_move (target, comparison_code,
8899 op0, cmpop1, mode,
8900 op0, op1, mode,
8901 unsignedp);
8903 /* If we could do the conditional move, emit the sequence,
8904 and return. */
8905 if (insn)
8907 rtx_insn *seq = get_insns ();
8908 end_sequence ();
8909 emit_insn (seq);
8910 return target;
8913 /* Otherwise discard the sequence and fall back to code with
8914 branches. */
8915 end_sequence ();
8918 if (target != op0)
8919 emit_move_insn (target, op0);
8921 lab = gen_label_rtx ();
8922 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8923 unsignedp, mode, NULL_RTX, NULL, lab,
8924 -1);
8926 emit_move_insn (target, op1);
8927 emit_label (lab);
8928 return target;
8930 case BIT_NOT_EXPR:
8931 op0 = expand_expr (treeop0, subtarget,
8932 VOIDmode, EXPAND_NORMAL);
8933 if (modifier == EXPAND_STACK_PARM)
8934 target = 0;
8935 /* In case we have to reduce the result to bitfield precision
8936 for unsigned bitfield expand this as XOR with a proper constant
8937 instead. */
8938 if (reduce_bit_field && TYPE_UNSIGNED (type))
8940 wide_int mask = wi::mask (TYPE_PRECISION (type),
8941 false, GET_MODE_PRECISION (mode));
8943 temp = expand_binop (mode, xor_optab, op0,
8944 immed_wide_int_const (mask, mode),
8945 target, 1, OPTAB_LIB_WIDEN);
8947 else
8948 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8949 gcc_assert (temp);
8950 return temp;
8952 /* ??? Can optimize bitwise operations with one arg constant.
8953 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8954 and (a bitwise1 b) bitwise2 b (etc)
8955 but that is probably not worth while. */
8957 case BIT_AND_EXPR:
8958 case BIT_IOR_EXPR:
8959 case BIT_XOR_EXPR:
8960 goto binop;
8962 case LROTATE_EXPR:
8963 case RROTATE_EXPR:
8964 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8965 || (GET_MODE_PRECISION (TYPE_MODE (type))
8966 == TYPE_PRECISION (type)));
8967 /* fall through */
8969 case LSHIFT_EXPR:
8970 case RSHIFT_EXPR:
8971 /* If this is a fixed-point operation, then we cannot use the code
8972 below because "expand_shift" doesn't support sat/no-sat fixed-point
8973 shifts. */
8974 if (ALL_FIXED_POINT_MODE_P (mode))
8975 goto binop;
8977 if (! safe_from_p (subtarget, treeop1, 1))
8978 subtarget = 0;
8979 if (modifier == EXPAND_STACK_PARM)
8980 target = 0;
8981 op0 = expand_expr (treeop0, subtarget,
8982 VOIDmode, EXPAND_NORMAL);
8983 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8984 unsignedp);
8985 if (code == LSHIFT_EXPR)
8986 temp = REDUCE_BIT_FIELD (temp);
8987 return temp;
8989 /* Could determine the answer when only additive constants differ. Also,
8990 the addition of one can be handled by changing the condition. */
8991 case LT_EXPR:
8992 case LE_EXPR:
8993 case GT_EXPR:
8994 case GE_EXPR:
8995 case EQ_EXPR:
8996 case NE_EXPR:
8997 case UNORDERED_EXPR:
8998 case ORDERED_EXPR:
8999 case UNLT_EXPR:
9000 case UNLE_EXPR:
9001 case UNGT_EXPR:
9002 case UNGE_EXPR:
9003 case UNEQ_EXPR:
9004 case LTGT_EXPR:
9006 temp = do_store_flag (ops,
9007 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9008 tmode != VOIDmode ? tmode : mode);
9009 if (temp)
9010 return temp;
9012 /* Use a compare and a jump for BLKmode comparisons, or for function
9013 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
9015 if ((target == 0
9016 || modifier == EXPAND_STACK_PARM
9017 || ! safe_from_p (target, treeop0, 1)
9018 || ! safe_from_p (target, treeop1, 1)
9019 /* Make sure we don't have a hard reg (such as function's return
9020 value) live across basic blocks, if not optimizing. */
9021 || (!optimize && REG_P (target)
9022 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9023 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9025 emit_move_insn (target, const0_rtx);
9027 rtx_code_label *lab1 = gen_label_rtx ();
9028 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9030 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9031 emit_move_insn (target, constm1_rtx);
9032 else
9033 emit_move_insn (target, const1_rtx);
9035 emit_label (lab1);
9036 return target;
9038 case COMPLEX_EXPR:
9039 /* Get the rtx code of the operands. */
9040 op0 = expand_normal (treeop0);
9041 op1 = expand_normal (treeop1);
9043 if (!target)
9044 target = gen_reg_rtx (TYPE_MODE (type));
9045 else
9046 /* If target overlaps with op1, then either we need to force
9047 op1 into a pseudo (if target also overlaps with op0),
9048 or write the complex parts in reverse order. */
9049 switch (GET_CODE (target))
9051 case CONCAT:
9052 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9054 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9056 complex_expr_force_op1:
9057 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9058 emit_move_insn (temp, op1);
9059 op1 = temp;
9060 break;
9062 complex_expr_swap_order:
9063 /* Move the imaginary (op1) and real (op0) parts to their
9064 location. */
9065 write_complex_part (target, op1, true);
9066 write_complex_part (target, op0, false);
9068 return target;
9070 break;
9071 case MEM:
9072 temp = adjust_address_nv (target,
9073 GET_MODE_INNER (GET_MODE (target)), 0);
9074 if (reg_overlap_mentioned_p (temp, op1))
9076 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9077 temp = adjust_address_nv (target, imode,
9078 GET_MODE_SIZE (imode));
9079 if (reg_overlap_mentioned_p (temp, op0))
9080 goto complex_expr_force_op1;
9081 goto complex_expr_swap_order;
9083 break;
9084 default:
9085 if (reg_overlap_mentioned_p (target, op1))
9087 if (reg_overlap_mentioned_p (target, op0))
9088 goto complex_expr_force_op1;
9089 goto complex_expr_swap_order;
9091 break;
9094 /* Move the real (op0) and imaginary (op1) parts to their location. */
9095 write_complex_part (target, op0, false);
9096 write_complex_part (target, op1, true);
9098 return target;
9100 case WIDEN_SUM_EXPR:
9102 tree oprnd0 = treeop0;
9103 tree oprnd1 = treeop1;
9105 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9106 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9107 target, unsignedp);
9108 return target;
9111 case REDUC_MAX_EXPR:
9112 case REDUC_MIN_EXPR:
9113 case REDUC_PLUS_EXPR:
9115 op0 = expand_normal (treeop0);
9116 this_optab = optab_for_tree_code (code, type, optab_default);
9117 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9119 if (optab_handler (this_optab, vec_mode) != CODE_FOR_nothing)
9121 struct expand_operand ops[2];
9122 enum insn_code icode = optab_handler (this_optab, vec_mode);
9124 create_output_operand (&ops[0], target, mode);
9125 create_input_operand (&ops[1], op0, vec_mode);
9126 if (maybe_expand_insn (icode, 2, ops))
9128 target = ops[0].value;
9129 if (GET_MODE (target) != mode)
9130 return gen_lowpart (tmode, target);
9131 return target;
9134 /* Fall back to optab with vector result, and then extract scalar. */
9135 this_optab = scalar_reduc_to_vector (this_optab, type);
9136 temp = expand_unop (vec_mode, this_optab, op0, NULL_RTX, unsignedp);
9137 gcc_assert (temp);
9138 /* The tree code produces a scalar result, but (somewhat by convention)
9139 the optab produces a vector with the result in element 0 if
9140 little-endian, or element N-1 if big-endian. So pull the scalar
9141 result out of that element. */
9142 int index = BYTES_BIG_ENDIAN ? GET_MODE_NUNITS (vec_mode) - 1 : 0;
9143 int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (vec_mode));
9144 temp = extract_bit_field (temp, bitsize, bitsize * index, unsignedp,
9145 target, mode, mode);
9146 gcc_assert (temp);
9147 return temp;
9150 case VEC_UNPACK_HI_EXPR:
9151 case VEC_UNPACK_LO_EXPR:
9153 op0 = expand_normal (treeop0);
9154 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9155 target, unsignedp);
9156 gcc_assert (temp);
9157 return temp;
9160 case VEC_UNPACK_FLOAT_HI_EXPR:
9161 case VEC_UNPACK_FLOAT_LO_EXPR:
9163 op0 = expand_normal (treeop0);
9164 /* The signedness is determined from input operand. */
9165 temp = expand_widen_pattern_expr
9166 (ops, op0, NULL_RTX, NULL_RTX,
9167 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9169 gcc_assert (temp);
9170 return temp;
9173 case VEC_WIDEN_MULT_HI_EXPR:
9174 case VEC_WIDEN_MULT_LO_EXPR:
9175 case VEC_WIDEN_MULT_EVEN_EXPR:
9176 case VEC_WIDEN_MULT_ODD_EXPR:
9177 case VEC_WIDEN_LSHIFT_HI_EXPR:
9178 case VEC_WIDEN_LSHIFT_LO_EXPR:
9179 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9180 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9181 target, unsignedp);
9182 gcc_assert (target);
9183 return target;
9185 case VEC_PACK_TRUNC_EXPR:
9186 case VEC_PACK_SAT_EXPR:
9187 case VEC_PACK_FIX_TRUNC_EXPR:
9188 mode = TYPE_MODE (TREE_TYPE (treeop0));
9189 goto binop;
9191 case VEC_PERM_EXPR:
9192 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9193 op2 = expand_normal (treeop2);
9195 /* Careful here: if the target doesn't support integral vector modes,
9196 a constant selection vector could wind up smooshed into a normal
9197 integral constant. */
9198 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9200 tree sel_type = TREE_TYPE (treeop2);
9201 machine_mode vmode
9202 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9203 TYPE_VECTOR_SUBPARTS (sel_type));
9204 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9205 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9206 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9208 else
9209 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9211 temp = expand_vec_perm (mode, op0, op1, op2, target);
9212 gcc_assert (temp);
9213 return temp;
9215 case DOT_PROD_EXPR:
9217 tree oprnd0 = treeop0;
9218 tree oprnd1 = treeop1;
9219 tree oprnd2 = treeop2;
9220 rtx op2;
9222 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9223 op2 = expand_normal (oprnd2);
9224 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9225 target, unsignedp);
9226 return target;
9229 case SAD_EXPR:
9231 tree oprnd0 = treeop0;
9232 tree oprnd1 = treeop1;
9233 tree oprnd2 = treeop2;
9234 rtx op2;
9236 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9237 op2 = expand_normal (oprnd2);
9238 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9239 target, unsignedp);
9240 return target;
9243 case REALIGN_LOAD_EXPR:
9245 tree oprnd0 = treeop0;
9246 tree oprnd1 = treeop1;
9247 tree oprnd2 = treeop2;
9248 rtx op2;
9250 this_optab = optab_for_tree_code (code, type, optab_default);
9251 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9252 op2 = expand_normal (oprnd2);
9253 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9254 target, unsignedp);
9255 gcc_assert (temp);
9256 return temp;
9259 case COND_EXPR:
9261 /* A COND_EXPR with its type being VOID_TYPE represents a
9262 conditional jump and is handled in
9263 expand_gimple_cond_expr. */
9264 gcc_assert (!VOID_TYPE_P (type));
9266 /* Note that COND_EXPRs whose type is a structure or union
9267 are required to be constructed to contain assignments of
9268 a temporary variable, so that we can evaluate them here
9269 for side effect only. If type is void, we must do likewise. */
9271 gcc_assert (!TREE_ADDRESSABLE (type)
9272 && !ignore
9273 && TREE_TYPE (treeop1) != void_type_node
9274 && TREE_TYPE (treeop2) != void_type_node);
9276 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9277 if (temp)
9278 return temp;
9280 /* If we are not to produce a result, we have no target. Otherwise,
9281 if a target was specified use it; it will not be used as an
9282 intermediate target unless it is safe. If no target, use a
9283 temporary. */
9285 if (modifier != EXPAND_STACK_PARM
9286 && original_target
9287 && safe_from_p (original_target, treeop0, 1)
9288 && GET_MODE (original_target) == mode
9289 && !MEM_P (original_target))
9290 temp = original_target;
9291 else
9292 temp = assign_temp (type, 0, 1);
9294 do_pending_stack_adjust ();
9295 NO_DEFER_POP;
9296 rtx_code_label *lab0 = gen_label_rtx ();
9297 rtx_code_label *lab1 = gen_label_rtx ();
9298 jumpifnot (treeop0, lab0, -1);
9299 store_expr (treeop1, temp,
9300 modifier == EXPAND_STACK_PARM,
9301 false);
9303 emit_jump_insn (gen_jump (lab1));
9304 emit_barrier ();
9305 emit_label (lab0);
9306 store_expr (treeop2, temp,
9307 modifier == EXPAND_STACK_PARM,
9308 false);
9310 emit_label (lab1);
9311 OK_DEFER_POP;
9312 return temp;
9315 case VEC_COND_EXPR:
9316 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9317 return target;
9319 default:
9320 gcc_unreachable ();
9323 /* Here to do an ordinary binary operator. */
9324 binop:
9325 expand_operands (treeop0, treeop1,
9326 subtarget, &op0, &op1, EXPAND_NORMAL);
9327 binop2:
9328 this_optab = optab_for_tree_code (code, type, optab_default);
9329 binop3:
9330 if (modifier == EXPAND_STACK_PARM)
9331 target = 0;
9332 temp = expand_binop (mode, this_optab, op0, op1, target,
9333 unsignedp, OPTAB_LIB_WIDEN);
9334 gcc_assert (temp);
9335 /* Bitwise operations do not need bitfield reduction as we expect their
9336 operands being properly truncated. */
9337 if (code == BIT_XOR_EXPR
9338 || code == BIT_AND_EXPR
9339 || code == BIT_IOR_EXPR)
9340 return temp;
9341 return REDUCE_BIT_FIELD (temp);
9343 #undef REDUCE_BIT_FIELD
9346 /* Return TRUE if expression STMT is suitable for replacement.
9347 Never consider memory loads as replaceable, because those don't ever lead
9348 into constant expressions. */
9350 static bool
9351 stmt_is_replaceable_p (gimple stmt)
9353 if (ssa_is_replaceable_p (stmt))
9355 /* Don't move around loads. */
9356 if (!gimple_assign_single_p (stmt)
9357 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9358 return true;
9360 return false;
9364 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9365 enum expand_modifier modifier, rtx *alt_rtl,
9366 bool inner_reference_p)
9368 rtx op0, op1, temp, decl_rtl;
9369 tree type;
9370 int unsignedp;
9371 machine_mode mode;
9372 enum tree_code code = TREE_CODE (exp);
9373 rtx subtarget, original_target;
9374 int ignore;
9375 tree context;
9376 bool reduce_bit_field;
9377 location_t loc = EXPR_LOCATION (exp);
9378 struct separate_ops ops;
9379 tree treeop0, treeop1, treeop2;
9380 tree ssa_name = NULL_TREE;
9381 gimple g;
9383 type = TREE_TYPE (exp);
9384 mode = TYPE_MODE (type);
9385 unsignedp = TYPE_UNSIGNED (type);
9387 treeop0 = treeop1 = treeop2 = NULL_TREE;
9388 if (!VL_EXP_CLASS_P (exp))
9389 switch (TREE_CODE_LENGTH (code))
9391 default:
9392 case 3: treeop2 = TREE_OPERAND (exp, 2);
9393 case 2: treeop1 = TREE_OPERAND (exp, 1);
9394 case 1: treeop0 = TREE_OPERAND (exp, 0);
9395 case 0: break;
9397 ops.code = code;
9398 ops.type = type;
9399 ops.op0 = treeop0;
9400 ops.op1 = treeop1;
9401 ops.op2 = treeop2;
9402 ops.location = loc;
9404 ignore = (target == const0_rtx
9405 || ((CONVERT_EXPR_CODE_P (code)
9406 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9407 && TREE_CODE (type) == VOID_TYPE));
9409 /* An operation in what may be a bit-field type needs the
9410 result to be reduced to the precision of the bit-field type,
9411 which is narrower than that of the type's mode. */
9412 reduce_bit_field = (!ignore
9413 && INTEGRAL_TYPE_P (type)
9414 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9416 /* If we are going to ignore this result, we need only do something
9417 if there is a side-effect somewhere in the expression. If there
9418 is, short-circuit the most common cases here. Note that we must
9419 not call expand_expr with anything but const0_rtx in case this
9420 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9422 if (ignore)
9424 if (! TREE_SIDE_EFFECTS (exp))
9425 return const0_rtx;
9427 /* Ensure we reference a volatile object even if value is ignored, but
9428 don't do this if all we are doing is taking its address. */
9429 if (TREE_THIS_VOLATILE (exp)
9430 && TREE_CODE (exp) != FUNCTION_DECL
9431 && mode != VOIDmode && mode != BLKmode
9432 && modifier != EXPAND_CONST_ADDRESS)
9434 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9435 if (MEM_P (temp))
9436 copy_to_reg (temp);
9437 return const0_rtx;
9440 if (TREE_CODE_CLASS (code) == tcc_unary
9441 || code == BIT_FIELD_REF
9442 || code == COMPONENT_REF
9443 || code == INDIRECT_REF)
9444 return expand_expr (treeop0, const0_rtx, VOIDmode,
9445 modifier);
9447 else if (TREE_CODE_CLASS (code) == tcc_binary
9448 || TREE_CODE_CLASS (code) == tcc_comparison
9449 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9451 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9452 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9453 return const0_rtx;
9456 target = 0;
9459 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9460 target = 0;
9462 /* Use subtarget as the target for operand 0 of a binary operation. */
9463 subtarget = get_subtarget (target);
9464 original_target = target;
9466 switch (code)
9468 case LABEL_DECL:
9470 tree function = decl_function_context (exp);
9472 temp = label_rtx (exp);
9473 temp = gen_rtx_LABEL_REF (Pmode, temp);
9475 if (function != current_function_decl
9476 && function != 0)
9477 LABEL_REF_NONLOCAL_P (temp) = 1;
9479 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9480 return temp;
9483 case SSA_NAME:
9484 /* ??? ivopts calls expander, without any preparation from
9485 out-of-ssa. So fake instructions as if this was an access to the
9486 base variable. This unnecessarily allocates a pseudo, see how we can
9487 reuse it, if partition base vars have it set already. */
9488 if (!currently_expanding_to_rtl)
9490 tree var = SSA_NAME_VAR (exp);
9491 if (var && DECL_RTL_SET_P (var))
9492 return DECL_RTL (var);
9493 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9494 LAST_VIRTUAL_REGISTER + 1);
9497 g = get_gimple_for_ssa_name (exp);
9498 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9499 if (g == NULL
9500 && modifier == EXPAND_INITIALIZER
9501 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9502 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9503 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9504 g = SSA_NAME_DEF_STMT (exp);
9505 if (g)
9507 rtx r;
9508 ops.code = gimple_assign_rhs_code (g);
9509 switch (get_gimple_rhs_class (ops.code))
9511 case GIMPLE_TERNARY_RHS:
9512 ops.op2 = gimple_assign_rhs3 (g);
9513 /* Fallthru */
9514 case GIMPLE_BINARY_RHS:
9515 ops.op1 = gimple_assign_rhs2 (g);
9517 /* Try to expand conditonal compare. */
9518 if (targetm.gen_ccmp_first)
9520 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9521 r = expand_ccmp_expr (g);
9522 if (r)
9523 break;
9525 /* Fallthru */
9526 case GIMPLE_UNARY_RHS:
9527 ops.op0 = gimple_assign_rhs1 (g);
9528 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9529 ops.location = gimple_location (g);
9530 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9531 break;
9532 case GIMPLE_SINGLE_RHS:
9534 location_t saved_loc = curr_insn_location ();
9535 set_curr_insn_location (gimple_location (g));
9536 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9537 tmode, modifier, NULL, inner_reference_p);
9538 set_curr_insn_location (saved_loc);
9539 break;
9541 default:
9542 gcc_unreachable ();
9544 if (REG_P (r) && !REG_EXPR (r))
9545 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9546 return r;
9549 ssa_name = exp;
9550 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9551 exp = SSA_NAME_VAR (ssa_name);
9552 goto expand_decl_rtl;
9554 case PARM_DECL:
9555 case VAR_DECL:
9556 /* If a static var's type was incomplete when the decl was written,
9557 but the type is complete now, lay out the decl now. */
9558 if (DECL_SIZE (exp) == 0
9559 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9560 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9561 layout_decl (exp, 0);
9563 /* ... fall through ... */
9565 case FUNCTION_DECL:
9566 case RESULT_DECL:
9567 decl_rtl = DECL_RTL (exp);
9568 expand_decl_rtl:
9569 gcc_assert (decl_rtl);
9570 decl_rtl = copy_rtx (decl_rtl);
9571 /* Record writes to register variables. */
9572 if (modifier == EXPAND_WRITE
9573 && REG_P (decl_rtl)
9574 && HARD_REGISTER_P (decl_rtl))
9575 add_to_hard_reg_set (&crtl->asm_clobbers,
9576 GET_MODE (decl_rtl), REGNO (decl_rtl));
9578 /* Ensure variable marked as used even if it doesn't go through
9579 a parser. If it hasn't be used yet, write out an external
9580 definition. */
9581 TREE_USED (exp) = 1;
9583 /* Show we haven't gotten RTL for this yet. */
9584 temp = 0;
9586 /* Variables inherited from containing functions should have
9587 been lowered by this point. */
9588 context = decl_function_context (exp);
9589 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9590 || context == current_function_decl
9591 || TREE_STATIC (exp)
9592 || DECL_EXTERNAL (exp)
9593 /* ??? C++ creates functions that are not TREE_STATIC. */
9594 || TREE_CODE (exp) == FUNCTION_DECL);
9596 /* This is the case of an array whose size is to be determined
9597 from its initializer, while the initializer is still being parsed.
9598 ??? We aren't parsing while expanding anymore. */
9600 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9601 temp = validize_mem (decl_rtl);
9603 /* If DECL_RTL is memory, we are in the normal case and the
9604 address is not valid, get the address into a register. */
9606 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9608 if (alt_rtl)
9609 *alt_rtl = decl_rtl;
9610 decl_rtl = use_anchored_address (decl_rtl);
9611 if (modifier != EXPAND_CONST_ADDRESS
9612 && modifier != EXPAND_SUM
9613 && !memory_address_addr_space_p (DECL_MODE (exp),
9614 XEXP (decl_rtl, 0),
9615 MEM_ADDR_SPACE (decl_rtl)))
9616 temp = replace_equiv_address (decl_rtl,
9617 copy_rtx (XEXP (decl_rtl, 0)));
9620 /* If we got something, return it. But first, set the alignment
9621 if the address is a register. */
9622 if (temp != 0)
9624 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9625 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9627 return temp;
9630 /* If the mode of DECL_RTL does not match that of the decl,
9631 there are two cases: we are dealing with a BLKmode value
9632 that is returned in a register, or we are dealing with
9633 a promoted value. In the latter case, return a SUBREG
9634 of the wanted mode, but mark it so that we know that it
9635 was already extended. */
9636 if (REG_P (decl_rtl)
9637 && DECL_MODE (exp) != BLKmode
9638 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9640 machine_mode pmode;
9642 /* Get the signedness to be used for this variable. Ensure we get
9643 the same mode we got when the variable was declared. */
9644 if (code == SSA_NAME
9645 && (g = SSA_NAME_DEF_STMT (ssa_name))
9646 && gimple_code (g) == GIMPLE_CALL
9647 && !gimple_call_internal_p (g))
9648 pmode = promote_function_mode (type, mode, &unsignedp,
9649 gimple_call_fntype (g),
9651 else
9652 pmode = promote_decl_mode (exp, &unsignedp);
9653 gcc_assert (GET_MODE (decl_rtl) == pmode);
9655 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9656 SUBREG_PROMOTED_VAR_P (temp) = 1;
9657 SUBREG_PROMOTED_SET (temp, unsignedp);
9658 return temp;
9661 return decl_rtl;
9663 case INTEGER_CST:
9664 /* Given that TYPE_PRECISION (type) is not always equal to
9665 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9666 the former to the latter according to the signedness of the
9667 type. */
9668 temp = immed_wide_int_const (wide_int::from
9669 (exp,
9670 GET_MODE_PRECISION (TYPE_MODE (type)),
9671 TYPE_SIGN (type)),
9672 TYPE_MODE (type));
9673 return temp;
9675 case VECTOR_CST:
9677 tree tmp = NULL_TREE;
9678 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9679 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9680 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9681 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9682 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9683 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9684 return const_vector_from_tree (exp);
9685 if (GET_MODE_CLASS (mode) == MODE_INT)
9687 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9688 if (type_for_mode)
9689 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9691 if (!tmp)
9693 vec<constructor_elt, va_gc> *v;
9694 unsigned i;
9695 vec_alloc (v, VECTOR_CST_NELTS (exp));
9696 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9697 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9698 tmp = build_constructor (type, v);
9700 return expand_expr (tmp, ignore ? const0_rtx : target,
9701 tmode, modifier);
9704 case CONST_DECL:
9705 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9707 case REAL_CST:
9708 /* If optimized, generate immediate CONST_DOUBLE
9709 which will be turned into memory by reload if necessary.
9711 We used to force a register so that loop.c could see it. But
9712 this does not allow gen_* patterns to perform optimizations with
9713 the constants. It also produces two insns in cases like "x = 1.0;".
9714 On most machines, floating-point constants are not permitted in
9715 many insns, so we'd end up copying it to a register in any case.
9717 Now, we do the copying in expand_binop, if appropriate. */
9718 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9719 TYPE_MODE (TREE_TYPE (exp)));
9721 case FIXED_CST:
9722 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9723 TYPE_MODE (TREE_TYPE (exp)));
9725 case COMPLEX_CST:
9726 /* Handle evaluating a complex constant in a CONCAT target. */
9727 if (original_target && GET_CODE (original_target) == CONCAT)
9729 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9730 rtx rtarg, itarg;
9732 rtarg = XEXP (original_target, 0);
9733 itarg = XEXP (original_target, 1);
9735 /* Move the real and imaginary parts separately. */
9736 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9737 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9739 if (op0 != rtarg)
9740 emit_move_insn (rtarg, op0);
9741 if (op1 != itarg)
9742 emit_move_insn (itarg, op1);
9744 return original_target;
9747 /* ... fall through ... */
9749 case STRING_CST:
9750 temp = expand_expr_constant (exp, 1, modifier);
9752 /* temp contains a constant address.
9753 On RISC machines where a constant address isn't valid,
9754 make some insns to get that address into a register. */
9755 if (modifier != EXPAND_CONST_ADDRESS
9756 && modifier != EXPAND_INITIALIZER
9757 && modifier != EXPAND_SUM
9758 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9759 MEM_ADDR_SPACE (temp)))
9760 return replace_equiv_address (temp,
9761 copy_rtx (XEXP (temp, 0)));
9762 return temp;
9764 case SAVE_EXPR:
9766 tree val = treeop0;
9767 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9768 inner_reference_p);
9770 if (!SAVE_EXPR_RESOLVED_P (exp))
9772 /* We can indeed still hit this case, typically via builtin
9773 expanders calling save_expr immediately before expanding
9774 something. Assume this means that we only have to deal
9775 with non-BLKmode values. */
9776 gcc_assert (GET_MODE (ret) != BLKmode);
9778 val = build_decl (curr_insn_location (),
9779 VAR_DECL, NULL, TREE_TYPE (exp));
9780 DECL_ARTIFICIAL (val) = 1;
9781 DECL_IGNORED_P (val) = 1;
9782 treeop0 = val;
9783 TREE_OPERAND (exp, 0) = treeop0;
9784 SAVE_EXPR_RESOLVED_P (exp) = 1;
9786 if (!CONSTANT_P (ret))
9787 ret = copy_to_reg (ret);
9788 SET_DECL_RTL (val, ret);
9791 return ret;
9795 case CONSTRUCTOR:
9796 /* If we don't need the result, just ensure we evaluate any
9797 subexpressions. */
9798 if (ignore)
9800 unsigned HOST_WIDE_INT idx;
9801 tree value;
9803 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9804 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9806 return const0_rtx;
9809 return expand_constructor (exp, target, modifier, false);
9811 case TARGET_MEM_REF:
9813 addr_space_t as
9814 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9815 enum insn_code icode;
9816 unsigned int align;
9818 op0 = addr_for_mem_ref (exp, as, true);
9819 op0 = memory_address_addr_space (mode, op0, as);
9820 temp = gen_rtx_MEM (mode, op0);
9821 set_mem_attributes (temp, exp, 0);
9822 set_mem_addr_space (temp, as);
9823 align = get_object_alignment (exp);
9824 if (modifier != EXPAND_WRITE
9825 && modifier != EXPAND_MEMORY
9826 && mode != BLKmode
9827 && align < GET_MODE_ALIGNMENT (mode)
9828 /* If the target does not have special handling for unaligned
9829 loads of mode then it can use regular moves for them. */
9830 && ((icode = optab_handler (movmisalign_optab, mode))
9831 != CODE_FOR_nothing))
9833 struct expand_operand ops[2];
9835 /* We've already validated the memory, and we're creating a
9836 new pseudo destination. The predicates really can't fail,
9837 nor can the generator. */
9838 create_output_operand (&ops[0], NULL_RTX, mode);
9839 create_fixed_operand (&ops[1], temp);
9840 expand_insn (icode, 2, ops);
9841 temp = ops[0].value;
9843 return temp;
9846 case MEM_REF:
9848 addr_space_t as
9849 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9850 machine_mode address_mode;
9851 tree base = TREE_OPERAND (exp, 0);
9852 gimple def_stmt;
9853 enum insn_code icode;
9854 unsigned align;
9855 /* Handle expansion of non-aliased memory with non-BLKmode. That
9856 might end up in a register. */
9857 if (mem_ref_refers_to_non_mem_p (exp))
9859 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9860 base = TREE_OPERAND (base, 0);
9861 if (offset == 0
9862 && tree_fits_uhwi_p (TYPE_SIZE (type))
9863 && (GET_MODE_BITSIZE (DECL_MODE (base))
9864 == tree_to_uhwi (TYPE_SIZE (type))))
9865 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9866 target, tmode, modifier);
9867 if (TYPE_MODE (type) == BLKmode)
9869 temp = assign_stack_temp (DECL_MODE (base),
9870 GET_MODE_SIZE (DECL_MODE (base)));
9871 store_expr (base, temp, 0, false);
9872 temp = adjust_address (temp, BLKmode, offset);
9873 set_mem_size (temp, int_size_in_bytes (type));
9874 return temp;
9876 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9877 bitsize_int (offset * BITS_PER_UNIT));
9878 return expand_expr (exp, target, tmode, modifier);
9880 address_mode = targetm.addr_space.address_mode (as);
9881 base = TREE_OPERAND (exp, 0);
9882 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9884 tree mask = gimple_assign_rhs2 (def_stmt);
9885 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9886 gimple_assign_rhs1 (def_stmt), mask);
9887 TREE_OPERAND (exp, 0) = base;
9889 align = get_object_alignment (exp);
9890 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9891 op0 = memory_address_addr_space (mode, op0, as);
9892 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9894 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9895 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9896 op0 = memory_address_addr_space (mode, op0, as);
9898 temp = gen_rtx_MEM (mode, op0);
9899 set_mem_attributes (temp, exp, 0);
9900 set_mem_addr_space (temp, as);
9901 if (TREE_THIS_VOLATILE (exp))
9902 MEM_VOLATILE_P (temp) = 1;
9903 if (modifier != EXPAND_WRITE
9904 && modifier != EXPAND_MEMORY
9905 && !inner_reference_p
9906 && mode != BLKmode
9907 && align < GET_MODE_ALIGNMENT (mode))
9909 if ((icode = optab_handler (movmisalign_optab, mode))
9910 != CODE_FOR_nothing)
9912 struct expand_operand ops[2];
9914 /* We've already validated the memory, and we're creating a
9915 new pseudo destination. The predicates really can't fail,
9916 nor can the generator. */
9917 create_output_operand (&ops[0], NULL_RTX, mode);
9918 create_fixed_operand (&ops[1], temp);
9919 expand_insn (icode, 2, ops);
9920 temp = ops[0].value;
9922 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9923 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9924 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9925 (modifier == EXPAND_STACK_PARM
9926 ? NULL_RTX : target),
9927 mode, mode);
9929 return temp;
9932 case ARRAY_REF:
9935 tree array = treeop0;
9936 tree index = treeop1;
9937 tree init;
9939 /* Fold an expression like: "foo"[2].
9940 This is not done in fold so it won't happen inside &.
9941 Don't fold if this is for wide characters since it's too
9942 difficult to do correctly and this is a very rare case. */
9944 if (modifier != EXPAND_CONST_ADDRESS
9945 && modifier != EXPAND_INITIALIZER
9946 && modifier != EXPAND_MEMORY)
9948 tree t = fold_read_from_constant_string (exp);
9950 if (t)
9951 return expand_expr (t, target, tmode, modifier);
9954 /* If this is a constant index into a constant array,
9955 just get the value from the array. Handle both the cases when
9956 we have an explicit constructor and when our operand is a variable
9957 that was declared const. */
9959 if (modifier != EXPAND_CONST_ADDRESS
9960 && modifier != EXPAND_INITIALIZER
9961 && modifier != EXPAND_MEMORY
9962 && TREE_CODE (array) == CONSTRUCTOR
9963 && ! TREE_SIDE_EFFECTS (array)
9964 && TREE_CODE (index) == INTEGER_CST)
9966 unsigned HOST_WIDE_INT ix;
9967 tree field, value;
9969 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9970 field, value)
9971 if (tree_int_cst_equal (field, index))
9973 if (!TREE_SIDE_EFFECTS (value))
9974 return expand_expr (fold (value), target, tmode, modifier);
9975 break;
9979 else if (optimize >= 1
9980 && modifier != EXPAND_CONST_ADDRESS
9981 && modifier != EXPAND_INITIALIZER
9982 && modifier != EXPAND_MEMORY
9983 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9984 && TREE_CODE (index) == INTEGER_CST
9985 && (TREE_CODE (array) == VAR_DECL
9986 || TREE_CODE (array) == CONST_DECL)
9987 && (init = ctor_for_folding (array)) != error_mark_node)
9989 if (init == NULL_TREE)
9991 tree value = build_zero_cst (type);
9992 if (TREE_CODE (value) == CONSTRUCTOR)
9994 /* If VALUE is a CONSTRUCTOR, this optimization is only
9995 useful if this doesn't store the CONSTRUCTOR into
9996 memory. If it does, it is more efficient to just
9997 load the data from the array directly. */
9998 rtx ret = expand_constructor (value, target,
9999 modifier, true);
10000 if (ret == NULL_RTX)
10001 value = NULL_TREE;
10004 if (value)
10005 return expand_expr (value, target, tmode, modifier);
10007 else if (TREE_CODE (init) == CONSTRUCTOR)
10009 unsigned HOST_WIDE_INT ix;
10010 tree field, value;
10012 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10013 field, value)
10014 if (tree_int_cst_equal (field, index))
10016 if (TREE_SIDE_EFFECTS (value))
10017 break;
10019 if (TREE_CODE (value) == CONSTRUCTOR)
10021 /* If VALUE is a CONSTRUCTOR, this
10022 optimization is only useful if
10023 this doesn't store the CONSTRUCTOR
10024 into memory. If it does, it is more
10025 efficient to just load the data from
10026 the array directly. */
10027 rtx ret = expand_constructor (value, target,
10028 modifier, true);
10029 if (ret == NULL_RTX)
10030 break;
10033 return
10034 expand_expr (fold (value), target, tmode, modifier);
10037 else if (TREE_CODE (init) == STRING_CST)
10039 tree low_bound = array_ref_low_bound (exp);
10040 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10042 /* Optimize the special case of a zero lower bound.
10044 We convert the lower bound to sizetype to avoid problems
10045 with constant folding. E.g. suppose the lower bound is
10046 1 and its mode is QI. Without the conversion
10047 (ARRAY + (INDEX - (unsigned char)1))
10048 becomes
10049 (ARRAY + (-(unsigned char)1) + INDEX)
10050 which becomes
10051 (ARRAY + 255 + INDEX). Oops! */
10052 if (!integer_zerop (low_bound))
10053 index1 = size_diffop_loc (loc, index1,
10054 fold_convert_loc (loc, sizetype,
10055 low_bound));
10057 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10059 tree type = TREE_TYPE (TREE_TYPE (init));
10060 machine_mode mode = TYPE_MODE (type);
10062 if (GET_MODE_CLASS (mode) == MODE_INT
10063 && GET_MODE_SIZE (mode) == 1)
10064 return gen_int_mode (TREE_STRING_POINTER (init)
10065 [TREE_INT_CST_LOW (index1)],
10066 mode);
10071 goto normal_inner_ref;
10073 case COMPONENT_REF:
10074 /* If the operand is a CONSTRUCTOR, we can just extract the
10075 appropriate field if it is present. */
10076 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10078 unsigned HOST_WIDE_INT idx;
10079 tree field, value;
10081 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10082 idx, field, value)
10083 if (field == treeop1
10084 /* We can normally use the value of the field in the
10085 CONSTRUCTOR. However, if this is a bitfield in
10086 an integral mode that we can fit in a HOST_WIDE_INT,
10087 we must mask only the number of bits in the bitfield,
10088 since this is done implicitly by the constructor. If
10089 the bitfield does not meet either of those conditions,
10090 we can't do this optimization. */
10091 && (! DECL_BIT_FIELD (field)
10092 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10093 && (GET_MODE_PRECISION (DECL_MODE (field))
10094 <= HOST_BITS_PER_WIDE_INT))))
10096 if (DECL_BIT_FIELD (field)
10097 && modifier == EXPAND_STACK_PARM)
10098 target = 0;
10099 op0 = expand_expr (value, target, tmode, modifier);
10100 if (DECL_BIT_FIELD (field))
10102 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10103 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10105 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10107 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10108 imode);
10109 op0 = expand_and (imode, op0, op1, target);
10111 else
10113 int count = GET_MODE_PRECISION (imode) - bitsize;
10115 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10116 target, 0);
10117 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10118 target, 0);
10122 return op0;
10125 goto normal_inner_ref;
10127 case BIT_FIELD_REF:
10128 case ARRAY_RANGE_REF:
10129 normal_inner_ref:
10131 machine_mode mode1, mode2;
10132 HOST_WIDE_INT bitsize, bitpos;
10133 tree offset;
10134 int volatilep = 0, must_force_mem;
10135 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10136 &mode1, &unsignedp, &volatilep, true);
10137 rtx orig_op0, memloc;
10138 bool clear_mem_expr = false;
10140 /* If we got back the original object, something is wrong. Perhaps
10141 we are evaluating an expression too early. In any event, don't
10142 infinitely recurse. */
10143 gcc_assert (tem != exp);
10145 /* If TEM's type is a union of variable size, pass TARGET to the inner
10146 computation, since it will need a temporary and TARGET is known
10147 to have to do. This occurs in unchecked conversion in Ada. */
10148 orig_op0 = op0
10149 = expand_expr_real (tem,
10150 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10151 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10152 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10153 != INTEGER_CST)
10154 && modifier != EXPAND_STACK_PARM
10155 ? target : NULL_RTX),
10156 VOIDmode,
10157 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10158 NULL, true);
10160 /* If the field has a mode, we want to access it in the
10161 field's mode, not the computed mode.
10162 If a MEM has VOIDmode (external with incomplete type),
10163 use BLKmode for it instead. */
10164 if (MEM_P (op0))
10166 if (mode1 != VOIDmode)
10167 op0 = adjust_address (op0, mode1, 0);
10168 else if (GET_MODE (op0) == VOIDmode)
10169 op0 = adjust_address (op0, BLKmode, 0);
10172 mode2
10173 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10175 /* If we have either an offset, a BLKmode result, or a reference
10176 outside the underlying object, we must force it to memory.
10177 Such a case can occur in Ada if we have unchecked conversion
10178 of an expression from a scalar type to an aggregate type or
10179 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10180 passed a partially uninitialized object or a view-conversion
10181 to a larger size. */
10182 must_force_mem = (offset
10183 || mode1 == BLKmode
10184 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10186 /* Handle CONCAT first. */
10187 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10189 if (bitpos == 0
10190 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10191 return op0;
10192 if (bitpos == 0
10193 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10194 && bitsize)
10196 op0 = XEXP (op0, 0);
10197 mode2 = GET_MODE (op0);
10199 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10200 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10201 && bitpos
10202 && bitsize)
10204 op0 = XEXP (op0, 1);
10205 bitpos = 0;
10206 mode2 = GET_MODE (op0);
10208 else
10209 /* Otherwise force into memory. */
10210 must_force_mem = 1;
10213 /* If this is a constant, put it in a register if it is a legitimate
10214 constant and we don't need a memory reference. */
10215 if (CONSTANT_P (op0)
10216 && mode2 != BLKmode
10217 && targetm.legitimate_constant_p (mode2, op0)
10218 && !must_force_mem)
10219 op0 = force_reg (mode2, op0);
10221 /* Otherwise, if this is a constant, try to force it to the constant
10222 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10223 is a legitimate constant. */
10224 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10225 op0 = validize_mem (memloc);
10227 /* Otherwise, if this is a constant or the object is not in memory
10228 and need be, put it there. */
10229 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10231 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10232 emit_move_insn (memloc, op0);
10233 op0 = memloc;
10234 clear_mem_expr = true;
10237 if (offset)
10239 machine_mode address_mode;
10240 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10241 EXPAND_SUM);
10243 gcc_assert (MEM_P (op0));
10245 address_mode = get_address_mode (op0);
10246 if (GET_MODE (offset_rtx) != address_mode)
10248 /* We cannot be sure that the RTL in offset_rtx is valid outside
10249 of a memory address context, so force it into a register
10250 before attempting to convert it to the desired mode. */
10251 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10252 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10255 /* See the comment in expand_assignment for the rationale. */
10256 if (mode1 != VOIDmode
10257 && bitpos != 0
10258 && bitsize > 0
10259 && (bitpos % bitsize) == 0
10260 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10261 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10263 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10264 bitpos = 0;
10267 op0 = offset_address (op0, offset_rtx,
10268 highest_pow2_factor (offset));
10271 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10272 record its alignment as BIGGEST_ALIGNMENT. */
10273 if (MEM_P (op0) && bitpos == 0 && offset != 0
10274 && is_aligning_offset (offset, tem))
10275 set_mem_align (op0, BIGGEST_ALIGNMENT);
10277 /* Don't forget about volatility even if this is a bitfield. */
10278 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10280 if (op0 == orig_op0)
10281 op0 = copy_rtx (op0);
10283 MEM_VOLATILE_P (op0) = 1;
10286 /* In cases where an aligned union has an unaligned object
10287 as a field, we might be extracting a BLKmode value from
10288 an integer-mode (e.g., SImode) object. Handle this case
10289 by doing the extract into an object as wide as the field
10290 (which we know to be the width of a basic mode), then
10291 storing into memory, and changing the mode to BLKmode. */
10292 if (mode1 == VOIDmode
10293 || REG_P (op0) || GET_CODE (op0) == SUBREG
10294 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10295 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10296 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10297 && modifier != EXPAND_CONST_ADDRESS
10298 && modifier != EXPAND_INITIALIZER
10299 && modifier != EXPAND_MEMORY)
10300 /* If the bitfield is volatile and the bitsize
10301 is narrower than the access size of the bitfield,
10302 we need to extract bitfields from the access. */
10303 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10304 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10305 && mode1 != BLKmode
10306 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10307 /* If the field isn't aligned enough to fetch as a memref,
10308 fetch it as a bit field. */
10309 || (mode1 != BLKmode
10310 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10311 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10312 || (MEM_P (op0)
10313 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10314 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10315 && modifier != EXPAND_MEMORY
10316 && ((modifier == EXPAND_CONST_ADDRESS
10317 || modifier == EXPAND_INITIALIZER)
10318 ? STRICT_ALIGNMENT
10319 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10320 || (bitpos % BITS_PER_UNIT != 0)))
10321 /* If the type and the field are a constant size and the
10322 size of the type isn't the same size as the bitfield,
10323 we must use bitfield operations. */
10324 || (bitsize >= 0
10325 && TYPE_SIZE (TREE_TYPE (exp))
10326 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10327 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10328 bitsize)))
10330 machine_mode ext_mode = mode;
10332 if (ext_mode == BLKmode
10333 && ! (target != 0 && MEM_P (op0)
10334 && MEM_P (target)
10335 && bitpos % BITS_PER_UNIT == 0))
10336 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10338 if (ext_mode == BLKmode)
10340 if (target == 0)
10341 target = assign_temp (type, 1, 1);
10343 /* ??? Unlike the similar test a few lines below, this one is
10344 very likely obsolete. */
10345 if (bitsize == 0)
10346 return target;
10348 /* In this case, BITPOS must start at a byte boundary and
10349 TARGET, if specified, must be a MEM. */
10350 gcc_assert (MEM_P (op0)
10351 && (!target || MEM_P (target))
10352 && !(bitpos % BITS_PER_UNIT));
10354 emit_block_move (target,
10355 adjust_address (op0, VOIDmode,
10356 bitpos / BITS_PER_UNIT),
10357 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10358 / BITS_PER_UNIT),
10359 (modifier == EXPAND_STACK_PARM
10360 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10362 return target;
10365 /* If we have nothing to extract, the result will be 0 for targets
10366 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10367 return 0 for the sake of consistency, as reading a zero-sized
10368 bitfield is valid in Ada and the value is fully specified. */
10369 if (bitsize == 0)
10370 return const0_rtx;
10372 op0 = validize_mem (op0);
10374 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10375 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10377 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10378 (modifier == EXPAND_STACK_PARM
10379 ? NULL_RTX : target),
10380 ext_mode, ext_mode);
10382 /* If the result is a record type and BITSIZE is narrower than
10383 the mode of OP0, an integral mode, and this is a big endian
10384 machine, we must put the field into the high-order bits. */
10385 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10386 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10387 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10388 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10389 GET_MODE_BITSIZE (GET_MODE (op0))
10390 - bitsize, op0, 1);
10392 /* If the result type is BLKmode, store the data into a temporary
10393 of the appropriate type, but with the mode corresponding to the
10394 mode for the data we have (op0's mode). */
10395 if (mode == BLKmode)
10397 rtx new_rtx
10398 = assign_stack_temp_for_type (ext_mode,
10399 GET_MODE_BITSIZE (ext_mode),
10400 type);
10401 emit_move_insn (new_rtx, op0);
10402 op0 = copy_rtx (new_rtx);
10403 PUT_MODE (op0, BLKmode);
10406 return op0;
10409 /* If the result is BLKmode, use that to access the object
10410 now as well. */
10411 if (mode == BLKmode)
10412 mode1 = BLKmode;
10414 /* Get a reference to just this component. */
10415 if (modifier == EXPAND_CONST_ADDRESS
10416 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10417 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10418 else
10419 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10421 if (op0 == orig_op0)
10422 op0 = copy_rtx (op0);
10424 set_mem_attributes (op0, exp, 0);
10426 if (REG_P (XEXP (op0, 0)))
10427 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10429 /* If op0 is a temporary because the original expressions was forced
10430 to memory, clear MEM_EXPR so that the original expression cannot
10431 be marked as addressable through MEM_EXPR of the temporary. */
10432 if (clear_mem_expr)
10433 set_mem_expr (op0, NULL_TREE);
10435 MEM_VOLATILE_P (op0) |= volatilep;
10436 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10437 || modifier == EXPAND_CONST_ADDRESS
10438 || modifier == EXPAND_INITIALIZER)
10439 return op0;
10441 if (target == 0)
10442 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10444 convert_move (target, op0, unsignedp);
10445 return target;
10448 case OBJ_TYPE_REF:
10449 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10451 case CALL_EXPR:
10452 /* All valid uses of __builtin_va_arg_pack () are removed during
10453 inlining. */
10454 if (CALL_EXPR_VA_ARG_PACK (exp))
10455 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10457 tree fndecl = get_callee_fndecl (exp), attr;
10459 if (fndecl
10460 && (attr = lookup_attribute ("error",
10461 DECL_ATTRIBUTES (fndecl))) != NULL)
10462 error ("%Kcall to %qs declared with attribute error: %s",
10463 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10464 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10465 if (fndecl
10466 && (attr = lookup_attribute ("warning",
10467 DECL_ATTRIBUTES (fndecl))) != NULL)
10468 warning_at (tree_nonartificial_location (exp),
10469 0, "%Kcall to %qs declared with attribute warning: %s",
10470 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10471 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10473 /* Check for a built-in function. */
10474 if (fndecl && DECL_BUILT_IN (fndecl))
10476 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10477 if (CALL_WITH_BOUNDS_P (exp))
10478 return expand_builtin_with_bounds (exp, target, subtarget,
10479 tmode, ignore);
10480 else
10481 return expand_builtin (exp, target, subtarget, tmode, ignore);
10484 return expand_call (exp, target, ignore);
10486 case VIEW_CONVERT_EXPR:
10487 op0 = NULL_RTX;
10489 /* If we are converting to BLKmode, try to avoid an intermediate
10490 temporary by fetching an inner memory reference. */
10491 if (mode == BLKmode
10492 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10493 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10494 && handled_component_p (treeop0))
10496 machine_mode mode1;
10497 HOST_WIDE_INT bitsize, bitpos;
10498 tree offset;
10499 int unsignedp;
10500 int volatilep = 0;
10501 tree tem
10502 = get_inner_reference (treeop0, &bitsize, &bitpos,
10503 &offset, &mode1, &unsignedp, &volatilep,
10504 true);
10505 rtx orig_op0;
10507 /* ??? We should work harder and deal with non-zero offsets. */
10508 if (!offset
10509 && (bitpos % BITS_PER_UNIT) == 0
10510 && bitsize >= 0
10511 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10513 /* See the normal_inner_ref case for the rationale. */
10514 orig_op0
10515 = expand_expr_real (tem,
10516 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10517 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10518 != INTEGER_CST)
10519 && modifier != EXPAND_STACK_PARM
10520 ? target : NULL_RTX),
10521 VOIDmode,
10522 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10523 NULL, true);
10525 if (MEM_P (orig_op0))
10527 op0 = orig_op0;
10529 /* Get a reference to just this component. */
10530 if (modifier == EXPAND_CONST_ADDRESS
10531 || modifier == EXPAND_SUM
10532 || modifier == EXPAND_INITIALIZER)
10533 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10534 else
10535 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10537 if (op0 == orig_op0)
10538 op0 = copy_rtx (op0);
10540 set_mem_attributes (op0, treeop0, 0);
10541 if (REG_P (XEXP (op0, 0)))
10542 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10544 MEM_VOLATILE_P (op0) |= volatilep;
10549 if (!op0)
10550 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10551 NULL, inner_reference_p);
10553 /* If the input and output modes are both the same, we are done. */
10554 if (mode == GET_MODE (op0))
10556 /* If neither mode is BLKmode, and both modes are the same size
10557 then we can use gen_lowpart. */
10558 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10559 && (GET_MODE_PRECISION (mode)
10560 == GET_MODE_PRECISION (GET_MODE (op0)))
10561 && !COMPLEX_MODE_P (GET_MODE (op0)))
10563 if (GET_CODE (op0) == SUBREG)
10564 op0 = force_reg (GET_MODE (op0), op0);
10565 temp = gen_lowpart_common (mode, op0);
10566 if (temp)
10567 op0 = temp;
10568 else
10570 if (!REG_P (op0) && !MEM_P (op0))
10571 op0 = force_reg (GET_MODE (op0), op0);
10572 op0 = gen_lowpart (mode, op0);
10575 /* If both types are integral, convert from one mode to the other. */
10576 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10577 op0 = convert_modes (mode, GET_MODE (op0), op0,
10578 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10579 /* If the output type is a bit-field type, do an extraction. */
10580 else if (reduce_bit_field)
10581 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10582 TYPE_UNSIGNED (type), NULL_RTX,
10583 mode, mode);
10584 /* As a last resort, spill op0 to memory, and reload it in a
10585 different mode. */
10586 else if (!MEM_P (op0))
10588 /* If the operand is not a MEM, force it into memory. Since we
10589 are going to be changing the mode of the MEM, don't call
10590 force_const_mem for constants because we don't allow pool
10591 constants to change mode. */
10592 tree inner_type = TREE_TYPE (treeop0);
10594 gcc_assert (!TREE_ADDRESSABLE (exp));
10596 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10597 target
10598 = assign_stack_temp_for_type
10599 (TYPE_MODE (inner_type),
10600 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10602 emit_move_insn (target, op0);
10603 op0 = target;
10606 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10607 output type is such that the operand is known to be aligned, indicate
10608 that it is. Otherwise, we need only be concerned about alignment for
10609 non-BLKmode results. */
10610 if (MEM_P (op0))
10612 enum insn_code icode;
10614 if (TYPE_ALIGN_OK (type))
10616 /* ??? Copying the MEM without substantially changing it might
10617 run afoul of the code handling volatile memory references in
10618 store_expr, which assumes that TARGET is returned unmodified
10619 if it has been used. */
10620 op0 = copy_rtx (op0);
10621 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10623 else if (modifier != EXPAND_WRITE
10624 && modifier != EXPAND_MEMORY
10625 && !inner_reference_p
10626 && mode != BLKmode
10627 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10629 /* If the target does have special handling for unaligned
10630 loads of mode then use them. */
10631 if ((icode = optab_handler (movmisalign_optab, mode))
10632 != CODE_FOR_nothing)
10634 rtx reg, insn;
10636 op0 = adjust_address (op0, mode, 0);
10637 /* We've already validated the memory, and we're creating a
10638 new pseudo destination. The predicates really can't
10639 fail. */
10640 reg = gen_reg_rtx (mode);
10642 /* Nor can the insn generator. */
10643 insn = GEN_FCN (icode) (reg, op0);
10644 emit_insn (insn);
10645 return reg;
10647 else if (STRICT_ALIGNMENT)
10649 tree inner_type = TREE_TYPE (treeop0);
10650 HOST_WIDE_INT temp_size
10651 = MAX (int_size_in_bytes (inner_type),
10652 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10653 rtx new_rtx
10654 = assign_stack_temp_for_type (mode, temp_size, type);
10655 rtx new_with_op0_mode
10656 = adjust_address (new_rtx, GET_MODE (op0), 0);
10658 gcc_assert (!TREE_ADDRESSABLE (exp));
10660 if (GET_MODE (op0) == BLKmode)
10661 emit_block_move (new_with_op0_mode, op0,
10662 GEN_INT (GET_MODE_SIZE (mode)),
10663 (modifier == EXPAND_STACK_PARM
10664 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10665 else
10666 emit_move_insn (new_with_op0_mode, op0);
10668 op0 = new_rtx;
10672 op0 = adjust_address (op0, mode, 0);
10675 return op0;
10677 case MODIFY_EXPR:
10679 tree lhs = treeop0;
10680 tree rhs = treeop1;
10681 gcc_assert (ignore);
10683 /* Check for |= or &= of a bitfield of size one into another bitfield
10684 of size 1. In this case, (unless we need the result of the
10685 assignment) we can do this more efficiently with a
10686 test followed by an assignment, if necessary.
10688 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10689 things change so we do, this code should be enhanced to
10690 support it. */
10691 if (TREE_CODE (lhs) == COMPONENT_REF
10692 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10693 || TREE_CODE (rhs) == BIT_AND_EXPR)
10694 && TREE_OPERAND (rhs, 0) == lhs
10695 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10696 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10697 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10699 rtx_code_label *label = gen_label_rtx ();
10700 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10701 do_jump (TREE_OPERAND (rhs, 1),
10702 value ? label : 0,
10703 value ? 0 : label, -1);
10704 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10705 false);
10706 do_pending_stack_adjust ();
10707 emit_label (label);
10708 return const0_rtx;
10711 expand_assignment (lhs, rhs, false);
10712 return const0_rtx;
10715 case ADDR_EXPR:
10716 return expand_expr_addr_expr (exp, target, tmode, modifier);
10718 case REALPART_EXPR:
10719 op0 = expand_normal (treeop0);
10720 return read_complex_part (op0, false);
10722 case IMAGPART_EXPR:
10723 op0 = expand_normal (treeop0);
10724 return read_complex_part (op0, true);
10726 case RETURN_EXPR:
10727 case LABEL_EXPR:
10728 case GOTO_EXPR:
10729 case SWITCH_EXPR:
10730 case ASM_EXPR:
10731 /* Expanded in cfgexpand.c. */
10732 gcc_unreachable ();
10734 case TRY_CATCH_EXPR:
10735 case CATCH_EXPR:
10736 case EH_FILTER_EXPR:
10737 case TRY_FINALLY_EXPR:
10738 /* Lowered by tree-eh.c. */
10739 gcc_unreachable ();
10741 case WITH_CLEANUP_EXPR:
10742 case CLEANUP_POINT_EXPR:
10743 case TARGET_EXPR:
10744 case CASE_LABEL_EXPR:
10745 case VA_ARG_EXPR:
10746 case BIND_EXPR:
10747 case INIT_EXPR:
10748 case CONJ_EXPR:
10749 case COMPOUND_EXPR:
10750 case PREINCREMENT_EXPR:
10751 case PREDECREMENT_EXPR:
10752 case POSTINCREMENT_EXPR:
10753 case POSTDECREMENT_EXPR:
10754 case LOOP_EXPR:
10755 case EXIT_EXPR:
10756 case COMPOUND_LITERAL_EXPR:
10757 /* Lowered by gimplify.c. */
10758 gcc_unreachable ();
10760 case FDESC_EXPR:
10761 /* Function descriptors are not valid except for as
10762 initialization constants, and should not be expanded. */
10763 gcc_unreachable ();
10765 case WITH_SIZE_EXPR:
10766 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10767 have pulled out the size to use in whatever context it needed. */
10768 return expand_expr_real (treeop0, original_target, tmode,
10769 modifier, alt_rtl, inner_reference_p);
10771 default:
10772 return expand_expr_real_2 (&ops, target, tmode, modifier);
10776 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10777 signedness of TYPE), possibly returning the result in TARGET. */
10778 static rtx
10779 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10781 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10782 if (target && GET_MODE (target) != GET_MODE (exp))
10783 target = 0;
10784 /* For constant values, reduce using build_int_cst_type. */
10785 if (CONST_INT_P (exp))
10787 HOST_WIDE_INT value = INTVAL (exp);
10788 tree t = build_int_cst_type (type, value);
10789 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10791 else if (TYPE_UNSIGNED (type))
10793 machine_mode mode = GET_MODE (exp);
10794 rtx mask = immed_wide_int_const
10795 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10796 return expand_and (mode, exp, mask, target);
10798 else
10800 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10801 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10802 exp, count, target, 0);
10803 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10804 exp, count, target, 0);
10808 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10809 when applied to the address of EXP produces an address known to be
10810 aligned more than BIGGEST_ALIGNMENT. */
10812 static int
10813 is_aligning_offset (const_tree offset, const_tree exp)
10815 /* Strip off any conversions. */
10816 while (CONVERT_EXPR_P (offset))
10817 offset = TREE_OPERAND (offset, 0);
10819 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10820 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10821 if (TREE_CODE (offset) != BIT_AND_EXPR
10822 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10823 || compare_tree_int (TREE_OPERAND (offset, 1),
10824 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10825 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10826 return 0;
10828 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10829 It must be NEGATE_EXPR. Then strip any more conversions. */
10830 offset = TREE_OPERAND (offset, 0);
10831 while (CONVERT_EXPR_P (offset))
10832 offset = TREE_OPERAND (offset, 0);
10834 if (TREE_CODE (offset) != NEGATE_EXPR)
10835 return 0;
10837 offset = TREE_OPERAND (offset, 0);
10838 while (CONVERT_EXPR_P (offset))
10839 offset = TREE_OPERAND (offset, 0);
10841 /* This must now be the address of EXP. */
10842 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10845 /* Return the tree node if an ARG corresponds to a string constant or zero
10846 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10847 in bytes within the string that ARG is accessing. The type of the
10848 offset will be `sizetype'. */
10850 tree
10851 string_constant (tree arg, tree *ptr_offset)
10853 tree array, offset, lower_bound;
10854 STRIP_NOPS (arg);
10856 if (TREE_CODE (arg) == ADDR_EXPR)
10858 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10860 *ptr_offset = size_zero_node;
10861 return TREE_OPERAND (arg, 0);
10863 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10865 array = TREE_OPERAND (arg, 0);
10866 offset = size_zero_node;
10868 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10870 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10871 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10872 if (TREE_CODE (array) != STRING_CST
10873 && TREE_CODE (array) != VAR_DECL)
10874 return 0;
10876 /* Check if the array has a nonzero lower bound. */
10877 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10878 if (!integer_zerop (lower_bound))
10880 /* If the offset and base aren't both constants, return 0. */
10881 if (TREE_CODE (lower_bound) != INTEGER_CST)
10882 return 0;
10883 if (TREE_CODE (offset) != INTEGER_CST)
10884 return 0;
10885 /* Adjust offset by the lower bound. */
10886 offset = size_diffop (fold_convert (sizetype, offset),
10887 fold_convert (sizetype, lower_bound));
10890 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10892 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10893 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10894 if (TREE_CODE (array) != ADDR_EXPR)
10895 return 0;
10896 array = TREE_OPERAND (array, 0);
10897 if (TREE_CODE (array) != STRING_CST
10898 && TREE_CODE (array) != VAR_DECL)
10899 return 0;
10901 else
10902 return 0;
10904 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10906 tree arg0 = TREE_OPERAND (arg, 0);
10907 tree arg1 = TREE_OPERAND (arg, 1);
10909 STRIP_NOPS (arg0);
10910 STRIP_NOPS (arg1);
10912 if (TREE_CODE (arg0) == ADDR_EXPR
10913 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10914 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10916 array = TREE_OPERAND (arg0, 0);
10917 offset = arg1;
10919 else if (TREE_CODE (arg1) == ADDR_EXPR
10920 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10921 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10923 array = TREE_OPERAND (arg1, 0);
10924 offset = arg0;
10926 else
10927 return 0;
10929 else
10930 return 0;
10932 if (TREE_CODE (array) == STRING_CST)
10934 *ptr_offset = fold_convert (sizetype, offset);
10935 return array;
10937 else if (TREE_CODE (array) == VAR_DECL
10938 || TREE_CODE (array) == CONST_DECL)
10940 int length;
10941 tree init = ctor_for_folding (array);
10943 /* Variables initialized to string literals can be handled too. */
10944 if (init == error_mark_node
10945 || !init
10946 || TREE_CODE (init) != STRING_CST)
10947 return 0;
10949 /* Avoid const char foo[4] = "abcde"; */
10950 if (DECL_SIZE_UNIT (array) == NULL_TREE
10951 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10952 || (length = TREE_STRING_LENGTH (init)) <= 0
10953 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10954 return 0;
10956 /* If variable is bigger than the string literal, OFFSET must be constant
10957 and inside of the bounds of the string literal. */
10958 offset = fold_convert (sizetype, offset);
10959 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10960 && (! tree_fits_uhwi_p (offset)
10961 || compare_tree_int (offset, length) >= 0))
10962 return 0;
10964 *ptr_offset = offset;
10965 return init;
10968 return 0;
10971 /* Generate code to calculate OPS, and exploded expression
10972 using a store-flag instruction and return an rtx for the result.
10973 OPS reflects a comparison.
10975 If TARGET is nonzero, store the result there if convenient.
10977 Return zero if there is no suitable set-flag instruction
10978 available on this machine.
10980 Once expand_expr has been called on the arguments of the comparison,
10981 we are committed to doing the store flag, since it is not safe to
10982 re-evaluate the expression. We emit the store-flag insn by calling
10983 emit_store_flag, but only expand the arguments if we have a reason
10984 to believe that emit_store_flag will be successful. If we think that
10985 it will, but it isn't, we have to simulate the store-flag with a
10986 set/jump/set sequence. */
10988 static rtx
10989 do_store_flag (sepops ops, rtx target, machine_mode mode)
10991 enum rtx_code code;
10992 tree arg0, arg1, type;
10993 tree tem;
10994 machine_mode operand_mode;
10995 int unsignedp;
10996 rtx op0, op1;
10997 rtx subtarget = target;
10998 location_t loc = ops->location;
11000 arg0 = ops->op0;
11001 arg1 = ops->op1;
11003 /* Don't crash if the comparison was erroneous. */
11004 if (arg0 == error_mark_node || arg1 == error_mark_node)
11005 return const0_rtx;
11007 type = TREE_TYPE (arg0);
11008 operand_mode = TYPE_MODE (type);
11009 unsignedp = TYPE_UNSIGNED (type);
11011 /* We won't bother with BLKmode store-flag operations because it would mean
11012 passing a lot of information to emit_store_flag. */
11013 if (operand_mode == BLKmode)
11014 return 0;
11016 /* We won't bother with store-flag operations involving function pointers
11017 when function pointers must be canonicalized before comparisons. */
11018 #ifdef HAVE_canonicalize_funcptr_for_compare
11019 if (HAVE_canonicalize_funcptr_for_compare
11020 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11021 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11022 == FUNCTION_TYPE))
11023 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11024 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11025 == FUNCTION_TYPE))))
11026 return 0;
11027 #endif
11029 STRIP_NOPS (arg0);
11030 STRIP_NOPS (arg1);
11032 /* For vector typed comparisons emit code to generate the desired
11033 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11034 expander for this. */
11035 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11037 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11038 tree if_true = constant_boolean_node (true, ops->type);
11039 tree if_false = constant_boolean_node (false, ops->type);
11040 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
11043 /* Get the rtx comparison code to use. We know that EXP is a comparison
11044 operation of some type. Some comparisons against 1 and -1 can be
11045 converted to comparisons with zero. Do so here so that the tests
11046 below will be aware that we have a comparison with zero. These
11047 tests will not catch constants in the first operand, but constants
11048 are rarely passed as the first operand. */
11050 switch (ops->code)
11052 case EQ_EXPR:
11053 code = EQ;
11054 break;
11055 case NE_EXPR:
11056 code = NE;
11057 break;
11058 case LT_EXPR:
11059 if (integer_onep (arg1))
11060 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11061 else
11062 code = unsignedp ? LTU : LT;
11063 break;
11064 case LE_EXPR:
11065 if (! unsignedp && integer_all_onesp (arg1))
11066 arg1 = integer_zero_node, code = LT;
11067 else
11068 code = unsignedp ? LEU : LE;
11069 break;
11070 case GT_EXPR:
11071 if (! unsignedp && integer_all_onesp (arg1))
11072 arg1 = integer_zero_node, code = GE;
11073 else
11074 code = unsignedp ? GTU : GT;
11075 break;
11076 case GE_EXPR:
11077 if (integer_onep (arg1))
11078 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11079 else
11080 code = unsignedp ? GEU : GE;
11081 break;
11083 case UNORDERED_EXPR:
11084 code = UNORDERED;
11085 break;
11086 case ORDERED_EXPR:
11087 code = ORDERED;
11088 break;
11089 case UNLT_EXPR:
11090 code = UNLT;
11091 break;
11092 case UNLE_EXPR:
11093 code = UNLE;
11094 break;
11095 case UNGT_EXPR:
11096 code = UNGT;
11097 break;
11098 case UNGE_EXPR:
11099 code = UNGE;
11100 break;
11101 case UNEQ_EXPR:
11102 code = UNEQ;
11103 break;
11104 case LTGT_EXPR:
11105 code = LTGT;
11106 break;
11108 default:
11109 gcc_unreachable ();
11112 /* Put a constant second. */
11113 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11114 || TREE_CODE (arg0) == FIXED_CST)
11116 tem = arg0; arg0 = arg1; arg1 = tem;
11117 code = swap_condition (code);
11120 /* If this is an equality or inequality test of a single bit, we can
11121 do this by shifting the bit being tested to the low-order bit and
11122 masking the result with the constant 1. If the condition was EQ,
11123 we xor it with 1. This does not require an scc insn and is faster
11124 than an scc insn even if we have it.
11126 The code to make this transformation was moved into fold_single_bit_test,
11127 so we just call into the folder and expand its result. */
11129 if ((code == NE || code == EQ)
11130 && integer_zerop (arg1)
11131 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11133 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11134 if (srcstmt
11135 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11137 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11138 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11139 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11140 gimple_assign_rhs1 (srcstmt),
11141 gimple_assign_rhs2 (srcstmt));
11142 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11143 if (temp)
11144 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11148 if (! get_subtarget (target)
11149 || GET_MODE (subtarget) != operand_mode)
11150 subtarget = 0;
11152 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11154 if (target == 0)
11155 target = gen_reg_rtx (mode);
11157 /* Try a cstore if possible. */
11158 return emit_store_flag_force (target, code, op0, op1,
11159 operand_mode, unsignedp,
11160 (TYPE_PRECISION (ops->type) == 1
11161 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11165 /* Stubs in case we haven't got a casesi insn. */
11166 #ifndef HAVE_casesi
11167 # define HAVE_casesi 0
11168 # define gen_casesi(a, b, c, d, e) (0)
11169 # define CODE_FOR_casesi CODE_FOR_nothing
11170 #endif
11172 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11173 0 otherwise (i.e. if there is no casesi instruction).
11175 DEFAULT_PROBABILITY is the probability of jumping to the default
11176 label. */
11178 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11179 rtx table_label, rtx default_label, rtx fallback_label,
11180 int default_probability)
11182 struct expand_operand ops[5];
11183 machine_mode index_mode = SImode;
11184 rtx op1, op2, index;
11186 if (! HAVE_casesi)
11187 return 0;
11189 /* Convert the index to SImode. */
11190 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11192 machine_mode omode = TYPE_MODE (index_type);
11193 rtx rangertx = expand_normal (range);
11195 /* We must handle the endpoints in the original mode. */
11196 index_expr = build2 (MINUS_EXPR, index_type,
11197 index_expr, minval);
11198 minval = integer_zero_node;
11199 index = expand_normal (index_expr);
11200 if (default_label)
11201 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11202 omode, 1, default_label,
11203 default_probability);
11204 /* Now we can safely truncate. */
11205 index = convert_to_mode (index_mode, index, 0);
11207 else
11209 if (TYPE_MODE (index_type) != index_mode)
11211 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11212 index_expr = fold_convert (index_type, index_expr);
11215 index = expand_normal (index_expr);
11218 do_pending_stack_adjust ();
11220 op1 = expand_normal (minval);
11221 op2 = expand_normal (range);
11223 create_input_operand (&ops[0], index, index_mode);
11224 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11225 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11226 create_fixed_operand (&ops[3], table_label);
11227 create_fixed_operand (&ops[4], (default_label
11228 ? default_label
11229 : fallback_label));
11230 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11231 return 1;
11234 /* Attempt to generate a tablejump instruction; same concept. */
11235 #ifndef HAVE_tablejump
11236 #define HAVE_tablejump 0
11237 #define gen_tablejump(x, y) (0)
11238 #endif
11240 /* Subroutine of the next function.
11242 INDEX is the value being switched on, with the lowest value
11243 in the table already subtracted.
11244 MODE is its expected mode (needed if INDEX is constant).
11245 RANGE is the length of the jump table.
11246 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11248 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11249 index value is out of range.
11250 DEFAULT_PROBABILITY is the probability of jumping to
11251 the default label. */
11253 static void
11254 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11255 rtx default_label, int default_probability)
11257 rtx temp, vector;
11259 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11260 cfun->cfg->max_jumptable_ents = INTVAL (range);
11262 /* Do an unsigned comparison (in the proper mode) between the index
11263 expression and the value which represents the length of the range.
11264 Since we just finished subtracting the lower bound of the range
11265 from the index expression, this comparison allows us to simultaneously
11266 check that the original index expression value is both greater than
11267 or equal to the minimum value of the range and less than or equal to
11268 the maximum value of the range. */
11270 if (default_label)
11271 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11272 default_label, default_probability);
11275 /* If index is in range, it must fit in Pmode.
11276 Convert to Pmode so we can index with it. */
11277 if (mode != Pmode)
11278 index = convert_to_mode (Pmode, index, 1);
11280 /* Don't let a MEM slip through, because then INDEX that comes
11281 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11282 and break_out_memory_refs will go to work on it and mess it up. */
11283 #ifdef PIC_CASE_VECTOR_ADDRESS
11284 if (flag_pic && !REG_P (index))
11285 index = copy_to_mode_reg (Pmode, index);
11286 #endif
11288 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11289 GET_MODE_SIZE, because this indicates how large insns are. The other
11290 uses should all be Pmode, because they are addresses. This code
11291 could fail if addresses and insns are not the same size. */
11292 index = simplify_gen_binary (MULT, Pmode, index,
11293 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11294 Pmode));
11295 index = simplify_gen_binary (PLUS, Pmode, index,
11296 gen_rtx_LABEL_REF (Pmode, table_label));
11298 #ifdef PIC_CASE_VECTOR_ADDRESS
11299 if (flag_pic)
11300 index = PIC_CASE_VECTOR_ADDRESS (index);
11301 else
11302 #endif
11303 index = memory_address (CASE_VECTOR_MODE, index);
11304 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11305 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11306 convert_move (temp, vector, 0);
11308 emit_jump_insn (gen_tablejump (temp, table_label));
11310 /* If we are generating PIC code or if the table is PC-relative, the
11311 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11312 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11313 emit_barrier ();
11317 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11318 rtx table_label, rtx default_label, int default_probability)
11320 rtx index;
11322 if (! HAVE_tablejump)
11323 return 0;
11325 index_expr = fold_build2 (MINUS_EXPR, index_type,
11326 fold_convert (index_type, index_expr),
11327 fold_convert (index_type, minval));
11328 index = expand_normal (index_expr);
11329 do_pending_stack_adjust ();
11331 do_tablejump (index, TYPE_MODE (index_type),
11332 convert_modes (TYPE_MODE (index_type),
11333 TYPE_MODE (TREE_TYPE (range)),
11334 expand_normal (range),
11335 TYPE_UNSIGNED (TREE_TYPE (range))),
11336 table_label, default_label, default_probability);
11337 return 1;
11340 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11341 static rtx
11342 const_vector_from_tree (tree exp)
11344 rtvec v;
11345 unsigned i;
11346 int units;
11347 tree elt;
11348 machine_mode inner, mode;
11350 mode = TYPE_MODE (TREE_TYPE (exp));
11352 if (initializer_zerop (exp))
11353 return CONST0_RTX (mode);
11355 units = GET_MODE_NUNITS (mode);
11356 inner = GET_MODE_INNER (mode);
11358 v = rtvec_alloc (units);
11360 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11362 elt = VECTOR_CST_ELT (exp, i);
11364 if (TREE_CODE (elt) == REAL_CST)
11365 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11366 inner);
11367 else if (TREE_CODE (elt) == FIXED_CST)
11368 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11369 inner);
11370 else
11371 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11374 return gen_rtx_CONST_VECTOR (mode, v);
11377 /* Build a decl for a personality function given a language prefix. */
11379 tree
11380 build_personality_function (const char *lang)
11382 const char *unwind_and_version;
11383 tree decl, type;
11384 char *name;
11386 switch (targetm_common.except_unwind_info (&global_options))
11388 case UI_NONE:
11389 return NULL;
11390 case UI_SJLJ:
11391 unwind_and_version = "_sj0";
11392 break;
11393 case UI_DWARF2:
11394 case UI_TARGET:
11395 unwind_and_version = "_v0";
11396 break;
11397 case UI_SEH:
11398 unwind_and_version = "_seh0";
11399 break;
11400 default:
11401 gcc_unreachable ();
11404 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11406 type = build_function_type_list (integer_type_node, integer_type_node,
11407 long_long_unsigned_type_node,
11408 ptr_type_node, ptr_type_node, NULL_TREE);
11409 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11410 get_identifier (name), type);
11411 DECL_ARTIFICIAL (decl) = 1;
11412 DECL_EXTERNAL (decl) = 1;
11413 TREE_PUBLIC (decl) = 1;
11415 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11416 are the flags assigned by targetm.encode_section_info. */
11417 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11419 return decl;
11422 /* Extracts the personality function of DECL and returns the corresponding
11423 libfunc. */
11426 get_personality_function (tree decl)
11428 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11429 enum eh_personality_kind pk;
11431 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11432 if (pk == eh_personality_none)
11433 return NULL;
11435 if (!personality
11436 && pk == eh_personality_any)
11437 personality = lang_hooks.eh_personality ();
11439 if (pk == eh_personality_lang)
11440 gcc_assert (personality != NULL_TREE);
11442 return XEXP (DECL_RTL (personality), 0);
11445 /* Returns a tree for the size of EXP in bytes. */
11447 static tree
11448 tree_expr_size (const_tree exp)
11450 if (DECL_P (exp)
11451 && DECL_SIZE_UNIT (exp) != 0)
11452 return DECL_SIZE_UNIT (exp);
11453 else
11454 return size_in_bytes (TREE_TYPE (exp));
11457 /* Return an rtx for the size in bytes of the value of EXP. */
11460 expr_size (tree exp)
11462 tree size;
11464 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11465 size = TREE_OPERAND (exp, 1);
11466 else
11468 size = tree_expr_size (exp);
11469 gcc_assert (size);
11470 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11473 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11476 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11477 if the size can vary or is larger than an integer. */
11479 static HOST_WIDE_INT
11480 int_expr_size (tree exp)
11482 tree size;
11484 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11485 size = TREE_OPERAND (exp, 1);
11486 else
11488 size = tree_expr_size (exp);
11489 gcc_assert (size);
11492 if (size == 0 || !tree_fits_shwi_p (size))
11493 return -1;
11495 return tree_to_shwi (size);
11498 #include "gt-expr.h"