Remove dg-options for sh*-*-* from gcc.c-torture/execute/pr44683.c.
[official-gcc.git] / gcc / expr.c
blobe7a7c16a0cb0f754e819935feda05ed5c09b363d
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "machmode.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "stringpool.h"
28 #include "stor-layout.h"
29 #include "attribs.h"
30 #include "varasm.h"
31 #include "flags.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "except.h"
35 #include "function.h"
36 #include "insn-config.h"
37 #include "insn-attr.h"
38 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
39 #include "expr.h"
40 #include "optabs.h"
41 #include "libfuncs.h"
42 #include "recog.h"
43 #include "reload.h"
44 #include "typeclass.h"
45 #include "toplev.h"
46 #include "langhooks.h"
47 #include "intl.h"
48 #include "tm_p.h"
49 #include "tree-iterator.h"
50 #include "basic-block.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "gimple-expr.h"
54 #include "is-a.h"
55 #include "gimple.h"
56 #include "gimple-ssa.h"
57 #include "cgraph.h"
58 #include "tree-ssanames.h"
59 #include "target.h"
60 #include "common/common-target.h"
61 #include "timevar.h"
62 #include "df.h"
63 #include "diagnostic.h"
64 #include "tree-ssa-live.h"
65 #include "tree-outof-ssa.h"
66 #include "target-globals.h"
67 #include "params.h"
68 #include "tree-ssa-address.h"
69 #include "cfgexpand.h"
70 #include "builtins.h"
71 #include "tree-ssa.h"
73 #ifndef STACK_PUSH_CODE
74 #ifdef STACK_GROWS_DOWNWARD
75 #define STACK_PUSH_CODE PRE_DEC
76 #else
77 #define STACK_PUSH_CODE PRE_INC
78 #endif
79 #endif
82 /* If this is nonzero, we do not bother generating VOLATILE
83 around volatile memory references, and we are willing to
84 output indirect addresses. If cse is to follow, we reject
85 indirect addresses so a useful potential cse is generated;
86 if it is used only once, instruction combination will produce
87 the same indirect address eventually. */
88 int cse_not_expected;
90 /* This structure is used by move_by_pieces to describe the move to
91 be performed. */
92 struct move_by_pieces_d
94 rtx to;
95 rtx to_addr;
96 int autinc_to;
97 int explicit_inc_to;
98 rtx from;
99 rtx from_addr;
100 int autinc_from;
101 int explicit_inc_from;
102 unsigned HOST_WIDE_INT len;
103 HOST_WIDE_INT offset;
104 int reverse;
107 /* This structure is used by store_by_pieces to describe the clear to
108 be performed. */
110 struct store_by_pieces_d
112 rtx to;
113 rtx to_addr;
114 int autinc_to;
115 int explicit_inc_to;
116 unsigned HOST_WIDE_INT len;
117 HOST_WIDE_INT offset;
118 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode);
119 void *constfundata;
120 int reverse;
123 static void move_by_pieces_1 (insn_gen_fn, machine_mode,
124 struct move_by_pieces_d *);
125 static bool block_move_libcall_safe_for_call_parm (void);
126 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
127 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
128 unsigned HOST_WIDE_INT);
129 static tree emit_block_move_libcall_fn (int);
130 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
131 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, enum machine_mode);
132 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
133 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
134 static void store_by_pieces_2 (insn_gen_fn, machine_mode,
135 struct store_by_pieces_d *);
136 static tree clear_storage_libcall_fn (int);
137 static rtx_insn *compress_float_constant (rtx, rtx);
138 static rtx get_subtarget (rtx);
139 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
140 HOST_WIDE_INT, enum machine_mode,
141 tree, int, alias_set_type);
142 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
143 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
144 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
145 enum machine_mode, tree, alias_set_type, bool);
147 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
149 static int is_aligning_offset (const_tree, const_tree);
150 static void expand_operands (tree, tree, rtx, rtx*, rtx*,
151 enum expand_modifier);
152 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
153 static rtx do_store_flag (sepops, rtx, enum machine_mode);
154 #ifdef PUSH_ROUNDING
155 static void emit_single_push_insn (enum machine_mode, rtx, tree);
156 #endif
157 static void do_tablejump (rtx, enum machine_mode, rtx, rtx, rtx, int);
158 static rtx const_vector_from_tree (tree);
159 static void write_complex_part (rtx, rtx, bool);
161 /* This macro is used to determine whether move_by_pieces should be called
162 to perform a structure copy. */
163 #ifndef MOVE_BY_PIECES_P
164 #define MOVE_BY_PIECES_P(SIZE, ALIGN) \
165 (move_by_pieces_ninsns (SIZE, ALIGN, MOVE_MAX_PIECES + 1) \
166 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
167 #endif
169 /* This macro is used to determine whether clear_by_pieces should be
170 called to clear storage. */
171 #ifndef CLEAR_BY_PIECES_P
172 #define CLEAR_BY_PIECES_P(SIZE, ALIGN) \
173 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
174 < (unsigned int) CLEAR_RATIO (optimize_insn_for_speed_p ()))
175 #endif
177 /* This macro is used to determine whether store_by_pieces should be
178 called to "memset" storage with byte values other than zero. */
179 #ifndef SET_BY_PIECES_P
180 #define SET_BY_PIECES_P(SIZE, ALIGN) \
181 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
182 < (unsigned int) SET_RATIO (optimize_insn_for_speed_p ()))
183 #endif
185 /* This macro is used to determine whether store_by_pieces should be
186 called to "memcpy" storage when the source is a constant string. */
187 #ifndef STORE_BY_PIECES_P
188 #define STORE_BY_PIECES_P(SIZE, ALIGN) \
189 (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
190 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
191 #endif
193 /* This is run to set up which modes can be used
194 directly in memory and to initialize the block move optab. It is run
195 at the beginning of compilation and when the target is reinitialized. */
197 void
198 init_expr_target (void)
200 rtx insn, pat;
201 enum machine_mode mode;
202 int num_clobbers;
203 rtx mem, mem1;
204 rtx reg;
206 /* Try indexing by frame ptr and try by stack ptr.
207 It is known that on the Convex the stack ptr isn't a valid index.
208 With luck, one or the other is valid on any machine. */
209 mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
210 mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
212 /* A scratch register we can modify in-place below to avoid
213 useless RTL allocations. */
214 reg = gen_rtx_REG (VOIDmode, -1);
216 insn = rtx_alloc (INSN);
217 pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
218 PATTERN (insn) = pat;
220 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
221 mode = (enum machine_mode) ((int) mode + 1))
223 int regno;
225 direct_load[(int) mode] = direct_store[(int) mode] = 0;
226 PUT_MODE (mem, mode);
227 PUT_MODE (mem1, mode);
228 PUT_MODE (reg, mode);
230 /* See if there is some register that can be used in this mode and
231 directly loaded or stored from memory. */
233 if (mode != VOIDmode && mode != BLKmode)
234 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
235 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
236 regno++)
238 if (! HARD_REGNO_MODE_OK (regno, mode))
239 continue;
241 SET_REGNO (reg, regno);
243 SET_SRC (pat) = mem;
244 SET_DEST (pat) = reg;
245 if (recog (pat, insn, &num_clobbers) >= 0)
246 direct_load[(int) mode] = 1;
248 SET_SRC (pat) = mem1;
249 SET_DEST (pat) = reg;
250 if (recog (pat, insn, &num_clobbers) >= 0)
251 direct_load[(int) mode] = 1;
253 SET_SRC (pat) = reg;
254 SET_DEST (pat) = mem;
255 if (recog (pat, insn, &num_clobbers) >= 0)
256 direct_store[(int) mode] = 1;
258 SET_SRC (pat) = reg;
259 SET_DEST (pat) = mem1;
260 if (recog (pat, insn, &num_clobbers) >= 0)
261 direct_store[(int) mode] = 1;
265 mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
267 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
268 mode = GET_MODE_WIDER_MODE (mode))
270 enum machine_mode srcmode;
271 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
272 srcmode = GET_MODE_WIDER_MODE (srcmode))
274 enum insn_code ic;
276 ic = can_extend_p (mode, srcmode, 0);
277 if (ic == CODE_FOR_nothing)
278 continue;
280 PUT_MODE (mem, srcmode);
282 if (insn_operand_matches (ic, 1, mem))
283 float_extend_from_mem[mode][srcmode] = true;
288 /* This is run at the start of compiling a function. */
290 void
291 init_expr (void)
293 memset (&crtl->expr, 0, sizeof (crtl->expr));
296 /* Copy data from FROM to TO, where the machine modes are not the same.
297 Both modes may be integer, or both may be floating, or both may be
298 fixed-point.
299 UNSIGNEDP should be nonzero if FROM is an unsigned type.
300 This causes zero-extension instead of sign-extension. */
302 void
303 convert_move (rtx to, rtx from, int unsignedp)
305 enum machine_mode to_mode = GET_MODE (to);
306 enum machine_mode from_mode = GET_MODE (from);
307 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
308 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
309 enum insn_code code;
310 rtx libcall;
312 /* rtx code for making an equivalent value. */
313 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
314 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
317 gcc_assert (to_real == from_real);
318 gcc_assert (to_mode != BLKmode);
319 gcc_assert (from_mode != BLKmode);
321 /* If the source and destination are already the same, then there's
322 nothing to do. */
323 if (to == from)
324 return;
326 /* If FROM is a SUBREG that indicates that we have already done at least
327 the required extension, strip it. We don't handle such SUBREGs as
328 TO here. */
330 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
331 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
332 >= GET_MODE_PRECISION (to_mode))
333 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
334 from = gen_lowpart (to_mode, from), from_mode = to_mode;
336 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
338 if (to_mode == from_mode
339 || (from_mode == VOIDmode && CONSTANT_P (from)))
341 emit_move_insn (to, from);
342 return;
345 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
347 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
349 if (VECTOR_MODE_P (to_mode))
350 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
351 else
352 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
354 emit_move_insn (to, from);
355 return;
358 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
360 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
361 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
362 return;
365 if (to_real)
367 rtx value;
368 rtx_insn *insns;
369 convert_optab tab;
371 gcc_assert ((GET_MODE_PRECISION (from_mode)
372 != GET_MODE_PRECISION (to_mode))
373 || (DECIMAL_FLOAT_MODE_P (from_mode)
374 != DECIMAL_FLOAT_MODE_P (to_mode)));
376 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
377 /* Conversion between decimal float and binary float, same size. */
378 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
379 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
380 tab = sext_optab;
381 else
382 tab = trunc_optab;
384 /* Try converting directly if the insn is supported. */
386 code = convert_optab_handler (tab, to_mode, from_mode);
387 if (code != CODE_FOR_nothing)
389 emit_unop_insn (code, to, from,
390 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
391 return;
394 /* Otherwise use a libcall. */
395 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
397 /* Is this conversion implemented yet? */
398 gcc_assert (libcall);
400 start_sequence ();
401 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
402 1, from, from_mode);
403 insns = get_insns ();
404 end_sequence ();
405 emit_libcall_block (insns, to, value,
406 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
407 from)
408 : gen_rtx_FLOAT_EXTEND (to_mode, from));
409 return;
412 /* Handle pointer conversion. */ /* SPEE 900220. */
413 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
415 convert_optab ctab;
417 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
418 ctab = trunc_optab;
419 else if (unsignedp)
420 ctab = zext_optab;
421 else
422 ctab = sext_optab;
424 if (convert_optab_handler (ctab, to_mode, from_mode)
425 != CODE_FOR_nothing)
427 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
428 to, from, UNKNOWN);
429 return;
433 /* Targets are expected to provide conversion insns between PxImode and
434 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
435 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
437 enum machine_mode full_mode
438 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
440 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
441 != CODE_FOR_nothing);
443 if (full_mode != from_mode)
444 from = convert_to_mode (full_mode, from, unsignedp);
445 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
446 to, from, UNKNOWN);
447 return;
449 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
451 rtx new_from;
452 enum machine_mode full_mode
453 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
454 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
455 enum insn_code icode;
457 icode = convert_optab_handler (ctab, full_mode, from_mode);
458 gcc_assert (icode != CODE_FOR_nothing);
460 if (to_mode == full_mode)
462 emit_unop_insn (icode, to, from, UNKNOWN);
463 return;
466 new_from = gen_reg_rtx (full_mode);
467 emit_unop_insn (icode, new_from, from, UNKNOWN);
469 /* else proceed to integer conversions below. */
470 from_mode = full_mode;
471 from = new_from;
474 /* Make sure both are fixed-point modes or both are not. */
475 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
476 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
477 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
479 /* If we widen from_mode to to_mode and they are in the same class,
480 we won't saturate the result.
481 Otherwise, always saturate the result to play safe. */
482 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
483 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
484 expand_fixed_convert (to, from, 0, 0);
485 else
486 expand_fixed_convert (to, from, 0, 1);
487 return;
490 /* Now both modes are integers. */
492 /* Handle expanding beyond a word. */
493 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
494 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
496 rtx_insn *insns;
497 rtx lowpart;
498 rtx fill_value;
499 rtx lowfrom;
500 int i;
501 enum machine_mode lowpart_mode;
502 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
504 /* Try converting directly if the insn is supported. */
505 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
506 != CODE_FOR_nothing)
508 /* If FROM is a SUBREG, put it into a register. Do this
509 so that we always generate the same set of insns for
510 better cse'ing; if an intermediate assignment occurred,
511 we won't be doing the operation directly on the SUBREG. */
512 if (optimize > 0 && GET_CODE (from) == SUBREG)
513 from = force_reg (from_mode, from);
514 emit_unop_insn (code, to, from, equiv_code);
515 return;
517 /* Next, try converting via full word. */
518 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
519 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
520 != CODE_FOR_nothing))
522 rtx word_to = gen_reg_rtx (word_mode);
523 if (REG_P (to))
525 if (reg_overlap_mentioned_p (to, from))
526 from = force_reg (from_mode, from);
527 emit_clobber (to);
529 convert_move (word_to, from, unsignedp);
530 emit_unop_insn (code, to, word_to, equiv_code);
531 return;
534 /* No special multiword conversion insn; do it by hand. */
535 start_sequence ();
537 /* Since we will turn this into a no conflict block, we must ensure the
538 the source does not overlap the target so force it into an isolated
539 register when maybe so. Likewise for any MEM input, since the
540 conversion sequence might require several references to it and we
541 must ensure we're getting the same value every time. */
543 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
544 from = force_reg (from_mode, from);
546 /* Get a copy of FROM widened to a word, if necessary. */
547 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
548 lowpart_mode = word_mode;
549 else
550 lowpart_mode = from_mode;
552 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
554 lowpart = gen_lowpart (lowpart_mode, to);
555 emit_move_insn (lowpart, lowfrom);
557 /* Compute the value to put in each remaining word. */
558 if (unsignedp)
559 fill_value = const0_rtx;
560 else
561 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
562 LT, lowfrom, const0_rtx,
563 lowpart_mode, 0, -1);
565 /* Fill the remaining words. */
566 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
568 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
569 rtx subword = operand_subword (to, index, 1, to_mode);
571 gcc_assert (subword);
573 if (fill_value != subword)
574 emit_move_insn (subword, fill_value);
577 insns = get_insns ();
578 end_sequence ();
580 emit_insn (insns);
581 return;
584 /* Truncating multi-word to a word or less. */
585 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
586 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
588 if (!((MEM_P (from)
589 && ! MEM_VOLATILE_P (from)
590 && direct_load[(int) to_mode]
591 && ! mode_dependent_address_p (XEXP (from, 0),
592 MEM_ADDR_SPACE (from)))
593 || REG_P (from)
594 || GET_CODE (from) == SUBREG))
595 from = force_reg (from_mode, from);
596 convert_move (to, gen_lowpart (word_mode, from), 0);
597 return;
600 /* Now follow all the conversions between integers
601 no more than a word long. */
603 /* For truncation, usually we can just refer to FROM in a narrower mode. */
604 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
605 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
607 if (!((MEM_P (from)
608 && ! MEM_VOLATILE_P (from)
609 && direct_load[(int) to_mode]
610 && ! mode_dependent_address_p (XEXP (from, 0),
611 MEM_ADDR_SPACE (from)))
612 || REG_P (from)
613 || GET_CODE (from) == SUBREG))
614 from = force_reg (from_mode, from);
615 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
616 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
617 from = copy_to_reg (from);
618 emit_move_insn (to, gen_lowpart (to_mode, from));
619 return;
622 /* Handle extension. */
623 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
625 /* Convert directly if that works. */
626 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
627 != CODE_FOR_nothing)
629 emit_unop_insn (code, to, from, equiv_code);
630 return;
632 else
634 enum machine_mode intermediate;
635 rtx tmp;
636 int shift_amount;
638 /* Search for a mode to convert via. */
639 for (intermediate = from_mode; intermediate != VOIDmode;
640 intermediate = GET_MODE_WIDER_MODE (intermediate))
641 if (((can_extend_p (to_mode, intermediate, unsignedp)
642 != CODE_FOR_nothing)
643 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
644 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
645 && (can_extend_p (intermediate, from_mode, unsignedp)
646 != CODE_FOR_nothing))
648 convert_move (to, convert_to_mode (intermediate, from,
649 unsignedp), unsignedp);
650 return;
653 /* No suitable intermediate mode.
654 Generate what we need with shifts. */
655 shift_amount = (GET_MODE_PRECISION (to_mode)
656 - GET_MODE_PRECISION (from_mode));
657 from = gen_lowpart (to_mode, force_reg (from_mode, from));
658 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
659 to, unsignedp);
660 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
661 to, unsignedp);
662 if (tmp != to)
663 emit_move_insn (to, tmp);
664 return;
668 /* Support special truncate insns for certain modes. */
669 if (convert_optab_handler (trunc_optab, to_mode,
670 from_mode) != CODE_FOR_nothing)
672 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
673 to, from, UNKNOWN);
674 return;
677 /* Handle truncation of volatile memrefs, and so on;
678 the things that couldn't be truncated directly,
679 and for which there was no special instruction.
681 ??? Code above formerly short-circuited this, for most integer
682 mode pairs, with a force_reg in from_mode followed by a recursive
683 call to this routine. Appears always to have been wrong. */
684 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
686 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
687 emit_move_insn (to, temp);
688 return;
691 /* Mode combination is not recognized. */
692 gcc_unreachable ();
695 /* Return an rtx for a value that would result
696 from converting X to mode MODE.
697 Both X and MODE may be floating, or both integer.
698 UNSIGNEDP is nonzero if X is an unsigned value.
699 This can be done by referring to a part of X in place
700 or by copying to a new temporary with conversion. */
703 convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
705 return convert_modes (mode, VOIDmode, x, unsignedp);
708 /* Return an rtx for a value that would result
709 from converting X from mode OLDMODE to mode MODE.
710 Both modes may be floating, or both integer.
711 UNSIGNEDP is nonzero if X is an unsigned value.
713 This can be done by referring to a part of X in place
714 or by copying to a new temporary with conversion.
716 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
719 convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
721 rtx temp;
723 /* If FROM is a SUBREG that indicates that we have already done at least
724 the required extension, strip it. */
726 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
727 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
728 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
729 x = gen_lowpart (mode, SUBREG_REG (x));
731 if (GET_MODE (x) != VOIDmode)
732 oldmode = GET_MODE (x);
734 if (mode == oldmode)
735 return x;
737 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
739 /* If the caller did not tell us the old mode, then there is not
740 much to do with respect to canonicalization. We have to
741 assume that all the bits are significant. */
742 if (GET_MODE_CLASS (oldmode) != MODE_INT)
743 oldmode = MAX_MODE_INT;
744 wide_int w = wide_int::from (std::make_pair (x, oldmode),
745 GET_MODE_PRECISION (mode),
746 unsignedp ? UNSIGNED : SIGNED);
747 return immed_wide_int_const (w, mode);
750 /* We can do this with a gen_lowpart if both desired and current modes
751 are integer, and this is either a constant integer, a register, or a
752 non-volatile MEM. */
753 if (GET_MODE_CLASS (mode) == MODE_INT
754 && GET_MODE_CLASS (oldmode) == MODE_INT
755 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
756 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
757 || (REG_P (x)
758 && (!HARD_REGISTER_P (x)
759 || HARD_REGNO_MODE_OK (REGNO (x), mode))
760 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
762 return gen_lowpart (mode, x);
764 /* Converting from integer constant into mode is always equivalent to an
765 subreg operation. */
766 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
768 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
769 return simplify_gen_subreg (mode, x, oldmode, 0);
772 temp = gen_reg_rtx (mode);
773 convert_move (temp, x, unsignedp);
774 return temp;
777 /* Return the largest alignment we can use for doing a move (or store)
778 of MAX_PIECES. ALIGN is the largest alignment we could use. */
780 static unsigned int
781 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
783 enum machine_mode tmode;
785 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
786 if (align >= GET_MODE_ALIGNMENT (tmode))
787 align = GET_MODE_ALIGNMENT (tmode);
788 else
790 enum machine_mode tmode, xmode;
792 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
793 tmode != VOIDmode;
794 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
795 if (GET_MODE_SIZE (tmode) > max_pieces
796 || SLOW_UNALIGNED_ACCESS (tmode, align))
797 break;
799 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
802 return align;
805 /* Return the widest integer mode no wider than SIZE. If no such mode
806 can be found, return VOIDmode. */
808 static enum machine_mode
809 widest_int_mode_for_size (unsigned int size)
811 enum machine_mode tmode, mode = VOIDmode;
813 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
814 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
815 if (GET_MODE_SIZE (tmode) < size)
816 mode = tmode;
818 return mode;
821 /* STORE_MAX_PIECES is the number of bytes at a time that we can
822 store efficiently. Due to internal GCC limitations, this is
823 MOVE_MAX_PIECES limited by the number of bytes GCC can represent
824 for an immediate constant. */
826 #define STORE_MAX_PIECES MIN (MOVE_MAX_PIECES, 2 * sizeof (HOST_WIDE_INT))
828 /* Determine whether the LEN bytes can be moved by using several move
829 instructions. Return nonzero if a call to move_by_pieces should
830 succeed. */
833 can_move_by_pieces (unsigned HOST_WIDE_INT len ATTRIBUTE_UNUSED,
834 unsigned int align ATTRIBUTE_UNUSED)
836 return MOVE_BY_PIECES_P (len, align);
839 /* Generate several move instructions to copy LEN bytes from block FROM to
840 block TO. (These are MEM rtx's with BLKmode).
842 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
843 used to push FROM to the stack.
845 ALIGN is maximum stack alignment we can assume.
847 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
848 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
849 stpcpy. */
852 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
853 unsigned int align, int endp)
855 struct move_by_pieces_d data;
856 enum machine_mode to_addr_mode;
857 enum machine_mode from_addr_mode = get_address_mode (from);
858 rtx to_addr, from_addr = XEXP (from, 0);
859 unsigned int max_size = MOVE_MAX_PIECES + 1;
860 enum insn_code icode;
862 align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
864 data.offset = 0;
865 data.from_addr = from_addr;
866 if (to)
868 to_addr_mode = get_address_mode (to);
869 to_addr = XEXP (to, 0);
870 data.to = to;
871 data.autinc_to
872 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
873 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
874 data.reverse
875 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
877 else
879 to_addr_mode = VOIDmode;
880 to_addr = NULL_RTX;
881 data.to = NULL_RTX;
882 data.autinc_to = 1;
883 #ifdef STACK_GROWS_DOWNWARD
884 data.reverse = 1;
885 #else
886 data.reverse = 0;
887 #endif
889 data.to_addr = to_addr;
890 data.from = from;
891 data.autinc_from
892 = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
893 || GET_CODE (from_addr) == POST_INC
894 || GET_CODE (from_addr) == POST_DEC);
896 data.explicit_inc_from = 0;
897 data.explicit_inc_to = 0;
898 if (data.reverse) data.offset = len;
899 data.len = len;
901 /* If copying requires more than two move insns,
902 copy addresses to registers (to make displacements shorter)
903 and use post-increment if available. */
904 if (!(data.autinc_from && data.autinc_to)
905 && move_by_pieces_ninsns (len, align, max_size) > 2)
907 /* Find the mode of the largest move...
908 MODE might not be used depending on the definitions of the
909 USE_* macros below. */
910 enum machine_mode mode ATTRIBUTE_UNUSED
911 = widest_int_mode_for_size (max_size);
913 if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
915 data.from_addr = copy_to_mode_reg (from_addr_mode,
916 plus_constant (from_addr_mode,
917 from_addr, len));
918 data.autinc_from = 1;
919 data.explicit_inc_from = -1;
921 if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
923 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
924 data.autinc_from = 1;
925 data.explicit_inc_from = 1;
927 if (!data.autinc_from && CONSTANT_P (from_addr))
928 data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
929 if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
931 data.to_addr = copy_to_mode_reg (to_addr_mode,
932 plus_constant (to_addr_mode,
933 to_addr, len));
934 data.autinc_to = 1;
935 data.explicit_inc_to = -1;
937 if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
939 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
940 data.autinc_to = 1;
941 data.explicit_inc_to = 1;
943 if (!data.autinc_to && CONSTANT_P (to_addr))
944 data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
947 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
949 /* First move what we can in the largest integer mode, then go to
950 successively smaller modes. */
952 while (max_size > 1 && data.len > 0)
954 enum machine_mode mode = widest_int_mode_for_size (max_size);
956 if (mode == VOIDmode)
957 break;
959 icode = optab_handler (mov_optab, mode);
960 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
961 move_by_pieces_1 (GEN_FCN (icode), mode, &data);
963 max_size = GET_MODE_SIZE (mode);
966 /* The code above should have handled everything. */
967 gcc_assert (!data.len);
969 if (endp)
971 rtx to1;
973 gcc_assert (!data.reverse);
974 if (data.autinc_to)
976 if (endp == 2)
978 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
979 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
980 else
981 data.to_addr = copy_to_mode_reg (to_addr_mode,
982 plus_constant (to_addr_mode,
983 data.to_addr,
984 -1));
986 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
987 data.offset);
989 else
991 if (endp == 2)
992 --data.offset;
993 to1 = adjust_address (data.to, QImode, data.offset);
995 return to1;
997 else
998 return data.to;
1001 /* Return number of insns required to move L bytes by pieces.
1002 ALIGN (in bits) is maximum alignment we can assume. */
1004 unsigned HOST_WIDE_INT
1005 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1006 unsigned int max_size)
1008 unsigned HOST_WIDE_INT n_insns = 0;
1010 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1012 while (max_size > 1 && l > 0)
1014 enum machine_mode mode;
1015 enum insn_code icode;
1017 mode = widest_int_mode_for_size (max_size);
1019 if (mode == VOIDmode)
1020 break;
1022 icode = optab_handler (mov_optab, mode);
1023 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1024 n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1026 max_size = GET_MODE_SIZE (mode);
1029 gcc_assert (!l);
1030 return n_insns;
1033 /* Subroutine of move_by_pieces. Move as many bytes as appropriate
1034 with move instructions for mode MODE. GENFUN is the gen_... function
1035 to make a move insn for that mode. DATA has all the other info. */
1037 static void
1038 move_by_pieces_1 (insn_gen_fn genfun, machine_mode mode,
1039 struct move_by_pieces_d *data)
1041 unsigned int size = GET_MODE_SIZE (mode);
1042 rtx to1 = NULL_RTX, from1;
1044 while (data->len >= size)
1046 if (data->reverse)
1047 data->offset -= size;
1049 if (data->to)
1051 if (data->autinc_to)
1052 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1053 data->offset);
1054 else
1055 to1 = adjust_address (data->to, mode, data->offset);
1058 if (data->autinc_from)
1059 from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1060 data->offset);
1061 else
1062 from1 = adjust_address (data->from, mode, data->offset);
1064 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1065 emit_insn (gen_add2_insn (data->to_addr,
1066 gen_int_mode (-(HOST_WIDE_INT) size,
1067 GET_MODE (data->to_addr))));
1068 if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1069 emit_insn (gen_add2_insn (data->from_addr,
1070 gen_int_mode (-(HOST_WIDE_INT) size,
1071 GET_MODE (data->from_addr))));
1073 if (data->to)
1074 emit_insn ((*genfun) (to1, from1));
1075 else
1077 #ifdef PUSH_ROUNDING
1078 emit_single_push_insn (mode, from1, NULL);
1079 #else
1080 gcc_unreachable ();
1081 #endif
1084 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1085 emit_insn (gen_add2_insn (data->to_addr,
1086 gen_int_mode (size,
1087 GET_MODE (data->to_addr))));
1088 if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1089 emit_insn (gen_add2_insn (data->from_addr,
1090 gen_int_mode (size,
1091 GET_MODE (data->from_addr))));
1093 if (! data->reverse)
1094 data->offset += size;
1096 data->len -= size;
1100 /* Emit code to move a block Y to a block X. This may be done with
1101 string-move instructions, with multiple scalar move instructions,
1102 or with a library call.
1104 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1105 SIZE is an rtx that says how long they are.
1106 ALIGN is the maximum alignment we can assume they have.
1107 METHOD describes what kind of copy this is, and what mechanisms may be used.
1108 MIN_SIZE is the minimal size of block to move
1109 MAX_SIZE is the maximal size of block to move, if it can not be represented
1110 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1112 Return the address of the new block, if memcpy is called and returns it,
1113 0 otherwise. */
1116 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1117 unsigned int expected_align, HOST_WIDE_INT expected_size,
1118 unsigned HOST_WIDE_INT min_size,
1119 unsigned HOST_WIDE_INT max_size,
1120 unsigned HOST_WIDE_INT probable_max_size)
1122 bool may_use_call;
1123 rtx retval = 0;
1124 unsigned int align;
1126 gcc_assert (size);
1127 if (CONST_INT_P (size)
1128 && INTVAL (size) == 0)
1129 return 0;
1131 switch (method)
1133 case BLOCK_OP_NORMAL:
1134 case BLOCK_OP_TAILCALL:
1135 may_use_call = true;
1136 break;
1138 case BLOCK_OP_CALL_PARM:
1139 may_use_call = block_move_libcall_safe_for_call_parm ();
1141 /* Make inhibit_defer_pop nonzero around the library call
1142 to force it to pop the arguments right away. */
1143 NO_DEFER_POP;
1144 break;
1146 case BLOCK_OP_NO_LIBCALL:
1147 may_use_call = false;
1148 break;
1150 default:
1151 gcc_unreachable ();
1154 gcc_assert (MEM_P (x) && MEM_P (y));
1155 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1156 gcc_assert (align >= BITS_PER_UNIT);
1158 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1159 block copy is more efficient for other large modes, e.g. DCmode. */
1160 x = adjust_address (x, BLKmode, 0);
1161 y = adjust_address (y, BLKmode, 0);
1163 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1164 can be incorrect is coming from __builtin_memcpy. */
1165 if (CONST_INT_P (size))
1167 x = shallow_copy_rtx (x);
1168 y = shallow_copy_rtx (y);
1169 set_mem_size (x, INTVAL (size));
1170 set_mem_size (y, INTVAL (size));
1173 if (CONST_INT_P (size) && MOVE_BY_PIECES_P (INTVAL (size), align))
1174 move_by_pieces (x, y, INTVAL (size), align, 0);
1175 else if (emit_block_move_via_movmem (x, y, size, align,
1176 expected_align, expected_size,
1177 min_size, max_size, probable_max_size))
1179 else if (may_use_call
1180 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1181 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1183 /* Since x and y are passed to a libcall, mark the corresponding
1184 tree EXPR as addressable. */
1185 tree y_expr = MEM_EXPR (y);
1186 tree x_expr = MEM_EXPR (x);
1187 if (y_expr)
1188 mark_addressable (y_expr);
1189 if (x_expr)
1190 mark_addressable (x_expr);
1191 retval = emit_block_move_via_libcall (x, y, size,
1192 method == BLOCK_OP_TAILCALL);
1195 else
1196 emit_block_move_via_loop (x, y, size, align);
1198 if (method == BLOCK_OP_CALL_PARM)
1199 OK_DEFER_POP;
1201 return retval;
1205 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1207 unsigned HOST_WIDE_INT max, min = 0;
1208 if (GET_CODE (size) == CONST_INT)
1209 min = max = UINTVAL (size);
1210 else
1211 max = GET_MODE_MASK (GET_MODE (size));
1212 return emit_block_move_hints (x, y, size, method, 0, -1,
1213 min, max, max);
1216 /* A subroutine of emit_block_move. Returns true if calling the
1217 block move libcall will not clobber any parameters which may have
1218 already been placed on the stack. */
1220 static bool
1221 block_move_libcall_safe_for_call_parm (void)
1223 #if defined (REG_PARM_STACK_SPACE)
1224 tree fn;
1225 #endif
1227 /* If arguments are pushed on the stack, then they're safe. */
1228 if (PUSH_ARGS)
1229 return true;
1231 /* If registers go on the stack anyway, any argument is sure to clobber
1232 an outgoing argument. */
1233 #if defined (REG_PARM_STACK_SPACE)
1234 fn = emit_block_move_libcall_fn (false);
1235 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1236 depend on its argument. */
1237 (void) fn;
1238 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1239 && REG_PARM_STACK_SPACE (fn) != 0)
1240 return false;
1241 #endif
1243 /* If any argument goes in memory, then it might clobber an outgoing
1244 argument. */
1246 CUMULATIVE_ARGS args_so_far_v;
1247 cumulative_args_t args_so_far;
1248 tree fn, arg;
1250 fn = emit_block_move_libcall_fn (false);
1251 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1252 args_so_far = pack_cumulative_args (&args_so_far_v);
1254 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1255 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1257 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1258 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1259 NULL_TREE, true);
1260 if (!tmp || !REG_P (tmp))
1261 return false;
1262 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1263 return false;
1264 targetm.calls.function_arg_advance (args_so_far, mode,
1265 NULL_TREE, true);
1268 return true;
1271 /* A subroutine of emit_block_move. Expand a movmem pattern;
1272 return true if successful. */
1274 static bool
1275 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1276 unsigned int expected_align, HOST_WIDE_INT expected_size,
1277 unsigned HOST_WIDE_INT min_size,
1278 unsigned HOST_WIDE_INT max_size,
1279 unsigned HOST_WIDE_INT probable_max_size)
1281 int save_volatile_ok = volatile_ok;
1282 enum machine_mode mode;
1284 if (expected_align < align)
1285 expected_align = align;
1286 if (expected_size != -1)
1288 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1289 expected_size = probable_max_size;
1290 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1291 expected_size = min_size;
1294 /* Since this is a move insn, we don't care about volatility. */
1295 volatile_ok = 1;
1297 /* Try the most limited insn first, because there's no point
1298 including more than one in the machine description unless
1299 the more limited one has some advantage. */
1301 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1302 mode = GET_MODE_WIDER_MODE (mode))
1304 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1306 if (code != CODE_FOR_nothing
1307 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1308 here because if SIZE is less than the mode mask, as it is
1309 returned by the macro, it will definitely be less than the
1310 actual mode mask. Since SIZE is within the Pmode address
1311 space, we limit MODE to Pmode. */
1312 && ((CONST_INT_P (size)
1313 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1314 <= (GET_MODE_MASK (mode) >> 1)))
1315 || max_size <= (GET_MODE_MASK (mode) >> 1)
1316 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1318 struct expand_operand ops[9];
1319 unsigned int nops;
1321 /* ??? When called via emit_block_move_for_call, it'd be
1322 nice if there were some way to inform the backend, so
1323 that it doesn't fail the expansion because it thinks
1324 emitting the libcall would be more efficient. */
1325 nops = insn_data[(int) code].n_generator_args;
1326 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1328 create_fixed_operand (&ops[0], x);
1329 create_fixed_operand (&ops[1], y);
1330 /* The check above guarantees that this size conversion is valid. */
1331 create_convert_operand_to (&ops[2], size, mode, true);
1332 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1333 if (nops >= 6)
1335 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1336 create_integer_operand (&ops[5], expected_size);
1338 if (nops >= 8)
1340 create_integer_operand (&ops[6], min_size);
1341 /* If we can not represent the maximal size,
1342 make parameter NULL. */
1343 if ((HOST_WIDE_INT) max_size != -1)
1344 create_integer_operand (&ops[7], max_size);
1345 else
1346 create_fixed_operand (&ops[7], NULL);
1348 if (nops == 9)
1350 /* If we can not represent the maximal size,
1351 make parameter NULL. */
1352 if ((HOST_WIDE_INT) probable_max_size != -1)
1353 create_integer_operand (&ops[8], probable_max_size);
1354 else
1355 create_fixed_operand (&ops[8], NULL);
1357 if (maybe_expand_insn (code, nops, ops))
1359 volatile_ok = save_volatile_ok;
1360 return true;
1365 volatile_ok = save_volatile_ok;
1366 return false;
1369 /* A subroutine of emit_block_move. Expand a call to memcpy.
1370 Return the return value from memcpy, 0 otherwise. */
1373 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1375 rtx dst_addr, src_addr;
1376 tree call_expr, fn, src_tree, dst_tree, size_tree;
1377 enum machine_mode size_mode;
1378 rtx retval;
1380 /* Emit code to copy the addresses of DST and SRC and SIZE into new
1381 pseudos. We can then place those new pseudos into a VAR_DECL and
1382 use them later. */
1384 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1385 src_addr = copy_addr_to_reg (XEXP (src, 0));
1387 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1388 src_addr = convert_memory_address (ptr_mode, src_addr);
1390 dst_tree = make_tree (ptr_type_node, dst_addr);
1391 src_tree = make_tree (ptr_type_node, src_addr);
1393 size_mode = TYPE_MODE (sizetype);
1395 size = convert_to_mode (size_mode, size, 1);
1396 size = copy_to_mode_reg (size_mode, size);
1398 /* It is incorrect to use the libcall calling conventions to call
1399 memcpy in this context. This could be a user call to memcpy and
1400 the user may wish to examine the return value from memcpy. For
1401 targets where libcalls and normal calls have different conventions
1402 for returning pointers, we could end up generating incorrect code. */
1404 size_tree = make_tree (sizetype, size);
1406 fn = emit_block_move_libcall_fn (true);
1407 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1408 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1410 retval = expand_normal (call_expr);
1412 return retval;
1415 /* A subroutine of emit_block_move_via_libcall. Create the tree node
1416 for the function we use for block copies. */
1418 static GTY(()) tree block_move_fn;
1420 void
1421 init_block_move_fn (const char *asmspec)
1423 if (!block_move_fn)
1425 tree args, fn, attrs, attr_args;
1427 fn = get_identifier ("memcpy");
1428 args = build_function_type_list (ptr_type_node, ptr_type_node,
1429 const_ptr_type_node, sizetype,
1430 NULL_TREE);
1432 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1433 DECL_EXTERNAL (fn) = 1;
1434 TREE_PUBLIC (fn) = 1;
1435 DECL_ARTIFICIAL (fn) = 1;
1436 TREE_NOTHROW (fn) = 1;
1437 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1438 DECL_VISIBILITY_SPECIFIED (fn) = 1;
1440 attr_args = build_tree_list (NULL_TREE, build_string (1, "1"));
1441 attrs = tree_cons (get_identifier ("fn spec"), attr_args, NULL);
1443 decl_attributes (&fn, attrs, ATTR_FLAG_BUILT_IN);
1445 block_move_fn = fn;
1448 if (asmspec)
1449 set_user_assembler_name (block_move_fn, asmspec);
1452 static tree
1453 emit_block_move_libcall_fn (int for_call)
1455 static bool emitted_extern;
1457 if (!block_move_fn)
1458 init_block_move_fn (NULL);
1460 if (for_call && !emitted_extern)
1462 emitted_extern = true;
1463 make_decl_rtl (block_move_fn);
1466 return block_move_fn;
1469 /* A subroutine of emit_block_move. Copy the data via an explicit
1470 loop. This is used only when libcalls are forbidden. */
1471 /* ??? It'd be nice to copy in hunks larger than QImode. */
1473 static void
1474 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1475 unsigned int align ATTRIBUTE_UNUSED)
1477 rtx_code_label *cmp_label, *top_label;
1478 rtx iter, x_addr, y_addr, tmp;
1479 enum machine_mode x_addr_mode = get_address_mode (x);
1480 enum machine_mode y_addr_mode = get_address_mode (y);
1481 enum machine_mode iter_mode;
1483 iter_mode = GET_MODE (size);
1484 if (iter_mode == VOIDmode)
1485 iter_mode = word_mode;
1487 top_label = gen_label_rtx ();
1488 cmp_label = gen_label_rtx ();
1489 iter = gen_reg_rtx (iter_mode);
1491 emit_move_insn (iter, const0_rtx);
1493 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1494 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1495 do_pending_stack_adjust ();
1497 emit_jump (cmp_label);
1498 emit_label (top_label);
1500 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1501 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1503 if (x_addr_mode != y_addr_mode)
1504 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1505 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1507 x = change_address (x, QImode, x_addr);
1508 y = change_address (y, QImode, y_addr);
1510 emit_move_insn (x, y);
1512 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1513 true, OPTAB_LIB_WIDEN);
1514 if (tmp != iter)
1515 emit_move_insn (iter, tmp);
1517 emit_label (cmp_label);
1519 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1520 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1523 /* Copy all or part of a value X into registers starting at REGNO.
1524 The number of registers to be filled is NREGS. */
1526 void
1527 move_block_to_reg (int regno, rtx x, int nregs, enum machine_mode mode)
1529 int i;
1530 #ifdef HAVE_load_multiple
1531 rtx pat;
1532 rtx_insn *last;
1533 #endif
1535 if (nregs == 0)
1536 return;
1538 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1539 x = validize_mem (force_const_mem (mode, x));
1541 /* See if the machine can do this with a load multiple insn. */
1542 #ifdef HAVE_load_multiple
1543 if (HAVE_load_multiple)
1545 last = get_last_insn ();
1546 pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1547 GEN_INT (nregs));
1548 if (pat)
1550 emit_insn (pat);
1551 return;
1553 else
1554 delete_insns_since (last);
1556 #endif
1558 for (i = 0; i < nregs; i++)
1559 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1560 operand_subword_force (x, i, mode));
1563 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1564 The number of registers to be filled is NREGS. */
1566 void
1567 move_block_from_reg (int regno, rtx x, int nregs)
1569 int i;
1571 if (nregs == 0)
1572 return;
1574 /* See if the machine can do this with a store multiple insn. */
1575 #ifdef HAVE_store_multiple
1576 if (HAVE_store_multiple)
1578 rtx_insn *last = get_last_insn ();
1579 rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1580 GEN_INT (nregs));
1581 if (pat)
1583 emit_insn (pat);
1584 return;
1586 else
1587 delete_insns_since (last);
1589 #endif
1591 for (i = 0; i < nregs; i++)
1593 rtx tem = operand_subword (x, i, 1, BLKmode);
1595 gcc_assert (tem);
1597 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1601 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1602 ORIG, where ORIG is a non-consecutive group of registers represented by
1603 a PARALLEL. The clone is identical to the original except in that the
1604 original set of registers is replaced by a new set of pseudo registers.
1605 The new set has the same modes as the original set. */
1608 gen_group_rtx (rtx orig)
1610 int i, length;
1611 rtx *tmps;
1613 gcc_assert (GET_CODE (orig) == PARALLEL);
1615 length = XVECLEN (orig, 0);
1616 tmps = XALLOCAVEC (rtx, length);
1618 /* Skip a NULL entry in first slot. */
1619 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1621 if (i)
1622 tmps[0] = 0;
1624 for (; i < length; i++)
1626 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1627 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1629 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1632 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1635 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
1636 except that values are placed in TMPS[i], and must later be moved
1637 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
1639 static void
1640 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1642 rtx src;
1643 int start, i;
1644 enum machine_mode m = GET_MODE (orig_src);
1646 gcc_assert (GET_CODE (dst) == PARALLEL);
1648 if (m != VOIDmode
1649 && !SCALAR_INT_MODE_P (m)
1650 && !MEM_P (orig_src)
1651 && GET_CODE (orig_src) != CONCAT)
1653 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1654 if (imode == BLKmode)
1655 src = assign_stack_temp (GET_MODE (orig_src), ssize);
1656 else
1657 src = gen_reg_rtx (imode);
1658 if (imode != BLKmode)
1659 src = gen_lowpart (GET_MODE (orig_src), src);
1660 emit_move_insn (src, orig_src);
1661 /* ...and back again. */
1662 if (imode != BLKmode)
1663 src = gen_lowpart (imode, src);
1664 emit_group_load_1 (tmps, dst, src, type, ssize);
1665 return;
1668 /* Check for a NULL entry, used to indicate that the parameter goes
1669 both on the stack and in registers. */
1670 if (XEXP (XVECEXP (dst, 0, 0), 0))
1671 start = 0;
1672 else
1673 start = 1;
1675 /* Process the pieces. */
1676 for (i = start; i < XVECLEN (dst, 0); i++)
1678 enum machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1679 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1680 unsigned int bytelen = GET_MODE_SIZE (mode);
1681 int shift = 0;
1683 /* Handle trailing fragments that run over the size of the struct. */
1684 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1686 /* Arrange to shift the fragment to where it belongs.
1687 extract_bit_field loads to the lsb of the reg. */
1688 if (
1689 #ifdef BLOCK_REG_PADDING
1690 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1691 == (BYTES_BIG_ENDIAN ? upward : downward)
1692 #else
1693 BYTES_BIG_ENDIAN
1694 #endif
1696 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1697 bytelen = ssize - bytepos;
1698 gcc_assert (bytelen > 0);
1701 /* If we won't be loading directly from memory, protect the real source
1702 from strange tricks we might play; but make sure that the source can
1703 be loaded directly into the destination. */
1704 src = orig_src;
1705 if (!MEM_P (orig_src)
1706 && (!CONSTANT_P (orig_src)
1707 || (GET_MODE (orig_src) != mode
1708 && GET_MODE (orig_src) != VOIDmode)))
1710 if (GET_MODE (orig_src) == VOIDmode)
1711 src = gen_reg_rtx (mode);
1712 else
1713 src = gen_reg_rtx (GET_MODE (orig_src));
1715 emit_move_insn (src, orig_src);
1718 /* Optimize the access just a bit. */
1719 if (MEM_P (src)
1720 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1721 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1722 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1723 && bytelen == GET_MODE_SIZE (mode))
1725 tmps[i] = gen_reg_rtx (mode);
1726 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1728 else if (COMPLEX_MODE_P (mode)
1729 && GET_MODE (src) == mode
1730 && bytelen == GET_MODE_SIZE (mode))
1731 /* Let emit_move_complex do the bulk of the work. */
1732 tmps[i] = src;
1733 else if (GET_CODE (src) == CONCAT)
1735 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1736 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1738 if ((bytepos == 0 && bytelen == slen0)
1739 || (bytepos != 0 && bytepos + bytelen <= slen))
1741 /* The following assumes that the concatenated objects all
1742 have the same size. In this case, a simple calculation
1743 can be used to determine the object and the bit field
1744 to be extracted. */
1745 tmps[i] = XEXP (src, bytepos / slen0);
1746 if (! CONSTANT_P (tmps[i])
1747 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1748 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1749 (bytepos % slen0) * BITS_PER_UNIT,
1750 1, NULL_RTX, mode, mode);
1752 else
1754 rtx mem;
1756 gcc_assert (!bytepos);
1757 mem = assign_stack_temp (GET_MODE (src), slen);
1758 emit_move_insn (mem, src);
1759 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1760 0, 1, NULL_RTX, mode, mode);
1763 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1764 SIMD register, which is currently broken. While we get GCC
1765 to emit proper RTL for these cases, let's dump to memory. */
1766 else if (VECTOR_MODE_P (GET_MODE (dst))
1767 && REG_P (src))
1769 int slen = GET_MODE_SIZE (GET_MODE (src));
1770 rtx mem;
1772 mem = assign_stack_temp (GET_MODE (src), slen);
1773 emit_move_insn (mem, src);
1774 tmps[i] = adjust_address (mem, mode, (int) bytepos);
1776 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1777 && XVECLEN (dst, 0) > 1)
1778 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
1779 else if (CONSTANT_P (src))
1781 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1783 if (len == ssize)
1784 tmps[i] = src;
1785 else
1787 rtx first, second;
1789 /* TODO: const_wide_int can have sizes other than this... */
1790 gcc_assert (2 * len == ssize);
1791 split_double (src, &first, &second);
1792 if (i)
1793 tmps[i] = second;
1794 else
1795 tmps[i] = first;
1798 else if (REG_P (src) && GET_MODE (src) == mode)
1799 tmps[i] = src;
1800 else
1801 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1802 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
1803 mode, mode);
1805 if (shift)
1806 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1807 shift, tmps[i], 0);
1811 /* Emit code to move a block SRC of type TYPE to a block DST,
1812 where DST is non-consecutive registers represented by a PARALLEL.
1813 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1814 if not known. */
1816 void
1817 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1819 rtx *tmps;
1820 int i;
1822 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1823 emit_group_load_1 (tmps, dst, src, type, ssize);
1825 /* Copy the extracted pieces into the proper (probable) hard regs. */
1826 for (i = 0; i < XVECLEN (dst, 0); i++)
1828 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1829 if (d == NULL)
1830 continue;
1831 emit_move_insn (d, tmps[i]);
1835 /* Similar, but load SRC into new pseudos in a format that looks like
1836 PARALLEL. This can later be fed to emit_group_move to get things
1837 in the right place. */
1840 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1842 rtvec vec;
1843 int i;
1845 vec = rtvec_alloc (XVECLEN (parallel, 0));
1846 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1848 /* Convert the vector to look just like the original PARALLEL, except
1849 with the computed values. */
1850 for (i = 0; i < XVECLEN (parallel, 0); i++)
1852 rtx e = XVECEXP (parallel, 0, i);
1853 rtx d = XEXP (e, 0);
1855 if (d)
1857 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1858 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1860 RTVEC_ELT (vec, i) = e;
1863 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1866 /* Emit code to move a block SRC to block DST, where SRC and DST are
1867 non-consecutive groups of registers, each represented by a PARALLEL. */
1869 void
1870 emit_group_move (rtx dst, rtx src)
1872 int i;
1874 gcc_assert (GET_CODE (src) == PARALLEL
1875 && GET_CODE (dst) == PARALLEL
1876 && XVECLEN (src, 0) == XVECLEN (dst, 0));
1878 /* Skip first entry if NULL. */
1879 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1880 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1881 XEXP (XVECEXP (src, 0, i), 0));
1884 /* Move a group of registers represented by a PARALLEL into pseudos. */
1887 emit_group_move_into_temps (rtx src)
1889 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1890 int i;
1892 for (i = 0; i < XVECLEN (src, 0); i++)
1894 rtx e = XVECEXP (src, 0, i);
1895 rtx d = XEXP (e, 0);
1897 if (d)
1898 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1899 RTVEC_ELT (vec, i) = e;
1902 return gen_rtx_PARALLEL (GET_MODE (src), vec);
1905 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1906 where SRC is non-consecutive registers represented by a PARALLEL.
1907 SSIZE represents the total size of block ORIG_DST, or -1 if not
1908 known. */
1910 void
1911 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1913 rtx *tmps, dst;
1914 int start, finish, i;
1915 enum machine_mode m = GET_MODE (orig_dst);
1917 gcc_assert (GET_CODE (src) == PARALLEL);
1919 if (!SCALAR_INT_MODE_P (m)
1920 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1922 enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1923 if (imode == BLKmode)
1924 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
1925 else
1926 dst = gen_reg_rtx (imode);
1927 emit_group_store (dst, src, type, ssize);
1928 if (imode != BLKmode)
1929 dst = gen_lowpart (GET_MODE (orig_dst), dst);
1930 emit_move_insn (orig_dst, dst);
1931 return;
1934 /* Check for a NULL entry, used to indicate that the parameter goes
1935 both on the stack and in registers. */
1936 if (XEXP (XVECEXP (src, 0, 0), 0))
1937 start = 0;
1938 else
1939 start = 1;
1940 finish = XVECLEN (src, 0);
1942 tmps = XALLOCAVEC (rtx, finish);
1944 /* Copy the (probable) hard regs into pseudos. */
1945 for (i = start; i < finish; i++)
1947 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1948 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1950 tmps[i] = gen_reg_rtx (GET_MODE (reg));
1951 emit_move_insn (tmps[i], reg);
1953 else
1954 tmps[i] = reg;
1957 /* If we won't be storing directly into memory, protect the real destination
1958 from strange tricks we might play. */
1959 dst = orig_dst;
1960 if (GET_CODE (dst) == PARALLEL)
1962 rtx temp;
1964 /* We can get a PARALLEL dst if there is a conditional expression in
1965 a return statement. In that case, the dst and src are the same,
1966 so no action is necessary. */
1967 if (rtx_equal_p (dst, src))
1968 return;
1970 /* It is unclear if we can ever reach here, but we may as well handle
1971 it. Allocate a temporary, and split this into a store/load to/from
1972 the temporary. */
1973 temp = assign_stack_temp (GET_MODE (dst), ssize);
1974 emit_group_store (temp, src, type, ssize);
1975 emit_group_load (dst, temp, type, ssize);
1976 return;
1978 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1980 enum machine_mode outer = GET_MODE (dst);
1981 enum machine_mode inner;
1982 HOST_WIDE_INT bytepos;
1983 bool done = false;
1984 rtx temp;
1986 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1987 dst = gen_reg_rtx (outer);
1989 /* Make life a bit easier for combine. */
1990 /* If the first element of the vector is the low part
1991 of the destination mode, use a paradoxical subreg to
1992 initialize the destination. */
1993 if (start < finish)
1995 inner = GET_MODE (tmps[start]);
1996 bytepos = subreg_lowpart_offset (inner, outer);
1997 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1999 temp = simplify_gen_subreg (outer, tmps[start],
2000 inner, 0);
2001 if (temp)
2003 emit_move_insn (dst, temp);
2004 done = true;
2005 start++;
2010 /* If the first element wasn't the low part, try the last. */
2011 if (!done
2012 && start < finish - 1)
2014 inner = GET_MODE (tmps[finish - 1]);
2015 bytepos = subreg_lowpart_offset (inner, outer);
2016 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2018 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2019 inner, 0);
2020 if (temp)
2022 emit_move_insn (dst, temp);
2023 done = true;
2024 finish--;
2029 /* Otherwise, simply initialize the result to zero. */
2030 if (!done)
2031 emit_move_insn (dst, CONST0_RTX (outer));
2034 /* Process the pieces. */
2035 for (i = start; i < finish; i++)
2037 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2038 enum machine_mode mode = GET_MODE (tmps[i]);
2039 unsigned int bytelen = GET_MODE_SIZE (mode);
2040 unsigned int adj_bytelen;
2041 rtx dest = dst;
2043 /* Handle trailing fragments that run over the size of the struct. */
2044 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2045 adj_bytelen = ssize - bytepos;
2046 else
2047 adj_bytelen = bytelen;
2049 if (GET_CODE (dst) == CONCAT)
2051 if (bytepos + adj_bytelen
2052 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2053 dest = XEXP (dst, 0);
2054 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2056 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2057 dest = XEXP (dst, 1);
2059 else
2061 enum machine_mode dest_mode = GET_MODE (dest);
2062 enum machine_mode tmp_mode = GET_MODE (tmps[i]);
2064 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2066 if (GET_MODE_ALIGNMENT (dest_mode)
2067 >= GET_MODE_ALIGNMENT (tmp_mode))
2069 dest = assign_stack_temp (dest_mode,
2070 GET_MODE_SIZE (dest_mode));
2071 emit_move_insn (adjust_address (dest,
2072 tmp_mode,
2073 bytepos),
2074 tmps[i]);
2075 dst = dest;
2077 else
2079 dest = assign_stack_temp (tmp_mode,
2080 GET_MODE_SIZE (tmp_mode));
2081 emit_move_insn (dest, tmps[i]);
2082 dst = adjust_address (dest, dest_mode, bytepos);
2084 break;
2088 /* Handle trailing fragments that run over the size of the struct. */
2089 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2091 /* store_bit_field always takes its value from the lsb.
2092 Move the fragment to the lsb if it's not already there. */
2093 if (
2094 #ifdef BLOCK_REG_PADDING
2095 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2096 == (BYTES_BIG_ENDIAN ? upward : downward)
2097 #else
2098 BYTES_BIG_ENDIAN
2099 #endif
2102 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2103 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2104 shift, tmps[i], 0);
2107 /* Make sure not to write past the end of the struct. */
2108 store_bit_field (dest,
2109 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2110 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2111 VOIDmode, tmps[i]);
2114 /* Optimize the access just a bit. */
2115 else if (MEM_P (dest)
2116 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2117 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2118 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2119 && bytelen == GET_MODE_SIZE (mode))
2120 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2122 else
2123 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2124 0, 0, mode, tmps[i]);
2127 /* Copy from the pseudo into the (probable) hard reg. */
2128 if (orig_dst != dst)
2129 emit_move_insn (orig_dst, dst);
2132 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2133 of the value stored in X. */
2136 maybe_emit_group_store (rtx x, tree type)
2138 enum machine_mode mode = TYPE_MODE (type);
2139 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2140 if (GET_CODE (x) == PARALLEL)
2142 rtx result = gen_reg_rtx (mode);
2143 emit_group_store (result, x, type, int_size_in_bytes (type));
2144 return result;
2146 return x;
2149 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2151 This is used on targets that return BLKmode values in registers. */
2153 void
2154 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2156 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2157 rtx src = NULL, dst = NULL;
2158 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2159 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2160 enum machine_mode mode = GET_MODE (srcreg);
2161 enum machine_mode tmode = GET_MODE (target);
2162 enum machine_mode copy_mode;
2164 /* BLKmode registers created in the back-end shouldn't have survived. */
2165 gcc_assert (mode != BLKmode);
2167 /* If the structure doesn't take up a whole number of words, see whether
2168 SRCREG is padded on the left or on the right. If it's on the left,
2169 set PADDING_CORRECTION to the number of bits to skip.
2171 In most ABIs, the structure will be returned at the least end of
2172 the register, which translates to right padding on little-endian
2173 targets and left padding on big-endian targets. The opposite
2174 holds if the structure is returned at the most significant
2175 end of the register. */
2176 if (bytes % UNITS_PER_WORD != 0
2177 && (targetm.calls.return_in_msb (type)
2178 ? !BYTES_BIG_ENDIAN
2179 : BYTES_BIG_ENDIAN))
2180 padding_correction
2181 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2183 /* We can use a single move if we have an exact mode for the size. */
2184 else if (MEM_P (target)
2185 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2186 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2187 && bytes == GET_MODE_SIZE (mode))
2189 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2190 return;
2193 /* And if we additionally have the same mode for a register. */
2194 else if (REG_P (target)
2195 && GET_MODE (target) == mode
2196 && bytes == GET_MODE_SIZE (mode))
2198 emit_move_insn (target, srcreg);
2199 return;
2202 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2203 into a new pseudo which is a full word. */
2204 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2206 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2207 mode = word_mode;
2210 /* Copy the structure BITSIZE bits at a time. If the target lives in
2211 memory, take care of not reading/writing past its end by selecting
2212 a copy mode suited to BITSIZE. This should always be possible given
2213 how it is computed.
2215 If the target lives in register, make sure not to select a copy mode
2216 larger than the mode of the register.
2218 We could probably emit more efficient code for machines which do not use
2219 strict alignment, but it doesn't seem worth the effort at the current
2220 time. */
2222 copy_mode = word_mode;
2223 if (MEM_P (target))
2225 enum machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2226 if (mem_mode != BLKmode)
2227 copy_mode = mem_mode;
2229 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2230 copy_mode = tmode;
2232 for (bitpos = 0, xbitpos = padding_correction;
2233 bitpos < bytes * BITS_PER_UNIT;
2234 bitpos += bitsize, xbitpos += bitsize)
2236 /* We need a new source operand each time xbitpos is on a
2237 word boundary and when xbitpos == padding_correction
2238 (the first time through). */
2239 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2240 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2242 /* We need a new destination operand each time bitpos is on
2243 a word boundary. */
2244 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2245 dst = target;
2246 else if (bitpos % BITS_PER_WORD == 0)
2247 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2249 /* Use xbitpos for the source extraction (right justified) and
2250 bitpos for the destination store (left justified). */
2251 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2252 extract_bit_field (src, bitsize,
2253 xbitpos % BITS_PER_WORD, 1,
2254 NULL_RTX, copy_mode, copy_mode));
2258 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2259 register if it contains any data, otherwise return null.
2261 This is used on targets that return BLKmode values in registers. */
2264 copy_blkmode_to_reg (enum machine_mode mode, tree src)
2266 int i, n_regs;
2267 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2268 unsigned int bitsize;
2269 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2270 enum machine_mode dst_mode;
2272 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2274 x = expand_normal (src);
2276 bytes = int_size_in_bytes (TREE_TYPE (src));
2277 if (bytes == 0)
2278 return NULL_RTX;
2280 /* If the structure doesn't take up a whole number of words, see
2281 whether the register value should be padded on the left or on
2282 the right. Set PADDING_CORRECTION to the number of padding
2283 bits needed on the left side.
2285 In most ABIs, the structure will be returned at the least end of
2286 the register, which translates to right padding on little-endian
2287 targets and left padding on big-endian targets. The opposite
2288 holds if the structure is returned at the most significant
2289 end of the register. */
2290 if (bytes % UNITS_PER_WORD != 0
2291 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2292 ? !BYTES_BIG_ENDIAN
2293 : BYTES_BIG_ENDIAN))
2294 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2295 * BITS_PER_UNIT));
2297 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2298 dst_words = XALLOCAVEC (rtx, n_regs);
2299 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2301 /* Copy the structure BITSIZE bits at a time. */
2302 for (bitpos = 0, xbitpos = padding_correction;
2303 bitpos < bytes * BITS_PER_UNIT;
2304 bitpos += bitsize, xbitpos += bitsize)
2306 /* We need a new destination pseudo each time xbitpos is
2307 on a word boundary and when xbitpos == padding_correction
2308 (the first time through). */
2309 if (xbitpos % BITS_PER_WORD == 0
2310 || xbitpos == padding_correction)
2312 /* Generate an appropriate register. */
2313 dst_word = gen_reg_rtx (word_mode);
2314 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2316 /* Clear the destination before we move anything into it. */
2317 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2320 /* We need a new source operand each time bitpos is on a word
2321 boundary. */
2322 if (bitpos % BITS_PER_WORD == 0)
2323 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2325 /* Use bitpos for the source extraction (left justified) and
2326 xbitpos for the destination store (right justified). */
2327 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2328 0, 0, word_mode,
2329 extract_bit_field (src_word, bitsize,
2330 bitpos % BITS_PER_WORD, 1,
2331 NULL_RTX, word_mode, word_mode));
2334 if (mode == BLKmode)
2336 /* Find the smallest integer mode large enough to hold the
2337 entire structure. */
2338 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2339 mode != VOIDmode;
2340 mode = GET_MODE_WIDER_MODE (mode))
2341 /* Have we found a large enough mode? */
2342 if (GET_MODE_SIZE (mode) >= bytes)
2343 break;
2345 /* A suitable mode should have been found. */
2346 gcc_assert (mode != VOIDmode);
2349 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2350 dst_mode = word_mode;
2351 else
2352 dst_mode = mode;
2353 dst = gen_reg_rtx (dst_mode);
2355 for (i = 0; i < n_regs; i++)
2356 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2358 if (mode != dst_mode)
2359 dst = gen_lowpart (mode, dst);
2361 return dst;
2364 /* Add a USE expression for REG to the (possibly empty) list pointed
2365 to by CALL_FUSAGE. REG must denote a hard register. */
2367 void
2368 use_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
2370 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2372 *call_fusage
2373 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2376 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2377 to by CALL_FUSAGE. REG must denote a hard register. */
2379 void
2380 clobber_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
2382 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2384 *call_fusage
2385 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2388 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2389 starting at REGNO. All of these registers must be hard registers. */
2391 void
2392 use_regs (rtx *call_fusage, int regno, int nregs)
2394 int i;
2396 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2398 for (i = 0; i < nregs; i++)
2399 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2402 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2403 PARALLEL REGS. This is for calls that pass values in multiple
2404 non-contiguous locations. The Irix 6 ABI has examples of this. */
2406 void
2407 use_group_regs (rtx *call_fusage, rtx regs)
2409 int i;
2411 for (i = 0; i < XVECLEN (regs, 0); i++)
2413 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2415 /* A NULL entry means the parameter goes both on the stack and in
2416 registers. This can also be a MEM for targets that pass values
2417 partially on the stack and partially in registers. */
2418 if (reg != 0 && REG_P (reg))
2419 use_reg (call_fusage, reg);
2423 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2424 assigment and the code of the expresion on the RHS is CODE. Return
2425 NULL otherwise. */
2427 static gimple
2428 get_def_for_expr (tree name, enum tree_code code)
2430 gimple def_stmt;
2432 if (TREE_CODE (name) != SSA_NAME)
2433 return NULL;
2435 def_stmt = get_gimple_for_ssa_name (name);
2436 if (!def_stmt
2437 || gimple_assign_rhs_code (def_stmt) != code)
2438 return NULL;
2440 return def_stmt;
2443 #ifdef HAVE_conditional_move
2444 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2445 assigment and the class of the expresion on the RHS is CLASS. Return
2446 NULL otherwise. */
2448 static gimple
2449 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2451 gimple def_stmt;
2453 if (TREE_CODE (name) != SSA_NAME)
2454 return NULL;
2456 def_stmt = get_gimple_for_ssa_name (name);
2457 if (!def_stmt
2458 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2459 return NULL;
2461 return def_stmt;
2463 #endif
2466 /* Determine whether the LEN bytes generated by CONSTFUN can be
2467 stored to memory using several move instructions. CONSTFUNDATA is
2468 a pointer which will be passed as argument in every CONSTFUN call.
2469 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2470 a memset operation and false if it's a copy of a constant string.
2471 Return nonzero if a call to store_by_pieces should succeed. */
2474 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2475 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2476 void *constfundata, unsigned int align, bool memsetp)
2478 unsigned HOST_WIDE_INT l;
2479 unsigned int max_size;
2480 HOST_WIDE_INT offset = 0;
2481 enum machine_mode mode;
2482 enum insn_code icode;
2483 int reverse;
2484 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
2485 rtx cst ATTRIBUTE_UNUSED;
2487 if (len == 0)
2488 return 1;
2490 if (! (memsetp
2491 ? SET_BY_PIECES_P (len, align)
2492 : STORE_BY_PIECES_P (len, align)))
2493 return 0;
2495 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2497 /* We would first store what we can in the largest integer mode, then go to
2498 successively smaller modes. */
2500 for (reverse = 0;
2501 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2502 reverse++)
2504 l = len;
2505 max_size = STORE_MAX_PIECES + 1;
2506 while (max_size > 1 && l > 0)
2508 mode = widest_int_mode_for_size (max_size);
2510 if (mode == VOIDmode)
2511 break;
2513 icode = optab_handler (mov_optab, mode);
2514 if (icode != CODE_FOR_nothing
2515 && align >= GET_MODE_ALIGNMENT (mode))
2517 unsigned int size = GET_MODE_SIZE (mode);
2519 while (l >= size)
2521 if (reverse)
2522 offset -= size;
2524 cst = (*constfun) (constfundata, offset, mode);
2525 if (!targetm.legitimate_constant_p (mode, cst))
2526 return 0;
2528 if (!reverse)
2529 offset += size;
2531 l -= size;
2535 max_size = GET_MODE_SIZE (mode);
2538 /* The code above should have handled everything. */
2539 gcc_assert (!l);
2542 return 1;
2545 /* Generate several move instructions to store LEN bytes generated by
2546 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
2547 pointer which will be passed as argument in every CONSTFUN call.
2548 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
2549 a memset operation and false if it's a copy of a constant string.
2550 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2551 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2552 stpcpy. */
2555 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2556 rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2557 void *constfundata, unsigned int align, bool memsetp, int endp)
2559 enum machine_mode to_addr_mode = get_address_mode (to);
2560 struct store_by_pieces_d data;
2562 if (len == 0)
2564 gcc_assert (endp != 2);
2565 return to;
2568 gcc_assert (memsetp
2569 ? SET_BY_PIECES_P (len, align)
2570 : STORE_BY_PIECES_P (len, align));
2571 data.constfun = constfun;
2572 data.constfundata = constfundata;
2573 data.len = len;
2574 data.to = to;
2575 store_by_pieces_1 (&data, align);
2576 if (endp)
2578 rtx to1;
2580 gcc_assert (!data.reverse);
2581 if (data.autinc_to)
2583 if (endp == 2)
2585 if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2586 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2587 else
2588 data.to_addr = copy_to_mode_reg (to_addr_mode,
2589 plus_constant (to_addr_mode,
2590 data.to_addr,
2591 -1));
2593 to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2594 data.offset);
2596 else
2598 if (endp == 2)
2599 --data.offset;
2600 to1 = adjust_address (data.to, QImode, data.offset);
2602 return to1;
2604 else
2605 return data.to;
2608 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
2609 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2611 static void
2612 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2614 struct store_by_pieces_d data;
2616 if (len == 0)
2617 return;
2619 data.constfun = clear_by_pieces_1;
2620 data.constfundata = NULL;
2621 data.len = len;
2622 data.to = to;
2623 store_by_pieces_1 (&data, align);
2626 /* Callback routine for clear_by_pieces.
2627 Return const0_rtx unconditionally. */
2629 static rtx
2630 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2631 HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2632 enum machine_mode mode ATTRIBUTE_UNUSED)
2634 return const0_rtx;
2637 /* Subroutine of clear_by_pieces and store_by_pieces.
2638 Generate several move instructions to store LEN bytes of block TO. (A MEM
2639 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
2641 static void
2642 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2643 unsigned int align ATTRIBUTE_UNUSED)
2645 enum machine_mode to_addr_mode = get_address_mode (data->to);
2646 rtx to_addr = XEXP (data->to, 0);
2647 unsigned int max_size = STORE_MAX_PIECES + 1;
2648 enum insn_code icode;
2650 data->offset = 0;
2651 data->to_addr = to_addr;
2652 data->autinc_to
2653 = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2654 || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2656 data->explicit_inc_to = 0;
2657 data->reverse
2658 = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2659 if (data->reverse)
2660 data->offset = data->len;
2662 /* If storing requires more than two move insns,
2663 copy addresses to registers (to make displacements shorter)
2664 and use post-increment if available. */
2665 if (!data->autinc_to
2666 && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2668 /* Determine the main mode we'll be using.
2669 MODE might not be used depending on the definitions of the
2670 USE_* macros below. */
2671 enum machine_mode mode ATTRIBUTE_UNUSED
2672 = widest_int_mode_for_size (max_size);
2674 if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2676 data->to_addr = copy_to_mode_reg (to_addr_mode,
2677 plus_constant (to_addr_mode,
2678 to_addr,
2679 data->len));
2680 data->autinc_to = 1;
2681 data->explicit_inc_to = -1;
2684 if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2685 && ! data->autinc_to)
2687 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2688 data->autinc_to = 1;
2689 data->explicit_inc_to = 1;
2692 if ( !data->autinc_to && CONSTANT_P (to_addr))
2693 data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2696 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2698 /* First store what we can in the largest integer mode, then go to
2699 successively smaller modes. */
2701 while (max_size > 1 && data->len > 0)
2703 enum machine_mode mode = widest_int_mode_for_size (max_size);
2705 if (mode == VOIDmode)
2706 break;
2708 icode = optab_handler (mov_optab, mode);
2709 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2710 store_by_pieces_2 (GEN_FCN (icode), mode, data);
2712 max_size = GET_MODE_SIZE (mode);
2715 /* The code above should have handled everything. */
2716 gcc_assert (!data->len);
2719 /* Subroutine of store_by_pieces_1. Store as many bytes as appropriate
2720 with move instructions for mode MODE. GENFUN is the gen_... function
2721 to make a move insn for that mode. DATA has all the other info. */
2723 static void
2724 store_by_pieces_2 (insn_gen_fn genfun, machine_mode mode,
2725 struct store_by_pieces_d *data)
2727 unsigned int size = GET_MODE_SIZE (mode);
2728 rtx to1, cst;
2730 while (data->len >= size)
2732 if (data->reverse)
2733 data->offset -= size;
2735 if (data->autinc_to)
2736 to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2737 data->offset);
2738 else
2739 to1 = adjust_address (data->to, mode, data->offset);
2741 if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2742 emit_insn (gen_add2_insn (data->to_addr,
2743 gen_int_mode (-(HOST_WIDE_INT) size,
2744 GET_MODE (data->to_addr))));
2746 cst = (*data->constfun) (data->constfundata, data->offset, mode);
2747 emit_insn ((*genfun) (to1, cst));
2749 if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2750 emit_insn (gen_add2_insn (data->to_addr,
2751 gen_int_mode (size,
2752 GET_MODE (data->to_addr))));
2754 if (! data->reverse)
2755 data->offset += size;
2757 data->len -= size;
2761 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2762 its length in bytes. */
2765 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2766 unsigned int expected_align, HOST_WIDE_INT expected_size,
2767 unsigned HOST_WIDE_INT min_size,
2768 unsigned HOST_WIDE_INT max_size,
2769 unsigned HOST_WIDE_INT probable_max_size)
2771 enum machine_mode mode = GET_MODE (object);
2772 unsigned int align;
2774 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2776 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2777 just move a zero. Otherwise, do this a piece at a time. */
2778 if (mode != BLKmode
2779 && CONST_INT_P (size)
2780 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2782 rtx zero = CONST0_RTX (mode);
2783 if (zero != NULL)
2785 emit_move_insn (object, zero);
2786 return NULL;
2789 if (COMPLEX_MODE_P (mode))
2791 zero = CONST0_RTX (GET_MODE_INNER (mode));
2792 if (zero != NULL)
2794 write_complex_part (object, zero, 0);
2795 write_complex_part (object, zero, 1);
2796 return NULL;
2801 if (size == const0_rtx)
2802 return NULL;
2804 align = MEM_ALIGN (object);
2806 if (CONST_INT_P (size)
2807 && CLEAR_BY_PIECES_P (INTVAL (size), align))
2808 clear_by_pieces (object, INTVAL (size), align);
2809 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2810 expected_align, expected_size,
2811 min_size, max_size, probable_max_size))
2813 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2814 return set_storage_via_libcall (object, size, const0_rtx,
2815 method == BLOCK_OP_TAILCALL);
2816 else
2817 gcc_unreachable ();
2819 return NULL;
2823 clear_storage (rtx object, rtx size, enum block_op_methods method)
2825 unsigned HOST_WIDE_INT max, min = 0;
2826 if (GET_CODE (size) == CONST_INT)
2827 min = max = UINTVAL (size);
2828 else
2829 max = GET_MODE_MASK (GET_MODE (size));
2830 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2834 /* A subroutine of clear_storage. Expand a call to memset.
2835 Return the return value of memset, 0 otherwise. */
2838 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2840 tree call_expr, fn, object_tree, size_tree, val_tree;
2841 enum machine_mode size_mode;
2842 rtx retval;
2844 /* Emit code to copy OBJECT and SIZE into new pseudos. We can then
2845 place those into new pseudos into a VAR_DECL and use them later. */
2847 object = copy_addr_to_reg (XEXP (object, 0));
2849 size_mode = TYPE_MODE (sizetype);
2850 size = convert_to_mode (size_mode, size, 1);
2851 size = copy_to_mode_reg (size_mode, size);
2853 /* It is incorrect to use the libcall calling conventions to call
2854 memset in this context. This could be a user call to memset and
2855 the user may wish to examine the return value from memset. For
2856 targets where libcalls and normal calls have different conventions
2857 for returning pointers, we could end up generating incorrect code. */
2859 object_tree = make_tree (ptr_type_node, object);
2860 if (!CONST_INT_P (val))
2861 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2862 size_tree = make_tree (sizetype, size);
2863 val_tree = make_tree (integer_type_node, val);
2865 fn = clear_storage_libcall_fn (true);
2866 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2867 CALL_EXPR_TAILCALL (call_expr) = tailcall;
2869 retval = expand_normal (call_expr);
2871 return retval;
2874 /* A subroutine of set_storage_via_libcall. Create the tree node
2875 for the function we use for block clears. */
2877 tree block_clear_fn;
2879 void
2880 init_block_clear_fn (const char *asmspec)
2882 if (!block_clear_fn)
2884 tree fn, args;
2886 fn = get_identifier ("memset");
2887 args = build_function_type_list (ptr_type_node, ptr_type_node,
2888 integer_type_node, sizetype,
2889 NULL_TREE);
2891 fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2892 DECL_EXTERNAL (fn) = 1;
2893 TREE_PUBLIC (fn) = 1;
2894 DECL_ARTIFICIAL (fn) = 1;
2895 TREE_NOTHROW (fn) = 1;
2896 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2897 DECL_VISIBILITY_SPECIFIED (fn) = 1;
2899 block_clear_fn = fn;
2902 if (asmspec)
2903 set_user_assembler_name (block_clear_fn, asmspec);
2906 static tree
2907 clear_storage_libcall_fn (int for_call)
2909 static bool emitted_extern;
2911 if (!block_clear_fn)
2912 init_block_clear_fn (NULL);
2914 if (for_call && !emitted_extern)
2916 emitted_extern = true;
2917 make_decl_rtl (block_clear_fn);
2920 return block_clear_fn;
2923 /* Expand a setmem pattern; return true if successful. */
2925 bool
2926 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2927 unsigned int expected_align, HOST_WIDE_INT expected_size,
2928 unsigned HOST_WIDE_INT min_size,
2929 unsigned HOST_WIDE_INT max_size,
2930 unsigned HOST_WIDE_INT probable_max_size)
2932 /* Try the most limited insn first, because there's no point
2933 including more than one in the machine description unless
2934 the more limited one has some advantage. */
2936 enum machine_mode mode;
2938 if (expected_align < align)
2939 expected_align = align;
2940 if (expected_size != -1)
2942 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
2943 expected_size = max_size;
2944 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
2945 expected_size = min_size;
2948 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2949 mode = GET_MODE_WIDER_MODE (mode))
2951 enum insn_code code = direct_optab_handler (setmem_optab, mode);
2953 if (code != CODE_FOR_nothing
2954 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
2955 here because if SIZE is less than the mode mask, as it is
2956 returned by the macro, it will definitely be less than the
2957 actual mode mask. Since SIZE is within the Pmode address
2958 space, we limit MODE to Pmode. */
2959 && ((CONST_INT_P (size)
2960 && ((unsigned HOST_WIDE_INT) INTVAL (size)
2961 <= (GET_MODE_MASK (mode) >> 1)))
2962 || max_size <= (GET_MODE_MASK (mode) >> 1)
2963 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
2965 struct expand_operand ops[9];
2966 unsigned int nops;
2968 nops = insn_data[(int) code].n_generator_args;
2969 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
2971 create_fixed_operand (&ops[0], object);
2972 /* The check above guarantees that this size conversion is valid. */
2973 create_convert_operand_to (&ops[1], size, mode, true);
2974 create_convert_operand_from (&ops[2], val, byte_mode, true);
2975 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2976 if (nops >= 6)
2978 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2979 create_integer_operand (&ops[5], expected_size);
2981 if (nops >= 8)
2983 create_integer_operand (&ops[6], min_size);
2984 /* If we can not represent the maximal size,
2985 make parameter NULL. */
2986 if ((HOST_WIDE_INT) max_size != -1)
2987 create_integer_operand (&ops[7], max_size);
2988 else
2989 create_fixed_operand (&ops[7], NULL);
2991 if (nops == 9)
2993 /* If we can not represent the maximal size,
2994 make parameter NULL. */
2995 if ((HOST_WIDE_INT) probable_max_size != -1)
2996 create_integer_operand (&ops[8], probable_max_size);
2997 else
2998 create_fixed_operand (&ops[8], NULL);
3000 if (maybe_expand_insn (code, nops, ops))
3001 return true;
3005 return false;
3009 /* Write to one of the components of the complex value CPLX. Write VAL to
3010 the real part if IMAG_P is false, and the imaginary part if its true. */
3012 static void
3013 write_complex_part (rtx cplx, rtx val, bool imag_p)
3015 enum machine_mode cmode;
3016 enum machine_mode imode;
3017 unsigned ibitsize;
3019 if (GET_CODE (cplx) == CONCAT)
3021 emit_move_insn (XEXP (cplx, imag_p), val);
3022 return;
3025 cmode = GET_MODE (cplx);
3026 imode = GET_MODE_INNER (cmode);
3027 ibitsize = GET_MODE_BITSIZE (imode);
3029 /* For MEMs simplify_gen_subreg may generate an invalid new address
3030 because, e.g., the original address is considered mode-dependent
3031 by the target, which restricts simplify_subreg from invoking
3032 adjust_address_nv. Instead of preparing fallback support for an
3033 invalid address, we call adjust_address_nv directly. */
3034 if (MEM_P (cplx))
3036 emit_move_insn (adjust_address_nv (cplx, imode,
3037 imag_p ? GET_MODE_SIZE (imode) : 0),
3038 val);
3039 return;
3042 /* If the sub-object is at least word sized, then we know that subregging
3043 will work. This special case is important, since store_bit_field
3044 wants to operate on integer modes, and there's rarely an OImode to
3045 correspond to TCmode. */
3046 if (ibitsize >= BITS_PER_WORD
3047 /* For hard regs we have exact predicates. Assume we can split
3048 the original object if it spans an even number of hard regs.
3049 This special case is important for SCmode on 64-bit platforms
3050 where the natural size of floating-point regs is 32-bit. */
3051 || (REG_P (cplx)
3052 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3053 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3055 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3056 imag_p ? GET_MODE_SIZE (imode) : 0);
3057 if (part)
3059 emit_move_insn (part, val);
3060 return;
3062 else
3063 /* simplify_gen_subreg may fail for sub-word MEMs. */
3064 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3067 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
3070 /* Extract one of the components of the complex value CPLX. Extract the
3071 real part if IMAG_P is false, and the imaginary part if it's true. */
3073 static rtx
3074 read_complex_part (rtx cplx, bool imag_p)
3076 enum machine_mode cmode, imode;
3077 unsigned ibitsize;
3079 if (GET_CODE (cplx) == CONCAT)
3080 return XEXP (cplx, imag_p);
3082 cmode = GET_MODE (cplx);
3083 imode = GET_MODE_INNER (cmode);
3084 ibitsize = GET_MODE_BITSIZE (imode);
3086 /* Special case reads from complex constants that got spilled to memory. */
3087 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3089 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3090 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3092 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3093 if (CONSTANT_CLASS_P (part))
3094 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3098 /* For MEMs simplify_gen_subreg may generate an invalid new address
3099 because, e.g., the original address is considered mode-dependent
3100 by the target, which restricts simplify_subreg from invoking
3101 adjust_address_nv. Instead of preparing fallback support for an
3102 invalid address, we call adjust_address_nv directly. */
3103 if (MEM_P (cplx))
3104 return adjust_address_nv (cplx, imode,
3105 imag_p ? GET_MODE_SIZE (imode) : 0);
3107 /* If the sub-object is at least word sized, then we know that subregging
3108 will work. This special case is important, since extract_bit_field
3109 wants to operate on integer modes, and there's rarely an OImode to
3110 correspond to TCmode. */
3111 if (ibitsize >= BITS_PER_WORD
3112 /* For hard regs we have exact predicates. Assume we can split
3113 the original object if it spans an even number of hard regs.
3114 This special case is important for SCmode on 64-bit platforms
3115 where the natural size of floating-point regs is 32-bit. */
3116 || (REG_P (cplx)
3117 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3118 && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
3120 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3121 imag_p ? GET_MODE_SIZE (imode) : 0);
3122 if (ret)
3123 return ret;
3124 else
3125 /* simplify_gen_subreg may fail for sub-word MEMs. */
3126 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3129 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3130 true, NULL_RTX, imode, imode);
3133 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3134 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3135 represented in NEW_MODE. If FORCE is true, this will never happen, as
3136 we'll force-create a SUBREG if needed. */
3138 static rtx
3139 emit_move_change_mode (enum machine_mode new_mode,
3140 enum machine_mode old_mode, rtx x, bool force)
3142 rtx ret;
3144 if (push_operand (x, GET_MODE (x)))
3146 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3147 MEM_COPY_ATTRIBUTES (ret, x);
3149 else if (MEM_P (x))
3151 /* We don't have to worry about changing the address since the
3152 size in bytes is supposed to be the same. */
3153 if (reload_in_progress)
3155 /* Copy the MEM to change the mode and move any
3156 substitutions from the old MEM to the new one. */
3157 ret = adjust_address_nv (x, new_mode, 0);
3158 copy_replacements (x, ret);
3160 else
3161 ret = adjust_address (x, new_mode, 0);
3163 else
3165 /* Note that we do want simplify_subreg's behavior of validating
3166 that the new mode is ok for a hard register. If we were to use
3167 simplify_gen_subreg, we would create the subreg, but would
3168 probably run into the target not being able to implement it. */
3169 /* Except, of course, when FORCE is true, when this is exactly what
3170 we want. Which is needed for CCmodes on some targets. */
3171 if (force)
3172 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3173 else
3174 ret = simplify_subreg (new_mode, x, old_mode, 0);
3177 return ret;
3180 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3181 an integer mode of the same size as MODE. Returns the instruction
3182 emitted, or NULL if such a move could not be generated. */
3184 static rtx_insn *
3185 emit_move_via_integer (enum machine_mode mode, rtx x, rtx y, bool force)
3187 enum machine_mode imode;
3188 enum insn_code code;
3190 /* There must exist a mode of the exact size we require. */
3191 imode = int_mode_for_mode (mode);
3192 if (imode == BLKmode)
3193 return NULL;
3195 /* The target must support moves in this mode. */
3196 code = optab_handler (mov_optab, imode);
3197 if (code == CODE_FOR_nothing)
3198 return NULL;
3200 x = emit_move_change_mode (imode, mode, x, force);
3201 if (x == NULL_RTX)
3202 return NULL;
3203 y = emit_move_change_mode (imode, mode, y, force);
3204 if (y == NULL_RTX)
3205 return NULL;
3206 return emit_insn (GEN_FCN (code) (x, y));
3209 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3210 Return an equivalent MEM that does not use an auto-increment. */
3213 emit_move_resolve_push (enum machine_mode mode, rtx x)
3215 enum rtx_code code = GET_CODE (XEXP (x, 0));
3216 HOST_WIDE_INT adjust;
3217 rtx temp;
3219 adjust = GET_MODE_SIZE (mode);
3220 #ifdef PUSH_ROUNDING
3221 adjust = PUSH_ROUNDING (adjust);
3222 #endif
3223 if (code == PRE_DEC || code == POST_DEC)
3224 adjust = -adjust;
3225 else if (code == PRE_MODIFY || code == POST_MODIFY)
3227 rtx expr = XEXP (XEXP (x, 0), 1);
3228 HOST_WIDE_INT val;
3230 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3231 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3232 val = INTVAL (XEXP (expr, 1));
3233 if (GET_CODE (expr) == MINUS)
3234 val = -val;
3235 gcc_assert (adjust == val || adjust == -val);
3236 adjust = val;
3239 /* Do not use anti_adjust_stack, since we don't want to update
3240 stack_pointer_delta. */
3241 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3242 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3243 0, OPTAB_LIB_WIDEN);
3244 if (temp != stack_pointer_rtx)
3245 emit_move_insn (stack_pointer_rtx, temp);
3247 switch (code)
3249 case PRE_INC:
3250 case PRE_DEC:
3251 case PRE_MODIFY:
3252 temp = stack_pointer_rtx;
3253 break;
3254 case POST_INC:
3255 case POST_DEC:
3256 case POST_MODIFY:
3257 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3258 break;
3259 default:
3260 gcc_unreachable ();
3263 return replace_equiv_address (x, temp);
3266 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3267 X is known to satisfy push_operand, and MODE is known to be complex.
3268 Returns the last instruction emitted. */
3270 rtx_insn *
3271 emit_move_complex_push (enum machine_mode mode, rtx x, rtx y)
3273 enum machine_mode submode = GET_MODE_INNER (mode);
3274 bool imag_first;
3276 #ifdef PUSH_ROUNDING
3277 unsigned int submodesize = GET_MODE_SIZE (submode);
3279 /* In case we output to the stack, but the size is smaller than the
3280 machine can push exactly, we need to use move instructions. */
3281 if (PUSH_ROUNDING (submodesize) != submodesize)
3283 x = emit_move_resolve_push (mode, x);
3284 return emit_move_insn (x, y);
3286 #endif
3288 /* Note that the real part always precedes the imag part in memory
3289 regardless of machine's endianness. */
3290 switch (GET_CODE (XEXP (x, 0)))
3292 case PRE_DEC:
3293 case POST_DEC:
3294 imag_first = true;
3295 break;
3296 case PRE_INC:
3297 case POST_INC:
3298 imag_first = false;
3299 break;
3300 default:
3301 gcc_unreachable ();
3304 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3305 read_complex_part (y, imag_first));
3306 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3307 read_complex_part (y, !imag_first));
3310 /* A subroutine of emit_move_complex. Perform the move from Y to X
3311 via two moves of the parts. Returns the last instruction emitted. */
3313 rtx_insn *
3314 emit_move_complex_parts (rtx x, rtx y)
3316 /* Show the output dies here. This is necessary for SUBREGs
3317 of pseudos since we cannot track their lifetimes correctly;
3318 hard regs shouldn't appear here except as return values. */
3319 if (!reload_completed && !reload_in_progress
3320 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3321 emit_clobber (x);
3323 write_complex_part (x, read_complex_part (y, false), false);
3324 write_complex_part (x, read_complex_part (y, true), true);
3326 return get_last_insn ();
3329 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3330 MODE is known to be complex. Returns the last instruction emitted. */
3332 static rtx_insn *
3333 emit_move_complex (enum machine_mode mode, rtx x, rtx y)
3335 bool try_int;
3337 /* Need to take special care for pushes, to maintain proper ordering
3338 of the data, and possibly extra padding. */
3339 if (push_operand (x, mode))
3340 return emit_move_complex_push (mode, x, y);
3342 /* See if we can coerce the target into moving both values at once, except
3343 for floating point where we favor moving as parts if this is easy. */
3344 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3345 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3346 && !(REG_P (x)
3347 && HARD_REGISTER_P (x)
3348 && hard_regno_nregs[REGNO (x)][mode] == 1)
3349 && !(REG_P (y)
3350 && HARD_REGISTER_P (y)
3351 && hard_regno_nregs[REGNO (y)][mode] == 1))
3352 try_int = false;
3353 /* Not possible if the values are inherently not adjacent. */
3354 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3355 try_int = false;
3356 /* Is possible if both are registers (or subregs of registers). */
3357 else if (register_operand (x, mode) && register_operand (y, mode))
3358 try_int = true;
3359 /* If one of the operands is a memory, and alignment constraints
3360 are friendly enough, we may be able to do combined memory operations.
3361 We do not attempt this if Y is a constant because that combination is
3362 usually better with the by-parts thing below. */
3363 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3364 && (!STRICT_ALIGNMENT
3365 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3366 try_int = true;
3367 else
3368 try_int = false;
3370 if (try_int)
3372 rtx_insn *ret;
3374 /* For memory to memory moves, optimal behavior can be had with the
3375 existing block move logic. */
3376 if (MEM_P (x) && MEM_P (y))
3378 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3379 BLOCK_OP_NO_LIBCALL);
3380 return get_last_insn ();
3383 ret = emit_move_via_integer (mode, x, y, true);
3384 if (ret)
3385 return ret;
3388 return emit_move_complex_parts (x, y);
3391 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3392 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3394 static rtx_insn *
3395 emit_move_ccmode (enum machine_mode mode, rtx x, rtx y)
3397 rtx_insn *ret;
3399 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3400 if (mode != CCmode)
3402 enum insn_code code = optab_handler (mov_optab, CCmode);
3403 if (code != CODE_FOR_nothing)
3405 x = emit_move_change_mode (CCmode, mode, x, true);
3406 y = emit_move_change_mode (CCmode, mode, y, true);
3407 return emit_insn (GEN_FCN (code) (x, y));
3411 /* Otherwise, find the MODE_INT mode of the same width. */
3412 ret = emit_move_via_integer (mode, x, y, false);
3413 gcc_assert (ret != NULL);
3414 return ret;
3417 /* Return true if word I of OP lies entirely in the
3418 undefined bits of a paradoxical subreg. */
3420 static bool
3421 undefined_operand_subword_p (const_rtx op, int i)
3423 enum machine_mode innermode, innermostmode;
3424 int offset;
3425 if (GET_CODE (op) != SUBREG)
3426 return false;
3427 innermode = GET_MODE (op);
3428 innermostmode = GET_MODE (SUBREG_REG (op));
3429 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3430 /* The SUBREG_BYTE represents offset, as if the value were stored in
3431 memory, except for a paradoxical subreg where we define
3432 SUBREG_BYTE to be 0; undo this exception as in
3433 simplify_subreg. */
3434 if (SUBREG_BYTE (op) == 0
3435 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3437 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3438 if (WORDS_BIG_ENDIAN)
3439 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3440 if (BYTES_BIG_ENDIAN)
3441 offset += difference % UNITS_PER_WORD;
3443 if (offset >= GET_MODE_SIZE (innermostmode)
3444 || offset <= -GET_MODE_SIZE (word_mode))
3445 return true;
3446 return false;
3449 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3450 MODE is any multi-word or full-word mode that lacks a move_insn
3451 pattern. Note that you will get better code if you define such
3452 patterns, even if they must turn into multiple assembler instructions. */
3454 static rtx_insn *
3455 emit_move_multi_word (enum machine_mode mode, rtx x, rtx y)
3457 rtx_insn *last_insn = 0;
3458 rtx_insn *seq;
3459 rtx inner;
3460 bool need_clobber;
3461 int i;
3463 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3465 /* If X is a push on the stack, do the push now and replace
3466 X with a reference to the stack pointer. */
3467 if (push_operand (x, mode))
3468 x = emit_move_resolve_push (mode, x);
3470 /* If we are in reload, see if either operand is a MEM whose address
3471 is scheduled for replacement. */
3472 if (reload_in_progress && MEM_P (x)
3473 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3474 x = replace_equiv_address_nv (x, inner);
3475 if (reload_in_progress && MEM_P (y)
3476 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3477 y = replace_equiv_address_nv (y, inner);
3479 start_sequence ();
3481 need_clobber = false;
3482 for (i = 0;
3483 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3484 i++)
3486 rtx xpart = operand_subword (x, i, 1, mode);
3487 rtx ypart;
3489 /* Do not generate code for a move if it would come entirely
3490 from the undefined bits of a paradoxical subreg. */
3491 if (undefined_operand_subword_p (y, i))
3492 continue;
3494 ypart = operand_subword (y, i, 1, mode);
3496 /* If we can't get a part of Y, put Y into memory if it is a
3497 constant. Otherwise, force it into a register. Then we must
3498 be able to get a part of Y. */
3499 if (ypart == 0 && CONSTANT_P (y))
3501 y = use_anchored_address (force_const_mem (mode, y));
3502 ypart = operand_subword (y, i, 1, mode);
3504 else if (ypart == 0)
3505 ypart = operand_subword_force (y, i, mode);
3507 gcc_assert (xpart && ypart);
3509 need_clobber |= (GET_CODE (xpart) == SUBREG);
3511 last_insn = emit_move_insn (xpart, ypart);
3514 seq = get_insns ();
3515 end_sequence ();
3517 /* Show the output dies here. This is necessary for SUBREGs
3518 of pseudos since we cannot track their lifetimes correctly;
3519 hard regs shouldn't appear here except as return values.
3520 We never want to emit such a clobber after reload. */
3521 if (x != y
3522 && ! (reload_in_progress || reload_completed)
3523 && need_clobber != 0)
3524 emit_clobber (x);
3526 emit_insn (seq);
3528 return last_insn;
3531 /* Low level part of emit_move_insn.
3532 Called just like emit_move_insn, but assumes X and Y
3533 are basically valid. */
3535 rtx_insn *
3536 emit_move_insn_1 (rtx x, rtx y)
3538 enum machine_mode mode = GET_MODE (x);
3539 enum insn_code code;
3541 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3543 code = optab_handler (mov_optab, mode);
3544 if (code != CODE_FOR_nothing)
3545 return emit_insn (GEN_FCN (code) (x, y));
3547 /* Expand complex moves by moving real part and imag part. */
3548 if (COMPLEX_MODE_P (mode))
3549 return emit_move_complex (mode, x, y);
3551 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3552 || ALL_FIXED_POINT_MODE_P (mode))
3554 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3556 /* If we can't find an integer mode, use multi words. */
3557 if (result)
3558 return result;
3559 else
3560 return emit_move_multi_word (mode, x, y);
3563 if (GET_MODE_CLASS (mode) == MODE_CC)
3564 return emit_move_ccmode (mode, x, y);
3566 /* Try using a move pattern for the corresponding integer mode. This is
3567 only safe when simplify_subreg can convert MODE constants into integer
3568 constants. At present, it can only do this reliably if the value
3569 fits within a HOST_WIDE_INT. */
3570 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3572 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3574 if (ret)
3576 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3577 return ret;
3581 return emit_move_multi_word (mode, x, y);
3584 /* Generate code to copy Y into X.
3585 Both Y and X must have the same mode, except that
3586 Y can be a constant with VOIDmode.
3587 This mode cannot be BLKmode; use emit_block_move for that.
3589 Return the last instruction emitted. */
3591 rtx_insn *
3592 emit_move_insn (rtx x, rtx y)
3594 enum machine_mode mode = GET_MODE (x);
3595 rtx y_cst = NULL_RTX;
3596 rtx_insn *last_insn;
3597 rtx set;
3599 gcc_assert (mode != BLKmode
3600 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3602 if (CONSTANT_P (y))
3604 if (optimize
3605 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3606 && (last_insn = compress_float_constant (x, y)))
3607 return last_insn;
3609 y_cst = y;
3611 if (!targetm.legitimate_constant_p (mode, y))
3613 y = force_const_mem (mode, y);
3615 /* If the target's cannot_force_const_mem prevented the spill,
3616 assume that the target's move expanders will also take care
3617 of the non-legitimate constant. */
3618 if (!y)
3619 y = y_cst;
3620 else
3621 y = use_anchored_address (y);
3625 /* If X or Y are memory references, verify that their addresses are valid
3626 for the machine. */
3627 if (MEM_P (x)
3628 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3629 MEM_ADDR_SPACE (x))
3630 && ! push_operand (x, GET_MODE (x))))
3631 x = validize_mem (x);
3633 if (MEM_P (y)
3634 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3635 MEM_ADDR_SPACE (y)))
3636 y = validize_mem (y);
3638 gcc_assert (mode != BLKmode);
3640 last_insn = emit_move_insn_1 (x, y);
3642 if (y_cst && REG_P (x)
3643 && (set = single_set (last_insn)) != NULL_RTX
3644 && SET_DEST (set) == x
3645 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3646 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3648 return last_insn;
3651 /* If Y is representable exactly in a narrower mode, and the target can
3652 perform the extension directly from constant or memory, then emit the
3653 move as an extension. */
3655 static rtx_insn *
3656 compress_float_constant (rtx x, rtx y)
3658 enum machine_mode dstmode = GET_MODE (x);
3659 enum machine_mode orig_srcmode = GET_MODE (y);
3660 enum machine_mode srcmode;
3661 REAL_VALUE_TYPE r;
3662 int oldcost, newcost;
3663 bool speed = optimize_insn_for_speed_p ();
3665 REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3667 if (targetm.legitimate_constant_p (dstmode, y))
3668 oldcost = set_src_cost (y, speed);
3669 else
3670 oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3672 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3673 srcmode != orig_srcmode;
3674 srcmode = GET_MODE_WIDER_MODE (srcmode))
3676 enum insn_code ic;
3677 rtx trunc_y;
3678 rtx_insn *last_insn;
3680 /* Skip if the target can't extend this way. */
3681 ic = can_extend_p (dstmode, srcmode, 0);
3682 if (ic == CODE_FOR_nothing)
3683 continue;
3685 /* Skip if the narrowed value isn't exact. */
3686 if (! exact_real_truncate (srcmode, &r))
3687 continue;
3689 trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3691 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3693 /* Skip if the target needs extra instructions to perform
3694 the extension. */
3695 if (!insn_operand_matches (ic, 1, trunc_y))
3696 continue;
3697 /* This is valid, but may not be cheaper than the original. */
3698 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3699 speed);
3700 if (oldcost < newcost)
3701 continue;
3703 else if (float_extend_from_mem[dstmode][srcmode])
3705 trunc_y = force_const_mem (srcmode, trunc_y);
3706 /* This is valid, but may not be cheaper than the original. */
3707 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3708 speed);
3709 if (oldcost < newcost)
3710 continue;
3711 trunc_y = validize_mem (trunc_y);
3713 else
3714 continue;
3716 /* For CSE's benefit, force the compressed constant pool entry
3717 into a new pseudo. This constant may be used in different modes,
3718 and if not, combine will put things back together for us. */
3719 trunc_y = force_reg (srcmode, trunc_y);
3721 /* If x is a hard register, perform the extension into a pseudo,
3722 so that e.g. stack realignment code is aware of it. */
3723 rtx target = x;
3724 if (REG_P (x) && HARD_REGISTER_P (x))
3725 target = gen_reg_rtx (dstmode);
3727 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3728 last_insn = get_last_insn ();
3730 if (REG_P (target))
3731 set_unique_reg_note (last_insn, REG_EQUAL, y);
3733 if (target != x)
3734 return emit_move_insn (x, target);
3735 return last_insn;
3738 return NULL;
3741 /* Pushing data onto the stack. */
3743 /* Push a block of length SIZE (perhaps variable)
3744 and return an rtx to address the beginning of the block.
3745 The value may be virtual_outgoing_args_rtx.
3747 EXTRA is the number of bytes of padding to push in addition to SIZE.
3748 BELOW nonzero means this padding comes at low addresses;
3749 otherwise, the padding comes at high addresses. */
3752 push_block (rtx size, int extra, int below)
3754 rtx temp;
3756 size = convert_modes (Pmode, ptr_mode, size, 1);
3757 if (CONSTANT_P (size))
3758 anti_adjust_stack (plus_constant (Pmode, size, extra));
3759 else if (REG_P (size) && extra == 0)
3760 anti_adjust_stack (size);
3761 else
3763 temp = copy_to_mode_reg (Pmode, size);
3764 if (extra != 0)
3765 temp = expand_binop (Pmode, add_optab, temp,
3766 gen_int_mode (extra, Pmode),
3767 temp, 0, OPTAB_LIB_WIDEN);
3768 anti_adjust_stack (temp);
3771 #ifndef STACK_GROWS_DOWNWARD
3772 if (0)
3773 #else
3774 if (1)
3775 #endif
3777 temp = virtual_outgoing_args_rtx;
3778 if (extra != 0 && below)
3779 temp = plus_constant (Pmode, temp, extra);
3781 else
3783 if (CONST_INT_P (size))
3784 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3785 -INTVAL (size) - (below ? 0 : extra));
3786 else if (extra != 0 && !below)
3787 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3788 negate_rtx (Pmode, plus_constant (Pmode, size,
3789 extra)));
3790 else
3791 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3792 negate_rtx (Pmode, size));
3795 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3798 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3800 static rtx
3801 mem_autoinc_base (rtx mem)
3803 if (MEM_P (mem))
3805 rtx addr = XEXP (mem, 0);
3806 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3807 return XEXP (addr, 0);
3809 return NULL;
3812 /* A utility routine used here, in reload, and in try_split. The insns
3813 after PREV up to and including LAST are known to adjust the stack,
3814 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3815 placing notes as appropriate. PREV may be NULL, indicating the
3816 entire insn sequence prior to LAST should be scanned.
3818 The set of allowed stack pointer modifications is small:
3819 (1) One or more auto-inc style memory references (aka pushes),
3820 (2) One or more addition/subtraction with the SP as destination,
3821 (3) A single move insn with the SP as destination,
3822 (4) A call_pop insn,
3823 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3825 Insns in the sequence that do not modify the SP are ignored,
3826 except for noreturn calls.
3828 The return value is the amount of adjustment that can be trivially
3829 verified, via immediate operand or auto-inc. If the adjustment
3830 cannot be trivially extracted, the return value is INT_MIN. */
3832 HOST_WIDE_INT
3833 find_args_size_adjust (rtx insn)
3835 rtx dest, set, pat;
3836 int i;
3838 pat = PATTERN (insn);
3839 set = NULL;
3841 /* Look for a call_pop pattern. */
3842 if (CALL_P (insn))
3844 /* We have to allow non-call_pop patterns for the case
3845 of emit_single_push_insn of a TLS address. */
3846 if (GET_CODE (pat) != PARALLEL)
3847 return 0;
3849 /* All call_pop have a stack pointer adjust in the parallel.
3850 The call itself is always first, and the stack adjust is
3851 usually last, so search from the end. */
3852 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3854 set = XVECEXP (pat, 0, i);
3855 if (GET_CODE (set) != SET)
3856 continue;
3857 dest = SET_DEST (set);
3858 if (dest == stack_pointer_rtx)
3859 break;
3861 /* We'd better have found the stack pointer adjust. */
3862 if (i == 0)
3863 return 0;
3864 /* Fall through to process the extracted SET and DEST
3865 as if it was a standalone insn. */
3867 else if (GET_CODE (pat) == SET)
3868 set = pat;
3869 else if ((set = single_set (insn)) != NULL)
3871 else if (GET_CODE (pat) == PARALLEL)
3873 /* ??? Some older ports use a parallel with a stack adjust
3874 and a store for a PUSH_ROUNDING pattern, rather than a
3875 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3876 /* ??? See h8300 and m68k, pushqi1. */
3877 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3879 set = XVECEXP (pat, 0, i);
3880 if (GET_CODE (set) != SET)
3881 continue;
3882 dest = SET_DEST (set);
3883 if (dest == stack_pointer_rtx)
3884 break;
3886 /* We do not expect an auto-inc of the sp in the parallel. */
3887 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3888 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3889 != stack_pointer_rtx);
3891 if (i < 0)
3892 return 0;
3894 else
3895 return 0;
3897 dest = SET_DEST (set);
3899 /* Look for direct modifications of the stack pointer. */
3900 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3902 /* Look for a trivial adjustment, otherwise assume nothing. */
3903 /* Note that the SPU restore_stack_block pattern refers to
3904 the stack pointer in V4SImode. Consider that non-trivial. */
3905 if (SCALAR_INT_MODE_P (GET_MODE (dest))
3906 && GET_CODE (SET_SRC (set)) == PLUS
3907 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3908 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3909 return INTVAL (XEXP (SET_SRC (set), 1));
3910 /* ??? Reload can generate no-op moves, which will be cleaned
3911 up later. Recognize it and continue searching. */
3912 else if (rtx_equal_p (dest, SET_SRC (set)))
3913 return 0;
3914 else
3915 return HOST_WIDE_INT_MIN;
3917 else
3919 rtx mem, addr;
3921 /* Otherwise only think about autoinc patterns. */
3922 if (mem_autoinc_base (dest) == stack_pointer_rtx)
3924 mem = dest;
3925 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3926 != stack_pointer_rtx);
3928 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3929 mem = SET_SRC (set);
3930 else
3931 return 0;
3933 addr = XEXP (mem, 0);
3934 switch (GET_CODE (addr))
3936 case PRE_INC:
3937 case POST_INC:
3938 return GET_MODE_SIZE (GET_MODE (mem));
3939 case PRE_DEC:
3940 case POST_DEC:
3941 return -GET_MODE_SIZE (GET_MODE (mem));
3942 case PRE_MODIFY:
3943 case POST_MODIFY:
3944 addr = XEXP (addr, 1);
3945 gcc_assert (GET_CODE (addr) == PLUS);
3946 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3947 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3948 return INTVAL (XEXP (addr, 1));
3949 default:
3950 gcc_unreachable ();
3956 fixup_args_size_notes (rtx prev, rtx uncast_last, int end_args_size)
3958 rtx_insn *last = safe_as_a <rtx_insn *> (uncast_last);
3959 int args_size = end_args_size;
3960 bool saw_unknown = false;
3961 rtx_insn *insn;
3963 for (insn = last; insn != prev; insn = PREV_INSN (insn))
3965 HOST_WIDE_INT this_delta;
3967 if (!NONDEBUG_INSN_P (insn))
3968 continue;
3970 this_delta = find_args_size_adjust (insn);
3971 if (this_delta == 0)
3973 if (!CALL_P (insn)
3974 || ACCUMULATE_OUTGOING_ARGS
3975 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3976 continue;
3979 gcc_assert (!saw_unknown);
3980 if (this_delta == HOST_WIDE_INT_MIN)
3981 saw_unknown = true;
3983 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3984 #ifdef STACK_GROWS_DOWNWARD
3985 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
3986 #endif
3987 args_size -= this_delta;
3990 return saw_unknown ? INT_MIN : args_size;
3993 #ifdef PUSH_ROUNDING
3994 /* Emit single push insn. */
3996 static void
3997 emit_single_push_insn_1 (enum machine_mode mode, rtx x, tree type)
3999 rtx dest_addr;
4000 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4001 rtx dest;
4002 enum insn_code icode;
4004 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4005 /* If there is push pattern, use it. Otherwise try old way of throwing
4006 MEM representing push operation to move expander. */
4007 icode = optab_handler (push_optab, mode);
4008 if (icode != CODE_FOR_nothing)
4010 struct expand_operand ops[1];
4012 create_input_operand (&ops[0], x, mode);
4013 if (maybe_expand_insn (icode, 1, ops))
4014 return;
4016 if (GET_MODE_SIZE (mode) == rounded_size)
4017 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4018 /* If we are to pad downward, adjust the stack pointer first and
4019 then store X into the stack location using an offset. This is
4020 because emit_move_insn does not know how to pad; it does not have
4021 access to type. */
4022 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4024 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4025 HOST_WIDE_INT offset;
4027 emit_move_insn (stack_pointer_rtx,
4028 expand_binop (Pmode,
4029 #ifdef STACK_GROWS_DOWNWARD
4030 sub_optab,
4031 #else
4032 add_optab,
4033 #endif
4034 stack_pointer_rtx,
4035 gen_int_mode (rounded_size, Pmode),
4036 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4038 offset = (HOST_WIDE_INT) padding_size;
4039 #ifdef STACK_GROWS_DOWNWARD
4040 if (STACK_PUSH_CODE == POST_DEC)
4041 /* We have already decremented the stack pointer, so get the
4042 previous value. */
4043 offset += (HOST_WIDE_INT) rounded_size;
4044 #else
4045 if (STACK_PUSH_CODE == POST_INC)
4046 /* We have already incremented the stack pointer, so get the
4047 previous value. */
4048 offset -= (HOST_WIDE_INT) rounded_size;
4049 #endif
4050 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4051 gen_int_mode (offset, Pmode));
4053 else
4055 #ifdef STACK_GROWS_DOWNWARD
4056 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4057 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4058 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4059 Pmode));
4060 #else
4061 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4062 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4063 gen_int_mode (rounded_size, Pmode));
4064 #endif
4065 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4068 dest = gen_rtx_MEM (mode, dest_addr);
4070 if (type != 0)
4072 set_mem_attributes (dest, type, 1);
4074 if (cfun->tail_call_marked)
4075 /* Function incoming arguments may overlap with sibling call
4076 outgoing arguments and we cannot allow reordering of reads
4077 from function arguments with stores to outgoing arguments
4078 of sibling calls. */
4079 set_mem_alias_set (dest, 0);
4081 emit_move_insn (dest, x);
4084 /* Emit and annotate a single push insn. */
4086 static void
4087 emit_single_push_insn (enum machine_mode mode, rtx x, tree type)
4089 int delta, old_delta = stack_pointer_delta;
4090 rtx_insn *prev = get_last_insn ();
4091 rtx_insn *last;
4093 emit_single_push_insn_1 (mode, x, type);
4095 last = get_last_insn ();
4097 /* Notice the common case where we emitted exactly one insn. */
4098 if (PREV_INSN (last) == prev)
4100 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4101 return;
4104 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4105 gcc_assert (delta == INT_MIN || delta == old_delta);
4107 #endif
4109 /* Generate code to push X onto the stack, assuming it has mode MODE and
4110 type TYPE.
4111 MODE is redundant except when X is a CONST_INT (since they don't
4112 carry mode info).
4113 SIZE is an rtx for the size of data to be copied (in bytes),
4114 needed only if X is BLKmode.
4116 ALIGN (in bits) is maximum alignment we can assume.
4118 If PARTIAL and REG are both nonzero, then copy that many of the first
4119 bytes of X into registers starting with REG, and push the rest of X.
4120 The amount of space pushed is decreased by PARTIAL bytes.
4121 REG must be a hard register in this case.
4122 If REG is zero but PARTIAL is not, take any all others actions for an
4123 argument partially in registers, but do not actually load any
4124 registers.
4126 EXTRA is the amount in bytes of extra space to leave next to this arg.
4127 This is ignored if an argument block has already been allocated.
4129 On a machine that lacks real push insns, ARGS_ADDR is the address of
4130 the bottom of the argument block for this call. We use indexing off there
4131 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4132 argument block has not been preallocated.
4134 ARGS_SO_FAR is the size of args previously pushed for this call.
4136 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4137 for arguments passed in registers. If nonzero, it will be the number
4138 of bytes required. */
4140 void
4141 emit_push_insn (rtx x, enum machine_mode mode, tree type, rtx size,
4142 unsigned int align, int partial, rtx reg, int extra,
4143 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4144 rtx alignment_pad)
4146 rtx xinner;
4147 enum direction stack_direction
4148 #ifdef STACK_GROWS_DOWNWARD
4149 = downward;
4150 #else
4151 = upward;
4152 #endif
4154 /* Decide where to pad the argument: `downward' for below,
4155 `upward' for above, or `none' for don't pad it.
4156 Default is below for small data on big-endian machines; else above. */
4157 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4159 /* Invert direction if stack is post-decrement.
4160 FIXME: why? */
4161 if (STACK_PUSH_CODE == POST_DEC)
4162 if (where_pad != none)
4163 where_pad = (where_pad == downward ? upward : downward);
4165 xinner = x;
4167 if (mode == BLKmode
4168 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4170 /* Copy a block into the stack, entirely or partially. */
4172 rtx temp;
4173 int used;
4174 int offset;
4175 int skip;
4177 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4178 used = partial - offset;
4180 if (mode != BLKmode)
4182 /* A value is to be stored in an insufficiently aligned
4183 stack slot; copy via a suitably aligned slot if
4184 necessary. */
4185 size = GEN_INT (GET_MODE_SIZE (mode));
4186 if (!MEM_P (xinner))
4188 temp = assign_temp (type, 1, 1);
4189 emit_move_insn (temp, xinner);
4190 xinner = temp;
4194 gcc_assert (size);
4196 /* USED is now the # of bytes we need not copy to the stack
4197 because registers will take care of them. */
4199 if (partial != 0)
4200 xinner = adjust_address (xinner, BLKmode, used);
4202 /* If the partial register-part of the arg counts in its stack size,
4203 skip the part of stack space corresponding to the registers.
4204 Otherwise, start copying to the beginning of the stack space,
4205 by setting SKIP to 0. */
4206 skip = (reg_parm_stack_space == 0) ? 0 : used;
4208 #ifdef PUSH_ROUNDING
4209 /* Do it with several push insns if that doesn't take lots of insns
4210 and if there is no difficulty with push insns that skip bytes
4211 on the stack for alignment purposes. */
4212 if (args_addr == 0
4213 && PUSH_ARGS
4214 && CONST_INT_P (size)
4215 && skip == 0
4216 && MEM_ALIGN (xinner) >= align
4217 && (MOVE_BY_PIECES_P ((unsigned) INTVAL (size) - used, align))
4218 /* Here we avoid the case of a structure whose weak alignment
4219 forces many pushes of a small amount of data,
4220 and such small pushes do rounding that causes trouble. */
4221 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4222 || align >= BIGGEST_ALIGNMENT
4223 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4224 == (align / BITS_PER_UNIT)))
4225 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4227 /* Push padding now if padding above and stack grows down,
4228 or if padding below and stack grows up.
4229 But if space already allocated, this has already been done. */
4230 if (extra && args_addr == 0
4231 && where_pad != none && where_pad != stack_direction)
4232 anti_adjust_stack (GEN_INT (extra));
4234 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4236 else
4237 #endif /* PUSH_ROUNDING */
4239 rtx target;
4241 /* Otherwise make space on the stack and copy the data
4242 to the address of that space. */
4244 /* Deduct words put into registers from the size we must copy. */
4245 if (partial != 0)
4247 if (CONST_INT_P (size))
4248 size = GEN_INT (INTVAL (size) - used);
4249 else
4250 size = expand_binop (GET_MODE (size), sub_optab, size,
4251 gen_int_mode (used, GET_MODE (size)),
4252 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4255 /* Get the address of the stack space.
4256 In this case, we do not deal with EXTRA separately.
4257 A single stack adjust will do. */
4258 if (! args_addr)
4260 temp = push_block (size, extra, where_pad == downward);
4261 extra = 0;
4263 else if (CONST_INT_P (args_so_far))
4264 temp = memory_address (BLKmode,
4265 plus_constant (Pmode, args_addr,
4266 skip + INTVAL (args_so_far)));
4267 else
4268 temp = memory_address (BLKmode,
4269 plus_constant (Pmode,
4270 gen_rtx_PLUS (Pmode,
4271 args_addr,
4272 args_so_far),
4273 skip));
4275 if (!ACCUMULATE_OUTGOING_ARGS)
4277 /* If the source is referenced relative to the stack pointer,
4278 copy it to another register to stabilize it. We do not need
4279 to do this if we know that we won't be changing sp. */
4281 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4282 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4283 temp = copy_to_reg (temp);
4286 target = gen_rtx_MEM (BLKmode, temp);
4288 /* We do *not* set_mem_attributes here, because incoming arguments
4289 may overlap with sibling call outgoing arguments and we cannot
4290 allow reordering of reads from function arguments with stores
4291 to outgoing arguments of sibling calls. We do, however, want
4292 to record the alignment of the stack slot. */
4293 /* ALIGN may well be better aligned than TYPE, e.g. due to
4294 PARM_BOUNDARY. Assume the caller isn't lying. */
4295 set_mem_align (target, align);
4297 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4300 else if (partial > 0)
4302 /* Scalar partly in registers. */
4304 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4305 int i;
4306 int not_stack;
4307 /* # bytes of start of argument
4308 that we must make space for but need not store. */
4309 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4310 int args_offset = INTVAL (args_so_far);
4311 int skip;
4313 /* Push padding now if padding above and stack grows down,
4314 or if padding below and stack grows up.
4315 But if space already allocated, this has already been done. */
4316 if (extra && args_addr == 0
4317 && where_pad != none && where_pad != stack_direction)
4318 anti_adjust_stack (GEN_INT (extra));
4320 /* If we make space by pushing it, we might as well push
4321 the real data. Otherwise, we can leave OFFSET nonzero
4322 and leave the space uninitialized. */
4323 if (args_addr == 0)
4324 offset = 0;
4326 /* Now NOT_STACK gets the number of words that we don't need to
4327 allocate on the stack. Convert OFFSET to words too. */
4328 not_stack = (partial - offset) / UNITS_PER_WORD;
4329 offset /= UNITS_PER_WORD;
4331 /* If the partial register-part of the arg counts in its stack size,
4332 skip the part of stack space corresponding to the registers.
4333 Otherwise, start copying to the beginning of the stack space,
4334 by setting SKIP to 0. */
4335 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4337 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4338 x = validize_mem (force_const_mem (mode, x));
4340 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4341 SUBREGs of such registers are not allowed. */
4342 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4343 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4344 x = copy_to_reg (x);
4346 /* Loop over all the words allocated on the stack for this arg. */
4347 /* We can do it by words, because any scalar bigger than a word
4348 has a size a multiple of a word. */
4349 for (i = size - 1; i >= not_stack; i--)
4350 if (i >= not_stack + offset)
4351 emit_push_insn (operand_subword_force (x, i, mode),
4352 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4353 0, args_addr,
4354 GEN_INT (args_offset + ((i - not_stack + skip)
4355 * UNITS_PER_WORD)),
4356 reg_parm_stack_space, alignment_pad);
4358 else
4360 rtx addr;
4361 rtx dest;
4363 /* Push padding now if padding above and stack grows down,
4364 or if padding below and stack grows up.
4365 But if space already allocated, this has already been done. */
4366 if (extra && args_addr == 0
4367 && where_pad != none && where_pad != stack_direction)
4368 anti_adjust_stack (GEN_INT (extra));
4370 #ifdef PUSH_ROUNDING
4371 if (args_addr == 0 && PUSH_ARGS)
4372 emit_single_push_insn (mode, x, type);
4373 else
4374 #endif
4376 if (CONST_INT_P (args_so_far))
4377 addr
4378 = memory_address (mode,
4379 plus_constant (Pmode, args_addr,
4380 INTVAL (args_so_far)));
4381 else
4382 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4383 args_so_far));
4384 dest = gen_rtx_MEM (mode, addr);
4386 /* We do *not* set_mem_attributes here, because incoming arguments
4387 may overlap with sibling call outgoing arguments and we cannot
4388 allow reordering of reads from function arguments with stores
4389 to outgoing arguments of sibling calls. We do, however, want
4390 to record the alignment of the stack slot. */
4391 /* ALIGN may well be better aligned than TYPE, e.g. due to
4392 PARM_BOUNDARY. Assume the caller isn't lying. */
4393 set_mem_align (dest, align);
4395 emit_move_insn (dest, x);
4399 /* If part should go in registers, copy that part
4400 into the appropriate registers. Do this now, at the end,
4401 since mem-to-mem copies above may do function calls. */
4402 if (partial > 0 && reg != 0)
4404 /* Handle calls that pass values in multiple non-contiguous locations.
4405 The Irix 6 ABI has examples of this. */
4406 if (GET_CODE (reg) == PARALLEL)
4407 emit_group_load (reg, x, type, -1);
4408 else
4410 gcc_assert (partial % UNITS_PER_WORD == 0);
4411 move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4415 if (extra && args_addr == 0 && where_pad == stack_direction)
4416 anti_adjust_stack (GEN_INT (extra));
4418 if (alignment_pad && args_addr == 0)
4419 anti_adjust_stack (alignment_pad);
4422 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4423 operations. */
4425 static rtx
4426 get_subtarget (rtx x)
4428 return (optimize
4429 || x == 0
4430 /* Only registers can be subtargets. */
4431 || !REG_P (x)
4432 /* Don't use hard regs to avoid extending their life. */
4433 || REGNO (x) < FIRST_PSEUDO_REGISTER
4434 ? 0 : x);
4437 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4438 FIELD is a bitfield. Returns true if the optimization was successful,
4439 and there's nothing else to do. */
4441 static bool
4442 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4443 unsigned HOST_WIDE_INT bitpos,
4444 unsigned HOST_WIDE_INT bitregion_start,
4445 unsigned HOST_WIDE_INT bitregion_end,
4446 enum machine_mode mode1, rtx str_rtx,
4447 tree to, tree src)
4449 enum machine_mode str_mode = GET_MODE (str_rtx);
4450 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4451 tree op0, op1;
4452 rtx value, result;
4453 optab binop;
4454 gimple srcstmt;
4455 enum tree_code code;
4457 if (mode1 != VOIDmode
4458 || bitsize >= BITS_PER_WORD
4459 || str_bitsize > BITS_PER_WORD
4460 || TREE_SIDE_EFFECTS (to)
4461 || TREE_THIS_VOLATILE (to))
4462 return false;
4464 STRIP_NOPS (src);
4465 if (TREE_CODE (src) != SSA_NAME)
4466 return false;
4467 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4468 return false;
4470 srcstmt = get_gimple_for_ssa_name (src);
4471 if (!srcstmt
4472 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4473 return false;
4475 code = gimple_assign_rhs_code (srcstmt);
4477 op0 = gimple_assign_rhs1 (srcstmt);
4479 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4480 to find its initialization. Hopefully the initialization will
4481 be from a bitfield load. */
4482 if (TREE_CODE (op0) == SSA_NAME)
4484 gimple op0stmt = get_gimple_for_ssa_name (op0);
4486 /* We want to eventually have OP0 be the same as TO, which
4487 should be a bitfield. */
4488 if (!op0stmt
4489 || !is_gimple_assign (op0stmt)
4490 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4491 return false;
4492 op0 = gimple_assign_rhs1 (op0stmt);
4495 op1 = gimple_assign_rhs2 (srcstmt);
4497 if (!operand_equal_p (to, op0, 0))
4498 return false;
4500 if (MEM_P (str_rtx))
4502 unsigned HOST_WIDE_INT offset1;
4504 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4505 str_mode = word_mode;
4506 str_mode = get_best_mode (bitsize, bitpos,
4507 bitregion_start, bitregion_end,
4508 MEM_ALIGN (str_rtx), str_mode, 0);
4509 if (str_mode == VOIDmode)
4510 return false;
4511 str_bitsize = GET_MODE_BITSIZE (str_mode);
4513 offset1 = bitpos;
4514 bitpos %= str_bitsize;
4515 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4516 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4518 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4519 return false;
4521 /* If the bit field covers the whole REG/MEM, store_field
4522 will likely generate better code. */
4523 if (bitsize >= str_bitsize)
4524 return false;
4526 /* We can't handle fields split across multiple entities. */
4527 if (bitpos + bitsize > str_bitsize)
4528 return false;
4530 if (BYTES_BIG_ENDIAN)
4531 bitpos = str_bitsize - bitpos - bitsize;
4533 switch (code)
4535 case PLUS_EXPR:
4536 case MINUS_EXPR:
4537 /* For now, just optimize the case of the topmost bitfield
4538 where we don't need to do any masking and also
4539 1 bit bitfields where xor can be used.
4540 We might win by one instruction for the other bitfields
4541 too if insv/extv instructions aren't used, so that
4542 can be added later. */
4543 if (bitpos + bitsize != str_bitsize
4544 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4545 break;
4547 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4548 value = convert_modes (str_mode,
4549 TYPE_MODE (TREE_TYPE (op1)), value,
4550 TYPE_UNSIGNED (TREE_TYPE (op1)));
4552 /* We may be accessing data outside the field, which means
4553 we can alias adjacent data. */
4554 if (MEM_P (str_rtx))
4556 str_rtx = shallow_copy_rtx (str_rtx);
4557 set_mem_alias_set (str_rtx, 0);
4558 set_mem_expr (str_rtx, 0);
4561 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4562 if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4564 value = expand_and (str_mode, value, const1_rtx, NULL);
4565 binop = xor_optab;
4567 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4568 result = expand_binop (str_mode, binop, str_rtx,
4569 value, str_rtx, 1, OPTAB_WIDEN);
4570 if (result != str_rtx)
4571 emit_move_insn (str_rtx, result);
4572 return true;
4574 case BIT_IOR_EXPR:
4575 case BIT_XOR_EXPR:
4576 if (TREE_CODE (op1) != INTEGER_CST)
4577 break;
4578 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4579 value = convert_modes (str_mode,
4580 TYPE_MODE (TREE_TYPE (op1)), value,
4581 TYPE_UNSIGNED (TREE_TYPE (op1)));
4583 /* We may be accessing data outside the field, which means
4584 we can alias adjacent data. */
4585 if (MEM_P (str_rtx))
4587 str_rtx = shallow_copy_rtx (str_rtx);
4588 set_mem_alias_set (str_rtx, 0);
4589 set_mem_expr (str_rtx, 0);
4592 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4593 if (bitpos + bitsize != str_bitsize)
4595 rtx mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1,
4596 str_mode);
4597 value = expand_and (str_mode, value, mask, NULL_RTX);
4599 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4600 result = expand_binop (str_mode, binop, str_rtx,
4601 value, str_rtx, 1, OPTAB_WIDEN);
4602 if (result != str_rtx)
4603 emit_move_insn (str_rtx, result);
4604 return true;
4606 default:
4607 break;
4610 return false;
4613 /* In the C++ memory model, consecutive bit fields in a structure are
4614 considered one memory location.
4616 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4617 returns the bit range of consecutive bits in which this COMPONENT_REF
4618 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4619 and *OFFSET may be adjusted in the process.
4621 If the access does not need to be restricted, 0 is returned in both
4622 *BITSTART and *BITEND. */
4624 static void
4625 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4626 unsigned HOST_WIDE_INT *bitend,
4627 tree exp,
4628 HOST_WIDE_INT *bitpos,
4629 tree *offset)
4631 HOST_WIDE_INT bitoffset;
4632 tree field, repr;
4634 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4636 field = TREE_OPERAND (exp, 1);
4637 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4638 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4639 need to limit the range we can access. */
4640 if (!repr)
4642 *bitstart = *bitend = 0;
4643 return;
4646 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4647 part of a larger bit field, then the representative does not serve any
4648 useful purpose. This can occur in Ada. */
4649 if (handled_component_p (TREE_OPERAND (exp, 0)))
4651 enum machine_mode rmode;
4652 HOST_WIDE_INT rbitsize, rbitpos;
4653 tree roffset;
4654 int unsignedp;
4655 int volatilep = 0;
4656 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4657 &roffset, &rmode, &unsignedp, &volatilep, false);
4658 if ((rbitpos % BITS_PER_UNIT) != 0)
4660 *bitstart = *bitend = 0;
4661 return;
4665 /* Compute the adjustment to bitpos from the offset of the field
4666 relative to the representative. DECL_FIELD_OFFSET of field and
4667 repr are the same by construction if they are not constants,
4668 see finish_bitfield_layout. */
4669 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4670 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4671 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4672 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4673 else
4674 bitoffset = 0;
4675 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4676 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4678 /* If the adjustment is larger than bitpos, we would have a negative bit
4679 position for the lower bound and this may wreak havoc later. Adjust
4680 offset and bitpos to make the lower bound non-negative in that case. */
4681 if (bitoffset > *bitpos)
4683 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4684 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4686 *bitpos += adjust;
4687 if (*offset == NULL_TREE)
4688 *offset = size_int (-adjust / BITS_PER_UNIT);
4689 else
4690 *offset
4691 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4692 *bitstart = 0;
4694 else
4695 *bitstart = *bitpos - bitoffset;
4697 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4700 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4701 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4702 DECL_RTL was not set yet, return NORTL. */
4704 static inline bool
4705 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4707 if (TREE_CODE (addr) != ADDR_EXPR)
4708 return false;
4710 tree base = TREE_OPERAND (addr, 0);
4712 if (!DECL_P (base)
4713 || TREE_ADDRESSABLE (base)
4714 || DECL_MODE (base) == BLKmode)
4715 return false;
4717 if (!DECL_RTL_SET_P (base))
4718 return nortl;
4720 return (!MEM_P (DECL_RTL (base)));
4723 /* Returns true if the MEM_REF REF refers to an object that does not
4724 reside in memory and has non-BLKmode. */
4726 static inline bool
4727 mem_ref_refers_to_non_mem_p (tree ref)
4729 tree base = TREE_OPERAND (ref, 0);
4730 return addr_expr_of_non_mem_decl_p_1 (base, false);
4733 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4734 is true, try generating a nontemporal store. */
4736 void
4737 expand_assignment (tree to, tree from, bool nontemporal)
4739 rtx to_rtx = 0;
4740 rtx result;
4741 enum machine_mode mode;
4742 unsigned int align;
4743 enum insn_code icode;
4745 /* Don't crash if the lhs of the assignment was erroneous. */
4746 if (TREE_CODE (to) == ERROR_MARK)
4748 expand_normal (from);
4749 return;
4752 /* Optimize away no-op moves without side-effects. */
4753 if (operand_equal_p (to, from, 0))
4754 return;
4756 /* Handle misaligned stores. */
4757 mode = TYPE_MODE (TREE_TYPE (to));
4758 if ((TREE_CODE (to) == MEM_REF
4759 || TREE_CODE (to) == TARGET_MEM_REF)
4760 && mode != BLKmode
4761 && !mem_ref_refers_to_non_mem_p (to)
4762 && ((align = get_object_alignment (to))
4763 < GET_MODE_ALIGNMENT (mode))
4764 && (((icode = optab_handler (movmisalign_optab, mode))
4765 != CODE_FOR_nothing)
4766 || SLOW_UNALIGNED_ACCESS (mode, align)))
4768 rtx reg, mem;
4770 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4771 reg = force_not_mem (reg);
4772 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4774 if (icode != CODE_FOR_nothing)
4776 struct expand_operand ops[2];
4778 create_fixed_operand (&ops[0], mem);
4779 create_input_operand (&ops[1], reg, mode);
4780 /* The movmisalign<mode> pattern cannot fail, else the assignment
4781 would silently be omitted. */
4782 expand_insn (icode, 2, ops);
4784 else
4785 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg);
4786 return;
4789 /* Assignment of a structure component needs special treatment
4790 if the structure component's rtx is not simply a MEM.
4791 Assignment of an array element at a constant index, and assignment of
4792 an array element in an unaligned packed structure field, has the same
4793 problem. Same for (partially) storing into a non-memory object. */
4794 if (handled_component_p (to)
4795 || (TREE_CODE (to) == MEM_REF
4796 && mem_ref_refers_to_non_mem_p (to))
4797 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4799 enum machine_mode mode1;
4800 HOST_WIDE_INT bitsize, bitpos;
4801 unsigned HOST_WIDE_INT bitregion_start = 0;
4802 unsigned HOST_WIDE_INT bitregion_end = 0;
4803 tree offset;
4804 int unsignedp;
4805 int volatilep = 0;
4806 tree tem;
4808 push_temp_slots ();
4809 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4810 &unsignedp, &volatilep, true);
4812 /* Make sure bitpos is not negative, it can wreak havoc later. */
4813 if (bitpos < 0)
4815 gcc_assert (offset == NULL_TREE);
4816 offset = size_int (bitpos >> (BITS_PER_UNIT == 8
4817 ? 3 : exact_log2 (BITS_PER_UNIT)));
4818 bitpos &= BITS_PER_UNIT - 1;
4821 if (TREE_CODE (to) == COMPONENT_REF
4822 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4823 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4824 /* The C++ memory model naturally applies to byte-aligned fields.
4825 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
4826 BITSIZE are not byte-aligned, there is no need to limit the range
4827 we can access. This can occur with packed structures in Ada. */
4828 else if (bitsize > 0
4829 && bitsize % BITS_PER_UNIT == 0
4830 && bitpos % BITS_PER_UNIT == 0)
4832 bitregion_start = bitpos;
4833 bitregion_end = bitpos + bitsize - 1;
4836 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4838 /* If the field has a mode, we want to access it in the
4839 field's mode, not the computed mode.
4840 If a MEM has VOIDmode (external with incomplete type),
4841 use BLKmode for it instead. */
4842 if (MEM_P (to_rtx))
4844 if (mode1 != VOIDmode)
4845 to_rtx = adjust_address (to_rtx, mode1, 0);
4846 else if (GET_MODE (to_rtx) == VOIDmode)
4847 to_rtx = adjust_address (to_rtx, BLKmode, 0);
4850 if (offset != 0)
4852 enum machine_mode address_mode;
4853 rtx offset_rtx;
4855 if (!MEM_P (to_rtx))
4857 /* We can get constant negative offsets into arrays with broken
4858 user code. Translate this to a trap instead of ICEing. */
4859 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4860 expand_builtin_trap ();
4861 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4864 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4865 address_mode = get_address_mode (to_rtx);
4866 if (GET_MODE (offset_rtx) != address_mode)
4867 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4869 /* If we have an expression in OFFSET_RTX and a non-zero
4870 byte offset in BITPOS, adding the byte offset before the
4871 OFFSET_RTX results in better intermediate code, which makes
4872 later rtl optimization passes perform better.
4874 We prefer intermediate code like this:
4876 r124:DI=r123:DI+0x18
4877 [r124:DI]=r121:DI
4879 ... instead of ...
4881 r124:DI=r123:DI+0x10
4882 [r124:DI+0x8]=r121:DI
4884 This is only done for aligned data values, as these can
4885 be expected to result in single move instructions. */
4886 if (mode1 != VOIDmode
4887 && bitpos != 0
4888 && bitsize > 0
4889 && (bitpos % bitsize) == 0
4890 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4891 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
4893 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4894 bitregion_start = 0;
4895 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
4896 bitregion_end -= bitpos;
4897 bitpos = 0;
4900 to_rtx = offset_address (to_rtx, offset_rtx,
4901 highest_pow2_factor_for_target (to,
4902 offset));
4905 /* No action is needed if the target is not a memory and the field
4906 lies completely outside that target. This can occur if the source
4907 code contains an out-of-bounds access to a small array. */
4908 if (!MEM_P (to_rtx)
4909 && GET_MODE (to_rtx) != BLKmode
4910 && (unsigned HOST_WIDE_INT) bitpos
4911 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4913 expand_normal (from);
4914 result = NULL;
4916 /* Handle expand_expr of a complex value returning a CONCAT. */
4917 else if (GET_CODE (to_rtx) == CONCAT)
4919 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4920 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4921 && bitpos == 0
4922 && bitsize == mode_bitsize)
4923 result = store_expr (from, to_rtx, false, nontemporal);
4924 else if (bitsize == mode_bitsize / 2
4925 && (bitpos == 0 || bitpos == mode_bitsize / 2))
4926 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4927 nontemporal);
4928 else if (bitpos + bitsize <= mode_bitsize / 2)
4929 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4930 bitregion_start, bitregion_end,
4931 mode1, from,
4932 get_alias_set (to), nontemporal);
4933 else if (bitpos >= mode_bitsize / 2)
4934 result = store_field (XEXP (to_rtx, 1), bitsize,
4935 bitpos - mode_bitsize / 2,
4936 bitregion_start, bitregion_end,
4937 mode1, from,
4938 get_alias_set (to), nontemporal);
4939 else if (bitpos == 0 && bitsize == mode_bitsize)
4941 rtx from_rtx;
4942 result = expand_normal (from);
4943 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4944 TYPE_MODE (TREE_TYPE (from)), 0);
4945 emit_move_insn (XEXP (to_rtx, 0),
4946 read_complex_part (from_rtx, false));
4947 emit_move_insn (XEXP (to_rtx, 1),
4948 read_complex_part (from_rtx, true));
4950 else
4952 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4953 GET_MODE_SIZE (GET_MODE (to_rtx)));
4954 write_complex_part (temp, XEXP (to_rtx, 0), false);
4955 write_complex_part (temp, XEXP (to_rtx, 1), true);
4956 result = store_field (temp, bitsize, bitpos,
4957 bitregion_start, bitregion_end,
4958 mode1, from,
4959 get_alias_set (to), nontemporal);
4960 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4961 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4964 else
4966 if (MEM_P (to_rtx))
4968 /* If the field is at offset zero, we could have been given the
4969 DECL_RTX of the parent struct. Don't munge it. */
4970 to_rtx = shallow_copy_rtx (to_rtx);
4971 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4972 if (volatilep)
4973 MEM_VOLATILE_P (to_rtx) = 1;
4976 if (optimize_bitfield_assignment_op (bitsize, bitpos,
4977 bitregion_start, bitregion_end,
4978 mode1,
4979 to_rtx, to, from))
4980 result = NULL;
4981 else
4982 result = store_field (to_rtx, bitsize, bitpos,
4983 bitregion_start, bitregion_end,
4984 mode1, from,
4985 get_alias_set (to), nontemporal);
4988 if (result)
4989 preserve_temp_slots (result);
4990 pop_temp_slots ();
4991 return;
4994 /* If the rhs is a function call and its value is not an aggregate,
4995 call the function before we start to compute the lhs.
4996 This is needed for correct code for cases such as
4997 val = setjmp (buf) on machines where reference to val
4998 requires loading up part of an address in a separate insn.
5000 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5001 since it might be a promoted variable where the zero- or sign- extension
5002 needs to be done. Handling this in the normal way is safe because no
5003 computation is done before the call. The same is true for SSA names. */
5004 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5005 && COMPLETE_TYPE_P (TREE_TYPE (from))
5006 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5007 && ! (((TREE_CODE (to) == VAR_DECL
5008 || TREE_CODE (to) == PARM_DECL
5009 || TREE_CODE (to) == RESULT_DECL)
5010 && REG_P (DECL_RTL (to)))
5011 || TREE_CODE (to) == SSA_NAME))
5013 rtx value;
5015 push_temp_slots ();
5016 value = expand_normal (from);
5017 if (to_rtx == 0)
5018 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5020 /* Handle calls that return values in multiple non-contiguous locations.
5021 The Irix 6 ABI has examples of this. */
5022 if (GET_CODE (to_rtx) == PARALLEL)
5024 if (GET_CODE (value) == PARALLEL)
5025 emit_group_move (to_rtx, value);
5026 else
5027 emit_group_load (to_rtx, value, TREE_TYPE (from),
5028 int_size_in_bytes (TREE_TYPE (from)));
5030 else if (GET_CODE (value) == PARALLEL)
5031 emit_group_store (to_rtx, value, TREE_TYPE (from),
5032 int_size_in_bytes (TREE_TYPE (from)));
5033 else if (GET_MODE (to_rtx) == BLKmode)
5035 /* Handle calls that return BLKmode values in registers. */
5036 if (REG_P (value))
5037 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5038 else
5039 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5041 else
5043 if (POINTER_TYPE_P (TREE_TYPE (to)))
5044 value = convert_memory_address_addr_space
5045 (GET_MODE (to_rtx), value,
5046 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5048 emit_move_insn (to_rtx, value);
5050 preserve_temp_slots (to_rtx);
5051 pop_temp_slots ();
5052 return;
5055 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5056 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5058 /* Don't move directly into a return register. */
5059 if (TREE_CODE (to) == RESULT_DECL
5060 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5062 rtx temp;
5064 push_temp_slots ();
5066 /* If the source is itself a return value, it still is in a pseudo at
5067 this point so we can move it back to the return register directly. */
5068 if (REG_P (to_rtx)
5069 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5070 && TREE_CODE (from) != CALL_EXPR)
5071 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5072 else
5073 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5075 /* Handle calls that return values in multiple non-contiguous locations.
5076 The Irix 6 ABI has examples of this. */
5077 if (GET_CODE (to_rtx) == PARALLEL)
5079 if (GET_CODE (temp) == PARALLEL)
5080 emit_group_move (to_rtx, temp);
5081 else
5082 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5083 int_size_in_bytes (TREE_TYPE (from)));
5085 else if (temp)
5086 emit_move_insn (to_rtx, temp);
5088 preserve_temp_slots (to_rtx);
5089 pop_temp_slots ();
5090 return;
5093 /* In case we are returning the contents of an object which overlaps
5094 the place the value is being stored, use a safe function when copying
5095 a value through a pointer into a structure value return block. */
5096 if (TREE_CODE (to) == RESULT_DECL
5097 && TREE_CODE (from) == INDIRECT_REF
5098 && ADDR_SPACE_GENERIC_P
5099 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5100 && refs_may_alias_p (to, from)
5101 && cfun->returns_struct
5102 && !cfun->returns_pcc_struct)
5104 rtx from_rtx, size;
5106 push_temp_slots ();
5107 size = expr_size (from);
5108 from_rtx = expand_normal (from);
5110 emit_library_call (memmove_libfunc, LCT_NORMAL,
5111 VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
5112 XEXP (from_rtx, 0), Pmode,
5113 convert_to_mode (TYPE_MODE (sizetype),
5114 size, TYPE_UNSIGNED (sizetype)),
5115 TYPE_MODE (sizetype));
5117 preserve_temp_slots (to_rtx);
5118 pop_temp_slots ();
5119 return;
5122 /* Compute FROM and store the value in the rtx we got. */
5124 push_temp_slots ();
5125 result = store_expr (from, to_rtx, 0, nontemporal);
5126 preserve_temp_slots (result);
5127 pop_temp_slots ();
5128 return;
5131 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5132 succeeded, false otherwise. */
5134 bool
5135 emit_storent_insn (rtx to, rtx from)
5137 struct expand_operand ops[2];
5138 enum machine_mode mode = GET_MODE (to);
5139 enum insn_code code = optab_handler (storent_optab, mode);
5141 if (code == CODE_FOR_nothing)
5142 return false;
5144 create_fixed_operand (&ops[0], to);
5145 create_input_operand (&ops[1], from, mode);
5146 return maybe_expand_insn (code, 2, ops);
5149 /* Generate code for computing expression EXP,
5150 and storing the value into TARGET.
5152 If the mode is BLKmode then we may return TARGET itself.
5153 It turns out that in BLKmode it doesn't cause a problem.
5154 because C has no operators that could combine two different
5155 assignments into the same BLKmode object with different values
5156 with no sequence point. Will other languages need this to
5157 be more thorough?
5159 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5160 stack, and block moves may need to be treated specially.
5162 If NONTEMPORAL is true, try using a nontemporal store instruction. */
5165 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
5167 rtx temp;
5168 rtx alt_rtl = NULL_RTX;
5169 location_t loc = curr_insn_location ();
5171 if (VOID_TYPE_P (TREE_TYPE (exp)))
5173 /* C++ can generate ?: expressions with a throw expression in one
5174 branch and an rvalue in the other. Here, we resolve attempts to
5175 store the throw expression's nonexistent result. */
5176 gcc_assert (!call_param_p);
5177 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5178 return NULL_RTX;
5180 if (TREE_CODE (exp) == COMPOUND_EXPR)
5182 /* Perform first part of compound expression, then assign from second
5183 part. */
5184 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5185 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5186 return store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5187 nontemporal);
5189 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5191 /* For conditional expression, get safe form of the target. Then
5192 test the condition, doing the appropriate assignment on either
5193 side. This avoids the creation of unnecessary temporaries.
5194 For non-BLKmode, it is more efficient not to do this. */
5196 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5198 do_pending_stack_adjust ();
5199 NO_DEFER_POP;
5200 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5201 store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5202 nontemporal);
5203 emit_jump_insn (gen_jump (lab2));
5204 emit_barrier ();
5205 emit_label (lab1);
5206 store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5207 nontemporal);
5208 emit_label (lab2);
5209 OK_DEFER_POP;
5211 return NULL_RTX;
5213 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5214 /* If this is a scalar in a register that is stored in a wider mode
5215 than the declared mode, compute the result into its declared mode
5216 and then convert to the wider mode. Our value is the computed
5217 expression. */
5219 rtx inner_target = 0;
5221 /* We can do the conversion inside EXP, which will often result
5222 in some optimizations. Do the conversion in two steps: first
5223 change the signedness, if needed, then the extend. But don't
5224 do this if the type of EXP is a subtype of something else
5225 since then the conversion might involve more than just
5226 converting modes. */
5227 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5228 && TREE_TYPE (TREE_TYPE (exp)) == 0
5229 && GET_MODE_PRECISION (GET_MODE (target))
5230 == TYPE_PRECISION (TREE_TYPE (exp)))
5232 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5233 TYPE_UNSIGNED (TREE_TYPE (exp))))
5235 /* Some types, e.g. Fortran's logical*4, won't have a signed
5236 version, so use the mode instead. */
5237 tree ntype
5238 = (signed_or_unsigned_type_for
5239 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5240 if (ntype == NULL)
5241 ntype = lang_hooks.types.type_for_mode
5242 (TYPE_MODE (TREE_TYPE (exp)),
5243 SUBREG_PROMOTED_SIGN (target));
5245 exp = fold_convert_loc (loc, ntype, exp);
5248 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5249 (GET_MODE (SUBREG_REG (target)),
5250 SUBREG_PROMOTED_SIGN (target)),
5251 exp);
5253 inner_target = SUBREG_REG (target);
5256 temp = expand_expr (exp, inner_target, VOIDmode,
5257 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5259 /* If TEMP is a VOIDmode constant, use convert_modes to make
5260 sure that we properly convert it. */
5261 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5263 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5264 temp, SUBREG_PROMOTED_SIGN (target));
5265 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5266 GET_MODE (target), temp,
5267 SUBREG_PROMOTED_SIGN (target));
5270 convert_move (SUBREG_REG (target), temp,
5271 SUBREG_PROMOTED_SIGN (target));
5273 return NULL_RTX;
5275 else if ((TREE_CODE (exp) == STRING_CST
5276 || (TREE_CODE (exp) == MEM_REF
5277 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5278 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5279 == STRING_CST
5280 && integer_zerop (TREE_OPERAND (exp, 1))))
5281 && !nontemporal && !call_param_p
5282 && MEM_P (target))
5284 /* Optimize initialization of an array with a STRING_CST. */
5285 HOST_WIDE_INT exp_len, str_copy_len;
5286 rtx dest_mem;
5287 tree str = TREE_CODE (exp) == STRING_CST
5288 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5290 exp_len = int_expr_size (exp);
5291 if (exp_len <= 0)
5292 goto normal_expr;
5294 if (TREE_STRING_LENGTH (str) <= 0)
5295 goto normal_expr;
5297 str_copy_len = strlen (TREE_STRING_POINTER (str));
5298 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5299 goto normal_expr;
5301 str_copy_len = TREE_STRING_LENGTH (str);
5302 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5303 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5305 str_copy_len += STORE_MAX_PIECES - 1;
5306 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5308 str_copy_len = MIN (str_copy_len, exp_len);
5309 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5310 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5311 MEM_ALIGN (target), false))
5312 goto normal_expr;
5314 dest_mem = target;
5316 dest_mem = store_by_pieces (dest_mem,
5317 str_copy_len, builtin_strncpy_read_str,
5318 CONST_CAST (char *,
5319 TREE_STRING_POINTER (str)),
5320 MEM_ALIGN (target), false,
5321 exp_len > str_copy_len ? 1 : 0);
5322 if (exp_len > str_copy_len)
5323 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5324 GEN_INT (exp_len - str_copy_len),
5325 BLOCK_OP_NORMAL);
5326 return NULL_RTX;
5328 else
5330 rtx tmp_target;
5332 normal_expr:
5333 /* If we want to use a nontemporal store, force the value to
5334 register first. */
5335 tmp_target = nontemporal ? NULL_RTX : target;
5336 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5337 (call_param_p
5338 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5339 &alt_rtl, false);
5342 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5343 the same as that of TARGET, adjust the constant. This is needed, for
5344 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5345 only a word-sized value. */
5346 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5347 && TREE_CODE (exp) != ERROR_MARK
5348 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5349 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5350 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5352 /* If value was not generated in the target, store it there.
5353 Convert the value to TARGET's type first if necessary and emit the
5354 pending incrementations that have been queued when expanding EXP.
5355 Note that we cannot emit the whole queue blindly because this will
5356 effectively disable the POST_INC optimization later.
5358 If TEMP and TARGET compare equal according to rtx_equal_p, but
5359 one or both of them are volatile memory refs, we have to distinguish
5360 two cases:
5361 - expand_expr has used TARGET. In this case, we must not generate
5362 another copy. This can be detected by TARGET being equal according
5363 to == .
5364 - expand_expr has not used TARGET - that means that the source just
5365 happens to have the same RTX form. Since temp will have been created
5366 by expand_expr, it will compare unequal according to == .
5367 We must generate a copy in this case, to reach the correct number
5368 of volatile memory references. */
5370 if ((! rtx_equal_p (temp, target)
5371 || (temp != target && (side_effects_p (temp)
5372 || side_effects_p (target))))
5373 && TREE_CODE (exp) != ERROR_MARK
5374 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5375 but TARGET is not valid memory reference, TEMP will differ
5376 from TARGET although it is really the same location. */
5377 && !(alt_rtl
5378 && rtx_equal_p (alt_rtl, target)
5379 && !side_effects_p (alt_rtl)
5380 && !side_effects_p (target))
5381 /* If there's nothing to copy, don't bother. Don't call
5382 expr_size unless necessary, because some front-ends (C++)
5383 expr_size-hook must not be given objects that are not
5384 supposed to be bit-copied or bit-initialized. */
5385 && expr_size (exp) != const0_rtx)
5387 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5389 if (GET_MODE (target) == BLKmode)
5391 /* Handle calls that return BLKmode values in registers. */
5392 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5393 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5394 else
5395 store_bit_field (target,
5396 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5397 0, 0, 0, GET_MODE (temp), temp);
5399 else
5400 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5403 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5405 /* Handle copying a string constant into an array. The string
5406 constant may be shorter than the array. So copy just the string's
5407 actual length, and clear the rest. First get the size of the data
5408 type of the string, which is actually the size of the target. */
5409 rtx size = expr_size (exp);
5411 if (CONST_INT_P (size)
5412 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5413 emit_block_move (target, temp, size,
5414 (call_param_p
5415 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5416 else
5418 enum machine_mode pointer_mode
5419 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5420 enum machine_mode address_mode = get_address_mode (target);
5422 /* Compute the size of the data to copy from the string. */
5423 tree copy_size
5424 = size_binop_loc (loc, MIN_EXPR,
5425 make_tree (sizetype, size),
5426 size_int (TREE_STRING_LENGTH (exp)));
5427 rtx copy_size_rtx
5428 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5429 (call_param_p
5430 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5431 rtx_code_label *label = 0;
5433 /* Copy that much. */
5434 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5435 TYPE_UNSIGNED (sizetype));
5436 emit_block_move (target, temp, copy_size_rtx,
5437 (call_param_p
5438 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5440 /* Figure out how much is left in TARGET that we have to clear.
5441 Do all calculations in pointer_mode. */
5442 if (CONST_INT_P (copy_size_rtx))
5444 size = plus_constant (address_mode, size,
5445 -INTVAL (copy_size_rtx));
5446 target = adjust_address (target, BLKmode,
5447 INTVAL (copy_size_rtx));
5449 else
5451 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5452 copy_size_rtx, NULL_RTX, 0,
5453 OPTAB_LIB_WIDEN);
5455 if (GET_MODE (copy_size_rtx) != address_mode)
5456 copy_size_rtx = convert_to_mode (address_mode,
5457 copy_size_rtx,
5458 TYPE_UNSIGNED (sizetype));
5460 target = offset_address (target, copy_size_rtx,
5461 highest_pow2_factor (copy_size));
5462 label = gen_label_rtx ();
5463 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5464 GET_MODE (size), 0, label);
5467 if (size != const0_rtx)
5468 clear_storage (target, size, BLOCK_OP_NORMAL);
5470 if (label)
5471 emit_label (label);
5474 /* Handle calls that return values in multiple non-contiguous locations.
5475 The Irix 6 ABI has examples of this. */
5476 else if (GET_CODE (target) == PARALLEL)
5478 if (GET_CODE (temp) == PARALLEL)
5479 emit_group_move (target, temp);
5480 else
5481 emit_group_load (target, temp, TREE_TYPE (exp),
5482 int_size_in_bytes (TREE_TYPE (exp)));
5484 else if (GET_CODE (temp) == PARALLEL)
5485 emit_group_store (target, temp, TREE_TYPE (exp),
5486 int_size_in_bytes (TREE_TYPE (exp)));
5487 else if (GET_MODE (temp) == BLKmode)
5488 emit_block_move (target, temp, expr_size (exp),
5489 (call_param_p
5490 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5491 /* If we emit a nontemporal store, there is nothing else to do. */
5492 else if (nontemporal && emit_storent_insn (target, temp))
5494 else
5496 temp = force_operand (temp, target);
5497 if (temp != target)
5498 emit_move_insn (target, temp);
5502 return NULL_RTX;
5505 /* Return true if field F of structure TYPE is a flexible array. */
5507 static bool
5508 flexible_array_member_p (const_tree f, const_tree type)
5510 const_tree tf;
5512 tf = TREE_TYPE (f);
5513 return (DECL_CHAIN (f) == NULL
5514 && TREE_CODE (tf) == ARRAY_TYPE
5515 && TYPE_DOMAIN (tf)
5516 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5517 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5518 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5519 && int_size_in_bytes (type) >= 0);
5522 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5523 must have in order for it to completely initialize a value of type TYPE.
5524 Return -1 if the number isn't known.
5526 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5528 static HOST_WIDE_INT
5529 count_type_elements (const_tree type, bool for_ctor_p)
5531 switch (TREE_CODE (type))
5533 case ARRAY_TYPE:
5535 tree nelts;
5537 nelts = array_type_nelts (type);
5538 if (nelts && tree_fits_uhwi_p (nelts))
5540 unsigned HOST_WIDE_INT n;
5542 n = tree_to_uhwi (nelts) + 1;
5543 if (n == 0 || for_ctor_p)
5544 return n;
5545 else
5546 return n * count_type_elements (TREE_TYPE (type), false);
5548 return for_ctor_p ? -1 : 1;
5551 case RECORD_TYPE:
5553 unsigned HOST_WIDE_INT n;
5554 tree f;
5556 n = 0;
5557 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5558 if (TREE_CODE (f) == FIELD_DECL)
5560 if (!for_ctor_p)
5561 n += count_type_elements (TREE_TYPE (f), false);
5562 else if (!flexible_array_member_p (f, type))
5563 /* Don't count flexible arrays, which are not supposed
5564 to be initialized. */
5565 n += 1;
5568 return n;
5571 case UNION_TYPE:
5572 case QUAL_UNION_TYPE:
5574 tree f;
5575 HOST_WIDE_INT n, m;
5577 gcc_assert (!for_ctor_p);
5578 /* Estimate the number of scalars in each field and pick the
5579 maximum. Other estimates would do instead; the idea is simply
5580 to make sure that the estimate is not sensitive to the ordering
5581 of the fields. */
5582 n = 1;
5583 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5584 if (TREE_CODE (f) == FIELD_DECL)
5586 m = count_type_elements (TREE_TYPE (f), false);
5587 /* If the field doesn't span the whole union, add an extra
5588 scalar for the rest. */
5589 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5590 TYPE_SIZE (type)) != 1)
5591 m++;
5592 if (n < m)
5593 n = m;
5595 return n;
5598 case COMPLEX_TYPE:
5599 return 2;
5601 case VECTOR_TYPE:
5602 return TYPE_VECTOR_SUBPARTS (type);
5604 case INTEGER_TYPE:
5605 case REAL_TYPE:
5606 case FIXED_POINT_TYPE:
5607 case ENUMERAL_TYPE:
5608 case BOOLEAN_TYPE:
5609 case POINTER_TYPE:
5610 case OFFSET_TYPE:
5611 case REFERENCE_TYPE:
5612 case NULLPTR_TYPE:
5613 return 1;
5615 case ERROR_MARK:
5616 return 0;
5618 case VOID_TYPE:
5619 case METHOD_TYPE:
5620 case FUNCTION_TYPE:
5621 case LANG_TYPE:
5622 default:
5623 gcc_unreachable ();
5627 /* Helper for categorize_ctor_elements. Identical interface. */
5629 static bool
5630 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5631 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5633 unsigned HOST_WIDE_INT idx;
5634 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5635 tree value, purpose, elt_type;
5637 /* Whether CTOR is a valid constant initializer, in accordance with what
5638 initializer_constant_valid_p does. If inferred from the constructor
5639 elements, true until proven otherwise. */
5640 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5641 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5643 nz_elts = 0;
5644 init_elts = 0;
5645 num_fields = 0;
5646 elt_type = NULL_TREE;
5648 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5650 HOST_WIDE_INT mult = 1;
5652 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5654 tree lo_index = TREE_OPERAND (purpose, 0);
5655 tree hi_index = TREE_OPERAND (purpose, 1);
5657 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5658 mult = (tree_to_uhwi (hi_index)
5659 - tree_to_uhwi (lo_index) + 1);
5661 num_fields += mult;
5662 elt_type = TREE_TYPE (value);
5664 switch (TREE_CODE (value))
5666 case CONSTRUCTOR:
5668 HOST_WIDE_INT nz = 0, ic = 0;
5670 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5671 p_complete);
5673 nz_elts += mult * nz;
5674 init_elts += mult * ic;
5676 if (const_from_elts_p && const_p)
5677 const_p = const_elt_p;
5679 break;
5681 case INTEGER_CST:
5682 case REAL_CST:
5683 case FIXED_CST:
5684 if (!initializer_zerop (value))
5685 nz_elts += mult;
5686 init_elts += mult;
5687 break;
5689 case STRING_CST:
5690 nz_elts += mult * TREE_STRING_LENGTH (value);
5691 init_elts += mult * TREE_STRING_LENGTH (value);
5692 break;
5694 case COMPLEX_CST:
5695 if (!initializer_zerop (TREE_REALPART (value)))
5696 nz_elts += mult;
5697 if (!initializer_zerop (TREE_IMAGPART (value)))
5698 nz_elts += mult;
5699 init_elts += mult;
5700 break;
5702 case VECTOR_CST:
5704 unsigned i;
5705 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5707 tree v = VECTOR_CST_ELT (value, i);
5708 if (!initializer_zerop (v))
5709 nz_elts += mult;
5710 init_elts += mult;
5713 break;
5715 default:
5717 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5718 nz_elts += mult * tc;
5719 init_elts += mult * tc;
5721 if (const_from_elts_p && const_p)
5722 const_p = initializer_constant_valid_p (value, elt_type)
5723 != NULL_TREE;
5725 break;
5729 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5730 num_fields, elt_type))
5731 *p_complete = false;
5733 *p_nz_elts += nz_elts;
5734 *p_init_elts += init_elts;
5736 return const_p;
5739 /* Examine CTOR to discover:
5740 * how many scalar fields are set to nonzero values,
5741 and place it in *P_NZ_ELTS;
5742 * how many scalar fields in total are in CTOR,
5743 and place it in *P_ELT_COUNT.
5744 * whether the constructor is complete -- in the sense that every
5745 meaningful byte is explicitly given a value --
5746 and place it in *P_COMPLETE.
5748 Return whether or not CTOR is a valid static constant initializer, the same
5749 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5751 bool
5752 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5753 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5755 *p_nz_elts = 0;
5756 *p_init_elts = 0;
5757 *p_complete = true;
5759 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5762 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5763 of which had type LAST_TYPE. Each element was itself a complete
5764 initializer, in the sense that every meaningful byte was explicitly
5765 given a value. Return true if the same is true for the constructor
5766 as a whole. */
5768 bool
5769 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5770 const_tree last_type)
5772 if (TREE_CODE (type) == UNION_TYPE
5773 || TREE_CODE (type) == QUAL_UNION_TYPE)
5775 if (num_elts == 0)
5776 return false;
5778 gcc_assert (num_elts == 1 && last_type);
5780 /* ??? We could look at each element of the union, and find the
5781 largest element. Which would avoid comparing the size of the
5782 initialized element against any tail padding in the union.
5783 Doesn't seem worth the effort... */
5784 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5787 return count_type_elements (type, true) == num_elts;
5790 /* Return 1 if EXP contains mostly (3/4) zeros. */
5792 static int
5793 mostly_zeros_p (const_tree exp)
5795 if (TREE_CODE (exp) == CONSTRUCTOR)
5797 HOST_WIDE_INT nz_elts, init_elts;
5798 bool complete_p;
5800 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5801 return !complete_p || nz_elts < init_elts / 4;
5804 return initializer_zerop (exp);
5807 /* Return 1 if EXP contains all zeros. */
5809 static int
5810 all_zeros_p (const_tree exp)
5812 if (TREE_CODE (exp) == CONSTRUCTOR)
5814 HOST_WIDE_INT nz_elts, init_elts;
5815 bool complete_p;
5817 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5818 return nz_elts == 0;
5821 return initializer_zerop (exp);
5824 /* Helper function for store_constructor.
5825 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5826 CLEARED is as for store_constructor.
5827 ALIAS_SET is the alias set to use for any stores.
5829 This provides a recursive shortcut back to store_constructor when it isn't
5830 necessary to go through store_field. This is so that we can pass through
5831 the cleared field to let store_constructor know that we may not have to
5832 clear a substructure if the outer structure has already been cleared. */
5834 static void
5835 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5836 HOST_WIDE_INT bitpos, enum machine_mode mode,
5837 tree exp, int cleared, alias_set_type alias_set)
5839 if (TREE_CODE (exp) == CONSTRUCTOR
5840 /* We can only call store_constructor recursively if the size and
5841 bit position are on a byte boundary. */
5842 && bitpos % BITS_PER_UNIT == 0
5843 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5844 /* If we have a nonzero bitpos for a register target, then we just
5845 let store_field do the bitfield handling. This is unlikely to
5846 generate unnecessary clear instructions anyways. */
5847 && (bitpos == 0 || MEM_P (target)))
5849 if (MEM_P (target))
5850 target
5851 = adjust_address (target,
5852 GET_MODE (target) == BLKmode
5853 || 0 != (bitpos
5854 % GET_MODE_ALIGNMENT (GET_MODE (target)))
5855 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5858 /* Update the alias set, if required. */
5859 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5860 && MEM_ALIAS_SET (target) != 0)
5862 target = copy_rtx (target);
5863 set_mem_alias_set (target, alias_set);
5866 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5868 else
5869 store_field (target, bitsize, bitpos, 0, 0, mode, exp, alias_set, false);
5873 /* Returns the number of FIELD_DECLs in TYPE. */
5875 static int
5876 fields_length (const_tree type)
5878 tree t = TYPE_FIELDS (type);
5879 int count = 0;
5881 for (; t; t = DECL_CHAIN (t))
5882 if (TREE_CODE (t) == FIELD_DECL)
5883 ++count;
5885 return count;
5889 /* Store the value of constructor EXP into the rtx TARGET.
5890 TARGET is either a REG or a MEM; we know it cannot conflict, since
5891 safe_from_p has been called.
5892 CLEARED is true if TARGET is known to have been zero'd.
5893 SIZE is the number of bytes of TARGET we are allowed to modify: this
5894 may not be the same as the size of EXP if we are assigning to a field
5895 which has been packed to exclude padding bits. */
5897 static void
5898 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5900 tree type = TREE_TYPE (exp);
5901 #ifdef WORD_REGISTER_OPERATIONS
5902 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5903 #endif
5905 switch (TREE_CODE (type))
5907 case RECORD_TYPE:
5908 case UNION_TYPE:
5909 case QUAL_UNION_TYPE:
5911 unsigned HOST_WIDE_INT idx;
5912 tree field, value;
5914 /* If size is zero or the target is already cleared, do nothing. */
5915 if (size == 0 || cleared)
5916 cleared = 1;
5917 /* We either clear the aggregate or indicate the value is dead. */
5918 else if ((TREE_CODE (type) == UNION_TYPE
5919 || TREE_CODE (type) == QUAL_UNION_TYPE)
5920 && ! CONSTRUCTOR_ELTS (exp))
5921 /* If the constructor is empty, clear the union. */
5923 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5924 cleared = 1;
5927 /* If we are building a static constructor into a register,
5928 set the initial value as zero so we can fold the value into
5929 a constant. But if more than one register is involved,
5930 this probably loses. */
5931 else if (REG_P (target) && TREE_STATIC (exp)
5932 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5934 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5935 cleared = 1;
5938 /* If the constructor has fewer fields than the structure or
5939 if we are initializing the structure to mostly zeros, clear
5940 the whole structure first. Don't do this if TARGET is a
5941 register whose mode size isn't equal to SIZE since
5942 clear_storage can't handle this case. */
5943 else if (size > 0
5944 && (((int)vec_safe_length (CONSTRUCTOR_ELTS (exp))
5945 != fields_length (type))
5946 || mostly_zeros_p (exp))
5947 && (!REG_P (target)
5948 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5949 == size)))
5951 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5952 cleared = 1;
5955 if (REG_P (target) && !cleared)
5956 emit_clobber (target);
5958 /* Store each element of the constructor into the
5959 corresponding field of TARGET. */
5960 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
5962 enum machine_mode mode;
5963 HOST_WIDE_INT bitsize;
5964 HOST_WIDE_INT bitpos = 0;
5965 tree offset;
5966 rtx to_rtx = target;
5968 /* Just ignore missing fields. We cleared the whole
5969 structure, above, if any fields are missing. */
5970 if (field == 0)
5971 continue;
5973 if (cleared && initializer_zerop (value))
5974 continue;
5976 if (tree_fits_uhwi_p (DECL_SIZE (field)))
5977 bitsize = tree_to_uhwi (DECL_SIZE (field));
5978 else
5979 bitsize = -1;
5981 mode = DECL_MODE (field);
5982 if (DECL_BIT_FIELD (field))
5983 mode = VOIDmode;
5985 offset = DECL_FIELD_OFFSET (field);
5986 if (tree_fits_shwi_p (offset)
5987 && tree_fits_shwi_p (bit_position (field)))
5989 bitpos = int_bit_position (field);
5990 offset = 0;
5992 else
5993 bitpos = tree_to_shwi (DECL_FIELD_BIT_OFFSET (field));
5995 if (offset)
5997 enum machine_mode address_mode;
5998 rtx offset_rtx;
6000 offset
6001 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
6002 make_tree (TREE_TYPE (exp),
6003 target));
6005 offset_rtx = expand_normal (offset);
6006 gcc_assert (MEM_P (to_rtx));
6008 address_mode = get_address_mode (to_rtx);
6009 if (GET_MODE (offset_rtx) != address_mode)
6010 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
6012 to_rtx = offset_address (to_rtx, offset_rtx,
6013 highest_pow2_factor (offset));
6016 #ifdef WORD_REGISTER_OPERATIONS
6017 /* If this initializes a field that is smaller than a
6018 word, at the start of a word, try to widen it to a full
6019 word. This special case allows us to output C++ member
6020 function initializations in a form that the optimizers
6021 can understand. */
6022 if (REG_P (target)
6023 && bitsize < BITS_PER_WORD
6024 && bitpos % BITS_PER_WORD == 0
6025 && GET_MODE_CLASS (mode) == MODE_INT
6026 && TREE_CODE (value) == INTEGER_CST
6027 && exp_size >= 0
6028 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6030 tree type = TREE_TYPE (value);
6032 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6034 type = lang_hooks.types.type_for_mode
6035 (word_mode, TYPE_UNSIGNED (type));
6036 value = fold_convert (type, value);
6039 if (BYTES_BIG_ENDIAN)
6040 value
6041 = fold_build2 (LSHIFT_EXPR, type, value,
6042 build_int_cst (type,
6043 BITS_PER_WORD - bitsize));
6044 bitsize = BITS_PER_WORD;
6045 mode = word_mode;
6047 #endif
6049 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6050 && DECL_NONADDRESSABLE_P (field))
6052 to_rtx = copy_rtx (to_rtx);
6053 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6056 store_constructor_field (to_rtx, bitsize, bitpos, mode,
6057 value, cleared,
6058 get_alias_set (TREE_TYPE (field)));
6060 break;
6062 case ARRAY_TYPE:
6064 tree value, index;
6065 unsigned HOST_WIDE_INT i;
6066 int need_to_clear;
6067 tree domain;
6068 tree elttype = TREE_TYPE (type);
6069 int const_bounds_p;
6070 HOST_WIDE_INT minelt = 0;
6071 HOST_WIDE_INT maxelt = 0;
6073 domain = TYPE_DOMAIN (type);
6074 const_bounds_p = (TYPE_MIN_VALUE (domain)
6075 && TYPE_MAX_VALUE (domain)
6076 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6077 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6079 /* If we have constant bounds for the range of the type, get them. */
6080 if (const_bounds_p)
6082 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6083 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6086 /* If the constructor has fewer elements than the array, clear
6087 the whole array first. Similarly if this is static
6088 constructor of a non-BLKmode object. */
6089 if (cleared)
6090 need_to_clear = 0;
6091 else if (REG_P (target) && TREE_STATIC (exp))
6092 need_to_clear = 1;
6093 else
6095 unsigned HOST_WIDE_INT idx;
6096 tree index, value;
6097 HOST_WIDE_INT count = 0, zero_count = 0;
6098 need_to_clear = ! const_bounds_p;
6100 /* This loop is a more accurate version of the loop in
6101 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6102 is also needed to check for missing elements. */
6103 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6105 HOST_WIDE_INT this_node_count;
6107 if (need_to_clear)
6108 break;
6110 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6112 tree lo_index = TREE_OPERAND (index, 0);
6113 tree hi_index = TREE_OPERAND (index, 1);
6115 if (! tree_fits_uhwi_p (lo_index)
6116 || ! tree_fits_uhwi_p (hi_index))
6118 need_to_clear = 1;
6119 break;
6122 this_node_count = (tree_to_uhwi (hi_index)
6123 - tree_to_uhwi (lo_index) + 1);
6125 else
6126 this_node_count = 1;
6128 count += this_node_count;
6129 if (mostly_zeros_p (value))
6130 zero_count += this_node_count;
6133 /* Clear the entire array first if there are any missing
6134 elements, or if the incidence of zero elements is >=
6135 75%. */
6136 if (! need_to_clear
6137 && (count < maxelt - minelt + 1
6138 || 4 * zero_count >= 3 * count))
6139 need_to_clear = 1;
6142 if (need_to_clear && size > 0)
6144 if (REG_P (target))
6145 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6146 else
6147 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6148 cleared = 1;
6151 if (!cleared && REG_P (target))
6152 /* Inform later passes that the old value is dead. */
6153 emit_clobber (target);
6155 /* Store each element of the constructor into the
6156 corresponding element of TARGET, determined by counting the
6157 elements. */
6158 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6160 enum machine_mode mode;
6161 HOST_WIDE_INT bitsize;
6162 HOST_WIDE_INT bitpos;
6163 rtx xtarget = target;
6165 if (cleared && initializer_zerop (value))
6166 continue;
6168 mode = TYPE_MODE (elttype);
6169 if (mode == BLKmode)
6170 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6171 ? tree_to_uhwi (TYPE_SIZE (elttype))
6172 : -1);
6173 else
6174 bitsize = GET_MODE_BITSIZE (mode);
6176 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6178 tree lo_index = TREE_OPERAND (index, 0);
6179 tree hi_index = TREE_OPERAND (index, 1);
6180 rtx index_r, pos_rtx;
6181 HOST_WIDE_INT lo, hi, count;
6182 tree position;
6184 /* If the range is constant and "small", unroll the loop. */
6185 if (const_bounds_p
6186 && tree_fits_shwi_p (lo_index)
6187 && tree_fits_shwi_p (hi_index)
6188 && (lo = tree_to_shwi (lo_index),
6189 hi = tree_to_shwi (hi_index),
6190 count = hi - lo + 1,
6191 (!MEM_P (target)
6192 || count <= 2
6193 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6194 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6195 <= 40 * 8)))))
6197 lo -= minelt; hi -= minelt;
6198 for (; lo <= hi; lo++)
6200 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6202 if (MEM_P (target)
6203 && !MEM_KEEP_ALIAS_SET_P (target)
6204 && TREE_CODE (type) == ARRAY_TYPE
6205 && TYPE_NONALIASED_COMPONENT (type))
6207 target = copy_rtx (target);
6208 MEM_KEEP_ALIAS_SET_P (target) = 1;
6211 store_constructor_field
6212 (target, bitsize, bitpos, mode, value, cleared,
6213 get_alias_set (elttype));
6216 else
6218 rtx_code_label *loop_start = gen_label_rtx ();
6219 rtx_code_label *loop_end = gen_label_rtx ();
6220 tree exit_cond;
6222 expand_normal (hi_index);
6224 index = build_decl (EXPR_LOCATION (exp),
6225 VAR_DECL, NULL_TREE, domain);
6226 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6227 SET_DECL_RTL (index, index_r);
6228 store_expr (lo_index, index_r, 0, false);
6230 /* Build the head of the loop. */
6231 do_pending_stack_adjust ();
6232 emit_label (loop_start);
6234 /* Assign value to element index. */
6235 position =
6236 fold_convert (ssizetype,
6237 fold_build2 (MINUS_EXPR,
6238 TREE_TYPE (index),
6239 index,
6240 TYPE_MIN_VALUE (domain)));
6242 position =
6243 size_binop (MULT_EXPR, position,
6244 fold_convert (ssizetype,
6245 TYPE_SIZE_UNIT (elttype)));
6247 pos_rtx = expand_normal (position);
6248 xtarget = offset_address (target, pos_rtx,
6249 highest_pow2_factor (position));
6250 xtarget = adjust_address (xtarget, mode, 0);
6251 if (TREE_CODE (value) == CONSTRUCTOR)
6252 store_constructor (value, xtarget, cleared,
6253 bitsize / BITS_PER_UNIT);
6254 else
6255 store_expr (value, xtarget, 0, false);
6257 /* Generate a conditional jump to exit the loop. */
6258 exit_cond = build2 (LT_EXPR, integer_type_node,
6259 index, hi_index);
6260 jumpif (exit_cond, loop_end, -1);
6262 /* Update the loop counter, and jump to the head of
6263 the loop. */
6264 expand_assignment (index,
6265 build2 (PLUS_EXPR, TREE_TYPE (index),
6266 index, integer_one_node),
6267 false);
6269 emit_jump (loop_start);
6271 /* Build the end of the loop. */
6272 emit_label (loop_end);
6275 else if ((index != 0 && ! tree_fits_shwi_p (index))
6276 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6278 tree position;
6280 if (index == 0)
6281 index = ssize_int (1);
6283 if (minelt)
6284 index = fold_convert (ssizetype,
6285 fold_build2 (MINUS_EXPR,
6286 TREE_TYPE (index),
6287 index,
6288 TYPE_MIN_VALUE (domain)));
6290 position =
6291 size_binop (MULT_EXPR, index,
6292 fold_convert (ssizetype,
6293 TYPE_SIZE_UNIT (elttype)));
6294 xtarget = offset_address (target,
6295 expand_normal (position),
6296 highest_pow2_factor (position));
6297 xtarget = adjust_address (xtarget, mode, 0);
6298 store_expr (value, xtarget, 0, false);
6300 else
6302 if (index != 0)
6303 bitpos = ((tree_to_shwi (index) - minelt)
6304 * tree_to_uhwi (TYPE_SIZE (elttype)));
6305 else
6306 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6308 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6309 && TREE_CODE (type) == ARRAY_TYPE
6310 && TYPE_NONALIASED_COMPONENT (type))
6312 target = copy_rtx (target);
6313 MEM_KEEP_ALIAS_SET_P (target) = 1;
6315 store_constructor_field (target, bitsize, bitpos, mode, value,
6316 cleared, get_alias_set (elttype));
6319 break;
6322 case VECTOR_TYPE:
6324 unsigned HOST_WIDE_INT idx;
6325 constructor_elt *ce;
6326 int i;
6327 int need_to_clear;
6328 int icode = CODE_FOR_nothing;
6329 tree elttype = TREE_TYPE (type);
6330 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6331 enum machine_mode eltmode = TYPE_MODE (elttype);
6332 HOST_WIDE_INT bitsize;
6333 HOST_WIDE_INT bitpos;
6334 rtvec vector = NULL;
6335 unsigned n_elts;
6336 alias_set_type alias;
6338 gcc_assert (eltmode != BLKmode);
6340 n_elts = TYPE_VECTOR_SUBPARTS (type);
6341 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6343 enum machine_mode mode = GET_MODE (target);
6345 icode = (int) optab_handler (vec_init_optab, mode);
6346 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6347 if (icode != CODE_FOR_nothing)
6349 tree value;
6351 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6352 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6354 icode = CODE_FOR_nothing;
6355 break;
6358 if (icode != CODE_FOR_nothing)
6360 unsigned int i;
6362 vector = rtvec_alloc (n_elts);
6363 for (i = 0; i < n_elts; i++)
6364 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6368 /* If the constructor has fewer elements than the vector,
6369 clear the whole array first. Similarly if this is static
6370 constructor of a non-BLKmode object. */
6371 if (cleared)
6372 need_to_clear = 0;
6373 else if (REG_P (target) && TREE_STATIC (exp))
6374 need_to_clear = 1;
6375 else
6377 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6378 tree value;
6380 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6382 int n_elts_here = tree_to_uhwi
6383 (int_const_binop (TRUNC_DIV_EXPR,
6384 TYPE_SIZE (TREE_TYPE (value)),
6385 TYPE_SIZE (elttype)));
6387 count += n_elts_here;
6388 if (mostly_zeros_p (value))
6389 zero_count += n_elts_here;
6392 /* Clear the entire vector first if there are any missing elements,
6393 or if the incidence of zero elements is >= 75%. */
6394 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6397 if (need_to_clear && size > 0 && !vector)
6399 if (REG_P (target))
6400 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6401 else
6402 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6403 cleared = 1;
6406 /* Inform later passes that the old value is dead. */
6407 if (!cleared && !vector && REG_P (target))
6408 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6410 if (MEM_P (target))
6411 alias = MEM_ALIAS_SET (target);
6412 else
6413 alias = get_alias_set (elttype);
6415 /* Store each element of the constructor into the corresponding
6416 element of TARGET, determined by counting the elements. */
6417 for (idx = 0, i = 0;
6418 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6419 idx++, i += bitsize / elt_size)
6421 HOST_WIDE_INT eltpos;
6422 tree value = ce->value;
6424 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6425 if (cleared && initializer_zerop (value))
6426 continue;
6428 if (ce->index)
6429 eltpos = tree_to_uhwi (ce->index);
6430 else
6431 eltpos = i;
6433 if (vector)
6435 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6436 elements. */
6437 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6438 RTVEC_ELT (vector, eltpos)
6439 = expand_normal (value);
6441 else
6443 enum machine_mode value_mode =
6444 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6445 ? TYPE_MODE (TREE_TYPE (value))
6446 : eltmode;
6447 bitpos = eltpos * elt_size;
6448 store_constructor_field (target, bitsize, bitpos, value_mode,
6449 value, cleared, alias);
6453 if (vector)
6454 emit_insn (GEN_FCN (icode)
6455 (target,
6456 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6457 break;
6460 default:
6461 gcc_unreachable ();
6465 /* Store the value of EXP (an expression tree)
6466 into a subfield of TARGET which has mode MODE and occupies
6467 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6468 If MODE is VOIDmode, it means that we are storing into a bit-field.
6470 BITREGION_START is bitpos of the first bitfield in this region.
6471 BITREGION_END is the bitpos of the ending bitfield in this region.
6472 These two fields are 0, if the C++ memory model does not apply,
6473 or we are not interested in keeping track of bitfield regions.
6475 Always return const0_rtx unless we have something particular to
6476 return.
6478 ALIAS_SET is the alias set for the destination. This value will
6479 (in general) be different from that for TARGET, since TARGET is a
6480 reference to the containing structure.
6482 If NONTEMPORAL is true, try generating a nontemporal store. */
6484 static rtx
6485 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6486 unsigned HOST_WIDE_INT bitregion_start,
6487 unsigned HOST_WIDE_INT bitregion_end,
6488 enum machine_mode mode, tree exp,
6489 alias_set_type alias_set, bool nontemporal)
6491 if (TREE_CODE (exp) == ERROR_MARK)
6492 return const0_rtx;
6494 /* If we have nothing to store, do nothing unless the expression has
6495 side-effects. */
6496 if (bitsize == 0)
6497 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6499 if (GET_CODE (target) == CONCAT)
6501 /* We're storing into a struct containing a single __complex. */
6503 gcc_assert (!bitpos);
6504 return store_expr (exp, target, 0, nontemporal);
6507 /* If the structure is in a register or if the component
6508 is a bit field, we cannot use addressing to access it.
6509 Use bit-field techniques or SUBREG to store in it. */
6511 if (mode == VOIDmode
6512 || (mode != BLKmode && ! direct_store[(int) mode]
6513 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6514 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6515 || REG_P (target)
6516 || GET_CODE (target) == SUBREG
6517 /* If the field isn't aligned enough to store as an ordinary memref,
6518 store it as a bit field. */
6519 || (mode != BLKmode
6520 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6521 || bitpos % GET_MODE_ALIGNMENT (mode))
6522 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6523 || (bitpos % BITS_PER_UNIT != 0)))
6524 || (bitsize >= 0 && mode != BLKmode
6525 && GET_MODE_BITSIZE (mode) > bitsize)
6526 /* If the RHS and field are a constant size and the size of the
6527 RHS isn't the same size as the bitfield, we must use bitfield
6528 operations. */
6529 || (bitsize >= 0
6530 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6531 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6532 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6533 decl we must use bitfield operations. */
6534 || (bitsize >= 0
6535 && TREE_CODE (exp) == MEM_REF
6536 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6537 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6538 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6539 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6541 rtx temp;
6542 gimple nop_def;
6544 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6545 implies a mask operation. If the precision is the same size as
6546 the field we're storing into, that mask is redundant. This is
6547 particularly common with bit field assignments generated by the
6548 C front end. */
6549 nop_def = get_def_for_expr (exp, NOP_EXPR);
6550 if (nop_def)
6552 tree type = TREE_TYPE (exp);
6553 if (INTEGRAL_TYPE_P (type)
6554 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6555 && bitsize == TYPE_PRECISION (type))
6557 tree op = gimple_assign_rhs1 (nop_def);
6558 type = TREE_TYPE (op);
6559 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6560 exp = op;
6564 temp = expand_normal (exp);
6566 /* If BITSIZE is narrower than the size of the type of EXP
6567 we will be narrowing TEMP. Normally, what's wanted are the
6568 low-order bits. However, if EXP's type is a record and this is
6569 big-endian machine, we want the upper BITSIZE bits. */
6570 if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6571 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6572 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6573 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6574 GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6575 NULL_RTX, 1);
6577 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6578 if (mode != VOIDmode && mode != BLKmode
6579 && mode != TYPE_MODE (TREE_TYPE (exp)))
6580 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6582 /* If the modes of TEMP and TARGET are both BLKmode, both
6583 must be in memory and BITPOS must be aligned on a byte
6584 boundary. If so, we simply do a block copy. Likewise
6585 for a BLKmode-like TARGET. */
6586 if (GET_MODE (temp) == BLKmode
6587 && (GET_MODE (target) == BLKmode
6588 || (MEM_P (target)
6589 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6590 && (bitpos % BITS_PER_UNIT) == 0
6591 && (bitsize % BITS_PER_UNIT) == 0)))
6593 gcc_assert (MEM_P (target) && MEM_P (temp)
6594 && (bitpos % BITS_PER_UNIT) == 0);
6596 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6597 emit_block_move (target, temp,
6598 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6599 / BITS_PER_UNIT),
6600 BLOCK_OP_NORMAL);
6602 return const0_rtx;
6605 /* Handle calls that return values in multiple non-contiguous locations.
6606 The Irix 6 ABI has examples of this. */
6607 if (GET_CODE (temp) == PARALLEL)
6609 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6610 rtx temp_target;
6611 if (mode == BLKmode || mode == VOIDmode)
6612 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6613 temp_target = gen_reg_rtx (mode);
6614 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6615 temp = temp_target;
6617 else if (mode == BLKmode)
6619 /* Handle calls that return BLKmode values in registers. */
6620 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6622 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6623 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6624 temp = temp_target;
6626 else
6628 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6629 rtx temp_target;
6630 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6631 temp_target = gen_reg_rtx (mode);
6632 temp_target
6633 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6634 temp_target, mode, mode);
6635 temp = temp_target;
6639 /* Store the value in the bitfield. */
6640 store_bit_field (target, bitsize, bitpos,
6641 bitregion_start, bitregion_end,
6642 mode, temp);
6644 return const0_rtx;
6646 else
6648 /* Now build a reference to just the desired component. */
6649 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6651 if (to_rtx == target)
6652 to_rtx = copy_rtx (to_rtx);
6654 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6655 set_mem_alias_set (to_rtx, alias_set);
6657 return store_expr (exp, to_rtx, 0, nontemporal);
6661 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6662 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6663 codes and find the ultimate containing object, which we return.
6665 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6666 bit position, and *PUNSIGNEDP to the signedness of the field.
6667 If the position of the field is variable, we store a tree
6668 giving the variable offset (in units) in *POFFSET.
6669 This offset is in addition to the bit position.
6670 If the position is not variable, we store 0 in *POFFSET.
6672 If any of the extraction expressions is volatile,
6673 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6675 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6676 Otherwise, it is a mode that can be used to access the field.
6678 If the field describes a variable-sized object, *PMODE is set to
6679 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6680 this case, but the address of the object can be found.
6682 If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6683 look through nodes that serve as markers of a greater alignment than
6684 the one that can be deduced from the expression. These nodes make it
6685 possible for front-ends to prevent temporaries from being created by
6686 the middle-end on alignment considerations. For that purpose, the
6687 normal operating mode at high-level is to always pass FALSE so that
6688 the ultimate containing object is really returned; moreover, the
6689 associated predicate handled_component_p will always return TRUE
6690 on these nodes, thus indicating that they are essentially handled
6691 by get_inner_reference. TRUE should only be passed when the caller
6692 is scanning the expression in order to build another representation
6693 and specifically knows how to handle these nodes; as such, this is
6694 the normal operating mode in the RTL expanders. */
6696 tree
6697 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6698 HOST_WIDE_INT *pbitpos, tree *poffset,
6699 enum machine_mode *pmode, int *punsignedp,
6700 int *pvolatilep, bool keep_aligning)
6702 tree size_tree = 0;
6703 enum machine_mode mode = VOIDmode;
6704 bool blkmode_bitfield = false;
6705 tree offset = size_zero_node;
6706 offset_int bit_offset = 0;
6708 /* First get the mode, signedness, and size. We do this from just the
6709 outermost expression. */
6710 *pbitsize = -1;
6711 if (TREE_CODE (exp) == COMPONENT_REF)
6713 tree field = TREE_OPERAND (exp, 1);
6714 size_tree = DECL_SIZE (field);
6715 if (flag_strict_volatile_bitfields > 0
6716 && TREE_THIS_VOLATILE (exp)
6717 && DECL_BIT_FIELD_TYPE (field)
6718 && DECL_MODE (field) != BLKmode)
6719 /* Volatile bitfields should be accessed in the mode of the
6720 field's type, not the mode computed based on the bit
6721 size. */
6722 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6723 else if (!DECL_BIT_FIELD (field))
6724 mode = DECL_MODE (field);
6725 else if (DECL_MODE (field) == BLKmode)
6726 blkmode_bitfield = true;
6728 *punsignedp = DECL_UNSIGNED (field);
6730 else if (TREE_CODE (exp) == BIT_FIELD_REF)
6732 size_tree = TREE_OPERAND (exp, 1);
6733 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6734 || TYPE_UNSIGNED (TREE_TYPE (exp)));
6736 /* For vector types, with the correct size of access, use the mode of
6737 inner type. */
6738 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6739 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6740 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6741 mode = TYPE_MODE (TREE_TYPE (exp));
6743 else
6745 mode = TYPE_MODE (TREE_TYPE (exp));
6746 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6748 if (mode == BLKmode)
6749 size_tree = TYPE_SIZE (TREE_TYPE (exp));
6750 else
6751 *pbitsize = GET_MODE_BITSIZE (mode);
6754 if (size_tree != 0)
6756 if (! tree_fits_uhwi_p (size_tree))
6757 mode = BLKmode, *pbitsize = -1;
6758 else
6759 *pbitsize = tree_to_uhwi (size_tree);
6762 /* Compute cumulative bit-offset for nested component-refs and array-refs,
6763 and find the ultimate containing object. */
6764 while (1)
6766 switch (TREE_CODE (exp))
6768 case BIT_FIELD_REF:
6769 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
6770 break;
6772 case COMPONENT_REF:
6774 tree field = TREE_OPERAND (exp, 1);
6775 tree this_offset = component_ref_field_offset (exp);
6777 /* If this field hasn't been filled in yet, don't go past it.
6778 This should only happen when folding expressions made during
6779 type construction. */
6780 if (this_offset == 0)
6781 break;
6783 offset = size_binop (PLUS_EXPR, offset, this_offset);
6784 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
6786 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
6788 break;
6790 case ARRAY_REF:
6791 case ARRAY_RANGE_REF:
6793 tree index = TREE_OPERAND (exp, 1);
6794 tree low_bound = array_ref_low_bound (exp);
6795 tree unit_size = array_ref_element_size (exp);
6797 /* We assume all arrays have sizes that are a multiple of a byte.
6798 First subtract the lower bound, if any, in the type of the
6799 index, then convert to sizetype and multiply by the size of
6800 the array element. */
6801 if (! integer_zerop (low_bound))
6802 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6803 index, low_bound);
6805 offset = size_binop (PLUS_EXPR, offset,
6806 size_binop (MULT_EXPR,
6807 fold_convert (sizetype, index),
6808 unit_size));
6810 break;
6812 case REALPART_EXPR:
6813 break;
6815 case IMAGPART_EXPR:
6816 bit_offset += *pbitsize;
6817 break;
6819 case VIEW_CONVERT_EXPR:
6820 if (keep_aligning && STRICT_ALIGNMENT
6821 && (TYPE_ALIGN (TREE_TYPE (exp))
6822 > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6823 && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6824 < BIGGEST_ALIGNMENT)
6825 && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6826 || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6827 goto done;
6828 break;
6830 case MEM_REF:
6831 /* Hand back the decl for MEM[&decl, off]. */
6832 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6834 tree off = TREE_OPERAND (exp, 1);
6835 if (!integer_zerop (off))
6837 offset_int boff, coff = mem_ref_offset (exp);
6838 boff = wi::lshift (coff, LOG2_BITS_PER_UNIT);
6839 bit_offset += boff;
6841 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6843 goto done;
6845 default:
6846 goto done;
6849 /* If any reference in the chain is volatile, the effect is volatile. */
6850 if (TREE_THIS_VOLATILE (exp))
6851 *pvolatilep = 1;
6853 exp = TREE_OPERAND (exp, 0);
6855 done:
6857 /* If OFFSET is constant, see if we can return the whole thing as a
6858 constant bit position. Make sure to handle overflow during
6859 this conversion. */
6860 if (TREE_CODE (offset) == INTEGER_CST)
6862 offset_int tem = wi::sext (wi::to_offset (offset),
6863 TYPE_PRECISION (sizetype));
6864 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
6865 tem += bit_offset;
6866 if (wi::fits_shwi_p (tem))
6868 *pbitpos = tem.to_shwi ();
6869 *poffset = offset = NULL_TREE;
6873 /* Otherwise, split it up. */
6874 if (offset)
6876 /* Avoid returning a negative bitpos as this may wreak havoc later. */
6877 if (wi::neg_p (bit_offset))
6879 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
6880 offset_int tem = bit_offset.and_not (mask);
6881 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6882 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
6883 bit_offset -= tem;
6884 tem = wi::arshift (tem, LOG2_BITS_PER_UNIT);
6885 offset = size_binop (PLUS_EXPR, offset,
6886 wide_int_to_tree (sizetype, tem));
6889 *pbitpos = bit_offset.to_shwi ();
6890 *poffset = offset;
6893 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
6894 if (mode == VOIDmode
6895 && blkmode_bitfield
6896 && (*pbitpos % BITS_PER_UNIT) == 0
6897 && (*pbitsize % BITS_PER_UNIT) == 0)
6898 *pmode = BLKmode;
6899 else
6900 *pmode = mode;
6902 return exp;
6905 /* Return a tree of sizetype representing the size, in bytes, of the element
6906 of EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6908 tree
6909 array_ref_element_size (tree exp)
6911 tree aligned_size = TREE_OPERAND (exp, 3);
6912 tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6913 location_t loc = EXPR_LOCATION (exp);
6915 /* If a size was specified in the ARRAY_REF, it's the size measured
6916 in alignment units of the element type. So multiply by that value. */
6917 if (aligned_size)
6919 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6920 sizetype from another type of the same width and signedness. */
6921 if (TREE_TYPE (aligned_size) != sizetype)
6922 aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6923 return size_binop_loc (loc, MULT_EXPR, aligned_size,
6924 size_int (TYPE_ALIGN_UNIT (elmt_type)));
6927 /* Otherwise, take the size from that of the element type. Substitute
6928 any PLACEHOLDER_EXPR that we have. */
6929 else
6930 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6933 /* Return a tree representing the lower bound of the array mentioned in
6934 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6936 tree
6937 array_ref_low_bound (tree exp)
6939 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6941 /* If a lower bound is specified in EXP, use it. */
6942 if (TREE_OPERAND (exp, 2))
6943 return TREE_OPERAND (exp, 2);
6945 /* Otherwise, if there is a domain type and it has a lower bound, use it,
6946 substituting for a PLACEHOLDER_EXPR as needed. */
6947 if (domain_type && TYPE_MIN_VALUE (domain_type))
6948 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6950 /* Otherwise, return a zero of the appropriate type. */
6951 return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
6954 /* Returns true if REF is an array reference to an array at the end of
6955 a structure. If this is the case, the array may be allocated larger
6956 than its upper bound implies. */
6958 bool
6959 array_at_struct_end_p (tree ref)
6961 if (TREE_CODE (ref) != ARRAY_REF
6962 && TREE_CODE (ref) != ARRAY_RANGE_REF)
6963 return false;
6965 while (handled_component_p (ref))
6967 /* If the reference chain contains a component reference to a
6968 non-union type and there follows another field the reference
6969 is not at the end of a structure. */
6970 if (TREE_CODE (ref) == COMPONENT_REF
6971 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
6973 tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
6974 while (nextf && TREE_CODE (nextf) != FIELD_DECL)
6975 nextf = DECL_CHAIN (nextf);
6976 if (nextf)
6977 return false;
6980 ref = TREE_OPERAND (ref, 0);
6983 /* If the reference is based on a declared entity, the size of the array
6984 is constrained by its given domain. */
6985 if (DECL_P (ref))
6986 return false;
6988 return true;
6991 /* Return a tree representing the upper bound of the array mentioned in
6992 EXP, an ARRAY_REF or an ARRAY_RANGE_REF. */
6994 tree
6995 array_ref_up_bound (tree exp)
6997 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6999 /* If there is a domain type and it has an upper bound, use it, substituting
7000 for a PLACEHOLDER_EXPR as needed. */
7001 if (domain_type && TYPE_MAX_VALUE (domain_type))
7002 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
7004 /* Otherwise fail. */
7005 return NULL_TREE;
7008 /* Return a tree representing the offset, in bytes, of the field referenced
7009 by EXP. This does not include any offset in DECL_FIELD_BIT_OFFSET. */
7011 tree
7012 component_ref_field_offset (tree exp)
7014 tree aligned_offset = TREE_OPERAND (exp, 2);
7015 tree field = TREE_OPERAND (exp, 1);
7016 location_t loc = EXPR_LOCATION (exp);
7018 /* If an offset was specified in the COMPONENT_REF, it's the offset measured
7019 in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT. So multiply by that
7020 value. */
7021 if (aligned_offset)
7023 /* ??? tree_ssa_useless_type_conversion will eliminate casts to
7024 sizetype from another type of the same width and signedness. */
7025 if (TREE_TYPE (aligned_offset) != sizetype)
7026 aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
7027 return size_binop_loc (loc, MULT_EXPR, aligned_offset,
7028 size_int (DECL_OFFSET_ALIGN (field)
7029 / BITS_PER_UNIT));
7032 /* Otherwise, take the offset from that of the field. Substitute
7033 any PLACEHOLDER_EXPR that we have. */
7034 else
7035 return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
7038 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7040 static unsigned HOST_WIDE_INT
7041 target_align (const_tree target)
7043 /* We might have a chain of nested references with intermediate misaligning
7044 bitfields components, so need to recurse to find out. */
7046 unsigned HOST_WIDE_INT this_align, outer_align;
7048 switch (TREE_CODE (target))
7050 case BIT_FIELD_REF:
7051 return 1;
7053 case COMPONENT_REF:
7054 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7055 outer_align = target_align (TREE_OPERAND (target, 0));
7056 return MIN (this_align, outer_align);
7058 case ARRAY_REF:
7059 case ARRAY_RANGE_REF:
7060 this_align = TYPE_ALIGN (TREE_TYPE (target));
7061 outer_align = target_align (TREE_OPERAND (target, 0));
7062 return MIN (this_align, outer_align);
7064 CASE_CONVERT:
7065 case NON_LVALUE_EXPR:
7066 case VIEW_CONVERT_EXPR:
7067 this_align = TYPE_ALIGN (TREE_TYPE (target));
7068 outer_align = target_align (TREE_OPERAND (target, 0));
7069 return MAX (this_align, outer_align);
7071 default:
7072 return TYPE_ALIGN (TREE_TYPE (target));
7077 /* Given an rtx VALUE that may contain additions and multiplications, return
7078 an equivalent value that just refers to a register, memory, or constant.
7079 This is done by generating instructions to perform the arithmetic and
7080 returning a pseudo-register containing the value.
7082 The returned value may be a REG, SUBREG, MEM or constant. */
7085 force_operand (rtx value, rtx target)
7087 rtx op1, op2;
7088 /* Use subtarget as the target for operand 0 of a binary operation. */
7089 rtx subtarget = get_subtarget (target);
7090 enum rtx_code code = GET_CODE (value);
7092 /* Check for subreg applied to an expression produced by loop optimizer. */
7093 if (code == SUBREG
7094 && !REG_P (SUBREG_REG (value))
7095 && !MEM_P (SUBREG_REG (value)))
7097 value
7098 = simplify_gen_subreg (GET_MODE (value),
7099 force_reg (GET_MODE (SUBREG_REG (value)),
7100 force_operand (SUBREG_REG (value),
7101 NULL_RTX)),
7102 GET_MODE (SUBREG_REG (value)),
7103 SUBREG_BYTE (value));
7104 code = GET_CODE (value);
7107 /* Check for a PIC address load. */
7108 if ((code == PLUS || code == MINUS)
7109 && XEXP (value, 0) == pic_offset_table_rtx
7110 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7111 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7112 || GET_CODE (XEXP (value, 1)) == CONST))
7114 if (!subtarget)
7115 subtarget = gen_reg_rtx (GET_MODE (value));
7116 emit_move_insn (subtarget, value);
7117 return subtarget;
7120 if (ARITHMETIC_P (value))
7122 op2 = XEXP (value, 1);
7123 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7124 subtarget = 0;
7125 if (code == MINUS && CONST_INT_P (op2))
7127 code = PLUS;
7128 op2 = negate_rtx (GET_MODE (value), op2);
7131 /* Check for an addition with OP2 a constant integer and our first
7132 operand a PLUS of a virtual register and something else. In that
7133 case, we want to emit the sum of the virtual register and the
7134 constant first and then add the other value. This allows virtual
7135 register instantiation to simply modify the constant rather than
7136 creating another one around this addition. */
7137 if (code == PLUS && CONST_INT_P (op2)
7138 && GET_CODE (XEXP (value, 0)) == PLUS
7139 && REG_P (XEXP (XEXP (value, 0), 0))
7140 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7141 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7143 rtx temp = expand_simple_binop (GET_MODE (value), code,
7144 XEXP (XEXP (value, 0), 0), op2,
7145 subtarget, 0, OPTAB_LIB_WIDEN);
7146 return expand_simple_binop (GET_MODE (value), code, temp,
7147 force_operand (XEXP (XEXP (value,
7148 0), 1), 0),
7149 target, 0, OPTAB_LIB_WIDEN);
7152 op1 = force_operand (XEXP (value, 0), subtarget);
7153 op2 = force_operand (op2, NULL_RTX);
7154 switch (code)
7156 case MULT:
7157 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7158 case DIV:
7159 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7160 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7161 target, 1, OPTAB_LIB_WIDEN);
7162 else
7163 return expand_divmod (0,
7164 FLOAT_MODE_P (GET_MODE (value))
7165 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7166 GET_MODE (value), op1, op2, target, 0);
7167 case MOD:
7168 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7169 target, 0);
7170 case UDIV:
7171 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7172 target, 1);
7173 case UMOD:
7174 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7175 target, 1);
7176 case ASHIFTRT:
7177 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7178 target, 0, OPTAB_LIB_WIDEN);
7179 default:
7180 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7181 target, 1, OPTAB_LIB_WIDEN);
7184 if (UNARY_P (value))
7186 if (!target)
7187 target = gen_reg_rtx (GET_MODE (value));
7188 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7189 switch (code)
7191 case ZERO_EXTEND:
7192 case SIGN_EXTEND:
7193 case TRUNCATE:
7194 case FLOAT_EXTEND:
7195 case FLOAT_TRUNCATE:
7196 convert_move (target, op1, code == ZERO_EXTEND);
7197 return target;
7199 case FIX:
7200 case UNSIGNED_FIX:
7201 expand_fix (target, op1, code == UNSIGNED_FIX);
7202 return target;
7204 case FLOAT:
7205 case UNSIGNED_FLOAT:
7206 expand_float (target, op1, code == UNSIGNED_FLOAT);
7207 return target;
7209 default:
7210 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7214 #ifdef INSN_SCHEDULING
7215 /* On machines that have insn scheduling, we want all memory reference to be
7216 explicit, so we need to deal with such paradoxical SUBREGs. */
7217 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7218 value
7219 = simplify_gen_subreg (GET_MODE (value),
7220 force_reg (GET_MODE (SUBREG_REG (value)),
7221 force_operand (SUBREG_REG (value),
7222 NULL_RTX)),
7223 GET_MODE (SUBREG_REG (value)),
7224 SUBREG_BYTE (value));
7225 #endif
7227 return value;
7230 /* Subroutine of expand_expr: return nonzero iff there is no way that
7231 EXP can reference X, which is being modified. TOP_P is nonzero if this
7232 call is going to be used to determine whether we need a temporary
7233 for EXP, as opposed to a recursive call to this function.
7235 It is always safe for this routine to return zero since it merely
7236 searches for optimization opportunities. */
7239 safe_from_p (const_rtx x, tree exp, int top_p)
7241 rtx exp_rtl = 0;
7242 int i, nops;
7244 if (x == 0
7245 /* If EXP has varying size, we MUST use a target since we currently
7246 have no way of allocating temporaries of variable size
7247 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7248 So we assume here that something at a higher level has prevented a
7249 clash. This is somewhat bogus, but the best we can do. Only
7250 do this when X is BLKmode and when we are at the top level. */
7251 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7252 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7253 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7254 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7255 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7256 != INTEGER_CST)
7257 && GET_MODE (x) == BLKmode)
7258 /* If X is in the outgoing argument area, it is always safe. */
7259 || (MEM_P (x)
7260 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7261 || (GET_CODE (XEXP (x, 0)) == PLUS
7262 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7263 return 1;
7265 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7266 find the underlying pseudo. */
7267 if (GET_CODE (x) == SUBREG)
7269 x = SUBREG_REG (x);
7270 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7271 return 0;
7274 /* Now look at our tree code and possibly recurse. */
7275 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7277 case tcc_declaration:
7278 exp_rtl = DECL_RTL_IF_SET (exp);
7279 break;
7281 case tcc_constant:
7282 return 1;
7284 case tcc_exceptional:
7285 if (TREE_CODE (exp) == TREE_LIST)
7287 while (1)
7289 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7290 return 0;
7291 exp = TREE_CHAIN (exp);
7292 if (!exp)
7293 return 1;
7294 if (TREE_CODE (exp) != TREE_LIST)
7295 return safe_from_p (x, exp, 0);
7298 else if (TREE_CODE (exp) == CONSTRUCTOR)
7300 constructor_elt *ce;
7301 unsigned HOST_WIDE_INT idx;
7303 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7304 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7305 || !safe_from_p (x, ce->value, 0))
7306 return 0;
7307 return 1;
7309 else if (TREE_CODE (exp) == ERROR_MARK)
7310 return 1; /* An already-visited SAVE_EXPR? */
7311 else
7312 return 0;
7314 case tcc_statement:
7315 /* The only case we look at here is the DECL_INITIAL inside a
7316 DECL_EXPR. */
7317 return (TREE_CODE (exp) != DECL_EXPR
7318 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7319 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7320 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7322 case tcc_binary:
7323 case tcc_comparison:
7324 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7325 return 0;
7326 /* Fall through. */
7328 case tcc_unary:
7329 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7331 case tcc_expression:
7332 case tcc_reference:
7333 case tcc_vl_exp:
7334 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7335 the expression. If it is set, we conflict iff we are that rtx or
7336 both are in memory. Otherwise, we check all operands of the
7337 expression recursively. */
7339 switch (TREE_CODE (exp))
7341 case ADDR_EXPR:
7342 /* If the operand is static or we are static, we can't conflict.
7343 Likewise if we don't conflict with the operand at all. */
7344 if (staticp (TREE_OPERAND (exp, 0))
7345 || TREE_STATIC (exp)
7346 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7347 return 1;
7349 /* Otherwise, the only way this can conflict is if we are taking
7350 the address of a DECL a that address if part of X, which is
7351 very rare. */
7352 exp = TREE_OPERAND (exp, 0);
7353 if (DECL_P (exp))
7355 if (!DECL_RTL_SET_P (exp)
7356 || !MEM_P (DECL_RTL (exp)))
7357 return 0;
7358 else
7359 exp_rtl = XEXP (DECL_RTL (exp), 0);
7361 break;
7363 case MEM_REF:
7364 if (MEM_P (x)
7365 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7366 get_alias_set (exp)))
7367 return 0;
7368 break;
7370 case CALL_EXPR:
7371 /* Assume that the call will clobber all hard registers and
7372 all of memory. */
7373 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7374 || MEM_P (x))
7375 return 0;
7376 break;
7378 case WITH_CLEANUP_EXPR:
7379 case CLEANUP_POINT_EXPR:
7380 /* Lowered by gimplify.c. */
7381 gcc_unreachable ();
7383 case SAVE_EXPR:
7384 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7386 default:
7387 break;
7390 /* If we have an rtx, we do not need to scan our operands. */
7391 if (exp_rtl)
7392 break;
7394 nops = TREE_OPERAND_LENGTH (exp);
7395 for (i = 0; i < nops; i++)
7396 if (TREE_OPERAND (exp, i) != 0
7397 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7398 return 0;
7400 break;
7402 case tcc_type:
7403 /* Should never get a type here. */
7404 gcc_unreachable ();
7407 /* If we have an rtl, find any enclosed object. Then see if we conflict
7408 with it. */
7409 if (exp_rtl)
7411 if (GET_CODE (exp_rtl) == SUBREG)
7413 exp_rtl = SUBREG_REG (exp_rtl);
7414 if (REG_P (exp_rtl)
7415 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7416 return 0;
7419 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7420 are memory and they conflict. */
7421 return ! (rtx_equal_p (x, exp_rtl)
7422 || (MEM_P (x) && MEM_P (exp_rtl)
7423 && true_dependence (exp_rtl, VOIDmode, x)));
7426 /* If we reach here, it is safe. */
7427 return 1;
7431 /* Return the highest power of two that EXP is known to be a multiple of.
7432 This is used in updating alignment of MEMs in array references. */
7434 unsigned HOST_WIDE_INT
7435 highest_pow2_factor (const_tree exp)
7437 unsigned HOST_WIDE_INT ret;
7438 int trailing_zeros = tree_ctz (exp);
7439 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7440 return BIGGEST_ALIGNMENT;
7441 ret = (unsigned HOST_WIDE_INT) 1 << trailing_zeros;
7442 if (ret > BIGGEST_ALIGNMENT)
7443 return BIGGEST_ALIGNMENT;
7444 return ret;
7447 /* Similar, except that the alignment requirements of TARGET are
7448 taken into account. Assume it is at least as aligned as its
7449 type, unless it is a COMPONENT_REF in which case the layout of
7450 the structure gives the alignment. */
7452 static unsigned HOST_WIDE_INT
7453 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7455 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7456 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7458 return MAX (factor, talign);
7461 #ifdef HAVE_conditional_move
7462 /* Convert the tree comparison code TCODE to the rtl one where the
7463 signedness is UNSIGNEDP. */
7465 static enum rtx_code
7466 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7468 enum rtx_code code;
7469 switch (tcode)
7471 case EQ_EXPR:
7472 code = EQ;
7473 break;
7474 case NE_EXPR:
7475 code = NE;
7476 break;
7477 case LT_EXPR:
7478 code = unsignedp ? LTU : LT;
7479 break;
7480 case LE_EXPR:
7481 code = unsignedp ? LEU : LE;
7482 break;
7483 case GT_EXPR:
7484 code = unsignedp ? GTU : GT;
7485 break;
7486 case GE_EXPR:
7487 code = unsignedp ? GEU : GE;
7488 break;
7489 case UNORDERED_EXPR:
7490 code = UNORDERED;
7491 break;
7492 case ORDERED_EXPR:
7493 code = ORDERED;
7494 break;
7495 case UNLT_EXPR:
7496 code = UNLT;
7497 break;
7498 case UNLE_EXPR:
7499 code = UNLE;
7500 break;
7501 case UNGT_EXPR:
7502 code = UNGT;
7503 break;
7504 case UNGE_EXPR:
7505 code = UNGE;
7506 break;
7507 case UNEQ_EXPR:
7508 code = UNEQ;
7509 break;
7510 case LTGT_EXPR:
7511 code = LTGT;
7512 break;
7514 default:
7515 gcc_unreachable ();
7517 return code;
7519 #endif
7521 /* Subroutine of expand_expr. Expand the two operands of a binary
7522 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7523 The value may be stored in TARGET if TARGET is nonzero. The
7524 MODIFIER argument is as documented by expand_expr. */
7526 static void
7527 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7528 enum expand_modifier modifier)
7530 if (! safe_from_p (target, exp1, 1))
7531 target = 0;
7532 if (operand_equal_p (exp0, exp1, 0))
7534 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7535 *op1 = copy_rtx (*op0);
7537 else
7539 /* If we need to preserve evaluation order, copy exp0 into its own
7540 temporary variable so that it can't be clobbered by exp1. */
7541 if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7542 exp0 = save_expr (exp0);
7543 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7544 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7549 /* Return a MEM that contains constant EXP. DEFER is as for
7550 output_constant_def and MODIFIER is as for expand_expr. */
7552 static rtx
7553 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7555 rtx mem;
7557 mem = output_constant_def (exp, defer);
7558 if (modifier != EXPAND_INITIALIZER)
7559 mem = use_anchored_address (mem);
7560 return mem;
7563 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7564 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7566 static rtx
7567 expand_expr_addr_expr_1 (tree exp, rtx target, enum machine_mode tmode,
7568 enum expand_modifier modifier, addr_space_t as)
7570 rtx result, subtarget;
7571 tree inner, offset;
7572 HOST_WIDE_INT bitsize, bitpos;
7573 int volatilep, unsignedp;
7574 enum machine_mode mode1;
7576 /* If we are taking the address of a constant and are at the top level,
7577 we have to use output_constant_def since we can't call force_const_mem
7578 at top level. */
7579 /* ??? This should be considered a front-end bug. We should not be
7580 generating ADDR_EXPR of something that isn't an LVALUE. The only
7581 exception here is STRING_CST. */
7582 if (CONSTANT_CLASS_P (exp))
7584 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7585 if (modifier < EXPAND_SUM)
7586 result = force_operand (result, target);
7587 return result;
7590 /* Everything must be something allowed by is_gimple_addressable. */
7591 switch (TREE_CODE (exp))
7593 case INDIRECT_REF:
7594 /* This case will happen via recursion for &a->b. */
7595 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7597 case MEM_REF:
7599 tree tem = TREE_OPERAND (exp, 0);
7600 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7601 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7602 return expand_expr (tem, target, tmode, modifier);
7605 case CONST_DECL:
7606 /* Expand the initializer like constants above. */
7607 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7608 0, modifier), 0);
7609 if (modifier < EXPAND_SUM)
7610 result = force_operand (result, target);
7611 return result;
7613 case REALPART_EXPR:
7614 /* The real part of the complex number is always first, therefore
7615 the address is the same as the address of the parent object. */
7616 offset = 0;
7617 bitpos = 0;
7618 inner = TREE_OPERAND (exp, 0);
7619 break;
7621 case IMAGPART_EXPR:
7622 /* The imaginary part of the complex number is always second.
7623 The expression is therefore always offset by the size of the
7624 scalar type. */
7625 offset = 0;
7626 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7627 inner = TREE_OPERAND (exp, 0);
7628 break;
7630 case COMPOUND_LITERAL_EXPR:
7631 /* Allow COMPOUND_LITERAL_EXPR in initializers, if e.g.
7632 rtl_for_decl_init is called on DECL_INITIAL with
7633 COMPOUNT_LITERAL_EXPRs in it, they aren't gimplified. */
7634 if (modifier == EXPAND_INITIALIZER
7635 && COMPOUND_LITERAL_EXPR_DECL (exp))
7636 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7637 target, tmode, modifier, as);
7638 /* FALLTHRU */
7639 default:
7640 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7641 expand_expr, as that can have various side effects; LABEL_DECLs for
7642 example, may not have their DECL_RTL set yet. Expand the rtl of
7643 CONSTRUCTORs too, which should yield a memory reference for the
7644 constructor's contents. Assume language specific tree nodes can
7645 be expanded in some interesting way. */
7646 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7647 if (DECL_P (exp)
7648 || TREE_CODE (exp) == CONSTRUCTOR
7649 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7651 result = expand_expr (exp, target, tmode,
7652 modifier == EXPAND_INITIALIZER
7653 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7655 /* If the DECL isn't in memory, then the DECL wasn't properly
7656 marked TREE_ADDRESSABLE, which will be either a front-end
7657 or a tree optimizer bug. */
7659 if (TREE_ADDRESSABLE (exp)
7660 && ! MEM_P (result)
7661 && ! targetm.calls.allocate_stack_slots_for_args ())
7663 error ("local frame unavailable (naked function?)");
7664 return result;
7666 else
7667 gcc_assert (MEM_P (result));
7668 result = XEXP (result, 0);
7670 /* ??? Is this needed anymore? */
7671 if (DECL_P (exp))
7672 TREE_USED (exp) = 1;
7674 if (modifier != EXPAND_INITIALIZER
7675 && modifier != EXPAND_CONST_ADDRESS
7676 && modifier != EXPAND_SUM)
7677 result = force_operand (result, target);
7678 return result;
7681 /* Pass FALSE as the last argument to get_inner_reference although
7682 we are expanding to RTL. The rationale is that we know how to
7683 handle "aligning nodes" here: we can just bypass them because
7684 they won't change the final object whose address will be returned
7685 (they actually exist only for that purpose). */
7686 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7687 &mode1, &unsignedp, &volatilep, false);
7688 break;
7691 /* We must have made progress. */
7692 gcc_assert (inner != exp);
7694 subtarget = offset || bitpos ? NULL_RTX : target;
7695 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7696 inner alignment, force the inner to be sufficiently aligned. */
7697 if (CONSTANT_CLASS_P (inner)
7698 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7700 inner = copy_node (inner);
7701 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7702 TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7703 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7705 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7707 if (offset)
7709 rtx tmp;
7711 if (modifier != EXPAND_NORMAL)
7712 result = force_operand (result, NULL);
7713 tmp = expand_expr (offset, NULL_RTX, tmode,
7714 modifier == EXPAND_INITIALIZER
7715 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7717 /* expand_expr is allowed to return an object in a mode other
7718 than TMODE. If it did, we need to convert. */
7719 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7720 tmp = convert_modes (tmode, GET_MODE (tmp),
7721 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7722 result = convert_memory_address_addr_space (tmode, result, as);
7723 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7725 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7726 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7727 else
7729 subtarget = bitpos ? NULL_RTX : target;
7730 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7731 1, OPTAB_LIB_WIDEN);
7735 if (bitpos)
7737 /* Someone beforehand should have rejected taking the address
7738 of such an object. */
7739 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7741 result = convert_memory_address_addr_space (tmode, result, as);
7742 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7743 if (modifier < EXPAND_SUM)
7744 result = force_operand (result, target);
7747 return result;
7750 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7751 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7753 static rtx
7754 expand_expr_addr_expr (tree exp, rtx target, enum machine_mode tmode,
7755 enum expand_modifier modifier)
7757 addr_space_t as = ADDR_SPACE_GENERIC;
7758 enum machine_mode address_mode = Pmode;
7759 enum machine_mode pointer_mode = ptr_mode;
7760 enum machine_mode rmode;
7761 rtx result;
7763 /* Target mode of VOIDmode says "whatever's natural". */
7764 if (tmode == VOIDmode)
7765 tmode = TYPE_MODE (TREE_TYPE (exp));
7767 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7769 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7770 address_mode = targetm.addr_space.address_mode (as);
7771 pointer_mode = targetm.addr_space.pointer_mode (as);
7774 /* We can get called with some Weird Things if the user does silliness
7775 like "(short) &a". In that case, convert_memory_address won't do
7776 the right thing, so ignore the given target mode. */
7777 if (tmode != address_mode && tmode != pointer_mode)
7778 tmode = address_mode;
7780 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7781 tmode, modifier, as);
7783 /* Despite expand_expr claims concerning ignoring TMODE when not
7784 strictly convenient, stuff breaks if we don't honor it. Note
7785 that combined with the above, we only do this for pointer modes. */
7786 rmode = GET_MODE (result);
7787 if (rmode == VOIDmode)
7788 rmode = tmode;
7789 if (rmode != tmode)
7790 result = convert_memory_address_addr_space (tmode, result, as);
7792 return result;
7795 /* Generate code for computing CONSTRUCTOR EXP.
7796 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7797 is TRUE, instead of creating a temporary variable in memory
7798 NULL is returned and the caller needs to handle it differently. */
7800 static rtx
7801 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7802 bool avoid_temp_mem)
7804 tree type = TREE_TYPE (exp);
7805 enum machine_mode mode = TYPE_MODE (type);
7807 /* Try to avoid creating a temporary at all. This is possible
7808 if all of the initializer is zero.
7809 FIXME: try to handle all [0..255] initializers we can handle
7810 with memset. */
7811 if (TREE_STATIC (exp)
7812 && !TREE_ADDRESSABLE (exp)
7813 && target != 0 && mode == BLKmode
7814 && all_zeros_p (exp))
7816 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7817 return target;
7820 /* All elts simple constants => refer to a constant in memory. But
7821 if this is a non-BLKmode mode, let it store a field at a time
7822 since that should make a CONST_INT, CONST_WIDE_INT or
7823 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7824 use, it is best to store directly into the target unless the type
7825 is large enough that memcpy will be used. If we are making an
7826 initializer and all operands are constant, put it in memory as
7827 well.
7829 FIXME: Avoid trying to fill vector constructors piece-meal.
7830 Output them with output_constant_def below unless we're sure
7831 they're zeros. This should go away when vector initializers
7832 are treated like VECTOR_CST instead of arrays. */
7833 if ((TREE_STATIC (exp)
7834 && ((mode == BLKmode
7835 && ! (target != 0 && safe_from_p (target, exp, 1)))
7836 || TREE_ADDRESSABLE (exp)
7837 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7838 && (! MOVE_BY_PIECES_P
7839 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7840 TYPE_ALIGN (type)))
7841 && ! mostly_zeros_p (exp))))
7842 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7843 && TREE_CONSTANT (exp)))
7845 rtx constructor;
7847 if (avoid_temp_mem)
7848 return NULL_RTX;
7850 constructor = expand_expr_constant (exp, 1, modifier);
7852 if (modifier != EXPAND_CONST_ADDRESS
7853 && modifier != EXPAND_INITIALIZER
7854 && modifier != EXPAND_SUM)
7855 constructor = validize_mem (constructor);
7857 return constructor;
7860 /* Handle calls that pass values in multiple non-contiguous
7861 locations. The Irix 6 ABI has examples of this. */
7862 if (target == 0 || ! safe_from_p (target, exp, 1)
7863 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7865 if (avoid_temp_mem)
7866 return NULL_RTX;
7868 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7871 store_constructor (exp, target, 0, int_expr_size (exp));
7872 return target;
7876 /* expand_expr: generate code for computing expression EXP.
7877 An rtx for the computed value is returned. The value is never null.
7878 In the case of a void EXP, const0_rtx is returned.
7880 The value may be stored in TARGET if TARGET is nonzero.
7881 TARGET is just a suggestion; callers must assume that
7882 the rtx returned may not be the same as TARGET.
7884 If TARGET is CONST0_RTX, it means that the value will be ignored.
7886 If TMODE is not VOIDmode, it suggests generating the
7887 result in mode TMODE. But this is done only when convenient.
7888 Otherwise, TMODE is ignored and the value generated in its natural mode.
7889 TMODE is just a suggestion; callers must assume that
7890 the rtx returned may not have mode TMODE.
7892 Note that TARGET may have neither TMODE nor MODE. In that case, it
7893 probably will not be used.
7895 If MODIFIER is EXPAND_SUM then when EXP is an addition
7896 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7897 or a nest of (PLUS ...) and (MINUS ...) where the terms are
7898 products as above, or REG or MEM, or constant.
7899 Ordinarily in such cases we would output mul or add instructions
7900 and then return a pseudo reg containing the sum.
7902 EXPAND_INITIALIZER is much like EXPAND_SUM except that
7903 it also marks a label as absolutely required (it can't be dead).
7904 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7905 This is used for outputting expressions used in initializers.
7907 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7908 with a constant address even if that address is not normally legitimate.
7909 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7911 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7912 a call parameter. Such targets require special care as we haven't yet
7913 marked TARGET so that it's safe from being trashed by libcalls. We
7914 don't want to use TARGET for anything but the final result;
7915 Intermediate values must go elsewhere. Additionally, calls to
7916 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7918 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7919 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7920 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
7921 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7922 recursively.
7924 If INNER_REFERENCE_P is true, we are expanding an inner reference.
7925 In this case, we don't adjust a returned MEM rtx that wouldn't be
7926 sufficiently aligned for its mode; instead, it's up to the caller
7927 to deal with it afterwards. This is used to make sure that unaligned
7928 base objects for which out-of-bounds accesses are supported, for
7929 example record types with trailing arrays, aren't realigned behind
7930 the back of the caller.
7931 The normal operating mode is to pass FALSE for this parameter. */
7934 expand_expr_real (tree exp, rtx target, enum machine_mode tmode,
7935 enum expand_modifier modifier, rtx *alt_rtl,
7936 bool inner_reference_p)
7938 rtx ret;
7940 /* Handle ERROR_MARK before anybody tries to access its type. */
7941 if (TREE_CODE (exp) == ERROR_MARK
7942 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7944 ret = CONST0_RTX (tmode);
7945 return ret ? ret : const0_rtx;
7948 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
7949 inner_reference_p);
7950 return ret;
7953 /* Try to expand the conditional expression which is represented by
7954 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If succeseds
7955 return the rtl reg which repsents the result. Otherwise return
7956 NULL_RTL. */
7958 static rtx
7959 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7960 tree treeop1 ATTRIBUTE_UNUSED,
7961 tree treeop2 ATTRIBUTE_UNUSED)
7963 #ifdef HAVE_conditional_move
7964 rtx insn;
7965 rtx op00, op01, op1, op2;
7966 enum rtx_code comparison_code;
7967 enum machine_mode comparison_mode;
7968 gimple srcstmt;
7969 rtx temp;
7970 tree type = TREE_TYPE (treeop1);
7971 int unsignedp = TYPE_UNSIGNED (type);
7972 enum machine_mode mode = TYPE_MODE (type);
7973 enum machine_mode orig_mode = mode;
7975 /* If we cannot do a conditional move on the mode, try doing it
7976 with the promoted mode. */
7977 if (!can_conditionally_move_p (mode))
7979 mode = promote_mode (type, mode, &unsignedp);
7980 if (!can_conditionally_move_p (mode))
7981 return NULL_RTX;
7982 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
7984 else
7985 temp = assign_temp (type, 0, 1);
7987 start_sequence ();
7988 expand_operands (treeop1, treeop2,
7989 temp, &op1, &op2, EXPAND_NORMAL);
7991 if (TREE_CODE (treeop0) == SSA_NAME
7992 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7994 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7995 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7996 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7997 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7998 comparison_mode = TYPE_MODE (type);
7999 unsignedp = TYPE_UNSIGNED (type);
8000 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8002 else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
8004 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8005 enum tree_code cmpcode = TREE_CODE (treeop0);
8006 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8007 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8008 unsignedp = TYPE_UNSIGNED (type);
8009 comparison_mode = TYPE_MODE (type);
8010 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8012 else
8014 op00 = expand_normal (treeop0);
8015 op01 = const0_rtx;
8016 comparison_code = NE;
8017 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8020 if (GET_MODE (op1) != mode)
8021 op1 = gen_lowpart (mode, op1);
8023 if (GET_MODE (op2) != mode)
8024 op2 = gen_lowpart (mode, op2);
8026 /* Try to emit the conditional move. */
8027 insn = emit_conditional_move (temp, comparison_code,
8028 op00, op01, comparison_mode,
8029 op1, op2, mode,
8030 unsignedp);
8032 /* If we could do the conditional move, emit the sequence,
8033 and return. */
8034 if (insn)
8036 rtx_insn *seq = get_insns ();
8037 end_sequence ();
8038 emit_insn (seq);
8039 return convert_modes (orig_mode, mode, temp, 0);
8042 /* Otherwise discard the sequence and fall back to code with
8043 branches. */
8044 end_sequence ();
8045 #endif
8046 return NULL_RTX;
8050 expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode,
8051 enum expand_modifier modifier)
8053 rtx op0, op1, op2, temp;
8054 tree type;
8055 int unsignedp;
8056 enum machine_mode mode;
8057 enum tree_code code = ops->code;
8058 optab this_optab;
8059 rtx subtarget, original_target;
8060 int ignore;
8061 bool reduce_bit_field;
8062 location_t loc = ops->location;
8063 tree treeop0, treeop1, treeop2;
8064 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8065 ? reduce_to_bit_field_precision ((expr), \
8066 target, \
8067 type) \
8068 : (expr))
8070 type = ops->type;
8071 mode = TYPE_MODE (type);
8072 unsignedp = TYPE_UNSIGNED (type);
8074 treeop0 = ops->op0;
8075 treeop1 = ops->op1;
8076 treeop2 = ops->op2;
8078 /* We should be called only on simple (binary or unary) expressions,
8079 exactly those that are valid in gimple expressions that aren't
8080 GIMPLE_SINGLE_RHS (or invalid). */
8081 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8082 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8083 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8085 ignore = (target == const0_rtx
8086 || ((CONVERT_EXPR_CODE_P (code)
8087 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8088 && TREE_CODE (type) == VOID_TYPE));
8090 /* We should be called only if we need the result. */
8091 gcc_assert (!ignore);
8093 /* An operation in what may be a bit-field type needs the
8094 result to be reduced to the precision of the bit-field type,
8095 which is narrower than that of the type's mode. */
8096 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8097 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8099 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8100 target = 0;
8102 /* Use subtarget as the target for operand 0 of a binary operation. */
8103 subtarget = get_subtarget (target);
8104 original_target = target;
8106 switch (code)
8108 case NON_LVALUE_EXPR:
8109 case PAREN_EXPR:
8110 CASE_CONVERT:
8111 if (treeop0 == error_mark_node)
8112 return const0_rtx;
8114 if (TREE_CODE (type) == UNION_TYPE)
8116 tree valtype = TREE_TYPE (treeop0);
8118 /* If both input and output are BLKmode, this conversion isn't doing
8119 anything except possibly changing memory attribute. */
8120 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8122 rtx result = expand_expr (treeop0, target, tmode,
8123 modifier);
8125 result = copy_rtx (result);
8126 set_mem_attributes (result, type, 0);
8127 return result;
8130 if (target == 0)
8132 if (TYPE_MODE (type) != BLKmode)
8133 target = gen_reg_rtx (TYPE_MODE (type));
8134 else
8135 target = assign_temp (type, 1, 1);
8138 if (MEM_P (target))
8139 /* Store data into beginning of memory target. */
8140 store_expr (treeop0,
8141 adjust_address (target, TYPE_MODE (valtype), 0),
8142 modifier == EXPAND_STACK_PARM,
8143 false);
8145 else
8147 gcc_assert (REG_P (target));
8149 /* Store this field into a union of the proper type. */
8150 store_field (target,
8151 MIN ((int_size_in_bytes (TREE_TYPE
8152 (treeop0))
8153 * BITS_PER_UNIT),
8154 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8155 0, 0, 0, TYPE_MODE (valtype), treeop0, 0, false);
8158 /* Return the entire union. */
8159 return target;
8162 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8164 op0 = expand_expr (treeop0, target, VOIDmode,
8165 modifier);
8167 /* If the signedness of the conversion differs and OP0 is
8168 a promoted SUBREG, clear that indication since we now
8169 have to do the proper extension. */
8170 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8171 && GET_CODE (op0) == SUBREG)
8172 SUBREG_PROMOTED_VAR_P (op0) = 0;
8174 return REDUCE_BIT_FIELD (op0);
8177 op0 = expand_expr (treeop0, NULL_RTX, mode,
8178 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8179 if (GET_MODE (op0) == mode)
8182 /* If OP0 is a constant, just convert it into the proper mode. */
8183 else if (CONSTANT_P (op0))
8185 tree inner_type = TREE_TYPE (treeop0);
8186 enum machine_mode inner_mode = GET_MODE (op0);
8188 if (inner_mode == VOIDmode)
8189 inner_mode = TYPE_MODE (inner_type);
8191 if (modifier == EXPAND_INITIALIZER)
8192 op0 = simplify_gen_subreg (mode, op0, inner_mode,
8193 subreg_lowpart_offset (mode,
8194 inner_mode));
8195 else
8196 op0= convert_modes (mode, inner_mode, op0,
8197 TYPE_UNSIGNED (inner_type));
8200 else if (modifier == EXPAND_INITIALIZER)
8201 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8203 else if (target == 0)
8204 op0 = convert_to_mode (mode, op0,
8205 TYPE_UNSIGNED (TREE_TYPE
8206 (treeop0)));
8207 else
8209 convert_move (target, op0,
8210 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8211 op0 = target;
8214 return REDUCE_BIT_FIELD (op0);
8216 case ADDR_SPACE_CONVERT_EXPR:
8218 tree treeop0_type = TREE_TYPE (treeop0);
8219 addr_space_t as_to;
8220 addr_space_t as_from;
8222 gcc_assert (POINTER_TYPE_P (type));
8223 gcc_assert (POINTER_TYPE_P (treeop0_type));
8225 as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8226 as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8228 /* Conversions between pointers to the same address space should
8229 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8230 gcc_assert (as_to != as_from);
8232 /* Ask target code to handle conversion between pointers
8233 to overlapping address spaces. */
8234 if (targetm.addr_space.subset_p (as_to, as_from)
8235 || targetm.addr_space.subset_p (as_from, as_to))
8237 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8238 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8239 gcc_assert (op0);
8240 return op0;
8243 /* For disjoint address spaces, converting anything but
8244 a null pointer invokes undefined behaviour. We simply
8245 always return a null pointer here. */
8246 return CONST0_RTX (mode);
8249 case POINTER_PLUS_EXPR:
8250 /* Even though the sizetype mode and the pointer's mode can be different
8251 expand is able to handle this correctly and get the correct result out
8252 of the PLUS_EXPR code. */
8253 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8254 if sizetype precision is smaller than pointer precision. */
8255 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8256 treeop1 = fold_convert_loc (loc, type,
8257 fold_convert_loc (loc, ssizetype,
8258 treeop1));
8259 /* If sizetype precision is larger than pointer precision, truncate the
8260 offset to have matching modes. */
8261 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8262 treeop1 = fold_convert_loc (loc, type, treeop1);
8264 case PLUS_EXPR:
8265 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8266 something else, make sure we add the register to the constant and
8267 then to the other thing. This case can occur during strength
8268 reduction and doing it this way will produce better code if the
8269 frame pointer or argument pointer is eliminated.
8271 fold-const.c will ensure that the constant is always in the inner
8272 PLUS_EXPR, so the only case we need to do anything about is if
8273 sp, ap, or fp is our second argument, in which case we must swap
8274 the innermost first argument and our second argument. */
8276 if (TREE_CODE (treeop0) == PLUS_EXPR
8277 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8278 && TREE_CODE (treeop1) == VAR_DECL
8279 && (DECL_RTL (treeop1) == frame_pointer_rtx
8280 || DECL_RTL (treeop1) == stack_pointer_rtx
8281 || DECL_RTL (treeop1) == arg_pointer_rtx))
8283 gcc_unreachable ();
8286 /* If the result is to be ptr_mode and we are adding an integer to
8287 something, we might be forming a constant. So try to use
8288 plus_constant. If it produces a sum and we can't accept it,
8289 use force_operand. This allows P = &ARR[const] to generate
8290 efficient code on machines where a SYMBOL_REF is not a valid
8291 address.
8293 If this is an EXPAND_SUM call, always return the sum. */
8294 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8295 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8297 if (modifier == EXPAND_STACK_PARM)
8298 target = 0;
8299 if (TREE_CODE (treeop0) == INTEGER_CST
8300 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8301 && TREE_CONSTANT (treeop1))
8303 rtx constant_part;
8304 HOST_WIDE_INT wc;
8305 enum machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8307 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8308 EXPAND_SUM);
8309 /* Use wi::shwi to ensure that the constant is
8310 truncated according to the mode of OP1, then sign extended
8311 to a HOST_WIDE_INT. Using the constant directly can result
8312 in non-canonical RTL in a 64x32 cross compile. */
8313 wc = TREE_INT_CST_LOW (treeop0);
8314 constant_part =
8315 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8316 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8317 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8318 op1 = force_operand (op1, target);
8319 return REDUCE_BIT_FIELD (op1);
8322 else if (TREE_CODE (treeop1) == INTEGER_CST
8323 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8324 && TREE_CONSTANT (treeop0))
8326 rtx constant_part;
8327 HOST_WIDE_INT wc;
8328 enum machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8330 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8331 (modifier == EXPAND_INITIALIZER
8332 ? EXPAND_INITIALIZER : EXPAND_SUM));
8333 if (! CONSTANT_P (op0))
8335 op1 = expand_expr (treeop1, NULL_RTX,
8336 VOIDmode, modifier);
8337 /* Return a PLUS if modifier says it's OK. */
8338 if (modifier == EXPAND_SUM
8339 || modifier == EXPAND_INITIALIZER)
8340 return simplify_gen_binary (PLUS, mode, op0, op1);
8341 goto binop2;
8343 /* Use wi::shwi to ensure that the constant is
8344 truncated according to the mode of OP1, then sign extended
8345 to a HOST_WIDE_INT. Using the constant directly can result
8346 in non-canonical RTL in a 64x32 cross compile. */
8347 wc = TREE_INT_CST_LOW (treeop1);
8348 constant_part
8349 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8350 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8351 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8352 op0 = force_operand (op0, target);
8353 return REDUCE_BIT_FIELD (op0);
8357 /* Use TER to expand pointer addition of a negated value
8358 as pointer subtraction. */
8359 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8360 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8361 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8362 && TREE_CODE (treeop1) == SSA_NAME
8363 && TYPE_MODE (TREE_TYPE (treeop0))
8364 == TYPE_MODE (TREE_TYPE (treeop1)))
8366 gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8367 if (def)
8369 treeop1 = gimple_assign_rhs1 (def);
8370 code = MINUS_EXPR;
8371 goto do_minus;
8375 /* No sense saving up arithmetic to be done
8376 if it's all in the wrong mode to form part of an address.
8377 And force_operand won't know whether to sign-extend or
8378 zero-extend. */
8379 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8380 || mode != ptr_mode)
8382 expand_operands (treeop0, treeop1,
8383 subtarget, &op0, &op1, EXPAND_NORMAL);
8384 if (op0 == const0_rtx)
8385 return op1;
8386 if (op1 == const0_rtx)
8387 return op0;
8388 goto binop2;
8391 expand_operands (treeop0, treeop1,
8392 subtarget, &op0, &op1, modifier);
8393 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8395 case MINUS_EXPR:
8396 do_minus:
8397 /* For initializers, we are allowed to return a MINUS of two
8398 symbolic constants. Here we handle all cases when both operands
8399 are constant. */
8400 /* Handle difference of two symbolic constants,
8401 for the sake of an initializer. */
8402 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8403 && really_constant_p (treeop0)
8404 && really_constant_p (treeop1))
8406 expand_operands (treeop0, treeop1,
8407 NULL_RTX, &op0, &op1, modifier);
8409 /* If the last operand is a CONST_INT, use plus_constant of
8410 the negated constant. Else make the MINUS. */
8411 if (CONST_INT_P (op1))
8412 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8413 -INTVAL (op1)));
8414 else
8415 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8418 /* No sense saving up arithmetic to be done
8419 if it's all in the wrong mode to form part of an address.
8420 And force_operand won't know whether to sign-extend or
8421 zero-extend. */
8422 if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8423 || mode != ptr_mode)
8424 goto binop;
8426 expand_operands (treeop0, treeop1,
8427 subtarget, &op0, &op1, modifier);
8429 /* Convert A - const to A + (-const). */
8430 if (CONST_INT_P (op1))
8432 op1 = negate_rtx (mode, op1);
8433 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8436 goto binop2;
8438 case WIDEN_MULT_PLUS_EXPR:
8439 case WIDEN_MULT_MINUS_EXPR:
8440 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8441 op2 = expand_normal (treeop2);
8442 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8443 target, unsignedp);
8444 return target;
8446 case WIDEN_MULT_EXPR:
8447 /* If first operand is constant, swap them.
8448 Thus the following special case checks need only
8449 check the second operand. */
8450 if (TREE_CODE (treeop0) == INTEGER_CST)
8452 tree t1 = treeop0;
8453 treeop0 = treeop1;
8454 treeop1 = t1;
8457 /* First, check if we have a multiplication of one signed and one
8458 unsigned operand. */
8459 if (TREE_CODE (treeop1) != INTEGER_CST
8460 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8461 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8463 enum machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8464 this_optab = usmul_widen_optab;
8465 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8466 != CODE_FOR_nothing)
8468 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8469 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8470 EXPAND_NORMAL);
8471 else
8472 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8473 EXPAND_NORMAL);
8474 /* op0 and op1 might still be constant, despite the above
8475 != INTEGER_CST check. Handle it. */
8476 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8478 op0 = convert_modes (innermode, mode, op0, true);
8479 op1 = convert_modes (innermode, mode, op1, false);
8480 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8481 target, unsignedp));
8483 goto binop3;
8486 /* Check for a multiplication with matching signedness. */
8487 else if ((TREE_CODE (treeop1) == INTEGER_CST
8488 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8489 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8490 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8492 tree op0type = TREE_TYPE (treeop0);
8493 enum machine_mode innermode = TYPE_MODE (op0type);
8494 bool zextend_p = TYPE_UNSIGNED (op0type);
8495 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8496 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8498 if (TREE_CODE (treeop0) != INTEGER_CST)
8500 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8501 != CODE_FOR_nothing)
8503 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8504 EXPAND_NORMAL);
8505 /* op0 and op1 might still be constant, despite the above
8506 != INTEGER_CST check. Handle it. */
8507 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8509 widen_mult_const:
8510 op0 = convert_modes (innermode, mode, op0, zextend_p);
8512 = convert_modes (innermode, mode, op1,
8513 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8514 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8515 target,
8516 unsignedp));
8518 temp = expand_widening_mult (mode, op0, op1, target,
8519 unsignedp, this_optab);
8520 return REDUCE_BIT_FIELD (temp);
8522 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8523 != CODE_FOR_nothing
8524 && innermode == word_mode)
8526 rtx htem, hipart;
8527 op0 = expand_normal (treeop0);
8528 if (TREE_CODE (treeop1) == INTEGER_CST)
8529 op1 = convert_modes (innermode, mode,
8530 expand_normal (treeop1),
8531 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8532 else
8533 op1 = expand_normal (treeop1);
8534 /* op0 and op1 might still be constant, despite the above
8535 != INTEGER_CST check. Handle it. */
8536 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8537 goto widen_mult_const;
8538 temp = expand_binop (mode, other_optab, op0, op1, target,
8539 unsignedp, OPTAB_LIB_WIDEN);
8540 hipart = gen_highpart (innermode, temp);
8541 htem = expand_mult_highpart_adjust (innermode, hipart,
8542 op0, op1, hipart,
8543 zextend_p);
8544 if (htem != hipart)
8545 emit_move_insn (hipart, htem);
8546 return REDUCE_BIT_FIELD (temp);
8550 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8551 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8552 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8553 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8555 case FMA_EXPR:
8557 optab opt = fma_optab;
8558 gimple def0, def2;
8560 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8561 call. */
8562 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8564 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8565 tree call_expr;
8567 gcc_assert (fn != NULL_TREE);
8568 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8569 return expand_builtin (call_expr, target, subtarget, mode, false);
8572 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8573 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8575 op0 = op2 = NULL;
8577 if (def0 && def2
8578 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8580 opt = fnms_optab;
8581 op0 = expand_normal (gimple_assign_rhs1 (def0));
8582 op2 = expand_normal (gimple_assign_rhs1 (def2));
8584 else if (def0
8585 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8587 opt = fnma_optab;
8588 op0 = expand_normal (gimple_assign_rhs1 (def0));
8590 else if (def2
8591 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8593 opt = fms_optab;
8594 op2 = expand_normal (gimple_assign_rhs1 (def2));
8597 if (op0 == NULL)
8598 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8599 if (op2 == NULL)
8600 op2 = expand_normal (treeop2);
8601 op1 = expand_normal (treeop1);
8603 return expand_ternary_op (TYPE_MODE (type), opt,
8604 op0, op1, op2, target, 0);
8607 case MULT_EXPR:
8608 /* If this is a fixed-point operation, then we cannot use the code
8609 below because "expand_mult" doesn't support sat/no-sat fixed-point
8610 multiplications. */
8611 if (ALL_FIXED_POINT_MODE_P (mode))
8612 goto binop;
8614 /* If first operand is constant, swap them.
8615 Thus the following special case checks need only
8616 check the second operand. */
8617 if (TREE_CODE (treeop0) == INTEGER_CST)
8619 tree t1 = treeop0;
8620 treeop0 = treeop1;
8621 treeop1 = t1;
8624 /* Attempt to return something suitable for generating an
8625 indexed address, for machines that support that. */
8627 if (modifier == EXPAND_SUM && mode == ptr_mode
8628 && tree_fits_shwi_p (treeop1))
8630 tree exp1 = treeop1;
8632 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8633 EXPAND_SUM);
8635 if (!REG_P (op0))
8636 op0 = force_operand (op0, NULL_RTX);
8637 if (!REG_P (op0))
8638 op0 = copy_to_mode_reg (mode, op0);
8640 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8641 gen_int_mode (tree_to_shwi (exp1),
8642 TYPE_MODE (TREE_TYPE (exp1)))));
8645 if (modifier == EXPAND_STACK_PARM)
8646 target = 0;
8648 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8649 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8651 case TRUNC_DIV_EXPR:
8652 case FLOOR_DIV_EXPR:
8653 case CEIL_DIV_EXPR:
8654 case ROUND_DIV_EXPR:
8655 case EXACT_DIV_EXPR:
8656 /* If this is a fixed-point operation, then we cannot use the code
8657 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8658 divisions. */
8659 if (ALL_FIXED_POINT_MODE_P (mode))
8660 goto binop;
8662 if (modifier == EXPAND_STACK_PARM)
8663 target = 0;
8664 /* Possible optimization: compute the dividend with EXPAND_SUM
8665 then if the divisor is constant can optimize the case
8666 where some terms of the dividend have coeffs divisible by it. */
8667 expand_operands (treeop0, treeop1,
8668 subtarget, &op0, &op1, EXPAND_NORMAL);
8669 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8671 case RDIV_EXPR:
8672 goto binop;
8674 case MULT_HIGHPART_EXPR:
8675 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8676 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8677 gcc_assert (temp);
8678 return temp;
8680 case TRUNC_MOD_EXPR:
8681 case FLOOR_MOD_EXPR:
8682 case CEIL_MOD_EXPR:
8683 case ROUND_MOD_EXPR:
8684 if (modifier == EXPAND_STACK_PARM)
8685 target = 0;
8686 expand_operands (treeop0, treeop1,
8687 subtarget, &op0, &op1, EXPAND_NORMAL);
8688 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8690 case FIXED_CONVERT_EXPR:
8691 op0 = expand_normal (treeop0);
8692 if (target == 0 || modifier == EXPAND_STACK_PARM)
8693 target = gen_reg_rtx (mode);
8695 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8696 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8697 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8698 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8699 else
8700 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8701 return target;
8703 case FIX_TRUNC_EXPR:
8704 op0 = expand_normal (treeop0);
8705 if (target == 0 || modifier == EXPAND_STACK_PARM)
8706 target = gen_reg_rtx (mode);
8707 expand_fix (target, op0, unsignedp);
8708 return target;
8710 case FLOAT_EXPR:
8711 op0 = expand_normal (treeop0);
8712 if (target == 0 || modifier == EXPAND_STACK_PARM)
8713 target = gen_reg_rtx (mode);
8714 /* expand_float can't figure out what to do if FROM has VOIDmode.
8715 So give it the correct mode. With -O, cse will optimize this. */
8716 if (GET_MODE (op0) == VOIDmode)
8717 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8718 op0);
8719 expand_float (target, op0,
8720 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8721 return target;
8723 case NEGATE_EXPR:
8724 op0 = expand_expr (treeop0, subtarget,
8725 VOIDmode, EXPAND_NORMAL);
8726 if (modifier == EXPAND_STACK_PARM)
8727 target = 0;
8728 temp = expand_unop (mode,
8729 optab_for_tree_code (NEGATE_EXPR, type,
8730 optab_default),
8731 op0, target, 0);
8732 gcc_assert (temp);
8733 return REDUCE_BIT_FIELD (temp);
8735 case ABS_EXPR:
8736 op0 = expand_expr (treeop0, subtarget,
8737 VOIDmode, EXPAND_NORMAL);
8738 if (modifier == EXPAND_STACK_PARM)
8739 target = 0;
8741 /* ABS_EXPR is not valid for complex arguments. */
8742 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8743 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8745 /* Unsigned abs is simply the operand. Testing here means we don't
8746 risk generating incorrect code below. */
8747 if (TYPE_UNSIGNED (type))
8748 return op0;
8750 return expand_abs (mode, op0, target, unsignedp,
8751 safe_from_p (target, treeop0, 1));
8753 case MAX_EXPR:
8754 case MIN_EXPR:
8755 target = original_target;
8756 if (target == 0
8757 || modifier == EXPAND_STACK_PARM
8758 || (MEM_P (target) && MEM_VOLATILE_P (target))
8759 || GET_MODE (target) != mode
8760 || (REG_P (target)
8761 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8762 target = gen_reg_rtx (mode);
8763 expand_operands (treeop0, treeop1,
8764 target, &op0, &op1, EXPAND_NORMAL);
8766 /* First try to do it with a special MIN or MAX instruction.
8767 If that does not win, use a conditional jump to select the proper
8768 value. */
8769 this_optab = optab_for_tree_code (code, type, optab_default);
8770 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8771 OPTAB_WIDEN);
8772 if (temp != 0)
8773 return temp;
8775 /* At this point, a MEM target is no longer useful; we will get better
8776 code without it. */
8778 if (! REG_P (target))
8779 target = gen_reg_rtx (mode);
8781 /* If op1 was placed in target, swap op0 and op1. */
8782 if (target != op0 && target == op1)
8784 temp = op0;
8785 op0 = op1;
8786 op1 = temp;
8789 /* We generate better code and avoid problems with op1 mentioning
8790 target by forcing op1 into a pseudo if it isn't a constant. */
8791 if (! CONSTANT_P (op1))
8792 op1 = force_reg (mode, op1);
8795 enum rtx_code comparison_code;
8796 rtx cmpop1 = op1;
8798 if (code == MAX_EXPR)
8799 comparison_code = unsignedp ? GEU : GE;
8800 else
8801 comparison_code = unsignedp ? LEU : LE;
8803 /* Canonicalize to comparisons against 0. */
8804 if (op1 == const1_rtx)
8806 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8807 or (a != 0 ? a : 1) for unsigned.
8808 For MIN we are safe converting (a <= 1 ? a : 1)
8809 into (a <= 0 ? a : 1) */
8810 cmpop1 = const0_rtx;
8811 if (code == MAX_EXPR)
8812 comparison_code = unsignedp ? NE : GT;
8814 if (op1 == constm1_rtx && !unsignedp)
8816 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8817 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8818 cmpop1 = const0_rtx;
8819 if (code == MIN_EXPR)
8820 comparison_code = LT;
8822 #ifdef HAVE_conditional_move
8823 /* Use a conditional move if possible. */
8824 if (can_conditionally_move_p (mode))
8826 rtx insn;
8828 start_sequence ();
8830 /* Try to emit the conditional move. */
8831 insn = emit_conditional_move (target, comparison_code,
8832 op0, cmpop1, mode,
8833 op0, op1, mode,
8834 unsignedp);
8836 /* If we could do the conditional move, emit the sequence,
8837 and return. */
8838 if (insn)
8840 rtx_insn *seq = get_insns ();
8841 end_sequence ();
8842 emit_insn (seq);
8843 return target;
8846 /* Otherwise discard the sequence and fall back to code with
8847 branches. */
8848 end_sequence ();
8850 #endif
8851 if (target != op0)
8852 emit_move_insn (target, op0);
8854 temp = gen_label_rtx ();
8855 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8856 unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8857 -1);
8859 emit_move_insn (target, op1);
8860 emit_label (temp);
8861 return target;
8863 case BIT_NOT_EXPR:
8864 op0 = expand_expr (treeop0, subtarget,
8865 VOIDmode, EXPAND_NORMAL);
8866 if (modifier == EXPAND_STACK_PARM)
8867 target = 0;
8868 /* In case we have to reduce the result to bitfield precision
8869 for unsigned bitfield expand this as XOR with a proper constant
8870 instead. */
8871 if (reduce_bit_field && TYPE_UNSIGNED (type))
8873 wide_int mask = wi::mask (TYPE_PRECISION (type),
8874 false, GET_MODE_PRECISION (mode));
8876 temp = expand_binop (mode, xor_optab, op0,
8877 immed_wide_int_const (mask, mode),
8878 target, 1, OPTAB_LIB_WIDEN);
8880 else
8881 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8882 gcc_assert (temp);
8883 return temp;
8885 /* ??? Can optimize bitwise operations with one arg constant.
8886 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8887 and (a bitwise1 b) bitwise2 b (etc)
8888 but that is probably not worth while. */
8890 case BIT_AND_EXPR:
8891 case BIT_IOR_EXPR:
8892 case BIT_XOR_EXPR:
8893 goto binop;
8895 case LROTATE_EXPR:
8896 case RROTATE_EXPR:
8897 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8898 || (GET_MODE_PRECISION (TYPE_MODE (type))
8899 == TYPE_PRECISION (type)));
8900 /* fall through */
8902 case LSHIFT_EXPR:
8903 case RSHIFT_EXPR:
8904 /* If this is a fixed-point operation, then we cannot use the code
8905 below because "expand_shift" doesn't support sat/no-sat fixed-point
8906 shifts. */
8907 if (ALL_FIXED_POINT_MODE_P (mode))
8908 goto binop;
8910 if (! safe_from_p (subtarget, treeop1, 1))
8911 subtarget = 0;
8912 if (modifier == EXPAND_STACK_PARM)
8913 target = 0;
8914 op0 = expand_expr (treeop0, subtarget,
8915 VOIDmode, EXPAND_NORMAL);
8916 temp = expand_variable_shift (code, mode, op0, treeop1, target,
8917 unsignedp);
8918 if (code == LSHIFT_EXPR)
8919 temp = REDUCE_BIT_FIELD (temp);
8920 return temp;
8922 /* Could determine the answer when only additive constants differ. Also,
8923 the addition of one can be handled by changing the condition. */
8924 case LT_EXPR:
8925 case LE_EXPR:
8926 case GT_EXPR:
8927 case GE_EXPR:
8928 case EQ_EXPR:
8929 case NE_EXPR:
8930 case UNORDERED_EXPR:
8931 case ORDERED_EXPR:
8932 case UNLT_EXPR:
8933 case UNLE_EXPR:
8934 case UNGT_EXPR:
8935 case UNGE_EXPR:
8936 case UNEQ_EXPR:
8937 case LTGT_EXPR:
8938 temp = do_store_flag (ops,
8939 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8940 tmode != VOIDmode ? tmode : mode);
8941 if (temp)
8942 return temp;
8944 /* Use a compare and a jump for BLKmode comparisons, or for function
8945 type comparisons is HAVE_canonicalize_funcptr_for_compare. */
8947 if ((target == 0
8948 || modifier == EXPAND_STACK_PARM
8949 || ! safe_from_p (target, treeop0, 1)
8950 || ! safe_from_p (target, treeop1, 1)
8951 /* Make sure we don't have a hard reg (such as function's return
8952 value) live across basic blocks, if not optimizing. */
8953 || (!optimize && REG_P (target)
8954 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8955 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8957 emit_move_insn (target, const0_rtx);
8959 op1 = gen_label_rtx ();
8960 jumpifnot_1 (code, treeop0, treeop1, op1, -1);
8962 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8963 emit_move_insn (target, constm1_rtx);
8964 else
8965 emit_move_insn (target, const1_rtx);
8967 emit_label (op1);
8968 return target;
8970 case COMPLEX_EXPR:
8971 /* Get the rtx code of the operands. */
8972 op0 = expand_normal (treeop0);
8973 op1 = expand_normal (treeop1);
8975 if (!target)
8976 target = gen_reg_rtx (TYPE_MODE (type));
8977 else
8978 /* If target overlaps with op1, then either we need to force
8979 op1 into a pseudo (if target also overlaps with op0),
8980 or write the complex parts in reverse order. */
8981 switch (GET_CODE (target))
8983 case CONCAT:
8984 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
8986 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
8988 complex_expr_force_op1:
8989 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
8990 emit_move_insn (temp, op1);
8991 op1 = temp;
8992 break;
8994 complex_expr_swap_order:
8995 /* Move the imaginary (op1) and real (op0) parts to their
8996 location. */
8997 write_complex_part (target, op1, true);
8998 write_complex_part (target, op0, false);
9000 return target;
9002 break;
9003 case MEM:
9004 temp = adjust_address_nv (target,
9005 GET_MODE_INNER (GET_MODE (target)), 0);
9006 if (reg_overlap_mentioned_p (temp, op1))
9008 enum machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9009 temp = adjust_address_nv (target, imode,
9010 GET_MODE_SIZE (imode));
9011 if (reg_overlap_mentioned_p (temp, op0))
9012 goto complex_expr_force_op1;
9013 goto complex_expr_swap_order;
9015 break;
9016 default:
9017 if (reg_overlap_mentioned_p (target, op1))
9019 if (reg_overlap_mentioned_p (target, op0))
9020 goto complex_expr_force_op1;
9021 goto complex_expr_swap_order;
9023 break;
9026 /* Move the real (op0) and imaginary (op1) parts to their location. */
9027 write_complex_part (target, op0, false);
9028 write_complex_part (target, op1, true);
9030 return target;
9032 case WIDEN_SUM_EXPR:
9034 tree oprnd0 = treeop0;
9035 tree oprnd1 = treeop1;
9037 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9038 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9039 target, unsignedp);
9040 return target;
9043 case REDUC_MAX_EXPR:
9044 case REDUC_MIN_EXPR:
9045 case REDUC_PLUS_EXPR:
9047 op0 = expand_normal (treeop0);
9048 this_optab = optab_for_tree_code (code, type, optab_default);
9049 temp = expand_unop (mode, this_optab, op0, target, unsignedp);
9050 gcc_assert (temp);
9051 return temp;
9054 case VEC_LSHIFT_EXPR:
9055 case VEC_RSHIFT_EXPR:
9057 target = expand_vec_shift_expr (ops, target);
9058 return target;
9061 case VEC_UNPACK_HI_EXPR:
9062 case VEC_UNPACK_LO_EXPR:
9064 op0 = expand_normal (treeop0);
9065 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9066 target, unsignedp);
9067 gcc_assert (temp);
9068 return temp;
9071 case VEC_UNPACK_FLOAT_HI_EXPR:
9072 case VEC_UNPACK_FLOAT_LO_EXPR:
9074 op0 = expand_normal (treeop0);
9075 /* The signedness is determined from input operand. */
9076 temp = expand_widen_pattern_expr
9077 (ops, op0, NULL_RTX, NULL_RTX,
9078 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9080 gcc_assert (temp);
9081 return temp;
9084 case VEC_WIDEN_MULT_HI_EXPR:
9085 case VEC_WIDEN_MULT_LO_EXPR:
9086 case VEC_WIDEN_MULT_EVEN_EXPR:
9087 case VEC_WIDEN_MULT_ODD_EXPR:
9088 case VEC_WIDEN_LSHIFT_HI_EXPR:
9089 case VEC_WIDEN_LSHIFT_LO_EXPR:
9090 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9091 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9092 target, unsignedp);
9093 gcc_assert (target);
9094 return target;
9096 case VEC_PACK_TRUNC_EXPR:
9097 case VEC_PACK_SAT_EXPR:
9098 case VEC_PACK_FIX_TRUNC_EXPR:
9099 mode = TYPE_MODE (TREE_TYPE (treeop0));
9100 goto binop;
9102 case VEC_PERM_EXPR:
9103 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9104 op2 = expand_normal (treeop2);
9106 /* Careful here: if the target doesn't support integral vector modes,
9107 a constant selection vector could wind up smooshed into a normal
9108 integral constant. */
9109 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9111 tree sel_type = TREE_TYPE (treeop2);
9112 enum machine_mode vmode
9113 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9114 TYPE_VECTOR_SUBPARTS (sel_type));
9115 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9116 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9117 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9119 else
9120 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9122 temp = expand_vec_perm (mode, op0, op1, op2, target);
9123 gcc_assert (temp);
9124 return temp;
9126 case DOT_PROD_EXPR:
9128 tree oprnd0 = treeop0;
9129 tree oprnd1 = treeop1;
9130 tree oprnd2 = treeop2;
9131 rtx op2;
9133 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9134 op2 = expand_normal (oprnd2);
9135 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9136 target, unsignedp);
9137 return target;
9140 case SAD_EXPR:
9142 tree oprnd0 = treeop0;
9143 tree oprnd1 = treeop1;
9144 tree oprnd2 = treeop2;
9145 rtx op2;
9147 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9148 op2 = expand_normal (oprnd2);
9149 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9150 target, unsignedp);
9151 return target;
9154 case REALIGN_LOAD_EXPR:
9156 tree oprnd0 = treeop0;
9157 tree oprnd1 = treeop1;
9158 tree oprnd2 = treeop2;
9159 rtx op2;
9161 this_optab = optab_for_tree_code (code, type, optab_default);
9162 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9163 op2 = expand_normal (oprnd2);
9164 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9165 target, unsignedp);
9166 gcc_assert (temp);
9167 return temp;
9170 case COND_EXPR:
9171 /* A COND_EXPR with its type being VOID_TYPE represents a
9172 conditional jump and is handled in
9173 expand_gimple_cond_expr. */
9174 gcc_assert (!VOID_TYPE_P (type));
9176 /* Note that COND_EXPRs whose type is a structure or union
9177 are required to be constructed to contain assignments of
9178 a temporary variable, so that we can evaluate them here
9179 for side effect only. If type is void, we must do likewise. */
9181 gcc_assert (!TREE_ADDRESSABLE (type)
9182 && !ignore
9183 && TREE_TYPE (treeop1) != void_type_node
9184 && TREE_TYPE (treeop2) != void_type_node);
9186 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9187 if (temp)
9188 return temp;
9190 /* If we are not to produce a result, we have no target. Otherwise,
9191 if a target was specified use it; it will not be used as an
9192 intermediate target unless it is safe. If no target, use a
9193 temporary. */
9195 if (modifier != EXPAND_STACK_PARM
9196 && original_target
9197 && safe_from_p (original_target, treeop0, 1)
9198 && GET_MODE (original_target) == mode
9199 && !MEM_P (original_target))
9200 temp = original_target;
9201 else
9202 temp = assign_temp (type, 0, 1);
9204 do_pending_stack_adjust ();
9205 NO_DEFER_POP;
9206 op0 = gen_label_rtx ();
9207 op1 = gen_label_rtx ();
9208 jumpifnot (treeop0, op0, -1);
9209 store_expr (treeop1, temp,
9210 modifier == EXPAND_STACK_PARM,
9211 false);
9213 emit_jump_insn (gen_jump (op1));
9214 emit_barrier ();
9215 emit_label (op0);
9216 store_expr (treeop2, temp,
9217 modifier == EXPAND_STACK_PARM,
9218 false);
9220 emit_label (op1);
9221 OK_DEFER_POP;
9222 return temp;
9224 case VEC_COND_EXPR:
9225 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9226 return target;
9228 default:
9229 gcc_unreachable ();
9232 /* Here to do an ordinary binary operator. */
9233 binop:
9234 expand_operands (treeop0, treeop1,
9235 subtarget, &op0, &op1, EXPAND_NORMAL);
9236 binop2:
9237 this_optab = optab_for_tree_code (code, type, optab_default);
9238 binop3:
9239 if (modifier == EXPAND_STACK_PARM)
9240 target = 0;
9241 temp = expand_binop (mode, this_optab, op0, op1, target,
9242 unsignedp, OPTAB_LIB_WIDEN);
9243 gcc_assert (temp);
9244 /* Bitwise operations do not need bitfield reduction as we expect their
9245 operands being properly truncated. */
9246 if (code == BIT_XOR_EXPR
9247 || code == BIT_AND_EXPR
9248 || code == BIT_IOR_EXPR)
9249 return temp;
9250 return REDUCE_BIT_FIELD (temp);
9252 #undef REDUCE_BIT_FIELD
9254 /* Return TRUE if value in SSA is zero and sign extended for wider mode MODE
9255 using value range information stored. Return FALSE otherwise.
9257 This is used to check if SUBREG is zero and sign extended and to set
9258 promoted mode SRP_SIGNED_AND_UNSIGNED to SUBREG. */
9260 bool
9261 promoted_for_signed_and_unsigned_p (tree ssa, enum machine_mode mode)
9263 wide_int min, max;
9265 if (ssa == NULL_TREE
9266 || TREE_CODE (ssa) != SSA_NAME
9267 || !INTEGRAL_TYPE_P (TREE_TYPE (ssa))
9268 || (TYPE_PRECISION (TREE_TYPE (ssa)) != GET_MODE_PRECISION (mode)))
9269 return false;
9271 /* Return FALSE if value_range is not recorded for SSA. */
9272 if (get_range_info (ssa, &min, &max) != VR_RANGE)
9273 return false;
9275 /* Return true (to set SRP_SIGNED_AND_UNSIGNED to SUBREG) if MSB of the
9276 smaller mode is not set (i.e. MSB of ssa is not set). */
9277 if (!wi::neg_p (min, SIGNED) && !wi::neg_p(max, SIGNED))
9278 return true;
9279 else
9280 return false;
9284 /* Return TRUE if expression STMT is suitable for replacement.
9285 Never consider memory loads as replaceable, because those don't ever lead
9286 into constant expressions. */
9288 static bool
9289 stmt_is_replaceable_p (gimple stmt)
9291 if (ssa_is_replaceable_p (stmt))
9293 /* Don't move around loads. */
9294 if (!gimple_assign_single_p (stmt)
9295 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9296 return true;
9298 return false;
9302 expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode,
9303 enum expand_modifier modifier, rtx *alt_rtl,
9304 bool inner_reference_p)
9306 rtx op0, op1, temp, decl_rtl;
9307 tree type;
9308 int unsignedp;
9309 enum machine_mode mode;
9310 enum tree_code code = TREE_CODE (exp);
9311 rtx subtarget, original_target;
9312 int ignore;
9313 tree context;
9314 bool reduce_bit_field;
9315 location_t loc = EXPR_LOCATION (exp);
9316 struct separate_ops ops;
9317 tree treeop0, treeop1, treeop2;
9318 tree ssa_name = NULL_TREE;
9319 gimple g;
9321 type = TREE_TYPE (exp);
9322 mode = TYPE_MODE (type);
9323 unsignedp = TYPE_UNSIGNED (type);
9325 treeop0 = treeop1 = treeop2 = NULL_TREE;
9326 if (!VL_EXP_CLASS_P (exp))
9327 switch (TREE_CODE_LENGTH (code))
9329 default:
9330 case 3: treeop2 = TREE_OPERAND (exp, 2);
9331 case 2: treeop1 = TREE_OPERAND (exp, 1);
9332 case 1: treeop0 = TREE_OPERAND (exp, 0);
9333 case 0: break;
9335 ops.code = code;
9336 ops.type = type;
9337 ops.op0 = treeop0;
9338 ops.op1 = treeop1;
9339 ops.op2 = treeop2;
9340 ops.location = loc;
9342 ignore = (target == const0_rtx
9343 || ((CONVERT_EXPR_CODE_P (code)
9344 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9345 && TREE_CODE (type) == VOID_TYPE));
9347 /* An operation in what may be a bit-field type needs the
9348 result to be reduced to the precision of the bit-field type,
9349 which is narrower than that of the type's mode. */
9350 reduce_bit_field = (!ignore
9351 && INTEGRAL_TYPE_P (type)
9352 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9354 /* If we are going to ignore this result, we need only do something
9355 if there is a side-effect somewhere in the expression. If there
9356 is, short-circuit the most common cases here. Note that we must
9357 not call expand_expr with anything but const0_rtx in case this
9358 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9360 if (ignore)
9362 if (! TREE_SIDE_EFFECTS (exp))
9363 return const0_rtx;
9365 /* Ensure we reference a volatile object even if value is ignored, but
9366 don't do this if all we are doing is taking its address. */
9367 if (TREE_THIS_VOLATILE (exp)
9368 && TREE_CODE (exp) != FUNCTION_DECL
9369 && mode != VOIDmode && mode != BLKmode
9370 && modifier != EXPAND_CONST_ADDRESS)
9372 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9373 if (MEM_P (temp))
9374 copy_to_reg (temp);
9375 return const0_rtx;
9378 if (TREE_CODE_CLASS (code) == tcc_unary
9379 || code == BIT_FIELD_REF
9380 || code == COMPONENT_REF
9381 || code == INDIRECT_REF)
9382 return expand_expr (treeop0, const0_rtx, VOIDmode,
9383 modifier);
9385 else if (TREE_CODE_CLASS (code) == tcc_binary
9386 || TREE_CODE_CLASS (code) == tcc_comparison
9387 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9389 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9390 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9391 return const0_rtx;
9394 target = 0;
9397 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9398 target = 0;
9400 /* Use subtarget as the target for operand 0 of a binary operation. */
9401 subtarget = get_subtarget (target);
9402 original_target = target;
9404 switch (code)
9406 case LABEL_DECL:
9408 tree function = decl_function_context (exp);
9410 temp = label_rtx (exp);
9411 temp = gen_rtx_LABEL_REF (Pmode, temp);
9413 if (function != current_function_decl
9414 && function != 0)
9415 LABEL_REF_NONLOCAL_P (temp) = 1;
9417 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9418 return temp;
9421 case SSA_NAME:
9422 /* ??? ivopts calls expander, without any preparation from
9423 out-of-ssa. So fake instructions as if this was an access to the
9424 base variable. This unnecessarily allocates a pseudo, see how we can
9425 reuse it, if partition base vars have it set already. */
9426 if (!currently_expanding_to_rtl)
9428 tree var = SSA_NAME_VAR (exp);
9429 if (var && DECL_RTL_SET_P (var))
9430 return DECL_RTL (var);
9431 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9432 LAST_VIRTUAL_REGISTER + 1);
9435 g = get_gimple_for_ssa_name (exp);
9436 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9437 if (g == NULL
9438 && modifier == EXPAND_INITIALIZER
9439 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9440 && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9441 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9442 g = SSA_NAME_DEF_STMT (exp);
9443 if (g)
9445 rtx r;
9446 ops.code = gimple_assign_rhs_code (g);
9447 switch (get_gimple_rhs_class (ops.code))
9449 case GIMPLE_TERNARY_RHS:
9450 ops.op2 = gimple_assign_rhs3 (g);
9451 /* Fallthru */
9452 case GIMPLE_BINARY_RHS:
9453 ops.op1 = gimple_assign_rhs2 (g);
9454 /* Fallthru */
9455 case GIMPLE_UNARY_RHS:
9456 ops.op0 = gimple_assign_rhs1 (g);
9457 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9458 ops.location = gimple_location (g);
9459 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9460 break;
9461 case GIMPLE_SINGLE_RHS:
9463 location_t saved_loc = curr_insn_location ();
9464 set_curr_insn_location (gimple_location (g));
9465 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9466 tmode, modifier, NULL, inner_reference_p);
9467 set_curr_insn_location (saved_loc);
9468 break;
9470 default:
9471 gcc_unreachable ();
9473 if (REG_P (r) && !REG_EXPR (r))
9474 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9475 return r;
9478 ssa_name = exp;
9479 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9480 exp = SSA_NAME_VAR (ssa_name);
9481 goto expand_decl_rtl;
9483 case PARM_DECL:
9484 case VAR_DECL:
9485 /* If a static var's type was incomplete when the decl was written,
9486 but the type is complete now, lay out the decl now. */
9487 if (DECL_SIZE (exp) == 0
9488 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9489 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9490 layout_decl (exp, 0);
9492 /* ... fall through ... */
9494 case FUNCTION_DECL:
9495 case RESULT_DECL:
9496 decl_rtl = DECL_RTL (exp);
9497 expand_decl_rtl:
9498 gcc_assert (decl_rtl);
9499 decl_rtl = copy_rtx (decl_rtl);
9500 /* Record writes to register variables. */
9501 if (modifier == EXPAND_WRITE
9502 && REG_P (decl_rtl)
9503 && HARD_REGISTER_P (decl_rtl))
9504 add_to_hard_reg_set (&crtl->asm_clobbers,
9505 GET_MODE (decl_rtl), REGNO (decl_rtl));
9507 /* Ensure variable marked as used even if it doesn't go through
9508 a parser. If it hasn't be used yet, write out an external
9509 definition. */
9510 TREE_USED (exp) = 1;
9512 /* Show we haven't gotten RTL for this yet. */
9513 temp = 0;
9515 /* Variables inherited from containing functions should have
9516 been lowered by this point. */
9517 context = decl_function_context (exp);
9518 gcc_assert (SCOPE_FILE_SCOPE_P (context)
9519 || context == current_function_decl
9520 || TREE_STATIC (exp)
9521 || DECL_EXTERNAL (exp)
9522 /* ??? C++ creates functions that are not TREE_STATIC. */
9523 || TREE_CODE (exp) == FUNCTION_DECL);
9525 /* This is the case of an array whose size is to be determined
9526 from its initializer, while the initializer is still being parsed.
9527 ??? We aren't parsing while expanding anymore. */
9529 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9530 temp = validize_mem (decl_rtl);
9532 /* If DECL_RTL is memory, we are in the normal case and the
9533 address is not valid, get the address into a register. */
9535 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9537 if (alt_rtl)
9538 *alt_rtl = decl_rtl;
9539 decl_rtl = use_anchored_address (decl_rtl);
9540 if (modifier != EXPAND_CONST_ADDRESS
9541 && modifier != EXPAND_SUM
9542 && !memory_address_addr_space_p (DECL_MODE (exp),
9543 XEXP (decl_rtl, 0),
9544 MEM_ADDR_SPACE (decl_rtl)))
9545 temp = replace_equiv_address (decl_rtl,
9546 copy_rtx (XEXP (decl_rtl, 0)));
9549 /* If we got something, return it. But first, set the alignment
9550 if the address is a register. */
9551 if (temp != 0)
9553 if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9554 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9556 return temp;
9559 /* If the mode of DECL_RTL does not match that of the decl,
9560 there are two cases: we are dealing with a BLKmode value
9561 that is returned in a register, or we are dealing with
9562 a promoted value. In the latter case, return a SUBREG
9563 of the wanted mode, but mark it so that we know that it
9564 was already extended. */
9565 if (REG_P (decl_rtl)
9566 && DECL_MODE (exp) != BLKmode
9567 && GET_MODE (decl_rtl) != DECL_MODE (exp))
9569 enum machine_mode pmode;
9571 /* Get the signedness to be used for this variable. Ensure we get
9572 the same mode we got when the variable was declared. */
9573 if (code == SSA_NAME
9574 && (g = SSA_NAME_DEF_STMT (ssa_name))
9575 && gimple_code (g) == GIMPLE_CALL
9576 && !gimple_call_internal_p (g))
9577 pmode = promote_function_mode (type, mode, &unsignedp,
9578 gimple_call_fntype (g),
9580 else
9581 pmode = promote_decl_mode (exp, &unsignedp);
9582 gcc_assert (GET_MODE (decl_rtl) == pmode);
9584 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9585 SUBREG_PROMOTED_VAR_P (temp) = 1;
9586 if (promoted_for_signed_and_unsigned_p (ssa_name, mode))
9587 SUBREG_PROMOTED_SET (temp, SRP_SIGNED_AND_UNSIGNED);
9588 else
9589 SUBREG_PROMOTED_SET (temp, unsignedp);
9590 return temp;
9593 return decl_rtl;
9595 case INTEGER_CST:
9596 /* Given that TYPE_PRECISION (type) is not always equal to
9597 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9598 the former to the latter according to the signedness of the
9599 type. */
9600 temp = immed_wide_int_const (wide_int::from
9601 (exp,
9602 GET_MODE_PRECISION (TYPE_MODE (type)),
9603 TYPE_SIGN (type)),
9604 TYPE_MODE (type));
9605 return temp;
9607 case VECTOR_CST:
9609 tree tmp = NULL_TREE;
9610 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9611 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9612 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9613 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9614 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9615 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9616 return const_vector_from_tree (exp);
9617 if (GET_MODE_CLASS (mode) == MODE_INT)
9619 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9620 if (type_for_mode)
9621 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9623 if (!tmp)
9625 vec<constructor_elt, va_gc> *v;
9626 unsigned i;
9627 vec_alloc (v, VECTOR_CST_NELTS (exp));
9628 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9629 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9630 tmp = build_constructor (type, v);
9632 return expand_expr (tmp, ignore ? const0_rtx : target,
9633 tmode, modifier);
9636 case CONST_DECL:
9637 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9639 case REAL_CST:
9640 /* If optimized, generate immediate CONST_DOUBLE
9641 which will be turned into memory by reload if necessary.
9643 We used to force a register so that loop.c could see it. But
9644 this does not allow gen_* patterns to perform optimizations with
9645 the constants. It also produces two insns in cases like "x = 1.0;".
9646 On most machines, floating-point constants are not permitted in
9647 many insns, so we'd end up copying it to a register in any case.
9649 Now, we do the copying in expand_binop, if appropriate. */
9650 return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9651 TYPE_MODE (TREE_TYPE (exp)));
9653 case FIXED_CST:
9654 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9655 TYPE_MODE (TREE_TYPE (exp)));
9657 case COMPLEX_CST:
9658 /* Handle evaluating a complex constant in a CONCAT target. */
9659 if (original_target && GET_CODE (original_target) == CONCAT)
9661 enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9662 rtx rtarg, itarg;
9664 rtarg = XEXP (original_target, 0);
9665 itarg = XEXP (original_target, 1);
9667 /* Move the real and imaginary parts separately. */
9668 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9669 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9671 if (op0 != rtarg)
9672 emit_move_insn (rtarg, op0);
9673 if (op1 != itarg)
9674 emit_move_insn (itarg, op1);
9676 return original_target;
9679 /* ... fall through ... */
9681 case STRING_CST:
9682 temp = expand_expr_constant (exp, 1, modifier);
9684 /* temp contains a constant address.
9685 On RISC machines where a constant address isn't valid,
9686 make some insns to get that address into a register. */
9687 if (modifier != EXPAND_CONST_ADDRESS
9688 && modifier != EXPAND_INITIALIZER
9689 && modifier != EXPAND_SUM
9690 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9691 MEM_ADDR_SPACE (temp)))
9692 return replace_equiv_address (temp,
9693 copy_rtx (XEXP (temp, 0)));
9694 return temp;
9696 case SAVE_EXPR:
9698 tree val = treeop0;
9699 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9700 inner_reference_p);
9702 if (!SAVE_EXPR_RESOLVED_P (exp))
9704 /* We can indeed still hit this case, typically via builtin
9705 expanders calling save_expr immediately before expanding
9706 something. Assume this means that we only have to deal
9707 with non-BLKmode values. */
9708 gcc_assert (GET_MODE (ret) != BLKmode);
9710 val = build_decl (curr_insn_location (),
9711 VAR_DECL, NULL, TREE_TYPE (exp));
9712 DECL_ARTIFICIAL (val) = 1;
9713 DECL_IGNORED_P (val) = 1;
9714 treeop0 = val;
9715 TREE_OPERAND (exp, 0) = treeop0;
9716 SAVE_EXPR_RESOLVED_P (exp) = 1;
9718 if (!CONSTANT_P (ret))
9719 ret = copy_to_reg (ret);
9720 SET_DECL_RTL (val, ret);
9723 return ret;
9727 case CONSTRUCTOR:
9728 /* If we don't need the result, just ensure we evaluate any
9729 subexpressions. */
9730 if (ignore)
9732 unsigned HOST_WIDE_INT idx;
9733 tree value;
9735 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9736 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9738 return const0_rtx;
9741 return expand_constructor (exp, target, modifier, false);
9743 case TARGET_MEM_REF:
9745 addr_space_t as
9746 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9747 enum insn_code icode;
9748 unsigned int align;
9750 op0 = addr_for_mem_ref (exp, as, true);
9751 op0 = memory_address_addr_space (mode, op0, as);
9752 temp = gen_rtx_MEM (mode, op0);
9753 set_mem_attributes (temp, exp, 0);
9754 set_mem_addr_space (temp, as);
9755 align = get_object_alignment (exp);
9756 if (modifier != EXPAND_WRITE
9757 && modifier != EXPAND_MEMORY
9758 && mode != BLKmode
9759 && align < GET_MODE_ALIGNMENT (mode)
9760 /* If the target does not have special handling for unaligned
9761 loads of mode then it can use regular moves for them. */
9762 && ((icode = optab_handler (movmisalign_optab, mode))
9763 != CODE_FOR_nothing))
9765 struct expand_operand ops[2];
9767 /* We've already validated the memory, and we're creating a
9768 new pseudo destination. The predicates really can't fail,
9769 nor can the generator. */
9770 create_output_operand (&ops[0], NULL_RTX, mode);
9771 create_fixed_operand (&ops[1], temp);
9772 expand_insn (icode, 2, ops);
9773 temp = ops[0].value;
9775 return temp;
9778 case MEM_REF:
9780 addr_space_t as
9781 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9782 enum machine_mode address_mode;
9783 tree base = TREE_OPERAND (exp, 0);
9784 gimple def_stmt;
9785 enum insn_code icode;
9786 unsigned align;
9787 /* Handle expansion of non-aliased memory with non-BLKmode. That
9788 might end up in a register. */
9789 if (mem_ref_refers_to_non_mem_p (exp))
9791 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
9792 base = TREE_OPERAND (base, 0);
9793 if (offset == 0
9794 && tree_fits_uhwi_p (TYPE_SIZE (type))
9795 && (GET_MODE_BITSIZE (DECL_MODE (base))
9796 == tree_to_uhwi (TYPE_SIZE (type))))
9797 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
9798 target, tmode, modifier);
9799 if (TYPE_MODE (type) == BLKmode)
9801 temp = assign_stack_temp (DECL_MODE (base),
9802 GET_MODE_SIZE (DECL_MODE (base)));
9803 store_expr (base, temp, 0, false);
9804 temp = adjust_address (temp, BLKmode, offset);
9805 set_mem_size (temp, int_size_in_bytes (type));
9806 return temp;
9808 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
9809 bitsize_int (offset * BITS_PER_UNIT));
9810 return expand_expr (exp, target, tmode, modifier);
9812 address_mode = targetm.addr_space.address_mode (as);
9813 base = TREE_OPERAND (exp, 0);
9814 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9816 tree mask = gimple_assign_rhs2 (def_stmt);
9817 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9818 gimple_assign_rhs1 (def_stmt), mask);
9819 TREE_OPERAND (exp, 0) = base;
9821 align = get_object_alignment (exp);
9822 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9823 op0 = memory_address_addr_space (mode, op0, as);
9824 if (!integer_zerop (TREE_OPERAND (exp, 1)))
9826 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
9827 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9828 op0 = memory_address_addr_space (mode, op0, as);
9830 temp = gen_rtx_MEM (mode, op0);
9831 set_mem_attributes (temp, exp, 0);
9832 set_mem_addr_space (temp, as);
9833 if (TREE_THIS_VOLATILE (exp))
9834 MEM_VOLATILE_P (temp) = 1;
9835 if (modifier != EXPAND_WRITE
9836 && modifier != EXPAND_MEMORY
9837 && !inner_reference_p
9838 && mode != BLKmode
9839 && align < GET_MODE_ALIGNMENT (mode))
9841 if ((icode = optab_handler (movmisalign_optab, mode))
9842 != CODE_FOR_nothing)
9844 struct expand_operand ops[2];
9846 /* We've already validated the memory, and we're creating a
9847 new pseudo destination. The predicates really can't fail,
9848 nor can the generator. */
9849 create_output_operand (&ops[0], NULL_RTX, mode);
9850 create_fixed_operand (&ops[1], temp);
9851 expand_insn (icode, 2, ops);
9852 temp = ops[0].value;
9854 else if (SLOW_UNALIGNED_ACCESS (mode, align))
9855 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9856 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9857 (modifier == EXPAND_STACK_PARM
9858 ? NULL_RTX : target),
9859 mode, mode);
9861 return temp;
9864 case ARRAY_REF:
9867 tree array = treeop0;
9868 tree index = treeop1;
9869 tree init;
9871 /* Fold an expression like: "foo"[2].
9872 This is not done in fold so it won't happen inside &.
9873 Don't fold if this is for wide characters since it's too
9874 difficult to do correctly and this is a very rare case. */
9876 if (modifier != EXPAND_CONST_ADDRESS
9877 && modifier != EXPAND_INITIALIZER
9878 && modifier != EXPAND_MEMORY)
9880 tree t = fold_read_from_constant_string (exp);
9882 if (t)
9883 return expand_expr (t, target, tmode, modifier);
9886 /* If this is a constant index into a constant array,
9887 just get the value from the array. Handle both the cases when
9888 we have an explicit constructor and when our operand is a variable
9889 that was declared const. */
9891 if (modifier != EXPAND_CONST_ADDRESS
9892 && modifier != EXPAND_INITIALIZER
9893 && modifier != EXPAND_MEMORY
9894 && TREE_CODE (array) == CONSTRUCTOR
9895 && ! TREE_SIDE_EFFECTS (array)
9896 && TREE_CODE (index) == INTEGER_CST)
9898 unsigned HOST_WIDE_INT ix;
9899 tree field, value;
9901 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9902 field, value)
9903 if (tree_int_cst_equal (field, index))
9905 if (!TREE_SIDE_EFFECTS (value))
9906 return expand_expr (fold (value), target, tmode, modifier);
9907 break;
9911 else if (optimize >= 1
9912 && modifier != EXPAND_CONST_ADDRESS
9913 && modifier != EXPAND_INITIALIZER
9914 && modifier != EXPAND_MEMORY
9915 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9916 && TREE_CODE (index) == INTEGER_CST
9917 && (TREE_CODE (array) == VAR_DECL
9918 || TREE_CODE (array) == CONST_DECL)
9919 && (init = ctor_for_folding (array)) != error_mark_node)
9921 if (init == NULL_TREE)
9923 tree value = build_zero_cst (type);
9924 if (TREE_CODE (value) == CONSTRUCTOR)
9926 /* If VALUE is a CONSTRUCTOR, this optimization is only
9927 useful if this doesn't store the CONSTRUCTOR into
9928 memory. If it does, it is more efficient to just
9929 load the data from the array directly. */
9930 rtx ret = expand_constructor (value, target,
9931 modifier, true);
9932 if (ret == NULL_RTX)
9933 value = NULL_TREE;
9936 if (value)
9937 return expand_expr (value, target, tmode, modifier);
9939 else if (TREE_CODE (init) == CONSTRUCTOR)
9941 unsigned HOST_WIDE_INT ix;
9942 tree field, value;
9944 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9945 field, value)
9946 if (tree_int_cst_equal (field, index))
9948 if (TREE_SIDE_EFFECTS (value))
9949 break;
9951 if (TREE_CODE (value) == CONSTRUCTOR)
9953 /* If VALUE is a CONSTRUCTOR, this
9954 optimization is only useful if
9955 this doesn't store the CONSTRUCTOR
9956 into memory. If it does, it is more
9957 efficient to just load the data from
9958 the array directly. */
9959 rtx ret = expand_constructor (value, target,
9960 modifier, true);
9961 if (ret == NULL_RTX)
9962 break;
9965 return
9966 expand_expr (fold (value), target, tmode, modifier);
9969 else if (TREE_CODE (init) == STRING_CST)
9971 tree low_bound = array_ref_low_bound (exp);
9972 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
9974 /* Optimize the special case of a zero lower bound.
9976 We convert the lower bound to sizetype to avoid problems
9977 with constant folding. E.g. suppose the lower bound is
9978 1 and its mode is QI. Without the conversion
9979 (ARRAY + (INDEX - (unsigned char)1))
9980 becomes
9981 (ARRAY + (-(unsigned char)1) + INDEX)
9982 which becomes
9983 (ARRAY + 255 + INDEX). Oops! */
9984 if (!integer_zerop (low_bound))
9985 index1 = size_diffop_loc (loc, index1,
9986 fold_convert_loc (loc, sizetype,
9987 low_bound));
9989 if (compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
9991 tree type = TREE_TYPE (TREE_TYPE (init));
9992 enum machine_mode mode = TYPE_MODE (type);
9994 if (GET_MODE_CLASS (mode) == MODE_INT
9995 && GET_MODE_SIZE (mode) == 1)
9996 return gen_int_mode (TREE_STRING_POINTER (init)
9997 [TREE_INT_CST_LOW (index1)],
9998 mode);
10003 goto normal_inner_ref;
10005 case COMPONENT_REF:
10006 /* If the operand is a CONSTRUCTOR, we can just extract the
10007 appropriate field if it is present. */
10008 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10010 unsigned HOST_WIDE_INT idx;
10011 tree field, value;
10013 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10014 idx, field, value)
10015 if (field == treeop1
10016 /* We can normally use the value of the field in the
10017 CONSTRUCTOR. However, if this is a bitfield in
10018 an integral mode that we can fit in a HOST_WIDE_INT,
10019 we must mask only the number of bits in the bitfield,
10020 since this is done implicitly by the constructor. If
10021 the bitfield does not meet either of those conditions,
10022 we can't do this optimization. */
10023 && (! DECL_BIT_FIELD (field)
10024 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10025 && (GET_MODE_PRECISION (DECL_MODE (field))
10026 <= HOST_BITS_PER_WIDE_INT))))
10028 if (DECL_BIT_FIELD (field)
10029 && modifier == EXPAND_STACK_PARM)
10030 target = 0;
10031 op0 = expand_expr (value, target, tmode, modifier);
10032 if (DECL_BIT_FIELD (field))
10034 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10035 enum machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10037 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10039 op1 = gen_int_mode (((HOST_WIDE_INT) 1 << bitsize) - 1,
10040 imode);
10041 op0 = expand_and (imode, op0, op1, target);
10043 else
10045 int count = GET_MODE_PRECISION (imode) - bitsize;
10047 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10048 target, 0);
10049 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10050 target, 0);
10054 return op0;
10057 goto normal_inner_ref;
10059 case BIT_FIELD_REF:
10060 case ARRAY_RANGE_REF:
10061 normal_inner_ref:
10063 enum machine_mode mode1, mode2;
10064 HOST_WIDE_INT bitsize, bitpos;
10065 tree offset;
10066 int volatilep = 0, must_force_mem;
10067 tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
10068 &mode1, &unsignedp, &volatilep, true);
10069 rtx orig_op0, memloc;
10070 bool mem_attrs_from_type = false;
10072 /* If we got back the original object, something is wrong. Perhaps
10073 we are evaluating an expression too early. In any event, don't
10074 infinitely recurse. */
10075 gcc_assert (tem != exp);
10077 /* If TEM's type is a union of variable size, pass TARGET to the inner
10078 computation, since it will need a temporary and TARGET is known
10079 to have to do. This occurs in unchecked conversion in Ada. */
10080 orig_op0 = op0
10081 = expand_expr_real (tem,
10082 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10083 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10084 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10085 != INTEGER_CST)
10086 && modifier != EXPAND_STACK_PARM
10087 ? target : NULL_RTX),
10088 VOIDmode,
10089 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10090 NULL, true);
10092 /* If the field has a mode, we want to access it in the
10093 field's mode, not the computed mode.
10094 If a MEM has VOIDmode (external with incomplete type),
10095 use BLKmode for it instead. */
10096 if (MEM_P (op0))
10098 if (mode1 != VOIDmode)
10099 op0 = adjust_address (op0, mode1, 0);
10100 else if (GET_MODE (op0) == VOIDmode)
10101 op0 = adjust_address (op0, BLKmode, 0);
10104 mode2
10105 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10107 /* If we have either an offset, a BLKmode result, or a reference
10108 outside the underlying object, we must force it to memory.
10109 Such a case can occur in Ada if we have unchecked conversion
10110 of an expression from a scalar type to an aggregate type or
10111 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10112 passed a partially uninitialized object or a view-conversion
10113 to a larger size. */
10114 must_force_mem = (offset
10115 || mode1 == BLKmode
10116 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10118 /* Handle CONCAT first. */
10119 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10121 if (bitpos == 0
10122 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
10123 return op0;
10124 if (bitpos == 0
10125 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10126 && bitsize)
10128 op0 = XEXP (op0, 0);
10129 mode2 = GET_MODE (op0);
10131 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10132 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10133 && bitpos
10134 && bitsize)
10136 op0 = XEXP (op0, 1);
10137 bitpos = 0;
10138 mode2 = GET_MODE (op0);
10140 else
10141 /* Otherwise force into memory. */
10142 must_force_mem = 1;
10145 /* If this is a constant, put it in a register if it is a legitimate
10146 constant and we don't need a memory reference. */
10147 if (CONSTANT_P (op0)
10148 && mode2 != BLKmode
10149 && targetm.legitimate_constant_p (mode2, op0)
10150 && !must_force_mem)
10151 op0 = force_reg (mode2, op0);
10153 /* Otherwise, if this is a constant, try to force it to the constant
10154 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10155 is a legitimate constant. */
10156 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10157 op0 = validize_mem (memloc);
10159 /* Otherwise, if this is a constant or the object is not in memory
10160 and need be, put it there. */
10161 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10163 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10164 emit_move_insn (memloc, op0);
10165 op0 = memloc;
10166 mem_attrs_from_type = true;
10169 if (offset)
10171 enum machine_mode address_mode;
10172 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10173 EXPAND_SUM);
10175 gcc_assert (MEM_P (op0));
10177 address_mode = get_address_mode (op0);
10178 if (GET_MODE (offset_rtx) != address_mode)
10179 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10181 /* See the comment in expand_assignment for the rationale. */
10182 if (mode1 != VOIDmode
10183 && bitpos != 0
10184 && bitsize > 0
10185 && (bitpos % bitsize) == 0
10186 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10187 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10189 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10190 bitpos = 0;
10193 op0 = offset_address (op0, offset_rtx,
10194 highest_pow2_factor (offset));
10197 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10198 record its alignment as BIGGEST_ALIGNMENT. */
10199 if (MEM_P (op0) && bitpos == 0 && offset != 0
10200 && is_aligning_offset (offset, tem))
10201 set_mem_align (op0, BIGGEST_ALIGNMENT);
10203 /* Don't forget about volatility even if this is a bitfield. */
10204 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10206 if (op0 == orig_op0)
10207 op0 = copy_rtx (op0);
10209 MEM_VOLATILE_P (op0) = 1;
10212 /* In cases where an aligned union has an unaligned object
10213 as a field, we might be extracting a BLKmode value from
10214 an integer-mode (e.g., SImode) object. Handle this case
10215 by doing the extract into an object as wide as the field
10216 (which we know to be the width of a basic mode), then
10217 storing into memory, and changing the mode to BLKmode. */
10218 if (mode1 == VOIDmode
10219 || REG_P (op0) || GET_CODE (op0) == SUBREG
10220 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10221 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10222 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10223 && modifier != EXPAND_CONST_ADDRESS
10224 && modifier != EXPAND_INITIALIZER
10225 && modifier != EXPAND_MEMORY)
10226 /* If the bitfield is volatile and the bitsize
10227 is narrower than the access size of the bitfield,
10228 we need to extract bitfields from the access. */
10229 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10230 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10231 && mode1 != BLKmode
10232 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10233 /* If the field isn't aligned enough to fetch as a memref,
10234 fetch it as a bit field. */
10235 || (mode1 != BLKmode
10236 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10237 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10238 || (MEM_P (op0)
10239 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10240 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10241 && modifier != EXPAND_MEMORY
10242 && ((modifier == EXPAND_CONST_ADDRESS
10243 || modifier == EXPAND_INITIALIZER)
10244 ? STRICT_ALIGNMENT
10245 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10246 || (bitpos % BITS_PER_UNIT != 0)))
10247 /* If the type and the field are a constant size and the
10248 size of the type isn't the same size as the bitfield,
10249 we must use bitfield operations. */
10250 || (bitsize >= 0
10251 && TYPE_SIZE (TREE_TYPE (exp))
10252 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10253 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10254 bitsize)))
10256 enum machine_mode ext_mode = mode;
10258 if (ext_mode == BLKmode
10259 && ! (target != 0 && MEM_P (op0)
10260 && MEM_P (target)
10261 && bitpos % BITS_PER_UNIT == 0))
10262 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10264 if (ext_mode == BLKmode)
10266 if (target == 0)
10267 target = assign_temp (type, 1, 1);
10269 /* ??? Unlike the similar test a few lines below, this one is
10270 very likely obsolete. */
10271 if (bitsize == 0)
10272 return target;
10274 /* In this case, BITPOS must start at a byte boundary and
10275 TARGET, if specified, must be a MEM. */
10276 gcc_assert (MEM_P (op0)
10277 && (!target || MEM_P (target))
10278 && !(bitpos % BITS_PER_UNIT));
10280 emit_block_move (target,
10281 adjust_address (op0, VOIDmode,
10282 bitpos / BITS_PER_UNIT),
10283 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10284 / BITS_PER_UNIT),
10285 (modifier == EXPAND_STACK_PARM
10286 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10288 return target;
10291 /* If we have nothing to extract, the result will be 0 for targets
10292 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10293 return 0 for the sake of consistency, as reading a zero-sized
10294 bitfield is valid in Ada and the value is fully specified. */
10295 if (bitsize == 0)
10296 return const0_rtx;
10298 op0 = validize_mem (op0);
10300 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10301 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10303 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10304 (modifier == EXPAND_STACK_PARM
10305 ? NULL_RTX : target),
10306 ext_mode, ext_mode);
10308 /* If the result is a record type and BITSIZE is narrower than
10309 the mode of OP0, an integral mode, and this is a big endian
10310 machine, we must put the field into the high-order bits. */
10311 if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10312 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10313 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10314 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10315 GET_MODE_BITSIZE (GET_MODE (op0))
10316 - bitsize, op0, 1);
10318 /* If the result type is BLKmode, store the data into a temporary
10319 of the appropriate type, but with the mode corresponding to the
10320 mode for the data we have (op0's mode). */
10321 if (mode == BLKmode)
10323 rtx new_rtx
10324 = assign_stack_temp_for_type (ext_mode,
10325 GET_MODE_BITSIZE (ext_mode),
10326 type);
10327 emit_move_insn (new_rtx, op0);
10328 op0 = copy_rtx (new_rtx);
10329 PUT_MODE (op0, BLKmode);
10332 return op0;
10335 /* If the result is BLKmode, use that to access the object
10336 now as well. */
10337 if (mode == BLKmode)
10338 mode1 = BLKmode;
10340 /* Get a reference to just this component. */
10341 if (modifier == EXPAND_CONST_ADDRESS
10342 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10343 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10344 else
10345 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10347 if (op0 == orig_op0)
10348 op0 = copy_rtx (op0);
10350 /* If op0 is a temporary because of forcing to memory, pass only the
10351 type to set_mem_attributes so that the original expression is never
10352 marked as ADDRESSABLE through MEM_EXPR of the temporary. */
10353 if (mem_attrs_from_type)
10354 set_mem_attributes (op0, type, 0);
10355 else
10356 set_mem_attributes (op0, exp, 0);
10358 if (REG_P (XEXP (op0, 0)))
10359 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10361 MEM_VOLATILE_P (op0) |= volatilep;
10362 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10363 || modifier == EXPAND_CONST_ADDRESS
10364 || modifier == EXPAND_INITIALIZER)
10365 return op0;
10367 if (target == 0)
10368 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10370 convert_move (target, op0, unsignedp);
10371 return target;
10374 case OBJ_TYPE_REF:
10375 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10377 case CALL_EXPR:
10378 /* All valid uses of __builtin_va_arg_pack () are removed during
10379 inlining. */
10380 if (CALL_EXPR_VA_ARG_PACK (exp))
10381 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10383 tree fndecl = get_callee_fndecl (exp), attr;
10385 if (fndecl
10386 && (attr = lookup_attribute ("error",
10387 DECL_ATTRIBUTES (fndecl))) != NULL)
10388 error ("%Kcall to %qs declared with attribute error: %s",
10389 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10390 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10391 if (fndecl
10392 && (attr = lookup_attribute ("warning",
10393 DECL_ATTRIBUTES (fndecl))) != NULL)
10394 warning_at (tree_nonartificial_location (exp),
10395 0, "%Kcall to %qs declared with attribute warning: %s",
10396 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10397 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10399 /* Check for a built-in function. */
10400 if (fndecl && DECL_BUILT_IN (fndecl))
10402 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10403 return expand_builtin (exp, target, subtarget, tmode, ignore);
10406 return expand_call (exp, target, ignore);
10408 case VIEW_CONVERT_EXPR:
10409 op0 = NULL_RTX;
10411 /* If we are converting to BLKmode, try to avoid an intermediate
10412 temporary by fetching an inner memory reference. */
10413 if (mode == BLKmode
10414 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10415 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10416 && handled_component_p (treeop0))
10418 enum machine_mode mode1;
10419 HOST_WIDE_INT bitsize, bitpos;
10420 tree offset;
10421 int unsignedp;
10422 int volatilep = 0;
10423 tree tem
10424 = get_inner_reference (treeop0, &bitsize, &bitpos,
10425 &offset, &mode1, &unsignedp, &volatilep,
10426 true);
10427 rtx orig_op0;
10429 /* ??? We should work harder and deal with non-zero offsets. */
10430 if (!offset
10431 && (bitpos % BITS_PER_UNIT) == 0
10432 && bitsize >= 0
10433 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10435 /* See the normal_inner_ref case for the rationale. */
10436 orig_op0
10437 = expand_expr_real (tem,
10438 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10439 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10440 != INTEGER_CST)
10441 && modifier != EXPAND_STACK_PARM
10442 ? target : NULL_RTX),
10443 VOIDmode,
10444 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10445 NULL, true);
10447 if (MEM_P (orig_op0))
10449 op0 = orig_op0;
10451 /* Get a reference to just this component. */
10452 if (modifier == EXPAND_CONST_ADDRESS
10453 || modifier == EXPAND_SUM
10454 || modifier == EXPAND_INITIALIZER)
10455 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10456 else
10457 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10459 if (op0 == orig_op0)
10460 op0 = copy_rtx (op0);
10462 set_mem_attributes (op0, treeop0, 0);
10463 if (REG_P (XEXP (op0, 0)))
10464 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10466 MEM_VOLATILE_P (op0) |= volatilep;
10471 if (!op0)
10472 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10473 NULL, inner_reference_p);
10475 /* If the input and output modes are both the same, we are done. */
10476 if (mode == GET_MODE (op0))
10478 /* If neither mode is BLKmode, and both modes are the same size
10479 then we can use gen_lowpart. */
10480 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10481 && (GET_MODE_PRECISION (mode)
10482 == GET_MODE_PRECISION (GET_MODE (op0)))
10483 && !COMPLEX_MODE_P (GET_MODE (op0)))
10485 if (GET_CODE (op0) == SUBREG)
10486 op0 = force_reg (GET_MODE (op0), op0);
10487 temp = gen_lowpart_common (mode, op0);
10488 if (temp)
10489 op0 = temp;
10490 else
10492 if (!REG_P (op0) && !MEM_P (op0))
10493 op0 = force_reg (GET_MODE (op0), op0);
10494 op0 = gen_lowpart (mode, op0);
10497 /* If both types are integral, convert from one mode to the other. */
10498 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10499 op0 = convert_modes (mode, GET_MODE (op0), op0,
10500 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10501 /* If the output type is a bit-field type, do an extraction. */
10502 else if (reduce_bit_field)
10503 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10504 TYPE_UNSIGNED (type), NULL_RTX,
10505 mode, mode);
10506 /* As a last resort, spill op0 to memory, and reload it in a
10507 different mode. */
10508 else if (!MEM_P (op0))
10510 /* If the operand is not a MEM, force it into memory. Since we
10511 are going to be changing the mode of the MEM, don't call
10512 force_const_mem for constants because we don't allow pool
10513 constants to change mode. */
10514 tree inner_type = TREE_TYPE (treeop0);
10516 gcc_assert (!TREE_ADDRESSABLE (exp));
10518 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10519 target
10520 = assign_stack_temp_for_type
10521 (TYPE_MODE (inner_type),
10522 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10524 emit_move_insn (target, op0);
10525 op0 = target;
10528 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10529 output type is such that the operand is known to be aligned, indicate
10530 that it is. Otherwise, we need only be concerned about alignment for
10531 non-BLKmode results. */
10532 if (MEM_P (op0))
10534 enum insn_code icode;
10536 if (TYPE_ALIGN_OK (type))
10538 /* ??? Copying the MEM without substantially changing it might
10539 run afoul of the code handling volatile memory references in
10540 store_expr, which assumes that TARGET is returned unmodified
10541 if it has been used. */
10542 op0 = copy_rtx (op0);
10543 set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10545 else if (modifier != EXPAND_WRITE
10546 && modifier != EXPAND_MEMORY
10547 && !inner_reference_p
10548 && mode != BLKmode
10549 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10551 /* If the target does have special handling for unaligned
10552 loads of mode then use them. */
10553 if ((icode = optab_handler (movmisalign_optab, mode))
10554 != CODE_FOR_nothing)
10556 rtx reg, insn;
10558 op0 = adjust_address (op0, mode, 0);
10559 /* We've already validated the memory, and we're creating a
10560 new pseudo destination. The predicates really can't
10561 fail. */
10562 reg = gen_reg_rtx (mode);
10564 /* Nor can the insn generator. */
10565 insn = GEN_FCN (icode) (reg, op0);
10566 emit_insn (insn);
10567 return reg;
10569 else if (STRICT_ALIGNMENT)
10571 tree inner_type = TREE_TYPE (treeop0);
10572 HOST_WIDE_INT temp_size
10573 = MAX (int_size_in_bytes (inner_type),
10574 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10575 rtx new_rtx
10576 = assign_stack_temp_for_type (mode, temp_size, type);
10577 rtx new_with_op0_mode
10578 = adjust_address (new_rtx, GET_MODE (op0), 0);
10580 gcc_assert (!TREE_ADDRESSABLE (exp));
10582 if (GET_MODE (op0) == BLKmode)
10583 emit_block_move (new_with_op0_mode, op0,
10584 GEN_INT (GET_MODE_SIZE (mode)),
10585 (modifier == EXPAND_STACK_PARM
10586 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10587 else
10588 emit_move_insn (new_with_op0_mode, op0);
10590 op0 = new_rtx;
10594 op0 = adjust_address (op0, mode, 0);
10597 return op0;
10599 case MODIFY_EXPR:
10601 tree lhs = treeop0;
10602 tree rhs = treeop1;
10603 gcc_assert (ignore);
10605 /* Check for |= or &= of a bitfield of size one into another bitfield
10606 of size 1. In this case, (unless we need the result of the
10607 assignment) we can do this more efficiently with a
10608 test followed by an assignment, if necessary.
10610 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10611 things change so we do, this code should be enhanced to
10612 support it. */
10613 if (TREE_CODE (lhs) == COMPONENT_REF
10614 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10615 || TREE_CODE (rhs) == BIT_AND_EXPR)
10616 && TREE_OPERAND (rhs, 0) == lhs
10617 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10618 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10619 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10621 rtx_code_label *label = gen_label_rtx ();
10622 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10623 do_jump (TREE_OPERAND (rhs, 1),
10624 value ? label : 0,
10625 value ? 0 : label, -1);
10626 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10627 false);
10628 do_pending_stack_adjust ();
10629 emit_label (label);
10630 return const0_rtx;
10633 expand_assignment (lhs, rhs, false);
10634 return const0_rtx;
10637 case ADDR_EXPR:
10638 return expand_expr_addr_expr (exp, target, tmode, modifier);
10640 case REALPART_EXPR:
10641 op0 = expand_normal (treeop0);
10642 return read_complex_part (op0, false);
10644 case IMAGPART_EXPR:
10645 op0 = expand_normal (treeop0);
10646 return read_complex_part (op0, true);
10648 case RETURN_EXPR:
10649 case LABEL_EXPR:
10650 case GOTO_EXPR:
10651 case SWITCH_EXPR:
10652 case ASM_EXPR:
10653 /* Expanded in cfgexpand.c. */
10654 gcc_unreachable ();
10656 case TRY_CATCH_EXPR:
10657 case CATCH_EXPR:
10658 case EH_FILTER_EXPR:
10659 case TRY_FINALLY_EXPR:
10660 /* Lowered by tree-eh.c. */
10661 gcc_unreachable ();
10663 case WITH_CLEANUP_EXPR:
10664 case CLEANUP_POINT_EXPR:
10665 case TARGET_EXPR:
10666 case CASE_LABEL_EXPR:
10667 case VA_ARG_EXPR:
10668 case BIND_EXPR:
10669 case INIT_EXPR:
10670 case CONJ_EXPR:
10671 case COMPOUND_EXPR:
10672 case PREINCREMENT_EXPR:
10673 case PREDECREMENT_EXPR:
10674 case POSTINCREMENT_EXPR:
10675 case POSTDECREMENT_EXPR:
10676 case LOOP_EXPR:
10677 case EXIT_EXPR:
10678 case COMPOUND_LITERAL_EXPR:
10679 /* Lowered by gimplify.c. */
10680 gcc_unreachable ();
10682 case FDESC_EXPR:
10683 /* Function descriptors are not valid except for as
10684 initialization constants, and should not be expanded. */
10685 gcc_unreachable ();
10687 case WITH_SIZE_EXPR:
10688 /* WITH_SIZE_EXPR expands to its first argument. The caller should
10689 have pulled out the size to use in whatever context it needed. */
10690 return expand_expr_real (treeop0, original_target, tmode,
10691 modifier, alt_rtl, inner_reference_p);
10693 default:
10694 return expand_expr_real_2 (&ops, target, tmode, modifier);
10698 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10699 signedness of TYPE), possibly returning the result in TARGET. */
10700 static rtx
10701 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10703 HOST_WIDE_INT prec = TYPE_PRECISION (type);
10704 if (target && GET_MODE (target) != GET_MODE (exp))
10705 target = 0;
10706 /* For constant values, reduce using build_int_cst_type. */
10707 if (CONST_INT_P (exp))
10709 HOST_WIDE_INT value = INTVAL (exp);
10710 tree t = build_int_cst_type (type, value);
10711 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10713 else if (TYPE_UNSIGNED (type))
10715 enum machine_mode mode = GET_MODE (exp);
10716 rtx mask = immed_wide_int_const
10717 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
10718 return expand_and (mode, exp, mask, target);
10720 else
10722 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10723 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10724 exp, count, target, 0);
10725 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10726 exp, count, target, 0);
10730 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10731 when applied to the address of EXP produces an address known to be
10732 aligned more than BIGGEST_ALIGNMENT. */
10734 static int
10735 is_aligning_offset (const_tree offset, const_tree exp)
10737 /* Strip off any conversions. */
10738 while (CONVERT_EXPR_P (offset))
10739 offset = TREE_OPERAND (offset, 0);
10741 /* We must now have a BIT_AND_EXPR with a constant that is one less than
10742 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
10743 if (TREE_CODE (offset) != BIT_AND_EXPR
10744 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
10745 || compare_tree_int (TREE_OPERAND (offset, 1),
10746 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10747 || exact_log2 (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1) < 0)
10748 return 0;
10750 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10751 It must be NEGATE_EXPR. Then strip any more conversions. */
10752 offset = TREE_OPERAND (offset, 0);
10753 while (CONVERT_EXPR_P (offset))
10754 offset = TREE_OPERAND (offset, 0);
10756 if (TREE_CODE (offset) != NEGATE_EXPR)
10757 return 0;
10759 offset = TREE_OPERAND (offset, 0);
10760 while (CONVERT_EXPR_P (offset))
10761 offset = TREE_OPERAND (offset, 0);
10763 /* This must now be the address of EXP. */
10764 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10767 /* Return the tree node if an ARG corresponds to a string constant or zero
10768 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
10769 in bytes within the string that ARG is accessing. The type of the
10770 offset will be `sizetype'. */
10772 tree
10773 string_constant (tree arg, tree *ptr_offset)
10775 tree array, offset, lower_bound;
10776 STRIP_NOPS (arg);
10778 if (TREE_CODE (arg) == ADDR_EXPR)
10780 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10782 *ptr_offset = size_zero_node;
10783 return TREE_OPERAND (arg, 0);
10785 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10787 array = TREE_OPERAND (arg, 0);
10788 offset = size_zero_node;
10790 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10792 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10793 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10794 if (TREE_CODE (array) != STRING_CST
10795 && TREE_CODE (array) != VAR_DECL)
10796 return 0;
10798 /* Check if the array has a nonzero lower bound. */
10799 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10800 if (!integer_zerop (lower_bound))
10802 /* If the offset and base aren't both constants, return 0. */
10803 if (TREE_CODE (lower_bound) != INTEGER_CST)
10804 return 0;
10805 if (TREE_CODE (offset) != INTEGER_CST)
10806 return 0;
10807 /* Adjust offset by the lower bound. */
10808 offset = size_diffop (fold_convert (sizetype, offset),
10809 fold_convert (sizetype, lower_bound));
10812 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10814 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10815 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10816 if (TREE_CODE (array) != ADDR_EXPR)
10817 return 0;
10818 array = TREE_OPERAND (array, 0);
10819 if (TREE_CODE (array) != STRING_CST
10820 && TREE_CODE (array) != VAR_DECL)
10821 return 0;
10823 else
10824 return 0;
10826 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10828 tree arg0 = TREE_OPERAND (arg, 0);
10829 tree arg1 = TREE_OPERAND (arg, 1);
10831 STRIP_NOPS (arg0);
10832 STRIP_NOPS (arg1);
10834 if (TREE_CODE (arg0) == ADDR_EXPR
10835 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10836 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10838 array = TREE_OPERAND (arg0, 0);
10839 offset = arg1;
10841 else if (TREE_CODE (arg1) == ADDR_EXPR
10842 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10843 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10845 array = TREE_OPERAND (arg1, 0);
10846 offset = arg0;
10848 else
10849 return 0;
10851 else
10852 return 0;
10854 if (TREE_CODE (array) == STRING_CST)
10856 *ptr_offset = fold_convert (sizetype, offset);
10857 return array;
10859 else if (TREE_CODE (array) == VAR_DECL
10860 || TREE_CODE (array) == CONST_DECL)
10862 int length;
10863 tree init = ctor_for_folding (array);
10865 /* Variables initialized to string literals can be handled too. */
10866 if (init == error_mark_node
10867 || !init
10868 || TREE_CODE (init) != STRING_CST)
10869 return 0;
10871 /* Avoid const char foo[4] = "abcde"; */
10872 if (DECL_SIZE_UNIT (array) == NULL_TREE
10873 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10874 || (length = TREE_STRING_LENGTH (init)) <= 0
10875 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10876 return 0;
10878 /* If variable is bigger than the string literal, OFFSET must be constant
10879 and inside of the bounds of the string literal. */
10880 offset = fold_convert (sizetype, offset);
10881 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10882 && (! tree_fits_uhwi_p (offset)
10883 || compare_tree_int (offset, length) >= 0))
10884 return 0;
10886 *ptr_offset = offset;
10887 return init;
10890 return 0;
10893 /* Generate code to calculate OPS, and exploded expression
10894 using a store-flag instruction and return an rtx for the result.
10895 OPS reflects a comparison.
10897 If TARGET is nonzero, store the result there if convenient.
10899 Return zero if there is no suitable set-flag instruction
10900 available on this machine.
10902 Once expand_expr has been called on the arguments of the comparison,
10903 we are committed to doing the store flag, since it is not safe to
10904 re-evaluate the expression. We emit the store-flag insn by calling
10905 emit_store_flag, but only expand the arguments if we have a reason
10906 to believe that emit_store_flag will be successful. If we think that
10907 it will, but it isn't, we have to simulate the store-flag with a
10908 set/jump/set sequence. */
10910 static rtx
10911 do_store_flag (sepops ops, rtx target, enum machine_mode mode)
10913 enum rtx_code code;
10914 tree arg0, arg1, type;
10915 tree tem;
10916 enum machine_mode operand_mode;
10917 int unsignedp;
10918 rtx op0, op1;
10919 rtx subtarget = target;
10920 location_t loc = ops->location;
10922 arg0 = ops->op0;
10923 arg1 = ops->op1;
10925 /* Don't crash if the comparison was erroneous. */
10926 if (arg0 == error_mark_node || arg1 == error_mark_node)
10927 return const0_rtx;
10929 type = TREE_TYPE (arg0);
10930 operand_mode = TYPE_MODE (type);
10931 unsignedp = TYPE_UNSIGNED (type);
10933 /* We won't bother with BLKmode store-flag operations because it would mean
10934 passing a lot of information to emit_store_flag. */
10935 if (operand_mode == BLKmode)
10936 return 0;
10938 /* We won't bother with store-flag operations involving function pointers
10939 when function pointers must be canonicalized before comparisons. */
10940 #ifdef HAVE_canonicalize_funcptr_for_compare
10941 if (HAVE_canonicalize_funcptr_for_compare
10942 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10943 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10944 == FUNCTION_TYPE))
10945 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10946 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10947 == FUNCTION_TYPE))))
10948 return 0;
10949 #endif
10951 STRIP_NOPS (arg0);
10952 STRIP_NOPS (arg1);
10954 /* For vector typed comparisons emit code to generate the desired
10955 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
10956 expander for this. */
10957 if (TREE_CODE (ops->type) == VECTOR_TYPE)
10959 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10960 tree if_true = constant_boolean_node (true, ops->type);
10961 tree if_false = constant_boolean_node (false, ops->type);
10962 return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10965 /* Get the rtx comparison code to use. We know that EXP is a comparison
10966 operation of some type. Some comparisons against 1 and -1 can be
10967 converted to comparisons with zero. Do so here so that the tests
10968 below will be aware that we have a comparison with zero. These
10969 tests will not catch constants in the first operand, but constants
10970 are rarely passed as the first operand. */
10972 switch (ops->code)
10974 case EQ_EXPR:
10975 code = EQ;
10976 break;
10977 case NE_EXPR:
10978 code = NE;
10979 break;
10980 case LT_EXPR:
10981 if (integer_onep (arg1))
10982 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10983 else
10984 code = unsignedp ? LTU : LT;
10985 break;
10986 case LE_EXPR:
10987 if (! unsignedp && integer_all_onesp (arg1))
10988 arg1 = integer_zero_node, code = LT;
10989 else
10990 code = unsignedp ? LEU : LE;
10991 break;
10992 case GT_EXPR:
10993 if (! unsignedp && integer_all_onesp (arg1))
10994 arg1 = integer_zero_node, code = GE;
10995 else
10996 code = unsignedp ? GTU : GT;
10997 break;
10998 case GE_EXPR:
10999 if (integer_onep (arg1))
11000 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11001 else
11002 code = unsignedp ? GEU : GE;
11003 break;
11005 case UNORDERED_EXPR:
11006 code = UNORDERED;
11007 break;
11008 case ORDERED_EXPR:
11009 code = ORDERED;
11010 break;
11011 case UNLT_EXPR:
11012 code = UNLT;
11013 break;
11014 case UNLE_EXPR:
11015 code = UNLE;
11016 break;
11017 case UNGT_EXPR:
11018 code = UNGT;
11019 break;
11020 case UNGE_EXPR:
11021 code = UNGE;
11022 break;
11023 case UNEQ_EXPR:
11024 code = UNEQ;
11025 break;
11026 case LTGT_EXPR:
11027 code = LTGT;
11028 break;
11030 default:
11031 gcc_unreachable ();
11034 /* Put a constant second. */
11035 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11036 || TREE_CODE (arg0) == FIXED_CST)
11038 tem = arg0; arg0 = arg1; arg1 = tem;
11039 code = swap_condition (code);
11042 /* If this is an equality or inequality test of a single bit, we can
11043 do this by shifting the bit being tested to the low-order bit and
11044 masking the result with the constant 1. If the condition was EQ,
11045 we xor it with 1. This does not require an scc insn and is faster
11046 than an scc insn even if we have it.
11048 The code to make this transformation was moved into fold_single_bit_test,
11049 so we just call into the folder and expand its result. */
11051 if ((code == NE || code == EQ)
11052 && integer_zerop (arg1)
11053 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11055 gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11056 if (srcstmt
11057 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11059 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11060 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11061 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11062 gimple_assign_rhs1 (srcstmt),
11063 gimple_assign_rhs2 (srcstmt));
11064 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11065 if (temp)
11066 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11070 if (! get_subtarget (target)
11071 || GET_MODE (subtarget) != operand_mode)
11072 subtarget = 0;
11074 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11076 if (target == 0)
11077 target = gen_reg_rtx (mode);
11079 /* Try a cstore if possible. */
11080 return emit_store_flag_force (target, code, op0, op1,
11081 operand_mode, unsignedp,
11082 (TYPE_PRECISION (ops->type) == 1
11083 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11087 /* Stubs in case we haven't got a casesi insn. */
11088 #ifndef HAVE_casesi
11089 # define HAVE_casesi 0
11090 # define gen_casesi(a, b, c, d, e) (0)
11091 # define CODE_FOR_casesi CODE_FOR_nothing
11092 #endif
11094 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11095 0 otherwise (i.e. if there is no casesi instruction).
11097 DEFAULT_PROBABILITY is the probability of jumping to the default
11098 label. */
11100 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11101 rtx table_label, rtx default_label, rtx fallback_label,
11102 int default_probability)
11104 struct expand_operand ops[5];
11105 enum machine_mode index_mode = SImode;
11106 rtx op1, op2, index;
11108 if (! HAVE_casesi)
11109 return 0;
11111 /* Convert the index to SImode. */
11112 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11114 enum machine_mode omode = TYPE_MODE (index_type);
11115 rtx rangertx = expand_normal (range);
11117 /* We must handle the endpoints in the original mode. */
11118 index_expr = build2 (MINUS_EXPR, index_type,
11119 index_expr, minval);
11120 minval = integer_zero_node;
11121 index = expand_normal (index_expr);
11122 if (default_label)
11123 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11124 omode, 1, default_label,
11125 default_probability);
11126 /* Now we can safely truncate. */
11127 index = convert_to_mode (index_mode, index, 0);
11129 else
11131 if (TYPE_MODE (index_type) != index_mode)
11133 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11134 index_expr = fold_convert (index_type, index_expr);
11137 index = expand_normal (index_expr);
11140 do_pending_stack_adjust ();
11142 op1 = expand_normal (minval);
11143 op2 = expand_normal (range);
11145 create_input_operand (&ops[0], index, index_mode);
11146 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11147 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11148 create_fixed_operand (&ops[3], table_label);
11149 create_fixed_operand (&ops[4], (default_label
11150 ? default_label
11151 : fallback_label));
11152 expand_jump_insn (CODE_FOR_casesi, 5, ops);
11153 return 1;
11156 /* Attempt to generate a tablejump instruction; same concept. */
11157 #ifndef HAVE_tablejump
11158 #define HAVE_tablejump 0
11159 #define gen_tablejump(x, y) (0)
11160 #endif
11162 /* Subroutine of the next function.
11164 INDEX is the value being switched on, with the lowest value
11165 in the table already subtracted.
11166 MODE is its expected mode (needed if INDEX is constant).
11167 RANGE is the length of the jump table.
11168 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11170 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11171 index value is out of range.
11172 DEFAULT_PROBABILITY is the probability of jumping to
11173 the default label. */
11175 static void
11176 do_tablejump (rtx index, enum machine_mode mode, rtx range, rtx table_label,
11177 rtx default_label, int default_probability)
11179 rtx temp, vector;
11181 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11182 cfun->cfg->max_jumptable_ents = INTVAL (range);
11184 /* Do an unsigned comparison (in the proper mode) between the index
11185 expression and the value which represents the length of the range.
11186 Since we just finished subtracting the lower bound of the range
11187 from the index expression, this comparison allows us to simultaneously
11188 check that the original index expression value is both greater than
11189 or equal to the minimum value of the range and less than or equal to
11190 the maximum value of the range. */
11192 if (default_label)
11193 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11194 default_label, default_probability);
11197 /* If index is in range, it must fit in Pmode.
11198 Convert to Pmode so we can index with it. */
11199 if (mode != Pmode)
11200 index = convert_to_mode (Pmode, index, 1);
11202 /* Don't let a MEM slip through, because then INDEX that comes
11203 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11204 and break_out_memory_refs will go to work on it and mess it up. */
11205 #ifdef PIC_CASE_VECTOR_ADDRESS
11206 if (flag_pic && !REG_P (index))
11207 index = copy_to_mode_reg (Pmode, index);
11208 #endif
11210 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11211 GET_MODE_SIZE, because this indicates how large insns are. The other
11212 uses should all be Pmode, because they are addresses. This code
11213 could fail if addresses and insns are not the same size. */
11214 index = simplify_gen_binary (MULT, Pmode, index,
11215 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11216 Pmode));
11217 index = simplify_gen_binary (PLUS, Pmode, index,
11218 gen_rtx_LABEL_REF (Pmode, table_label));
11220 #ifdef PIC_CASE_VECTOR_ADDRESS
11221 if (flag_pic)
11222 index = PIC_CASE_VECTOR_ADDRESS (index);
11223 else
11224 #endif
11225 index = memory_address (CASE_VECTOR_MODE, index);
11226 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11227 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11228 convert_move (temp, vector, 0);
11230 emit_jump_insn (gen_tablejump (temp, table_label));
11232 /* If we are generating PIC code or if the table is PC-relative, the
11233 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11234 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11235 emit_barrier ();
11239 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11240 rtx table_label, rtx default_label, int default_probability)
11242 rtx index;
11244 if (! HAVE_tablejump)
11245 return 0;
11247 index_expr = fold_build2 (MINUS_EXPR, index_type,
11248 fold_convert (index_type, index_expr),
11249 fold_convert (index_type, minval));
11250 index = expand_normal (index_expr);
11251 do_pending_stack_adjust ();
11253 do_tablejump (index, TYPE_MODE (index_type),
11254 convert_modes (TYPE_MODE (index_type),
11255 TYPE_MODE (TREE_TYPE (range)),
11256 expand_normal (range),
11257 TYPE_UNSIGNED (TREE_TYPE (range))),
11258 table_label, default_label, default_probability);
11259 return 1;
11262 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11263 static rtx
11264 const_vector_from_tree (tree exp)
11266 rtvec v;
11267 unsigned i;
11268 int units;
11269 tree elt;
11270 enum machine_mode inner, mode;
11272 mode = TYPE_MODE (TREE_TYPE (exp));
11274 if (initializer_zerop (exp))
11275 return CONST0_RTX (mode);
11277 units = GET_MODE_NUNITS (mode);
11278 inner = GET_MODE_INNER (mode);
11280 v = rtvec_alloc (units);
11282 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11284 elt = VECTOR_CST_ELT (exp, i);
11286 if (TREE_CODE (elt) == REAL_CST)
11287 RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11288 inner);
11289 else if (TREE_CODE (elt) == FIXED_CST)
11290 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11291 inner);
11292 else
11293 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11296 return gen_rtx_CONST_VECTOR (mode, v);
11299 /* Build a decl for a personality function given a language prefix. */
11301 tree
11302 build_personality_function (const char *lang)
11304 const char *unwind_and_version;
11305 tree decl, type;
11306 char *name;
11308 switch (targetm_common.except_unwind_info (&global_options))
11310 case UI_NONE:
11311 return NULL;
11312 case UI_SJLJ:
11313 unwind_and_version = "_sj0";
11314 break;
11315 case UI_DWARF2:
11316 case UI_TARGET:
11317 unwind_and_version = "_v0";
11318 break;
11319 case UI_SEH:
11320 unwind_and_version = "_seh0";
11321 break;
11322 default:
11323 gcc_unreachable ();
11326 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11328 type = build_function_type_list (integer_type_node, integer_type_node,
11329 long_long_unsigned_type_node,
11330 ptr_type_node, ptr_type_node, NULL_TREE);
11331 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11332 get_identifier (name), type);
11333 DECL_ARTIFICIAL (decl) = 1;
11334 DECL_EXTERNAL (decl) = 1;
11335 TREE_PUBLIC (decl) = 1;
11337 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11338 are the flags assigned by targetm.encode_section_info. */
11339 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11341 return decl;
11344 /* Extracts the personality function of DECL and returns the corresponding
11345 libfunc. */
11348 get_personality_function (tree decl)
11350 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11351 enum eh_personality_kind pk;
11353 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11354 if (pk == eh_personality_none)
11355 return NULL;
11357 if (!personality
11358 && pk == eh_personality_any)
11359 personality = lang_hooks.eh_personality ();
11361 if (pk == eh_personality_lang)
11362 gcc_assert (personality != NULL_TREE);
11364 return XEXP (DECL_RTL (personality), 0);
11367 #include "gt-expr.h"