t-linux64: Delete the 32-bit multilib that uses software floating point emulation.
[official-gcc.git] / gcc / expr.c
blobe6def73e6e4fa2ccd966feb328a040a4842bcd1d
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 2012 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 "common/common-target.h"
53 #include "timevar.h"
54 #include "df.h"
55 #include "diagnostic.h"
56 #include "ssaexpand.h"
57 #include "target-globals.h"
58 #include "params.h"
60 /* Decide whether a function's arguments should be processed
61 from first to last or from last to first.
63 They should if the stack and args grow in opposite directions, but
64 only if we have push insns. */
66 #ifdef PUSH_ROUNDING
68 #ifndef PUSH_ARGS_REVERSED
69 #if defined (STACK_GROWS_DOWNWARD) != defined (ARGS_GROW_DOWNWARD)
70 #define PUSH_ARGS_REVERSED /* If it's last to first. */
71 #endif
72 #endif
74 #endif
76 #ifndef STACK_PUSH_CODE
77 #ifdef STACK_GROWS_DOWNWARD
78 #define STACK_PUSH_CODE PRE_DEC
79 #else
80 #define STACK_PUSH_CODE PRE_INC
81 #endif
82 #endif
85 /* If this is nonzero, we do not bother generating VOLATILE
86 around volatile memory references, and we are willing to
87 output indirect addresses. If cse is to follow, we reject
88 indirect addresses so a useful potential cse is generated;
89 if it is used only once, instruction combination will produce
90 the same indirect address eventually. */
91 int cse_not_expected;
93 /* This structure is used by move_by_pieces to describe the move to
94 be performed. */
95 struct move_by_pieces_d
97 rtx to;
98 rtx to_addr;
99 int autinc_to;
100 int explicit_inc_to;
101 rtx from;
102 rtx from_addr;
103 int autinc_from;
104 int explicit_inc_from;
105 unsigned HOST_WIDE_INT len;
106 HOST_WIDE_INT offset;
107 int reverse;
110 /* This structure is used by store_by_pieces to describe the clear to
111 be performed. */
113 struct store_by_pieces_d
115 rtx to;
116 rtx to_addr;
117 int autinc_to;
118 int explicit_inc_to;
119 unsigned HOST_WIDE_INT len;
120 HOST_WIDE_INT offset;
121 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode);
122 void *constfundata;
123 int reverse;
126 static void move_by_pieces_1 (rtx (*) (rtx, ...), enum machine_mode,
127 struct move_by_pieces_d *);
128 static bool block_move_libcall_safe_for_call_parm (void);
129 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT);
130 static tree emit_block_move_libcall_fn (int);
131 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
132 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, enum machine_mode);
133 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
134 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
135 static void store_by_pieces_2 (rtx (*) (rtx, ...), enum machine_mode,
136 struct store_by_pieces_d *);
137 static tree clear_storage_libcall_fn (int);
138 static rtx compress_float_constant (rtx, rtx);
139 static rtx get_subtarget (rtx);
140 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
141 HOST_WIDE_INT, enum machine_mode,
142 tree, tree, int, alias_set_type);
143 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
144 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
145 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
146 enum machine_mode,
147 tree, tree, alias_set_type, bool);
149 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
151 static int is_aligning_offset (const_tree, const_tree);
152 static void expand_operands (tree, tree, rtx, rtx*, rtx*,
153 enum expand_modifier);
154 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
155 static rtx do_store_flag (sepops, rtx, enum machine_mode);
156 #ifdef PUSH_ROUNDING
157 static void emit_single_push_insn (enum machine_mode, rtx, tree);
158 #endif
159 static void do_tablejump (rtx, enum machine_mode, rtx, rtx, rtx);
160 static rtx const_vector_from_tree (tree);
161 static void write_complex_part (rtx, rtx, bool);
163 /* This macro is used to determine whether move_by_pieces should be called
164 to perform a structure copy. */
165 #ifndef MOVE_BY_PIECES_P
166 #define MOVE_BY_PIECES_P(SIZE, ALIGN) \
167 (move_by_pieces_ninsns (SIZE, ALIGN, MOVE_MAX_PIECES + 1) \
168 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
169 #endif
171 /* This macro is used to determine whether clear_by_pieces should be
172 called to clear storage. */
173 #ifndef CLEAR_BY_PIECES_P
174 #define CLEAR_BY_PIECES_P(SIZE, ALIGN) \
175 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
176 < (unsigned int) CLEAR_RATIO (optimize_insn_for_speed_p ()))
177 #endif
179 /* This macro is used to determine whether store_by_pieces should be
180 called to "memset" storage with byte values other than zero. */
181 #ifndef SET_BY_PIECES_P
182 #define SET_BY_PIECES_P(SIZE, ALIGN) \
183 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
184 < (unsigned int) SET_RATIO (optimize_insn_for_speed_p ()))
185 #endif
187 /* This macro is used to determine whether store_by_pieces should be
188 called to "memcpy" storage when the source is a constant string. */
189 #ifndef STORE_BY_PIECES_P
190 #define STORE_BY_PIECES_P(SIZE, ALIGN) \
191 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
192 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
193 #endif
195 /* SLOW_UNALIGNED_ACCESS is nonzero if unaligned accesses are very slow. */
197 #ifndef SLOW_UNALIGNED_ACCESS
198 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
199 #endif
201 /* This is run to set up which modes can be used
202 directly in memory and to initialize the block move optab. It is run
203 at the beginning of compilation and when the target is reinitialized. */
205 void
206 init_expr_target (void)
208 rtx insn, pat;
209 enum machine_mode mode;
210 int num_clobbers;
211 rtx mem, mem1;
212 rtx reg;
214 /* Try indexing by frame ptr and try by stack ptr.
215 It is known that on the Convex the stack ptr isn't a valid index.
216 With luck, one or the other is valid on any machine. */
217 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
218 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
220 /* A scratch register we can modify in-place below to avoid
221 useless RTL allocations. */
222 reg = gen_rtx_REG (VOIDmode, -1);
224 insn = rtx_alloc (INSN);
225 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
226 PATTERN (insn) = pat;
228 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
229 mode = (enum machine_mode) ((int) mode + 1))
231 int regno;
233 direct_load[(int) mode] = direct_store[(int) mode] = 0;
234 PUT_MODE (mem, mode);
235 PUT_MODE (mem1, mode);
236 PUT_MODE (reg, mode);
238 /* See if there is some register that can be used in this mode and
239 directly loaded or stored from memory. */
241 if (mode != VOIDmode && mode != BLKmode)
242 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
243 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
244 regno++)
246 if (! HARD_REGNO_MODE_OK (regno, mode))
247 continue;
249 SET_REGNO (reg, regno);
251 SET_SRC (pat) = mem;
252 SET_DEST (pat) = reg;
253 if (recog (pat, insn, &num_clobbers) >= 0)
254 direct_load[(int) mode] = 1;
256 SET_SRC (pat) = mem1;
257 SET_DEST (pat) = reg;
258 if (recog (pat, insn, &num_clobbers) >= 0)
259 direct_load[(int) mode] = 1;
261 SET_SRC (pat) = reg;
262 SET_DEST (pat) = mem;
263 if (recog (pat, insn, &num_clobbers) >= 0)
264 direct_store[(int) mode] = 1;
266 SET_SRC (pat) = reg;
267 SET_DEST (pat) = mem1;
268 if (recog (pat, insn, &num_clobbers) >= 0)
269 direct_store[(int) mode] = 1;
273 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
275 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
276 mode = GET_MODE_WIDER_MODE (mode))
278 enum machine_mode srcmode;
279 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
280 srcmode = GET_MODE_WIDER_MODE (srcmode))
282 enum insn_code ic;
284 ic = can_extend_p (mode, srcmode, 0);
285 if (ic == CODE_FOR_nothing)
286 continue;
288 PUT_MODE (mem, srcmode);
290 if (insn_operand_matches (ic, 1, mem))
291 float_extend_from_mem[mode][srcmode] = true;
296 /* This is run at the start of compiling a function. */
298 void
299 init_expr (void)
301 memset (&crtl->expr, 0, sizeof (crtl->expr));
304 /* Copy data from FROM to TO, where the machine modes are not the same.
305 Both modes may be integer, or both may be floating, or both may be
306 fixed-point.
307 UNSIGNEDP should be nonzero if FROM is an unsigned type.
308 This causes zero-extension instead of sign-extension. */
310 void
311 convert_move (rtx to, rtx from, int unsignedp)
313 enum machine_mode to_mode = GET_MODE (to);
314 enum machine_mode from_mode = GET_MODE (from);
315 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
316 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
317 enum insn_code code;
318 rtx libcall;
320 /* rtx code for making an equivalent value. */
321 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
322 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
325 gcc_assert (to_real == from_real);
326 gcc_assert (to_mode != BLKmode);
327 gcc_assert (from_mode != BLKmode);
329 /* If the source and destination are already the same, then there's
330 nothing to do. */
331 if (to == from)
332 return;
334 /* If FROM is a SUBREG that indicates that we have already done at least
335 the required extension, strip it. We don't handle such SUBREGs as
336 TO here. */
338 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
339 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
340 >= GET_MODE_PRECISION (to_mode))
341 && SUBREG_PROMOTED_UNSIGNED_P (from) == unsignedp)
342 from = gen_lowpart (to_mode, from), from_mode = to_mode;
344 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
346 if (to_mode == from_mode
347 || (from_mode == VOIDmode && CONSTANT_P (from)))
349 emit_move_insn (to, from);
350 return;
353 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
355 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
357 if (VECTOR_MODE_P (to_mode))
358 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
359 else
360 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
362 emit_move_insn (to, from);
363 return;
366 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
368 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
369 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
370 return;
373 if (to_real)
375 rtx value, insns;
376 convert_optab tab;
378 gcc_assert ((GET_MODE_PRECISION (from_mode)
379 != GET_MODE_PRECISION (to_mode))
380 || (DECIMAL_FLOAT_MODE_P (from_mode)
381 != DECIMAL_FLOAT_MODE_P (to_mode)));
383 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
384 /* Conversion between decimal float and binary float, same size. */
385 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
386 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
387 tab = sext_optab;
388 else
389 tab = trunc_optab;
391 /* Try converting directly if the insn is supported. */
393 code = convert_optab_handler (tab, to_mode, from_mode);
394 if (code != CODE_FOR_nothing)
396 emit_unop_insn (code, to, from,
397 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
398 return;
401 /* Otherwise use a libcall. */
402 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
404 /* Is this conversion implemented yet? */
405 gcc_assert (libcall);
407 start_sequence ();
408 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
409 1, from, from_mode);
410 insns = get_insns ();
411 end_sequence ();
412 emit_libcall_block (insns, to, value,
413 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
414 from)
415 : gen_rtx_FLOAT_EXTEND (to_mode, from));
416 return;
419 /* Handle pointer conversion. */ /* SPEE 900220. */
420 /* Targets are expected to provide conversion insns between PxImode and
421 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
422 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
424 enum machine_mode full_mode
425 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
427 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
428 != CODE_FOR_nothing);
430 if (full_mode != from_mode)
431 from = convert_to_mode (full_mode, from, unsignedp);
432 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
433 to, from, UNKNOWN);
434 return;
436 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
438 rtx new_from;
439 enum machine_mode full_mode
440 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
441 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
442 enum insn_code icode;
444 icode = convert_optab_handler (ctab, full_mode, from_mode);
445 gcc_assert (icode != CODE_FOR_nothing);
447 if (to_mode == full_mode)
449 emit_unop_insn (icode, to, from, UNKNOWN);
450 return;
453 new_from = gen_reg_rtx (full_mode);
454 emit_unop_insn (icode, new_from, from, UNKNOWN);
456 /* else proceed to integer conversions below. */
457 from_mode = full_mode;
458 from = new_from;
461 /* Make sure both are fixed-point modes or both are not. */
462 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
463 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
464 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
466 /* If we widen from_mode to to_mode and they are in the same class,
467 we won't saturate the result.
468 Otherwise, always saturate the result to play safe. */
469 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
470 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
471 expand_fixed_convert (to, from, 0, 0);
472 else
473 expand_fixed_convert (to, from, 0, 1);
474 return;
477 /* Now both modes are integers. */
479 /* Handle expanding beyond a word. */
480 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
481 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
483 rtx 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_PRECISION (from_mode) < BITS_PER_WORD
506 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
507 != CODE_FOR_nothing))
509 rtx word_to = gen_reg_rtx (word_mode);
510 if (REG_P (to))
512 if (reg_overlap_mentioned_p (to, from))
513 from = force_reg (from_mode, from);
514 emit_clobber (to);
516 convert_move (word_to, from, unsignedp);
517 emit_unop_insn (code, to, word_to, equiv_code);
518 return;
521 /* No special multiword conversion insn; do it by hand. */
522 start_sequence ();
524 /* Since we will turn this into a no conflict block, we must ensure
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_PRECISION (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_PRECISION (from_mode) > BITS_PER_WORD
570 && GET_MODE_PRECISION (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_MODES_P (to_mode, from_mode))
590 if (!((MEM_P (from)
591 && ! MEM_VOLATILE_P (from)
592 && direct_load[(int) to_mode]
593 && ! mode_dependent_address_p (XEXP (from, 0)))
594 || REG_P (from)
595 || GET_CODE (from) == SUBREG))
596 from = force_reg (from_mode, from);
597 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
598 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
599 from = copy_to_reg (from);
600 emit_move_insn (to, gen_lowpart (to_mode, from));
601 return;
604 /* Handle extension. */
605 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
607 /* Convert directly if that works. */
608 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
609 != CODE_FOR_nothing)
611 emit_unop_insn (code, to, from, equiv_code);
612 return;
614 else
616 enum machine_mode intermediate;
617 rtx tmp;
618 int shift_amount;
620 /* Search for a mode to convert via. */
621 for (intermediate = from_mode; intermediate != VOIDmode;
622 intermediate = GET_MODE_WIDER_MODE (intermediate))
623 if (((can_extend_p (to_mode, intermediate, unsignedp)
624 != CODE_FOR_nothing)
625 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
626 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
627 && (can_extend_p (intermediate, from_mode, unsignedp)
628 != CODE_FOR_nothing))
630 convert_move (to, convert_to_mode (intermediate, from,
631 unsignedp), unsignedp);
632 return;
635 /* No suitable intermediate mode.
636 Generate what we need with shifts. */
637 shift_amount = (GET_MODE_PRECISION (to_mode)
638 - GET_MODE_PRECISION (from_mode));
639 from = gen_lowpart (to_mode, force_reg (from_mode, from));
640 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
641 to, unsignedp);
642 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
643 to, unsignedp);
644 if (tmp != to)
645 emit_move_insn (to, tmp);
646 return;
650 /* Support special truncate insns for certain modes. */
651 if (convert_optab_handler (trunc_optab, to_mode,
652 from_mode) != CODE_FOR_nothing)
654 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
655 to, from, UNKNOWN);
656 return;
659 /* Handle truncation of volatile memrefs, and so on;
660 the things that couldn't be truncated directly,
661 and for which there was no special instruction.
663 ??? Code above formerly short-circuited this, for most integer
664 mode pairs, with a force_reg in from_mode followed by a recursive
665 call to this routine. Appears always to have been wrong. */
666 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
668 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
669 emit_move_insn (to, temp);
670 return;
673 /* Mode combination is not recognized. */
674 gcc_unreachable ();
677 /* Return an rtx for a value that would result
678 from converting X to mode MODE.
679 Both X and MODE may be floating, or both integer.
680 UNSIGNEDP is nonzero if X is an unsigned value.
681 This can be done by referring to a part of X in place
682 or by copying to a new temporary with conversion. */
685 convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
687 return convert_modes (mode, VOIDmode, x, unsignedp);
690 /* Return an rtx for a value that would result
691 from converting X from mode OLDMODE to mode MODE.
692 Both modes may be floating, or both integer.
693 UNSIGNEDP is nonzero if X is an unsigned value.
695 This can be done by referring to a part of X in place
696 or by copying to a new temporary with conversion.
698 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
701 convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
703 rtx temp;
705 /* If FROM is a SUBREG that indicates that we have already done at least
706 the required extension, strip it. */
708 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
709 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
710 && SUBREG_PROMOTED_UNSIGNED_P (x) == unsignedp)
711 x = gen_lowpart (mode, x);
713 if (GET_MODE (x) != VOIDmode)
714 oldmode = GET_MODE (x);
716 if (mode == oldmode)
717 return x;
719 /* There is one case that we must handle specially: If we are converting
720 a CONST_INT into a mode whose size is twice HOST_BITS_PER_WIDE_INT and
721 we are to interpret the constant as unsigned, gen_lowpart will do
722 the wrong if the constant appears negative. What we want to do is
723 make the high-order word of the constant zero, not all ones. */
725 if (unsignedp && GET_MODE_CLASS (mode) == MODE_INT
726 && GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT
727 && CONST_INT_P (x) && INTVAL (x) < 0)
729 double_int val = uhwi_to_double_int (INTVAL (x));
731 /* We need to zero extend VAL. */
732 if (oldmode != VOIDmode)
733 val = double_int_zext (val, GET_MODE_BITSIZE (oldmode));
735 return immed_double_int_const (val, mode);
738 /* We can do this with a gen_lowpart if both desired and current modes
739 are integer, and this is either a constant integer, a register, or a
740 non-volatile MEM. Except for the constant case where MODE is no
741 wider than HOST_BITS_PER_WIDE_INT, we must be narrowing the operand. */
743 if ((CONST_INT_P (x)
744 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT)
745 || (GET_MODE_CLASS (mode) == MODE_INT
746 && GET_MODE_CLASS (oldmode) == MODE_INT
747 && (GET_CODE (x) == CONST_DOUBLE
748 || (GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
749 && ((MEM_P (x) && ! MEM_VOLATILE_P (x)
750 && direct_load[(int) mode])
751 || (REG_P (x)
752 && (! HARD_REGISTER_P (x)
753 || HARD_REGNO_MODE_OK (REGNO (x), mode))
754 && TRULY_NOOP_TRUNCATION_MODES_P (mode,
755 GET_MODE (x))))))))
757 /* ?? If we don't know OLDMODE, we have to assume here that
758 X does not need sign- or zero-extension. This may not be
759 the case, but it's the best we can do. */
760 if (CONST_INT_P (x) && oldmode != VOIDmode
761 && GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (oldmode))
763 HOST_WIDE_INT val = INTVAL (x);
765 /* We must sign or zero-extend in this case. Start by
766 zero-extending, then sign extend if we need to. */
767 val &= GET_MODE_MASK (oldmode);
768 if (! unsignedp
769 && val_signbit_known_set_p (oldmode, val))
770 val |= ~GET_MODE_MASK (oldmode);
772 return gen_int_mode (val, mode);
775 return gen_lowpart (mode, x);
778 /* Converting from integer constant into mode is always equivalent to an
779 subreg operation. */
780 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
782 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
783 return simplify_gen_subreg (mode, x, oldmode, 0);
786 temp = gen_reg_rtx (mode);
787 convert_move (temp, x, unsignedp);
788 return temp;
791 /* Return the largest alignment we can use for doing a move (or store)
792 of MAX_PIECES. ALIGN is the largest alignment we could use. */
794 static unsigned int
795 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
797 enum machine_mode tmode;
799 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
800 if (align >= GET_MODE_ALIGNMENT (tmode))
801 align = GET_MODE_ALIGNMENT (tmode);
802 else
804 enum machine_mode tmode, xmode;
806 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
807 tmode != VOIDmode;
808 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
809 if (GET_MODE_SIZE (tmode) > max_pieces
810 || SLOW_UNALIGNED_ACCESS (tmode, align))
811 break;
813 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
816 return align;
819 /* Return the widest integer mode no wider than SIZE. If no such mode
820 can be found, return VOIDmode. */
822 static enum machine_mode
823 widest_int_mode_for_size (unsigned int size)
825 enum machine_mode tmode, mode = VOIDmode;
827 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
828 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
829 if (GET_MODE_SIZE (tmode) < size)
830 mode = tmode;
832 return mode;
835 /* STORE_MAX_PIECES is the number of bytes at a time that we can
836 store efficiently. Due to internal GCC limitations, this is
837 MOVE_MAX_PIECES limited by the number of bytes GCC can represent
838 for an immediate constant. */
840 #define STORE_MAX_PIECES MIN (MOVE_MAX_PIECES, 2 * sizeof (HOST_WIDE_INT))
842 /* Determine whether the LEN bytes can be moved by using several move
843 instructions. Return nonzero if a call to move_by_pieces should
844 succeed. */
847 can_move_by_pieces (unsigned HOST_WIDE_INT len,
848 unsigned int align ATTRIBUTE_UNUSED)
850 return MOVE_BY_PIECES_P (len, align);
853 /* Generate several move instructions to copy LEN bytes from block FROM to
854 block TO. (These are MEM rtx's with BLKmode).
856 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
857 used to push FROM to the stack.
859 ALIGN is maximum stack alignment we can assume.
861 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
862 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
863 stpcpy. */
866 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
867 unsigned int align, int endp)
869 struct move_by_pieces_d data;
870 enum machine_mode to_addr_mode;
871 enum machine_mode from_addr_mode = get_address_mode (from);
872 rtx to_addr, from_addr = XEXP (from, 0);
873 unsigned int max_size = MOVE_MAX_PIECES + 1;
874 enum insn_code icode;
876 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
878 data.offset = 0;
879 data.from_addr = from_addr;
880 if (to)
882 to_addr_mode = get_address_mode (to);
883 to_addr = XEXP (to, 0);
884 data.to = to;
885 data.autinc_to
886 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
887 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
888 data.reverse
889 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
891 else
893 to_addr_mode = VOIDmode;
894 to_addr = NULL_RTX;
895 data.to = NULL_RTX;
896 data.autinc_to = 1;
897 #ifdef STACK_GROWS_DOWNWARD
898 data.reverse = 1;
899 #else
900 data.reverse = 0;
901 #endif
903 data.to_addr = to_addr;
904 data.from = from;
905 data.autinc_from
906 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
907 || GET_CODE (from_addr) == POST_INC
908 || GET_CODE (from_addr) == POST_DEC);
910 data.explicit_inc_from = 0;
911 data.explicit_inc_to = 0;
912 if (data.reverse) data.offset = len;
913 data.len = len;
915 /* If copying requires more than two move insns,
916 copy addresses to registers (to make displacements shorter)
917 and use post-increment if available. */
918 if (!(data.autinc_from && data.autinc_to)
919 && move_by_pieces_ninsns (len, align, max_size) > 2)
921 /* Find the mode of the largest move...
922 MODE might not be used depending on the definitions of the
923 USE_* macros below. */
924 enum machine_mode mode ATTRIBUTE_UNUSED
925 = widest_int_mode_for_size (max_size);
927 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
929 data.from_addr = copy_to_mode_reg (from_addr_mode,
930 plus_constant (from_addr_mode,
931 from_addr, len));
932 data.autinc_from = 1;
933 data.explicit_inc_from = -1;
935 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
937 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
938 data.autinc_from = 1;
939 data.explicit_inc_from = 1;
941 if (!data.autinc_from && CONSTANT_P (from_addr))
942 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
943 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
945 data.to_addr = copy_to_mode_reg (to_addr_mode,
946 plus_constant (to_addr_mode,
947 to_addr, len));
948 data.autinc_to = 1;
949 data.explicit_inc_to = -1;
951 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
953 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
954 data.autinc_to = 1;
955 data.explicit_inc_to = 1;
957 if (!data.autinc_to && CONSTANT_P (to_addr))
958 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
961 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
963 /* First move what we can in the largest integer mode, then go to
964 successively smaller modes. */
966 while (max_size > 1)
968 enum machine_mode mode = widest_int_mode_for_size (max_size);
970 if (mode == VOIDmode)
971 break;
973 icode = optab_handler (mov_optab, mode);
974 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
975 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
977 max_size = GET_MODE_SIZE (mode);
980 /* The code above should have handled everything. */
981 gcc_assert (!data.len);
983 if (endp)
985 rtx to1;
987 gcc_assert (!data.reverse);
988 if (data.autinc_to)
990 if (endp == 2)
992 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
993 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
994 else
995 data.to_addr = copy_to_mode_reg (to_addr_mode,
996 plus_constant (to_addr_mode,
997 data.to_addr,
998 -1));
1000 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
1001 data.offset);
1003 else
1005 if (endp == 2)
1006 --data.offset;
1007 to1 = adjust_address (data.to, QImode, data.offset);
1009 return to1;
1011 else
1012 return data.to;
1015 /* Return number of insns required to move L bytes by pieces.
1016 ALIGN (in bits) is maximum alignment we can assume. */
1018 unsigned HOST_WIDE_INT
1019 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1020 unsigned int max_size)
1022 unsigned HOST_WIDE_INT n_insns = 0;
1024 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1026 while (max_size > 1)
1028 enum machine_mode mode;
1029 enum insn_code icode;
1031 mode = widest_int_mode_for_size (max_size);
1033 if (mode == VOIDmode)
1034 break;
1036 icode = optab_handler (mov_optab, mode);
1037 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1038 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1040 max_size = GET_MODE_SIZE (mode);
1043 gcc_assert (!l);
1044 return n_insns;
1047 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1048 with move instructions for mode MODE. GENFUN is the gen_... function
1049 to make a move insn for that mode. DATA has all the other info. */
1051 static void
1052 move_by_pieces_1 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
1053 struct move_by_pieces_d *data)
1055 unsigned int size = GET_MODE_SIZE (mode);
1056 rtx to1 = NULL_RTX, from1;
1058 while (data->len >= size)
1060 if (data->reverse)
1061 data->offset -= size;
1063 if (data->to)
1065 if (data->autinc_to)
1066 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1067 data->offset);
1068 else
1069 to1 = adjust_address (data->to, mode, data->offset);
1072 if (data->autinc_from)
1073 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1074 data->offset);
1075 else
1076 from1 = adjust_address (data->from, mode, data->offset);
1078 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1079 emit_insn (gen_add2_insn (data->to_addr,
1080 GEN_INT (-(HOST_WIDE_INT)size)));
1081 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1082 emit_insn (gen_add2_insn (data->from_addr,
1083 GEN_INT (-(HOST_WIDE_INT)size)));
1085 if (data->to)
1086 emit_insn ((*genfun) (to1, from1));
1087 else
1089 #ifdef PUSH_ROUNDING
1090 emit_single_push_insn (mode, from1, NULL);
1091 #else
1092 gcc_unreachable ();
1093 #endif
1096 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1097 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
1098 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1099 emit_insn (gen_add2_insn (data->from_addr, GEN_INT (size)));
1101 if (! data->reverse)
1102 data->offset += size;
1104 data->len -= size;
1108 /* Emit code to move a block Y to a block X. This may be done with
1109 string-move instructions, with multiple scalar move instructions,
1110 or with a library call.
1112 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1113 SIZE is an rtx that says how long they are.
1114 ALIGN is the maximum alignment we can assume they have.
1115 METHOD describes what kind of copy this is, and what mechanisms may be used.
1117 Return the address of the new block, if memcpy is called and returns it,
1118 0 otherwise. */
1121 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1122 unsigned int expected_align, HOST_WIDE_INT expected_size)
1124 bool may_use_call;
1125 rtx retval = 0;
1126 unsigned int align;
1128 gcc_assert (size);
1129 if (CONST_INT_P (size)
1130 && INTVAL (size) == 0)
1131 return 0;
1133 switch (method)
1135 case BLOCK_OP_NORMAL:
1136 case BLOCK_OP_TAILCALL:
1137 may_use_call = true;
1138 break;
1140 case BLOCK_OP_CALL_PARM:
1141 may_use_call = block_move_libcall_safe_for_call_parm ();
1143 /* Make inhibit_defer_pop nonzero around the library call
1144 to force it to pop the arguments right away. */
1145 NO_DEFER_POP;
1146 break;
1148 case BLOCK_OP_NO_LIBCALL:
1149 may_use_call = false;
1150 break;
1152 default:
1153 gcc_unreachable ();
1156 gcc_assert (MEM_P (x) && MEM_P (y));
1157 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1158 gcc_assert (align >= BITS_PER_UNIT);
1160 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1161 block copy is more efficient for other large modes, e.g. DCmode. */
1162 x = adjust_address (x, BLKmode, 0);
1163 y = adjust_address (y, BLKmode, 0);
1165 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1166 can be incorrect is coming from __builtin_memcpy. */
1167 if (CONST_INT_P (size))
1169 x = shallow_copy_rtx (x);
1170 y = shallow_copy_rtx (y);
1171 set_mem_size (x, INTVAL (size));
1172 set_mem_size (y, INTVAL (size));
1175 if (CONST_INT_P (size) && MOVE_BY_PIECES_P (INTVAL (size), align))
1176 move_by_pieces (x, y, INTVAL (size), align, 0);
1177 else if (emit_block_move_via_movmem (x, y, size, align,
1178 expected_align, expected_size))
1180 else if (may_use_call
1181 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1182 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1184 /* Since x and y are passed to a libcall, mark the corresponding
1185 tree EXPR as addressable. */
1186 tree y_expr = MEM_EXPR (y);
1187 tree x_expr = MEM_EXPR (x);
1188 if (y_expr)
1189 mark_addressable (y_expr);
1190 if (x_expr)
1191 mark_addressable (x_expr);
1192 retval = emit_block_move_via_libcall (x, y, size,
1193 method == BLOCK_OP_TAILCALL);
1196 else
1197 emit_block_move_via_loop (x, y, size, align);
1199 if (method == BLOCK_OP_CALL_PARM)
1200 OK_DEFER_POP;
1202 return retval;
1206 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1208 return emit_block_move_hints (x, y, size, method, 0, -1);
1211 /* A subroutine of emit_block_move. Returns true if calling the
1212 block move libcall will not clobber any parameters which may have
1213 already been placed on the stack. */
1215 static bool
1216 block_move_libcall_safe_for_call_parm (void)
1218 #if defined (REG_PARM_STACK_SPACE)
1219 tree fn;
1220 #endif
1222 /* If arguments are pushed on the stack, then they're safe. */
1223 if (PUSH_ARGS)
1224 return true;
1226 /* If registers go on the stack anyway, any argument is sure to clobber
1227 an outgoing argument. */
1228 #if defined (REG_PARM_STACK_SPACE)
1229 fn = emit_block_move_libcall_fn (false);
1230 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1231 depend on its argument. */
1232 (void) fn;
1233 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1234 && REG_PARM_STACK_SPACE (fn) != 0)
1235 return false;
1236 #endif
1238 /* If any argument goes in memory, then it might clobber an outgoing
1239 argument. */
1241 CUMULATIVE_ARGS args_so_far_v;
1242 cumulative_args_t args_so_far;
1243 tree fn, arg;
1245 fn = emit_block_move_libcall_fn (false);
1246 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1247 args_so_far = pack_cumulative_args (&args_so_far_v);
1249 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1250 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1252 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1253 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1254 NULL_TREE, true);
1255 if (!tmp || !REG_P (tmp))
1256 return false;
1257 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1258 return false;
1259 targetm.calls.function_arg_advance (args_so_far, mode,
1260 NULL_TREE, true);
1263 return true;
1266 /* A subroutine of emit_block_move. Expand a movmem pattern;
1267 return true if successful. */
1269 static bool
1270 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1271 unsigned int expected_align, HOST_WIDE_INT expected_size)
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);
1291 if (code != CODE_FOR_nothing
1292 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1293 here because if SIZE is less than the mode mask, as it is
1294 returned by the macro, it will definitely be less than the
1295 actual mode mask. */
1296 && ((CONST_INT_P (size)
1297 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1298 <= (GET_MODE_MASK (mode) >> 1)))
1299 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
1301 struct expand_operand ops[6];
1302 unsigned int nops;
1304 /* ??? When called via emit_block_move_for_call, it'd be
1305 nice if there were some way to inform the backend, so
1306 that it doesn't fail the expansion because it thinks
1307 emitting the libcall would be more efficient. */
1308 nops = insn_data[(int) code].n_generator_args;
1309 gcc_assert (nops == 4 || nops == 6);
1311 create_fixed_operand (&ops[0], x);
1312 create_fixed_operand (&ops[1], y);
1313 /* The check above guarantees that this size conversion is valid. */
1314 create_convert_operand_to (&ops[2], size, mode, true);
1315 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1316 if (nops == 6)
1318 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1319 create_integer_operand (&ops[5], expected_size);
1321 if (maybe_expand_insn (code, nops, ops))
1323 volatile_ok = save_volatile_ok;
1324 return true;
1329 volatile_ok = save_volatile_ok;
1330 return false;
1333 /* A subroutine of emit_block_move. Expand a call to memcpy.
1334 Return the return value from memcpy, 0 otherwise. */
1337 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1339 rtx dst_addr, src_addr;
1340 tree call_expr, fn, src_tree, dst_tree, size_tree;
1341 enum machine_mode size_mode;
1342 rtx retval;
1344 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1345 pseudos. We can then place those new pseudos into a VAR_DECL and
1346 use them later. */
1348 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1349 src_addr = copy_addr_to_reg (XEXP (src, 0));
1351 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1352 src_addr = convert_memory_address (ptr_mode, src_addr);
1354 dst_tree = make_tree (ptr_type_node, dst_addr);
1355 src_tree = make_tree (ptr_type_node, src_addr);
1357 size_mode = TYPE_MODE (sizetype);
1359 size = convert_to_mode (size_mode, size, 1);
1360 size = copy_to_mode_reg (size_mode, size);
1362 /* It is incorrect to use the libcall calling conventions to call
1363 memcpy in this context. This could be a user call to memcpy and
1364 the user may wish to examine the return value from memcpy. For
1365 targets where libcalls and normal calls have different conventions
1366 for returning pointers, we could end up generating incorrect code. */
1368 size_tree = make_tree (sizetype, size);
1370 fn = emit_block_move_libcall_fn (true);
1371 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1372 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1374 retval = expand_normal (call_expr);
1376 return retval;
1379 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1380 for the function we use for block copies. */
1382 static GTY(()) tree block_move_fn;
1384 void
1385 init_block_move_fn (const char *asmspec)
1387 if (!block_move_fn)
1389 tree args, fn, attrs, attr_args;
1391 fn = get_identifier ("memcpy");
1392 args = build_function_type_list (ptr_type_node, ptr_type_node,
1393 const_ptr_type_node, sizetype,
1394 NULL_TREE);
1396 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1397 DECL_EXTERNAL (fn) = 1;
1398 TREE_PUBLIC (fn) = 1;
1399 DECL_ARTIFICIAL (fn) = 1;
1400 TREE_NOTHROW (fn) = 1;
1401 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1402 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1404 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1405 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1407 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1409 block_move_fn = fn;
1412 if (asmspec)
1413 set_user_assembler_name (block_move_fn, asmspec);
1416 static tree
1417 emit_block_move_libcall_fn (int for_call)
1419 static bool emitted_extern;
1421 if (!block_move_fn)
1422 init_block_move_fn (NULL);
1424 if (for_call && !emitted_extern)
1426 emitted_extern = true;
1427 make_decl_rtl (block_move_fn);
1430 return block_move_fn;
1433 /* A subroutine of emit_block_move. Copy the data via an explicit
1434 loop. This is used only when libcalls are forbidden. */
1435 /* ??? It'd be nice to copy in hunks larger than QImode. */
1437 static void
1438 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1439 unsigned int align ATTRIBUTE_UNUSED)
1441 rtx cmp_label, top_label, iter, x_addr, y_addr, tmp;
1442 enum machine_mode x_addr_mode = get_address_mode (x);
1443 enum machine_mode y_addr_mode = get_address_mode (y);
1444 enum machine_mode iter_mode;
1446 iter_mode = GET_MODE (size);
1447 if (iter_mode == VOIDmode)
1448 iter_mode = word_mode;
1450 top_label = gen_label_rtx ();
1451 cmp_label = gen_label_rtx ();
1452 iter = gen_reg_rtx (iter_mode);
1454 emit_move_insn (iter, const0_rtx);
1456 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1457 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1458 do_pending_stack_adjust ();
1460 emit_jump (cmp_label);
1461 emit_label (top_label);
1463 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1464 x_addr = gen_rtx_PLUS (x_addr_mode, x_addr, tmp);
1466 if (x_addr_mode != y_addr_mode)
1467 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1468 y_addr = gen_rtx_PLUS (y_addr_mode, y_addr, tmp);
1470 x = change_address (x, QImode, x_addr);
1471 y = change_address (y, QImode, y_addr);
1473 emit_move_insn (x, y);
1475 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1476 true, OPTAB_LIB_WIDEN);
1477 if (tmp != iter)
1478 emit_move_insn (iter, tmp);
1480 emit_label (cmp_label);
1482 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1483 true, top_label);
1486 /* Copy all or part of a value X into registers starting at REGNO.
1487 The number of registers to be filled is NREGS. */
1489 void
1490 move_block_to_reg (int regno, rtx x, int nregs, enum machine_mode mode)
1492 int i;
1493 #ifdef HAVE_load_multiple
1494 rtx pat;
1495 rtx last;
1496 #endif
1498 if (nregs == 0)
1499 return;
1501 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1502 x = validize_mem (force_const_mem (mode, x));
1504 /* See if the machine can do this with a load multiple insn. */
1505 #ifdef HAVE_load_multiple
1506 if (HAVE_load_multiple)
1508 last = get_last_insn ();
1509 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1510 GEN_INT (nregs));
1511 if (pat)
1513 emit_insn (pat);
1514 return;
1516 else
1517 delete_insns_since (last);
1519 #endif
1521 for (i = 0; i < nregs; i++)
1522 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1523 operand_subword_force (x, i, mode));
1526 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1527 The number of registers to be filled is NREGS. */
1529 void
1530 move_block_from_reg (int regno, rtx x, int nregs)
1532 int i;
1534 if (nregs == 0)
1535 return;
1537 /* See if the machine can do this with a store multiple insn. */
1538 #ifdef HAVE_store_multiple
1539 if (HAVE_store_multiple)
1541 rtx last = get_last_insn ();
1542 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1543 GEN_INT (nregs));
1544 if (pat)
1546 emit_insn (pat);
1547 return;
1549 else
1550 delete_insns_since (last);
1552 #endif
1554 for (i = 0; i < nregs; i++)
1556 rtx tem = operand_subword (x, i, 1, BLKmode);
1558 gcc_assert (tem);
1560 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1564 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1565 ORIG, where ORIG is a non-consecutive group of registers represented by
1566 a PARALLEL. The clone is identical to the original except in that the
1567 original set of registers is replaced by a new set of pseudo registers.
1568 The new set has the same modes as the original set. */
1571 gen_group_rtx (rtx orig)
1573 int i, length;
1574 rtx *tmps;
1576 gcc_assert (GET_CODE (orig) == PARALLEL);
1578 length = XVECLEN (orig, 0);
1579 tmps = XALLOCAVEC (rtx, length);
1581 /* Skip a NULL entry in first slot. */
1582 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1584 if (i)
1585 tmps[0] = 0;
1587 for (; i < length; i++)
1589 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1590 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1592 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1595 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1598 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1599 except that values are placed in TMPS[i], and must later be moved
1600 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1602 static void
1603 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1605 rtx src;
1606 int start, i;
1607 enum machine_mode m = GET_MODE (orig_src);
1609 gcc_assert (GET_CODE (dst) == PARALLEL);
1611 if (m != VOIDmode
1612 && !SCALAR_INT_MODE_P (m)
1613 && !MEM_P (orig_src)
1614 && GET_CODE (orig_src) != CONCAT)
1616 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1617 if (imode == BLKmode)
1618 src = assign_stack_temp (GET_MODE (orig_src), ssize, 0);
1619 else
1620 src = gen_reg_rtx (imode);
1621 if (imode != BLKmode)
1622 src = gen_lowpart (GET_MODE (orig_src), src);
1623 emit_move_insn (src, orig_src);
1624 /* ...and back again. */
1625 if (imode != BLKmode)
1626 src = gen_lowpart (imode, src);
1627 emit_group_load_1 (tmps, dst, src, type, ssize);
1628 return;
1631 /* Check for a NULL entry, used to indicate that the parameter goes
1632 both on the stack and in registers. */
1633 if (XEXP (XVECEXP (dst, 0, 0), 0))
1634 start = 0;
1635 else
1636 start = 1;
1638 /* Process the pieces. */
1639 for (i = start; i < XVECLEN (dst, 0); i++)
1641 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1642 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1643 unsigned int bytelen = GET_MODE_SIZE (mode);
1644 int shift = 0;
1646 /* Handle trailing fragments that run over the size of the struct. */
1647 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1649 /* Arrange to shift the fragment to where it belongs.
1650 extract_bit_field loads to the lsb of the reg. */
1651 if (
1652 #ifdef BLOCK_REG_PADDING
1653 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1654 == (BYTES_BIG_ENDIAN ? upward : downward)
1655 #else
1656 BYTES_BIG_ENDIAN
1657 #endif
1659 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1660 bytelen = ssize - bytepos;
1661 gcc_assert (bytelen > 0);
1664 /* If we won't be loading directly from memory, protect the real source
1665 from strange tricks we might play; but make sure that the source can
1666 be loaded directly into the destination. */
1667 src = orig_src;
1668 if (!MEM_P (orig_src)
1669 && (!CONSTANT_P (orig_src)
1670 || (GET_MODE (orig_src) != mode
1671 && GET_MODE (orig_src) != VOIDmode)))
1673 if (GET_MODE (orig_src) == VOIDmode)
1674 src = gen_reg_rtx (mode);
1675 else
1676 src = gen_reg_rtx (GET_MODE (orig_src));
1678 emit_move_insn (src, orig_src);
1681 /* Optimize the access just a bit. */
1682 if (MEM_P (src)
1683 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1684 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1685 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1686 && bytelen == GET_MODE_SIZE (mode))
1688 tmps[i] = gen_reg_rtx (mode);
1689 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1691 else if (COMPLEX_MODE_P (mode)
1692 && GET_MODE (src) == mode
1693 && bytelen == GET_MODE_SIZE (mode))
1694 /* Let emit_move_complex do the bulk of the work. */
1695 tmps[i] = src;
1696 else if (GET_CODE (src) == CONCAT)
1698 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1699 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1701 if ((bytepos == 0 && bytelen == slen0)
1702 || (bytepos != 0 && bytepos + bytelen <= slen))
1704 /* The following assumes that the concatenated objects all
1705 have the same size. In this case, a simple calculation
1706 can be used to determine the object and the bit field
1707 to be extracted. */
1708 tmps[i] = XEXP (src, bytepos / slen0);
1709 if (! CONSTANT_P (tmps[i])
1710 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1711 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1712 (bytepos % slen0) * BITS_PER_UNIT,
1713 1, false, NULL_RTX, mode, mode);
1715 else
1717 rtx mem;
1719 gcc_assert (!bytepos);
1720 mem = assign_stack_temp (GET_MODE (src), slen, 0);
1721 emit_move_insn (mem, src);
1722 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1723 0, 1, false, NULL_RTX, mode, mode);
1726 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1727 SIMD register, which is currently broken. While we get GCC
1728 to emit proper RTL for these cases, let's dump to memory. */
1729 else if (VECTOR_MODE_P (GET_MODE (dst))
1730 && REG_P (src))
1732 int slen = GET_MODE_SIZE (GET_MODE (src));
1733 rtx mem;
1735 mem = assign_stack_temp (GET_MODE (src), slen, 0);
1736 emit_move_insn (mem, src);
1737 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1739 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1740 && XVECLEN (dst, 0) > 1)
1741 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE(dst), bytepos);
1742 else if (CONSTANT_P (src))
1744 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1746 if (len == ssize)
1747 tmps[i] = src;
1748 else
1750 rtx first, second;
1752 gcc_assert (2 * len == ssize);
1753 split_double (src, &first, &second);
1754 if (i)
1755 tmps[i] = second;
1756 else
1757 tmps[i] = first;
1760 else if (REG_P (src) && GET_MODE (src) == mode)
1761 tmps[i] = src;
1762 else
1763 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1764 bytepos * BITS_PER_UNIT, 1, false, NULL_RTX,
1765 mode, mode);
1767 if (shift)
1768 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1769 shift, tmps[i], 0);
1773 /* Emit code to move a block SRC of type TYPE to a block DST,
1774 where DST is non-consecutive registers represented by a PARALLEL.
1775 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1776 if not known. */
1778 void
1779 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1781 rtx *tmps;
1782 int i;
1784 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1785 emit_group_load_1 (tmps, dst, src, type, ssize);
1787 /* Copy the extracted pieces into the proper (probable) hard regs. */
1788 for (i = 0; i < XVECLEN (dst, 0); i++)
1790 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1791 if (d == NULL)
1792 continue;
1793 emit_move_insn (d, tmps[i]);
1797 /* Similar, but load SRC into new pseudos in a format that looks like
1798 PARALLEL. This can later be fed to emit_group_move to get things
1799 in the right place. */
1802 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1804 rtvec vec;
1805 int i;
1807 vec = rtvec_alloc (XVECLEN (parallel, 0));
1808 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1810 /* Convert the vector to look just like the original PARALLEL, except
1811 with the computed values. */
1812 for (i = 0; i < XVECLEN (parallel, 0); i++)
1814 rtx e = XVECEXP (parallel, 0, i);
1815 rtx d = XEXP (e, 0);
1817 if (d)
1819 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1820 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1822 RTVEC_ELT (vec, i) = e;
1825 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1828 /* Emit code to move a block SRC to block DST, where SRC and DST are
1829 non-consecutive groups of registers, each represented by a PARALLEL. */
1831 void
1832 emit_group_move (rtx dst, rtx src)
1834 int i;
1836 gcc_assert (GET_CODE (src) == PARALLEL
1837 && GET_CODE (dst) == PARALLEL
1838 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1840 /* Skip first entry if NULL. */
1841 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1842 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1843 XEXP (XVECEXP (src, 0, i), 0));
1846 /* Move a group of registers represented by a PARALLEL into pseudos. */
1849 emit_group_move_into_temps (rtx src)
1851 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1852 int i;
1854 for (i = 0; i < XVECLEN (src, 0); i++)
1856 rtx e = XVECEXP (src, 0, i);
1857 rtx d = XEXP (e, 0);
1859 if (d)
1860 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1861 RTVEC_ELT (vec, i) = e;
1864 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1867 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1868 where SRC is non-consecutive registers represented by a PARALLEL.
1869 SSIZE represents the total size of block ORIG_DST, or -1 if not
1870 known. */
1872 void
1873 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1875 rtx *tmps, dst;
1876 int start, finish, i;
1877 enum machine_mode m = GET_MODE (orig_dst);
1879 gcc_assert (GET_CODE (src) == PARALLEL);
1881 if (!SCALAR_INT_MODE_P (m)
1882 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1884 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1885 if (imode == BLKmode)
1886 dst = assign_stack_temp (GET_MODE (orig_dst), ssize, 0);
1887 else
1888 dst = gen_reg_rtx (imode);
1889 emit_group_store (dst, src, type, ssize);
1890 if (imode != BLKmode)
1891 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1892 emit_move_insn (orig_dst, dst);
1893 return;
1896 /* Check for a NULL entry, used to indicate that the parameter goes
1897 both on the stack and in registers. */
1898 if (XEXP (XVECEXP (src, 0, 0), 0))
1899 start = 0;
1900 else
1901 start = 1;
1902 finish = XVECLEN (src, 0);
1904 tmps = XALLOCAVEC (rtx, finish);
1906 /* Copy the (probable) hard regs into pseudos. */
1907 for (i = start; i < finish; i++)
1909 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1910 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1912 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1913 emit_move_insn (tmps[i], reg);
1915 else
1916 tmps[i] = reg;
1919 /* If we won't be storing directly into memory, protect the real destination
1920 from strange tricks we might play. */
1921 dst = orig_dst;
1922 if (GET_CODE (dst) == PARALLEL)
1924 rtx temp;
1926 /* We can get a PARALLEL dst if there is a conditional expression in
1927 a return statement. In that case, the dst and src are the same,
1928 so no action is necessary. */
1929 if (rtx_equal_p (dst, src))
1930 return;
1932 /* It is unclear if we can ever reach here, but we may as well handle
1933 it. Allocate a temporary, and split this into a store/load to/from
1934 the temporary. */
1936 temp = assign_stack_temp (GET_MODE (dst), ssize, 0);
1937 emit_group_store (temp, src, type, ssize);
1938 emit_group_load (dst, temp, type, ssize);
1939 return;
1941 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1943 enum machine_mode outer = GET_MODE (dst);
1944 enum machine_mode inner;
1945 HOST_WIDE_INT bytepos;
1946 bool done = false;
1947 rtx temp;
1949 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1950 dst = gen_reg_rtx (outer);
1952 /* Make life a bit easier for combine. */
1953 /* If the first element of the vector is the low part
1954 of the destination mode, use a paradoxical subreg to
1955 initialize the destination. */
1956 if (start < finish)
1958 inner = GET_MODE (tmps[start]);
1959 bytepos = subreg_lowpart_offset (inner, outer);
1960 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1962 temp = simplify_gen_subreg (outer, tmps[start],
1963 inner, 0);
1964 if (temp)
1966 emit_move_insn (dst, temp);
1967 done = true;
1968 start++;
1973 /* If the first element wasn't the low part, try the last. */
1974 if (!done
1975 && start < finish - 1)
1977 inner = GET_MODE (tmps[finish - 1]);
1978 bytepos = subreg_lowpart_offset (inner, outer);
1979 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1981 temp = simplify_gen_subreg (outer, tmps[finish - 1],
1982 inner, 0);
1983 if (temp)
1985 emit_move_insn (dst, temp);
1986 done = true;
1987 finish--;
1992 /* Otherwise, simply initialize the result to zero. */
1993 if (!done)
1994 emit_move_insn (dst, CONST0_RTX (outer));
1997 /* Process the pieces. */
1998 for (i = start; i < finish; i++)
2000 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2001 enum machine_mode mode = GET_MODE (tmps[i]);
2002 unsigned int bytelen = GET_MODE_SIZE (mode);
2003 unsigned int adj_bytelen = bytelen;
2004 rtx dest = dst;
2006 /* Handle trailing fragments that run over the size of the struct. */
2007 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2008 adj_bytelen = ssize - bytepos;
2010 if (GET_CODE (dst) == CONCAT)
2012 if (bytepos + adj_bytelen
2013 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2014 dest = XEXP (dst, 0);
2015 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2017 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2018 dest = XEXP (dst, 1);
2020 else
2022 enum machine_mode dest_mode = GET_MODE (dest);
2023 enum machine_mode tmp_mode = GET_MODE (tmps[i]);
2025 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2027 if (GET_MODE_ALIGNMENT (dest_mode)
2028 >= GET_MODE_ALIGNMENT (tmp_mode))
2030 dest = assign_stack_temp (dest_mode,
2031 GET_MODE_SIZE (dest_mode),
2033 emit_move_insn (adjust_address (dest,
2034 tmp_mode,
2035 bytepos),
2036 tmps[i]);
2037 dst = dest;
2039 else
2041 dest = assign_stack_temp (tmp_mode,
2042 GET_MODE_SIZE (tmp_mode),
2044 emit_move_insn (dest, tmps[i]);
2045 dst = adjust_address (dest, dest_mode, bytepos);
2047 break;
2051 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2053 /* store_bit_field always takes its value from the lsb.
2054 Move the fragment to the lsb if it's not already there. */
2055 if (
2056 #ifdef BLOCK_REG_PADDING
2057 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2058 == (BYTES_BIG_ENDIAN ? upward : downward)
2059 #else
2060 BYTES_BIG_ENDIAN
2061 #endif
2064 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2065 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2066 shift, tmps[i], 0);
2068 bytelen = adj_bytelen;
2071 /* Optimize the access just a bit. */
2072 if (MEM_P (dest)
2073 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2074 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2075 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2076 && bytelen == GET_MODE_SIZE (mode))
2077 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2078 else
2079 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2080 0, 0, mode, tmps[i]);
2083 /* Copy from the pseudo into the (probable) hard reg. */
2084 if (orig_dst != dst)
2085 emit_move_insn (orig_dst, dst);
2088 /* Generate code to copy a BLKmode object of TYPE out of a
2089 set of registers starting with SRCREG into TGTBLK. If TGTBLK
2090 is null, a stack temporary is created. TGTBLK is returned.
2092 The purpose of this routine is to handle functions that return
2093 BLKmode structures in registers. Some machines (the PA for example)
2094 want to return all small structures in registers regardless of the
2095 structure's alignment. */
2098 copy_blkmode_from_reg (rtx tgtblk, rtx srcreg, tree type)
2100 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2101 rtx src = NULL, dst = NULL;
2102 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2103 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2104 enum machine_mode copy_mode;
2106 if (tgtblk == 0)
2108 tgtblk = assign_temp (build_qualified_type (type,
2109 (TYPE_QUALS (type)
2110 | TYPE_QUAL_CONST)),
2111 0, 1, 1);
2112 preserve_temp_slots (tgtblk);
2115 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2116 into a new pseudo which is a full word. */
2118 if (GET_MODE (srcreg) != BLKmode
2119 && GET_MODE_SIZE (GET_MODE (srcreg)) < UNITS_PER_WORD)
2120 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2122 /* If the structure doesn't take up a whole number of words, see whether
2123 SRCREG is padded on the left or on the right. If it's on the left,
2124 set PADDING_CORRECTION to the number of bits to skip.
2126 In most ABIs, the structure will be returned at the least end of
2127 the register, which translates to right padding on little-endian
2128 targets and left padding on big-endian targets. The opposite
2129 holds if the structure is returned at the most significant
2130 end of the register. */
2131 if (bytes % UNITS_PER_WORD != 0
2132 && (targetm.calls.return_in_msb (type)
2133 ? !BYTES_BIG_ENDIAN
2134 : BYTES_BIG_ENDIAN))
2135 padding_correction
2136 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2138 /* Copy the structure BITSIZE bits at a time. If the target lives in
2139 memory, take care of not reading/writing past its end by selecting
2140 a copy mode suited to BITSIZE. This should always be possible given
2141 how it is computed.
2143 We could probably emit more efficient code for machines which do not use
2144 strict alignment, but it doesn't seem worth the effort at the current
2145 time. */
2147 copy_mode = word_mode;
2148 if (MEM_P (tgtblk))
2150 enum machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2151 if (mem_mode != BLKmode)
2152 copy_mode = mem_mode;
2155 for (bitpos = 0, xbitpos = padding_correction;
2156 bitpos < bytes * BITS_PER_UNIT;
2157 bitpos += bitsize, xbitpos += bitsize)
2159 /* We need a new source operand each time xbitpos is on a
2160 word boundary and when xbitpos == padding_correction
2161 (the first time through). */
2162 if (xbitpos % BITS_PER_WORD == 0
2163 || xbitpos == padding_correction)
2164 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD,
2165 GET_MODE (srcreg));
2167 /* We need a new destination operand each time bitpos is on
2168 a word boundary. */
2169 if (bitpos % BITS_PER_WORD == 0)
2170 dst = operand_subword (tgtblk, bitpos / BITS_PER_WORD, 1, BLKmode);
2172 /* Use xbitpos for the source extraction (right justified) and
2173 bitpos for the destination store (left justified). */
2174 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2175 extract_bit_field (src, bitsize,
2176 xbitpos % BITS_PER_WORD, 1, false,
2177 NULL_RTX, copy_mode, copy_mode));
2180 return tgtblk;
2183 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2184 register if it contains any data, otherwise return null.
2186 This is used on targets that return BLKmode values in registers. */
2189 copy_blkmode_to_reg (enum machine_mode mode, tree src)
2191 int i, n_regs;
2192 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2193 unsigned int bitsize;
2194 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2195 enum machine_mode dst_mode;
2197 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2199 x = expand_normal (src);
2201 bytes = int_size_in_bytes (TREE_TYPE (src));
2202 if (bytes == 0)
2203 return NULL_RTX;
2205 /* If the structure doesn't take up a whole number of words, see
2206 whether the register value should be padded on the left or on
2207 the right. Set PADDING_CORRECTION to the number of padding
2208 bits needed on the left side.
2210 In most ABIs, the structure will be returned at the least end of
2211 the register, which translates to right padding on little-endian
2212 targets and left padding on big-endian targets. The opposite
2213 holds if the structure is returned at the most significant
2214 end of the register. */
2215 if (bytes % UNITS_PER_WORD != 0
2216 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2217 ? !BYTES_BIG_ENDIAN
2218 : BYTES_BIG_ENDIAN))
2219 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2220 * BITS_PER_UNIT));
2222 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2223 dst_words = XALLOCAVEC (rtx, n_regs);
2224 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2226 /* Copy the structure BITSIZE bits at a time. */
2227 for (bitpos = 0, xbitpos = padding_correction;
2228 bitpos < bytes * BITS_PER_UNIT;
2229 bitpos += bitsize, xbitpos += bitsize)
2231 /* We need a new destination pseudo each time xbitpos is
2232 on a word boundary and when xbitpos == padding_correction
2233 (the first time through). */
2234 if (xbitpos % BITS_PER_WORD == 0
2235 || xbitpos == padding_correction)
2237 /* Generate an appropriate register. */
2238 dst_word = gen_reg_rtx (word_mode);
2239 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2241 /* Clear the destination before we move anything into it. */
2242 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2245 /* We need a new source operand each time bitpos is on a word
2246 boundary. */
2247 if (bitpos % BITS_PER_WORD == 0)
2248 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2250 /* Use bitpos for the source extraction (left justified) and
2251 xbitpos for the destination store (right justified). */
2252 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2253 0, 0, word_mode,
2254 extract_bit_field (src_word, bitsize,
2255 bitpos % BITS_PER_WORD, 1, false,
2256 NULL_RTX, word_mode, word_mode));
2259 if (mode == BLKmode)
2261 /* Find the smallest integer mode large enough to hold the
2262 entire structure. */
2263 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2264 mode != VOIDmode;
2265 mode = GET_MODE_WIDER_MODE (mode))
2266 /* Have we found a large enough mode? */
2267 if (GET_MODE_SIZE (mode) >= bytes)
2268 break;
2270 /* A suitable mode should have been found. */
2271 gcc_assert (mode != VOIDmode);
2274 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2275 dst_mode = word_mode;
2276 else
2277 dst_mode = mode;
2278 dst = gen_reg_rtx (dst_mode);
2280 for (i = 0; i < n_regs; i++)
2281 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2283 if (mode != dst_mode)
2284 dst = gen_lowpart (mode, dst);
2286 return dst;
2289 /* Add a USE expression for REG to the (possibly empty) list pointed
2290 to by CALL_FUSAGE. REG must denote a hard register. */
2292 void
2293 use_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
2295 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2297 *call_fusage
2298 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2301 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2302 starting at REGNO. All of these registers must be hard registers. */
2304 void
2305 use_regs (rtx *call_fusage, int regno, int nregs)
2307 int i;
2309 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2311 for (i = 0; i < nregs; i++)
2312 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2315 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2316 PARALLEL REGS. This is for calls that pass values in multiple
2317 non-contiguous locations. The Irix 6 ABI has examples of this. */
2319 void
2320 use_group_regs (rtx *call_fusage, rtx regs)
2322 int i;
2324 for (i = 0; i < XVECLEN (regs, 0); i++)
2326 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2328 /* A NULL entry means the parameter goes both on the stack and in
2329 registers. This can also be a MEM for targets that pass values
2330 partially on the stack and partially in registers. */
2331 if (reg != 0 && REG_P (reg))
2332 use_reg (call_fusage, reg);
2336 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2337 assigment and the code of the expresion on the RHS is CODE. Return
2338 NULL otherwise. */
2340 static gimple
2341 get_def_for_expr (tree name, enum tree_code code)
2343 gimple def_stmt;
2345 if (TREE_CODE (name) != SSA_NAME)
2346 return NULL;
2348 def_stmt = get_gimple_for_ssa_name (name);
2349 if (!def_stmt
2350 || gimple_assign_rhs_code (def_stmt) != code)
2351 return NULL;
2353 return def_stmt;
2356 #ifdef HAVE_conditional_move
2357 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2358 assigment and the class of the expresion on the RHS is CLASS. Return
2359 NULL otherwise. */
2361 static gimple
2362 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2364 gimple def_stmt;
2366 if (TREE_CODE (name) != SSA_NAME)
2367 return NULL;
2369 def_stmt = get_gimple_for_ssa_name (name);
2370 if (!def_stmt
2371 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2372 return NULL;
2374 return def_stmt;
2376 #endif
2379 /* Determine whether the LEN bytes generated by CONSTFUN can be
2380 stored to memory using several move instructions. CONSTFUNDATA is
2381 a pointer which will be passed as argument in every CONSTFUN call.
2382 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2383 a memset operation and false if it's a copy of a constant string.
2384 Return nonzero if a call to store_by_pieces should succeed. */
2387 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2388 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2389 void *constfundata, unsigned int align, bool memsetp)
2391 unsigned HOST_WIDE_INT l;
2392 unsigned int max_size;
2393 HOST_WIDE_INT offset = 0;
2394 enum machine_mode mode;
2395 enum insn_code icode;
2396 int reverse;
2397 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2398 rtx cst ATTRIBUTE_UNUSED;
2400 if (len == 0)
2401 return 1;
2403 if (! (memsetp
2404 ? SET_BY_PIECES_P (len, align)
2405 : STORE_BY_PIECES_P (len, align)))
2406 return 0;
2408 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2410 /* We would first store what we can in the largest integer mode, then go to
2411 successively smaller modes. */
2413 for (reverse = 0;
2414 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2415 reverse++)
2417 l = len;
2418 max_size = STORE_MAX_PIECES + 1;
2419 while (max_size > 1)
2421 mode = widest_int_mode_for_size (max_size);
2423 if (mode == VOIDmode)
2424 break;
2426 icode = optab_handler (mov_optab, mode);
2427 if (icode != CODE_FOR_nothing
2428 && align >= GET_MODE_ALIGNMENT (mode))
2430 unsigned int size = GET_MODE_SIZE (mode);
2432 while (l >= size)
2434 if (reverse)
2435 offset -= size;
2437 cst = (*constfun) (constfundata, offset, mode);
2438 if (!targetm.legitimate_constant_p (mode, cst))
2439 return 0;
2441 if (!reverse)
2442 offset += size;
2444 l -= size;
2448 max_size = GET_MODE_SIZE (mode);
2451 /* The code above should have handled everything. */
2452 gcc_assert (!l);
2455 return 1;
2458 /* Generate several move instructions to store LEN bytes generated by
2459 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2460 pointer which will be passed as argument in every CONSTFUN call.
2461 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2462 a memset operation and false if it's a copy of a constant string.
2463 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2464 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2465 stpcpy. */
2468 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2469 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2470 void *constfundata, unsigned int align, bool memsetp, int endp)
2472 enum machine_mode to_addr_mode = get_address_mode (to);
2473 struct store_by_pieces_d data;
2475 if (len == 0)
2477 gcc_assert (endp != 2);
2478 return to;
2481 gcc_assert (memsetp
2482 ? SET_BY_PIECES_P (len, align)
2483 : STORE_BY_PIECES_P (len, align));
2484 data.constfun = constfun;
2485 data.constfundata = constfundata;
2486 data.len = len;
2487 data.to = to;
2488 store_by_pieces_1 (&data, align);
2489 if (endp)
2491 rtx to1;
2493 gcc_assert (!data.reverse);
2494 if (data.autinc_to)
2496 if (endp == 2)
2498 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2499 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2500 else
2501 data.to_addr = copy_to_mode_reg (to_addr_mode,
2502 plus_constant (to_addr_mode,
2503 data.to_addr,
2504 -1));
2506 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2507 data.offset);
2509 else
2511 if (endp == 2)
2512 --data.offset;
2513 to1 = adjust_address (data.to, QImode, data.offset);
2515 return to1;
2517 else
2518 return data.to;
2521 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2522 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2524 static void
2525 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2527 struct store_by_pieces_d data;
2529 if (len == 0)
2530 return;
2532 data.constfun = clear_by_pieces_1;
2533 data.constfundata = NULL;
2534 data.len = len;
2535 data.to = to;
2536 store_by_pieces_1 (&data, align);
2539 /* Callback routine for clear_by_pieces.
2540 Return const0_rtx unconditionally. */
2542 static rtx
2543 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2544 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2545 enum machine_mode mode ATTRIBUTE_UNUSED)
2547 return const0_rtx;
2550 /* Subroutine of clear_by_pieces and store_by_pieces.
2551 Generate several move instructions to store LEN bytes of block TO. (A MEM
2552 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2554 static void
2555 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2556 unsigned int align ATTRIBUTE_UNUSED)
2558 enum machine_mode to_addr_mode = get_address_mode (data->to);
2559 rtx to_addr = XEXP (data->to, 0);
2560 unsigned int max_size = STORE_MAX_PIECES + 1;
2561 enum insn_code icode;
2563 data->offset = 0;
2564 data->to_addr = to_addr;
2565 data->autinc_to
2566 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2567 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2569 data->explicit_inc_to = 0;
2570 data->reverse
2571 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2572 if (data->reverse)
2573 data->offset = data->len;
2575 /* If storing requires more than two move insns,
2576 copy addresses to registers (to make displacements shorter)
2577 and use post-increment if available. */
2578 if (!data->autinc_to
2579 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2581 /* Determine the main mode we'll be using.
2582 MODE might not be used depending on the definitions of the
2583 USE_* macros below. */
2584 enum machine_mode mode ATTRIBUTE_UNUSED
2585 = widest_int_mode_for_size (max_size);
2587 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2589 data->to_addr = copy_to_mode_reg (to_addr_mode,
2590 plus_constant (to_addr_mode,
2591 to_addr,
2592 data->len));
2593 data->autinc_to = 1;
2594 data->explicit_inc_to = -1;
2597 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2598 && ! data->autinc_to)
2600 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2601 data->autinc_to = 1;
2602 data->explicit_inc_to = 1;
2605 if ( !data->autinc_to && CONSTANT_P (to_addr))
2606 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2609 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2611 /* First store what we can in the largest integer mode, then go to
2612 successively smaller modes. */
2614 while (max_size > 1)
2616 enum machine_mode mode = widest_int_mode_for_size (max_size);
2618 if (mode == VOIDmode)
2619 break;
2621 icode = optab_handler (mov_optab, mode);
2622 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2623 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2625 max_size = GET_MODE_SIZE (mode);
2628 /* The code above should have handled everything. */
2629 gcc_assert (!data->len);
2632 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2633 with move instructions for mode MODE. GENFUN is the gen_... function
2634 to make a move insn for that mode. DATA has all the other info. */
2636 static void
2637 store_by_pieces_2 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
2638 struct store_by_pieces_d *data)
2640 unsigned int size = GET_MODE_SIZE (mode);
2641 rtx to1, cst;
2643 while (data->len >= size)
2645 if (data->reverse)
2646 data->offset -= size;
2648 if (data->autinc_to)
2649 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2650 data->offset);
2651 else
2652 to1 = adjust_address (data->to, mode, data->offset);
2654 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2655 emit_insn (gen_add2_insn (data->to_addr,
2656 GEN_INT (-(HOST_WIDE_INT) size)));
2658 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2659 emit_insn ((*genfun) (to1, cst));
2661 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2662 emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
2664 if (! data->reverse)
2665 data->offset += size;
2667 data->len -= size;
2671 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2672 its length in bytes. */
2675 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2676 unsigned int expected_align, HOST_WIDE_INT expected_size)
2678 enum machine_mode mode = GET_MODE (object);
2679 unsigned int align;
2681 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2683 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2684 just move a zero. Otherwise, do this a piece at a time. */
2685 if (mode != BLKmode
2686 && CONST_INT_P (size)
2687 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2689 rtx zero = CONST0_RTX (mode);
2690 if (zero != NULL)
2692 emit_move_insn (object, zero);
2693 return NULL;
2696 if (COMPLEX_MODE_P (mode))
2698 zero = CONST0_RTX (GET_MODE_INNER (mode));
2699 if (zero != NULL)
2701 write_complex_part (object, zero, 0);
2702 write_complex_part (object, zero, 1);
2703 return NULL;
2708 if (size == const0_rtx)
2709 return NULL;
2711 align = MEM_ALIGN (object);
2713 if (CONST_INT_P (size)
2714 && CLEAR_BY_PIECES_P (INTVAL (size), align))
2715 clear_by_pieces (object, INTVAL (size), align);
2716 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2717 expected_align, expected_size))
2719 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2720 return set_storage_via_libcall (object, size, const0_rtx,
2721 method == BLOCK_OP_TAILCALL);
2722 else
2723 gcc_unreachable ();
2725 return NULL;
2729 clear_storage (rtx object, rtx size, enum block_op_methods method)
2731 return clear_storage_hints (object, size, method, 0, -1);
2735 /* A subroutine of clear_storage. Expand a call to memset.
2736 Return the return value of memset, 0 otherwise. */
2739 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2741 tree call_expr, fn, object_tree, size_tree, val_tree;
2742 enum machine_mode size_mode;
2743 rtx retval;
2745 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2746 place those into new pseudos into a VAR_DECL and use them later. */
2748 object = copy_addr_to_reg (XEXP (object, 0));
2750 size_mode = TYPE_MODE (sizetype);
2751 size = convert_to_mode (size_mode, size, 1);
2752 size = copy_to_mode_reg (size_mode, size);
2754 /* It is incorrect to use the libcall calling conventions to call
2755 memset in this context. This could be a user call to memset and
2756 the user may wish to examine the return value from memset. For
2757 targets where libcalls and normal calls have different conventions
2758 for returning pointers, we could end up generating incorrect code. */
2760 object_tree = make_tree (ptr_type_node, object);
2761 if (!CONST_INT_P (val))
2762 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2763 size_tree = make_tree (sizetype, size);
2764 val_tree = make_tree (integer_type_node, val);
2766 fn = clear_storage_libcall_fn (true);
2767 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2768 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2770 retval = expand_normal (call_expr);
2772 return retval;
2775 /* A subroutine of set_storage_via_libcall. Create the tree node
2776 for the function we use for block clears. */
2778 tree block_clear_fn;
2780 void
2781 init_block_clear_fn (const char *asmspec)
2783 if (!block_clear_fn)
2785 tree fn, args;
2787 fn = get_identifier ("memset");
2788 args = build_function_type_list (ptr_type_node, ptr_type_node,
2789 integer_type_node, sizetype,
2790 NULL_TREE);
2792 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2793 DECL_EXTERNAL (fn) = 1;
2794 TREE_PUBLIC (fn) = 1;
2795 DECL_ARTIFICIAL (fn) = 1;
2796 TREE_NOTHROW (fn) = 1;
2797 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2798 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2800 block_clear_fn = fn;
2803 if (asmspec)
2804 set_user_assembler_name (block_clear_fn, asmspec);
2807 static tree
2808 clear_storage_libcall_fn (int for_call)
2810 static bool emitted_extern;
2812 if (!block_clear_fn)
2813 init_block_clear_fn (NULL);
2815 if (for_call && !emitted_extern)
2817 emitted_extern = true;
2818 make_decl_rtl (block_clear_fn);
2821 return block_clear_fn;
2824 /* Expand a setmem pattern; return true if successful. */
2826 bool
2827 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2828 unsigned int expected_align, HOST_WIDE_INT expected_size)
2830 /* Try the most limited insn first, because there's no point
2831 including more than one in the machine description unless
2832 the more limited one has some advantage. */
2834 enum machine_mode mode;
2836 if (expected_align < align)
2837 expected_align = align;
2839 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2840 mode = GET_MODE_WIDER_MODE (mode))
2842 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2844 if (code != CODE_FOR_nothing
2845 /* We don't need MODE to be narrower than
2846 BITS_PER_HOST_WIDE_INT here because if SIZE is less than
2847 the mode mask, as it is returned by the macro, it will
2848 definitely be less than the actual mode mask. */
2849 && ((CONST_INT_P (size)
2850 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2851 <= (GET_MODE_MASK (mode) >> 1)))
2852 || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
2854 struct expand_operand ops[6];
2855 unsigned int nops;
2857 nops = insn_data[(int) code].n_generator_args;
2858 gcc_assert (nops == 4 || nops == 6);
2860 create_fixed_operand (&ops[0], object);
2861 /* The check above guarantees that this size conversion is valid. */
2862 create_convert_operand_to (&ops[1], size, mode, true);
2863 create_convert_operand_from (&ops[2], val, byte_mode, true);
2864 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2865 if (nops == 6)
2867 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2868 create_integer_operand (&ops[5], expected_size);
2870 if (maybe_expand_insn (code, nops, ops))
2871 return true;
2875 return false;
2879 /* Write to one of the components of the complex value CPLX. Write VAL to
2880 the real part if IMAG_P is false, and the imaginary part if its true. */
2882 static void
2883 write_complex_part (rtx cplx, rtx val, bool imag_p)
2885 enum machine_mode cmode;
2886 enum machine_mode imode;
2887 unsigned ibitsize;
2889 if (GET_CODE (cplx) == CONCAT)
2891 emit_move_insn (XEXP (cplx, imag_p), val);
2892 return;
2895 cmode = GET_MODE (cplx);
2896 imode = GET_MODE_INNER (cmode);
2897 ibitsize = GET_MODE_BITSIZE (imode);
2899 /* For MEMs simplify_gen_subreg may generate an invalid new address
2900 because, e.g., the original address is considered mode-dependent
2901 by the target, which restricts simplify_subreg from invoking
2902 adjust_address_nv. Instead of preparing fallback support for an
2903 invalid address, we call adjust_address_nv directly. */
2904 if (MEM_P (cplx))
2906 emit_move_insn (adjust_address_nv (cplx, imode,
2907 imag_p ? GET_MODE_SIZE (imode) : 0),
2908 val);
2909 return;
2912 /* If the sub-object is at least word sized, then we know that subregging
2913 will work. This special case is important, since store_bit_field
2914 wants to operate on integer modes, and there's rarely an OImode to
2915 correspond to TCmode. */
2916 if (ibitsize >= BITS_PER_WORD
2917 /* For hard regs we have exact predicates. Assume we can split
2918 the original object if it spans an even number of hard regs.
2919 This special case is important for SCmode on 64-bit platforms
2920 where the natural size of floating-point regs is 32-bit. */
2921 || (REG_P (cplx)
2922 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
2923 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
2925 rtx part = simplify_gen_subreg (imode, cplx, cmode,
2926 imag_p ? GET_MODE_SIZE (imode) : 0);
2927 if (part)
2929 emit_move_insn (part, val);
2930 return;
2932 else
2933 /* simplify_gen_subreg may fail for sub-word MEMs. */
2934 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
2937 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
2940 /* Extract one of the components of the complex value CPLX. Extract the
2941 real part if IMAG_P is false, and the imaginary part if it's true. */
2943 static rtx
2944 read_complex_part (rtx cplx, bool imag_p)
2946 enum machine_mode cmode, imode;
2947 unsigned ibitsize;
2949 if (GET_CODE (cplx) == CONCAT)
2950 return XEXP (cplx, imag_p);
2952 cmode = GET_MODE (cplx);
2953 imode = GET_MODE_INNER (cmode);
2954 ibitsize = GET_MODE_BITSIZE (imode);
2956 /* Special case reads from complex constants that got spilled to memory. */
2957 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
2959 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
2960 if (decl && TREE_CODE (decl) == COMPLEX_CST)
2962 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
2963 if (CONSTANT_CLASS_P (part))
2964 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
2968 /* For MEMs simplify_gen_subreg may generate an invalid new address
2969 because, e.g., the original address is considered mode-dependent
2970 by the target, which restricts simplify_subreg from invoking
2971 adjust_address_nv. Instead of preparing fallback support for an
2972 invalid address, we call adjust_address_nv directly. */
2973 if (MEM_P (cplx))
2974 return adjust_address_nv (cplx, imode,
2975 imag_p ? GET_MODE_SIZE (imode) : 0);
2977 /* If the sub-object is at least word sized, then we know that subregging
2978 will work. This special case is important, since extract_bit_field
2979 wants to operate on integer modes, and there's rarely an OImode to
2980 correspond to TCmode. */
2981 if (ibitsize >= BITS_PER_WORD
2982 /* For hard regs we have exact predicates. Assume we can split
2983 the original object if it spans an even number of hard regs.
2984 This special case is important for SCmode on 64-bit platforms
2985 where the natural size of floating-point regs is 32-bit. */
2986 || (REG_P (cplx)
2987 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
2988 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
2990 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
2991 imag_p ? GET_MODE_SIZE (imode) : 0);
2992 if (ret)
2993 return ret;
2994 else
2995 /* simplify_gen_subreg may fail for sub-word MEMs. */
2996 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
2999 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3000 true, false, NULL_RTX, imode, imode);
3003 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3004 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3005 represented in NEW_MODE. If FORCE is true, this will never happen, as
3006 we'll force-create a SUBREG if needed. */
3008 static rtx
3009 emit_move_change_mode (enum machine_mode new_mode,
3010 enum machine_mode old_mode, rtx x, bool force)
3012 rtx ret;
3014 if (push_operand (x, GET_MODE (x)))
3016 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3017 MEM_COPY_ATTRIBUTES (ret, x);
3019 else if (MEM_P (x))
3021 /* We don't have to worry about changing the address since the
3022 size in bytes is supposed to be the same. */
3023 if (reload_in_progress)
3025 /* Copy the MEM to change the mode and move any
3026 substitutions from the old MEM to the new one. */
3027 ret = adjust_address_nv (x, new_mode, 0);
3028 copy_replacements (x, ret);
3030 else
3031 ret = adjust_address (x, new_mode, 0);
3033 else
3035 /* Note that we do want simplify_subreg's behavior of validating
3036 that the new mode is ok for a hard register. If we were to use
3037 simplify_gen_subreg, we would create the subreg, but would
3038 probably run into the target not being able to implement it. */
3039 /* Except, of course, when FORCE is true, when this is exactly what
3040 we want. Which is needed for CCmodes on some targets. */
3041 if (force)
3042 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3043 else
3044 ret = simplify_subreg (new_mode, x, old_mode, 0);
3047 return ret;
3050 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3051 an integer mode of the same size as MODE. Returns the instruction
3052 emitted, or NULL if such a move could not be generated. */
3054 static rtx
3055 emit_move_via_integer (enum machine_mode mode, rtx x, rtx y, bool force)
3057 enum machine_mode imode;
3058 enum insn_code code;
3060 /* There must exist a mode of the exact size we require. */
3061 imode = int_mode_for_mode (mode);
3062 if (imode == BLKmode)
3063 return NULL_RTX;
3065 /* The target must support moves in this mode. */
3066 code = optab_handler (mov_optab, imode);
3067 if (code == CODE_FOR_nothing)
3068 return NULL_RTX;
3070 x = emit_move_change_mode (imode, mode, x, force);
3071 if (x == NULL_RTX)
3072 return NULL_RTX;
3073 y = emit_move_change_mode (imode, mode, y, force);
3074 if (y == NULL_RTX)
3075 return NULL_RTX;
3076 return emit_insn (GEN_FCN (code) (x, y));
3079 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3080 Return an equivalent MEM that does not use an auto-increment. */
3082 static rtx
3083 emit_move_resolve_push (enum machine_mode mode, rtx x)
3085 enum rtx_code code = GET_CODE (XEXP (x, 0));
3086 HOST_WIDE_INT adjust;
3087 rtx temp;
3089 adjust = GET_MODE_SIZE (mode);
3090 #ifdef PUSH_ROUNDING
3091 adjust = PUSH_ROUNDING (adjust);
3092 #endif
3093 if (code == PRE_DEC || code == POST_DEC)
3094 adjust = -adjust;
3095 else if (code == PRE_MODIFY || code == POST_MODIFY)
3097 rtx expr = XEXP (XEXP (x, 0), 1);
3098 HOST_WIDE_INT val;
3100 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3101 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3102 val = INTVAL (XEXP (expr, 1));
3103 if (GET_CODE (expr) == MINUS)
3104 val = -val;
3105 gcc_assert (adjust == val || adjust == -val);
3106 adjust = val;
3109 /* Do not use anti_adjust_stack, since we don't want to update
3110 stack_pointer_delta. */
3111 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3112 GEN_INT (adjust), stack_pointer_rtx,
3113 0, OPTAB_LIB_WIDEN);
3114 if (temp != stack_pointer_rtx)
3115 emit_move_insn (stack_pointer_rtx, temp);
3117 switch (code)
3119 case PRE_INC:
3120 case PRE_DEC:
3121 case PRE_MODIFY:
3122 temp = stack_pointer_rtx;
3123 break;
3124 case POST_INC:
3125 case POST_DEC:
3126 case POST_MODIFY:
3127 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3128 break;
3129 default:
3130 gcc_unreachable ();
3133 return replace_equiv_address (x, temp);
3136 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3137 X is known to satisfy push_operand, and MODE is known to be complex.
3138 Returns the last instruction emitted. */
3141 emit_move_complex_push (enum machine_mode mode, rtx x, rtx y)
3143 enum machine_mode submode = GET_MODE_INNER (mode);
3144 bool imag_first;
3146 #ifdef PUSH_ROUNDING
3147 unsigned int submodesize = GET_MODE_SIZE (submode);
3149 /* In case we output to the stack, but the size is smaller than the
3150 machine can push exactly, we need to use move instructions. */
3151 if (PUSH_ROUNDING (submodesize) != submodesize)
3153 x = emit_move_resolve_push (mode, x);
3154 return emit_move_insn (x, y);
3156 #endif
3158 /* Note that the real part always precedes the imag part in memory
3159 regardless of machine's endianness. */
3160 switch (GET_CODE (XEXP (x, 0)))
3162 case PRE_DEC:
3163 case POST_DEC:
3164 imag_first = true;
3165 break;
3166 case PRE_INC:
3167 case POST_INC:
3168 imag_first = false;
3169 break;
3170 default:
3171 gcc_unreachable ();
3174 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3175 read_complex_part (y, imag_first));
3176 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3177 read_complex_part (y, !imag_first));
3180 /* A subroutine of emit_move_complex. Perform the move from Y to X
3181 via two moves of the parts. Returns the last instruction emitted. */
3184 emit_move_complex_parts (rtx x, rtx y)
3186 /* Show the output dies here. This is necessary for SUBREGs
3187 of pseudos since we cannot track their lifetimes correctly;
3188 hard regs shouldn't appear here except as return values. */
3189 if (!reload_completed && !reload_in_progress
3190 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3191 emit_clobber (x);
3193 write_complex_part (x, read_complex_part (y, false), false);
3194 write_complex_part (x, read_complex_part (y, true), true);
3196 return get_last_insn ();
3199 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3200 MODE is known to be complex. Returns the last instruction emitted. */
3202 static rtx
3203 emit_move_complex (enum machine_mode mode, rtx x, rtx y)
3205 bool try_int;
3207 /* Need to take special care for pushes, to maintain proper ordering
3208 of the data, and possibly extra padding. */
3209 if (push_operand (x, mode))
3210 return emit_move_complex_push (mode, x, y);
3212 /* See if we can coerce the target into moving both values at once. */
3214 /* Move floating point as parts. */
3215 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3216 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing)
3217 try_int = false;
3218 /* Not possible if the values are inherently not adjacent. */
3219 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3220 try_int = false;
3221 /* Is possible if both are registers (or subregs of registers). */
3222 else if (register_operand (x, mode) && register_operand (y, mode))
3223 try_int = true;
3224 /* If one of the operands is a memory, and alignment constraints
3225 are friendly enough, we may be able to do combined memory operations.
3226 We do not attempt this if Y is a constant because that combination is
3227 usually better with the by-parts thing below. */
3228 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3229 && (!STRICT_ALIGNMENT
3230 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3231 try_int = true;
3232 else
3233 try_int = false;
3235 if (try_int)
3237 rtx ret;
3239 /* For memory to memory moves, optimal behavior can be had with the
3240 existing block move logic. */
3241 if (MEM_P (x) && MEM_P (y))
3243 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3244 BLOCK_OP_NO_LIBCALL);
3245 return get_last_insn ();
3248 ret = emit_move_via_integer (mode, x, y, true);
3249 if (ret)
3250 return ret;
3253 return emit_move_complex_parts (x, y);
3256 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3257 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3259 static rtx
3260 emit_move_ccmode (enum machine_mode mode, rtx x, rtx y)
3262 rtx ret;
3264 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3265 if (mode != CCmode)
3267 enum insn_code code = optab_handler (mov_optab, CCmode);
3268 if (code != CODE_FOR_nothing)
3270 x = emit_move_change_mode (CCmode, mode, x, true);
3271 y = emit_move_change_mode (CCmode, mode, y, true);
3272 return emit_insn (GEN_FCN (code) (x, y));
3276 /* Otherwise, find the MODE_INT mode of the same width. */
3277 ret = emit_move_via_integer (mode, x, y, false);
3278 gcc_assert (ret != NULL);
3279 return ret;
3282 /* Return true if word I of OP lies entirely in the
3283 undefined bits of a paradoxical subreg. */
3285 static bool
3286 undefined_operand_subword_p (const_rtx op, int i)
3288 enum machine_mode innermode, innermostmode;
3289 int offset;
3290 if (GET_CODE (op) != SUBREG)
3291 return false;
3292 innermode = GET_MODE (op);
3293 innermostmode = GET_MODE (SUBREG_REG (op));
3294 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3295 /* The SUBREG_BYTE represents offset, as if the value were stored in
3296 memory, except for a paradoxical subreg where we define
3297 SUBREG_BYTE to be 0; undo this exception as in
3298 simplify_subreg. */
3299 if (SUBREG_BYTE (op) == 0
3300 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3302 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3303 if (WORDS_BIG_ENDIAN)
3304 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3305 if (BYTES_BIG_ENDIAN)
3306 offset += difference % UNITS_PER_WORD;
3308 if (offset >= GET_MODE_SIZE (innermostmode)
3309 || offset <= -GET_MODE_SIZE (word_mode))
3310 return true;
3311 return false;
3314 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3315 MODE is any multi-word or full-word mode that lacks a move_insn
3316 pattern. Note that you will get better code if you define such
3317 patterns, even if they must turn into multiple assembler instructions. */
3319 static rtx
3320 emit_move_multi_word (enum machine_mode mode, rtx x, rtx y)
3322 rtx last_insn = 0;
3323 rtx seq, inner;
3324 bool need_clobber;
3325 int i;
3327 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3329 /* If X is a push on the stack, do the push now and replace
3330 X with a reference to the stack pointer. */
3331 if (push_operand (x, mode))
3332 x = emit_move_resolve_push (mode, x);
3334 /* If we are in reload, see if either operand is a MEM whose address
3335 is scheduled for replacement. */
3336 if (reload_in_progress && MEM_P (x)
3337 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3338 x = replace_equiv_address_nv (x, inner);
3339 if (reload_in_progress && MEM_P (y)
3340 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3341 y = replace_equiv_address_nv (y, inner);
3343 start_sequence ();
3345 need_clobber = false;
3346 for (i = 0;
3347 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3348 i++)
3350 rtx xpart = operand_subword (x, i, 1, mode);
3351 rtx ypart;
3353 /* Do not generate code for a move if it would come entirely
3354 from the undefined bits of a paradoxical subreg. */
3355 if (undefined_operand_subword_p (y, i))
3356 continue;
3358 ypart = operand_subword (y, i, 1, mode);
3360 /* If we can't get a part of Y, put Y into memory if it is a
3361 constant. Otherwise, force it into a register. Then we must
3362 be able to get a part of Y. */
3363 if (ypart == 0 && CONSTANT_P (y))
3365 y = use_anchored_address (force_const_mem (mode, y));
3366 ypart = operand_subword (y, i, 1, mode);
3368 else if (ypart == 0)
3369 ypart = operand_subword_force (y, i, mode);
3371 gcc_assert (xpart && ypart);
3373 need_clobber |= (GET_CODE (xpart) == SUBREG);
3375 last_insn = emit_move_insn (xpart, ypart);
3378 seq = get_insns ();
3379 end_sequence ();
3381 /* Show the output dies here. This is necessary for SUBREGs
3382 of pseudos since we cannot track their lifetimes correctly;
3383 hard regs shouldn't appear here except as return values.
3384 We never want to emit such a clobber after reload. */
3385 if (x != y
3386 && ! (reload_in_progress || reload_completed)
3387 && need_clobber != 0)
3388 emit_clobber (x);
3390 emit_insn (seq);
3392 return last_insn;
3395 /* Low level part of emit_move_insn.
3396 Called just like emit_move_insn, but assumes X and Y
3397 are basically valid. */
3400 emit_move_insn_1 (rtx x, rtx y)
3402 enum machine_mode mode = GET_MODE (x);
3403 enum insn_code code;
3405 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3407 code = optab_handler (mov_optab, mode);
3408 if (code != CODE_FOR_nothing)
3409 return emit_insn (GEN_FCN (code) (x, y));
3411 /* Expand complex moves by moving real part and imag part. */
3412 if (COMPLEX_MODE_P (mode))
3413 return emit_move_complex (mode, x, y);
3415 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3416 || ALL_FIXED_POINT_MODE_P (mode))
3418 rtx result = emit_move_via_integer (mode, x, y, true);
3420 /* If we can't find an integer mode, use multi words. */
3421 if (result)
3422 return result;
3423 else
3424 return emit_move_multi_word (mode, x, y);
3427 if (GET_MODE_CLASS (mode) == MODE_CC)
3428 return emit_move_ccmode (mode, x, y);
3430 /* Try using a move pattern for the corresponding integer mode. This is
3431 only safe when simplify_subreg can convert MODE constants into integer
3432 constants. At present, it can only do this reliably if the value
3433 fits within a HOST_WIDE_INT. */
3434 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3436 rtx ret = emit_move_via_integer (mode, x, y, false);
3437 if (ret)
3438 return ret;
3441 return emit_move_multi_word (mode, x, y);
3444 /* Generate code to copy Y into X.
3445 Both Y and X must have the same mode, except that
3446 Y can be a constant with VOIDmode.
3447 This mode cannot be BLKmode; use emit_block_move for that.
3449 Return the last instruction emitted. */
3452 emit_move_insn (rtx x, rtx y)
3454 enum machine_mode mode = GET_MODE (x);
3455 rtx y_cst = NULL_RTX;
3456 rtx last_insn, set;
3458 gcc_assert (mode != BLKmode
3459 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3461 if (CONSTANT_P (y))
3463 if (optimize
3464 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3465 && (last_insn = compress_float_constant (x, y)))
3466 return last_insn;
3468 y_cst = y;
3470 if (!targetm.legitimate_constant_p (mode, y))
3472 y = force_const_mem (mode, y);
3474 /* If the target's cannot_force_const_mem prevented the spill,
3475 assume that the target's move expanders will also take care
3476 of the non-legitimate constant. */
3477 if (!y)
3478 y = y_cst;
3479 else
3480 y = use_anchored_address (y);
3484 /* If X or Y are memory references, verify that their addresses are valid
3485 for the machine. */
3486 if (MEM_P (x)
3487 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3488 MEM_ADDR_SPACE (x))
3489 && ! push_operand (x, GET_MODE (x))))
3490 x = validize_mem (x);
3492 if (MEM_P (y)
3493 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3494 MEM_ADDR_SPACE (y)))
3495 y = validize_mem (y);
3497 gcc_assert (mode != BLKmode);
3499 last_insn = emit_move_insn_1 (x, y);
3501 if (y_cst && REG_P (x)
3502 && (set = single_set (last_insn)) != NULL_RTX
3503 && SET_DEST (set) == x
3504 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3505 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3507 return last_insn;
3510 /* If Y is representable exactly in a narrower mode, and the target can
3511 perform the extension directly from constant or memory, then emit the
3512 move as an extension. */
3514 static rtx
3515 compress_float_constant (rtx x, rtx y)
3517 enum machine_mode dstmode = GET_MODE (x);
3518 enum machine_mode orig_srcmode = GET_MODE (y);
3519 enum machine_mode srcmode;
3520 REAL_VALUE_TYPE r;
3521 int oldcost, newcost;
3522 bool speed = optimize_insn_for_speed_p ();
3524 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3526 if (targetm.legitimate_constant_p (dstmode, y))
3527 oldcost = set_src_cost (y, speed);
3528 else
3529 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3531 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3532 srcmode != orig_srcmode;
3533 srcmode = GET_MODE_WIDER_MODE (srcmode))
3535 enum insn_code ic;
3536 rtx trunc_y, last_insn;
3538 /* Skip if the target can't extend this way. */
3539 ic = can_extend_p (dstmode, srcmode, 0);
3540 if (ic == CODE_FOR_nothing)
3541 continue;
3543 /* Skip if the narrowed value isn't exact. */
3544 if (! exact_real_truncate (srcmode, &r))
3545 continue;
3547 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3549 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3551 /* Skip if the target needs extra instructions to perform
3552 the extension. */
3553 if (!insn_operand_matches (ic, 1, trunc_y))
3554 continue;
3555 /* This is valid, but may not be cheaper than the original. */
3556 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3557 speed);
3558 if (oldcost < newcost)
3559 continue;
3561 else if (float_extend_from_mem[dstmode][srcmode])
3563 trunc_y = force_const_mem (srcmode, trunc_y);
3564 /* This is valid, but may not be cheaper than the original. */
3565 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3566 speed);
3567 if (oldcost < newcost)
3568 continue;
3569 trunc_y = validize_mem (trunc_y);
3571 else
3572 continue;
3574 /* For CSE's benefit, force the compressed constant pool entry
3575 into a new pseudo. This constant may be used in different modes,
3576 and if not, combine will put things back together for us. */
3577 trunc_y = force_reg (srcmode, trunc_y);
3578 emit_unop_insn (ic, x, trunc_y, UNKNOWN);
3579 last_insn = get_last_insn ();
3581 if (REG_P (x))
3582 set_unique_reg_note (last_insn, REG_EQUAL, y);
3584 return last_insn;
3587 return NULL_RTX;
3590 /* Pushing data onto the stack. */
3592 /* Push a block of length SIZE (perhaps variable)
3593 and return an rtx to address the beginning of the block.
3594 The value may be virtual_outgoing_args_rtx.
3596 EXTRA is the number of bytes of padding to push in addition to SIZE.
3597 BELOW nonzero means this padding comes at low addresses;
3598 otherwise, the padding comes at high addresses. */
3601 push_block (rtx size, int extra, int below)
3603 rtx temp;
3605 size = convert_modes (Pmode, ptr_mode, size, 1);
3606 if (CONSTANT_P (size))
3607 anti_adjust_stack (plus_constant (Pmode, size, extra));
3608 else if (REG_P (size) && extra == 0)
3609 anti_adjust_stack (size);
3610 else
3612 temp = copy_to_mode_reg (Pmode, size);
3613 if (extra != 0)
3614 temp = expand_binop (Pmode, add_optab, temp, GEN_INT (extra),
3615 temp, 0, OPTAB_LIB_WIDEN);
3616 anti_adjust_stack (temp);
3619 #ifndef STACK_GROWS_DOWNWARD
3620 if (0)
3621 #else
3622 if (1)
3623 #endif
3625 temp = virtual_outgoing_args_rtx;
3626 if (extra != 0 && below)
3627 temp = plus_constant (Pmode, temp, extra);
3629 else
3631 if (CONST_INT_P (size))
3632 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3633 -INTVAL (size) - (below ? 0 : extra));
3634 else if (extra != 0 && !below)
3635 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3636 negate_rtx (Pmode, plus_constant (Pmode, size,
3637 extra)));
3638 else
3639 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3640 negate_rtx (Pmode, size));
3643 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3646 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3648 static rtx
3649 mem_autoinc_base (rtx mem)
3651 if (MEM_P (mem))
3653 rtx addr = XEXP (mem, 0);
3654 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3655 return XEXP (addr, 0);
3657 return NULL;
3660 /* A utility routine used here, in reload, and in try_split. The insns
3661 after PREV up to and including LAST are known to adjust the stack,
3662 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3663 placing notes as appropriate. PREV may be NULL, indicating the
3664 entire insn sequence prior to LAST should be scanned.
3666 The set of allowed stack pointer modifications is small:
3667 (1) One or more auto-inc style memory references (aka pushes),
3668 (2) One or more addition/subtraction with the SP as destination,
3669 (3) A single move insn with the SP as destination,
3670 (4) A call_pop insn,
3671 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3673 Insns in the sequence that do not modify the SP are ignored,
3674 except for noreturn calls.
3676 The return value is the amount of adjustment that can be trivially
3677 verified, via immediate operand or auto-inc. If the adjustment
3678 cannot be trivially extracted, the return value is INT_MIN. */
3680 HOST_WIDE_INT
3681 find_args_size_adjust (rtx insn)
3683 rtx dest, set, pat;
3684 int i;
3686 pat = PATTERN (insn);
3687 set = NULL;
3689 /* Look for a call_pop pattern. */
3690 if (CALL_P (insn))
3692 /* We have to allow non-call_pop patterns for the case
3693 of emit_single_push_insn of a TLS address. */
3694 if (GET_CODE (pat) != PARALLEL)
3695 return 0;
3697 /* All call_pop have a stack pointer adjust in the parallel.
3698 The call itself is always first, and the stack adjust is
3699 usually last, so search from the end. */
3700 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3702 set = XVECEXP (pat, 0, i);
3703 if (GET_CODE (set) != SET)
3704 continue;
3705 dest = SET_DEST (set);
3706 if (dest == stack_pointer_rtx)
3707 break;
3709 /* We'd better have found the stack pointer adjust. */
3710 if (i == 0)
3711 return 0;
3712 /* Fall through to process the extracted SET and DEST
3713 as if it was a standalone insn. */
3715 else if (GET_CODE (pat) == SET)
3716 set = pat;
3717 else if ((set = single_set (insn)) != NULL)
3719 else if (GET_CODE (pat) == PARALLEL)
3721 /* ??? Some older ports use a parallel with a stack adjust
3722 and a store for a PUSH_ROUNDING pattern, rather than a
3723 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3724 /* ??? See h8300 and m68k, pushqi1. */
3725 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3727 set = XVECEXP (pat, 0, i);
3728 if (GET_CODE (set) != SET)
3729 continue;
3730 dest = SET_DEST (set);
3731 if (dest == stack_pointer_rtx)
3732 break;
3734 /* We do not expect an auto-inc of the sp in the parallel. */
3735 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3736 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3737 != stack_pointer_rtx);
3739 if (i < 0)
3740 return 0;
3742 else
3743 return 0;
3745 dest = SET_DEST (set);
3747 /* Look for direct modifications of the stack pointer. */
3748 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3750 /* Look for a trivial adjustment, otherwise assume nothing. */
3751 /* Note that the SPU restore_stack_block pattern refers to
3752 the stack pointer in V4SImode. Consider that non-trivial. */
3753 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3754 && GET_CODE (SET_SRC (set)) == PLUS
3755 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3756 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3757 return INTVAL (XEXP (SET_SRC (set), 1));
3758 /* ??? Reload can generate no-op moves, which will be cleaned
3759 up later. Recognize it and continue searching. */
3760 else if (rtx_equal_p (dest, SET_SRC (set)))
3761 return 0;
3762 else
3763 return HOST_WIDE_INT_MIN;
3765 else
3767 rtx mem, addr;
3769 /* Otherwise only think about autoinc patterns. */
3770 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3772 mem = dest;
3773 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3774 != stack_pointer_rtx);
3776 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3777 mem = SET_SRC (set);
3778 else
3779 return 0;
3781 addr = XEXP (mem, 0);
3782 switch (GET_CODE (addr))
3784 case PRE_INC:
3785 case POST_INC:
3786 return GET_MODE_SIZE (GET_MODE (mem));
3787 case PRE_DEC:
3788 case POST_DEC:
3789 return -GET_MODE_SIZE (GET_MODE (mem));
3790 case PRE_MODIFY:
3791 case POST_MODIFY:
3792 addr = XEXP (addr, 1);
3793 gcc_assert (GET_CODE (addr) == PLUS);
3794 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3795 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3796 return INTVAL (XEXP (addr, 1));
3797 default:
3798 gcc_unreachable ();
3804 fixup_args_size_notes (rtx prev, rtx last, int end_args_size)
3806 int args_size = end_args_size;
3807 bool saw_unknown = false;
3808 rtx insn;
3810 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3812 HOST_WIDE_INT this_delta;
3814 if (!NONDEBUG_INSN_P (insn))
3815 continue;
3817 this_delta = find_args_size_adjust (insn);
3818 if (this_delta == 0)
3820 if (!CALL_P (insn)
3821 || ACCUMULATE_OUTGOING_ARGS
3822 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3823 continue;
3826 gcc_assert (!saw_unknown);
3827 if (this_delta == HOST_WIDE_INT_MIN)
3828 saw_unknown = true;
3830 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3831 #ifdef STACK_GROWS_DOWNWARD
3832 this_delta = -this_delta;
3833 #endif
3834 args_size -= this_delta;
3837 return saw_unknown ? INT_MIN : args_size;
3840 #ifdef PUSH_ROUNDING
3841 /* Emit single push insn. */
3843 static void
3844 emit_single_push_insn_1 (enum machine_mode mode, rtx x, tree type)
3846 rtx dest_addr;
3847 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3848 rtx dest;
3849 enum insn_code icode;
3851 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3852 /* If there is push pattern, use it. Otherwise try old way of throwing
3853 MEM representing push operation to move expander. */
3854 icode = optab_handler (push_optab, mode);
3855 if (icode != CODE_FOR_nothing)
3857 struct expand_operand ops[1];
3859 create_input_operand (&ops[0], x, mode);
3860 if (maybe_expand_insn (icode, 1, ops))
3861 return;
3863 if (GET_MODE_SIZE (mode) == rounded_size)
3864 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
3865 /* If we are to pad downward, adjust the stack pointer first and
3866 then store X into the stack location using an offset. This is
3867 because emit_move_insn does not know how to pad; it does not have
3868 access to type. */
3869 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
3871 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
3872 HOST_WIDE_INT offset;
3874 emit_move_insn (stack_pointer_rtx,
3875 expand_binop (Pmode,
3876 #ifdef STACK_GROWS_DOWNWARD
3877 sub_optab,
3878 #else
3879 add_optab,
3880 #endif
3881 stack_pointer_rtx,
3882 GEN_INT (rounded_size),
3883 NULL_RTX, 0, OPTAB_LIB_WIDEN));
3885 offset = (HOST_WIDE_INT) padding_size;
3886 #ifdef STACK_GROWS_DOWNWARD
3887 if (STACK_PUSH_CODE == POST_DEC)
3888 /* We have already decremented the stack pointer, so get the
3889 previous value. */
3890 offset += (HOST_WIDE_INT) rounded_size;
3891 #else
3892 if (STACK_PUSH_CODE == POST_INC)
3893 /* We have already incremented the stack pointer, so get the
3894 previous value. */
3895 offset -= (HOST_WIDE_INT) rounded_size;
3896 #endif
3897 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
3899 else
3901 #ifdef STACK_GROWS_DOWNWARD
3902 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
3903 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3904 GEN_INT (-(HOST_WIDE_INT) rounded_size));
3905 #else
3906 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
3907 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3908 GEN_INT (rounded_size));
3909 #endif
3910 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
3913 dest = gen_rtx_MEM (mode, dest_addr);
3915 if (type != 0)
3917 set_mem_attributes (dest, type, 1);
3919 if (flag_optimize_sibling_calls)
3920 /* Function incoming arguments may overlap with sibling call
3921 outgoing arguments and we cannot allow reordering of reads
3922 from function arguments with stores to outgoing arguments
3923 of sibling calls. */
3924 set_mem_alias_set (dest, 0);
3926 emit_move_insn (dest, x);
3929 /* Emit and annotate a single push insn. */
3931 static void
3932 emit_single_push_insn (enum machine_mode mode, rtx x, tree type)
3934 int delta, old_delta = stack_pointer_delta;
3935 rtx prev = get_last_insn ();
3936 rtx last;
3938 emit_single_push_insn_1 (mode, x, type);
3940 last = get_last_insn ();
3942 /* Notice the common case where we emitted exactly one insn. */
3943 if (PREV_INSN (last) == prev)
3945 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
3946 return;
3949 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
3950 gcc_assert (delta == INT_MIN || delta == old_delta);
3952 #endif
3954 /* Generate code to push X onto the stack, assuming it has mode MODE and
3955 type TYPE.
3956 MODE is redundant except when X is a CONST_INT (since they don't
3957 carry mode info).
3958 SIZE is an rtx for the size of data to be copied (in bytes),
3959 needed only if X is BLKmode.
3961 ALIGN (in bits) is maximum alignment we can assume.
3963 If PARTIAL and REG are both nonzero, then copy that many of the first
3964 bytes of X into registers starting with REG, and push the rest of X.
3965 The amount of space pushed is decreased by PARTIAL bytes.
3966 REG must be a hard register in this case.
3967 If REG is zero but PARTIAL is not, take any all others actions for an
3968 argument partially in registers, but do not actually load any
3969 registers.
3971 EXTRA is the amount in bytes of extra space to leave next to this arg.
3972 This is ignored if an argument block has already been allocated.
3974 On a machine that lacks real push insns, ARGS_ADDR is the address of
3975 the bottom of the argument block for this call. We use indexing off there
3976 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
3977 argument block has not been preallocated.
3979 ARGS_SO_FAR is the size of args previously pushed for this call.
3981 REG_PARM_STACK_SPACE is nonzero if functions require stack space
3982 for arguments passed in registers. If nonzero, it will be the number
3983 of bytes required. */
3985 void
3986 emit_push_insn (rtx x, enum machine_mode mode, tree type, rtx size,
3987 unsigned int align, int partial, rtx reg, int extra,
3988 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
3989 rtx alignment_pad)
3991 rtx xinner;
3992 enum direction stack_direction
3993 #ifdef STACK_GROWS_DOWNWARD
3994 = downward;
3995 #else
3996 = upward;
3997 #endif
3999 /* Decide where to pad the argument: `downward' for below,
4000 `upward' for above, or `none' for don't pad it.
4001 Default is below for small data on big-endian machines; else above. */
4002 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4004 /* Invert direction if stack is post-decrement.
4005 FIXME: why? */
4006 if (STACK_PUSH_CODE == POST_DEC)
4007 if (where_pad != none)
4008 where_pad = (where_pad == downward ? upward : downward);
4010 xinner = x;
4012 if (mode == BLKmode
4013 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4015 /* Copy a block into the stack, entirely or partially. */
4017 rtx temp;
4018 int used;
4019 int offset;
4020 int skip;
4022 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4023 used = partial - offset;
4025 if (mode != BLKmode)
4027 /* A value is to be stored in an insufficiently aligned
4028 stack slot; copy via a suitably aligned slot if
4029 necessary. */
4030 size = GEN_INT (GET_MODE_SIZE (mode));
4031 if (!MEM_P (xinner))
4033 temp = assign_temp (type, 0, 1, 1);
4034 emit_move_insn (temp, xinner);
4035 xinner = temp;
4039 gcc_assert (size);
4041 /* USED is now the # of bytes we need not copy to the stack
4042 because registers will take care of them. */
4044 if (partial != 0)
4045 xinner = adjust_address (xinner, BLKmode, used);
4047 /* If the partial register-part of the arg counts in its stack size,
4048 skip the part of stack space corresponding to the registers.
4049 Otherwise, start copying to the beginning of the stack space,
4050 by setting SKIP to 0. */
4051 skip = (reg_parm_stack_space == 0) ? 0 : used;
4053 #ifdef PUSH_ROUNDING
4054 /* Do it with several push insns if that doesn't take lots of insns
4055 and if there is no difficulty with push insns that skip bytes
4056 on the stack for alignment purposes. */
4057 if (args_addr == 0
4058 && PUSH_ARGS
4059 && CONST_INT_P (size)
4060 && skip == 0
4061 && MEM_ALIGN (xinner) >= align
4062 && (MOVE_BY_PIECES_P ((unsigned) INTVAL (size) - used, align))
4063 /* Here we avoid the case of a structure whose weak alignment
4064 forces many pushes of a small amount of data,
4065 and such small pushes do rounding that causes trouble. */
4066 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4067 || align >= BIGGEST_ALIGNMENT
4068 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4069 == (align / BITS_PER_UNIT)))
4070 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4072 /* Push padding now if padding above and stack grows down,
4073 or if padding below and stack grows up.
4074 But if space already allocated, this has already been done. */
4075 if (extra && args_addr == 0
4076 && where_pad != none && where_pad != stack_direction)
4077 anti_adjust_stack (GEN_INT (extra));
4079 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4081 else
4082 #endif /* PUSH_ROUNDING */
4084 rtx target;
4086 /* Otherwise make space on the stack and copy the data
4087 to the address of that space. */
4089 /* Deduct words put into registers from the size we must copy. */
4090 if (partial != 0)
4092 if (CONST_INT_P (size))
4093 size = GEN_INT (INTVAL (size) - used);
4094 else
4095 size = expand_binop (GET_MODE (size), sub_optab, size,
4096 GEN_INT (used), NULL_RTX, 0,
4097 OPTAB_LIB_WIDEN);
4100 /* Get the address of the stack space.
4101 In this case, we do not deal with EXTRA separately.
4102 A single stack adjust will do. */
4103 if (! args_addr)
4105 temp = push_block (size, extra, where_pad == downward);
4106 extra = 0;
4108 else if (CONST_INT_P (args_so_far))
4109 temp = memory_address (BLKmode,
4110 plus_constant (Pmode, args_addr,
4111 skip + INTVAL (args_so_far)));
4112 else
4113 temp = memory_address (BLKmode,
4114 plus_constant (Pmode,
4115 gen_rtx_PLUS (Pmode,
4116 args_addr,
4117 args_so_far),
4118 skip));
4120 if (!ACCUMULATE_OUTGOING_ARGS)
4122 /* If the source is referenced relative to the stack pointer,
4123 copy it to another register to stabilize it. We do not need
4124 to do this if we know that we won't be changing sp. */
4126 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4127 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4128 temp = copy_to_reg (temp);
4131 target = gen_rtx_MEM (BLKmode, temp);
4133 /* We do *not* set_mem_attributes here, because incoming arguments
4134 may overlap with sibling call outgoing arguments and we cannot
4135 allow reordering of reads from function arguments with stores
4136 to outgoing arguments of sibling calls. We do, however, want
4137 to record the alignment of the stack slot. */
4138 /* ALIGN may well be better aligned than TYPE, e.g. due to
4139 PARM_BOUNDARY. Assume the caller isn't lying. */
4140 set_mem_align (target, align);
4142 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4145 else if (partial > 0)
4147 /* Scalar partly in registers. */
4149 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4150 int i;
4151 int not_stack;
4152 /* # bytes of start of argument
4153 that we must make space for but need not store. */
4154 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4155 int args_offset = INTVAL (args_so_far);
4156 int skip;
4158 /* Push padding now if padding above and stack grows down,
4159 or if padding below and stack grows up.
4160 But if space already allocated, this has already been done. */
4161 if (extra && args_addr == 0
4162 && where_pad != none && where_pad != stack_direction)
4163 anti_adjust_stack (GEN_INT (extra));
4165 /* If we make space by pushing it, we might as well push
4166 the real data. Otherwise, we can leave OFFSET nonzero
4167 and leave the space uninitialized. */
4168 if (args_addr == 0)
4169 offset = 0;
4171 /* Now NOT_STACK gets the number of words that we don't need to
4172 allocate on the stack. Convert OFFSET to words too. */
4173 not_stack = (partial - offset) / UNITS_PER_WORD;
4174 offset /= UNITS_PER_WORD;
4176 /* If the partial register-part of the arg counts in its stack size,
4177 skip the part of stack space corresponding to the registers.
4178 Otherwise, start copying to the beginning of the stack space,
4179 by setting SKIP to 0. */
4180 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4182 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4183 x = validize_mem (force_const_mem (mode, x));
4185 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4186 SUBREGs of such registers are not allowed. */
4187 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4188 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4189 x = copy_to_reg (x);
4191 /* Loop over all the words allocated on the stack for this arg. */
4192 /* We can do it by words, because any scalar bigger than a word
4193 has a size a multiple of a word. */
4194 #ifndef PUSH_ARGS_REVERSED
4195 for (i = not_stack; i < size; i++)
4196 #else
4197 for (i = size - 1; i >= not_stack; i--)
4198 #endif
4199 if (i >= not_stack + offset)
4200 emit_push_insn (operand_subword_force (x, i, mode),
4201 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4202 0, args_addr,
4203 GEN_INT (args_offset + ((i - not_stack + skip)
4204 * UNITS_PER_WORD)),
4205 reg_parm_stack_space, alignment_pad);
4207 else
4209 rtx addr;
4210 rtx dest;
4212 /* Push padding now if padding above and stack grows down,
4213 or if padding below and stack grows up.
4214 But if space already allocated, this has already been done. */
4215 if (extra && args_addr == 0
4216 && where_pad != none && where_pad != stack_direction)
4217 anti_adjust_stack (GEN_INT (extra));
4219 #ifdef PUSH_ROUNDING
4220 if (args_addr == 0 && PUSH_ARGS)
4221 emit_single_push_insn (mode, x, type);
4222 else
4223 #endif
4225 if (CONST_INT_P (args_so_far))
4226 addr
4227 = memory_address (mode,
4228 plus_constant (Pmode, args_addr,
4229 INTVAL (args_so_far)));
4230 else
4231 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4232 args_so_far));
4233 dest = gen_rtx_MEM (mode, addr);
4235 /* We do *not* set_mem_attributes here, because incoming arguments
4236 may overlap with sibling call outgoing arguments and we cannot
4237 allow reordering of reads from function arguments with stores
4238 to outgoing arguments of sibling calls. We do, however, want
4239 to record the alignment of the stack slot. */
4240 /* ALIGN may well be better aligned than TYPE, e.g. due to
4241 PARM_BOUNDARY. Assume the caller isn't lying. */
4242 set_mem_align (dest, align);
4244 emit_move_insn (dest, x);
4248 /* If part should go in registers, copy that part
4249 into the appropriate registers. Do this now, at the end,
4250 since mem-to-mem copies above may do function calls. */
4251 if (partial > 0 && reg != 0)
4253 /* Handle calls that pass values in multiple non-contiguous locations.
4254 The Irix 6 ABI has examples of this. */
4255 if (GET_CODE (reg) == PARALLEL)
4256 emit_group_load (reg, x, type, -1);
4257 else
4259 gcc_assert (partial % UNITS_PER_WORD == 0);
4260 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4264 if (extra && args_addr == 0 && where_pad == stack_direction)
4265 anti_adjust_stack (GEN_INT (extra));
4267 if (alignment_pad && args_addr == 0)
4268 anti_adjust_stack (alignment_pad);
4271 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4272 operations. */
4274 static rtx
4275 get_subtarget (rtx x)
4277 return (optimize
4278 || x == 0
4279 /* Only registers can be subtargets. */
4280 || !REG_P (x)
4281 /* Don't use hard regs to avoid extending their life. */
4282 || REGNO (x) < FIRST_PSEUDO_REGISTER
4283 ? 0 : x);
4286 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4287 FIELD is a bitfield. Returns true if the optimization was successful,
4288 and there's nothing else to do. */
4290 static bool
4291 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4292 unsigned HOST_WIDE_INT bitpos,
4293 unsigned HOST_WIDE_INT bitregion_start,
4294 unsigned HOST_WIDE_INT bitregion_end,
4295 enum machine_mode mode1, rtx str_rtx,
4296 tree to, tree src)
4298 enum machine_mode str_mode = GET_MODE (str_rtx);
4299 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4300 tree op0, op1;
4301 rtx value, result;
4302 optab binop;
4303 gimple srcstmt;
4304 enum tree_code code;
4306 if (mode1 != VOIDmode
4307 || bitsize >= BITS_PER_WORD
4308 || str_bitsize > BITS_PER_WORD
4309 || TREE_SIDE_EFFECTS (to)
4310 || TREE_THIS_VOLATILE (to))
4311 return false;
4313 STRIP_NOPS (src);
4314 if (TREE_CODE (src) != SSA_NAME)
4315 return false;
4316 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4317 return false;
4319 srcstmt = get_gimple_for_ssa_name (src);
4320 if (!srcstmt
4321 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4322 return false;
4324 code = gimple_assign_rhs_code (srcstmt);
4326 op0 = gimple_assign_rhs1 (srcstmt);
4328 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4329 to find its initialization. Hopefully the initialization will
4330 be from a bitfield load. */
4331 if (TREE_CODE (op0) == SSA_NAME)
4333 gimple op0stmt = get_gimple_for_ssa_name (op0);
4335 /* We want to eventually have OP0 be the same as TO, which
4336 should be a bitfield. */
4337 if (!op0stmt
4338 || !is_gimple_assign (op0stmt)
4339 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4340 return false;
4341 op0 = gimple_assign_rhs1 (op0stmt);
4344 op1 = gimple_assign_rhs2 (srcstmt);
4346 if (!operand_equal_p (to, op0, 0))
4347 return false;
4349 if (MEM_P (str_rtx))
4351 unsigned HOST_WIDE_INT offset1;
4353 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4354 str_mode = word_mode;
4355 str_mode = get_best_mode (bitsize, bitpos,
4356 bitregion_start, bitregion_end,
4357 MEM_ALIGN (str_rtx), str_mode, 0);
4358 if (str_mode == VOIDmode)
4359 return false;
4360 str_bitsize = GET_MODE_BITSIZE (str_mode);
4362 offset1 = bitpos;
4363 bitpos %= str_bitsize;
4364 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4365 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4367 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4368 return false;
4370 /* If the bit field covers the whole REG/MEM, store_field
4371 will likely generate better code. */
4372 if (bitsize >= str_bitsize)
4373 return false;
4375 /* We can't handle fields split across multiple entities. */
4376 if (bitpos + bitsize > str_bitsize)
4377 return false;
4379 if (BYTES_BIG_ENDIAN)
4380 bitpos = str_bitsize - bitpos - bitsize;
4382 switch (code)
4384 case PLUS_EXPR:
4385 case MINUS_EXPR:
4386 /* For now, just optimize the case of the topmost bitfield
4387 where we don't need to do any masking and also
4388 1 bit bitfields where xor can be used.
4389 We might win by one instruction for the other bitfields
4390 too if insv/extv instructions aren't used, so that
4391 can be added later. */
4392 if (bitpos + bitsize != str_bitsize
4393 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4394 break;
4396 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4397 value = convert_modes (str_mode,
4398 TYPE_MODE (TREE_TYPE (op1)), value,
4399 TYPE_UNSIGNED (TREE_TYPE (op1)));
4401 /* We may be accessing data outside the field, which means
4402 we can alias adjacent data. */
4403 if (MEM_P (str_rtx))
4405 str_rtx = shallow_copy_rtx (str_rtx);
4406 set_mem_alias_set (str_rtx, 0);
4407 set_mem_expr (str_rtx, 0);
4410 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4411 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4413 value = expand_and (str_mode, value, const1_rtx, NULL);
4414 binop = xor_optab;
4416 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4417 result = expand_binop (str_mode, binop, str_rtx,
4418 value, str_rtx, 1, OPTAB_WIDEN);
4419 if (result != str_rtx)
4420 emit_move_insn (str_rtx, result);
4421 return true;
4423 case BIT_IOR_EXPR:
4424 case BIT_XOR_EXPR:
4425 if (TREE_CODE (op1) != INTEGER_CST)
4426 break;
4427 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4428 value = convert_modes (str_mode,
4429 TYPE_MODE (TREE_TYPE (op1)), value,
4430 TYPE_UNSIGNED (TREE_TYPE (op1)));
4432 /* We may be accessing data outside the field, which means
4433 we can alias adjacent data. */
4434 if (MEM_P (str_rtx))
4436 str_rtx = shallow_copy_rtx (str_rtx);
4437 set_mem_alias_set (str_rtx, 0);
4438 set_mem_expr (str_rtx, 0);
4441 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4442 if (bitpos + bitsize != str_bitsize)
4444 rtx mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1);
4445 value = expand_and (str_mode, value, mask, NULL_RTX);
4447 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4448 result = expand_binop (str_mode, binop, str_rtx,
4449 value, str_rtx, 1, OPTAB_WIDEN);
4450 if (result != str_rtx)
4451 emit_move_insn (str_rtx, result);
4452 return true;
4454 default:
4455 break;
4458 return false;
4461 /* In the C++ memory model, consecutive bit fields in a structure are
4462 considered one memory location.
4464 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4465 returns the bit range of consecutive bits in which this COMPONENT_REF
4466 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4467 and *OFFSET may be adjusted in the process.
4469 If the access does not need to be restricted, 0 is returned in both
4470 *BITSTART and *BITEND. */
4472 static void
4473 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4474 unsigned HOST_WIDE_INT *bitend,
4475 tree exp,
4476 HOST_WIDE_INT *bitpos,
4477 tree *offset)
4479 HOST_WIDE_INT bitoffset;
4480 tree field, repr;
4482 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4484 field = TREE_OPERAND (exp, 1);
4485 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4486 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4487 need to limit the range we can access. */
4488 if (!repr)
4490 *bitstart = *bitend = 0;
4491 return;
4494 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4495 part of a larger bit field, then the representative does not serve any
4496 useful purpose. This can occur in Ada. */
4497 if (handled_component_p (TREE_OPERAND (exp, 0)))
4499 enum machine_mode rmode;
4500 HOST_WIDE_INT rbitsize, rbitpos;
4501 tree roffset;
4502 int unsignedp;
4503 int volatilep = 0;
4504 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4505 &roffset, &rmode, &unsignedp, &volatilep, false);
4506 if ((rbitpos % BITS_PER_UNIT) != 0)
4508 *bitstart = *bitend = 0;
4509 return;
4513 /* Compute the adjustment to bitpos from the offset of the field
4514 relative to the representative. DECL_FIELD_OFFSET of field and
4515 repr are the same by construction if they are not constants,
4516 see finish_bitfield_layout. */
4517 if (host_integerp (DECL_FIELD_OFFSET (field), 1)
4518 && host_integerp (DECL_FIELD_OFFSET (repr), 1))
4519 bitoffset = (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
4520 - tree_low_cst (DECL_FIELD_OFFSET (repr), 1)) * BITS_PER_UNIT;
4521 else
4522 bitoffset = 0;
4523 bitoffset += (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
4524 - tree_low_cst (DECL_FIELD_BIT_OFFSET (repr), 1));
4526 /* If the adjustment is larger than bitpos, we would have a negative bit
4527 position for the lower bound and this may wreak havoc later. This can
4528 occur only if we have a non-null offset, so adjust offset and bitpos
4529 to make the lower bound non-negative. */
4530 if (bitoffset > *bitpos)
4532 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4534 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4535 gcc_assert (*offset != NULL_TREE);
4537 *bitpos += adjust;
4538 *offset
4539 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4540 *bitstart = 0;
4542 else
4543 *bitstart = *bitpos - bitoffset;
4545 *bitend = *bitstart + tree_low_cst (DECL_SIZE (repr), 1) - 1;
4548 /* Returns true if the MEM_REF REF refers to an object that does not
4549 reside in memory and has non-BLKmode. */
4551 static bool
4552 mem_ref_refers_to_non_mem_p (tree ref)
4554 tree base = TREE_OPERAND (ref, 0);
4555 if (TREE_CODE (base) != ADDR_EXPR)
4556 return false;
4557 base = TREE_OPERAND (base, 0);
4558 return (DECL_P (base)
4559 && !TREE_ADDRESSABLE (base)
4560 && DECL_MODE (base) != BLKmode
4561 && DECL_RTL_SET_P (base)
4562 && !MEM_P (DECL_RTL (base)));
4565 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4566 is true, try generating a nontemporal store. */
4568 void
4569 expand_assignment (tree to, tree from, bool nontemporal)
4571 rtx to_rtx = 0;
4572 rtx result;
4573 enum machine_mode mode;
4574 unsigned int align;
4575 enum insn_code icode;
4577 /* Don't crash if the lhs of the assignment was erroneous. */
4578 if (TREE_CODE (to) == ERROR_MARK)
4580 expand_normal (from);
4581 return;
4584 /* Optimize away no-op moves without side-effects. */
4585 if (operand_equal_p (to, from, 0))
4586 return;
4588 /* Handle misaligned stores. */
4589 mode = TYPE_MODE (TREE_TYPE (to));
4590 if ((TREE_CODE (to) == MEM_REF
4591 || TREE_CODE (to) == TARGET_MEM_REF)
4592 && mode != BLKmode
4593 && !mem_ref_refers_to_non_mem_p (to)
4594 && ((align = get_object_or_type_alignment (to))
4595 < GET_MODE_ALIGNMENT (mode))
4596 && (((icode = optab_handler (movmisalign_optab, mode))
4597 != CODE_FOR_nothing)
4598 || SLOW_UNALIGNED_ACCESS (mode, align)))
4600 rtx reg, mem;
4602 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4603 reg = force_not_mem (reg);
4604 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4606 if (icode != CODE_FOR_nothing)
4608 struct expand_operand ops[2];
4610 create_fixed_operand (&ops[0], mem);
4611 create_input_operand (&ops[1], reg, mode);
4612 /* The movmisalign<mode> pattern cannot fail, else the assignment
4613 would silently be omitted. */
4614 expand_insn (icode, 2, ops);
4616 else
4617 store_bit_field (mem, GET_MODE_BITSIZE (mode),
4618 0, 0, 0, mode, reg);
4619 return;
4622 /* Assignment of a structure component needs special treatment
4623 if the structure component's rtx is not simply a MEM.
4624 Assignment of an array element at a constant index, and assignment of
4625 an array element in an unaligned packed structure field, has the same
4626 problem. Same for (partially) storing into a non-memory object. */
4627 if (handled_component_p (to)
4628 || (TREE_CODE (to) == MEM_REF
4629 && mem_ref_refers_to_non_mem_p (to))
4630 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4632 enum machine_mode mode1;
4633 HOST_WIDE_INT bitsize, bitpos;
4634 unsigned HOST_WIDE_INT bitregion_start = 0;
4635 unsigned HOST_WIDE_INT bitregion_end = 0;
4636 tree offset;
4637 int unsignedp;
4638 int volatilep = 0;
4639 tree tem;
4640 bool misalignp;
4641 rtx mem = NULL_RTX;
4643 push_temp_slots ();
4644 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4645 &unsignedp, &volatilep, true);
4647 if (TREE_CODE (to) == COMPONENT_REF
4648 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4649 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4651 /* If we are going to use store_bit_field and extract_bit_field,
4652 make sure to_rtx will be safe for multiple use. */
4653 mode = TYPE_MODE (TREE_TYPE (tem));
4654 if (TREE_CODE (tem) == MEM_REF
4655 && mode != BLKmode
4656 && ((align = get_object_or_type_alignment (tem))
4657 < GET_MODE_ALIGNMENT (mode))
4658 && ((icode = optab_handler (movmisalign_optab, mode))
4659 != CODE_FOR_nothing))
4661 struct expand_operand ops[2];
4663 misalignp = true;
4664 to_rtx = gen_reg_rtx (mode);
4665 mem = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4667 /* If the misaligned store doesn't overwrite all bits, perform
4668 rmw cycle on MEM. */
4669 if (bitsize != GET_MODE_BITSIZE (mode))
4671 create_input_operand (&ops[0], to_rtx, mode);
4672 create_fixed_operand (&ops[1], mem);
4673 /* The movmisalign<mode> pattern cannot fail, else the assignment
4674 would silently be omitted. */
4675 expand_insn (icode, 2, ops);
4677 mem = copy_rtx (mem);
4680 else
4682 misalignp = false;
4683 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4686 /* If the bitfield is volatile, we want to access it in the
4687 field's mode, not the computed mode.
4688 If a MEM has VOIDmode (external with incomplete type),
4689 use BLKmode for it instead. */
4690 if (MEM_P (to_rtx))
4692 if (volatilep && flag_strict_volatile_bitfields > 0)
4693 to_rtx = adjust_address (to_rtx, mode1, 0);
4694 else if (GET_MODE (to_rtx) == VOIDmode)
4695 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4698 if (offset != 0)
4700 enum machine_mode address_mode;
4701 rtx offset_rtx;
4703 if (!MEM_P (to_rtx))
4705 /* We can get constant negative offsets into arrays with broken
4706 user code. Translate this to a trap instead of ICEing. */
4707 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4708 expand_builtin_trap ();
4709 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4712 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4713 address_mode = get_address_mode (to_rtx);
4714 if (GET_MODE (offset_rtx) != address_mode)
4715 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4717 /* A constant address in TO_RTX can have VOIDmode, we must not try
4718 to call force_reg for that case. Avoid that case. */
4719 if (MEM_P (to_rtx)
4720 && GET_MODE (to_rtx) == BLKmode
4721 && GET_MODE (XEXP (to_rtx, 0)) != VOIDmode
4722 && bitsize > 0
4723 && (bitpos % bitsize) == 0
4724 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4725 && MEM_ALIGN (to_rtx) == GET_MODE_ALIGNMENT (mode1))
4727 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4728 bitpos = 0;
4731 to_rtx = offset_address (to_rtx, offset_rtx,
4732 highest_pow2_factor_for_target (to,
4733 offset));
4736 /* No action is needed if the target is not a memory and the field
4737 lies completely outside that target. This can occur if the source
4738 code contains an out-of-bounds access to a small array. */
4739 if (!MEM_P (to_rtx)
4740 && GET_MODE (to_rtx) != BLKmode
4741 && (unsigned HOST_WIDE_INT) bitpos
4742 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4744 expand_normal (from);
4745 result = NULL;
4747 /* Handle expand_expr of a complex value returning a CONCAT. */
4748 else if (GET_CODE (to_rtx) == CONCAT)
4750 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4751 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4752 && bitpos == 0
4753 && bitsize == mode_bitsize)
4754 result = store_expr (from, to_rtx, false, nontemporal);
4755 else if (bitsize == mode_bitsize / 2
4756 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4757 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4758 nontemporal);
4759 else if (bitpos + bitsize <= mode_bitsize / 2)
4760 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4761 bitregion_start, bitregion_end,
4762 mode1, from, TREE_TYPE (tem),
4763 get_alias_set (to), nontemporal);
4764 else if (bitpos >= mode_bitsize / 2)
4765 result = store_field (XEXP (to_rtx, 1), bitsize,
4766 bitpos - mode_bitsize / 2,
4767 bitregion_start, bitregion_end,
4768 mode1, from,
4769 TREE_TYPE (tem), get_alias_set (to),
4770 nontemporal);
4771 else if (bitpos == 0 && bitsize == mode_bitsize)
4773 rtx from_rtx;
4774 result = expand_normal (from);
4775 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4776 TYPE_MODE (TREE_TYPE (from)), 0);
4777 emit_move_insn (XEXP (to_rtx, 0),
4778 read_complex_part (from_rtx, false));
4779 emit_move_insn (XEXP (to_rtx, 1),
4780 read_complex_part (from_rtx, true));
4782 else
4784 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4785 GET_MODE_SIZE (GET_MODE (to_rtx)),
4787 write_complex_part (temp, XEXP (to_rtx, 0), false);
4788 write_complex_part (temp, XEXP (to_rtx, 1), true);
4789 result = store_field (temp, bitsize, bitpos,
4790 bitregion_start, bitregion_end,
4791 mode1, from,
4792 TREE_TYPE (tem), get_alias_set (to),
4793 nontemporal);
4794 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4795 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4798 else
4800 if (MEM_P (to_rtx))
4802 /* If the field is at offset zero, we could have been given the
4803 DECL_RTX of the parent struct. Don't munge it. */
4804 to_rtx = shallow_copy_rtx (to_rtx);
4806 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4808 /* Deal with volatile and readonly fields. The former is only
4809 done for MEM. Also set MEM_KEEP_ALIAS_SET_P if needed. */
4810 if (volatilep)
4811 MEM_VOLATILE_P (to_rtx) = 1;
4812 if (component_uses_parent_alias_set (to))
4813 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
4816 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4817 bitregion_start, bitregion_end,
4818 mode1,
4819 to_rtx, to, from))
4820 result = NULL;
4821 else
4822 result = store_field (to_rtx, bitsize, bitpos,
4823 bitregion_start, bitregion_end,
4824 mode1, from,
4825 TREE_TYPE (tem), get_alias_set (to),
4826 nontemporal);
4829 if (misalignp)
4831 struct expand_operand ops[2];
4833 create_fixed_operand (&ops[0], mem);
4834 create_input_operand (&ops[1], to_rtx, mode);
4835 /* The movmisalign<mode> pattern cannot fail, else the assignment
4836 would silently be omitted. */
4837 expand_insn (icode, 2, ops);
4840 if (result)
4841 preserve_temp_slots (result);
4842 free_temp_slots ();
4843 pop_temp_slots ();
4844 return;
4847 /* If the rhs is a function call and its value is not an aggregate,
4848 call the function before we start to compute the lhs.
4849 This is needed for correct code for cases such as
4850 val = setjmp (buf) on machines where reference to val
4851 requires loading up part of an address in a separate insn.
4853 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4854 since it might be a promoted variable where the zero- or sign- extension
4855 needs to be done. Handling this in the normal way is safe because no
4856 computation is done before the call. The same is true for SSA names. */
4857 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
4858 && COMPLETE_TYPE_P (TREE_TYPE (from))
4859 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
4860 && ! (((TREE_CODE (to) == VAR_DECL
4861 || TREE_CODE (to) == PARM_DECL
4862 || TREE_CODE (to) == RESULT_DECL)
4863 && REG_P (DECL_RTL (to)))
4864 || TREE_CODE (to) == SSA_NAME))
4866 rtx value;
4868 push_temp_slots ();
4869 value = expand_normal (from);
4870 if (to_rtx == 0)
4871 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4873 /* Handle calls that return values in multiple non-contiguous locations.
4874 The Irix 6 ABI has examples of this. */
4875 if (GET_CODE (to_rtx) == PARALLEL)
4876 emit_group_load (to_rtx, value, TREE_TYPE (from),
4877 int_size_in_bytes (TREE_TYPE (from)));
4878 else if (GET_MODE (to_rtx) == BLKmode)
4879 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
4880 else
4882 if (POINTER_TYPE_P (TREE_TYPE (to)))
4883 value = convert_memory_address_addr_space
4884 (GET_MODE (to_rtx), value,
4885 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
4887 emit_move_insn (to_rtx, value);
4889 preserve_temp_slots (to_rtx);
4890 free_temp_slots ();
4891 pop_temp_slots ();
4892 return;
4895 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
4896 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4898 /* Don't move directly into a return register. */
4899 if (TREE_CODE (to) == RESULT_DECL
4900 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
4902 rtx temp;
4904 push_temp_slots ();
4905 if (REG_P (to_rtx) && TYPE_MODE (TREE_TYPE (from)) == BLKmode)
4906 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
4907 else
4908 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
4910 if (GET_CODE (to_rtx) == PARALLEL)
4911 emit_group_load (to_rtx, temp, TREE_TYPE (from),
4912 int_size_in_bytes (TREE_TYPE (from)));
4913 else if (temp)
4914 emit_move_insn (to_rtx, temp);
4916 preserve_temp_slots (to_rtx);
4917 free_temp_slots ();
4918 pop_temp_slots ();
4919 return;
4922 /* In case we are returning the contents of an object which overlaps
4923 the place the value is being stored, use a safe function when copying
4924 a value through a pointer into a structure value return block. */
4925 if (TREE_CODE (to) == RESULT_DECL
4926 && TREE_CODE (from) == INDIRECT_REF
4927 && ADDR_SPACE_GENERIC_P
4928 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
4929 && refs_may_alias_p (to, from)
4930 && cfun->returns_struct
4931 && !cfun->returns_pcc_struct)
4933 rtx from_rtx, size;
4935 push_temp_slots ();
4936 size = expr_size (from);
4937 from_rtx = expand_normal (from);
4939 emit_library_call (memmove_libfunc, LCT_NORMAL,
4940 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
4941 XEXP (from_rtx, 0), Pmode,
4942 convert_to_mode (TYPE_MODE (sizetype),
4943 size, TYPE_UNSIGNED (sizetype)),
4944 TYPE_MODE (sizetype));
4946 preserve_temp_slots (to_rtx);
4947 free_temp_slots ();
4948 pop_temp_slots ();
4949 return;
4952 /* Compute FROM and store the value in the rtx we got. */
4954 push_temp_slots ();
4955 result = store_expr (from, to_rtx, 0, nontemporal);
4956 preserve_temp_slots (result);
4957 free_temp_slots ();
4958 pop_temp_slots ();
4959 return;
4962 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
4963 succeeded, false otherwise. */
4965 bool
4966 emit_storent_insn (rtx to, rtx from)
4968 struct expand_operand ops[2];
4969 enum machine_mode mode = GET_MODE (to);
4970 enum insn_code code = optab_handler (storent_optab, mode);
4972 if (code == CODE_FOR_nothing)
4973 return false;
4975 create_fixed_operand (&ops[0], to);
4976 create_input_operand (&ops[1], from, mode);
4977 return maybe_expand_insn (code, 2, ops);
4980 /* Generate code for computing expression EXP,
4981 and storing the value into TARGET.
4983 If the mode is BLKmode then we may return TARGET itself.
4984 It turns out that in BLKmode it doesn't cause a problem.
4985 because C has no operators that could combine two different
4986 assignments into the same BLKmode object with different values
4987 with no sequence point. Will other languages need this to
4988 be more thorough?
4990 If CALL_PARAM_P is nonzero, this is a store into a call param on the
4991 stack, and block moves may need to be treated specially.
4993 If NONTEMPORAL is true, try using a nontemporal store instruction. */
4996 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
4998 rtx temp;
4999 rtx alt_rtl = NULL_RTX;
5000 location_t loc = EXPR_LOCATION (exp);
5002 if (VOID_TYPE_P (TREE_TYPE (exp)))
5004 /* C++ can generate ?: expressions with a throw expression in one
5005 branch and an rvalue in the other. Here, we resolve attempts to
5006 store the throw expression's nonexistent result. */
5007 gcc_assert (!call_param_p);
5008 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5009 return NULL_RTX;
5011 if (TREE_CODE (exp) == COMPOUND_EXPR)
5013 /* Perform first part of compound expression, then assign from second
5014 part. */
5015 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5016 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5017 return store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5018 nontemporal);
5020 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5022 /* For conditional expression, get safe form of the target. Then
5023 test the condition, doing the appropriate assignment on either
5024 side. This avoids the creation of unnecessary temporaries.
5025 For non-BLKmode, it is more efficient not to do this. */
5027 rtx lab1 = gen_label_rtx (), lab2 = gen_label_rtx ();
5029 do_pending_stack_adjust ();
5030 NO_DEFER_POP;
5031 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5032 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5033 nontemporal);
5034 emit_jump_insn (gen_jump (lab2));
5035 emit_barrier ();
5036 emit_label (lab1);
5037 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5038 nontemporal);
5039 emit_label (lab2);
5040 OK_DEFER_POP;
5042 return NULL_RTX;
5044 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5045 /* If this is a scalar in a register that is stored in a wider mode
5046 than the declared mode, compute the result into its declared mode
5047 and then convert to the wider mode. Our value is the computed
5048 expression. */
5050 rtx inner_target = 0;
5052 /* We can do the conversion inside EXP, which will often result
5053 in some optimizations. Do the conversion in two steps: first
5054 change the signedness, if needed, then the extend. But don't
5055 do this if the type of EXP is a subtype of something else
5056 since then the conversion might involve more than just
5057 converting modes. */
5058 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5059 && TREE_TYPE (TREE_TYPE (exp)) == 0
5060 && GET_MODE_PRECISION (GET_MODE (target))
5061 == TYPE_PRECISION (TREE_TYPE (exp)))
5063 if (TYPE_UNSIGNED (TREE_TYPE (exp))
5064 != SUBREG_PROMOTED_UNSIGNED_P (target))
5066 /* Some types, e.g. Fortran's logical*4, won't have a signed
5067 version, so use the mode instead. */
5068 tree ntype
5069 = (signed_or_unsigned_type_for
5070 (SUBREG_PROMOTED_UNSIGNED_P (target), TREE_TYPE (exp)));
5071 if (ntype == NULL)
5072 ntype = lang_hooks.types.type_for_mode
5073 (TYPE_MODE (TREE_TYPE (exp)),
5074 SUBREG_PROMOTED_UNSIGNED_P (target));
5076 exp = fold_convert_loc (loc, ntype, exp);
5079 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5080 (GET_MODE (SUBREG_REG (target)),
5081 SUBREG_PROMOTED_UNSIGNED_P (target)),
5082 exp);
5084 inner_target = SUBREG_REG (target);
5087 temp = expand_expr (exp, inner_target, VOIDmode,
5088 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5090 /* If TEMP is a VOIDmode constant, use convert_modes to make
5091 sure that we properly convert it. */
5092 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5094 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5095 temp, SUBREG_PROMOTED_UNSIGNED_P (target));
5096 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5097 GET_MODE (target), temp,
5098 SUBREG_PROMOTED_UNSIGNED_P (target));
5101 convert_move (SUBREG_REG (target), temp,
5102 SUBREG_PROMOTED_UNSIGNED_P (target));
5104 return NULL_RTX;
5106 else if ((TREE_CODE (exp) == STRING_CST
5107 || (TREE_CODE (exp) == MEM_REF
5108 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5109 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5110 == STRING_CST
5111 && integer_zerop (TREE_OPERAND (exp, 1))))
5112 && !nontemporal && !call_param_p
5113 && MEM_P (target))
5115 /* Optimize initialization of an array with a STRING_CST. */
5116 HOST_WIDE_INT exp_len, str_copy_len;
5117 rtx dest_mem;
5118 tree str = TREE_CODE (exp) == STRING_CST
5119 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5121 exp_len = int_expr_size (exp);
5122 if (exp_len <= 0)
5123 goto normal_expr;
5125 if (TREE_STRING_LENGTH (str) <= 0)
5126 goto normal_expr;
5128 str_copy_len = strlen (TREE_STRING_POINTER (str));
5129 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5130 goto normal_expr;
5132 str_copy_len = TREE_STRING_LENGTH (str);
5133 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5134 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5136 str_copy_len += STORE_MAX_PIECES - 1;
5137 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5139 str_copy_len = MIN (str_copy_len, exp_len);
5140 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5141 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5142 MEM_ALIGN (target), false))
5143 goto normal_expr;
5145 dest_mem = target;
5147 dest_mem = store_by_pieces (dest_mem,
5148 str_copy_len, builtin_strncpy_read_str,
5149 CONST_CAST (char *,
5150 TREE_STRING_POINTER (str)),
5151 MEM_ALIGN (target), false,
5152 exp_len > str_copy_len ? 1 : 0);
5153 if (exp_len > str_copy_len)
5154 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5155 GEN_INT (exp_len - str_copy_len),
5156 BLOCK_OP_NORMAL);
5157 return NULL_RTX;
5159 else
5161 rtx tmp_target;
5163 normal_expr:
5164 /* If we want to use a nontemporal store, force the value to
5165 register first. */
5166 tmp_target = nontemporal ? NULL_RTX : target;
5167 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5168 (call_param_p
5169 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5170 &alt_rtl);
5173 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5174 the same as that of TARGET, adjust the constant. This is needed, for
5175 example, in case it is a CONST_DOUBLE and we want only a word-sized
5176 value. */
5177 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5178 && TREE_CODE (exp) != ERROR_MARK
5179 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5180 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5181 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5183 /* If value was not generated in the target, store it there.
5184 Convert the value to TARGET's type first if necessary and emit the
5185 pending incrementations that have been queued when expanding EXP.
5186 Note that we cannot emit the whole queue blindly because this will
5187 effectively disable the POST_INC optimization later.
5189 If TEMP and TARGET compare equal according to rtx_equal_p, but
5190 one or both of them are volatile memory refs, we have to distinguish
5191 two cases:
5192 - expand_expr has used TARGET. In this case, we must not generate
5193 another copy. This can be detected by TARGET being equal according
5194 to == .
5195 - expand_expr has not used TARGET - that means that the source just
5196 happens to have the same RTX form. Since temp will have been created
5197 by expand_expr, it will compare unequal according to == .
5198 We must generate a copy in this case, to reach the correct number
5199 of volatile memory references. */
5201 if ((! rtx_equal_p (temp, target)
5202 || (temp != target && (side_effects_p (temp)
5203 || side_effects_p (target))))
5204 && TREE_CODE (exp) != ERROR_MARK
5205 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5206 but TARGET is not valid memory reference, TEMP will differ
5207 from TARGET although it is really the same location. */
5208 && !(alt_rtl
5209 && rtx_equal_p (alt_rtl, target)
5210 && !side_effects_p (alt_rtl)
5211 && !side_effects_p (target))
5212 /* If there's nothing to copy, don't bother. Don't call
5213 expr_size unless necessary, because some front-ends (C++)
5214 expr_size-hook must not be given objects that are not
5215 supposed to be bit-copied or bit-initialized. */
5216 && expr_size (exp) != const0_rtx)
5218 if (GET_MODE (temp) != GET_MODE (target)
5219 && GET_MODE (temp) != VOIDmode)
5221 int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
5222 if (GET_MODE (target) == BLKmode
5223 && GET_MODE (temp) == BLKmode)
5224 emit_block_move (target, temp, expr_size (exp),
5225 (call_param_p
5226 ? BLOCK_OP_CALL_PARM
5227 : BLOCK_OP_NORMAL));
5228 else if (GET_MODE (target) == BLKmode)
5229 store_bit_field (target, INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5230 0, 0, 0, GET_MODE (temp), temp);
5231 else
5232 convert_move (target, temp, unsignedp);
5235 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5237 /* Handle copying a string constant into an array. The string
5238 constant may be shorter than the array. So copy just the string's
5239 actual length, and clear the rest. First get the size of the data
5240 type of the string, which is actually the size of the target. */
5241 rtx size = expr_size (exp);
5243 if (CONST_INT_P (size)
5244 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5245 emit_block_move (target, temp, size,
5246 (call_param_p
5247 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5248 else
5250 enum machine_mode pointer_mode
5251 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5252 enum machine_mode address_mode = get_address_mode (target);
5254 /* Compute the size of the data to copy from the string. */
5255 tree copy_size
5256 = size_binop_loc (loc, MIN_EXPR,
5257 make_tree (sizetype, size),
5258 size_int (TREE_STRING_LENGTH (exp)));
5259 rtx copy_size_rtx
5260 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5261 (call_param_p
5262 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5263 rtx label = 0;
5265 /* Copy that much. */
5266 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5267 TYPE_UNSIGNED (sizetype));
5268 emit_block_move (target, temp, copy_size_rtx,
5269 (call_param_p
5270 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5272 /* Figure out how much is left in TARGET that we have to clear.
5273 Do all calculations in pointer_mode. */
5274 if (CONST_INT_P (copy_size_rtx))
5276 size = plus_constant (address_mode, size,
5277 -INTVAL (copy_size_rtx));
5278 target = adjust_address (target, BLKmode,
5279 INTVAL (copy_size_rtx));
5281 else
5283 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5284 copy_size_rtx, NULL_RTX, 0,
5285 OPTAB_LIB_WIDEN);
5287 if (GET_MODE (copy_size_rtx) != address_mode)
5288 copy_size_rtx = convert_to_mode (address_mode,
5289 copy_size_rtx,
5290 TYPE_UNSIGNED (sizetype));
5292 target = offset_address (target, copy_size_rtx,
5293 highest_pow2_factor (copy_size));
5294 label = gen_label_rtx ();
5295 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5296 GET_MODE (size), 0, label);
5299 if (size != const0_rtx)
5300 clear_storage (target, size, BLOCK_OP_NORMAL);
5302 if (label)
5303 emit_label (label);
5306 /* Handle calls that return values in multiple non-contiguous locations.
5307 The Irix 6 ABI has examples of this. */
5308 else if (GET_CODE (target) == PARALLEL)
5309 emit_group_load (target, temp, TREE_TYPE (exp),
5310 int_size_in_bytes (TREE_TYPE (exp)));
5311 else if (GET_MODE (temp) == BLKmode)
5312 emit_block_move (target, temp, expr_size (exp),
5313 (call_param_p
5314 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5315 else if (nontemporal
5316 && emit_storent_insn (target, temp))
5317 /* If we managed to emit a nontemporal store, there is nothing else to
5318 do. */
5320 else
5322 temp = force_operand (temp, target);
5323 if (temp != target)
5324 emit_move_insn (target, temp);
5328 return NULL_RTX;
5331 /* Return true if field F of structure TYPE is a flexible array. */
5333 static bool
5334 flexible_array_member_p (const_tree f, const_tree type)
5336 const_tree tf;
5338 tf = TREE_TYPE (f);
5339 return (DECL_CHAIN (f) == NULL
5340 && TREE_CODE (tf) == ARRAY_TYPE
5341 && TYPE_DOMAIN (tf)
5342 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5343 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5344 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5345 && int_size_in_bytes (type) >= 0);
5348 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5349 must have in order for it to completely initialize a value of type TYPE.
5350 Return -1 if the number isn't known.
5352 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5354 static HOST_WIDE_INT
5355 count_type_elements (const_tree type, bool for_ctor_p)
5357 switch (TREE_CODE (type))
5359 case ARRAY_TYPE:
5361 tree nelts;
5363 nelts = array_type_nelts (type);
5364 if (nelts && host_integerp (nelts, 1))
5366 unsigned HOST_WIDE_INT n;
5368 n = tree_low_cst (nelts, 1) + 1;
5369 if (n == 0 || for_ctor_p)
5370 return n;
5371 else
5372 return n * count_type_elements (TREE_TYPE (type), false);
5374 return for_ctor_p ? -1 : 1;
5377 case RECORD_TYPE:
5379 unsigned HOST_WIDE_INT n;
5380 tree f;
5382 n = 0;
5383 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5384 if (TREE_CODE (f) == FIELD_DECL)
5386 if (!for_ctor_p)
5387 n += count_type_elements (TREE_TYPE (f), false);
5388 else if (!flexible_array_member_p (f, type))
5389 /* Don't count flexible arrays, which are not supposed
5390 to be initialized. */
5391 n += 1;
5394 return n;
5397 case UNION_TYPE:
5398 case QUAL_UNION_TYPE:
5400 tree f;
5401 HOST_WIDE_INT n, m;
5403 gcc_assert (!for_ctor_p);
5404 /* Estimate the number of scalars in each field and pick the
5405 maximum. Other estimates would do instead; the idea is simply
5406 to make sure that the estimate is not sensitive to the ordering
5407 of the fields. */
5408 n = 1;
5409 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5410 if (TREE_CODE (f) == FIELD_DECL)
5412 m = count_type_elements (TREE_TYPE (f), false);
5413 /* If the field doesn't span the whole union, add an extra
5414 scalar for the rest. */
5415 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5416 TYPE_SIZE (type)) != 1)
5417 m++;
5418 if (n < m)
5419 n = m;
5421 return n;
5424 case COMPLEX_TYPE:
5425 return 2;
5427 case VECTOR_TYPE:
5428 return TYPE_VECTOR_SUBPARTS (type);
5430 case INTEGER_TYPE:
5431 case REAL_TYPE:
5432 case FIXED_POINT_TYPE:
5433 case ENUMERAL_TYPE:
5434 case BOOLEAN_TYPE:
5435 case POINTER_TYPE:
5436 case OFFSET_TYPE:
5437 case REFERENCE_TYPE:
5438 case NULLPTR_TYPE:
5439 return 1;
5441 case ERROR_MARK:
5442 return 0;
5444 case VOID_TYPE:
5445 case METHOD_TYPE:
5446 case FUNCTION_TYPE:
5447 case LANG_TYPE:
5448 default:
5449 gcc_unreachable ();
5453 /* Helper for categorize_ctor_elements. Identical interface. */
5455 static bool
5456 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5457 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5459 unsigned HOST_WIDE_INT idx;
5460 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5461 tree value, purpose, elt_type;
5463 /* Whether CTOR is a valid constant initializer, in accordance with what
5464 initializer_constant_valid_p does. If inferred from the constructor
5465 elements, true until proven otherwise. */
5466 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5467 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5469 nz_elts = 0;
5470 init_elts = 0;
5471 num_fields = 0;
5472 elt_type = NULL_TREE;
5474 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5476 HOST_WIDE_INT mult = 1;
5478 if (TREE_CODE (purpose) == RANGE_EXPR)
5480 tree lo_index = TREE_OPERAND (purpose, 0);
5481 tree hi_index = TREE_OPERAND (purpose, 1);
5483 if (host_integerp (lo_index, 1) && host_integerp (hi_index, 1))
5484 mult = (tree_low_cst (hi_index, 1)
5485 - tree_low_cst (lo_index, 1) + 1);
5487 num_fields += mult;
5488 elt_type = TREE_TYPE (value);
5490 switch (TREE_CODE (value))
5492 case CONSTRUCTOR:
5494 HOST_WIDE_INT nz = 0, ic = 0;
5496 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5497 p_complete);
5499 nz_elts += mult * nz;
5500 init_elts += mult * ic;
5502 if (const_from_elts_p && const_p)
5503 const_p = const_elt_p;
5505 break;
5507 case INTEGER_CST:
5508 case REAL_CST:
5509 case FIXED_CST:
5510 if (!initializer_zerop (value))
5511 nz_elts += mult;
5512 init_elts += mult;
5513 break;
5515 case STRING_CST:
5516 nz_elts += mult * TREE_STRING_LENGTH (value);
5517 init_elts += mult * TREE_STRING_LENGTH (value);
5518 break;
5520 case COMPLEX_CST:
5521 if (!initializer_zerop (TREE_REALPART (value)))
5522 nz_elts += mult;
5523 if (!initializer_zerop (TREE_IMAGPART (value)))
5524 nz_elts += mult;
5525 init_elts += mult;
5526 break;
5528 case VECTOR_CST:
5530 unsigned i;
5531 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5533 tree v = VECTOR_CST_ELT (value, i);
5534 if (!initializer_zerop (v))
5535 nz_elts += mult;
5536 init_elts += mult;
5539 break;
5541 default:
5543 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5544 nz_elts += mult * tc;
5545 init_elts += mult * tc;
5547 if (const_from_elts_p && const_p)
5548 const_p = initializer_constant_valid_p (value, elt_type)
5549 != NULL_TREE;
5551 break;
5555 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5556 num_fields, elt_type))
5557 *p_complete = false;
5559 *p_nz_elts += nz_elts;
5560 *p_init_elts += init_elts;
5562 return const_p;
5565 /* Examine CTOR to discover:
5566 * how many scalar fields are set to nonzero values,
5567 and place it in *P_NZ_ELTS;
5568 * how many scalar fields in total are in CTOR,
5569 and place it in *P_ELT_COUNT.
5570 * whether the constructor is complete -- in the sense that every
5571 meaningful byte is explicitly given a value --
5572 and place it in *P_COMPLETE.
5574 Return whether or not CTOR is a valid static constant initializer, the same
5575 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5577 bool
5578 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5579 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5581 *p_nz_elts = 0;
5582 *p_init_elts = 0;
5583 *p_complete = true;
5585 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5588 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5589 of which had type LAST_TYPE. Each element was itself a complete
5590 initializer, in the sense that every meaningful byte was explicitly
5591 given a value. Return true if the same is true for the constructor
5592 as a whole. */
5594 bool
5595 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5596 const_tree last_type)
5598 if (TREE_CODE (type) == UNION_TYPE
5599 || TREE_CODE (type) == QUAL_UNION_TYPE)
5601 if (num_elts == 0)
5602 return false;
5604 gcc_assert (num_elts == 1 && last_type);
5606 /* ??? We could look at each element of the union, and find the
5607 largest element. Which would avoid comparing the size of the
5608 initialized element against any tail padding in the union.
5609 Doesn't seem worth the effort... */
5610 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5613 return count_type_elements (type, true) == num_elts;
5616 /* Return 1 if EXP contains mostly (3/4) zeros. */
5618 static int
5619 mostly_zeros_p (const_tree exp)
5621 if (TREE_CODE (exp) == CONSTRUCTOR)
5623 HOST_WIDE_INT nz_elts, init_elts;
5624 bool complete_p;
5626 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5627 return !complete_p || nz_elts < init_elts / 4;
5630 return initializer_zerop (exp);
5633 /* Return 1 if EXP contains all zeros. */
5635 static int
5636 all_zeros_p (const_tree exp)
5638 if (TREE_CODE (exp) == CONSTRUCTOR)
5640 HOST_WIDE_INT nz_elts, init_elts;
5641 bool complete_p;
5643 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5644 return nz_elts == 0;
5647 return initializer_zerop (exp);
5650 /* Helper function for store_constructor.
5651 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5652 TYPE is the type of the CONSTRUCTOR, not the element type.
5653 CLEARED is as for store_constructor.
5654 ALIAS_SET is the alias set to use for any stores.
5656 This provides a recursive shortcut back to store_constructor when it isn't
5657 necessary to go through store_field. This is so that we can pass through
5658 the cleared field to let store_constructor know that we may not have to
5659 clear a substructure if the outer structure has already been cleared. */
5661 static void
5662 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5663 HOST_WIDE_INT bitpos, enum machine_mode mode,
5664 tree exp, tree type, int cleared,
5665 alias_set_type alias_set)
5667 if (TREE_CODE (exp) == CONSTRUCTOR
5668 /* We can only call store_constructor recursively if the size and
5669 bit position are on a byte boundary. */
5670 && bitpos % BITS_PER_UNIT == 0
5671 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5672 /* If we have a nonzero bitpos for a register target, then we just
5673 let store_field do the bitfield handling. This is unlikely to
5674 generate unnecessary clear instructions anyways. */
5675 && (bitpos == 0 || MEM_P (target)))
5677 if (MEM_P (target))
5678 target
5679 = adjust_address (target,
5680 GET_MODE (target) == BLKmode
5681 || 0 != (bitpos
5682 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5683 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5686 /* Update the alias set, if required. */
5687 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5688 && MEM_ALIAS_SET (target) != 0)
5690 target = copy_rtx (target);
5691 set_mem_alias_set (target, alias_set);
5694 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5696 else
5697 store_field (target, bitsize, bitpos, 0, 0, mode, exp, type, alias_set,
5698 false);
5701 /* Store the value of constructor EXP into the rtx TARGET.
5702 TARGET is either a REG or a MEM; we know it cannot conflict, since
5703 safe_from_p has been called.
5704 CLEARED is true if TARGET is known to have been zero'd.
5705 SIZE is the number of bytes of TARGET we are allowed to modify: this
5706 may not be the same as the size of EXP if we are assigning to a field
5707 which has been packed to exclude padding bits. */
5709 static void
5710 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5712 tree type = TREE_TYPE (exp);
5713 #ifdef WORD_REGISTER_OPERATIONS
5714 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5715 #endif
5717 switch (TREE_CODE (type))
5719 case RECORD_TYPE:
5720 case UNION_TYPE:
5721 case QUAL_UNION_TYPE:
5723 unsigned HOST_WIDE_INT idx;
5724 tree field, value;
5726 /* If size is zero or the target is already cleared, do nothing. */
5727 if (size == 0 || cleared)
5728 cleared = 1;
5729 /* We either clear the aggregate or indicate the value is dead. */
5730 else if ((TREE_CODE (type) == UNION_TYPE
5731 || TREE_CODE (type) == QUAL_UNION_TYPE)
5732 && ! CONSTRUCTOR_ELTS (exp))
5733 /* If the constructor is empty, clear the union. */
5735 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5736 cleared = 1;
5739 /* If we are building a static constructor into a register,
5740 set the initial value as zero so we can fold the value into
5741 a constant. But if more than one register is involved,
5742 this probably loses. */
5743 else if (REG_P (target) && TREE_STATIC (exp)
5744 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5746 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5747 cleared = 1;
5750 /* If the constructor has fewer fields than the structure or
5751 if we are initializing the structure to mostly zeros, clear
5752 the whole structure first. Don't do this if TARGET is a
5753 register whose mode size isn't equal to SIZE since
5754 clear_storage can't handle this case. */
5755 else if (size > 0
5756 && (((int)VEC_length (constructor_elt, CONSTRUCTOR_ELTS (exp))
5757 != fields_length (type))
5758 || mostly_zeros_p (exp))
5759 && (!REG_P (target)
5760 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5761 == size)))
5763 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5764 cleared = 1;
5767 if (REG_P (target) && !cleared)
5768 emit_clobber (target);
5770 /* Store each element of the constructor into the
5771 corresponding field of TARGET. */
5772 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
5774 enum machine_mode mode;
5775 HOST_WIDE_INT bitsize;
5776 HOST_WIDE_INT bitpos = 0;
5777 tree offset;
5778 rtx to_rtx = target;
5780 /* Just ignore missing fields. We cleared the whole
5781 structure, above, if any fields are missing. */
5782 if (field == 0)
5783 continue;
5785 if (cleared && initializer_zerop (value))
5786 continue;
5788 if (host_integerp (DECL_SIZE (field), 1))
5789 bitsize = tree_low_cst (DECL_SIZE (field), 1);
5790 else
5791 bitsize = -1;
5793 mode = DECL_MODE (field);
5794 if (DECL_BIT_FIELD (field))
5795 mode = VOIDmode;
5797 offset = DECL_FIELD_OFFSET (field);
5798 if (host_integerp (offset, 0)
5799 && host_integerp (bit_position (field), 0))
5801 bitpos = int_bit_position (field);
5802 offset = 0;
5804 else
5805 bitpos = tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 0);
5807 if (offset)
5809 enum machine_mode address_mode;
5810 rtx offset_rtx;
5812 offset
5813 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
5814 make_tree (TREE_TYPE (exp),
5815 target));
5817 offset_rtx = expand_normal (offset);
5818 gcc_assert (MEM_P (to_rtx));
5820 address_mode = get_address_mode (to_rtx);
5821 if (GET_MODE (offset_rtx) != address_mode)
5822 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5824 to_rtx = offset_address (to_rtx, offset_rtx,
5825 highest_pow2_factor (offset));
5828 #ifdef WORD_REGISTER_OPERATIONS
5829 /* If this initializes a field that is smaller than a
5830 word, at the start of a word, try to widen it to a full
5831 word. This special case allows us to output C++ member
5832 function initializations in a form that the optimizers
5833 can understand. */
5834 if (REG_P (target)
5835 && bitsize < BITS_PER_WORD
5836 && bitpos % BITS_PER_WORD == 0
5837 && GET_MODE_CLASS (mode) == MODE_INT
5838 && TREE_CODE (value) == INTEGER_CST
5839 && exp_size >= 0
5840 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
5842 tree type = TREE_TYPE (value);
5844 if (TYPE_PRECISION (type) < BITS_PER_WORD)
5846 type = lang_hooks.types.type_for_mode
5847 (word_mode, TYPE_UNSIGNED (type));
5848 value = fold_convert (type, value);
5851 if (BYTES_BIG_ENDIAN)
5852 value
5853 = fold_build2 (LSHIFT_EXPR, type, value,
5854 build_int_cst (type,
5855 BITS_PER_WORD - bitsize));
5856 bitsize = BITS_PER_WORD;
5857 mode = word_mode;
5859 #endif
5861 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
5862 && DECL_NONADDRESSABLE_P (field))
5864 to_rtx = copy_rtx (to_rtx);
5865 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
5868 store_constructor_field (to_rtx, bitsize, bitpos, mode,
5869 value, type, cleared,
5870 get_alias_set (TREE_TYPE (field)));
5872 break;
5874 case ARRAY_TYPE:
5876 tree value, index;
5877 unsigned HOST_WIDE_INT i;
5878 int need_to_clear;
5879 tree domain;
5880 tree elttype = TREE_TYPE (type);
5881 int const_bounds_p;
5882 HOST_WIDE_INT minelt = 0;
5883 HOST_WIDE_INT maxelt = 0;
5885 domain = TYPE_DOMAIN (type);
5886 const_bounds_p = (TYPE_MIN_VALUE (domain)
5887 && TYPE_MAX_VALUE (domain)
5888 && host_integerp (TYPE_MIN_VALUE (domain), 0)
5889 && host_integerp (TYPE_MAX_VALUE (domain), 0));
5891 /* If we have constant bounds for the range of the type, get them. */
5892 if (const_bounds_p)
5894 minelt = tree_low_cst (TYPE_MIN_VALUE (domain), 0);
5895 maxelt = tree_low_cst (TYPE_MAX_VALUE (domain), 0);
5898 /* If the constructor has fewer elements than the array, clear
5899 the whole array first. Similarly if this is static
5900 constructor of a non-BLKmode object. */
5901 if (cleared)
5902 need_to_clear = 0;
5903 else if (REG_P (target) && TREE_STATIC (exp))
5904 need_to_clear = 1;
5905 else
5907 unsigned HOST_WIDE_INT idx;
5908 tree index, value;
5909 HOST_WIDE_INT count = 0, zero_count = 0;
5910 need_to_clear = ! const_bounds_p;
5912 /* This loop is a more accurate version of the loop in
5913 mostly_zeros_p (it handles RANGE_EXPR in an index). It
5914 is also needed to check for missing elements. */
5915 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
5917 HOST_WIDE_INT this_node_count;
5919 if (need_to_clear)
5920 break;
5922 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5924 tree lo_index = TREE_OPERAND (index, 0);
5925 tree hi_index = TREE_OPERAND (index, 1);
5927 if (! host_integerp (lo_index, 1)
5928 || ! host_integerp (hi_index, 1))
5930 need_to_clear = 1;
5931 break;
5934 this_node_count = (tree_low_cst (hi_index, 1)
5935 - tree_low_cst (lo_index, 1) + 1);
5937 else
5938 this_node_count = 1;
5940 count += this_node_count;
5941 if (mostly_zeros_p (value))
5942 zero_count += this_node_count;
5945 /* Clear the entire array first if there are any missing
5946 elements, or if the incidence of zero elements is >=
5947 75%. */
5948 if (! need_to_clear
5949 && (count < maxelt - minelt + 1
5950 || 4 * zero_count >= 3 * count))
5951 need_to_clear = 1;
5954 if (need_to_clear && size > 0)
5956 if (REG_P (target))
5957 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5958 else
5959 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5960 cleared = 1;
5963 if (!cleared && REG_P (target))
5964 /* Inform later passes that the old value is dead. */
5965 emit_clobber (target);
5967 /* Store each element of the constructor into the
5968 corresponding element of TARGET, determined by counting the
5969 elements. */
5970 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
5972 enum machine_mode mode;
5973 HOST_WIDE_INT bitsize;
5974 HOST_WIDE_INT bitpos;
5975 rtx xtarget = target;
5977 if (cleared && initializer_zerop (value))
5978 continue;
5980 mode = TYPE_MODE (elttype);
5981 if (mode == BLKmode)
5982 bitsize = (host_integerp (TYPE_SIZE (elttype), 1)
5983 ? tree_low_cst (TYPE_SIZE (elttype), 1)
5984 : -1);
5985 else
5986 bitsize = GET_MODE_BITSIZE (mode);
5988 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5990 tree lo_index = TREE_OPERAND (index, 0);
5991 tree hi_index = TREE_OPERAND (index, 1);
5992 rtx index_r, pos_rtx;
5993 HOST_WIDE_INT lo, hi, count;
5994 tree position;
5996 /* If the range is constant and "small", unroll the loop. */
5997 if (const_bounds_p
5998 && host_integerp (lo_index, 0)
5999 && host_integerp (hi_index, 0)
6000 && (lo = tree_low_cst (lo_index, 0),
6001 hi = tree_low_cst (hi_index, 0),
6002 count = hi - lo + 1,
6003 (!MEM_P (target)
6004 || count <= 2
6005 || (host_integerp (TYPE_SIZE (elttype), 1)
6006 && (tree_low_cst (TYPE_SIZE (elttype), 1) * count
6007 <= 40 * 8)))))
6009 lo -= minelt; hi -= minelt;
6010 for (; lo <= hi; lo++)
6012 bitpos = lo * tree_low_cst (TYPE_SIZE (elttype), 0);
6014 if (MEM_P (target)
6015 && !MEM_KEEP_ALIAS_SET_P (target)
6016 && TREE_CODE (type) == ARRAY_TYPE
6017 && TYPE_NONALIASED_COMPONENT (type))
6019 target = copy_rtx (target);
6020 MEM_KEEP_ALIAS_SET_P (target) = 1;
6023 store_constructor_field
6024 (target, bitsize, bitpos, mode, value, type, cleared,
6025 get_alias_set (elttype));
6028 else
6030 rtx loop_start = gen_label_rtx ();
6031 rtx loop_end = gen_label_rtx ();
6032 tree exit_cond;
6034 expand_normal (hi_index);
6036 index = build_decl (EXPR_LOCATION (exp),
6037 VAR_DECL, NULL_TREE, domain);
6038 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6039 SET_DECL_RTL (index, index_r);
6040 store_expr (lo_index, index_r, 0, false);
6042 /* Build the head of the loop. */
6043 do_pending_stack_adjust ();
6044 emit_label (loop_start);
6046 /* Assign value to element index. */
6047 position =
6048 fold_convert (ssizetype,
6049 fold_build2 (MINUS_EXPR,
6050 TREE_TYPE (index),
6051 index,
6052 TYPE_MIN_VALUE (domain)));
6054 position =
6055 size_binop (MULT_EXPR, position,
6056 fold_convert (ssizetype,
6057 TYPE_SIZE_UNIT (elttype)));
6059 pos_rtx = expand_normal (position);
6060 xtarget = offset_address (target, pos_rtx,
6061 highest_pow2_factor (position));
6062 xtarget = adjust_address (xtarget, mode, 0);
6063 if (TREE_CODE (value) == CONSTRUCTOR)
6064 store_constructor (value, xtarget, cleared,
6065 bitsize / BITS_PER_UNIT);
6066 else
6067 store_expr (value, xtarget, 0, false);
6069 /* Generate a conditional jump to exit the loop. */
6070 exit_cond = build2 (LT_EXPR, integer_type_node,
6071 index, hi_index);
6072 jumpif (exit_cond, loop_end, -1);
6074 /* Update the loop counter, and jump to the head of
6075 the loop. */
6076 expand_assignment (index,
6077 build2 (PLUS_EXPR, TREE_TYPE (index),
6078 index, integer_one_node),
6079 false);
6081 emit_jump (loop_start);
6083 /* Build the end of the loop. */
6084 emit_label (loop_end);
6087 else if ((index != 0 && ! host_integerp (index, 0))
6088 || ! host_integerp (TYPE_SIZE (elttype), 1))
6090 tree position;
6092 if (index == 0)
6093 index = ssize_int (1);
6095 if (minelt)
6096 index = fold_convert (ssizetype,
6097 fold_build2 (MINUS_EXPR,
6098 TREE_TYPE (index),
6099 index,
6100 TYPE_MIN_VALUE (domain)));
6102 position =
6103 size_binop (MULT_EXPR, index,
6104 fold_convert (ssizetype,
6105 TYPE_SIZE_UNIT (elttype)));
6106 xtarget = offset_address (target,
6107 expand_normal (position),
6108 highest_pow2_factor (position));
6109 xtarget = adjust_address (xtarget, mode, 0);
6110 store_expr (value, xtarget, 0, false);
6112 else
6114 if (index != 0)
6115 bitpos = ((tree_low_cst (index, 0) - minelt)
6116 * tree_low_cst (TYPE_SIZE (elttype), 1));
6117 else
6118 bitpos = (i * tree_low_cst (TYPE_SIZE (elttype), 1));
6120 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6121 && TREE_CODE (type) == ARRAY_TYPE
6122 && TYPE_NONALIASED_COMPONENT (type))
6124 target = copy_rtx (target);
6125 MEM_KEEP_ALIAS_SET_P (target) = 1;
6127 store_constructor_field (target, bitsize, bitpos, mode, value,
6128 type, cleared, get_alias_set (elttype));
6131 break;
6134 case VECTOR_TYPE:
6136 unsigned HOST_WIDE_INT idx;
6137 constructor_elt *ce;
6138 int i;
6139 int need_to_clear;
6140 int icode = 0;
6141 tree elttype = TREE_TYPE (type);
6142 int elt_size = tree_low_cst (TYPE_SIZE (elttype), 1);
6143 enum machine_mode eltmode = TYPE_MODE (elttype);
6144 HOST_WIDE_INT bitsize;
6145 HOST_WIDE_INT bitpos;
6146 rtvec vector = NULL;
6147 unsigned n_elts;
6148 alias_set_type alias;
6150 gcc_assert (eltmode != BLKmode);
6152 n_elts = TYPE_VECTOR_SUBPARTS (type);
6153 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6155 enum machine_mode mode = GET_MODE (target);
6157 icode = (int) optab_handler (vec_init_optab, mode);
6158 if (icode != CODE_FOR_nothing)
6160 unsigned int i;
6162 vector = rtvec_alloc (n_elts);
6163 for (i = 0; i < n_elts; i++)
6164 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6168 /* If the constructor has fewer elements than the vector,
6169 clear the whole array first. Similarly if this is static
6170 constructor of a non-BLKmode object. */
6171 if (cleared)
6172 need_to_clear = 0;
6173 else if (REG_P (target) && TREE_STATIC (exp))
6174 need_to_clear = 1;
6175 else
6177 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6178 tree value;
6180 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6182 int n_elts_here = tree_low_cst
6183 (int_const_binop (TRUNC_DIV_EXPR,
6184 TYPE_SIZE (TREE_TYPE (value)),
6185 TYPE_SIZE (elttype)), 1);
6187 count += n_elts_here;
6188 if (mostly_zeros_p (value))
6189 zero_count += n_elts_here;
6192 /* Clear the entire vector first if there are any missing elements,
6193 or if the incidence of zero elements is >= 75%. */
6194 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6197 if (need_to_clear && size > 0 && !vector)
6199 if (REG_P (target))
6200 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6201 else
6202 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6203 cleared = 1;
6206 /* Inform later passes that the old value is dead. */
6207 if (!cleared && !vector && REG_P (target))
6208 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6210 if (MEM_P (target))
6211 alias = MEM_ALIAS_SET (target);
6212 else
6213 alias = get_alias_set (elttype);
6215 /* Store each element of the constructor into the corresponding
6216 element of TARGET, determined by counting the elements. */
6217 for (idx = 0, i = 0;
6218 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce);
6219 idx++, i += bitsize / elt_size)
6221 HOST_WIDE_INT eltpos;
6222 tree value = ce->value;
6224 bitsize = tree_low_cst (TYPE_SIZE (TREE_TYPE (value)), 1);
6225 if (cleared && initializer_zerop (value))
6226 continue;
6228 if (ce->index)
6229 eltpos = tree_low_cst (ce->index, 1);
6230 else
6231 eltpos = i;
6233 if (vector)
6235 /* Vector CONSTRUCTORs should only be built from smaller
6236 vectors in the case of BLKmode vectors. */
6237 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6238 RTVEC_ELT (vector, eltpos)
6239 = expand_normal (value);
6241 else
6243 enum machine_mode value_mode =
6244 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6245 ? TYPE_MODE (TREE_TYPE (value))
6246 : eltmode;
6247 bitpos = eltpos * elt_size;
6248 store_constructor_field (target, bitsize, bitpos,
6249 value_mode, value, type,
6250 cleared, alias);
6254 if (vector)
6255 emit_insn (GEN_FCN (icode)
6256 (target,
6257 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6258 break;
6261 default:
6262 gcc_unreachable ();
6266 /* Store the value of EXP (an expression tree)
6267 into a subfield of TARGET which has mode MODE and occupies
6268 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6269 If MODE is VOIDmode, it means that we are storing into a bit-field.
6271 BITREGION_START is bitpos of the first bitfield in this region.
6272 BITREGION_END is the bitpos of the ending bitfield in this region.
6273 These two fields are 0, if the C++ memory model does not apply,
6274 or we are not interested in keeping track of bitfield regions.
6276 Always return const0_rtx unless we have something particular to
6277 return.
6279 TYPE is the type of the underlying object,
6281 ALIAS_SET is the alias set for the destination. This value will
6282 (in general) be different from that for TARGET, since TARGET is a
6283 reference to the containing structure.
6285 If NONTEMPORAL is true, try generating a nontemporal store. */
6287 static rtx
6288 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6289 unsigned HOST_WIDE_INT bitregion_start,
6290 unsigned HOST_WIDE_INT bitregion_end,
6291 enum machine_mode mode, tree exp, tree type,
6292 alias_set_type alias_set, bool nontemporal)
6294 if (TREE_CODE (exp) == ERROR_MARK)
6295 return const0_rtx;
6297 /* If we have nothing to store, do nothing unless the expression has
6298 side-effects. */
6299 if (bitsize == 0)
6300 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6302 /* If we are storing into an unaligned field of an aligned union that is
6303 in a register, we may have the mode of TARGET being an integer mode but
6304 MODE == BLKmode. In that case, get an aligned object whose size and
6305 alignment are the same as TARGET and store TARGET into it (we can avoid
6306 the store if the field being stored is the entire width of TARGET). Then
6307 call ourselves recursively to store the field into a BLKmode version of
6308 that object. Finally, load from the object into TARGET. This is not
6309 very efficient in general, but should only be slightly more expensive
6310 than the otherwise-required unaligned accesses. Perhaps this can be
6311 cleaned up later. It's tempting to make OBJECT readonly, but it's set
6312 twice, once with emit_move_insn and once via store_field. */
6314 if (mode == BLKmode
6315 && (REG_P (target) || GET_CODE (target) == SUBREG))
6317 rtx object = assign_temp (type, 0, 1, 1);
6318 rtx blk_object = adjust_address (object, BLKmode, 0);
6320 if (bitsize != (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (target)))
6321 emit_move_insn (object, target);
6323 store_field (blk_object, bitsize, bitpos,
6324 bitregion_start, bitregion_end,
6325 mode, exp, type, MEM_ALIAS_SET (blk_object), nontemporal);
6327 emit_move_insn (target, object);
6329 /* We want to return the BLKmode version of the data. */
6330 return blk_object;
6333 if (GET_CODE (target) == CONCAT)
6335 /* We're storing into a struct containing a single __complex. */
6337 gcc_assert (!bitpos);
6338 return store_expr (exp, target, 0, nontemporal);
6341 /* If the structure is in a register or if the component
6342 is a bit field, we cannot use addressing to access it.
6343 Use bit-field techniques or SUBREG to store in it. */
6345 if (mode == VOIDmode
6346 || (mode != BLKmode && ! direct_store[(int) mode]
6347 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6348 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6349 || REG_P (target)
6350 || GET_CODE (target) == SUBREG
6351 /* If the field isn't aligned enough to store as an ordinary memref,
6352 store it as a bit field. */
6353 || (mode != BLKmode
6354 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6355 || bitpos % GET_MODE_ALIGNMENT (mode))
6356 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6357 || (bitpos % BITS_PER_UNIT != 0)))
6358 || (bitsize >= 0 && mode != BLKmode
6359 && GET_MODE_BITSIZE (mode) > bitsize)
6360 /* If the RHS and field are a constant size and the size of the
6361 RHS isn't the same size as the bitfield, we must use bitfield
6362 operations. */
6363 || (bitsize >= 0
6364 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6365 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6366 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6367 decl we must use bitfield operations. */
6368 || (bitsize >= 0
6369 && TREE_CODE (exp) == MEM_REF
6370 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6371 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6372 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6373 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6375 rtx temp;
6376 gimple nop_def;
6378 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6379 implies a mask operation. If the precision is the same size as
6380 the field we're storing into, that mask is redundant. This is
6381 particularly common with bit field assignments generated by the
6382 C front end. */
6383 nop_def = get_def_for_expr (exp, NOP_EXPR);
6384 if (nop_def)
6386 tree type = TREE_TYPE (exp);
6387 if (INTEGRAL_TYPE_P (type)
6388 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6389 && bitsize == TYPE_PRECISION (type))
6391 tree op = gimple_assign_rhs1 (nop_def);
6392 type = TREE_TYPE (op);
6393 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6394 exp = op;
6398 temp = expand_normal (exp);
6400 /* If BITSIZE is narrower than the size of the type of EXP
6401 we will be narrowing TEMP. Normally, what's wanted are the
6402 low-order bits. However, if EXP's type is a record and this is
6403 big-endian machine, we want the upper BITSIZE bits. */
6404 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6405 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6406 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6407 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6408 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6409 NULL_RTX, 1);
6411 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6412 if (mode != VOIDmode && mode != BLKmode
6413 && mode != TYPE_MODE (TREE_TYPE (exp)))
6414 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6416 /* If the modes of TEMP and TARGET are both BLKmode, both
6417 must be in memory and BITPOS must be aligned on a byte
6418 boundary. If so, we simply do a block copy. Likewise
6419 for a BLKmode-like TARGET. */
6420 if (GET_MODE (temp) == BLKmode
6421 && (GET_MODE (target) == BLKmode
6422 || (MEM_P (target)
6423 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6424 && (bitpos % BITS_PER_UNIT) == 0
6425 && (bitsize % BITS_PER_UNIT) == 0)))
6427 gcc_assert (MEM_P (target) && MEM_P (temp)
6428 && (bitpos % BITS_PER_UNIT) == 0);
6430 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6431 emit_block_move (target, temp,
6432 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6433 / BITS_PER_UNIT),
6434 BLOCK_OP_NORMAL);
6436 return const0_rtx;
6439 /* Store the value in the bitfield. */
6440 store_bit_field (target, bitsize, bitpos,
6441 bitregion_start, bitregion_end,
6442 mode, temp);
6444 return const0_rtx;
6446 else
6448 /* Now build a reference to just the desired component. */
6449 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6451 if (to_rtx == target)
6452 to_rtx = copy_rtx (to_rtx);
6454 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6455 set_mem_alias_set (to_rtx, alias_set);
6457 return store_expr (exp, to_rtx, 0, nontemporal);
6461 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6462 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6463 codes and find the ultimate containing object, which we return.
6465 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6466 bit position, and *PUNSIGNEDP to the signedness of the field.
6467 If the position of the field is variable, we store a tree
6468 giving the variable offset (in units) in *POFFSET.
6469 This offset is in addition to the bit position.
6470 If the position is not variable, we store 0 in *POFFSET.
6472 If any of the extraction expressions is volatile,
6473 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6475 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6476 Otherwise, it is a mode that can be used to access the field.
6478 If the field describes a variable-sized object, *PMODE is set to
6479 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6480 this case, but the address of the object can be found.
6482 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6483 look through nodes that serve as markers of a greater alignment than
6484 the one that can be deduced from the expression. These nodes make it
6485 possible for front-ends to prevent temporaries from being created by
6486 the middle-end on alignment considerations. For that purpose, the
6487 normal operating mode at high-level is to always pass FALSE so that
6488 the ultimate containing object is really returned; moreover, the
6489 associated predicate handled_component_p will always return TRUE
6490 on these nodes, thus indicating that they are essentially handled
6491 by get_inner_reference. TRUE should only be passed when the caller
6492 is scanning the expression in order to build another representation
6493 and specifically knows how to handle these nodes; as such, this is
6494 the normal operating mode in the RTL expanders. */
6496 tree
6497 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6498 HOST_WIDE_INT *pbitpos, tree *poffset,
6499 enum machine_mode *pmode, int *punsignedp,
6500 int *pvolatilep, bool keep_aligning)
6502 tree size_tree = 0;
6503 enum machine_mode mode = VOIDmode;
6504 bool blkmode_bitfield = false;
6505 tree offset = size_zero_node;
6506 double_int bit_offset = double_int_zero;
6508 /* First get the mode, signedness, and size. We do this from just the
6509 outermost expression. */
6510 *pbitsize = -1;
6511 if (TREE_CODE (exp) == COMPONENT_REF)
6513 tree field = TREE_OPERAND (exp, 1);
6514 size_tree = DECL_SIZE (field);
6515 if (!DECL_BIT_FIELD (field))
6516 mode = DECL_MODE (field);
6517 else if (DECL_MODE (field) == BLKmode)
6518 blkmode_bitfield = true;
6519 else if (TREE_THIS_VOLATILE (exp)
6520 && flag_strict_volatile_bitfields > 0)
6521 /* Volatile bitfields should be accessed in the mode of the
6522 field's type, not the mode computed based on the bit
6523 size. */
6524 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6526 *punsignedp = DECL_UNSIGNED (field);
6528 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6530 size_tree = TREE_OPERAND (exp, 1);
6531 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6532 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6534 /* For vector types, with the correct size of access, use the mode of
6535 inner type. */
6536 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6537 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6538 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6539 mode = TYPE_MODE (TREE_TYPE (exp));
6541 else
6543 mode = TYPE_MODE (TREE_TYPE (exp));
6544 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6546 if (mode == BLKmode)
6547 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6548 else
6549 *pbitsize = GET_MODE_BITSIZE (mode);
6552 if (size_tree != 0)
6554 if (! host_integerp (size_tree, 1))
6555 mode = BLKmode, *pbitsize = -1;
6556 else
6557 *pbitsize = tree_low_cst (size_tree, 1);
6560 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6561 and find the ultimate containing object. */
6562 while (1)
6564 switch (TREE_CODE (exp))
6566 case BIT_FIELD_REF:
6567 bit_offset
6568 = double_int_add (bit_offset,
6569 tree_to_double_int (TREE_OPERAND (exp, 2)));
6570 break;
6572 case COMPONENT_REF:
6574 tree field = TREE_OPERAND (exp, 1);
6575 tree this_offset = component_ref_field_offset (exp);
6577 /* If this field hasn't been filled in yet, don't go past it.
6578 This should only happen when folding expressions made during
6579 type construction. */
6580 if (this_offset == 0)
6581 break;
6583 offset = size_binop (PLUS_EXPR, offset, this_offset);
6584 bit_offset = double_int_add (bit_offset,
6585 tree_to_double_int
6586 (DECL_FIELD_BIT_OFFSET (field)));
6588 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6590 break;
6592 case ARRAY_REF:
6593 case ARRAY_RANGE_REF:
6595 tree index = TREE_OPERAND (exp, 1);
6596 tree low_bound = array_ref_low_bound (exp);
6597 tree unit_size = array_ref_element_size (exp);
6599 /* We assume all arrays have sizes that are a multiple of a byte.
6600 First subtract the lower bound, if any, in the type of the
6601 index, then convert to sizetype and multiply by the size of
6602 the array element. */
6603 if (! integer_zerop (low_bound))
6604 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6605 index, low_bound);
6607 offset = size_binop (PLUS_EXPR, offset,
6608 size_binop (MULT_EXPR,
6609 fold_convert (sizetype, index),
6610 unit_size));
6612 break;
6614 case REALPART_EXPR:
6615 break;
6617 case IMAGPART_EXPR:
6618 bit_offset = double_int_add (bit_offset,
6619 uhwi_to_double_int (*pbitsize));
6620 break;
6622 case VIEW_CONVERT_EXPR:
6623 if (keep_aligning && STRICT_ALIGNMENT
6624 && (TYPE_ALIGN (TREE_TYPE (exp))
6625 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6626 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6627 < BIGGEST_ALIGNMENT)
6628 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6629 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6630 goto done;
6631 break;
6633 case MEM_REF:
6634 /* Hand back the decl for MEM[&decl, off]. */
6635 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6637 tree off = TREE_OPERAND (exp, 1);
6638 if (!integer_zerop (off))
6640 double_int boff, coff = mem_ref_offset (exp);
6641 boff = double_int_lshift (coff,
6642 BITS_PER_UNIT == 8
6643 ? 3 : exact_log2 (BITS_PER_UNIT),
6644 HOST_BITS_PER_DOUBLE_INT, true);
6645 bit_offset = double_int_add (bit_offset, boff);
6647 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6649 goto done;
6651 default:
6652 goto done;
6655 /* If any reference in the chain is volatile, the effect is volatile. */
6656 if (TREE_THIS_VOLATILE (exp))
6657 *pvolatilep = 1;
6659 exp = TREE_OPERAND (exp, 0);
6661 done:
6663 /* If OFFSET is constant, see if we can return the whole thing as a
6664 constant bit position. Make sure to handle overflow during
6665 this conversion. */
6666 if (TREE_CODE (offset) == INTEGER_CST)
6668 double_int tem = tree_to_double_int (offset);
6669 tem = double_int_sext (tem, TYPE_PRECISION (sizetype));
6670 tem = double_int_lshift (tem,
6671 BITS_PER_UNIT == 8
6672 ? 3 : exact_log2 (BITS_PER_UNIT),
6673 HOST_BITS_PER_DOUBLE_INT, true);
6674 tem = double_int_add (tem, bit_offset);
6675 if (double_int_fits_in_shwi_p (tem))
6677 *pbitpos = double_int_to_shwi (tem);
6678 *poffset = offset = NULL_TREE;
6682 /* Otherwise, split it up. */
6683 if (offset)
6685 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6686 if (double_int_negative_p (bit_offset))
6688 double_int mask
6689 = double_int_mask (BITS_PER_UNIT == 8
6690 ? 3 : exact_log2 (BITS_PER_UNIT));
6691 double_int tem = double_int_and_not (bit_offset, mask);
6692 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6693 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6694 bit_offset = double_int_sub (bit_offset, tem);
6695 tem = double_int_rshift (tem,
6696 BITS_PER_UNIT == 8
6697 ? 3 : exact_log2 (BITS_PER_UNIT),
6698 HOST_BITS_PER_DOUBLE_INT, true);
6699 offset = size_binop (PLUS_EXPR, offset,
6700 double_int_to_tree (sizetype, tem));
6703 *pbitpos = double_int_to_shwi (bit_offset);
6704 *poffset = offset;
6707 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6708 if (mode == VOIDmode
6709 && blkmode_bitfield
6710 && (*pbitpos % BITS_PER_UNIT) == 0
6711 && (*pbitsize % BITS_PER_UNIT) == 0)
6712 *pmode = BLKmode;
6713 else
6714 *pmode = mode;
6716 return exp;
6719 /* Given an expression EXP that may be a COMPONENT_REF, an ARRAY_REF or an
6720 ARRAY_RANGE_REF, look for whether EXP or any nested component-refs within
6721 EXP is marked as PACKED. */
6723 bool
6724 contains_packed_reference (const_tree exp)
6726 bool packed_p = false;
6728 while (1)
6730 switch (TREE_CODE (exp))
6732 case COMPONENT_REF:
6734 tree field = TREE_OPERAND (exp, 1);
6735 packed_p = DECL_PACKED (field)
6736 || TYPE_PACKED (TREE_TYPE (field))
6737 || TYPE_PACKED (TREE_TYPE (exp));
6738 if (packed_p)
6739 goto done;
6741 break;
6743 case BIT_FIELD_REF:
6744 case ARRAY_REF:
6745 case ARRAY_RANGE_REF:
6746 case REALPART_EXPR:
6747 case IMAGPART_EXPR:
6748 case VIEW_CONVERT_EXPR:
6749 break;
6751 default:
6752 goto done;
6754 exp = TREE_OPERAND (exp, 0);
6756 done:
6757 return packed_p;
6760 /* Return a tree of sizetype representing the size, in bytes, of the element
6761 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6763 tree
6764 array_ref_element_size (tree exp)
6766 tree aligned_size = TREE_OPERAND (exp, 3);
6767 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6768 location_t loc = EXPR_LOCATION (exp);
6770 /* If a size was specified in the ARRAY_REF, it's the size measured
6771 in alignment units of the element type. So multiply by that value. */
6772 if (aligned_size)
6774 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6775 sizetype from another type of the same width and signedness. */
6776 if (TREE_TYPE (aligned_size) != sizetype)
6777 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6778 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6779 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6782 /* Otherwise, take the size from that of the element type. Substitute
6783 any PLACEHOLDER_EXPR that we have. */
6784 else
6785 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6788 /* Return a tree representing the lower bound of the array mentioned in
6789 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6791 tree
6792 array_ref_low_bound (tree exp)
6794 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6796 /* If a lower bound is specified in EXP, use it. */
6797 if (TREE_OPERAND (exp, 2))
6798 return TREE_OPERAND (exp, 2);
6800 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6801 substituting for a PLACEHOLDER_EXPR as needed. */
6802 if (domain_type && TYPE_MIN_VALUE (domain_type))
6803 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6805 /* Otherwise, return a zero of the appropriate type. */
6806 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
6809 /* Returns true if REF is an array reference to an array at the end of
6810 a structure. If this is the case, the array may be allocated larger
6811 than its upper bound implies. */
6813 bool
6814 array_at_struct_end_p (tree ref)
6816 if (TREE_CODE (ref) != ARRAY_REF
6817 && TREE_CODE (ref) != ARRAY_RANGE_REF)
6818 return false;
6820 while (handled_component_p (ref))
6822 /* If the reference chain contains a component reference to a
6823 non-union type and there follows another field the reference
6824 is not at the end of a structure. */
6825 if (TREE_CODE (ref) == COMPONENT_REF
6826 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
6828 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
6829 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
6830 nextf = DECL_CHAIN (nextf);
6831 if (nextf)
6832 return false;
6835 ref = TREE_OPERAND (ref, 0);
6838 /* If the reference is based on a declared entity, the size of the array
6839 is constrained by its given domain. */
6840 if (DECL_P (ref))
6841 return false;
6843 return true;
6846 /* Return a tree representing the upper bound of the array mentioned in
6847 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6849 tree
6850 array_ref_up_bound (tree exp)
6852 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6854 /* If there is a domain type and it has an upper bound, use it, substituting
6855 for a PLACEHOLDER_EXPR as needed. */
6856 if (domain_type && TYPE_MAX_VALUE (domain_type))
6857 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
6859 /* Otherwise fail. */
6860 return NULL_TREE;
6863 /* Return a tree representing the offset, in bytes, of the field referenced
6864 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
6866 tree
6867 component_ref_field_offset (tree exp)
6869 tree aligned_offset = TREE_OPERAND (exp, 2);
6870 tree field = TREE_OPERAND (exp, 1);
6871 location_t loc = EXPR_LOCATION (exp);
6873 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
6874 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
6875 value. */
6876 if (aligned_offset)
6878 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6879 sizetype from another type of the same width and signedness. */
6880 if (TREE_TYPE (aligned_offset) != sizetype)
6881 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
6882 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
6883 size_int (DECL_OFFSET_ALIGN (field)
6884 / BITS_PER_UNIT));
6887 /* Otherwise, take the offset from that of the field. Substitute
6888 any PLACEHOLDER_EXPR that we have. */
6889 else
6890 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
6893 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
6895 static unsigned HOST_WIDE_INT
6896 target_align (const_tree target)
6898 /* We might have a chain of nested references with intermediate misaligning
6899 bitfields components, so need to recurse to find out. */
6901 unsigned HOST_WIDE_INT this_align, outer_align;
6903 switch (TREE_CODE (target))
6905 case BIT_FIELD_REF:
6906 return 1;
6908 case COMPONENT_REF:
6909 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
6910 outer_align = target_align (TREE_OPERAND (target, 0));
6911 return MIN (this_align, outer_align);
6913 case ARRAY_REF:
6914 case ARRAY_RANGE_REF:
6915 this_align = TYPE_ALIGN (TREE_TYPE (target));
6916 outer_align = target_align (TREE_OPERAND (target, 0));
6917 return MIN (this_align, outer_align);
6919 CASE_CONVERT:
6920 case NON_LVALUE_EXPR:
6921 case VIEW_CONVERT_EXPR:
6922 this_align = TYPE_ALIGN (TREE_TYPE (target));
6923 outer_align = target_align (TREE_OPERAND (target, 0));
6924 return MAX (this_align, outer_align);
6926 default:
6927 return TYPE_ALIGN (TREE_TYPE (target));
6932 /* Given an rtx VALUE that may contain additions and multiplications, return
6933 an equivalent value that just refers to a register, memory, or constant.
6934 This is done by generating instructions to perform the arithmetic and
6935 returning a pseudo-register containing the value.
6937 The returned value may be a REG, SUBREG, MEM or constant. */
6940 force_operand (rtx value, rtx target)
6942 rtx op1, op2;
6943 /* Use subtarget as the target for operand 0 of a binary operation. */
6944 rtx subtarget = get_subtarget (target);
6945 enum rtx_code code = GET_CODE (value);
6947 /* Check for subreg applied to an expression produced by loop optimizer. */
6948 if (code == SUBREG
6949 && !REG_P (SUBREG_REG (value))
6950 && !MEM_P (SUBREG_REG (value)))
6952 value
6953 = simplify_gen_subreg (GET_MODE (value),
6954 force_reg (GET_MODE (SUBREG_REG (value)),
6955 force_operand (SUBREG_REG (value),
6956 NULL_RTX)),
6957 GET_MODE (SUBREG_REG (value)),
6958 SUBREG_BYTE (value));
6959 code = GET_CODE (value);
6962 /* Check for a PIC address load. */
6963 if ((code == PLUS || code == MINUS)
6964 && XEXP (value, 0) == pic_offset_table_rtx
6965 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
6966 || GET_CODE (XEXP (value, 1)) == LABEL_REF
6967 || GET_CODE (XEXP (value, 1)) == CONST))
6969 if (!subtarget)
6970 subtarget = gen_reg_rtx (GET_MODE (value));
6971 emit_move_insn (subtarget, value);
6972 return subtarget;
6975 if (ARITHMETIC_P (value))
6977 op2 = XEXP (value, 1);
6978 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
6979 subtarget = 0;
6980 if (code == MINUS && CONST_INT_P (op2))
6982 code = PLUS;
6983 op2 = negate_rtx (GET_MODE (value), op2);
6986 /* Check for an addition with OP2 a constant integer and our first
6987 operand a PLUS of a virtual register and something else. In that
6988 case, we want to emit the sum of the virtual register and the
6989 constant first and then add the other value. This allows virtual
6990 register instantiation to simply modify the constant rather than
6991 creating another one around this addition. */
6992 if (code == PLUS && CONST_INT_P (op2)
6993 && GET_CODE (XEXP (value, 0)) == PLUS
6994 && REG_P (XEXP (XEXP (value, 0), 0))
6995 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
6996 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
6998 rtx temp = expand_simple_binop (GET_MODE (value), code,
6999 XEXP (XEXP (value, 0), 0), op2,
7000 subtarget, 0, OPTAB_LIB_WIDEN);
7001 return expand_simple_binop (GET_MODE (value), code, temp,
7002 force_operand (XEXP (XEXP (value,
7003 0), 1), 0),
7004 target, 0, OPTAB_LIB_WIDEN);
7007 op1 = force_operand (XEXP (value, 0), subtarget);
7008 op2 = force_operand (op2, NULL_RTX);
7009 switch (code)
7011 case MULT:
7012 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7013 case DIV:
7014 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7015 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7016 target, 1, OPTAB_LIB_WIDEN);
7017 else
7018 return expand_divmod (0,
7019 FLOAT_MODE_P (GET_MODE (value))
7020 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7021 GET_MODE (value), op1, op2, target, 0);
7022 case MOD:
7023 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7024 target, 0);
7025 case UDIV:
7026 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7027 target, 1);
7028 case UMOD:
7029 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7030 target, 1);
7031 case ASHIFTRT:
7032 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7033 target, 0, OPTAB_LIB_WIDEN);
7034 default:
7035 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7036 target, 1, OPTAB_LIB_WIDEN);
7039 if (UNARY_P (value))
7041 if (!target)
7042 target = gen_reg_rtx (GET_MODE (value));
7043 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7044 switch (code)
7046 case ZERO_EXTEND:
7047 case SIGN_EXTEND:
7048 case TRUNCATE:
7049 case FLOAT_EXTEND:
7050 case FLOAT_TRUNCATE:
7051 convert_move (target, op1, code == ZERO_EXTEND);
7052 return target;
7054 case FIX:
7055 case UNSIGNED_FIX:
7056 expand_fix (target, op1, code == UNSIGNED_FIX);
7057 return target;
7059 case FLOAT:
7060 case UNSIGNED_FLOAT:
7061 expand_float (target, op1, code == UNSIGNED_FLOAT);
7062 return target;
7064 default:
7065 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7069 #ifdef INSN_SCHEDULING
7070 /* On machines that have insn scheduling, we want all memory reference to be
7071 explicit, so we need to deal with such paradoxical SUBREGs. */
7072 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7073 value
7074 = simplify_gen_subreg (GET_MODE (value),
7075 force_reg (GET_MODE (SUBREG_REG (value)),
7076 force_operand (SUBREG_REG (value),
7077 NULL_RTX)),
7078 GET_MODE (SUBREG_REG (value)),
7079 SUBREG_BYTE (value));
7080 #endif
7082 return value;
7085 /* Subroutine of expand_expr: return nonzero iff there is no way that
7086 EXP can reference X, which is being modified. TOP_P is nonzero if this
7087 call is going to be used to determine whether we need a temporary
7088 for EXP, as opposed to a recursive call to this function.
7090 It is always safe for this routine to return zero since it merely
7091 searches for optimization opportunities. */
7094 safe_from_p (const_rtx x, tree exp, int top_p)
7096 rtx exp_rtl = 0;
7097 int i, nops;
7099 if (x == 0
7100 /* If EXP has varying size, we MUST use a target since we currently
7101 have no way of allocating temporaries of variable size
7102 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7103 So we assume here that something at a higher level has prevented a
7104 clash. This is somewhat bogus, but the best we can do. Only
7105 do this when X is BLKmode and when we are at the top level. */
7106 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7107 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7108 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7109 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7110 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7111 != INTEGER_CST)
7112 && GET_MODE (x) == BLKmode)
7113 /* If X is in the outgoing argument area, it is always safe. */
7114 || (MEM_P (x)
7115 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7116 || (GET_CODE (XEXP (x, 0)) == PLUS
7117 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7118 return 1;
7120 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7121 find the underlying pseudo. */
7122 if (GET_CODE (x) == SUBREG)
7124 x = SUBREG_REG (x);
7125 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7126 return 0;
7129 /* Now look at our tree code and possibly recurse. */
7130 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7132 case tcc_declaration:
7133 exp_rtl = DECL_RTL_IF_SET (exp);
7134 break;
7136 case tcc_constant:
7137 return 1;
7139 case tcc_exceptional:
7140 if (TREE_CODE (exp) == TREE_LIST)
7142 while (1)
7144 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7145 return 0;
7146 exp = TREE_CHAIN (exp);
7147 if (!exp)
7148 return 1;
7149 if (TREE_CODE (exp) != TREE_LIST)
7150 return safe_from_p (x, exp, 0);
7153 else if (TREE_CODE (exp) == CONSTRUCTOR)
7155 constructor_elt *ce;
7156 unsigned HOST_WIDE_INT idx;
7158 FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce)
7159 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7160 || !safe_from_p (x, ce->value, 0))
7161 return 0;
7162 return 1;
7164 else if (TREE_CODE (exp) == ERROR_MARK)
7165 return 1; /* An already-visited SAVE_EXPR? */
7166 else
7167 return 0;
7169 case tcc_statement:
7170 /* The only case we look at here is the DECL_INITIAL inside a
7171 DECL_EXPR. */
7172 return (TREE_CODE (exp) != DECL_EXPR
7173 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7174 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7175 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7177 case tcc_binary:
7178 case tcc_comparison:
7179 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7180 return 0;
7181 /* Fall through. */
7183 case tcc_unary:
7184 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7186 case tcc_expression:
7187 case tcc_reference:
7188 case tcc_vl_exp:
7189 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7190 the expression. If it is set, we conflict iff we are that rtx or
7191 both are in memory. Otherwise, we check all operands of the
7192 expression recursively. */
7194 switch (TREE_CODE (exp))
7196 case ADDR_EXPR:
7197 /* If the operand is static or we are static, we can't conflict.
7198 Likewise if we don't conflict with the operand at all. */
7199 if (staticp (TREE_OPERAND (exp, 0))
7200 || TREE_STATIC (exp)
7201 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7202 return 1;
7204 /* Otherwise, the only way this can conflict is if we are taking
7205 the address of a DECL a that address if part of X, which is
7206 very rare. */
7207 exp = TREE_OPERAND (exp, 0);
7208 if (DECL_P (exp))
7210 if (!DECL_RTL_SET_P (exp)
7211 || !MEM_P (DECL_RTL (exp)))
7212 return 0;
7213 else
7214 exp_rtl = XEXP (DECL_RTL (exp), 0);
7216 break;
7218 case MEM_REF:
7219 if (MEM_P (x)
7220 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7221 get_alias_set (exp)))
7222 return 0;
7223 break;
7225 case CALL_EXPR:
7226 /* Assume that the call will clobber all hard registers and
7227 all of memory. */
7228 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7229 || MEM_P (x))
7230 return 0;
7231 break;
7233 case WITH_CLEANUP_EXPR:
7234 case CLEANUP_POINT_EXPR:
7235 /* Lowered by gimplify.c. */
7236 gcc_unreachable ();
7238 case SAVE_EXPR:
7239 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7241 default:
7242 break;
7245 /* If we have an rtx, we do not need to scan our operands. */
7246 if (exp_rtl)
7247 break;
7249 nops = TREE_OPERAND_LENGTH (exp);
7250 for (i = 0; i < nops; i++)
7251 if (TREE_OPERAND (exp, i) != 0
7252 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7253 return 0;
7255 break;
7257 case tcc_type:
7258 /* Should never get a type here. */
7259 gcc_unreachable ();
7262 /* If we have an rtl, find any enclosed object. Then see if we conflict
7263 with it. */
7264 if (exp_rtl)
7266 if (GET_CODE (exp_rtl) == SUBREG)
7268 exp_rtl = SUBREG_REG (exp_rtl);
7269 if (REG_P (exp_rtl)
7270 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7271 return 0;
7274 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7275 are memory and they conflict. */
7276 return ! (rtx_equal_p (x, exp_rtl)
7277 || (MEM_P (x) && MEM_P (exp_rtl)
7278 && true_dependence (exp_rtl, VOIDmode, x)));
7281 /* If we reach here, it is safe. */
7282 return 1;
7286 /* Return the highest power of two that EXP is known to be a multiple of.
7287 This is used in updating alignment of MEMs in array references. */
7289 unsigned HOST_WIDE_INT
7290 highest_pow2_factor (const_tree exp)
7292 unsigned HOST_WIDE_INT c0, c1;
7294 switch (TREE_CODE (exp))
7296 case INTEGER_CST:
7297 /* We can find the lowest bit that's a one. If the low
7298 HOST_BITS_PER_WIDE_INT bits are zero, return BIGGEST_ALIGNMENT.
7299 We need to handle this case since we can find it in a COND_EXPR,
7300 a MIN_EXPR, or a MAX_EXPR. If the constant overflows, we have an
7301 erroneous program, so return BIGGEST_ALIGNMENT to avoid any
7302 later ICE. */
7303 if (TREE_OVERFLOW (exp))
7304 return BIGGEST_ALIGNMENT;
7305 else
7307 /* Note: tree_low_cst is intentionally not used here,
7308 we don't care about the upper bits. */
7309 c0 = TREE_INT_CST_LOW (exp);
7310 c0 &= -c0;
7311 return c0 ? c0 : BIGGEST_ALIGNMENT;
7313 break;
7315 case PLUS_EXPR: case MINUS_EXPR: case MIN_EXPR: case MAX_EXPR:
7316 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7317 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7318 return MIN (c0, c1);
7320 case MULT_EXPR:
7321 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7322 c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7323 return c0 * c1;
7325 case ROUND_DIV_EXPR: case TRUNC_DIV_EXPR: case FLOOR_DIV_EXPR:
7326 case CEIL_DIV_EXPR:
7327 if (integer_pow2p (TREE_OPERAND (exp, 1))
7328 && host_integerp (TREE_OPERAND (exp, 1), 1))
7330 c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7331 c1 = tree_low_cst (TREE_OPERAND (exp, 1), 1);
7332 return MAX (1, c0 / c1);
7334 break;
7336 case BIT_AND_EXPR:
7337 /* The highest power of two of a bit-and expression is the maximum of
7338 that of its operands. We typically get here for a complex LHS and
7339 a constant negative power of two on the RHS to force an explicit
7340 alignment, so don't bother looking at the LHS. */
7341 return highest_pow2_factor (TREE_OPERAND (exp, 1));
7343 CASE_CONVERT:
7344 case SAVE_EXPR:
7345 return highest_pow2_factor (TREE_OPERAND (exp, 0));
7347 case COMPOUND_EXPR:
7348 return highest_pow2_factor (TREE_OPERAND (exp, 1));
7350 case COND_EXPR:
7351 c0 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7352 c1 = highest_pow2_factor (TREE_OPERAND (exp, 2));
7353 return MIN (c0, c1);
7355 default:
7356 break;
7359 return 1;
7362 /* Similar, except that the alignment requirements of TARGET are
7363 taken into account. Assume it is at least as aligned as its
7364 type, unless it is a COMPONENT_REF in which case the layout of
7365 the structure gives the alignment. */
7367 static unsigned HOST_WIDE_INT
7368 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7370 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7371 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7373 return MAX (factor, talign);
7376 #ifdef HAVE_conditional_move
7377 /* Convert the tree comparision code TCODE to the rtl one where the
7378 signedness is UNSIGNEDP. */
7380 static enum rtx_code
7381 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7383 enum rtx_code code;
7384 switch (tcode)
7386 case EQ_EXPR:
7387 code = EQ;
7388 break;
7389 case NE_EXPR:
7390 code = NE;
7391 break;
7392 case LT_EXPR:
7393 code = unsignedp ? LTU : LT;
7394 break;
7395 case LE_EXPR:
7396 code = unsignedp ? LEU : LE;
7397 break;
7398 case GT_EXPR:
7399 code = unsignedp ? GTU : GT;
7400 break;
7401 case GE_EXPR:
7402 code = unsignedp ? GEU : GE;
7403 break;
7404 case UNORDERED_EXPR:
7405 code = UNORDERED;
7406 break;
7407 case ORDERED_EXPR:
7408 code = ORDERED;
7409 break;
7410 case UNLT_EXPR:
7411 code = UNLT;
7412 break;
7413 case UNLE_EXPR:
7414 code = UNLE;
7415 break;
7416 case UNGT_EXPR:
7417 code = UNGT;
7418 break;
7419 case UNGE_EXPR:
7420 code = UNGE;
7421 break;
7422 case UNEQ_EXPR:
7423 code = UNEQ;
7424 break;
7425 case LTGT_EXPR:
7426 code = LTGT;
7427 break;
7429 default:
7430 gcc_unreachable ();
7432 return code;
7434 #endif
7436 /* Subroutine of expand_expr. Expand the two operands of a binary
7437 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7438 The value may be stored in TARGET if TARGET is nonzero. The
7439 MODIFIER argument is as documented by expand_expr. */
7441 static void
7442 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7443 enum expand_modifier modifier)
7445 if (! safe_from_p (target, exp1, 1))
7446 target = 0;
7447 if (operand_equal_p (exp0, exp1, 0))
7449 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7450 *op1 = copy_rtx (*op0);
7452 else
7454 /* If we need to preserve evaluation order, copy exp0 into its own
7455 temporary variable so that it can't be clobbered by exp1. */
7456 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7457 exp0 = save_expr (exp0);
7458 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7459 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7464 /* Return a MEM that contains constant EXP. DEFER is as for
7465 output_constant_def and MODIFIER is as for expand_expr. */
7467 static rtx
7468 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7470 rtx mem;
7472 mem = output_constant_def (exp, defer);
7473 if (modifier != EXPAND_INITIALIZER)
7474 mem = use_anchored_address (mem);
7475 return mem;
7478 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7479 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7481 static rtx
7482 expand_expr_addr_expr_1 (tree exp, rtx target, enum machine_mode tmode,
7483 enum expand_modifier modifier, addr_space_t as)
7485 rtx result, subtarget;
7486 tree inner, offset;
7487 HOST_WIDE_INT bitsize, bitpos;
7488 int volatilep, unsignedp;
7489 enum machine_mode mode1;
7491 /* If we are taking the address of a constant and are at the top level,
7492 we have to use output_constant_def since we can't call force_const_mem
7493 at top level. */
7494 /* ??? This should be considered a front-end bug. We should not be
7495 generating ADDR_EXPR of something that isn't an LVALUE. The only
7496 exception here is STRING_CST. */
7497 if (CONSTANT_CLASS_P (exp))
7499 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7500 if (modifier < EXPAND_SUM)
7501 result = force_operand (result, target);
7502 return result;
7505 /* Everything must be something allowed by is_gimple_addressable. */
7506 switch (TREE_CODE (exp))
7508 case INDIRECT_REF:
7509 /* This case will happen via recursion for &a->b. */
7510 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7512 case MEM_REF:
7514 tree tem = TREE_OPERAND (exp, 0);
7515 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7516 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7517 return expand_expr (tem, target, tmode, modifier);
7520 case CONST_DECL:
7521 /* Expand the initializer like constants above. */
7522 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7523 0, modifier), 0);
7524 if (modifier < EXPAND_SUM)
7525 result = force_operand (result, target);
7526 return result;
7528 case REALPART_EXPR:
7529 /* The real part of the complex number is always first, therefore
7530 the address is the same as the address of the parent object. */
7531 offset = 0;
7532 bitpos = 0;
7533 inner = TREE_OPERAND (exp, 0);
7534 break;
7536 case IMAGPART_EXPR:
7537 /* The imaginary part of the complex number is always second.
7538 The expression is therefore always offset by the size of the
7539 scalar type. */
7540 offset = 0;
7541 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7542 inner = TREE_OPERAND (exp, 0);
7543 break;
7545 default:
7546 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7547 expand_expr, as that can have various side effects; LABEL_DECLs for
7548 example, may not have their DECL_RTL set yet. Expand the rtl of
7549 CONSTRUCTORs too, which should yield a memory reference for the
7550 constructor's contents. Assume language specific tree nodes can
7551 be expanded in some interesting way. */
7552 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7553 if (DECL_P (exp)
7554 || TREE_CODE (exp) == CONSTRUCTOR
7555 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7557 result = expand_expr (exp, target, tmode,
7558 modifier == EXPAND_INITIALIZER
7559 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7561 /* If the DECL isn't in memory, then the DECL wasn't properly
7562 marked TREE_ADDRESSABLE, which will be either a front-end
7563 or a tree optimizer bug. */
7565 if (TREE_ADDRESSABLE (exp)
7566 && ! MEM_P (result)
7567 && ! targetm.calls.allocate_stack_slots_for_args())
7569 error ("local frame unavailable (naked function?)");
7570 return result;
7572 else
7573 gcc_assert (MEM_P (result));
7574 result = XEXP (result, 0);
7576 /* ??? Is this needed anymore? */
7577 if (DECL_P (exp))
7578 TREE_USED (exp) = 1;
7580 if (modifier != EXPAND_INITIALIZER
7581 && modifier != EXPAND_CONST_ADDRESS
7582 && modifier != EXPAND_SUM)
7583 result = force_operand (result, target);
7584 return result;
7587 /* Pass FALSE as the last argument to get_inner_reference although
7588 we are expanding to RTL. The rationale is that we know how to
7589 handle "aligning nodes" here: we can just bypass them because
7590 they won't change the final object whose address will be returned
7591 (they actually exist only for that purpose). */
7592 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7593 &mode1, &unsignedp, &volatilep, false);
7594 break;
7597 /* We must have made progress. */
7598 gcc_assert (inner != exp);
7600 subtarget = offset || bitpos ? NULL_RTX : target;
7601 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7602 inner alignment, force the inner to be sufficiently aligned. */
7603 if (CONSTANT_CLASS_P (inner)
7604 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7606 inner = copy_node (inner);
7607 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7608 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7609 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7611 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7613 if (offset)
7615 rtx tmp;
7617 if (modifier != EXPAND_NORMAL)
7618 result = force_operand (result, NULL);
7619 tmp = expand_expr (offset, NULL_RTX, tmode,
7620 modifier == EXPAND_INITIALIZER
7621 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7623 result = convert_memory_address_addr_space (tmode, result, as);
7624 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7626 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7627 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7628 else
7630 subtarget = bitpos ? NULL_RTX : target;
7631 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7632 1, OPTAB_LIB_WIDEN);
7636 if (bitpos)
7638 /* Someone beforehand should have rejected taking the address
7639 of such an object. */
7640 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7642 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7643 if (modifier < EXPAND_SUM)
7644 result = force_operand (result, target);
7647 return result;
7650 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7651 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7653 static rtx
7654 expand_expr_addr_expr (tree exp, rtx target, enum machine_mode tmode,
7655 enum expand_modifier modifier)
7657 addr_space_t as = ADDR_SPACE_GENERIC;
7658 enum machine_mode address_mode = Pmode;
7659 enum machine_mode pointer_mode = ptr_mode;
7660 enum machine_mode rmode;
7661 rtx result;
7663 /* Target mode of VOIDmode says "whatever's natural". */
7664 if (tmode == VOIDmode)
7665 tmode = TYPE_MODE (TREE_TYPE (exp));
7667 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7669 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7670 address_mode = targetm.addr_space.address_mode (as);
7671 pointer_mode = targetm.addr_space.pointer_mode (as);
7674 /* We can get called with some Weird Things if the user does silliness
7675 like "(short) &a". In that case, convert_memory_address won't do
7676 the right thing, so ignore the given target mode. */
7677 if (tmode != address_mode && tmode != pointer_mode)
7678 tmode = address_mode;
7680 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7681 tmode, modifier, as);
7683 /* Despite expand_expr claims concerning ignoring TMODE when not
7684 strictly convenient, stuff breaks if we don't honor it. Note
7685 that combined with the above, we only do this for pointer modes. */
7686 rmode = GET_MODE (result);
7687 if (rmode == VOIDmode)
7688 rmode = tmode;
7689 if (rmode != tmode)
7690 result = convert_memory_address_addr_space (tmode, result, as);
7692 return result;
7695 /* Generate code for computing CONSTRUCTOR EXP.
7696 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7697 is TRUE, instead of creating a temporary variable in memory
7698 NULL is returned and the caller needs to handle it differently. */
7700 static rtx
7701 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7702 bool avoid_temp_mem)
7704 tree type = TREE_TYPE (exp);
7705 enum machine_mode mode = TYPE_MODE (type);
7707 /* Try to avoid creating a temporary at all. This is possible
7708 if all of the initializer is zero.
7709 FIXME: try to handle all [0..255] initializers we can handle
7710 with memset. */
7711 if (TREE_STATIC (exp)
7712 && !TREE_ADDRESSABLE (exp)
7713 && target != 0 && mode == BLKmode
7714 && all_zeros_p (exp))
7716 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7717 return target;
7720 /* All elts simple constants => refer to a constant in memory. But
7721 if this is a non-BLKmode mode, let it store a field at a time
7722 since that should make a CONST_INT or CONST_DOUBLE when we
7723 fold. Likewise, if we have a target we can use, it is best to
7724 store directly into the target unless the type is large enough
7725 that memcpy will be used. If we are making an initializer and
7726 all operands are constant, put it in memory as well.
7728 FIXME: Avoid trying to fill vector constructors piece-meal.
7729 Output them with output_constant_def below unless we're sure
7730 they're zeros. This should go away when vector initializers
7731 are treated like VECTOR_CST instead of arrays. */
7732 if ((TREE_STATIC (exp)
7733 && ((mode == BLKmode
7734 && ! (target != 0 && safe_from_p (target, exp, 1)))
7735 || TREE_ADDRESSABLE (exp)
7736 || (host_integerp (TYPE_SIZE_UNIT (type), 1)
7737 && (! MOVE_BY_PIECES_P
7738 (tree_low_cst (TYPE_SIZE_UNIT (type), 1),
7739 TYPE_ALIGN (type)))
7740 && ! mostly_zeros_p (exp))))
7741 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7742 && TREE_CONSTANT (exp)))
7744 rtx constructor;
7746 if (avoid_temp_mem)
7747 return NULL_RTX;
7749 constructor = expand_expr_constant (exp, 1, modifier);
7751 if (modifier != EXPAND_CONST_ADDRESS
7752 && modifier != EXPAND_INITIALIZER
7753 && modifier != EXPAND_SUM)
7754 constructor = validize_mem (constructor);
7756 return constructor;
7759 /* Handle calls that pass values in multiple non-contiguous
7760 locations. The Irix 6 ABI has examples of this. */
7761 if (target == 0 || ! safe_from_p (target, exp, 1)
7762 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7764 if (avoid_temp_mem)
7765 return NULL_RTX;
7767 target
7768 = assign_temp (build_qualified_type (type, (TYPE_QUALS (type)
7769 | (TREE_READONLY (exp)
7770 * TYPE_QUAL_CONST))),
7771 0, TREE_ADDRESSABLE (exp), 1);
7774 store_constructor (exp, target, 0, int_expr_size (exp));
7775 return target;
7779 /* expand_expr: generate code for computing expression EXP.
7780 An rtx for the computed value is returned. The value is never null.
7781 In the case of a void EXP, const0_rtx is returned.
7783 The value may be stored in TARGET if TARGET is nonzero.
7784 TARGET is just a suggestion; callers must assume that
7785 the rtx returned may not be the same as TARGET.
7787 If TARGET is CONST0_RTX, it means that the value will be ignored.
7789 If TMODE is not VOIDmode, it suggests generating the
7790 result in mode TMODE. But this is done only when convenient.
7791 Otherwise, TMODE is ignored and the value generated in its natural mode.
7792 TMODE is just a suggestion; callers must assume that
7793 the rtx returned may not have mode TMODE.
7795 Note that TARGET may have neither TMODE nor MODE. In that case, it
7796 probably will not be used.
7798 If MODIFIER is EXPAND_SUM then when EXP is an addition
7799 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7800 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7801 products as above, or REG or MEM, or constant.
7802 Ordinarily in such cases we would output mul or add instructions
7803 and then return a pseudo reg containing the sum.
7805 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7806 it also marks a label as absolutely required (it can't be dead).
7807 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7808 This is used for outputting expressions used in initializers.
7810 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7811 with a constant address even if that address is not normally legitimate.
7812 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7814 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7815 a call parameter. Such targets require special care as we haven't yet
7816 marked TARGET so that it's safe from being trashed by libcalls. We
7817 don't want to use TARGET for anything but the final result;
7818 Intermediate values must go elsewhere. Additionally, calls to
7819 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7821 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7822 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7823 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7824 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7825 recursively. */
7828 expand_expr_real (tree exp, rtx target, enum machine_mode tmode,
7829 enum expand_modifier modifier, rtx *alt_rtl)
7831 rtx ret;
7833 /* Handle ERROR_MARK before anybody tries to access its type. */
7834 if (TREE_CODE (exp) == ERROR_MARK
7835 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7837 ret = CONST0_RTX (tmode);
7838 return ret ? ret : const0_rtx;
7841 /* If this is an expression of some kind and it has an associated line
7842 number, then emit the line number before expanding the expression.
7844 We need to save and restore the file and line information so that
7845 errors discovered during expansion are emitted with the right
7846 information. It would be better of the diagnostic routines
7847 used the file/line information embedded in the tree nodes rather
7848 than globals. */
7849 if (cfun && EXPR_HAS_LOCATION (exp))
7851 location_t saved_location = input_location;
7852 location_t saved_curr_loc = get_curr_insn_source_location ();
7853 tree saved_block = get_curr_insn_block ();
7854 input_location = EXPR_LOCATION (exp);
7855 set_curr_insn_source_location (input_location);
7857 /* Record where the insns produced belong. */
7858 set_curr_insn_block (TREE_BLOCK (exp));
7860 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7862 input_location = saved_location;
7863 set_curr_insn_block (saved_block);
7864 set_curr_insn_source_location (saved_curr_loc);
7866 else
7868 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7871 return ret;
7874 /* Try to expand the conditional expression which is represented by
7875 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
7876 return the rtl reg which repsents the result. Otherwise return
7877 NULL_RTL. */
7879 static rtx
7880 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7881 tree treeop1 ATTRIBUTE_UNUSED,
7882 tree treeop2 ATTRIBUTE_UNUSED)
7884 #ifdef HAVE_conditional_move
7885 rtx insn;
7886 rtx op00, op01, op1, op2;
7887 enum rtx_code comparison_code;
7888 enum machine_mode comparison_mode;
7889 gimple srcstmt;
7890 rtx temp;
7891 tree type = TREE_TYPE (treeop1);
7892 int unsignedp = TYPE_UNSIGNED (type);
7893 enum machine_mode mode = TYPE_MODE (type);
7895 temp = assign_temp (type, 0, 0, 1);
7897 /* If we cannot do a conditional move on the mode, try doing it
7898 with the promoted mode. */
7899 if (!can_conditionally_move_p (mode))
7900 mode = promote_mode (type, mode, &unsignedp);
7902 if (!can_conditionally_move_p (mode))
7903 return NULL_RTX;
7905 start_sequence ();
7906 expand_operands (treeop1, treeop2,
7907 temp, &op1, &op2, EXPAND_NORMAL);
7909 if (TREE_CODE (treeop0) == SSA_NAME
7910 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7912 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7913 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7914 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7915 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7916 comparison_mode = TYPE_MODE (type);
7917 unsignedp = TYPE_UNSIGNED (type);
7918 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7920 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
7922 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7923 enum tree_code cmpcode = TREE_CODE (treeop0);
7924 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
7925 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
7926 unsignedp = TYPE_UNSIGNED (type);
7927 comparison_mode = TYPE_MODE (type);
7928 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7930 else
7932 op00 = expand_normal (treeop0);
7933 op01 = const0_rtx;
7934 comparison_code = NE;
7935 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
7938 if (GET_MODE (op1) != mode)
7939 op1 = gen_lowpart (mode, op1);
7941 if (GET_MODE (op2) != mode)
7942 op2 = gen_lowpart (mode, op2);
7944 /* Try to emit the conditional move. */
7945 insn = emit_conditional_move (temp, comparison_code,
7946 op00, op01, comparison_mode,
7947 op1, op2, mode,
7948 unsignedp);
7950 /* If we could do the conditional move, emit the sequence,
7951 and return. */
7952 if (insn)
7954 rtx seq = get_insns ();
7955 end_sequence ();
7956 emit_insn (seq);
7957 return temp;
7960 /* Otherwise discard the sequence and fall back to code with
7961 branches. */
7962 end_sequence ();
7963 #endif
7964 return NULL_RTX;
7968 expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode,
7969 enum expand_modifier modifier)
7971 rtx op0, op1, op2, temp;
7972 tree type;
7973 int unsignedp;
7974 enum machine_mode mode;
7975 enum tree_code code = ops->code;
7976 optab this_optab;
7977 rtx subtarget, original_target;
7978 int ignore;
7979 bool reduce_bit_field;
7980 location_t loc = ops->location;
7981 tree treeop0, treeop1, treeop2;
7982 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
7983 ? reduce_to_bit_field_precision ((expr), \
7984 target, \
7985 type) \
7986 : (expr))
7988 type = ops->type;
7989 mode = TYPE_MODE (type);
7990 unsignedp = TYPE_UNSIGNED (type);
7992 treeop0 = ops->op0;
7993 treeop1 = ops->op1;
7994 treeop2 = ops->op2;
7996 /* We should be called only on simple (binary or unary) expressions,
7997 exactly those that are valid in gimple expressions that aren't
7998 GIMPLE_SINGLE_RHS (or invalid). */
7999 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8000 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8001 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8003 ignore = (target == const0_rtx
8004 || ((CONVERT_EXPR_CODE_P (code)
8005 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8006 && TREE_CODE (type) == VOID_TYPE));
8008 /* We should be called only if we need the result. */
8009 gcc_assert (!ignore);
8011 /* An operation in what may be a bit-field type needs the
8012 result to be reduced to the precision of the bit-field type,
8013 which is narrower than that of the type's mode. */
8014 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8015 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8017 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8018 target = 0;
8020 /* Use subtarget as the target for operand 0 of a binary operation. */
8021 subtarget = get_subtarget (target);
8022 original_target = target;
8024 switch (code)
8026 case NON_LVALUE_EXPR:
8027 case PAREN_EXPR:
8028 CASE_CONVERT:
8029 if (treeop0 == error_mark_node)
8030 return const0_rtx;
8032 if (TREE_CODE (type) == UNION_TYPE)
8034 tree valtype = TREE_TYPE (treeop0);
8036 /* If both input and output are BLKmode, this conversion isn't doing
8037 anything except possibly changing memory attribute. */
8038 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8040 rtx result = expand_expr (treeop0, target, tmode,
8041 modifier);
8043 result = copy_rtx (result);
8044 set_mem_attributes (result, type, 0);
8045 return result;
8048 if (target == 0)
8050 if (TYPE_MODE (type) != BLKmode)
8051 target = gen_reg_rtx (TYPE_MODE (type));
8052 else
8053 target = assign_temp (type, 0, 1, 1);
8056 if (MEM_P (target))
8057 /* Store data into beginning of memory target. */
8058 store_expr (treeop0,
8059 adjust_address (target, TYPE_MODE (valtype), 0),
8060 modifier == EXPAND_STACK_PARM,
8061 false);
8063 else
8065 gcc_assert (REG_P (target));
8067 /* Store this field into a union of the proper type. */
8068 store_field (target,
8069 MIN ((int_size_in_bytes (TREE_TYPE
8070 (treeop0))
8071 * BITS_PER_UNIT),
8072 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8073 0, 0, 0, TYPE_MODE (valtype), treeop0,
8074 type, 0, false);
8077 /* Return the entire union. */
8078 return target;
8081 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8083 op0 = expand_expr (treeop0, target, VOIDmode,
8084 modifier);
8086 /* If the signedness of the conversion differs and OP0 is
8087 a promoted SUBREG, clear that indication since we now
8088 have to do the proper extension. */
8089 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8090 && GET_CODE (op0) == SUBREG)
8091 SUBREG_PROMOTED_VAR_P (op0) = 0;
8093 return REDUCE_BIT_FIELD (op0);
8096 op0 = expand_expr (treeop0, NULL_RTX, mode,
8097 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8098 if (GET_MODE (op0) == mode)
8101 /* If OP0 is a constant, just convert it into the proper mode. */
8102 else if (CONSTANT_P (op0))
8104 tree inner_type = TREE_TYPE (treeop0);
8105 enum machine_mode inner_mode = GET_MODE (op0);
8107 if (inner_mode == VOIDmode)
8108 inner_mode = TYPE_MODE (inner_type);
8110 if (modifier == EXPAND_INITIALIZER)
8111 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8112 subreg_lowpart_offset (mode,
8113 inner_mode));
8114 else
8115 op0= convert_modes (mode, inner_mode, op0,
8116 TYPE_UNSIGNED (inner_type));
8119 else if (modifier == EXPAND_INITIALIZER)
8120 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8122 else if (target == 0)
8123 op0 = convert_to_mode (mode, op0,
8124 TYPE_UNSIGNED (TREE_TYPE
8125 (treeop0)));
8126 else
8128 convert_move (target, op0,
8129 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8130 op0 = target;
8133 return REDUCE_BIT_FIELD (op0);
8135 case ADDR_SPACE_CONVERT_EXPR:
8137 tree treeop0_type = TREE_TYPE (treeop0);
8138 addr_space_t as_to;
8139 addr_space_t as_from;
8141 gcc_assert (POINTER_TYPE_P (type));
8142 gcc_assert (POINTER_TYPE_P (treeop0_type));
8144 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8145 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8147 /* Conversions between pointers to the same address space should
8148 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8149 gcc_assert (as_to != as_from);
8151 /* Ask target code to handle conversion between pointers
8152 to overlapping address spaces. */
8153 if (targetm.addr_space.subset_p (as_to, as_from)
8154 || targetm.addr_space.subset_p (as_from, as_to))
8156 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8157 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8158 gcc_assert (op0);
8159 return op0;
8162 /* For disjoint address spaces, converting anything but
8163 a null pointer invokes undefined behaviour. We simply
8164 always return a null pointer here. */
8165 return CONST0_RTX (mode);
8168 case POINTER_PLUS_EXPR:
8169 /* Even though the sizetype mode and the pointer's mode can be different
8170 expand is able to handle this correctly and get the correct result out
8171 of the PLUS_EXPR code. */
8172 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8173 if sizetype precision is smaller than pointer precision. */
8174 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8175 treeop1 = fold_convert_loc (loc, type,
8176 fold_convert_loc (loc, ssizetype,
8177 treeop1));
8178 /* If sizetype precision is larger than pointer precision, truncate the
8179 offset to have matching modes. */
8180 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8181 treeop1 = fold_convert_loc (loc, type, treeop1);
8183 case PLUS_EXPR:
8184 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8185 something else, make sure we add the register to the constant and
8186 then to the other thing. This case can occur during strength
8187 reduction and doing it this way will produce better code if the
8188 frame pointer or argument pointer is eliminated.
8190 fold-const.c will ensure that the constant is always in the inner
8191 PLUS_EXPR, so the only case we need to do anything about is if
8192 sp, ap, or fp is our second argument, in which case we must swap
8193 the innermost first argument and our second argument. */
8195 if (TREE_CODE (treeop0) == PLUS_EXPR
8196 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8197 && TREE_CODE (treeop1) == VAR_DECL
8198 && (DECL_RTL (treeop1) == frame_pointer_rtx
8199 || DECL_RTL (treeop1) == stack_pointer_rtx
8200 || DECL_RTL (treeop1) == arg_pointer_rtx))
8202 gcc_unreachable ();
8205 /* If the result is to be ptr_mode and we are adding an integer to
8206 something, we might be forming a constant. So try to use
8207 plus_constant. If it produces a sum and we can't accept it,
8208 use force_operand. This allows P = &ARR[const] to generate
8209 efficient code on machines where a SYMBOL_REF is not a valid
8210 address.
8212 If this is an EXPAND_SUM call, always return the sum. */
8213 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8214 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8216 if (modifier == EXPAND_STACK_PARM)
8217 target = 0;
8218 if (TREE_CODE (treeop0) == INTEGER_CST
8219 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8220 && TREE_CONSTANT (treeop1))
8222 rtx constant_part;
8224 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8225 EXPAND_SUM);
8226 /* Use immed_double_const to ensure that the constant is
8227 truncated according to the mode of OP1, then sign extended
8228 to a HOST_WIDE_INT. Using the constant directly can result
8229 in non-canonical RTL in a 64x32 cross compile. */
8230 constant_part
8231 = immed_double_const (TREE_INT_CST_LOW (treeop0),
8232 (HOST_WIDE_INT) 0,
8233 TYPE_MODE (TREE_TYPE (treeop1)));
8234 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8235 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8236 op1 = force_operand (op1, target);
8237 return REDUCE_BIT_FIELD (op1);
8240 else if (TREE_CODE (treeop1) == INTEGER_CST
8241 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8242 && TREE_CONSTANT (treeop0))
8244 rtx constant_part;
8246 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8247 (modifier == EXPAND_INITIALIZER
8248 ? EXPAND_INITIALIZER : EXPAND_SUM));
8249 if (! CONSTANT_P (op0))
8251 op1 = expand_expr (treeop1, NULL_RTX,
8252 VOIDmode, modifier);
8253 /* Return a PLUS if modifier says it's OK. */
8254 if (modifier == EXPAND_SUM
8255 || modifier == EXPAND_INITIALIZER)
8256 return simplify_gen_binary (PLUS, mode, op0, op1);
8257 goto binop2;
8259 /* Use immed_double_const to ensure that the constant is
8260 truncated according to the mode of OP1, then sign extended
8261 to a HOST_WIDE_INT. Using the constant directly can result
8262 in non-canonical RTL in a 64x32 cross compile. */
8263 constant_part
8264 = immed_double_const (TREE_INT_CST_LOW (treeop1),
8265 (HOST_WIDE_INT) 0,
8266 TYPE_MODE (TREE_TYPE (treeop0)));
8267 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8268 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8269 op0 = force_operand (op0, target);
8270 return REDUCE_BIT_FIELD (op0);
8274 /* Use TER to expand pointer addition of a negated value
8275 as pointer subtraction. */
8276 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8277 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8278 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8279 && TREE_CODE (treeop1) == SSA_NAME
8280 && TYPE_MODE (TREE_TYPE (treeop0))
8281 == TYPE_MODE (TREE_TYPE (treeop1)))
8283 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8284 if (def)
8286 treeop1 = gimple_assign_rhs1 (def);
8287 code = MINUS_EXPR;
8288 goto do_minus;
8292 /* No sense saving up arithmetic to be done
8293 if it's all in the wrong mode to form part of an address.
8294 And force_operand won't know whether to sign-extend or
8295 zero-extend. */
8296 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8297 || mode != ptr_mode)
8299 expand_operands (treeop0, treeop1,
8300 subtarget, &op0, &op1, EXPAND_NORMAL);
8301 if (op0 == const0_rtx)
8302 return op1;
8303 if (op1 == const0_rtx)
8304 return op0;
8305 goto binop2;
8308 expand_operands (treeop0, treeop1,
8309 subtarget, &op0, &op1, modifier);
8310 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8312 case MINUS_EXPR:
8313 do_minus:
8314 /* For initializers, we are allowed to return a MINUS of two
8315 symbolic constants. Here we handle all cases when both operands
8316 are constant. */
8317 /* Handle difference of two symbolic constants,
8318 for the sake of an initializer. */
8319 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8320 && really_constant_p (treeop0)
8321 && really_constant_p (treeop1))
8323 expand_operands (treeop0, treeop1,
8324 NULL_RTX, &op0, &op1, modifier);
8326 /* If the last operand is a CONST_INT, use plus_constant of
8327 the negated constant. Else make the MINUS. */
8328 if (CONST_INT_P (op1))
8329 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8330 -INTVAL (op1)));
8331 else
8332 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8335 /* No sense saving up arithmetic to be done
8336 if it's all in the wrong mode to form part of an address.
8337 And force_operand won't know whether to sign-extend or
8338 zero-extend. */
8339 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8340 || mode != ptr_mode)
8341 goto binop;
8343 expand_operands (treeop0, treeop1,
8344 subtarget, &op0, &op1, modifier);
8346 /* Convert A - const to A + (-const). */
8347 if (CONST_INT_P (op1))
8349 op1 = negate_rtx (mode, op1);
8350 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8353 goto binop2;
8355 case WIDEN_MULT_PLUS_EXPR:
8356 case WIDEN_MULT_MINUS_EXPR:
8357 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8358 op2 = expand_normal (treeop2);
8359 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8360 target, unsignedp);
8361 return target;
8363 case WIDEN_MULT_EXPR:
8364 /* If first operand is constant, swap them.
8365 Thus the following special case checks need only
8366 check the second operand. */
8367 if (TREE_CODE (treeop0) == INTEGER_CST)
8369 tree t1 = treeop0;
8370 treeop0 = treeop1;
8371 treeop1 = t1;
8374 /* First, check if we have a multiplication of one signed and one
8375 unsigned operand. */
8376 if (TREE_CODE (treeop1) != INTEGER_CST
8377 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8378 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8380 enum machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8381 this_optab = usmul_widen_optab;
8382 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8383 != CODE_FOR_nothing)
8385 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8386 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8387 EXPAND_NORMAL);
8388 else
8389 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8390 EXPAND_NORMAL);
8391 goto binop3;
8394 /* Check for a multiplication with matching signedness. */
8395 else if ((TREE_CODE (treeop1) == INTEGER_CST
8396 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8397 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8398 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8400 tree op0type = TREE_TYPE (treeop0);
8401 enum machine_mode innermode = TYPE_MODE (op0type);
8402 bool zextend_p = TYPE_UNSIGNED (op0type);
8403 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8404 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8406 if (TREE_CODE (treeop0) != INTEGER_CST)
8408 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8409 != CODE_FOR_nothing)
8411 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8412 EXPAND_NORMAL);
8413 temp = expand_widening_mult (mode, op0, op1, target,
8414 unsignedp, this_optab);
8415 return REDUCE_BIT_FIELD (temp);
8417 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8418 != CODE_FOR_nothing
8419 && innermode == word_mode)
8421 rtx htem, hipart;
8422 op0 = expand_normal (treeop0);
8423 if (TREE_CODE (treeop1) == INTEGER_CST)
8424 op1 = convert_modes (innermode, mode,
8425 expand_normal (treeop1), unsignedp);
8426 else
8427 op1 = expand_normal (treeop1);
8428 temp = expand_binop (mode, other_optab, op0, op1, target,
8429 unsignedp, OPTAB_LIB_WIDEN);
8430 hipart = gen_highpart (innermode, temp);
8431 htem = expand_mult_highpart_adjust (innermode, hipart,
8432 op0, op1, hipart,
8433 zextend_p);
8434 if (htem != hipart)
8435 emit_move_insn (hipart, htem);
8436 return REDUCE_BIT_FIELD (temp);
8440 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8441 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8442 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8443 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8445 case FMA_EXPR:
8447 optab opt = fma_optab;
8448 gimple def0, def2;
8450 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8451 call. */
8452 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8454 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8455 tree call_expr;
8457 gcc_assert (fn != NULL_TREE);
8458 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8459 return expand_builtin (call_expr, target, subtarget, mode, false);
8462 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8463 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8465 op0 = op2 = NULL;
8467 if (def0 && def2
8468 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8470 opt = fnms_optab;
8471 op0 = expand_normal (gimple_assign_rhs1 (def0));
8472 op2 = expand_normal (gimple_assign_rhs1 (def2));
8474 else if (def0
8475 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8477 opt = fnma_optab;
8478 op0 = expand_normal (gimple_assign_rhs1 (def0));
8480 else if (def2
8481 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8483 opt = fms_optab;
8484 op2 = expand_normal (gimple_assign_rhs1 (def2));
8487 if (op0 == NULL)
8488 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8489 if (op2 == NULL)
8490 op2 = expand_normal (treeop2);
8491 op1 = expand_normal (treeop1);
8493 return expand_ternary_op (TYPE_MODE (type), opt,
8494 op0, op1, op2, target, 0);
8497 case MULT_EXPR:
8498 /* If this is a fixed-point operation, then we cannot use the code
8499 below because "expand_mult" doesn't support sat/no-sat fixed-point
8500 multiplications. */
8501 if (ALL_FIXED_POINT_MODE_P (mode))
8502 goto binop;
8504 /* If first operand is constant, swap them.
8505 Thus the following special case checks need only
8506 check the second operand. */
8507 if (TREE_CODE (treeop0) == INTEGER_CST)
8509 tree t1 = treeop0;
8510 treeop0 = treeop1;
8511 treeop1 = t1;
8514 /* Attempt to return something suitable for generating an
8515 indexed address, for machines that support that. */
8517 if (modifier == EXPAND_SUM && mode == ptr_mode
8518 && host_integerp (treeop1, 0))
8520 tree exp1 = treeop1;
8522 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8523 EXPAND_SUM);
8525 if (!REG_P (op0))
8526 op0 = force_operand (op0, NULL_RTX);
8527 if (!REG_P (op0))
8528 op0 = copy_to_mode_reg (mode, op0);
8530 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8531 gen_int_mode (tree_low_cst (exp1, 0),
8532 TYPE_MODE (TREE_TYPE (exp1)))));
8535 if (modifier == EXPAND_STACK_PARM)
8536 target = 0;
8538 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8539 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8541 case TRUNC_DIV_EXPR:
8542 case FLOOR_DIV_EXPR:
8543 case CEIL_DIV_EXPR:
8544 case ROUND_DIV_EXPR:
8545 case EXACT_DIV_EXPR:
8546 /* If this is a fixed-point operation, then we cannot use the code
8547 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8548 divisions. */
8549 if (ALL_FIXED_POINT_MODE_P (mode))
8550 goto binop;
8552 if (modifier == EXPAND_STACK_PARM)
8553 target = 0;
8554 /* Possible optimization: compute the dividend with EXPAND_SUM
8555 then if the divisor is constant can optimize the case
8556 where some terms of the dividend have coeffs divisible by it. */
8557 expand_operands (treeop0, treeop1,
8558 subtarget, &op0, &op1, EXPAND_NORMAL);
8559 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8561 case RDIV_EXPR:
8562 goto binop;
8564 case TRUNC_MOD_EXPR:
8565 case FLOOR_MOD_EXPR:
8566 case CEIL_MOD_EXPR:
8567 case ROUND_MOD_EXPR:
8568 if (modifier == EXPAND_STACK_PARM)
8569 target = 0;
8570 expand_operands (treeop0, treeop1,
8571 subtarget, &op0, &op1, EXPAND_NORMAL);
8572 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8574 case FIXED_CONVERT_EXPR:
8575 op0 = expand_normal (treeop0);
8576 if (target == 0 || modifier == EXPAND_STACK_PARM)
8577 target = gen_reg_rtx (mode);
8579 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8580 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8581 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8582 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8583 else
8584 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8585 return target;
8587 case FIX_TRUNC_EXPR:
8588 op0 = expand_normal (treeop0);
8589 if (target == 0 || modifier == EXPAND_STACK_PARM)
8590 target = gen_reg_rtx (mode);
8591 expand_fix (target, op0, unsignedp);
8592 return target;
8594 case FLOAT_EXPR:
8595 op0 = expand_normal (treeop0);
8596 if (target == 0 || modifier == EXPAND_STACK_PARM)
8597 target = gen_reg_rtx (mode);
8598 /* expand_float can't figure out what to do if FROM has VOIDmode.
8599 So give it the correct mode. With -O, cse will optimize this. */
8600 if (GET_MODE (op0) == VOIDmode)
8601 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8602 op0);
8603 expand_float (target, op0,
8604 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8605 return target;
8607 case NEGATE_EXPR:
8608 op0 = expand_expr (treeop0, subtarget,
8609 VOIDmode, EXPAND_NORMAL);
8610 if (modifier == EXPAND_STACK_PARM)
8611 target = 0;
8612 temp = expand_unop (mode,
8613 optab_for_tree_code (NEGATE_EXPR, type,
8614 optab_default),
8615 op0, target, 0);
8616 gcc_assert (temp);
8617 return REDUCE_BIT_FIELD (temp);
8619 case ABS_EXPR:
8620 op0 = expand_expr (treeop0, subtarget,
8621 VOIDmode, EXPAND_NORMAL);
8622 if (modifier == EXPAND_STACK_PARM)
8623 target = 0;
8625 /* ABS_EXPR is not valid for complex arguments. */
8626 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8627 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8629 /* Unsigned abs is simply the operand. Testing here means we don't
8630 risk generating incorrect code below. */
8631 if (TYPE_UNSIGNED (type))
8632 return op0;
8634 return expand_abs (mode, op0, target, unsignedp,
8635 safe_from_p (target, treeop0, 1));
8637 case MAX_EXPR:
8638 case MIN_EXPR:
8639 target = original_target;
8640 if (target == 0
8641 || modifier == EXPAND_STACK_PARM
8642 || (MEM_P (target) && MEM_VOLATILE_P (target))
8643 || GET_MODE (target) != mode
8644 || (REG_P (target)
8645 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8646 target = gen_reg_rtx (mode);
8647 expand_operands (treeop0, treeop1,
8648 target, &op0, &op1, EXPAND_NORMAL);
8650 /* First try to do it with a special MIN or MAX instruction.
8651 If that does not win, use a conditional jump to select the proper
8652 value. */
8653 this_optab = optab_for_tree_code (code, type, optab_default);
8654 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8655 OPTAB_WIDEN);
8656 if (temp != 0)
8657 return temp;
8659 /* At this point, a MEM target is no longer useful; we will get better
8660 code without it. */
8662 if (! REG_P (target))
8663 target = gen_reg_rtx (mode);
8665 /* If op1 was placed in target, swap op0 and op1. */
8666 if (target != op0 && target == op1)
8668 temp = op0;
8669 op0 = op1;
8670 op1 = temp;
8673 /* We generate better code and avoid problems with op1 mentioning
8674 target by forcing op1 into a pseudo if it isn't a constant. */
8675 if (! CONSTANT_P (op1))
8676 op1 = force_reg (mode, op1);
8679 enum rtx_code comparison_code;
8680 rtx cmpop1 = op1;
8682 if (code == MAX_EXPR)
8683 comparison_code = unsignedp ? GEU : GE;
8684 else
8685 comparison_code = unsignedp ? LEU : LE;
8687 /* Canonicalize to comparisons against 0. */
8688 if (op1 == const1_rtx)
8690 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8691 or (a != 0 ? a : 1) for unsigned.
8692 For MIN we are safe converting (a <= 1 ? a : 1)
8693 into (a <= 0 ? a : 1) */
8694 cmpop1 = const0_rtx;
8695 if (code == MAX_EXPR)
8696 comparison_code = unsignedp ? NE : GT;
8698 if (op1 == constm1_rtx && !unsignedp)
8700 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8701 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8702 cmpop1 = const0_rtx;
8703 if (code == MIN_EXPR)
8704 comparison_code = LT;
8706 #ifdef HAVE_conditional_move
8707 /* Use a conditional move if possible. */
8708 if (can_conditionally_move_p (mode))
8710 rtx insn;
8712 /* ??? Same problem as in expmed.c: emit_conditional_move
8713 forces a stack adjustment via compare_from_rtx, and we
8714 lose the stack adjustment if the sequence we are about
8715 to create is discarded. */
8716 do_pending_stack_adjust ();
8718 start_sequence ();
8720 /* Try to emit the conditional move. */
8721 insn = emit_conditional_move (target, comparison_code,
8722 op0, cmpop1, mode,
8723 op0, op1, mode,
8724 unsignedp);
8726 /* If we could do the conditional move, emit the sequence,
8727 and return. */
8728 if (insn)
8730 rtx seq = get_insns ();
8731 end_sequence ();
8732 emit_insn (seq);
8733 return target;
8736 /* Otherwise discard the sequence and fall back to code with
8737 branches. */
8738 end_sequence ();
8740 #endif
8741 if (target != op0)
8742 emit_move_insn (target, op0);
8744 temp = gen_label_rtx ();
8745 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8746 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8747 -1);
8749 emit_move_insn (target, op1);
8750 emit_label (temp);
8751 return target;
8753 case BIT_NOT_EXPR:
8754 op0 = expand_expr (treeop0, subtarget,
8755 VOIDmode, EXPAND_NORMAL);
8756 if (modifier == EXPAND_STACK_PARM)
8757 target = 0;
8758 /* In case we have to reduce the result to bitfield precision
8759 for unsigned bitfield expand this as XOR with a proper constant
8760 instead. */
8761 if (reduce_bit_field && TYPE_UNSIGNED (type))
8762 temp = expand_binop (mode, xor_optab, op0,
8763 immed_double_int_const
8764 (double_int_mask (TYPE_PRECISION (type)), mode),
8765 target, 1, OPTAB_LIB_WIDEN);
8766 else
8767 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8768 gcc_assert (temp);
8769 return temp;
8771 /* ??? Can optimize bitwise operations with one arg constant.
8772 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8773 and (a bitwise1 b) bitwise2 b (etc)
8774 but that is probably not worth while. */
8776 case BIT_AND_EXPR:
8777 case BIT_IOR_EXPR:
8778 case BIT_XOR_EXPR:
8779 goto binop;
8781 case LROTATE_EXPR:
8782 case RROTATE_EXPR:
8783 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8784 || (GET_MODE_PRECISION (TYPE_MODE (type))
8785 == TYPE_PRECISION (type)));
8786 /* fall through */
8788 case LSHIFT_EXPR:
8789 case RSHIFT_EXPR:
8790 /* If this is a fixed-point operation, then we cannot use the code
8791 below because "expand_shift" doesn't support sat/no-sat fixed-point
8792 shifts. */
8793 if (ALL_FIXED_POINT_MODE_P (mode))
8794 goto binop;
8796 if (! safe_from_p (subtarget, treeop1, 1))
8797 subtarget = 0;
8798 if (modifier == EXPAND_STACK_PARM)
8799 target = 0;
8800 op0 = expand_expr (treeop0, subtarget,
8801 VOIDmode, EXPAND_NORMAL);
8802 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8803 unsignedp);
8804 if (code == LSHIFT_EXPR)
8805 temp = REDUCE_BIT_FIELD (temp);
8806 return temp;
8808 /* Could determine the answer when only additive constants differ. Also,
8809 the addition of one can be handled by changing the condition. */
8810 case LT_EXPR:
8811 case LE_EXPR:
8812 case GT_EXPR:
8813 case GE_EXPR:
8814 case EQ_EXPR:
8815 case NE_EXPR:
8816 case UNORDERED_EXPR:
8817 case ORDERED_EXPR:
8818 case UNLT_EXPR:
8819 case UNLE_EXPR:
8820 case UNGT_EXPR:
8821 case UNGE_EXPR:
8822 case UNEQ_EXPR:
8823 case LTGT_EXPR:
8824 temp = do_store_flag (ops,
8825 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8826 tmode != VOIDmode ? tmode : mode);
8827 if (temp)
8828 return temp;
8830 /* Use a compare and a jump for BLKmode comparisons, or for function
8831 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
8833 if ((target == 0
8834 || modifier == EXPAND_STACK_PARM
8835 || ! safe_from_p (target, treeop0, 1)
8836 || ! safe_from_p (target, treeop1, 1)
8837 /* Make sure we don't have a hard reg (such as function's return
8838 value) live across basic blocks, if not optimizing. */
8839 || (!optimize && REG_P (target)
8840 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8841 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8843 emit_move_insn (target, const0_rtx);
8845 op1 = gen_label_rtx ();
8846 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
8848 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8849 emit_move_insn (target, constm1_rtx);
8850 else
8851 emit_move_insn (target, const1_rtx);
8853 emit_label (op1);
8854 return target;
8856 case COMPLEX_EXPR:
8857 /* Get the rtx code of the operands. */
8858 op0 = expand_normal (treeop0);
8859 op1 = expand_normal (treeop1);
8861 if (!target)
8862 target = gen_reg_rtx (TYPE_MODE (type));
8864 /* Move the real (op0) and imaginary (op1) parts to their location. */
8865 write_complex_part (target, op0, false);
8866 write_complex_part (target, op1, true);
8868 return target;
8870 case WIDEN_SUM_EXPR:
8872 tree oprnd0 = treeop0;
8873 tree oprnd1 = treeop1;
8875 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8876 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
8877 target, unsignedp);
8878 return target;
8881 case REDUC_MAX_EXPR:
8882 case REDUC_MIN_EXPR:
8883 case REDUC_PLUS_EXPR:
8885 op0 = expand_normal (treeop0);
8886 this_optab = optab_for_tree_code (code, type, optab_default);
8887 temp = expand_unop (mode, this_optab, op0, target, unsignedp);
8888 gcc_assert (temp);
8889 return temp;
8892 case VEC_LSHIFT_EXPR:
8893 case VEC_RSHIFT_EXPR:
8895 target = expand_vec_shift_expr (ops, target);
8896 return target;
8899 case VEC_UNPACK_HI_EXPR:
8900 case VEC_UNPACK_LO_EXPR:
8902 op0 = expand_normal (treeop0);
8903 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
8904 target, unsignedp);
8905 gcc_assert (temp);
8906 return temp;
8909 case VEC_UNPACK_FLOAT_HI_EXPR:
8910 case VEC_UNPACK_FLOAT_LO_EXPR:
8912 op0 = expand_normal (treeop0);
8913 /* The signedness is determined from input operand. */
8914 temp = expand_widen_pattern_expr
8915 (ops, op0, NULL_RTX, NULL_RTX,
8916 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8918 gcc_assert (temp);
8919 return temp;
8922 case VEC_WIDEN_MULT_HI_EXPR:
8923 case VEC_WIDEN_MULT_LO_EXPR:
8925 tree oprnd0 = treeop0;
8926 tree oprnd1 = treeop1;
8928 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8929 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
8930 target, unsignedp);
8931 gcc_assert (target);
8932 return target;
8935 case VEC_WIDEN_LSHIFT_HI_EXPR:
8936 case VEC_WIDEN_LSHIFT_LO_EXPR:
8938 tree oprnd0 = treeop0;
8939 tree oprnd1 = treeop1;
8941 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8942 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
8943 target, unsignedp);
8944 gcc_assert (target);
8945 return target;
8948 case VEC_PACK_TRUNC_EXPR:
8949 case VEC_PACK_SAT_EXPR:
8950 case VEC_PACK_FIX_TRUNC_EXPR:
8951 mode = TYPE_MODE (TREE_TYPE (treeop0));
8952 goto binop;
8954 case VEC_PERM_EXPR:
8955 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
8956 op2 = expand_normal (treeop2);
8958 /* Careful here: if the target doesn't support integral vector modes,
8959 a constant selection vector could wind up smooshed into a normal
8960 integral constant. */
8961 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
8963 tree sel_type = TREE_TYPE (treeop2);
8964 enum machine_mode vmode
8965 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
8966 TYPE_VECTOR_SUBPARTS (sel_type));
8967 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
8968 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
8969 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
8971 else
8972 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
8974 temp = expand_vec_perm (mode, op0, op1, op2, target);
8975 gcc_assert (temp);
8976 return temp;
8978 case DOT_PROD_EXPR:
8980 tree oprnd0 = treeop0;
8981 tree oprnd1 = treeop1;
8982 tree oprnd2 = treeop2;
8983 rtx op2;
8985 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8986 op2 = expand_normal (oprnd2);
8987 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8988 target, unsignedp);
8989 return target;
8992 case REALIGN_LOAD_EXPR:
8994 tree oprnd0 = treeop0;
8995 tree oprnd1 = treeop1;
8996 tree oprnd2 = treeop2;
8997 rtx op2;
8999 this_optab = optab_for_tree_code (code, type, optab_default);
9000 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9001 op2 = expand_normal (oprnd2);
9002 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9003 target, unsignedp);
9004 gcc_assert (temp);
9005 return temp;
9008 case COND_EXPR:
9009 /* A COND_EXPR with its type being VOID_TYPE represents a
9010 conditional jump and is handled in
9011 expand_gimple_cond_expr. */
9012 gcc_assert (!VOID_TYPE_P (type));
9014 /* Note that COND_EXPRs whose type is a structure or union
9015 are required to be constructed to contain assignments of
9016 a temporary variable, so that we can evaluate them here
9017 for side effect only. If type is void, we must do likewise. */
9019 gcc_assert (!TREE_ADDRESSABLE (type)
9020 && !ignore
9021 && TREE_TYPE (treeop1) != void_type_node
9022 && TREE_TYPE (treeop2) != void_type_node);
9024 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9025 if (temp)
9026 return temp;
9028 /* If we are not to produce a result, we have no target. Otherwise,
9029 if a target was specified use it; it will not be used as an
9030 intermediate target unless it is safe. If no target, use a
9031 temporary. */
9033 if (modifier != EXPAND_STACK_PARM
9034 && original_target
9035 && safe_from_p (original_target, treeop0, 1)
9036 && GET_MODE (original_target) == mode
9037 && !MEM_P (original_target))
9038 temp = original_target;
9039 else
9040 temp = assign_temp (type, 0, 0, 1);
9042 do_pending_stack_adjust ();
9043 NO_DEFER_POP;
9044 op0 = gen_label_rtx ();
9045 op1 = gen_label_rtx ();
9046 jumpifnot (treeop0, op0, -1);
9047 store_expr (treeop1, temp,
9048 modifier == EXPAND_STACK_PARM,
9049 false);
9051 emit_jump_insn (gen_jump (op1));
9052 emit_barrier ();
9053 emit_label (op0);
9054 store_expr (treeop2, temp,
9055 modifier == EXPAND_STACK_PARM,
9056 false);
9058 emit_label (op1);
9059 OK_DEFER_POP;
9060 return temp;
9062 case VEC_COND_EXPR:
9063 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9064 return target;
9066 default:
9067 gcc_unreachable ();
9070 /* Here to do an ordinary binary operator. */
9071 binop:
9072 expand_operands (treeop0, treeop1,
9073 subtarget, &op0, &op1, EXPAND_NORMAL);
9074 binop2:
9075 this_optab = optab_for_tree_code (code, type, optab_default);
9076 binop3:
9077 if (modifier == EXPAND_STACK_PARM)
9078 target = 0;
9079 temp = expand_binop (mode, this_optab, op0, op1, target,
9080 unsignedp, OPTAB_LIB_WIDEN);
9081 gcc_assert (temp);
9082 /* Bitwise operations do not need bitfield reduction as we expect their
9083 operands being properly truncated. */
9084 if (code == BIT_XOR_EXPR
9085 || code == BIT_AND_EXPR
9086 || code == BIT_IOR_EXPR)
9087 return temp;
9088 return REDUCE_BIT_FIELD (temp);
9090 #undef REDUCE_BIT_FIELD
9093 expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode,
9094 enum expand_modifier modifier, rtx *alt_rtl)
9096 rtx op0, op1, temp, decl_rtl;
9097 tree type;
9098 int unsignedp;
9099 enum machine_mode mode;
9100 enum tree_code code = TREE_CODE (exp);
9101 rtx subtarget, original_target;
9102 int ignore;
9103 tree context;
9104 bool reduce_bit_field;
9105 location_t loc = EXPR_LOCATION (exp);
9106 struct separate_ops ops;
9107 tree treeop0, treeop1, treeop2;
9108 tree ssa_name = NULL_TREE;
9109 gimple g;
9111 type = TREE_TYPE (exp);
9112 mode = TYPE_MODE (type);
9113 unsignedp = TYPE_UNSIGNED (type);
9115 treeop0 = treeop1 = treeop2 = NULL_TREE;
9116 if (!VL_EXP_CLASS_P (exp))
9117 switch (TREE_CODE_LENGTH (code))
9119 default:
9120 case 3: treeop2 = TREE_OPERAND (exp, 2);
9121 case 2: treeop1 = TREE_OPERAND (exp, 1);
9122 case 1: treeop0 = TREE_OPERAND (exp, 0);
9123 case 0: break;
9125 ops.code = code;
9126 ops.type = type;
9127 ops.op0 = treeop0;
9128 ops.op1 = treeop1;
9129 ops.op2 = treeop2;
9130 ops.location = loc;
9132 ignore = (target == const0_rtx
9133 || ((CONVERT_EXPR_CODE_P (code)
9134 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9135 && TREE_CODE (type) == VOID_TYPE));
9137 /* An operation in what may be a bit-field type needs the
9138 result to be reduced to the precision of the bit-field type,
9139 which is narrower than that of the type's mode. */
9140 reduce_bit_field = (!ignore
9141 && INTEGRAL_TYPE_P (type)
9142 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9144 /* If we are going to ignore this result, we need only do something
9145 if there is a side-effect somewhere in the expression. If there
9146 is, short-circuit the most common cases here. Note that we must
9147 not call expand_expr with anything but const0_rtx in case this
9148 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9150 if (ignore)
9152 if (! TREE_SIDE_EFFECTS (exp))
9153 return const0_rtx;
9155 /* Ensure we reference a volatile object even if value is ignored, but
9156 don't do this if all we are doing is taking its address. */
9157 if (TREE_THIS_VOLATILE (exp)
9158 && TREE_CODE (exp) != FUNCTION_DECL
9159 && mode != VOIDmode && mode != BLKmode
9160 && modifier != EXPAND_CONST_ADDRESS)
9162 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9163 if (MEM_P (temp))
9164 copy_to_reg (temp);
9165 return const0_rtx;
9168 if (TREE_CODE_CLASS (code) == tcc_unary
9169 || code == COMPONENT_REF || code == INDIRECT_REF)
9170 return expand_expr (treeop0, const0_rtx, VOIDmode,
9171 modifier);
9173 else if (TREE_CODE_CLASS (code) == tcc_binary
9174 || TREE_CODE_CLASS (code) == tcc_comparison
9175 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9177 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9178 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9179 return const0_rtx;
9181 else if (code == BIT_FIELD_REF)
9183 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9184 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9185 expand_expr (treeop2, const0_rtx, VOIDmode, modifier);
9186 return const0_rtx;
9189 target = 0;
9192 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9193 target = 0;
9195 /* Use subtarget as the target for operand 0 of a binary operation. */
9196 subtarget = get_subtarget (target);
9197 original_target = target;
9199 switch (code)
9201 case LABEL_DECL:
9203 tree function = decl_function_context (exp);
9205 temp = label_rtx (exp);
9206 temp = gen_rtx_LABEL_REF (Pmode, temp);
9208 if (function != current_function_decl
9209 && function != 0)
9210 LABEL_REF_NONLOCAL_P (temp) = 1;
9212 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9213 return temp;
9216 case SSA_NAME:
9217 /* ??? ivopts calls expander, without any preparation from
9218 out-of-ssa. So fake instructions as if this was an access to the
9219 base variable. This unnecessarily allocates a pseudo, see how we can
9220 reuse it, if partition base vars have it set already. */
9221 if (!currently_expanding_to_rtl)
9222 return expand_expr_real_1 (SSA_NAME_VAR (exp), target, tmode, modifier,
9223 NULL);
9225 g = get_gimple_for_ssa_name (exp);
9226 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9227 if (g == NULL
9228 && modifier == EXPAND_INITIALIZER
9229 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9230 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9231 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9232 g = SSA_NAME_DEF_STMT (exp);
9233 if (g)
9235 rtx r = expand_expr_real (gimple_assign_rhs_to_tree (g), target,
9236 tmode, modifier, NULL);
9237 if (REG_P (r) && !REG_EXPR (r))
9238 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9239 return r;
9242 ssa_name = exp;
9243 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9244 exp = SSA_NAME_VAR (ssa_name);
9245 goto expand_decl_rtl;
9247 case PARM_DECL:
9248 case VAR_DECL:
9249 /* If a static var's type was incomplete when the decl was written,
9250 but the type is complete now, lay out the decl now. */
9251 if (DECL_SIZE (exp) == 0
9252 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9253 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9254 layout_decl (exp, 0);
9256 /* ... fall through ... */
9258 case FUNCTION_DECL:
9259 case RESULT_DECL:
9260 decl_rtl = DECL_RTL (exp);
9261 expand_decl_rtl:
9262 gcc_assert (decl_rtl);
9263 decl_rtl = copy_rtx (decl_rtl);
9264 /* Record writes to register variables. */
9265 if (modifier == EXPAND_WRITE
9266 && REG_P (decl_rtl)
9267 && HARD_REGISTER_P (decl_rtl))
9268 add_to_hard_reg_set (&crtl->asm_clobbers,
9269 GET_MODE (decl_rtl), REGNO (decl_rtl));
9271 /* Ensure variable marked as used even if it doesn't go through
9272 a parser. If it hasn't be used yet, write out an external
9273 definition. */
9274 TREE_USED (exp) = 1;
9276 /* Show we haven't gotten RTL for this yet. */
9277 temp = 0;
9279 /* Variables inherited from containing functions should have
9280 been lowered by this point. */
9281 context = decl_function_context (exp);
9282 gcc_assert (!context
9283 || context == current_function_decl
9284 || TREE_STATIC (exp)
9285 || DECL_EXTERNAL (exp)
9286 /* ??? C++ creates functions that are not TREE_STATIC. */
9287 || TREE_CODE (exp) == FUNCTION_DECL);
9289 /* This is the case of an array whose size is to be determined
9290 from its initializer, while the initializer is still being parsed.
9291 See expand_decl. */
9293 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9294 temp = validize_mem (decl_rtl);
9296 /* If DECL_RTL is memory, we are in the normal case and the
9297 address is not valid, get the address into a register. */
9299 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9301 if (alt_rtl)
9302 *alt_rtl = decl_rtl;
9303 decl_rtl = use_anchored_address (decl_rtl);
9304 if (modifier != EXPAND_CONST_ADDRESS
9305 && modifier != EXPAND_SUM
9306 && !memory_address_addr_space_p (DECL_MODE (exp),
9307 XEXP (decl_rtl, 0),
9308 MEM_ADDR_SPACE (decl_rtl)))
9309 temp = replace_equiv_address (decl_rtl,
9310 copy_rtx (XEXP (decl_rtl, 0)));
9313 /* If we got something, return it. But first, set the alignment
9314 if the address is a register. */
9315 if (temp != 0)
9317 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9318 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9320 return temp;
9323 /* If the mode of DECL_RTL does not match that of the decl,
9324 there are two cases: we are dealing with a BLKmode value
9325 that is returned in a register, or we are dealing with
9326 a promoted value. In the latter case, return a SUBREG
9327 of the wanted mode, but mark it so that we know that it
9328 was already extended. */
9329 if (REG_P (decl_rtl)
9330 && DECL_MODE (exp) != BLKmode
9331 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9333 enum machine_mode pmode;
9335 /* Get the signedness to be used for this variable. Ensure we get
9336 the same mode we got when the variable was declared. */
9337 if (code == SSA_NAME
9338 && (g = SSA_NAME_DEF_STMT (ssa_name))
9339 && gimple_code (g) == GIMPLE_CALL)
9341 gcc_assert (!gimple_call_internal_p (g));
9342 pmode = promote_function_mode (type, mode, &unsignedp,
9343 gimple_call_fntype (g),
9346 else
9347 pmode = promote_decl_mode (exp, &unsignedp);
9348 gcc_assert (GET_MODE (decl_rtl) == pmode);
9350 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9351 SUBREG_PROMOTED_VAR_P (temp) = 1;
9352 SUBREG_PROMOTED_UNSIGNED_SET (temp, unsignedp);
9353 return temp;
9356 return decl_rtl;
9358 case INTEGER_CST:
9359 temp = immed_double_const (TREE_INT_CST_LOW (exp),
9360 TREE_INT_CST_HIGH (exp), mode);
9362 return temp;
9364 case VECTOR_CST:
9366 tree tmp = NULL_TREE;
9367 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9368 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9369 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9370 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9371 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9372 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9373 return const_vector_from_tree (exp);
9374 if (GET_MODE_CLASS (mode) == MODE_INT)
9376 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9377 if (type_for_mode)
9378 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9380 if (!tmp)
9382 VEC(constructor_elt,gc) *v;
9383 unsigned i;
9384 v = VEC_alloc (constructor_elt, gc, VECTOR_CST_NELTS (exp));
9385 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9386 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9387 tmp = build_constructor (type, v);
9389 return expand_expr (tmp, ignore ? const0_rtx : target,
9390 tmode, modifier);
9393 case CONST_DECL:
9394 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9396 case REAL_CST:
9397 /* If optimized, generate immediate CONST_DOUBLE
9398 which will be turned into memory by reload if necessary.
9400 We used to force a register so that loop.c could see it. But
9401 this does not allow gen_* patterns to perform optimizations with
9402 the constants. It also produces two insns in cases like "x = 1.0;".
9403 On most machines, floating-point constants are not permitted in
9404 many insns, so we'd end up copying it to a register in any case.
9406 Now, we do the copying in expand_binop, if appropriate. */
9407 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9408 TYPE_MODE (TREE_TYPE (exp)));
9410 case FIXED_CST:
9411 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9412 TYPE_MODE (TREE_TYPE (exp)));
9414 case COMPLEX_CST:
9415 /* Handle evaluating a complex constant in a CONCAT target. */
9416 if (original_target && GET_CODE (original_target) == CONCAT)
9418 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9419 rtx rtarg, itarg;
9421 rtarg = XEXP (original_target, 0);
9422 itarg = XEXP (original_target, 1);
9424 /* Move the real and imaginary parts separately. */
9425 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9426 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9428 if (op0 != rtarg)
9429 emit_move_insn (rtarg, op0);
9430 if (op1 != itarg)
9431 emit_move_insn (itarg, op1);
9433 return original_target;
9436 /* ... fall through ... */
9438 case STRING_CST:
9439 temp = expand_expr_constant (exp, 1, modifier);
9441 /* temp contains a constant address.
9442 On RISC machines where a constant address isn't valid,
9443 make some insns to get that address into a register. */
9444 if (modifier != EXPAND_CONST_ADDRESS
9445 && modifier != EXPAND_INITIALIZER
9446 && modifier != EXPAND_SUM
9447 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9448 MEM_ADDR_SPACE (temp)))
9449 return replace_equiv_address (temp,
9450 copy_rtx (XEXP (temp, 0)));
9451 return temp;
9453 case SAVE_EXPR:
9455 tree val = treeop0;
9456 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl);
9458 if (!SAVE_EXPR_RESOLVED_P (exp))
9460 /* We can indeed still hit this case, typically via builtin
9461 expanders calling save_expr immediately before expanding
9462 something. Assume this means that we only have to deal
9463 with non-BLKmode values. */
9464 gcc_assert (GET_MODE (ret) != BLKmode);
9466 val = build_decl (EXPR_LOCATION (exp),
9467 VAR_DECL, NULL, TREE_TYPE (exp));
9468 DECL_ARTIFICIAL (val) = 1;
9469 DECL_IGNORED_P (val) = 1;
9470 treeop0 = val;
9471 TREE_OPERAND (exp, 0) = treeop0;
9472 SAVE_EXPR_RESOLVED_P (exp) = 1;
9474 if (!CONSTANT_P (ret))
9475 ret = copy_to_reg (ret);
9476 SET_DECL_RTL (val, ret);
9479 return ret;
9483 case CONSTRUCTOR:
9484 /* If we don't need the result, just ensure we evaluate any
9485 subexpressions. */
9486 if (ignore)
9488 unsigned HOST_WIDE_INT idx;
9489 tree value;
9491 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9492 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9494 return const0_rtx;
9497 return expand_constructor (exp, target, modifier, false);
9499 case TARGET_MEM_REF:
9501 addr_space_t as
9502 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9503 struct mem_address addr;
9504 enum insn_code icode;
9505 unsigned int align;
9507 get_address_description (exp, &addr);
9508 op0 = addr_for_mem_ref (&addr, as, true);
9509 op0 = memory_address_addr_space (mode, op0, as);
9510 temp = gen_rtx_MEM (mode, op0);
9511 set_mem_attributes (temp, exp, 0);
9512 set_mem_addr_space (temp, as);
9513 align = get_object_or_type_alignment (exp);
9514 if (modifier != EXPAND_WRITE
9515 && mode != BLKmode
9516 && align < GET_MODE_ALIGNMENT (mode)
9517 /* If the target does not have special handling for unaligned
9518 loads of mode then it can use regular moves for them. */
9519 && ((icode = optab_handler (movmisalign_optab, mode))
9520 != CODE_FOR_nothing))
9522 struct expand_operand ops[2];
9524 /* We've already validated the memory, and we're creating a
9525 new pseudo destination. The predicates really can't fail,
9526 nor can the generator. */
9527 create_output_operand (&ops[0], NULL_RTX, mode);
9528 create_fixed_operand (&ops[1], temp);
9529 expand_insn (icode, 2, ops);
9530 return ops[0].value;
9532 return temp;
9535 case MEM_REF:
9537 addr_space_t as
9538 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9539 enum machine_mode address_mode;
9540 tree base = TREE_OPERAND (exp, 0);
9541 gimple def_stmt;
9542 enum insn_code icode;
9543 unsigned align;
9544 /* Handle expansion of non-aliased memory with non-BLKmode. That
9545 might end up in a register. */
9546 if (mem_ref_refers_to_non_mem_p (exp))
9548 HOST_WIDE_INT offset = mem_ref_offset (exp).low;
9549 tree bit_offset;
9550 tree bftype;
9551 base = TREE_OPERAND (base, 0);
9552 if (offset == 0
9553 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1)
9554 && (GET_MODE_BITSIZE (DECL_MODE (base))
9555 == TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))))
9556 return expand_expr (build1 (VIEW_CONVERT_EXPR,
9557 TREE_TYPE (exp), base),
9558 target, tmode, modifier);
9559 bit_offset = bitsize_int (offset * BITS_PER_UNIT);
9560 bftype = TREE_TYPE (base);
9561 if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
9562 bftype = TREE_TYPE (exp);
9563 else
9565 temp = assign_stack_temp (DECL_MODE (base),
9566 GET_MODE_SIZE (DECL_MODE (base)),
9568 store_expr (base, temp, 0, false);
9569 temp = adjust_address (temp, BLKmode, offset);
9570 set_mem_size (temp, int_size_in_bytes (TREE_TYPE (exp)));
9571 return temp;
9573 return expand_expr (build3 (BIT_FIELD_REF, bftype,
9574 base,
9575 TYPE_SIZE (TREE_TYPE (exp)),
9576 bit_offset),
9577 target, tmode, modifier);
9579 address_mode = targetm.addr_space.address_mode (as);
9580 base = TREE_OPERAND (exp, 0);
9581 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9583 tree mask = gimple_assign_rhs2 (def_stmt);
9584 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9585 gimple_assign_rhs1 (def_stmt), mask);
9586 TREE_OPERAND (exp, 0) = base;
9588 align = get_object_or_type_alignment (exp);
9589 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9590 op0 = memory_address_addr_space (address_mode, op0, as);
9591 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9593 rtx off
9594 = immed_double_int_const (mem_ref_offset (exp), address_mode);
9595 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9597 op0 = memory_address_addr_space (mode, op0, as);
9598 temp = gen_rtx_MEM (mode, op0);
9599 set_mem_attributes (temp, exp, 0);
9600 set_mem_addr_space (temp, as);
9601 if (TREE_THIS_VOLATILE (exp))
9602 MEM_VOLATILE_P (temp) = 1;
9603 if (modifier != EXPAND_WRITE
9604 && mode != BLKmode
9605 && align < GET_MODE_ALIGNMENT (mode))
9607 if ((icode = optab_handler (movmisalign_optab, mode))
9608 != CODE_FOR_nothing)
9610 struct expand_operand ops[2];
9612 /* We've already validated the memory, and we're creating a
9613 new pseudo destination. The predicates really can't fail,
9614 nor can the generator. */
9615 create_output_operand (&ops[0], NULL_RTX, mode);
9616 create_fixed_operand (&ops[1], temp);
9617 expand_insn (icode, 2, ops);
9618 return ops[0].value;
9620 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9621 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9622 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9623 true, (modifier == EXPAND_STACK_PARM
9624 ? NULL_RTX : target),
9625 mode, mode);
9627 return temp;
9630 case ARRAY_REF:
9633 tree array = treeop0;
9634 tree index = treeop1;
9636 /* Fold an expression like: "foo"[2].
9637 This is not done in fold so it won't happen inside &.
9638 Don't fold if this is for wide characters since it's too
9639 difficult to do correctly and this is a very rare case. */
9641 if (modifier != EXPAND_CONST_ADDRESS
9642 && modifier != EXPAND_INITIALIZER
9643 && modifier != EXPAND_MEMORY)
9645 tree t = fold_read_from_constant_string (exp);
9647 if (t)
9648 return expand_expr (t, target, tmode, modifier);
9651 /* If this is a constant index into a constant array,
9652 just get the value from the array. Handle both the cases when
9653 we have an explicit constructor and when our operand is a variable
9654 that was declared const. */
9656 if (modifier != EXPAND_CONST_ADDRESS
9657 && modifier != EXPAND_INITIALIZER
9658 && modifier != EXPAND_MEMORY
9659 && TREE_CODE (array) == CONSTRUCTOR
9660 && ! TREE_SIDE_EFFECTS (array)
9661 && TREE_CODE (index) == INTEGER_CST)
9663 unsigned HOST_WIDE_INT ix;
9664 tree field, value;
9666 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9667 field, value)
9668 if (tree_int_cst_equal (field, index))
9670 if (!TREE_SIDE_EFFECTS (value))
9671 return expand_expr (fold (value), target, tmode, modifier);
9672 break;
9676 else if (optimize >= 1
9677 && modifier != EXPAND_CONST_ADDRESS
9678 && modifier != EXPAND_INITIALIZER
9679 && modifier != EXPAND_MEMORY
9680 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9681 && TREE_CODE (array) == VAR_DECL && DECL_INITIAL (array)
9682 && TREE_CODE (DECL_INITIAL (array)) != ERROR_MARK
9683 && const_value_known_p (array))
9685 if (TREE_CODE (index) == INTEGER_CST)
9687 tree init = DECL_INITIAL (array);
9689 if (TREE_CODE (init) == CONSTRUCTOR)
9691 unsigned HOST_WIDE_INT ix;
9692 tree field, value;
9694 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9695 field, value)
9696 if (tree_int_cst_equal (field, index))
9698 if (TREE_SIDE_EFFECTS (value))
9699 break;
9701 if (TREE_CODE (value) == CONSTRUCTOR)
9703 /* If VALUE is a CONSTRUCTOR, this
9704 optimization is only useful if
9705 this doesn't store the CONSTRUCTOR
9706 into memory. If it does, it is more
9707 efficient to just load the data from
9708 the array directly. */
9709 rtx ret = expand_constructor (value, target,
9710 modifier, true);
9711 if (ret == NULL_RTX)
9712 break;
9715 return expand_expr (fold (value), target, tmode,
9716 modifier);
9719 else if(TREE_CODE (init) == STRING_CST)
9721 tree index1 = index;
9722 tree low_bound = array_ref_low_bound (exp);
9723 index1 = fold_convert_loc (loc, sizetype,
9724 treeop1);
9726 /* Optimize the special-case of a zero lower bound.
9728 We convert the low_bound to sizetype to avoid some problems
9729 with constant folding. (E.g. suppose the lower bound is 1,
9730 and its mode is QI. Without the conversion,l (ARRAY
9731 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9732 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
9734 if (! integer_zerop (low_bound))
9735 index1 = size_diffop_loc (loc, index1,
9736 fold_convert_loc (loc, sizetype,
9737 low_bound));
9739 if (0 > compare_tree_int (index1,
9740 TREE_STRING_LENGTH (init)))
9742 tree type = TREE_TYPE (TREE_TYPE (init));
9743 enum machine_mode mode = TYPE_MODE (type);
9745 if (GET_MODE_CLASS (mode) == MODE_INT
9746 && GET_MODE_SIZE (mode) == 1)
9747 return gen_int_mode (TREE_STRING_POINTER (init)
9748 [TREE_INT_CST_LOW (index1)],
9749 mode);
9755 goto normal_inner_ref;
9757 case COMPONENT_REF:
9758 /* If the operand is a CONSTRUCTOR, we can just extract the
9759 appropriate field if it is present. */
9760 if (TREE_CODE (treeop0) == CONSTRUCTOR)
9762 unsigned HOST_WIDE_INT idx;
9763 tree field, value;
9765 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
9766 idx, field, value)
9767 if (field == treeop1
9768 /* We can normally use the value of the field in the
9769 CONSTRUCTOR. However, if this is a bitfield in
9770 an integral mode that we can fit in a HOST_WIDE_INT,
9771 we must mask only the number of bits in the bitfield,
9772 since this is done implicitly by the constructor. If
9773 the bitfield does not meet either of those conditions,
9774 we can't do this optimization. */
9775 && (! DECL_BIT_FIELD (field)
9776 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
9777 && (GET_MODE_PRECISION (DECL_MODE (field))
9778 <= HOST_BITS_PER_WIDE_INT))))
9780 if (DECL_BIT_FIELD (field)
9781 && modifier == EXPAND_STACK_PARM)
9782 target = 0;
9783 op0 = expand_expr (value, target, tmode, modifier);
9784 if (DECL_BIT_FIELD (field))
9786 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
9787 enum machine_mode imode = TYPE_MODE (TREE_TYPE (field));
9789 if (TYPE_UNSIGNED (TREE_TYPE (field)))
9791 op1 = GEN_INT (((HOST_WIDE_INT) 1 << bitsize) - 1);
9792 op0 = expand_and (imode, op0, op1, target);
9794 else
9796 int count = GET_MODE_PRECISION (imode) - bitsize;
9798 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
9799 target, 0);
9800 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
9801 target, 0);
9805 return op0;
9808 goto normal_inner_ref;
9810 case BIT_FIELD_REF:
9811 case ARRAY_RANGE_REF:
9812 normal_inner_ref:
9814 enum machine_mode mode1, mode2;
9815 HOST_WIDE_INT bitsize, bitpos;
9816 tree offset;
9817 int volatilep = 0, must_force_mem;
9818 bool packedp = false;
9819 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
9820 &mode1, &unsignedp, &volatilep, true);
9821 rtx orig_op0, memloc;
9822 bool mem_attrs_from_type = false;
9824 /* If we got back the original object, something is wrong. Perhaps
9825 we are evaluating an expression too early. In any event, don't
9826 infinitely recurse. */
9827 gcc_assert (tem != exp);
9829 if (TYPE_PACKED (TREE_TYPE (TREE_OPERAND (exp, 0)))
9830 || (TREE_CODE (TREE_OPERAND (exp, 1)) == FIELD_DECL
9831 && DECL_PACKED (TREE_OPERAND (exp, 1))))
9832 packedp = true;
9834 /* If TEM's type is a union of variable size, pass TARGET to the inner
9835 computation, since it will need a temporary and TARGET is known
9836 to have to do. This occurs in unchecked conversion in Ada. */
9837 orig_op0 = op0
9838 = expand_expr (tem,
9839 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
9840 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
9841 != INTEGER_CST)
9842 && modifier != EXPAND_STACK_PARM
9843 ? target : NULL_RTX),
9844 VOIDmode,
9845 (modifier == EXPAND_INITIALIZER
9846 || modifier == EXPAND_CONST_ADDRESS
9847 || modifier == EXPAND_STACK_PARM)
9848 ? modifier : EXPAND_NORMAL);
9851 /* If the bitfield is volatile, we want to access it in the
9852 field's mode, not the computed mode.
9853 If a MEM has VOIDmode (external with incomplete type),
9854 use BLKmode for it instead. */
9855 if (MEM_P (op0))
9857 if (volatilep && flag_strict_volatile_bitfields > 0)
9858 op0 = adjust_address (op0, mode1, 0);
9859 else if (GET_MODE (op0) == VOIDmode)
9860 op0 = adjust_address (op0, BLKmode, 0);
9863 mode2
9864 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
9866 /* If we have either an offset, a BLKmode result, or a reference
9867 outside the underlying object, we must force it to memory.
9868 Such a case can occur in Ada if we have unchecked conversion
9869 of an expression from a scalar type to an aggregate type or
9870 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
9871 passed a partially uninitialized object or a view-conversion
9872 to a larger size. */
9873 must_force_mem = (offset
9874 || mode1 == BLKmode
9875 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
9877 /* Handle CONCAT first. */
9878 if (GET_CODE (op0) == CONCAT && !must_force_mem)
9880 if (bitpos == 0
9881 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
9882 return op0;
9883 if (bitpos == 0
9884 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9885 && bitsize)
9887 op0 = XEXP (op0, 0);
9888 mode2 = GET_MODE (op0);
9890 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9891 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
9892 && bitpos
9893 && bitsize)
9895 op0 = XEXP (op0, 1);
9896 bitpos = 0;
9897 mode2 = GET_MODE (op0);
9899 else
9900 /* Otherwise force into memory. */
9901 must_force_mem = 1;
9904 /* If this is a constant, put it in a register if it is a legitimate
9905 constant and we don't need a memory reference. */
9906 if (CONSTANT_P (op0)
9907 && mode2 != BLKmode
9908 && targetm.legitimate_constant_p (mode2, op0)
9909 && !must_force_mem)
9910 op0 = force_reg (mode2, op0);
9912 /* Otherwise, if this is a constant, try to force it to the constant
9913 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
9914 is a legitimate constant. */
9915 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
9916 op0 = validize_mem (memloc);
9918 /* Otherwise, if this is a constant or the object is not in memory
9919 and need be, put it there. */
9920 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
9922 tree nt = build_qualified_type (TREE_TYPE (tem),
9923 (TYPE_QUALS (TREE_TYPE (tem))
9924 | TYPE_QUAL_CONST));
9925 memloc = assign_temp (nt, 1, 1, 1);
9926 emit_move_insn (memloc, op0);
9927 op0 = memloc;
9928 mem_attrs_from_type = true;
9931 if (offset)
9933 enum machine_mode address_mode;
9934 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
9935 EXPAND_SUM);
9937 gcc_assert (MEM_P (op0));
9939 address_mode = get_address_mode (op0);
9940 if (GET_MODE (offset_rtx) != address_mode)
9941 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
9943 if (GET_MODE (op0) == BLKmode
9944 /* A constant address in OP0 can have VOIDmode, we must
9945 not try to call force_reg in that case. */
9946 && GET_MODE (XEXP (op0, 0)) != VOIDmode
9947 && bitsize != 0
9948 && (bitpos % bitsize) == 0
9949 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
9950 && MEM_ALIGN (op0) == GET_MODE_ALIGNMENT (mode1))
9952 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
9953 bitpos = 0;
9956 op0 = offset_address (op0, offset_rtx,
9957 highest_pow2_factor (offset));
9960 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
9961 record its alignment as BIGGEST_ALIGNMENT. */
9962 if (MEM_P (op0) && bitpos == 0 && offset != 0
9963 && is_aligning_offset (offset, tem))
9964 set_mem_align (op0, BIGGEST_ALIGNMENT);
9966 /* Don't forget about volatility even if this is a bitfield. */
9967 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
9969 if (op0 == orig_op0)
9970 op0 = copy_rtx (op0);
9972 MEM_VOLATILE_P (op0) = 1;
9975 /* In cases where an aligned union has an unaligned object
9976 as a field, we might be extracting a BLKmode value from
9977 an integer-mode (e.g., SImode) object. Handle this case
9978 by doing the extract into an object as wide as the field
9979 (which we know to be the width of a basic mode), then
9980 storing into memory, and changing the mode to BLKmode. */
9981 if (mode1 == VOIDmode
9982 || REG_P (op0) || GET_CODE (op0) == SUBREG
9983 || (mode1 != BLKmode && ! direct_load[(int) mode1]
9984 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9985 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
9986 && modifier != EXPAND_CONST_ADDRESS
9987 && modifier != EXPAND_INITIALIZER)
9988 /* If the field is volatile, we always want an aligned
9989 access. Do this in following two situations:
9990 1. the access is not already naturally
9991 aligned, otherwise "normal" (non-bitfield) volatile fields
9992 become non-addressable.
9993 2. the bitsize is narrower than the access size. Need
9994 to extract bitfields from the access. */
9995 || (volatilep && flag_strict_volatile_bitfields > 0
9996 && (bitpos % GET_MODE_ALIGNMENT (mode) != 0
9997 || (mode1 != BLKmode
9998 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)))
9999 /* If the field isn't aligned enough to fetch as a memref,
10000 fetch it as a bit field. */
10001 || (mode1 != BLKmode
10002 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10003 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10004 || (MEM_P (op0)
10005 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10006 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10007 && ((modifier == EXPAND_CONST_ADDRESS
10008 || modifier == EXPAND_INITIALIZER)
10009 ? STRICT_ALIGNMENT
10010 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10011 || (bitpos % BITS_PER_UNIT != 0)))
10012 /* If the type and the field are a constant size and the
10013 size of the type isn't the same size as the bitfield,
10014 we must use bitfield operations. */
10015 || (bitsize >= 0
10016 && TYPE_SIZE (TREE_TYPE (exp))
10017 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10018 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10019 bitsize)))
10021 enum machine_mode ext_mode = mode;
10023 if (ext_mode == BLKmode
10024 && ! (target != 0 && MEM_P (op0)
10025 && MEM_P (target)
10026 && bitpos % BITS_PER_UNIT == 0))
10027 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10029 if (ext_mode == BLKmode)
10031 if (target == 0)
10032 target = assign_temp (type, 0, 1, 1);
10034 if (bitsize == 0)
10035 return target;
10037 /* In this case, BITPOS must start at a byte boundary and
10038 TARGET, if specified, must be a MEM. */
10039 gcc_assert (MEM_P (op0)
10040 && (!target || MEM_P (target))
10041 && !(bitpos % BITS_PER_UNIT));
10043 emit_block_move (target,
10044 adjust_address (op0, VOIDmode,
10045 bitpos / BITS_PER_UNIT),
10046 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10047 / BITS_PER_UNIT),
10048 (modifier == EXPAND_STACK_PARM
10049 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10051 return target;
10054 op0 = validize_mem (op0);
10056 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10057 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10059 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp, packedp,
10060 (modifier == EXPAND_STACK_PARM
10061 ? NULL_RTX : target),
10062 ext_mode, ext_mode);
10064 /* If the result is a record type and BITSIZE is narrower than
10065 the mode of OP0, an integral mode, and this is a big endian
10066 machine, we must put the field into the high-order bits. */
10067 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10068 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10069 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10070 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10071 GET_MODE_BITSIZE (GET_MODE (op0))
10072 - bitsize, op0, 1);
10074 /* If the result type is BLKmode, store the data into a temporary
10075 of the appropriate type, but with the mode corresponding to the
10076 mode for the data we have (op0's mode). It's tempting to make
10077 this a constant type, since we know it's only being stored once,
10078 but that can cause problems if we are taking the address of this
10079 COMPONENT_REF because the MEM of any reference via that address
10080 will have flags corresponding to the type, which will not
10081 necessarily be constant. */
10082 if (mode == BLKmode)
10084 rtx new_rtx;
10086 new_rtx = assign_stack_temp_for_type (ext_mode,
10087 GET_MODE_BITSIZE (ext_mode),
10088 0, type);
10089 emit_move_insn (new_rtx, op0);
10090 op0 = copy_rtx (new_rtx);
10091 PUT_MODE (op0, BLKmode);
10094 return op0;
10097 /* If the result is BLKmode, use that to access the object
10098 now as well. */
10099 if (mode == BLKmode)
10100 mode1 = BLKmode;
10102 /* Get a reference to just this component. */
10103 if (modifier == EXPAND_CONST_ADDRESS
10104 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10105 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10106 else
10107 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10109 if (op0 == orig_op0)
10110 op0 = copy_rtx (op0);
10112 /* If op0 is a temporary because of forcing to memory, pass only the
10113 type to set_mem_attributes so that the original expression is never
10114 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10115 if (mem_attrs_from_type)
10116 set_mem_attributes (op0, type, 0);
10117 else
10118 set_mem_attributes (op0, exp, 0);
10120 if (REG_P (XEXP (op0, 0)))
10121 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10123 MEM_VOLATILE_P (op0) |= volatilep;
10124 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10125 || modifier == EXPAND_CONST_ADDRESS
10126 || modifier == EXPAND_INITIALIZER)
10127 return op0;
10128 else if (target == 0)
10129 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10131 convert_move (target, op0, unsignedp);
10132 return target;
10135 case OBJ_TYPE_REF:
10136 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10138 case CALL_EXPR:
10139 /* All valid uses of __builtin_va_arg_pack () are removed during
10140 inlining. */
10141 if (CALL_EXPR_VA_ARG_PACK (exp))
10142 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10144 tree fndecl = get_callee_fndecl (exp), attr;
10146 if (fndecl
10147 && (attr = lookup_attribute ("error",
10148 DECL_ATTRIBUTES (fndecl))) != NULL)
10149 error ("%Kcall to %qs declared with attribute error: %s",
10150 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10151 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10152 if (fndecl
10153 && (attr = lookup_attribute ("warning",
10154 DECL_ATTRIBUTES (fndecl))) != NULL)
10155 warning_at (tree_nonartificial_location (exp),
10156 0, "%Kcall to %qs declared with attribute warning: %s",
10157 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10158 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10160 /* Check for a built-in function. */
10161 if (fndecl && DECL_BUILT_IN (fndecl))
10163 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10164 return expand_builtin (exp, target, subtarget, tmode, ignore);
10167 return expand_call (exp, target, ignore);
10169 case VIEW_CONVERT_EXPR:
10170 op0 = NULL_RTX;
10172 /* If we are converting to BLKmode, try to avoid an intermediate
10173 temporary by fetching an inner memory reference. */
10174 if (mode == BLKmode
10175 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10176 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10177 && handled_component_p (treeop0))
10179 enum machine_mode mode1;
10180 HOST_WIDE_INT bitsize, bitpos;
10181 tree offset;
10182 int unsignedp;
10183 int volatilep = 0;
10184 tree tem
10185 = get_inner_reference (treeop0, &bitsize, &bitpos,
10186 &offset, &mode1, &unsignedp, &volatilep,
10187 true);
10188 rtx orig_op0;
10190 /* ??? We should work harder and deal with non-zero offsets. */
10191 if (!offset
10192 && (bitpos % BITS_PER_UNIT) == 0
10193 && bitsize >= 0
10194 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) == 0)
10196 /* See the normal_inner_ref case for the rationale. */
10197 orig_op0
10198 = expand_expr (tem,
10199 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10200 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10201 != INTEGER_CST)
10202 && modifier != EXPAND_STACK_PARM
10203 ? target : NULL_RTX),
10204 VOIDmode,
10205 (modifier == EXPAND_INITIALIZER
10206 || modifier == EXPAND_CONST_ADDRESS
10207 || modifier == EXPAND_STACK_PARM)
10208 ? modifier : EXPAND_NORMAL);
10210 if (MEM_P (orig_op0))
10212 op0 = orig_op0;
10214 /* Get a reference to just this component. */
10215 if (modifier == EXPAND_CONST_ADDRESS
10216 || modifier == EXPAND_SUM
10217 || modifier == EXPAND_INITIALIZER)
10218 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10219 else
10220 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10222 if (op0 == orig_op0)
10223 op0 = copy_rtx (op0);
10225 set_mem_attributes (op0, treeop0, 0);
10226 if (REG_P (XEXP (op0, 0)))
10227 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10229 MEM_VOLATILE_P (op0) |= volatilep;
10234 if (!op0)
10235 op0 = expand_expr (treeop0,
10236 NULL_RTX, VOIDmode, modifier);
10238 /* If the input and output modes are both the same, we are done. */
10239 if (mode == GET_MODE (op0))
10241 /* If neither mode is BLKmode, and both modes are the same size
10242 then we can use gen_lowpart. */
10243 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10244 && (GET_MODE_PRECISION (mode)
10245 == GET_MODE_PRECISION (GET_MODE (op0)))
10246 && !COMPLEX_MODE_P (GET_MODE (op0)))
10248 if (GET_CODE (op0) == SUBREG)
10249 op0 = force_reg (GET_MODE (op0), op0);
10250 temp = gen_lowpart_common (mode, op0);
10251 if (temp)
10252 op0 = temp;
10253 else
10255 if (!REG_P (op0) && !MEM_P (op0))
10256 op0 = force_reg (GET_MODE (op0), op0);
10257 op0 = gen_lowpart (mode, op0);
10260 /* If both types are integral, convert from one mode to the other. */
10261 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10262 op0 = convert_modes (mode, GET_MODE (op0), op0,
10263 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10264 /* As a last resort, spill op0 to memory, and reload it in a
10265 different mode. */
10266 else if (!MEM_P (op0))
10268 /* If the operand is not a MEM, force it into memory. Since we
10269 are going to be changing the mode of the MEM, don't call
10270 force_const_mem for constants because we don't allow pool
10271 constants to change mode. */
10272 tree inner_type = TREE_TYPE (treeop0);
10274 gcc_assert (!TREE_ADDRESSABLE (exp));
10276 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10277 target
10278 = assign_stack_temp_for_type
10279 (TYPE_MODE (inner_type),
10280 GET_MODE_SIZE (TYPE_MODE (inner_type)), 0, inner_type);
10282 emit_move_insn (target, op0);
10283 op0 = target;
10286 /* At this point, OP0 is in the correct mode. If the output type is
10287 such that the operand is known to be aligned, indicate that it is.
10288 Otherwise, we need only be concerned about alignment for non-BLKmode
10289 results. */
10290 if (MEM_P (op0))
10292 enum insn_code icode;
10294 op0 = copy_rtx (op0);
10296 if (TYPE_ALIGN_OK (type))
10297 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10298 else if (mode != BLKmode
10299 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode)
10300 /* If the target does have special handling for unaligned
10301 loads of mode then use them. */
10302 && ((icode = optab_handler (movmisalign_optab, mode))
10303 != CODE_FOR_nothing))
10305 rtx reg, insn;
10307 op0 = adjust_address (op0, mode, 0);
10308 /* We've already validated the memory, and we're creating a
10309 new pseudo destination. The predicates really can't
10310 fail. */
10311 reg = gen_reg_rtx (mode);
10313 /* Nor can the insn generator. */
10314 insn = GEN_FCN (icode) (reg, op0);
10315 emit_insn (insn);
10316 return reg;
10318 else if (STRICT_ALIGNMENT
10319 && mode != BLKmode
10320 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10322 tree inner_type = TREE_TYPE (treeop0);
10323 HOST_WIDE_INT temp_size
10324 = MAX (int_size_in_bytes (inner_type),
10325 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10326 rtx new_rtx
10327 = assign_stack_temp_for_type (mode, temp_size, 0, type);
10328 rtx new_with_op0_mode
10329 = adjust_address (new_rtx, GET_MODE (op0), 0);
10331 gcc_assert (!TREE_ADDRESSABLE (exp));
10333 if (GET_MODE (op0) == BLKmode)
10334 emit_block_move (new_with_op0_mode, op0,
10335 GEN_INT (GET_MODE_SIZE (mode)),
10336 (modifier == EXPAND_STACK_PARM
10337 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10338 else
10339 emit_move_insn (new_with_op0_mode, op0);
10341 op0 = new_rtx;
10344 op0 = adjust_address (op0, mode, 0);
10347 return op0;
10349 case MODIFY_EXPR:
10351 tree lhs = treeop0;
10352 tree rhs = treeop1;
10353 gcc_assert (ignore);
10355 /* Check for |= or &= of a bitfield of size one into another bitfield
10356 of size 1. In this case, (unless we need the result of the
10357 assignment) we can do this more efficiently with a
10358 test followed by an assignment, if necessary.
10360 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10361 things change so we do, this code should be enhanced to
10362 support it. */
10363 if (TREE_CODE (lhs) == COMPONENT_REF
10364 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10365 || TREE_CODE (rhs) == BIT_AND_EXPR)
10366 && TREE_OPERAND (rhs, 0) == lhs
10367 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10368 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10369 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10371 rtx label = gen_label_rtx ();
10372 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10373 do_jump (TREE_OPERAND (rhs, 1),
10374 value ? label : 0,
10375 value ? 0 : label, -1);
10376 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10377 MOVE_NONTEMPORAL (exp));
10378 do_pending_stack_adjust ();
10379 emit_label (label);
10380 return const0_rtx;
10383 expand_assignment (lhs, rhs, MOVE_NONTEMPORAL (exp));
10384 return const0_rtx;
10387 case ADDR_EXPR:
10388 return expand_expr_addr_expr (exp, target, tmode, modifier);
10390 case REALPART_EXPR:
10391 op0 = expand_normal (treeop0);
10392 return read_complex_part (op0, false);
10394 case IMAGPART_EXPR:
10395 op0 = expand_normal (treeop0);
10396 return read_complex_part (op0, true);
10398 case RETURN_EXPR:
10399 case LABEL_EXPR:
10400 case GOTO_EXPR:
10401 case SWITCH_EXPR:
10402 case ASM_EXPR:
10403 /* Expanded in cfgexpand.c. */
10404 gcc_unreachable ();
10406 case TRY_CATCH_EXPR:
10407 case CATCH_EXPR:
10408 case EH_FILTER_EXPR:
10409 case TRY_FINALLY_EXPR:
10410 /* Lowered by tree-eh.c. */
10411 gcc_unreachable ();
10413 case WITH_CLEANUP_EXPR:
10414 case CLEANUP_POINT_EXPR:
10415 case TARGET_EXPR:
10416 case CASE_LABEL_EXPR:
10417 case VA_ARG_EXPR:
10418 case BIND_EXPR:
10419 case INIT_EXPR:
10420 case CONJ_EXPR:
10421 case COMPOUND_EXPR:
10422 case PREINCREMENT_EXPR:
10423 case PREDECREMENT_EXPR:
10424 case POSTINCREMENT_EXPR:
10425 case POSTDECREMENT_EXPR:
10426 case LOOP_EXPR:
10427 case EXIT_EXPR:
10428 /* Lowered by gimplify.c. */
10429 gcc_unreachable ();
10431 case FDESC_EXPR:
10432 /* Function descriptors are not valid except for as
10433 initialization constants, and should not be expanded. */
10434 gcc_unreachable ();
10436 case WITH_SIZE_EXPR:
10437 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10438 have pulled out the size to use in whatever context it needed. */
10439 return expand_expr_real (treeop0, original_target, tmode,
10440 modifier, alt_rtl);
10442 case COMPOUND_LITERAL_EXPR:
10444 /* Initialize the anonymous variable declared in the compound
10445 literal, then return the variable. */
10446 tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
10448 /* Create RTL for this variable. */
10449 if (!DECL_RTL_SET_P (decl))
10451 if (DECL_HARD_REGISTER (decl))
10452 /* The user specified an assembler name for this variable.
10453 Set that up now. */
10454 rest_of_decl_compilation (decl, 0, 0);
10455 else
10456 expand_decl (decl);
10459 return expand_expr_real (decl, original_target, tmode,
10460 modifier, alt_rtl);
10463 default:
10464 return expand_expr_real_2 (&ops, target, tmode, modifier);
10468 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10469 signedness of TYPE), possibly returning the result in TARGET. */
10470 static rtx
10471 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10473 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10474 if (target && GET_MODE (target) != GET_MODE (exp))
10475 target = 0;
10476 /* For constant values, reduce using build_int_cst_type. */
10477 if (CONST_INT_P (exp))
10479 HOST_WIDE_INT value = INTVAL (exp);
10480 tree t = build_int_cst_type (type, value);
10481 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10483 else if (TYPE_UNSIGNED (type))
10485 rtx mask = immed_double_int_const (double_int_mask (prec),
10486 GET_MODE (exp));
10487 return expand_and (GET_MODE (exp), exp, mask, target);
10489 else
10491 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10492 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10493 exp, count, target, 0);
10494 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10495 exp, count, target, 0);
10499 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10500 when applied to the address of EXP produces an address known to be
10501 aligned more than BIGGEST_ALIGNMENT. */
10503 static int
10504 is_aligning_offset (const_tree offset, const_tree exp)
10506 /* Strip off any conversions. */
10507 while (CONVERT_EXPR_P (offset))
10508 offset = TREE_OPERAND (offset, 0);
10510 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10511 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10512 if (TREE_CODE (offset) != BIT_AND_EXPR
10513 || !host_integerp (TREE_OPERAND (offset, 1), 1)
10514 || compare_tree_int (TREE_OPERAND (offset, 1),
10515 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10516 || !exact_log2 (tree_low_cst (TREE_OPERAND (offset, 1), 1) + 1) < 0)
10517 return 0;
10519 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10520 It must be NEGATE_EXPR. Then strip any more conversions. */
10521 offset = TREE_OPERAND (offset, 0);
10522 while (CONVERT_EXPR_P (offset))
10523 offset = TREE_OPERAND (offset, 0);
10525 if (TREE_CODE (offset) != NEGATE_EXPR)
10526 return 0;
10528 offset = TREE_OPERAND (offset, 0);
10529 while (CONVERT_EXPR_P (offset))
10530 offset = TREE_OPERAND (offset, 0);
10532 /* This must now be the address of EXP. */
10533 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10536 /* Return the tree node if an ARG corresponds to a string constant or zero
10537 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10538 in bytes within the string that ARG is accessing. The type of the
10539 offset will be `sizetype'. */
10541 tree
10542 string_constant (tree arg, tree *ptr_offset)
10544 tree array, offset, lower_bound;
10545 STRIP_NOPS (arg);
10547 if (TREE_CODE (arg) == ADDR_EXPR)
10549 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10551 *ptr_offset = size_zero_node;
10552 return TREE_OPERAND (arg, 0);
10554 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10556 array = TREE_OPERAND (arg, 0);
10557 offset = size_zero_node;
10559 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10561 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10562 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10563 if (TREE_CODE (array) != STRING_CST
10564 && TREE_CODE (array) != VAR_DECL)
10565 return 0;
10567 /* Check if the array has a nonzero lower bound. */
10568 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10569 if (!integer_zerop (lower_bound))
10571 /* If the offset and base aren't both constants, return 0. */
10572 if (TREE_CODE (lower_bound) != INTEGER_CST)
10573 return 0;
10574 if (TREE_CODE (offset) != INTEGER_CST)
10575 return 0;
10576 /* Adjust offset by the lower bound. */
10577 offset = size_diffop (fold_convert (sizetype, offset),
10578 fold_convert (sizetype, lower_bound));
10581 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10583 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10584 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10585 if (TREE_CODE (array) != ADDR_EXPR)
10586 return 0;
10587 array = TREE_OPERAND (array, 0);
10588 if (TREE_CODE (array) != STRING_CST
10589 && TREE_CODE (array) != VAR_DECL)
10590 return 0;
10592 else
10593 return 0;
10595 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10597 tree arg0 = TREE_OPERAND (arg, 0);
10598 tree arg1 = TREE_OPERAND (arg, 1);
10600 STRIP_NOPS (arg0);
10601 STRIP_NOPS (arg1);
10603 if (TREE_CODE (arg0) == ADDR_EXPR
10604 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10605 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10607 array = TREE_OPERAND (arg0, 0);
10608 offset = arg1;
10610 else if (TREE_CODE (arg1) == ADDR_EXPR
10611 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10612 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10614 array = TREE_OPERAND (arg1, 0);
10615 offset = arg0;
10617 else
10618 return 0;
10620 else
10621 return 0;
10623 if (TREE_CODE (array) == STRING_CST)
10625 *ptr_offset = fold_convert (sizetype, offset);
10626 return array;
10628 else if (TREE_CODE (array) == VAR_DECL
10629 || TREE_CODE (array) == CONST_DECL)
10631 int length;
10633 /* Variables initialized to string literals can be handled too. */
10634 if (!const_value_known_p (array)
10635 || !DECL_INITIAL (array)
10636 || TREE_CODE (DECL_INITIAL (array)) != STRING_CST)
10637 return 0;
10639 /* Avoid const char foo[4] = "abcde"; */
10640 if (DECL_SIZE_UNIT (array) == NULL_TREE
10641 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10642 || (length = TREE_STRING_LENGTH (DECL_INITIAL (array))) <= 0
10643 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10644 return 0;
10646 /* If variable is bigger than the string literal, OFFSET must be constant
10647 and inside of the bounds of the string literal. */
10648 offset = fold_convert (sizetype, offset);
10649 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10650 && (! host_integerp (offset, 1)
10651 || compare_tree_int (offset, length) >= 0))
10652 return 0;
10654 *ptr_offset = offset;
10655 return DECL_INITIAL (array);
10658 return 0;
10661 /* Generate code to calculate OPS, and exploded expression
10662 using a store-flag instruction and return an rtx for the result.
10663 OPS reflects a comparison.
10665 If TARGET is nonzero, store the result there if convenient.
10667 Return zero if there is no suitable set-flag instruction
10668 available on this machine.
10670 Once expand_expr has been called on the arguments of the comparison,
10671 we are committed to doing the store flag, since it is not safe to
10672 re-evaluate the expression. We emit the store-flag insn by calling
10673 emit_store_flag, but only expand the arguments if we have a reason
10674 to believe that emit_store_flag will be successful. If we think that
10675 it will, but it isn't, we have to simulate the store-flag with a
10676 set/jump/set sequence. */
10678 static rtx
10679 do_store_flag (sepops ops, rtx target, enum machine_mode mode)
10681 enum rtx_code code;
10682 tree arg0, arg1, type;
10683 tree tem;
10684 enum machine_mode operand_mode;
10685 int unsignedp;
10686 rtx op0, op1;
10687 rtx subtarget = target;
10688 location_t loc = ops->location;
10690 arg0 = ops->op0;
10691 arg1 = ops->op1;
10693 /* Don't crash if the comparison was erroneous. */
10694 if (arg0 == error_mark_node || arg1 == error_mark_node)
10695 return const0_rtx;
10697 type = TREE_TYPE (arg0);
10698 operand_mode = TYPE_MODE (type);
10699 unsignedp = TYPE_UNSIGNED (type);
10701 /* We won't bother with BLKmode store-flag operations because it would mean
10702 passing a lot of information to emit_store_flag. */
10703 if (operand_mode == BLKmode)
10704 return 0;
10706 /* We won't bother with store-flag operations involving function pointers
10707 when function pointers must be canonicalized before comparisons. */
10708 #ifdef HAVE_canonicalize_funcptr_for_compare
10709 if (HAVE_canonicalize_funcptr_for_compare
10710 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10711 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10712 == FUNCTION_TYPE))
10713 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10714 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10715 == FUNCTION_TYPE))))
10716 return 0;
10717 #endif
10719 STRIP_NOPS (arg0);
10720 STRIP_NOPS (arg1);
10722 /* For vector typed comparisons emit code to generate the desired
10723 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10724 expander for this. */
10725 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10727 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10728 tree if_true = constant_boolean_node (true, ops->type);
10729 tree if_false = constant_boolean_node (false, ops->type);
10730 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10733 /* For vector typed comparisons emit code to generate the desired
10734 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10735 expander for this. */
10736 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10738 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10739 tree if_true = constant_boolean_node (true, ops->type);
10740 tree if_false = constant_boolean_node (false, ops->type);
10741 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10744 /* Get the rtx comparison code to use. We know that EXP is a comparison
10745 operation of some type. Some comparisons against 1 and -1 can be
10746 converted to comparisons with zero. Do so here so that the tests
10747 below will be aware that we have a comparison with zero. These
10748 tests will not catch constants in the first operand, but constants
10749 are rarely passed as the first operand. */
10751 switch (ops->code)
10753 case EQ_EXPR:
10754 code = EQ;
10755 break;
10756 case NE_EXPR:
10757 code = NE;
10758 break;
10759 case LT_EXPR:
10760 if (integer_onep (arg1))
10761 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10762 else
10763 code = unsignedp ? LTU : LT;
10764 break;
10765 case LE_EXPR:
10766 if (! unsignedp && integer_all_onesp (arg1))
10767 arg1 = integer_zero_node, code = LT;
10768 else
10769 code = unsignedp ? LEU : LE;
10770 break;
10771 case GT_EXPR:
10772 if (! unsignedp && integer_all_onesp (arg1))
10773 arg1 = integer_zero_node, code = GE;
10774 else
10775 code = unsignedp ? GTU : GT;
10776 break;
10777 case GE_EXPR:
10778 if (integer_onep (arg1))
10779 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
10780 else
10781 code = unsignedp ? GEU : GE;
10782 break;
10784 case UNORDERED_EXPR:
10785 code = UNORDERED;
10786 break;
10787 case ORDERED_EXPR:
10788 code = ORDERED;
10789 break;
10790 case UNLT_EXPR:
10791 code = UNLT;
10792 break;
10793 case UNLE_EXPR:
10794 code = UNLE;
10795 break;
10796 case UNGT_EXPR:
10797 code = UNGT;
10798 break;
10799 case UNGE_EXPR:
10800 code = UNGE;
10801 break;
10802 case UNEQ_EXPR:
10803 code = UNEQ;
10804 break;
10805 case LTGT_EXPR:
10806 code = LTGT;
10807 break;
10809 default:
10810 gcc_unreachable ();
10813 /* Put a constant second. */
10814 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
10815 || TREE_CODE (arg0) == FIXED_CST)
10817 tem = arg0; arg0 = arg1; arg1 = tem;
10818 code = swap_condition (code);
10821 /* If this is an equality or inequality test of a single bit, we can
10822 do this by shifting the bit being tested to the low-order bit and
10823 masking the result with the constant 1. If the condition was EQ,
10824 we xor it with 1. This does not require an scc insn and is faster
10825 than an scc insn even if we have it.
10827 The code to make this transformation was moved into fold_single_bit_test,
10828 so we just call into the folder and expand its result. */
10830 if ((code == NE || code == EQ)
10831 && integer_zerop (arg1)
10832 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
10834 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
10835 if (srcstmt
10836 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
10838 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
10839 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10840 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
10841 gimple_assign_rhs1 (srcstmt),
10842 gimple_assign_rhs2 (srcstmt));
10843 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
10844 if (temp)
10845 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
10849 if (! get_subtarget (target)
10850 || GET_MODE (subtarget) != operand_mode)
10851 subtarget = 0;
10853 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
10855 if (target == 0)
10856 target = gen_reg_rtx (mode);
10858 /* Try a cstore if possible. */
10859 return emit_store_flag_force (target, code, op0, op1,
10860 operand_mode, unsignedp,
10861 (TYPE_PRECISION (ops->type) == 1
10862 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
10866 /* Stubs in case we haven't got a casesi insn. */
10867 #ifndef HAVE_casesi
10868 # define HAVE_casesi 0
10869 # define gen_casesi(a, b, c, d, e) (0)
10870 # define CODE_FOR_casesi CODE_FOR_nothing
10871 #endif
10873 /* Attempt to generate a casesi instruction. Returns 1 if successful,
10874 0 otherwise (i.e. if there is no casesi instruction). */
10876 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
10877 rtx table_label ATTRIBUTE_UNUSED, rtx default_label,
10878 rtx fallback_label ATTRIBUTE_UNUSED)
10880 struct expand_operand ops[5];
10881 enum machine_mode index_mode = SImode;
10882 rtx op1, op2, index;
10884 if (! HAVE_casesi)
10885 return 0;
10887 /* Convert the index to SImode. */
10888 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
10890 enum machine_mode omode = TYPE_MODE (index_type);
10891 rtx rangertx = expand_normal (range);
10893 /* We must handle the endpoints in the original mode. */
10894 index_expr = build2 (MINUS_EXPR, index_type,
10895 index_expr, minval);
10896 minval = integer_zero_node;
10897 index = expand_normal (index_expr);
10898 if (default_label)
10899 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
10900 omode, 1, default_label);
10901 /* Now we can safely truncate. */
10902 index = convert_to_mode (index_mode, index, 0);
10904 else
10906 if (TYPE_MODE (index_type) != index_mode)
10908 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
10909 index_expr = fold_convert (index_type, index_expr);
10912 index = expand_normal (index_expr);
10915 do_pending_stack_adjust ();
10917 op1 = expand_normal (minval);
10918 op2 = expand_normal (range);
10920 create_input_operand (&ops[0], index, index_mode);
10921 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
10922 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
10923 create_fixed_operand (&ops[3], table_label);
10924 create_fixed_operand (&ops[4], (default_label
10925 ? default_label
10926 : fallback_label));
10927 expand_jump_insn (CODE_FOR_casesi, 5, ops);
10928 return 1;
10931 /* Attempt to generate a tablejump instruction; same concept. */
10932 #ifndef HAVE_tablejump
10933 #define HAVE_tablejump 0
10934 #define gen_tablejump(x, y) (0)
10935 #endif
10937 /* Subroutine of the next function.
10939 INDEX is the value being switched on, with the lowest value
10940 in the table already subtracted.
10941 MODE is its expected mode (needed if INDEX is constant).
10942 RANGE is the length of the jump table.
10943 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
10945 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
10946 index value is out of range. */
10948 static void
10949 do_tablejump (rtx index, enum machine_mode mode, rtx range, rtx table_label,
10950 rtx default_label)
10952 rtx temp, vector;
10954 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
10955 cfun->cfg->max_jumptable_ents = INTVAL (range);
10957 /* Do an unsigned comparison (in the proper mode) between the index
10958 expression and the value which represents the length of the range.
10959 Since we just finished subtracting the lower bound of the range
10960 from the index expression, this comparison allows us to simultaneously
10961 check that the original index expression value is both greater than
10962 or equal to the minimum value of the range and less than or equal to
10963 the maximum value of the range. */
10965 if (default_label)
10966 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
10967 default_label);
10969 /* If index is in range, it must fit in Pmode.
10970 Convert to Pmode so we can index with it. */
10971 if (mode != Pmode)
10972 index = convert_to_mode (Pmode, index, 1);
10974 /* Don't let a MEM slip through, because then INDEX that comes
10975 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
10976 and break_out_memory_refs will go to work on it and mess it up. */
10977 #ifdef PIC_CASE_VECTOR_ADDRESS
10978 if (flag_pic && !REG_P (index))
10979 index = copy_to_mode_reg (Pmode, index);
10980 #endif
10982 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
10983 GET_MODE_SIZE, because this indicates how large insns are. The other
10984 uses should all be Pmode, because they are addresses. This code
10985 could fail if addresses and insns are not the same size. */
10986 index = gen_rtx_PLUS (Pmode,
10987 gen_rtx_MULT (Pmode, index,
10988 GEN_INT (GET_MODE_SIZE (CASE_VECTOR_MODE))),
10989 gen_rtx_LABEL_REF (Pmode, table_label));
10990 #ifdef PIC_CASE_VECTOR_ADDRESS
10991 if (flag_pic)
10992 index = PIC_CASE_VECTOR_ADDRESS (index);
10993 else
10994 #endif
10995 index = memory_address (CASE_VECTOR_MODE, index);
10996 temp = gen_reg_rtx (CASE_VECTOR_MODE);
10997 vector = gen_const_mem (CASE_VECTOR_MODE, index);
10998 convert_move (temp, vector, 0);
11000 emit_jump_insn (gen_tablejump (temp, table_label));
11002 /* If we are generating PIC code or if the table is PC-relative, the
11003 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11004 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11005 emit_barrier ();
11009 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11010 rtx table_label, rtx default_label)
11012 rtx index;
11014 if (! HAVE_tablejump)
11015 return 0;
11017 index_expr = fold_build2 (MINUS_EXPR, index_type,
11018 fold_convert (index_type, index_expr),
11019 fold_convert (index_type, minval));
11020 index = expand_normal (index_expr);
11021 do_pending_stack_adjust ();
11023 do_tablejump (index, TYPE_MODE (index_type),
11024 convert_modes (TYPE_MODE (index_type),
11025 TYPE_MODE (TREE_TYPE (range)),
11026 expand_normal (range),
11027 TYPE_UNSIGNED (TREE_TYPE (range))),
11028 table_label, default_label);
11029 return 1;
11032 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11033 static rtx
11034 const_vector_from_tree (tree exp)
11036 rtvec v;
11037 unsigned i;
11038 int units;
11039 tree elt;
11040 enum machine_mode inner, mode;
11042 mode = TYPE_MODE (TREE_TYPE (exp));
11044 if (initializer_zerop (exp))
11045 return CONST0_RTX (mode);
11047 units = GET_MODE_NUNITS (mode);
11048 inner = GET_MODE_INNER (mode);
11050 v = rtvec_alloc (units);
11052 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11054 elt = VECTOR_CST_ELT (exp, i);
11056 if (TREE_CODE (elt) == REAL_CST)
11057 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11058 inner);
11059 else if (TREE_CODE (elt) == FIXED_CST)
11060 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11061 inner);
11062 else
11063 RTVEC_ELT (v, i) = immed_double_int_const (tree_to_double_int (elt),
11064 inner);
11067 return gen_rtx_CONST_VECTOR (mode, v);
11070 /* Build a decl for a personality function given a language prefix. */
11072 tree
11073 build_personality_function (const char *lang)
11075 const char *unwind_and_version;
11076 tree decl, type;
11077 char *name;
11079 switch (targetm_common.except_unwind_info (&global_options))
11081 case UI_NONE:
11082 return NULL;
11083 case UI_SJLJ:
11084 unwind_and_version = "_sj0";
11085 break;
11086 case UI_DWARF2:
11087 case UI_TARGET:
11088 unwind_and_version = "_v0";
11089 break;
11090 default:
11091 gcc_unreachable ();
11094 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11096 type = build_function_type_list (integer_type_node, integer_type_node,
11097 long_long_unsigned_type_node,
11098 ptr_type_node, ptr_type_node, NULL_TREE);
11099 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11100 get_identifier (name), type);
11101 DECL_ARTIFICIAL (decl) = 1;
11102 DECL_EXTERNAL (decl) = 1;
11103 TREE_PUBLIC (decl) = 1;
11105 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11106 are the flags assigned by targetm.encode_section_info. */
11107 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11109 return decl;
11112 /* Extracts the personality function of DECL and returns the corresponding
11113 libfunc. */
11116 get_personality_function (tree decl)
11118 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11119 enum eh_personality_kind pk;
11121 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11122 if (pk == eh_personality_none)
11123 return NULL;
11125 if (!personality
11126 && pk == eh_personality_any)
11127 personality = lang_hooks.eh_personality ();
11129 if (pk == eh_personality_lang)
11130 gcc_assert (personality != NULL_TREE);
11132 return XEXP (DECL_RTL (personality), 0);
11135 #include "gt-expr.h"