* fi.po: Update.
[official-gcc.git] / gcc / expr.c
bloba7692157a5fb84e6d501c3c46536645c0b251df3
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2017 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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "ssa.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "regs.h"
35 #include "emit-rtl.h"
36 #include "recog.h"
37 #include "cgraph.h"
38 #include "diagnostic.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "stor-layout.h"
42 #include "attribs.h"
43 #include "varasm.h"
44 #include "except.h"
45 #include "insn-attr.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "stmt.h"
50 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
51 #include "expr.h"
52 #include "optabs-tree.h"
53 #include "libfuncs.h"
54 #include "reload.h"
55 #include "langhooks.h"
56 #include "common/common-target.h"
57 #include "tree-ssa-live.h"
58 #include "tree-outof-ssa.h"
59 #include "tree-ssa-address.h"
60 #include "builtins.h"
61 #include "tree-chkp.h"
62 #include "rtl-chkp.h"
63 #include "ccmp.h"
66 /* If this is nonzero, we do not bother generating VOLATILE
67 around volatile memory references, and we are willing to
68 output indirect addresses. If cse is to follow, we reject
69 indirect addresses so a useful potential cse is generated;
70 if it is used only once, instruction combination will produce
71 the same indirect address eventually. */
72 int cse_not_expected;
74 static bool block_move_libcall_safe_for_call_parm (void);
75 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
76 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT);
78 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
79 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
80 static rtx_insn *compress_float_constant (rtx, rtx);
81 static rtx get_subtarget (rtx);
82 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
83 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
84 unsigned HOST_WIDE_INT, machine_mode,
85 tree, int, alias_set_type, bool);
86 static void store_constructor (tree, rtx, int, HOST_WIDE_INT, bool);
87 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
88 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
89 machine_mode, tree, alias_set_type, bool, bool);
91 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
93 static int is_aligning_offset (const_tree, const_tree);
94 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
95 static rtx do_store_flag (sepops, rtx, machine_mode);
96 #ifdef PUSH_ROUNDING
97 static void emit_single_push_insn (machine_mode, rtx, tree);
98 #endif
99 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
100 static rtx const_vector_from_tree (tree);
101 static rtx const_scalar_mask_from_tree (tree);
102 static tree tree_expr_size (const_tree);
103 static HOST_WIDE_INT int_expr_size (tree);
106 /* This is run to set up which modes can be used
107 directly in memory and to initialize the block move optab. It is run
108 at the beginning of compilation and when the target is reinitialized. */
110 void
111 init_expr_target (void)
113 rtx pat;
114 machine_mode mode;
115 int num_clobbers;
116 rtx mem, mem1;
117 rtx reg;
119 /* Try indexing by frame ptr and try by stack ptr.
120 It is known that on the Convex the stack ptr isn't a valid index.
121 With luck, one or the other is valid on any machine. */
122 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
123 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
125 /* A scratch register we can modify in-place below to avoid
126 useless RTL allocations. */
127 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
129 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
130 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
131 PATTERN (insn) = pat;
133 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
134 mode = (machine_mode) ((int) mode + 1))
136 int regno;
138 direct_load[(int) mode] = direct_store[(int) mode] = 0;
139 PUT_MODE (mem, mode);
140 PUT_MODE (mem1, mode);
142 /* See if there is some register that can be used in this mode and
143 directly loaded or stored from memory. */
145 if (mode != VOIDmode && mode != BLKmode)
146 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
147 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
148 regno++)
150 if (! HARD_REGNO_MODE_OK (regno, mode))
151 continue;
153 set_mode_and_regno (reg, mode, regno);
155 SET_SRC (pat) = mem;
156 SET_DEST (pat) = reg;
157 if (recog (pat, insn, &num_clobbers) >= 0)
158 direct_load[(int) mode] = 1;
160 SET_SRC (pat) = mem1;
161 SET_DEST (pat) = reg;
162 if (recog (pat, insn, &num_clobbers) >= 0)
163 direct_load[(int) mode] = 1;
165 SET_SRC (pat) = reg;
166 SET_DEST (pat) = mem;
167 if (recog (pat, insn, &num_clobbers) >= 0)
168 direct_store[(int) mode] = 1;
170 SET_SRC (pat) = reg;
171 SET_DEST (pat) = mem1;
172 if (recog (pat, insn, &num_clobbers) >= 0)
173 direct_store[(int) mode] = 1;
177 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
179 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
180 mode = GET_MODE_WIDER_MODE (mode))
182 machine_mode srcmode;
183 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
184 srcmode = GET_MODE_WIDER_MODE (srcmode))
186 enum insn_code ic;
188 ic = can_extend_p (mode, srcmode, 0);
189 if (ic == CODE_FOR_nothing)
190 continue;
192 PUT_MODE (mem, srcmode);
194 if (insn_operand_matches (ic, 1, mem))
195 float_extend_from_mem[mode][srcmode] = true;
200 /* This is run at the start of compiling a function. */
202 void
203 init_expr (void)
205 memset (&crtl->expr, 0, sizeof (crtl->expr));
208 /* Copy data from FROM to TO, where the machine modes are not the same.
209 Both modes may be integer, or both may be floating, or both may be
210 fixed-point.
211 UNSIGNEDP should be nonzero if FROM is an unsigned type.
212 This causes zero-extension instead of sign-extension. */
214 void
215 convert_move (rtx to, rtx from, int unsignedp)
217 machine_mode to_mode = GET_MODE (to);
218 machine_mode from_mode = GET_MODE (from);
219 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
220 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
221 enum insn_code code;
222 rtx libcall;
224 /* rtx code for making an equivalent value. */
225 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
226 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
229 gcc_assert (to_real == from_real);
230 gcc_assert (to_mode != BLKmode);
231 gcc_assert (from_mode != BLKmode);
233 /* If the source and destination are already the same, then there's
234 nothing to do. */
235 if (to == from)
236 return;
238 /* If FROM is a SUBREG that indicates that we have already done at least
239 the required extension, strip it. We don't handle such SUBREGs as
240 TO here. */
242 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
243 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
244 >= GET_MODE_PRECISION (to_mode))
245 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
246 from = gen_lowpart (to_mode, from), from_mode = to_mode;
248 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
250 if (to_mode == from_mode
251 || (from_mode == VOIDmode && CONSTANT_P (from)))
253 emit_move_insn (to, from);
254 return;
257 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
259 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
261 if (VECTOR_MODE_P (to_mode))
262 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
263 else
264 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
266 emit_move_insn (to, from);
267 return;
270 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
272 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
273 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
274 return;
277 if (to_real)
279 rtx value;
280 rtx_insn *insns;
281 convert_optab tab;
283 gcc_assert ((GET_MODE_PRECISION (from_mode)
284 != GET_MODE_PRECISION (to_mode))
285 || (DECIMAL_FLOAT_MODE_P (from_mode)
286 != DECIMAL_FLOAT_MODE_P (to_mode)));
288 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
289 /* Conversion between decimal float and binary float, same size. */
290 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
291 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
292 tab = sext_optab;
293 else
294 tab = trunc_optab;
296 /* Try converting directly if the insn is supported. */
298 code = convert_optab_handler (tab, to_mode, from_mode);
299 if (code != CODE_FOR_nothing)
301 emit_unop_insn (code, to, from,
302 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
303 return;
306 /* Otherwise use a libcall. */
307 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
309 /* Is this conversion implemented yet? */
310 gcc_assert (libcall);
312 start_sequence ();
313 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
314 1, from, from_mode);
315 insns = get_insns ();
316 end_sequence ();
317 emit_libcall_block (insns, to, value,
318 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
319 from)
320 : gen_rtx_FLOAT_EXTEND (to_mode, from));
321 return;
324 /* Handle pointer conversion. */ /* SPEE 900220. */
325 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
327 convert_optab ctab;
329 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
330 ctab = trunc_optab;
331 else if (unsignedp)
332 ctab = zext_optab;
333 else
334 ctab = sext_optab;
336 if (convert_optab_handler (ctab, to_mode, from_mode)
337 != CODE_FOR_nothing)
339 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
340 to, from, UNKNOWN);
341 return;
345 /* Targets are expected to provide conversion insns between PxImode and
346 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
347 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
349 machine_mode full_mode
350 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
352 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
353 != CODE_FOR_nothing);
355 if (full_mode != from_mode)
356 from = convert_to_mode (full_mode, from, unsignedp);
357 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
358 to, from, UNKNOWN);
359 return;
361 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
363 rtx new_from;
364 machine_mode full_mode
365 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
366 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
367 enum insn_code icode;
369 icode = convert_optab_handler (ctab, full_mode, from_mode);
370 gcc_assert (icode != CODE_FOR_nothing);
372 if (to_mode == full_mode)
374 emit_unop_insn (icode, to, from, UNKNOWN);
375 return;
378 new_from = gen_reg_rtx (full_mode);
379 emit_unop_insn (icode, new_from, from, UNKNOWN);
381 /* else proceed to integer conversions below. */
382 from_mode = full_mode;
383 from = new_from;
386 /* Make sure both are fixed-point modes or both are not. */
387 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
388 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
389 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
391 /* If we widen from_mode to to_mode and they are in the same class,
392 we won't saturate the result.
393 Otherwise, always saturate the result to play safe. */
394 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
395 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
396 expand_fixed_convert (to, from, 0, 0);
397 else
398 expand_fixed_convert (to, from, 0, 1);
399 return;
402 /* Now both modes are integers. */
404 /* Handle expanding beyond a word. */
405 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
406 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
408 rtx_insn *insns;
409 rtx lowpart;
410 rtx fill_value;
411 rtx lowfrom;
412 int i;
413 machine_mode lowpart_mode;
414 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
416 /* Try converting directly if the insn is supported. */
417 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
418 != CODE_FOR_nothing)
420 /* If FROM is a SUBREG, put it into a register. Do this
421 so that we always generate the same set of insns for
422 better cse'ing; if an intermediate assignment occurred,
423 we won't be doing the operation directly on the SUBREG. */
424 if (optimize > 0 && GET_CODE (from) == SUBREG)
425 from = force_reg (from_mode, from);
426 emit_unop_insn (code, to, from, equiv_code);
427 return;
429 /* Next, try converting via full word. */
430 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
431 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
432 != CODE_FOR_nothing))
434 rtx word_to = gen_reg_rtx (word_mode);
435 if (REG_P (to))
437 if (reg_overlap_mentioned_p (to, from))
438 from = force_reg (from_mode, from);
439 emit_clobber (to);
441 convert_move (word_to, from, unsignedp);
442 emit_unop_insn (code, to, word_to, equiv_code);
443 return;
446 /* No special multiword conversion insn; do it by hand. */
447 start_sequence ();
449 /* Since we will turn this into a no conflict block, we must ensure
450 the source does not overlap the target so force it into an isolated
451 register when maybe so. Likewise for any MEM input, since the
452 conversion sequence might require several references to it and we
453 must ensure we're getting the same value every time. */
455 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
456 from = force_reg (from_mode, from);
458 /* Get a copy of FROM widened to a word, if necessary. */
459 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
460 lowpart_mode = word_mode;
461 else
462 lowpart_mode = from_mode;
464 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
466 lowpart = gen_lowpart (lowpart_mode, to);
467 emit_move_insn (lowpart, lowfrom);
469 /* Compute the value to put in each remaining word. */
470 if (unsignedp)
471 fill_value = const0_rtx;
472 else
473 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
474 LT, lowfrom, const0_rtx,
475 lowpart_mode, 0, -1);
477 /* Fill the remaining words. */
478 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
480 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
481 rtx subword = operand_subword (to, index, 1, to_mode);
483 gcc_assert (subword);
485 if (fill_value != subword)
486 emit_move_insn (subword, fill_value);
489 insns = get_insns ();
490 end_sequence ();
492 emit_insn (insns);
493 return;
496 /* Truncating multi-word to a word or less. */
497 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
498 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
500 if (!((MEM_P (from)
501 && ! MEM_VOLATILE_P (from)
502 && direct_load[(int) to_mode]
503 && ! mode_dependent_address_p (XEXP (from, 0),
504 MEM_ADDR_SPACE (from)))
505 || REG_P (from)
506 || GET_CODE (from) == SUBREG))
507 from = force_reg (from_mode, from);
508 convert_move (to, gen_lowpart (word_mode, from), 0);
509 return;
512 /* Now follow all the conversions between integers
513 no more than a word long. */
515 /* For truncation, usually we can just refer to FROM in a narrower mode. */
516 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
517 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
519 if (!((MEM_P (from)
520 && ! MEM_VOLATILE_P (from)
521 && direct_load[(int) to_mode]
522 && ! mode_dependent_address_p (XEXP (from, 0),
523 MEM_ADDR_SPACE (from)))
524 || REG_P (from)
525 || GET_CODE (from) == SUBREG))
526 from = force_reg (from_mode, from);
527 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
528 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
529 from = copy_to_reg (from);
530 emit_move_insn (to, gen_lowpart (to_mode, from));
531 return;
534 /* Handle extension. */
535 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
537 /* Convert directly if that works. */
538 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
539 != CODE_FOR_nothing)
541 emit_unop_insn (code, to, from, equiv_code);
542 return;
544 else
546 machine_mode intermediate;
547 rtx tmp;
548 int shift_amount;
550 /* Search for a mode to convert via. */
551 for (intermediate = from_mode; intermediate != VOIDmode;
552 intermediate = GET_MODE_WIDER_MODE (intermediate))
553 if (((can_extend_p (to_mode, intermediate, unsignedp)
554 != CODE_FOR_nothing)
555 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
556 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
557 && (can_extend_p (intermediate, from_mode, unsignedp)
558 != CODE_FOR_nothing))
560 convert_move (to, convert_to_mode (intermediate, from,
561 unsignedp), unsignedp);
562 return;
565 /* No suitable intermediate mode.
566 Generate what we need with shifts. */
567 shift_amount = (GET_MODE_PRECISION (to_mode)
568 - GET_MODE_PRECISION (from_mode));
569 from = gen_lowpart (to_mode, force_reg (from_mode, from));
570 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
571 to, unsignedp);
572 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
573 to, unsignedp);
574 if (tmp != to)
575 emit_move_insn (to, tmp);
576 return;
580 /* Support special truncate insns for certain modes. */
581 if (convert_optab_handler (trunc_optab, to_mode,
582 from_mode) != CODE_FOR_nothing)
584 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
585 to, from, UNKNOWN);
586 return;
589 /* Handle truncation of volatile memrefs, and so on;
590 the things that couldn't be truncated directly,
591 and for which there was no special instruction.
593 ??? Code above formerly short-circuited this, for most integer
594 mode pairs, with a force_reg in from_mode followed by a recursive
595 call to this routine. Appears always to have been wrong. */
596 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
598 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
599 emit_move_insn (to, temp);
600 return;
603 /* Mode combination is not recognized. */
604 gcc_unreachable ();
607 /* Return an rtx for a value that would result
608 from converting X to mode MODE.
609 Both X and MODE may be floating, or both integer.
610 UNSIGNEDP is nonzero if X is an unsigned value.
611 This can be done by referring to a part of X in place
612 or by copying to a new temporary with conversion. */
615 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
617 return convert_modes (mode, VOIDmode, x, unsignedp);
620 /* Return an rtx for a value that would result
621 from converting X from mode OLDMODE to mode MODE.
622 Both modes may be floating, or both integer.
623 UNSIGNEDP is nonzero if X is an unsigned value.
625 This can be done by referring to a part of X in place
626 or by copying to a new temporary with conversion.
628 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
631 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
633 rtx temp;
635 /* If FROM is a SUBREG that indicates that we have already done at least
636 the required extension, strip it. */
638 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
639 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
640 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
641 x = gen_lowpart (mode, SUBREG_REG (x));
643 if (GET_MODE (x) != VOIDmode)
644 oldmode = GET_MODE (x);
646 if (mode == oldmode)
647 return x;
649 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
651 /* If the caller did not tell us the old mode, then there is not
652 much to do with respect to canonicalization. We have to
653 assume that all the bits are significant. */
654 if (GET_MODE_CLASS (oldmode) != MODE_INT)
655 oldmode = MAX_MODE_INT;
656 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
657 GET_MODE_PRECISION (mode),
658 unsignedp ? UNSIGNED : SIGNED);
659 return immed_wide_int_const (w, mode);
662 /* We can do this with a gen_lowpart if both desired and current modes
663 are integer, and this is either a constant integer, a register, or a
664 non-volatile MEM. */
665 if (GET_MODE_CLASS (mode) == MODE_INT
666 && GET_MODE_CLASS (oldmode) == MODE_INT
667 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
668 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
669 || (REG_P (x)
670 && (!HARD_REGISTER_P (x)
671 || HARD_REGNO_MODE_OK (REGNO (x), mode))
672 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
674 return gen_lowpart (mode, x);
676 /* Converting from integer constant into mode is always equivalent to an
677 subreg operation. */
678 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
680 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
681 return simplify_gen_subreg (mode, x, oldmode, 0);
684 temp = gen_reg_rtx (mode);
685 convert_move (temp, x, unsignedp);
686 return temp;
689 /* Return the largest alignment we can use for doing a move (or store)
690 of MAX_PIECES. ALIGN is the largest alignment we could use. */
692 static unsigned int
693 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
695 machine_mode tmode;
697 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
698 if (align >= GET_MODE_ALIGNMENT (tmode))
699 align = GET_MODE_ALIGNMENT (tmode);
700 else
702 machine_mode tmode, xmode;
704 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
705 tmode != VOIDmode;
706 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
707 if (GET_MODE_SIZE (tmode) > max_pieces
708 || SLOW_UNALIGNED_ACCESS (tmode, align))
709 break;
711 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
714 return align;
717 /* Return the widest integer mode no wider than SIZE. If no such mode
718 can be found, return VOIDmode. */
720 static machine_mode
721 widest_int_mode_for_size (unsigned int size)
723 machine_mode tmode, mode = VOIDmode;
725 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
726 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
727 if (GET_MODE_SIZE (tmode) < size)
728 mode = tmode;
730 return mode;
733 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
734 and should be performed piecewise. */
736 static bool
737 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
738 enum by_pieces_operation op)
740 return targetm.use_by_pieces_infrastructure_p (len, align, op,
741 optimize_insn_for_speed_p ());
744 /* Determine whether the LEN bytes can be moved by using several move
745 instructions. Return nonzero if a call to move_by_pieces should
746 succeed. */
748 bool
749 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
751 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
754 /* Return number of insns required to perform operation OP by pieces
755 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
757 unsigned HOST_WIDE_INT
758 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
759 unsigned int max_size, by_pieces_operation op)
761 unsigned HOST_WIDE_INT n_insns = 0;
763 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
765 while (max_size > 1 && l > 0)
767 machine_mode mode;
768 enum insn_code icode;
770 mode = widest_int_mode_for_size (max_size);
772 if (mode == VOIDmode)
773 break;
774 unsigned int modesize = GET_MODE_SIZE (mode);
776 icode = optab_handler (mov_optab, mode);
777 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
779 unsigned HOST_WIDE_INT n_pieces = l / modesize;
780 l %= modesize;
781 switch (op)
783 default:
784 n_insns += n_pieces;
785 break;
787 case COMPARE_BY_PIECES:
788 int batch = targetm.compare_by_pieces_branch_ratio (mode);
789 int batch_ops = 4 * batch - 1;
790 unsigned HOST_WIDE_INT full = n_pieces / batch;
791 n_insns += full * batch_ops;
792 if (n_pieces % batch != 0)
793 n_insns++;
794 break;
798 max_size = modesize;
801 gcc_assert (!l);
802 return n_insns;
805 /* Used when performing piecewise block operations, holds information
806 about one of the memory objects involved. The member functions
807 can be used to generate code for loading from the object and
808 updating the address when iterating. */
810 class pieces_addr
812 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
813 stack pushes. */
814 rtx m_obj;
815 /* The address of the object. Can differ from that seen in the
816 MEM rtx if we copied the address to a register. */
817 rtx m_addr;
818 /* Nonzero if the address on the object has an autoincrement already,
819 signifies whether that was an increment or decrement. */
820 signed char m_addr_inc;
821 /* Nonzero if we intend to use autoinc without the address already
822 having autoinc form. We will insert add insns around each memory
823 reference, expecting later passes to form autoinc addressing modes.
824 The only supported options are predecrement and postincrement. */
825 signed char m_explicit_inc;
826 /* True if we have either of the two possible cases of using
827 autoincrement. */
828 bool m_auto;
829 /* True if this is an address to be used for load operations rather
830 than stores. */
831 bool m_is_load;
833 /* Optionally, a function to obtain constants for any given offset into
834 the objects, and data associated with it. */
835 by_pieces_constfn m_constfn;
836 void *m_cfndata;
837 public:
838 pieces_addr (rtx, bool, by_pieces_constfn, void *);
839 rtx adjust (machine_mode, HOST_WIDE_INT);
840 void increment_address (HOST_WIDE_INT);
841 void maybe_predec (HOST_WIDE_INT);
842 void maybe_postinc (HOST_WIDE_INT);
843 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
844 int get_addr_inc ()
846 return m_addr_inc;
850 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
851 true if the operation to be performed on this object is a load
852 rather than a store. For stores, OBJ can be NULL, in which case we
853 assume the operation is a stack push. For loads, the optional
854 CONSTFN and its associated CFNDATA can be used in place of the
855 memory load. */
857 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
858 void *cfndata)
859 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
861 m_addr_inc = 0;
862 m_auto = false;
863 if (obj)
865 rtx addr = XEXP (obj, 0);
866 rtx_code code = GET_CODE (addr);
867 m_addr = addr;
868 bool dec = code == PRE_DEC || code == POST_DEC;
869 bool inc = code == PRE_INC || code == POST_INC;
870 m_auto = inc || dec;
871 if (m_auto)
872 m_addr_inc = dec ? -1 : 1;
874 /* While we have always looked for these codes here, the code
875 implementing the memory operation has never handled them.
876 Support could be added later if necessary or beneficial. */
877 gcc_assert (code != PRE_INC && code != POST_DEC);
879 else
881 m_addr = NULL_RTX;
882 if (!is_load)
884 m_auto = true;
885 if (STACK_GROWS_DOWNWARD)
886 m_addr_inc = -1;
887 else
888 m_addr_inc = 1;
890 else
891 gcc_assert (constfn != NULL);
893 m_explicit_inc = 0;
894 if (constfn)
895 gcc_assert (is_load);
898 /* Decide whether to use autoinc for an address involved in a memory op.
899 MODE is the mode of the accesses, REVERSE is true if we've decided to
900 perform the operation starting from the end, and LEN is the length of
901 the operation. Don't override an earlier decision to set m_auto. */
903 void
904 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
905 HOST_WIDE_INT len)
907 if (m_auto || m_obj == NULL_RTX)
908 return;
910 bool use_predec = (m_is_load
911 ? USE_LOAD_PRE_DECREMENT (mode)
912 : USE_STORE_PRE_DECREMENT (mode));
913 bool use_postinc = (m_is_load
914 ? USE_LOAD_POST_INCREMENT (mode)
915 : USE_STORE_POST_INCREMENT (mode));
916 machine_mode addr_mode = get_address_mode (m_obj);
918 if (use_predec && reverse)
920 m_addr = copy_to_mode_reg (addr_mode,
921 plus_constant (addr_mode,
922 m_addr, len));
923 m_auto = true;
924 m_explicit_inc = -1;
926 else if (use_postinc && !reverse)
928 m_addr = copy_to_mode_reg (addr_mode, m_addr);
929 m_auto = true;
930 m_explicit_inc = 1;
932 else if (CONSTANT_P (m_addr))
933 m_addr = copy_to_mode_reg (addr_mode, m_addr);
936 /* Adjust the address to refer to the data at OFFSET in MODE. If we
937 are using autoincrement for this address, we don't add the offset,
938 but we still modify the MEM's properties. */
941 pieces_addr::adjust (machine_mode mode, HOST_WIDE_INT offset)
943 if (m_constfn)
944 return m_constfn (m_cfndata, offset, mode);
945 if (m_obj == NULL_RTX)
946 return NULL_RTX;
947 if (m_auto)
948 return adjust_automodify_address (m_obj, mode, m_addr, offset);
949 else
950 return adjust_address (m_obj, mode, offset);
953 /* Emit an add instruction to increment the address by SIZE. */
955 void
956 pieces_addr::increment_address (HOST_WIDE_INT size)
958 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
959 emit_insn (gen_add2_insn (m_addr, amount));
962 /* If we are supposed to decrement the address after each access, emit code
963 to do so now. Increment by SIZE (which has should have the correct sign
964 already). */
966 void
967 pieces_addr::maybe_predec (HOST_WIDE_INT size)
969 if (m_explicit_inc >= 0)
970 return;
971 gcc_assert (HAVE_PRE_DECREMENT);
972 increment_address (size);
975 /* If we are supposed to decrement the address after each access, emit code
976 to do so now. Increment by SIZE. */
978 void
979 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
981 if (m_explicit_inc <= 0)
982 return;
983 gcc_assert (HAVE_POST_INCREMENT);
984 increment_address (size);
987 /* This structure is used by do_op_by_pieces to describe the operation
988 to be performed. */
990 class op_by_pieces_d
992 protected:
993 pieces_addr m_to, m_from;
994 unsigned HOST_WIDE_INT m_len;
995 HOST_WIDE_INT m_offset;
996 unsigned int m_align;
997 unsigned int m_max_size;
998 bool m_reverse;
1000 /* Virtual functions, overriden by derived classes for the specific
1001 operation. */
1002 virtual void generate (rtx, rtx, machine_mode) = 0;
1003 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1004 virtual void finish_mode (machine_mode)
1008 public:
1009 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1010 unsigned HOST_WIDE_INT, unsigned int);
1011 void run ();
1014 /* The constructor for an op_by_pieces_d structure. We require two
1015 objects named TO and FROM, which are identified as loads or stores
1016 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1017 and its associated FROM_CFN_DATA can be used to replace loads with
1018 constant values. LEN describes the length of the operation. */
1020 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1021 rtx from, bool from_load,
1022 by_pieces_constfn from_cfn,
1023 void *from_cfn_data,
1024 unsigned HOST_WIDE_INT len,
1025 unsigned int align)
1026 : m_to (to, to_load, NULL, NULL),
1027 m_from (from, from_load, from_cfn, from_cfn_data),
1028 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1030 int toi = m_to.get_addr_inc ();
1031 int fromi = m_from.get_addr_inc ();
1032 if (toi >= 0 && fromi >= 0)
1033 m_reverse = false;
1034 else if (toi <= 0 && fromi <= 0)
1035 m_reverse = true;
1036 else
1037 gcc_unreachable ();
1039 m_offset = m_reverse ? len : 0;
1040 align = MIN (to ? MEM_ALIGN (to) : align,
1041 from ? MEM_ALIGN (from) : align);
1043 /* If copying requires more than two move insns,
1044 copy addresses to registers (to make displacements shorter)
1045 and use post-increment if available. */
1046 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1048 /* Find the mode of the largest comparison. */
1049 machine_mode mode = widest_int_mode_for_size (m_max_size);
1051 m_from.decide_autoinc (mode, m_reverse, len);
1052 m_to.decide_autoinc (mode, m_reverse, len);
1055 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1056 m_align = align;
1059 /* This function contains the main loop used for expanding a block
1060 operation. First move what we can in the largest integer mode,
1061 then go to successively smaller modes. For every access, call
1062 GENFUN with the two operands and the EXTRA_DATA. */
1064 void
1065 op_by_pieces_d::run ()
1067 while (m_max_size > 1 && m_len > 0)
1069 machine_mode mode = widest_int_mode_for_size (m_max_size);
1071 if (mode == VOIDmode)
1072 break;
1074 if (prepare_mode (mode, m_align))
1076 unsigned int size = GET_MODE_SIZE (mode);
1077 rtx to1 = NULL_RTX, from1;
1079 while (m_len >= size)
1081 if (m_reverse)
1082 m_offset -= size;
1084 to1 = m_to.adjust (mode, m_offset);
1085 from1 = m_from.adjust (mode, m_offset);
1087 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1088 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1090 generate (to1, from1, mode);
1092 m_to.maybe_postinc (size);
1093 m_from.maybe_postinc (size);
1095 if (!m_reverse)
1096 m_offset += size;
1098 m_len -= size;
1101 finish_mode (mode);
1104 m_max_size = GET_MODE_SIZE (mode);
1107 /* The code above should have handled everything. */
1108 gcc_assert (!m_len);
1111 /* Derived class from op_by_pieces_d, providing support for block move
1112 operations. */
1114 class move_by_pieces_d : public op_by_pieces_d
1116 insn_gen_fn m_gen_fun;
1117 void generate (rtx, rtx, machine_mode);
1118 bool prepare_mode (machine_mode, unsigned int);
1120 public:
1121 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1122 unsigned int align)
1123 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1126 rtx finish_endp (int);
1129 /* Return true if MODE can be used for a set of copies, given an
1130 alignment ALIGN. Prepare whatever data is necessary for later
1131 calls to generate. */
1133 bool
1134 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1136 insn_code icode = optab_handler (mov_optab, mode);
1137 m_gen_fun = GEN_FCN (icode);
1138 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1141 /* A callback used when iterating for a compare_by_pieces_operation.
1142 OP0 and OP1 are the values that have been loaded and should be
1143 compared in MODE. If OP0 is NULL, this means we should generate a
1144 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1145 gen function that should be used to generate the mode. */
1147 void
1148 move_by_pieces_d::generate (rtx op0, rtx op1,
1149 machine_mode mode ATTRIBUTE_UNUSED)
1151 #ifdef PUSH_ROUNDING
1152 if (op0 == NULL_RTX)
1154 emit_single_push_insn (mode, op1, NULL);
1155 return;
1157 #endif
1158 emit_insn (m_gen_fun (op0, op1));
1161 /* Perform the final adjustment at the end of a string to obtain the
1162 correct return value for the block operation. If ENDP is 1 return
1163 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1164 end minus one byte ala stpcpy. */
1167 move_by_pieces_d::finish_endp (int endp)
1169 gcc_assert (!m_reverse);
1170 if (endp == 2)
1172 m_to.maybe_postinc (-1);
1173 --m_offset;
1175 return m_to.adjust (QImode, m_offset);
1178 /* Generate several move instructions to copy LEN bytes from block FROM to
1179 block TO. (These are MEM rtx's with BLKmode).
1181 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1182 used to push FROM to the stack.
1184 ALIGN is maximum stack alignment we can assume.
1186 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1187 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1188 stpcpy. */
1191 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1192 unsigned int align, int endp)
1194 #ifndef PUSH_ROUNDING
1195 if (to == NULL)
1196 gcc_unreachable ();
1197 #endif
1199 move_by_pieces_d data (to, from, len, align);
1201 data.run ();
1203 if (endp)
1204 return data.finish_endp (endp);
1205 else
1206 return to;
1209 /* Derived class from op_by_pieces_d, providing support for block move
1210 operations. */
1212 class store_by_pieces_d : public op_by_pieces_d
1214 insn_gen_fn m_gen_fun;
1215 void generate (rtx, rtx, machine_mode);
1216 bool prepare_mode (machine_mode, unsigned int);
1218 public:
1219 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1220 unsigned HOST_WIDE_INT len, unsigned int align)
1221 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1224 rtx finish_endp (int);
1227 /* Return true if MODE can be used for a set of stores, given an
1228 alignment ALIGN. Prepare whatever data is necessary for later
1229 calls to generate. */
1231 bool
1232 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1234 insn_code icode = optab_handler (mov_optab, mode);
1235 m_gen_fun = GEN_FCN (icode);
1236 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1239 /* A callback used when iterating for a store_by_pieces_operation.
1240 OP0 and OP1 are the values that have been loaded and should be
1241 compared in MODE. If OP0 is NULL, this means we should generate a
1242 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1243 gen function that should be used to generate the mode. */
1245 void
1246 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1248 emit_insn (m_gen_fun (op0, op1));
1251 /* Perform the final adjustment at the end of a string to obtain the
1252 correct return value for the block operation. If ENDP is 1 return
1253 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1254 end minus one byte ala stpcpy. */
1257 store_by_pieces_d::finish_endp (int endp)
1259 gcc_assert (!m_reverse);
1260 if (endp == 2)
1262 m_to.maybe_postinc (-1);
1263 --m_offset;
1265 return m_to.adjust (QImode, m_offset);
1268 /* Determine whether the LEN bytes generated by CONSTFUN can be
1269 stored to memory using several move instructions. CONSTFUNDATA is
1270 a pointer which will be passed as argument in every CONSTFUN call.
1271 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1272 a memset operation and false if it's a copy of a constant string.
1273 Return nonzero if a call to store_by_pieces should succeed. */
1276 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1277 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1278 void *constfundata, unsigned int align, bool memsetp)
1280 unsigned HOST_WIDE_INT l;
1281 unsigned int max_size;
1282 HOST_WIDE_INT offset = 0;
1283 machine_mode mode;
1284 enum insn_code icode;
1285 int reverse;
1286 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1287 rtx cst ATTRIBUTE_UNUSED;
1289 if (len == 0)
1290 return 1;
1292 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1293 memsetp
1294 ? SET_BY_PIECES
1295 : STORE_BY_PIECES,
1296 optimize_insn_for_speed_p ()))
1297 return 0;
1299 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1301 /* We would first store what we can in the largest integer mode, then go to
1302 successively smaller modes. */
1304 for (reverse = 0;
1305 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1306 reverse++)
1308 l = len;
1309 max_size = STORE_MAX_PIECES + 1;
1310 while (max_size > 1 && l > 0)
1312 mode = widest_int_mode_for_size (max_size);
1314 if (mode == VOIDmode)
1315 break;
1317 icode = optab_handler (mov_optab, mode);
1318 if (icode != CODE_FOR_nothing
1319 && align >= GET_MODE_ALIGNMENT (mode))
1321 unsigned int size = GET_MODE_SIZE (mode);
1323 while (l >= size)
1325 if (reverse)
1326 offset -= size;
1328 cst = (*constfun) (constfundata, offset, mode);
1329 if (!targetm.legitimate_constant_p (mode, cst))
1330 return 0;
1332 if (!reverse)
1333 offset += size;
1335 l -= size;
1339 max_size = GET_MODE_SIZE (mode);
1342 /* The code above should have handled everything. */
1343 gcc_assert (!l);
1346 return 1;
1349 /* Generate several move instructions to store LEN bytes generated by
1350 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1351 pointer which will be passed as argument in every CONSTFUN call.
1352 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1353 a memset operation and false if it's a copy of a constant string.
1354 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1355 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1356 stpcpy. */
1359 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1360 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1361 void *constfundata, unsigned int align, bool memsetp, int endp)
1363 if (len == 0)
1365 gcc_assert (endp != 2);
1366 return to;
1369 gcc_assert (targetm.use_by_pieces_infrastructure_p
1370 (len, align,
1371 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1372 optimize_insn_for_speed_p ()));
1374 store_by_pieces_d data (to, constfun, constfundata, len, align);
1375 data.run ();
1377 if (endp)
1378 return data.finish_endp (endp);
1379 else
1380 return to;
1383 /* Callback routine for clear_by_pieces.
1384 Return const0_rtx unconditionally. */
1386 static rtx
1387 clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode)
1389 return const0_rtx;
1392 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1393 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1395 static void
1396 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1398 if (len == 0)
1399 return;
1401 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1402 data.run ();
1405 /* Context used by compare_by_pieces_genfn. It stores the fail label
1406 to jump to in case of miscomparison, and for branch ratios greater than 1,
1407 it stores an accumulator and the current and maximum counts before
1408 emitting another branch. */
1410 class compare_by_pieces_d : public op_by_pieces_d
1412 rtx_code_label *m_fail_label;
1413 rtx m_accumulator;
1414 int m_count, m_batch;
1416 void generate (rtx, rtx, machine_mode);
1417 bool prepare_mode (machine_mode, unsigned int);
1418 void finish_mode (machine_mode);
1419 public:
1420 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1421 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1422 rtx_code_label *fail_label)
1423 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1425 m_fail_label = fail_label;
1429 /* A callback used when iterating for a compare_by_pieces_operation.
1430 OP0 and OP1 are the values that have been loaded and should be
1431 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1432 context structure. */
1434 void
1435 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1437 if (m_batch > 1)
1439 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1440 true, OPTAB_LIB_WIDEN);
1441 if (m_count != 0)
1442 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1443 true, OPTAB_LIB_WIDEN);
1444 m_accumulator = temp;
1446 if (++m_count < m_batch)
1447 return;
1449 m_count = 0;
1450 op0 = m_accumulator;
1451 op1 = const0_rtx;
1452 m_accumulator = NULL_RTX;
1454 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1455 m_fail_label, -1);
1458 /* Return true if MODE can be used for a set of moves and comparisons,
1459 given an alignment ALIGN. Prepare whatever data is necessary for
1460 later calls to generate. */
1462 bool
1463 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1465 insn_code icode = optab_handler (mov_optab, mode);
1466 if (icode == CODE_FOR_nothing
1467 || align < GET_MODE_ALIGNMENT (mode)
1468 || !can_compare_p (EQ, mode, ccp_jump))
1469 return false;
1470 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1471 if (m_batch < 0)
1472 return false;
1473 m_accumulator = NULL_RTX;
1474 m_count = 0;
1475 return true;
1478 /* Called after expanding a series of comparisons in MODE. If we have
1479 accumulated results for which we haven't emitted a branch yet, do
1480 so now. */
1482 void
1483 compare_by_pieces_d::finish_mode (machine_mode mode)
1485 if (m_accumulator != NULL_RTX)
1486 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1487 NULL_RTX, NULL, m_fail_label, -1);
1490 /* Generate several move instructions to compare LEN bytes from blocks
1491 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1493 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1494 used to push FROM to the stack.
1496 ALIGN is maximum stack alignment we can assume.
1498 Optionally, the caller can pass a constfn and associated data in A1_CFN
1499 and A1_CFN_DATA. describing that the second operand being compared is a
1500 known constant and how to obtain its data. */
1502 static rtx
1503 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1504 rtx target, unsigned int align,
1505 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1507 rtx_code_label *fail_label = gen_label_rtx ();
1508 rtx_code_label *end_label = gen_label_rtx ();
1510 if (target == NULL_RTX
1511 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1512 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1514 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1515 fail_label);
1517 data.run ();
1519 emit_move_insn (target, const0_rtx);
1520 emit_jump (end_label);
1521 emit_barrier ();
1522 emit_label (fail_label);
1523 emit_move_insn (target, const1_rtx);
1524 emit_label (end_label);
1526 return target;
1529 /* Emit code to move a block Y to a block X. This may be done with
1530 string-move instructions, with multiple scalar move instructions,
1531 or with a library call.
1533 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1534 SIZE is an rtx that says how long they are.
1535 ALIGN is the maximum alignment we can assume they have.
1536 METHOD describes what kind of copy this is, and what mechanisms may be used.
1537 MIN_SIZE is the minimal size of block to move
1538 MAX_SIZE is the maximal size of block to move, if it can not be represented
1539 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1541 Return the address of the new block, if memcpy is called and returns it,
1542 0 otherwise. */
1545 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1546 unsigned int expected_align, HOST_WIDE_INT expected_size,
1547 unsigned HOST_WIDE_INT min_size,
1548 unsigned HOST_WIDE_INT max_size,
1549 unsigned HOST_WIDE_INT probable_max_size)
1551 bool may_use_call;
1552 rtx retval = 0;
1553 unsigned int align;
1555 gcc_assert (size);
1556 if (CONST_INT_P (size) && INTVAL (size) == 0)
1557 return 0;
1559 switch (method)
1561 case BLOCK_OP_NORMAL:
1562 case BLOCK_OP_TAILCALL:
1563 may_use_call = true;
1564 break;
1566 case BLOCK_OP_CALL_PARM:
1567 may_use_call = block_move_libcall_safe_for_call_parm ();
1569 /* Make inhibit_defer_pop nonzero around the library call
1570 to force it to pop the arguments right away. */
1571 NO_DEFER_POP;
1572 break;
1574 case BLOCK_OP_NO_LIBCALL:
1575 may_use_call = false;
1576 break;
1578 default:
1579 gcc_unreachable ();
1582 gcc_assert (MEM_P (x) && MEM_P (y));
1583 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1584 gcc_assert (align >= BITS_PER_UNIT);
1586 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1587 block copy is more efficient for other large modes, e.g. DCmode. */
1588 x = adjust_address (x, BLKmode, 0);
1589 y = adjust_address (y, BLKmode, 0);
1591 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1592 can be incorrect is coming from __builtin_memcpy. */
1593 if (CONST_INT_P (size))
1595 x = shallow_copy_rtx (x);
1596 y = shallow_copy_rtx (y);
1597 set_mem_size (x, INTVAL (size));
1598 set_mem_size (y, INTVAL (size));
1601 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1602 move_by_pieces (x, y, INTVAL (size), align, 0);
1603 else if (emit_block_move_via_movmem (x, y, size, align,
1604 expected_align, expected_size,
1605 min_size, max_size, probable_max_size))
1607 else if (may_use_call
1608 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1609 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1611 /* Since x and y are passed to a libcall, mark the corresponding
1612 tree EXPR as addressable. */
1613 tree y_expr = MEM_EXPR (y);
1614 tree x_expr = MEM_EXPR (x);
1615 if (y_expr)
1616 mark_addressable (y_expr);
1617 if (x_expr)
1618 mark_addressable (x_expr);
1619 retval = emit_block_copy_via_libcall (x, y, size,
1620 method == BLOCK_OP_TAILCALL);
1623 else
1624 emit_block_move_via_loop (x, y, size, align);
1626 if (method == BLOCK_OP_CALL_PARM)
1627 OK_DEFER_POP;
1629 return retval;
1633 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1635 unsigned HOST_WIDE_INT max, min = 0;
1636 if (GET_CODE (size) == CONST_INT)
1637 min = max = UINTVAL (size);
1638 else
1639 max = GET_MODE_MASK (GET_MODE (size));
1640 return emit_block_move_hints (x, y, size, method, 0, -1,
1641 min, max, max);
1644 /* A subroutine of emit_block_move. Returns true if calling the
1645 block move libcall will not clobber any parameters which may have
1646 already been placed on the stack. */
1648 static bool
1649 block_move_libcall_safe_for_call_parm (void)
1651 #if defined (REG_PARM_STACK_SPACE)
1652 tree fn;
1653 #endif
1655 /* If arguments are pushed on the stack, then they're safe. */
1656 if (PUSH_ARGS)
1657 return true;
1659 /* If registers go on the stack anyway, any argument is sure to clobber
1660 an outgoing argument. */
1661 #if defined (REG_PARM_STACK_SPACE)
1662 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1663 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1664 depend on its argument. */
1665 (void) fn;
1666 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1667 && REG_PARM_STACK_SPACE (fn) != 0)
1668 return false;
1669 #endif
1671 /* If any argument goes in memory, then it might clobber an outgoing
1672 argument. */
1674 CUMULATIVE_ARGS args_so_far_v;
1675 cumulative_args_t args_so_far;
1676 tree fn, arg;
1678 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1679 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1680 args_so_far = pack_cumulative_args (&args_so_far_v);
1682 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1683 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1685 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1686 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1687 NULL_TREE, true);
1688 if (!tmp || !REG_P (tmp))
1689 return false;
1690 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1691 return false;
1692 targetm.calls.function_arg_advance (args_so_far, mode,
1693 NULL_TREE, true);
1696 return true;
1699 /* A subroutine of emit_block_move. Expand a movmem pattern;
1700 return true if successful. */
1702 static bool
1703 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1704 unsigned int expected_align, HOST_WIDE_INT expected_size,
1705 unsigned HOST_WIDE_INT min_size,
1706 unsigned HOST_WIDE_INT max_size,
1707 unsigned HOST_WIDE_INT probable_max_size)
1709 int save_volatile_ok = volatile_ok;
1710 machine_mode mode;
1712 if (expected_align < align)
1713 expected_align = align;
1714 if (expected_size != -1)
1716 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1717 expected_size = probable_max_size;
1718 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1719 expected_size = min_size;
1722 /* Since this is a move insn, we don't care about volatility. */
1723 volatile_ok = 1;
1725 /* Try the most limited insn first, because there's no point
1726 including more than one in the machine description unless
1727 the more limited one has some advantage. */
1729 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1730 mode = GET_MODE_WIDER_MODE (mode))
1732 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1734 if (code != CODE_FOR_nothing
1735 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1736 here because if SIZE is less than the mode mask, as it is
1737 returned by the macro, it will definitely be less than the
1738 actual mode mask. Since SIZE is within the Pmode address
1739 space, we limit MODE to Pmode. */
1740 && ((CONST_INT_P (size)
1741 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1742 <= (GET_MODE_MASK (mode) >> 1)))
1743 || max_size <= (GET_MODE_MASK (mode) >> 1)
1744 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1746 struct expand_operand ops[9];
1747 unsigned int nops;
1749 /* ??? When called via emit_block_move_for_call, it'd be
1750 nice if there were some way to inform the backend, so
1751 that it doesn't fail the expansion because it thinks
1752 emitting the libcall would be more efficient. */
1753 nops = insn_data[(int) code].n_generator_args;
1754 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1756 create_fixed_operand (&ops[0], x);
1757 create_fixed_operand (&ops[1], y);
1758 /* The check above guarantees that this size conversion is valid. */
1759 create_convert_operand_to (&ops[2], size, mode, true);
1760 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1761 if (nops >= 6)
1763 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1764 create_integer_operand (&ops[5], expected_size);
1766 if (nops >= 8)
1768 create_integer_operand (&ops[6], min_size);
1769 /* If we can not represent the maximal size,
1770 make parameter NULL. */
1771 if ((HOST_WIDE_INT) max_size != -1)
1772 create_integer_operand (&ops[7], max_size);
1773 else
1774 create_fixed_operand (&ops[7], NULL);
1776 if (nops == 9)
1778 /* If we can not represent the maximal size,
1779 make parameter NULL. */
1780 if ((HOST_WIDE_INT) probable_max_size != -1)
1781 create_integer_operand (&ops[8], probable_max_size);
1782 else
1783 create_fixed_operand (&ops[8], NULL);
1785 if (maybe_expand_insn (code, nops, ops))
1787 volatile_ok = save_volatile_ok;
1788 return true;
1793 volatile_ok = save_volatile_ok;
1794 return false;
1797 /* A subroutine of emit_block_move. Copy the data via an explicit
1798 loop. This is used only when libcalls are forbidden. */
1799 /* ??? It'd be nice to copy in hunks larger than QImode. */
1801 static void
1802 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1803 unsigned int align ATTRIBUTE_UNUSED)
1805 rtx_code_label *cmp_label, *top_label;
1806 rtx iter, x_addr, y_addr, tmp;
1807 machine_mode x_addr_mode = get_address_mode (x);
1808 machine_mode y_addr_mode = get_address_mode (y);
1809 machine_mode iter_mode;
1811 iter_mode = GET_MODE (size);
1812 if (iter_mode == VOIDmode)
1813 iter_mode = word_mode;
1815 top_label = gen_label_rtx ();
1816 cmp_label = gen_label_rtx ();
1817 iter = gen_reg_rtx (iter_mode);
1819 emit_move_insn (iter, const0_rtx);
1821 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1822 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1823 do_pending_stack_adjust ();
1825 emit_jump (cmp_label);
1826 emit_label (top_label);
1828 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1829 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1831 if (x_addr_mode != y_addr_mode)
1832 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1833 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1835 x = change_address (x, QImode, x_addr);
1836 y = change_address (y, QImode, y_addr);
1838 emit_move_insn (x, y);
1840 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1841 true, OPTAB_LIB_WIDEN);
1842 if (tmp != iter)
1843 emit_move_insn (iter, tmp);
1845 emit_label (cmp_label);
1847 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1848 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1851 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1852 TAILCALL is true if this is a tail call. */
1855 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1856 rtx size, bool tailcall)
1858 rtx dst_addr, src_addr;
1859 tree call_expr, dst_tree, src_tree, size_tree;
1860 machine_mode size_mode;
1862 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1863 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1864 dst_tree = make_tree (ptr_type_node, dst_addr);
1866 src_addr = copy_addr_to_reg (XEXP (src, 0));
1867 src_addr = convert_memory_address (ptr_mode, src_addr);
1868 src_tree = make_tree (ptr_type_node, src_addr);
1870 size_mode = TYPE_MODE (sizetype);
1871 size = convert_to_mode (size_mode, size, 1);
1872 size = copy_to_mode_reg (size_mode, size);
1873 size_tree = make_tree (sizetype, size);
1875 /* It is incorrect to use the libcall calling conventions for calls to
1876 memcpy/memmove/memcmp because they can be provided by the user. */
1877 tree fn = builtin_decl_implicit (fncode);
1878 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1879 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1881 return expand_call (call_expr, NULL_RTX, false);
1884 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1885 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1886 otherwise return null. */
1889 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1890 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1891 HOST_WIDE_INT align)
1893 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1895 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1896 target = NULL_RTX;
1898 struct expand_operand ops[5];
1899 create_output_operand (&ops[0], target, insn_mode);
1900 create_fixed_operand (&ops[1], arg1_rtx);
1901 create_fixed_operand (&ops[2], arg2_rtx);
1902 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1903 TYPE_UNSIGNED (arg3_type));
1904 create_integer_operand (&ops[4], align);
1905 if (maybe_expand_insn (icode, 5, ops))
1906 return ops[0].value;
1907 return NULL_RTX;
1910 /* Expand a block compare between X and Y with length LEN using the
1911 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1912 of the expression that was used to calculate the length. ALIGN
1913 gives the known minimum common alignment. */
1915 static rtx
1916 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1917 unsigned align)
1919 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1920 implementing memcmp because it will stop if it encounters two
1921 zero bytes. */
1922 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1924 if (icode == CODE_FOR_nothing)
1925 return NULL_RTX;
1927 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1930 /* Emit code to compare a block Y to a block X. This may be done with
1931 string-compare instructions, with multiple scalar instructions,
1932 or with a library call.
1934 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1935 they are. LEN_TYPE is the type of the expression that was used to
1936 calculate it.
1938 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1939 value of a normal memcmp call, instead we can just compare for equality.
1940 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1941 returning NULL_RTX.
1943 Optionally, the caller can pass a constfn and associated data in Y_CFN
1944 and Y_CFN_DATA. describing that the second operand being compared is a
1945 known constant and how to obtain its data.
1946 Return the result of the comparison, or NULL_RTX if we failed to
1947 perform the operation. */
1950 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1951 bool equality_only, by_pieces_constfn y_cfn,
1952 void *y_cfndata)
1954 rtx result = 0;
1956 if (CONST_INT_P (len) && INTVAL (len) == 0)
1957 return const0_rtx;
1959 gcc_assert (MEM_P (x) && MEM_P (y));
1960 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1961 gcc_assert (align >= BITS_PER_UNIT);
1963 x = adjust_address (x, BLKmode, 0);
1964 y = adjust_address (y, BLKmode, 0);
1966 if (equality_only
1967 && CONST_INT_P (len)
1968 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1969 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1970 y_cfn, y_cfndata);
1971 else
1972 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1974 return result;
1977 /* Copy all or part of a value X into registers starting at REGNO.
1978 The number of registers to be filled is NREGS. */
1980 void
1981 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1983 if (nregs == 0)
1984 return;
1986 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1987 x = validize_mem (force_const_mem (mode, x));
1989 /* See if the machine can do this with a load multiple insn. */
1990 if (targetm.have_load_multiple ())
1992 rtx_insn *last = get_last_insn ();
1993 rtx first = gen_rtx_REG (word_mode, regno);
1994 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1995 GEN_INT (nregs)))
1997 emit_insn (pat);
1998 return;
2000 else
2001 delete_insns_since (last);
2004 for (int i = 0; i < nregs; i++)
2005 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2006 operand_subword_force (x, i, mode));
2009 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2010 The number of registers to be filled is NREGS. */
2012 void
2013 move_block_from_reg (int regno, rtx x, int nregs)
2015 if (nregs == 0)
2016 return;
2018 /* See if the machine can do this with a store multiple insn. */
2019 if (targetm.have_store_multiple ())
2021 rtx_insn *last = get_last_insn ();
2022 rtx first = gen_rtx_REG (word_mode, regno);
2023 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2024 GEN_INT (nregs)))
2026 emit_insn (pat);
2027 return;
2029 else
2030 delete_insns_since (last);
2033 for (int i = 0; i < nregs; i++)
2035 rtx tem = operand_subword (x, i, 1, BLKmode);
2037 gcc_assert (tem);
2039 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2043 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2044 ORIG, where ORIG is a non-consecutive group of registers represented by
2045 a PARALLEL. The clone is identical to the original except in that the
2046 original set of registers is replaced by a new set of pseudo registers.
2047 The new set has the same modes as the original set. */
2050 gen_group_rtx (rtx orig)
2052 int i, length;
2053 rtx *tmps;
2055 gcc_assert (GET_CODE (orig) == PARALLEL);
2057 length = XVECLEN (orig, 0);
2058 tmps = XALLOCAVEC (rtx, length);
2060 /* Skip a NULL entry in first slot. */
2061 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2063 if (i)
2064 tmps[0] = 0;
2066 for (; i < length; i++)
2068 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2069 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2071 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2074 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2077 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2078 except that values are placed in TMPS[i], and must later be moved
2079 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2081 static void
2082 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2084 rtx src;
2085 int start, i;
2086 machine_mode m = GET_MODE (orig_src);
2088 gcc_assert (GET_CODE (dst) == PARALLEL);
2090 if (m != VOIDmode
2091 && !SCALAR_INT_MODE_P (m)
2092 && !MEM_P (orig_src)
2093 && GET_CODE (orig_src) != CONCAT)
2095 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
2096 if (imode == BLKmode)
2097 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2098 else
2099 src = gen_reg_rtx (imode);
2100 if (imode != BLKmode)
2101 src = gen_lowpart (GET_MODE (orig_src), src);
2102 emit_move_insn (src, orig_src);
2103 /* ...and back again. */
2104 if (imode != BLKmode)
2105 src = gen_lowpart (imode, src);
2106 emit_group_load_1 (tmps, dst, src, type, ssize);
2107 return;
2110 /* Check for a NULL entry, used to indicate that the parameter goes
2111 both on the stack and in registers. */
2112 if (XEXP (XVECEXP (dst, 0, 0), 0))
2113 start = 0;
2114 else
2115 start = 1;
2117 /* Process the pieces. */
2118 for (i = start; i < XVECLEN (dst, 0); i++)
2120 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2121 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2122 unsigned int bytelen = GET_MODE_SIZE (mode);
2123 int shift = 0;
2125 /* Handle trailing fragments that run over the size of the struct. */
2126 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2128 /* Arrange to shift the fragment to where it belongs.
2129 extract_bit_field loads to the lsb of the reg. */
2130 if (
2131 #ifdef BLOCK_REG_PADDING
2132 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2133 == (BYTES_BIG_ENDIAN ? upward : downward)
2134 #else
2135 BYTES_BIG_ENDIAN
2136 #endif
2138 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2139 bytelen = ssize - bytepos;
2140 gcc_assert (bytelen > 0);
2143 /* If we won't be loading directly from memory, protect the real source
2144 from strange tricks we might play; but make sure that the source can
2145 be loaded directly into the destination. */
2146 src = orig_src;
2147 if (!MEM_P (orig_src)
2148 && (!CONSTANT_P (orig_src)
2149 || (GET_MODE (orig_src) != mode
2150 && GET_MODE (orig_src) != VOIDmode)))
2152 if (GET_MODE (orig_src) == VOIDmode)
2153 src = gen_reg_rtx (mode);
2154 else
2155 src = gen_reg_rtx (GET_MODE (orig_src));
2157 emit_move_insn (src, orig_src);
2160 /* Optimize the access just a bit. */
2161 if (MEM_P (src)
2162 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
2163 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2164 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2165 && bytelen == GET_MODE_SIZE (mode))
2167 tmps[i] = gen_reg_rtx (mode);
2168 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2170 else if (COMPLEX_MODE_P (mode)
2171 && GET_MODE (src) == mode
2172 && bytelen == GET_MODE_SIZE (mode))
2173 /* Let emit_move_complex do the bulk of the work. */
2174 tmps[i] = src;
2175 else if (GET_CODE (src) == CONCAT)
2177 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2178 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2179 unsigned int elt = bytepos / slen0;
2180 unsigned int subpos = bytepos % slen0;
2182 if (subpos + bytelen <= slen0)
2184 /* The following assumes that the concatenated objects all
2185 have the same size. In this case, a simple calculation
2186 can be used to determine the object and the bit field
2187 to be extracted. */
2188 tmps[i] = XEXP (src, elt);
2189 if (subpos != 0
2190 || subpos + bytelen != slen0
2191 || (!CONSTANT_P (tmps[i])
2192 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2193 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2194 subpos * BITS_PER_UNIT,
2195 1, NULL_RTX, mode, mode, false);
2197 else
2199 rtx mem;
2201 gcc_assert (!bytepos);
2202 mem = assign_stack_temp (GET_MODE (src), slen);
2203 emit_move_insn (mem, src);
2204 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2205 0, 1, NULL_RTX, mode, mode, false);
2208 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2209 SIMD register, which is currently broken. While we get GCC
2210 to emit proper RTL for these cases, let's dump to memory. */
2211 else if (VECTOR_MODE_P (GET_MODE (dst))
2212 && REG_P (src))
2214 int slen = GET_MODE_SIZE (GET_MODE (src));
2215 rtx mem;
2217 mem = assign_stack_temp (GET_MODE (src), slen);
2218 emit_move_insn (mem, src);
2219 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2221 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2222 && XVECLEN (dst, 0) > 1)
2223 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2224 else if (CONSTANT_P (src))
2226 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2228 if (len == ssize)
2229 tmps[i] = src;
2230 else
2232 rtx first, second;
2234 /* TODO: const_wide_int can have sizes other than this... */
2235 gcc_assert (2 * len == ssize);
2236 split_double (src, &first, &second);
2237 if (i)
2238 tmps[i] = second;
2239 else
2240 tmps[i] = first;
2243 else if (REG_P (src) && GET_MODE (src) == mode)
2244 tmps[i] = src;
2245 else
2246 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2247 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2248 mode, mode, false);
2250 if (shift)
2251 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2252 shift, tmps[i], 0);
2256 /* Emit code to move a block SRC of type TYPE to a block DST,
2257 where DST is non-consecutive registers represented by a PARALLEL.
2258 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2259 if not known. */
2261 void
2262 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2264 rtx *tmps;
2265 int i;
2267 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2268 emit_group_load_1 (tmps, dst, src, type, ssize);
2270 /* Copy the extracted pieces into the proper (probable) hard regs. */
2271 for (i = 0; i < XVECLEN (dst, 0); i++)
2273 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2274 if (d == NULL)
2275 continue;
2276 emit_move_insn (d, tmps[i]);
2280 /* Similar, but load SRC into new pseudos in a format that looks like
2281 PARALLEL. This can later be fed to emit_group_move to get things
2282 in the right place. */
2285 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2287 rtvec vec;
2288 int i;
2290 vec = rtvec_alloc (XVECLEN (parallel, 0));
2291 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2293 /* Convert the vector to look just like the original PARALLEL, except
2294 with the computed values. */
2295 for (i = 0; i < XVECLEN (parallel, 0); i++)
2297 rtx e = XVECEXP (parallel, 0, i);
2298 rtx d = XEXP (e, 0);
2300 if (d)
2302 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2303 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2305 RTVEC_ELT (vec, i) = e;
2308 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2311 /* Emit code to move a block SRC to block DST, where SRC and DST are
2312 non-consecutive groups of registers, each represented by a PARALLEL. */
2314 void
2315 emit_group_move (rtx dst, rtx src)
2317 int i;
2319 gcc_assert (GET_CODE (src) == PARALLEL
2320 && GET_CODE (dst) == PARALLEL
2321 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2323 /* Skip first entry if NULL. */
2324 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2325 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2326 XEXP (XVECEXP (src, 0, i), 0));
2329 /* Move a group of registers represented by a PARALLEL into pseudos. */
2332 emit_group_move_into_temps (rtx src)
2334 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2335 int i;
2337 for (i = 0; i < XVECLEN (src, 0); i++)
2339 rtx e = XVECEXP (src, 0, i);
2340 rtx d = XEXP (e, 0);
2342 if (d)
2343 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2344 RTVEC_ELT (vec, i) = e;
2347 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2350 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2351 where SRC is non-consecutive registers represented by a PARALLEL.
2352 SSIZE represents the total size of block ORIG_DST, or -1 if not
2353 known. */
2355 void
2356 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2358 rtx *tmps, dst;
2359 int start, finish, i;
2360 machine_mode m = GET_MODE (orig_dst);
2362 gcc_assert (GET_CODE (src) == PARALLEL);
2364 if (!SCALAR_INT_MODE_P (m)
2365 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2367 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
2368 if (imode == BLKmode)
2369 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2370 else
2371 dst = gen_reg_rtx (imode);
2372 emit_group_store (dst, src, type, ssize);
2373 if (imode != BLKmode)
2374 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2375 emit_move_insn (orig_dst, dst);
2376 return;
2379 /* Check for a NULL entry, used to indicate that the parameter goes
2380 both on the stack and in registers. */
2381 if (XEXP (XVECEXP (src, 0, 0), 0))
2382 start = 0;
2383 else
2384 start = 1;
2385 finish = XVECLEN (src, 0);
2387 tmps = XALLOCAVEC (rtx, finish);
2389 /* Copy the (probable) hard regs into pseudos. */
2390 for (i = start; i < finish; i++)
2392 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2393 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2395 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2396 emit_move_insn (tmps[i], reg);
2398 else
2399 tmps[i] = reg;
2402 /* If we won't be storing directly into memory, protect the real destination
2403 from strange tricks we might play. */
2404 dst = orig_dst;
2405 if (GET_CODE (dst) == PARALLEL)
2407 rtx temp;
2409 /* We can get a PARALLEL dst if there is a conditional expression in
2410 a return statement. In that case, the dst and src are the same,
2411 so no action is necessary. */
2412 if (rtx_equal_p (dst, src))
2413 return;
2415 /* It is unclear if we can ever reach here, but we may as well handle
2416 it. Allocate a temporary, and split this into a store/load to/from
2417 the temporary. */
2418 temp = assign_stack_temp (GET_MODE (dst), ssize);
2419 emit_group_store (temp, src, type, ssize);
2420 emit_group_load (dst, temp, type, ssize);
2421 return;
2423 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2425 machine_mode outer = GET_MODE (dst);
2426 machine_mode inner;
2427 HOST_WIDE_INT bytepos;
2428 bool done = false;
2429 rtx temp;
2431 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2432 dst = gen_reg_rtx (outer);
2434 /* Make life a bit easier for combine. */
2435 /* If the first element of the vector is the low part
2436 of the destination mode, use a paradoxical subreg to
2437 initialize the destination. */
2438 if (start < finish)
2440 inner = GET_MODE (tmps[start]);
2441 bytepos = subreg_lowpart_offset (inner, outer);
2442 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2444 temp = simplify_gen_subreg (outer, tmps[start],
2445 inner, 0);
2446 if (temp)
2448 emit_move_insn (dst, temp);
2449 done = true;
2450 start++;
2455 /* If the first element wasn't the low part, try the last. */
2456 if (!done
2457 && start < finish - 1)
2459 inner = GET_MODE (tmps[finish - 1]);
2460 bytepos = subreg_lowpart_offset (inner, outer);
2461 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2463 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2464 inner, 0);
2465 if (temp)
2467 emit_move_insn (dst, temp);
2468 done = true;
2469 finish--;
2474 /* Otherwise, simply initialize the result to zero. */
2475 if (!done)
2476 emit_move_insn (dst, CONST0_RTX (outer));
2479 /* Process the pieces. */
2480 for (i = start; i < finish; i++)
2482 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2483 machine_mode mode = GET_MODE (tmps[i]);
2484 unsigned int bytelen = GET_MODE_SIZE (mode);
2485 unsigned int adj_bytelen;
2486 rtx dest = dst;
2488 /* Handle trailing fragments that run over the size of the struct. */
2489 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2490 adj_bytelen = ssize - bytepos;
2491 else
2492 adj_bytelen = bytelen;
2494 if (GET_CODE (dst) == CONCAT)
2496 if (bytepos + adj_bytelen
2497 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2498 dest = XEXP (dst, 0);
2499 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2501 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2502 dest = XEXP (dst, 1);
2504 else
2506 machine_mode dest_mode = GET_MODE (dest);
2507 machine_mode tmp_mode = GET_MODE (tmps[i]);
2509 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2511 if (GET_MODE_ALIGNMENT (dest_mode)
2512 >= GET_MODE_ALIGNMENT (tmp_mode))
2514 dest = assign_stack_temp (dest_mode,
2515 GET_MODE_SIZE (dest_mode));
2516 emit_move_insn (adjust_address (dest,
2517 tmp_mode,
2518 bytepos),
2519 tmps[i]);
2520 dst = dest;
2522 else
2524 dest = assign_stack_temp (tmp_mode,
2525 GET_MODE_SIZE (tmp_mode));
2526 emit_move_insn (dest, tmps[i]);
2527 dst = adjust_address (dest, dest_mode, bytepos);
2529 break;
2533 /* Handle trailing fragments that run over the size of the struct. */
2534 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2536 /* store_bit_field always takes its value from the lsb.
2537 Move the fragment to the lsb if it's not already there. */
2538 if (
2539 #ifdef BLOCK_REG_PADDING
2540 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2541 == (BYTES_BIG_ENDIAN ? upward : downward)
2542 #else
2543 BYTES_BIG_ENDIAN
2544 #endif
2547 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2548 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2549 shift, tmps[i], 0);
2552 /* Make sure not to write past the end of the struct. */
2553 store_bit_field (dest,
2554 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2555 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2556 VOIDmode, tmps[i], false);
2559 /* Optimize the access just a bit. */
2560 else if (MEM_P (dest)
2561 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2562 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2563 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2564 && bytelen == GET_MODE_SIZE (mode))
2565 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2567 else
2568 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2569 0, 0, mode, tmps[i], false);
2572 /* Copy from the pseudo into the (probable) hard reg. */
2573 if (orig_dst != dst)
2574 emit_move_insn (orig_dst, dst);
2577 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2578 of the value stored in X. */
2581 maybe_emit_group_store (rtx x, tree type)
2583 machine_mode mode = TYPE_MODE (type);
2584 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2585 if (GET_CODE (x) == PARALLEL)
2587 rtx result = gen_reg_rtx (mode);
2588 emit_group_store (result, x, type, int_size_in_bytes (type));
2589 return result;
2591 return x;
2594 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2596 This is used on targets that return BLKmode values in registers. */
2598 static void
2599 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2601 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2602 rtx src = NULL, dst = NULL;
2603 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2604 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2605 machine_mode mode = GET_MODE (srcreg);
2606 machine_mode tmode = GET_MODE (target);
2607 machine_mode copy_mode;
2609 /* BLKmode registers created in the back-end shouldn't have survived. */
2610 gcc_assert (mode != BLKmode);
2612 /* If the structure doesn't take up a whole number of words, see whether
2613 SRCREG is padded on the left or on the right. If it's on the left,
2614 set PADDING_CORRECTION to the number of bits to skip.
2616 In most ABIs, the structure will be returned at the least end of
2617 the register, which translates to right padding on little-endian
2618 targets and left padding on big-endian targets. The opposite
2619 holds if the structure is returned at the most significant
2620 end of the register. */
2621 if (bytes % UNITS_PER_WORD != 0
2622 && (targetm.calls.return_in_msb (type)
2623 ? !BYTES_BIG_ENDIAN
2624 : BYTES_BIG_ENDIAN))
2625 padding_correction
2626 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2628 /* We can use a single move if we have an exact mode for the size. */
2629 else if (MEM_P (target)
2630 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2631 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2632 && bytes == GET_MODE_SIZE (mode))
2634 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2635 return;
2638 /* And if we additionally have the same mode for a register. */
2639 else if (REG_P (target)
2640 && GET_MODE (target) == mode
2641 && bytes == GET_MODE_SIZE (mode))
2643 emit_move_insn (target, srcreg);
2644 return;
2647 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2648 into a new pseudo which is a full word. */
2649 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2651 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2652 mode = word_mode;
2655 /* Copy the structure BITSIZE bits at a time. If the target lives in
2656 memory, take care of not reading/writing past its end by selecting
2657 a copy mode suited to BITSIZE. This should always be possible given
2658 how it is computed.
2660 If the target lives in register, make sure not to select a copy mode
2661 larger than the mode of the register.
2663 We could probably emit more efficient code for machines which do not use
2664 strict alignment, but it doesn't seem worth the effort at the current
2665 time. */
2667 copy_mode = word_mode;
2668 if (MEM_P (target))
2670 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2671 if (mem_mode != BLKmode)
2672 copy_mode = mem_mode;
2674 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2675 copy_mode = tmode;
2677 for (bitpos = 0, xbitpos = padding_correction;
2678 bitpos < bytes * BITS_PER_UNIT;
2679 bitpos += bitsize, xbitpos += bitsize)
2681 /* We need a new source operand each time xbitpos is on a
2682 word boundary and when xbitpos == padding_correction
2683 (the first time through). */
2684 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2685 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2687 /* We need a new destination operand each time bitpos is on
2688 a word boundary. */
2689 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2690 dst = target;
2691 else if (bitpos % BITS_PER_WORD == 0)
2692 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2694 /* Use xbitpos for the source extraction (right justified) and
2695 bitpos for the destination store (left justified). */
2696 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2697 extract_bit_field (src, bitsize,
2698 xbitpos % BITS_PER_WORD, 1,
2699 NULL_RTX, copy_mode, copy_mode,
2700 false),
2701 false);
2705 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2706 register if it contains any data, otherwise return null.
2708 This is used on targets that return BLKmode values in registers. */
2711 copy_blkmode_to_reg (machine_mode mode, tree src)
2713 int i, n_regs;
2714 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2715 unsigned int bitsize;
2716 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2717 machine_mode dst_mode;
2719 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2721 x = expand_normal (src);
2723 bytes = int_size_in_bytes (TREE_TYPE (src));
2724 if (bytes == 0)
2725 return NULL_RTX;
2727 /* If the structure doesn't take up a whole number of words, see
2728 whether the register value should be padded on the left or on
2729 the right. Set PADDING_CORRECTION to the number of padding
2730 bits needed on the left side.
2732 In most ABIs, the structure will be returned at the least end of
2733 the register, which translates to right padding on little-endian
2734 targets and left padding on big-endian targets. The opposite
2735 holds if the structure is returned at the most significant
2736 end of the register. */
2737 if (bytes % UNITS_PER_WORD != 0
2738 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2739 ? !BYTES_BIG_ENDIAN
2740 : BYTES_BIG_ENDIAN))
2741 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2742 * BITS_PER_UNIT));
2744 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2745 dst_words = XALLOCAVEC (rtx, n_regs);
2746 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2748 /* Copy the structure BITSIZE bits at a time. */
2749 for (bitpos = 0, xbitpos = padding_correction;
2750 bitpos < bytes * BITS_PER_UNIT;
2751 bitpos += bitsize, xbitpos += bitsize)
2753 /* We need a new destination pseudo each time xbitpos is
2754 on a word boundary and when xbitpos == padding_correction
2755 (the first time through). */
2756 if (xbitpos % BITS_PER_WORD == 0
2757 || xbitpos == padding_correction)
2759 /* Generate an appropriate register. */
2760 dst_word = gen_reg_rtx (word_mode);
2761 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2763 /* Clear the destination before we move anything into it. */
2764 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2767 /* We need a new source operand each time bitpos is on a word
2768 boundary. */
2769 if (bitpos % BITS_PER_WORD == 0)
2770 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2772 /* Use bitpos for the source extraction (left justified) and
2773 xbitpos for the destination store (right justified). */
2774 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2775 0, 0, word_mode,
2776 extract_bit_field (src_word, bitsize,
2777 bitpos % BITS_PER_WORD, 1,
2778 NULL_RTX, word_mode, word_mode,
2779 false),
2780 false);
2783 if (mode == BLKmode)
2785 /* Find the smallest integer mode large enough to hold the
2786 entire structure. */
2787 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2788 mode != VOIDmode;
2789 mode = GET_MODE_WIDER_MODE (mode))
2790 /* Have we found a large enough mode? */
2791 if (GET_MODE_SIZE (mode) >= bytes)
2792 break;
2794 /* A suitable mode should have been found. */
2795 gcc_assert (mode != VOIDmode);
2798 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2799 dst_mode = word_mode;
2800 else
2801 dst_mode = mode;
2802 dst = gen_reg_rtx (dst_mode);
2804 for (i = 0; i < n_regs; i++)
2805 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2807 if (mode != dst_mode)
2808 dst = gen_lowpart (mode, dst);
2810 return dst;
2813 /* Add a USE expression for REG to the (possibly empty) list pointed
2814 to by CALL_FUSAGE. REG must denote a hard register. */
2816 void
2817 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2819 gcc_assert (REG_P (reg));
2821 if (!HARD_REGISTER_P (reg))
2822 return;
2824 *call_fusage
2825 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2828 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2829 to by CALL_FUSAGE. REG must denote a hard register. */
2831 void
2832 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2834 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2836 *call_fusage
2837 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2840 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2841 starting at REGNO. All of these registers must be hard registers. */
2843 void
2844 use_regs (rtx *call_fusage, int regno, int nregs)
2846 int i;
2848 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2850 for (i = 0; i < nregs; i++)
2851 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2854 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2855 PARALLEL REGS. This is for calls that pass values in multiple
2856 non-contiguous locations. The Irix 6 ABI has examples of this. */
2858 void
2859 use_group_regs (rtx *call_fusage, rtx regs)
2861 int i;
2863 for (i = 0; i < XVECLEN (regs, 0); i++)
2865 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2867 /* A NULL entry means the parameter goes both on the stack and in
2868 registers. This can also be a MEM for targets that pass values
2869 partially on the stack and partially in registers. */
2870 if (reg != 0 && REG_P (reg))
2871 use_reg (call_fusage, reg);
2875 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2876 assigment and the code of the expresion on the RHS is CODE. Return
2877 NULL otherwise. */
2879 static gimple *
2880 get_def_for_expr (tree name, enum tree_code code)
2882 gimple *def_stmt;
2884 if (TREE_CODE (name) != SSA_NAME)
2885 return NULL;
2887 def_stmt = get_gimple_for_ssa_name (name);
2888 if (!def_stmt
2889 || gimple_assign_rhs_code (def_stmt) != code)
2890 return NULL;
2892 return def_stmt;
2895 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2896 assigment and the class of the expresion on the RHS is CLASS. Return
2897 NULL otherwise. */
2899 static gimple *
2900 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2902 gimple *def_stmt;
2904 if (TREE_CODE (name) != SSA_NAME)
2905 return NULL;
2907 def_stmt = get_gimple_for_ssa_name (name);
2908 if (!def_stmt
2909 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2910 return NULL;
2912 return def_stmt;
2915 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2916 its length in bytes. */
2919 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2920 unsigned int expected_align, HOST_WIDE_INT expected_size,
2921 unsigned HOST_WIDE_INT min_size,
2922 unsigned HOST_WIDE_INT max_size,
2923 unsigned HOST_WIDE_INT probable_max_size)
2925 machine_mode mode = GET_MODE (object);
2926 unsigned int align;
2928 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2930 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2931 just move a zero. Otherwise, do this a piece at a time. */
2932 if (mode != BLKmode
2933 && CONST_INT_P (size)
2934 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2936 rtx zero = CONST0_RTX (mode);
2937 if (zero != NULL)
2939 emit_move_insn (object, zero);
2940 return NULL;
2943 if (COMPLEX_MODE_P (mode))
2945 zero = CONST0_RTX (GET_MODE_INNER (mode));
2946 if (zero != NULL)
2948 write_complex_part (object, zero, 0);
2949 write_complex_part (object, zero, 1);
2950 return NULL;
2955 if (size == const0_rtx)
2956 return NULL;
2958 align = MEM_ALIGN (object);
2960 if (CONST_INT_P (size)
2961 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2962 CLEAR_BY_PIECES,
2963 optimize_insn_for_speed_p ()))
2964 clear_by_pieces (object, INTVAL (size), align);
2965 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2966 expected_align, expected_size,
2967 min_size, max_size, probable_max_size))
2969 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2970 return set_storage_via_libcall (object, size, const0_rtx,
2971 method == BLOCK_OP_TAILCALL);
2972 else
2973 gcc_unreachable ();
2975 return NULL;
2979 clear_storage (rtx object, rtx size, enum block_op_methods method)
2981 unsigned HOST_WIDE_INT max, min = 0;
2982 if (GET_CODE (size) == CONST_INT)
2983 min = max = UINTVAL (size);
2984 else
2985 max = GET_MODE_MASK (GET_MODE (size));
2986 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2990 /* A subroutine of clear_storage. Expand a call to memset.
2991 Return the return value of memset, 0 otherwise. */
2994 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2996 tree call_expr, fn, object_tree, size_tree, val_tree;
2997 machine_mode size_mode;
2999 object = copy_addr_to_reg (XEXP (object, 0));
3000 object_tree = make_tree (ptr_type_node, object);
3002 if (!CONST_INT_P (val))
3003 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3004 val_tree = make_tree (integer_type_node, val);
3006 size_mode = TYPE_MODE (sizetype);
3007 size = convert_to_mode (size_mode, size, 1);
3008 size = copy_to_mode_reg (size_mode, size);
3009 size_tree = make_tree (sizetype, size);
3011 /* It is incorrect to use the libcall calling conventions for calls to
3012 memset because it can be provided by the user. */
3013 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3014 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3015 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3017 return expand_call (call_expr, NULL_RTX, false);
3020 /* Expand a setmem pattern; return true if successful. */
3022 bool
3023 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3024 unsigned int expected_align, HOST_WIDE_INT expected_size,
3025 unsigned HOST_WIDE_INT min_size,
3026 unsigned HOST_WIDE_INT max_size,
3027 unsigned HOST_WIDE_INT probable_max_size)
3029 /* Try the most limited insn first, because there's no point
3030 including more than one in the machine description unless
3031 the more limited one has some advantage. */
3033 machine_mode mode;
3035 if (expected_align < align)
3036 expected_align = align;
3037 if (expected_size != -1)
3039 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3040 expected_size = max_size;
3041 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3042 expected_size = min_size;
3045 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3046 mode = GET_MODE_WIDER_MODE (mode))
3048 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3050 if (code != CODE_FOR_nothing
3051 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3052 here because if SIZE is less than the mode mask, as it is
3053 returned by the macro, it will definitely be less than the
3054 actual mode mask. Since SIZE is within the Pmode address
3055 space, we limit MODE to Pmode. */
3056 && ((CONST_INT_P (size)
3057 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3058 <= (GET_MODE_MASK (mode) >> 1)))
3059 || max_size <= (GET_MODE_MASK (mode) >> 1)
3060 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3062 struct expand_operand ops[9];
3063 unsigned int nops;
3065 nops = insn_data[(int) code].n_generator_args;
3066 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3068 create_fixed_operand (&ops[0], object);
3069 /* The check above guarantees that this size conversion is valid. */
3070 create_convert_operand_to (&ops[1], size, mode, true);
3071 create_convert_operand_from (&ops[2], val, byte_mode, true);
3072 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3073 if (nops >= 6)
3075 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3076 create_integer_operand (&ops[5], expected_size);
3078 if (nops >= 8)
3080 create_integer_operand (&ops[6], min_size);
3081 /* If we can not represent the maximal size,
3082 make parameter NULL. */
3083 if ((HOST_WIDE_INT) max_size != -1)
3084 create_integer_operand (&ops[7], max_size);
3085 else
3086 create_fixed_operand (&ops[7], NULL);
3088 if (nops == 9)
3090 /* If we can not represent the maximal size,
3091 make parameter NULL. */
3092 if ((HOST_WIDE_INT) probable_max_size != -1)
3093 create_integer_operand (&ops[8], probable_max_size);
3094 else
3095 create_fixed_operand (&ops[8], NULL);
3097 if (maybe_expand_insn (code, nops, ops))
3098 return true;
3102 return false;
3106 /* Write to one of the components of the complex value CPLX. Write VAL to
3107 the real part if IMAG_P is false, and the imaginary part if its true. */
3109 void
3110 write_complex_part (rtx cplx, rtx val, bool imag_p)
3112 machine_mode cmode;
3113 machine_mode imode;
3114 unsigned ibitsize;
3116 if (GET_CODE (cplx) == CONCAT)
3118 emit_move_insn (XEXP (cplx, imag_p), val);
3119 return;
3122 cmode = GET_MODE (cplx);
3123 imode = GET_MODE_INNER (cmode);
3124 ibitsize = GET_MODE_BITSIZE (imode);
3126 /* For MEMs simplify_gen_subreg may generate an invalid new address
3127 because, e.g., the original address is considered mode-dependent
3128 by the target, which restricts simplify_subreg from invoking
3129 adjust_address_nv. Instead of preparing fallback support for an
3130 invalid address, we call adjust_address_nv directly. */
3131 if (MEM_P (cplx))
3133 emit_move_insn (adjust_address_nv (cplx, imode,
3134 imag_p ? GET_MODE_SIZE (imode) : 0),
3135 val);
3136 return;
3139 /* If the sub-object is at least word sized, then we know that subregging
3140 will work. This special case is important, since store_bit_field
3141 wants to operate on integer modes, and there's rarely an OImode to
3142 correspond to TCmode. */
3143 if (ibitsize >= BITS_PER_WORD
3144 /* For hard regs we have exact predicates. Assume we can split
3145 the original object if it spans an even number of hard regs.
3146 This special case is important for SCmode on 64-bit platforms
3147 where the natural size of floating-point regs is 32-bit. */
3148 || (REG_P (cplx)
3149 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3150 && REG_NREGS (cplx) % 2 == 0))
3152 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3153 imag_p ? GET_MODE_SIZE (imode) : 0);
3154 if (part)
3156 emit_move_insn (part, val);
3157 return;
3159 else
3160 /* simplify_gen_subreg may fail for sub-word MEMs. */
3161 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3164 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3165 false);
3168 /* Extract one of the components of the complex value CPLX. Extract the
3169 real part if IMAG_P is false, and the imaginary part if it's true. */
3172 read_complex_part (rtx cplx, bool imag_p)
3174 machine_mode cmode, imode;
3175 unsigned ibitsize;
3177 if (GET_CODE (cplx) == CONCAT)
3178 return XEXP (cplx, imag_p);
3180 cmode = GET_MODE (cplx);
3181 imode = GET_MODE_INNER (cmode);
3182 ibitsize = GET_MODE_BITSIZE (imode);
3184 /* Special case reads from complex constants that got spilled to memory. */
3185 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3187 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3188 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3190 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3191 if (CONSTANT_CLASS_P (part))
3192 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3196 /* For MEMs simplify_gen_subreg may generate an invalid new address
3197 because, e.g., the original address is considered mode-dependent
3198 by the target, which restricts simplify_subreg from invoking
3199 adjust_address_nv. Instead of preparing fallback support for an
3200 invalid address, we call adjust_address_nv directly. */
3201 if (MEM_P (cplx))
3202 return adjust_address_nv (cplx, imode,
3203 imag_p ? GET_MODE_SIZE (imode) : 0);
3205 /* If the sub-object is at least word sized, then we know that subregging
3206 will work. This special case is important, since extract_bit_field
3207 wants to operate on integer modes, and there's rarely an OImode to
3208 correspond to TCmode. */
3209 if (ibitsize >= BITS_PER_WORD
3210 /* For hard regs we have exact predicates. Assume we can split
3211 the original object if it spans an even number of hard regs.
3212 This special case is important for SCmode on 64-bit platforms
3213 where the natural size of floating-point regs is 32-bit. */
3214 || (REG_P (cplx)
3215 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3216 && REG_NREGS (cplx) % 2 == 0))
3218 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3219 imag_p ? GET_MODE_SIZE (imode) : 0);
3220 if (ret)
3221 return ret;
3222 else
3223 /* simplify_gen_subreg may fail for sub-word MEMs. */
3224 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3227 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3228 true, NULL_RTX, imode, imode, false);
3231 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3232 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3233 represented in NEW_MODE. If FORCE is true, this will never happen, as
3234 we'll force-create a SUBREG if needed. */
3236 static rtx
3237 emit_move_change_mode (machine_mode new_mode,
3238 machine_mode old_mode, rtx x, bool force)
3240 rtx ret;
3242 if (push_operand (x, GET_MODE (x)))
3244 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3245 MEM_COPY_ATTRIBUTES (ret, x);
3247 else if (MEM_P (x))
3249 /* We don't have to worry about changing the address since the
3250 size in bytes is supposed to be the same. */
3251 if (reload_in_progress)
3253 /* Copy the MEM to change the mode and move any
3254 substitutions from the old MEM to the new one. */
3255 ret = adjust_address_nv (x, new_mode, 0);
3256 copy_replacements (x, ret);
3258 else
3259 ret = adjust_address (x, new_mode, 0);
3261 else
3263 /* Note that we do want simplify_subreg's behavior of validating
3264 that the new mode is ok for a hard register. If we were to use
3265 simplify_gen_subreg, we would create the subreg, but would
3266 probably run into the target not being able to implement it. */
3267 /* Except, of course, when FORCE is true, when this is exactly what
3268 we want. Which is needed for CCmodes on some targets. */
3269 if (force)
3270 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3271 else
3272 ret = simplify_subreg (new_mode, x, old_mode, 0);
3275 return ret;
3278 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3279 an integer mode of the same size as MODE. Returns the instruction
3280 emitted, or NULL if such a move could not be generated. */
3282 static rtx_insn *
3283 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3285 machine_mode imode;
3286 enum insn_code code;
3288 /* There must exist a mode of the exact size we require. */
3289 imode = int_mode_for_mode (mode);
3290 if (imode == BLKmode)
3291 return NULL;
3293 /* The target must support moves in this mode. */
3294 code = optab_handler (mov_optab, imode);
3295 if (code == CODE_FOR_nothing)
3296 return NULL;
3298 x = emit_move_change_mode (imode, mode, x, force);
3299 if (x == NULL_RTX)
3300 return NULL;
3301 y = emit_move_change_mode (imode, mode, y, force);
3302 if (y == NULL_RTX)
3303 return NULL;
3304 return emit_insn (GEN_FCN (code) (x, y));
3307 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3308 Return an equivalent MEM that does not use an auto-increment. */
3311 emit_move_resolve_push (machine_mode mode, rtx x)
3313 enum rtx_code code = GET_CODE (XEXP (x, 0));
3314 HOST_WIDE_INT adjust;
3315 rtx temp;
3317 adjust = GET_MODE_SIZE (mode);
3318 #ifdef PUSH_ROUNDING
3319 adjust = PUSH_ROUNDING (adjust);
3320 #endif
3321 if (code == PRE_DEC || code == POST_DEC)
3322 adjust = -adjust;
3323 else if (code == PRE_MODIFY || code == POST_MODIFY)
3325 rtx expr = XEXP (XEXP (x, 0), 1);
3326 HOST_WIDE_INT val;
3328 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3329 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3330 val = INTVAL (XEXP (expr, 1));
3331 if (GET_CODE (expr) == MINUS)
3332 val = -val;
3333 gcc_assert (adjust == val || adjust == -val);
3334 adjust = val;
3337 /* Do not use anti_adjust_stack, since we don't want to update
3338 stack_pointer_delta. */
3339 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3340 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3341 0, OPTAB_LIB_WIDEN);
3342 if (temp != stack_pointer_rtx)
3343 emit_move_insn (stack_pointer_rtx, temp);
3345 switch (code)
3347 case PRE_INC:
3348 case PRE_DEC:
3349 case PRE_MODIFY:
3350 temp = stack_pointer_rtx;
3351 break;
3352 case POST_INC:
3353 case POST_DEC:
3354 case POST_MODIFY:
3355 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3356 break;
3357 default:
3358 gcc_unreachable ();
3361 return replace_equiv_address (x, temp);
3364 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3365 X is known to satisfy push_operand, and MODE is known to be complex.
3366 Returns the last instruction emitted. */
3368 rtx_insn *
3369 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3371 machine_mode submode = GET_MODE_INNER (mode);
3372 bool imag_first;
3374 #ifdef PUSH_ROUNDING
3375 unsigned int submodesize = GET_MODE_SIZE (submode);
3377 /* In case we output to the stack, but the size is smaller than the
3378 machine can push exactly, we need to use move instructions. */
3379 if (PUSH_ROUNDING (submodesize) != submodesize)
3381 x = emit_move_resolve_push (mode, x);
3382 return emit_move_insn (x, y);
3384 #endif
3386 /* Note that the real part always precedes the imag part in memory
3387 regardless of machine's endianness. */
3388 switch (GET_CODE (XEXP (x, 0)))
3390 case PRE_DEC:
3391 case POST_DEC:
3392 imag_first = true;
3393 break;
3394 case PRE_INC:
3395 case POST_INC:
3396 imag_first = false;
3397 break;
3398 default:
3399 gcc_unreachable ();
3402 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3403 read_complex_part (y, imag_first));
3404 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3405 read_complex_part (y, !imag_first));
3408 /* A subroutine of emit_move_complex. Perform the move from Y to X
3409 via two moves of the parts. Returns the last instruction emitted. */
3411 rtx_insn *
3412 emit_move_complex_parts (rtx x, rtx y)
3414 /* Show the output dies here. This is necessary for SUBREGs
3415 of pseudos since we cannot track their lifetimes correctly;
3416 hard regs shouldn't appear here except as return values. */
3417 if (!reload_completed && !reload_in_progress
3418 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3419 emit_clobber (x);
3421 write_complex_part (x, read_complex_part (y, false), false);
3422 write_complex_part (x, read_complex_part (y, true), true);
3424 return get_last_insn ();
3427 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3428 MODE is known to be complex. Returns the last instruction emitted. */
3430 static rtx_insn *
3431 emit_move_complex (machine_mode mode, rtx x, rtx y)
3433 bool try_int;
3435 /* Need to take special care for pushes, to maintain proper ordering
3436 of the data, and possibly extra padding. */
3437 if (push_operand (x, mode))
3438 return emit_move_complex_push (mode, x, y);
3440 /* See if we can coerce the target into moving both values at once, except
3441 for floating point where we favor moving as parts if this is easy. */
3442 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3443 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3444 && !(REG_P (x)
3445 && HARD_REGISTER_P (x)
3446 && REG_NREGS (x) == 1)
3447 && !(REG_P (y)
3448 && HARD_REGISTER_P (y)
3449 && REG_NREGS (y) == 1))
3450 try_int = false;
3451 /* Not possible if the values are inherently not adjacent. */
3452 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3453 try_int = false;
3454 /* Is possible if both are registers (or subregs of registers). */
3455 else if (register_operand (x, mode) && register_operand (y, mode))
3456 try_int = true;
3457 /* If one of the operands is a memory, and alignment constraints
3458 are friendly enough, we may be able to do combined memory operations.
3459 We do not attempt this if Y is a constant because that combination is
3460 usually better with the by-parts thing below. */
3461 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3462 && (!STRICT_ALIGNMENT
3463 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3464 try_int = true;
3465 else
3466 try_int = false;
3468 if (try_int)
3470 rtx_insn *ret;
3472 /* For memory to memory moves, optimal behavior can be had with the
3473 existing block move logic. */
3474 if (MEM_P (x) && MEM_P (y))
3476 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3477 BLOCK_OP_NO_LIBCALL);
3478 return get_last_insn ();
3481 ret = emit_move_via_integer (mode, x, y, true);
3482 if (ret)
3483 return ret;
3486 return emit_move_complex_parts (x, y);
3489 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3490 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3492 static rtx_insn *
3493 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3495 rtx_insn *ret;
3497 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3498 if (mode != CCmode)
3500 enum insn_code code = optab_handler (mov_optab, CCmode);
3501 if (code != CODE_FOR_nothing)
3503 x = emit_move_change_mode (CCmode, mode, x, true);
3504 y = emit_move_change_mode (CCmode, mode, y, true);
3505 return emit_insn (GEN_FCN (code) (x, y));
3509 /* Otherwise, find the MODE_INT mode of the same width. */
3510 ret = emit_move_via_integer (mode, x, y, false);
3511 gcc_assert (ret != NULL);
3512 return ret;
3515 /* Return true if word I of OP lies entirely in the
3516 undefined bits of a paradoxical subreg. */
3518 static bool
3519 undefined_operand_subword_p (const_rtx op, int i)
3521 machine_mode innermode, innermostmode;
3522 int offset;
3523 if (GET_CODE (op) != SUBREG)
3524 return false;
3525 innermode = GET_MODE (op);
3526 innermostmode = GET_MODE (SUBREG_REG (op));
3527 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3528 /* The SUBREG_BYTE represents offset, as if the value were stored in
3529 memory, except for a paradoxical subreg where we define
3530 SUBREG_BYTE to be 0; undo this exception as in
3531 simplify_subreg. */
3532 if (SUBREG_BYTE (op) == 0
3533 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3535 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3536 if (WORDS_BIG_ENDIAN)
3537 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3538 if (BYTES_BIG_ENDIAN)
3539 offset += difference % UNITS_PER_WORD;
3541 if (offset >= GET_MODE_SIZE (innermostmode)
3542 || offset <= -GET_MODE_SIZE (word_mode))
3543 return true;
3544 return false;
3547 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3548 MODE is any multi-word or full-word mode that lacks a move_insn
3549 pattern. Note that you will get better code if you define such
3550 patterns, even if they must turn into multiple assembler instructions. */
3552 static rtx_insn *
3553 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3555 rtx_insn *last_insn = 0;
3556 rtx_insn *seq;
3557 rtx inner;
3558 bool need_clobber;
3559 int i;
3561 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3563 /* If X is a push on the stack, do the push now and replace
3564 X with a reference to the stack pointer. */
3565 if (push_operand (x, mode))
3566 x = emit_move_resolve_push (mode, x);
3568 /* If we are in reload, see if either operand is a MEM whose address
3569 is scheduled for replacement. */
3570 if (reload_in_progress && MEM_P (x)
3571 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3572 x = replace_equiv_address_nv (x, inner);
3573 if (reload_in_progress && MEM_P (y)
3574 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3575 y = replace_equiv_address_nv (y, inner);
3577 start_sequence ();
3579 need_clobber = false;
3580 for (i = 0;
3581 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3582 i++)
3584 rtx xpart = operand_subword (x, i, 1, mode);
3585 rtx ypart;
3587 /* Do not generate code for a move if it would come entirely
3588 from the undefined bits of a paradoxical subreg. */
3589 if (undefined_operand_subword_p (y, i))
3590 continue;
3592 ypart = operand_subword (y, i, 1, mode);
3594 /* If we can't get a part of Y, put Y into memory if it is a
3595 constant. Otherwise, force it into a register. Then we must
3596 be able to get a part of Y. */
3597 if (ypart == 0 && CONSTANT_P (y))
3599 y = use_anchored_address (force_const_mem (mode, y));
3600 ypart = operand_subword (y, i, 1, mode);
3602 else if (ypart == 0)
3603 ypart = operand_subword_force (y, i, mode);
3605 gcc_assert (xpart && ypart);
3607 need_clobber |= (GET_CODE (xpart) == SUBREG);
3609 last_insn = emit_move_insn (xpart, ypart);
3612 seq = get_insns ();
3613 end_sequence ();
3615 /* Show the output dies here. This is necessary for SUBREGs
3616 of pseudos since we cannot track their lifetimes correctly;
3617 hard regs shouldn't appear here except as return values.
3618 We never want to emit such a clobber after reload. */
3619 if (x != y
3620 && ! (reload_in_progress || reload_completed)
3621 && need_clobber != 0)
3622 emit_clobber (x);
3624 emit_insn (seq);
3626 return last_insn;
3629 /* Low level part of emit_move_insn.
3630 Called just like emit_move_insn, but assumes X and Y
3631 are basically valid. */
3633 rtx_insn *
3634 emit_move_insn_1 (rtx x, rtx y)
3636 machine_mode mode = GET_MODE (x);
3637 enum insn_code code;
3639 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3641 code = optab_handler (mov_optab, mode);
3642 if (code != CODE_FOR_nothing)
3643 return emit_insn (GEN_FCN (code) (x, y));
3645 /* Expand complex moves by moving real part and imag part. */
3646 if (COMPLEX_MODE_P (mode))
3647 return emit_move_complex (mode, x, y);
3649 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3650 || ALL_FIXED_POINT_MODE_P (mode))
3652 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3654 /* If we can't find an integer mode, use multi words. */
3655 if (result)
3656 return result;
3657 else
3658 return emit_move_multi_word (mode, x, y);
3661 if (GET_MODE_CLASS (mode) == MODE_CC)
3662 return emit_move_ccmode (mode, x, y);
3664 /* Try using a move pattern for the corresponding integer mode. This is
3665 only safe when simplify_subreg can convert MODE constants into integer
3666 constants. At present, it can only do this reliably if the value
3667 fits within a HOST_WIDE_INT. */
3668 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3670 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3672 if (ret)
3674 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3675 return ret;
3679 return emit_move_multi_word (mode, x, y);
3682 /* Generate code to copy Y into X.
3683 Both Y and X must have the same mode, except that
3684 Y can be a constant with VOIDmode.
3685 This mode cannot be BLKmode; use emit_block_move for that.
3687 Return the last instruction emitted. */
3689 rtx_insn *
3690 emit_move_insn (rtx x, rtx y)
3692 machine_mode mode = GET_MODE (x);
3693 rtx y_cst = NULL_RTX;
3694 rtx_insn *last_insn;
3695 rtx set;
3697 gcc_assert (mode != BLKmode
3698 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3700 if (CONSTANT_P (y))
3702 if (optimize
3703 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3704 && (last_insn = compress_float_constant (x, y)))
3705 return last_insn;
3707 y_cst = y;
3709 if (!targetm.legitimate_constant_p (mode, y))
3711 y = force_const_mem (mode, y);
3713 /* If the target's cannot_force_const_mem prevented the spill,
3714 assume that the target's move expanders will also take care
3715 of the non-legitimate constant. */
3716 if (!y)
3717 y = y_cst;
3718 else
3719 y = use_anchored_address (y);
3723 /* If X or Y are memory references, verify that their addresses are valid
3724 for the machine. */
3725 if (MEM_P (x)
3726 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3727 MEM_ADDR_SPACE (x))
3728 && ! push_operand (x, GET_MODE (x))))
3729 x = validize_mem (x);
3731 if (MEM_P (y)
3732 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3733 MEM_ADDR_SPACE (y)))
3734 y = validize_mem (y);
3736 gcc_assert (mode != BLKmode);
3738 last_insn = emit_move_insn_1 (x, y);
3740 if (y_cst && REG_P (x)
3741 && (set = single_set (last_insn)) != NULL_RTX
3742 && SET_DEST (set) == x
3743 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3744 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3746 return last_insn;
3749 /* Generate the body of an instruction to copy Y into X.
3750 It may be a list of insns, if one insn isn't enough. */
3752 rtx_insn *
3753 gen_move_insn (rtx x, rtx y)
3755 rtx_insn *seq;
3757 start_sequence ();
3758 emit_move_insn_1 (x, y);
3759 seq = get_insns ();
3760 end_sequence ();
3761 return seq;
3764 /* If Y is representable exactly in a narrower mode, and the target can
3765 perform the extension directly from constant or memory, then emit the
3766 move as an extension. */
3768 static rtx_insn *
3769 compress_float_constant (rtx x, rtx y)
3771 machine_mode dstmode = GET_MODE (x);
3772 machine_mode orig_srcmode = GET_MODE (y);
3773 machine_mode srcmode;
3774 const REAL_VALUE_TYPE *r;
3775 int oldcost, newcost;
3776 bool speed = optimize_insn_for_speed_p ();
3778 r = CONST_DOUBLE_REAL_VALUE (y);
3780 if (targetm.legitimate_constant_p (dstmode, y))
3781 oldcost = set_src_cost (y, orig_srcmode, speed);
3782 else
3783 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3785 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3786 srcmode != orig_srcmode;
3787 srcmode = GET_MODE_WIDER_MODE (srcmode))
3789 enum insn_code ic;
3790 rtx trunc_y;
3791 rtx_insn *last_insn;
3793 /* Skip if the target can't extend this way. */
3794 ic = can_extend_p (dstmode, srcmode, 0);
3795 if (ic == CODE_FOR_nothing)
3796 continue;
3798 /* Skip if the narrowed value isn't exact. */
3799 if (! exact_real_truncate (srcmode, r))
3800 continue;
3802 trunc_y = const_double_from_real_value (*r, srcmode);
3804 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3806 /* Skip if the target needs extra instructions to perform
3807 the extension. */
3808 if (!insn_operand_matches (ic, 1, trunc_y))
3809 continue;
3810 /* This is valid, but may not be cheaper than the original. */
3811 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3812 dstmode, speed);
3813 if (oldcost < newcost)
3814 continue;
3816 else if (float_extend_from_mem[dstmode][srcmode])
3818 trunc_y = force_const_mem (srcmode, trunc_y);
3819 /* This is valid, but may not be cheaper than the original. */
3820 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3821 dstmode, speed);
3822 if (oldcost < newcost)
3823 continue;
3824 trunc_y = validize_mem (trunc_y);
3826 else
3827 continue;
3829 /* For CSE's benefit, force the compressed constant pool entry
3830 into a new pseudo. This constant may be used in different modes,
3831 and if not, combine will put things back together for us. */
3832 trunc_y = force_reg (srcmode, trunc_y);
3834 /* If x is a hard register, perform the extension into a pseudo,
3835 so that e.g. stack realignment code is aware of it. */
3836 rtx target = x;
3837 if (REG_P (x) && HARD_REGISTER_P (x))
3838 target = gen_reg_rtx (dstmode);
3840 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3841 last_insn = get_last_insn ();
3843 if (REG_P (target))
3844 set_unique_reg_note (last_insn, REG_EQUAL, y);
3846 if (target != x)
3847 return emit_move_insn (x, target);
3848 return last_insn;
3851 return NULL;
3854 /* Pushing data onto the stack. */
3856 /* Push a block of length SIZE (perhaps variable)
3857 and return an rtx to address the beginning of the block.
3858 The value may be virtual_outgoing_args_rtx.
3860 EXTRA is the number of bytes of padding to push in addition to SIZE.
3861 BELOW nonzero means this padding comes at low addresses;
3862 otherwise, the padding comes at high addresses. */
3865 push_block (rtx size, int extra, int below)
3867 rtx temp;
3869 size = convert_modes (Pmode, ptr_mode, size, 1);
3870 if (CONSTANT_P (size))
3871 anti_adjust_stack (plus_constant (Pmode, size, extra));
3872 else if (REG_P (size) && extra == 0)
3873 anti_adjust_stack (size);
3874 else
3876 temp = copy_to_mode_reg (Pmode, size);
3877 if (extra != 0)
3878 temp = expand_binop (Pmode, add_optab, temp,
3879 gen_int_mode (extra, Pmode),
3880 temp, 0, OPTAB_LIB_WIDEN);
3881 anti_adjust_stack (temp);
3884 if (STACK_GROWS_DOWNWARD)
3886 temp = virtual_outgoing_args_rtx;
3887 if (extra != 0 && below)
3888 temp = plus_constant (Pmode, temp, extra);
3890 else
3892 if (CONST_INT_P (size))
3893 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3894 -INTVAL (size) - (below ? 0 : extra));
3895 else if (extra != 0 && !below)
3896 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3897 negate_rtx (Pmode, plus_constant (Pmode, size,
3898 extra)));
3899 else
3900 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3901 negate_rtx (Pmode, size));
3904 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3907 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3909 static rtx
3910 mem_autoinc_base (rtx mem)
3912 if (MEM_P (mem))
3914 rtx addr = XEXP (mem, 0);
3915 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3916 return XEXP (addr, 0);
3918 return NULL;
3921 /* A utility routine used here, in reload, and in try_split. The insns
3922 after PREV up to and including LAST are known to adjust the stack,
3923 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3924 placing notes as appropriate. PREV may be NULL, indicating the
3925 entire insn sequence prior to LAST should be scanned.
3927 The set of allowed stack pointer modifications is small:
3928 (1) One or more auto-inc style memory references (aka pushes),
3929 (2) One or more addition/subtraction with the SP as destination,
3930 (3) A single move insn with the SP as destination,
3931 (4) A call_pop insn,
3932 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3934 Insns in the sequence that do not modify the SP are ignored,
3935 except for noreturn calls.
3937 The return value is the amount of adjustment that can be trivially
3938 verified, via immediate operand or auto-inc. If the adjustment
3939 cannot be trivially extracted, the return value is INT_MIN. */
3941 HOST_WIDE_INT
3942 find_args_size_adjust (rtx_insn *insn)
3944 rtx dest, set, pat;
3945 int i;
3947 pat = PATTERN (insn);
3948 set = NULL;
3950 /* Look for a call_pop pattern. */
3951 if (CALL_P (insn))
3953 /* We have to allow non-call_pop patterns for the case
3954 of emit_single_push_insn of a TLS address. */
3955 if (GET_CODE (pat) != PARALLEL)
3956 return 0;
3958 /* All call_pop have a stack pointer adjust in the parallel.
3959 The call itself is always first, and the stack adjust is
3960 usually last, so search from the end. */
3961 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3963 set = XVECEXP (pat, 0, i);
3964 if (GET_CODE (set) != SET)
3965 continue;
3966 dest = SET_DEST (set);
3967 if (dest == stack_pointer_rtx)
3968 break;
3970 /* We'd better have found the stack pointer adjust. */
3971 if (i == 0)
3972 return 0;
3973 /* Fall through to process the extracted SET and DEST
3974 as if it was a standalone insn. */
3976 else if (GET_CODE (pat) == SET)
3977 set = pat;
3978 else if ((set = single_set (insn)) != NULL)
3980 else if (GET_CODE (pat) == PARALLEL)
3982 /* ??? Some older ports use a parallel with a stack adjust
3983 and a store for a PUSH_ROUNDING pattern, rather than a
3984 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3985 /* ??? See h8300 and m68k, pushqi1. */
3986 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3988 set = XVECEXP (pat, 0, i);
3989 if (GET_CODE (set) != SET)
3990 continue;
3991 dest = SET_DEST (set);
3992 if (dest == stack_pointer_rtx)
3993 break;
3995 /* We do not expect an auto-inc of the sp in the parallel. */
3996 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3997 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3998 != stack_pointer_rtx);
4000 if (i < 0)
4001 return 0;
4003 else
4004 return 0;
4006 dest = SET_DEST (set);
4008 /* Look for direct modifications of the stack pointer. */
4009 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4011 /* Look for a trivial adjustment, otherwise assume nothing. */
4012 /* Note that the SPU restore_stack_block pattern refers to
4013 the stack pointer in V4SImode. Consider that non-trivial. */
4014 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4015 && GET_CODE (SET_SRC (set)) == PLUS
4016 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4017 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4018 return INTVAL (XEXP (SET_SRC (set), 1));
4019 /* ??? Reload can generate no-op moves, which will be cleaned
4020 up later. Recognize it and continue searching. */
4021 else if (rtx_equal_p (dest, SET_SRC (set)))
4022 return 0;
4023 else
4024 return HOST_WIDE_INT_MIN;
4026 else
4028 rtx mem, addr;
4030 /* Otherwise only think about autoinc patterns. */
4031 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4033 mem = dest;
4034 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4035 != stack_pointer_rtx);
4037 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4038 mem = SET_SRC (set);
4039 else
4040 return 0;
4042 addr = XEXP (mem, 0);
4043 switch (GET_CODE (addr))
4045 case PRE_INC:
4046 case POST_INC:
4047 return GET_MODE_SIZE (GET_MODE (mem));
4048 case PRE_DEC:
4049 case POST_DEC:
4050 return -GET_MODE_SIZE (GET_MODE (mem));
4051 case PRE_MODIFY:
4052 case POST_MODIFY:
4053 addr = XEXP (addr, 1);
4054 gcc_assert (GET_CODE (addr) == PLUS);
4055 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4056 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4057 return INTVAL (XEXP (addr, 1));
4058 default:
4059 gcc_unreachable ();
4065 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4067 int args_size = end_args_size;
4068 bool saw_unknown = false;
4069 rtx_insn *insn;
4071 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4073 HOST_WIDE_INT this_delta;
4075 if (!NONDEBUG_INSN_P (insn))
4076 continue;
4078 this_delta = find_args_size_adjust (insn);
4079 if (this_delta == 0)
4081 if (!CALL_P (insn)
4082 || ACCUMULATE_OUTGOING_ARGS
4083 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4084 continue;
4087 gcc_assert (!saw_unknown);
4088 if (this_delta == HOST_WIDE_INT_MIN)
4089 saw_unknown = true;
4091 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4092 if (STACK_GROWS_DOWNWARD)
4093 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4095 args_size -= this_delta;
4098 return saw_unknown ? INT_MIN : args_size;
4101 #ifdef PUSH_ROUNDING
4102 /* Emit single push insn. */
4104 static void
4105 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4107 rtx dest_addr;
4108 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4109 rtx dest;
4110 enum insn_code icode;
4112 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4113 /* If there is push pattern, use it. Otherwise try old way of throwing
4114 MEM representing push operation to move expander. */
4115 icode = optab_handler (push_optab, mode);
4116 if (icode != CODE_FOR_nothing)
4118 struct expand_operand ops[1];
4120 create_input_operand (&ops[0], x, mode);
4121 if (maybe_expand_insn (icode, 1, ops))
4122 return;
4124 if (GET_MODE_SIZE (mode) == rounded_size)
4125 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4126 /* If we are to pad downward, adjust the stack pointer first and
4127 then store X into the stack location using an offset. This is
4128 because emit_move_insn does not know how to pad; it does not have
4129 access to type. */
4130 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4132 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4133 HOST_WIDE_INT offset;
4135 emit_move_insn (stack_pointer_rtx,
4136 expand_binop (Pmode,
4137 STACK_GROWS_DOWNWARD ? sub_optab
4138 : add_optab,
4139 stack_pointer_rtx,
4140 gen_int_mode (rounded_size, Pmode),
4141 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4143 offset = (HOST_WIDE_INT) padding_size;
4144 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4145 /* We have already decremented the stack pointer, so get the
4146 previous value. */
4147 offset += (HOST_WIDE_INT) rounded_size;
4149 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4150 /* We have already incremented the stack pointer, so get the
4151 previous value. */
4152 offset -= (HOST_WIDE_INT) rounded_size;
4154 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4155 gen_int_mode (offset, Pmode));
4157 else
4159 if (STACK_GROWS_DOWNWARD)
4160 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4161 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4162 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4163 Pmode));
4164 else
4165 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4166 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4167 gen_int_mode (rounded_size, Pmode));
4169 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4172 dest = gen_rtx_MEM (mode, dest_addr);
4174 if (type != 0)
4176 set_mem_attributes (dest, type, 1);
4178 if (cfun->tail_call_marked)
4179 /* Function incoming arguments may overlap with sibling call
4180 outgoing arguments and we cannot allow reordering of reads
4181 from function arguments with stores to outgoing arguments
4182 of sibling calls. */
4183 set_mem_alias_set (dest, 0);
4185 emit_move_insn (dest, x);
4188 /* Emit and annotate a single push insn. */
4190 static void
4191 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4193 int delta, old_delta = stack_pointer_delta;
4194 rtx_insn *prev = get_last_insn ();
4195 rtx_insn *last;
4197 emit_single_push_insn_1 (mode, x, type);
4199 last = get_last_insn ();
4201 /* Notice the common case where we emitted exactly one insn. */
4202 if (PREV_INSN (last) == prev)
4204 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4205 return;
4208 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4209 gcc_assert (delta == INT_MIN || delta == old_delta);
4211 #endif
4213 /* If reading SIZE bytes from X will end up reading from
4214 Y return the number of bytes that overlap. Return -1
4215 if there is no overlap or -2 if we can't determine
4216 (for example when X and Y have different base registers). */
4218 static int
4219 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4221 rtx tmp = plus_constant (Pmode, x, size);
4222 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4224 if (!CONST_INT_P (sub))
4225 return -2;
4227 HOST_WIDE_INT val = INTVAL (sub);
4229 return IN_RANGE (val, 1, size) ? val : -1;
4232 /* Generate code to push X onto the stack, assuming it has mode MODE and
4233 type TYPE.
4234 MODE is redundant except when X is a CONST_INT (since they don't
4235 carry mode info).
4236 SIZE is an rtx for the size of data to be copied (in bytes),
4237 needed only if X is BLKmode.
4238 Return true if successful. May return false if asked to push a
4239 partial argument during a sibcall optimization (as specified by
4240 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4241 to not overlap.
4243 ALIGN (in bits) is maximum alignment we can assume.
4245 If PARTIAL and REG are both nonzero, then copy that many of the first
4246 bytes of X into registers starting with REG, and push the rest of X.
4247 The amount of space pushed is decreased by PARTIAL bytes.
4248 REG must be a hard register in this case.
4249 If REG is zero but PARTIAL is not, take any all others actions for an
4250 argument partially in registers, but do not actually load any
4251 registers.
4253 EXTRA is the amount in bytes of extra space to leave next to this arg.
4254 This is ignored if an argument block has already been allocated.
4256 On a machine that lacks real push insns, ARGS_ADDR is the address of
4257 the bottom of the argument block for this call. We use indexing off there
4258 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4259 argument block has not been preallocated.
4261 ARGS_SO_FAR is the size of args previously pushed for this call.
4263 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4264 for arguments passed in registers. If nonzero, it will be the number
4265 of bytes required. */
4267 bool
4268 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4269 unsigned int align, int partial, rtx reg, int extra,
4270 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4271 rtx alignment_pad, bool sibcall_p)
4273 rtx xinner;
4274 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4276 /* Decide where to pad the argument: `downward' for below,
4277 `upward' for above, or `none' for don't pad it.
4278 Default is below for small data on big-endian machines; else above. */
4279 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4281 /* Invert direction if stack is post-decrement.
4282 FIXME: why? */
4283 if (STACK_PUSH_CODE == POST_DEC)
4284 if (where_pad != none)
4285 where_pad = (where_pad == downward ? upward : downward);
4287 xinner = x;
4289 int nregs = partial / UNITS_PER_WORD;
4290 rtx *tmp_regs = NULL;
4291 int overlapping = 0;
4293 if (mode == BLKmode
4294 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4296 /* Copy a block into the stack, entirely or partially. */
4298 rtx temp;
4299 int used;
4300 int offset;
4301 int skip;
4303 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4304 used = partial - offset;
4306 if (mode != BLKmode)
4308 /* A value is to be stored in an insufficiently aligned
4309 stack slot; copy via a suitably aligned slot if
4310 necessary. */
4311 size = GEN_INT (GET_MODE_SIZE (mode));
4312 if (!MEM_P (xinner))
4314 temp = assign_temp (type, 1, 1);
4315 emit_move_insn (temp, xinner);
4316 xinner = temp;
4320 gcc_assert (size);
4322 /* USED is now the # of bytes we need not copy to the stack
4323 because registers will take care of them. */
4325 if (partial != 0)
4326 xinner = adjust_address (xinner, BLKmode, used);
4328 /* If the partial register-part of the arg counts in its stack size,
4329 skip the part of stack space corresponding to the registers.
4330 Otherwise, start copying to the beginning of the stack space,
4331 by setting SKIP to 0. */
4332 skip = (reg_parm_stack_space == 0) ? 0 : used;
4334 #ifdef PUSH_ROUNDING
4335 /* Do it with several push insns if that doesn't take lots of insns
4336 and if there is no difficulty with push insns that skip bytes
4337 on the stack for alignment purposes. */
4338 if (args_addr == 0
4339 && PUSH_ARGS
4340 && CONST_INT_P (size)
4341 && skip == 0
4342 && MEM_ALIGN (xinner) >= align
4343 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4344 /* Here we avoid the case of a structure whose weak alignment
4345 forces many pushes of a small amount of data,
4346 and such small pushes do rounding that causes trouble. */
4347 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4348 || align >= BIGGEST_ALIGNMENT
4349 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4350 == (align / BITS_PER_UNIT)))
4351 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4353 /* Push padding now if padding above and stack grows down,
4354 or if padding below and stack grows up.
4355 But if space already allocated, this has already been done. */
4356 if (extra && args_addr == 0
4357 && where_pad != none && where_pad != stack_direction)
4358 anti_adjust_stack (GEN_INT (extra));
4360 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4362 else
4363 #endif /* PUSH_ROUNDING */
4365 rtx target;
4367 /* Otherwise make space on the stack and copy the data
4368 to the address of that space. */
4370 /* Deduct words put into registers from the size we must copy. */
4371 if (partial != 0)
4373 if (CONST_INT_P (size))
4374 size = GEN_INT (INTVAL (size) - used);
4375 else
4376 size = expand_binop (GET_MODE (size), sub_optab, size,
4377 gen_int_mode (used, GET_MODE (size)),
4378 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4381 /* Get the address of the stack space.
4382 In this case, we do not deal with EXTRA separately.
4383 A single stack adjust will do. */
4384 if (! args_addr)
4386 temp = push_block (size, extra, where_pad == downward);
4387 extra = 0;
4389 else if (CONST_INT_P (args_so_far))
4390 temp = memory_address (BLKmode,
4391 plus_constant (Pmode, args_addr,
4392 skip + INTVAL (args_so_far)));
4393 else
4394 temp = memory_address (BLKmode,
4395 plus_constant (Pmode,
4396 gen_rtx_PLUS (Pmode,
4397 args_addr,
4398 args_so_far),
4399 skip));
4401 if (!ACCUMULATE_OUTGOING_ARGS)
4403 /* If the source is referenced relative to the stack pointer,
4404 copy it to another register to stabilize it. We do not need
4405 to do this if we know that we won't be changing sp. */
4407 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4408 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4409 temp = copy_to_reg (temp);
4412 target = gen_rtx_MEM (BLKmode, temp);
4414 /* We do *not* set_mem_attributes here, because incoming arguments
4415 may overlap with sibling call outgoing arguments and we cannot
4416 allow reordering of reads from function arguments with stores
4417 to outgoing arguments of sibling calls. We do, however, want
4418 to record the alignment of the stack slot. */
4419 /* ALIGN may well be better aligned than TYPE, e.g. due to
4420 PARM_BOUNDARY. Assume the caller isn't lying. */
4421 set_mem_align (target, align);
4423 /* If part should go in registers and pushing to that part would
4424 overwrite some of the values that need to go into regs, load the
4425 overlapping values into temporary pseudos to be moved into the hard
4426 regs at the end after the stack pushing has completed.
4427 We cannot load them directly into the hard regs here because
4428 they can be clobbered by the block move expansions.
4429 See PR 65358. */
4431 if (partial > 0 && reg != 0 && mode == BLKmode
4432 && GET_CODE (reg) != PARALLEL)
4434 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4435 if (overlapping > 0)
4437 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4438 overlapping /= UNITS_PER_WORD;
4440 tmp_regs = XALLOCAVEC (rtx, overlapping);
4442 for (int i = 0; i < overlapping; i++)
4443 tmp_regs[i] = gen_reg_rtx (word_mode);
4445 for (int i = 0; i < overlapping; i++)
4446 emit_move_insn (tmp_regs[i],
4447 operand_subword_force (target, i, mode));
4449 else if (overlapping == -1)
4450 overlapping = 0;
4451 /* Could not determine whether there is overlap.
4452 Fail the sibcall. */
4453 else
4455 overlapping = 0;
4456 if (sibcall_p)
4457 return false;
4460 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4463 else if (partial > 0)
4465 /* Scalar partly in registers. */
4467 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4468 int i;
4469 int not_stack;
4470 /* # bytes of start of argument
4471 that we must make space for but need not store. */
4472 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4473 int args_offset = INTVAL (args_so_far);
4474 int skip;
4476 /* Push padding now if padding above and stack grows down,
4477 or if padding below and stack grows up.
4478 But if space already allocated, this has already been done. */
4479 if (extra && args_addr == 0
4480 && where_pad != none && where_pad != stack_direction)
4481 anti_adjust_stack (GEN_INT (extra));
4483 /* If we make space by pushing it, we might as well push
4484 the real data. Otherwise, we can leave OFFSET nonzero
4485 and leave the space uninitialized. */
4486 if (args_addr == 0)
4487 offset = 0;
4489 /* Now NOT_STACK gets the number of words that we don't need to
4490 allocate on the stack. Convert OFFSET to words too. */
4491 not_stack = (partial - offset) / UNITS_PER_WORD;
4492 offset /= UNITS_PER_WORD;
4494 /* If the partial register-part of the arg counts in its stack size,
4495 skip the part of stack space corresponding to the registers.
4496 Otherwise, start copying to the beginning of the stack space,
4497 by setting SKIP to 0. */
4498 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4500 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4501 x = validize_mem (force_const_mem (mode, x));
4503 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4504 SUBREGs of such registers are not allowed. */
4505 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4506 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4507 x = copy_to_reg (x);
4509 /* Loop over all the words allocated on the stack for this arg. */
4510 /* We can do it by words, because any scalar bigger than a word
4511 has a size a multiple of a word. */
4512 for (i = size - 1; i >= not_stack; i--)
4513 if (i >= not_stack + offset)
4514 if (!emit_push_insn (operand_subword_force (x, i, mode),
4515 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4516 0, args_addr,
4517 GEN_INT (args_offset + ((i - not_stack + skip)
4518 * UNITS_PER_WORD)),
4519 reg_parm_stack_space, alignment_pad, sibcall_p))
4520 return false;
4522 else
4524 rtx addr;
4525 rtx dest;
4527 /* Push padding now if padding above and stack grows down,
4528 or if padding below and stack grows up.
4529 But if space already allocated, this has already been done. */
4530 if (extra && args_addr == 0
4531 && where_pad != none && where_pad != stack_direction)
4532 anti_adjust_stack (GEN_INT (extra));
4534 #ifdef PUSH_ROUNDING
4535 if (args_addr == 0 && PUSH_ARGS)
4536 emit_single_push_insn (mode, x, type);
4537 else
4538 #endif
4540 if (CONST_INT_P (args_so_far))
4541 addr
4542 = memory_address (mode,
4543 plus_constant (Pmode, args_addr,
4544 INTVAL (args_so_far)));
4545 else
4546 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4547 args_so_far));
4548 dest = gen_rtx_MEM (mode, addr);
4550 /* We do *not* set_mem_attributes here, because incoming arguments
4551 may overlap with sibling call outgoing arguments and we cannot
4552 allow reordering of reads from function arguments with stores
4553 to outgoing arguments of sibling calls. We do, however, want
4554 to record the alignment of the stack slot. */
4555 /* ALIGN may well be better aligned than TYPE, e.g. due to
4556 PARM_BOUNDARY. Assume the caller isn't lying. */
4557 set_mem_align (dest, align);
4559 emit_move_insn (dest, x);
4563 /* Move the partial arguments into the registers and any overlapping
4564 values that we moved into the pseudos in tmp_regs. */
4565 if (partial > 0 && reg != 0)
4567 /* Handle calls that pass values in multiple non-contiguous locations.
4568 The Irix 6 ABI has examples of this. */
4569 if (GET_CODE (reg) == PARALLEL)
4570 emit_group_load (reg, x, type, -1);
4571 else
4573 gcc_assert (partial % UNITS_PER_WORD == 0);
4574 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4576 for (int i = 0; i < overlapping; i++)
4577 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4578 + nregs - overlapping + i),
4579 tmp_regs[i]);
4584 if (extra && args_addr == 0 && where_pad == stack_direction)
4585 anti_adjust_stack (GEN_INT (extra));
4587 if (alignment_pad && args_addr == 0)
4588 anti_adjust_stack (alignment_pad);
4590 return true;
4593 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4594 operations. */
4596 static rtx
4597 get_subtarget (rtx x)
4599 return (optimize
4600 || x == 0
4601 /* Only registers can be subtargets. */
4602 || !REG_P (x)
4603 /* Don't use hard regs to avoid extending their life. */
4604 || REGNO (x) < FIRST_PSEUDO_REGISTER
4605 ? 0 : x);
4608 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4609 FIELD is a bitfield. Returns true if the optimization was successful,
4610 and there's nothing else to do. */
4612 static bool
4613 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4614 unsigned HOST_WIDE_INT bitpos,
4615 unsigned HOST_WIDE_INT bitregion_start,
4616 unsigned HOST_WIDE_INT bitregion_end,
4617 machine_mode mode1, rtx str_rtx,
4618 tree to, tree src, bool reverse)
4620 machine_mode str_mode = GET_MODE (str_rtx);
4621 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4622 tree op0, op1;
4623 rtx value, result;
4624 optab binop;
4625 gimple *srcstmt;
4626 enum tree_code code;
4628 if (mode1 != VOIDmode
4629 || bitsize >= BITS_PER_WORD
4630 || str_bitsize > BITS_PER_WORD
4631 || TREE_SIDE_EFFECTS (to)
4632 || TREE_THIS_VOLATILE (to))
4633 return false;
4635 STRIP_NOPS (src);
4636 if (TREE_CODE (src) != SSA_NAME)
4637 return false;
4638 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4639 return false;
4641 srcstmt = get_gimple_for_ssa_name (src);
4642 if (!srcstmt
4643 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4644 return false;
4646 code = gimple_assign_rhs_code (srcstmt);
4648 op0 = gimple_assign_rhs1 (srcstmt);
4650 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4651 to find its initialization. Hopefully the initialization will
4652 be from a bitfield load. */
4653 if (TREE_CODE (op0) == SSA_NAME)
4655 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4657 /* We want to eventually have OP0 be the same as TO, which
4658 should be a bitfield. */
4659 if (!op0stmt
4660 || !is_gimple_assign (op0stmt)
4661 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4662 return false;
4663 op0 = gimple_assign_rhs1 (op0stmt);
4666 op1 = gimple_assign_rhs2 (srcstmt);
4668 if (!operand_equal_p (to, op0, 0))
4669 return false;
4671 if (MEM_P (str_rtx))
4673 unsigned HOST_WIDE_INT offset1;
4675 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4676 str_mode = word_mode;
4677 str_mode = get_best_mode (bitsize, bitpos,
4678 bitregion_start, bitregion_end,
4679 MEM_ALIGN (str_rtx), str_mode, 0);
4680 if (str_mode == VOIDmode)
4681 return false;
4682 str_bitsize = GET_MODE_BITSIZE (str_mode);
4684 offset1 = bitpos;
4685 bitpos %= str_bitsize;
4686 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4687 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4689 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4690 return false;
4691 else
4692 gcc_assert (!reverse);
4694 /* If the bit field covers the whole REG/MEM, store_field
4695 will likely generate better code. */
4696 if (bitsize >= str_bitsize)
4697 return false;
4699 /* We can't handle fields split across multiple entities. */
4700 if (bitpos + bitsize > str_bitsize)
4701 return false;
4703 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4704 bitpos = str_bitsize - bitpos - bitsize;
4706 switch (code)
4708 case PLUS_EXPR:
4709 case MINUS_EXPR:
4710 /* For now, just optimize the case of the topmost bitfield
4711 where we don't need to do any masking and also
4712 1 bit bitfields where xor can be used.
4713 We might win by one instruction for the other bitfields
4714 too if insv/extv instructions aren't used, so that
4715 can be added later. */
4716 if ((reverse || bitpos + bitsize != str_bitsize)
4717 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4718 break;
4720 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4721 value = convert_modes (str_mode,
4722 TYPE_MODE (TREE_TYPE (op1)), value,
4723 TYPE_UNSIGNED (TREE_TYPE (op1)));
4725 /* We may be accessing data outside the field, which means
4726 we can alias adjacent data. */
4727 if (MEM_P (str_rtx))
4729 str_rtx = shallow_copy_rtx (str_rtx);
4730 set_mem_alias_set (str_rtx, 0);
4731 set_mem_expr (str_rtx, 0);
4734 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4736 value = expand_and (str_mode, value, const1_rtx, NULL);
4737 binop = xor_optab;
4739 else
4740 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4742 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4743 if (reverse)
4744 value = flip_storage_order (str_mode, value);
4745 result = expand_binop (str_mode, binop, str_rtx,
4746 value, str_rtx, 1, OPTAB_WIDEN);
4747 if (result != str_rtx)
4748 emit_move_insn (str_rtx, result);
4749 return true;
4751 case BIT_IOR_EXPR:
4752 case BIT_XOR_EXPR:
4753 if (TREE_CODE (op1) != INTEGER_CST)
4754 break;
4755 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4756 value = convert_modes (str_mode,
4757 TYPE_MODE (TREE_TYPE (op1)), value,
4758 TYPE_UNSIGNED (TREE_TYPE (op1)));
4760 /* We may be accessing data outside the field, which means
4761 we can alias adjacent data. */
4762 if (MEM_P (str_rtx))
4764 str_rtx = shallow_copy_rtx (str_rtx);
4765 set_mem_alias_set (str_rtx, 0);
4766 set_mem_expr (str_rtx, 0);
4769 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4770 if (bitpos + bitsize != str_bitsize)
4772 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4773 str_mode);
4774 value = expand_and (str_mode, value, mask, NULL_RTX);
4776 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4777 if (reverse)
4778 value = flip_storage_order (str_mode, value);
4779 result = expand_binop (str_mode, binop, str_rtx,
4780 value, str_rtx, 1, OPTAB_WIDEN);
4781 if (result != str_rtx)
4782 emit_move_insn (str_rtx, result);
4783 return true;
4785 default:
4786 break;
4789 return false;
4792 /* In the C++ memory model, consecutive bit fields in a structure are
4793 considered one memory location.
4795 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4796 returns the bit range of consecutive bits in which this COMPONENT_REF
4797 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4798 and *OFFSET may be adjusted in the process.
4800 If the access does not need to be restricted, 0 is returned in both
4801 *BITSTART and *BITEND. */
4803 void
4804 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4805 unsigned HOST_WIDE_INT *bitend,
4806 tree exp,
4807 HOST_WIDE_INT *bitpos,
4808 tree *offset)
4810 HOST_WIDE_INT bitoffset;
4811 tree field, repr;
4813 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4815 field = TREE_OPERAND (exp, 1);
4816 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4817 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4818 need to limit the range we can access. */
4819 if (!repr)
4821 *bitstart = *bitend = 0;
4822 return;
4825 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4826 part of a larger bit field, then the representative does not serve any
4827 useful purpose. This can occur in Ada. */
4828 if (handled_component_p (TREE_OPERAND (exp, 0)))
4830 machine_mode rmode;
4831 HOST_WIDE_INT rbitsize, rbitpos;
4832 tree roffset;
4833 int unsignedp, reversep, volatilep = 0;
4834 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4835 &roffset, &rmode, &unsignedp, &reversep,
4836 &volatilep);
4837 if ((rbitpos % BITS_PER_UNIT) != 0)
4839 *bitstart = *bitend = 0;
4840 return;
4844 /* Compute the adjustment to bitpos from the offset of the field
4845 relative to the representative. DECL_FIELD_OFFSET of field and
4846 repr are the same by construction if they are not constants,
4847 see finish_bitfield_layout. */
4848 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4849 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4850 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4851 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4852 else
4853 bitoffset = 0;
4854 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4855 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4857 /* If the adjustment is larger than bitpos, we would have a negative bit
4858 position for the lower bound and this may wreak havoc later. Adjust
4859 offset and bitpos to make the lower bound non-negative in that case. */
4860 if (bitoffset > *bitpos)
4862 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4863 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4865 *bitpos += adjust;
4866 if (*offset == NULL_TREE)
4867 *offset = size_int (-adjust / BITS_PER_UNIT);
4868 else
4869 *offset
4870 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4871 *bitstart = 0;
4873 else
4874 *bitstart = *bitpos - bitoffset;
4876 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4879 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4880 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4881 DECL_RTL was not set yet, return NORTL. */
4883 static inline bool
4884 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4886 if (TREE_CODE (addr) != ADDR_EXPR)
4887 return false;
4889 tree base = TREE_OPERAND (addr, 0);
4891 if (!DECL_P (base)
4892 || TREE_ADDRESSABLE (base)
4893 || DECL_MODE (base) == BLKmode)
4894 return false;
4896 if (!DECL_RTL_SET_P (base))
4897 return nortl;
4899 return (!MEM_P (DECL_RTL (base)));
4902 /* Returns true if the MEM_REF REF refers to an object that does not
4903 reside in memory and has non-BLKmode. */
4905 static inline bool
4906 mem_ref_refers_to_non_mem_p (tree ref)
4908 tree base = TREE_OPERAND (ref, 0);
4909 return addr_expr_of_non_mem_decl_p_1 (base, false);
4912 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4913 is true, try generating a nontemporal store. */
4915 void
4916 expand_assignment (tree to, tree from, bool nontemporal)
4918 rtx to_rtx = 0;
4919 rtx result;
4920 machine_mode mode;
4921 unsigned int align;
4922 enum insn_code icode;
4924 /* Don't crash if the lhs of the assignment was erroneous. */
4925 if (TREE_CODE (to) == ERROR_MARK)
4927 expand_normal (from);
4928 return;
4931 /* Optimize away no-op moves without side-effects. */
4932 if (operand_equal_p (to, from, 0))
4933 return;
4935 /* Handle misaligned stores. */
4936 mode = TYPE_MODE (TREE_TYPE (to));
4937 if ((TREE_CODE (to) == MEM_REF
4938 || TREE_CODE (to) == TARGET_MEM_REF)
4939 && mode != BLKmode
4940 && !mem_ref_refers_to_non_mem_p (to)
4941 && ((align = get_object_alignment (to))
4942 < GET_MODE_ALIGNMENT (mode))
4943 && (((icode = optab_handler (movmisalign_optab, mode))
4944 != CODE_FOR_nothing)
4945 || SLOW_UNALIGNED_ACCESS (mode, align)))
4947 rtx reg, mem;
4949 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4950 reg = force_not_mem (reg);
4951 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4952 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4953 reg = flip_storage_order (mode, reg);
4955 if (icode != CODE_FOR_nothing)
4957 struct expand_operand ops[2];
4959 create_fixed_operand (&ops[0], mem);
4960 create_input_operand (&ops[1], reg, mode);
4961 /* The movmisalign<mode> pattern cannot fail, else the assignment
4962 would silently be omitted. */
4963 expand_insn (icode, 2, ops);
4965 else
4966 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4967 false);
4968 return;
4971 /* Assignment of a structure component needs special treatment
4972 if the structure component's rtx is not simply a MEM.
4973 Assignment of an array element at a constant index, and assignment of
4974 an array element in an unaligned packed structure field, has the same
4975 problem. Same for (partially) storing into a non-memory object. */
4976 if (handled_component_p (to)
4977 || (TREE_CODE (to) == MEM_REF
4978 && (REF_REVERSE_STORAGE_ORDER (to)
4979 || mem_ref_refers_to_non_mem_p (to)))
4980 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4982 machine_mode mode1;
4983 HOST_WIDE_INT bitsize, bitpos;
4984 unsigned HOST_WIDE_INT bitregion_start = 0;
4985 unsigned HOST_WIDE_INT bitregion_end = 0;
4986 tree offset;
4987 int unsignedp, reversep, volatilep = 0;
4988 tree tem;
4990 push_temp_slots ();
4991 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4992 &unsignedp, &reversep, &volatilep);
4994 /* Make sure bitpos is not negative, it can wreak havoc later. */
4995 if (bitpos < 0)
4997 gcc_assert (offset == NULL_TREE);
4998 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
4999 bitpos &= BITS_PER_UNIT - 1;
5002 if (TREE_CODE (to) == COMPONENT_REF
5003 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5004 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5005 /* The C++ memory model naturally applies to byte-aligned fields.
5006 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5007 BITSIZE are not byte-aligned, there is no need to limit the range
5008 we can access. This can occur with packed structures in Ada. */
5009 else if (bitsize > 0
5010 && bitsize % BITS_PER_UNIT == 0
5011 && bitpos % BITS_PER_UNIT == 0)
5013 bitregion_start = bitpos;
5014 bitregion_end = bitpos + bitsize - 1;
5017 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5019 /* If the field has a mode, we want to access it in the
5020 field's mode, not the computed mode.
5021 If a MEM has VOIDmode (external with incomplete type),
5022 use BLKmode for it instead. */
5023 if (MEM_P (to_rtx))
5025 if (mode1 != VOIDmode)
5026 to_rtx = adjust_address (to_rtx, mode1, 0);
5027 else if (GET_MODE (to_rtx) == VOIDmode)
5028 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5031 if (offset != 0)
5033 machine_mode address_mode;
5034 rtx offset_rtx;
5036 if (!MEM_P (to_rtx))
5038 /* We can get constant negative offsets into arrays with broken
5039 user code. Translate this to a trap instead of ICEing. */
5040 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5041 expand_builtin_trap ();
5042 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5045 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5046 address_mode = get_address_mode (to_rtx);
5047 if (GET_MODE (offset_rtx) != address_mode)
5049 /* We cannot be sure that the RTL in offset_rtx is valid outside
5050 of a memory address context, so force it into a register
5051 before attempting to convert it to the desired mode. */
5052 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5053 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5056 /* If we have an expression in OFFSET_RTX and a non-zero
5057 byte offset in BITPOS, adding the byte offset before the
5058 OFFSET_RTX results in better intermediate code, which makes
5059 later rtl optimization passes perform better.
5061 We prefer intermediate code like this:
5063 r124:DI=r123:DI+0x18
5064 [r124:DI]=r121:DI
5066 ... instead of ...
5068 r124:DI=r123:DI+0x10
5069 [r124:DI+0x8]=r121:DI
5071 This is only done for aligned data values, as these can
5072 be expected to result in single move instructions. */
5073 if (mode1 != VOIDmode
5074 && bitpos != 0
5075 && bitsize > 0
5076 && (bitpos % bitsize) == 0
5077 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5078 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5080 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5081 bitregion_start = 0;
5082 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5083 bitregion_end -= bitpos;
5084 bitpos = 0;
5087 to_rtx = offset_address (to_rtx, offset_rtx,
5088 highest_pow2_factor_for_target (to,
5089 offset));
5092 /* No action is needed if the target is not a memory and the field
5093 lies completely outside that target. This can occur if the source
5094 code contains an out-of-bounds access to a small array. */
5095 if (!MEM_P (to_rtx)
5096 && GET_MODE (to_rtx) != BLKmode
5097 && (unsigned HOST_WIDE_INT) bitpos
5098 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5100 expand_normal (from);
5101 result = NULL;
5103 /* Handle expand_expr of a complex value returning a CONCAT. */
5104 else if (GET_CODE (to_rtx) == CONCAT)
5106 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5107 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5108 && bitpos == 0
5109 && bitsize == mode_bitsize)
5110 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5111 else if (bitsize == mode_bitsize / 2
5112 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5113 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5114 nontemporal, reversep);
5115 else if (bitpos + bitsize <= mode_bitsize / 2)
5116 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5117 bitregion_start, bitregion_end,
5118 mode1, from, get_alias_set (to),
5119 nontemporal, reversep);
5120 else if (bitpos >= mode_bitsize / 2)
5121 result = store_field (XEXP (to_rtx, 1), bitsize,
5122 bitpos - mode_bitsize / 2,
5123 bitregion_start, bitregion_end,
5124 mode1, from, get_alias_set (to),
5125 nontemporal, reversep);
5126 else if (bitpos == 0 && bitsize == mode_bitsize)
5128 rtx from_rtx;
5129 result = expand_normal (from);
5130 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5131 TYPE_MODE (TREE_TYPE (from)), 0);
5132 emit_move_insn (XEXP (to_rtx, 0),
5133 read_complex_part (from_rtx, false));
5134 emit_move_insn (XEXP (to_rtx, 1),
5135 read_complex_part (from_rtx, true));
5137 else
5139 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5140 GET_MODE_SIZE (GET_MODE (to_rtx)));
5141 write_complex_part (temp, XEXP (to_rtx, 0), false);
5142 write_complex_part (temp, XEXP (to_rtx, 1), true);
5143 result = store_field (temp, bitsize, bitpos,
5144 bitregion_start, bitregion_end,
5145 mode1, from, get_alias_set (to),
5146 nontemporal, reversep);
5147 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5148 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5151 else
5153 if (MEM_P (to_rtx))
5155 /* If the field is at offset zero, we could have been given the
5156 DECL_RTX of the parent struct. Don't munge it. */
5157 to_rtx = shallow_copy_rtx (to_rtx);
5158 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5159 if (volatilep)
5160 MEM_VOLATILE_P (to_rtx) = 1;
5163 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5164 bitregion_start, bitregion_end,
5165 mode1, to_rtx, to, from,
5166 reversep))
5167 result = NULL;
5168 else
5169 result = store_field (to_rtx, bitsize, bitpos,
5170 bitregion_start, bitregion_end,
5171 mode1, from, get_alias_set (to),
5172 nontemporal, reversep);
5175 if (result)
5176 preserve_temp_slots (result);
5177 pop_temp_slots ();
5178 return;
5181 /* If the rhs is a function call and its value is not an aggregate,
5182 call the function before we start to compute the lhs.
5183 This is needed for correct code for cases such as
5184 val = setjmp (buf) on machines where reference to val
5185 requires loading up part of an address in a separate insn.
5187 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5188 since it might be a promoted variable where the zero- or sign- extension
5189 needs to be done. Handling this in the normal way is safe because no
5190 computation is done before the call. The same is true for SSA names. */
5191 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5192 && COMPLETE_TYPE_P (TREE_TYPE (from))
5193 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5194 && ! (((VAR_P (to)
5195 || TREE_CODE (to) == PARM_DECL
5196 || TREE_CODE (to) == RESULT_DECL)
5197 && REG_P (DECL_RTL (to)))
5198 || TREE_CODE (to) == SSA_NAME))
5200 rtx value;
5201 rtx bounds;
5203 push_temp_slots ();
5204 value = expand_normal (from);
5206 /* Split value and bounds to store them separately. */
5207 chkp_split_slot (value, &value, &bounds);
5209 if (to_rtx == 0)
5210 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5212 /* Handle calls that return values in multiple non-contiguous locations.
5213 The Irix 6 ABI has examples of this. */
5214 if (GET_CODE (to_rtx) == PARALLEL)
5216 if (GET_CODE (value) == PARALLEL)
5217 emit_group_move (to_rtx, value);
5218 else
5219 emit_group_load (to_rtx, value, TREE_TYPE (from),
5220 int_size_in_bytes (TREE_TYPE (from)));
5222 else if (GET_CODE (value) == PARALLEL)
5223 emit_group_store (to_rtx, value, TREE_TYPE (from),
5224 int_size_in_bytes (TREE_TYPE (from)));
5225 else if (GET_MODE (to_rtx) == BLKmode)
5227 /* Handle calls that return BLKmode values in registers. */
5228 if (REG_P (value))
5229 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5230 else
5231 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5233 else
5235 if (POINTER_TYPE_P (TREE_TYPE (to)))
5236 value = convert_memory_address_addr_space
5237 (GET_MODE (to_rtx), value,
5238 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5240 emit_move_insn (to_rtx, value);
5243 /* Store bounds if required. */
5244 if (bounds
5245 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5247 gcc_assert (MEM_P (to_rtx));
5248 chkp_emit_bounds_store (bounds, value, to_rtx);
5251 preserve_temp_slots (to_rtx);
5252 pop_temp_slots ();
5253 return;
5256 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5257 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5259 /* Don't move directly into a return register. */
5260 if (TREE_CODE (to) == RESULT_DECL
5261 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5263 rtx temp;
5265 push_temp_slots ();
5267 /* If the source is itself a return value, it still is in a pseudo at
5268 this point so we can move it back to the return register directly. */
5269 if (REG_P (to_rtx)
5270 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5271 && TREE_CODE (from) != CALL_EXPR)
5272 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5273 else
5274 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5276 /* Handle calls that return values in multiple non-contiguous locations.
5277 The Irix 6 ABI has examples of this. */
5278 if (GET_CODE (to_rtx) == PARALLEL)
5280 if (GET_CODE (temp) == PARALLEL)
5281 emit_group_move (to_rtx, temp);
5282 else
5283 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5284 int_size_in_bytes (TREE_TYPE (from)));
5286 else if (temp)
5287 emit_move_insn (to_rtx, temp);
5289 preserve_temp_slots (to_rtx);
5290 pop_temp_slots ();
5291 return;
5294 /* In case we are returning the contents of an object which overlaps
5295 the place the value is being stored, use a safe function when copying
5296 a value through a pointer into a structure value return block. */
5297 if (TREE_CODE (to) == RESULT_DECL
5298 && TREE_CODE (from) == INDIRECT_REF
5299 && ADDR_SPACE_GENERIC_P
5300 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5301 && refs_may_alias_p (to, from)
5302 && cfun->returns_struct
5303 && !cfun->returns_pcc_struct)
5305 rtx from_rtx, size;
5307 push_temp_slots ();
5308 size = expr_size (from);
5309 from_rtx = expand_normal (from);
5311 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5313 preserve_temp_slots (to_rtx);
5314 pop_temp_slots ();
5315 return;
5318 /* Compute FROM and store the value in the rtx we got. */
5320 push_temp_slots ();
5321 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5322 preserve_temp_slots (result);
5323 pop_temp_slots ();
5324 return;
5327 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5328 succeeded, false otherwise. */
5330 bool
5331 emit_storent_insn (rtx to, rtx from)
5333 struct expand_operand ops[2];
5334 machine_mode mode = GET_MODE (to);
5335 enum insn_code code = optab_handler (storent_optab, mode);
5337 if (code == CODE_FOR_nothing)
5338 return false;
5340 create_fixed_operand (&ops[0], to);
5341 create_input_operand (&ops[1], from, mode);
5342 return maybe_expand_insn (code, 2, ops);
5345 /* Generate code for computing expression EXP,
5346 and storing the value into TARGET.
5348 If the mode is BLKmode then we may return TARGET itself.
5349 It turns out that in BLKmode it doesn't cause a problem.
5350 because C has no operators that could combine two different
5351 assignments into the same BLKmode object with different values
5352 with no sequence point. Will other languages need this to
5353 be more thorough?
5355 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5356 stack, and block moves may need to be treated specially.
5358 If NONTEMPORAL is true, try using a nontemporal store instruction.
5360 If REVERSE is true, the store is to be done in reverse order.
5362 If BTARGET is not NULL then computed bounds of EXP are
5363 associated with BTARGET. */
5366 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5367 bool nontemporal, bool reverse, tree btarget)
5369 rtx temp;
5370 rtx alt_rtl = NULL_RTX;
5371 location_t loc = curr_insn_location ();
5373 if (VOID_TYPE_P (TREE_TYPE (exp)))
5375 /* C++ can generate ?: expressions with a throw expression in one
5376 branch and an rvalue in the other. Here, we resolve attempts to
5377 store the throw expression's nonexistent result. */
5378 gcc_assert (!call_param_p);
5379 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5380 return NULL_RTX;
5382 if (TREE_CODE (exp) == COMPOUND_EXPR)
5384 /* Perform first part of compound expression, then assign from second
5385 part. */
5386 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5387 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5388 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5389 call_param_p, nontemporal, reverse,
5390 btarget);
5392 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5394 /* For conditional expression, get safe form of the target. Then
5395 test the condition, doing the appropriate assignment on either
5396 side. This avoids the creation of unnecessary temporaries.
5397 For non-BLKmode, it is more efficient not to do this. */
5399 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5401 do_pending_stack_adjust ();
5402 NO_DEFER_POP;
5403 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5404 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5405 nontemporal, reverse, btarget);
5406 emit_jump_insn (targetm.gen_jump (lab2));
5407 emit_barrier ();
5408 emit_label (lab1);
5409 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5410 nontemporal, reverse, btarget);
5411 emit_label (lab2);
5412 OK_DEFER_POP;
5414 return NULL_RTX;
5416 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5417 /* If this is a scalar in a register that is stored in a wider mode
5418 than the declared mode, compute the result into its declared mode
5419 and then convert to the wider mode. Our value is the computed
5420 expression. */
5422 rtx inner_target = 0;
5424 /* We can do the conversion inside EXP, which will often result
5425 in some optimizations. Do the conversion in two steps: first
5426 change the signedness, if needed, then the extend. But don't
5427 do this if the type of EXP is a subtype of something else
5428 since then the conversion might involve more than just
5429 converting modes. */
5430 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5431 && TREE_TYPE (TREE_TYPE (exp)) == 0
5432 && GET_MODE_PRECISION (GET_MODE (target))
5433 == TYPE_PRECISION (TREE_TYPE (exp)))
5435 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5436 TYPE_UNSIGNED (TREE_TYPE (exp))))
5438 /* Some types, e.g. Fortran's logical*4, won't have a signed
5439 version, so use the mode instead. */
5440 tree ntype
5441 = (signed_or_unsigned_type_for
5442 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5443 if (ntype == NULL)
5444 ntype = lang_hooks.types.type_for_mode
5445 (TYPE_MODE (TREE_TYPE (exp)),
5446 SUBREG_PROMOTED_SIGN (target));
5448 exp = fold_convert_loc (loc, ntype, exp);
5451 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5452 (GET_MODE (SUBREG_REG (target)),
5453 SUBREG_PROMOTED_SIGN (target)),
5454 exp);
5456 inner_target = SUBREG_REG (target);
5459 temp = expand_expr (exp, inner_target, VOIDmode,
5460 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5462 /* Handle bounds returned by call. */
5463 if (TREE_CODE (exp) == CALL_EXPR)
5465 rtx bounds;
5466 chkp_split_slot (temp, &temp, &bounds);
5467 if (bounds && btarget)
5469 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5470 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5471 chkp_set_rtl_bounds (btarget, tmp);
5475 /* If TEMP is a VOIDmode constant, use convert_modes to make
5476 sure that we properly convert it. */
5477 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5479 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5480 temp, SUBREG_PROMOTED_SIGN (target));
5481 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5482 GET_MODE (target), temp,
5483 SUBREG_PROMOTED_SIGN (target));
5486 convert_move (SUBREG_REG (target), temp,
5487 SUBREG_PROMOTED_SIGN (target));
5489 return NULL_RTX;
5491 else if ((TREE_CODE (exp) == STRING_CST
5492 || (TREE_CODE (exp) == MEM_REF
5493 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5494 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5495 == STRING_CST
5496 && integer_zerop (TREE_OPERAND (exp, 1))))
5497 && !nontemporal && !call_param_p
5498 && MEM_P (target))
5500 /* Optimize initialization of an array with a STRING_CST. */
5501 HOST_WIDE_INT exp_len, str_copy_len;
5502 rtx dest_mem;
5503 tree str = TREE_CODE (exp) == STRING_CST
5504 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5506 exp_len = int_expr_size (exp);
5507 if (exp_len <= 0)
5508 goto normal_expr;
5510 if (TREE_STRING_LENGTH (str) <= 0)
5511 goto normal_expr;
5513 str_copy_len = strlen (TREE_STRING_POINTER (str));
5514 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5515 goto normal_expr;
5517 str_copy_len = TREE_STRING_LENGTH (str);
5518 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5519 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5521 str_copy_len += STORE_MAX_PIECES - 1;
5522 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5524 str_copy_len = MIN (str_copy_len, exp_len);
5525 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5526 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5527 MEM_ALIGN (target), false))
5528 goto normal_expr;
5530 dest_mem = target;
5532 dest_mem = store_by_pieces (dest_mem,
5533 str_copy_len, builtin_strncpy_read_str,
5534 CONST_CAST (char *,
5535 TREE_STRING_POINTER (str)),
5536 MEM_ALIGN (target), false,
5537 exp_len > str_copy_len ? 1 : 0);
5538 if (exp_len > str_copy_len)
5539 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5540 GEN_INT (exp_len - str_copy_len),
5541 BLOCK_OP_NORMAL);
5542 return NULL_RTX;
5544 else
5546 rtx tmp_target;
5548 normal_expr:
5549 /* If we want to use a nontemporal or a reverse order store, force the
5550 value into a register first. */
5551 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5552 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5553 (call_param_p
5554 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5555 &alt_rtl, false);
5557 /* Handle bounds returned by call. */
5558 if (TREE_CODE (exp) == CALL_EXPR)
5560 rtx bounds;
5561 chkp_split_slot (temp, &temp, &bounds);
5562 if (bounds && btarget)
5564 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5565 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5566 chkp_set_rtl_bounds (btarget, tmp);
5571 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5572 the same as that of TARGET, adjust the constant. This is needed, for
5573 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5574 only a word-sized value. */
5575 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5576 && TREE_CODE (exp) != ERROR_MARK
5577 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5578 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5579 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5581 /* If value was not generated in the target, store it there.
5582 Convert the value to TARGET's type first if necessary and emit the
5583 pending incrementations that have been queued when expanding EXP.
5584 Note that we cannot emit the whole queue blindly because this will
5585 effectively disable the POST_INC optimization later.
5587 If TEMP and TARGET compare equal according to rtx_equal_p, but
5588 one or both of them are volatile memory refs, we have to distinguish
5589 two cases:
5590 - expand_expr has used TARGET. In this case, we must not generate
5591 another copy. This can be detected by TARGET being equal according
5592 to == .
5593 - expand_expr has not used TARGET - that means that the source just
5594 happens to have the same RTX form. Since temp will have been created
5595 by expand_expr, it will compare unequal according to == .
5596 We must generate a copy in this case, to reach the correct number
5597 of volatile memory references. */
5599 if ((! rtx_equal_p (temp, target)
5600 || (temp != target && (side_effects_p (temp)
5601 || side_effects_p (target))))
5602 && TREE_CODE (exp) != ERROR_MARK
5603 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5604 but TARGET is not valid memory reference, TEMP will differ
5605 from TARGET although it is really the same location. */
5606 && !(alt_rtl
5607 && rtx_equal_p (alt_rtl, target)
5608 && !side_effects_p (alt_rtl)
5609 && !side_effects_p (target))
5610 /* If there's nothing to copy, don't bother. Don't call
5611 expr_size unless necessary, because some front-ends (C++)
5612 expr_size-hook must not be given objects that are not
5613 supposed to be bit-copied or bit-initialized. */
5614 && expr_size (exp) != const0_rtx)
5616 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5618 if (GET_MODE (target) == BLKmode)
5620 /* Handle calls that return BLKmode values in registers. */
5621 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5622 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5623 else
5624 store_bit_field (target,
5625 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5626 0, 0, 0, GET_MODE (temp), temp, reverse);
5628 else
5629 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5632 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5634 /* Handle copying a string constant into an array. The string
5635 constant may be shorter than the array. So copy just the string's
5636 actual length, and clear the rest. First get the size of the data
5637 type of the string, which is actually the size of the target. */
5638 rtx size = expr_size (exp);
5640 if (CONST_INT_P (size)
5641 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5642 emit_block_move (target, temp, size,
5643 (call_param_p
5644 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5645 else
5647 machine_mode pointer_mode
5648 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5649 machine_mode address_mode = get_address_mode (target);
5651 /* Compute the size of the data to copy from the string. */
5652 tree copy_size
5653 = size_binop_loc (loc, MIN_EXPR,
5654 make_tree (sizetype, size),
5655 size_int (TREE_STRING_LENGTH (exp)));
5656 rtx copy_size_rtx
5657 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5658 (call_param_p
5659 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5660 rtx_code_label *label = 0;
5662 /* Copy that much. */
5663 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5664 TYPE_UNSIGNED (sizetype));
5665 emit_block_move (target, temp, copy_size_rtx,
5666 (call_param_p
5667 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5669 /* Figure out how much is left in TARGET that we have to clear.
5670 Do all calculations in pointer_mode. */
5671 if (CONST_INT_P (copy_size_rtx))
5673 size = plus_constant (address_mode, size,
5674 -INTVAL (copy_size_rtx));
5675 target = adjust_address (target, BLKmode,
5676 INTVAL (copy_size_rtx));
5678 else
5680 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5681 copy_size_rtx, NULL_RTX, 0,
5682 OPTAB_LIB_WIDEN);
5684 if (GET_MODE (copy_size_rtx) != address_mode)
5685 copy_size_rtx = convert_to_mode (address_mode,
5686 copy_size_rtx,
5687 TYPE_UNSIGNED (sizetype));
5689 target = offset_address (target, copy_size_rtx,
5690 highest_pow2_factor (copy_size));
5691 label = gen_label_rtx ();
5692 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5693 GET_MODE (size), 0, label);
5696 if (size != const0_rtx)
5697 clear_storage (target, size, BLOCK_OP_NORMAL);
5699 if (label)
5700 emit_label (label);
5703 /* Handle calls that return values in multiple non-contiguous locations.
5704 The Irix 6 ABI has examples of this. */
5705 else if (GET_CODE (target) == PARALLEL)
5707 if (GET_CODE (temp) == PARALLEL)
5708 emit_group_move (target, temp);
5709 else
5710 emit_group_load (target, temp, TREE_TYPE (exp),
5711 int_size_in_bytes (TREE_TYPE (exp)));
5713 else if (GET_CODE (temp) == PARALLEL)
5714 emit_group_store (target, temp, TREE_TYPE (exp),
5715 int_size_in_bytes (TREE_TYPE (exp)));
5716 else if (GET_MODE (temp) == BLKmode)
5717 emit_block_move (target, temp, expr_size (exp),
5718 (call_param_p
5719 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5720 /* If we emit a nontemporal store, there is nothing else to do. */
5721 else if (nontemporal && emit_storent_insn (target, temp))
5723 else
5725 if (reverse)
5726 temp = flip_storage_order (GET_MODE (target), temp);
5727 temp = force_operand (temp, target);
5728 if (temp != target)
5729 emit_move_insn (target, temp);
5733 return NULL_RTX;
5736 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5738 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5739 bool reverse)
5741 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5742 reverse, NULL);
5745 /* Return true if field F of structure TYPE is a flexible array. */
5747 static bool
5748 flexible_array_member_p (const_tree f, const_tree type)
5750 const_tree tf;
5752 tf = TREE_TYPE (f);
5753 return (DECL_CHAIN (f) == NULL
5754 && TREE_CODE (tf) == ARRAY_TYPE
5755 && TYPE_DOMAIN (tf)
5756 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5757 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5758 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5759 && int_size_in_bytes (type) >= 0);
5762 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5763 must have in order for it to completely initialize a value of type TYPE.
5764 Return -1 if the number isn't known.
5766 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5768 static HOST_WIDE_INT
5769 count_type_elements (const_tree type, bool for_ctor_p)
5771 switch (TREE_CODE (type))
5773 case ARRAY_TYPE:
5775 tree nelts;
5777 nelts = array_type_nelts (type);
5778 if (nelts && tree_fits_uhwi_p (nelts))
5780 unsigned HOST_WIDE_INT n;
5782 n = tree_to_uhwi (nelts) + 1;
5783 if (n == 0 || for_ctor_p)
5784 return n;
5785 else
5786 return n * count_type_elements (TREE_TYPE (type), false);
5788 return for_ctor_p ? -1 : 1;
5791 case RECORD_TYPE:
5793 unsigned HOST_WIDE_INT n;
5794 tree f;
5796 n = 0;
5797 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5798 if (TREE_CODE (f) == FIELD_DECL)
5800 if (!for_ctor_p)
5801 n += count_type_elements (TREE_TYPE (f), false);
5802 else if (!flexible_array_member_p (f, type))
5803 /* Don't count flexible arrays, which are not supposed
5804 to be initialized. */
5805 n += 1;
5808 return n;
5811 case UNION_TYPE:
5812 case QUAL_UNION_TYPE:
5814 tree f;
5815 HOST_WIDE_INT n, m;
5817 gcc_assert (!for_ctor_p);
5818 /* Estimate the number of scalars in each field and pick the
5819 maximum. Other estimates would do instead; the idea is simply
5820 to make sure that the estimate is not sensitive to the ordering
5821 of the fields. */
5822 n = 1;
5823 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5824 if (TREE_CODE (f) == FIELD_DECL)
5826 m = count_type_elements (TREE_TYPE (f), false);
5827 /* If the field doesn't span the whole union, add an extra
5828 scalar for the rest. */
5829 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5830 TYPE_SIZE (type)) != 1)
5831 m++;
5832 if (n < m)
5833 n = m;
5835 return n;
5838 case COMPLEX_TYPE:
5839 return 2;
5841 case VECTOR_TYPE:
5842 return TYPE_VECTOR_SUBPARTS (type);
5844 case INTEGER_TYPE:
5845 case REAL_TYPE:
5846 case FIXED_POINT_TYPE:
5847 case ENUMERAL_TYPE:
5848 case BOOLEAN_TYPE:
5849 case POINTER_TYPE:
5850 case OFFSET_TYPE:
5851 case REFERENCE_TYPE:
5852 case NULLPTR_TYPE:
5853 return 1;
5855 case ERROR_MARK:
5856 return 0;
5858 case VOID_TYPE:
5859 case METHOD_TYPE:
5860 case FUNCTION_TYPE:
5861 case LANG_TYPE:
5862 default:
5863 gcc_unreachable ();
5867 /* Helper for categorize_ctor_elements. Identical interface. */
5869 static bool
5870 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5871 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5873 unsigned HOST_WIDE_INT idx;
5874 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5875 tree value, purpose, elt_type;
5877 /* Whether CTOR is a valid constant initializer, in accordance with what
5878 initializer_constant_valid_p does. If inferred from the constructor
5879 elements, true until proven otherwise. */
5880 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5881 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5883 nz_elts = 0;
5884 init_elts = 0;
5885 num_fields = 0;
5886 elt_type = NULL_TREE;
5888 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5890 HOST_WIDE_INT mult = 1;
5892 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5894 tree lo_index = TREE_OPERAND (purpose, 0);
5895 tree hi_index = TREE_OPERAND (purpose, 1);
5897 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5898 mult = (tree_to_uhwi (hi_index)
5899 - tree_to_uhwi (lo_index) + 1);
5901 num_fields += mult;
5902 elt_type = TREE_TYPE (value);
5904 switch (TREE_CODE (value))
5906 case CONSTRUCTOR:
5908 HOST_WIDE_INT nz = 0, ic = 0;
5910 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5911 p_complete);
5913 nz_elts += mult * nz;
5914 init_elts += mult * ic;
5916 if (const_from_elts_p && const_p)
5917 const_p = const_elt_p;
5919 break;
5921 case INTEGER_CST:
5922 case REAL_CST:
5923 case FIXED_CST:
5924 if (!initializer_zerop (value))
5925 nz_elts += mult;
5926 init_elts += mult;
5927 break;
5929 case STRING_CST:
5930 nz_elts += mult * TREE_STRING_LENGTH (value);
5931 init_elts += mult * TREE_STRING_LENGTH (value);
5932 break;
5934 case COMPLEX_CST:
5935 if (!initializer_zerop (TREE_REALPART (value)))
5936 nz_elts += mult;
5937 if (!initializer_zerop (TREE_IMAGPART (value)))
5938 nz_elts += mult;
5939 init_elts += mult;
5940 break;
5942 case VECTOR_CST:
5944 unsigned i;
5945 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5947 tree v = VECTOR_CST_ELT (value, i);
5948 if (!initializer_zerop (v))
5949 nz_elts += mult;
5950 init_elts += mult;
5953 break;
5955 default:
5957 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5958 nz_elts += mult * tc;
5959 init_elts += mult * tc;
5961 if (const_from_elts_p && const_p)
5962 const_p
5963 = initializer_constant_valid_p (value,
5964 elt_type,
5965 TYPE_REVERSE_STORAGE_ORDER
5966 (TREE_TYPE (ctor)))
5967 != NULL_TREE;
5969 break;
5973 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5974 num_fields, elt_type))
5975 *p_complete = false;
5977 *p_nz_elts += nz_elts;
5978 *p_init_elts += init_elts;
5980 return const_p;
5983 /* Examine CTOR to discover:
5984 * how many scalar fields are set to nonzero values,
5985 and place it in *P_NZ_ELTS;
5986 * how many scalar fields in total are in CTOR,
5987 and place it in *P_ELT_COUNT.
5988 * whether the constructor is complete -- in the sense that every
5989 meaningful byte is explicitly given a value --
5990 and place it in *P_COMPLETE.
5992 Return whether or not CTOR is a valid static constant initializer, the same
5993 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5995 bool
5996 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5997 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5999 *p_nz_elts = 0;
6000 *p_init_elts = 0;
6001 *p_complete = true;
6003 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6006 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6007 of which had type LAST_TYPE. Each element was itself a complete
6008 initializer, in the sense that every meaningful byte was explicitly
6009 given a value. Return true if the same is true for the constructor
6010 as a whole. */
6012 bool
6013 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6014 const_tree last_type)
6016 if (TREE_CODE (type) == UNION_TYPE
6017 || TREE_CODE (type) == QUAL_UNION_TYPE)
6019 if (num_elts == 0)
6020 return false;
6022 gcc_assert (num_elts == 1 && last_type);
6024 /* ??? We could look at each element of the union, and find the
6025 largest element. Which would avoid comparing the size of the
6026 initialized element against any tail padding in the union.
6027 Doesn't seem worth the effort... */
6028 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6031 return count_type_elements (type, true) == num_elts;
6034 /* Return 1 if EXP contains mostly (3/4) zeros. */
6036 static int
6037 mostly_zeros_p (const_tree exp)
6039 if (TREE_CODE (exp) == CONSTRUCTOR)
6041 HOST_WIDE_INT nz_elts, init_elts;
6042 bool complete_p;
6044 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6045 return !complete_p || nz_elts < init_elts / 4;
6048 return initializer_zerop (exp);
6051 /* Return 1 if EXP contains all zeros. */
6053 static int
6054 all_zeros_p (const_tree exp)
6056 if (TREE_CODE (exp) == CONSTRUCTOR)
6058 HOST_WIDE_INT nz_elts, init_elts;
6059 bool complete_p;
6061 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6062 return nz_elts == 0;
6065 return initializer_zerop (exp);
6068 /* Helper function for store_constructor.
6069 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6070 CLEARED is as for store_constructor.
6071 ALIAS_SET is the alias set to use for any stores.
6072 If REVERSE is true, the store is to be done in reverse order.
6074 This provides a recursive shortcut back to store_constructor when it isn't
6075 necessary to go through store_field. This is so that we can pass through
6076 the cleared field to let store_constructor know that we may not have to
6077 clear a substructure if the outer structure has already been cleared. */
6079 static void
6080 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6081 HOST_WIDE_INT bitpos,
6082 unsigned HOST_WIDE_INT bitregion_start,
6083 unsigned HOST_WIDE_INT bitregion_end,
6084 machine_mode mode,
6085 tree exp, int cleared,
6086 alias_set_type alias_set, bool reverse)
6088 if (TREE_CODE (exp) == CONSTRUCTOR
6089 /* We can only call store_constructor recursively if the size and
6090 bit position are on a byte boundary. */
6091 && bitpos % BITS_PER_UNIT == 0
6092 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6093 /* If we have a nonzero bitpos for a register target, then we just
6094 let store_field do the bitfield handling. This is unlikely to
6095 generate unnecessary clear instructions anyways. */
6096 && (bitpos == 0 || MEM_P (target)))
6098 if (MEM_P (target))
6099 target
6100 = adjust_address (target,
6101 GET_MODE (target) == BLKmode
6102 || 0 != (bitpos
6103 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6104 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6107 /* Update the alias set, if required. */
6108 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6109 && MEM_ALIAS_SET (target) != 0)
6111 target = copy_rtx (target);
6112 set_mem_alias_set (target, alias_set);
6115 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6116 reverse);
6118 else
6119 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6120 exp, alias_set, false, reverse);
6124 /* Returns the number of FIELD_DECLs in TYPE. */
6126 static int
6127 fields_length (const_tree type)
6129 tree t = TYPE_FIELDS (type);
6130 int count = 0;
6132 for (; t; t = DECL_CHAIN (t))
6133 if (TREE_CODE (t) == FIELD_DECL)
6134 ++count;
6136 return count;
6140 /* Store the value of constructor EXP into the rtx TARGET.
6141 TARGET is either a REG or a MEM; we know it cannot conflict, since
6142 safe_from_p has been called.
6143 CLEARED is true if TARGET is known to have been zero'd.
6144 SIZE is the number of bytes of TARGET we are allowed to modify: this
6145 may not be the same as the size of EXP if we are assigning to a field
6146 which has been packed to exclude padding bits.
6147 If REVERSE is true, the store is to be done in reverse order. */
6149 static void
6150 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6151 bool reverse)
6153 tree type = TREE_TYPE (exp);
6154 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6155 HOST_WIDE_INT bitregion_end = size > 0 ? size * BITS_PER_UNIT - 1 : 0;
6157 switch (TREE_CODE (type))
6159 case RECORD_TYPE:
6160 case UNION_TYPE:
6161 case QUAL_UNION_TYPE:
6163 unsigned HOST_WIDE_INT idx;
6164 tree field, value;
6166 /* The storage order is specified for every aggregate type. */
6167 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6169 /* If size is zero or the target is already cleared, do nothing. */
6170 if (size == 0 || cleared)
6171 cleared = 1;
6172 /* We either clear the aggregate or indicate the value is dead. */
6173 else if ((TREE_CODE (type) == UNION_TYPE
6174 || TREE_CODE (type) == QUAL_UNION_TYPE)
6175 && ! CONSTRUCTOR_ELTS (exp))
6176 /* If the constructor is empty, clear the union. */
6178 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6179 cleared = 1;
6182 /* If we are building a static constructor into a register,
6183 set the initial value as zero so we can fold the value into
6184 a constant. But if more than one register is involved,
6185 this probably loses. */
6186 else if (REG_P (target) && TREE_STATIC (exp)
6187 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6189 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6190 cleared = 1;
6193 /* If the constructor has fewer fields than the structure or
6194 if we are initializing the structure to mostly zeros, clear
6195 the whole structure first. Don't do this if TARGET is a
6196 register whose mode size isn't equal to SIZE since
6197 clear_storage can't handle this case. */
6198 else if (size > 0
6199 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6200 || mostly_zeros_p (exp))
6201 && (!REG_P (target)
6202 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6203 == size)))
6205 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6206 cleared = 1;
6209 if (REG_P (target) && !cleared)
6210 emit_clobber (target);
6212 /* Store each element of the constructor into the
6213 corresponding field of TARGET. */
6214 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6216 machine_mode mode;
6217 HOST_WIDE_INT bitsize;
6218 HOST_WIDE_INT bitpos = 0;
6219 tree offset;
6220 rtx to_rtx = target;
6222 /* Just ignore missing fields. We cleared the whole
6223 structure, above, if any fields are missing. */
6224 if (field == 0)
6225 continue;
6227 if (cleared && initializer_zerop (value))
6228 continue;
6230 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6231 bitsize = tree_to_uhwi (DECL_SIZE (field));
6232 else
6233 gcc_unreachable ();
6235 mode = DECL_MODE (field);
6236 if (DECL_BIT_FIELD (field))
6237 mode = VOIDmode;
6239 offset = DECL_FIELD_OFFSET (field);
6240 if (tree_fits_shwi_p (offset)
6241 && tree_fits_shwi_p (bit_position (field)))
6243 bitpos = int_bit_position (field);
6244 offset = NULL_TREE;
6246 else
6247 gcc_unreachable ();
6249 /* If this initializes a field that is smaller than a
6250 word, at the start of a word, try to widen it to a full
6251 word. This special case allows us to output C++ member
6252 function initializations in a form that the optimizers
6253 can understand. */
6254 if (WORD_REGISTER_OPERATIONS
6255 && REG_P (target)
6256 && bitsize < BITS_PER_WORD
6257 && bitpos % BITS_PER_WORD == 0
6258 && GET_MODE_CLASS (mode) == MODE_INT
6259 && TREE_CODE (value) == INTEGER_CST
6260 && exp_size >= 0
6261 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6263 tree type = TREE_TYPE (value);
6265 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6267 type = lang_hooks.types.type_for_mode
6268 (word_mode, TYPE_UNSIGNED (type));
6269 value = fold_convert (type, value);
6270 /* Make sure the bits beyond the original bitsize are zero
6271 so that we can correctly avoid extra zeroing stores in
6272 later constructor elements. */
6273 tree bitsize_mask
6274 = wide_int_to_tree (type, wi::mask (bitsize, false,
6275 BITS_PER_WORD));
6276 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6279 if (BYTES_BIG_ENDIAN)
6280 value
6281 = fold_build2 (LSHIFT_EXPR, type, value,
6282 build_int_cst (type,
6283 BITS_PER_WORD - bitsize));
6284 bitsize = BITS_PER_WORD;
6285 mode = word_mode;
6288 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6289 && DECL_NONADDRESSABLE_P (field))
6291 to_rtx = copy_rtx (to_rtx);
6292 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6295 store_constructor_field (to_rtx, bitsize, bitpos,
6296 0, bitregion_end, mode,
6297 value, cleared,
6298 get_alias_set (TREE_TYPE (field)),
6299 reverse);
6301 break;
6303 case ARRAY_TYPE:
6305 tree value, index;
6306 unsigned HOST_WIDE_INT i;
6307 int need_to_clear;
6308 tree domain;
6309 tree elttype = TREE_TYPE (type);
6310 int const_bounds_p;
6311 HOST_WIDE_INT minelt = 0;
6312 HOST_WIDE_INT maxelt = 0;
6314 /* The storage order is specified for every aggregate type. */
6315 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6317 domain = TYPE_DOMAIN (type);
6318 const_bounds_p = (TYPE_MIN_VALUE (domain)
6319 && TYPE_MAX_VALUE (domain)
6320 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6321 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6323 /* If we have constant bounds for the range of the type, get them. */
6324 if (const_bounds_p)
6326 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6327 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6330 /* If the constructor has fewer elements than the array, clear
6331 the whole array first. Similarly if this is static
6332 constructor of a non-BLKmode object. */
6333 if (cleared)
6334 need_to_clear = 0;
6335 else if (REG_P (target) && TREE_STATIC (exp))
6336 need_to_clear = 1;
6337 else
6339 unsigned HOST_WIDE_INT idx;
6340 tree index, value;
6341 HOST_WIDE_INT count = 0, zero_count = 0;
6342 need_to_clear = ! const_bounds_p;
6344 /* This loop is a more accurate version of the loop in
6345 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6346 is also needed to check for missing elements. */
6347 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6349 HOST_WIDE_INT this_node_count;
6351 if (need_to_clear)
6352 break;
6354 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6356 tree lo_index = TREE_OPERAND (index, 0);
6357 tree hi_index = TREE_OPERAND (index, 1);
6359 if (! tree_fits_uhwi_p (lo_index)
6360 || ! tree_fits_uhwi_p (hi_index))
6362 need_to_clear = 1;
6363 break;
6366 this_node_count = (tree_to_uhwi (hi_index)
6367 - tree_to_uhwi (lo_index) + 1);
6369 else
6370 this_node_count = 1;
6372 count += this_node_count;
6373 if (mostly_zeros_p (value))
6374 zero_count += this_node_count;
6377 /* Clear the entire array first if there are any missing
6378 elements, or if the incidence of zero elements is >=
6379 75%. */
6380 if (! need_to_clear
6381 && (count < maxelt - minelt + 1
6382 || 4 * zero_count >= 3 * count))
6383 need_to_clear = 1;
6386 if (need_to_clear && size > 0)
6388 if (REG_P (target))
6389 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6390 else
6391 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6392 cleared = 1;
6395 if (!cleared && REG_P (target))
6396 /* Inform later passes that the old value is dead. */
6397 emit_clobber (target);
6399 /* Store each element of the constructor into the
6400 corresponding element of TARGET, determined by counting the
6401 elements. */
6402 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6404 machine_mode mode;
6405 HOST_WIDE_INT bitsize;
6406 HOST_WIDE_INT bitpos;
6407 rtx xtarget = target;
6409 if (cleared && initializer_zerop (value))
6410 continue;
6412 mode = TYPE_MODE (elttype);
6413 if (mode == BLKmode)
6414 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6415 ? tree_to_uhwi (TYPE_SIZE (elttype))
6416 : -1);
6417 else
6418 bitsize = GET_MODE_BITSIZE (mode);
6420 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6422 tree lo_index = TREE_OPERAND (index, 0);
6423 tree hi_index = TREE_OPERAND (index, 1);
6424 rtx index_r, pos_rtx;
6425 HOST_WIDE_INT lo, hi, count;
6426 tree position;
6428 /* If the range is constant and "small", unroll the loop. */
6429 if (const_bounds_p
6430 && tree_fits_shwi_p (lo_index)
6431 && tree_fits_shwi_p (hi_index)
6432 && (lo = tree_to_shwi (lo_index),
6433 hi = tree_to_shwi (hi_index),
6434 count = hi - lo + 1,
6435 (!MEM_P (target)
6436 || count <= 2
6437 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6438 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6439 <= 40 * 8)))))
6441 lo -= minelt; hi -= minelt;
6442 for (; lo <= hi; lo++)
6444 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6446 if (MEM_P (target)
6447 && !MEM_KEEP_ALIAS_SET_P (target)
6448 && TREE_CODE (type) == ARRAY_TYPE
6449 && TYPE_NONALIASED_COMPONENT (type))
6451 target = copy_rtx (target);
6452 MEM_KEEP_ALIAS_SET_P (target) = 1;
6455 store_constructor_field
6456 (target, bitsize, bitpos, 0, bitregion_end,
6457 mode, value, cleared,
6458 get_alias_set (elttype), reverse);
6461 else
6463 rtx_code_label *loop_start = gen_label_rtx ();
6464 rtx_code_label *loop_end = gen_label_rtx ();
6465 tree exit_cond;
6467 expand_normal (hi_index);
6469 index = build_decl (EXPR_LOCATION (exp),
6470 VAR_DECL, NULL_TREE, domain);
6471 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6472 SET_DECL_RTL (index, index_r);
6473 store_expr (lo_index, index_r, 0, false, reverse);
6475 /* Build the head of the loop. */
6476 do_pending_stack_adjust ();
6477 emit_label (loop_start);
6479 /* Assign value to element index. */
6480 position =
6481 fold_convert (ssizetype,
6482 fold_build2 (MINUS_EXPR,
6483 TREE_TYPE (index),
6484 index,
6485 TYPE_MIN_VALUE (domain)));
6487 position =
6488 size_binop (MULT_EXPR, position,
6489 fold_convert (ssizetype,
6490 TYPE_SIZE_UNIT (elttype)));
6492 pos_rtx = expand_normal (position);
6493 xtarget = offset_address (target, pos_rtx,
6494 highest_pow2_factor (position));
6495 xtarget = adjust_address (xtarget, mode, 0);
6496 if (TREE_CODE (value) == CONSTRUCTOR)
6497 store_constructor (value, xtarget, cleared,
6498 bitsize / BITS_PER_UNIT, reverse);
6499 else
6500 store_expr (value, xtarget, 0, false, reverse);
6502 /* Generate a conditional jump to exit the loop. */
6503 exit_cond = build2 (LT_EXPR, integer_type_node,
6504 index, hi_index);
6505 jumpif (exit_cond, loop_end, -1);
6507 /* Update the loop counter, and jump to the head of
6508 the loop. */
6509 expand_assignment (index,
6510 build2 (PLUS_EXPR, TREE_TYPE (index),
6511 index, integer_one_node),
6512 false);
6514 emit_jump (loop_start);
6516 /* Build the end of the loop. */
6517 emit_label (loop_end);
6520 else if ((index != 0 && ! tree_fits_shwi_p (index))
6521 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6523 tree position;
6525 if (index == 0)
6526 index = ssize_int (1);
6528 if (minelt)
6529 index = fold_convert (ssizetype,
6530 fold_build2 (MINUS_EXPR,
6531 TREE_TYPE (index),
6532 index,
6533 TYPE_MIN_VALUE (domain)));
6535 position =
6536 size_binop (MULT_EXPR, index,
6537 fold_convert (ssizetype,
6538 TYPE_SIZE_UNIT (elttype)));
6539 xtarget = offset_address (target,
6540 expand_normal (position),
6541 highest_pow2_factor (position));
6542 xtarget = adjust_address (xtarget, mode, 0);
6543 store_expr (value, xtarget, 0, false, reverse);
6545 else
6547 if (index != 0)
6548 bitpos = ((tree_to_shwi (index) - minelt)
6549 * tree_to_uhwi (TYPE_SIZE (elttype)));
6550 else
6551 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6553 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6554 && TREE_CODE (type) == ARRAY_TYPE
6555 && TYPE_NONALIASED_COMPONENT (type))
6557 target = copy_rtx (target);
6558 MEM_KEEP_ALIAS_SET_P (target) = 1;
6560 store_constructor_field (target, bitsize, bitpos, 0,
6561 bitregion_end, mode, value,
6562 cleared, get_alias_set (elttype),
6563 reverse);
6566 break;
6569 case VECTOR_TYPE:
6571 unsigned HOST_WIDE_INT idx;
6572 constructor_elt *ce;
6573 int i;
6574 int need_to_clear;
6575 int icode = CODE_FOR_nothing;
6576 tree elttype = TREE_TYPE (type);
6577 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6578 machine_mode eltmode = TYPE_MODE (elttype);
6579 HOST_WIDE_INT bitsize;
6580 HOST_WIDE_INT bitpos;
6581 rtvec vector = NULL;
6582 unsigned n_elts;
6583 alias_set_type alias;
6585 gcc_assert (eltmode != BLKmode);
6587 n_elts = TYPE_VECTOR_SUBPARTS (type);
6588 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6590 machine_mode mode = GET_MODE (target);
6592 icode = (int) optab_handler (vec_init_optab, mode);
6593 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6594 if (icode != CODE_FOR_nothing)
6596 tree value;
6598 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6599 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6601 icode = CODE_FOR_nothing;
6602 break;
6605 if (icode != CODE_FOR_nothing)
6607 unsigned int i;
6609 vector = rtvec_alloc (n_elts);
6610 for (i = 0; i < n_elts; i++)
6611 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6615 /* If the constructor has fewer elements than the vector,
6616 clear the whole array first. Similarly if this is static
6617 constructor of a non-BLKmode object. */
6618 if (cleared)
6619 need_to_clear = 0;
6620 else if (REG_P (target) && TREE_STATIC (exp))
6621 need_to_clear = 1;
6622 else
6624 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6625 tree value;
6627 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6629 int n_elts_here = tree_to_uhwi
6630 (int_const_binop (TRUNC_DIV_EXPR,
6631 TYPE_SIZE (TREE_TYPE (value)),
6632 TYPE_SIZE (elttype)));
6634 count += n_elts_here;
6635 if (mostly_zeros_p (value))
6636 zero_count += n_elts_here;
6639 /* Clear the entire vector first if there are any missing elements,
6640 or if the incidence of zero elements is >= 75%. */
6641 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6644 if (need_to_clear && size > 0 && !vector)
6646 if (REG_P (target))
6647 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6648 else
6649 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6650 cleared = 1;
6653 /* Inform later passes that the old value is dead. */
6654 if (!cleared && !vector && REG_P (target))
6655 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6657 if (MEM_P (target))
6658 alias = MEM_ALIAS_SET (target);
6659 else
6660 alias = get_alias_set (elttype);
6662 /* Store each element of the constructor into the corresponding
6663 element of TARGET, determined by counting the elements. */
6664 for (idx = 0, i = 0;
6665 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6666 idx++, i += bitsize / elt_size)
6668 HOST_WIDE_INT eltpos;
6669 tree value = ce->value;
6671 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6672 if (cleared && initializer_zerop (value))
6673 continue;
6675 if (ce->index)
6676 eltpos = tree_to_uhwi (ce->index);
6677 else
6678 eltpos = i;
6680 if (vector)
6682 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6683 elements. */
6684 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6685 RTVEC_ELT (vector, eltpos)
6686 = expand_normal (value);
6688 else
6690 machine_mode value_mode =
6691 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6692 ? TYPE_MODE (TREE_TYPE (value))
6693 : eltmode;
6694 bitpos = eltpos * elt_size;
6695 store_constructor_field (target, bitsize, bitpos, 0,
6696 bitregion_end, value_mode,
6697 value, cleared, alias, reverse);
6701 if (vector)
6702 emit_insn (GEN_FCN (icode)
6703 (target,
6704 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6705 break;
6708 default:
6709 gcc_unreachable ();
6713 /* Store the value of EXP (an expression tree)
6714 into a subfield of TARGET which has mode MODE and occupies
6715 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6716 If MODE is VOIDmode, it means that we are storing into a bit-field.
6718 BITREGION_START is bitpos of the first bitfield in this region.
6719 BITREGION_END is the bitpos of the ending bitfield in this region.
6720 These two fields are 0, if the C++ memory model does not apply,
6721 or we are not interested in keeping track of bitfield regions.
6723 Always return const0_rtx unless we have something particular to
6724 return.
6726 ALIAS_SET is the alias set for the destination. This value will
6727 (in general) be different from that for TARGET, since TARGET is a
6728 reference to the containing structure.
6730 If NONTEMPORAL is true, try generating a nontemporal store.
6732 If REVERSE is true, the store is to be done in reverse order. */
6734 static rtx
6735 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6736 unsigned HOST_WIDE_INT bitregion_start,
6737 unsigned HOST_WIDE_INT bitregion_end,
6738 machine_mode mode, tree exp,
6739 alias_set_type alias_set, bool nontemporal, bool reverse)
6741 if (TREE_CODE (exp) == ERROR_MARK)
6742 return const0_rtx;
6744 /* If we have nothing to store, do nothing unless the expression has
6745 side-effects. */
6746 if (bitsize == 0)
6747 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6749 if (GET_CODE (target) == CONCAT)
6751 /* We're storing into a struct containing a single __complex. */
6753 gcc_assert (!bitpos);
6754 return store_expr (exp, target, 0, nontemporal, reverse);
6757 /* If the structure is in a register or if the component
6758 is a bit field, we cannot use addressing to access it.
6759 Use bit-field techniques or SUBREG to store in it. */
6761 if (mode == VOIDmode
6762 || (mode != BLKmode && ! direct_store[(int) mode]
6763 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6764 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6765 || REG_P (target)
6766 || GET_CODE (target) == SUBREG
6767 /* If the field isn't aligned enough to store as an ordinary memref,
6768 store it as a bit field. */
6769 || (mode != BLKmode
6770 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6771 || bitpos % GET_MODE_ALIGNMENT (mode))
6772 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6773 || (bitpos % BITS_PER_UNIT != 0)))
6774 || (bitsize >= 0 && mode != BLKmode
6775 && GET_MODE_BITSIZE (mode) > bitsize)
6776 /* If the RHS and field are a constant size and the size of the
6777 RHS isn't the same size as the bitfield, we must use bitfield
6778 operations. */
6779 || (bitsize >= 0
6780 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6781 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6782 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6783 we will handle specially below. */
6784 && !(TREE_CODE (exp) == CONSTRUCTOR
6785 && bitsize % BITS_PER_UNIT == 0)
6786 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6787 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6788 includes some extra padding. store_expr / expand_expr will in
6789 that case call get_inner_reference that will have the bitsize
6790 we check here and thus the block move will not clobber the
6791 padding that shouldn't be clobbered. In the future we could
6792 replace the TREE_ADDRESSABLE check with a check that
6793 get_base_address needs to live in memory. */
6794 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6795 || TREE_CODE (exp) != COMPONENT_REF
6796 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6797 || (bitsize % BITS_PER_UNIT != 0)
6798 || (bitpos % BITS_PER_UNIT != 0)
6799 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6800 != 0)))
6801 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6802 decl we must use bitfield operations. */
6803 || (bitsize >= 0
6804 && TREE_CODE (exp) == MEM_REF
6805 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6806 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6807 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6808 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6810 rtx temp;
6811 gimple *nop_def;
6813 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6814 implies a mask operation. If the precision is the same size as
6815 the field we're storing into, that mask is redundant. This is
6816 particularly common with bit field assignments generated by the
6817 C front end. */
6818 nop_def = get_def_for_expr (exp, NOP_EXPR);
6819 if (nop_def)
6821 tree type = TREE_TYPE (exp);
6822 if (INTEGRAL_TYPE_P (type)
6823 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6824 && bitsize == TYPE_PRECISION (type))
6826 tree op = gimple_assign_rhs1 (nop_def);
6827 type = TREE_TYPE (op);
6828 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6829 exp = op;
6833 temp = expand_normal (exp);
6835 /* If the value has a record type and an integral mode then, if BITSIZE
6836 is narrower than this mode and this is for big-endian data, we must
6837 first put the value into the low-order bits. Moreover, the field may
6838 be not aligned on a byte boundary; in this case, if it has reverse
6839 storage order, it needs to be accessed as a scalar field with reverse
6840 storage order and we must first put the value into target order. */
6841 if (TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
6842 && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT)
6844 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (temp));
6846 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6848 if (reverse)
6849 temp = flip_storage_order (GET_MODE (temp), temp);
6851 if (bitsize < size
6852 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6853 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6854 size - bitsize, NULL_RTX, 1);
6857 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6858 if (mode != VOIDmode && mode != BLKmode
6859 && mode != TYPE_MODE (TREE_TYPE (exp)))
6860 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6862 /* If TEMP is not a PARALLEL (see below) and its mode and that of TARGET
6863 are both BLKmode, both must be in memory and BITPOS must be aligned
6864 on a byte boundary. If so, we simply do a block copy. Likewise for
6865 a BLKmode-like TARGET. */
6866 if (GET_CODE (temp) != PARALLEL
6867 && GET_MODE (temp) == BLKmode
6868 && (GET_MODE (target) == BLKmode
6869 || (MEM_P (target)
6870 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6871 && (bitpos % BITS_PER_UNIT) == 0
6872 && (bitsize % BITS_PER_UNIT) == 0)))
6874 gcc_assert (MEM_P (target) && MEM_P (temp)
6875 && (bitpos % BITS_PER_UNIT) == 0);
6877 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6878 emit_block_move (target, temp,
6879 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6880 / BITS_PER_UNIT),
6881 BLOCK_OP_NORMAL);
6883 return const0_rtx;
6886 /* Handle calls that return values in multiple non-contiguous locations.
6887 The Irix 6 ABI has examples of this. */
6888 if (GET_CODE (temp) == PARALLEL)
6890 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6891 rtx temp_target;
6892 if (mode == BLKmode || mode == VOIDmode)
6893 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6894 temp_target = gen_reg_rtx (mode);
6895 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6896 temp = temp_target;
6898 else if (mode == BLKmode)
6900 /* Handle calls that return BLKmode values in registers. */
6901 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6903 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6904 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6905 temp = temp_target;
6907 else
6909 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6910 rtx temp_target;
6911 mode = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6912 temp_target = gen_reg_rtx (mode);
6913 temp_target
6914 = extract_bit_field (temp, size * BITS_PER_UNIT, 0, 1,
6915 temp_target, mode, mode, false);
6916 temp = temp_target;
6920 /* Store the value in the bitfield. */
6921 store_bit_field (target, bitsize, bitpos,
6922 bitregion_start, bitregion_end,
6923 mode, temp, reverse);
6925 return const0_rtx;
6927 else
6929 /* Now build a reference to just the desired component. */
6930 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6932 if (to_rtx == target)
6933 to_rtx = copy_rtx (to_rtx);
6935 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6936 set_mem_alias_set (to_rtx, alias_set);
6938 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6939 into a target smaller than its type; handle that case now. */
6940 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6942 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6943 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6944 return to_rtx;
6947 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6951 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6952 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6953 codes and find the ultimate containing object, which we return.
6955 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6956 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6957 storage order of the field.
6958 If the position of the field is variable, we store a tree
6959 giving the variable offset (in units) in *POFFSET.
6960 This offset is in addition to the bit position.
6961 If the position is not variable, we store 0 in *POFFSET.
6963 If any of the extraction expressions is volatile,
6964 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6966 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6967 Otherwise, it is a mode that can be used to access the field.
6969 If the field describes a variable-sized object, *PMODE is set to
6970 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6971 this case, but the address of the object can be found. */
6973 tree
6974 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6975 HOST_WIDE_INT *pbitpos, tree *poffset,
6976 machine_mode *pmode, int *punsignedp,
6977 int *preversep, int *pvolatilep)
6979 tree size_tree = 0;
6980 machine_mode mode = VOIDmode;
6981 bool blkmode_bitfield = false;
6982 tree offset = size_zero_node;
6983 offset_int bit_offset = 0;
6985 /* First get the mode, signedness, storage order and size. We do this from
6986 just the outermost expression. */
6987 *pbitsize = -1;
6988 if (TREE_CODE (exp) == COMPONENT_REF)
6990 tree field = TREE_OPERAND (exp, 1);
6991 size_tree = DECL_SIZE (field);
6992 if (flag_strict_volatile_bitfields > 0
6993 && TREE_THIS_VOLATILE (exp)
6994 && DECL_BIT_FIELD_TYPE (field)
6995 && DECL_MODE (field) != BLKmode)
6996 /* Volatile bitfields should be accessed in the mode of the
6997 field's type, not the mode computed based on the bit
6998 size. */
6999 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7000 else if (!DECL_BIT_FIELD (field))
7001 mode = DECL_MODE (field);
7002 else if (DECL_MODE (field) == BLKmode)
7003 blkmode_bitfield = true;
7005 *punsignedp = DECL_UNSIGNED (field);
7007 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7009 size_tree = TREE_OPERAND (exp, 1);
7010 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7011 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7013 /* For vector types, with the correct size of access, use the mode of
7014 inner type. */
7015 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7016 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7017 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7018 mode = TYPE_MODE (TREE_TYPE (exp));
7020 else
7022 mode = TYPE_MODE (TREE_TYPE (exp));
7023 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7025 if (mode == BLKmode)
7026 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7027 else
7028 *pbitsize = GET_MODE_BITSIZE (mode);
7031 if (size_tree != 0)
7033 if (! tree_fits_uhwi_p (size_tree))
7034 mode = BLKmode, *pbitsize = -1;
7035 else
7036 *pbitsize = tree_to_uhwi (size_tree);
7039 *preversep = reverse_storage_order_for_component_p (exp);
7041 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7042 and find the ultimate containing object. */
7043 while (1)
7045 switch (TREE_CODE (exp))
7047 case BIT_FIELD_REF:
7048 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7049 break;
7051 case COMPONENT_REF:
7053 tree field = TREE_OPERAND (exp, 1);
7054 tree this_offset = component_ref_field_offset (exp);
7056 /* If this field hasn't been filled in yet, don't go past it.
7057 This should only happen when folding expressions made during
7058 type construction. */
7059 if (this_offset == 0)
7060 break;
7062 offset = size_binop (PLUS_EXPR, offset, this_offset);
7063 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7065 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7067 break;
7069 case ARRAY_REF:
7070 case ARRAY_RANGE_REF:
7072 tree index = TREE_OPERAND (exp, 1);
7073 tree low_bound = array_ref_low_bound (exp);
7074 tree unit_size = array_ref_element_size (exp);
7076 /* We assume all arrays have sizes that are a multiple of a byte.
7077 First subtract the lower bound, if any, in the type of the
7078 index, then convert to sizetype and multiply by the size of
7079 the array element. */
7080 if (! integer_zerop (low_bound))
7081 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7082 index, low_bound);
7084 offset = size_binop (PLUS_EXPR, offset,
7085 size_binop (MULT_EXPR,
7086 fold_convert (sizetype, index),
7087 unit_size));
7089 break;
7091 case REALPART_EXPR:
7092 break;
7094 case IMAGPART_EXPR:
7095 bit_offset += *pbitsize;
7096 break;
7098 case VIEW_CONVERT_EXPR:
7099 break;
7101 case MEM_REF:
7102 /* Hand back the decl for MEM[&decl, off]. */
7103 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7105 tree off = TREE_OPERAND (exp, 1);
7106 if (!integer_zerop (off))
7108 offset_int boff, coff = mem_ref_offset (exp);
7109 boff = coff << LOG2_BITS_PER_UNIT;
7110 bit_offset += boff;
7112 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7114 goto done;
7116 default:
7117 goto done;
7120 /* If any reference in the chain is volatile, the effect is volatile. */
7121 if (TREE_THIS_VOLATILE (exp))
7122 *pvolatilep = 1;
7124 exp = TREE_OPERAND (exp, 0);
7126 done:
7128 /* If OFFSET is constant, see if we can return the whole thing as a
7129 constant bit position. Make sure to handle overflow during
7130 this conversion. */
7131 if (TREE_CODE (offset) == INTEGER_CST)
7133 offset_int tem = wi::sext (wi::to_offset (offset),
7134 TYPE_PRECISION (sizetype));
7135 tem <<= LOG2_BITS_PER_UNIT;
7136 tem += bit_offset;
7137 if (wi::fits_shwi_p (tem))
7139 *pbitpos = tem.to_shwi ();
7140 *poffset = offset = NULL_TREE;
7144 /* Otherwise, split it up. */
7145 if (offset)
7147 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7148 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7150 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7151 offset_int tem = bit_offset.and_not (mask);
7152 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7153 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7154 bit_offset -= tem;
7155 tem >>= LOG2_BITS_PER_UNIT;
7156 offset = size_binop (PLUS_EXPR, offset,
7157 wide_int_to_tree (sizetype, tem));
7160 *pbitpos = bit_offset.to_shwi ();
7161 *poffset = offset;
7164 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7165 if (mode == VOIDmode
7166 && blkmode_bitfield
7167 && (*pbitpos % BITS_PER_UNIT) == 0
7168 && (*pbitsize % BITS_PER_UNIT) == 0)
7169 *pmode = BLKmode;
7170 else
7171 *pmode = mode;
7173 return exp;
7176 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7178 static unsigned HOST_WIDE_INT
7179 target_align (const_tree target)
7181 /* We might have a chain of nested references with intermediate misaligning
7182 bitfields components, so need to recurse to find out. */
7184 unsigned HOST_WIDE_INT this_align, outer_align;
7186 switch (TREE_CODE (target))
7188 case BIT_FIELD_REF:
7189 return 1;
7191 case COMPONENT_REF:
7192 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7193 outer_align = target_align (TREE_OPERAND (target, 0));
7194 return MIN (this_align, outer_align);
7196 case ARRAY_REF:
7197 case ARRAY_RANGE_REF:
7198 this_align = TYPE_ALIGN (TREE_TYPE (target));
7199 outer_align = target_align (TREE_OPERAND (target, 0));
7200 return MIN (this_align, outer_align);
7202 CASE_CONVERT:
7203 case NON_LVALUE_EXPR:
7204 case VIEW_CONVERT_EXPR:
7205 this_align = TYPE_ALIGN (TREE_TYPE (target));
7206 outer_align = target_align (TREE_OPERAND (target, 0));
7207 return MAX (this_align, outer_align);
7209 default:
7210 return TYPE_ALIGN (TREE_TYPE (target));
7215 /* Given an rtx VALUE that may contain additions and multiplications, return
7216 an equivalent value that just refers to a register, memory, or constant.
7217 This is done by generating instructions to perform the arithmetic and
7218 returning a pseudo-register containing the value.
7220 The returned value may be a REG, SUBREG, MEM or constant. */
7223 force_operand (rtx value, rtx target)
7225 rtx op1, op2;
7226 /* Use subtarget as the target for operand 0 of a binary operation. */
7227 rtx subtarget = get_subtarget (target);
7228 enum rtx_code code = GET_CODE (value);
7230 /* Check for subreg applied to an expression produced by loop optimizer. */
7231 if (code == SUBREG
7232 && !REG_P (SUBREG_REG (value))
7233 && !MEM_P (SUBREG_REG (value)))
7235 value
7236 = simplify_gen_subreg (GET_MODE (value),
7237 force_reg (GET_MODE (SUBREG_REG (value)),
7238 force_operand (SUBREG_REG (value),
7239 NULL_RTX)),
7240 GET_MODE (SUBREG_REG (value)),
7241 SUBREG_BYTE (value));
7242 code = GET_CODE (value);
7245 /* Check for a PIC address load. */
7246 if ((code == PLUS || code == MINUS)
7247 && XEXP (value, 0) == pic_offset_table_rtx
7248 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7249 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7250 || GET_CODE (XEXP (value, 1)) == CONST))
7252 if (!subtarget)
7253 subtarget = gen_reg_rtx (GET_MODE (value));
7254 emit_move_insn (subtarget, value);
7255 return subtarget;
7258 if (ARITHMETIC_P (value))
7260 op2 = XEXP (value, 1);
7261 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7262 subtarget = 0;
7263 if (code == MINUS && CONST_INT_P (op2))
7265 code = PLUS;
7266 op2 = negate_rtx (GET_MODE (value), op2);
7269 /* Check for an addition with OP2 a constant integer and our first
7270 operand a PLUS of a virtual register and something else. In that
7271 case, we want to emit the sum of the virtual register and the
7272 constant first and then add the other value. This allows virtual
7273 register instantiation to simply modify the constant rather than
7274 creating another one around this addition. */
7275 if (code == PLUS && CONST_INT_P (op2)
7276 && GET_CODE (XEXP (value, 0)) == PLUS
7277 && REG_P (XEXP (XEXP (value, 0), 0))
7278 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7279 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7281 rtx temp = expand_simple_binop (GET_MODE (value), code,
7282 XEXP (XEXP (value, 0), 0), op2,
7283 subtarget, 0, OPTAB_LIB_WIDEN);
7284 return expand_simple_binop (GET_MODE (value), code, temp,
7285 force_operand (XEXP (XEXP (value,
7286 0), 1), 0),
7287 target, 0, OPTAB_LIB_WIDEN);
7290 op1 = force_operand (XEXP (value, 0), subtarget);
7291 op2 = force_operand (op2, NULL_RTX);
7292 switch (code)
7294 case MULT:
7295 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7296 case DIV:
7297 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7298 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7299 target, 1, OPTAB_LIB_WIDEN);
7300 else
7301 return expand_divmod (0,
7302 FLOAT_MODE_P (GET_MODE (value))
7303 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7304 GET_MODE (value), op1, op2, target, 0);
7305 case MOD:
7306 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7307 target, 0);
7308 case UDIV:
7309 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7310 target, 1);
7311 case UMOD:
7312 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7313 target, 1);
7314 case ASHIFTRT:
7315 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7316 target, 0, OPTAB_LIB_WIDEN);
7317 default:
7318 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7319 target, 1, OPTAB_LIB_WIDEN);
7322 if (UNARY_P (value))
7324 if (!target)
7325 target = gen_reg_rtx (GET_MODE (value));
7326 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7327 switch (code)
7329 case ZERO_EXTEND:
7330 case SIGN_EXTEND:
7331 case TRUNCATE:
7332 case FLOAT_EXTEND:
7333 case FLOAT_TRUNCATE:
7334 convert_move (target, op1, code == ZERO_EXTEND);
7335 return target;
7337 case FIX:
7338 case UNSIGNED_FIX:
7339 expand_fix (target, op1, code == UNSIGNED_FIX);
7340 return target;
7342 case FLOAT:
7343 case UNSIGNED_FLOAT:
7344 expand_float (target, op1, code == UNSIGNED_FLOAT);
7345 return target;
7347 default:
7348 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7352 #ifdef INSN_SCHEDULING
7353 /* On machines that have insn scheduling, we want all memory reference to be
7354 explicit, so we need to deal with such paradoxical SUBREGs. */
7355 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7356 value
7357 = simplify_gen_subreg (GET_MODE (value),
7358 force_reg (GET_MODE (SUBREG_REG (value)),
7359 force_operand (SUBREG_REG (value),
7360 NULL_RTX)),
7361 GET_MODE (SUBREG_REG (value)),
7362 SUBREG_BYTE (value));
7363 #endif
7365 return value;
7368 /* Subroutine of expand_expr: return nonzero iff there is no way that
7369 EXP can reference X, which is being modified. TOP_P is nonzero if this
7370 call is going to be used to determine whether we need a temporary
7371 for EXP, as opposed to a recursive call to this function.
7373 It is always safe for this routine to return zero since it merely
7374 searches for optimization opportunities. */
7377 safe_from_p (const_rtx x, tree exp, int top_p)
7379 rtx exp_rtl = 0;
7380 int i, nops;
7382 if (x == 0
7383 /* If EXP has varying size, we MUST use a target since we currently
7384 have no way of allocating temporaries of variable size
7385 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7386 So we assume here that something at a higher level has prevented a
7387 clash. This is somewhat bogus, but the best we can do. Only
7388 do this when X is BLKmode and when we are at the top level. */
7389 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7390 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7391 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7392 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7393 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7394 != INTEGER_CST)
7395 && GET_MODE (x) == BLKmode)
7396 /* If X is in the outgoing argument area, it is always safe. */
7397 || (MEM_P (x)
7398 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7399 || (GET_CODE (XEXP (x, 0)) == PLUS
7400 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7401 return 1;
7403 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7404 find the underlying pseudo. */
7405 if (GET_CODE (x) == SUBREG)
7407 x = SUBREG_REG (x);
7408 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7409 return 0;
7412 /* Now look at our tree code and possibly recurse. */
7413 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7415 case tcc_declaration:
7416 exp_rtl = DECL_RTL_IF_SET (exp);
7417 break;
7419 case tcc_constant:
7420 return 1;
7422 case tcc_exceptional:
7423 if (TREE_CODE (exp) == TREE_LIST)
7425 while (1)
7427 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7428 return 0;
7429 exp = TREE_CHAIN (exp);
7430 if (!exp)
7431 return 1;
7432 if (TREE_CODE (exp) != TREE_LIST)
7433 return safe_from_p (x, exp, 0);
7436 else if (TREE_CODE (exp) == CONSTRUCTOR)
7438 constructor_elt *ce;
7439 unsigned HOST_WIDE_INT idx;
7441 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7442 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7443 || !safe_from_p (x, ce->value, 0))
7444 return 0;
7445 return 1;
7447 else if (TREE_CODE (exp) == ERROR_MARK)
7448 return 1; /* An already-visited SAVE_EXPR? */
7449 else
7450 return 0;
7452 case tcc_statement:
7453 /* The only case we look at here is the DECL_INITIAL inside a
7454 DECL_EXPR. */
7455 return (TREE_CODE (exp) != DECL_EXPR
7456 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7457 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7458 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7460 case tcc_binary:
7461 case tcc_comparison:
7462 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7463 return 0;
7464 /* Fall through. */
7466 case tcc_unary:
7467 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7469 case tcc_expression:
7470 case tcc_reference:
7471 case tcc_vl_exp:
7472 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7473 the expression. If it is set, we conflict iff we are that rtx or
7474 both are in memory. Otherwise, we check all operands of the
7475 expression recursively. */
7477 switch (TREE_CODE (exp))
7479 case ADDR_EXPR:
7480 /* If the operand is static or we are static, we can't conflict.
7481 Likewise if we don't conflict with the operand at all. */
7482 if (staticp (TREE_OPERAND (exp, 0))
7483 || TREE_STATIC (exp)
7484 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7485 return 1;
7487 /* Otherwise, the only way this can conflict is if we are taking
7488 the address of a DECL a that address if part of X, which is
7489 very rare. */
7490 exp = TREE_OPERAND (exp, 0);
7491 if (DECL_P (exp))
7493 if (!DECL_RTL_SET_P (exp)
7494 || !MEM_P (DECL_RTL (exp)))
7495 return 0;
7496 else
7497 exp_rtl = XEXP (DECL_RTL (exp), 0);
7499 break;
7501 case MEM_REF:
7502 if (MEM_P (x)
7503 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7504 get_alias_set (exp)))
7505 return 0;
7506 break;
7508 case CALL_EXPR:
7509 /* Assume that the call will clobber all hard registers and
7510 all of memory. */
7511 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7512 || MEM_P (x))
7513 return 0;
7514 break;
7516 case WITH_CLEANUP_EXPR:
7517 case CLEANUP_POINT_EXPR:
7518 /* Lowered by gimplify.c. */
7519 gcc_unreachable ();
7521 case SAVE_EXPR:
7522 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7524 default:
7525 break;
7528 /* If we have an rtx, we do not need to scan our operands. */
7529 if (exp_rtl)
7530 break;
7532 nops = TREE_OPERAND_LENGTH (exp);
7533 for (i = 0; i < nops; i++)
7534 if (TREE_OPERAND (exp, i) != 0
7535 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7536 return 0;
7538 break;
7540 case tcc_type:
7541 /* Should never get a type here. */
7542 gcc_unreachable ();
7545 /* If we have an rtl, find any enclosed object. Then see if we conflict
7546 with it. */
7547 if (exp_rtl)
7549 if (GET_CODE (exp_rtl) == SUBREG)
7551 exp_rtl = SUBREG_REG (exp_rtl);
7552 if (REG_P (exp_rtl)
7553 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7554 return 0;
7557 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7558 are memory and they conflict. */
7559 return ! (rtx_equal_p (x, exp_rtl)
7560 || (MEM_P (x) && MEM_P (exp_rtl)
7561 && true_dependence (exp_rtl, VOIDmode, x)));
7564 /* If we reach here, it is safe. */
7565 return 1;
7569 /* Return the highest power of two that EXP is known to be a multiple of.
7570 This is used in updating alignment of MEMs in array references. */
7572 unsigned HOST_WIDE_INT
7573 highest_pow2_factor (const_tree exp)
7575 unsigned HOST_WIDE_INT ret;
7576 int trailing_zeros = tree_ctz (exp);
7577 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7578 return BIGGEST_ALIGNMENT;
7579 ret = HOST_WIDE_INT_1U << trailing_zeros;
7580 if (ret > BIGGEST_ALIGNMENT)
7581 return BIGGEST_ALIGNMENT;
7582 return ret;
7585 /* Similar, except that the alignment requirements of TARGET are
7586 taken into account. Assume it is at least as aligned as its
7587 type, unless it is a COMPONENT_REF in which case the layout of
7588 the structure gives the alignment. */
7590 static unsigned HOST_WIDE_INT
7591 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7593 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7594 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7596 return MAX (factor, talign);
7599 /* Convert the tree comparison code TCODE to the rtl one where the
7600 signedness is UNSIGNEDP. */
7602 static enum rtx_code
7603 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7605 enum rtx_code code;
7606 switch (tcode)
7608 case EQ_EXPR:
7609 code = EQ;
7610 break;
7611 case NE_EXPR:
7612 code = NE;
7613 break;
7614 case LT_EXPR:
7615 code = unsignedp ? LTU : LT;
7616 break;
7617 case LE_EXPR:
7618 code = unsignedp ? LEU : LE;
7619 break;
7620 case GT_EXPR:
7621 code = unsignedp ? GTU : GT;
7622 break;
7623 case GE_EXPR:
7624 code = unsignedp ? GEU : GE;
7625 break;
7626 case UNORDERED_EXPR:
7627 code = UNORDERED;
7628 break;
7629 case ORDERED_EXPR:
7630 code = ORDERED;
7631 break;
7632 case UNLT_EXPR:
7633 code = UNLT;
7634 break;
7635 case UNLE_EXPR:
7636 code = UNLE;
7637 break;
7638 case UNGT_EXPR:
7639 code = UNGT;
7640 break;
7641 case UNGE_EXPR:
7642 code = UNGE;
7643 break;
7644 case UNEQ_EXPR:
7645 code = UNEQ;
7646 break;
7647 case LTGT_EXPR:
7648 code = LTGT;
7649 break;
7651 default:
7652 gcc_unreachable ();
7654 return code;
7657 /* Subroutine of expand_expr. Expand the two operands of a binary
7658 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7659 The value may be stored in TARGET if TARGET is nonzero. The
7660 MODIFIER argument is as documented by expand_expr. */
7662 void
7663 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7664 enum expand_modifier modifier)
7666 if (! safe_from_p (target, exp1, 1))
7667 target = 0;
7668 if (operand_equal_p (exp0, exp1, 0))
7670 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7671 *op1 = copy_rtx (*op0);
7673 else
7675 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7676 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7681 /* Return a MEM that contains constant EXP. DEFER is as for
7682 output_constant_def and MODIFIER is as for expand_expr. */
7684 static rtx
7685 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7687 rtx mem;
7689 mem = output_constant_def (exp, defer);
7690 if (modifier != EXPAND_INITIALIZER)
7691 mem = use_anchored_address (mem);
7692 return mem;
7695 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7696 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7698 static rtx
7699 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7700 enum expand_modifier modifier, addr_space_t as)
7702 rtx result, subtarget;
7703 tree inner, offset;
7704 HOST_WIDE_INT bitsize, bitpos;
7705 int unsignedp, reversep, volatilep = 0;
7706 machine_mode mode1;
7708 /* If we are taking the address of a constant and are at the top level,
7709 we have to use output_constant_def since we can't call force_const_mem
7710 at top level. */
7711 /* ??? This should be considered a front-end bug. We should not be
7712 generating ADDR_EXPR of something that isn't an LVALUE. The only
7713 exception here is STRING_CST. */
7714 if (CONSTANT_CLASS_P (exp))
7716 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7717 if (modifier < EXPAND_SUM)
7718 result = force_operand (result, target);
7719 return result;
7722 /* Everything must be something allowed by is_gimple_addressable. */
7723 switch (TREE_CODE (exp))
7725 case INDIRECT_REF:
7726 /* This case will happen via recursion for &a->b. */
7727 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7729 case MEM_REF:
7731 tree tem = TREE_OPERAND (exp, 0);
7732 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7733 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7734 return expand_expr (tem, target, tmode, modifier);
7737 case CONST_DECL:
7738 /* Expand the initializer like constants above. */
7739 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7740 0, modifier), 0);
7741 if (modifier < EXPAND_SUM)
7742 result = force_operand (result, target);
7743 return result;
7745 case REALPART_EXPR:
7746 /* The real part of the complex number is always first, therefore
7747 the address is the same as the address of the parent object. */
7748 offset = 0;
7749 bitpos = 0;
7750 inner = TREE_OPERAND (exp, 0);
7751 break;
7753 case IMAGPART_EXPR:
7754 /* The imaginary part of the complex number is always second.
7755 The expression is therefore always offset by the size of the
7756 scalar type. */
7757 offset = 0;
7758 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7759 inner = TREE_OPERAND (exp, 0);
7760 break;
7762 case COMPOUND_LITERAL_EXPR:
7763 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7764 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7765 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7766 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7767 the initializers aren't gimplified. */
7768 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7769 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7770 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7771 target, tmode, modifier, as);
7772 /* FALLTHRU */
7773 default:
7774 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7775 expand_expr, as that can have various side effects; LABEL_DECLs for
7776 example, may not have their DECL_RTL set yet. Expand the rtl of
7777 CONSTRUCTORs too, which should yield a memory reference for the
7778 constructor's contents. Assume language specific tree nodes can
7779 be expanded in some interesting way. */
7780 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7781 if (DECL_P (exp)
7782 || TREE_CODE (exp) == CONSTRUCTOR
7783 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7785 result = expand_expr (exp, target, tmode,
7786 modifier == EXPAND_INITIALIZER
7787 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7789 /* If the DECL isn't in memory, then the DECL wasn't properly
7790 marked TREE_ADDRESSABLE, which will be either a front-end
7791 or a tree optimizer bug. */
7793 gcc_assert (MEM_P (result));
7794 result = XEXP (result, 0);
7796 /* ??? Is this needed anymore? */
7797 if (DECL_P (exp))
7798 TREE_USED (exp) = 1;
7800 if (modifier != EXPAND_INITIALIZER
7801 && modifier != EXPAND_CONST_ADDRESS
7802 && modifier != EXPAND_SUM)
7803 result = force_operand (result, target);
7804 return result;
7807 /* Pass FALSE as the last argument to get_inner_reference although
7808 we are expanding to RTL. The rationale is that we know how to
7809 handle "aligning nodes" here: we can just bypass them because
7810 they won't change the final object whose address will be returned
7811 (they actually exist only for that purpose). */
7812 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7813 &unsignedp, &reversep, &volatilep);
7814 break;
7817 /* We must have made progress. */
7818 gcc_assert (inner != exp);
7820 subtarget = offset || bitpos ? NULL_RTX : target;
7821 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7822 inner alignment, force the inner to be sufficiently aligned. */
7823 if (CONSTANT_CLASS_P (inner)
7824 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7826 inner = copy_node (inner);
7827 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7828 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7829 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7831 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7833 if (offset)
7835 rtx tmp;
7837 if (modifier != EXPAND_NORMAL)
7838 result = force_operand (result, NULL);
7839 tmp = expand_expr (offset, NULL_RTX, tmode,
7840 modifier == EXPAND_INITIALIZER
7841 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7843 /* expand_expr is allowed to return an object in a mode other
7844 than TMODE. If it did, we need to convert. */
7845 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7846 tmp = convert_modes (tmode, GET_MODE (tmp),
7847 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7848 result = convert_memory_address_addr_space (tmode, result, as);
7849 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7851 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7852 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7853 else
7855 subtarget = bitpos ? NULL_RTX : target;
7856 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7857 1, OPTAB_LIB_WIDEN);
7861 if (bitpos)
7863 /* Someone beforehand should have rejected taking the address
7864 of such an object. */
7865 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7867 result = convert_memory_address_addr_space (tmode, result, as);
7868 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7869 if (modifier < EXPAND_SUM)
7870 result = force_operand (result, target);
7873 return result;
7876 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7877 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7879 static rtx
7880 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7881 enum expand_modifier modifier)
7883 addr_space_t as = ADDR_SPACE_GENERIC;
7884 machine_mode address_mode = Pmode;
7885 machine_mode pointer_mode = ptr_mode;
7886 machine_mode rmode;
7887 rtx result;
7889 /* Target mode of VOIDmode says "whatever's natural". */
7890 if (tmode == VOIDmode)
7891 tmode = TYPE_MODE (TREE_TYPE (exp));
7893 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7895 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7896 address_mode = targetm.addr_space.address_mode (as);
7897 pointer_mode = targetm.addr_space.pointer_mode (as);
7900 /* We can get called with some Weird Things if the user does silliness
7901 like "(short) &a". In that case, convert_memory_address won't do
7902 the right thing, so ignore the given target mode. */
7903 if (tmode != address_mode && tmode != pointer_mode)
7904 tmode = address_mode;
7906 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7907 tmode, modifier, as);
7909 /* Despite expand_expr claims concerning ignoring TMODE when not
7910 strictly convenient, stuff breaks if we don't honor it. Note
7911 that combined with the above, we only do this for pointer modes. */
7912 rmode = GET_MODE (result);
7913 if (rmode == VOIDmode)
7914 rmode = tmode;
7915 if (rmode != tmode)
7916 result = convert_memory_address_addr_space (tmode, result, as);
7918 return result;
7921 /* Generate code for computing CONSTRUCTOR EXP.
7922 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7923 is TRUE, instead of creating a temporary variable in memory
7924 NULL is returned and the caller needs to handle it differently. */
7926 static rtx
7927 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7928 bool avoid_temp_mem)
7930 tree type = TREE_TYPE (exp);
7931 machine_mode mode = TYPE_MODE (type);
7933 /* Try to avoid creating a temporary at all. This is possible
7934 if all of the initializer is zero.
7935 FIXME: try to handle all [0..255] initializers we can handle
7936 with memset. */
7937 if (TREE_STATIC (exp)
7938 && !TREE_ADDRESSABLE (exp)
7939 && target != 0 && mode == BLKmode
7940 && all_zeros_p (exp))
7942 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7943 return target;
7946 /* All elts simple constants => refer to a constant in memory. But
7947 if this is a non-BLKmode mode, let it store a field at a time
7948 since that should make a CONST_INT, CONST_WIDE_INT or
7949 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7950 use, it is best to store directly into the target unless the type
7951 is large enough that memcpy will be used. If we are making an
7952 initializer and all operands are constant, put it in memory as
7953 well.
7955 FIXME: Avoid trying to fill vector constructors piece-meal.
7956 Output them with output_constant_def below unless we're sure
7957 they're zeros. This should go away when vector initializers
7958 are treated like VECTOR_CST instead of arrays. */
7959 if ((TREE_STATIC (exp)
7960 && ((mode == BLKmode
7961 && ! (target != 0 && safe_from_p (target, exp, 1)))
7962 || TREE_ADDRESSABLE (exp)
7963 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7964 && (! can_move_by_pieces
7965 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7966 TYPE_ALIGN (type)))
7967 && ! mostly_zeros_p (exp))))
7968 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7969 && TREE_CONSTANT (exp)))
7971 rtx constructor;
7973 if (avoid_temp_mem)
7974 return NULL_RTX;
7976 constructor = expand_expr_constant (exp, 1, modifier);
7978 if (modifier != EXPAND_CONST_ADDRESS
7979 && modifier != EXPAND_INITIALIZER
7980 && modifier != EXPAND_SUM)
7981 constructor = validize_mem (constructor);
7983 return constructor;
7986 /* Handle calls that pass values in multiple non-contiguous
7987 locations. The Irix 6 ABI has examples of this. */
7988 if (target == 0 || ! safe_from_p (target, exp, 1)
7989 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7991 if (avoid_temp_mem)
7992 return NULL_RTX;
7994 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7997 store_constructor (exp, target, 0, int_expr_size (exp), false);
7998 return target;
8002 /* expand_expr: generate code for computing expression EXP.
8003 An rtx for the computed value is returned. The value is never null.
8004 In the case of a void EXP, const0_rtx is returned.
8006 The value may be stored in TARGET if TARGET is nonzero.
8007 TARGET is just a suggestion; callers must assume that
8008 the rtx returned may not be the same as TARGET.
8010 If TARGET is CONST0_RTX, it means that the value will be ignored.
8012 If TMODE is not VOIDmode, it suggests generating the
8013 result in mode TMODE. But this is done only when convenient.
8014 Otherwise, TMODE is ignored and the value generated in its natural mode.
8015 TMODE is just a suggestion; callers must assume that
8016 the rtx returned may not have mode TMODE.
8018 Note that TARGET may have neither TMODE nor MODE. In that case, it
8019 probably will not be used.
8021 If MODIFIER is EXPAND_SUM then when EXP is an addition
8022 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8023 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8024 products as above, or REG or MEM, or constant.
8025 Ordinarily in such cases we would output mul or add instructions
8026 and then return a pseudo reg containing the sum.
8028 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8029 it also marks a label as absolutely required (it can't be dead).
8030 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8031 This is used for outputting expressions used in initializers.
8033 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8034 with a constant address even if that address is not normally legitimate.
8035 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8037 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8038 a call parameter. Such targets require special care as we haven't yet
8039 marked TARGET so that it's safe from being trashed by libcalls. We
8040 don't want to use TARGET for anything but the final result;
8041 Intermediate values must go elsewhere. Additionally, calls to
8042 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8044 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8045 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8046 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8047 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8048 recursively.
8050 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8051 In this case, we don't adjust a returned MEM rtx that wouldn't be
8052 sufficiently aligned for its mode; instead, it's up to the caller
8053 to deal with it afterwards. This is used to make sure that unaligned
8054 base objects for which out-of-bounds accesses are supported, for
8055 example record types with trailing arrays, aren't realigned behind
8056 the back of the caller.
8057 The normal operating mode is to pass FALSE for this parameter. */
8060 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8061 enum expand_modifier modifier, rtx *alt_rtl,
8062 bool inner_reference_p)
8064 rtx ret;
8066 /* Handle ERROR_MARK before anybody tries to access its type. */
8067 if (TREE_CODE (exp) == ERROR_MARK
8068 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8070 ret = CONST0_RTX (tmode);
8071 return ret ? ret : const0_rtx;
8074 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8075 inner_reference_p);
8076 return ret;
8079 /* Try to expand the conditional expression which is represented by
8080 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8081 return the rtl reg which represents the result. Otherwise return
8082 NULL_RTX. */
8084 static rtx
8085 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8086 tree treeop1 ATTRIBUTE_UNUSED,
8087 tree treeop2 ATTRIBUTE_UNUSED)
8089 rtx insn;
8090 rtx op00, op01, op1, op2;
8091 enum rtx_code comparison_code;
8092 machine_mode comparison_mode;
8093 gimple *srcstmt;
8094 rtx temp;
8095 tree type = TREE_TYPE (treeop1);
8096 int unsignedp = TYPE_UNSIGNED (type);
8097 machine_mode mode = TYPE_MODE (type);
8098 machine_mode orig_mode = mode;
8099 static bool expanding_cond_expr_using_cmove = false;
8101 /* Conditional move expansion can end up TERing two operands which,
8102 when recursively hitting conditional expressions can result in
8103 exponential behavior if the cmove expansion ultimatively fails.
8104 It's hardly profitable to TER a cmove into a cmove so avoid doing
8105 that by failing early if we end up recursing. */
8106 if (expanding_cond_expr_using_cmove)
8107 return NULL_RTX;
8109 /* If we cannot do a conditional move on the mode, try doing it
8110 with the promoted mode. */
8111 if (!can_conditionally_move_p (mode))
8113 mode = promote_mode (type, mode, &unsignedp);
8114 if (!can_conditionally_move_p (mode))
8115 return NULL_RTX;
8116 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8118 else
8119 temp = assign_temp (type, 0, 1);
8121 expanding_cond_expr_using_cmove = true;
8122 start_sequence ();
8123 expand_operands (treeop1, treeop2,
8124 temp, &op1, &op2, EXPAND_NORMAL);
8126 if (TREE_CODE (treeop0) == SSA_NAME
8127 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8129 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8130 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8131 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8132 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8133 comparison_mode = TYPE_MODE (type);
8134 unsignedp = TYPE_UNSIGNED (type);
8135 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8137 else if (COMPARISON_CLASS_P (treeop0))
8139 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8140 enum tree_code cmpcode = TREE_CODE (treeop0);
8141 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8142 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8143 unsignedp = TYPE_UNSIGNED (type);
8144 comparison_mode = TYPE_MODE (type);
8145 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8147 else
8149 op00 = expand_normal (treeop0);
8150 op01 = const0_rtx;
8151 comparison_code = NE;
8152 comparison_mode = GET_MODE (op00);
8153 if (comparison_mode == VOIDmode)
8154 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8156 expanding_cond_expr_using_cmove = false;
8158 if (GET_MODE (op1) != mode)
8159 op1 = gen_lowpart (mode, op1);
8161 if (GET_MODE (op2) != mode)
8162 op2 = gen_lowpart (mode, op2);
8164 /* Try to emit the conditional move. */
8165 insn = emit_conditional_move (temp, comparison_code,
8166 op00, op01, comparison_mode,
8167 op1, op2, mode,
8168 unsignedp);
8170 /* If we could do the conditional move, emit the sequence,
8171 and return. */
8172 if (insn)
8174 rtx_insn *seq = get_insns ();
8175 end_sequence ();
8176 emit_insn (seq);
8177 return convert_modes (orig_mode, mode, temp, 0);
8180 /* Otherwise discard the sequence and fall back to code with
8181 branches. */
8182 end_sequence ();
8183 return NULL_RTX;
8187 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8188 enum expand_modifier modifier)
8190 rtx op0, op1, op2, temp;
8191 rtx_code_label *lab;
8192 tree type;
8193 int unsignedp;
8194 machine_mode mode;
8195 enum tree_code code = ops->code;
8196 optab this_optab;
8197 rtx subtarget, original_target;
8198 int ignore;
8199 bool reduce_bit_field;
8200 location_t loc = ops->location;
8201 tree treeop0, treeop1, treeop2;
8202 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8203 ? reduce_to_bit_field_precision ((expr), \
8204 target, \
8205 type) \
8206 : (expr))
8208 type = ops->type;
8209 mode = TYPE_MODE (type);
8210 unsignedp = TYPE_UNSIGNED (type);
8212 treeop0 = ops->op0;
8213 treeop1 = ops->op1;
8214 treeop2 = ops->op2;
8216 /* We should be called only on simple (binary or unary) expressions,
8217 exactly those that are valid in gimple expressions that aren't
8218 GIMPLE_SINGLE_RHS (or invalid). */
8219 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8220 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8221 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8223 ignore = (target == const0_rtx
8224 || ((CONVERT_EXPR_CODE_P (code)
8225 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8226 && TREE_CODE (type) == VOID_TYPE));
8228 /* We should be called only if we need the result. */
8229 gcc_assert (!ignore);
8231 /* An operation in what may be a bit-field type needs the
8232 result to be reduced to the precision of the bit-field type,
8233 which is narrower than that of the type's mode. */
8234 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8235 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8237 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8238 target = 0;
8240 /* Use subtarget as the target for operand 0 of a binary operation. */
8241 subtarget = get_subtarget (target);
8242 original_target = target;
8244 switch (code)
8246 case NON_LVALUE_EXPR:
8247 case PAREN_EXPR:
8248 CASE_CONVERT:
8249 if (treeop0 == error_mark_node)
8250 return const0_rtx;
8252 if (TREE_CODE (type) == UNION_TYPE)
8254 tree valtype = TREE_TYPE (treeop0);
8256 /* If both input and output are BLKmode, this conversion isn't doing
8257 anything except possibly changing memory attribute. */
8258 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8260 rtx result = expand_expr (treeop0, target, tmode,
8261 modifier);
8263 result = copy_rtx (result);
8264 set_mem_attributes (result, type, 0);
8265 return result;
8268 if (target == 0)
8270 if (TYPE_MODE (type) != BLKmode)
8271 target = gen_reg_rtx (TYPE_MODE (type));
8272 else
8273 target = assign_temp (type, 1, 1);
8276 if (MEM_P (target))
8277 /* Store data into beginning of memory target. */
8278 store_expr (treeop0,
8279 adjust_address (target, TYPE_MODE (valtype), 0),
8280 modifier == EXPAND_STACK_PARM,
8281 false, TYPE_REVERSE_STORAGE_ORDER (type));
8283 else
8285 gcc_assert (REG_P (target)
8286 && !TYPE_REVERSE_STORAGE_ORDER (type));
8288 /* Store this field into a union of the proper type. */
8289 store_field (target,
8290 MIN ((int_size_in_bytes (TREE_TYPE
8291 (treeop0))
8292 * BITS_PER_UNIT),
8293 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8294 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8295 false, false);
8298 /* Return the entire union. */
8299 return target;
8302 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8304 op0 = expand_expr (treeop0, target, VOIDmode,
8305 modifier);
8307 /* If the signedness of the conversion differs and OP0 is
8308 a promoted SUBREG, clear that indication since we now
8309 have to do the proper extension. */
8310 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8311 && GET_CODE (op0) == SUBREG)
8312 SUBREG_PROMOTED_VAR_P (op0) = 0;
8314 return REDUCE_BIT_FIELD (op0);
8317 op0 = expand_expr (treeop0, NULL_RTX, mode,
8318 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8319 if (GET_MODE (op0) == mode)
8322 /* If OP0 is a constant, just convert it into the proper mode. */
8323 else if (CONSTANT_P (op0))
8325 tree inner_type = TREE_TYPE (treeop0);
8326 machine_mode inner_mode = GET_MODE (op0);
8328 if (inner_mode == VOIDmode)
8329 inner_mode = TYPE_MODE (inner_type);
8331 if (modifier == EXPAND_INITIALIZER)
8332 op0 = lowpart_subreg (mode, op0, inner_mode);
8333 else
8334 op0= convert_modes (mode, inner_mode, op0,
8335 TYPE_UNSIGNED (inner_type));
8338 else if (modifier == EXPAND_INITIALIZER)
8339 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8341 else if (target == 0)
8342 op0 = convert_to_mode (mode, op0,
8343 TYPE_UNSIGNED (TREE_TYPE
8344 (treeop0)));
8345 else
8347 convert_move (target, op0,
8348 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8349 op0 = target;
8352 return REDUCE_BIT_FIELD (op0);
8354 case ADDR_SPACE_CONVERT_EXPR:
8356 tree treeop0_type = TREE_TYPE (treeop0);
8358 gcc_assert (POINTER_TYPE_P (type));
8359 gcc_assert (POINTER_TYPE_P (treeop0_type));
8361 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8362 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8364 /* Conversions between pointers to the same address space should
8365 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8366 gcc_assert (as_to != as_from);
8368 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8370 /* Ask target code to handle conversion between pointers
8371 to overlapping address spaces. */
8372 if (targetm.addr_space.subset_p (as_to, as_from)
8373 || targetm.addr_space.subset_p (as_from, as_to))
8375 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8377 else
8379 /* For disjoint address spaces, converting anything but a null
8380 pointer invokes undefined behavior. We truncate or extend the
8381 value as if we'd converted via integers, which handles 0 as
8382 required, and all others as the programmer likely expects. */
8383 #ifndef POINTERS_EXTEND_UNSIGNED
8384 const int POINTERS_EXTEND_UNSIGNED = 1;
8385 #endif
8386 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8387 op0, POINTERS_EXTEND_UNSIGNED);
8389 gcc_assert (op0);
8390 return op0;
8393 case POINTER_PLUS_EXPR:
8394 /* Even though the sizetype mode and the pointer's mode can be different
8395 expand is able to handle this correctly and get the correct result out
8396 of the PLUS_EXPR code. */
8397 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8398 if sizetype precision is smaller than pointer precision. */
8399 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8400 treeop1 = fold_convert_loc (loc, type,
8401 fold_convert_loc (loc, ssizetype,
8402 treeop1));
8403 /* If sizetype precision is larger than pointer precision, truncate the
8404 offset to have matching modes. */
8405 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8406 treeop1 = fold_convert_loc (loc, type, treeop1);
8407 /* FALLTHRU */
8409 case PLUS_EXPR:
8410 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8411 something else, make sure we add the register to the constant and
8412 then to the other thing. This case can occur during strength
8413 reduction and doing it this way will produce better code if the
8414 frame pointer or argument pointer is eliminated.
8416 fold-const.c will ensure that the constant is always in the inner
8417 PLUS_EXPR, so the only case we need to do anything about is if
8418 sp, ap, or fp is our second argument, in which case we must swap
8419 the innermost first argument and our second argument. */
8421 if (TREE_CODE (treeop0) == PLUS_EXPR
8422 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8423 && VAR_P (treeop1)
8424 && (DECL_RTL (treeop1) == frame_pointer_rtx
8425 || DECL_RTL (treeop1) == stack_pointer_rtx
8426 || DECL_RTL (treeop1) == arg_pointer_rtx))
8428 gcc_unreachable ();
8431 /* If the result is to be ptr_mode and we are adding an integer to
8432 something, we might be forming a constant. So try to use
8433 plus_constant. If it produces a sum and we can't accept it,
8434 use force_operand. This allows P = &ARR[const] to generate
8435 efficient code on machines where a SYMBOL_REF is not a valid
8436 address.
8438 If this is an EXPAND_SUM call, always return the sum. */
8439 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8440 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8442 if (modifier == EXPAND_STACK_PARM)
8443 target = 0;
8444 if (TREE_CODE (treeop0) == INTEGER_CST
8445 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8446 && TREE_CONSTANT (treeop1))
8448 rtx constant_part;
8449 HOST_WIDE_INT wc;
8450 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8452 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8453 EXPAND_SUM);
8454 /* Use wi::shwi to ensure that the constant is
8455 truncated according to the mode of OP1, then sign extended
8456 to a HOST_WIDE_INT. Using the constant directly can result
8457 in non-canonical RTL in a 64x32 cross compile. */
8458 wc = TREE_INT_CST_LOW (treeop0);
8459 constant_part =
8460 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8461 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8462 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8463 op1 = force_operand (op1, target);
8464 return REDUCE_BIT_FIELD (op1);
8467 else if (TREE_CODE (treeop1) == INTEGER_CST
8468 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8469 && TREE_CONSTANT (treeop0))
8471 rtx constant_part;
8472 HOST_WIDE_INT wc;
8473 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8475 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8476 (modifier == EXPAND_INITIALIZER
8477 ? EXPAND_INITIALIZER : EXPAND_SUM));
8478 if (! CONSTANT_P (op0))
8480 op1 = expand_expr (treeop1, NULL_RTX,
8481 VOIDmode, modifier);
8482 /* Return a PLUS if modifier says it's OK. */
8483 if (modifier == EXPAND_SUM
8484 || modifier == EXPAND_INITIALIZER)
8485 return simplify_gen_binary (PLUS, mode, op0, op1);
8486 goto binop2;
8488 /* Use wi::shwi to ensure that the constant is
8489 truncated according to the mode of OP1, then sign extended
8490 to a HOST_WIDE_INT. Using the constant directly can result
8491 in non-canonical RTL in a 64x32 cross compile. */
8492 wc = TREE_INT_CST_LOW (treeop1);
8493 constant_part
8494 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8495 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8496 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8497 op0 = force_operand (op0, target);
8498 return REDUCE_BIT_FIELD (op0);
8502 /* Use TER to expand pointer addition of a negated value
8503 as pointer subtraction. */
8504 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8505 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8506 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8507 && TREE_CODE (treeop1) == SSA_NAME
8508 && TYPE_MODE (TREE_TYPE (treeop0))
8509 == TYPE_MODE (TREE_TYPE (treeop1)))
8511 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8512 if (def)
8514 treeop1 = gimple_assign_rhs1 (def);
8515 code = MINUS_EXPR;
8516 goto do_minus;
8520 /* No sense saving up arithmetic to be done
8521 if it's all in the wrong mode to form part of an address.
8522 And force_operand won't know whether to sign-extend or
8523 zero-extend. */
8524 if (modifier != EXPAND_INITIALIZER
8525 && (modifier != EXPAND_SUM || mode != ptr_mode))
8527 expand_operands (treeop0, treeop1,
8528 subtarget, &op0, &op1, modifier);
8529 if (op0 == const0_rtx)
8530 return op1;
8531 if (op1 == const0_rtx)
8532 return op0;
8533 goto binop2;
8536 expand_operands (treeop0, treeop1,
8537 subtarget, &op0, &op1, modifier);
8538 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8540 case MINUS_EXPR:
8541 do_minus:
8542 /* For initializers, we are allowed to return a MINUS of two
8543 symbolic constants. Here we handle all cases when both operands
8544 are constant. */
8545 /* Handle difference of two symbolic constants,
8546 for the sake of an initializer. */
8547 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8548 && really_constant_p (treeop0)
8549 && really_constant_p (treeop1))
8551 expand_operands (treeop0, treeop1,
8552 NULL_RTX, &op0, &op1, modifier);
8554 /* If the last operand is a CONST_INT, use plus_constant of
8555 the negated constant. Else make the MINUS. */
8556 if (CONST_INT_P (op1))
8557 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8558 -INTVAL (op1)));
8559 else
8560 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8563 /* No sense saving up arithmetic to be done
8564 if it's all in the wrong mode to form part of an address.
8565 And force_operand won't know whether to sign-extend or
8566 zero-extend. */
8567 if (modifier != EXPAND_INITIALIZER
8568 && (modifier != EXPAND_SUM || mode != ptr_mode))
8569 goto binop;
8571 expand_operands (treeop0, treeop1,
8572 subtarget, &op0, &op1, modifier);
8574 /* Convert A - const to A + (-const). */
8575 if (CONST_INT_P (op1))
8577 op1 = negate_rtx (mode, op1);
8578 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8581 goto binop2;
8583 case WIDEN_MULT_PLUS_EXPR:
8584 case WIDEN_MULT_MINUS_EXPR:
8585 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8586 op2 = expand_normal (treeop2);
8587 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8588 target, unsignedp);
8589 return target;
8591 case WIDEN_MULT_EXPR:
8592 /* If first operand is constant, swap them.
8593 Thus the following special case checks need only
8594 check the second operand. */
8595 if (TREE_CODE (treeop0) == INTEGER_CST)
8596 std::swap (treeop0, treeop1);
8598 /* First, check if we have a multiplication of one signed and one
8599 unsigned operand. */
8600 if (TREE_CODE (treeop1) != INTEGER_CST
8601 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8602 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8604 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8605 this_optab = usmul_widen_optab;
8606 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8607 != CODE_FOR_nothing)
8609 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8610 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8611 EXPAND_NORMAL);
8612 else
8613 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8614 EXPAND_NORMAL);
8615 /* op0 and op1 might still be constant, despite the above
8616 != INTEGER_CST check. Handle it. */
8617 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8619 op0 = convert_modes (innermode, mode, op0, true);
8620 op1 = convert_modes (innermode, mode, op1, false);
8621 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8622 target, unsignedp));
8624 goto binop3;
8627 /* Check for a multiplication with matching signedness. */
8628 else if ((TREE_CODE (treeop1) == INTEGER_CST
8629 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8630 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8631 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8633 tree op0type = TREE_TYPE (treeop0);
8634 machine_mode innermode = TYPE_MODE (op0type);
8635 bool zextend_p = TYPE_UNSIGNED (op0type);
8636 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8637 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8639 if (TREE_CODE (treeop0) != INTEGER_CST)
8641 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8642 != CODE_FOR_nothing)
8644 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8645 EXPAND_NORMAL);
8646 /* op0 and op1 might still be constant, despite the above
8647 != INTEGER_CST check. Handle it. */
8648 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8650 widen_mult_const:
8651 op0 = convert_modes (innermode, mode, op0, zextend_p);
8653 = convert_modes (innermode, mode, op1,
8654 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8655 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8656 target,
8657 unsignedp));
8659 temp = expand_widening_mult (mode, op0, op1, target,
8660 unsignedp, this_optab);
8661 return REDUCE_BIT_FIELD (temp);
8663 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8664 != CODE_FOR_nothing
8665 && innermode == word_mode)
8667 rtx htem, hipart;
8668 op0 = expand_normal (treeop0);
8669 if (TREE_CODE (treeop1) == INTEGER_CST)
8670 op1 = convert_modes (innermode, mode,
8671 expand_normal (treeop1),
8672 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8673 else
8674 op1 = expand_normal (treeop1);
8675 /* op0 and op1 might still be constant, despite the above
8676 != INTEGER_CST check. Handle it. */
8677 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8678 goto widen_mult_const;
8679 temp = expand_binop (mode, other_optab, op0, op1, target,
8680 unsignedp, OPTAB_LIB_WIDEN);
8681 hipart = gen_highpart (innermode, temp);
8682 htem = expand_mult_highpart_adjust (innermode, hipart,
8683 op0, op1, hipart,
8684 zextend_p);
8685 if (htem != hipart)
8686 emit_move_insn (hipart, htem);
8687 return REDUCE_BIT_FIELD (temp);
8691 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8692 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8693 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8694 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8696 case FMA_EXPR:
8698 optab opt = fma_optab;
8699 gimple *def0, *def2;
8701 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8702 call. */
8703 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8705 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8706 tree call_expr;
8708 gcc_assert (fn != NULL_TREE);
8709 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8710 return expand_builtin (call_expr, target, subtarget, mode, false);
8713 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8714 /* The multiplication is commutative - look at its 2nd operand
8715 if the first isn't fed by a negate. */
8716 if (!def0)
8718 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8719 /* Swap operands if the 2nd operand is fed by a negate. */
8720 if (def0)
8721 std::swap (treeop0, treeop1);
8723 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8725 op0 = op2 = NULL;
8727 if (def0 && def2
8728 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8730 opt = fnms_optab;
8731 op0 = expand_normal (gimple_assign_rhs1 (def0));
8732 op2 = expand_normal (gimple_assign_rhs1 (def2));
8734 else if (def0
8735 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8737 opt = fnma_optab;
8738 op0 = expand_normal (gimple_assign_rhs1 (def0));
8740 else if (def2
8741 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8743 opt = fms_optab;
8744 op2 = expand_normal (gimple_assign_rhs1 (def2));
8747 if (op0 == NULL)
8748 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8749 if (op2 == NULL)
8750 op2 = expand_normal (treeop2);
8751 op1 = expand_normal (treeop1);
8753 return expand_ternary_op (TYPE_MODE (type), opt,
8754 op0, op1, op2, target, 0);
8757 case MULT_EXPR:
8758 /* If this is a fixed-point operation, then we cannot use the code
8759 below because "expand_mult" doesn't support sat/no-sat fixed-point
8760 multiplications. */
8761 if (ALL_FIXED_POINT_MODE_P (mode))
8762 goto binop;
8764 /* If first operand is constant, swap them.
8765 Thus the following special case checks need only
8766 check the second operand. */
8767 if (TREE_CODE (treeop0) == INTEGER_CST)
8768 std::swap (treeop0, treeop1);
8770 /* Attempt to return something suitable for generating an
8771 indexed address, for machines that support that. */
8773 if (modifier == EXPAND_SUM && mode == ptr_mode
8774 && tree_fits_shwi_p (treeop1))
8776 tree exp1 = treeop1;
8778 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8779 EXPAND_SUM);
8781 if (!REG_P (op0))
8782 op0 = force_operand (op0, NULL_RTX);
8783 if (!REG_P (op0))
8784 op0 = copy_to_mode_reg (mode, op0);
8786 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8787 gen_int_mode (tree_to_shwi (exp1),
8788 TYPE_MODE (TREE_TYPE (exp1)))));
8791 if (modifier == EXPAND_STACK_PARM)
8792 target = 0;
8794 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8795 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8797 case TRUNC_DIV_EXPR:
8798 case FLOOR_DIV_EXPR:
8799 case CEIL_DIV_EXPR:
8800 case ROUND_DIV_EXPR:
8801 case EXACT_DIV_EXPR:
8802 /* If this is a fixed-point operation, then we cannot use the code
8803 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8804 divisions. */
8805 if (ALL_FIXED_POINT_MODE_P (mode))
8806 goto binop;
8808 if (modifier == EXPAND_STACK_PARM)
8809 target = 0;
8810 /* Possible optimization: compute the dividend with EXPAND_SUM
8811 then if the divisor is constant can optimize the case
8812 where some terms of the dividend have coeffs divisible by it. */
8813 expand_operands (treeop0, treeop1,
8814 subtarget, &op0, &op1, EXPAND_NORMAL);
8815 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8817 case RDIV_EXPR:
8818 goto binop;
8820 case MULT_HIGHPART_EXPR:
8821 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8822 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8823 gcc_assert (temp);
8824 return temp;
8826 case TRUNC_MOD_EXPR:
8827 case FLOOR_MOD_EXPR:
8828 case CEIL_MOD_EXPR:
8829 case ROUND_MOD_EXPR:
8830 if (modifier == EXPAND_STACK_PARM)
8831 target = 0;
8832 expand_operands (treeop0, treeop1,
8833 subtarget, &op0, &op1, EXPAND_NORMAL);
8834 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8836 case FIXED_CONVERT_EXPR:
8837 op0 = expand_normal (treeop0);
8838 if (target == 0 || modifier == EXPAND_STACK_PARM)
8839 target = gen_reg_rtx (mode);
8841 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8842 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8843 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8844 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8845 else
8846 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8847 return target;
8849 case FIX_TRUNC_EXPR:
8850 op0 = expand_normal (treeop0);
8851 if (target == 0 || modifier == EXPAND_STACK_PARM)
8852 target = gen_reg_rtx (mode);
8853 expand_fix (target, op0, unsignedp);
8854 return target;
8856 case FLOAT_EXPR:
8857 op0 = expand_normal (treeop0);
8858 if (target == 0 || modifier == EXPAND_STACK_PARM)
8859 target = gen_reg_rtx (mode);
8860 /* expand_float can't figure out what to do if FROM has VOIDmode.
8861 So give it the correct mode. With -O, cse will optimize this. */
8862 if (GET_MODE (op0) == VOIDmode)
8863 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8864 op0);
8865 expand_float (target, op0,
8866 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8867 return target;
8869 case NEGATE_EXPR:
8870 op0 = expand_expr (treeop0, subtarget,
8871 VOIDmode, EXPAND_NORMAL);
8872 if (modifier == EXPAND_STACK_PARM)
8873 target = 0;
8874 temp = expand_unop (mode,
8875 optab_for_tree_code (NEGATE_EXPR, type,
8876 optab_default),
8877 op0, target, 0);
8878 gcc_assert (temp);
8879 return REDUCE_BIT_FIELD (temp);
8881 case ABS_EXPR:
8882 op0 = expand_expr (treeop0, subtarget,
8883 VOIDmode, EXPAND_NORMAL);
8884 if (modifier == EXPAND_STACK_PARM)
8885 target = 0;
8887 /* ABS_EXPR is not valid for complex arguments. */
8888 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8889 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8891 /* Unsigned abs is simply the operand. Testing here means we don't
8892 risk generating incorrect code below. */
8893 if (TYPE_UNSIGNED (type))
8894 return op0;
8896 return expand_abs (mode, op0, target, unsignedp,
8897 safe_from_p (target, treeop0, 1));
8899 case MAX_EXPR:
8900 case MIN_EXPR:
8901 target = original_target;
8902 if (target == 0
8903 || modifier == EXPAND_STACK_PARM
8904 || (MEM_P (target) && MEM_VOLATILE_P (target))
8905 || GET_MODE (target) != mode
8906 || (REG_P (target)
8907 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8908 target = gen_reg_rtx (mode);
8909 expand_operands (treeop0, treeop1,
8910 target, &op0, &op1, EXPAND_NORMAL);
8912 /* First try to do it with a special MIN or MAX instruction.
8913 If that does not win, use a conditional jump to select the proper
8914 value. */
8915 this_optab = optab_for_tree_code (code, type, optab_default);
8916 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8917 OPTAB_WIDEN);
8918 if (temp != 0)
8919 return temp;
8921 /* At this point, a MEM target is no longer useful; we will get better
8922 code without it. */
8924 if (! REG_P (target))
8925 target = gen_reg_rtx (mode);
8927 /* If op1 was placed in target, swap op0 and op1. */
8928 if (target != op0 && target == op1)
8929 std::swap (op0, op1);
8931 /* We generate better code and avoid problems with op1 mentioning
8932 target by forcing op1 into a pseudo if it isn't a constant. */
8933 if (! CONSTANT_P (op1))
8934 op1 = force_reg (mode, op1);
8937 enum rtx_code comparison_code;
8938 rtx cmpop1 = op1;
8940 if (code == MAX_EXPR)
8941 comparison_code = unsignedp ? GEU : GE;
8942 else
8943 comparison_code = unsignedp ? LEU : LE;
8945 /* Canonicalize to comparisons against 0. */
8946 if (op1 == const1_rtx)
8948 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8949 or (a != 0 ? a : 1) for unsigned.
8950 For MIN we are safe converting (a <= 1 ? a : 1)
8951 into (a <= 0 ? a : 1) */
8952 cmpop1 = const0_rtx;
8953 if (code == MAX_EXPR)
8954 comparison_code = unsignedp ? NE : GT;
8956 if (op1 == constm1_rtx && !unsignedp)
8958 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8959 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8960 cmpop1 = const0_rtx;
8961 if (code == MIN_EXPR)
8962 comparison_code = LT;
8965 /* Use a conditional move if possible. */
8966 if (can_conditionally_move_p (mode))
8968 rtx insn;
8970 start_sequence ();
8972 /* Try to emit the conditional move. */
8973 insn = emit_conditional_move (target, comparison_code,
8974 op0, cmpop1, mode,
8975 op0, op1, mode,
8976 unsignedp);
8978 /* If we could do the conditional move, emit the sequence,
8979 and return. */
8980 if (insn)
8982 rtx_insn *seq = get_insns ();
8983 end_sequence ();
8984 emit_insn (seq);
8985 return target;
8988 /* Otherwise discard the sequence and fall back to code with
8989 branches. */
8990 end_sequence ();
8993 if (target != op0)
8994 emit_move_insn (target, op0);
8996 lab = gen_label_rtx ();
8997 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8998 unsignedp, mode, NULL_RTX, NULL, lab,
8999 -1);
9001 emit_move_insn (target, op1);
9002 emit_label (lab);
9003 return target;
9005 case BIT_NOT_EXPR:
9006 op0 = expand_expr (treeop0, subtarget,
9007 VOIDmode, EXPAND_NORMAL);
9008 if (modifier == EXPAND_STACK_PARM)
9009 target = 0;
9010 /* In case we have to reduce the result to bitfield precision
9011 for unsigned bitfield expand this as XOR with a proper constant
9012 instead. */
9013 if (reduce_bit_field && TYPE_UNSIGNED (type))
9015 wide_int mask = wi::mask (TYPE_PRECISION (type),
9016 false, GET_MODE_PRECISION (mode));
9018 temp = expand_binop (mode, xor_optab, op0,
9019 immed_wide_int_const (mask, mode),
9020 target, 1, OPTAB_LIB_WIDEN);
9022 else
9023 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9024 gcc_assert (temp);
9025 return temp;
9027 /* ??? Can optimize bitwise operations with one arg constant.
9028 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9029 and (a bitwise1 b) bitwise2 b (etc)
9030 but that is probably not worth while. */
9032 case BIT_AND_EXPR:
9033 case BIT_IOR_EXPR:
9034 case BIT_XOR_EXPR:
9035 goto binop;
9037 case LROTATE_EXPR:
9038 case RROTATE_EXPR:
9039 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9040 || (GET_MODE_PRECISION (TYPE_MODE (type))
9041 == TYPE_PRECISION (type)));
9042 /* fall through */
9044 case LSHIFT_EXPR:
9045 case RSHIFT_EXPR:
9047 /* If this is a fixed-point operation, then we cannot use the code
9048 below because "expand_shift" doesn't support sat/no-sat fixed-point
9049 shifts. */
9050 if (ALL_FIXED_POINT_MODE_P (mode))
9051 goto binop;
9053 if (! safe_from_p (subtarget, treeop1, 1))
9054 subtarget = 0;
9055 if (modifier == EXPAND_STACK_PARM)
9056 target = 0;
9057 op0 = expand_expr (treeop0, subtarget,
9058 VOIDmode, EXPAND_NORMAL);
9060 /* Left shift optimization when shifting across word_size boundary.
9062 If mode == GET_MODE_WIDER_MODE (word_mode), then normally there isn't
9063 native instruction to support this wide mode left shift. Given below
9064 scenario:
9066 Type A = (Type) B << C
9068 |< T >|
9069 | dest_high | dest_low |
9071 | word_size |
9073 If the shift amount C caused we shift B to across the word size
9074 boundary, i.e part of B shifted into high half of destination
9075 register, and part of B remains in the low half, then GCC will use
9076 the following left shift expand logic:
9078 1. Initialize dest_low to B.
9079 2. Initialize every bit of dest_high to the sign bit of B.
9080 3. Logic left shift dest_low by C bit to finalize dest_low.
9081 The value of dest_low before this shift is kept in a temp D.
9082 4. Logic left shift dest_high by C.
9083 5. Logic right shift D by (word_size - C).
9084 6. Or the result of 4 and 5 to finalize dest_high.
9086 While, by checking gimple statements, if operand B is coming from
9087 signed extension, then we can simplify above expand logic into:
9089 1. dest_high = src_low >> (word_size - C).
9090 2. dest_low = src_low << C.
9092 We can use one arithmetic right shift to finish all the purpose of
9093 steps 2, 4, 5, 6, thus we reduce the steps needed from 6 into 2. */
9095 temp = NULL_RTX;
9096 if (code == LSHIFT_EXPR
9097 && target
9098 && REG_P (target)
9099 && ! unsignedp
9100 && mode == GET_MODE_WIDER_MODE (word_mode)
9101 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
9102 && TREE_CONSTANT (treeop1)
9103 && TREE_CODE (treeop0) == SSA_NAME)
9105 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9106 if (is_gimple_assign (def)
9107 && gimple_assign_rhs_code (def) == NOP_EXPR)
9109 machine_mode rmode = TYPE_MODE
9110 (TREE_TYPE (gimple_assign_rhs1 (def)));
9112 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
9113 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9114 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9115 >= GET_MODE_BITSIZE (word_mode)))
9117 rtx_insn *seq, *seq_old;
9118 unsigned int high_off = subreg_highpart_offset (word_mode,
9119 mode);
9120 rtx low = lowpart_subreg (word_mode, op0, mode);
9121 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9122 rtx dest_high = simplify_gen_subreg (word_mode, target,
9123 mode, high_off);
9124 HOST_WIDE_INT ramount = (BITS_PER_WORD
9125 - TREE_INT_CST_LOW (treeop1));
9126 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9128 start_sequence ();
9129 /* dest_high = src_low >> (word_size - C). */
9130 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9131 rshift, dest_high, unsignedp);
9132 if (temp != dest_high)
9133 emit_move_insn (dest_high, temp);
9135 /* dest_low = src_low << C. */
9136 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9137 treeop1, dest_low, unsignedp);
9138 if (temp != dest_low)
9139 emit_move_insn (dest_low, temp);
9141 seq = get_insns ();
9142 end_sequence ();
9143 temp = target ;
9145 if (have_insn_for (ASHIFT, mode))
9147 bool speed_p = optimize_insn_for_speed_p ();
9148 start_sequence ();
9149 rtx ret_old = expand_variable_shift (code, mode, op0,
9150 treeop1, target,
9151 unsignedp);
9153 seq_old = get_insns ();
9154 end_sequence ();
9155 if (seq_cost (seq, speed_p)
9156 >= seq_cost (seq_old, speed_p))
9158 seq = seq_old;
9159 temp = ret_old;
9162 emit_insn (seq);
9167 if (temp == NULL_RTX)
9168 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9169 unsignedp);
9170 if (code == LSHIFT_EXPR)
9171 temp = REDUCE_BIT_FIELD (temp);
9172 return temp;
9175 /* Could determine the answer when only additive constants differ. Also,
9176 the addition of one can be handled by changing the condition. */
9177 case LT_EXPR:
9178 case LE_EXPR:
9179 case GT_EXPR:
9180 case GE_EXPR:
9181 case EQ_EXPR:
9182 case NE_EXPR:
9183 case UNORDERED_EXPR:
9184 case ORDERED_EXPR:
9185 case UNLT_EXPR:
9186 case UNLE_EXPR:
9187 case UNGT_EXPR:
9188 case UNGE_EXPR:
9189 case UNEQ_EXPR:
9190 case LTGT_EXPR:
9192 temp = do_store_flag (ops,
9193 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9194 tmode != VOIDmode ? tmode : mode);
9195 if (temp)
9196 return temp;
9198 /* Use a compare and a jump for BLKmode comparisons, or for function
9199 type comparisons is have_canonicalize_funcptr_for_compare. */
9201 if ((target == 0
9202 || modifier == EXPAND_STACK_PARM
9203 || ! safe_from_p (target, treeop0, 1)
9204 || ! safe_from_p (target, treeop1, 1)
9205 /* Make sure we don't have a hard reg (such as function's return
9206 value) live across basic blocks, if not optimizing. */
9207 || (!optimize && REG_P (target)
9208 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9209 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9211 emit_move_insn (target, const0_rtx);
9213 rtx_code_label *lab1 = gen_label_rtx ();
9214 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9216 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9217 emit_move_insn (target, constm1_rtx);
9218 else
9219 emit_move_insn (target, const1_rtx);
9221 emit_label (lab1);
9222 return target;
9224 case COMPLEX_EXPR:
9225 /* Get the rtx code of the operands. */
9226 op0 = expand_normal (treeop0);
9227 op1 = expand_normal (treeop1);
9229 if (!target)
9230 target = gen_reg_rtx (TYPE_MODE (type));
9231 else
9232 /* If target overlaps with op1, then either we need to force
9233 op1 into a pseudo (if target also overlaps with op0),
9234 or write the complex parts in reverse order. */
9235 switch (GET_CODE (target))
9237 case CONCAT:
9238 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9240 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9242 complex_expr_force_op1:
9243 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9244 emit_move_insn (temp, op1);
9245 op1 = temp;
9246 break;
9248 complex_expr_swap_order:
9249 /* Move the imaginary (op1) and real (op0) parts to their
9250 location. */
9251 write_complex_part (target, op1, true);
9252 write_complex_part (target, op0, false);
9254 return target;
9256 break;
9257 case MEM:
9258 temp = adjust_address_nv (target,
9259 GET_MODE_INNER (GET_MODE (target)), 0);
9260 if (reg_overlap_mentioned_p (temp, op1))
9262 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9263 temp = adjust_address_nv (target, imode,
9264 GET_MODE_SIZE (imode));
9265 if (reg_overlap_mentioned_p (temp, op0))
9266 goto complex_expr_force_op1;
9267 goto complex_expr_swap_order;
9269 break;
9270 default:
9271 if (reg_overlap_mentioned_p (target, op1))
9273 if (reg_overlap_mentioned_p (target, op0))
9274 goto complex_expr_force_op1;
9275 goto complex_expr_swap_order;
9277 break;
9280 /* Move the real (op0) and imaginary (op1) parts to their location. */
9281 write_complex_part (target, op0, false);
9282 write_complex_part (target, op1, true);
9284 return target;
9286 case WIDEN_SUM_EXPR:
9288 tree oprnd0 = treeop0;
9289 tree oprnd1 = treeop1;
9291 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9292 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9293 target, unsignedp);
9294 return target;
9297 case REDUC_MAX_EXPR:
9298 case REDUC_MIN_EXPR:
9299 case REDUC_PLUS_EXPR:
9301 op0 = expand_normal (treeop0);
9302 this_optab = optab_for_tree_code (code, type, optab_default);
9303 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9305 struct expand_operand ops[2];
9306 enum insn_code icode = optab_handler (this_optab, vec_mode);
9308 create_output_operand (&ops[0], target, mode);
9309 create_input_operand (&ops[1], op0, vec_mode);
9310 expand_insn (icode, 2, ops);
9311 target = ops[0].value;
9312 if (GET_MODE (target) != mode)
9313 return gen_lowpart (tmode, target);
9314 return target;
9317 case VEC_UNPACK_HI_EXPR:
9318 case VEC_UNPACK_LO_EXPR:
9320 op0 = expand_normal (treeop0);
9321 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9322 target, unsignedp);
9323 gcc_assert (temp);
9324 return temp;
9327 case VEC_UNPACK_FLOAT_HI_EXPR:
9328 case VEC_UNPACK_FLOAT_LO_EXPR:
9330 op0 = expand_normal (treeop0);
9331 /* The signedness is determined from input operand. */
9332 temp = expand_widen_pattern_expr
9333 (ops, op0, NULL_RTX, NULL_RTX,
9334 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9336 gcc_assert (temp);
9337 return temp;
9340 case VEC_WIDEN_MULT_HI_EXPR:
9341 case VEC_WIDEN_MULT_LO_EXPR:
9342 case VEC_WIDEN_MULT_EVEN_EXPR:
9343 case VEC_WIDEN_MULT_ODD_EXPR:
9344 case VEC_WIDEN_LSHIFT_HI_EXPR:
9345 case VEC_WIDEN_LSHIFT_LO_EXPR:
9346 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9347 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9348 target, unsignedp);
9349 gcc_assert (target);
9350 return target;
9352 case VEC_PACK_TRUNC_EXPR:
9353 case VEC_PACK_SAT_EXPR:
9354 case VEC_PACK_FIX_TRUNC_EXPR:
9355 mode = TYPE_MODE (TREE_TYPE (treeop0));
9356 goto binop;
9358 case VEC_PERM_EXPR:
9359 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9360 op2 = expand_normal (treeop2);
9362 /* Careful here: if the target doesn't support integral vector modes,
9363 a constant selection vector could wind up smooshed into a normal
9364 integral constant. */
9365 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9367 tree sel_type = TREE_TYPE (treeop2);
9368 machine_mode vmode
9369 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9370 TYPE_VECTOR_SUBPARTS (sel_type));
9371 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9372 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9373 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9375 else
9376 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9378 temp = expand_vec_perm (mode, op0, op1, op2, target);
9379 gcc_assert (temp);
9380 return temp;
9382 case DOT_PROD_EXPR:
9384 tree oprnd0 = treeop0;
9385 tree oprnd1 = treeop1;
9386 tree oprnd2 = treeop2;
9387 rtx op2;
9389 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9390 op2 = expand_normal (oprnd2);
9391 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9392 target, unsignedp);
9393 return target;
9396 case SAD_EXPR:
9398 tree oprnd0 = treeop0;
9399 tree oprnd1 = treeop1;
9400 tree oprnd2 = treeop2;
9401 rtx op2;
9403 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9404 op2 = expand_normal (oprnd2);
9405 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9406 target, unsignedp);
9407 return target;
9410 case REALIGN_LOAD_EXPR:
9412 tree oprnd0 = treeop0;
9413 tree oprnd1 = treeop1;
9414 tree oprnd2 = treeop2;
9415 rtx op2;
9417 this_optab = optab_for_tree_code (code, type, optab_default);
9418 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9419 op2 = expand_normal (oprnd2);
9420 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9421 target, unsignedp);
9422 gcc_assert (temp);
9423 return temp;
9426 case COND_EXPR:
9428 /* A COND_EXPR with its type being VOID_TYPE represents a
9429 conditional jump and is handled in
9430 expand_gimple_cond_expr. */
9431 gcc_assert (!VOID_TYPE_P (type));
9433 /* Note that COND_EXPRs whose type is a structure or union
9434 are required to be constructed to contain assignments of
9435 a temporary variable, so that we can evaluate them here
9436 for side effect only. If type is void, we must do likewise. */
9438 gcc_assert (!TREE_ADDRESSABLE (type)
9439 && !ignore
9440 && TREE_TYPE (treeop1) != void_type_node
9441 && TREE_TYPE (treeop2) != void_type_node);
9443 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9444 if (temp)
9445 return temp;
9447 /* If we are not to produce a result, we have no target. Otherwise,
9448 if a target was specified use it; it will not be used as an
9449 intermediate target unless it is safe. If no target, use a
9450 temporary. */
9452 if (modifier != EXPAND_STACK_PARM
9453 && original_target
9454 && safe_from_p (original_target, treeop0, 1)
9455 && GET_MODE (original_target) == mode
9456 && !MEM_P (original_target))
9457 temp = original_target;
9458 else
9459 temp = assign_temp (type, 0, 1);
9461 do_pending_stack_adjust ();
9462 NO_DEFER_POP;
9463 rtx_code_label *lab0 = gen_label_rtx ();
9464 rtx_code_label *lab1 = gen_label_rtx ();
9465 jumpifnot (treeop0, lab0, -1);
9466 store_expr (treeop1, temp,
9467 modifier == EXPAND_STACK_PARM,
9468 false, false);
9470 emit_jump_insn (targetm.gen_jump (lab1));
9471 emit_barrier ();
9472 emit_label (lab0);
9473 store_expr (treeop2, temp,
9474 modifier == EXPAND_STACK_PARM,
9475 false, false);
9477 emit_label (lab1);
9478 OK_DEFER_POP;
9479 return temp;
9482 case VEC_COND_EXPR:
9483 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9484 return target;
9486 case BIT_INSERT_EXPR:
9488 unsigned bitpos = tree_to_uhwi (treeop2);
9489 unsigned bitsize;
9490 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9491 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9492 else
9493 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9494 rtx op0 = expand_normal (treeop0);
9495 rtx op1 = expand_normal (treeop1);
9496 rtx dst = gen_reg_rtx (mode);
9497 emit_move_insn (dst, op0);
9498 store_bit_field (dst, bitsize, bitpos, 0, 0,
9499 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9500 return dst;
9503 default:
9504 gcc_unreachable ();
9507 /* Here to do an ordinary binary operator. */
9508 binop:
9509 expand_operands (treeop0, treeop1,
9510 subtarget, &op0, &op1, EXPAND_NORMAL);
9511 binop2:
9512 this_optab = optab_for_tree_code (code, type, optab_default);
9513 binop3:
9514 if (modifier == EXPAND_STACK_PARM)
9515 target = 0;
9516 temp = expand_binop (mode, this_optab, op0, op1, target,
9517 unsignedp, OPTAB_LIB_WIDEN);
9518 gcc_assert (temp);
9519 /* Bitwise operations do not need bitfield reduction as we expect their
9520 operands being properly truncated. */
9521 if (code == BIT_XOR_EXPR
9522 || code == BIT_AND_EXPR
9523 || code == BIT_IOR_EXPR)
9524 return temp;
9525 return REDUCE_BIT_FIELD (temp);
9527 #undef REDUCE_BIT_FIELD
9530 /* Return TRUE if expression STMT is suitable for replacement.
9531 Never consider memory loads as replaceable, because those don't ever lead
9532 into constant expressions. */
9534 static bool
9535 stmt_is_replaceable_p (gimple *stmt)
9537 if (ssa_is_replaceable_p (stmt))
9539 /* Don't move around loads. */
9540 if (!gimple_assign_single_p (stmt)
9541 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9542 return true;
9544 return false;
9548 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9549 enum expand_modifier modifier, rtx *alt_rtl,
9550 bool inner_reference_p)
9552 rtx op0, op1, temp, decl_rtl;
9553 tree type;
9554 int unsignedp;
9555 machine_mode mode, dmode;
9556 enum tree_code code = TREE_CODE (exp);
9557 rtx subtarget, original_target;
9558 int ignore;
9559 tree context;
9560 bool reduce_bit_field;
9561 location_t loc = EXPR_LOCATION (exp);
9562 struct separate_ops ops;
9563 tree treeop0, treeop1, treeop2;
9564 tree ssa_name = NULL_TREE;
9565 gimple *g;
9567 type = TREE_TYPE (exp);
9568 mode = TYPE_MODE (type);
9569 unsignedp = TYPE_UNSIGNED (type);
9571 treeop0 = treeop1 = treeop2 = NULL_TREE;
9572 if (!VL_EXP_CLASS_P (exp))
9573 switch (TREE_CODE_LENGTH (code))
9575 default:
9576 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9577 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9578 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9579 case 0: break;
9581 ops.code = code;
9582 ops.type = type;
9583 ops.op0 = treeop0;
9584 ops.op1 = treeop1;
9585 ops.op2 = treeop2;
9586 ops.location = loc;
9588 ignore = (target == const0_rtx
9589 || ((CONVERT_EXPR_CODE_P (code)
9590 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9591 && TREE_CODE (type) == VOID_TYPE));
9593 /* An operation in what may be a bit-field type needs the
9594 result to be reduced to the precision of the bit-field type,
9595 which is narrower than that of the type's mode. */
9596 reduce_bit_field = (!ignore
9597 && INTEGRAL_TYPE_P (type)
9598 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9600 /* If we are going to ignore this result, we need only do something
9601 if there is a side-effect somewhere in the expression. If there
9602 is, short-circuit the most common cases here. Note that we must
9603 not call expand_expr with anything but const0_rtx in case this
9604 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9606 if (ignore)
9608 if (! TREE_SIDE_EFFECTS (exp))
9609 return const0_rtx;
9611 /* Ensure we reference a volatile object even if value is ignored, but
9612 don't do this if all we are doing is taking its address. */
9613 if (TREE_THIS_VOLATILE (exp)
9614 && TREE_CODE (exp) != FUNCTION_DECL
9615 && mode != VOIDmode && mode != BLKmode
9616 && modifier != EXPAND_CONST_ADDRESS)
9618 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9619 if (MEM_P (temp))
9620 copy_to_reg (temp);
9621 return const0_rtx;
9624 if (TREE_CODE_CLASS (code) == tcc_unary
9625 || code == BIT_FIELD_REF
9626 || code == COMPONENT_REF
9627 || code == INDIRECT_REF)
9628 return expand_expr (treeop0, const0_rtx, VOIDmode,
9629 modifier);
9631 else if (TREE_CODE_CLASS (code) == tcc_binary
9632 || TREE_CODE_CLASS (code) == tcc_comparison
9633 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9635 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9636 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9637 return const0_rtx;
9640 target = 0;
9643 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9644 target = 0;
9646 /* Use subtarget as the target for operand 0 of a binary operation. */
9647 subtarget = get_subtarget (target);
9648 original_target = target;
9650 switch (code)
9652 case LABEL_DECL:
9654 tree function = decl_function_context (exp);
9656 temp = label_rtx (exp);
9657 temp = gen_rtx_LABEL_REF (Pmode, temp);
9659 if (function != current_function_decl
9660 && function != 0)
9661 LABEL_REF_NONLOCAL_P (temp) = 1;
9663 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9664 return temp;
9667 case SSA_NAME:
9668 /* ??? ivopts calls expander, without any preparation from
9669 out-of-ssa. So fake instructions as if this was an access to the
9670 base variable. This unnecessarily allocates a pseudo, see how we can
9671 reuse it, if partition base vars have it set already. */
9672 if (!currently_expanding_to_rtl)
9674 tree var = SSA_NAME_VAR (exp);
9675 if (var && DECL_RTL_SET_P (var))
9676 return DECL_RTL (var);
9677 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9678 LAST_VIRTUAL_REGISTER + 1);
9681 g = get_gimple_for_ssa_name (exp);
9682 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9683 if (g == NULL
9684 && modifier == EXPAND_INITIALIZER
9685 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9686 && (optimize || !SSA_NAME_VAR (exp)
9687 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9688 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9689 g = SSA_NAME_DEF_STMT (exp);
9690 if (g)
9692 rtx r;
9693 location_t saved_loc = curr_insn_location ();
9694 location_t loc = gimple_location (g);
9695 if (loc != UNKNOWN_LOCATION)
9696 set_curr_insn_location (loc);
9697 ops.code = gimple_assign_rhs_code (g);
9698 switch (get_gimple_rhs_class (ops.code))
9700 case GIMPLE_TERNARY_RHS:
9701 ops.op2 = gimple_assign_rhs3 (g);
9702 /* Fallthru */
9703 case GIMPLE_BINARY_RHS:
9704 ops.op1 = gimple_assign_rhs2 (g);
9706 /* Try to expand conditonal compare. */
9707 if (targetm.gen_ccmp_first)
9709 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9710 r = expand_ccmp_expr (g);
9711 if (r)
9712 break;
9714 /* Fallthru */
9715 case GIMPLE_UNARY_RHS:
9716 ops.op0 = gimple_assign_rhs1 (g);
9717 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9718 ops.location = loc;
9719 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9720 break;
9721 case GIMPLE_SINGLE_RHS:
9723 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9724 tmode, modifier, NULL, inner_reference_p);
9725 break;
9727 default:
9728 gcc_unreachable ();
9730 set_curr_insn_location (saved_loc);
9731 if (REG_P (r) && !REG_EXPR (r))
9732 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9733 return r;
9736 ssa_name = exp;
9737 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9738 exp = SSA_NAME_VAR (ssa_name);
9739 goto expand_decl_rtl;
9741 case PARM_DECL:
9742 case VAR_DECL:
9743 /* If a static var's type was incomplete when the decl was written,
9744 but the type is complete now, lay out the decl now. */
9745 if (DECL_SIZE (exp) == 0
9746 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9747 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9748 layout_decl (exp, 0);
9750 /* fall through */
9752 case FUNCTION_DECL:
9753 case RESULT_DECL:
9754 decl_rtl = DECL_RTL (exp);
9755 expand_decl_rtl:
9756 gcc_assert (decl_rtl);
9758 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9759 settings for VECTOR_TYPE_P that might switch for the function. */
9760 if (currently_expanding_to_rtl
9761 && code == VAR_DECL && MEM_P (decl_rtl)
9762 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9763 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9764 else
9765 decl_rtl = copy_rtx (decl_rtl);
9767 /* Record writes to register variables. */
9768 if (modifier == EXPAND_WRITE
9769 && REG_P (decl_rtl)
9770 && HARD_REGISTER_P (decl_rtl))
9771 add_to_hard_reg_set (&crtl->asm_clobbers,
9772 GET_MODE (decl_rtl), REGNO (decl_rtl));
9774 /* Ensure variable marked as used even if it doesn't go through
9775 a parser. If it hasn't be used yet, write out an external
9776 definition. */
9777 if (exp)
9778 TREE_USED (exp) = 1;
9780 /* Show we haven't gotten RTL for this yet. */
9781 temp = 0;
9783 /* Variables inherited from containing functions should have
9784 been lowered by this point. */
9785 if (exp)
9786 context = decl_function_context (exp);
9787 gcc_assert (!exp
9788 || SCOPE_FILE_SCOPE_P (context)
9789 || context == current_function_decl
9790 || TREE_STATIC (exp)
9791 || DECL_EXTERNAL (exp)
9792 /* ??? C++ creates functions that are not TREE_STATIC. */
9793 || TREE_CODE (exp) == FUNCTION_DECL);
9795 /* This is the case of an array whose size is to be determined
9796 from its initializer, while the initializer is still being parsed.
9797 ??? We aren't parsing while expanding anymore. */
9799 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9800 temp = validize_mem (decl_rtl);
9802 /* If DECL_RTL is memory, we are in the normal case and the
9803 address is not valid, get the address into a register. */
9805 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9807 if (alt_rtl)
9808 *alt_rtl = decl_rtl;
9809 decl_rtl = use_anchored_address (decl_rtl);
9810 if (modifier != EXPAND_CONST_ADDRESS
9811 && modifier != EXPAND_SUM
9812 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9813 : GET_MODE (decl_rtl),
9814 XEXP (decl_rtl, 0),
9815 MEM_ADDR_SPACE (decl_rtl)))
9816 temp = replace_equiv_address (decl_rtl,
9817 copy_rtx (XEXP (decl_rtl, 0)));
9820 /* If we got something, return it. But first, set the alignment
9821 if the address is a register. */
9822 if (temp != 0)
9824 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9825 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9827 return temp;
9830 if (exp)
9831 dmode = DECL_MODE (exp);
9832 else
9833 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9835 /* If the mode of DECL_RTL does not match that of the decl,
9836 there are two cases: we are dealing with a BLKmode value
9837 that is returned in a register, or we are dealing with
9838 a promoted value. In the latter case, return a SUBREG
9839 of the wanted mode, but mark it so that we know that it
9840 was already extended. */
9841 if (REG_P (decl_rtl)
9842 && dmode != BLKmode
9843 && GET_MODE (decl_rtl) != dmode)
9845 machine_mode pmode;
9847 /* Get the signedness to be used for this variable. Ensure we get
9848 the same mode we got when the variable was declared. */
9849 if (code != SSA_NAME)
9850 pmode = promote_decl_mode (exp, &unsignedp);
9851 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9852 && gimple_code (g) == GIMPLE_CALL
9853 && !gimple_call_internal_p (g))
9854 pmode = promote_function_mode (type, mode, &unsignedp,
9855 gimple_call_fntype (g),
9857 else
9858 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9859 gcc_assert (GET_MODE (decl_rtl) == pmode);
9861 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9862 SUBREG_PROMOTED_VAR_P (temp) = 1;
9863 SUBREG_PROMOTED_SET (temp, unsignedp);
9864 return temp;
9867 return decl_rtl;
9869 case INTEGER_CST:
9870 /* Given that TYPE_PRECISION (type) is not always equal to
9871 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9872 the former to the latter according to the signedness of the
9873 type. */
9874 temp = immed_wide_int_const (wi::to_wide
9875 (exp,
9876 GET_MODE_PRECISION (TYPE_MODE (type))),
9877 TYPE_MODE (type));
9878 return temp;
9880 case VECTOR_CST:
9882 tree tmp = NULL_TREE;
9883 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9884 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9885 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9886 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9887 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9888 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9889 return const_vector_from_tree (exp);
9890 if (GET_MODE_CLASS (mode) == MODE_INT)
9892 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9893 return const_scalar_mask_from_tree (exp);
9894 else
9896 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9897 if (type_for_mode)
9898 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9899 type_for_mode, exp);
9902 if (!tmp)
9904 vec<constructor_elt, va_gc> *v;
9905 unsigned i;
9906 vec_alloc (v, VECTOR_CST_NELTS (exp));
9907 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9908 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9909 tmp = build_constructor (type, v);
9911 return expand_expr (tmp, ignore ? const0_rtx : target,
9912 tmode, modifier);
9915 case CONST_DECL:
9916 if (modifier == EXPAND_WRITE)
9918 /* Writing into CONST_DECL is always invalid, but handle it
9919 gracefully. */
9920 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9921 machine_mode address_mode = targetm.addr_space.address_mode (as);
9922 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9923 EXPAND_NORMAL, as);
9924 op0 = memory_address_addr_space (mode, op0, as);
9925 temp = gen_rtx_MEM (mode, op0);
9926 set_mem_addr_space (temp, as);
9927 return temp;
9929 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9931 case REAL_CST:
9932 /* If optimized, generate immediate CONST_DOUBLE
9933 which will be turned into memory by reload if necessary.
9935 We used to force a register so that loop.c could see it. But
9936 this does not allow gen_* patterns to perform optimizations with
9937 the constants. It also produces two insns in cases like "x = 1.0;".
9938 On most machines, floating-point constants are not permitted in
9939 many insns, so we'd end up copying it to a register in any case.
9941 Now, we do the copying in expand_binop, if appropriate. */
9942 return const_double_from_real_value (TREE_REAL_CST (exp),
9943 TYPE_MODE (TREE_TYPE (exp)));
9945 case FIXED_CST:
9946 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9947 TYPE_MODE (TREE_TYPE (exp)));
9949 case COMPLEX_CST:
9950 /* Handle evaluating a complex constant in a CONCAT target. */
9951 if (original_target && GET_CODE (original_target) == CONCAT)
9953 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9954 rtx rtarg, itarg;
9956 rtarg = XEXP (original_target, 0);
9957 itarg = XEXP (original_target, 1);
9959 /* Move the real and imaginary parts separately. */
9960 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9961 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9963 if (op0 != rtarg)
9964 emit_move_insn (rtarg, op0);
9965 if (op1 != itarg)
9966 emit_move_insn (itarg, op1);
9968 return original_target;
9971 /* fall through */
9973 case STRING_CST:
9974 temp = expand_expr_constant (exp, 1, modifier);
9976 /* temp contains a constant address.
9977 On RISC machines where a constant address isn't valid,
9978 make some insns to get that address into a register. */
9979 if (modifier != EXPAND_CONST_ADDRESS
9980 && modifier != EXPAND_INITIALIZER
9981 && modifier != EXPAND_SUM
9982 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9983 MEM_ADDR_SPACE (temp)))
9984 return replace_equiv_address (temp,
9985 copy_rtx (XEXP (temp, 0)));
9986 return temp;
9988 case SAVE_EXPR:
9990 tree val = treeop0;
9991 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
9992 inner_reference_p);
9994 if (!SAVE_EXPR_RESOLVED_P (exp))
9996 /* We can indeed still hit this case, typically via builtin
9997 expanders calling save_expr immediately before expanding
9998 something. Assume this means that we only have to deal
9999 with non-BLKmode values. */
10000 gcc_assert (GET_MODE (ret) != BLKmode);
10002 val = build_decl (curr_insn_location (),
10003 VAR_DECL, NULL, TREE_TYPE (exp));
10004 DECL_ARTIFICIAL (val) = 1;
10005 DECL_IGNORED_P (val) = 1;
10006 treeop0 = val;
10007 TREE_OPERAND (exp, 0) = treeop0;
10008 SAVE_EXPR_RESOLVED_P (exp) = 1;
10010 if (!CONSTANT_P (ret))
10011 ret = copy_to_reg (ret);
10012 SET_DECL_RTL (val, ret);
10015 return ret;
10019 case CONSTRUCTOR:
10020 /* If we don't need the result, just ensure we evaluate any
10021 subexpressions. */
10022 if (ignore)
10024 unsigned HOST_WIDE_INT idx;
10025 tree value;
10027 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10028 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10030 return const0_rtx;
10033 return expand_constructor (exp, target, modifier, false);
10035 case TARGET_MEM_REF:
10037 addr_space_t as
10038 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10039 enum insn_code icode;
10040 unsigned int align;
10042 op0 = addr_for_mem_ref (exp, as, true);
10043 op0 = memory_address_addr_space (mode, op0, as);
10044 temp = gen_rtx_MEM (mode, op0);
10045 set_mem_attributes (temp, exp, 0);
10046 set_mem_addr_space (temp, as);
10047 align = get_object_alignment (exp);
10048 if (modifier != EXPAND_WRITE
10049 && modifier != EXPAND_MEMORY
10050 && mode != BLKmode
10051 && align < GET_MODE_ALIGNMENT (mode)
10052 /* If the target does not have special handling for unaligned
10053 loads of mode then it can use regular moves for them. */
10054 && ((icode = optab_handler (movmisalign_optab, mode))
10055 != CODE_FOR_nothing))
10057 struct expand_operand ops[2];
10059 /* We've already validated the memory, and we're creating a
10060 new pseudo destination. The predicates really can't fail,
10061 nor can the generator. */
10062 create_output_operand (&ops[0], NULL_RTX, mode);
10063 create_fixed_operand (&ops[1], temp);
10064 expand_insn (icode, 2, ops);
10065 temp = ops[0].value;
10067 return temp;
10070 case MEM_REF:
10072 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10073 addr_space_t as
10074 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10075 machine_mode address_mode;
10076 tree base = TREE_OPERAND (exp, 0);
10077 gimple *def_stmt;
10078 enum insn_code icode;
10079 unsigned align;
10080 /* Handle expansion of non-aliased memory with non-BLKmode. That
10081 might end up in a register. */
10082 if (mem_ref_refers_to_non_mem_p (exp))
10084 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10085 base = TREE_OPERAND (base, 0);
10086 if (offset == 0
10087 && !reverse
10088 && tree_fits_uhwi_p (TYPE_SIZE (type))
10089 && (GET_MODE_BITSIZE (DECL_MODE (base))
10090 == tree_to_uhwi (TYPE_SIZE (type))))
10091 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10092 target, tmode, modifier);
10093 if (TYPE_MODE (type) == BLKmode)
10095 temp = assign_stack_temp (DECL_MODE (base),
10096 GET_MODE_SIZE (DECL_MODE (base)));
10097 store_expr (base, temp, 0, false, false);
10098 temp = adjust_address (temp, BLKmode, offset);
10099 set_mem_size (temp, int_size_in_bytes (type));
10100 return temp;
10102 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10103 bitsize_int (offset * BITS_PER_UNIT));
10104 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10105 return expand_expr (exp, target, tmode, modifier);
10107 address_mode = targetm.addr_space.address_mode (as);
10108 base = TREE_OPERAND (exp, 0);
10109 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10111 tree mask = gimple_assign_rhs2 (def_stmt);
10112 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10113 gimple_assign_rhs1 (def_stmt), mask);
10114 TREE_OPERAND (exp, 0) = base;
10116 align = get_object_alignment (exp);
10117 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10118 op0 = memory_address_addr_space (mode, op0, as);
10119 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10121 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10122 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10123 op0 = memory_address_addr_space (mode, op0, as);
10125 temp = gen_rtx_MEM (mode, op0);
10126 set_mem_attributes (temp, exp, 0);
10127 set_mem_addr_space (temp, as);
10128 if (TREE_THIS_VOLATILE (exp))
10129 MEM_VOLATILE_P (temp) = 1;
10130 if (modifier != EXPAND_WRITE
10131 && modifier != EXPAND_MEMORY
10132 && !inner_reference_p
10133 && mode != BLKmode
10134 && align < GET_MODE_ALIGNMENT (mode))
10136 if ((icode = optab_handler (movmisalign_optab, mode))
10137 != CODE_FOR_nothing)
10139 struct expand_operand ops[2];
10141 /* We've already validated the memory, and we're creating a
10142 new pseudo destination. The predicates really can't fail,
10143 nor can the generator. */
10144 create_output_operand (&ops[0], NULL_RTX, mode);
10145 create_fixed_operand (&ops[1], temp);
10146 expand_insn (icode, 2, ops);
10147 temp = ops[0].value;
10149 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10150 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10151 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10152 (modifier == EXPAND_STACK_PARM
10153 ? NULL_RTX : target),
10154 mode, mode, false);
10156 if (reverse
10157 && modifier != EXPAND_MEMORY
10158 && modifier != EXPAND_WRITE)
10159 temp = flip_storage_order (mode, temp);
10160 return temp;
10163 case ARRAY_REF:
10166 tree array = treeop0;
10167 tree index = treeop1;
10168 tree init;
10170 /* Fold an expression like: "foo"[2].
10171 This is not done in fold so it won't happen inside &.
10172 Don't fold if this is for wide characters since it's too
10173 difficult to do correctly and this is a very rare case. */
10175 if (modifier != EXPAND_CONST_ADDRESS
10176 && modifier != EXPAND_INITIALIZER
10177 && modifier != EXPAND_MEMORY)
10179 tree t = fold_read_from_constant_string (exp);
10181 if (t)
10182 return expand_expr (t, target, tmode, modifier);
10185 /* If this is a constant index into a constant array,
10186 just get the value from the array. Handle both the cases when
10187 we have an explicit constructor and when our operand is a variable
10188 that was declared const. */
10190 if (modifier != EXPAND_CONST_ADDRESS
10191 && modifier != EXPAND_INITIALIZER
10192 && modifier != EXPAND_MEMORY
10193 && TREE_CODE (array) == CONSTRUCTOR
10194 && ! TREE_SIDE_EFFECTS (array)
10195 && TREE_CODE (index) == INTEGER_CST)
10197 unsigned HOST_WIDE_INT ix;
10198 tree field, value;
10200 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10201 field, value)
10202 if (tree_int_cst_equal (field, index))
10204 if (!TREE_SIDE_EFFECTS (value))
10205 return expand_expr (fold (value), target, tmode, modifier);
10206 break;
10210 else if (optimize >= 1
10211 && modifier != EXPAND_CONST_ADDRESS
10212 && modifier != EXPAND_INITIALIZER
10213 && modifier != EXPAND_MEMORY
10214 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10215 && TREE_CODE (index) == INTEGER_CST
10216 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10217 && (init = ctor_for_folding (array)) != error_mark_node)
10219 if (init == NULL_TREE)
10221 tree value = build_zero_cst (type);
10222 if (TREE_CODE (value) == CONSTRUCTOR)
10224 /* If VALUE is a CONSTRUCTOR, this optimization is only
10225 useful if this doesn't store the CONSTRUCTOR into
10226 memory. If it does, it is more efficient to just
10227 load the data from the array directly. */
10228 rtx ret = expand_constructor (value, target,
10229 modifier, true);
10230 if (ret == NULL_RTX)
10231 value = NULL_TREE;
10234 if (value)
10235 return expand_expr (value, target, tmode, modifier);
10237 else if (TREE_CODE (init) == CONSTRUCTOR)
10239 unsigned HOST_WIDE_INT ix;
10240 tree field, value;
10242 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10243 field, value)
10244 if (tree_int_cst_equal (field, index))
10246 if (TREE_SIDE_EFFECTS (value))
10247 break;
10249 if (TREE_CODE (value) == CONSTRUCTOR)
10251 /* If VALUE is a CONSTRUCTOR, this
10252 optimization is only useful if
10253 this doesn't store the CONSTRUCTOR
10254 into memory. If it does, it is more
10255 efficient to just load the data from
10256 the array directly. */
10257 rtx ret = expand_constructor (value, target,
10258 modifier, true);
10259 if (ret == NULL_RTX)
10260 break;
10263 return
10264 expand_expr (fold (value), target, tmode, modifier);
10267 else if (TREE_CODE (init) == STRING_CST)
10269 tree low_bound = array_ref_low_bound (exp);
10270 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10272 /* Optimize the special case of a zero lower bound.
10274 We convert the lower bound to sizetype to avoid problems
10275 with constant folding. E.g. suppose the lower bound is
10276 1 and its mode is QI. Without the conversion
10277 (ARRAY + (INDEX - (unsigned char)1))
10278 becomes
10279 (ARRAY + (-(unsigned char)1) + INDEX)
10280 which becomes
10281 (ARRAY + 255 + INDEX). Oops! */
10282 if (!integer_zerop (low_bound))
10283 index1 = size_diffop_loc (loc, index1,
10284 fold_convert_loc (loc, sizetype,
10285 low_bound));
10287 if (tree_fits_uhwi_p (index1)
10288 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10290 tree type = TREE_TYPE (TREE_TYPE (init));
10291 machine_mode mode = TYPE_MODE (type);
10293 if (GET_MODE_CLASS (mode) == MODE_INT
10294 && GET_MODE_SIZE (mode) == 1)
10295 return gen_int_mode (TREE_STRING_POINTER (init)
10296 [TREE_INT_CST_LOW (index1)],
10297 mode);
10302 goto normal_inner_ref;
10304 case COMPONENT_REF:
10305 /* If the operand is a CONSTRUCTOR, we can just extract the
10306 appropriate field if it is present. */
10307 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10309 unsigned HOST_WIDE_INT idx;
10310 tree field, value;
10312 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10313 idx, field, value)
10314 if (field == treeop1
10315 /* We can normally use the value of the field in the
10316 CONSTRUCTOR. However, if this is a bitfield in
10317 an integral mode that we can fit in a HOST_WIDE_INT,
10318 we must mask only the number of bits in the bitfield,
10319 since this is done implicitly by the constructor. If
10320 the bitfield does not meet either of those conditions,
10321 we can't do this optimization. */
10322 && (! DECL_BIT_FIELD (field)
10323 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10324 && (GET_MODE_PRECISION (DECL_MODE (field))
10325 <= HOST_BITS_PER_WIDE_INT))))
10327 if (DECL_BIT_FIELD (field)
10328 && modifier == EXPAND_STACK_PARM)
10329 target = 0;
10330 op0 = expand_expr (value, target, tmode, modifier);
10331 if (DECL_BIT_FIELD (field))
10333 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10334 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10336 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10338 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10339 imode);
10340 op0 = expand_and (imode, op0, op1, target);
10342 else
10344 int count = GET_MODE_PRECISION (imode) - bitsize;
10346 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10347 target, 0);
10348 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10349 target, 0);
10353 return op0;
10356 goto normal_inner_ref;
10358 case BIT_FIELD_REF:
10359 case ARRAY_RANGE_REF:
10360 normal_inner_ref:
10362 machine_mode mode1, mode2;
10363 HOST_WIDE_INT bitsize, bitpos;
10364 tree offset;
10365 int reversep, volatilep = 0, must_force_mem;
10366 tree tem
10367 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10368 &unsignedp, &reversep, &volatilep);
10369 rtx orig_op0, memloc;
10370 bool clear_mem_expr = false;
10372 /* If we got back the original object, something is wrong. Perhaps
10373 we are evaluating an expression too early. In any event, don't
10374 infinitely recurse. */
10375 gcc_assert (tem != exp);
10377 /* If TEM's type is a union of variable size, pass TARGET to the inner
10378 computation, since it will need a temporary and TARGET is known
10379 to have to do. This occurs in unchecked conversion in Ada. */
10380 orig_op0 = op0
10381 = expand_expr_real (tem,
10382 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10383 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10384 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10385 != INTEGER_CST)
10386 && modifier != EXPAND_STACK_PARM
10387 ? target : NULL_RTX),
10388 VOIDmode,
10389 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10390 NULL, true);
10392 /* If the field has a mode, we want to access it in the
10393 field's mode, not the computed mode.
10394 If a MEM has VOIDmode (external with incomplete type),
10395 use BLKmode for it instead. */
10396 if (MEM_P (op0))
10398 if (mode1 != VOIDmode)
10399 op0 = adjust_address (op0, mode1, 0);
10400 else if (GET_MODE (op0) == VOIDmode)
10401 op0 = adjust_address (op0, BLKmode, 0);
10404 mode2
10405 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10407 /* If we have either an offset, a BLKmode result, or a reference
10408 outside the underlying object, we must force it to memory.
10409 Such a case can occur in Ada if we have unchecked conversion
10410 of an expression from a scalar type to an aggregate type or
10411 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10412 passed a partially uninitialized object or a view-conversion
10413 to a larger size. */
10414 must_force_mem = (offset
10415 || mode1 == BLKmode
10416 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10418 /* Handle CONCAT first. */
10419 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10421 if (bitpos == 0
10422 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10423 && COMPLEX_MODE_P (mode1)
10424 && COMPLEX_MODE_P (GET_MODE (op0))
10425 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10426 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10428 if (reversep)
10429 op0 = flip_storage_order (GET_MODE (op0), op0);
10430 if (mode1 != GET_MODE (op0))
10432 rtx parts[2];
10433 for (int i = 0; i < 2; i++)
10435 rtx op = read_complex_part (op0, i != 0);
10436 if (GET_CODE (op) == SUBREG)
10437 op = force_reg (GET_MODE (op), op);
10438 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10439 op);
10440 if (temp)
10441 op = temp;
10442 else
10444 if (!REG_P (op) && !MEM_P (op))
10445 op = force_reg (GET_MODE (op), op);
10446 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10448 parts[i] = op;
10450 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10452 return op0;
10454 if (bitpos == 0
10455 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10456 && bitsize)
10458 op0 = XEXP (op0, 0);
10459 mode2 = GET_MODE (op0);
10461 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10462 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10463 && bitpos
10464 && bitsize)
10466 op0 = XEXP (op0, 1);
10467 bitpos = 0;
10468 mode2 = GET_MODE (op0);
10470 else
10471 /* Otherwise force into memory. */
10472 must_force_mem = 1;
10475 /* If this is a constant, put it in a register if it is a legitimate
10476 constant and we don't need a memory reference. */
10477 if (CONSTANT_P (op0)
10478 && mode2 != BLKmode
10479 && targetm.legitimate_constant_p (mode2, op0)
10480 && !must_force_mem)
10481 op0 = force_reg (mode2, op0);
10483 /* Otherwise, if this is a constant, try to force it to the constant
10484 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10485 is a legitimate constant. */
10486 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10487 op0 = validize_mem (memloc);
10489 /* Otherwise, if this is a constant or the object is not in memory
10490 and need be, put it there. */
10491 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10493 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10494 emit_move_insn (memloc, op0);
10495 op0 = memloc;
10496 clear_mem_expr = true;
10499 if (offset)
10501 machine_mode address_mode;
10502 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10503 EXPAND_SUM);
10505 gcc_assert (MEM_P (op0));
10507 address_mode = get_address_mode (op0);
10508 if (GET_MODE (offset_rtx) != address_mode)
10510 /* We cannot be sure that the RTL in offset_rtx is valid outside
10511 of a memory address context, so force it into a register
10512 before attempting to convert it to the desired mode. */
10513 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10514 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10517 /* See the comment in expand_assignment for the rationale. */
10518 if (mode1 != VOIDmode
10519 && bitpos != 0
10520 && bitsize > 0
10521 && (bitpos % bitsize) == 0
10522 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10523 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10525 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10526 bitpos = 0;
10529 op0 = offset_address (op0, offset_rtx,
10530 highest_pow2_factor (offset));
10533 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10534 record its alignment as BIGGEST_ALIGNMENT. */
10535 if (MEM_P (op0) && bitpos == 0 && offset != 0
10536 && is_aligning_offset (offset, tem))
10537 set_mem_align (op0, BIGGEST_ALIGNMENT);
10539 /* Don't forget about volatility even if this is a bitfield. */
10540 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10542 if (op0 == orig_op0)
10543 op0 = copy_rtx (op0);
10545 MEM_VOLATILE_P (op0) = 1;
10548 /* In cases where an aligned union has an unaligned object
10549 as a field, we might be extracting a BLKmode value from
10550 an integer-mode (e.g., SImode) object. Handle this case
10551 by doing the extract into an object as wide as the field
10552 (which we know to be the width of a basic mode), then
10553 storing into memory, and changing the mode to BLKmode. */
10554 if (mode1 == VOIDmode
10555 || REG_P (op0) || GET_CODE (op0) == SUBREG
10556 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10557 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10558 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10559 && modifier != EXPAND_CONST_ADDRESS
10560 && modifier != EXPAND_INITIALIZER
10561 && modifier != EXPAND_MEMORY)
10562 /* If the bitfield is volatile and the bitsize
10563 is narrower than the access size of the bitfield,
10564 we need to extract bitfields from the access. */
10565 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10566 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10567 && mode1 != BLKmode
10568 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10569 /* If the field isn't aligned enough to fetch as a memref,
10570 fetch it as a bit field. */
10571 || (mode1 != BLKmode
10572 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10573 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10574 || (MEM_P (op0)
10575 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10576 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10577 && modifier != EXPAND_MEMORY
10578 && ((modifier == EXPAND_CONST_ADDRESS
10579 || modifier == EXPAND_INITIALIZER)
10580 ? STRICT_ALIGNMENT
10581 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10582 || (bitpos % BITS_PER_UNIT != 0)))
10583 /* If the type and the field are a constant size and the
10584 size of the type isn't the same size as the bitfield,
10585 we must use bitfield operations. */
10586 || (bitsize >= 0
10587 && TYPE_SIZE (TREE_TYPE (exp))
10588 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10589 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10590 bitsize)))
10592 machine_mode ext_mode = mode;
10594 if (ext_mode == BLKmode
10595 && ! (target != 0 && MEM_P (op0)
10596 && MEM_P (target)
10597 && bitpos % BITS_PER_UNIT == 0))
10598 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10600 if (ext_mode == BLKmode)
10602 if (target == 0)
10603 target = assign_temp (type, 1, 1);
10605 /* ??? Unlike the similar test a few lines below, this one is
10606 very likely obsolete. */
10607 if (bitsize == 0)
10608 return target;
10610 /* In this case, BITPOS must start at a byte boundary and
10611 TARGET, if specified, must be a MEM. */
10612 gcc_assert (MEM_P (op0)
10613 && (!target || MEM_P (target))
10614 && !(bitpos % BITS_PER_UNIT));
10616 emit_block_move (target,
10617 adjust_address (op0, VOIDmode,
10618 bitpos / BITS_PER_UNIT),
10619 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10620 / BITS_PER_UNIT),
10621 (modifier == EXPAND_STACK_PARM
10622 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10624 return target;
10627 /* If we have nothing to extract, the result will be 0 for targets
10628 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10629 return 0 for the sake of consistency, as reading a zero-sized
10630 bitfield is valid in Ada and the value is fully specified. */
10631 if (bitsize == 0)
10632 return const0_rtx;
10634 op0 = validize_mem (op0);
10636 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10637 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10639 /* If the result has a record type and the extraction is done in
10640 an integral mode, then the field may be not aligned on a byte
10641 boundary; in this case, if it has reverse storage order, it
10642 needs to be extracted as a scalar field with reverse storage
10643 order and put back into memory order afterwards. */
10644 if (TREE_CODE (type) == RECORD_TYPE
10645 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10646 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10648 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10649 (modifier == EXPAND_STACK_PARM
10650 ? NULL_RTX : target),
10651 ext_mode, ext_mode, reversep);
10653 /* If the result has a record type and the mode of OP0 is an
10654 integral mode then, if BITSIZE is narrower than this mode
10655 and this is for big-endian data, we must put the field
10656 into the high-order bits. And we must also put it back
10657 into memory order if it has been previously reversed. */
10658 if (TREE_CODE (type) == RECORD_TYPE
10659 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
10661 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (op0));
10663 if (bitsize < size
10664 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10665 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10666 size - bitsize, op0, 1);
10668 if (reversep)
10669 op0 = flip_storage_order (GET_MODE (op0), op0);
10672 /* If the result type is BLKmode, store the data into a temporary
10673 of the appropriate type, but with the mode corresponding to the
10674 mode for the data we have (op0's mode). */
10675 if (mode == BLKmode)
10677 rtx new_rtx
10678 = assign_stack_temp_for_type (ext_mode,
10679 GET_MODE_BITSIZE (ext_mode),
10680 type);
10681 emit_move_insn (new_rtx, op0);
10682 op0 = copy_rtx (new_rtx);
10683 PUT_MODE (op0, BLKmode);
10686 return op0;
10689 /* If the result is BLKmode, use that to access the object
10690 now as well. */
10691 if (mode == BLKmode)
10692 mode1 = BLKmode;
10694 /* Get a reference to just this component. */
10695 if (modifier == EXPAND_CONST_ADDRESS
10696 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10697 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10698 else
10699 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10701 if (op0 == orig_op0)
10702 op0 = copy_rtx (op0);
10704 /* Don't set memory attributes if the base expression is
10705 SSA_NAME that got expanded as a MEM. In that case, we should
10706 just honor its original memory attributes. */
10707 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10708 set_mem_attributes (op0, exp, 0);
10710 if (REG_P (XEXP (op0, 0)))
10711 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10713 /* If op0 is a temporary because the original expressions was forced
10714 to memory, clear MEM_EXPR so that the original expression cannot
10715 be marked as addressable through MEM_EXPR of the temporary. */
10716 if (clear_mem_expr)
10717 set_mem_expr (op0, NULL_TREE);
10719 MEM_VOLATILE_P (op0) |= volatilep;
10721 if (reversep
10722 && modifier != EXPAND_MEMORY
10723 && modifier != EXPAND_WRITE)
10724 op0 = flip_storage_order (mode1, op0);
10726 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10727 || modifier == EXPAND_CONST_ADDRESS
10728 || modifier == EXPAND_INITIALIZER)
10729 return op0;
10731 if (target == 0)
10732 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10734 convert_move (target, op0, unsignedp);
10735 return target;
10738 case OBJ_TYPE_REF:
10739 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10741 case CALL_EXPR:
10742 /* All valid uses of __builtin_va_arg_pack () are removed during
10743 inlining. */
10744 if (CALL_EXPR_VA_ARG_PACK (exp))
10745 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10747 tree fndecl = get_callee_fndecl (exp), attr;
10749 if (fndecl
10750 && (attr = lookup_attribute ("error",
10751 DECL_ATTRIBUTES (fndecl))) != NULL)
10752 error ("%Kcall to %qs declared with attribute error: %s",
10753 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10754 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10755 if (fndecl
10756 && (attr = lookup_attribute ("warning",
10757 DECL_ATTRIBUTES (fndecl))) != NULL)
10758 warning_at (tree_nonartificial_location (exp),
10759 0, "%Kcall to %qs declared with attribute warning: %s",
10760 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10761 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10763 /* Check for a built-in function. */
10764 if (fndecl && DECL_BUILT_IN (fndecl))
10766 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10767 if (CALL_WITH_BOUNDS_P (exp))
10768 return expand_builtin_with_bounds (exp, target, subtarget,
10769 tmode, ignore);
10770 else
10771 return expand_builtin (exp, target, subtarget, tmode, ignore);
10774 return expand_call (exp, target, ignore);
10776 case VIEW_CONVERT_EXPR:
10777 op0 = NULL_RTX;
10779 /* If we are converting to BLKmode, try to avoid an intermediate
10780 temporary by fetching an inner memory reference. */
10781 if (mode == BLKmode
10782 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10783 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10784 && handled_component_p (treeop0))
10786 machine_mode mode1;
10787 HOST_WIDE_INT bitsize, bitpos;
10788 tree offset;
10789 int unsignedp, reversep, volatilep = 0;
10790 tree tem
10791 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10792 &unsignedp, &reversep, &volatilep);
10793 rtx orig_op0;
10795 /* ??? We should work harder and deal with non-zero offsets. */
10796 if (!offset
10797 && (bitpos % BITS_PER_UNIT) == 0
10798 && !reversep
10799 && bitsize >= 0
10800 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10802 /* See the normal_inner_ref case for the rationale. */
10803 orig_op0
10804 = expand_expr_real (tem,
10805 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10806 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10807 != INTEGER_CST)
10808 && modifier != EXPAND_STACK_PARM
10809 ? target : NULL_RTX),
10810 VOIDmode,
10811 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10812 NULL, true);
10814 if (MEM_P (orig_op0))
10816 op0 = orig_op0;
10818 /* Get a reference to just this component. */
10819 if (modifier == EXPAND_CONST_ADDRESS
10820 || modifier == EXPAND_SUM
10821 || modifier == EXPAND_INITIALIZER)
10822 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10823 else
10824 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10826 if (op0 == orig_op0)
10827 op0 = copy_rtx (op0);
10829 set_mem_attributes (op0, treeop0, 0);
10830 if (REG_P (XEXP (op0, 0)))
10831 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10833 MEM_VOLATILE_P (op0) |= volatilep;
10838 if (!op0)
10839 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10840 NULL, inner_reference_p);
10842 /* If the input and output modes are both the same, we are done. */
10843 if (mode == GET_MODE (op0))
10845 /* If neither mode is BLKmode, and both modes are the same size
10846 then we can use gen_lowpart. */
10847 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10848 && (GET_MODE_PRECISION (mode)
10849 == GET_MODE_PRECISION (GET_MODE (op0)))
10850 && !COMPLEX_MODE_P (GET_MODE (op0)))
10852 if (GET_CODE (op0) == SUBREG)
10853 op0 = force_reg (GET_MODE (op0), op0);
10854 temp = gen_lowpart_common (mode, op0);
10855 if (temp)
10856 op0 = temp;
10857 else
10859 if (!REG_P (op0) && !MEM_P (op0))
10860 op0 = force_reg (GET_MODE (op0), op0);
10861 op0 = gen_lowpart (mode, op0);
10864 /* If both types are integral, convert from one mode to the other. */
10865 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10866 op0 = convert_modes (mode, GET_MODE (op0), op0,
10867 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10868 /* If the output type is a bit-field type, do an extraction. */
10869 else if (reduce_bit_field)
10870 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10871 TYPE_UNSIGNED (type), NULL_RTX,
10872 mode, mode, false);
10873 /* As a last resort, spill op0 to memory, and reload it in a
10874 different mode. */
10875 else if (!MEM_P (op0))
10877 /* If the operand is not a MEM, force it into memory. Since we
10878 are going to be changing the mode of the MEM, don't call
10879 force_const_mem for constants because we don't allow pool
10880 constants to change mode. */
10881 tree inner_type = TREE_TYPE (treeop0);
10883 gcc_assert (!TREE_ADDRESSABLE (exp));
10885 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10886 target
10887 = assign_stack_temp_for_type
10888 (TYPE_MODE (inner_type),
10889 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10891 emit_move_insn (target, op0);
10892 op0 = target;
10895 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10896 output type is such that the operand is known to be aligned, indicate
10897 that it is. Otherwise, we need only be concerned about alignment for
10898 non-BLKmode results. */
10899 if (MEM_P (op0))
10901 enum insn_code icode;
10903 if (modifier != EXPAND_WRITE
10904 && modifier != EXPAND_MEMORY
10905 && !inner_reference_p
10906 && mode != BLKmode
10907 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10909 /* If the target does have special handling for unaligned
10910 loads of mode then use them. */
10911 if ((icode = optab_handler (movmisalign_optab, mode))
10912 != CODE_FOR_nothing)
10914 rtx reg;
10916 op0 = adjust_address (op0, mode, 0);
10917 /* We've already validated the memory, and we're creating a
10918 new pseudo destination. The predicates really can't
10919 fail. */
10920 reg = gen_reg_rtx (mode);
10922 /* Nor can the insn generator. */
10923 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10924 emit_insn (insn);
10925 return reg;
10927 else if (STRICT_ALIGNMENT)
10929 tree inner_type = TREE_TYPE (treeop0);
10930 HOST_WIDE_INT temp_size
10931 = MAX (int_size_in_bytes (inner_type),
10932 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10933 rtx new_rtx
10934 = assign_stack_temp_for_type (mode, temp_size, type);
10935 rtx new_with_op0_mode
10936 = adjust_address (new_rtx, GET_MODE (op0), 0);
10938 gcc_assert (!TREE_ADDRESSABLE (exp));
10940 if (GET_MODE (op0) == BLKmode)
10941 emit_block_move (new_with_op0_mode, op0,
10942 GEN_INT (GET_MODE_SIZE (mode)),
10943 (modifier == EXPAND_STACK_PARM
10944 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10945 else
10946 emit_move_insn (new_with_op0_mode, op0);
10948 op0 = new_rtx;
10952 op0 = adjust_address (op0, mode, 0);
10955 return op0;
10957 case MODIFY_EXPR:
10959 tree lhs = treeop0;
10960 tree rhs = treeop1;
10961 gcc_assert (ignore);
10963 /* Check for |= or &= of a bitfield of size one into another bitfield
10964 of size 1. In this case, (unless we need the result of the
10965 assignment) we can do this more efficiently with a
10966 test followed by an assignment, if necessary.
10968 ??? At this point, we can't get a BIT_FIELD_REF here. But if
10969 things change so we do, this code should be enhanced to
10970 support it. */
10971 if (TREE_CODE (lhs) == COMPONENT_REF
10972 && (TREE_CODE (rhs) == BIT_IOR_EXPR
10973 || TREE_CODE (rhs) == BIT_AND_EXPR)
10974 && TREE_OPERAND (rhs, 0) == lhs
10975 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10976 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10977 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10979 rtx_code_label *label = gen_label_rtx ();
10980 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10981 do_jump (TREE_OPERAND (rhs, 1),
10982 value ? label : 0,
10983 value ? 0 : label, -1);
10984 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10985 false);
10986 do_pending_stack_adjust ();
10987 emit_label (label);
10988 return const0_rtx;
10991 expand_assignment (lhs, rhs, false);
10992 return const0_rtx;
10995 case ADDR_EXPR:
10996 return expand_expr_addr_expr (exp, target, tmode, modifier);
10998 case REALPART_EXPR:
10999 op0 = expand_normal (treeop0);
11000 return read_complex_part (op0, false);
11002 case IMAGPART_EXPR:
11003 op0 = expand_normal (treeop0);
11004 return read_complex_part (op0, true);
11006 case RETURN_EXPR:
11007 case LABEL_EXPR:
11008 case GOTO_EXPR:
11009 case SWITCH_EXPR:
11010 case ASM_EXPR:
11011 /* Expanded in cfgexpand.c. */
11012 gcc_unreachable ();
11014 case TRY_CATCH_EXPR:
11015 case CATCH_EXPR:
11016 case EH_FILTER_EXPR:
11017 case TRY_FINALLY_EXPR:
11018 /* Lowered by tree-eh.c. */
11019 gcc_unreachable ();
11021 case WITH_CLEANUP_EXPR:
11022 case CLEANUP_POINT_EXPR:
11023 case TARGET_EXPR:
11024 case CASE_LABEL_EXPR:
11025 case VA_ARG_EXPR:
11026 case BIND_EXPR:
11027 case INIT_EXPR:
11028 case CONJ_EXPR:
11029 case COMPOUND_EXPR:
11030 case PREINCREMENT_EXPR:
11031 case PREDECREMENT_EXPR:
11032 case POSTINCREMENT_EXPR:
11033 case POSTDECREMENT_EXPR:
11034 case LOOP_EXPR:
11035 case EXIT_EXPR:
11036 case COMPOUND_LITERAL_EXPR:
11037 /* Lowered by gimplify.c. */
11038 gcc_unreachable ();
11040 case FDESC_EXPR:
11041 /* Function descriptors are not valid except for as
11042 initialization constants, and should not be expanded. */
11043 gcc_unreachable ();
11045 case WITH_SIZE_EXPR:
11046 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11047 have pulled out the size to use in whatever context it needed. */
11048 return expand_expr_real (treeop0, original_target, tmode,
11049 modifier, alt_rtl, inner_reference_p);
11051 default:
11052 return expand_expr_real_2 (&ops, target, tmode, modifier);
11056 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11057 signedness of TYPE), possibly returning the result in TARGET. */
11058 static rtx
11059 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11061 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11062 if (target && GET_MODE (target) != GET_MODE (exp))
11063 target = 0;
11064 /* For constant values, reduce using build_int_cst_type. */
11065 if (CONST_INT_P (exp))
11067 HOST_WIDE_INT value = INTVAL (exp);
11068 tree t = build_int_cst_type (type, value);
11069 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11071 else if (TYPE_UNSIGNED (type))
11073 machine_mode mode = GET_MODE (exp);
11074 rtx mask = immed_wide_int_const
11075 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11076 return expand_and (mode, exp, mask, target);
11078 else
11080 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11081 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11082 exp, count, target, 0);
11083 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11084 exp, count, target, 0);
11088 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11089 when applied to the address of EXP produces an address known to be
11090 aligned more than BIGGEST_ALIGNMENT. */
11092 static int
11093 is_aligning_offset (const_tree offset, const_tree exp)
11095 /* Strip off any conversions. */
11096 while (CONVERT_EXPR_P (offset))
11097 offset = TREE_OPERAND (offset, 0);
11099 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11100 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11101 if (TREE_CODE (offset) != BIT_AND_EXPR
11102 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11103 || compare_tree_int (TREE_OPERAND (offset, 1),
11104 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11105 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11106 return 0;
11108 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11109 It must be NEGATE_EXPR. Then strip any more conversions. */
11110 offset = TREE_OPERAND (offset, 0);
11111 while (CONVERT_EXPR_P (offset))
11112 offset = TREE_OPERAND (offset, 0);
11114 if (TREE_CODE (offset) != NEGATE_EXPR)
11115 return 0;
11117 offset = TREE_OPERAND (offset, 0);
11118 while (CONVERT_EXPR_P (offset))
11119 offset = TREE_OPERAND (offset, 0);
11121 /* This must now be the address of EXP. */
11122 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11125 /* Return the tree node if an ARG corresponds to a string constant or zero
11126 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11127 in bytes within the string that ARG is accessing. The type of the
11128 offset will be `sizetype'. */
11130 tree
11131 string_constant (tree arg, tree *ptr_offset)
11133 tree array, offset, lower_bound;
11134 STRIP_NOPS (arg);
11136 if (TREE_CODE (arg) == ADDR_EXPR)
11138 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11140 *ptr_offset = size_zero_node;
11141 return TREE_OPERAND (arg, 0);
11143 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11145 array = TREE_OPERAND (arg, 0);
11146 offset = size_zero_node;
11148 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11150 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11151 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11152 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11153 return 0;
11155 /* Check if the array has a nonzero lower bound. */
11156 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11157 if (!integer_zerop (lower_bound))
11159 /* If the offset and base aren't both constants, return 0. */
11160 if (TREE_CODE (lower_bound) != INTEGER_CST)
11161 return 0;
11162 if (TREE_CODE (offset) != INTEGER_CST)
11163 return 0;
11164 /* Adjust offset by the lower bound. */
11165 offset = size_diffop (fold_convert (sizetype, offset),
11166 fold_convert (sizetype, lower_bound));
11169 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11171 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11172 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11173 if (TREE_CODE (array) != ADDR_EXPR)
11174 return 0;
11175 array = TREE_OPERAND (array, 0);
11176 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11177 return 0;
11179 else
11180 return 0;
11182 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11184 tree arg0 = TREE_OPERAND (arg, 0);
11185 tree arg1 = TREE_OPERAND (arg, 1);
11187 STRIP_NOPS (arg0);
11188 STRIP_NOPS (arg1);
11190 if (TREE_CODE (arg0) == ADDR_EXPR
11191 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11192 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11194 array = TREE_OPERAND (arg0, 0);
11195 offset = arg1;
11197 else if (TREE_CODE (arg1) == ADDR_EXPR
11198 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11199 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11201 array = TREE_OPERAND (arg1, 0);
11202 offset = arg0;
11204 else
11205 return 0;
11207 else
11208 return 0;
11210 if (TREE_CODE (array) == STRING_CST)
11212 *ptr_offset = fold_convert (sizetype, offset);
11213 return array;
11215 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11217 int length;
11218 tree init = ctor_for_folding (array);
11220 /* Variables initialized to string literals can be handled too. */
11221 if (init == error_mark_node
11222 || !init
11223 || TREE_CODE (init) != STRING_CST)
11224 return 0;
11226 /* Avoid const char foo[4] = "abcde"; */
11227 if (DECL_SIZE_UNIT (array) == NULL_TREE
11228 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11229 || (length = TREE_STRING_LENGTH (init)) <= 0
11230 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11231 return 0;
11233 /* If variable is bigger than the string literal, OFFSET must be constant
11234 and inside of the bounds of the string literal. */
11235 offset = fold_convert (sizetype, offset);
11236 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11237 && (! tree_fits_uhwi_p (offset)
11238 || compare_tree_int (offset, length) >= 0))
11239 return 0;
11241 *ptr_offset = offset;
11242 return init;
11245 return 0;
11248 /* Generate code to calculate OPS, and exploded expression
11249 using a store-flag instruction and return an rtx for the result.
11250 OPS reflects a comparison.
11252 If TARGET is nonzero, store the result there if convenient.
11254 Return zero if there is no suitable set-flag instruction
11255 available on this machine.
11257 Once expand_expr has been called on the arguments of the comparison,
11258 we are committed to doing the store flag, since it is not safe to
11259 re-evaluate the expression. We emit the store-flag insn by calling
11260 emit_store_flag, but only expand the arguments if we have a reason
11261 to believe that emit_store_flag will be successful. If we think that
11262 it will, but it isn't, we have to simulate the store-flag with a
11263 set/jump/set sequence. */
11265 static rtx
11266 do_store_flag (sepops ops, rtx target, machine_mode mode)
11268 enum rtx_code code;
11269 tree arg0, arg1, type;
11270 machine_mode operand_mode;
11271 int unsignedp;
11272 rtx op0, op1;
11273 rtx subtarget = target;
11274 location_t loc = ops->location;
11276 arg0 = ops->op0;
11277 arg1 = ops->op1;
11279 /* Don't crash if the comparison was erroneous. */
11280 if (arg0 == error_mark_node || arg1 == error_mark_node)
11281 return const0_rtx;
11283 type = TREE_TYPE (arg0);
11284 operand_mode = TYPE_MODE (type);
11285 unsignedp = TYPE_UNSIGNED (type);
11287 /* We won't bother with BLKmode store-flag operations because it would mean
11288 passing a lot of information to emit_store_flag. */
11289 if (operand_mode == BLKmode)
11290 return 0;
11292 /* We won't bother with store-flag operations involving function pointers
11293 when function pointers must be canonicalized before comparisons. */
11294 if (targetm.have_canonicalize_funcptr_for_compare ()
11295 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11296 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11297 == FUNCTION_TYPE))
11298 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11299 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11300 == FUNCTION_TYPE))))
11301 return 0;
11303 STRIP_NOPS (arg0);
11304 STRIP_NOPS (arg1);
11306 /* For vector typed comparisons emit code to generate the desired
11307 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11308 expander for this. */
11309 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11311 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11312 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11313 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11314 return expand_vec_cmp_expr (ops->type, ifexp, target);
11315 else
11317 tree if_true = constant_boolean_node (true, ops->type);
11318 tree if_false = constant_boolean_node (false, ops->type);
11319 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11320 if_false, target);
11324 /* Get the rtx comparison code to use. We know that EXP is a comparison
11325 operation of some type. Some comparisons against 1 and -1 can be
11326 converted to comparisons with zero. Do so here so that the tests
11327 below will be aware that we have a comparison with zero. These
11328 tests will not catch constants in the first operand, but constants
11329 are rarely passed as the first operand. */
11331 switch (ops->code)
11333 case EQ_EXPR:
11334 code = EQ;
11335 break;
11336 case NE_EXPR:
11337 code = NE;
11338 break;
11339 case LT_EXPR:
11340 if (integer_onep (arg1))
11341 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11342 else
11343 code = unsignedp ? LTU : LT;
11344 break;
11345 case LE_EXPR:
11346 if (! unsignedp && integer_all_onesp (arg1))
11347 arg1 = integer_zero_node, code = LT;
11348 else
11349 code = unsignedp ? LEU : LE;
11350 break;
11351 case GT_EXPR:
11352 if (! unsignedp && integer_all_onesp (arg1))
11353 arg1 = integer_zero_node, code = GE;
11354 else
11355 code = unsignedp ? GTU : GT;
11356 break;
11357 case GE_EXPR:
11358 if (integer_onep (arg1))
11359 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11360 else
11361 code = unsignedp ? GEU : GE;
11362 break;
11364 case UNORDERED_EXPR:
11365 code = UNORDERED;
11366 break;
11367 case ORDERED_EXPR:
11368 code = ORDERED;
11369 break;
11370 case UNLT_EXPR:
11371 code = UNLT;
11372 break;
11373 case UNLE_EXPR:
11374 code = UNLE;
11375 break;
11376 case UNGT_EXPR:
11377 code = UNGT;
11378 break;
11379 case UNGE_EXPR:
11380 code = UNGE;
11381 break;
11382 case UNEQ_EXPR:
11383 code = UNEQ;
11384 break;
11385 case LTGT_EXPR:
11386 code = LTGT;
11387 break;
11389 default:
11390 gcc_unreachable ();
11393 /* Put a constant second. */
11394 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11395 || TREE_CODE (arg0) == FIXED_CST)
11397 std::swap (arg0, arg1);
11398 code = swap_condition (code);
11401 /* If this is an equality or inequality test of a single bit, we can
11402 do this by shifting the bit being tested to the low-order bit and
11403 masking the result with the constant 1. If the condition was EQ,
11404 we xor it with 1. This does not require an scc insn and is faster
11405 than an scc insn even if we have it.
11407 The code to make this transformation was moved into fold_single_bit_test,
11408 so we just call into the folder and expand its result. */
11410 if ((code == NE || code == EQ)
11411 && integer_zerop (arg1)
11412 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11414 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11415 if (srcstmt
11416 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11418 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11419 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11420 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11421 gimple_assign_rhs1 (srcstmt),
11422 gimple_assign_rhs2 (srcstmt));
11423 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11424 if (temp)
11425 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11429 if (! get_subtarget (target)
11430 || GET_MODE (subtarget) != operand_mode)
11431 subtarget = 0;
11433 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11435 if (target == 0)
11436 target = gen_reg_rtx (mode);
11438 /* Try a cstore if possible. */
11439 return emit_store_flag_force (target, code, op0, op1,
11440 operand_mode, unsignedp,
11441 (TYPE_PRECISION (ops->type) == 1
11442 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11445 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11446 0 otherwise (i.e. if there is no casesi instruction).
11448 DEFAULT_PROBABILITY is the probability of jumping to the default
11449 label. */
11451 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11452 rtx table_label, rtx default_label, rtx fallback_label,
11453 int default_probability)
11455 struct expand_operand ops[5];
11456 machine_mode index_mode = SImode;
11457 rtx op1, op2, index;
11459 if (! targetm.have_casesi ())
11460 return 0;
11462 /* Convert the index to SImode. */
11463 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11465 machine_mode omode = TYPE_MODE (index_type);
11466 rtx rangertx = expand_normal (range);
11468 /* We must handle the endpoints in the original mode. */
11469 index_expr = build2 (MINUS_EXPR, index_type,
11470 index_expr, minval);
11471 minval = integer_zero_node;
11472 index = expand_normal (index_expr);
11473 if (default_label)
11474 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11475 omode, 1, default_label,
11476 default_probability);
11477 /* Now we can safely truncate. */
11478 index = convert_to_mode (index_mode, index, 0);
11480 else
11482 if (TYPE_MODE (index_type) != index_mode)
11484 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11485 index_expr = fold_convert (index_type, index_expr);
11488 index = expand_normal (index_expr);
11491 do_pending_stack_adjust ();
11493 op1 = expand_normal (minval);
11494 op2 = expand_normal (range);
11496 create_input_operand (&ops[0], index, index_mode);
11497 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11498 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11499 create_fixed_operand (&ops[3], table_label);
11500 create_fixed_operand (&ops[4], (default_label
11501 ? default_label
11502 : fallback_label));
11503 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11504 return 1;
11507 /* Attempt to generate a tablejump instruction; same concept. */
11508 /* Subroutine of the next function.
11510 INDEX is the value being switched on, with the lowest value
11511 in the table already subtracted.
11512 MODE is its expected mode (needed if INDEX is constant).
11513 RANGE is the length of the jump table.
11514 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11516 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11517 index value is out of range.
11518 DEFAULT_PROBABILITY is the probability of jumping to
11519 the default label. */
11521 static void
11522 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11523 rtx default_label, int default_probability)
11525 rtx temp, vector;
11527 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11528 cfun->cfg->max_jumptable_ents = INTVAL (range);
11530 /* Do an unsigned comparison (in the proper mode) between the index
11531 expression and the value which represents the length of the range.
11532 Since we just finished subtracting the lower bound of the range
11533 from the index expression, this comparison allows us to simultaneously
11534 check that the original index expression value is both greater than
11535 or equal to the minimum value of the range and less than or equal to
11536 the maximum value of the range. */
11538 if (default_label)
11539 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11540 default_label, default_probability);
11543 /* If index is in range, it must fit in Pmode.
11544 Convert to Pmode so we can index with it. */
11545 if (mode != Pmode)
11546 index = convert_to_mode (Pmode, index, 1);
11548 /* Don't let a MEM slip through, because then INDEX that comes
11549 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11550 and break_out_memory_refs will go to work on it and mess it up. */
11551 #ifdef PIC_CASE_VECTOR_ADDRESS
11552 if (flag_pic && !REG_P (index))
11553 index = copy_to_mode_reg (Pmode, index);
11554 #endif
11556 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11557 GET_MODE_SIZE, because this indicates how large insns are. The other
11558 uses should all be Pmode, because they are addresses. This code
11559 could fail if addresses and insns are not the same size. */
11560 index = simplify_gen_binary (MULT, Pmode, index,
11561 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11562 Pmode));
11563 index = simplify_gen_binary (PLUS, Pmode, index,
11564 gen_rtx_LABEL_REF (Pmode, table_label));
11566 #ifdef PIC_CASE_VECTOR_ADDRESS
11567 if (flag_pic)
11568 index = PIC_CASE_VECTOR_ADDRESS (index);
11569 else
11570 #endif
11571 index = memory_address (CASE_VECTOR_MODE, index);
11572 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11573 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11574 convert_move (temp, vector, 0);
11576 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11578 /* If we are generating PIC code or if the table is PC-relative, the
11579 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11580 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11581 emit_barrier ();
11585 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11586 rtx table_label, rtx default_label, int default_probability)
11588 rtx index;
11590 if (! targetm.have_tablejump ())
11591 return 0;
11593 index_expr = fold_build2 (MINUS_EXPR, index_type,
11594 fold_convert (index_type, index_expr),
11595 fold_convert (index_type, minval));
11596 index = expand_normal (index_expr);
11597 do_pending_stack_adjust ();
11599 do_tablejump (index, TYPE_MODE (index_type),
11600 convert_modes (TYPE_MODE (index_type),
11601 TYPE_MODE (TREE_TYPE (range)),
11602 expand_normal (range),
11603 TYPE_UNSIGNED (TREE_TYPE (range))),
11604 table_label, default_label, default_probability);
11605 return 1;
11608 /* Return a CONST_VECTOR rtx representing vector mask for
11609 a VECTOR_CST of booleans. */
11610 static rtx
11611 const_vector_mask_from_tree (tree exp)
11613 rtvec v;
11614 unsigned i;
11615 int units;
11616 tree elt;
11617 machine_mode inner, mode;
11619 mode = TYPE_MODE (TREE_TYPE (exp));
11620 units = GET_MODE_NUNITS (mode);
11621 inner = GET_MODE_INNER (mode);
11623 v = rtvec_alloc (units);
11625 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11627 elt = VECTOR_CST_ELT (exp, i);
11629 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11630 if (integer_zerop (elt))
11631 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11632 else if (integer_onep (elt)
11633 || integer_minus_onep (elt))
11634 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11635 else
11636 gcc_unreachable ();
11639 return gen_rtx_CONST_VECTOR (mode, v);
11642 /* Return a CONST_INT rtx representing vector mask for
11643 a VECTOR_CST of booleans. */
11644 static rtx
11645 const_scalar_mask_from_tree (tree exp)
11647 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11648 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11649 tree elt;
11650 unsigned i;
11652 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11654 elt = VECTOR_CST_ELT (exp, i);
11655 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11656 if (integer_all_onesp (elt))
11657 res = wi::set_bit (res, i);
11658 else
11659 gcc_assert (integer_zerop (elt));
11662 return immed_wide_int_const (res, mode);
11665 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11666 static rtx
11667 const_vector_from_tree (tree exp)
11669 rtvec v;
11670 unsigned i;
11671 int units;
11672 tree elt;
11673 machine_mode inner, mode;
11675 mode = TYPE_MODE (TREE_TYPE (exp));
11677 if (initializer_zerop (exp))
11678 return CONST0_RTX (mode);
11680 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11681 return const_vector_mask_from_tree (exp);
11683 units = GET_MODE_NUNITS (mode);
11684 inner = GET_MODE_INNER (mode);
11686 v = rtvec_alloc (units);
11688 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11690 elt = VECTOR_CST_ELT (exp, i);
11692 if (TREE_CODE (elt) == REAL_CST)
11693 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11694 inner);
11695 else if (TREE_CODE (elt) == FIXED_CST)
11696 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11697 inner);
11698 else
11699 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11702 return gen_rtx_CONST_VECTOR (mode, v);
11705 /* Build a decl for a personality function given a language prefix. */
11707 tree
11708 build_personality_function (const char *lang)
11710 const char *unwind_and_version;
11711 tree decl, type;
11712 char *name;
11714 switch (targetm_common.except_unwind_info (&global_options))
11716 case UI_NONE:
11717 return NULL;
11718 case UI_SJLJ:
11719 unwind_and_version = "_sj0";
11720 break;
11721 case UI_DWARF2:
11722 case UI_TARGET:
11723 unwind_and_version = "_v0";
11724 break;
11725 case UI_SEH:
11726 unwind_and_version = "_seh0";
11727 break;
11728 default:
11729 gcc_unreachable ();
11732 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11734 type = build_function_type_list (integer_type_node, integer_type_node,
11735 long_long_unsigned_type_node,
11736 ptr_type_node, ptr_type_node, NULL_TREE);
11737 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11738 get_identifier (name), type);
11739 DECL_ARTIFICIAL (decl) = 1;
11740 DECL_EXTERNAL (decl) = 1;
11741 TREE_PUBLIC (decl) = 1;
11743 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11744 are the flags assigned by targetm.encode_section_info. */
11745 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11747 return decl;
11750 /* Extracts the personality function of DECL and returns the corresponding
11751 libfunc. */
11754 get_personality_function (tree decl)
11756 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11757 enum eh_personality_kind pk;
11759 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11760 if (pk == eh_personality_none)
11761 return NULL;
11763 if (!personality
11764 && pk == eh_personality_any)
11765 personality = lang_hooks.eh_personality ();
11767 if (pk == eh_personality_lang)
11768 gcc_assert (personality != NULL_TREE);
11770 return XEXP (DECL_RTL (personality), 0);
11773 /* Returns a tree for the size of EXP in bytes. */
11775 static tree
11776 tree_expr_size (const_tree exp)
11778 if (DECL_P (exp)
11779 && DECL_SIZE_UNIT (exp) != 0)
11780 return DECL_SIZE_UNIT (exp);
11781 else
11782 return size_in_bytes (TREE_TYPE (exp));
11785 /* Return an rtx for the size in bytes of the value of EXP. */
11788 expr_size (tree exp)
11790 tree size;
11792 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11793 size = TREE_OPERAND (exp, 1);
11794 else
11796 size = tree_expr_size (exp);
11797 gcc_assert (size);
11798 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11801 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11804 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11805 if the size can vary or is larger than an integer. */
11807 static HOST_WIDE_INT
11808 int_expr_size (tree exp)
11810 tree size;
11812 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11813 size = TREE_OPERAND (exp, 1);
11814 else
11816 size = tree_expr_size (exp);
11817 gcc_assert (size);
11820 if (size == 0 || !tree_fits_shwi_p (size))
11821 return -1;
11823 return tree_to_shwi (size);