Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_6_3-mobile / gcc / expr.c
blobd4cf118c22ec37ed568a8a73661c37c9dfcbb247
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "except.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "insn-attr.h"
36 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "reload.h"
42 #include "output.h"
43 #include "typeclass.h"
44 #include "toplev.h"
45 #include "langhooks.h"
46 #include "intl.h"
47 #include "tm_p.h"
48 #include "tree-iterator.h"
49 #include "tree-pass.h"
50 #include "tree-flow.h"
51 #include "target.h"
52 #include "timevar.h"
53 #include "df.h"
54 #include "diagnostic.h"
55 #include "ssaexpand.h"
56 #include "target-globals.h"
58 /* Decide whether a function's arguments should be processed
59 from first to last or from last to first.
61 They should if the stack and args grow in opposite directions, but
62 only if we have push insns. */
64 #ifdef PUSH_ROUNDING
66 #ifndef PUSH_ARGS_REVERSED
67 #if defined (STACK_GROWS_DOWNWARD) != defined (ARGS_GROW_DOWNWARD)
68 #define PUSH_ARGS_REVERSED /* If it's last to first. */
69 #endif
70 #endif
72 #endif
74 #ifndef STACK_PUSH_CODE
75 #ifdef STACK_GROWS_DOWNWARD
76 #define STACK_PUSH_CODE PRE_DEC
77 #else
78 #define STACK_PUSH_CODE PRE_INC
79 #endif
80 #endif
83 /* If this is nonzero, we do not bother generating VOLATILE
84 around volatile memory references, and we are willing to
85 output indirect addresses. If cse is to follow, we reject
86 indirect addresses so a useful potential cse is generated;
87 if it is used only once, instruction combination will produce
88 the same indirect address eventually. */
89 int cse_not_expected;
91 /* This structure is used by move_by_pieces to describe the move to
92 be performed. */
93 struct move_by_pieces_d
95 rtx to;
96 rtx to_addr;
97 int autinc_to;
98 int explicit_inc_to;
99 rtx from;
100 rtx from_addr;
101 int autinc_from;
102 int explicit_inc_from;
103 unsigned HOST_WIDE_INT len;
104 HOST_WIDE_INT offset;
105 int reverse;
108 /* This structure is used by store_by_pieces to describe the clear to
109 be performed. */
111 struct store_by_pieces_d
113 rtx to;
114 rtx to_addr;
115 int autinc_to;
116 int explicit_inc_to;
117 unsigned HOST_WIDE_INT len;
118 HOST_WIDE_INT offset;
119 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode);
120 void *constfundata;
121 int reverse;
124 static unsigned HOST_WIDE_INT move_by_pieces_ninsns (unsigned HOST_WIDE_INT,
125 unsigned int,
126 unsigned int);
127 static void move_by_pieces_1 (rtx (*) (rtx, ...), enum machine_mode,
128 struct move_by_pieces_d *);
129 static bool block_move_libcall_safe_for_call_parm (void);
130 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT);
131 static tree emit_block_move_libcall_fn (int);
132 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
133 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, enum machine_mode);
134 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
135 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
136 static void store_by_pieces_2 (rtx (*) (rtx, ...), enum machine_mode,
137 struct store_by_pieces_d *);
138 static tree clear_storage_libcall_fn (int);
139 static rtx compress_float_constant (rtx, rtx);
140 static rtx get_subtarget (rtx);
141 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
142 HOST_WIDE_INT, enum machine_mode,
143 tree, tree, int, alias_set_type);
144 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
145 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT, enum machine_mode,
146 tree, tree, alias_set_type, bool);
148 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
150 static int is_aligning_offset (const_tree, const_tree);
151 static void expand_operands (tree, tree, rtx, rtx*, rtx*,
152 enum expand_modifier);
153 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
154 static rtx do_store_flag (sepops, rtx, enum machine_mode);
155 #ifdef PUSH_ROUNDING
156 static void emit_single_push_insn (enum machine_mode, rtx, tree);
157 #endif
158 static void do_tablejump (rtx, enum machine_mode, rtx, rtx, rtx, int);
159 static rtx const_vector_from_tree (tree);
160 static void write_complex_part (rtx, rtx, bool);
162 /* This macro is used to determine whether move_by_pieces should be called
163 to perform a structure copy. */
164 #ifndef MOVE_BY_PIECES_P
165 #define MOVE_BY_PIECES_P(SIZE, ALIGN) \
166 (move_by_pieces_ninsns (SIZE, ALIGN, MOVE_MAX_PIECES + 1) \
167 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
168 #endif
170 /* This macro is used to determine whether clear_by_pieces should be
171 called to clear storage. */
172 #ifndef CLEAR_BY_PIECES_P
173 #define CLEAR_BY_PIECES_P(SIZE, ALIGN) \
174 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
175 < (unsigned int) CLEAR_RATIO (optimize_insn_for_speed_p ()))
176 #endif
178 /* This macro is used to determine whether store_by_pieces should be
179 called to "memset" storage with byte values other than zero. */
180 #ifndef SET_BY_PIECES_P
181 #define SET_BY_PIECES_P(SIZE, ALIGN) \
182 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
183 < (unsigned int) SET_RATIO (optimize_insn_for_speed_p ()))
184 #endif
186 /* This macro is used to determine whether store_by_pieces should be
187 called to "memcpy" storage when the source is a constant string. */
188 #ifndef STORE_BY_PIECES_P
189 #define STORE_BY_PIECES_P(SIZE, ALIGN) \
190 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
191 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
192 #endif
194 /* SLOW_UNALIGNED_ACCESS is nonzero if unaligned accesses are very slow. */
196 #ifndef SLOW_UNALIGNED_ACCESS
197 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
198 #endif
200 /* This is run to set up which modes can be used
201 directly in memory and to initialize the block move optab. It is run
202 at the beginning of compilation and when the target is reinitialized. */
204 void
205 init_expr_target (void)
207 rtx insn, pat;
208 enum machine_mode mode;
209 int num_clobbers;
210 rtx mem, mem1;
211 rtx reg;
213 /* Try indexing by frame ptr and try by stack ptr.
214 It is known that on the Convex the stack ptr isn't a valid index.
215 With luck, one or the other is valid on any machine. */
216 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
217 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
219 /* A scratch register we can modify in-place below to avoid
220 useless RTL allocations. */
221 reg = gen_rtx_REG (VOIDmode, -1);
223 insn = rtx_alloc (INSN);
224 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
225 PATTERN (insn) = pat;
227 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
228 mode = (enum machine_mode) ((int) mode + 1))
230 int regno;
232 direct_load[(int) mode] = direct_store[(int) mode] = 0;
233 PUT_MODE (mem, mode);
234 PUT_MODE (mem1, mode);
235 PUT_MODE (reg, mode);
237 /* See if there is some register that can be used in this mode and
238 directly loaded or stored from memory. */
240 if (mode != VOIDmode && mode != BLKmode)
241 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
242 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
243 regno++)
245 if (! HARD_REGNO_MODE_OK (regno, mode))
246 continue;
248 SET_REGNO (reg, regno);
250 SET_SRC (pat) = mem;
251 SET_DEST (pat) = reg;
252 if (recog (pat, insn, &num_clobbers) >= 0)
253 direct_load[(int) mode] = 1;
255 SET_SRC (pat) = mem1;
256 SET_DEST (pat) = reg;
257 if (recog (pat, insn, &num_clobbers) >= 0)
258 direct_load[(int) mode] = 1;
260 SET_SRC (pat) = reg;
261 SET_DEST (pat) = mem;
262 if (recog (pat, insn, &num_clobbers) >= 0)
263 direct_store[(int) mode] = 1;
265 SET_SRC (pat) = reg;
266 SET_DEST (pat) = mem1;
267 if (recog (pat, insn, &num_clobbers) >= 0)
268 direct_store[(int) mode] = 1;
272 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
274 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
275 mode = GET_MODE_WIDER_MODE (mode))
277 enum machine_mode srcmode;
278 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
279 srcmode = GET_MODE_WIDER_MODE (srcmode))
281 enum insn_code ic;
283 ic = can_extend_p (mode, srcmode, 0);
284 if (ic == CODE_FOR_nothing)
285 continue;
287 PUT_MODE (mem, srcmode);
289 if ((*insn_data[ic].operand[1].predicate) (mem, srcmode))
290 float_extend_from_mem[mode][srcmode] = true;
295 /* This is run at the start of compiling a function. */
297 void
298 init_expr (void)
300 memset (&crtl->expr, 0, sizeof (crtl->expr));
303 /* Copy data from FROM to TO, where the machine modes are not the same.
304 Both modes may be integer, or both may be floating, or both may be
305 fixed-point.
306 UNSIGNEDP should be nonzero if FROM is an unsigned type.
307 This causes zero-extension instead of sign-extension. */
309 void
310 convert_move (rtx to, rtx from, int unsignedp)
312 enum machine_mode to_mode = GET_MODE (to);
313 enum machine_mode from_mode = GET_MODE (from);
314 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
315 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
316 enum insn_code code;
317 rtx libcall;
319 /* rtx code for making an equivalent value. */
320 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
321 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
324 gcc_assert (to_real == from_real);
325 gcc_assert (to_mode != BLKmode);
326 gcc_assert (from_mode != BLKmode);
328 /* If the source and destination are already the same, then there's
329 nothing to do. */
330 if (to == from)
331 return;
333 /* If FROM is a SUBREG that indicates that we have already done at least
334 the required extension, strip it. We don't handle such SUBREGs as
335 TO here. */
337 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
338 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (from)))
339 >= GET_MODE_SIZE (to_mode))
340 && SUBREG_PROMOTED_UNSIGNED_P (from) == unsignedp)
341 from = gen_lowpart (to_mode, from), from_mode = to_mode;
343 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
345 if (to_mode == from_mode
346 || (from_mode == VOIDmode && CONSTANT_P (from)))
348 emit_move_insn (to, from);
349 return;
352 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
354 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
356 if (VECTOR_MODE_P (to_mode))
357 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
358 else
359 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
361 emit_move_insn (to, from);
362 return;
365 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
367 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
368 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
369 return;
372 if (to_real)
374 rtx value, insns;
375 convert_optab tab;
377 gcc_assert ((GET_MODE_PRECISION (from_mode)
378 != GET_MODE_PRECISION (to_mode))
379 || (DECIMAL_FLOAT_MODE_P (from_mode)
380 != DECIMAL_FLOAT_MODE_P (to_mode)));
382 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
383 /* Conversion between decimal float and binary float, same size. */
384 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
385 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
386 tab = sext_optab;
387 else
388 tab = trunc_optab;
390 /* Try converting directly if the insn is supported. */
392 code = convert_optab_handler (tab, to_mode, from_mode);
393 if (code != CODE_FOR_nothing)
395 emit_unop_insn (code, to, from,
396 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
397 return;
400 /* Otherwise use a libcall. */
401 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
403 /* Is this conversion implemented yet? */
404 gcc_assert (libcall);
406 start_sequence ();
407 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
408 1, from, from_mode);
409 insns = get_insns ();
410 end_sequence ();
411 emit_libcall_block (insns, to, value,
412 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
413 from)
414 : gen_rtx_FLOAT_EXTEND (to_mode, from));
415 return;
418 /* Handle pointer conversion. */ /* SPEE 900220. */
419 /* Targets are expected to provide conversion insns between PxImode and
420 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
421 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
423 enum machine_mode full_mode
424 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
426 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
427 != CODE_FOR_nothing);
429 if (full_mode != from_mode)
430 from = convert_to_mode (full_mode, from, unsignedp);
431 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
432 to, from, UNKNOWN);
433 return;
435 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
437 rtx new_from;
438 enum machine_mode full_mode
439 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
441 gcc_assert (convert_optab_handler (sext_optab, full_mode, from_mode)
442 != CODE_FOR_nothing);
444 if (to_mode == full_mode)
446 emit_unop_insn (convert_optab_handler (sext_optab, full_mode,
447 from_mode),
448 to, from, UNKNOWN);
449 return;
452 new_from = gen_reg_rtx (full_mode);
453 emit_unop_insn (convert_optab_handler (sext_optab, full_mode, from_mode),
454 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_BITSIZE (from_mode) < GET_MODE_BITSIZE (to_mode)
481 && GET_MODE_BITSIZE (to_mode) > BITS_PER_WORD)
483 rtx insns;
484 rtx lowpart;
485 rtx fill_value;
486 rtx lowfrom;
487 int i;
488 enum 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_BITSIZE (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
525 that the source does not overlap the target. */
527 if (reg_overlap_mentioned_p (to, from))
528 from = force_reg (from_mode, from);
530 /* Get a copy of FROM widened to a word, if necessary. */
531 if (GET_MODE_BITSIZE (from_mode) < BITS_PER_WORD)
532 lowpart_mode = word_mode;
533 else
534 lowpart_mode = from_mode;
536 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
538 lowpart = gen_lowpart (lowpart_mode, to);
539 emit_move_insn (lowpart, lowfrom);
541 /* Compute the value to put in each remaining word. */
542 if (unsignedp)
543 fill_value = const0_rtx;
544 else
545 fill_value = emit_store_flag (gen_reg_rtx (word_mode),
546 LT, lowfrom, const0_rtx,
547 VOIDmode, 0, -1);
549 /* Fill the remaining words. */
550 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
552 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
553 rtx subword = operand_subword (to, index, 1, to_mode);
555 gcc_assert (subword);
557 if (fill_value != subword)
558 emit_move_insn (subword, fill_value);
561 insns = get_insns ();
562 end_sequence ();
564 emit_insn (insns);
565 return;
568 /* Truncating multi-word to a word or less. */
569 if (GET_MODE_BITSIZE (from_mode) > BITS_PER_WORD
570 && GET_MODE_BITSIZE (to_mode) <= BITS_PER_WORD)
572 if (!((MEM_P (from)
573 && ! MEM_VOLATILE_P (from)
574 && direct_load[(int) to_mode]
575 && ! mode_dependent_address_p (XEXP (from, 0)))
576 || REG_P (from)
577 || GET_CODE (from) == SUBREG))
578 from = force_reg (from_mode, from);
579 convert_move (to, gen_lowpart (word_mode, from), 0);
580 return;
583 /* Now follow all the conversions between integers
584 no more than a word long. */
586 /* For truncation, usually we can just refer to FROM in a narrower mode. */
587 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
588 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (to_mode),
589 GET_MODE_BITSIZE (from_mode)))
591 if (!((MEM_P (from)
592 && ! MEM_VOLATILE_P (from)
593 && direct_load[(int) to_mode]
594 && ! mode_dependent_address_p (XEXP (from, 0)))
595 || REG_P (from)
596 || GET_CODE (from) == SUBREG))
597 from = force_reg (from_mode, from);
598 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
599 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
600 from = copy_to_reg (from);
601 emit_move_insn (to, gen_lowpart (to_mode, from));
602 return;
605 /* Handle extension. */
606 if (GET_MODE_BITSIZE (to_mode) > GET_MODE_BITSIZE (from_mode))
608 /* Convert directly if that works. */
609 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
610 != CODE_FOR_nothing)
612 emit_unop_insn (code, to, from, equiv_code);
613 return;
615 else
617 enum machine_mode intermediate;
618 rtx tmp;
619 tree shift_amount;
621 /* Search for a mode to convert via. */
622 for (intermediate = from_mode; intermediate != VOIDmode;
623 intermediate = GET_MODE_WIDER_MODE (intermediate))
624 if (((can_extend_p (to_mode, intermediate, unsignedp)
625 != CODE_FOR_nothing)
626 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
627 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (to_mode),
628 GET_MODE_BITSIZE (intermediate))))
629 && (can_extend_p (intermediate, from_mode, unsignedp)
630 != CODE_FOR_nothing))
632 convert_move (to, convert_to_mode (intermediate, from,
633 unsignedp), unsignedp);
634 return;
637 /* No suitable intermediate mode.
638 Generate what we need with shifts. */
639 shift_amount = build_int_cst (NULL_TREE,
640 GET_MODE_BITSIZE (to_mode)
641 - GET_MODE_BITSIZE (from_mode));
642 from = gen_lowpart (to_mode, force_reg (from_mode, from));
643 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
644 to, unsignedp);
645 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
646 to, unsignedp);
647 if (tmp != to)
648 emit_move_insn (to, tmp);
649 return;
653 /* Support special truncate insns for certain modes. */
654 if (convert_optab_handler (trunc_optab, to_mode,
655 from_mode) != CODE_FOR_nothing)
657 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
658 to, from, UNKNOWN);
659 return;
662 /* Handle truncation of volatile memrefs, and so on;
663 the things that couldn't be truncated directly,
664 and for which there was no special instruction.
666 ??? Code above formerly short-circuited this, for most integer
667 mode pairs, with a force_reg in from_mode followed by a recursive
668 call to this routine. Appears always to have been wrong. */
669 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode))
671 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
672 emit_move_insn (to, temp);
673 return;
676 /* Mode combination is not recognized. */
677 gcc_unreachable ();
680 /* Return an rtx for a value that would result
681 from converting X to mode MODE.
682 Both X and MODE may be floating, or both integer.
683 UNSIGNEDP is nonzero if X is an unsigned value.
684 This can be done by referring to a part of X in place
685 or by copying to a new temporary with conversion. */
688 convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
690 return convert_modes (mode, VOIDmode, x, unsignedp);
693 /* Return an rtx for a value that would result
694 from converting X from mode OLDMODE to mode MODE.
695 Both modes may be floating, or both integer.
696 UNSIGNEDP is nonzero if X is an unsigned value.
698 This can be done by referring to a part of X in place
699 or by copying to a new temporary with conversion.
701 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
704 convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
706 rtx temp;
708 /* If FROM is a SUBREG that indicates that we have already done at least
709 the required extension, strip it. */
711 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
712 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
713 && SUBREG_PROMOTED_UNSIGNED_P (x) == unsignedp)
714 x = gen_lowpart (mode, x);
716 if (GET_MODE (x) != VOIDmode)
717 oldmode = GET_MODE (x);
719 if (mode == oldmode)
720 return x;
722 /* There is one case that we must handle specially: If we are converting
723 a CONST_INT into a mode whose size is twice HOST_BITS_PER_WIDE_INT and
724 we are to interpret the constant as unsigned, gen_lowpart will do
725 the wrong if the constant appears negative. What we want to do is
726 make the high-order word of the constant zero, not all ones. */
728 if (unsignedp && GET_MODE_CLASS (mode) == MODE_INT
729 && GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT
730 && CONST_INT_P (x) && INTVAL (x) < 0)
732 double_int val = uhwi_to_double_int (INTVAL (x));
734 /* We need to zero extend VAL. */
735 if (oldmode != VOIDmode)
736 val = double_int_zext (val, GET_MODE_BITSIZE (oldmode));
738 return immed_double_int_const (val, mode);
741 /* We can do this with a gen_lowpart if both desired and current modes
742 are integer, and this is either a constant integer, a register, or a
743 non-volatile MEM. Except for the constant case where MODE is no
744 wider than HOST_BITS_PER_WIDE_INT, we must be narrowing the operand. */
746 if ((CONST_INT_P (x)
747 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
748 || (GET_MODE_CLASS (mode) == MODE_INT
749 && GET_MODE_CLASS (oldmode) == MODE_INT
750 && (GET_CODE (x) == CONST_DOUBLE
751 || (GET_MODE_SIZE (mode) <= GET_MODE_SIZE (oldmode)
752 && ((MEM_P (x) && ! MEM_VOLATILE_P (x)
753 && direct_load[(int) mode])
754 || (REG_P (x)
755 && (! HARD_REGISTER_P (x)
756 || HARD_REGNO_MODE_OK (REGNO (x), mode))
757 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
758 GET_MODE_BITSIZE (GET_MODE (x)))))))))
760 /* ?? If we don't know OLDMODE, we have to assume here that
761 X does not need sign- or zero-extension. This may not be
762 the case, but it's the best we can do. */
763 if (CONST_INT_P (x) && oldmode != VOIDmode
764 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (oldmode))
766 HOST_WIDE_INT val = INTVAL (x);
767 int width = GET_MODE_BITSIZE (oldmode);
769 /* We must sign or zero-extend in this case. Start by
770 zero-extending, then sign extend if we need to. */
771 val &= ((HOST_WIDE_INT) 1 << width) - 1;
772 if (! unsignedp
773 && (val & ((HOST_WIDE_INT) 1 << (width - 1))))
774 val |= (HOST_WIDE_INT) (-1) << width;
776 return gen_int_mode (val, mode);
779 return gen_lowpart (mode, x);
782 /* Converting from integer constant into mode is always equivalent to an
783 subreg operation. */
784 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
786 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
787 return simplify_gen_subreg (mode, x, oldmode, 0);
790 temp = gen_reg_rtx (mode);
791 convert_move (temp, x, unsignedp);
792 return temp;
795 /* Return the largest alignment we can use for doing a move (or store)
796 of MAX_PIECES. ALIGN is the largest alignment we could use. */
798 static unsigned int
799 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
801 enum machine_mode tmode;
803 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
804 if (align >= GET_MODE_ALIGNMENT (tmode))
805 align = GET_MODE_ALIGNMENT (tmode);
806 else
808 enum machine_mode tmode, xmode;
810 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
811 tmode != VOIDmode;
812 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
813 if (GET_MODE_SIZE (tmode) > max_pieces
814 || SLOW_UNALIGNED_ACCESS (tmode, align))
815 break;
817 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
820 return align;
823 /* Return the widest integer mode no wider than SIZE. If no such mode
824 can be found, return VOIDmode. */
826 static enum machine_mode
827 widest_int_mode_for_size (unsigned int size)
829 enum machine_mode tmode, mode = VOIDmode;
831 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
832 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
833 if (GET_MODE_SIZE (tmode) < size)
834 mode = tmode;
836 return mode;
839 /* STORE_MAX_PIECES is the number of bytes at a time that we can
840 store efficiently. Due to internal GCC limitations, this is
841 MOVE_MAX_PIECES limited by the number of bytes GCC can represent
842 for an immediate constant. */
844 #define STORE_MAX_PIECES MIN (MOVE_MAX_PIECES, 2 * sizeof (HOST_WIDE_INT))
846 /* Determine whether the LEN bytes can be moved by using several move
847 instructions. Return nonzero if a call to move_by_pieces should
848 succeed. */
851 can_move_by_pieces (unsigned HOST_WIDE_INT len,
852 unsigned int align ATTRIBUTE_UNUSED)
854 return MOVE_BY_PIECES_P (len, align);
857 /* Generate several move instructions to copy LEN bytes from block FROM to
858 block TO. (These are MEM rtx's with BLKmode).
860 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
861 used to push FROM to the stack.
863 ALIGN is maximum stack alignment we can assume.
865 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
866 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
867 stpcpy. */
870 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
871 unsigned int align, int endp)
873 struct move_by_pieces_d data;
874 enum machine_mode to_addr_mode, from_addr_mode
875 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (from));
876 rtx to_addr, from_addr = XEXP (from, 0);
877 unsigned int max_size = MOVE_MAX_PIECES + 1;
878 enum insn_code icode;
880 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
882 data.offset = 0;
883 data.from_addr = from_addr;
884 if (to)
886 to_addr_mode = targetm.addr_space.address_mode (MEM_ADDR_SPACE (to));
887 to_addr = XEXP (to, 0);
888 data.to = to;
889 data.autinc_to
890 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
891 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
892 data.reverse
893 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
895 else
897 to_addr_mode = VOIDmode;
898 to_addr = NULL_RTX;
899 data.to = NULL_RTX;
900 data.autinc_to = 1;
901 #ifdef STACK_GROWS_DOWNWARD
902 data.reverse = 1;
903 #else
904 data.reverse = 0;
905 #endif
907 data.to_addr = to_addr;
908 data.from = from;
909 data.autinc_from
910 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
911 || GET_CODE (from_addr) == POST_INC
912 || GET_CODE (from_addr) == POST_DEC);
914 data.explicit_inc_from = 0;
915 data.explicit_inc_to = 0;
916 if (data.reverse) data.offset = len;
917 data.len = len;
919 /* If copying requires more than two move insns,
920 copy addresses to registers (to make displacements shorter)
921 and use post-increment if available. */
922 if (!(data.autinc_from && data.autinc_to)
923 && move_by_pieces_ninsns (len, align, max_size) > 2)
925 /* Find the mode of the largest move...
926 MODE might not be used depending on the definitions of the
927 USE_* macros below. */
928 enum machine_mode mode ATTRIBUTE_UNUSED
929 = widest_int_mode_for_size (max_size);
931 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
933 data.from_addr = copy_to_mode_reg (from_addr_mode,
934 plus_constant (from_addr, len));
935 data.autinc_from = 1;
936 data.explicit_inc_from = -1;
938 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
940 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
941 data.autinc_from = 1;
942 data.explicit_inc_from = 1;
944 if (!data.autinc_from && CONSTANT_P (from_addr))
945 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
946 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
948 data.to_addr = copy_to_mode_reg (to_addr_mode,
949 plus_constant (to_addr, len));
950 data.autinc_to = 1;
951 data.explicit_inc_to = -1;
953 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
955 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
956 data.autinc_to = 1;
957 data.explicit_inc_to = 1;
959 if (!data.autinc_to && CONSTANT_P (to_addr))
960 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
963 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
965 /* First move what we can in the largest integer mode, then go to
966 successively smaller modes. */
968 while (max_size > 1)
970 enum machine_mode mode = widest_int_mode_for_size (max_size);
972 if (mode == VOIDmode)
973 break;
975 icode = optab_handler (mov_optab, mode);
976 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
977 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
979 max_size = GET_MODE_SIZE (mode);
982 /* The code above should have handled everything. */
983 gcc_assert (!data.len);
985 if (endp)
987 rtx to1;
989 gcc_assert (!data.reverse);
990 if (data.autinc_to)
992 if (endp == 2)
994 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
995 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
996 else
997 data.to_addr = copy_to_mode_reg (to_addr_mode,
998 plus_constant (data.to_addr,
999 -1));
1001 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
1002 data.offset);
1004 else
1006 if (endp == 2)
1007 --data.offset;
1008 to1 = adjust_address (data.to, QImode, data.offset);
1010 return to1;
1012 else
1013 return data.to;
1016 /* Return number of insns required to move L bytes by pieces.
1017 ALIGN (in bits) is maximum alignment we can assume. */
1019 static unsigned HOST_WIDE_INT
1020 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1021 unsigned int max_size)
1023 unsigned HOST_WIDE_INT n_insns = 0;
1025 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1027 while (max_size > 1)
1029 enum machine_mode mode;
1030 enum insn_code icode;
1032 mode = widest_int_mode_for_size (max_size);
1034 if (mode == VOIDmode)
1035 break;
1037 icode = optab_handler (mov_optab, mode);
1038 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1039 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1041 max_size = GET_MODE_SIZE (mode);
1044 gcc_assert (!l);
1045 return n_insns;
1048 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1049 with move instructions for mode MODE. GENFUN is the gen_... function
1050 to make a move insn for that mode. DATA has all the other info. */
1052 static void
1053 move_by_pieces_1 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
1054 struct move_by_pieces_d *data)
1056 unsigned int size = GET_MODE_SIZE (mode);
1057 rtx to1 = NULL_RTX, from1;
1059 while (data->len >= size)
1061 if (data->reverse)
1062 data->offset -= size;
1064 if (data->to)
1066 if (data->autinc_to)
1067 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1068 data->offset);
1069 else
1070 to1 = adjust_address (data->to, mode, data->offset);
1073 if (data->autinc_from)
1074 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1075 data->offset);
1076 else
1077 from1 = adjust_address (data->from, mode, data->offset);
1079 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1080 emit_insn (gen_add2_insn (data->to_addr,
1081 GEN_INT (-(HOST_WIDE_INT)size)));
1082 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1083 emit_insn (gen_add2_insn (data->from_addr,
1084 GEN_INT (-(HOST_WIDE_INT)size)));
1086 if (data->to)
1087 emit_insn ((*genfun) (to1, from1));
1088 else
1090 #ifdef PUSH_ROUNDING
1091 emit_single_push_insn (mode, from1, NULL);
1092 #else
1093 gcc_unreachable ();
1094 #endif
1097 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1098 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
1099 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1100 emit_insn (gen_add2_insn (data->from_addr, GEN_INT (size)));
1102 if (! data->reverse)
1103 data->offset += size;
1105 data->len -= size;
1109 /* Emit code to move a block Y to a block X. This may be done with
1110 string-move instructions, with multiple scalar move instructions,
1111 or with a library call.
1113 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1114 SIZE is an rtx that says how long they are.
1115 ALIGN is the maximum alignment we can assume they have.
1116 METHOD describes what kind of copy this is, and what mechanisms may be used.
1118 Return the address of the new block, if memcpy is called and returns it,
1119 0 otherwise. */
1122 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1123 unsigned int expected_align, HOST_WIDE_INT expected_size)
1125 bool may_use_call;
1126 rtx retval = 0;
1127 unsigned int align;
1129 gcc_assert (size);
1130 if (CONST_INT_P (size)
1131 && INTVAL (size) == 0)
1132 return 0;
1134 switch (method)
1136 case BLOCK_OP_NORMAL:
1137 case BLOCK_OP_TAILCALL:
1138 may_use_call = true;
1139 break;
1141 case BLOCK_OP_CALL_PARM:
1142 may_use_call = block_move_libcall_safe_for_call_parm ();
1144 /* Make inhibit_defer_pop nonzero around the library call
1145 to force it to pop the arguments right away. */
1146 NO_DEFER_POP;
1147 break;
1149 case BLOCK_OP_NO_LIBCALL:
1150 may_use_call = false;
1151 break;
1153 default:
1154 gcc_unreachable ();
1157 gcc_assert (MEM_P (x) && MEM_P (y));
1158 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1159 gcc_assert (align >= BITS_PER_UNIT);
1161 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1162 block copy is more efficient for other large modes, e.g. DCmode. */
1163 x = adjust_address (x, BLKmode, 0);
1164 y = adjust_address (y, BLKmode, 0);
1166 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1167 can be incorrect is coming from __builtin_memcpy. */
1168 if (CONST_INT_P (size))
1170 x = shallow_copy_rtx (x);
1171 y = shallow_copy_rtx (y);
1172 set_mem_size (x, size);
1173 set_mem_size (y, size);
1176 if (CONST_INT_P (size) && MOVE_BY_PIECES_P (INTVAL (size), align))
1177 move_by_pieces (x, y, INTVAL (size), align, 0);
1178 else if (emit_block_move_via_movmem (x, y, size, align,
1179 expected_align, expected_size))
1181 else if (may_use_call
1182 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1183 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1185 /* Since x and y are passed to a libcall, mark the corresponding
1186 tree EXPR as addressable. */
1187 tree y_expr = MEM_EXPR (y);
1188 tree x_expr = MEM_EXPR (x);
1189 if (y_expr)
1190 mark_addressable (y_expr);
1191 if (x_expr)
1192 mark_addressable (x_expr);
1193 retval = emit_block_move_via_libcall (x, y, size,
1194 method == BLOCK_OP_TAILCALL);
1197 else
1198 emit_block_move_via_loop (x, y, size, align);
1200 if (method == BLOCK_OP_CALL_PARM)
1201 OK_DEFER_POP;
1203 return retval;
1207 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1209 return emit_block_move_hints (x, y, size, method, 0, -1);
1212 /* A subroutine of emit_block_move. Returns true if calling the
1213 block move libcall will not clobber any parameters which may have
1214 already been placed on the stack. */
1216 static bool
1217 block_move_libcall_safe_for_call_parm (void)
1219 #if defined (REG_PARM_STACK_SPACE)
1220 tree fn;
1221 #endif
1223 /* If arguments are pushed on the stack, then they're safe. */
1224 if (PUSH_ARGS)
1225 return true;
1227 /* If registers go on the stack anyway, any argument is sure to clobber
1228 an outgoing argument. */
1229 #if defined (REG_PARM_STACK_SPACE)
1230 fn = emit_block_move_libcall_fn (false);
1231 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1232 depend on its argument. */
1233 (void) fn;
1234 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1235 && REG_PARM_STACK_SPACE (fn) != 0)
1236 return false;
1237 #endif
1239 /* If any argument goes in memory, then it might clobber an outgoing
1240 argument. */
1242 CUMULATIVE_ARGS args_so_far;
1243 tree fn, arg;
1245 fn = emit_block_move_libcall_fn (false);
1246 INIT_CUMULATIVE_ARGS (args_so_far, TREE_TYPE (fn), NULL_RTX, 0, 3);
1248 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1249 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1251 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1252 rtx tmp = targetm.calls.function_arg (&args_so_far, mode,
1253 NULL_TREE, true);
1254 if (!tmp || !REG_P (tmp))
1255 return false;
1256 if (targetm.calls.arg_partial_bytes (&args_so_far, mode, NULL, 1))
1257 return false;
1258 targetm.calls.function_arg_advance (&args_so_far, mode,
1259 NULL_TREE, true);
1262 return true;
1265 /* A subroutine of emit_block_move. Expand a movmem pattern;
1266 return true if successful. */
1268 static bool
1269 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1270 unsigned int expected_align, HOST_WIDE_INT expected_size)
1272 rtx opalign = GEN_INT (align / BITS_PER_UNIT);
1273 int save_volatile_ok = volatile_ok;
1274 enum machine_mode mode;
1276 if (expected_align < align)
1277 expected_align = align;
1279 /* Since this is a move insn, we don't care about volatility. */
1280 volatile_ok = 1;
1282 /* Try the most limited insn first, because there's no point
1283 including more than one in the machine description unless
1284 the more limited one has some advantage. */
1286 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1287 mode = GET_MODE_WIDER_MODE (mode))
1289 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1290 insn_operand_predicate_fn pred;
1292 if (code != CODE_FOR_nothing
1293 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1294 here because if SIZE is less than the mode mask, as it is
1295 returned by the macro, it will definitely be less than the
1296 actual mode mask. */
1297 && ((CONST_INT_P (size)
1298 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1299 <= (GET_MODE_MASK (mode) >> 1)))
1300 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD)
1301 && ((pred = insn_data[(int) code].operand[0].predicate) == 0
1302 || (*pred) (x, BLKmode))
1303 && ((pred = insn_data[(int) code].operand[1].predicate) == 0
1304 || (*pred) (y, BLKmode))
1305 && ((pred = insn_data[(int) code].operand[3].predicate) == 0
1306 || (*pred) (opalign, VOIDmode)))
1308 rtx op2;
1309 rtx last = get_last_insn ();
1310 rtx pat;
1312 op2 = convert_to_mode (mode, size, 1);
1313 pred = insn_data[(int) code].operand[2].predicate;
1314 if (pred != 0 && ! (*pred) (op2, mode))
1315 op2 = copy_to_mode_reg (mode, op2);
1317 /* ??? When called via emit_block_move_for_call, it'd be
1318 nice if there were some way to inform the backend, so
1319 that it doesn't fail the expansion because it thinks
1320 emitting the libcall would be more efficient. */
1322 if (insn_data[(int) code].n_operands == 4)
1323 pat = GEN_FCN ((int) code) (x, y, op2, opalign);
1324 else
1325 pat = GEN_FCN ((int) code) (x, y, op2, opalign,
1326 GEN_INT (expected_align
1327 / BITS_PER_UNIT),
1328 GEN_INT (expected_size));
1329 if (pat)
1331 emit_insn (pat);
1332 volatile_ok = save_volatile_ok;
1333 return true;
1335 else
1336 delete_insns_since (last);
1340 volatile_ok = save_volatile_ok;
1341 return false;
1344 /* A subroutine of emit_block_move. Expand a call to memcpy.
1345 Return the return value from memcpy, 0 otherwise. */
1348 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1350 rtx dst_addr, src_addr;
1351 tree call_expr, fn, src_tree, dst_tree, size_tree;
1352 enum machine_mode size_mode;
1353 rtx retval;
1355 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1356 pseudos. We can then place those new pseudos into a VAR_DECL and
1357 use them later. */
1359 dst_addr = copy_to_mode_reg (Pmode, XEXP (dst, 0));
1360 src_addr = copy_to_mode_reg (Pmode, XEXP (src, 0));
1362 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1363 src_addr = convert_memory_address (ptr_mode, src_addr);
1365 dst_tree = make_tree (ptr_type_node, dst_addr);
1366 src_tree = make_tree (ptr_type_node, src_addr);
1368 size_mode = TYPE_MODE (sizetype);
1370 size = convert_to_mode (size_mode, size, 1);
1371 size = copy_to_mode_reg (size_mode, size);
1373 /* It is incorrect to use the libcall calling conventions to call
1374 memcpy in this context. This could be a user call to memcpy and
1375 the user may wish to examine the return value from memcpy. For
1376 targets where libcalls and normal calls have different conventions
1377 for returning pointers, we could end up generating incorrect code. */
1379 size_tree = make_tree (sizetype, size);
1381 fn = emit_block_move_libcall_fn (true);
1382 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1383 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1385 retval = expand_normal (call_expr);
1387 return retval;
1390 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1391 for the function we use for block copies. The first time FOR_CALL
1392 is true, we call assemble_external. */
1394 static GTY(()) tree block_move_fn;
1396 void
1397 init_block_move_fn (const char *asmspec)
1399 if (!block_move_fn)
1401 tree args, fn;
1403 fn = get_identifier ("memcpy");
1404 args = build_function_type_list (ptr_type_node, ptr_type_node,
1405 const_ptr_type_node, sizetype,
1406 NULL_TREE);
1408 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1409 DECL_EXTERNAL (fn) = 1;
1410 TREE_PUBLIC (fn) = 1;
1411 DECL_ARTIFICIAL (fn) = 1;
1412 TREE_NOTHROW (fn) = 1;
1413 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1414 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1416 block_move_fn = fn;
1419 if (asmspec)
1420 set_user_assembler_name (block_move_fn, asmspec);
1423 static tree
1424 emit_block_move_libcall_fn (int for_call)
1426 static bool emitted_extern;
1428 if (!block_move_fn)
1429 init_block_move_fn (NULL);
1431 if (for_call && !emitted_extern)
1433 emitted_extern = true;
1434 make_decl_rtl (block_move_fn);
1435 assemble_external (block_move_fn);
1438 return block_move_fn;
1441 /* A subroutine of emit_block_move. Copy the data via an explicit
1442 loop. This is used only when libcalls are forbidden. */
1443 /* ??? It'd be nice to copy in hunks larger than QImode. */
1445 static void
1446 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1447 unsigned int align ATTRIBUTE_UNUSED)
1449 rtx cmp_label, top_label, iter, x_addr, y_addr, tmp;
1450 enum machine_mode x_addr_mode
1451 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
1452 enum machine_mode y_addr_mode
1453 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (y));
1454 enum machine_mode iter_mode;
1456 iter_mode = GET_MODE (size);
1457 if (iter_mode == VOIDmode)
1458 iter_mode = word_mode;
1460 top_label = gen_label_rtx ();
1461 cmp_label = gen_label_rtx ();
1462 iter = gen_reg_rtx (iter_mode);
1464 emit_move_insn (iter, const0_rtx);
1466 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1467 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1468 do_pending_stack_adjust ();
1470 emit_jump (cmp_label);
1471 emit_label (top_label);
1473 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1474 x_addr = gen_rtx_PLUS (x_addr_mode, x_addr, tmp);
1476 if (x_addr_mode != y_addr_mode)
1477 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1478 y_addr = gen_rtx_PLUS (y_addr_mode, y_addr, tmp);
1480 x = change_address (x, QImode, x_addr);
1481 y = change_address (y, QImode, y_addr);
1483 emit_move_insn (x, y);
1485 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1486 true, OPTAB_LIB_WIDEN);
1487 if (tmp != iter)
1488 emit_move_insn (iter, tmp);
1490 emit_label (cmp_label);
1492 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1493 true, top_label);
1496 /* Copy all or part of a value X into registers starting at REGNO.
1497 The number of registers to be filled is NREGS. */
1499 void
1500 move_block_to_reg (int regno, rtx x, int nregs, enum machine_mode mode)
1502 int i;
1503 #ifdef HAVE_load_multiple
1504 rtx pat;
1505 rtx last;
1506 #endif
1508 if (nregs == 0)
1509 return;
1511 if (CONSTANT_P (x) && ! LEGITIMATE_CONSTANT_P (x))
1512 x = validize_mem (force_const_mem (mode, x));
1514 /* See if the machine can do this with a load multiple insn. */
1515 #ifdef HAVE_load_multiple
1516 if (HAVE_load_multiple)
1518 last = get_last_insn ();
1519 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1520 GEN_INT (nregs));
1521 if (pat)
1523 emit_insn (pat);
1524 return;
1526 else
1527 delete_insns_since (last);
1529 #endif
1531 for (i = 0; i < nregs; i++)
1532 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1533 operand_subword_force (x, i, mode));
1536 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1537 The number of registers to be filled is NREGS. */
1539 void
1540 move_block_from_reg (int regno, rtx x, int nregs)
1542 int i;
1544 if (nregs == 0)
1545 return;
1547 /* See if the machine can do this with a store multiple insn. */
1548 #ifdef HAVE_store_multiple
1549 if (HAVE_store_multiple)
1551 rtx last = get_last_insn ();
1552 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1553 GEN_INT (nregs));
1554 if (pat)
1556 emit_insn (pat);
1557 return;
1559 else
1560 delete_insns_since (last);
1562 #endif
1564 for (i = 0; i < nregs; i++)
1566 rtx tem = operand_subword (x, i, 1, BLKmode);
1568 gcc_assert (tem);
1570 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1574 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1575 ORIG, where ORIG is a non-consecutive group of registers represented by
1576 a PARALLEL. The clone is identical to the original except in that the
1577 original set of registers is replaced by a new set of pseudo registers.
1578 The new set has the same modes as the original set. */
1581 gen_group_rtx (rtx orig)
1583 int i, length;
1584 rtx *tmps;
1586 gcc_assert (GET_CODE (orig) == PARALLEL);
1588 length = XVECLEN (orig, 0);
1589 tmps = XALLOCAVEC (rtx, length);
1591 /* Skip a NULL entry in first slot. */
1592 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1594 if (i)
1595 tmps[0] = 0;
1597 for (; i < length; i++)
1599 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1600 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1602 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1605 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1608 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1609 except that values are placed in TMPS[i], and must later be moved
1610 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1612 static void
1613 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1615 rtx src;
1616 int start, i;
1617 enum machine_mode m = GET_MODE (orig_src);
1619 gcc_assert (GET_CODE (dst) == PARALLEL);
1621 if (m != VOIDmode
1622 && !SCALAR_INT_MODE_P (m)
1623 && !MEM_P (orig_src)
1624 && GET_CODE (orig_src) != CONCAT)
1626 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1627 if (imode == BLKmode)
1628 src = assign_stack_temp (GET_MODE (orig_src), ssize, 0);
1629 else
1630 src = gen_reg_rtx (imode);
1631 if (imode != BLKmode)
1632 src = gen_lowpart (GET_MODE (orig_src), src);
1633 emit_move_insn (src, orig_src);
1634 /* ...and back again. */
1635 if (imode != BLKmode)
1636 src = gen_lowpart (imode, src);
1637 emit_group_load_1 (tmps, dst, src, type, ssize);
1638 return;
1641 /* Check for a NULL entry, used to indicate that the parameter goes
1642 both on the stack and in registers. */
1643 if (XEXP (XVECEXP (dst, 0, 0), 0))
1644 start = 0;
1645 else
1646 start = 1;
1648 /* Process the pieces. */
1649 for (i = start; i < XVECLEN (dst, 0); i++)
1651 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1652 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1653 unsigned int bytelen = GET_MODE_SIZE (mode);
1654 int shift = 0;
1656 /* Handle trailing fragments that run over the size of the struct. */
1657 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1659 /* Arrange to shift the fragment to where it belongs.
1660 extract_bit_field loads to the lsb of the reg. */
1661 if (
1662 #ifdef BLOCK_REG_PADDING
1663 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1664 == (BYTES_BIG_ENDIAN ? upward : downward)
1665 #else
1666 BYTES_BIG_ENDIAN
1667 #endif
1669 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1670 bytelen = ssize - bytepos;
1671 gcc_assert (bytelen > 0);
1674 /* If we won't be loading directly from memory, protect the real source
1675 from strange tricks we might play; but make sure that the source can
1676 be loaded directly into the destination. */
1677 src = orig_src;
1678 if (!MEM_P (orig_src)
1679 && (!CONSTANT_P (orig_src)
1680 || (GET_MODE (orig_src) != mode
1681 && GET_MODE (orig_src) != VOIDmode)))
1683 if (GET_MODE (orig_src) == VOIDmode)
1684 src = gen_reg_rtx (mode);
1685 else
1686 src = gen_reg_rtx (GET_MODE (orig_src));
1688 emit_move_insn (src, orig_src);
1691 /* Optimize the access just a bit. */
1692 if (MEM_P (src)
1693 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1694 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1695 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1696 && bytelen == GET_MODE_SIZE (mode))
1698 tmps[i] = gen_reg_rtx (mode);
1699 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1701 else if (COMPLEX_MODE_P (mode)
1702 && GET_MODE (src) == mode
1703 && bytelen == GET_MODE_SIZE (mode))
1704 /* Let emit_move_complex do the bulk of the work. */
1705 tmps[i] = src;
1706 else if (GET_CODE (src) == CONCAT)
1708 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1709 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1711 if ((bytepos == 0 && bytelen == slen0)
1712 || (bytepos != 0 && bytepos + bytelen <= slen))
1714 /* The following assumes that the concatenated objects all
1715 have the same size. In this case, a simple calculation
1716 can be used to determine the object and the bit field
1717 to be extracted. */
1718 tmps[i] = XEXP (src, bytepos / slen0);
1719 if (! CONSTANT_P (tmps[i])
1720 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1721 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1722 (bytepos % slen0) * BITS_PER_UNIT,
1723 1, false, NULL_RTX, mode, mode);
1725 else
1727 rtx mem;
1729 gcc_assert (!bytepos);
1730 mem = assign_stack_temp (GET_MODE (src), slen, 0);
1731 emit_move_insn (mem, src);
1732 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1733 0, 1, false, NULL_RTX, mode, mode);
1736 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1737 SIMD register, which is currently broken. While we get GCC
1738 to emit proper RTL for these cases, let's dump to memory. */
1739 else if (VECTOR_MODE_P (GET_MODE (dst))
1740 && REG_P (src))
1742 int slen = GET_MODE_SIZE (GET_MODE (src));
1743 rtx mem;
1745 mem = assign_stack_temp (GET_MODE (src), slen, 0);
1746 emit_move_insn (mem, src);
1747 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1749 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1750 && XVECLEN (dst, 0) > 1)
1751 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE(dst), bytepos);
1752 else if (CONSTANT_P (src))
1754 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1756 if (len == ssize)
1757 tmps[i] = src;
1758 else
1760 rtx first, second;
1762 gcc_assert (2 * len == ssize);
1763 split_double (src, &first, &second);
1764 if (i)
1765 tmps[i] = second;
1766 else
1767 tmps[i] = first;
1770 else if (REG_P (src) && GET_MODE (src) == mode)
1771 tmps[i] = src;
1772 else
1773 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1774 bytepos * BITS_PER_UNIT, 1, false, NULL_RTX,
1775 mode, mode);
1777 if (shift)
1778 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1779 build_int_cst (NULL_TREE, shift), tmps[i], 0);
1783 /* Emit code to move a block SRC of type TYPE to a block DST,
1784 where DST is non-consecutive registers represented by a PARALLEL.
1785 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1786 if not known. */
1788 void
1789 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1791 rtx *tmps;
1792 int i;
1794 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1795 emit_group_load_1 (tmps, dst, src, type, ssize);
1797 /* Copy the extracted pieces into the proper (probable) hard regs. */
1798 for (i = 0; i < XVECLEN (dst, 0); i++)
1800 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1801 if (d == NULL)
1802 continue;
1803 emit_move_insn (d, tmps[i]);
1807 /* Similar, but load SRC into new pseudos in a format that looks like
1808 PARALLEL. This can later be fed to emit_group_move to get things
1809 in the right place. */
1812 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1814 rtvec vec;
1815 int i;
1817 vec = rtvec_alloc (XVECLEN (parallel, 0));
1818 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1820 /* Convert the vector to look just like the original PARALLEL, except
1821 with the computed values. */
1822 for (i = 0; i < XVECLEN (parallel, 0); i++)
1824 rtx e = XVECEXP (parallel, 0, i);
1825 rtx d = XEXP (e, 0);
1827 if (d)
1829 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1830 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1832 RTVEC_ELT (vec, i) = e;
1835 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1838 /* Emit code to move a block SRC to block DST, where SRC and DST are
1839 non-consecutive groups of registers, each represented by a PARALLEL. */
1841 void
1842 emit_group_move (rtx dst, rtx src)
1844 int i;
1846 gcc_assert (GET_CODE (src) == PARALLEL
1847 && GET_CODE (dst) == PARALLEL
1848 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1850 /* Skip first entry if NULL. */
1851 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1852 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1853 XEXP (XVECEXP (src, 0, i), 0));
1856 /* Move a group of registers represented by a PARALLEL into pseudos. */
1859 emit_group_move_into_temps (rtx src)
1861 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1862 int i;
1864 for (i = 0; i < XVECLEN (src, 0); i++)
1866 rtx e = XVECEXP (src, 0, i);
1867 rtx d = XEXP (e, 0);
1869 if (d)
1870 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1871 RTVEC_ELT (vec, i) = e;
1874 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1877 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1878 where SRC is non-consecutive registers represented by a PARALLEL.
1879 SSIZE represents the total size of block ORIG_DST, or -1 if not
1880 known. */
1882 void
1883 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1885 rtx *tmps, dst;
1886 int start, finish, i;
1887 enum machine_mode m = GET_MODE (orig_dst);
1889 gcc_assert (GET_CODE (src) == PARALLEL);
1891 if (!SCALAR_INT_MODE_P (m)
1892 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1894 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1895 if (imode == BLKmode)
1896 dst = assign_stack_temp (GET_MODE (orig_dst), ssize, 0);
1897 else
1898 dst = gen_reg_rtx (imode);
1899 emit_group_store (dst, src, type, ssize);
1900 if (imode != BLKmode)
1901 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1902 emit_move_insn (orig_dst, dst);
1903 return;
1906 /* Check for a NULL entry, used to indicate that the parameter goes
1907 both on the stack and in registers. */
1908 if (XEXP (XVECEXP (src, 0, 0), 0))
1909 start = 0;
1910 else
1911 start = 1;
1912 finish = XVECLEN (src, 0);
1914 tmps = XALLOCAVEC (rtx, finish);
1916 /* Copy the (probable) hard regs into pseudos. */
1917 for (i = start; i < finish; i++)
1919 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1920 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1922 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1923 emit_move_insn (tmps[i], reg);
1925 else
1926 tmps[i] = reg;
1929 /* If we won't be storing directly into memory, protect the real destination
1930 from strange tricks we might play. */
1931 dst = orig_dst;
1932 if (GET_CODE (dst) == PARALLEL)
1934 rtx temp;
1936 /* We can get a PARALLEL dst if there is a conditional expression in
1937 a return statement. In that case, the dst and src are the same,
1938 so no action is necessary. */
1939 if (rtx_equal_p (dst, src))
1940 return;
1942 /* It is unclear if we can ever reach here, but we may as well handle
1943 it. Allocate a temporary, and split this into a store/load to/from
1944 the temporary. */
1946 temp = assign_stack_temp (GET_MODE (dst), ssize, 0);
1947 emit_group_store (temp, src, type, ssize);
1948 emit_group_load (dst, temp, type, ssize);
1949 return;
1951 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1953 enum machine_mode outer = GET_MODE (dst);
1954 enum machine_mode inner;
1955 HOST_WIDE_INT bytepos;
1956 bool done = false;
1957 rtx temp;
1959 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1960 dst = gen_reg_rtx (outer);
1962 /* Make life a bit easier for combine. */
1963 /* If the first element of the vector is the low part
1964 of the destination mode, use a paradoxical subreg to
1965 initialize the destination. */
1966 if (start < finish)
1968 inner = GET_MODE (tmps[start]);
1969 bytepos = subreg_lowpart_offset (inner, outer);
1970 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1972 temp = simplify_gen_subreg (outer, tmps[start],
1973 inner, 0);
1974 if (temp)
1976 emit_move_insn (dst, temp);
1977 done = true;
1978 start++;
1983 /* If the first element wasn't the low part, try the last. */
1984 if (!done
1985 && start < finish - 1)
1987 inner = GET_MODE (tmps[finish - 1]);
1988 bytepos = subreg_lowpart_offset (inner, outer);
1989 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1991 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1992 inner, 0);
1993 if (temp)
1995 emit_move_insn (dst, temp);
1996 done = true;
1997 finish--;
2002 /* Otherwise, simply initialize the result to zero. */
2003 if (!done)
2004 emit_move_insn (dst, CONST0_RTX (outer));
2007 /* Process the pieces. */
2008 for (i = start; i < finish; i++)
2010 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2011 enum machine_mode mode = GET_MODE (tmps[i]);
2012 unsigned int bytelen = GET_MODE_SIZE (mode);
2013 unsigned int adj_bytelen = bytelen;
2014 rtx dest = dst;
2016 /* Handle trailing fragments that run over the size of the struct. */
2017 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2018 adj_bytelen = ssize - bytepos;
2020 if (GET_CODE (dst) == CONCAT)
2022 if (bytepos + adj_bytelen
2023 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2024 dest = XEXP (dst, 0);
2025 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2027 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2028 dest = XEXP (dst, 1);
2030 else
2032 enum machine_mode dest_mode = GET_MODE (dest);
2033 enum machine_mode tmp_mode = GET_MODE (tmps[i]);
2035 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2037 if (GET_MODE_ALIGNMENT (dest_mode)
2038 >= GET_MODE_ALIGNMENT (tmp_mode))
2040 dest = assign_stack_temp (dest_mode,
2041 GET_MODE_SIZE (dest_mode),
2043 emit_move_insn (adjust_address (dest,
2044 tmp_mode,
2045 bytepos),
2046 tmps[i]);
2047 dst = dest;
2049 else
2051 dest = assign_stack_temp (tmp_mode,
2052 GET_MODE_SIZE (tmp_mode),
2054 emit_move_insn (dest, tmps[i]);
2055 dst = adjust_address (dest, dest_mode, bytepos);
2057 break;
2061 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2063 /* store_bit_field always takes its value from the lsb.
2064 Move the fragment to the lsb if it's not already there. */
2065 if (
2066 #ifdef BLOCK_REG_PADDING
2067 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2068 == (BYTES_BIG_ENDIAN ? upward : downward)
2069 #else
2070 BYTES_BIG_ENDIAN
2071 #endif
2074 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2075 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2076 build_int_cst (NULL_TREE, shift),
2077 tmps[i], 0);
2079 bytelen = adj_bytelen;
2082 /* Optimize the access just a bit. */
2083 if (MEM_P (dest)
2084 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2085 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2086 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2087 && bytelen == GET_MODE_SIZE (mode))
2088 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2089 else
2090 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2091 mode, tmps[i]);
2094 /* Copy from the pseudo into the (probable) hard reg. */
2095 if (orig_dst != dst)
2096 emit_move_insn (orig_dst, dst);
2099 /* Generate code to copy a BLKmode object of TYPE out of a
2100 set of registers starting with SRCREG into TGTBLK. If TGTBLK
2101 is null, a stack temporary is created. TGTBLK is returned.
2103 The purpose of this routine is to handle functions that return
2104 BLKmode structures in registers. Some machines (the PA for example)
2105 want to return all small structures in registers regardless of the
2106 structure's alignment. */
2109 copy_blkmode_from_reg (rtx tgtblk, rtx srcreg, tree type)
2111 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2112 rtx src = NULL, dst = NULL;
2113 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2114 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2115 enum machine_mode copy_mode;
2117 if (tgtblk == 0)
2119 tgtblk = assign_temp (build_qualified_type (type,
2120 (TYPE_QUALS (type)
2121 | TYPE_QUAL_CONST)),
2122 0, 1, 1);
2123 preserve_temp_slots (tgtblk);
2126 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2127 into a new pseudo which is a full word. */
2129 if (GET_MODE (srcreg) != BLKmode
2130 && GET_MODE_SIZE (GET_MODE (srcreg)) < UNITS_PER_WORD)
2131 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2133 /* If the structure doesn't take up a whole number of words, see whether
2134 SRCREG is padded on the left or on the right. If it's on the left,
2135 set PADDING_CORRECTION to the number of bits to skip.
2137 In most ABIs, the structure will be returned at the least end of
2138 the register, which translates to right padding on little-endian
2139 targets and left padding on big-endian targets. The opposite
2140 holds if the structure is returned at the most significant
2141 end of the register. */
2142 if (bytes % UNITS_PER_WORD != 0
2143 && (targetm.calls.return_in_msb (type)
2144 ? !BYTES_BIG_ENDIAN
2145 : BYTES_BIG_ENDIAN))
2146 padding_correction
2147 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2149 /* Copy the structure BITSIZE bits at a time. If the target lives in
2150 memory, take care of not reading/writing past its end by selecting
2151 a copy mode suited to BITSIZE. This should always be possible given
2152 how it is computed.
2154 We could probably emit more efficient code for machines which do not use
2155 strict alignment, but it doesn't seem worth the effort at the current
2156 time. */
2158 copy_mode = word_mode;
2159 if (MEM_P (tgtblk))
2161 enum machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2162 if (mem_mode != BLKmode)
2163 copy_mode = mem_mode;
2166 for (bitpos = 0, xbitpos = padding_correction;
2167 bitpos < bytes * BITS_PER_UNIT;
2168 bitpos += bitsize, xbitpos += bitsize)
2170 /* We need a new source operand each time xbitpos is on a
2171 word boundary and when xbitpos == padding_correction
2172 (the first time through). */
2173 if (xbitpos % BITS_PER_WORD == 0
2174 || xbitpos == padding_correction)
2175 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD,
2176 GET_MODE (srcreg));
2178 /* We need a new destination operand each time bitpos is on
2179 a word boundary. */
2180 if (bitpos % BITS_PER_WORD == 0)
2181 dst = operand_subword (tgtblk, bitpos / BITS_PER_WORD, 1, BLKmode);
2183 /* Use xbitpos for the source extraction (right justified) and
2184 bitpos for the destination store (left justified). */
2185 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, copy_mode,
2186 extract_bit_field (src, bitsize,
2187 xbitpos % BITS_PER_WORD, 1, false,
2188 NULL_RTX, copy_mode, copy_mode));
2191 return tgtblk;
2194 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2195 register if it contains any data, otherwise return null.
2197 This is used on targets that return BLKmode values in registers. */
2200 copy_blkmode_to_reg (enum machine_mode mode, tree src)
2202 int i, n_regs;
2203 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2204 unsigned int bitsize;
2205 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2206 enum machine_mode dst_mode;
2208 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2210 x = expand_normal (src);
2212 bytes = int_size_in_bytes (TREE_TYPE (src));
2213 if (bytes == 0)
2214 return NULL_RTX;
2216 /* If the structure doesn't take up a whole number of words, see
2217 whether the register value should be padded on the left or on
2218 the right. Set PADDING_CORRECTION to the number of padding
2219 bits needed on the left side.
2221 In most ABIs, the structure will be returned at the least end of
2222 the register, which translates to right padding on little-endian
2223 targets and left padding on big-endian targets. The opposite
2224 holds if the structure is returned at the most significant
2225 end of the register. */
2226 if (bytes % UNITS_PER_WORD != 0
2227 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2228 ? !BYTES_BIG_ENDIAN
2229 : BYTES_BIG_ENDIAN))
2230 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2231 * BITS_PER_UNIT));
2233 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2234 dst_words = XALLOCAVEC (rtx, n_regs);
2235 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2237 /* Copy the structure BITSIZE bits at a time. */
2238 for (bitpos = 0, xbitpos = padding_correction;
2239 bitpos < bytes * BITS_PER_UNIT;
2240 bitpos += bitsize, xbitpos += bitsize)
2242 /* We need a new destination pseudo each time xbitpos is
2243 on a word boundary and when xbitpos == padding_correction
2244 (the first time through). */
2245 if (xbitpos % BITS_PER_WORD == 0
2246 || xbitpos == padding_correction)
2248 /* Generate an appropriate register. */
2249 dst_word = gen_reg_rtx (word_mode);
2250 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2252 /* Clear the destination before we move anything into it. */
2253 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2256 /* We need a new source operand each time bitpos is on a word
2257 boundary. */
2258 if (bitpos % BITS_PER_WORD == 0)
2259 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2261 /* Use bitpos for the source extraction (left justified) and
2262 xbitpos for the destination store (right justified). */
2263 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD, word_mode,
2264 extract_bit_field (src_word, bitsize,
2265 bitpos % BITS_PER_WORD, 1, false,
2266 NULL_RTX, word_mode, word_mode));
2269 if (mode == BLKmode)
2271 /* Find the smallest integer mode large enough to hold the
2272 entire structure. */
2273 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2274 mode != VOIDmode;
2275 mode = GET_MODE_WIDER_MODE (mode))
2276 /* Have we found a large enough mode? */
2277 if (GET_MODE_SIZE (mode) >= bytes)
2278 break;
2280 /* A suitable mode should have been found. */
2281 gcc_assert (mode != VOIDmode);
2284 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2285 dst_mode = word_mode;
2286 else
2287 dst_mode = mode;
2288 dst = gen_reg_rtx (dst_mode);
2290 for (i = 0; i < n_regs; i++)
2291 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2293 if (mode != dst_mode)
2294 dst = gen_lowpart (mode, dst);
2296 return dst;
2299 /* Add a USE expression for REG to the (possibly empty) list pointed
2300 to by CALL_FUSAGE. REG must denote a hard register. */
2302 void
2303 use_reg (rtx *call_fusage, rtx reg)
2305 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2307 *call_fusage
2308 = gen_rtx_EXPR_LIST (VOIDmode,
2309 gen_rtx_USE (VOIDmode, reg), *call_fusage);
2312 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2313 starting at REGNO. All of these registers must be hard registers. */
2315 void
2316 use_regs (rtx *call_fusage, int regno, int nregs)
2318 int i;
2320 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2322 for (i = 0; i < nregs; i++)
2323 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2326 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2327 PARALLEL REGS. This is for calls that pass values in multiple
2328 non-contiguous locations. The Irix 6 ABI has examples of this. */
2330 void
2331 use_group_regs (rtx *call_fusage, rtx regs)
2333 int i;
2335 for (i = 0; i < XVECLEN (regs, 0); i++)
2337 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2339 /* A NULL entry means the parameter goes both on the stack and in
2340 registers. This can also be a MEM for targets that pass values
2341 partially on the stack and partially in registers. */
2342 if (reg != 0 && REG_P (reg))
2343 use_reg (call_fusage, reg);
2347 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2348 assigment and the code of the expresion on the RHS is CODE. Return
2349 NULL otherwise. */
2351 static gimple
2352 get_def_for_expr (tree name, enum tree_code code)
2354 gimple def_stmt;
2356 if (TREE_CODE (name) != SSA_NAME)
2357 return NULL;
2359 def_stmt = get_gimple_for_ssa_name (name);
2360 if (!def_stmt
2361 || gimple_assign_rhs_code (def_stmt) != code)
2362 return NULL;
2364 return def_stmt;
2368 /* Determine whether the LEN bytes generated by CONSTFUN can be
2369 stored to memory using several move instructions. CONSTFUNDATA is
2370 a pointer which will be passed as argument in every CONSTFUN call.
2371 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2372 a memset operation and false if it's a copy of a constant string.
2373 Return nonzero if a call to store_by_pieces should succeed. */
2376 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2377 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2378 void *constfundata, unsigned int align, bool memsetp)
2380 unsigned HOST_WIDE_INT l;
2381 unsigned int max_size;
2382 HOST_WIDE_INT offset = 0;
2383 enum machine_mode mode;
2384 enum insn_code icode;
2385 int reverse;
2386 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2387 rtx cst ATTRIBUTE_UNUSED;
2389 if (len == 0)
2390 return 1;
2392 if (! (memsetp
2393 ? SET_BY_PIECES_P (len, align)
2394 : STORE_BY_PIECES_P (len, align)))
2395 return 0;
2397 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2399 /* We would first store what we can in the largest integer mode, then go to
2400 successively smaller modes. */
2402 for (reverse = 0;
2403 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2404 reverse++)
2406 l = len;
2407 max_size = STORE_MAX_PIECES + 1;
2408 while (max_size > 1)
2410 mode = widest_int_mode_for_size (max_size);
2412 if (mode == VOIDmode)
2413 break;
2415 icode = optab_handler (mov_optab, mode);
2416 if (icode != CODE_FOR_nothing
2417 && align >= GET_MODE_ALIGNMENT (mode))
2419 unsigned int size = GET_MODE_SIZE (mode);
2421 while (l >= size)
2423 if (reverse)
2424 offset -= size;
2426 cst = (*constfun) (constfundata, offset, mode);
2427 if (!LEGITIMATE_CONSTANT_P (cst))
2428 return 0;
2430 if (!reverse)
2431 offset += size;
2433 l -= size;
2437 max_size = GET_MODE_SIZE (mode);
2440 /* The code above should have handled everything. */
2441 gcc_assert (!l);
2444 return 1;
2447 /* Generate several move instructions to store LEN bytes generated by
2448 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2449 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 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2453 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2454 stpcpy. */
2457 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2458 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2459 void *constfundata, unsigned int align, bool memsetp, int endp)
2461 enum machine_mode to_addr_mode
2462 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (to));
2463 struct store_by_pieces_d data;
2465 if (len == 0)
2467 gcc_assert (endp != 2);
2468 return to;
2471 gcc_assert (memsetp
2472 ? SET_BY_PIECES_P (len, align)
2473 : STORE_BY_PIECES_P (len, align));
2474 data.constfun = constfun;
2475 data.constfundata = constfundata;
2476 data.len = len;
2477 data.to = to;
2478 store_by_pieces_1 (&data, align);
2479 if (endp)
2481 rtx to1;
2483 gcc_assert (!data.reverse);
2484 if (data.autinc_to)
2486 if (endp == 2)
2488 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2489 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2490 else
2491 data.to_addr = copy_to_mode_reg (to_addr_mode,
2492 plus_constant (data.to_addr,
2493 -1));
2495 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2496 data.offset);
2498 else
2500 if (endp == 2)
2501 --data.offset;
2502 to1 = adjust_address (data.to, QImode, data.offset);
2504 return to1;
2506 else
2507 return data.to;
2510 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2511 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2513 static void
2514 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2516 struct store_by_pieces_d data;
2518 if (len == 0)
2519 return;
2521 data.constfun = clear_by_pieces_1;
2522 data.constfundata = NULL;
2523 data.len = len;
2524 data.to = to;
2525 store_by_pieces_1 (&data, align);
2528 /* Callback routine for clear_by_pieces.
2529 Return const0_rtx unconditionally. */
2531 static rtx
2532 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2533 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2534 enum machine_mode mode ATTRIBUTE_UNUSED)
2536 return const0_rtx;
2539 /* Subroutine of clear_by_pieces and store_by_pieces.
2540 Generate several move instructions to store LEN bytes of block TO. (A MEM
2541 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2543 static void
2544 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2545 unsigned int align ATTRIBUTE_UNUSED)
2547 enum machine_mode to_addr_mode
2548 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (data->to));
2549 rtx to_addr = XEXP (data->to, 0);
2550 unsigned int max_size = STORE_MAX_PIECES + 1;
2551 enum insn_code icode;
2553 data->offset = 0;
2554 data->to_addr = to_addr;
2555 data->autinc_to
2556 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2557 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2559 data->explicit_inc_to = 0;
2560 data->reverse
2561 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2562 if (data->reverse)
2563 data->offset = data->len;
2565 /* If storing requires more than two move insns,
2566 copy addresses to registers (to make displacements shorter)
2567 and use post-increment if available. */
2568 if (!data->autinc_to
2569 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2571 /* Determine the main mode we'll be using.
2572 MODE might not be used depending on the definitions of the
2573 USE_* macros below. */
2574 enum machine_mode mode ATTRIBUTE_UNUSED
2575 = widest_int_mode_for_size (max_size);
2577 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2579 data->to_addr = copy_to_mode_reg (to_addr_mode,
2580 plus_constant (to_addr, data->len));
2581 data->autinc_to = 1;
2582 data->explicit_inc_to = -1;
2585 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2586 && ! data->autinc_to)
2588 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2589 data->autinc_to = 1;
2590 data->explicit_inc_to = 1;
2593 if ( !data->autinc_to && CONSTANT_P (to_addr))
2594 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2597 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2599 /* First store what we can in the largest integer mode, then go to
2600 successively smaller modes. */
2602 while (max_size > 1)
2604 enum machine_mode mode = widest_int_mode_for_size (max_size);
2606 if (mode == VOIDmode)
2607 break;
2609 icode = optab_handler (mov_optab, mode);
2610 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2611 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2613 max_size = GET_MODE_SIZE (mode);
2616 /* The code above should have handled everything. */
2617 gcc_assert (!data->len);
2620 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2621 with move instructions for mode MODE. GENFUN is the gen_... function
2622 to make a move insn for that mode. DATA has all the other info. */
2624 static void
2625 store_by_pieces_2 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
2626 struct store_by_pieces_d *data)
2628 unsigned int size = GET_MODE_SIZE (mode);
2629 rtx to1, cst;
2631 while (data->len >= size)
2633 if (data->reverse)
2634 data->offset -= size;
2636 if (data->autinc_to)
2637 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2638 data->offset);
2639 else
2640 to1 = adjust_address (data->to, mode, data->offset);
2642 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2643 emit_insn (gen_add2_insn (data->to_addr,
2644 GEN_INT (-(HOST_WIDE_INT) size)));
2646 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2647 emit_insn ((*genfun) (to1, cst));
2649 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2650 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
2652 if (! data->reverse)
2653 data->offset += size;
2655 data->len -= size;
2659 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2660 its length in bytes. */
2663 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2664 unsigned int expected_align, HOST_WIDE_INT expected_size)
2666 enum machine_mode mode = GET_MODE (object);
2667 unsigned int align;
2669 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2671 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2672 just move a zero. Otherwise, do this a piece at a time. */
2673 if (mode != BLKmode
2674 && CONST_INT_P (size)
2675 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2677 rtx zero = CONST0_RTX (mode);
2678 if (zero != NULL)
2680 emit_move_insn (object, zero);
2681 return NULL;
2684 if (COMPLEX_MODE_P (mode))
2686 zero = CONST0_RTX (GET_MODE_INNER (mode));
2687 if (zero != NULL)
2689 write_complex_part (object, zero, 0);
2690 write_complex_part (object, zero, 1);
2691 return NULL;
2696 if (size == const0_rtx)
2697 return NULL;
2699 align = MEM_ALIGN (object);
2701 if (CONST_INT_P (size)
2702 && CLEAR_BY_PIECES_P (INTVAL (size), align))
2703 clear_by_pieces (object, INTVAL (size), align);
2704 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2705 expected_align, expected_size))
2707 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2708 return set_storage_via_libcall (object, size, const0_rtx,
2709 method == BLOCK_OP_TAILCALL);
2710 else
2711 gcc_unreachable ();
2713 return NULL;
2717 clear_storage (rtx object, rtx size, enum block_op_methods method)
2719 return clear_storage_hints (object, size, method, 0, -1);
2723 /* A subroutine of clear_storage. Expand a call to memset.
2724 Return the return value of memset, 0 otherwise. */
2727 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2729 tree call_expr, fn, object_tree, size_tree, val_tree;
2730 enum machine_mode size_mode;
2731 rtx retval;
2733 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2734 place those into new pseudos into a VAR_DECL and use them later. */
2736 object = copy_to_mode_reg (Pmode, XEXP (object, 0));
2738 size_mode = TYPE_MODE (sizetype);
2739 size = convert_to_mode (size_mode, size, 1);
2740 size = copy_to_mode_reg (size_mode, size);
2742 /* It is incorrect to use the libcall calling conventions to call
2743 memset in this context. This could be a user call to memset and
2744 the user may wish to examine the return value from memset. For
2745 targets where libcalls and normal calls have different conventions
2746 for returning pointers, we could end up generating incorrect code. */
2748 object_tree = make_tree (ptr_type_node, object);
2749 if (!CONST_INT_P (val))
2750 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2751 size_tree = make_tree (sizetype, size);
2752 val_tree = make_tree (integer_type_node, val);
2754 fn = clear_storage_libcall_fn (true);
2755 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2756 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2758 retval = expand_normal (call_expr);
2760 return retval;
2763 /* A subroutine of set_storage_via_libcall. Create the tree node
2764 for the function we use for block clears. The first time FOR_CALL
2765 is true, we call assemble_external. */
2767 tree block_clear_fn;
2769 void
2770 init_block_clear_fn (const char *asmspec)
2772 if (!block_clear_fn)
2774 tree fn, args;
2776 fn = get_identifier ("memset");
2777 args = build_function_type_list (ptr_type_node, ptr_type_node,
2778 integer_type_node, sizetype,
2779 NULL_TREE);
2781 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2782 DECL_EXTERNAL (fn) = 1;
2783 TREE_PUBLIC (fn) = 1;
2784 DECL_ARTIFICIAL (fn) = 1;
2785 TREE_NOTHROW (fn) = 1;
2786 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2787 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2789 block_clear_fn = fn;
2792 if (asmspec)
2793 set_user_assembler_name (block_clear_fn, asmspec);
2796 static tree
2797 clear_storage_libcall_fn (int for_call)
2799 static bool emitted_extern;
2801 if (!block_clear_fn)
2802 init_block_clear_fn (NULL);
2804 if (for_call && !emitted_extern)
2806 emitted_extern = true;
2807 make_decl_rtl (block_clear_fn);
2808 assemble_external (block_clear_fn);
2811 return block_clear_fn;
2814 /* Expand a setmem pattern; return true if successful. */
2816 bool
2817 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2818 unsigned int expected_align, HOST_WIDE_INT expected_size)
2820 /* Try the most limited insn first, because there's no point
2821 including more than one in the machine description unless
2822 the more limited one has some advantage. */
2824 rtx opalign = GEN_INT (align / BITS_PER_UNIT);
2825 enum machine_mode mode;
2827 if (expected_align < align)
2828 expected_align = align;
2830 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2831 mode = GET_MODE_WIDER_MODE (mode))
2833 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2834 insn_operand_predicate_fn pred;
2836 if (code != CODE_FOR_nothing
2837 /* We don't need MODE to be narrower than
2838 BITS_PER_HOST_WIDE_INT here because if SIZE is less than
2839 the mode mask, as it is returned by the macro, it will
2840 definitely be less than the actual mode mask. */
2841 && ((CONST_INT_P (size)
2842 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2843 <= (GET_MODE_MASK (mode) >> 1)))
2844 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD)
2845 && ((pred = insn_data[(int) code].operand[0].predicate) == 0
2846 || (*pred) (object, BLKmode))
2847 && ((pred = insn_data[(int) code].operand[3].predicate) == 0
2848 || (*pred) (opalign, VOIDmode)))
2850 rtx opsize, opchar;
2851 enum machine_mode char_mode;
2852 rtx last = get_last_insn ();
2853 rtx pat;
2855 opsize = convert_to_mode (mode, size, 1);
2856 pred = insn_data[(int) code].operand[1].predicate;
2857 if (pred != 0 && ! (*pred) (opsize, mode))
2858 opsize = copy_to_mode_reg (mode, opsize);
2860 opchar = val;
2861 char_mode = insn_data[(int) code].operand[2].mode;
2862 if (char_mode != VOIDmode)
2864 opchar = convert_to_mode (char_mode, opchar, 1);
2865 pred = insn_data[(int) code].operand[2].predicate;
2866 if (pred != 0 && ! (*pred) (opchar, char_mode))
2867 opchar = copy_to_mode_reg (char_mode, opchar);
2870 if (insn_data[(int) code].n_operands == 4)
2871 pat = GEN_FCN ((int) code) (object, opsize, opchar, opalign);
2872 else
2873 pat = GEN_FCN ((int) code) (object, opsize, opchar, opalign,
2874 GEN_INT (expected_align
2875 / BITS_PER_UNIT),
2876 GEN_INT (expected_size));
2877 if (pat)
2879 emit_insn (pat);
2880 return true;
2882 else
2883 delete_insns_since (last);
2887 return false;
2891 /* Write to one of the components of the complex value CPLX. Write VAL to
2892 the real part if IMAG_P is false, and the imaginary part if its true. */
2894 static void
2895 write_complex_part (rtx cplx, rtx val, bool imag_p)
2897 enum machine_mode cmode;
2898 enum machine_mode imode;
2899 unsigned ibitsize;
2901 if (GET_CODE (cplx) == CONCAT)
2903 emit_move_insn (XEXP (cplx, imag_p), val);
2904 return;
2907 cmode = GET_MODE (cplx);
2908 imode = GET_MODE_INNER (cmode);
2909 ibitsize = GET_MODE_BITSIZE (imode);
2911 /* For MEMs simplify_gen_subreg may generate an invalid new address
2912 because, e.g., the original address is considered mode-dependent
2913 by the target, which restricts simplify_subreg from invoking
2914 adjust_address_nv. Instead of preparing fallback support for an
2915 invalid address, we call adjust_address_nv directly. */
2916 if (MEM_P (cplx))
2918 emit_move_insn (adjust_address_nv (cplx, imode,
2919 imag_p ? GET_MODE_SIZE (imode) : 0),
2920 val);
2921 return;
2924 /* If the sub-object is at least word sized, then we know that subregging
2925 will work. This special case is important, since store_bit_field
2926 wants to operate on integer modes, and there's rarely an OImode to
2927 correspond to TCmode. */
2928 if (ibitsize >= BITS_PER_WORD
2929 /* For hard regs we have exact predicates. Assume we can split
2930 the original object if it spans an even number of hard regs.
2931 This special case is important for SCmode on 64-bit platforms
2932 where the natural size of floating-point regs is 32-bit. */
2933 || (REG_P (cplx)
2934 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
2935 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
2937 rtx part = simplify_gen_subreg (imode, cplx, cmode,
2938 imag_p ? GET_MODE_SIZE (imode) : 0);
2939 if (part)
2941 emit_move_insn (part, val);
2942 return;
2944 else
2945 /* simplify_gen_subreg may fail for sub-word MEMs. */
2946 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
2949 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, imode, val);
2952 /* Extract one of the components of the complex value CPLX. Extract the
2953 real part if IMAG_P is false, and the imaginary part if it's true. */
2955 static rtx
2956 read_complex_part (rtx cplx, bool imag_p)
2958 enum machine_mode cmode, imode;
2959 unsigned ibitsize;
2961 if (GET_CODE (cplx) == CONCAT)
2962 return XEXP (cplx, imag_p);
2964 cmode = GET_MODE (cplx);
2965 imode = GET_MODE_INNER (cmode);
2966 ibitsize = GET_MODE_BITSIZE (imode);
2968 /* Special case reads from complex constants that got spilled to memory. */
2969 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
2971 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
2972 if (decl && TREE_CODE (decl) == COMPLEX_CST)
2974 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
2975 if (CONSTANT_CLASS_P (part))
2976 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
2980 /* For MEMs simplify_gen_subreg may generate an invalid new address
2981 because, e.g., the original address is considered mode-dependent
2982 by the target, which restricts simplify_subreg from invoking
2983 adjust_address_nv. Instead of preparing fallback support for an
2984 invalid address, we call adjust_address_nv directly. */
2985 if (MEM_P (cplx))
2986 return adjust_address_nv (cplx, imode,
2987 imag_p ? GET_MODE_SIZE (imode) : 0);
2989 /* If the sub-object is at least word sized, then we know that subregging
2990 will work. This special case is important, since extract_bit_field
2991 wants to operate on integer modes, and there's rarely an OImode to
2992 correspond to TCmode. */
2993 if (ibitsize >= BITS_PER_WORD
2994 /* For hard regs we have exact predicates. Assume we can split
2995 the original object if it spans an even number of hard regs.
2996 This special case is important for SCmode on 64-bit platforms
2997 where the natural size of floating-point regs is 32-bit. */
2998 || (REG_P (cplx)
2999 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3000 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3002 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3003 imag_p ? GET_MODE_SIZE (imode) : 0);
3004 if (ret)
3005 return ret;
3006 else
3007 /* simplify_gen_subreg may fail for sub-word MEMs. */
3008 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3011 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3012 true, false, NULL_RTX, imode, imode);
3015 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3016 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3017 represented in NEW_MODE. If FORCE is true, this will never happen, as
3018 we'll force-create a SUBREG if needed. */
3020 static rtx
3021 emit_move_change_mode (enum machine_mode new_mode,
3022 enum machine_mode old_mode, rtx x, bool force)
3024 rtx ret;
3026 if (push_operand (x, GET_MODE (x)))
3028 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3029 MEM_COPY_ATTRIBUTES (ret, x);
3031 else if (MEM_P (x))
3033 /* We don't have to worry about changing the address since the
3034 size in bytes is supposed to be the same. */
3035 if (reload_in_progress)
3037 /* Copy the MEM to change the mode and move any
3038 substitutions from the old MEM to the new one. */
3039 ret = adjust_address_nv (x, new_mode, 0);
3040 copy_replacements (x, ret);
3042 else
3043 ret = adjust_address (x, new_mode, 0);
3045 else
3047 /* Note that we do want simplify_subreg's behavior of validating
3048 that the new mode is ok for a hard register. If we were to use
3049 simplify_gen_subreg, we would create the subreg, but would
3050 probably run into the target not being able to implement it. */
3051 /* Except, of course, when FORCE is true, when this is exactly what
3052 we want. Which is needed for CCmodes on some targets. */
3053 if (force)
3054 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3055 else
3056 ret = simplify_subreg (new_mode, x, old_mode, 0);
3059 return ret;
3062 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3063 an integer mode of the same size as MODE. Returns the instruction
3064 emitted, or NULL if such a move could not be generated. */
3066 static rtx
3067 emit_move_via_integer (enum machine_mode mode, rtx x, rtx y, bool force)
3069 enum machine_mode imode;
3070 enum insn_code code;
3072 /* There must exist a mode of the exact size we require. */
3073 imode = int_mode_for_mode (mode);
3074 if (imode == BLKmode)
3075 return NULL_RTX;
3077 /* The target must support moves in this mode. */
3078 code = optab_handler (mov_optab, imode);
3079 if (code == CODE_FOR_nothing)
3080 return NULL_RTX;
3082 x = emit_move_change_mode (imode, mode, x, force);
3083 if (x == NULL_RTX)
3084 return NULL_RTX;
3085 y = emit_move_change_mode (imode, mode, y, force);
3086 if (y == NULL_RTX)
3087 return NULL_RTX;
3088 return emit_insn (GEN_FCN (code) (x, y));
3091 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3092 Return an equivalent MEM that does not use an auto-increment. */
3094 static rtx
3095 emit_move_resolve_push (enum machine_mode mode, rtx x)
3097 enum rtx_code code = GET_CODE (XEXP (x, 0));
3098 HOST_WIDE_INT adjust;
3099 rtx temp;
3101 adjust = GET_MODE_SIZE (mode);
3102 #ifdef PUSH_ROUNDING
3103 adjust = PUSH_ROUNDING (adjust);
3104 #endif
3105 if (code == PRE_DEC || code == POST_DEC)
3106 adjust = -adjust;
3107 else if (code == PRE_MODIFY || code == POST_MODIFY)
3109 rtx expr = XEXP (XEXP (x, 0), 1);
3110 HOST_WIDE_INT val;
3112 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3113 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3114 val = INTVAL (XEXP (expr, 1));
3115 if (GET_CODE (expr) == MINUS)
3116 val = -val;
3117 gcc_assert (adjust == val || adjust == -val);
3118 adjust = val;
3121 /* Do not use anti_adjust_stack, since we don't want to update
3122 stack_pointer_delta. */
3123 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3124 GEN_INT (adjust), stack_pointer_rtx,
3125 0, OPTAB_LIB_WIDEN);
3126 if (temp != stack_pointer_rtx)
3127 emit_move_insn (stack_pointer_rtx, temp);
3129 switch (code)
3131 case PRE_INC:
3132 case PRE_DEC:
3133 case PRE_MODIFY:
3134 temp = stack_pointer_rtx;
3135 break;
3136 case POST_INC:
3137 case POST_DEC:
3138 case POST_MODIFY:
3139 temp = plus_constant (stack_pointer_rtx, -adjust);
3140 break;
3141 default:
3142 gcc_unreachable ();
3145 return replace_equiv_address (x, temp);
3148 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3149 X is known to satisfy push_operand, and MODE is known to be complex.
3150 Returns the last instruction emitted. */
3153 emit_move_complex_push (enum machine_mode mode, rtx x, rtx y)
3155 enum machine_mode submode = GET_MODE_INNER (mode);
3156 bool imag_first;
3158 #ifdef PUSH_ROUNDING
3159 unsigned int submodesize = GET_MODE_SIZE (submode);
3161 /* In case we output to the stack, but the size is smaller than the
3162 machine can push exactly, we need to use move instructions. */
3163 if (PUSH_ROUNDING (submodesize) != submodesize)
3165 x = emit_move_resolve_push (mode, x);
3166 return emit_move_insn (x, y);
3168 #endif
3170 /* Note that the real part always precedes the imag part in memory
3171 regardless of machine's endianness. */
3172 switch (GET_CODE (XEXP (x, 0)))
3174 case PRE_DEC:
3175 case POST_DEC:
3176 imag_first = true;
3177 break;
3178 case PRE_INC:
3179 case POST_INC:
3180 imag_first = false;
3181 break;
3182 default:
3183 gcc_unreachable ();
3186 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3187 read_complex_part (y, imag_first));
3188 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3189 read_complex_part (y, !imag_first));
3192 /* A subroutine of emit_move_complex. Perform the move from Y to X
3193 via two moves of the parts. Returns the last instruction emitted. */
3196 emit_move_complex_parts (rtx x, rtx y)
3198 /* Show the output dies here. This is necessary for SUBREGs
3199 of pseudos since we cannot track their lifetimes correctly;
3200 hard regs shouldn't appear here except as return values. */
3201 if (!reload_completed && !reload_in_progress
3202 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3203 emit_clobber (x);
3205 write_complex_part (x, read_complex_part (y, false), false);
3206 write_complex_part (x, read_complex_part (y, true), true);
3208 return get_last_insn ();
3211 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3212 MODE is known to be complex. Returns the last instruction emitted. */
3214 static rtx
3215 emit_move_complex (enum machine_mode mode, rtx x, rtx y)
3217 bool try_int;
3219 /* Need to take special care for pushes, to maintain proper ordering
3220 of the data, and possibly extra padding. */
3221 if (push_operand (x, mode))
3222 return emit_move_complex_push (mode, x, y);
3224 /* See if we can coerce the target into moving both values at once. */
3226 /* Move floating point as parts. */
3227 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3228 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing)
3229 try_int = false;
3230 /* Not possible if the values are inherently not adjacent. */
3231 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3232 try_int = false;
3233 /* Is possible if both are registers (or subregs of registers). */
3234 else if (register_operand (x, mode) && register_operand (y, mode))
3235 try_int = true;
3236 /* If one of the operands is a memory, and alignment constraints
3237 are friendly enough, we may be able to do combined memory operations.
3238 We do not attempt this if Y is a constant because that combination is
3239 usually better with the by-parts thing below. */
3240 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3241 && (!STRICT_ALIGNMENT
3242 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3243 try_int = true;
3244 else
3245 try_int = false;
3247 if (try_int)
3249 rtx ret;
3251 /* For memory to memory moves, optimal behavior can be had with the
3252 existing block move logic. */
3253 if (MEM_P (x) && MEM_P (y))
3255 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3256 BLOCK_OP_NO_LIBCALL);
3257 return get_last_insn ();
3260 ret = emit_move_via_integer (mode, x, y, true);
3261 if (ret)
3262 return ret;
3265 return emit_move_complex_parts (x, y);
3268 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3269 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3271 static rtx
3272 emit_move_ccmode (enum machine_mode mode, rtx x, rtx y)
3274 rtx ret;
3276 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3277 if (mode != CCmode)
3279 enum insn_code code = optab_handler (mov_optab, CCmode);
3280 if (code != CODE_FOR_nothing)
3282 x = emit_move_change_mode (CCmode, mode, x, true);
3283 y = emit_move_change_mode (CCmode, mode, y, true);
3284 return emit_insn (GEN_FCN (code) (x, y));
3288 /* Otherwise, find the MODE_INT mode of the same width. */
3289 ret = emit_move_via_integer (mode, x, y, false);
3290 gcc_assert (ret != NULL);
3291 return ret;
3294 /* Return true if word I of OP lies entirely in the
3295 undefined bits of a paradoxical subreg. */
3297 static bool
3298 undefined_operand_subword_p (const_rtx op, int i)
3300 enum machine_mode innermode, innermostmode;
3301 int offset;
3302 if (GET_CODE (op) != SUBREG)
3303 return false;
3304 innermode = GET_MODE (op);
3305 innermostmode = GET_MODE (SUBREG_REG (op));
3306 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3307 /* The SUBREG_BYTE represents offset, as if the value were stored in
3308 memory, except for a paradoxical subreg where we define
3309 SUBREG_BYTE to be 0; undo this exception as in
3310 simplify_subreg. */
3311 if (SUBREG_BYTE (op) == 0
3312 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3314 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3315 if (WORDS_BIG_ENDIAN)
3316 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3317 if (BYTES_BIG_ENDIAN)
3318 offset += difference % UNITS_PER_WORD;
3320 if (offset >= GET_MODE_SIZE (innermostmode)
3321 || offset <= -GET_MODE_SIZE (word_mode))
3322 return true;
3323 return false;
3326 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3327 MODE is any multi-word or full-word mode that lacks a move_insn
3328 pattern. Note that you will get better code if you define such
3329 patterns, even if they must turn into multiple assembler instructions. */
3331 static rtx
3332 emit_move_multi_word (enum machine_mode mode, rtx x, rtx y)
3334 rtx last_insn = 0;
3335 rtx seq, inner;
3336 bool need_clobber;
3337 int i;
3339 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3341 /* If X is a push on the stack, do the push now and replace
3342 X with a reference to the stack pointer. */
3343 if (push_operand (x, mode))
3344 x = emit_move_resolve_push (mode, x);
3346 /* If we are in reload, see if either operand is a MEM whose address
3347 is scheduled for replacement. */
3348 if (reload_in_progress && MEM_P (x)
3349 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3350 x = replace_equiv_address_nv (x, inner);
3351 if (reload_in_progress && MEM_P (y)
3352 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3353 y = replace_equiv_address_nv (y, inner);
3355 start_sequence ();
3357 need_clobber = false;
3358 for (i = 0;
3359 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3360 i++)
3362 rtx xpart = operand_subword (x, i, 1, mode);
3363 rtx ypart;
3365 /* Do not generate code for a move if it would come entirely
3366 from the undefined bits of a paradoxical subreg. */
3367 if (undefined_operand_subword_p (y, i))
3368 continue;
3370 ypart = operand_subword (y, i, 1, mode);
3372 /* If we can't get a part of Y, put Y into memory if it is a
3373 constant. Otherwise, force it into a register. Then we must
3374 be able to get a part of Y. */
3375 if (ypart == 0 && CONSTANT_P (y))
3377 y = use_anchored_address (force_const_mem (mode, y));
3378 ypart = operand_subword (y, i, 1, mode);
3380 else if (ypart == 0)
3381 ypart = operand_subword_force (y, i, mode);
3383 gcc_assert (xpart && ypart);
3385 need_clobber |= (GET_CODE (xpart) == SUBREG);
3387 last_insn = emit_move_insn (xpart, ypart);
3390 seq = get_insns ();
3391 end_sequence ();
3393 /* Show the output dies here. This is necessary for SUBREGs
3394 of pseudos since we cannot track their lifetimes correctly;
3395 hard regs shouldn't appear here except as return values.
3396 We never want to emit such a clobber after reload. */
3397 if (x != y
3398 && ! (reload_in_progress || reload_completed)
3399 && need_clobber != 0)
3400 emit_clobber (x);
3402 emit_insn (seq);
3404 return last_insn;
3407 /* Low level part of emit_move_insn.
3408 Called just like emit_move_insn, but assumes X and Y
3409 are basically valid. */
3412 emit_move_insn_1 (rtx x, rtx y)
3414 enum machine_mode mode = GET_MODE (x);
3415 enum insn_code code;
3417 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3419 code = optab_handler (mov_optab, mode);
3420 if (code != CODE_FOR_nothing)
3421 return emit_insn (GEN_FCN (code) (x, y));
3423 /* Expand complex moves by moving real part and imag part. */
3424 if (COMPLEX_MODE_P (mode))
3425 return emit_move_complex (mode, x, y);
3427 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3428 || ALL_FIXED_POINT_MODE_P (mode))
3430 rtx result = emit_move_via_integer (mode, x, y, true);
3432 /* If we can't find an integer mode, use multi words. */
3433 if (result)
3434 return result;
3435 else
3436 return emit_move_multi_word (mode, x, y);
3439 if (GET_MODE_CLASS (mode) == MODE_CC)
3440 return emit_move_ccmode (mode, x, y);
3442 /* Try using a move pattern for the corresponding integer mode. This is
3443 only safe when simplify_subreg can convert MODE constants into integer
3444 constants. At present, it can only do this reliably if the value
3445 fits within a HOST_WIDE_INT. */
3446 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3448 rtx ret = emit_move_via_integer (mode, x, y, false);
3449 if (ret)
3450 return ret;
3453 return emit_move_multi_word (mode, x, y);
3456 /* Generate code to copy Y into X.
3457 Both Y and X must have the same mode, except that
3458 Y can be a constant with VOIDmode.
3459 This mode cannot be BLKmode; use emit_block_move for that.
3461 Return the last instruction emitted. */
3464 emit_move_insn (rtx x, rtx y)
3466 enum machine_mode mode = GET_MODE (x);
3467 rtx y_cst = NULL_RTX;
3468 rtx last_insn, set;
3470 gcc_assert (mode != BLKmode
3471 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3473 if (CONSTANT_P (y))
3475 if (optimize
3476 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3477 && (last_insn = compress_float_constant (x, y)))
3478 return last_insn;
3480 y_cst = y;
3482 if (!LEGITIMATE_CONSTANT_P (y))
3484 y = force_const_mem (mode, y);
3486 /* If the target's cannot_force_const_mem prevented the spill,
3487 assume that the target's move expanders will also take care
3488 of the non-legitimate constant. */
3489 if (!y)
3490 y = y_cst;
3491 else
3492 y = use_anchored_address (y);
3496 /* If X or Y are memory references, verify that their addresses are valid
3497 for the machine. */
3498 if (MEM_P (x)
3499 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3500 MEM_ADDR_SPACE (x))
3501 && ! push_operand (x, GET_MODE (x))))
3502 x = validize_mem (x);
3504 if (MEM_P (y)
3505 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3506 MEM_ADDR_SPACE (y)))
3507 y = validize_mem (y);
3509 gcc_assert (mode != BLKmode);
3511 last_insn = emit_move_insn_1 (x, y);
3513 if (y_cst && REG_P (x)
3514 && (set = single_set (last_insn)) != NULL_RTX
3515 && SET_DEST (set) == x
3516 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3517 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3519 return last_insn;
3522 /* If Y is representable exactly in a narrower mode, and the target can
3523 perform the extension directly from constant or memory, then emit the
3524 move as an extension. */
3526 static rtx
3527 compress_float_constant (rtx x, rtx y)
3529 enum machine_mode dstmode = GET_MODE (x);
3530 enum machine_mode orig_srcmode = GET_MODE (y);
3531 enum machine_mode srcmode;
3532 REAL_VALUE_TYPE r;
3533 int oldcost, newcost;
3534 bool speed = optimize_insn_for_speed_p ();
3536 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3538 if (LEGITIMATE_CONSTANT_P (y))
3539 oldcost = rtx_cost (y, SET, speed);
3540 else
3541 oldcost = rtx_cost (force_const_mem (dstmode, y), SET, speed);
3543 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3544 srcmode != orig_srcmode;
3545 srcmode = GET_MODE_WIDER_MODE (srcmode))
3547 enum insn_code ic;
3548 rtx trunc_y, last_insn;
3550 /* Skip if the target can't extend this way. */
3551 ic = can_extend_p (dstmode, srcmode, 0);
3552 if (ic == CODE_FOR_nothing)
3553 continue;
3555 /* Skip if the narrowed value isn't exact. */
3556 if (! exact_real_truncate (srcmode, &r))
3557 continue;
3559 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3561 if (LEGITIMATE_CONSTANT_P (trunc_y))
3563 /* Skip if the target needs extra instructions to perform
3564 the extension. */
3565 if (! (*insn_data[ic].operand[1].predicate) (trunc_y, srcmode))
3566 continue;
3567 /* This is valid, but may not be cheaper than the original. */
3568 newcost = rtx_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y), SET, speed);
3569 if (oldcost < newcost)
3570 continue;
3572 else if (float_extend_from_mem[dstmode][srcmode])
3574 trunc_y = force_const_mem (srcmode, trunc_y);
3575 /* This is valid, but may not be cheaper than the original. */
3576 newcost = rtx_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y), SET, speed);
3577 if (oldcost < newcost)
3578 continue;
3579 trunc_y = validize_mem (trunc_y);
3581 else
3582 continue;
3584 /* For CSE's benefit, force the compressed constant pool entry
3585 into a new pseudo. This constant may be used in different modes,
3586 and if not, combine will put things back together for us. */
3587 trunc_y = force_reg (srcmode, trunc_y);
3588 emit_unop_insn (ic, x, trunc_y, UNKNOWN);
3589 last_insn = get_last_insn ();
3591 if (REG_P (x))
3592 set_unique_reg_note (last_insn, REG_EQUAL, y);
3594 return last_insn;
3597 return NULL_RTX;
3600 /* Pushing data onto the stack. */
3602 /* Push a block of length SIZE (perhaps variable)
3603 and return an rtx to address the beginning of the block.
3604 The value may be virtual_outgoing_args_rtx.
3606 EXTRA is the number of bytes of padding to push in addition to SIZE.
3607 BELOW nonzero means this padding comes at low addresses;
3608 otherwise, the padding comes at high addresses. */
3611 push_block (rtx size, int extra, int below)
3613 rtx temp;
3615 size = convert_modes (Pmode, ptr_mode, size, 1);
3616 if (CONSTANT_P (size))
3617 anti_adjust_stack (plus_constant (size, extra));
3618 else if (REG_P (size) && extra == 0)
3619 anti_adjust_stack (size);
3620 else
3622 temp = copy_to_mode_reg (Pmode, size);
3623 if (extra != 0)
3624 temp = expand_binop (Pmode, add_optab, temp, GEN_INT (extra),
3625 temp, 0, OPTAB_LIB_WIDEN);
3626 anti_adjust_stack (temp);
3629 #ifndef STACK_GROWS_DOWNWARD
3630 if (0)
3631 #else
3632 if (1)
3633 #endif
3635 temp = virtual_outgoing_args_rtx;
3636 if (extra != 0 && below)
3637 temp = plus_constant (temp, extra);
3639 else
3641 if (CONST_INT_P (size))
3642 temp = plus_constant (virtual_outgoing_args_rtx,
3643 -INTVAL (size) - (below ? 0 : extra));
3644 else if (extra != 0 && !below)
3645 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3646 negate_rtx (Pmode, plus_constant (size, extra)));
3647 else
3648 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3649 negate_rtx (Pmode, size));
3652 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3655 #ifdef PUSH_ROUNDING
3657 /* Emit single push insn. */
3659 static void
3660 emit_single_push_insn (enum machine_mode mode, rtx x, tree type)
3662 rtx dest_addr;
3663 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3664 rtx dest;
3665 enum insn_code icode;
3666 insn_operand_predicate_fn pred;
3668 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3669 /* If there is push pattern, use it. Otherwise try old way of throwing
3670 MEM representing push operation to move expander. */
3671 icode = optab_handler (push_optab, mode);
3672 if (icode != CODE_FOR_nothing)
3674 if (((pred = insn_data[(int) icode].operand[0].predicate)
3675 && !((*pred) (x, mode))))
3676 x = force_reg (mode, x);
3677 emit_insn (GEN_FCN (icode) (x));
3678 return;
3680 if (GET_MODE_SIZE (mode) == rounded_size)
3681 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
3682 /* If we are to pad downward, adjust the stack pointer first and
3683 then store X into the stack location using an offset. This is
3684 because emit_move_insn does not know how to pad; it does not have
3685 access to type. */
3686 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
3688 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
3689 HOST_WIDE_INT offset;
3691 emit_move_insn (stack_pointer_rtx,
3692 expand_binop (Pmode,
3693 #ifdef STACK_GROWS_DOWNWARD
3694 sub_optab,
3695 #else
3696 add_optab,
3697 #endif
3698 stack_pointer_rtx,
3699 GEN_INT (rounded_size),
3700 NULL_RTX, 0, OPTAB_LIB_WIDEN));
3702 offset = (HOST_WIDE_INT) padding_size;
3703 #ifdef STACK_GROWS_DOWNWARD
3704 if (STACK_PUSH_CODE == POST_DEC)
3705 /* We have already decremented the stack pointer, so get the
3706 previous value. */
3707 offset += (HOST_WIDE_INT) rounded_size;
3708 #else
3709 if (STACK_PUSH_CODE == POST_INC)
3710 /* We have already incremented the stack pointer, so get the
3711 previous value. */
3712 offset -= (HOST_WIDE_INT) rounded_size;
3713 #endif
3714 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
3716 else
3718 #ifdef STACK_GROWS_DOWNWARD
3719 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
3720 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3721 GEN_INT (-(HOST_WIDE_INT) rounded_size));
3722 #else
3723 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
3724 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3725 GEN_INT (rounded_size));
3726 #endif
3727 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
3730 dest = gen_rtx_MEM (mode, dest_addr);
3732 if (type != 0)
3734 set_mem_attributes (dest, type, 1);
3736 if (flag_optimize_sibling_calls)
3737 /* Function incoming arguments may overlap with sibling call
3738 outgoing arguments and we cannot allow reordering of reads
3739 from function arguments with stores to outgoing arguments
3740 of sibling calls. */
3741 set_mem_alias_set (dest, 0);
3743 emit_move_insn (dest, x);
3745 #endif
3747 /* Generate code to push X onto the stack, assuming it has mode MODE and
3748 type TYPE.
3749 MODE is redundant except when X is a CONST_INT (since they don't
3750 carry mode info).
3751 SIZE is an rtx for the size of data to be copied (in bytes),
3752 needed only if X is BLKmode.
3754 ALIGN (in bits) is maximum alignment we can assume.
3756 If PARTIAL and REG are both nonzero, then copy that many of the first
3757 bytes of X into registers starting with REG, and push the rest of X.
3758 The amount of space pushed is decreased by PARTIAL bytes.
3759 REG must be a hard register in this case.
3760 If REG is zero but PARTIAL is not, take any all others actions for an
3761 argument partially in registers, but do not actually load any
3762 registers.
3764 EXTRA is the amount in bytes of extra space to leave next to this arg.
3765 This is ignored if an argument block has already been allocated.
3767 On a machine that lacks real push insns, ARGS_ADDR is the address of
3768 the bottom of the argument block for this call. We use indexing off there
3769 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
3770 argument block has not been preallocated.
3772 ARGS_SO_FAR is the size of args previously pushed for this call.
3774 REG_PARM_STACK_SPACE is nonzero if functions require stack space
3775 for arguments passed in registers. If nonzero, it will be the number
3776 of bytes required. */
3778 void
3779 emit_push_insn (rtx x, enum machine_mode mode, tree type, rtx size,
3780 unsigned int align, int partial, rtx reg, int extra,
3781 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
3782 rtx alignment_pad)
3784 rtx xinner;
3785 enum direction stack_direction
3786 #ifdef STACK_GROWS_DOWNWARD
3787 = downward;
3788 #else
3789 = upward;
3790 #endif
3792 /* Decide where to pad the argument: `downward' for below,
3793 `upward' for above, or `none' for don't pad it.
3794 Default is below for small data on big-endian machines; else above. */
3795 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
3797 /* Invert direction if stack is post-decrement.
3798 FIXME: why? */
3799 if (STACK_PUSH_CODE == POST_DEC)
3800 if (where_pad != none)
3801 where_pad = (where_pad == downward ? upward : downward);
3803 xinner = x;
3805 if (mode == BLKmode
3806 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
3808 /* Copy a block into the stack, entirely or partially. */
3810 rtx temp;
3811 int used;
3812 int offset;
3813 int skip;
3815 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
3816 used = partial - offset;
3818 if (mode != BLKmode)
3820 /* A value is to be stored in an insufficiently aligned
3821 stack slot; copy via a suitably aligned slot if
3822 necessary. */
3823 size = GEN_INT (GET_MODE_SIZE (mode));
3824 if (!MEM_P (xinner))
3826 temp = assign_temp (type, 0, 1, 1);
3827 emit_move_insn (temp, xinner);
3828 xinner = temp;
3832 gcc_assert (size);
3834 /* USED is now the # of bytes we need not copy to the stack
3835 because registers will take care of them. */
3837 if (partial != 0)
3838 xinner = adjust_address (xinner, BLKmode, used);
3840 /* If the partial register-part of the arg counts in its stack size,
3841 skip the part of stack space corresponding to the registers.
3842 Otherwise, start copying to the beginning of the stack space,
3843 by setting SKIP to 0. */
3844 skip = (reg_parm_stack_space == 0) ? 0 : used;
3846 #ifdef PUSH_ROUNDING
3847 /* Do it with several push insns if that doesn't take lots of insns
3848 and if there is no difficulty with push insns that skip bytes
3849 on the stack for alignment purposes. */
3850 if (args_addr == 0
3851 && PUSH_ARGS
3852 && CONST_INT_P (size)
3853 && skip == 0
3854 && MEM_ALIGN (xinner) >= align
3855 && (MOVE_BY_PIECES_P ((unsigned) INTVAL (size) - used, align))
3856 /* Here we avoid the case of a structure whose weak alignment
3857 forces many pushes of a small amount of data,
3858 and such small pushes do rounding that causes trouble. */
3859 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
3860 || align >= BIGGEST_ALIGNMENT
3861 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
3862 == (align / BITS_PER_UNIT)))
3863 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
3865 /* Push padding now if padding above and stack grows down,
3866 or if padding below and stack grows up.
3867 But if space already allocated, this has already been done. */
3868 if (extra && args_addr == 0
3869 && where_pad != none && where_pad != stack_direction)
3870 anti_adjust_stack (GEN_INT (extra));
3872 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
3874 else
3875 #endif /* PUSH_ROUNDING */
3877 rtx target;
3879 /* Otherwise make space on the stack and copy the data
3880 to the address of that space. */
3882 /* Deduct words put into registers from the size we must copy. */
3883 if (partial != 0)
3885 if (CONST_INT_P (size))
3886 size = GEN_INT (INTVAL (size) - used);
3887 else
3888 size = expand_binop (GET_MODE (size), sub_optab, size,
3889 GEN_INT (used), NULL_RTX, 0,
3890 OPTAB_LIB_WIDEN);
3893 /* Get the address of the stack space.
3894 In this case, we do not deal with EXTRA separately.
3895 A single stack adjust will do. */
3896 if (! args_addr)
3898 temp = push_block (size, extra, where_pad == downward);
3899 extra = 0;
3901 else if (CONST_INT_P (args_so_far))
3902 temp = memory_address (BLKmode,
3903 plus_constant (args_addr,
3904 skip + INTVAL (args_so_far)));
3905 else
3906 temp = memory_address (BLKmode,
3907 plus_constant (gen_rtx_PLUS (Pmode,
3908 args_addr,
3909 args_so_far),
3910 skip));
3912 if (!ACCUMULATE_OUTGOING_ARGS)
3914 /* If the source is referenced relative to the stack pointer,
3915 copy it to another register to stabilize it. We do not need
3916 to do this if we know that we won't be changing sp. */
3918 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
3919 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
3920 temp = copy_to_reg (temp);
3923 target = gen_rtx_MEM (BLKmode, temp);
3925 /* We do *not* set_mem_attributes here, because incoming arguments
3926 may overlap with sibling call outgoing arguments and we cannot
3927 allow reordering of reads from function arguments with stores
3928 to outgoing arguments of sibling calls. We do, however, want
3929 to record the alignment of the stack slot. */
3930 /* ALIGN may well be better aligned than TYPE, e.g. due to
3931 PARM_BOUNDARY. Assume the caller isn't lying. */
3932 set_mem_align (target, align);
3934 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
3937 else if (partial > 0)
3939 /* Scalar partly in registers. */
3941 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
3942 int i;
3943 int not_stack;
3944 /* # bytes of start of argument
3945 that we must make space for but need not store. */
3946 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
3947 int args_offset = INTVAL (args_so_far);
3948 int skip;
3950 /* Push padding now if padding above and stack grows down,
3951 or if padding below and stack grows up.
3952 But if space already allocated, this has already been done. */
3953 if (extra && args_addr == 0
3954 && where_pad != none && where_pad != stack_direction)
3955 anti_adjust_stack (GEN_INT (extra));
3957 /* If we make space by pushing it, we might as well push
3958 the real data. Otherwise, we can leave OFFSET nonzero
3959 and leave the space uninitialized. */
3960 if (args_addr == 0)
3961 offset = 0;
3963 /* Now NOT_STACK gets the number of words that we don't need to
3964 allocate on the stack. Convert OFFSET to words too. */
3965 not_stack = (partial - offset) / UNITS_PER_WORD;
3966 offset /= UNITS_PER_WORD;
3968 /* If the partial register-part of the arg counts in its stack size,
3969 skip the part of stack space corresponding to the registers.
3970 Otherwise, start copying to the beginning of the stack space,
3971 by setting SKIP to 0. */
3972 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
3974 if (CONSTANT_P (x) && ! LEGITIMATE_CONSTANT_P (x))
3975 x = validize_mem (force_const_mem (mode, x));
3977 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
3978 SUBREGs of such registers are not allowed. */
3979 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
3980 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
3981 x = copy_to_reg (x);
3983 /* Loop over all the words allocated on the stack for this arg. */
3984 /* We can do it by words, because any scalar bigger than a word
3985 has a size a multiple of a word. */
3986 #ifndef PUSH_ARGS_REVERSED
3987 for (i = not_stack; i < size; i++)
3988 #else
3989 for (i = size - 1; i >= not_stack; i--)
3990 #endif
3991 if (i >= not_stack + offset)
3992 emit_push_insn (operand_subword_force (x, i, mode),
3993 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
3994 0, args_addr,
3995 GEN_INT (args_offset + ((i - not_stack + skip)
3996 * UNITS_PER_WORD)),
3997 reg_parm_stack_space, alignment_pad);
3999 else
4001 rtx addr;
4002 rtx dest;
4004 /* Push padding now if padding above and stack grows down,
4005 or if padding below and stack grows up.
4006 But if space already allocated, this has already been done. */
4007 if (extra && args_addr == 0
4008 && where_pad != none && where_pad != stack_direction)
4009 anti_adjust_stack (GEN_INT (extra));
4011 #ifdef PUSH_ROUNDING
4012 if (args_addr == 0 && PUSH_ARGS)
4013 emit_single_push_insn (mode, x, type);
4014 else
4015 #endif
4017 if (CONST_INT_P (args_so_far))
4018 addr
4019 = memory_address (mode,
4020 plus_constant (args_addr,
4021 INTVAL (args_so_far)));
4022 else
4023 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4024 args_so_far));
4025 dest = gen_rtx_MEM (mode, addr);
4027 /* We do *not* set_mem_attributes here, because incoming arguments
4028 may overlap with sibling call outgoing arguments and we cannot
4029 allow reordering of reads from function arguments with stores
4030 to outgoing arguments of sibling calls. We do, however, want
4031 to record the alignment of the stack slot. */
4032 /* ALIGN may well be better aligned than TYPE, e.g. due to
4033 PARM_BOUNDARY. Assume the caller isn't lying. */
4034 set_mem_align (dest, align);
4036 emit_move_insn (dest, x);
4040 /* If part should go in registers, copy that part
4041 into the appropriate registers. Do this now, at the end,
4042 since mem-to-mem copies above may do function calls. */
4043 if (partial > 0 && reg != 0)
4045 /* Handle calls that pass values in multiple non-contiguous locations.
4046 The Irix 6 ABI has examples of this. */
4047 if (GET_CODE (reg) == PARALLEL)
4048 emit_group_load (reg, x, type, -1);
4049 else
4051 gcc_assert (partial % UNITS_PER_WORD == 0);
4052 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4056 if (extra && args_addr == 0 && where_pad == stack_direction)
4057 anti_adjust_stack (GEN_INT (extra));
4059 if (alignment_pad && args_addr == 0)
4060 anti_adjust_stack (alignment_pad);
4063 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4064 operations. */
4066 static rtx
4067 get_subtarget (rtx x)
4069 return (optimize
4070 || x == 0
4071 /* Only registers can be subtargets. */
4072 || !REG_P (x)
4073 /* Don't use hard regs to avoid extending their life. */
4074 || REGNO (x) < FIRST_PSEUDO_REGISTER
4075 ? 0 : x);
4078 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4079 FIELD is a bitfield. Returns true if the optimization was successful,
4080 and there's nothing else to do. */
4082 static bool
4083 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4084 unsigned HOST_WIDE_INT bitpos,
4085 enum machine_mode mode1, rtx str_rtx,
4086 tree to, tree src)
4088 enum machine_mode str_mode = GET_MODE (str_rtx);
4089 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4090 tree op0, op1;
4091 rtx value, result;
4092 optab binop;
4094 if (mode1 != VOIDmode
4095 || bitsize >= BITS_PER_WORD
4096 || str_bitsize > BITS_PER_WORD
4097 || TREE_SIDE_EFFECTS (to)
4098 || TREE_THIS_VOLATILE (to))
4099 return false;
4101 STRIP_NOPS (src);
4102 if (!BINARY_CLASS_P (src)
4103 || TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4104 return false;
4106 op0 = TREE_OPERAND (src, 0);
4107 op1 = TREE_OPERAND (src, 1);
4108 STRIP_NOPS (op0);
4110 if (!operand_equal_p (to, op0, 0))
4111 return false;
4113 if (MEM_P (str_rtx))
4115 unsigned HOST_WIDE_INT offset1;
4117 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4118 str_mode = word_mode;
4119 str_mode = get_best_mode (bitsize, bitpos,
4120 MEM_ALIGN (str_rtx), str_mode, 0);
4121 if (str_mode == VOIDmode)
4122 return false;
4123 str_bitsize = GET_MODE_BITSIZE (str_mode);
4125 offset1 = bitpos;
4126 bitpos %= str_bitsize;
4127 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4128 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4130 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4131 return false;
4133 /* If the bit field covers the whole REG/MEM, store_field
4134 will likely generate better code. */
4135 if (bitsize >= str_bitsize)
4136 return false;
4138 /* We can't handle fields split across multiple entities. */
4139 if (bitpos + bitsize > str_bitsize)
4140 return false;
4142 if (BYTES_BIG_ENDIAN)
4143 bitpos = str_bitsize - bitpos - bitsize;
4145 switch (TREE_CODE (src))
4147 case PLUS_EXPR:
4148 case MINUS_EXPR:
4149 /* For now, just optimize the case of the topmost bitfield
4150 where we don't need to do any masking and also
4151 1 bit bitfields where xor can be used.
4152 We might win by one instruction for the other bitfields
4153 too if insv/extv instructions aren't used, so that
4154 can be added later. */
4155 if (bitpos + bitsize != str_bitsize
4156 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4157 break;
4159 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4160 value = convert_modes (str_mode,
4161 TYPE_MODE (TREE_TYPE (op1)), value,
4162 TYPE_UNSIGNED (TREE_TYPE (op1)));
4164 /* We may be accessing data outside the field, which means
4165 we can alias adjacent data. */
4166 if (MEM_P (str_rtx))
4168 str_rtx = shallow_copy_rtx (str_rtx);
4169 set_mem_alias_set (str_rtx, 0);
4170 set_mem_expr (str_rtx, 0);
4173 binop = TREE_CODE (src) == PLUS_EXPR ? add_optab : sub_optab;
4174 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4176 value = expand_and (str_mode, value, const1_rtx, NULL);
4177 binop = xor_optab;
4179 value = expand_shift (LSHIFT_EXPR, str_mode, value,
4180 build_int_cst (NULL_TREE, bitpos),
4181 NULL_RTX, 1);
4182 result = expand_binop (str_mode, binop, str_rtx,
4183 value, str_rtx, 1, OPTAB_WIDEN);
4184 if (result != str_rtx)
4185 emit_move_insn (str_rtx, result);
4186 return true;
4188 case BIT_IOR_EXPR:
4189 case BIT_XOR_EXPR:
4190 if (TREE_CODE (op1) != INTEGER_CST)
4191 break;
4192 value = expand_expr (op1, NULL_RTX, GET_MODE (str_rtx), EXPAND_NORMAL);
4193 value = convert_modes (GET_MODE (str_rtx),
4194 TYPE_MODE (TREE_TYPE (op1)), value,
4195 TYPE_UNSIGNED (TREE_TYPE (op1)));
4197 /* We may be accessing data outside the field, which means
4198 we can alias adjacent data. */
4199 if (MEM_P (str_rtx))
4201 str_rtx = shallow_copy_rtx (str_rtx);
4202 set_mem_alias_set (str_rtx, 0);
4203 set_mem_expr (str_rtx, 0);
4206 binop = TREE_CODE (src) == BIT_IOR_EXPR ? ior_optab : xor_optab;
4207 if (bitpos + bitsize != GET_MODE_BITSIZE (GET_MODE (str_rtx)))
4209 rtx mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << bitsize)
4210 - 1);
4211 value = expand_and (GET_MODE (str_rtx), value, mask,
4212 NULL_RTX);
4214 value = expand_shift (LSHIFT_EXPR, GET_MODE (str_rtx), value,
4215 build_int_cst (NULL_TREE, bitpos),
4216 NULL_RTX, 1);
4217 result = expand_binop (GET_MODE (str_rtx), binop, str_rtx,
4218 value, str_rtx, 1, OPTAB_WIDEN);
4219 if (result != str_rtx)
4220 emit_move_insn (str_rtx, result);
4221 return true;
4223 default:
4224 break;
4227 return false;
4231 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4232 is true, try generating a nontemporal store. */
4234 void
4235 expand_assignment (tree to, tree from, bool nontemporal)
4237 rtx to_rtx = 0;
4238 rtx result;
4239 enum machine_mode mode;
4240 int align, icode;
4242 /* Don't crash if the lhs of the assignment was erroneous. */
4243 if (TREE_CODE (to) == ERROR_MARK)
4245 result = expand_normal (from);
4246 return;
4249 /* Optimize away no-op moves without side-effects. */
4250 if (operand_equal_p (to, from, 0))
4251 return;
4253 mode = TYPE_MODE (TREE_TYPE (to));
4254 if ((TREE_CODE (to) == MEM_REF
4255 || TREE_CODE (to) == TARGET_MEM_REF)
4256 && mode != BLKmode
4257 && ((align = MAX (TYPE_ALIGN (TREE_TYPE (to)),
4258 get_object_alignment (to, BIGGEST_ALIGNMENT)))
4259 < (signed) GET_MODE_ALIGNMENT (mode))
4260 && ((icode = optab_handler (movmisalign_optab, mode))
4261 != CODE_FOR_nothing))
4263 enum machine_mode address_mode, op_mode1;
4264 rtx insn, reg, op0, mem;
4266 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4267 reg = force_not_mem (reg);
4269 if (TREE_CODE (to) == MEM_REF)
4271 addr_space_t as
4272 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (to, 1))));
4273 tree base = TREE_OPERAND (to, 0);
4274 address_mode = targetm.addr_space.address_mode (as);
4275 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4276 op0 = convert_memory_address_addr_space (address_mode, op0, as);
4277 if (!integer_zerop (TREE_OPERAND (to, 1)))
4279 rtx off
4280 = immed_double_int_const (mem_ref_offset (to), address_mode);
4281 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
4283 op0 = memory_address_addr_space (mode, op0, as);
4284 mem = gen_rtx_MEM (mode, op0);
4285 set_mem_attributes (mem, to, 0);
4286 set_mem_addr_space (mem, as);
4288 else if (TREE_CODE (to) == TARGET_MEM_REF)
4290 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (to));
4291 struct mem_address addr;
4293 get_address_description (to, &addr);
4294 op0 = addr_for_mem_ref (&addr, as, true);
4295 op0 = memory_address_addr_space (mode, op0, as);
4296 mem = gen_rtx_MEM (mode, op0);
4297 set_mem_attributes (mem, to, 0);
4298 set_mem_addr_space (mem, as);
4300 else
4301 gcc_unreachable ();
4302 if (TREE_THIS_VOLATILE (to))
4303 MEM_VOLATILE_P (mem) = 1;
4305 op_mode1 = insn_data[icode].operand[1].mode;
4306 if (! (*insn_data[icode].operand[1].predicate) (reg, op_mode1)
4307 && op_mode1 != VOIDmode)
4308 reg = copy_to_mode_reg (op_mode1, reg);
4310 insn = GEN_FCN (icode) (mem, reg);
4311 /* The movmisalign<mode> pattern cannot fail, else the assignment would
4312 silently be omitted. */
4313 gcc_assert (insn != NULL_RTX);
4314 emit_insn (insn);
4315 return;
4318 /* Assignment of a structure component needs special treatment
4319 if the structure component's rtx is not simply a MEM.
4320 Assignment of an array element at a constant index, and assignment of
4321 an array element in an unaligned packed structure field, has the same
4322 problem. */
4323 if (handled_component_p (to)
4324 /* ??? We only need to handle MEM_REF here if the access is not
4325 a full access of the base object. */
4326 || (TREE_CODE (to) == MEM_REF
4327 && TREE_CODE (TREE_OPERAND (to, 0)) == ADDR_EXPR)
4328 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4330 enum machine_mode mode1;
4331 HOST_WIDE_INT bitsize, bitpos;
4332 tree offset;
4333 int unsignedp;
4334 int volatilep = 0;
4335 tree tem;
4337 push_temp_slots ();
4338 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4339 &unsignedp, &volatilep, true);
4341 /* If we are going to use store_bit_field and extract_bit_field,
4342 make sure to_rtx will be safe for multiple use. */
4344 to_rtx = expand_normal (tem);
4346 /* If the bitfield is volatile, we want to access it in the
4347 field's mode, not the computed mode.
4348 If a MEM has VOIDmode (external with incomplete type),
4349 use BLKmode for it instead. */
4350 if (MEM_P (to_rtx))
4352 if (volatilep && flag_strict_volatile_bitfields > 0)
4353 to_rtx = adjust_address (to_rtx, mode1, 0);
4354 else if (GET_MODE (to_rtx) == VOIDmode)
4355 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4358 if (offset != 0)
4360 enum machine_mode address_mode;
4361 rtx offset_rtx;
4363 if (!MEM_P (to_rtx))
4365 /* We can get constant negative offsets into arrays with broken
4366 user code. Translate this to a trap instead of ICEing. */
4367 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4368 expand_builtin_trap ();
4369 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4372 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4373 address_mode
4374 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (to_rtx));
4375 if (GET_MODE (offset_rtx) != address_mode)
4376 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4378 /* A constant address in TO_RTX can have VOIDmode, we must not try
4379 to call force_reg for that case. Avoid that case. */
4380 if (MEM_P (to_rtx)
4381 && GET_MODE (to_rtx) == BLKmode
4382 && GET_MODE (XEXP (to_rtx, 0)) != VOIDmode
4383 && bitsize > 0
4384 && (bitpos % bitsize) == 0
4385 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4386 && MEM_ALIGN (to_rtx) == GET_MODE_ALIGNMENT (mode1))
4388 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4389 bitpos = 0;
4392 to_rtx = offset_address (to_rtx, offset_rtx,
4393 highest_pow2_factor_for_target (to,
4394 offset));
4397 /* No action is needed if the target is not a memory and the field
4398 lies completely outside that target. This can occur if the source
4399 code contains an out-of-bounds access to a small array. */
4400 if (!MEM_P (to_rtx)
4401 && GET_MODE (to_rtx) != BLKmode
4402 && (unsigned HOST_WIDE_INT) bitpos
4403 >= GET_MODE_BITSIZE (GET_MODE (to_rtx)))
4405 expand_normal (from);
4406 result = NULL;
4408 /* Handle expand_expr of a complex value returning a CONCAT. */
4409 else if (GET_CODE (to_rtx) == CONCAT)
4411 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4412 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4413 && bitpos == 0
4414 && bitsize == mode_bitsize)
4415 result = store_expr (from, to_rtx, false, nontemporal);
4416 else if (bitsize == mode_bitsize / 2
4417 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4418 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4419 nontemporal);
4420 else if (bitpos + bitsize <= mode_bitsize / 2)
4421 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4422 mode1, from, TREE_TYPE (tem),
4423 get_alias_set (to), nontemporal);
4424 else if (bitpos >= mode_bitsize / 2)
4425 result = store_field (XEXP (to_rtx, 1), bitsize,
4426 bitpos - mode_bitsize / 2, mode1, from,
4427 TREE_TYPE (tem), get_alias_set (to),
4428 nontemporal);
4429 else if (bitpos == 0 && bitsize == mode_bitsize)
4431 rtx from_rtx;
4432 result = expand_normal (from);
4433 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4434 TYPE_MODE (TREE_TYPE (from)), 0);
4435 emit_move_insn (XEXP (to_rtx, 0),
4436 read_complex_part (from_rtx, false));
4437 emit_move_insn (XEXP (to_rtx, 1),
4438 read_complex_part (from_rtx, true));
4440 else
4442 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4443 GET_MODE_SIZE (GET_MODE (to_rtx)),
4445 write_complex_part (temp, XEXP (to_rtx, 0), false);
4446 write_complex_part (temp, XEXP (to_rtx, 1), true);
4447 result = store_field (temp, bitsize, bitpos, mode1, from,
4448 TREE_TYPE (tem), get_alias_set (to),
4449 nontemporal);
4450 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4451 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4454 else
4456 if (MEM_P (to_rtx))
4458 /* If the field is at offset zero, we could have been given the
4459 DECL_RTX of the parent struct. Don't munge it. */
4460 to_rtx = shallow_copy_rtx (to_rtx);
4462 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4464 /* Deal with volatile and readonly fields. The former is only
4465 done for MEM. Also set MEM_KEEP_ALIAS_SET_P if needed. */
4466 if (volatilep)
4467 MEM_VOLATILE_P (to_rtx) = 1;
4468 if (component_uses_parent_alias_set (to))
4469 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
4472 if (optimize_bitfield_assignment_op (bitsize, bitpos, mode1,
4473 to_rtx, to, from))
4474 result = NULL;
4475 else
4476 result = store_field (to_rtx, bitsize, bitpos, mode1, from,
4477 TREE_TYPE (tem), get_alias_set (to),
4478 nontemporal);
4481 if (result)
4482 preserve_temp_slots (result);
4483 free_temp_slots ();
4484 pop_temp_slots ();
4485 return;
4488 /* If the rhs is a function call and its value is not an aggregate,
4489 call the function before we start to compute the lhs.
4490 This is needed for correct code for cases such as
4491 val = setjmp (buf) on machines where reference to val
4492 requires loading up part of an address in a separate insn.
4494 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4495 since it might be a promoted variable where the zero- or sign- extension
4496 needs to be done. Handling this in the normal way is safe because no
4497 computation is done before the call. The same is true for SSA names. */
4498 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
4499 && COMPLETE_TYPE_P (TREE_TYPE (from))
4500 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
4501 && ! (((TREE_CODE (to) == VAR_DECL
4502 || TREE_CODE (to) == PARM_DECL
4503 || TREE_CODE (to) == RESULT_DECL)
4504 && REG_P (DECL_RTL (to)))
4505 || TREE_CODE (to) == SSA_NAME))
4507 rtx value;
4509 push_temp_slots ();
4510 value = expand_normal (from);
4511 if (to_rtx == 0)
4512 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4514 /* Handle calls that return values in multiple non-contiguous locations.
4515 The Irix 6 ABI has examples of this. */
4516 if (GET_CODE (to_rtx) == PARALLEL)
4517 emit_group_load (to_rtx, value, TREE_TYPE (from),
4518 int_size_in_bytes (TREE_TYPE (from)));
4519 else if (GET_MODE (to_rtx) == BLKmode)
4520 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
4521 else
4523 if (POINTER_TYPE_P (TREE_TYPE (to)))
4524 value = convert_memory_address_addr_space
4525 (GET_MODE (to_rtx), value,
4526 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
4528 emit_move_insn (to_rtx, value);
4530 preserve_temp_slots (to_rtx);
4531 free_temp_slots ();
4532 pop_temp_slots ();
4533 return;
4536 /* Ordinary treatment. Expand TO to get a REG or MEM rtx.
4537 Don't re-expand if it was expanded already (in COMPONENT_REF case). */
4539 if (to_rtx == 0)
4540 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4542 /* Don't move directly into a return register. */
4543 if (TREE_CODE (to) == RESULT_DECL
4544 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
4546 rtx temp;
4548 push_temp_slots ();
4549 if (REG_P (to_rtx) && TYPE_MODE (TREE_TYPE (from)) == BLKmode)
4550 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
4551 else
4552 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
4554 if (GET_CODE (to_rtx) == PARALLEL)
4555 emit_group_load (to_rtx, temp, TREE_TYPE (from),
4556 int_size_in_bytes (TREE_TYPE (from)));
4557 else if (temp)
4558 emit_move_insn (to_rtx, temp);
4560 preserve_temp_slots (to_rtx);
4561 free_temp_slots ();
4562 pop_temp_slots ();
4563 return;
4566 /* In case we are returning the contents of an object which overlaps
4567 the place the value is being stored, use a safe function when copying
4568 a value through a pointer into a structure value return block. */
4569 if (TREE_CODE (to) == RESULT_DECL
4570 && TREE_CODE (from) == INDIRECT_REF
4571 && ADDR_SPACE_GENERIC_P
4572 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
4573 && refs_may_alias_p (to, from)
4574 && cfun->returns_struct
4575 && !cfun->returns_pcc_struct)
4577 rtx from_rtx, size;
4579 push_temp_slots ();
4580 size = expr_size (from);
4581 from_rtx = expand_normal (from);
4583 emit_library_call (memmove_libfunc, LCT_NORMAL,
4584 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
4585 XEXP (from_rtx, 0), Pmode,
4586 convert_to_mode (TYPE_MODE (sizetype),
4587 size, TYPE_UNSIGNED (sizetype)),
4588 TYPE_MODE (sizetype));
4590 preserve_temp_slots (to_rtx);
4591 free_temp_slots ();
4592 pop_temp_slots ();
4593 return;
4596 /* Compute FROM and store the value in the rtx we got. */
4598 push_temp_slots ();
4599 result = store_expr (from, to_rtx, 0, nontemporal);
4600 preserve_temp_slots (result);
4601 free_temp_slots ();
4602 pop_temp_slots ();
4603 return;
4606 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
4607 succeeded, false otherwise. */
4609 bool
4610 emit_storent_insn (rtx to, rtx from)
4612 enum machine_mode mode = GET_MODE (to), imode;
4613 enum insn_code code = optab_handler (storent_optab, mode);
4614 rtx pattern;
4616 if (code == CODE_FOR_nothing)
4617 return false;
4619 imode = insn_data[code].operand[0].mode;
4620 if (!insn_data[code].operand[0].predicate (to, imode))
4621 return false;
4623 imode = insn_data[code].operand[1].mode;
4624 if (!insn_data[code].operand[1].predicate (from, imode))
4626 from = copy_to_mode_reg (imode, from);
4627 if (!insn_data[code].operand[1].predicate (from, imode))
4628 return false;
4631 pattern = GEN_FCN (code) (to, from);
4632 if (pattern == NULL_RTX)
4633 return false;
4635 emit_insn (pattern);
4636 return true;
4639 /* Generate code for computing expression EXP,
4640 and storing the value into TARGET.
4642 If the mode is BLKmode then we may return TARGET itself.
4643 It turns out that in BLKmode it doesn't cause a problem.
4644 because C has no operators that could combine two different
4645 assignments into the same BLKmode object with different values
4646 with no sequence point. Will other languages need this to
4647 be more thorough?
4649 If CALL_PARAM_P is nonzero, this is a store into a call param on the
4650 stack, and block moves may need to be treated specially.
4652 If NONTEMPORAL is true, try using a nontemporal store instruction. */
4655 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
4657 rtx temp;
4658 rtx alt_rtl = NULL_RTX;
4659 location_t loc = EXPR_LOCATION (exp);
4661 if (VOID_TYPE_P (TREE_TYPE (exp)))
4663 /* C++ can generate ?: expressions with a throw expression in one
4664 branch and an rvalue in the other. Here, we resolve attempts to
4665 store the throw expression's nonexistent result. */
4666 gcc_assert (!call_param_p);
4667 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
4668 return NULL_RTX;
4670 if (TREE_CODE (exp) == COMPOUND_EXPR)
4672 /* Perform first part of compound expression, then assign from second
4673 part. */
4674 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
4675 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
4676 return store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
4677 nontemporal);
4679 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
4681 /* For conditional expression, get safe form of the target. Then
4682 test the condition, doing the appropriate assignment on either
4683 side. This avoids the creation of unnecessary temporaries.
4684 For non-BLKmode, it is more efficient not to do this. */
4686 rtx lab1 = gen_label_rtx (), lab2 = gen_label_rtx ();
4688 do_pending_stack_adjust ();
4689 NO_DEFER_POP;
4690 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
4691 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
4692 nontemporal);
4693 emit_jump_insn (gen_jump (lab2));
4694 emit_barrier ();
4695 emit_label (lab1);
4696 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
4697 nontemporal);
4698 emit_label (lab2);
4699 OK_DEFER_POP;
4701 return NULL_RTX;
4703 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
4704 /* If this is a scalar in a register that is stored in a wider mode
4705 than the declared mode, compute the result into its declared mode
4706 and then convert to the wider mode. Our value is the computed
4707 expression. */
4709 rtx inner_target = 0;
4711 /* We can do the conversion inside EXP, which will often result
4712 in some optimizations. Do the conversion in two steps: first
4713 change the signedness, if needed, then the extend. But don't
4714 do this if the type of EXP is a subtype of something else
4715 since then the conversion might involve more than just
4716 converting modes. */
4717 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
4718 && TREE_TYPE (TREE_TYPE (exp)) == 0
4719 && GET_MODE_PRECISION (GET_MODE (target))
4720 == TYPE_PRECISION (TREE_TYPE (exp)))
4722 if (TYPE_UNSIGNED (TREE_TYPE (exp))
4723 != SUBREG_PROMOTED_UNSIGNED_P (target))
4725 /* Some types, e.g. Fortran's logical*4, won't have a signed
4726 version, so use the mode instead. */
4727 tree ntype
4728 = (signed_or_unsigned_type_for
4729 (SUBREG_PROMOTED_UNSIGNED_P (target), TREE_TYPE (exp)));
4730 if (ntype == NULL)
4731 ntype = lang_hooks.types.type_for_mode
4732 (TYPE_MODE (TREE_TYPE (exp)),
4733 SUBREG_PROMOTED_UNSIGNED_P (target));
4735 exp = fold_convert_loc (loc, ntype, exp);
4738 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
4739 (GET_MODE (SUBREG_REG (target)),
4740 SUBREG_PROMOTED_UNSIGNED_P (target)),
4741 exp);
4743 inner_target = SUBREG_REG (target);
4746 temp = expand_expr (exp, inner_target, VOIDmode,
4747 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
4749 /* If TEMP is a VOIDmode constant, use convert_modes to make
4750 sure that we properly convert it. */
4751 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
4753 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
4754 temp, SUBREG_PROMOTED_UNSIGNED_P (target));
4755 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
4756 GET_MODE (target), temp,
4757 SUBREG_PROMOTED_UNSIGNED_P (target));
4760 convert_move (SUBREG_REG (target), temp,
4761 SUBREG_PROMOTED_UNSIGNED_P (target));
4763 return NULL_RTX;
4765 else if ((TREE_CODE (exp) == STRING_CST
4766 || (TREE_CODE (exp) == MEM_REF
4767 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
4768 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4769 == STRING_CST
4770 && integer_zerop (TREE_OPERAND (exp, 1))))
4771 && !nontemporal && !call_param_p
4772 && MEM_P (target))
4774 /* Optimize initialization of an array with a STRING_CST. */
4775 HOST_WIDE_INT exp_len, str_copy_len;
4776 rtx dest_mem;
4777 tree str = TREE_CODE (exp) == STRING_CST
4778 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4780 exp_len = int_expr_size (exp);
4781 if (exp_len <= 0)
4782 goto normal_expr;
4784 if (TREE_STRING_LENGTH (str) <= 0)
4785 goto normal_expr;
4787 str_copy_len = strlen (TREE_STRING_POINTER (str));
4788 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
4789 goto normal_expr;
4791 str_copy_len = TREE_STRING_LENGTH (str);
4792 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
4793 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
4795 str_copy_len += STORE_MAX_PIECES - 1;
4796 str_copy_len &= ~(STORE_MAX_PIECES - 1);
4798 str_copy_len = MIN (str_copy_len, exp_len);
4799 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
4800 CONST_CAST (char *, TREE_STRING_POINTER (str)),
4801 MEM_ALIGN (target), false))
4802 goto normal_expr;
4804 dest_mem = target;
4806 dest_mem = store_by_pieces (dest_mem,
4807 str_copy_len, builtin_strncpy_read_str,
4808 CONST_CAST (char *,
4809 TREE_STRING_POINTER (str)),
4810 MEM_ALIGN (target), false,
4811 exp_len > str_copy_len ? 1 : 0);
4812 if (exp_len > str_copy_len)
4813 clear_storage (adjust_address (dest_mem, BLKmode, 0),
4814 GEN_INT (exp_len - str_copy_len),
4815 BLOCK_OP_NORMAL);
4816 return NULL_RTX;
4818 else
4820 rtx tmp_target;
4822 normal_expr:
4823 /* If we want to use a nontemporal store, force the value to
4824 register first. */
4825 tmp_target = nontemporal ? NULL_RTX : target;
4826 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
4827 (call_param_p
4828 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
4829 &alt_rtl);
4832 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
4833 the same as that of TARGET, adjust the constant. This is needed, for
4834 example, in case it is a CONST_DOUBLE and we want only a word-sized
4835 value. */
4836 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
4837 && TREE_CODE (exp) != ERROR_MARK
4838 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
4839 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
4840 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
4842 /* If value was not generated in the target, store it there.
4843 Convert the value to TARGET's type first if necessary and emit the
4844 pending incrementations that have been queued when expanding EXP.
4845 Note that we cannot emit the whole queue blindly because this will
4846 effectively disable the POST_INC optimization later.
4848 If TEMP and TARGET compare equal according to rtx_equal_p, but
4849 one or both of them are volatile memory refs, we have to distinguish
4850 two cases:
4851 - expand_expr has used TARGET. In this case, we must not generate
4852 another copy. This can be detected by TARGET being equal according
4853 to == .
4854 - expand_expr has not used TARGET - that means that the source just
4855 happens to have the same RTX form. Since temp will have been created
4856 by expand_expr, it will compare unequal according to == .
4857 We must generate a copy in this case, to reach the correct number
4858 of volatile memory references. */
4860 if ((! rtx_equal_p (temp, target)
4861 || (temp != target && (side_effects_p (temp)
4862 || side_effects_p (target))))
4863 && TREE_CODE (exp) != ERROR_MARK
4864 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
4865 but TARGET is not valid memory reference, TEMP will differ
4866 from TARGET although it is really the same location. */
4867 && !(alt_rtl
4868 && rtx_equal_p (alt_rtl, target)
4869 && !side_effects_p (alt_rtl)
4870 && !side_effects_p (target))
4871 /* If there's nothing to copy, don't bother. Don't call
4872 expr_size unless necessary, because some front-ends (C++)
4873 expr_size-hook must not be given objects that are not
4874 supposed to be bit-copied or bit-initialized. */
4875 && expr_size (exp) != const0_rtx)
4877 if (GET_MODE (temp) != GET_MODE (target)
4878 && GET_MODE (temp) != VOIDmode)
4880 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
4881 if (GET_MODE (target) == BLKmode
4882 && GET_MODE (temp) == BLKmode)
4883 emit_block_move (target, temp, expr_size (exp),
4884 (call_param_p
4885 ? BLOCK_OP_CALL_PARM
4886 : BLOCK_OP_NORMAL));
4887 else if (GET_MODE (target) == BLKmode)
4888 store_bit_field (target, INTVAL (expr_size (exp)) * BITS_PER_UNIT,
4889 0, GET_MODE (temp), temp);
4890 else
4891 convert_move (target, temp, unsignedp);
4894 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
4896 /* Handle copying a string constant into an array. The string
4897 constant may be shorter than the array. So copy just the string's
4898 actual length, and clear the rest. First get the size of the data
4899 type of the string, which is actually the size of the target. */
4900 rtx size = expr_size (exp);
4902 if (CONST_INT_P (size)
4903 && INTVAL (size) < TREE_STRING_LENGTH (exp))
4904 emit_block_move (target, temp, size,
4905 (call_param_p
4906 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
4907 else
4909 enum machine_mode pointer_mode
4910 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
4911 enum machine_mode address_mode
4912 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (target));
4914 /* Compute the size of the data to copy from the string. */
4915 tree copy_size
4916 = size_binop_loc (loc, MIN_EXPR,
4917 make_tree (sizetype, size),
4918 size_int (TREE_STRING_LENGTH (exp)));
4919 rtx copy_size_rtx
4920 = expand_expr (copy_size, NULL_RTX, VOIDmode,
4921 (call_param_p
4922 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
4923 rtx label = 0;
4925 /* Copy that much. */
4926 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
4927 TYPE_UNSIGNED (sizetype));
4928 emit_block_move (target, temp, copy_size_rtx,
4929 (call_param_p
4930 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
4932 /* Figure out how much is left in TARGET that we have to clear.
4933 Do all calculations in pointer_mode. */
4934 if (CONST_INT_P (copy_size_rtx))
4936 size = plus_constant (size, -INTVAL (copy_size_rtx));
4937 target = adjust_address (target, BLKmode,
4938 INTVAL (copy_size_rtx));
4940 else
4942 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
4943 copy_size_rtx, NULL_RTX, 0,
4944 OPTAB_LIB_WIDEN);
4946 if (GET_MODE (copy_size_rtx) != address_mode)
4947 copy_size_rtx = convert_to_mode (address_mode,
4948 copy_size_rtx,
4949 TYPE_UNSIGNED (sizetype));
4951 target = offset_address (target, copy_size_rtx,
4952 highest_pow2_factor (copy_size));
4953 label = gen_label_rtx ();
4954 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
4955 GET_MODE (size), 0, label);
4958 if (size != const0_rtx)
4959 clear_storage (target, size, BLOCK_OP_NORMAL);
4961 if (label)
4962 emit_label (label);
4965 /* Handle calls that return values in multiple non-contiguous locations.
4966 The Irix 6 ABI has examples of this. */
4967 else if (GET_CODE (target) == PARALLEL)
4968 emit_group_load (target, temp, TREE_TYPE (exp),
4969 int_size_in_bytes (TREE_TYPE (exp)));
4970 else if (GET_MODE (temp) == BLKmode)
4971 emit_block_move (target, temp, expr_size (exp),
4972 (call_param_p
4973 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
4974 else if (nontemporal
4975 && emit_storent_insn (target, temp))
4976 /* If we managed to emit a nontemporal store, there is nothing else to
4977 do. */
4979 else
4981 temp = force_operand (temp, target);
4982 if (temp != target)
4983 emit_move_insn (target, temp);
4987 return NULL_RTX;
4990 /* Helper for categorize_ctor_elements. Identical interface. */
4992 static bool
4993 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
4994 HOST_WIDE_INT *p_elt_count,
4995 bool *p_must_clear)
4997 unsigned HOST_WIDE_INT idx;
4998 HOST_WIDE_INT nz_elts, elt_count;
4999 tree value, purpose;
5001 /* Whether CTOR is a valid constant initializer, in accordance with what
5002 initializer_constant_valid_p does. If inferred from the constructor
5003 elements, true until proven otherwise. */
5004 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5005 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5007 nz_elts = 0;
5008 elt_count = 0;
5010 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5012 HOST_WIDE_INT mult = 1;
5014 if (TREE_CODE (purpose) == RANGE_EXPR)
5016 tree lo_index = TREE_OPERAND (purpose, 0);
5017 tree hi_index = TREE_OPERAND (purpose, 1);
5019 if (host_integerp (lo_index, 1) && host_integerp (hi_index, 1))
5020 mult = (tree_low_cst (hi_index, 1)
5021 - tree_low_cst (lo_index, 1) + 1);
5024 switch (TREE_CODE (value))
5026 case CONSTRUCTOR:
5028 HOST_WIDE_INT nz = 0, ic = 0;
5030 bool const_elt_p
5031 = categorize_ctor_elements_1 (value, &nz, &ic, p_must_clear);
5033 nz_elts += mult * nz;
5034 elt_count += mult * ic;
5036 if (const_from_elts_p && const_p)
5037 const_p = const_elt_p;
5039 break;
5041 case INTEGER_CST:
5042 case REAL_CST:
5043 case FIXED_CST:
5044 if (!initializer_zerop (value))
5045 nz_elts += mult;
5046 elt_count += mult;
5047 break;
5049 case STRING_CST:
5050 nz_elts += mult * TREE_STRING_LENGTH (value);
5051 elt_count += mult * TREE_STRING_LENGTH (value);
5052 break;
5054 case COMPLEX_CST:
5055 if (!initializer_zerop (TREE_REALPART (value)))
5056 nz_elts += mult;
5057 if (!initializer_zerop (TREE_IMAGPART (value)))
5058 nz_elts += mult;
5059 elt_count += mult;
5060 break;
5062 case VECTOR_CST:
5064 tree v;
5065 for (v = TREE_VECTOR_CST_ELTS (value); v; v = TREE_CHAIN (v))
5067 if (!initializer_zerop (TREE_VALUE (v)))
5068 nz_elts += mult;
5069 elt_count += mult;
5072 break;
5074 default:
5076 HOST_WIDE_INT tc = count_type_elements (TREE_TYPE (value), true);
5077 if (tc < 1)
5078 tc = 1;
5079 nz_elts += mult * tc;
5080 elt_count += mult * tc;
5082 if (const_from_elts_p && const_p)
5083 const_p = initializer_constant_valid_p (value, TREE_TYPE (value))
5084 != NULL_TREE;
5086 break;
5090 if (!*p_must_clear
5091 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
5092 || TREE_CODE (TREE_TYPE (ctor)) == QUAL_UNION_TYPE))
5094 tree init_sub_type;
5095 bool clear_this = true;
5097 if (!VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)))
5099 /* We don't expect more than one element of the union to be
5100 initialized. Not sure what we should do otherwise... */
5101 gcc_assert (VEC_length (constructor_elt, CONSTRUCTOR_ELTS (ctor))
5102 == 1);
5104 init_sub_type = TREE_TYPE (VEC_index (constructor_elt,
5105 CONSTRUCTOR_ELTS (ctor),
5106 0)->value);
5108 /* ??? We could look at each element of the union, and find the
5109 largest element. Which would avoid comparing the size of the
5110 initialized element against any tail padding in the union.
5111 Doesn't seem worth the effort... */
5112 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (ctor)),
5113 TYPE_SIZE (init_sub_type)) == 1)
5115 /* And now we have to find out if the element itself is fully
5116 constructed. E.g. for union { struct { int a, b; } s; } u
5117 = { .s = { .a = 1 } }. */
5118 if (elt_count == count_type_elements (init_sub_type, false))
5119 clear_this = false;
5123 *p_must_clear = clear_this;
5126 *p_nz_elts += nz_elts;
5127 *p_elt_count += elt_count;
5129 return const_p;
5132 /* Examine CTOR to discover:
5133 * how many scalar fields are set to nonzero values,
5134 and place it in *P_NZ_ELTS;
5135 * how many scalar fields in total are in CTOR,
5136 and place it in *P_ELT_COUNT.
5137 * if a type is a union, and the initializer from the constructor
5138 is not the largest element in the union, then set *p_must_clear.
5140 Return whether or not CTOR is a valid static constant initializer, the same
5141 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5143 bool
5144 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5145 HOST_WIDE_INT *p_elt_count,
5146 bool *p_must_clear)
5148 *p_nz_elts = 0;
5149 *p_elt_count = 0;
5150 *p_must_clear = false;
5152 return
5153 categorize_ctor_elements_1 (ctor, p_nz_elts, p_elt_count, p_must_clear);
5156 /* Count the number of scalars in TYPE. Return -1 on overflow or
5157 variable-sized. If ALLOW_FLEXARR is true, don't count flexible
5158 array member at the end of the structure. */
5160 HOST_WIDE_INT
5161 count_type_elements (const_tree type, bool allow_flexarr)
5163 const HOST_WIDE_INT max = ~((HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT-1));
5164 switch (TREE_CODE (type))
5166 case ARRAY_TYPE:
5168 tree telts = array_type_nelts (type);
5169 if (telts && host_integerp (telts, 1))
5171 HOST_WIDE_INT n = tree_low_cst (telts, 1) + 1;
5172 HOST_WIDE_INT m = count_type_elements (TREE_TYPE (type), false);
5173 if (n == 0)
5174 return 0;
5175 else if (max / n > m)
5176 return n * m;
5178 return -1;
5181 case RECORD_TYPE:
5183 HOST_WIDE_INT n = 0, t;
5184 tree f;
5186 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5187 if (TREE_CODE (f) == FIELD_DECL)
5189 t = count_type_elements (TREE_TYPE (f), false);
5190 if (t < 0)
5192 /* Check for structures with flexible array member. */
5193 tree tf = TREE_TYPE (f);
5194 if (allow_flexarr
5195 && DECL_CHAIN (f) == NULL
5196 && TREE_CODE (tf) == ARRAY_TYPE
5197 && TYPE_DOMAIN (tf)
5198 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5199 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5200 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5201 && int_size_in_bytes (type) >= 0)
5202 break;
5204 return -1;
5206 n += t;
5209 return n;
5212 case UNION_TYPE:
5213 case QUAL_UNION_TYPE:
5214 return -1;
5216 case COMPLEX_TYPE:
5217 return 2;
5219 case VECTOR_TYPE:
5220 return TYPE_VECTOR_SUBPARTS (type);
5222 case INTEGER_TYPE:
5223 case REAL_TYPE:
5224 case FIXED_POINT_TYPE:
5225 case ENUMERAL_TYPE:
5226 case BOOLEAN_TYPE:
5227 case POINTER_TYPE:
5228 case OFFSET_TYPE:
5229 case REFERENCE_TYPE:
5230 return 1;
5232 case ERROR_MARK:
5233 return 0;
5235 case VOID_TYPE:
5236 case METHOD_TYPE:
5237 case FUNCTION_TYPE:
5238 case LANG_TYPE:
5239 default:
5240 gcc_unreachable ();
5244 /* Return 1 if EXP contains mostly (3/4) zeros. */
5246 static int
5247 mostly_zeros_p (const_tree exp)
5249 if (TREE_CODE (exp) == CONSTRUCTOR)
5252 HOST_WIDE_INT nz_elts, count, elts;
5253 bool must_clear;
5255 categorize_ctor_elements (exp, &nz_elts, &count, &must_clear);
5256 if (must_clear)
5257 return 1;
5259 elts = count_type_elements (TREE_TYPE (exp), false);
5261 return nz_elts < elts / 4;
5264 return initializer_zerop (exp);
5267 /* Return 1 if EXP contains all zeros. */
5269 static int
5270 all_zeros_p (const_tree exp)
5272 if (TREE_CODE (exp) == CONSTRUCTOR)
5275 HOST_WIDE_INT nz_elts, count;
5276 bool must_clear;
5278 categorize_ctor_elements (exp, &nz_elts, &count, &must_clear);
5279 return nz_elts == 0;
5282 return initializer_zerop (exp);
5285 /* Helper function for store_constructor.
5286 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5287 TYPE is the type of the CONSTRUCTOR, not the element type.
5288 CLEARED is as for store_constructor.
5289 ALIAS_SET is the alias set to use for any stores.
5291 This provides a recursive shortcut back to store_constructor when it isn't
5292 necessary to go through store_field. This is so that we can pass through
5293 the cleared field to let store_constructor know that we may not have to
5294 clear a substructure if the outer structure has already been cleared. */
5296 static void
5297 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5298 HOST_WIDE_INT bitpos, enum machine_mode mode,
5299 tree exp, tree type, int cleared,
5300 alias_set_type alias_set)
5302 if (TREE_CODE (exp) == CONSTRUCTOR
5303 /* We can only call store_constructor recursively if the size and
5304 bit position are on a byte boundary. */
5305 && bitpos % BITS_PER_UNIT == 0
5306 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5307 /* If we have a nonzero bitpos for a register target, then we just
5308 let store_field do the bitfield handling. This is unlikely to
5309 generate unnecessary clear instructions anyways. */
5310 && (bitpos == 0 || MEM_P (target)))
5312 if (MEM_P (target))
5313 target
5314 = adjust_address (target,
5315 GET_MODE (target) == BLKmode
5316 || 0 != (bitpos
5317 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5318 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5321 /* Update the alias set, if required. */
5322 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5323 && MEM_ALIAS_SET (target) != 0)
5325 target = copy_rtx (target);
5326 set_mem_alias_set (target, alias_set);
5329 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5331 else
5332 store_field (target, bitsize, bitpos, mode, exp, type, alias_set, false);
5335 /* Store the value of constructor EXP into the rtx TARGET.
5336 TARGET is either a REG or a MEM; we know it cannot conflict, since
5337 safe_from_p has been called.
5338 CLEARED is true if TARGET is known to have been zero'd.
5339 SIZE is the number of bytes of TARGET we are allowed to modify: this
5340 may not be the same as the size of EXP if we are assigning to a field
5341 which has been packed to exclude padding bits. */
5343 static void
5344 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5346 tree type = TREE_TYPE (exp);
5347 #ifdef WORD_REGISTER_OPERATIONS
5348 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5349 #endif
5351 switch (TREE_CODE (type))
5353 case RECORD_TYPE:
5354 case UNION_TYPE:
5355 case QUAL_UNION_TYPE:
5357 unsigned HOST_WIDE_INT idx;
5358 tree field, value;
5360 /* If size is zero or the target is already cleared, do nothing. */
5361 if (size == 0 || cleared)
5362 cleared = 1;
5363 /* We either clear the aggregate or indicate the value is dead. */
5364 else if ((TREE_CODE (type) == UNION_TYPE
5365 || TREE_CODE (type) == QUAL_UNION_TYPE)
5366 && ! CONSTRUCTOR_ELTS (exp))
5367 /* If the constructor is empty, clear the union. */
5369 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5370 cleared = 1;
5373 /* If we are building a static constructor into a register,
5374 set the initial value as zero so we can fold the value into
5375 a constant. But if more than one register is involved,
5376 this probably loses. */
5377 else if (REG_P (target) && TREE_STATIC (exp)
5378 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5380 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5381 cleared = 1;
5384 /* If the constructor has fewer fields than the structure or
5385 if we are initializing the structure to mostly zeros, clear
5386 the whole structure first. Don't do this if TARGET is a
5387 register whose mode size isn't equal to SIZE since
5388 clear_storage can't handle this case. */
5389 else if (size > 0
5390 && (((int)VEC_length (constructor_elt, CONSTRUCTOR_ELTS (exp))
5391 != fields_length (type))
5392 || mostly_zeros_p (exp))
5393 && (!REG_P (target)
5394 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5395 == size)))
5397 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5398 cleared = 1;
5401 if (REG_P (target) && !cleared)
5402 emit_clobber (target);
5404 /* Store each element of the constructor into the
5405 corresponding field of TARGET. */
5406 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
5408 enum machine_mode mode;
5409 HOST_WIDE_INT bitsize;
5410 HOST_WIDE_INT bitpos = 0;
5411 tree offset;
5412 rtx to_rtx = target;
5414 /* Just ignore missing fields. We cleared the whole
5415 structure, above, if any fields are missing. */
5416 if (field == 0)
5417 continue;
5419 if (cleared && initializer_zerop (value))
5420 continue;
5422 if (host_integerp (DECL_SIZE (field), 1))
5423 bitsize = tree_low_cst (DECL_SIZE (field), 1);
5424 else
5425 bitsize = -1;
5427 mode = DECL_MODE (field);
5428 if (DECL_BIT_FIELD (field))
5429 mode = VOIDmode;
5431 offset = DECL_FIELD_OFFSET (field);
5432 if (host_integerp (offset, 0)
5433 && host_integerp (bit_position (field), 0))
5435 bitpos = int_bit_position (field);
5436 offset = 0;
5438 else
5439 bitpos = tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 0);
5441 if (offset)
5443 enum machine_mode address_mode;
5444 rtx offset_rtx;
5446 offset
5447 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
5448 make_tree (TREE_TYPE (exp),
5449 target));
5451 offset_rtx = expand_normal (offset);
5452 gcc_assert (MEM_P (to_rtx));
5454 address_mode
5455 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (to_rtx));
5456 if (GET_MODE (offset_rtx) != address_mode)
5457 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5459 to_rtx = offset_address (to_rtx, offset_rtx,
5460 highest_pow2_factor (offset));
5463 #ifdef WORD_REGISTER_OPERATIONS
5464 /* If this initializes a field that is smaller than a
5465 word, at the start of a word, try to widen it to a full
5466 word. This special case allows us to output C++ member
5467 function initializations in a form that the optimizers
5468 can understand. */
5469 if (REG_P (target)
5470 && bitsize < BITS_PER_WORD
5471 && bitpos % BITS_PER_WORD == 0
5472 && GET_MODE_CLASS (mode) == MODE_INT
5473 && TREE_CODE (value) == INTEGER_CST
5474 && exp_size >= 0
5475 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
5477 tree type = TREE_TYPE (value);
5479 if (TYPE_PRECISION (type) < BITS_PER_WORD)
5481 type = lang_hooks.types.type_for_size
5482 (BITS_PER_WORD, TYPE_UNSIGNED (type));
5483 value = fold_convert (type, value);
5486 if (BYTES_BIG_ENDIAN)
5487 value
5488 = fold_build2 (LSHIFT_EXPR, type, value,
5489 build_int_cst (type,
5490 BITS_PER_WORD - bitsize));
5491 bitsize = BITS_PER_WORD;
5492 mode = word_mode;
5494 #endif
5496 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
5497 && DECL_NONADDRESSABLE_P (field))
5499 to_rtx = copy_rtx (to_rtx);
5500 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
5503 store_constructor_field (to_rtx, bitsize, bitpos, mode,
5504 value, type, cleared,
5505 get_alias_set (TREE_TYPE (field)));
5507 break;
5509 case ARRAY_TYPE:
5511 tree value, index;
5512 unsigned HOST_WIDE_INT i;
5513 int need_to_clear;
5514 tree domain;
5515 tree elttype = TREE_TYPE (type);
5516 int const_bounds_p;
5517 HOST_WIDE_INT minelt = 0;
5518 HOST_WIDE_INT maxelt = 0;
5520 domain = TYPE_DOMAIN (type);
5521 const_bounds_p = (TYPE_MIN_VALUE (domain)
5522 && TYPE_MAX_VALUE (domain)
5523 && host_integerp (TYPE_MIN_VALUE (domain), 0)
5524 && host_integerp (TYPE_MAX_VALUE (domain), 0));
5526 /* If we have constant bounds for the range of the type, get them. */
5527 if (const_bounds_p)
5529 minelt = tree_low_cst (TYPE_MIN_VALUE (domain), 0);
5530 maxelt = tree_low_cst (TYPE_MAX_VALUE (domain), 0);
5533 /* If the constructor has fewer elements than the array, clear
5534 the whole array first. Similarly if this is static
5535 constructor of a non-BLKmode object. */
5536 if (cleared)
5537 need_to_clear = 0;
5538 else if (REG_P (target) && TREE_STATIC (exp))
5539 need_to_clear = 1;
5540 else
5542 unsigned HOST_WIDE_INT idx;
5543 tree index, value;
5544 HOST_WIDE_INT count = 0, zero_count = 0;
5545 need_to_clear = ! const_bounds_p;
5547 /* This loop is a more accurate version of the loop in
5548 mostly_zeros_p (it handles RANGE_EXPR in an index). It
5549 is also needed to check for missing elements. */
5550 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
5552 HOST_WIDE_INT this_node_count;
5554 if (need_to_clear)
5555 break;
5557 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5559 tree lo_index = TREE_OPERAND (index, 0);
5560 tree hi_index = TREE_OPERAND (index, 1);
5562 if (! host_integerp (lo_index, 1)
5563 || ! host_integerp (hi_index, 1))
5565 need_to_clear = 1;
5566 break;
5569 this_node_count = (tree_low_cst (hi_index, 1)
5570 - tree_low_cst (lo_index, 1) + 1);
5572 else
5573 this_node_count = 1;
5575 count += this_node_count;
5576 if (mostly_zeros_p (value))
5577 zero_count += this_node_count;
5580 /* Clear the entire array first if there are any missing
5581 elements, or if the incidence of zero elements is >=
5582 75%. */
5583 if (! need_to_clear
5584 && (count < maxelt - minelt + 1
5585 || 4 * zero_count >= 3 * count))
5586 need_to_clear = 1;
5589 if (need_to_clear && size > 0)
5591 if (REG_P (target))
5592 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5593 else
5594 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5595 cleared = 1;
5598 if (!cleared && REG_P (target))
5599 /* Inform later passes that the old value is dead. */
5600 emit_clobber (target);
5602 /* Store each element of the constructor into the
5603 corresponding element of TARGET, determined by counting the
5604 elements. */
5605 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
5607 enum machine_mode mode;
5608 HOST_WIDE_INT bitsize;
5609 HOST_WIDE_INT bitpos;
5610 rtx xtarget = target;
5612 if (cleared && initializer_zerop (value))
5613 continue;
5615 mode = TYPE_MODE (elttype);
5616 if (mode == BLKmode)
5617 bitsize = (host_integerp (TYPE_SIZE (elttype), 1)
5618 ? tree_low_cst (TYPE_SIZE (elttype), 1)
5619 : -1);
5620 else
5621 bitsize = GET_MODE_BITSIZE (mode);
5623 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5625 tree lo_index = TREE_OPERAND (index, 0);
5626 tree hi_index = TREE_OPERAND (index, 1);
5627 rtx index_r, pos_rtx;
5628 HOST_WIDE_INT lo, hi, count;
5629 tree position;
5631 /* If the range is constant and "small", unroll the loop. */
5632 if (const_bounds_p
5633 && host_integerp (lo_index, 0)
5634 && host_integerp (hi_index, 0)
5635 && (lo = tree_low_cst (lo_index, 0),
5636 hi = tree_low_cst (hi_index, 0),
5637 count = hi - lo + 1,
5638 (!MEM_P (target)
5639 || count <= 2
5640 || (host_integerp (TYPE_SIZE (elttype), 1)
5641 && (tree_low_cst (TYPE_SIZE (elttype), 1) * count
5642 <= 40 * 8)))))
5644 lo -= minelt; hi -= minelt;
5645 for (; lo <= hi; lo++)
5647 bitpos = lo * tree_low_cst (TYPE_SIZE (elttype), 0);
5649 if (MEM_P (target)
5650 && !MEM_KEEP_ALIAS_SET_P (target)
5651 && TREE_CODE (type) == ARRAY_TYPE
5652 && TYPE_NONALIASED_COMPONENT (type))
5654 target = copy_rtx (target);
5655 MEM_KEEP_ALIAS_SET_P (target) = 1;
5658 store_constructor_field
5659 (target, bitsize, bitpos, mode, value, type, cleared,
5660 get_alias_set (elttype));
5663 else
5665 rtx loop_start = gen_label_rtx ();
5666 rtx loop_end = gen_label_rtx ();
5667 tree exit_cond;
5669 expand_normal (hi_index);
5671 index = build_decl (EXPR_LOCATION (exp),
5672 VAR_DECL, NULL_TREE, domain);
5673 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
5674 SET_DECL_RTL (index, index_r);
5675 store_expr (lo_index, index_r, 0, false);
5677 /* Build the head of the loop. */
5678 do_pending_stack_adjust ();
5679 emit_label (loop_start);
5681 /* Assign value to element index. */
5682 position =
5683 fold_convert (ssizetype,
5684 fold_build2 (MINUS_EXPR,
5685 TREE_TYPE (index),
5686 index,
5687 TYPE_MIN_VALUE (domain)));
5689 position =
5690 size_binop (MULT_EXPR, position,
5691 fold_convert (ssizetype,
5692 TYPE_SIZE_UNIT (elttype)));
5694 pos_rtx = expand_normal (position);
5695 xtarget = offset_address (target, pos_rtx,
5696 highest_pow2_factor (position));
5697 xtarget = adjust_address (xtarget, mode, 0);
5698 if (TREE_CODE (value) == CONSTRUCTOR)
5699 store_constructor (value, xtarget, cleared,
5700 bitsize / BITS_PER_UNIT);
5701 else
5702 store_expr (value, xtarget, 0, false);
5704 /* Generate a conditional jump to exit the loop. */
5705 exit_cond = build2 (LT_EXPR, integer_type_node,
5706 index, hi_index);
5707 jumpif (exit_cond, loop_end, -1);
5709 /* Update the loop counter, and jump to the head of
5710 the loop. */
5711 expand_assignment (index,
5712 build2 (PLUS_EXPR, TREE_TYPE (index),
5713 index, integer_one_node),
5714 false);
5716 emit_jump (loop_start);
5718 /* Build the end of the loop. */
5719 emit_label (loop_end);
5722 else if ((index != 0 && ! host_integerp (index, 0))
5723 || ! host_integerp (TYPE_SIZE (elttype), 1))
5725 tree position;
5727 if (index == 0)
5728 index = ssize_int (1);
5730 if (minelt)
5731 index = fold_convert (ssizetype,
5732 fold_build2 (MINUS_EXPR,
5733 TREE_TYPE (index),
5734 index,
5735 TYPE_MIN_VALUE (domain)));
5737 position =
5738 size_binop (MULT_EXPR, index,
5739 fold_convert (ssizetype,
5740 TYPE_SIZE_UNIT (elttype)));
5741 xtarget = offset_address (target,
5742 expand_normal (position),
5743 highest_pow2_factor (position));
5744 xtarget = adjust_address (xtarget, mode, 0);
5745 store_expr (value, xtarget, 0, false);
5747 else
5749 if (index != 0)
5750 bitpos = ((tree_low_cst (index, 0) - minelt)
5751 * tree_low_cst (TYPE_SIZE (elttype), 1));
5752 else
5753 bitpos = (i * tree_low_cst (TYPE_SIZE (elttype), 1));
5755 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
5756 && TREE_CODE (type) == ARRAY_TYPE
5757 && TYPE_NONALIASED_COMPONENT (type))
5759 target = copy_rtx (target);
5760 MEM_KEEP_ALIAS_SET_P (target) = 1;
5762 store_constructor_field (target, bitsize, bitpos, mode, value,
5763 type, cleared, get_alias_set (elttype));
5766 break;
5769 case VECTOR_TYPE:
5771 unsigned HOST_WIDE_INT idx;
5772 constructor_elt *ce;
5773 int i;
5774 int need_to_clear;
5775 int icode = 0;
5776 tree elttype = TREE_TYPE (type);
5777 int elt_size = tree_low_cst (TYPE_SIZE (elttype), 1);
5778 enum machine_mode eltmode = TYPE_MODE (elttype);
5779 HOST_WIDE_INT bitsize;
5780 HOST_WIDE_INT bitpos;
5781 rtvec vector = NULL;
5782 unsigned n_elts;
5783 alias_set_type alias;
5785 gcc_assert (eltmode != BLKmode);
5787 n_elts = TYPE_VECTOR_SUBPARTS (type);
5788 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
5790 enum machine_mode mode = GET_MODE (target);
5792 icode = (int) optab_handler (vec_init_optab, mode);
5793 if (icode != CODE_FOR_nothing)
5795 unsigned int i;
5797 vector = rtvec_alloc (n_elts);
5798 for (i = 0; i < n_elts; i++)
5799 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
5803 /* If the constructor has fewer elements than the vector,
5804 clear the whole array first. Similarly if this is static
5805 constructor of a non-BLKmode object. */
5806 if (cleared)
5807 need_to_clear = 0;
5808 else if (REG_P (target) && TREE_STATIC (exp))
5809 need_to_clear = 1;
5810 else
5812 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
5813 tree value;
5815 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
5817 int n_elts_here = tree_low_cst
5818 (int_const_binop (TRUNC_DIV_EXPR,
5819 TYPE_SIZE (TREE_TYPE (value)),
5820 TYPE_SIZE (elttype), 0), 1);
5822 count += n_elts_here;
5823 if (mostly_zeros_p (value))
5824 zero_count += n_elts_here;
5827 /* Clear the entire vector first if there are any missing elements,
5828 or if the incidence of zero elements is >= 75%. */
5829 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
5832 if (need_to_clear && size > 0 && !vector)
5834 if (REG_P (target))
5835 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5836 else
5837 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5838 cleared = 1;
5841 /* Inform later passes that the old value is dead. */
5842 if (!cleared && !vector && REG_P (target))
5843 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5845 if (MEM_P (target))
5846 alias = MEM_ALIAS_SET (target);
5847 else
5848 alias = get_alias_set (elttype);
5850 /* Store each element of the constructor into the corresponding
5851 element of TARGET, determined by counting the elements. */
5852 for (idx = 0, i = 0;
5853 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce);
5854 idx++, i += bitsize / elt_size)
5856 HOST_WIDE_INT eltpos;
5857 tree value = ce->value;
5859 bitsize = tree_low_cst (TYPE_SIZE (TREE_TYPE (value)), 1);
5860 if (cleared && initializer_zerop (value))
5861 continue;
5863 if (ce->index)
5864 eltpos = tree_low_cst (ce->index, 1);
5865 else
5866 eltpos = i;
5868 if (vector)
5870 /* Vector CONSTRUCTORs should only be built from smaller
5871 vectors in the case of BLKmode vectors. */
5872 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
5873 RTVEC_ELT (vector, eltpos)
5874 = expand_normal (value);
5876 else
5878 enum machine_mode value_mode =
5879 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
5880 ? TYPE_MODE (TREE_TYPE (value))
5881 : eltmode;
5882 bitpos = eltpos * elt_size;
5883 store_constructor_field (target, bitsize, bitpos,
5884 value_mode, value, type,
5885 cleared, alias);
5889 if (vector)
5890 emit_insn (GEN_FCN (icode)
5891 (target,
5892 gen_rtx_PARALLEL (GET_MODE (target), vector)));
5893 break;
5896 default:
5897 gcc_unreachable ();
5901 /* Store the value of EXP (an expression tree)
5902 into a subfield of TARGET which has mode MODE and occupies
5903 BITSIZE bits, starting BITPOS bits from the start of TARGET.
5904 If MODE is VOIDmode, it means that we are storing into a bit-field.
5906 Always return const0_rtx unless we have something particular to
5907 return.
5909 TYPE is the type of the underlying object,
5911 ALIAS_SET is the alias set for the destination. This value will
5912 (in general) be different from that for TARGET, since TARGET is a
5913 reference to the containing structure.
5915 If NONTEMPORAL is true, try generating a nontemporal store. */
5917 static rtx
5918 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
5919 enum machine_mode mode, tree exp, tree type,
5920 alias_set_type alias_set, bool nontemporal)
5922 if (TREE_CODE (exp) == ERROR_MARK)
5923 return const0_rtx;
5925 /* If we have nothing to store, do nothing unless the expression has
5926 side-effects. */
5927 if (bitsize == 0)
5928 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5930 /* If we are storing into an unaligned field of an aligned union that is
5931 in a register, we may have the mode of TARGET being an integer mode but
5932 MODE == BLKmode. In that case, get an aligned object whose size and
5933 alignment are the same as TARGET and store TARGET into it (we can avoid
5934 the store if the field being stored is the entire width of TARGET). Then
5935 call ourselves recursively to store the field into a BLKmode version of
5936 that object. Finally, load from the object into TARGET. This is not
5937 very efficient in general, but should only be slightly more expensive
5938 than the otherwise-required unaligned accesses. Perhaps this can be
5939 cleaned up later. It's tempting to make OBJECT readonly, but it's set
5940 twice, once with emit_move_insn and once via store_field. */
5942 if (mode == BLKmode
5943 && (REG_P (target) || GET_CODE (target) == SUBREG))
5945 rtx object = assign_temp (type, 0, 1, 1);
5946 rtx blk_object = adjust_address (object, BLKmode, 0);
5948 if (bitsize != (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (target)))
5949 emit_move_insn (object, target);
5951 store_field (blk_object, bitsize, bitpos, mode, exp, type, alias_set,
5952 nontemporal);
5954 emit_move_insn (target, object);
5956 /* We want to return the BLKmode version of the data. */
5957 return blk_object;
5960 if (GET_CODE (target) == CONCAT)
5962 /* We're storing into a struct containing a single __complex. */
5964 gcc_assert (!bitpos);
5965 return store_expr (exp, target, 0, nontemporal);
5968 /* If the structure is in a register or if the component
5969 is a bit field, we cannot use addressing to access it.
5970 Use bit-field techniques or SUBREG to store in it. */
5972 if (mode == VOIDmode
5973 || (mode != BLKmode && ! direct_store[(int) mode]
5974 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
5975 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
5976 || REG_P (target)
5977 || GET_CODE (target) == SUBREG
5978 /* If the field isn't aligned enough to store as an ordinary memref,
5979 store it as a bit field. */
5980 || (mode != BLKmode
5981 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
5982 || bitpos % GET_MODE_ALIGNMENT (mode))
5983 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
5984 || (bitpos % BITS_PER_UNIT != 0)))
5985 /* If the RHS and field are a constant size and the size of the
5986 RHS isn't the same size as the bitfield, we must use bitfield
5987 operations. */
5988 || (bitsize >= 0
5989 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
5990 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
5991 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
5992 decl we must use bitfield operations. */
5993 || (bitsize >= 0
5994 && TREE_CODE (exp) == MEM_REF
5995 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5996 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5997 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
5998 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6000 rtx temp;
6001 gimple nop_def;
6003 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6004 implies a mask operation. If the precision is the same size as
6005 the field we're storing into, that mask is redundant. This is
6006 particularly common with bit field assignments generated by the
6007 C front end. */
6008 nop_def = get_def_for_expr (exp, NOP_EXPR);
6009 if (nop_def)
6011 tree type = TREE_TYPE (exp);
6012 if (INTEGRAL_TYPE_P (type)
6013 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6014 && bitsize == TYPE_PRECISION (type))
6016 tree op = gimple_assign_rhs1 (nop_def);
6017 type = TREE_TYPE (op);
6018 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6019 exp = op;
6023 temp = expand_normal (exp);
6025 /* If BITSIZE is narrower than the size of the type of EXP
6026 we will be narrowing TEMP. Normally, what's wanted are the
6027 low-order bits. However, if EXP's type is a record and this is
6028 big-endian machine, we want the upper BITSIZE bits. */
6029 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6030 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6031 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6032 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6033 size_int (GET_MODE_BITSIZE (GET_MODE (temp))
6034 - bitsize),
6035 NULL_RTX, 1);
6037 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to
6038 MODE. */
6039 if (mode != VOIDmode && mode != BLKmode
6040 && mode != TYPE_MODE (TREE_TYPE (exp)))
6041 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6043 /* If the modes of TEMP and TARGET are both BLKmode, both
6044 must be in memory and BITPOS must be aligned on a byte
6045 boundary. If so, we simply do a block copy. Likewise
6046 for a BLKmode-like TARGET. */
6047 if (GET_MODE (temp) == BLKmode
6048 && (GET_MODE (target) == BLKmode
6049 || (MEM_P (target)
6050 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6051 && (bitpos % BITS_PER_UNIT) == 0
6052 && (bitsize % BITS_PER_UNIT) == 0)))
6054 gcc_assert (MEM_P (target) && MEM_P (temp)
6055 && (bitpos % BITS_PER_UNIT) == 0);
6057 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6058 emit_block_move (target, temp,
6059 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6060 / BITS_PER_UNIT),
6061 BLOCK_OP_NORMAL);
6063 return const0_rtx;
6066 /* Store the value in the bitfield. */
6067 store_bit_field (target, bitsize, bitpos, mode, temp);
6069 return const0_rtx;
6071 else
6073 /* Now build a reference to just the desired component. */
6074 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6076 if (to_rtx == target)
6077 to_rtx = copy_rtx (to_rtx);
6079 if (!MEM_SCALAR_P (to_rtx))
6080 MEM_IN_STRUCT_P (to_rtx) = 1;
6081 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6082 set_mem_alias_set (to_rtx, alias_set);
6084 return store_expr (exp, to_rtx, 0, nontemporal);
6088 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6089 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6090 codes and find the ultimate containing object, which we return.
6092 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6093 bit position, and *PUNSIGNEDP to the signedness of the field.
6094 If the position of the field is variable, we store a tree
6095 giving the variable offset (in units) in *POFFSET.
6096 This offset is in addition to the bit position.
6097 If the position is not variable, we store 0 in *POFFSET.
6099 If any of the extraction expressions is volatile,
6100 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6102 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6103 Otherwise, it is a mode that can be used to access the field.
6105 If the field describes a variable-sized object, *PMODE is set to
6106 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6107 this case, but the address of the object can be found.
6109 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6110 look through nodes that serve as markers of a greater alignment than
6111 the one that can be deduced from the expression. These nodes make it
6112 possible for front-ends to prevent temporaries from being created by
6113 the middle-end on alignment considerations. For that purpose, the
6114 normal operating mode at high-level is to always pass FALSE so that
6115 the ultimate containing object is really returned; moreover, the
6116 associated predicate handled_component_p will always return TRUE
6117 on these nodes, thus indicating that they are essentially handled
6118 by get_inner_reference. TRUE should only be passed when the caller
6119 is scanning the expression in order to build another representation
6120 and specifically knows how to handle these nodes; as such, this is
6121 the normal operating mode in the RTL expanders. */
6123 tree
6124 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6125 HOST_WIDE_INT *pbitpos, tree *poffset,
6126 enum machine_mode *pmode, int *punsignedp,
6127 int *pvolatilep, bool keep_aligning)
6129 tree size_tree = 0;
6130 enum machine_mode mode = VOIDmode;
6131 bool blkmode_bitfield = false;
6132 tree offset = size_zero_node;
6133 double_int bit_offset = double_int_zero;
6135 /* First get the mode, signedness, and size. We do this from just the
6136 outermost expression. */
6137 *pbitsize = -1;
6138 if (TREE_CODE (exp) == COMPONENT_REF)
6140 tree field = TREE_OPERAND (exp, 1);
6141 size_tree = DECL_SIZE (field);
6142 if (!DECL_BIT_FIELD (field))
6143 mode = DECL_MODE (field);
6144 else if (DECL_MODE (field) == BLKmode)
6145 blkmode_bitfield = true;
6146 else if (TREE_THIS_VOLATILE (exp)
6147 && flag_strict_volatile_bitfields > 0)
6148 /* Volatile bitfields should be accessed in the mode of the
6149 field's type, not the mode computed based on the bit
6150 size. */
6151 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6153 *punsignedp = DECL_UNSIGNED (field);
6155 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6157 size_tree = TREE_OPERAND (exp, 1);
6158 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6159 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6161 /* For vector types, with the correct size of access, use the mode of
6162 inner type. */
6163 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6164 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6165 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6166 mode = TYPE_MODE (TREE_TYPE (exp));
6168 else
6170 mode = TYPE_MODE (TREE_TYPE (exp));
6171 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6173 if (mode == BLKmode)
6174 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6175 else
6176 *pbitsize = GET_MODE_BITSIZE (mode);
6179 if (size_tree != 0)
6181 if (! host_integerp (size_tree, 1))
6182 mode = BLKmode, *pbitsize = -1;
6183 else
6184 *pbitsize = tree_low_cst (size_tree, 1);
6187 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6188 and find the ultimate containing object. */
6189 while (1)
6191 switch (TREE_CODE (exp))
6193 case BIT_FIELD_REF:
6194 bit_offset
6195 = double_int_add (bit_offset,
6196 tree_to_double_int (TREE_OPERAND (exp, 2)));
6197 break;
6199 case COMPONENT_REF:
6201 tree field = TREE_OPERAND (exp, 1);
6202 tree this_offset = component_ref_field_offset (exp);
6204 /* If this field hasn't been filled in yet, don't go past it.
6205 This should only happen when folding expressions made during
6206 type construction. */
6207 if (this_offset == 0)
6208 break;
6210 offset = size_binop (PLUS_EXPR, offset, this_offset);
6211 bit_offset = double_int_add (bit_offset,
6212 tree_to_double_int
6213 (DECL_FIELD_BIT_OFFSET (field)));
6215 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6217 break;
6219 case ARRAY_REF:
6220 case ARRAY_RANGE_REF:
6222 tree index = TREE_OPERAND (exp, 1);
6223 tree low_bound = array_ref_low_bound (exp);
6224 tree unit_size = array_ref_element_size (exp);
6226 /* We assume all arrays have sizes that are a multiple of a byte.
6227 First subtract the lower bound, if any, in the type of the
6228 index, then convert to sizetype and multiply by the size of
6229 the array element. */
6230 if (! integer_zerop (low_bound))
6231 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6232 index, low_bound);
6234 offset = size_binop (PLUS_EXPR, offset,
6235 size_binop (MULT_EXPR,
6236 fold_convert (sizetype, index),
6237 unit_size));
6239 break;
6241 case REALPART_EXPR:
6242 break;
6244 case IMAGPART_EXPR:
6245 bit_offset = double_int_add (bit_offset,
6246 uhwi_to_double_int (*pbitsize));
6247 break;
6249 case VIEW_CONVERT_EXPR:
6250 if (keep_aligning && STRICT_ALIGNMENT
6251 && (TYPE_ALIGN (TREE_TYPE (exp))
6252 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6253 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6254 < BIGGEST_ALIGNMENT)
6255 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6256 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6257 goto done;
6258 break;
6260 case MEM_REF:
6261 /* Hand back the decl for MEM[&decl, off]. */
6262 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6264 tree off = TREE_OPERAND (exp, 1);
6265 if (!integer_zerop (off))
6267 double_int boff, coff = mem_ref_offset (exp);
6268 boff = double_int_lshift (coff,
6269 BITS_PER_UNIT == 8
6270 ? 3 : exact_log2 (BITS_PER_UNIT),
6271 HOST_BITS_PER_DOUBLE_INT, true);
6272 bit_offset = double_int_add (bit_offset, boff);
6274 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6276 goto done;
6278 default:
6279 goto done;
6282 /* If any reference in the chain is volatile, the effect is volatile. */
6283 if (TREE_THIS_VOLATILE (exp))
6284 *pvolatilep = 1;
6286 exp = TREE_OPERAND (exp, 0);
6288 done:
6290 /* If OFFSET is constant, see if we can return the whole thing as a
6291 constant bit position. Make sure to handle overflow during
6292 this conversion. */
6293 if (host_integerp (offset, 0))
6295 double_int tem = double_int_lshift (tree_to_double_int (offset),
6296 BITS_PER_UNIT == 8
6297 ? 3 : exact_log2 (BITS_PER_UNIT),
6298 HOST_BITS_PER_DOUBLE_INT, true);
6299 tem = double_int_add (tem, bit_offset);
6300 if (double_int_fits_in_shwi_p (tem))
6302 *pbitpos = double_int_to_shwi (tem);
6303 *poffset = offset = NULL_TREE;
6307 /* Otherwise, split it up. */
6308 if (offset)
6310 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6311 if (double_int_negative_p (bit_offset))
6313 double_int mask
6314 = double_int_mask (BITS_PER_UNIT == 8
6315 ? 3 : exact_log2 (BITS_PER_UNIT));
6316 double_int tem = double_int_and_not (bit_offset, mask);
6317 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6318 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6319 bit_offset = double_int_sub (bit_offset, tem);
6320 tem = double_int_rshift (tem,
6321 BITS_PER_UNIT == 8
6322 ? 3 : exact_log2 (BITS_PER_UNIT),
6323 HOST_BITS_PER_DOUBLE_INT, true);
6324 offset = size_binop (PLUS_EXPR, offset,
6325 double_int_to_tree (sizetype, tem));
6328 *pbitpos = double_int_to_shwi (bit_offset);
6329 *poffset = offset;
6332 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6333 if (mode == VOIDmode
6334 && blkmode_bitfield
6335 && (*pbitpos % BITS_PER_UNIT) == 0
6336 && (*pbitsize % BITS_PER_UNIT) == 0)
6337 *pmode = BLKmode;
6338 else
6339 *pmode = mode;
6341 return exp;
6344 /* Given an expression EXP that may be a COMPONENT_REF, an ARRAY_REF or an
6345 ARRAY_RANGE_REF, look for whether EXP or any nested component-refs within
6346 EXP is marked as PACKED. */
6348 bool
6349 contains_packed_reference (const_tree exp)
6351 bool packed_p = false;
6353 while (1)
6355 switch (TREE_CODE (exp))
6357 case COMPONENT_REF:
6359 tree field = TREE_OPERAND (exp, 1);
6360 packed_p = DECL_PACKED (field)
6361 || TYPE_PACKED (TREE_TYPE (field))
6362 || TYPE_PACKED (TREE_TYPE (exp));
6363 if (packed_p)
6364 goto done;
6366 break;
6368 case BIT_FIELD_REF:
6369 case ARRAY_REF:
6370 case ARRAY_RANGE_REF:
6371 case REALPART_EXPR:
6372 case IMAGPART_EXPR:
6373 case VIEW_CONVERT_EXPR:
6374 break;
6376 default:
6377 goto done;
6379 exp = TREE_OPERAND (exp, 0);
6381 done:
6382 return packed_p;
6385 /* Return a tree of sizetype representing the size, in bytes, of the element
6386 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6388 tree
6389 array_ref_element_size (tree exp)
6391 tree aligned_size = TREE_OPERAND (exp, 3);
6392 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6393 location_t loc = EXPR_LOCATION (exp);
6395 /* If a size was specified in the ARRAY_REF, it's the size measured
6396 in alignment units of the element type. So multiply by that value. */
6397 if (aligned_size)
6399 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6400 sizetype from another type of the same width and signedness. */
6401 if (TREE_TYPE (aligned_size) != sizetype)
6402 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6403 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6404 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6407 /* Otherwise, take the size from that of the element type. Substitute
6408 any PLACEHOLDER_EXPR that we have. */
6409 else
6410 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6413 /* Return a tree representing the lower bound of the array mentioned in
6414 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6416 tree
6417 array_ref_low_bound (tree exp)
6419 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6421 /* If a lower bound is specified in EXP, use it. */
6422 if (TREE_OPERAND (exp, 2))
6423 return TREE_OPERAND (exp, 2);
6425 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6426 substituting for a PLACEHOLDER_EXPR as needed. */
6427 if (domain_type && TYPE_MIN_VALUE (domain_type))
6428 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6430 /* Otherwise, return a zero of the appropriate type. */
6431 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
6434 /* Return a tree representing the upper bound of the array mentioned in
6435 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6437 tree
6438 array_ref_up_bound (tree exp)
6440 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6442 /* If there is a domain type and it has an upper bound, use it, substituting
6443 for a PLACEHOLDER_EXPR as needed. */
6444 if (domain_type && TYPE_MAX_VALUE (domain_type))
6445 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
6447 /* Otherwise fail. */
6448 return NULL_TREE;
6451 /* Return a tree representing the offset, in bytes, of the field referenced
6452 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
6454 tree
6455 component_ref_field_offset (tree exp)
6457 tree aligned_offset = TREE_OPERAND (exp, 2);
6458 tree field = TREE_OPERAND (exp, 1);
6459 location_t loc = EXPR_LOCATION (exp);
6461 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
6462 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
6463 value. */
6464 if (aligned_offset)
6466 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6467 sizetype from another type of the same width and signedness. */
6468 if (TREE_TYPE (aligned_offset) != sizetype)
6469 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
6470 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
6471 size_int (DECL_OFFSET_ALIGN (field)
6472 / BITS_PER_UNIT));
6475 /* Otherwise, take the offset from that of the field. Substitute
6476 any PLACEHOLDER_EXPR that we have. */
6477 else
6478 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
6481 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
6483 static unsigned HOST_WIDE_INT
6484 target_align (const_tree target)
6486 /* We might have a chain of nested references with intermediate misaligning
6487 bitfields components, so need to recurse to find out. */
6489 unsigned HOST_WIDE_INT this_align, outer_align;
6491 switch (TREE_CODE (target))
6493 case BIT_FIELD_REF:
6494 return 1;
6496 case COMPONENT_REF:
6497 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
6498 outer_align = target_align (TREE_OPERAND (target, 0));
6499 return MIN (this_align, outer_align);
6501 case ARRAY_REF:
6502 case ARRAY_RANGE_REF:
6503 this_align = TYPE_ALIGN (TREE_TYPE (target));
6504 outer_align = target_align (TREE_OPERAND (target, 0));
6505 return MIN (this_align, outer_align);
6507 CASE_CONVERT:
6508 case NON_LVALUE_EXPR:
6509 case VIEW_CONVERT_EXPR:
6510 this_align = TYPE_ALIGN (TREE_TYPE (target));
6511 outer_align = target_align (TREE_OPERAND (target, 0));
6512 return MAX (this_align, outer_align);
6514 default:
6515 return TYPE_ALIGN (TREE_TYPE (target));
6520 /* Given an rtx VALUE that may contain additions and multiplications, return
6521 an equivalent value that just refers to a register, memory, or constant.
6522 This is done by generating instructions to perform the arithmetic and
6523 returning a pseudo-register containing the value.
6525 The returned value may be a REG, SUBREG, MEM or constant. */
6528 force_operand (rtx value, rtx target)
6530 rtx op1, op2;
6531 /* Use subtarget as the target for operand 0 of a binary operation. */
6532 rtx subtarget = get_subtarget (target);
6533 enum rtx_code code = GET_CODE (value);
6535 /* Check for subreg applied to an expression produced by loop optimizer. */
6536 if (code == SUBREG
6537 && !REG_P (SUBREG_REG (value))
6538 && !MEM_P (SUBREG_REG (value)))
6540 value
6541 = simplify_gen_subreg (GET_MODE (value),
6542 force_reg (GET_MODE (SUBREG_REG (value)),
6543 force_operand (SUBREG_REG (value),
6544 NULL_RTX)),
6545 GET_MODE (SUBREG_REG (value)),
6546 SUBREG_BYTE (value));
6547 code = GET_CODE (value);
6550 /* Check for a PIC address load. */
6551 if ((code == PLUS || code == MINUS)
6552 && XEXP (value, 0) == pic_offset_table_rtx
6553 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
6554 || GET_CODE (XEXP (value, 1)) == LABEL_REF
6555 || GET_CODE (XEXP (value, 1)) == CONST))
6557 if (!subtarget)
6558 subtarget = gen_reg_rtx (GET_MODE (value));
6559 emit_move_insn (subtarget, value);
6560 return subtarget;
6563 if (ARITHMETIC_P (value))
6565 op2 = XEXP (value, 1);
6566 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
6567 subtarget = 0;
6568 if (code == MINUS && CONST_INT_P (op2))
6570 code = PLUS;
6571 op2 = negate_rtx (GET_MODE (value), op2);
6574 /* Check for an addition with OP2 a constant integer and our first
6575 operand a PLUS of a virtual register and something else. In that
6576 case, we want to emit the sum of the virtual register and the
6577 constant first and then add the other value. This allows virtual
6578 register instantiation to simply modify the constant rather than
6579 creating another one around this addition. */
6580 if (code == PLUS && CONST_INT_P (op2)
6581 && GET_CODE (XEXP (value, 0)) == PLUS
6582 && REG_P (XEXP (XEXP (value, 0), 0))
6583 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
6584 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
6586 rtx temp = expand_simple_binop (GET_MODE (value), code,
6587 XEXP (XEXP (value, 0), 0), op2,
6588 subtarget, 0, OPTAB_LIB_WIDEN);
6589 return expand_simple_binop (GET_MODE (value), code, temp,
6590 force_operand (XEXP (XEXP (value,
6591 0), 1), 0),
6592 target, 0, OPTAB_LIB_WIDEN);
6595 op1 = force_operand (XEXP (value, 0), subtarget);
6596 op2 = force_operand (op2, NULL_RTX);
6597 switch (code)
6599 case MULT:
6600 return expand_mult (GET_MODE (value), op1, op2, target, 1);
6601 case DIV:
6602 if (!INTEGRAL_MODE_P (GET_MODE (value)))
6603 return expand_simple_binop (GET_MODE (value), code, op1, op2,
6604 target, 1, OPTAB_LIB_WIDEN);
6605 else
6606 return expand_divmod (0,
6607 FLOAT_MODE_P (GET_MODE (value))
6608 ? RDIV_EXPR : TRUNC_DIV_EXPR,
6609 GET_MODE (value), op1, op2, target, 0);
6610 case MOD:
6611 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
6612 target, 0);
6613 case UDIV:
6614 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
6615 target, 1);
6616 case UMOD:
6617 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
6618 target, 1);
6619 case ASHIFTRT:
6620 return expand_simple_binop (GET_MODE (value), code, op1, op2,
6621 target, 0, OPTAB_LIB_WIDEN);
6622 default:
6623 return expand_simple_binop (GET_MODE (value), code, op1, op2,
6624 target, 1, OPTAB_LIB_WIDEN);
6627 if (UNARY_P (value))
6629 if (!target)
6630 target = gen_reg_rtx (GET_MODE (value));
6631 op1 = force_operand (XEXP (value, 0), NULL_RTX);
6632 switch (code)
6634 case ZERO_EXTEND:
6635 case SIGN_EXTEND:
6636 case TRUNCATE:
6637 case FLOAT_EXTEND:
6638 case FLOAT_TRUNCATE:
6639 convert_move (target, op1, code == ZERO_EXTEND);
6640 return target;
6642 case FIX:
6643 case UNSIGNED_FIX:
6644 expand_fix (target, op1, code == UNSIGNED_FIX);
6645 return target;
6647 case FLOAT:
6648 case UNSIGNED_FLOAT:
6649 expand_float (target, op1, code == UNSIGNED_FLOAT);
6650 return target;
6652 default:
6653 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
6657 #ifdef INSN_SCHEDULING
6658 /* On machines that have insn scheduling, we want all memory reference to be
6659 explicit, so we need to deal with such paradoxical SUBREGs. */
6660 if (GET_CODE (value) == SUBREG && MEM_P (SUBREG_REG (value))
6661 && (GET_MODE_SIZE (GET_MODE (value))
6662 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (value)))))
6663 value
6664 = simplify_gen_subreg (GET_MODE (value),
6665 force_reg (GET_MODE (SUBREG_REG (value)),
6666 force_operand (SUBREG_REG (value),
6667 NULL_RTX)),
6668 GET_MODE (SUBREG_REG (value)),
6669 SUBREG_BYTE (value));
6670 #endif
6672 return value;
6675 /* Subroutine of expand_expr: return nonzero iff there is no way that
6676 EXP can reference X, which is being modified. TOP_P is nonzero if this
6677 call is going to be used to determine whether we need a temporary
6678 for EXP, as opposed to a recursive call to this function.
6680 It is always safe for this routine to return zero since it merely
6681 searches for optimization opportunities. */
6684 safe_from_p (const_rtx x, tree exp, int top_p)
6686 rtx exp_rtl = 0;
6687 int i, nops;
6689 if (x == 0
6690 /* If EXP has varying size, we MUST use a target since we currently
6691 have no way of allocating temporaries of variable size
6692 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
6693 So we assume here that something at a higher level has prevented a
6694 clash. This is somewhat bogus, but the best we can do. Only
6695 do this when X is BLKmode and when we are at the top level. */
6696 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
6697 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
6698 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
6699 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
6700 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
6701 != INTEGER_CST)
6702 && GET_MODE (x) == BLKmode)
6703 /* If X is in the outgoing argument area, it is always safe. */
6704 || (MEM_P (x)
6705 && (XEXP (x, 0) == virtual_outgoing_args_rtx
6706 || (GET_CODE (XEXP (x, 0)) == PLUS
6707 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
6708 return 1;
6710 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
6711 find the underlying pseudo. */
6712 if (GET_CODE (x) == SUBREG)
6714 x = SUBREG_REG (x);
6715 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
6716 return 0;
6719 /* Now look at our tree code and possibly recurse. */
6720 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
6722 case tcc_declaration:
6723 exp_rtl = DECL_RTL_IF_SET (exp);
6724 break;
6726 case tcc_constant:
6727 return 1;
6729 case tcc_exceptional:
6730 if (TREE_CODE (exp) == TREE_LIST)
6732 while (1)
6734 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
6735 return 0;
6736 exp = TREE_CHAIN (exp);
6737 if (!exp)
6738 return 1;
6739 if (TREE_CODE (exp) != TREE_LIST)
6740 return safe_from_p (x, exp, 0);
6743 else if (TREE_CODE (exp) == CONSTRUCTOR)
6745 constructor_elt *ce;
6746 unsigned HOST_WIDE_INT idx;
6748 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce)
6749 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
6750 || !safe_from_p (x, ce->value, 0))
6751 return 0;
6752 return 1;
6754 else if (TREE_CODE (exp) == ERROR_MARK)
6755 return 1; /* An already-visited SAVE_EXPR? */
6756 else
6757 return 0;
6759 case tcc_statement:
6760 /* The only case we look at here is the DECL_INITIAL inside a
6761 DECL_EXPR. */
6762 return (TREE_CODE (exp) != DECL_EXPR
6763 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
6764 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
6765 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
6767 case tcc_binary:
6768 case tcc_comparison:
6769 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
6770 return 0;
6771 /* Fall through. */
6773 case tcc_unary:
6774 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
6776 case tcc_expression:
6777 case tcc_reference:
6778 case tcc_vl_exp:
6779 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
6780 the expression. If it is set, we conflict iff we are that rtx or
6781 both are in memory. Otherwise, we check all operands of the
6782 expression recursively. */
6784 switch (TREE_CODE (exp))
6786 case ADDR_EXPR:
6787 /* If the operand is static or we are static, we can't conflict.
6788 Likewise if we don't conflict with the operand at all. */
6789 if (staticp (TREE_OPERAND (exp, 0))
6790 || TREE_STATIC (exp)
6791 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
6792 return 1;
6794 /* Otherwise, the only way this can conflict is if we are taking
6795 the address of a DECL a that address if part of X, which is
6796 very rare. */
6797 exp = TREE_OPERAND (exp, 0);
6798 if (DECL_P (exp))
6800 if (!DECL_RTL_SET_P (exp)
6801 || !MEM_P (DECL_RTL (exp)))
6802 return 0;
6803 else
6804 exp_rtl = XEXP (DECL_RTL (exp), 0);
6806 break;
6808 case MEM_REF:
6809 if (MEM_P (x)
6810 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
6811 get_alias_set (exp)))
6812 return 0;
6813 break;
6815 case CALL_EXPR:
6816 /* Assume that the call will clobber all hard registers and
6817 all of memory. */
6818 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
6819 || MEM_P (x))
6820 return 0;
6821 break;
6823 case WITH_CLEANUP_EXPR:
6824 case CLEANUP_POINT_EXPR:
6825 /* Lowered by gimplify.c. */
6826 gcc_unreachable ();
6828 case SAVE_EXPR:
6829 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
6831 default:
6832 break;
6835 /* If we have an rtx, we do not need to scan our operands. */
6836 if (exp_rtl)
6837 break;
6839 nops = TREE_OPERAND_LENGTH (exp);
6840 for (i = 0; i < nops; i++)
6841 if (TREE_OPERAND (exp, i) != 0
6842 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
6843 return 0;
6845 break;
6847 case tcc_type:
6848 /* Should never get a type here. */
6849 gcc_unreachable ();
6852 /* If we have an rtl, find any enclosed object. Then see if we conflict
6853 with it. */
6854 if (exp_rtl)
6856 if (GET_CODE (exp_rtl) == SUBREG)
6858 exp_rtl = SUBREG_REG (exp_rtl);
6859 if (REG_P (exp_rtl)
6860 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
6861 return 0;
6864 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
6865 are memory and they conflict. */
6866 return ! (rtx_equal_p (x, exp_rtl)
6867 || (MEM_P (x) && MEM_P (exp_rtl)
6868 && true_dependence (exp_rtl, VOIDmode, x,
6869 rtx_addr_varies_p)));
6872 /* If we reach here, it is safe. */
6873 return 1;
6877 /* Return the highest power of two that EXP is known to be a multiple of.
6878 This is used in updating alignment of MEMs in array references. */
6880 unsigned HOST_WIDE_INT
6881 highest_pow2_factor (const_tree exp)
6883 unsigned HOST_WIDE_INT c0, c1;
6885 switch (TREE_CODE (exp))
6887 case INTEGER_CST:
6888 /* We can find the lowest bit that's a one. If the low
6889 HOST_BITS_PER_WIDE_INT bits are zero, return BIGGEST_ALIGNMENT.
6890 We need to handle this case since we can find it in a COND_EXPR,
6891 a MIN_EXPR, or a MAX_EXPR. If the constant overflows, we have an
6892 erroneous program, so return BIGGEST_ALIGNMENT to avoid any
6893 later ICE. */
6894 if (TREE_OVERFLOW (exp))
6895 return BIGGEST_ALIGNMENT;
6896 else
6898 /* Note: tree_low_cst is intentionally not used here,
6899 we don't care about the upper bits. */
6900 c0 = TREE_INT_CST_LOW (exp);
6901 c0 &= -c0;
6902 return c0 ? c0 : BIGGEST_ALIGNMENT;
6904 break;
6906 case PLUS_EXPR: case MINUS_EXPR: case MIN_EXPR: case MAX_EXPR:
6907 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
6908 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
6909 return MIN (c0, c1);
6911 case MULT_EXPR:
6912 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
6913 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
6914 return c0 * c1;
6916 case ROUND_DIV_EXPR: case TRUNC_DIV_EXPR: case FLOOR_DIV_EXPR:
6917 case CEIL_DIV_EXPR:
6918 if (integer_pow2p (TREE_OPERAND (exp, 1))
6919 && host_integerp (TREE_OPERAND (exp, 1), 1))
6921 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
6922 c1 = tree_low_cst (TREE_OPERAND (exp, 1), 1);
6923 return MAX (1, c0 / c1);
6925 break;
6927 case BIT_AND_EXPR:
6928 /* The highest power of two of a bit-and expression is the maximum of
6929 that of its operands. We typically get here for a complex LHS and
6930 a constant negative power of two on the RHS to force an explicit
6931 alignment, so don't bother looking at the LHS. */
6932 return highest_pow2_factor (TREE_OPERAND (exp, 1));
6934 CASE_CONVERT:
6935 case SAVE_EXPR:
6936 return highest_pow2_factor (TREE_OPERAND (exp, 0));
6938 case COMPOUND_EXPR:
6939 return highest_pow2_factor (TREE_OPERAND (exp, 1));
6941 case COND_EXPR:
6942 c0 = highest_pow2_factor (TREE_OPERAND (exp, 1));
6943 c1 = highest_pow2_factor (TREE_OPERAND (exp, 2));
6944 return MIN (c0, c1);
6946 default:
6947 break;
6950 return 1;
6953 /* Similar, except that the alignment requirements of TARGET are
6954 taken into account. Assume it is at least as aligned as its
6955 type, unless it is a COMPONENT_REF in which case the layout of
6956 the structure gives the alignment. */
6958 static unsigned HOST_WIDE_INT
6959 highest_pow2_factor_for_target (const_tree target, const_tree exp)
6961 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
6962 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
6964 return MAX (factor, talign);
6967 /* Subroutine of expand_expr. Expand the two operands of a binary
6968 expression EXP0 and EXP1 placing the results in OP0 and OP1.
6969 The value may be stored in TARGET if TARGET is nonzero. The
6970 MODIFIER argument is as documented by expand_expr. */
6972 static void
6973 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
6974 enum expand_modifier modifier)
6976 if (! safe_from_p (target, exp1, 1))
6977 target = 0;
6978 if (operand_equal_p (exp0, exp1, 0))
6980 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
6981 *op1 = copy_rtx (*op0);
6983 else
6985 /* If we need to preserve evaluation order, copy exp0 into its own
6986 temporary variable so that it can't be clobbered by exp1. */
6987 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
6988 exp0 = save_expr (exp0);
6989 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
6990 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
6995 /* Return a MEM that contains constant EXP. DEFER is as for
6996 output_constant_def and MODIFIER is as for expand_expr. */
6998 static rtx
6999 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7001 rtx mem;
7003 mem = output_constant_def (exp, defer);
7004 if (modifier != EXPAND_INITIALIZER)
7005 mem = use_anchored_address (mem);
7006 return mem;
7009 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7010 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7012 static rtx
7013 expand_expr_addr_expr_1 (tree exp, rtx target, enum machine_mode tmode,
7014 enum expand_modifier modifier, addr_space_t as)
7016 rtx result, subtarget;
7017 tree inner, offset;
7018 HOST_WIDE_INT bitsize, bitpos;
7019 int volatilep, unsignedp;
7020 enum machine_mode mode1;
7022 /* If we are taking the address of a constant and are at the top level,
7023 we have to use output_constant_def since we can't call force_const_mem
7024 at top level. */
7025 /* ??? This should be considered a front-end bug. We should not be
7026 generating ADDR_EXPR of something that isn't an LVALUE. The only
7027 exception here is STRING_CST. */
7028 if (CONSTANT_CLASS_P (exp))
7030 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7031 if (modifier < EXPAND_SUM)
7032 result = force_operand (result, target);
7033 return result;
7036 /* Everything must be something allowed by is_gimple_addressable. */
7037 switch (TREE_CODE (exp))
7039 case INDIRECT_REF:
7040 /* This case will happen via recursion for &a->b. */
7041 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7043 case MEM_REF:
7045 tree tem = TREE_OPERAND (exp, 0);
7046 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7047 tem = build2 (POINTER_PLUS_EXPR, TREE_TYPE (TREE_OPERAND (exp, 1)),
7048 tem,
7049 double_int_to_tree (sizetype, mem_ref_offset (exp)));
7050 return expand_expr (tem, target, tmode, modifier);
7053 case CONST_DECL:
7054 /* Expand the initializer like constants above. */
7055 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7056 0, modifier), 0);
7057 if (modifier < EXPAND_SUM)
7058 result = force_operand (result, target);
7059 return result;
7061 case REALPART_EXPR:
7062 /* The real part of the complex number is always first, therefore
7063 the address is the same as the address of the parent object. */
7064 offset = 0;
7065 bitpos = 0;
7066 inner = TREE_OPERAND (exp, 0);
7067 break;
7069 case IMAGPART_EXPR:
7070 /* The imaginary part of the complex number is always second.
7071 The expression is therefore always offset by the size of the
7072 scalar type. */
7073 offset = 0;
7074 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7075 inner = TREE_OPERAND (exp, 0);
7076 break;
7078 default:
7079 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7080 expand_expr, as that can have various side effects; LABEL_DECLs for
7081 example, may not have their DECL_RTL set yet. Expand the rtl of
7082 CONSTRUCTORs too, which should yield a memory reference for the
7083 constructor's contents. Assume language specific tree nodes can
7084 be expanded in some interesting way. */
7085 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7086 if (DECL_P (exp)
7087 || TREE_CODE (exp) == CONSTRUCTOR
7088 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7090 result = expand_expr (exp, target, tmode,
7091 modifier == EXPAND_INITIALIZER
7092 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7094 if (TREE_ADDRESSABLE (exp)
7095 && ! MEM_P (result)
7096 && ! targetm.calls.allocate_stack_slots_for_args ())
7098 error ("local frame unavailable (naked function?)");
7099 return result;
7102 /* If the DECL isn't in memory, then the DECL wasn't properly
7103 marked TREE_ADDRESSABLE, which will be either a front-end
7104 or a tree optimizer bug. */
7105 gcc_assert (MEM_P (result));
7106 result = XEXP (result, 0);
7108 /* ??? Is this needed anymore? */
7109 if (DECL_P (exp) && !TREE_USED (exp) == 0)
7111 assemble_external (exp);
7112 TREE_USED (exp) = 1;
7115 if (modifier != EXPAND_INITIALIZER
7116 && modifier != EXPAND_CONST_ADDRESS)
7117 result = force_operand (result, target);
7118 return result;
7121 /* Pass FALSE as the last argument to get_inner_reference although
7122 we are expanding to RTL. The rationale is that we know how to
7123 handle "aligning nodes" here: we can just bypass them because
7124 they won't change the final object whose address will be returned
7125 (they actually exist only for that purpose). */
7126 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7127 &mode1, &unsignedp, &volatilep, false);
7128 break;
7131 /* We must have made progress. */
7132 gcc_assert (inner != exp);
7134 subtarget = offset || bitpos ? NULL_RTX : target;
7135 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7136 inner alignment, force the inner to be sufficiently aligned. */
7137 if (CONSTANT_CLASS_P (inner)
7138 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7140 inner = copy_node (inner);
7141 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7142 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7143 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7145 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7147 if (offset)
7149 rtx tmp;
7151 if (modifier != EXPAND_NORMAL)
7152 result = force_operand (result, NULL);
7153 tmp = expand_expr (offset, NULL_RTX, tmode,
7154 modifier == EXPAND_INITIALIZER
7155 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7157 result = convert_memory_address_addr_space (tmode, result, as);
7158 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7160 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7161 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7162 else
7164 subtarget = bitpos ? NULL_RTX : target;
7165 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7166 1, OPTAB_LIB_WIDEN);
7170 if (bitpos)
7172 /* Someone beforehand should have rejected taking the address
7173 of such an object. */
7174 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7176 result = plus_constant (result, bitpos / BITS_PER_UNIT);
7177 if (modifier < EXPAND_SUM)
7178 result = force_operand (result, target);
7181 return result;
7184 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7185 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7187 static rtx
7188 expand_expr_addr_expr (tree exp, rtx target, enum machine_mode tmode,
7189 enum expand_modifier modifier)
7191 addr_space_t as = ADDR_SPACE_GENERIC;
7192 enum machine_mode address_mode = Pmode;
7193 enum machine_mode pointer_mode = ptr_mode;
7194 enum machine_mode rmode;
7195 rtx result;
7197 /* Target mode of VOIDmode says "whatever's natural". */
7198 if (tmode == VOIDmode)
7199 tmode = TYPE_MODE (TREE_TYPE (exp));
7201 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7203 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7204 address_mode = targetm.addr_space.address_mode (as);
7205 pointer_mode = targetm.addr_space.pointer_mode (as);
7208 /* We can get called with some Weird Things if the user does silliness
7209 like "(short) &a". In that case, convert_memory_address won't do
7210 the right thing, so ignore the given target mode. */
7211 if (tmode != address_mode && tmode != pointer_mode)
7212 tmode = address_mode;
7214 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7215 tmode, modifier, as);
7217 /* Despite expand_expr claims concerning ignoring TMODE when not
7218 strictly convenient, stuff breaks if we don't honor it. Note
7219 that combined with the above, we only do this for pointer modes. */
7220 rmode = GET_MODE (result);
7221 if (rmode == VOIDmode)
7222 rmode = tmode;
7223 if (rmode != tmode)
7224 result = convert_memory_address_addr_space (tmode, result, as);
7226 return result;
7229 /* Generate code for computing CONSTRUCTOR EXP.
7230 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7231 is TRUE, instead of creating a temporary variable in memory
7232 NULL is returned and the caller needs to handle it differently. */
7234 static rtx
7235 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7236 bool avoid_temp_mem)
7238 tree type = TREE_TYPE (exp);
7239 enum machine_mode mode = TYPE_MODE (type);
7241 /* Try to avoid creating a temporary at all. This is possible
7242 if all of the initializer is zero.
7243 FIXME: try to handle all [0..255] initializers we can handle
7244 with memset. */
7245 if (TREE_STATIC (exp)
7246 && !TREE_ADDRESSABLE (exp)
7247 && target != 0 && mode == BLKmode
7248 && all_zeros_p (exp))
7250 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7251 return target;
7254 /* All elts simple constants => refer to a constant in memory. But
7255 if this is a non-BLKmode mode, let it store a field at a time
7256 since that should make a CONST_INT or CONST_DOUBLE when we
7257 fold. Likewise, if we have a target we can use, it is best to
7258 store directly into the target unless the type is large enough
7259 that memcpy will be used. If we are making an initializer and
7260 all operands are constant, put it in memory as well.
7262 FIXME: Avoid trying to fill vector constructors piece-meal.
7263 Output them with output_constant_def below unless we're sure
7264 they're zeros. This should go away when vector initializers
7265 are treated like VECTOR_CST instead of arrays. */
7266 if ((TREE_STATIC (exp)
7267 && ((mode == BLKmode
7268 && ! (target != 0 && safe_from_p (target, exp, 1)))
7269 || TREE_ADDRESSABLE (exp)
7270 || (host_integerp (TYPE_SIZE_UNIT (type), 1)
7271 && (! MOVE_BY_PIECES_P
7272 (tree_low_cst (TYPE_SIZE_UNIT (type), 1),
7273 TYPE_ALIGN (type)))
7274 && ! mostly_zeros_p (exp))))
7275 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7276 && TREE_CONSTANT (exp)))
7278 rtx constructor;
7280 if (avoid_temp_mem)
7281 return NULL_RTX;
7283 constructor = expand_expr_constant (exp, 1, modifier);
7285 if (modifier != EXPAND_CONST_ADDRESS
7286 && modifier != EXPAND_INITIALIZER
7287 && modifier != EXPAND_SUM)
7288 constructor = validize_mem (constructor);
7290 return constructor;
7293 /* Handle calls that pass values in multiple non-contiguous
7294 locations. The Irix 6 ABI has examples of this. */
7295 if (target == 0 || ! safe_from_p (target, exp, 1)
7296 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7298 if (avoid_temp_mem)
7299 return NULL_RTX;
7301 target
7302 = assign_temp (build_qualified_type (type, (TYPE_QUALS (type)
7303 | (TREE_READONLY (exp)
7304 * TYPE_QUAL_CONST))),
7305 0, TREE_ADDRESSABLE (exp), 1);
7308 store_constructor (exp, target, 0, int_expr_size (exp));
7309 return target;
7313 /* expand_expr: generate code for computing expression EXP.
7314 An rtx for the computed value is returned. The value is never null.
7315 In the case of a void EXP, const0_rtx is returned.
7317 The value may be stored in TARGET if TARGET is nonzero.
7318 TARGET is just a suggestion; callers must assume that
7319 the rtx returned may not be the same as TARGET.
7321 If TARGET is CONST0_RTX, it means that the value will be ignored.
7323 If TMODE is not VOIDmode, it suggests generating the
7324 result in mode TMODE. But this is done only when convenient.
7325 Otherwise, TMODE is ignored and the value generated in its natural mode.
7326 TMODE is just a suggestion; callers must assume that
7327 the rtx returned may not have mode TMODE.
7329 Note that TARGET may have neither TMODE nor MODE. In that case, it
7330 probably will not be used.
7332 If MODIFIER is EXPAND_SUM then when EXP is an addition
7333 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7334 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7335 products as above, or REG or MEM, or constant.
7336 Ordinarily in such cases we would output mul or add instructions
7337 and then return a pseudo reg containing the sum.
7339 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7340 it also marks a label as absolutely required (it can't be dead).
7341 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7342 This is used for outputting expressions used in initializers.
7344 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7345 with a constant address even if that address is not normally legitimate.
7346 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7348 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7349 a call parameter. Such targets require special care as we haven't yet
7350 marked TARGET so that it's safe from being trashed by libcalls. We
7351 don't want to use TARGET for anything but the final result;
7352 Intermediate values must go elsewhere. Additionally, calls to
7353 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7355 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7356 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7357 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7358 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7359 recursively. */
7362 expand_expr_real (tree exp, rtx target, enum machine_mode tmode,
7363 enum expand_modifier modifier, rtx *alt_rtl)
7365 rtx ret;
7367 /* Handle ERROR_MARK before anybody tries to access its type. */
7368 if (TREE_CODE (exp) == ERROR_MARK
7369 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7371 ret = CONST0_RTX (tmode);
7372 return ret ? ret : const0_rtx;
7375 /* If this is an expression of some kind and it has an associated line
7376 number, then emit the line number before expanding the expression.
7378 We need to save and restore the file and line information so that
7379 errors discovered during expansion are emitted with the right
7380 information. It would be better of the diagnostic routines
7381 used the file/line information embedded in the tree nodes rather
7382 than globals. */
7383 if (cfun && EXPR_HAS_LOCATION (exp))
7385 location_t saved_location = input_location;
7386 location_t saved_curr_loc = get_curr_insn_source_location ();
7387 tree saved_block = get_curr_insn_block ();
7388 input_location = EXPR_LOCATION (exp);
7389 set_curr_insn_source_location (input_location);
7391 /* Record where the insns produced belong. */
7392 set_curr_insn_block (TREE_BLOCK (exp));
7394 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7396 input_location = saved_location;
7397 set_curr_insn_block (saved_block);
7398 set_curr_insn_source_location (saved_curr_loc);
7400 else
7402 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7405 return ret;
7409 expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode,
7410 enum expand_modifier modifier)
7412 rtx op0, op1, op2, temp;
7413 tree type;
7414 int unsignedp;
7415 enum machine_mode mode;
7416 enum tree_code code = ops->code;
7417 optab this_optab;
7418 rtx subtarget, original_target;
7419 int ignore;
7420 bool reduce_bit_field;
7421 location_t loc = ops->location;
7422 tree treeop0, treeop1, treeop2;
7423 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
7424 ? reduce_to_bit_field_precision ((expr), \
7425 target, \
7426 type) \
7427 : (expr))
7429 type = ops->type;
7430 mode = TYPE_MODE (type);
7431 unsignedp = TYPE_UNSIGNED (type);
7433 treeop0 = ops->op0;
7434 treeop1 = ops->op1;
7435 treeop2 = ops->op2;
7437 /* We should be called only on simple (binary or unary) expressions,
7438 exactly those that are valid in gimple expressions that aren't
7439 GIMPLE_SINGLE_RHS (or invalid). */
7440 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
7441 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
7442 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
7444 ignore = (target == const0_rtx
7445 || ((CONVERT_EXPR_CODE_P (code)
7446 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
7447 && TREE_CODE (type) == VOID_TYPE));
7449 /* We should be called only if we need the result. */
7450 gcc_assert (!ignore);
7452 /* An operation in what may be a bit-field type needs the
7453 result to be reduced to the precision of the bit-field type,
7454 which is narrower than that of the type's mode. */
7455 reduce_bit_field = (TREE_CODE (type) == INTEGER_TYPE
7456 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
7458 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
7459 target = 0;
7461 /* Use subtarget as the target for operand 0 of a binary operation. */
7462 subtarget = get_subtarget (target);
7463 original_target = target;
7465 switch (code)
7467 case NON_LVALUE_EXPR:
7468 case PAREN_EXPR:
7469 CASE_CONVERT:
7470 if (treeop0 == error_mark_node)
7471 return const0_rtx;
7473 if (TREE_CODE (type) == UNION_TYPE)
7475 tree valtype = TREE_TYPE (treeop0);
7477 /* If both input and output are BLKmode, this conversion isn't doing
7478 anything except possibly changing memory attribute. */
7479 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
7481 rtx result = expand_expr (treeop0, target, tmode,
7482 modifier);
7484 result = copy_rtx (result);
7485 set_mem_attributes (result, type, 0);
7486 return result;
7489 if (target == 0)
7491 if (TYPE_MODE (type) != BLKmode)
7492 target = gen_reg_rtx (TYPE_MODE (type));
7493 else
7494 target = assign_temp (type, 0, 1, 1);
7497 if (MEM_P (target))
7498 /* Store data into beginning of memory target. */
7499 store_expr (treeop0,
7500 adjust_address (target, TYPE_MODE (valtype), 0),
7501 modifier == EXPAND_STACK_PARM,
7502 false);
7504 else
7506 gcc_assert (REG_P (target));
7508 /* Store this field into a union of the proper type. */
7509 store_field (target,
7510 MIN ((int_size_in_bytes (TREE_TYPE
7511 (treeop0))
7512 * BITS_PER_UNIT),
7513 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
7514 0, TYPE_MODE (valtype), treeop0,
7515 type, 0, false);
7518 /* Return the entire union. */
7519 return target;
7522 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
7524 op0 = expand_expr (treeop0, target, VOIDmode,
7525 modifier);
7527 /* If the signedness of the conversion differs and OP0 is
7528 a promoted SUBREG, clear that indication since we now
7529 have to do the proper extension. */
7530 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
7531 && GET_CODE (op0) == SUBREG)
7532 SUBREG_PROMOTED_VAR_P (op0) = 0;
7534 return REDUCE_BIT_FIELD (op0);
7537 op0 = expand_expr (treeop0, NULL_RTX, mode,
7538 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
7539 if (GET_MODE (op0) == mode)
7542 /* If OP0 is a constant, just convert it into the proper mode. */
7543 else if (CONSTANT_P (op0))
7545 tree inner_type = TREE_TYPE (treeop0);
7546 enum machine_mode inner_mode = TYPE_MODE (inner_type);
7548 if (modifier == EXPAND_INITIALIZER)
7549 op0 = simplify_gen_subreg (mode, op0, inner_mode,
7550 subreg_lowpart_offset (mode,
7551 inner_mode));
7552 else
7553 op0= convert_modes (mode, inner_mode, op0,
7554 TYPE_UNSIGNED (inner_type));
7557 else if (modifier == EXPAND_INITIALIZER)
7558 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
7560 else if (target == 0)
7561 op0 = convert_to_mode (mode, op0,
7562 TYPE_UNSIGNED (TREE_TYPE
7563 (treeop0)));
7564 else
7566 convert_move (target, op0,
7567 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
7568 op0 = target;
7571 return REDUCE_BIT_FIELD (op0);
7573 case ADDR_SPACE_CONVERT_EXPR:
7575 tree treeop0_type = TREE_TYPE (treeop0);
7576 addr_space_t as_to;
7577 addr_space_t as_from;
7579 gcc_assert (POINTER_TYPE_P (type));
7580 gcc_assert (POINTER_TYPE_P (treeop0_type));
7582 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
7583 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
7585 /* Conversions between pointers to the same address space should
7586 have been implemented via CONVERT_EXPR / NOP_EXPR. */
7587 gcc_assert (as_to != as_from);
7589 /* Ask target code to handle conversion between pointers
7590 to overlapping address spaces. */
7591 if (targetm.addr_space.subset_p (as_to, as_from)
7592 || targetm.addr_space.subset_p (as_from, as_to))
7594 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
7595 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
7596 gcc_assert (op0);
7597 return op0;
7600 /* For disjoint address spaces, converting anything but
7601 a null pointer invokes undefined behaviour. We simply
7602 always return a null pointer here. */
7603 return CONST0_RTX (mode);
7606 case POINTER_PLUS_EXPR:
7607 /* Even though the sizetype mode and the pointer's mode can be different
7608 expand is able to handle this correctly and get the correct result out
7609 of the PLUS_EXPR code. */
7610 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
7611 if sizetype precision is smaller than pointer precision. */
7612 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
7613 treeop1 = fold_convert_loc (loc, type,
7614 fold_convert_loc (loc, ssizetype,
7615 treeop1));
7616 case PLUS_EXPR:
7617 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
7618 something else, make sure we add the register to the constant and
7619 then to the other thing. This case can occur during strength
7620 reduction and doing it this way will produce better code if the
7621 frame pointer or argument pointer is eliminated.
7623 fold-const.c will ensure that the constant is always in the inner
7624 PLUS_EXPR, so the only case we need to do anything about is if
7625 sp, ap, or fp is our second argument, in which case we must swap
7626 the innermost first argument and our second argument. */
7628 if (TREE_CODE (treeop0) == PLUS_EXPR
7629 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
7630 && TREE_CODE (treeop1) == VAR_DECL
7631 && (DECL_RTL (treeop1) == frame_pointer_rtx
7632 || DECL_RTL (treeop1) == stack_pointer_rtx
7633 || DECL_RTL (treeop1) == arg_pointer_rtx))
7635 tree t = treeop1;
7637 treeop1 = TREE_OPERAND (treeop0, 0);
7638 TREE_OPERAND (treeop0, 0) = t;
7641 /* If the result is to be ptr_mode and we are adding an integer to
7642 something, we might be forming a constant. So try to use
7643 plus_constant. If it produces a sum and we can't accept it,
7644 use force_operand. This allows P = &ARR[const] to generate
7645 efficient code on machines where a SYMBOL_REF is not a valid
7646 address.
7648 If this is an EXPAND_SUM call, always return the sum. */
7649 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
7650 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
7652 if (modifier == EXPAND_STACK_PARM)
7653 target = 0;
7654 if (TREE_CODE (treeop0) == INTEGER_CST
7655 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7656 && TREE_CONSTANT (treeop1))
7658 rtx constant_part;
7660 op1 = expand_expr (treeop1, subtarget, VOIDmode,
7661 EXPAND_SUM);
7662 /* Use immed_double_const to ensure that the constant is
7663 truncated according to the mode of OP1, then sign extended
7664 to a HOST_WIDE_INT. Using the constant directly can result
7665 in non-canonical RTL in a 64x32 cross compile. */
7666 constant_part
7667 = immed_double_const (TREE_INT_CST_LOW (treeop0),
7668 (HOST_WIDE_INT) 0,
7669 TYPE_MODE (TREE_TYPE (treeop1)));
7670 op1 = plus_constant (op1, INTVAL (constant_part));
7671 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
7672 op1 = force_operand (op1, target);
7673 return REDUCE_BIT_FIELD (op1);
7676 else if (TREE_CODE (treeop1) == INTEGER_CST
7677 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7678 && TREE_CONSTANT (treeop0))
7680 rtx constant_part;
7682 op0 = expand_expr (treeop0, subtarget, VOIDmode,
7683 (modifier == EXPAND_INITIALIZER
7684 ? EXPAND_INITIALIZER : EXPAND_SUM));
7685 if (! CONSTANT_P (op0))
7687 op1 = expand_expr (treeop1, NULL_RTX,
7688 VOIDmode, modifier);
7689 /* Return a PLUS if modifier says it's OK. */
7690 if (modifier == EXPAND_SUM
7691 || modifier == EXPAND_INITIALIZER)
7692 return simplify_gen_binary (PLUS, mode, op0, op1);
7693 goto binop2;
7695 /* Use immed_double_const to ensure that the constant is
7696 truncated according to the mode of OP1, then sign extended
7697 to a HOST_WIDE_INT. Using the constant directly can result
7698 in non-canonical RTL in a 64x32 cross compile. */
7699 constant_part
7700 = immed_double_const (TREE_INT_CST_LOW (treeop1),
7701 (HOST_WIDE_INT) 0,
7702 TYPE_MODE (TREE_TYPE (treeop0)));
7703 op0 = plus_constant (op0, INTVAL (constant_part));
7704 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
7705 op0 = force_operand (op0, target);
7706 return REDUCE_BIT_FIELD (op0);
7710 /* Use TER to expand pointer addition of a negated value
7711 as pointer subtraction. */
7712 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
7713 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
7714 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
7715 && TREE_CODE (treeop1) == SSA_NAME
7716 && TYPE_MODE (TREE_TYPE (treeop0))
7717 == TYPE_MODE (TREE_TYPE (treeop1)))
7719 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
7720 if (def)
7722 treeop1 = gimple_assign_rhs1 (def);
7723 code = MINUS_EXPR;
7724 goto do_minus;
7728 /* No sense saving up arithmetic to be done
7729 if it's all in the wrong mode to form part of an address.
7730 And force_operand won't know whether to sign-extend or
7731 zero-extend. */
7732 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
7733 || mode != ptr_mode)
7735 expand_operands (treeop0, treeop1,
7736 subtarget, &op0, &op1, EXPAND_NORMAL);
7737 if (op0 == const0_rtx)
7738 return op1;
7739 if (op1 == const0_rtx)
7740 return op0;
7741 goto binop2;
7744 expand_operands (treeop0, treeop1,
7745 subtarget, &op0, &op1, modifier);
7746 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
7748 case MINUS_EXPR:
7749 do_minus:
7750 /* For initializers, we are allowed to return a MINUS of two
7751 symbolic constants. Here we handle all cases when both operands
7752 are constant. */
7753 /* Handle difference of two symbolic constants,
7754 for the sake of an initializer. */
7755 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7756 && really_constant_p (treeop0)
7757 && really_constant_p (treeop1))
7759 expand_operands (treeop0, treeop1,
7760 NULL_RTX, &op0, &op1, modifier);
7762 /* If the last operand is a CONST_INT, use plus_constant of
7763 the negated constant. Else make the MINUS. */
7764 if (CONST_INT_P (op1))
7765 return REDUCE_BIT_FIELD (plus_constant (op0, - INTVAL (op1)));
7766 else
7767 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
7770 /* No sense saving up arithmetic to be done
7771 if it's all in the wrong mode to form part of an address.
7772 And force_operand won't know whether to sign-extend or
7773 zero-extend. */
7774 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
7775 || mode != ptr_mode)
7776 goto binop;
7778 expand_operands (treeop0, treeop1,
7779 subtarget, &op0, &op1, modifier);
7781 /* Convert A - const to A + (-const). */
7782 if (CONST_INT_P (op1))
7784 op1 = negate_rtx (mode, op1);
7785 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
7788 goto binop2;
7790 case WIDEN_MULT_PLUS_EXPR:
7791 case WIDEN_MULT_MINUS_EXPR:
7792 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
7793 op2 = expand_normal (treeop2);
7794 target = expand_widen_pattern_expr (ops, op0, op1, op2,
7795 target, unsignedp);
7796 return target;
7798 case WIDEN_MULT_EXPR:
7799 /* If first operand is constant, swap them.
7800 Thus the following special case checks need only
7801 check the second operand. */
7802 if (TREE_CODE (treeop0) == INTEGER_CST)
7804 tree t1 = treeop0;
7805 treeop0 = treeop1;
7806 treeop1 = t1;
7809 /* First, check if we have a multiplication of one signed and one
7810 unsigned operand. */
7811 if (TREE_CODE (treeop1) != INTEGER_CST
7812 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
7813 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
7815 enum machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
7816 this_optab = usmul_widen_optab;
7817 if (mode == GET_MODE_2XWIDER_MODE (innermode))
7819 if (optab_handler (this_optab, mode) != CODE_FOR_nothing)
7821 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
7822 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
7823 EXPAND_NORMAL);
7824 else
7825 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
7826 EXPAND_NORMAL);
7827 goto binop3;
7831 /* Check for a multiplication with matching signedness. */
7832 else if ((TREE_CODE (treeop1) == INTEGER_CST
7833 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
7834 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
7835 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
7837 tree op0type = TREE_TYPE (treeop0);
7838 enum machine_mode innermode = TYPE_MODE (op0type);
7839 bool zextend_p = TYPE_UNSIGNED (op0type);
7840 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
7841 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
7843 if (mode == GET_MODE_2XWIDER_MODE (innermode)
7844 && TREE_CODE (treeop0) != INTEGER_CST)
7846 if (optab_handler (this_optab, mode) != CODE_FOR_nothing)
7848 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
7849 EXPAND_NORMAL);
7850 temp = expand_widening_mult (mode, op0, op1, target,
7851 unsignedp, this_optab);
7852 return REDUCE_BIT_FIELD (temp);
7854 if (optab_handler (other_optab, mode) != CODE_FOR_nothing
7855 && innermode == word_mode)
7857 rtx htem, hipart;
7858 op0 = expand_normal (treeop0);
7859 if (TREE_CODE (treeop1) == INTEGER_CST)
7860 op1 = convert_modes (innermode, mode,
7861 expand_normal (treeop1), unsignedp);
7862 else
7863 op1 = expand_normal (treeop1);
7864 temp = expand_binop (mode, other_optab, op0, op1, target,
7865 unsignedp, OPTAB_LIB_WIDEN);
7866 hipart = gen_highpart (innermode, temp);
7867 htem = expand_mult_highpart_adjust (innermode, hipart,
7868 op0, op1, hipart,
7869 zextend_p);
7870 if (htem != hipart)
7871 emit_move_insn (hipart, htem);
7872 return REDUCE_BIT_FIELD (temp);
7876 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
7877 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
7878 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
7879 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
7881 case FMA_EXPR:
7883 optab opt = fma_optab;
7884 gimple def0, def2;
7886 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
7887 call. */
7888 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
7890 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
7891 tree call_expr;
7893 gcc_assert (fn != NULL_TREE);
7894 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
7895 return expand_builtin (call_expr, target, subtarget, mode, false);
7898 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
7899 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
7901 op0 = op2 = NULL;
7903 if (def0 && def2
7904 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
7906 opt = fnms_optab;
7907 op0 = expand_normal (gimple_assign_rhs1 (def0));
7908 op2 = expand_normal (gimple_assign_rhs1 (def2));
7910 else if (def0
7911 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
7913 opt = fnma_optab;
7914 op0 = expand_normal (gimple_assign_rhs1 (def0));
7916 else if (def2
7917 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
7919 opt = fms_optab;
7920 op2 = expand_normal (gimple_assign_rhs1 (def2));
7923 if (op0 == NULL)
7924 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
7925 if (op2 == NULL)
7926 op2 = expand_normal (treeop2);
7927 op1 = expand_normal (treeop1);
7929 return expand_ternary_op (TYPE_MODE (type), opt,
7930 op0, op1, op2, target, 0);
7933 case MULT_EXPR:
7934 /* If this is a fixed-point operation, then we cannot use the code
7935 below because "expand_mult" doesn't support sat/no-sat fixed-point
7936 multiplications. */
7937 if (ALL_FIXED_POINT_MODE_P (mode))
7938 goto binop;
7940 /* If first operand is constant, swap them.
7941 Thus the following special case checks need only
7942 check the second operand. */
7943 if (TREE_CODE (treeop0) == INTEGER_CST)
7945 tree t1 = treeop0;
7946 treeop0 = treeop1;
7947 treeop1 = t1;
7950 /* Attempt to return something suitable for generating an
7951 indexed address, for machines that support that. */
7953 if (modifier == EXPAND_SUM && mode == ptr_mode
7954 && host_integerp (treeop1, 0))
7956 tree exp1 = treeop1;
7958 op0 = expand_expr (treeop0, subtarget, VOIDmode,
7959 EXPAND_SUM);
7961 if (!REG_P (op0))
7962 op0 = force_operand (op0, NULL_RTX);
7963 if (!REG_P (op0))
7964 op0 = copy_to_mode_reg (mode, op0);
7966 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
7967 gen_int_mode (tree_low_cst (exp1, 0),
7968 TYPE_MODE (TREE_TYPE (exp1)))));
7971 if (modifier == EXPAND_STACK_PARM)
7972 target = 0;
7974 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
7975 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
7977 case TRUNC_DIV_EXPR:
7978 case FLOOR_DIV_EXPR:
7979 case CEIL_DIV_EXPR:
7980 case ROUND_DIV_EXPR:
7981 case EXACT_DIV_EXPR:
7982 /* If this is a fixed-point operation, then we cannot use the code
7983 below because "expand_divmod" doesn't support sat/no-sat fixed-point
7984 divisions. */
7985 if (ALL_FIXED_POINT_MODE_P (mode))
7986 goto binop;
7988 if (modifier == EXPAND_STACK_PARM)
7989 target = 0;
7990 /* Possible optimization: compute the dividend with EXPAND_SUM
7991 then if the divisor is constant can optimize the case
7992 where some terms of the dividend have coeffs divisible by it. */
7993 expand_operands (treeop0, treeop1,
7994 subtarget, &op0, &op1, EXPAND_NORMAL);
7995 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
7997 case RDIV_EXPR:
7998 goto binop;
8000 case TRUNC_MOD_EXPR:
8001 case FLOOR_MOD_EXPR:
8002 case CEIL_MOD_EXPR:
8003 case ROUND_MOD_EXPR:
8004 if (modifier == EXPAND_STACK_PARM)
8005 target = 0;
8006 expand_operands (treeop0, treeop1,
8007 subtarget, &op0, &op1, EXPAND_NORMAL);
8008 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8010 case FIXED_CONVERT_EXPR:
8011 op0 = expand_normal (treeop0);
8012 if (target == 0 || modifier == EXPAND_STACK_PARM)
8013 target = gen_reg_rtx (mode);
8015 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8016 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8017 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8018 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8019 else
8020 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8021 return target;
8023 case FIX_TRUNC_EXPR:
8024 op0 = expand_normal (treeop0);
8025 if (target == 0 || modifier == EXPAND_STACK_PARM)
8026 target = gen_reg_rtx (mode);
8027 expand_fix (target, op0, unsignedp);
8028 return target;
8030 case FLOAT_EXPR:
8031 op0 = expand_normal (treeop0);
8032 if (target == 0 || modifier == EXPAND_STACK_PARM)
8033 target = gen_reg_rtx (mode);
8034 /* expand_float can't figure out what to do if FROM has VOIDmode.
8035 So give it the correct mode. With -O, cse will optimize this. */
8036 if (GET_MODE (op0) == VOIDmode)
8037 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8038 op0);
8039 expand_float (target, op0,
8040 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8041 return target;
8043 case NEGATE_EXPR:
8044 op0 = expand_expr (treeop0, subtarget,
8045 VOIDmode, EXPAND_NORMAL);
8046 if (modifier == EXPAND_STACK_PARM)
8047 target = 0;
8048 temp = expand_unop (mode,
8049 optab_for_tree_code (NEGATE_EXPR, type,
8050 optab_default),
8051 op0, target, 0);
8052 gcc_assert (temp);
8053 return REDUCE_BIT_FIELD (temp);
8055 case ABS_EXPR:
8056 op0 = expand_expr (treeop0, subtarget,
8057 VOIDmode, EXPAND_NORMAL);
8058 if (modifier == EXPAND_STACK_PARM)
8059 target = 0;
8061 /* ABS_EXPR is not valid for complex arguments. */
8062 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8063 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8065 /* Unsigned abs is simply the operand. Testing here means we don't
8066 risk generating incorrect code below. */
8067 if (TYPE_UNSIGNED (type))
8068 return op0;
8070 return expand_abs (mode, op0, target, unsignedp,
8071 safe_from_p (target, treeop0, 1));
8073 case MAX_EXPR:
8074 case MIN_EXPR:
8075 target = original_target;
8076 if (target == 0
8077 || modifier == EXPAND_STACK_PARM
8078 || (MEM_P (target) && MEM_VOLATILE_P (target))
8079 || GET_MODE (target) != mode
8080 || (REG_P (target)
8081 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8082 target = gen_reg_rtx (mode);
8083 expand_operands (treeop0, treeop1,
8084 target, &op0, &op1, EXPAND_NORMAL);
8086 /* First try to do it with a special MIN or MAX instruction.
8087 If that does not win, use a conditional jump to select the proper
8088 value. */
8089 this_optab = optab_for_tree_code (code, type, optab_default);
8090 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8091 OPTAB_WIDEN);
8092 if (temp != 0)
8093 return temp;
8095 /* At this point, a MEM target is no longer useful; we will get better
8096 code without it. */
8098 if (! REG_P (target))
8099 target = gen_reg_rtx (mode);
8101 /* If op1 was placed in target, swap op0 and op1. */
8102 if (target != op0 && target == op1)
8104 temp = op0;
8105 op0 = op1;
8106 op1 = temp;
8109 /* We generate better code and avoid problems with op1 mentioning
8110 target by forcing op1 into a pseudo if it isn't a constant. */
8111 if (! CONSTANT_P (op1))
8112 op1 = force_reg (mode, op1);
8115 enum rtx_code comparison_code;
8116 rtx cmpop1 = op1;
8118 if (code == MAX_EXPR)
8119 comparison_code = unsignedp ? GEU : GE;
8120 else
8121 comparison_code = unsignedp ? LEU : LE;
8123 /* Canonicalize to comparisons against 0. */
8124 if (op1 == const1_rtx)
8126 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8127 or (a != 0 ? a : 1) for unsigned.
8128 For MIN we are safe converting (a <= 1 ? a : 1)
8129 into (a <= 0 ? a : 1) */
8130 cmpop1 = const0_rtx;
8131 if (code == MAX_EXPR)
8132 comparison_code = unsignedp ? NE : GT;
8134 if (op1 == constm1_rtx && !unsignedp)
8136 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8137 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8138 cmpop1 = const0_rtx;
8139 if (code == MIN_EXPR)
8140 comparison_code = LT;
8142 #ifdef HAVE_conditional_move
8143 /* Use a conditional move if possible. */
8144 if (can_conditionally_move_p (mode))
8146 rtx insn;
8148 /* ??? Same problem as in expmed.c: emit_conditional_move
8149 forces a stack adjustment via compare_from_rtx, and we
8150 lose the stack adjustment if the sequence we are about
8151 to create is discarded. */
8152 do_pending_stack_adjust ();
8154 start_sequence ();
8156 /* Try to emit the conditional move. */
8157 insn = emit_conditional_move (target, comparison_code,
8158 op0, cmpop1, mode,
8159 op0, op1, mode,
8160 unsignedp);
8162 /* If we could do the conditional move, emit the sequence,
8163 and return. */
8164 if (insn)
8166 rtx seq = get_insns ();
8167 end_sequence ();
8168 emit_insn (seq);
8169 return target;
8172 /* Otherwise discard the sequence and fall back to code with
8173 branches. */
8174 end_sequence ();
8176 #endif
8177 if (target != op0)
8178 emit_move_insn (target, op0);
8180 temp = gen_label_rtx ();
8181 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8182 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8183 -1);
8185 emit_move_insn (target, op1);
8186 emit_label (temp);
8187 return target;
8189 case BIT_NOT_EXPR:
8190 op0 = expand_expr (treeop0, subtarget,
8191 VOIDmode, EXPAND_NORMAL);
8192 if (modifier == EXPAND_STACK_PARM)
8193 target = 0;
8194 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8195 gcc_assert (temp);
8196 return temp;
8198 /* ??? Can optimize bitwise operations with one arg constant.
8199 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8200 and (a bitwise1 b) bitwise2 b (etc)
8201 but that is probably not worth while. */
8203 /* BIT_AND_EXPR is for bitwise anding. TRUTH_AND_EXPR is for anding two
8204 boolean values when we want in all cases to compute both of them. In
8205 general it is fastest to do TRUTH_AND_EXPR by computing both operands
8206 as actual zero-or-1 values and then bitwise anding. In cases where
8207 there cannot be any side effects, better code would be made by
8208 treating TRUTH_AND_EXPR like TRUTH_ANDIF_EXPR; but the question is
8209 how to recognize those cases. */
8211 case TRUTH_AND_EXPR:
8212 code = BIT_AND_EXPR;
8213 case BIT_AND_EXPR:
8214 goto binop;
8216 case TRUTH_OR_EXPR:
8217 code = BIT_IOR_EXPR;
8218 case BIT_IOR_EXPR:
8219 goto binop;
8221 case TRUTH_XOR_EXPR:
8222 code = BIT_XOR_EXPR;
8223 case BIT_XOR_EXPR:
8224 goto binop;
8226 case LROTATE_EXPR:
8227 case RROTATE_EXPR:
8228 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8229 || (GET_MODE_PRECISION (TYPE_MODE (type))
8230 == TYPE_PRECISION (type)));
8231 /* fall through */
8233 case LSHIFT_EXPR:
8234 case RSHIFT_EXPR:
8235 /* If this is a fixed-point operation, then we cannot use the code
8236 below because "expand_shift" doesn't support sat/no-sat fixed-point
8237 shifts. */
8238 if (ALL_FIXED_POINT_MODE_P (mode))
8239 goto binop;
8241 if (! safe_from_p (subtarget, treeop1, 1))
8242 subtarget = 0;
8243 if (modifier == EXPAND_STACK_PARM)
8244 target = 0;
8245 op0 = expand_expr (treeop0, subtarget,
8246 VOIDmode, EXPAND_NORMAL);
8247 temp = expand_shift (code, mode, op0, treeop1, target,
8248 unsignedp);
8249 if (code == LSHIFT_EXPR)
8250 temp = REDUCE_BIT_FIELD (temp);
8251 return temp;
8253 /* Could determine the answer when only additive constants differ. Also,
8254 the addition of one can be handled by changing the condition. */
8255 case LT_EXPR:
8256 case LE_EXPR:
8257 case GT_EXPR:
8258 case GE_EXPR:
8259 case EQ_EXPR:
8260 case NE_EXPR:
8261 case UNORDERED_EXPR:
8262 case ORDERED_EXPR:
8263 case UNLT_EXPR:
8264 case UNLE_EXPR:
8265 case UNGT_EXPR:
8266 case UNGE_EXPR:
8267 case UNEQ_EXPR:
8268 case LTGT_EXPR:
8269 temp = do_store_flag (ops,
8270 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8271 tmode != VOIDmode ? tmode : mode);
8272 if (temp)
8273 return temp;
8275 /* Use a compare and a jump for BLKmode comparisons, or for function
8276 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
8278 if ((target == 0
8279 || modifier == EXPAND_STACK_PARM
8280 || ! safe_from_p (target, treeop0, 1)
8281 || ! safe_from_p (target, treeop1, 1)
8282 /* Make sure we don't have a hard reg (such as function's return
8283 value) live across basic blocks, if not optimizing. */
8284 || (!optimize && REG_P (target)
8285 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8286 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8288 emit_move_insn (target, const0_rtx);
8290 op1 = gen_label_rtx ();
8291 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
8293 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8294 emit_move_insn (target, constm1_rtx);
8295 else
8296 emit_move_insn (target, const1_rtx);
8298 emit_label (op1);
8299 return target;
8301 case TRUTH_NOT_EXPR:
8302 if (modifier == EXPAND_STACK_PARM)
8303 target = 0;
8304 op0 = expand_expr (treeop0, target,
8305 VOIDmode, EXPAND_NORMAL);
8306 /* The parser is careful to generate TRUTH_NOT_EXPR
8307 only with operands that are always zero or one. */
8308 temp = expand_binop (mode, xor_optab, op0, const1_rtx,
8309 target, 1, OPTAB_LIB_WIDEN);
8310 gcc_assert (temp);
8311 return temp;
8313 case COMPLEX_EXPR:
8314 /* Get the rtx code of the operands. */
8315 op0 = expand_normal (treeop0);
8316 op1 = expand_normal (treeop1);
8318 if (!target)
8319 target = gen_reg_rtx (TYPE_MODE (type));
8321 /* Move the real (op0) and imaginary (op1) parts to their location. */
8322 write_complex_part (target, op0, false);
8323 write_complex_part (target, op1, true);
8325 return target;
8327 case WIDEN_SUM_EXPR:
8329 tree oprnd0 = treeop0;
8330 tree oprnd1 = treeop1;
8332 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8333 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
8334 target, unsignedp);
8335 return target;
8338 case REDUC_MAX_EXPR:
8339 case REDUC_MIN_EXPR:
8340 case REDUC_PLUS_EXPR:
8342 op0 = expand_normal (treeop0);
8343 this_optab = optab_for_tree_code (code, type, optab_default);
8344 temp = expand_unop (mode, this_optab, op0, target, unsignedp);
8345 gcc_assert (temp);
8346 return temp;
8349 case VEC_EXTRACT_EVEN_EXPR:
8350 case VEC_EXTRACT_ODD_EXPR:
8352 expand_operands (treeop0, treeop1,
8353 NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8354 this_optab = optab_for_tree_code (code, type, optab_default);
8355 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8356 OPTAB_WIDEN);
8357 gcc_assert (temp);
8358 return temp;
8361 case VEC_INTERLEAVE_HIGH_EXPR:
8362 case VEC_INTERLEAVE_LOW_EXPR:
8364 expand_operands (treeop0, treeop1,
8365 NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8366 this_optab = optab_for_tree_code (code, type, optab_default);
8367 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8368 OPTAB_WIDEN);
8369 gcc_assert (temp);
8370 return temp;
8373 case VEC_LSHIFT_EXPR:
8374 case VEC_RSHIFT_EXPR:
8376 target = expand_vec_shift_expr (ops, target);
8377 return target;
8380 case VEC_UNPACK_HI_EXPR:
8381 case VEC_UNPACK_LO_EXPR:
8383 op0 = expand_normal (treeop0);
8384 this_optab = optab_for_tree_code (code, type, optab_default);
8385 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
8386 target, unsignedp);
8387 gcc_assert (temp);
8388 return temp;
8391 case VEC_UNPACK_FLOAT_HI_EXPR:
8392 case VEC_UNPACK_FLOAT_LO_EXPR:
8394 op0 = expand_normal (treeop0);
8395 /* The signedness is determined from input operand. */
8396 this_optab = optab_for_tree_code (code,
8397 TREE_TYPE (treeop0),
8398 optab_default);
8399 temp = expand_widen_pattern_expr
8400 (ops, op0, NULL_RTX, NULL_RTX,
8401 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8403 gcc_assert (temp);
8404 return temp;
8407 case VEC_WIDEN_MULT_HI_EXPR:
8408 case VEC_WIDEN_MULT_LO_EXPR:
8410 tree oprnd0 = treeop0;
8411 tree oprnd1 = treeop1;
8413 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8414 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
8415 target, unsignedp);
8416 gcc_assert (target);
8417 return target;
8420 case VEC_PACK_TRUNC_EXPR:
8421 case VEC_PACK_SAT_EXPR:
8422 case VEC_PACK_FIX_TRUNC_EXPR:
8423 mode = TYPE_MODE (TREE_TYPE (treeop0));
8424 goto binop;
8426 default:
8427 gcc_unreachable ();
8430 /* Here to do an ordinary binary operator. */
8431 binop:
8432 expand_operands (treeop0, treeop1,
8433 subtarget, &op0, &op1, EXPAND_NORMAL);
8434 binop2:
8435 this_optab = optab_for_tree_code (code, type, optab_default);
8436 binop3:
8437 if (modifier == EXPAND_STACK_PARM)
8438 target = 0;
8439 temp = expand_binop (mode, this_optab, op0, op1, target,
8440 unsignedp, OPTAB_LIB_WIDEN);
8441 gcc_assert (temp);
8442 return REDUCE_BIT_FIELD (temp);
8444 #undef REDUCE_BIT_FIELD
8447 expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode,
8448 enum expand_modifier modifier, rtx *alt_rtl)
8450 rtx op0, op1, temp, decl_rtl;
8451 tree type;
8452 int unsignedp;
8453 enum machine_mode mode;
8454 enum tree_code code = TREE_CODE (exp);
8455 optab this_optab;
8456 rtx subtarget, original_target;
8457 int ignore;
8458 tree context;
8459 bool reduce_bit_field;
8460 location_t loc = EXPR_LOCATION (exp);
8461 struct separate_ops ops;
8462 tree treeop0, treeop1, treeop2;
8463 tree ssa_name = NULL_TREE;
8464 gimple g;
8466 type = TREE_TYPE (exp);
8467 mode = TYPE_MODE (type);
8468 unsignedp = TYPE_UNSIGNED (type);
8470 treeop0 = treeop1 = treeop2 = NULL_TREE;
8471 if (!VL_EXP_CLASS_P (exp))
8472 switch (TREE_CODE_LENGTH (code))
8474 default:
8475 case 3: treeop2 = TREE_OPERAND (exp, 2);
8476 case 2: treeop1 = TREE_OPERAND (exp, 1);
8477 case 1: treeop0 = TREE_OPERAND (exp, 0);
8478 case 0: break;
8480 ops.code = code;
8481 ops.type = type;
8482 ops.op0 = treeop0;
8483 ops.op1 = treeop1;
8484 ops.op2 = treeop2;
8485 ops.location = loc;
8487 ignore = (target == const0_rtx
8488 || ((CONVERT_EXPR_CODE_P (code)
8489 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8490 && TREE_CODE (type) == VOID_TYPE));
8492 /* An operation in what may be a bit-field type needs the
8493 result to be reduced to the precision of the bit-field type,
8494 which is narrower than that of the type's mode. */
8495 reduce_bit_field = (!ignore
8496 && TREE_CODE (type) == INTEGER_TYPE
8497 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8499 /* If we are going to ignore this result, we need only do something
8500 if there is a side-effect somewhere in the expression. If there
8501 is, short-circuit the most common cases here. Note that we must
8502 not call expand_expr with anything but const0_rtx in case this
8503 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
8505 if (ignore)
8507 if (! TREE_SIDE_EFFECTS (exp))
8508 return const0_rtx;
8510 /* Ensure we reference a volatile object even if value is ignored, but
8511 don't do this if all we are doing is taking its address. */
8512 if (TREE_THIS_VOLATILE (exp)
8513 && TREE_CODE (exp) != FUNCTION_DECL
8514 && mode != VOIDmode && mode != BLKmode
8515 && modifier != EXPAND_CONST_ADDRESS)
8517 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
8518 if (MEM_P (temp))
8519 temp = copy_to_reg (temp);
8520 return const0_rtx;
8523 if (TREE_CODE_CLASS (code) == tcc_unary
8524 || code == COMPONENT_REF || code == INDIRECT_REF)
8525 return expand_expr (treeop0, const0_rtx, VOIDmode,
8526 modifier);
8528 else if (TREE_CODE_CLASS (code) == tcc_binary
8529 || TREE_CODE_CLASS (code) == tcc_comparison
8530 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
8532 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
8533 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
8534 return const0_rtx;
8536 else if (code == BIT_FIELD_REF)
8538 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
8539 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
8540 expand_expr (treeop2, const0_rtx, VOIDmode, modifier);
8541 return const0_rtx;
8544 target = 0;
8547 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8548 target = 0;
8550 /* Use subtarget as the target for operand 0 of a binary operation. */
8551 subtarget = get_subtarget (target);
8552 original_target = target;
8554 switch (code)
8556 case LABEL_DECL:
8558 tree function = decl_function_context (exp);
8560 temp = label_rtx (exp);
8561 temp = gen_rtx_LABEL_REF (Pmode, temp);
8563 if (function != current_function_decl
8564 && function != 0)
8565 LABEL_REF_NONLOCAL_P (temp) = 1;
8567 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
8568 return temp;
8571 case SSA_NAME:
8572 /* ??? ivopts calls expander, without any preparation from
8573 out-of-ssa. So fake instructions as if this was an access to the
8574 base variable. This unnecessarily allocates a pseudo, see how we can
8575 reuse it, if partition base vars have it set already. */
8576 if (!currently_expanding_to_rtl)
8577 return expand_expr_real_1 (SSA_NAME_VAR (exp), target, tmode, modifier,
8578 NULL);
8580 g = get_gimple_for_ssa_name (exp);
8581 /* For EXPAND_INITIALIZER try harder to get something simpler. */
8582 if (g == NULL
8583 && modifier == EXPAND_INITIALIZER
8584 && !SSA_NAME_IS_DEFAULT_DEF (exp)
8585 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
8586 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
8587 g = SSA_NAME_DEF_STMT (exp);
8588 if (g)
8589 return expand_expr_real (gimple_assign_rhs_to_tree (g), target, tmode,
8590 modifier, NULL);
8592 ssa_name = exp;
8593 decl_rtl = get_rtx_for_ssa_name (ssa_name);
8594 exp = SSA_NAME_VAR (ssa_name);
8595 goto expand_decl_rtl;
8597 case PARM_DECL:
8598 case VAR_DECL:
8599 /* If a static var's type was incomplete when the decl was written,
8600 but the type is complete now, lay out the decl now. */
8601 if (DECL_SIZE (exp) == 0
8602 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
8603 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
8604 layout_decl (exp, 0);
8606 /* ... fall through ... */
8608 case FUNCTION_DECL:
8609 case RESULT_DECL:
8610 decl_rtl = DECL_RTL (exp);
8611 expand_decl_rtl:
8612 gcc_assert (decl_rtl);
8613 decl_rtl = copy_rtx (decl_rtl);
8614 /* Record writes to register variables. */
8615 if (modifier == EXPAND_WRITE && REG_P (decl_rtl)
8616 && REGNO (decl_rtl) < FIRST_PSEUDO_REGISTER)
8618 int i = REGNO (decl_rtl);
8619 int nregs = hard_regno_nregs[i][GET_MODE (decl_rtl)];
8620 while (nregs)
8622 SET_HARD_REG_BIT (crtl->asm_clobbers, i);
8623 i++;
8624 nregs--;
8628 /* Ensure variable marked as used even if it doesn't go through
8629 a parser. If it hasn't be used yet, write out an external
8630 definition. */
8631 if (! TREE_USED (exp))
8633 assemble_external (exp);
8634 TREE_USED (exp) = 1;
8637 /* Show we haven't gotten RTL for this yet. */
8638 temp = 0;
8640 /* Variables inherited from containing functions should have
8641 been lowered by this point. */
8642 context = decl_function_context (exp);
8643 gcc_assert (!context
8644 || context == current_function_decl
8645 || TREE_STATIC (exp)
8646 || DECL_EXTERNAL (exp)
8647 /* ??? C++ creates functions that are not TREE_STATIC. */
8648 || TREE_CODE (exp) == FUNCTION_DECL);
8650 /* This is the case of an array whose size is to be determined
8651 from its initializer, while the initializer is still being parsed.
8652 See expand_decl. */
8654 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
8655 temp = validize_mem (decl_rtl);
8657 /* If DECL_RTL is memory, we are in the normal case and the
8658 address is not valid, get the address into a register. */
8660 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
8662 if (alt_rtl)
8663 *alt_rtl = decl_rtl;
8664 decl_rtl = use_anchored_address (decl_rtl);
8665 if (modifier != EXPAND_CONST_ADDRESS
8666 && modifier != EXPAND_SUM
8667 && !memory_address_addr_space_p (DECL_MODE (exp),
8668 XEXP (decl_rtl, 0),
8669 MEM_ADDR_SPACE (decl_rtl)))
8670 temp = replace_equiv_address (decl_rtl,
8671 copy_rtx (XEXP (decl_rtl, 0)));
8674 /* If we got something, return it. But first, set the alignment
8675 if the address is a register. */
8676 if (temp != 0)
8678 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
8679 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
8681 return temp;
8684 /* If the mode of DECL_RTL does not match that of the decl,
8685 there are two cases: we are dealing with a BLKmode value
8686 that is returned in a register, or we are dealing with
8687 a promoted value. In the latter case, return a SUBREG
8688 of the wanted mode, but mark it so that we know that it
8689 was already extended. */
8690 if (REG_P (decl_rtl)
8691 && DECL_MODE (exp) != BLKmode
8692 && GET_MODE (decl_rtl) != DECL_MODE (exp))
8694 enum machine_mode pmode;
8696 /* Get the signedness to be used for this variable. Ensure we get
8697 the same mode we got when the variable was declared. */
8698 if (code == SSA_NAME
8699 && (g = SSA_NAME_DEF_STMT (ssa_name))
8700 && gimple_code (g) == GIMPLE_CALL)
8701 pmode = promote_function_mode (type, mode, &unsignedp,
8702 TREE_TYPE
8703 (TREE_TYPE (gimple_call_fn (g))),
8705 else
8706 pmode = promote_decl_mode (exp, &unsignedp);
8707 gcc_assert (GET_MODE (decl_rtl) == pmode);
8709 temp = gen_lowpart_SUBREG (mode, decl_rtl);
8710 SUBREG_PROMOTED_VAR_P (temp) = 1;
8711 SUBREG_PROMOTED_UNSIGNED_SET (temp, unsignedp);
8712 return temp;
8715 return decl_rtl;
8717 case INTEGER_CST:
8718 temp = immed_double_const (TREE_INT_CST_LOW (exp),
8719 TREE_INT_CST_HIGH (exp), mode);
8721 return temp;
8723 case VECTOR_CST:
8725 tree tmp = NULL_TREE;
8726 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
8727 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
8728 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
8729 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
8730 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
8731 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
8732 return const_vector_from_tree (exp);
8733 if (GET_MODE_CLASS (mode) == MODE_INT)
8735 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
8736 if (type_for_mode)
8737 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
8739 if (!tmp)
8740 tmp = build_constructor_from_list (type,
8741 TREE_VECTOR_CST_ELTS (exp));
8742 return expand_expr (tmp, ignore ? const0_rtx : target,
8743 tmode, modifier);
8746 case CONST_DECL:
8747 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
8749 case REAL_CST:
8750 /* If optimized, generate immediate CONST_DOUBLE
8751 which will be turned into memory by reload if necessary.
8753 We used to force a register so that loop.c could see it. But
8754 this does not allow gen_* patterns to perform optimizations with
8755 the constants. It also produces two insns in cases like "x = 1.0;".
8756 On most machines, floating-point constants are not permitted in
8757 many insns, so we'd end up copying it to a register in any case.
8759 Now, we do the copying in expand_binop, if appropriate. */
8760 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
8761 TYPE_MODE (TREE_TYPE (exp)));
8763 case FIXED_CST:
8764 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
8765 TYPE_MODE (TREE_TYPE (exp)));
8767 case COMPLEX_CST:
8768 /* Handle evaluating a complex constant in a CONCAT target. */
8769 if (original_target && GET_CODE (original_target) == CONCAT)
8771 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
8772 rtx rtarg, itarg;
8774 rtarg = XEXP (original_target, 0);
8775 itarg = XEXP (original_target, 1);
8777 /* Move the real and imaginary parts separately. */
8778 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
8779 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
8781 if (op0 != rtarg)
8782 emit_move_insn (rtarg, op0);
8783 if (op1 != itarg)
8784 emit_move_insn (itarg, op1);
8786 return original_target;
8789 /* ... fall through ... */
8791 case STRING_CST:
8792 temp = expand_expr_constant (exp, 1, modifier);
8794 /* temp contains a constant address.
8795 On RISC machines where a constant address isn't valid,
8796 make some insns to get that address into a register. */
8797 if (modifier != EXPAND_CONST_ADDRESS
8798 && modifier != EXPAND_INITIALIZER
8799 && modifier != EXPAND_SUM
8800 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
8801 MEM_ADDR_SPACE (temp)))
8802 return replace_equiv_address (temp,
8803 copy_rtx (XEXP (temp, 0)));
8804 return temp;
8806 case SAVE_EXPR:
8808 tree val = treeop0;
8809 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl);
8811 if (!SAVE_EXPR_RESOLVED_P (exp))
8813 /* We can indeed still hit this case, typically via builtin
8814 expanders calling save_expr immediately before expanding
8815 something. Assume this means that we only have to deal
8816 with non-BLKmode values. */
8817 gcc_assert (GET_MODE (ret) != BLKmode);
8819 val = build_decl (EXPR_LOCATION (exp),
8820 VAR_DECL, NULL, TREE_TYPE (exp));
8821 DECL_ARTIFICIAL (val) = 1;
8822 DECL_IGNORED_P (val) = 1;
8823 treeop0 = val;
8824 TREE_OPERAND (exp, 0) = treeop0;
8825 SAVE_EXPR_RESOLVED_P (exp) = 1;
8827 if (!CONSTANT_P (ret))
8828 ret = copy_to_reg (ret);
8829 SET_DECL_RTL (val, ret);
8832 return ret;
8836 case CONSTRUCTOR:
8837 /* If we don't need the result, just ensure we evaluate any
8838 subexpressions. */
8839 if (ignore)
8841 unsigned HOST_WIDE_INT idx;
8842 tree value;
8844 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
8845 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
8847 return const0_rtx;
8850 return expand_constructor (exp, target, modifier, false);
8852 case TARGET_MEM_REF:
8854 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
8855 struct mem_address addr;
8856 int icode, align;
8858 get_address_description (exp, &addr);
8859 op0 = addr_for_mem_ref (&addr, as, true);
8860 op0 = memory_address_addr_space (mode, op0, as);
8861 temp = gen_rtx_MEM (mode, op0);
8862 set_mem_attributes (temp, exp, 0);
8863 set_mem_addr_space (temp, as);
8864 align = MAX (TYPE_ALIGN (TREE_TYPE (exp)),
8865 get_object_alignment (exp, BIGGEST_ALIGNMENT));
8866 if (mode != BLKmode
8867 && (unsigned) align < GET_MODE_ALIGNMENT (mode)
8868 /* If the target does not have special handling for unaligned
8869 loads of mode then it can use regular moves for them. */
8870 && ((icode = optab_handler (movmisalign_optab, mode))
8871 != CODE_FOR_nothing))
8873 rtx reg, insn;
8875 /* We've already validated the memory, and we're creating a
8876 new pseudo destination. The predicates really can't fail. */
8877 reg = gen_reg_rtx (mode);
8879 /* Nor can the insn generator. */
8880 insn = GEN_FCN (icode) (reg, temp);
8881 gcc_assert (insn != NULL_RTX);
8882 emit_insn (insn);
8884 return reg;
8886 return temp;
8889 case MEM_REF:
8891 addr_space_t as
8892 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 1))));
8893 enum machine_mode address_mode;
8894 tree base = TREE_OPERAND (exp, 0);
8895 gimple def_stmt;
8896 int icode, align;
8897 /* Handle expansion of non-aliased memory with non-BLKmode. That
8898 might end up in a register. */
8899 if (TREE_CODE (base) == ADDR_EXPR)
8901 HOST_WIDE_INT offset = mem_ref_offset (exp).low;
8902 tree bit_offset;
8903 base = TREE_OPERAND (base, 0);
8904 if (!DECL_P (base))
8906 HOST_WIDE_INT off;
8907 base = get_addr_base_and_unit_offset (base, &off);
8908 gcc_assert (base);
8909 offset += off;
8911 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
8912 decl we must use bitfield operations. */
8913 if (DECL_P (base)
8914 && !TREE_ADDRESSABLE (base)
8915 && DECL_MODE (base) != BLKmode
8916 && DECL_RTL_SET_P (base)
8917 && !MEM_P (DECL_RTL (base)))
8919 tree bftype;
8920 if (offset == 0
8921 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1)
8922 && (GET_MODE_BITSIZE (DECL_MODE (base))
8923 == TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))))
8924 return expand_expr (build1 (VIEW_CONVERT_EXPR,
8925 TREE_TYPE (exp), base),
8926 target, tmode, modifier);
8927 bit_offset = bitsize_int (offset * BITS_PER_UNIT);
8928 bftype = TREE_TYPE (base);
8929 if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
8930 bftype = TREE_TYPE (exp);
8931 return expand_expr (build3 (BIT_FIELD_REF, bftype,
8932 base,
8933 TYPE_SIZE (TREE_TYPE (exp)),
8934 bit_offset),
8935 target, tmode, modifier);
8938 address_mode = targetm.addr_space.address_mode (as);
8939 base = TREE_OPERAND (exp, 0);
8940 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
8942 tree mask = gimple_assign_rhs2 (def_stmt);
8943 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
8944 gimple_assign_rhs1 (def_stmt), mask);
8945 TREE_OPERAND (exp, 0) = base;
8947 align = MAX (TYPE_ALIGN (TREE_TYPE (exp)),
8948 get_object_alignment (exp, BIGGEST_ALIGNMENT));
8949 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
8950 op0 = memory_address_addr_space (address_mode, op0, as);
8951 if (!integer_zerop (TREE_OPERAND (exp, 1)))
8953 rtx off
8954 = immed_double_int_const (mem_ref_offset (exp), address_mode);
8955 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
8957 op0 = memory_address_addr_space (mode, op0, as);
8958 temp = gen_rtx_MEM (mode, op0);
8959 set_mem_attributes (temp, exp, 0);
8960 set_mem_addr_space (temp, as);
8961 if (TREE_THIS_VOLATILE (exp))
8962 MEM_VOLATILE_P (temp) = 1;
8963 if (mode != BLKmode
8964 && (unsigned) align < GET_MODE_ALIGNMENT (mode)
8965 /* If the target does not have special handling for unaligned
8966 loads of mode then it can use regular moves for them. */
8967 && ((icode = optab_handler (movmisalign_optab, mode))
8968 != CODE_FOR_nothing))
8970 rtx reg, insn;
8972 /* We've already validated the memory, and we're creating a
8973 new pseudo destination. The predicates really can't fail. */
8974 reg = gen_reg_rtx (mode);
8976 /* Nor can the insn generator. */
8977 insn = GEN_FCN (icode) (reg, temp);
8978 emit_insn (insn);
8980 return reg;
8982 return temp;
8985 case ARRAY_REF:
8988 tree array = treeop0;
8989 tree index = treeop1;
8991 /* Fold an expression like: "foo"[2].
8992 This is not done in fold so it won't happen inside &.
8993 Don't fold if this is for wide characters since it's too
8994 difficult to do correctly and this is a very rare case. */
8996 if (modifier != EXPAND_CONST_ADDRESS
8997 && modifier != EXPAND_INITIALIZER
8998 && modifier != EXPAND_MEMORY)
9000 tree t = fold_read_from_constant_string (exp);
9002 if (t)
9003 return expand_expr (t, target, tmode, modifier);
9006 /* If this is a constant index into a constant array,
9007 just get the value from the array. Handle both the cases when
9008 we have an explicit constructor and when our operand is a variable
9009 that was declared const. */
9011 if (modifier != EXPAND_CONST_ADDRESS
9012 && modifier != EXPAND_INITIALIZER
9013 && modifier != EXPAND_MEMORY
9014 && TREE_CODE (array) == CONSTRUCTOR
9015 && ! TREE_SIDE_EFFECTS (array)
9016 && TREE_CODE (index) == INTEGER_CST)
9018 unsigned HOST_WIDE_INT ix;
9019 tree field, value;
9021 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9022 field, value)
9023 if (tree_int_cst_equal (field, index))
9025 if (!TREE_SIDE_EFFECTS (value))
9026 return expand_expr (fold (value), target, tmode, modifier);
9027 break;
9031 else if (optimize >= 1
9032 && modifier != EXPAND_CONST_ADDRESS
9033 && modifier != EXPAND_INITIALIZER
9034 && modifier != EXPAND_MEMORY
9035 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9036 && TREE_CODE (array) == VAR_DECL && DECL_INITIAL (array)
9037 && TREE_CODE (DECL_INITIAL (array)) != ERROR_MARK
9038 && const_value_known_p (array))
9040 if (TREE_CODE (index) == INTEGER_CST)
9042 tree init = DECL_INITIAL (array);
9044 if (TREE_CODE (init) == CONSTRUCTOR)
9046 unsigned HOST_WIDE_INT ix;
9047 tree field, value;
9049 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9050 field, value)
9051 if (tree_int_cst_equal (field, index))
9053 if (TREE_SIDE_EFFECTS (value))
9054 break;
9056 if (TREE_CODE (value) == CONSTRUCTOR)
9058 /* If VALUE is a CONSTRUCTOR, this
9059 optimization is only useful if
9060 this doesn't store the CONSTRUCTOR
9061 into memory. If it does, it is more
9062 efficient to just load the data from
9063 the array directly. */
9064 rtx ret = expand_constructor (value, target,
9065 modifier, true);
9066 if (ret == NULL_RTX)
9067 break;
9070 return expand_expr (fold (value), target, tmode,
9071 modifier);
9074 else if(TREE_CODE (init) == STRING_CST)
9076 tree index1 = index;
9077 tree low_bound = array_ref_low_bound (exp);
9078 index1 = fold_convert_loc (loc, sizetype,
9079 treeop1);
9081 /* Optimize the special-case of a zero lower bound.
9083 We convert the low_bound to sizetype to avoid some problems
9084 with constant folding. (E.g. suppose the lower bound is 1,
9085 and its mode is QI. Without the conversion,l (ARRAY
9086 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9087 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
9089 if (! integer_zerop (low_bound))
9090 index1 = size_diffop_loc (loc, index1,
9091 fold_convert_loc (loc, sizetype,
9092 low_bound));
9094 if (0 > compare_tree_int (index1,
9095 TREE_STRING_LENGTH (init)))
9097 tree type = TREE_TYPE (TREE_TYPE (init));
9098 enum machine_mode mode = TYPE_MODE (type);
9100 if (GET_MODE_CLASS (mode) == MODE_INT
9101 && GET_MODE_SIZE (mode) == 1)
9102 return gen_int_mode (TREE_STRING_POINTER (init)
9103 [TREE_INT_CST_LOW (index1)],
9104 mode);
9110 goto normal_inner_ref;
9112 case COMPONENT_REF:
9113 /* If the operand is a CONSTRUCTOR, we can just extract the
9114 appropriate field if it is present. */
9115 if (TREE_CODE (treeop0) == CONSTRUCTOR)
9117 unsigned HOST_WIDE_INT idx;
9118 tree field, value;
9120 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
9121 idx, field, value)
9122 if (field == treeop1
9123 /* We can normally use the value of the field in the
9124 CONSTRUCTOR. However, if this is a bitfield in
9125 an integral mode that we can fit in a HOST_WIDE_INT,
9126 we must mask only the number of bits in the bitfield,
9127 since this is done implicitly by the constructor. If
9128 the bitfield does not meet either of those conditions,
9129 we can't do this optimization. */
9130 && (! DECL_BIT_FIELD (field)
9131 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
9132 && (GET_MODE_BITSIZE (DECL_MODE (field))
9133 <= HOST_BITS_PER_WIDE_INT))))
9135 if (DECL_BIT_FIELD (field)
9136 && modifier == EXPAND_STACK_PARM)
9137 target = 0;
9138 op0 = expand_expr (value, target, tmode, modifier);
9139 if (DECL_BIT_FIELD (field))
9141 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
9142 enum machine_mode imode = TYPE_MODE (TREE_TYPE (field));
9144 if (TYPE_UNSIGNED (TREE_TYPE (field)))
9146 op1 = GEN_INT (((HOST_WIDE_INT) 1 << bitsize) - 1);
9147 op0 = expand_and (imode, op0, op1, target);
9149 else
9151 tree count
9152 = build_int_cst (NULL_TREE,
9153 GET_MODE_BITSIZE (imode) - bitsize);
9155 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
9156 target, 0);
9157 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
9158 target, 0);
9162 return op0;
9165 goto normal_inner_ref;
9167 case BIT_FIELD_REF:
9168 case ARRAY_RANGE_REF:
9169 normal_inner_ref:
9171 enum machine_mode mode1, mode2;
9172 HOST_WIDE_INT bitsize, bitpos;
9173 tree offset;
9174 int volatilep = 0, must_force_mem;
9175 bool packedp = false;
9176 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
9177 &mode1, &unsignedp, &volatilep, true);
9178 rtx orig_op0, memloc;
9180 /* If we got back the original object, something is wrong. Perhaps
9181 we are evaluating an expression too early. In any event, don't
9182 infinitely recurse. */
9183 gcc_assert (tem != exp);
9185 if (TYPE_PACKED (TREE_TYPE (TREE_OPERAND (exp, 0)))
9186 || (TREE_CODE (TREE_OPERAND (exp, 1)) == FIELD_DECL
9187 && DECL_PACKED (TREE_OPERAND (exp, 1))))
9188 packedp = true;
9190 /* If TEM's type is a union of variable size, pass TARGET to the inner
9191 computation, since it will need a temporary and TARGET is known
9192 to have to do. This occurs in unchecked conversion in Ada. */
9193 orig_op0 = op0
9194 = expand_expr (tem,
9195 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
9196 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
9197 != INTEGER_CST)
9198 && modifier != EXPAND_STACK_PARM
9199 ? target : NULL_RTX),
9200 VOIDmode,
9201 (modifier == EXPAND_INITIALIZER
9202 || modifier == EXPAND_CONST_ADDRESS
9203 || modifier == EXPAND_STACK_PARM)
9204 ? modifier : EXPAND_NORMAL);
9207 /* If the bitfield is volatile, we want to access it in the
9208 field's mode, not the computed mode.
9209 If a MEM has VOIDmode (external with incomplete type),
9210 use BLKmode for it instead. */
9211 if (MEM_P (op0))
9213 if (volatilep && flag_strict_volatile_bitfields > 0)
9214 op0 = adjust_address (op0, mode1, 0);
9215 else if (GET_MODE (op0) == VOIDmode)
9216 op0 = adjust_address (op0, BLKmode, 0);
9219 mode2
9220 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
9222 /* If we have either an offset, a BLKmode result, or a reference
9223 outside the underlying object, we must force it to memory.
9224 Such a case can occur in Ada if we have unchecked conversion
9225 of an expression from a scalar type to an aggregate type or
9226 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
9227 passed a partially uninitialized object or a view-conversion
9228 to a larger size. */
9229 must_force_mem = (offset
9230 || mode1 == BLKmode
9231 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
9233 /* Handle CONCAT first. */
9234 if (GET_CODE (op0) == CONCAT && !must_force_mem)
9236 if (bitpos == 0
9237 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
9238 return op0;
9239 if (bitpos == 0
9240 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9241 && bitsize)
9243 op0 = XEXP (op0, 0);
9244 mode2 = GET_MODE (op0);
9246 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9247 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
9248 && bitpos
9249 && bitsize)
9251 op0 = XEXP (op0, 1);
9252 bitpos = 0;
9253 mode2 = GET_MODE (op0);
9255 else
9256 /* Otherwise force into memory. */
9257 must_force_mem = 1;
9260 /* If this is a constant, put it in a register if it is a legitimate
9261 constant and we don't need a memory reference. */
9262 if (CONSTANT_P (op0)
9263 && mode2 != BLKmode
9264 && LEGITIMATE_CONSTANT_P (op0)
9265 && !must_force_mem)
9266 op0 = force_reg (mode2, op0);
9268 /* Otherwise, if this is a constant, try to force it to the constant
9269 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
9270 is a legitimate constant. */
9271 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
9272 op0 = validize_mem (memloc);
9274 /* Otherwise, if this is a constant or the object is not in memory
9275 and need be, put it there. */
9276 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
9278 tree nt = build_qualified_type (TREE_TYPE (tem),
9279 (TYPE_QUALS (TREE_TYPE (tem))
9280 | TYPE_QUAL_CONST));
9281 memloc = assign_temp (nt, 1, 1, 1);
9282 emit_move_insn (memloc, op0);
9283 op0 = memloc;
9286 if (offset)
9288 enum machine_mode address_mode;
9289 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
9290 EXPAND_SUM);
9292 gcc_assert (MEM_P (op0));
9294 address_mode
9295 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (op0));
9296 if (GET_MODE (offset_rtx) != address_mode)
9297 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
9299 if (GET_MODE (op0) == BLKmode
9300 /* A constant address in OP0 can have VOIDmode, we must
9301 not try to call force_reg in that case. */
9302 && GET_MODE (XEXP (op0, 0)) != VOIDmode
9303 && bitsize != 0
9304 && (bitpos % bitsize) == 0
9305 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
9306 && MEM_ALIGN (op0) == GET_MODE_ALIGNMENT (mode1))
9308 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
9309 bitpos = 0;
9312 op0 = offset_address (op0, offset_rtx,
9313 highest_pow2_factor (offset));
9316 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
9317 record its alignment as BIGGEST_ALIGNMENT. */
9318 if (MEM_P (op0) && bitpos == 0 && offset != 0
9319 && is_aligning_offset (offset, tem))
9320 set_mem_align (op0, BIGGEST_ALIGNMENT);
9322 /* Don't forget about volatility even if this is a bitfield. */
9323 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
9325 if (op0 == orig_op0)
9326 op0 = copy_rtx (op0);
9328 MEM_VOLATILE_P (op0) = 1;
9331 /* In cases where an aligned union has an unaligned object
9332 as a field, we might be extracting a BLKmode value from
9333 an integer-mode (e.g., SImode) object. Handle this case
9334 by doing the extract into an object as wide as the field
9335 (which we know to be the width of a basic mode), then
9336 storing into memory, and changing the mode to BLKmode. */
9337 if (mode1 == VOIDmode
9338 || REG_P (op0) || GET_CODE (op0) == SUBREG
9339 || (mode1 != BLKmode && ! direct_load[(int) mode1]
9340 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9341 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
9342 && modifier != EXPAND_CONST_ADDRESS
9343 && modifier != EXPAND_INITIALIZER)
9344 /* If the field is volatile, we always want an aligned
9345 access. Do this in following two situations:
9346 1. the access is not already naturally
9347 aligned, otherwise "normal" (non-bitfield) volatile fields
9348 become non-addressable.
9349 2. the bitsize is narrower than the access size. Need
9350 to extract bitfields from the access. */
9351 || (volatilep && flag_strict_volatile_bitfields > 0
9352 && (bitpos % GET_MODE_ALIGNMENT (mode) != 0
9353 || (mode1 != BLKmode
9354 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)))
9355 /* If the field isn't aligned enough to fetch as a memref,
9356 fetch it as a bit field. */
9357 || (mode1 != BLKmode
9358 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
9359 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
9360 || (MEM_P (op0)
9361 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
9362 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
9363 && ((modifier == EXPAND_CONST_ADDRESS
9364 || modifier == EXPAND_INITIALIZER)
9365 ? STRICT_ALIGNMENT
9366 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
9367 || (bitpos % BITS_PER_UNIT != 0)))
9368 /* If the type and the field are a constant size and the
9369 size of the type isn't the same size as the bitfield,
9370 we must use bitfield operations. */
9371 || (bitsize >= 0
9372 && TYPE_SIZE (TREE_TYPE (exp))
9373 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
9374 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
9375 bitsize)))
9377 enum machine_mode ext_mode = mode;
9379 if (ext_mode == BLKmode
9380 && ! (target != 0 && MEM_P (op0)
9381 && MEM_P (target)
9382 && bitpos % BITS_PER_UNIT == 0))
9383 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
9385 if (ext_mode == BLKmode)
9387 if (target == 0)
9388 target = assign_temp (type, 0, 1, 1);
9390 if (bitsize == 0)
9391 return target;
9393 /* In this case, BITPOS must start at a byte boundary and
9394 TARGET, if specified, must be a MEM. */
9395 gcc_assert (MEM_P (op0)
9396 && (!target || MEM_P (target))
9397 && !(bitpos % BITS_PER_UNIT));
9399 emit_block_move (target,
9400 adjust_address (op0, VOIDmode,
9401 bitpos / BITS_PER_UNIT),
9402 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
9403 / BITS_PER_UNIT),
9404 (modifier == EXPAND_STACK_PARM
9405 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
9407 return target;
9410 op0 = validize_mem (op0);
9412 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
9413 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
9415 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp, packedp,
9416 (modifier == EXPAND_STACK_PARM
9417 ? NULL_RTX : target),
9418 ext_mode, ext_mode);
9420 /* If the result is a record type and BITSIZE is narrower than
9421 the mode of OP0, an integral mode, and this is a big endian
9422 machine, we must put the field into the high-order bits. */
9423 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
9424 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9425 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
9426 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
9427 size_int (GET_MODE_BITSIZE (GET_MODE (op0))
9428 - bitsize),
9429 op0, 1);
9431 /* If the result type is BLKmode, store the data into a temporary
9432 of the appropriate type, but with the mode corresponding to the
9433 mode for the data we have (op0's mode). It's tempting to make
9434 this a constant type, since we know it's only being stored once,
9435 but that can cause problems if we are taking the address of this
9436 COMPONENT_REF because the MEM of any reference via that address
9437 will have flags corresponding to the type, which will not
9438 necessarily be constant. */
9439 if (mode == BLKmode)
9441 HOST_WIDE_INT size = GET_MODE_BITSIZE (ext_mode);
9442 rtx new_rtx;
9444 /* If the reference doesn't use the alias set of its type,
9445 we cannot create the temporary using that type. */
9446 if (component_uses_parent_alias_set (exp))
9448 new_rtx = assign_stack_local (ext_mode, size, 0);
9449 set_mem_alias_set (new_rtx, get_alias_set (exp));
9451 else
9452 new_rtx = assign_stack_temp_for_type (ext_mode, size, 0, type);
9454 emit_move_insn (new_rtx, op0);
9455 op0 = copy_rtx (new_rtx);
9456 PUT_MODE (op0, BLKmode);
9457 set_mem_attributes (op0, exp, 1);
9460 return op0;
9463 /* If the result is BLKmode, use that to access the object
9464 now as well. */
9465 if (mode == BLKmode)
9466 mode1 = BLKmode;
9468 /* Get a reference to just this component. */
9469 if (modifier == EXPAND_CONST_ADDRESS
9470 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
9471 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
9472 else
9473 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
9475 if (op0 == orig_op0)
9476 op0 = copy_rtx (op0);
9478 set_mem_attributes (op0, exp, 0);
9479 if (REG_P (XEXP (op0, 0)))
9480 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
9482 MEM_VOLATILE_P (op0) |= volatilep;
9483 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
9484 || modifier == EXPAND_CONST_ADDRESS
9485 || modifier == EXPAND_INITIALIZER)
9486 return op0;
9487 else if (target == 0)
9488 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9490 convert_move (target, op0, unsignedp);
9491 return target;
9494 case OBJ_TYPE_REF:
9495 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
9497 case CALL_EXPR:
9498 /* All valid uses of __builtin_va_arg_pack () are removed during
9499 inlining. */
9500 if (CALL_EXPR_VA_ARG_PACK (exp))
9501 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
9503 tree fndecl = get_callee_fndecl (exp), attr;
9505 if (fndecl
9506 && (attr = lookup_attribute ("error",
9507 DECL_ATTRIBUTES (fndecl))) != NULL)
9508 error ("%Kcall to %qs declared with attribute error: %s",
9509 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
9510 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
9511 if (fndecl
9512 && (attr = lookup_attribute ("warning",
9513 DECL_ATTRIBUTES (fndecl))) != NULL)
9514 warning_at (tree_nonartificial_location (exp),
9515 0, "%Kcall to %qs declared with attribute warning: %s",
9516 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
9517 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
9519 /* Check for a built-in function. */
9520 if (fndecl && DECL_BUILT_IN (fndecl))
9522 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
9523 return expand_builtin (exp, target, subtarget, tmode, ignore);
9526 return expand_call (exp, target, ignore);
9528 case VIEW_CONVERT_EXPR:
9529 op0 = NULL_RTX;
9531 /* If we are converting to BLKmode, try to avoid an intermediate
9532 temporary by fetching an inner memory reference. */
9533 if (mode == BLKmode
9534 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
9535 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
9536 && handled_component_p (treeop0))
9538 enum machine_mode mode1;
9539 HOST_WIDE_INT bitsize, bitpos;
9540 tree offset;
9541 int unsignedp;
9542 int volatilep = 0;
9543 tree tem
9544 = get_inner_reference (treeop0, &bitsize, &bitpos,
9545 &offset, &mode1, &unsignedp, &volatilep,
9546 true);
9547 rtx orig_op0;
9549 /* ??? We should work harder and deal with non-zero offsets. */
9550 if (!offset
9551 && (bitpos % BITS_PER_UNIT) == 0
9552 && bitsize >= 0
9553 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) == 0)
9555 /* See the normal_inner_ref case for the rationale. */
9556 orig_op0
9557 = expand_expr (tem,
9558 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
9559 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
9560 != INTEGER_CST)
9561 && modifier != EXPAND_STACK_PARM
9562 ? target : NULL_RTX),
9563 VOIDmode,
9564 (modifier == EXPAND_INITIALIZER
9565 || modifier == EXPAND_CONST_ADDRESS
9566 || modifier == EXPAND_STACK_PARM)
9567 ? modifier : EXPAND_NORMAL);
9569 if (MEM_P (orig_op0))
9571 op0 = orig_op0;
9573 /* Get a reference to just this component. */
9574 if (modifier == EXPAND_CONST_ADDRESS
9575 || modifier == EXPAND_SUM
9576 || modifier == EXPAND_INITIALIZER)
9577 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
9578 else
9579 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
9581 if (op0 == orig_op0)
9582 op0 = copy_rtx (op0);
9584 set_mem_attributes (op0, treeop0, 0);
9585 if (REG_P (XEXP (op0, 0)))
9586 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
9588 MEM_VOLATILE_P (op0) |= volatilep;
9593 if (!op0)
9594 op0 = expand_expr (treeop0,
9595 NULL_RTX, VOIDmode, modifier);
9597 /* If the input and output modes are both the same, we are done. */
9598 if (mode == GET_MODE (op0))
9600 /* If neither mode is BLKmode, and both modes are the same size
9601 then we can use gen_lowpart. */
9602 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
9603 && GET_MODE_SIZE (mode) == GET_MODE_SIZE (GET_MODE (op0))
9604 && !COMPLEX_MODE_P (GET_MODE (op0)))
9606 if (GET_CODE (op0) == SUBREG)
9607 op0 = force_reg (GET_MODE (op0), op0);
9608 temp = gen_lowpart_common (mode, op0);
9609 if (temp)
9610 op0 = temp;
9611 else
9613 if (!REG_P (op0) && !MEM_P (op0))
9614 op0 = force_reg (GET_MODE (op0), op0);
9615 op0 = gen_lowpart (mode, op0);
9618 /* If both types are integral, convert from one mode to the other. */
9619 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
9620 op0 = convert_modes (mode, GET_MODE (op0), op0,
9621 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9622 /* As a last resort, spill op0 to memory, and reload it in a
9623 different mode. */
9624 else if (!MEM_P (op0))
9626 /* If the operand is not a MEM, force it into memory. Since we
9627 are going to be changing the mode of the MEM, don't call
9628 force_const_mem for constants because we don't allow pool
9629 constants to change mode. */
9630 tree inner_type = TREE_TYPE (treeop0);
9632 gcc_assert (!TREE_ADDRESSABLE (exp));
9634 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
9635 target
9636 = assign_stack_temp_for_type
9637 (TYPE_MODE (inner_type),
9638 GET_MODE_SIZE (TYPE_MODE (inner_type)), 0, inner_type);
9640 emit_move_insn (target, op0);
9641 op0 = target;
9644 /* At this point, OP0 is in the correct mode. If the output type is
9645 such that the operand is known to be aligned, indicate that it is.
9646 Otherwise, we need only be concerned about alignment for non-BLKmode
9647 results. */
9648 if (MEM_P (op0))
9650 enum insn_code icode;
9652 op0 = copy_rtx (op0);
9654 if (TYPE_ALIGN_OK (type))
9655 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
9656 else if (mode != BLKmode
9657 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode)
9658 /* If the target does have special handling for unaligned
9659 loads of mode then use them. */
9660 && ((icode = optab_handler (movmisalign_optab, mode))
9661 != CODE_FOR_nothing))
9663 rtx reg, insn;
9665 op0 = adjust_address (op0, mode, 0);
9666 /* We've already validated the memory, and we're creating a
9667 new pseudo destination. The predicates really can't
9668 fail. */
9669 reg = gen_reg_rtx (mode);
9671 /* Nor can the insn generator. */
9672 insn = GEN_FCN (icode) (reg, op0);
9673 emit_insn (insn);
9674 return reg;
9676 else if (STRICT_ALIGNMENT
9677 && mode != BLKmode
9678 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
9680 tree inner_type = TREE_TYPE (treeop0);
9681 HOST_WIDE_INT temp_size
9682 = MAX (int_size_in_bytes (inner_type),
9683 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
9684 rtx new_rtx
9685 = assign_stack_temp_for_type (mode, temp_size, 0, type);
9686 rtx new_with_op0_mode
9687 = adjust_address (new_rtx, GET_MODE (op0), 0);
9689 gcc_assert (!TREE_ADDRESSABLE (exp));
9691 if (GET_MODE (op0) == BLKmode)
9692 emit_block_move (new_with_op0_mode, op0,
9693 GEN_INT (GET_MODE_SIZE (mode)),
9694 (modifier == EXPAND_STACK_PARM
9695 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
9696 else
9697 emit_move_insn (new_with_op0_mode, op0);
9699 op0 = new_rtx;
9702 op0 = adjust_address (op0, mode, 0);
9705 return op0;
9707 /* Use a compare and a jump for BLKmode comparisons, or for function
9708 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
9710 /* Although TRUTH_{AND,OR}IF_EXPR aren't present in GIMPLE, they
9711 are occassionally created by folding during expansion. */
9712 case TRUTH_ANDIF_EXPR:
9713 case TRUTH_ORIF_EXPR:
9714 if (! ignore
9715 && (target == 0
9716 || modifier == EXPAND_STACK_PARM
9717 || ! safe_from_p (target, treeop0, 1)
9718 || ! safe_from_p (target, treeop1, 1)
9719 /* Make sure we don't have a hard reg (such as function's return
9720 value) live across basic blocks, if not optimizing. */
9721 || (!optimize && REG_P (target)
9722 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9723 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9725 if (target)
9726 emit_move_insn (target, const0_rtx);
9728 op1 = gen_label_rtx ();
9729 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
9731 if (target)
9732 emit_move_insn (target, const1_rtx);
9734 emit_label (op1);
9735 return ignore ? const0_rtx : target;
9737 case STATEMENT_LIST:
9739 tree_stmt_iterator iter;
9741 gcc_assert (ignore);
9743 for (iter = tsi_start (exp); !tsi_end_p (iter); tsi_next (&iter))
9744 expand_expr (tsi_stmt (iter), const0_rtx, VOIDmode, modifier);
9746 return const0_rtx;
9748 case COND_EXPR:
9749 /* A COND_EXPR with its type being VOID_TYPE represents a
9750 conditional jump and is handled in
9751 expand_gimple_cond_expr. */
9752 gcc_assert (!VOID_TYPE_P (type));
9754 /* Note that COND_EXPRs whose type is a structure or union
9755 are required to be constructed to contain assignments of
9756 a temporary variable, so that we can evaluate them here
9757 for side effect only. If type is void, we must do likewise. */
9759 gcc_assert (!TREE_ADDRESSABLE (type)
9760 && !ignore
9761 && TREE_TYPE (treeop1) != void_type_node
9762 && TREE_TYPE (treeop2) != void_type_node);
9764 /* If we are not to produce a result, we have no target. Otherwise,
9765 if a target was specified use it; it will not be used as an
9766 intermediate target unless it is safe. If no target, use a
9767 temporary. */
9769 if (modifier != EXPAND_STACK_PARM
9770 && original_target
9771 && safe_from_p (original_target, treeop0, 1)
9772 && GET_MODE (original_target) == mode
9773 #ifdef HAVE_conditional_move
9774 && (! can_conditionally_move_p (mode)
9775 || REG_P (original_target))
9776 #endif
9777 && !MEM_P (original_target))
9778 temp = original_target;
9779 else
9780 temp = assign_temp (type, 0, 0, 1);
9782 do_pending_stack_adjust ();
9783 NO_DEFER_POP;
9784 op0 = gen_label_rtx ();
9785 op1 = gen_label_rtx ();
9786 jumpifnot (treeop0, op0, -1);
9787 store_expr (treeop1, temp,
9788 modifier == EXPAND_STACK_PARM,
9789 false);
9791 emit_jump_insn (gen_jump (op1));
9792 emit_barrier ();
9793 emit_label (op0);
9794 store_expr (treeop2, temp,
9795 modifier == EXPAND_STACK_PARM,
9796 false);
9798 emit_label (op1);
9799 OK_DEFER_POP;
9800 return temp;
9802 case VEC_COND_EXPR:
9803 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9804 return target;
9806 case MODIFY_EXPR:
9808 tree lhs = treeop0;
9809 tree rhs = treeop1;
9810 gcc_assert (ignore);
9812 /* Check for |= or &= of a bitfield of size one into another bitfield
9813 of size 1. In this case, (unless we need the result of the
9814 assignment) we can do this more efficiently with a
9815 test followed by an assignment, if necessary.
9817 ??? At this point, we can't get a BIT_FIELD_REF here. But if
9818 things change so we do, this code should be enhanced to
9819 support it. */
9820 if (TREE_CODE (lhs) == COMPONENT_REF
9821 && (TREE_CODE (rhs) == BIT_IOR_EXPR
9822 || TREE_CODE (rhs) == BIT_AND_EXPR)
9823 && TREE_OPERAND (rhs, 0) == lhs
9824 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
9825 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
9826 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
9828 rtx label = gen_label_rtx ();
9829 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
9830 do_jump (TREE_OPERAND (rhs, 1),
9831 value ? label : 0,
9832 value ? 0 : label, -1);
9833 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
9834 MOVE_NONTEMPORAL (exp));
9835 do_pending_stack_adjust ();
9836 emit_label (label);
9837 return const0_rtx;
9840 expand_assignment (lhs, rhs, MOVE_NONTEMPORAL (exp));
9841 return const0_rtx;
9844 case ADDR_EXPR:
9845 return expand_expr_addr_expr (exp, target, tmode, modifier);
9847 case REALPART_EXPR:
9848 op0 = expand_normal (treeop0);
9849 return read_complex_part (op0, false);
9851 case IMAGPART_EXPR:
9852 op0 = expand_normal (treeop0);
9853 return read_complex_part (op0, true);
9855 case RETURN_EXPR:
9856 case LABEL_EXPR:
9857 case GOTO_EXPR:
9858 case SWITCH_EXPR:
9859 case ASM_EXPR:
9860 /* Expanded in cfgexpand.c. */
9861 gcc_unreachable ();
9863 case TRY_CATCH_EXPR:
9864 case CATCH_EXPR:
9865 case EH_FILTER_EXPR:
9866 case TRY_FINALLY_EXPR:
9867 /* Lowered by tree-eh.c. */
9868 gcc_unreachable ();
9870 case WITH_CLEANUP_EXPR:
9871 case CLEANUP_POINT_EXPR:
9872 case TARGET_EXPR:
9873 case CASE_LABEL_EXPR:
9874 case VA_ARG_EXPR:
9875 case BIND_EXPR:
9876 case INIT_EXPR:
9877 case CONJ_EXPR:
9878 case COMPOUND_EXPR:
9879 case PREINCREMENT_EXPR:
9880 case PREDECREMENT_EXPR:
9881 case POSTINCREMENT_EXPR:
9882 case POSTDECREMENT_EXPR:
9883 case LOOP_EXPR:
9884 case EXIT_EXPR:
9885 /* Lowered by gimplify.c. */
9886 gcc_unreachable ();
9888 case FDESC_EXPR:
9889 /* Function descriptors are not valid except for as
9890 initialization constants, and should not be expanded. */
9891 gcc_unreachable ();
9893 case WITH_SIZE_EXPR:
9894 /* WITH_SIZE_EXPR expands to its first argument. The caller should
9895 have pulled out the size to use in whatever context it needed. */
9896 return expand_expr_real (treeop0, original_target, tmode,
9897 modifier, alt_rtl);
9899 case REALIGN_LOAD_EXPR:
9901 tree oprnd0 = treeop0;
9902 tree oprnd1 = treeop1;
9903 tree oprnd2 = treeop2;
9904 rtx op2;
9906 this_optab = optab_for_tree_code (code, type, optab_default);
9907 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9908 op2 = expand_normal (oprnd2);
9909 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9910 target, unsignedp);
9911 gcc_assert (temp);
9912 return temp;
9915 case DOT_PROD_EXPR:
9917 tree oprnd0 = treeop0;
9918 tree oprnd1 = treeop1;
9919 tree oprnd2 = treeop2;
9920 rtx op2;
9922 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9923 op2 = expand_normal (oprnd2);
9924 target = expand_widen_pattern_expr (&ops, op0, op1, op2,
9925 target, unsignedp);
9926 return target;
9929 case COMPOUND_LITERAL_EXPR:
9931 /* Initialize the anonymous variable declared in the compound
9932 literal, then return the variable. */
9933 tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
9935 /* Create RTL for this variable. */
9936 if (!DECL_RTL_SET_P (decl))
9938 if (DECL_HARD_REGISTER (decl))
9939 /* The user specified an assembler name for this variable.
9940 Set that up now. */
9941 rest_of_decl_compilation (decl, 0, 0);
9942 else
9943 expand_decl (decl);
9946 return expand_expr_real (decl, original_target, tmode,
9947 modifier, alt_rtl);
9950 default:
9951 return expand_expr_real_2 (&ops, target, tmode, modifier);
9955 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
9956 signedness of TYPE), possibly returning the result in TARGET. */
9957 static rtx
9958 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
9960 HOST_WIDE_INT prec = TYPE_PRECISION (type);
9961 if (target && GET_MODE (target) != GET_MODE (exp))
9962 target = 0;
9963 /* For constant values, reduce using build_int_cst_type. */
9964 if (CONST_INT_P (exp))
9966 HOST_WIDE_INT value = INTVAL (exp);
9967 tree t = build_int_cst_type (type, value);
9968 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
9970 else if (TYPE_UNSIGNED (type))
9972 rtx mask = immed_double_int_const (double_int_mask (prec),
9973 GET_MODE (exp));
9974 return expand_and (GET_MODE (exp), exp, mask, target);
9976 else
9978 tree count = build_int_cst (NULL_TREE,
9979 GET_MODE_BITSIZE (GET_MODE (exp)) - prec);
9980 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp), exp, count, target, 0);
9981 return expand_shift (RSHIFT_EXPR, GET_MODE (exp), exp, count, target, 0);
9985 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
9986 when applied to the address of EXP produces an address known to be
9987 aligned more than BIGGEST_ALIGNMENT. */
9989 static int
9990 is_aligning_offset (const_tree offset, const_tree exp)
9992 /* Strip off any conversions. */
9993 while (CONVERT_EXPR_P (offset))
9994 offset = TREE_OPERAND (offset, 0);
9996 /* We must now have a BIT_AND_EXPR with a constant that is one less than
9997 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
9998 if (TREE_CODE (offset) != BIT_AND_EXPR
9999 || !host_integerp (TREE_OPERAND (offset, 1), 1)
10000 || compare_tree_int (TREE_OPERAND (offset, 1),
10001 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10002 || !exact_log2 (tree_low_cst (TREE_OPERAND (offset, 1), 1) + 1) < 0)
10003 return 0;
10005 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10006 It must be NEGATE_EXPR. Then strip any more conversions. */
10007 offset = TREE_OPERAND (offset, 0);
10008 while (CONVERT_EXPR_P (offset))
10009 offset = TREE_OPERAND (offset, 0);
10011 if (TREE_CODE (offset) != NEGATE_EXPR)
10012 return 0;
10014 offset = TREE_OPERAND (offset, 0);
10015 while (CONVERT_EXPR_P (offset))
10016 offset = TREE_OPERAND (offset, 0);
10018 /* This must now be the address of EXP. */
10019 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10022 /* Return the tree node if an ARG corresponds to a string constant or zero
10023 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10024 in bytes within the string that ARG is accessing. The type of the
10025 offset will be `sizetype'. */
10027 tree
10028 string_constant (tree arg, tree *ptr_offset)
10030 tree array, offset, lower_bound;
10031 STRIP_NOPS (arg);
10033 if (TREE_CODE (arg) == ADDR_EXPR)
10035 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10037 *ptr_offset = size_zero_node;
10038 return TREE_OPERAND (arg, 0);
10040 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10042 array = TREE_OPERAND (arg, 0);
10043 offset = size_zero_node;
10045 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10047 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10048 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10049 if (TREE_CODE (array) != STRING_CST
10050 && TREE_CODE (array) != VAR_DECL)
10051 return 0;
10053 /* Check if the array has a nonzero lower bound. */
10054 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10055 if (!integer_zerop (lower_bound))
10057 /* If the offset and base aren't both constants, return 0. */
10058 if (TREE_CODE (lower_bound) != INTEGER_CST)
10059 return 0;
10060 if (TREE_CODE (offset) != INTEGER_CST)
10061 return 0;
10062 /* Adjust offset by the lower bound. */
10063 offset = size_diffop (fold_convert (sizetype, offset),
10064 fold_convert (sizetype, lower_bound));
10067 else
10068 return 0;
10070 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10072 tree arg0 = TREE_OPERAND (arg, 0);
10073 tree arg1 = TREE_OPERAND (arg, 1);
10075 STRIP_NOPS (arg0);
10076 STRIP_NOPS (arg1);
10078 if (TREE_CODE (arg0) == ADDR_EXPR
10079 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10080 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10082 array = TREE_OPERAND (arg0, 0);
10083 offset = arg1;
10085 else if (TREE_CODE (arg1) == ADDR_EXPR
10086 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10087 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10089 array = TREE_OPERAND (arg1, 0);
10090 offset = arg0;
10092 else
10093 return 0;
10095 else
10096 return 0;
10098 if (TREE_CODE (array) == STRING_CST)
10100 *ptr_offset = fold_convert (sizetype, offset);
10101 return array;
10103 else if (TREE_CODE (array) == VAR_DECL
10104 || TREE_CODE (array) == CONST_DECL)
10106 int length;
10108 /* Variables initialized to string literals can be handled too. */
10109 if (!const_value_known_p (array)
10110 || !DECL_INITIAL (array)
10111 || TREE_CODE (DECL_INITIAL (array)) != STRING_CST)
10112 return 0;
10114 /* Avoid const char foo[4] = "abcde"; */
10115 if (DECL_SIZE_UNIT (array) == NULL_TREE
10116 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10117 || (length = TREE_STRING_LENGTH (DECL_INITIAL (array))) <= 0
10118 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10119 return 0;
10121 /* If variable is bigger than the string literal, OFFSET must be constant
10122 and inside of the bounds of the string literal. */
10123 offset = fold_convert (sizetype, offset);
10124 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10125 && (! host_integerp (offset, 1)
10126 || compare_tree_int (offset, length) >= 0))
10127 return 0;
10129 *ptr_offset = offset;
10130 return DECL_INITIAL (array);
10133 return 0;
10136 /* Generate code to calculate OPS, and exploded expression
10137 using a store-flag instruction and return an rtx for the result.
10138 OPS reflects a comparison.
10140 If TARGET is nonzero, store the result there if convenient.
10142 Return zero if there is no suitable set-flag instruction
10143 available on this machine.
10145 Once expand_expr has been called on the arguments of the comparison,
10146 we are committed to doing the store flag, since it is not safe to
10147 re-evaluate the expression. We emit the store-flag insn by calling
10148 emit_store_flag, but only expand the arguments if we have a reason
10149 to believe that emit_store_flag will be successful. If we think that
10150 it will, but it isn't, we have to simulate the store-flag with a
10151 set/jump/set sequence. */
10153 static rtx
10154 do_store_flag (sepops ops, rtx target, enum machine_mode mode)
10156 enum rtx_code code;
10157 tree arg0, arg1, type;
10158 tree tem;
10159 enum machine_mode operand_mode;
10160 int unsignedp;
10161 rtx op0, op1;
10162 rtx subtarget = target;
10163 location_t loc = ops->location;
10165 arg0 = ops->op0;
10166 arg1 = ops->op1;
10168 /* Don't crash if the comparison was erroneous. */
10169 if (arg0 == error_mark_node || arg1 == error_mark_node)
10170 return const0_rtx;
10172 type = TREE_TYPE (arg0);
10173 operand_mode = TYPE_MODE (type);
10174 unsignedp = TYPE_UNSIGNED (type);
10176 /* We won't bother with BLKmode store-flag operations because it would mean
10177 passing a lot of information to emit_store_flag. */
10178 if (operand_mode == BLKmode)
10179 return 0;
10181 /* We won't bother with store-flag operations involving function pointers
10182 when function pointers must be canonicalized before comparisons. */
10183 #ifdef HAVE_canonicalize_funcptr_for_compare
10184 if (HAVE_canonicalize_funcptr_for_compare
10185 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10186 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10187 == FUNCTION_TYPE))
10188 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10189 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10190 == FUNCTION_TYPE))))
10191 return 0;
10192 #endif
10194 STRIP_NOPS (arg0);
10195 STRIP_NOPS (arg1);
10197 /* Get the rtx comparison code to use. We know that EXP is a comparison
10198 operation of some type. Some comparisons against 1 and -1 can be
10199 converted to comparisons with zero. Do so here so that the tests
10200 below will be aware that we have a comparison with zero. These
10201 tests will not catch constants in the first operand, but constants
10202 are rarely passed as the first operand. */
10204 switch (ops->code)
10206 case EQ_EXPR:
10207 code = EQ;
10208 break;
10209 case NE_EXPR:
10210 code = NE;
10211 break;
10212 case LT_EXPR:
10213 if (integer_onep (arg1))
10214 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10215 else
10216 code = unsignedp ? LTU : LT;
10217 break;
10218 case LE_EXPR:
10219 if (! unsignedp && integer_all_onesp (arg1))
10220 arg1 = integer_zero_node, code = LT;
10221 else
10222 code = unsignedp ? LEU : LE;
10223 break;
10224 case GT_EXPR:
10225 if (! unsignedp && integer_all_onesp (arg1))
10226 arg1 = integer_zero_node, code = GE;
10227 else
10228 code = unsignedp ? GTU : GT;
10229 break;
10230 case GE_EXPR:
10231 if (integer_onep (arg1))
10232 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
10233 else
10234 code = unsignedp ? GEU : GE;
10235 break;
10237 case UNORDERED_EXPR:
10238 code = UNORDERED;
10239 break;
10240 case ORDERED_EXPR:
10241 code = ORDERED;
10242 break;
10243 case UNLT_EXPR:
10244 code = UNLT;
10245 break;
10246 case UNLE_EXPR:
10247 code = UNLE;
10248 break;
10249 case UNGT_EXPR:
10250 code = UNGT;
10251 break;
10252 case UNGE_EXPR:
10253 code = UNGE;
10254 break;
10255 case UNEQ_EXPR:
10256 code = UNEQ;
10257 break;
10258 case LTGT_EXPR:
10259 code = LTGT;
10260 break;
10262 default:
10263 gcc_unreachable ();
10266 /* Put a constant second. */
10267 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
10268 || TREE_CODE (arg0) == FIXED_CST)
10270 tem = arg0; arg0 = arg1; arg1 = tem;
10271 code = swap_condition (code);
10274 /* If this is an equality or inequality test of a single bit, we can
10275 do this by shifting the bit being tested to the low-order bit and
10276 masking the result with the constant 1. If the condition was EQ,
10277 we xor it with 1. This does not require an scc insn and is faster
10278 than an scc insn even if we have it.
10280 The code to make this transformation was moved into fold_single_bit_test,
10281 so we just call into the folder and expand its result. */
10283 if ((code == NE || code == EQ)
10284 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
10285 && integer_pow2p (TREE_OPERAND (arg0, 1))
10286 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
10288 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10289 return expand_expr (fold_single_bit_test (loc,
10290 code == NE ? NE_EXPR : EQ_EXPR,
10291 arg0, arg1, type),
10292 target, VOIDmode, EXPAND_NORMAL);
10295 if (! get_subtarget (target)
10296 || GET_MODE (subtarget) != operand_mode)
10297 subtarget = 0;
10299 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
10301 if (target == 0)
10302 target = gen_reg_rtx (mode);
10304 /* Try a cstore if possible. */
10305 return emit_store_flag_force (target, code, op0, op1,
10306 operand_mode, unsignedp,
10307 (TYPE_PRECISION (ops->type) == 1
10308 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
10312 /* Stubs in case we haven't got a casesi insn. */
10313 #ifndef HAVE_casesi
10314 # define HAVE_casesi 0
10315 # define gen_casesi(a, b, c, d, e) (0)
10316 # define CODE_FOR_casesi CODE_FOR_nothing
10317 #endif
10319 /* Attempt to generate a casesi instruction. Returns 1 if successful,
10320 0 otherwise (i.e. if there is no casesi instruction). */
10322 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
10323 rtx table_label ATTRIBUTE_UNUSED, rtx default_label,
10324 rtx fallback_label ATTRIBUTE_UNUSED)
10326 enum machine_mode index_mode = SImode;
10327 int index_bits = GET_MODE_BITSIZE (index_mode);
10328 rtx op1, op2, index;
10329 enum machine_mode op_mode;
10331 if (! HAVE_casesi)
10332 return 0;
10334 /* Convert the index to SImode. */
10335 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
10337 enum machine_mode omode = TYPE_MODE (index_type);
10338 rtx rangertx = expand_normal (range);
10340 /* We must handle the endpoints in the original mode. */
10341 index_expr = build2 (MINUS_EXPR, index_type,
10342 index_expr, minval);
10343 minval = integer_zero_node;
10344 index = expand_normal (index_expr);
10345 if (default_label)
10346 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
10347 omode, 1, default_label);
10348 /* Now we can safely truncate. */
10349 index = convert_to_mode (index_mode, index, 0);
10351 else
10353 if (TYPE_MODE (index_type) != index_mode)
10355 index_type = lang_hooks.types.type_for_size (index_bits, 0);
10356 index_expr = fold_convert (index_type, index_expr);
10359 index = expand_normal (index_expr);
10362 do_pending_stack_adjust ();
10364 op_mode = insn_data[(int) CODE_FOR_casesi].operand[0].mode;
10365 if (! (*insn_data[(int) CODE_FOR_casesi].operand[0].predicate)
10366 (index, op_mode))
10367 index = copy_to_mode_reg (op_mode, index);
10369 op1 = expand_normal (minval);
10371 op_mode = insn_data[(int) CODE_FOR_casesi].operand[1].mode;
10372 op1 = convert_modes (op_mode, TYPE_MODE (TREE_TYPE (minval)),
10373 op1, TYPE_UNSIGNED (TREE_TYPE (minval)));
10374 if (! (*insn_data[(int) CODE_FOR_casesi].operand[1].predicate)
10375 (op1, op_mode))
10376 op1 = copy_to_mode_reg (op_mode, op1);
10378 op2 = expand_normal (range);
10380 op_mode = insn_data[(int) CODE_FOR_casesi].operand[2].mode;
10381 op2 = convert_modes (op_mode, TYPE_MODE (TREE_TYPE (range)),
10382 op2, TYPE_UNSIGNED (TREE_TYPE (range)));
10383 if (! (*insn_data[(int) CODE_FOR_casesi].operand[2].predicate)
10384 (op2, op_mode))
10385 op2 = copy_to_mode_reg (op_mode, op2);
10387 emit_jump_insn (gen_casesi (index, op1, op2,
10388 table_label, !default_label
10389 ? fallback_label : default_label));
10390 return 1;
10393 /* Attempt to generate a tablejump instruction; same concept. */
10394 #ifndef HAVE_tablejump
10395 #define HAVE_tablejump 0
10396 #define gen_tablejump(x, y) (0)
10397 #endif
10399 /* Subroutine of the next function.
10401 INDEX is the value being switched on, with the lowest value
10402 in the table already subtracted.
10403 MODE is its expected mode (needed if INDEX is constant).
10404 RANGE is the length of the jump table.
10405 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
10407 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
10408 index value is out of range. */
10410 static void
10411 do_tablejump (rtx index, enum machine_mode mode, rtx range, rtx table_label,
10412 rtx default_label, int default_probability)
10414 rtx temp, vector;
10416 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
10417 cfun->cfg->max_jumptable_ents = INTVAL (range);
10419 /* Do an unsigned comparison (in the proper mode) between the index
10420 expression and the value which represents the length of the range.
10421 Since we just finished subtracting the lower bound of the range
10422 from the index expression, this comparison allows us to simultaneously
10423 check that the original index expression value is both greater than
10424 or equal to the minimum value of the range and less than or equal to
10425 the maximum value of the range. */
10427 if (default_label)
10429 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
10430 default_label);
10431 if (default_probability != -1)
10433 rtx jump_insn = get_last_insn();
10434 add_reg_note (jump_insn, REG_BR_PROB, GEN_INT (default_probability));
10439 /* If index is in range, it must fit in Pmode.
10440 Convert to Pmode so we can index with it. */
10441 if (mode != Pmode)
10442 index = convert_to_mode (Pmode, index, 1);
10444 /* Don't let a MEM slip through, because then INDEX that comes
10445 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
10446 and break_out_memory_refs will go to work on it and mess it up. */
10447 #ifdef PIC_CASE_VECTOR_ADDRESS
10448 if (flag_pic && !REG_P (index))
10449 index = copy_to_mode_reg (Pmode, index);
10450 #endif
10452 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
10453 GET_MODE_SIZE, because this indicates how large insns are. The other
10454 uses should all be Pmode, because they are addresses. This code
10455 could fail if addresses and insns are not the same size. */
10456 index = gen_rtx_PLUS (Pmode,
10457 gen_rtx_MULT (Pmode, index,
10458 GEN_INT (GET_MODE_SIZE (CASE_VECTOR_MODE))),
10459 gen_rtx_LABEL_REF (Pmode, table_label));
10460 #ifdef PIC_CASE_VECTOR_ADDRESS
10461 if (flag_pic)
10462 index = PIC_CASE_VECTOR_ADDRESS (index);
10463 else
10464 #endif
10465 index = memory_address (CASE_VECTOR_MODE, index);
10466 temp = gen_reg_rtx (CASE_VECTOR_MODE);
10467 vector = gen_const_mem (CASE_VECTOR_MODE, index);
10468 convert_move (temp, vector, 0);
10470 emit_jump_insn (gen_tablejump (temp, table_label));
10472 /* If we are generating PIC code or if the table is PC-relative, the
10473 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
10474 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
10475 emit_barrier ();
10479 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
10480 rtx table_label, rtx default_label, int default_probability)
10482 rtx index;
10484 if (! HAVE_tablejump)
10485 return 0;
10487 index_expr = fold_build2 (MINUS_EXPR, index_type,
10488 fold_convert (index_type, index_expr),
10489 fold_convert (index_type, minval));
10490 index = expand_normal (index_expr);
10491 do_pending_stack_adjust ();
10493 do_tablejump (index, TYPE_MODE (index_type),
10494 convert_modes (TYPE_MODE (index_type),
10495 TYPE_MODE (TREE_TYPE (range)),
10496 expand_normal (range),
10497 TYPE_UNSIGNED (TREE_TYPE (range))),
10498 table_label, default_label, default_probability);
10499 return 1;
10502 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
10503 static rtx
10504 const_vector_from_tree (tree exp)
10506 rtvec v;
10507 int units, i;
10508 tree link, elt;
10509 enum machine_mode inner, mode;
10511 mode = TYPE_MODE (TREE_TYPE (exp));
10513 if (initializer_zerop (exp))
10514 return CONST0_RTX (mode);
10516 units = GET_MODE_NUNITS (mode);
10517 inner = GET_MODE_INNER (mode);
10519 v = rtvec_alloc (units);
10521 link = TREE_VECTOR_CST_ELTS (exp);
10522 for (i = 0; link; link = TREE_CHAIN (link), ++i)
10524 elt = TREE_VALUE (link);
10526 if (TREE_CODE (elt) == REAL_CST)
10527 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
10528 inner);
10529 else if (TREE_CODE (elt) == FIXED_CST)
10530 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
10531 inner);
10532 else
10533 RTVEC_ELT (v, i) = immed_double_int_const (tree_to_double_int (elt),
10534 inner);
10537 /* Initialize remaining elements to 0. */
10538 for (; i < units; ++i)
10539 RTVEC_ELT (v, i) = CONST0_RTX (inner);
10541 return gen_rtx_CONST_VECTOR (mode, v);
10544 /* Build a decl for a personality function given a language prefix. */
10546 tree
10547 build_personality_function (const char *lang)
10549 const char *unwind_and_version;
10550 tree decl, type;
10551 char *name;
10553 switch (targetm.except_unwind_info (&global_options))
10555 case UI_NONE:
10556 return NULL;
10557 case UI_SJLJ:
10558 unwind_and_version = "_sj0";
10559 break;
10560 case UI_DWARF2:
10561 case UI_TARGET:
10562 unwind_and_version = "_v0";
10563 break;
10564 default:
10565 gcc_unreachable ();
10568 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
10570 type = build_function_type_list (integer_type_node, integer_type_node,
10571 long_long_unsigned_type_node,
10572 ptr_type_node, ptr_type_node, NULL_TREE);
10573 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
10574 get_identifier (name), type);
10575 DECL_ARTIFICIAL (decl) = 1;
10576 DECL_EXTERNAL (decl) = 1;
10577 TREE_PUBLIC (decl) = 1;
10579 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
10580 are the flags assigned by targetm.encode_section_info. */
10581 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
10583 return decl;
10586 /* Extracts the personality function of DECL and returns the corresponding
10587 libfunc. */
10590 get_personality_function (tree decl)
10592 tree personality = DECL_FUNCTION_PERSONALITY (decl);
10593 enum eh_personality_kind pk;
10595 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
10596 if (pk == eh_personality_none)
10597 return NULL;
10599 if (!personality
10600 && pk == eh_personality_any)
10601 personality = lang_hooks.eh_personality ();
10603 if (pk == eh_personality_lang)
10604 gcc_assert (personality != NULL_TREE);
10606 return XEXP (DECL_RTL (personality), 0);
10609 #include "gt-expr.h"