[ARM] Add source mode to coprocessor pattern SETs
[official-gcc.git] / gcc / expr.c
blob91d7ea217229fac62380b5d4b646961bf7c836c1
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 /* Handle calls that return values in multiple non-contiguous locations.
6836 The Irix 6 ABI has examples of this. */
6837 if (GET_CODE (temp) == PARALLEL)
6839 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6840 machine_mode temp_mode
6841 = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6842 rtx temp_target = gen_reg_rtx (temp_mode);
6843 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6844 temp = temp_target;
6847 /* Handle calls that return BLKmode values in registers. */
6848 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6850 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6851 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6852 temp = temp_target;
6855 /* If the value has aggregate type and an integral mode then, if BITSIZE
6856 is narrower than this mode and this is for big-endian data, we first
6857 need to put the value into the low-order bits for store_bit_field,
6858 except when MODE is BLKmode and BITSIZE larger than the word size
6859 (see the handling of fields larger than a word in store_bit_field).
6860 Moreover, the field may be not aligned on a byte boundary; in this
6861 case, if it has reverse storage order, it needs to be accessed as a
6862 scalar field with reverse storage order and we must first put the
6863 value into target order. */
6864 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6865 && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT)
6867 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (temp));
6869 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6871 if (reverse)
6872 temp = flip_storage_order (GET_MODE (temp), temp);
6874 if (bitsize < size
6875 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6876 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6877 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6878 size - bitsize, NULL_RTX, 1);
6881 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6882 if (mode != VOIDmode && mode != BLKmode
6883 && mode != TYPE_MODE (TREE_TYPE (exp)))
6884 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6886 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6887 and BITPOS must be aligned on a byte boundary. If so, we simply do
6888 a block copy. Likewise for a BLKmode-like TARGET. */
6889 if (GET_MODE (temp) == BLKmode
6890 && (GET_MODE (target) == BLKmode
6891 || (MEM_P (target)
6892 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6893 && (bitpos % BITS_PER_UNIT) == 0
6894 && (bitsize % BITS_PER_UNIT) == 0)))
6896 gcc_assert (MEM_P (target) && MEM_P (temp)
6897 && (bitpos % BITS_PER_UNIT) == 0);
6899 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6900 emit_block_move (target, temp,
6901 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6902 / BITS_PER_UNIT),
6903 BLOCK_OP_NORMAL);
6905 return const0_rtx;
6908 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6909 word size, we need to load the value (see again store_bit_field). */
6910 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6912 machine_mode temp_mode = smallest_mode_for_size (bitsize, MODE_INT);
6913 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6914 temp_mode, false);
6917 /* Store the value in the bitfield. */
6918 store_bit_field (target, bitsize, bitpos,
6919 bitregion_start, bitregion_end,
6920 mode, temp, reverse);
6922 return const0_rtx;
6924 else
6926 /* Now build a reference to just the desired component. */
6927 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6929 if (to_rtx == target)
6930 to_rtx = copy_rtx (to_rtx);
6932 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6933 set_mem_alias_set (to_rtx, alias_set);
6935 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6936 into a target smaller than its type; handle that case now. */
6937 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6939 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6940 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6941 return to_rtx;
6944 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6948 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6949 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6950 codes and find the ultimate containing object, which we return.
6952 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6953 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6954 storage order of the field.
6955 If the position of the field is variable, we store a tree
6956 giving the variable offset (in units) in *POFFSET.
6957 This offset is in addition to the bit position.
6958 If the position is not variable, we store 0 in *POFFSET.
6960 If any of the extraction expressions is volatile,
6961 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6963 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6964 Otherwise, it is a mode that can be used to access the field.
6966 If the field describes a variable-sized object, *PMODE is set to
6967 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6968 this case, but the address of the object can be found. */
6970 tree
6971 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6972 HOST_WIDE_INT *pbitpos, tree *poffset,
6973 machine_mode *pmode, int *punsignedp,
6974 int *preversep, int *pvolatilep)
6976 tree size_tree = 0;
6977 machine_mode mode = VOIDmode;
6978 bool blkmode_bitfield = false;
6979 tree offset = size_zero_node;
6980 offset_int bit_offset = 0;
6982 /* First get the mode, signedness, storage order and size. We do this from
6983 just the outermost expression. */
6984 *pbitsize = -1;
6985 if (TREE_CODE (exp) == COMPONENT_REF)
6987 tree field = TREE_OPERAND (exp, 1);
6988 size_tree = DECL_SIZE (field);
6989 if (flag_strict_volatile_bitfields > 0
6990 && TREE_THIS_VOLATILE (exp)
6991 && DECL_BIT_FIELD_TYPE (field)
6992 && DECL_MODE (field) != BLKmode)
6993 /* Volatile bitfields should be accessed in the mode of the
6994 field's type, not the mode computed based on the bit
6995 size. */
6996 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6997 else if (!DECL_BIT_FIELD (field))
6998 mode = DECL_MODE (field);
6999 else if (DECL_MODE (field) == BLKmode)
7000 blkmode_bitfield = true;
7002 *punsignedp = DECL_UNSIGNED (field);
7004 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7006 size_tree = TREE_OPERAND (exp, 1);
7007 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7008 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7010 /* For vector types, with the correct size of access, use the mode of
7011 inner type. */
7012 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7013 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7014 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7015 mode = TYPE_MODE (TREE_TYPE (exp));
7017 else
7019 mode = TYPE_MODE (TREE_TYPE (exp));
7020 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7022 if (mode == BLKmode)
7023 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7024 else
7025 *pbitsize = GET_MODE_BITSIZE (mode);
7028 if (size_tree != 0)
7030 if (! tree_fits_uhwi_p (size_tree))
7031 mode = BLKmode, *pbitsize = -1;
7032 else
7033 *pbitsize = tree_to_uhwi (size_tree);
7036 *preversep = reverse_storage_order_for_component_p (exp);
7038 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7039 and find the ultimate containing object. */
7040 while (1)
7042 switch (TREE_CODE (exp))
7044 case BIT_FIELD_REF:
7045 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7046 break;
7048 case COMPONENT_REF:
7050 tree field = TREE_OPERAND (exp, 1);
7051 tree this_offset = component_ref_field_offset (exp);
7053 /* If this field hasn't been filled in yet, don't go past it.
7054 This should only happen when folding expressions made during
7055 type construction. */
7056 if (this_offset == 0)
7057 break;
7059 offset = size_binop (PLUS_EXPR, offset, this_offset);
7060 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7062 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7064 break;
7066 case ARRAY_REF:
7067 case ARRAY_RANGE_REF:
7069 tree index = TREE_OPERAND (exp, 1);
7070 tree low_bound = array_ref_low_bound (exp);
7071 tree unit_size = array_ref_element_size (exp);
7073 /* We assume all arrays have sizes that are a multiple of a byte.
7074 First subtract the lower bound, if any, in the type of the
7075 index, then convert to sizetype and multiply by the size of
7076 the array element. */
7077 if (! integer_zerop (low_bound))
7078 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7079 index, low_bound);
7081 offset = size_binop (PLUS_EXPR, offset,
7082 size_binop (MULT_EXPR,
7083 fold_convert (sizetype, index),
7084 unit_size));
7086 break;
7088 case REALPART_EXPR:
7089 break;
7091 case IMAGPART_EXPR:
7092 bit_offset += *pbitsize;
7093 break;
7095 case VIEW_CONVERT_EXPR:
7096 break;
7098 case MEM_REF:
7099 /* Hand back the decl for MEM[&decl, off]. */
7100 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7102 tree off = TREE_OPERAND (exp, 1);
7103 if (!integer_zerop (off))
7105 offset_int boff, coff = mem_ref_offset (exp);
7106 boff = coff << LOG2_BITS_PER_UNIT;
7107 bit_offset += boff;
7109 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7111 goto done;
7113 default:
7114 goto done;
7117 /* If any reference in the chain is volatile, the effect is volatile. */
7118 if (TREE_THIS_VOLATILE (exp))
7119 *pvolatilep = 1;
7121 exp = TREE_OPERAND (exp, 0);
7123 done:
7125 /* If OFFSET is constant, see if we can return the whole thing as a
7126 constant bit position. Make sure to handle overflow during
7127 this conversion. */
7128 if (TREE_CODE (offset) == INTEGER_CST)
7130 offset_int tem = wi::sext (wi::to_offset (offset),
7131 TYPE_PRECISION (sizetype));
7132 tem <<= LOG2_BITS_PER_UNIT;
7133 tem += bit_offset;
7134 if (wi::fits_shwi_p (tem))
7136 *pbitpos = tem.to_shwi ();
7137 *poffset = offset = NULL_TREE;
7141 /* Otherwise, split it up. */
7142 if (offset)
7144 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7145 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7147 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7148 offset_int tem = bit_offset.and_not (mask);
7149 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7150 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7151 bit_offset -= tem;
7152 tem >>= LOG2_BITS_PER_UNIT;
7153 offset = size_binop (PLUS_EXPR, offset,
7154 wide_int_to_tree (sizetype, tem));
7157 *pbitpos = bit_offset.to_shwi ();
7158 *poffset = offset;
7161 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7162 if (mode == VOIDmode
7163 && blkmode_bitfield
7164 && (*pbitpos % BITS_PER_UNIT) == 0
7165 && (*pbitsize % BITS_PER_UNIT) == 0)
7166 *pmode = BLKmode;
7167 else
7168 *pmode = mode;
7170 return exp;
7173 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7175 static unsigned HOST_WIDE_INT
7176 target_align (const_tree target)
7178 /* We might have a chain of nested references with intermediate misaligning
7179 bitfields components, so need to recurse to find out. */
7181 unsigned HOST_WIDE_INT this_align, outer_align;
7183 switch (TREE_CODE (target))
7185 case BIT_FIELD_REF:
7186 return 1;
7188 case COMPONENT_REF:
7189 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7190 outer_align = target_align (TREE_OPERAND (target, 0));
7191 return MIN (this_align, outer_align);
7193 case ARRAY_REF:
7194 case ARRAY_RANGE_REF:
7195 this_align = TYPE_ALIGN (TREE_TYPE (target));
7196 outer_align = target_align (TREE_OPERAND (target, 0));
7197 return MIN (this_align, outer_align);
7199 CASE_CONVERT:
7200 case NON_LVALUE_EXPR:
7201 case VIEW_CONVERT_EXPR:
7202 this_align = TYPE_ALIGN (TREE_TYPE (target));
7203 outer_align = target_align (TREE_OPERAND (target, 0));
7204 return MAX (this_align, outer_align);
7206 default:
7207 return TYPE_ALIGN (TREE_TYPE (target));
7212 /* Given an rtx VALUE that may contain additions and multiplications, return
7213 an equivalent value that just refers to a register, memory, or constant.
7214 This is done by generating instructions to perform the arithmetic and
7215 returning a pseudo-register containing the value.
7217 The returned value may be a REG, SUBREG, MEM or constant. */
7220 force_operand (rtx value, rtx target)
7222 rtx op1, op2;
7223 /* Use subtarget as the target for operand 0 of a binary operation. */
7224 rtx subtarget = get_subtarget (target);
7225 enum rtx_code code = GET_CODE (value);
7227 /* Check for subreg applied to an expression produced by loop optimizer. */
7228 if (code == SUBREG
7229 && !REG_P (SUBREG_REG (value))
7230 && !MEM_P (SUBREG_REG (value)))
7232 value
7233 = simplify_gen_subreg (GET_MODE (value),
7234 force_reg (GET_MODE (SUBREG_REG (value)),
7235 force_operand (SUBREG_REG (value),
7236 NULL_RTX)),
7237 GET_MODE (SUBREG_REG (value)),
7238 SUBREG_BYTE (value));
7239 code = GET_CODE (value);
7242 /* Check for a PIC address load. */
7243 if ((code == PLUS || code == MINUS)
7244 && XEXP (value, 0) == pic_offset_table_rtx
7245 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7246 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7247 || GET_CODE (XEXP (value, 1)) == CONST))
7249 if (!subtarget)
7250 subtarget = gen_reg_rtx (GET_MODE (value));
7251 emit_move_insn (subtarget, value);
7252 return subtarget;
7255 if (ARITHMETIC_P (value))
7257 op2 = XEXP (value, 1);
7258 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7259 subtarget = 0;
7260 if (code == MINUS && CONST_INT_P (op2))
7262 code = PLUS;
7263 op2 = negate_rtx (GET_MODE (value), op2);
7266 /* Check for an addition with OP2 a constant integer and our first
7267 operand a PLUS of a virtual register and something else. In that
7268 case, we want to emit the sum of the virtual register and the
7269 constant first and then add the other value. This allows virtual
7270 register instantiation to simply modify the constant rather than
7271 creating another one around this addition. */
7272 if (code == PLUS && CONST_INT_P (op2)
7273 && GET_CODE (XEXP (value, 0)) == PLUS
7274 && REG_P (XEXP (XEXP (value, 0), 0))
7275 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7276 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7278 rtx temp = expand_simple_binop (GET_MODE (value), code,
7279 XEXP (XEXP (value, 0), 0), op2,
7280 subtarget, 0, OPTAB_LIB_WIDEN);
7281 return expand_simple_binop (GET_MODE (value), code, temp,
7282 force_operand (XEXP (XEXP (value,
7283 0), 1), 0),
7284 target, 0, OPTAB_LIB_WIDEN);
7287 op1 = force_operand (XEXP (value, 0), subtarget);
7288 op2 = force_operand (op2, NULL_RTX);
7289 switch (code)
7291 case MULT:
7292 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7293 case DIV:
7294 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7295 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7296 target, 1, OPTAB_LIB_WIDEN);
7297 else
7298 return expand_divmod (0,
7299 FLOAT_MODE_P (GET_MODE (value))
7300 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7301 GET_MODE (value), op1, op2, target, 0);
7302 case MOD:
7303 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7304 target, 0);
7305 case UDIV:
7306 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7307 target, 1);
7308 case UMOD:
7309 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7310 target, 1);
7311 case ASHIFTRT:
7312 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7313 target, 0, OPTAB_LIB_WIDEN);
7314 default:
7315 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7316 target, 1, OPTAB_LIB_WIDEN);
7319 if (UNARY_P (value))
7321 if (!target)
7322 target = gen_reg_rtx (GET_MODE (value));
7323 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7324 switch (code)
7326 case ZERO_EXTEND:
7327 case SIGN_EXTEND:
7328 case TRUNCATE:
7329 case FLOAT_EXTEND:
7330 case FLOAT_TRUNCATE:
7331 convert_move (target, op1, code == ZERO_EXTEND);
7332 return target;
7334 case FIX:
7335 case UNSIGNED_FIX:
7336 expand_fix (target, op1, code == UNSIGNED_FIX);
7337 return target;
7339 case FLOAT:
7340 case UNSIGNED_FLOAT:
7341 expand_float (target, op1, code == UNSIGNED_FLOAT);
7342 return target;
7344 default:
7345 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7349 #ifdef INSN_SCHEDULING
7350 /* On machines that have insn scheduling, we want all memory reference to be
7351 explicit, so we need to deal with such paradoxical SUBREGs. */
7352 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7353 value
7354 = simplify_gen_subreg (GET_MODE (value),
7355 force_reg (GET_MODE (SUBREG_REG (value)),
7356 force_operand (SUBREG_REG (value),
7357 NULL_RTX)),
7358 GET_MODE (SUBREG_REG (value)),
7359 SUBREG_BYTE (value));
7360 #endif
7362 return value;
7365 /* Subroutine of expand_expr: return nonzero iff there is no way that
7366 EXP can reference X, which is being modified. TOP_P is nonzero if this
7367 call is going to be used to determine whether we need a temporary
7368 for EXP, as opposed to a recursive call to this function.
7370 It is always safe for this routine to return zero since it merely
7371 searches for optimization opportunities. */
7374 safe_from_p (const_rtx x, tree exp, int top_p)
7376 rtx exp_rtl = 0;
7377 int i, nops;
7379 if (x == 0
7380 /* If EXP has varying size, we MUST use a target since we currently
7381 have no way of allocating temporaries of variable size
7382 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7383 So we assume here that something at a higher level has prevented a
7384 clash. This is somewhat bogus, but the best we can do. Only
7385 do this when X is BLKmode and when we are at the top level. */
7386 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7387 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7388 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7389 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7390 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7391 != INTEGER_CST)
7392 && GET_MODE (x) == BLKmode)
7393 /* If X is in the outgoing argument area, it is always safe. */
7394 || (MEM_P (x)
7395 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7396 || (GET_CODE (XEXP (x, 0)) == PLUS
7397 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7398 return 1;
7400 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7401 find the underlying pseudo. */
7402 if (GET_CODE (x) == SUBREG)
7404 x = SUBREG_REG (x);
7405 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7406 return 0;
7409 /* Now look at our tree code and possibly recurse. */
7410 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7412 case tcc_declaration:
7413 exp_rtl = DECL_RTL_IF_SET (exp);
7414 break;
7416 case tcc_constant:
7417 return 1;
7419 case tcc_exceptional:
7420 if (TREE_CODE (exp) == TREE_LIST)
7422 while (1)
7424 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7425 return 0;
7426 exp = TREE_CHAIN (exp);
7427 if (!exp)
7428 return 1;
7429 if (TREE_CODE (exp) != TREE_LIST)
7430 return safe_from_p (x, exp, 0);
7433 else if (TREE_CODE (exp) == CONSTRUCTOR)
7435 constructor_elt *ce;
7436 unsigned HOST_WIDE_INT idx;
7438 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7439 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7440 || !safe_from_p (x, ce->value, 0))
7441 return 0;
7442 return 1;
7444 else if (TREE_CODE (exp) == ERROR_MARK)
7445 return 1; /* An already-visited SAVE_EXPR? */
7446 else
7447 return 0;
7449 case tcc_statement:
7450 /* The only case we look at here is the DECL_INITIAL inside a
7451 DECL_EXPR. */
7452 return (TREE_CODE (exp) != DECL_EXPR
7453 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7454 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7455 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7457 case tcc_binary:
7458 case tcc_comparison:
7459 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7460 return 0;
7461 /* Fall through. */
7463 case tcc_unary:
7464 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7466 case tcc_expression:
7467 case tcc_reference:
7468 case tcc_vl_exp:
7469 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7470 the expression. If it is set, we conflict iff we are that rtx or
7471 both are in memory. Otherwise, we check all operands of the
7472 expression recursively. */
7474 switch (TREE_CODE (exp))
7476 case ADDR_EXPR:
7477 /* If the operand is static or we are static, we can't conflict.
7478 Likewise if we don't conflict with the operand at all. */
7479 if (staticp (TREE_OPERAND (exp, 0))
7480 || TREE_STATIC (exp)
7481 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7482 return 1;
7484 /* Otherwise, the only way this can conflict is if we are taking
7485 the address of a DECL a that address if part of X, which is
7486 very rare. */
7487 exp = TREE_OPERAND (exp, 0);
7488 if (DECL_P (exp))
7490 if (!DECL_RTL_SET_P (exp)
7491 || !MEM_P (DECL_RTL (exp)))
7492 return 0;
7493 else
7494 exp_rtl = XEXP (DECL_RTL (exp), 0);
7496 break;
7498 case MEM_REF:
7499 if (MEM_P (x)
7500 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7501 get_alias_set (exp)))
7502 return 0;
7503 break;
7505 case CALL_EXPR:
7506 /* Assume that the call will clobber all hard registers and
7507 all of memory. */
7508 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7509 || MEM_P (x))
7510 return 0;
7511 break;
7513 case WITH_CLEANUP_EXPR:
7514 case CLEANUP_POINT_EXPR:
7515 /* Lowered by gimplify.c. */
7516 gcc_unreachable ();
7518 case SAVE_EXPR:
7519 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7521 default:
7522 break;
7525 /* If we have an rtx, we do not need to scan our operands. */
7526 if (exp_rtl)
7527 break;
7529 nops = TREE_OPERAND_LENGTH (exp);
7530 for (i = 0; i < nops; i++)
7531 if (TREE_OPERAND (exp, i) != 0
7532 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7533 return 0;
7535 break;
7537 case tcc_type:
7538 /* Should never get a type here. */
7539 gcc_unreachable ();
7542 /* If we have an rtl, find any enclosed object. Then see if we conflict
7543 with it. */
7544 if (exp_rtl)
7546 if (GET_CODE (exp_rtl) == SUBREG)
7548 exp_rtl = SUBREG_REG (exp_rtl);
7549 if (REG_P (exp_rtl)
7550 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7551 return 0;
7554 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7555 are memory and they conflict. */
7556 return ! (rtx_equal_p (x, exp_rtl)
7557 || (MEM_P (x) && MEM_P (exp_rtl)
7558 && true_dependence (exp_rtl, VOIDmode, x)));
7561 /* If we reach here, it is safe. */
7562 return 1;
7566 /* Return the highest power of two that EXP is known to be a multiple of.
7567 This is used in updating alignment of MEMs in array references. */
7569 unsigned HOST_WIDE_INT
7570 highest_pow2_factor (const_tree exp)
7572 unsigned HOST_WIDE_INT ret;
7573 int trailing_zeros = tree_ctz (exp);
7574 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7575 return BIGGEST_ALIGNMENT;
7576 ret = HOST_WIDE_INT_1U << trailing_zeros;
7577 if (ret > BIGGEST_ALIGNMENT)
7578 return BIGGEST_ALIGNMENT;
7579 return ret;
7582 /* Similar, except that the alignment requirements of TARGET are
7583 taken into account. Assume it is at least as aligned as its
7584 type, unless it is a COMPONENT_REF in which case the layout of
7585 the structure gives the alignment. */
7587 static unsigned HOST_WIDE_INT
7588 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7590 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7591 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7593 return MAX (factor, talign);
7596 /* Convert the tree comparison code TCODE to the rtl one where the
7597 signedness is UNSIGNEDP. */
7599 static enum rtx_code
7600 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7602 enum rtx_code code;
7603 switch (tcode)
7605 case EQ_EXPR:
7606 code = EQ;
7607 break;
7608 case NE_EXPR:
7609 code = NE;
7610 break;
7611 case LT_EXPR:
7612 code = unsignedp ? LTU : LT;
7613 break;
7614 case LE_EXPR:
7615 code = unsignedp ? LEU : LE;
7616 break;
7617 case GT_EXPR:
7618 code = unsignedp ? GTU : GT;
7619 break;
7620 case GE_EXPR:
7621 code = unsignedp ? GEU : GE;
7622 break;
7623 case UNORDERED_EXPR:
7624 code = UNORDERED;
7625 break;
7626 case ORDERED_EXPR:
7627 code = ORDERED;
7628 break;
7629 case UNLT_EXPR:
7630 code = UNLT;
7631 break;
7632 case UNLE_EXPR:
7633 code = UNLE;
7634 break;
7635 case UNGT_EXPR:
7636 code = UNGT;
7637 break;
7638 case UNGE_EXPR:
7639 code = UNGE;
7640 break;
7641 case UNEQ_EXPR:
7642 code = UNEQ;
7643 break;
7644 case LTGT_EXPR:
7645 code = LTGT;
7646 break;
7648 default:
7649 gcc_unreachable ();
7651 return code;
7654 /* Subroutine of expand_expr. Expand the two operands of a binary
7655 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7656 The value may be stored in TARGET if TARGET is nonzero. The
7657 MODIFIER argument is as documented by expand_expr. */
7659 void
7660 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7661 enum expand_modifier modifier)
7663 if (! safe_from_p (target, exp1, 1))
7664 target = 0;
7665 if (operand_equal_p (exp0, exp1, 0))
7667 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7668 *op1 = copy_rtx (*op0);
7670 else
7672 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7673 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7678 /* Return a MEM that contains constant EXP. DEFER is as for
7679 output_constant_def and MODIFIER is as for expand_expr. */
7681 static rtx
7682 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7684 rtx mem;
7686 mem = output_constant_def (exp, defer);
7687 if (modifier != EXPAND_INITIALIZER)
7688 mem = use_anchored_address (mem);
7689 return mem;
7692 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7693 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7695 static rtx
7696 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7697 enum expand_modifier modifier, addr_space_t as)
7699 rtx result, subtarget;
7700 tree inner, offset;
7701 HOST_WIDE_INT bitsize, bitpos;
7702 int unsignedp, reversep, volatilep = 0;
7703 machine_mode mode1;
7705 /* If we are taking the address of a constant and are at the top level,
7706 we have to use output_constant_def since we can't call force_const_mem
7707 at top level. */
7708 /* ??? This should be considered a front-end bug. We should not be
7709 generating ADDR_EXPR of something that isn't an LVALUE. The only
7710 exception here is STRING_CST. */
7711 if (CONSTANT_CLASS_P (exp))
7713 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7714 if (modifier < EXPAND_SUM)
7715 result = force_operand (result, target);
7716 return result;
7719 /* Everything must be something allowed by is_gimple_addressable. */
7720 switch (TREE_CODE (exp))
7722 case INDIRECT_REF:
7723 /* This case will happen via recursion for &a->b. */
7724 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7726 case MEM_REF:
7728 tree tem = TREE_OPERAND (exp, 0);
7729 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7730 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7731 return expand_expr (tem, target, tmode, modifier);
7734 case CONST_DECL:
7735 /* Expand the initializer like constants above. */
7736 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7737 0, modifier), 0);
7738 if (modifier < EXPAND_SUM)
7739 result = force_operand (result, target);
7740 return result;
7742 case REALPART_EXPR:
7743 /* The real part of the complex number is always first, therefore
7744 the address is the same as the address of the parent object. */
7745 offset = 0;
7746 bitpos = 0;
7747 inner = TREE_OPERAND (exp, 0);
7748 break;
7750 case IMAGPART_EXPR:
7751 /* The imaginary part of the complex number is always second.
7752 The expression is therefore always offset by the size of the
7753 scalar type. */
7754 offset = 0;
7755 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7756 inner = TREE_OPERAND (exp, 0);
7757 break;
7759 case COMPOUND_LITERAL_EXPR:
7760 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7761 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7762 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7763 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7764 the initializers aren't gimplified. */
7765 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7766 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7767 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7768 target, tmode, modifier, as);
7769 /* FALLTHRU */
7770 default:
7771 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7772 expand_expr, as that can have various side effects; LABEL_DECLs for
7773 example, may not have their DECL_RTL set yet. Expand the rtl of
7774 CONSTRUCTORs too, which should yield a memory reference for the
7775 constructor's contents. Assume language specific tree nodes can
7776 be expanded in some interesting way. */
7777 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7778 if (DECL_P (exp)
7779 || TREE_CODE (exp) == CONSTRUCTOR
7780 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7782 result = expand_expr (exp, target, tmode,
7783 modifier == EXPAND_INITIALIZER
7784 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7786 /* If the DECL isn't in memory, then the DECL wasn't properly
7787 marked TREE_ADDRESSABLE, which will be either a front-end
7788 or a tree optimizer bug. */
7790 gcc_assert (MEM_P (result));
7791 result = XEXP (result, 0);
7793 /* ??? Is this needed anymore? */
7794 if (DECL_P (exp))
7795 TREE_USED (exp) = 1;
7797 if (modifier != EXPAND_INITIALIZER
7798 && modifier != EXPAND_CONST_ADDRESS
7799 && modifier != EXPAND_SUM)
7800 result = force_operand (result, target);
7801 return result;
7804 /* Pass FALSE as the last argument to get_inner_reference although
7805 we are expanding to RTL. The rationale is that we know how to
7806 handle "aligning nodes" here: we can just bypass them because
7807 they won't change the final object whose address will be returned
7808 (they actually exist only for that purpose). */
7809 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7810 &unsignedp, &reversep, &volatilep);
7811 break;
7814 /* We must have made progress. */
7815 gcc_assert (inner != exp);
7817 subtarget = offset || bitpos ? NULL_RTX : target;
7818 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7819 inner alignment, force the inner to be sufficiently aligned. */
7820 if (CONSTANT_CLASS_P (inner)
7821 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7823 inner = copy_node (inner);
7824 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7825 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7826 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7828 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7830 if (offset)
7832 rtx tmp;
7834 if (modifier != EXPAND_NORMAL)
7835 result = force_operand (result, NULL);
7836 tmp = expand_expr (offset, NULL_RTX, tmode,
7837 modifier == EXPAND_INITIALIZER
7838 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7840 /* expand_expr is allowed to return an object in a mode other
7841 than TMODE. If it did, we need to convert. */
7842 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7843 tmp = convert_modes (tmode, GET_MODE (tmp),
7844 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7845 result = convert_memory_address_addr_space (tmode, result, as);
7846 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7848 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7849 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7850 else
7852 subtarget = bitpos ? NULL_RTX : target;
7853 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7854 1, OPTAB_LIB_WIDEN);
7858 if (bitpos)
7860 /* Someone beforehand should have rejected taking the address
7861 of such an object. */
7862 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7864 result = convert_memory_address_addr_space (tmode, result, as);
7865 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7866 if (modifier < EXPAND_SUM)
7867 result = force_operand (result, target);
7870 return result;
7873 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7874 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7876 static rtx
7877 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7878 enum expand_modifier modifier)
7880 addr_space_t as = ADDR_SPACE_GENERIC;
7881 machine_mode address_mode = Pmode;
7882 machine_mode pointer_mode = ptr_mode;
7883 machine_mode rmode;
7884 rtx result;
7886 /* Target mode of VOIDmode says "whatever's natural". */
7887 if (tmode == VOIDmode)
7888 tmode = TYPE_MODE (TREE_TYPE (exp));
7890 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7892 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7893 address_mode = targetm.addr_space.address_mode (as);
7894 pointer_mode = targetm.addr_space.pointer_mode (as);
7897 /* We can get called with some Weird Things if the user does silliness
7898 like "(short) &a". In that case, convert_memory_address won't do
7899 the right thing, so ignore the given target mode. */
7900 if (tmode != address_mode && tmode != pointer_mode)
7901 tmode = address_mode;
7903 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7904 tmode, modifier, as);
7906 /* Despite expand_expr claims concerning ignoring TMODE when not
7907 strictly convenient, stuff breaks if we don't honor it. Note
7908 that combined with the above, we only do this for pointer modes. */
7909 rmode = GET_MODE (result);
7910 if (rmode == VOIDmode)
7911 rmode = tmode;
7912 if (rmode != tmode)
7913 result = convert_memory_address_addr_space (tmode, result, as);
7915 return result;
7918 /* Generate code for computing CONSTRUCTOR EXP.
7919 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7920 is TRUE, instead of creating a temporary variable in memory
7921 NULL is returned and the caller needs to handle it differently. */
7923 static rtx
7924 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7925 bool avoid_temp_mem)
7927 tree type = TREE_TYPE (exp);
7928 machine_mode mode = TYPE_MODE (type);
7930 /* Try to avoid creating a temporary at all. This is possible
7931 if all of the initializer is zero.
7932 FIXME: try to handle all [0..255] initializers we can handle
7933 with memset. */
7934 if (TREE_STATIC (exp)
7935 && !TREE_ADDRESSABLE (exp)
7936 && target != 0 && mode == BLKmode
7937 && all_zeros_p (exp))
7939 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7940 return target;
7943 /* All elts simple constants => refer to a constant in memory. But
7944 if this is a non-BLKmode mode, let it store a field at a time
7945 since that should make a CONST_INT, CONST_WIDE_INT or
7946 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7947 use, it is best to store directly into the target unless the type
7948 is large enough that memcpy will be used. If we are making an
7949 initializer and all operands are constant, put it in memory as
7950 well.
7952 FIXME: Avoid trying to fill vector constructors piece-meal.
7953 Output them with output_constant_def below unless we're sure
7954 they're zeros. This should go away when vector initializers
7955 are treated like VECTOR_CST instead of arrays. */
7956 if ((TREE_STATIC (exp)
7957 && ((mode == BLKmode
7958 && ! (target != 0 && safe_from_p (target, exp, 1)))
7959 || TREE_ADDRESSABLE (exp)
7960 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7961 && (! can_move_by_pieces
7962 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7963 TYPE_ALIGN (type)))
7964 && ! mostly_zeros_p (exp))))
7965 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7966 && TREE_CONSTANT (exp)))
7968 rtx constructor;
7970 if (avoid_temp_mem)
7971 return NULL_RTX;
7973 constructor = expand_expr_constant (exp, 1, modifier);
7975 if (modifier != EXPAND_CONST_ADDRESS
7976 && modifier != EXPAND_INITIALIZER
7977 && modifier != EXPAND_SUM)
7978 constructor = validize_mem (constructor);
7980 return constructor;
7983 /* Handle calls that pass values in multiple non-contiguous
7984 locations. The Irix 6 ABI has examples of this. */
7985 if (target == 0 || ! safe_from_p (target, exp, 1)
7986 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7988 if (avoid_temp_mem)
7989 return NULL_RTX;
7991 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7994 store_constructor (exp, target, 0, int_expr_size (exp), false);
7995 return target;
7999 /* expand_expr: generate code for computing expression EXP.
8000 An rtx for the computed value is returned. The value is never null.
8001 In the case of a void EXP, const0_rtx is returned.
8003 The value may be stored in TARGET if TARGET is nonzero.
8004 TARGET is just a suggestion; callers must assume that
8005 the rtx returned may not be the same as TARGET.
8007 If TARGET is CONST0_RTX, it means that the value will be ignored.
8009 If TMODE is not VOIDmode, it suggests generating the
8010 result in mode TMODE. But this is done only when convenient.
8011 Otherwise, TMODE is ignored and the value generated in its natural mode.
8012 TMODE is just a suggestion; callers must assume that
8013 the rtx returned may not have mode TMODE.
8015 Note that TARGET may have neither TMODE nor MODE. In that case, it
8016 probably will not be used.
8018 If MODIFIER is EXPAND_SUM then when EXP is an addition
8019 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8020 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8021 products as above, or REG or MEM, or constant.
8022 Ordinarily in such cases we would output mul or add instructions
8023 and then return a pseudo reg containing the sum.
8025 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8026 it also marks a label as absolutely required (it can't be dead).
8027 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8028 This is used for outputting expressions used in initializers.
8030 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8031 with a constant address even if that address is not normally legitimate.
8032 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8034 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8035 a call parameter. Such targets require special care as we haven't yet
8036 marked TARGET so that it's safe from being trashed by libcalls. We
8037 don't want to use TARGET for anything but the final result;
8038 Intermediate values must go elsewhere. Additionally, calls to
8039 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8041 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8042 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8043 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8044 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8045 recursively.
8047 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8048 In this case, we don't adjust a returned MEM rtx that wouldn't be
8049 sufficiently aligned for its mode; instead, it's up to the caller
8050 to deal with it afterwards. This is used to make sure that unaligned
8051 base objects for which out-of-bounds accesses are supported, for
8052 example record types with trailing arrays, aren't realigned behind
8053 the back of the caller.
8054 The normal operating mode is to pass FALSE for this parameter. */
8057 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8058 enum expand_modifier modifier, rtx *alt_rtl,
8059 bool inner_reference_p)
8061 rtx ret;
8063 /* Handle ERROR_MARK before anybody tries to access its type. */
8064 if (TREE_CODE (exp) == ERROR_MARK
8065 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8067 ret = CONST0_RTX (tmode);
8068 return ret ? ret : const0_rtx;
8071 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8072 inner_reference_p);
8073 return ret;
8076 /* Try to expand the conditional expression which is represented by
8077 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8078 return the rtl reg which represents the result. Otherwise return
8079 NULL_RTX. */
8081 static rtx
8082 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8083 tree treeop1 ATTRIBUTE_UNUSED,
8084 tree treeop2 ATTRIBUTE_UNUSED)
8086 rtx insn;
8087 rtx op00, op01, op1, op2;
8088 enum rtx_code comparison_code;
8089 machine_mode comparison_mode;
8090 gimple *srcstmt;
8091 rtx temp;
8092 tree type = TREE_TYPE (treeop1);
8093 int unsignedp = TYPE_UNSIGNED (type);
8094 machine_mode mode = TYPE_MODE (type);
8095 machine_mode orig_mode = mode;
8096 static bool expanding_cond_expr_using_cmove = false;
8098 /* Conditional move expansion can end up TERing two operands which,
8099 when recursively hitting conditional expressions can result in
8100 exponential behavior if the cmove expansion ultimatively fails.
8101 It's hardly profitable to TER a cmove into a cmove so avoid doing
8102 that by failing early if we end up recursing. */
8103 if (expanding_cond_expr_using_cmove)
8104 return NULL_RTX;
8106 /* If we cannot do a conditional move on the mode, try doing it
8107 with the promoted mode. */
8108 if (!can_conditionally_move_p (mode))
8110 mode = promote_mode (type, mode, &unsignedp);
8111 if (!can_conditionally_move_p (mode))
8112 return NULL_RTX;
8113 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8115 else
8116 temp = assign_temp (type, 0, 1);
8118 expanding_cond_expr_using_cmove = true;
8119 start_sequence ();
8120 expand_operands (treeop1, treeop2,
8121 temp, &op1, &op2, EXPAND_NORMAL);
8123 if (TREE_CODE (treeop0) == SSA_NAME
8124 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8126 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8127 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8128 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8129 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8130 comparison_mode = TYPE_MODE (type);
8131 unsignedp = TYPE_UNSIGNED (type);
8132 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8134 else if (COMPARISON_CLASS_P (treeop0))
8136 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8137 enum tree_code cmpcode = TREE_CODE (treeop0);
8138 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8139 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8140 unsignedp = TYPE_UNSIGNED (type);
8141 comparison_mode = TYPE_MODE (type);
8142 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8144 else
8146 op00 = expand_normal (treeop0);
8147 op01 = const0_rtx;
8148 comparison_code = NE;
8149 comparison_mode = GET_MODE (op00);
8150 if (comparison_mode == VOIDmode)
8151 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8153 expanding_cond_expr_using_cmove = false;
8155 if (GET_MODE (op1) != mode)
8156 op1 = gen_lowpart (mode, op1);
8158 if (GET_MODE (op2) != mode)
8159 op2 = gen_lowpart (mode, op2);
8161 /* Try to emit the conditional move. */
8162 insn = emit_conditional_move (temp, comparison_code,
8163 op00, op01, comparison_mode,
8164 op1, op2, mode,
8165 unsignedp);
8167 /* If we could do the conditional move, emit the sequence,
8168 and return. */
8169 if (insn)
8171 rtx_insn *seq = get_insns ();
8172 end_sequence ();
8173 emit_insn (seq);
8174 return convert_modes (orig_mode, mode, temp, 0);
8177 /* Otherwise discard the sequence and fall back to code with
8178 branches. */
8179 end_sequence ();
8180 return NULL_RTX;
8184 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8185 enum expand_modifier modifier)
8187 rtx op0, op1, op2, temp;
8188 rtx_code_label *lab;
8189 tree type;
8190 int unsignedp;
8191 machine_mode mode;
8192 enum tree_code code = ops->code;
8193 optab this_optab;
8194 rtx subtarget, original_target;
8195 int ignore;
8196 bool reduce_bit_field;
8197 location_t loc = ops->location;
8198 tree treeop0, treeop1, treeop2;
8199 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8200 ? reduce_to_bit_field_precision ((expr), \
8201 target, \
8202 type) \
8203 : (expr))
8205 type = ops->type;
8206 mode = TYPE_MODE (type);
8207 unsignedp = TYPE_UNSIGNED (type);
8209 treeop0 = ops->op0;
8210 treeop1 = ops->op1;
8211 treeop2 = ops->op2;
8213 /* We should be called only on simple (binary or unary) expressions,
8214 exactly those that are valid in gimple expressions that aren't
8215 GIMPLE_SINGLE_RHS (or invalid). */
8216 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8217 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8218 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8220 ignore = (target == const0_rtx
8221 || ((CONVERT_EXPR_CODE_P (code)
8222 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8223 && TREE_CODE (type) == VOID_TYPE));
8225 /* We should be called only if we need the result. */
8226 gcc_assert (!ignore);
8228 /* An operation in what may be a bit-field type needs the
8229 result to be reduced to the precision of the bit-field type,
8230 which is narrower than that of the type's mode. */
8231 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8232 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8234 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8235 target = 0;
8237 /* Use subtarget as the target for operand 0 of a binary operation. */
8238 subtarget = get_subtarget (target);
8239 original_target = target;
8241 switch (code)
8243 case NON_LVALUE_EXPR:
8244 case PAREN_EXPR:
8245 CASE_CONVERT:
8246 if (treeop0 == error_mark_node)
8247 return const0_rtx;
8249 if (TREE_CODE (type) == UNION_TYPE)
8251 tree valtype = TREE_TYPE (treeop0);
8253 /* If both input and output are BLKmode, this conversion isn't doing
8254 anything except possibly changing memory attribute. */
8255 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8257 rtx result = expand_expr (treeop0, target, tmode,
8258 modifier);
8260 result = copy_rtx (result);
8261 set_mem_attributes (result, type, 0);
8262 return result;
8265 if (target == 0)
8267 if (TYPE_MODE (type) != BLKmode)
8268 target = gen_reg_rtx (TYPE_MODE (type));
8269 else
8270 target = assign_temp (type, 1, 1);
8273 if (MEM_P (target))
8274 /* Store data into beginning of memory target. */
8275 store_expr (treeop0,
8276 adjust_address (target, TYPE_MODE (valtype), 0),
8277 modifier == EXPAND_STACK_PARM,
8278 false, TYPE_REVERSE_STORAGE_ORDER (type));
8280 else
8282 gcc_assert (REG_P (target)
8283 && !TYPE_REVERSE_STORAGE_ORDER (type));
8285 /* Store this field into a union of the proper type. */
8286 store_field (target,
8287 MIN ((int_size_in_bytes (TREE_TYPE
8288 (treeop0))
8289 * BITS_PER_UNIT),
8290 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8291 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8292 false, false);
8295 /* Return the entire union. */
8296 return target;
8299 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8301 op0 = expand_expr (treeop0, target, VOIDmode,
8302 modifier);
8304 /* If the signedness of the conversion differs and OP0 is
8305 a promoted SUBREG, clear that indication since we now
8306 have to do the proper extension. */
8307 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8308 && GET_CODE (op0) == SUBREG)
8309 SUBREG_PROMOTED_VAR_P (op0) = 0;
8311 return REDUCE_BIT_FIELD (op0);
8314 op0 = expand_expr (treeop0, NULL_RTX, mode,
8315 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8316 if (GET_MODE (op0) == mode)
8319 /* If OP0 is a constant, just convert it into the proper mode. */
8320 else if (CONSTANT_P (op0))
8322 tree inner_type = TREE_TYPE (treeop0);
8323 machine_mode inner_mode = GET_MODE (op0);
8325 if (inner_mode == VOIDmode)
8326 inner_mode = TYPE_MODE (inner_type);
8328 if (modifier == EXPAND_INITIALIZER)
8329 op0 = lowpart_subreg (mode, op0, inner_mode);
8330 else
8331 op0= convert_modes (mode, inner_mode, op0,
8332 TYPE_UNSIGNED (inner_type));
8335 else if (modifier == EXPAND_INITIALIZER)
8336 op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8338 else if (target == 0)
8339 op0 = convert_to_mode (mode, op0,
8340 TYPE_UNSIGNED (TREE_TYPE
8341 (treeop0)));
8342 else
8344 convert_move (target, op0,
8345 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8346 op0 = target;
8349 return REDUCE_BIT_FIELD (op0);
8351 case ADDR_SPACE_CONVERT_EXPR:
8353 tree treeop0_type = TREE_TYPE (treeop0);
8355 gcc_assert (POINTER_TYPE_P (type));
8356 gcc_assert (POINTER_TYPE_P (treeop0_type));
8358 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8359 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8361 /* Conversions between pointers to the same address space should
8362 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8363 gcc_assert (as_to != as_from);
8365 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8367 /* Ask target code to handle conversion between pointers
8368 to overlapping address spaces. */
8369 if (targetm.addr_space.subset_p (as_to, as_from)
8370 || targetm.addr_space.subset_p (as_from, as_to))
8372 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8374 else
8376 /* For disjoint address spaces, converting anything but a null
8377 pointer invokes undefined behavior. We truncate or extend the
8378 value as if we'd converted via integers, which handles 0 as
8379 required, and all others as the programmer likely expects. */
8380 #ifndef POINTERS_EXTEND_UNSIGNED
8381 const int POINTERS_EXTEND_UNSIGNED = 1;
8382 #endif
8383 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8384 op0, POINTERS_EXTEND_UNSIGNED);
8386 gcc_assert (op0);
8387 return op0;
8390 case POINTER_PLUS_EXPR:
8391 /* Even though the sizetype mode and the pointer's mode can be different
8392 expand is able to handle this correctly and get the correct result out
8393 of the PLUS_EXPR code. */
8394 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8395 if sizetype precision is smaller than pointer precision. */
8396 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8397 treeop1 = fold_convert_loc (loc, type,
8398 fold_convert_loc (loc, ssizetype,
8399 treeop1));
8400 /* If sizetype precision is larger than pointer precision, truncate the
8401 offset to have matching modes. */
8402 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8403 treeop1 = fold_convert_loc (loc, type, treeop1);
8404 /* FALLTHRU */
8406 case PLUS_EXPR:
8407 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8408 something else, make sure we add the register to the constant and
8409 then to the other thing. This case can occur during strength
8410 reduction and doing it this way will produce better code if the
8411 frame pointer or argument pointer is eliminated.
8413 fold-const.c will ensure that the constant is always in the inner
8414 PLUS_EXPR, so the only case we need to do anything about is if
8415 sp, ap, or fp is our second argument, in which case we must swap
8416 the innermost first argument and our second argument. */
8418 if (TREE_CODE (treeop0) == PLUS_EXPR
8419 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8420 && VAR_P (treeop1)
8421 && (DECL_RTL (treeop1) == frame_pointer_rtx
8422 || DECL_RTL (treeop1) == stack_pointer_rtx
8423 || DECL_RTL (treeop1) == arg_pointer_rtx))
8425 gcc_unreachable ();
8428 /* If the result is to be ptr_mode and we are adding an integer to
8429 something, we might be forming a constant. So try to use
8430 plus_constant. If it produces a sum and we can't accept it,
8431 use force_operand. This allows P = &ARR[const] to generate
8432 efficient code on machines where a SYMBOL_REF is not a valid
8433 address.
8435 If this is an EXPAND_SUM call, always return the sum. */
8436 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8437 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8439 if (modifier == EXPAND_STACK_PARM)
8440 target = 0;
8441 if (TREE_CODE (treeop0) == INTEGER_CST
8442 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8443 && TREE_CONSTANT (treeop1))
8445 rtx constant_part;
8446 HOST_WIDE_INT wc;
8447 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8449 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8450 EXPAND_SUM);
8451 /* Use wi::shwi to ensure that the constant is
8452 truncated according to the mode of OP1, then sign extended
8453 to a HOST_WIDE_INT. Using the constant directly can result
8454 in non-canonical RTL in a 64x32 cross compile. */
8455 wc = TREE_INT_CST_LOW (treeop0);
8456 constant_part =
8457 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8458 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8459 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8460 op1 = force_operand (op1, target);
8461 return REDUCE_BIT_FIELD (op1);
8464 else if (TREE_CODE (treeop1) == INTEGER_CST
8465 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8466 && TREE_CONSTANT (treeop0))
8468 rtx constant_part;
8469 HOST_WIDE_INT wc;
8470 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8472 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8473 (modifier == EXPAND_INITIALIZER
8474 ? EXPAND_INITIALIZER : EXPAND_SUM));
8475 if (! CONSTANT_P (op0))
8477 op1 = expand_expr (treeop1, NULL_RTX,
8478 VOIDmode, modifier);
8479 /* Return a PLUS if modifier says it's OK. */
8480 if (modifier == EXPAND_SUM
8481 || modifier == EXPAND_INITIALIZER)
8482 return simplify_gen_binary (PLUS, mode, op0, op1);
8483 goto binop2;
8485 /* Use wi::shwi to ensure that the constant is
8486 truncated according to the mode of OP1, then sign extended
8487 to a HOST_WIDE_INT. Using the constant directly can result
8488 in non-canonical RTL in a 64x32 cross compile. */
8489 wc = TREE_INT_CST_LOW (treeop1);
8490 constant_part
8491 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8492 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8493 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8494 op0 = force_operand (op0, target);
8495 return REDUCE_BIT_FIELD (op0);
8499 /* Use TER to expand pointer addition of a negated value
8500 as pointer subtraction. */
8501 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8502 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8503 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8504 && TREE_CODE (treeop1) == SSA_NAME
8505 && TYPE_MODE (TREE_TYPE (treeop0))
8506 == TYPE_MODE (TREE_TYPE (treeop1)))
8508 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8509 if (def)
8511 treeop1 = gimple_assign_rhs1 (def);
8512 code = MINUS_EXPR;
8513 goto do_minus;
8517 /* No sense saving up arithmetic to be done
8518 if it's all in the wrong mode to form part of an address.
8519 And force_operand won't know whether to sign-extend or
8520 zero-extend. */
8521 if (modifier != EXPAND_INITIALIZER
8522 && (modifier != EXPAND_SUM || mode != ptr_mode))
8524 expand_operands (treeop0, treeop1,
8525 subtarget, &op0, &op1, modifier);
8526 if (op0 == const0_rtx)
8527 return op1;
8528 if (op1 == const0_rtx)
8529 return op0;
8530 goto binop2;
8533 expand_operands (treeop0, treeop1,
8534 subtarget, &op0, &op1, modifier);
8535 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8537 case MINUS_EXPR:
8538 do_minus:
8539 /* For initializers, we are allowed to return a MINUS of two
8540 symbolic constants. Here we handle all cases when both operands
8541 are constant. */
8542 /* Handle difference of two symbolic constants,
8543 for the sake of an initializer. */
8544 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8545 && really_constant_p (treeop0)
8546 && really_constant_p (treeop1))
8548 expand_operands (treeop0, treeop1,
8549 NULL_RTX, &op0, &op1, modifier);
8551 /* If the last operand is a CONST_INT, use plus_constant of
8552 the negated constant. Else make the MINUS. */
8553 if (CONST_INT_P (op1))
8554 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8555 -INTVAL (op1)));
8556 else
8557 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8560 /* No sense saving up arithmetic to be done
8561 if it's all in the wrong mode to form part of an address.
8562 And force_operand won't know whether to sign-extend or
8563 zero-extend. */
8564 if (modifier != EXPAND_INITIALIZER
8565 && (modifier != EXPAND_SUM || mode != ptr_mode))
8566 goto binop;
8568 expand_operands (treeop0, treeop1,
8569 subtarget, &op0, &op1, modifier);
8571 /* Convert A - const to A + (-const). */
8572 if (CONST_INT_P (op1))
8574 op1 = negate_rtx (mode, op1);
8575 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8578 goto binop2;
8580 case WIDEN_MULT_PLUS_EXPR:
8581 case WIDEN_MULT_MINUS_EXPR:
8582 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8583 op2 = expand_normal (treeop2);
8584 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8585 target, unsignedp);
8586 return target;
8588 case WIDEN_MULT_EXPR:
8589 /* If first operand is constant, swap them.
8590 Thus the following special case checks need only
8591 check the second operand. */
8592 if (TREE_CODE (treeop0) == INTEGER_CST)
8593 std::swap (treeop0, treeop1);
8595 /* First, check if we have a multiplication of one signed and one
8596 unsigned operand. */
8597 if (TREE_CODE (treeop1) != INTEGER_CST
8598 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8599 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8601 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8602 this_optab = usmul_widen_optab;
8603 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8604 != CODE_FOR_nothing)
8606 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8607 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8608 EXPAND_NORMAL);
8609 else
8610 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8611 EXPAND_NORMAL);
8612 /* op0 and op1 might still be constant, despite the above
8613 != INTEGER_CST check. Handle it. */
8614 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8616 op0 = convert_modes (innermode, mode, op0, true);
8617 op1 = convert_modes (innermode, mode, op1, false);
8618 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8619 target, unsignedp));
8621 goto binop3;
8624 /* Check for a multiplication with matching signedness. */
8625 else if ((TREE_CODE (treeop1) == INTEGER_CST
8626 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8627 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8628 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8630 tree op0type = TREE_TYPE (treeop0);
8631 machine_mode innermode = TYPE_MODE (op0type);
8632 bool zextend_p = TYPE_UNSIGNED (op0type);
8633 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8634 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8636 if (TREE_CODE (treeop0) != INTEGER_CST)
8638 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8639 != CODE_FOR_nothing)
8641 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8642 EXPAND_NORMAL);
8643 /* op0 and op1 might still be constant, despite the above
8644 != INTEGER_CST check. Handle it. */
8645 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8647 widen_mult_const:
8648 op0 = convert_modes (innermode, mode, op0, zextend_p);
8650 = convert_modes (innermode, mode, op1,
8651 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8652 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8653 target,
8654 unsignedp));
8656 temp = expand_widening_mult (mode, op0, op1, target,
8657 unsignedp, this_optab);
8658 return REDUCE_BIT_FIELD (temp);
8660 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8661 != CODE_FOR_nothing
8662 && innermode == word_mode)
8664 rtx htem, hipart;
8665 op0 = expand_normal (treeop0);
8666 if (TREE_CODE (treeop1) == INTEGER_CST)
8667 op1 = convert_modes (innermode, mode,
8668 expand_normal (treeop1),
8669 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8670 else
8671 op1 = expand_normal (treeop1);
8672 /* op0 and op1 might still be constant, despite the above
8673 != INTEGER_CST check. Handle it. */
8674 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8675 goto widen_mult_const;
8676 temp = expand_binop (mode, other_optab, op0, op1, target,
8677 unsignedp, OPTAB_LIB_WIDEN);
8678 hipart = gen_highpart (innermode, temp);
8679 htem = expand_mult_highpart_adjust (innermode, hipart,
8680 op0, op1, hipart,
8681 zextend_p);
8682 if (htem != hipart)
8683 emit_move_insn (hipart, htem);
8684 return REDUCE_BIT_FIELD (temp);
8688 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8689 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8690 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8691 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8693 case FMA_EXPR:
8695 optab opt = fma_optab;
8696 gimple *def0, *def2;
8698 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8699 call. */
8700 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8702 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8703 tree call_expr;
8705 gcc_assert (fn != NULL_TREE);
8706 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8707 return expand_builtin (call_expr, target, subtarget, mode, false);
8710 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8711 /* The multiplication is commutative - look at its 2nd operand
8712 if the first isn't fed by a negate. */
8713 if (!def0)
8715 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8716 /* Swap operands if the 2nd operand is fed by a negate. */
8717 if (def0)
8718 std::swap (treeop0, treeop1);
8720 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8722 op0 = op2 = NULL;
8724 if (def0 && def2
8725 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8727 opt = fnms_optab;
8728 op0 = expand_normal (gimple_assign_rhs1 (def0));
8729 op2 = expand_normal (gimple_assign_rhs1 (def2));
8731 else if (def0
8732 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8734 opt = fnma_optab;
8735 op0 = expand_normal (gimple_assign_rhs1 (def0));
8737 else if (def2
8738 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8740 opt = fms_optab;
8741 op2 = expand_normal (gimple_assign_rhs1 (def2));
8744 if (op0 == NULL)
8745 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8746 if (op2 == NULL)
8747 op2 = expand_normal (treeop2);
8748 op1 = expand_normal (treeop1);
8750 return expand_ternary_op (TYPE_MODE (type), opt,
8751 op0, op1, op2, target, 0);
8754 case MULT_EXPR:
8755 /* If this is a fixed-point operation, then we cannot use the code
8756 below because "expand_mult" doesn't support sat/no-sat fixed-point
8757 multiplications. */
8758 if (ALL_FIXED_POINT_MODE_P (mode))
8759 goto binop;
8761 /* If first operand is constant, swap them.
8762 Thus the following special case checks need only
8763 check the second operand. */
8764 if (TREE_CODE (treeop0) == INTEGER_CST)
8765 std::swap (treeop0, treeop1);
8767 /* Attempt to return something suitable for generating an
8768 indexed address, for machines that support that. */
8770 if (modifier == EXPAND_SUM && mode == ptr_mode
8771 && tree_fits_shwi_p (treeop1))
8773 tree exp1 = treeop1;
8775 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8776 EXPAND_SUM);
8778 if (!REG_P (op0))
8779 op0 = force_operand (op0, NULL_RTX);
8780 if (!REG_P (op0))
8781 op0 = copy_to_mode_reg (mode, op0);
8783 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8784 gen_int_mode (tree_to_shwi (exp1),
8785 TYPE_MODE (TREE_TYPE (exp1)))));
8788 if (modifier == EXPAND_STACK_PARM)
8789 target = 0;
8791 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8792 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8794 case TRUNC_DIV_EXPR:
8795 case FLOOR_DIV_EXPR:
8796 case CEIL_DIV_EXPR:
8797 case ROUND_DIV_EXPR:
8798 case EXACT_DIV_EXPR:
8799 /* If this is a fixed-point operation, then we cannot use the code
8800 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8801 divisions. */
8802 if (ALL_FIXED_POINT_MODE_P (mode))
8803 goto binop;
8805 if (modifier == EXPAND_STACK_PARM)
8806 target = 0;
8807 /* Possible optimization: compute the dividend with EXPAND_SUM
8808 then if the divisor is constant can optimize the case
8809 where some terms of the dividend have coeffs divisible by it. */
8810 expand_operands (treeop0, treeop1,
8811 subtarget, &op0, &op1, EXPAND_NORMAL);
8812 if (SCALAR_INT_MODE_P (mode)
8813 && optimize >= 2
8814 && get_range_pos_neg (treeop0) == 1
8815 && get_range_pos_neg (treeop1) == 1)
8817 /* If both arguments are known to be positive when interpreted
8818 as signed, we can expand it as both signed and unsigned
8819 division or modulo. Choose the cheaper sequence in that case. */
8820 bool speed_p = optimize_insn_for_speed_p ();
8821 do_pending_stack_adjust ();
8822 start_sequence ();
8823 rtx uns_ret = expand_divmod (0, code, mode, op0, op1, target, 1);
8824 rtx_insn *uns_insns = get_insns ();
8825 end_sequence ();
8826 start_sequence ();
8827 rtx sgn_ret = expand_divmod (0, code, mode, op0, op1, target, 0);
8828 rtx_insn *sgn_insns = get_insns ();
8829 end_sequence ();
8830 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8831 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8832 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8834 emit_insn (uns_insns);
8835 return uns_ret;
8837 emit_insn (sgn_insns);
8838 return sgn_ret;
8840 return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8842 case RDIV_EXPR:
8843 goto binop;
8845 case MULT_HIGHPART_EXPR:
8846 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8847 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8848 gcc_assert (temp);
8849 return temp;
8851 case TRUNC_MOD_EXPR:
8852 case FLOOR_MOD_EXPR:
8853 case CEIL_MOD_EXPR:
8854 case ROUND_MOD_EXPR:
8855 if (modifier == EXPAND_STACK_PARM)
8856 target = 0;
8857 expand_operands (treeop0, treeop1,
8858 subtarget, &op0, &op1, EXPAND_NORMAL);
8859 return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8861 case FIXED_CONVERT_EXPR:
8862 op0 = expand_normal (treeop0);
8863 if (target == 0 || modifier == EXPAND_STACK_PARM)
8864 target = gen_reg_rtx (mode);
8866 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8867 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8868 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8869 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8870 else
8871 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8872 return target;
8874 case FIX_TRUNC_EXPR:
8875 op0 = expand_normal (treeop0);
8876 if (target == 0 || modifier == EXPAND_STACK_PARM)
8877 target = gen_reg_rtx (mode);
8878 expand_fix (target, op0, unsignedp);
8879 return target;
8881 case FLOAT_EXPR:
8882 op0 = expand_normal (treeop0);
8883 if (target == 0 || modifier == EXPAND_STACK_PARM)
8884 target = gen_reg_rtx (mode);
8885 /* expand_float can't figure out what to do if FROM has VOIDmode.
8886 So give it the correct mode. With -O, cse will optimize this. */
8887 if (GET_MODE (op0) == VOIDmode)
8888 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8889 op0);
8890 expand_float (target, op0,
8891 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8892 return target;
8894 case NEGATE_EXPR:
8895 op0 = expand_expr (treeop0, subtarget,
8896 VOIDmode, EXPAND_NORMAL);
8897 if (modifier == EXPAND_STACK_PARM)
8898 target = 0;
8899 temp = expand_unop (mode,
8900 optab_for_tree_code (NEGATE_EXPR, type,
8901 optab_default),
8902 op0, target, 0);
8903 gcc_assert (temp);
8904 return REDUCE_BIT_FIELD (temp);
8906 case ABS_EXPR:
8907 op0 = expand_expr (treeop0, subtarget,
8908 VOIDmode, EXPAND_NORMAL);
8909 if (modifier == EXPAND_STACK_PARM)
8910 target = 0;
8912 /* ABS_EXPR is not valid for complex arguments. */
8913 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8914 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8916 /* Unsigned abs is simply the operand. Testing here means we don't
8917 risk generating incorrect code below. */
8918 if (TYPE_UNSIGNED (type))
8919 return op0;
8921 return expand_abs (mode, op0, target, unsignedp,
8922 safe_from_p (target, treeop0, 1));
8924 case MAX_EXPR:
8925 case MIN_EXPR:
8926 target = original_target;
8927 if (target == 0
8928 || modifier == EXPAND_STACK_PARM
8929 || (MEM_P (target) && MEM_VOLATILE_P (target))
8930 || GET_MODE (target) != mode
8931 || (REG_P (target)
8932 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8933 target = gen_reg_rtx (mode);
8934 expand_operands (treeop0, treeop1,
8935 target, &op0, &op1, EXPAND_NORMAL);
8937 /* First try to do it with a special MIN or MAX instruction.
8938 If that does not win, use a conditional jump to select the proper
8939 value. */
8940 this_optab = optab_for_tree_code (code, type, optab_default);
8941 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8942 OPTAB_WIDEN);
8943 if (temp != 0)
8944 return temp;
8946 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8947 and similarly for MAX <x, y>. */
8948 if (VECTOR_TYPE_P (type))
8950 tree t0 = make_tree (type, op0);
8951 tree t1 = make_tree (type, op1);
8952 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
8953 type, t0, t1);
8954 return expand_vec_cond_expr (type, comparison, t0, t1,
8955 original_target);
8958 /* At this point, a MEM target is no longer useful; we will get better
8959 code without it. */
8961 if (! REG_P (target))
8962 target = gen_reg_rtx (mode);
8964 /* If op1 was placed in target, swap op0 and op1. */
8965 if (target != op0 && target == op1)
8966 std::swap (op0, op1);
8968 /* We generate better code and avoid problems with op1 mentioning
8969 target by forcing op1 into a pseudo if it isn't a constant. */
8970 if (! CONSTANT_P (op1))
8971 op1 = force_reg (mode, op1);
8974 enum rtx_code comparison_code;
8975 rtx cmpop1 = op1;
8977 if (code == MAX_EXPR)
8978 comparison_code = unsignedp ? GEU : GE;
8979 else
8980 comparison_code = unsignedp ? LEU : LE;
8982 /* Canonicalize to comparisons against 0. */
8983 if (op1 == const1_rtx)
8985 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8986 or (a != 0 ? a : 1) for unsigned.
8987 For MIN we are safe converting (a <= 1 ? a : 1)
8988 into (a <= 0 ? a : 1) */
8989 cmpop1 = const0_rtx;
8990 if (code == MAX_EXPR)
8991 comparison_code = unsignedp ? NE : GT;
8993 if (op1 == constm1_rtx && !unsignedp)
8995 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8996 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8997 cmpop1 = const0_rtx;
8998 if (code == MIN_EXPR)
8999 comparison_code = LT;
9002 /* Use a conditional move if possible. */
9003 if (can_conditionally_move_p (mode))
9005 rtx insn;
9007 start_sequence ();
9009 /* Try to emit the conditional move. */
9010 insn = emit_conditional_move (target, comparison_code,
9011 op0, cmpop1, mode,
9012 op0, op1, mode,
9013 unsignedp);
9015 /* If we could do the conditional move, emit the sequence,
9016 and return. */
9017 if (insn)
9019 rtx_insn *seq = get_insns ();
9020 end_sequence ();
9021 emit_insn (seq);
9022 return target;
9025 /* Otherwise discard the sequence and fall back to code with
9026 branches. */
9027 end_sequence ();
9030 if (target != op0)
9031 emit_move_insn (target, op0);
9033 lab = gen_label_rtx ();
9034 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9035 unsignedp, mode, NULL_RTX, NULL, lab,
9036 -1);
9038 emit_move_insn (target, op1);
9039 emit_label (lab);
9040 return target;
9042 case BIT_NOT_EXPR:
9043 op0 = expand_expr (treeop0, subtarget,
9044 VOIDmode, EXPAND_NORMAL);
9045 if (modifier == EXPAND_STACK_PARM)
9046 target = 0;
9047 /* In case we have to reduce the result to bitfield precision
9048 for unsigned bitfield expand this as XOR with a proper constant
9049 instead. */
9050 if (reduce_bit_field && TYPE_UNSIGNED (type))
9052 wide_int mask = wi::mask (TYPE_PRECISION (type),
9053 false, GET_MODE_PRECISION (mode));
9055 temp = expand_binop (mode, xor_optab, op0,
9056 immed_wide_int_const (mask, mode),
9057 target, 1, OPTAB_LIB_WIDEN);
9059 else
9060 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9061 gcc_assert (temp);
9062 return temp;
9064 /* ??? Can optimize bitwise operations with one arg constant.
9065 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9066 and (a bitwise1 b) bitwise2 b (etc)
9067 but that is probably not worth while. */
9069 case BIT_AND_EXPR:
9070 case BIT_IOR_EXPR:
9071 case BIT_XOR_EXPR:
9072 goto binop;
9074 case LROTATE_EXPR:
9075 case RROTATE_EXPR:
9076 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9077 || (GET_MODE_PRECISION (TYPE_MODE (type))
9078 == TYPE_PRECISION (type)));
9079 /* fall through */
9081 case LSHIFT_EXPR:
9082 case RSHIFT_EXPR:
9084 /* If this is a fixed-point operation, then we cannot use the code
9085 below because "expand_shift" doesn't support sat/no-sat fixed-point
9086 shifts. */
9087 if (ALL_FIXED_POINT_MODE_P (mode))
9088 goto binop;
9090 if (! safe_from_p (subtarget, treeop1, 1))
9091 subtarget = 0;
9092 if (modifier == EXPAND_STACK_PARM)
9093 target = 0;
9094 op0 = expand_expr (treeop0, subtarget,
9095 VOIDmode, EXPAND_NORMAL);
9097 /* Left shift optimization when shifting across word_size boundary.
9099 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9100 there isn't native instruction to support this wide mode
9101 left shift. Given below scenario:
9103 Type A = (Type) B << C
9105 |< T >|
9106 | dest_high | dest_low |
9108 | word_size |
9110 If the shift amount C caused we shift B to across the word
9111 size boundary, i.e part of B shifted into high half of
9112 destination register, and part of B remains in the low
9113 half, then GCC will use the following left shift expand
9114 logic:
9116 1. Initialize dest_low to B.
9117 2. Initialize every bit of dest_high to the sign bit of B.
9118 3. Logic left shift dest_low by C bit to finalize dest_low.
9119 The value of dest_low before this shift is kept in a temp D.
9120 4. Logic left shift dest_high by C.
9121 5. Logic right shift D by (word_size - C).
9122 6. Or the result of 4 and 5 to finalize dest_high.
9124 While, by checking gimple statements, if operand B is
9125 coming from signed extension, then we can simplify above
9126 expand logic into:
9128 1. dest_high = src_low >> (word_size - C).
9129 2. dest_low = src_low << C.
9131 We can use one arithmetic right shift to finish all the
9132 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9133 needed from 6 into 2.
9135 The case is similar for zero extension, except that we
9136 initialize dest_high to zero rather than copies of the sign
9137 bit from B. Furthermore, we need to use a logical right shift
9138 in this case.
9140 The choice of sign-extension versus zero-extension is
9141 determined entirely by whether or not B is signed and is
9142 independent of the current setting of unsignedp. */
9144 temp = NULL_RTX;
9145 if (code == LSHIFT_EXPR
9146 && target
9147 && REG_P (target)
9148 && mode == GET_MODE_WIDER_MODE (word_mode)
9149 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
9150 && TREE_CONSTANT (treeop1)
9151 && TREE_CODE (treeop0) == SSA_NAME)
9153 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9154 if (is_gimple_assign (def)
9155 && gimple_assign_rhs_code (def) == NOP_EXPR)
9157 machine_mode rmode = TYPE_MODE
9158 (TREE_TYPE (gimple_assign_rhs1 (def)));
9160 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
9161 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9162 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9163 >= GET_MODE_BITSIZE (word_mode)))
9165 rtx_insn *seq, *seq_old;
9166 unsigned int high_off = subreg_highpart_offset (word_mode,
9167 mode);
9168 bool extend_unsigned
9169 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9170 rtx low = lowpart_subreg (word_mode, op0, mode);
9171 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9172 rtx dest_high = simplify_gen_subreg (word_mode, target,
9173 mode, high_off);
9174 HOST_WIDE_INT ramount = (BITS_PER_WORD
9175 - TREE_INT_CST_LOW (treeop1));
9176 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9178 start_sequence ();
9179 /* dest_high = src_low >> (word_size - C). */
9180 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9181 rshift, dest_high,
9182 extend_unsigned);
9183 if (temp != dest_high)
9184 emit_move_insn (dest_high, temp);
9186 /* dest_low = src_low << C. */
9187 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9188 treeop1, dest_low, unsignedp);
9189 if (temp != dest_low)
9190 emit_move_insn (dest_low, temp);
9192 seq = get_insns ();
9193 end_sequence ();
9194 temp = target ;
9196 if (have_insn_for (ASHIFT, mode))
9198 bool speed_p = optimize_insn_for_speed_p ();
9199 start_sequence ();
9200 rtx ret_old = expand_variable_shift (code, mode, op0,
9201 treeop1, target,
9202 unsignedp);
9204 seq_old = get_insns ();
9205 end_sequence ();
9206 if (seq_cost (seq, speed_p)
9207 >= seq_cost (seq_old, speed_p))
9209 seq = seq_old;
9210 temp = ret_old;
9213 emit_insn (seq);
9218 if (temp == NULL_RTX)
9219 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9220 unsignedp);
9221 if (code == LSHIFT_EXPR)
9222 temp = REDUCE_BIT_FIELD (temp);
9223 return temp;
9226 /* Could determine the answer when only additive constants differ. Also,
9227 the addition of one can be handled by changing the condition. */
9228 case LT_EXPR:
9229 case LE_EXPR:
9230 case GT_EXPR:
9231 case GE_EXPR:
9232 case EQ_EXPR:
9233 case NE_EXPR:
9234 case UNORDERED_EXPR:
9235 case ORDERED_EXPR:
9236 case UNLT_EXPR:
9237 case UNLE_EXPR:
9238 case UNGT_EXPR:
9239 case UNGE_EXPR:
9240 case UNEQ_EXPR:
9241 case LTGT_EXPR:
9243 temp = do_store_flag (ops,
9244 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9245 tmode != VOIDmode ? tmode : mode);
9246 if (temp)
9247 return temp;
9249 /* Use a compare and a jump for BLKmode comparisons, or for function
9250 type comparisons is have_canonicalize_funcptr_for_compare. */
9252 if ((target == 0
9253 || modifier == EXPAND_STACK_PARM
9254 || ! safe_from_p (target, treeop0, 1)
9255 || ! safe_from_p (target, treeop1, 1)
9256 /* Make sure we don't have a hard reg (such as function's return
9257 value) live across basic blocks, if not optimizing. */
9258 || (!optimize && REG_P (target)
9259 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9260 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9262 emit_move_insn (target, const0_rtx);
9264 rtx_code_label *lab1 = gen_label_rtx ();
9265 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9267 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9268 emit_move_insn (target, constm1_rtx);
9269 else
9270 emit_move_insn (target, const1_rtx);
9272 emit_label (lab1);
9273 return target;
9275 case COMPLEX_EXPR:
9276 /* Get the rtx code of the operands. */
9277 op0 = expand_normal (treeop0);
9278 op1 = expand_normal (treeop1);
9280 if (!target)
9281 target = gen_reg_rtx (TYPE_MODE (type));
9282 else
9283 /* If target overlaps with op1, then either we need to force
9284 op1 into a pseudo (if target also overlaps with op0),
9285 or write the complex parts in reverse order. */
9286 switch (GET_CODE (target))
9288 case CONCAT:
9289 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9291 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9293 complex_expr_force_op1:
9294 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9295 emit_move_insn (temp, op1);
9296 op1 = temp;
9297 break;
9299 complex_expr_swap_order:
9300 /* Move the imaginary (op1) and real (op0) parts to their
9301 location. */
9302 write_complex_part (target, op1, true);
9303 write_complex_part (target, op0, false);
9305 return target;
9307 break;
9308 case MEM:
9309 temp = adjust_address_nv (target,
9310 GET_MODE_INNER (GET_MODE (target)), 0);
9311 if (reg_overlap_mentioned_p (temp, op1))
9313 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9314 temp = adjust_address_nv (target, imode,
9315 GET_MODE_SIZE (imode));
9316 if (reg_overlap_mentioned_p (temp, op0))
9317 goto complex_expr_force_op1;
9318 goto complex_expr_swap_order;
9320 break;
9321 default:
9322 if (reg_overlap_mentioned_p (target, op1))
9324 if (reg_overlap_mentioned_p (target, op0))
9325 goto complex_expr_force_op1;
9326 goto complex_expr_swap_order;
9328 break;
9331 /* Move the real (op0) and imaginary (op1) parts to their location. */
9332 write_complex_part (target, op0, false);
9333 write_complex_part (target, op1, true);
9335 return target;
9337 case WIDEN_SUM_EXPR:
9339 tree oprnd0 = treeop0;
9340 tree oprnd1 = treeop1;
9342 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9343 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9344 target, unsignedp);
9345 return target;
9348 case REDUC_MAX_EXPR:
9349 case REDUC_MIN_EXPR:
9350 case REDUC_PLUS_EXPR:
9352 op0 = expand_normal (treeop0);
9353 this_optab = optab_for_tree_code (code, type, optab_default);
9354 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9356 struct expand_operand ops[2];
9357 enum insn_code icode = optab_handler (this_optab, vec_mode);
9359 create_output_operand (&ops[0], target, mode);
9360 create_input_operand (&ops[1], op0, vec_mode);
9361 expand_insn (icode, 2, ops);
9362 target = ops[0].value;
9363 if (GET_MODE (target) != mode)
9364 return gen_lowpart (tmode, target);
9365 return target;
9368 case VEC_UNPACK_HI_EXPR:
9369 case VEC_UNPACK_LO_EXPR:
9371 op0 = expand_normal (treeop0);
9372 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9373 target, unsignedp);
9374 gcc_assert (temp);
9375 return temp;
9378 case VEC_UNPACK_FLOAT_HI_EXPR:
9379 case VEC_UNPACK_FLOAT_LO_EXPR:
9381 op0 = expand_normal (treeop0);
9382 /* The signedness is determined from input operand. */
9383 temp = expand_widen_pattern_expr
9384 (ops, op0, NULL_RTX, NULL_RTX,
9385 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9387 gcc_assert (temp);
9388 return temp;
9391 case VEC_WIDEN_MULT_HI_EXPR:
9392 case VEC_WIDEN_MULT_LO_EXPR:
9393 case VEC_WIDEN_MULT_EVEN_EXPR:
9394 case VEC_WIDEN_MULT_ODD_EXPR:
9395 case VEC_WIDEN_LSHIFT_HI_EXPR:
9396 case VEC_WIDEN_LSHIFT_LO_EXPR:
9397 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9398 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9399 target, unsignedp);
9400 gcc_assert (target);
9401 return target;
9403 case VEC_PACK_TRUNC_EXPR:
9404 case VEC_PACK_SAT_EXPR:
9405 case VEC_PACK_FIX_TRUNC_EXPR:
9406 mode = TYPE_MODE (TREE_TYPE (treeop0));
9407 goto binop;
9409 case VEC_PERM_EXPR:
9410 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9411 op2 = expand_normal (treeop2);
9413 /* Careful here: if the target doesn't support integral vector modes,
9414 a constant selection vector could wind up smooshed into a normal
9415 integral constant. */
9416 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9418 tree sel_type = TREE_TYPE (treeop2);
9419 machine_mode vmode
9420 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9421 TYPE_VECTOR_SUBPARTS (sel_type));
9422 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9423 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9424 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9426 else
9427 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9429 temp = expand_vec_perm (mode, op0, op1, op2, target);
9430 gcc_assert (temp);
9431 return temp;
9433 case DOT_PROD_EXPR:
9435 tree oprnd0 = treeop0;
9436 tree oprnd1 = treeop1;
9437 tree oprnd2 = treeop2;
9438 rtx op2;
9440 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9441 op2 = expand_normal (oprnd2);
9442 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9443 target, unsignedp);
9444 return target;
9447 case SAD_EXPR:
9449 tree oprnd0 = treeop0;
9450 tree oprnd1 = treeop1;
9451 tree oprnd2 = treeop2;
9452 rtx op2;
9454 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9455 op2 = expand_normal (oprnd2);
9456 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9457 target, unsignedp);
9458 return target;
9461 case REALIGN_LOAD_EXPR:
9463 tree oprnd0 = treeop0;
9464 tree oprnd1 = treeop1;
9465 tree oprnd2 = treeop2;
9466 rtx op2;
9468 this_optab = optab_for_tree_code (code, type, optab_default);
9469 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9470 op2 = expand_normal (oprnd2);
9471 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9472 target, unsignedp);
9473 gcc_assert (temp);
9474 return temp;
9477 case COND_EXPR:
9479 /* A COND_EXPR with its type being VOID_TYPE represents a
9480 conditional jump and is handled in
9481 expand_gimple_cond_expr. */
9482 gcc_assert (!VOID_TYPE_P (type));
9484 /* Note that COND_EXPRs whose type is a structure or union
9485 are required to be constructed to contain assignments of
9486 a temporary variable, so that we can evaluate them here
9487 for side effect only. If type is void, we must do likewise. */
9489 gcc_assert (!TREE_ADDRESSABLE (type)
9490 && !ignore
9491 && TREE_TYPE (treeop1) != void_type_node
9492 && TREE_TYPE (treeop2) != void_type_node);
9494 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9495 if (temp)
9496 return temp;
9498 /* If we are not to produce a result, we have no target. Otherwise,
9499 if a target was specified use it; it will not be used as an
9500 intermediate target unless it is safe. If no target, use a
9501 temporary. */
9503 if (modifier != EXPAND_STACK_PARM
9504 && original_target
9505 && safe_from_p (original_target, treeop0, 1)
9506 && GET_MODE (original_target) == mode
9507 && !MEM_P (original_target))
9508 temp = original_target;
9509 else
9510 temp = assign_temp (type, 0, 1);
9512 do_pending_stack_adjust ();
9513 NO_DEFER_POP;
9514 rtx_code_label *lab0 = gen_label_rtx ();
9515 rtx_code_label *lab1 = gen_label_rtx ();
9516 jumpifnot (treeop0, lab0, -1);
9517 store_expr (treeop1, temp,
9518 modifier == EXPAND_STACK_PARM,
9519 false, false);
9521 emit_jump_insn (targetm.gen_jump (lab1));
9522 emit_barrier ();
9523 emit_label (lab0);
9524 store_expr (treeop2, temp,
9525 modifier == EXPAND_STACK_PARM,
9526 false, false);
9528 emit_label (lab1);
9529 OK_DEFER_POP;
9530 return temp;
9533 case VEC_COND_EXPR:
9534 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9535 return target;
9537 case BIT_INSERT_EXPR:
9539 unsigned bitpos = tree_to_uhwi (treeop2);
9540 unsigned bitsize;
9541 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9542 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9543 else
9544 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9545 rtx op0 = expand_normal (treeop0);
9546 rtx op1 = expand_normal (treeop1);
9547 rtx dst = gen_reg_rtx (mode);
9548 emit_move_insn (dst, op0);
9549 store_bit_field (dst, bitsize, bitpos, 0, 0,
9550 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9551 return dst;
9554 default:
9555 gcc_unreachable ();
9558 /* Here to do an ordinary binary operator. */
9559 binop:
9560 expand_operands (treeop0, treeop1,
9561 subtarget, &op0, &op1, EXPAND_NORMAL);
9562 binop2:
9563 this_optab = optab_for_tree_code (code, type, optab_default);
9564 binop3:
9565 if (modifier == EXPAND_STACK_PARM)
9566 target = 0;
9567 temp = expand_binop (mode, this_optab, op0, op1, target,
9568 unsignedp, OPTAB_LIB_WIDEN);
9569 gcc_assert (temp);
9570 /* Bitwise operations do not need bitfield reduction as we expect their
9571 operands being properly truncated. */
9572 if (code == BIT_XOR_EXPR
9573 || code == BIT_AND_EXPR
9574 || code == BIT_IOR_EXPR)
9575 return temp;
9576 return REDUCE_BIT_FIELD (temp);
9578 #undef REDUCE_BIT_FIELD
9581 /* Return TRUE if expression STMT is suitable for replacement.
9582 Never consider memory loads as replaceable, because those don't ever lead
9583 into constant expressions. */
9585 static bool
9586 stmt_is_replaceable_p (gimple *stmt)
9588 if (ssa_is_replaceable_p (stmt))
9590 /* Don't move around loads. */
9591 if (!gimple_assign_single_p (stmt)
9592 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9593 return true;
9595 return false;
9599 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9600 enum expand_modifier modifier, rtx *alt_rtl,
9601 bool inner_reference_p)
9603 rtx op0, op1, temp, decl_rtl;
9604 tree type;
9605 int unsignedp;
9606 machine_mode mode, dmode;
9607 enum tree_code code = TREE_CODE (exp);
9608 rtx subtarget, original_target;
9609 int ignore;
9610 tree context;
9611 bool reduce_bit_field;
9612 location_t loc = EXPR_LOCATION (exp);
9613 struct separate_ops ops;
9614 tree treeop0, treeop1, treeop2;
9615 tree ssa_name = NULL_TREE;
9616 gimple *g;
9618 type = TREE_TYPE (exp);
9619 mode = TYPE_MODE (type);
9620 unsignedp = TYPE_UNSIGNED (type);
9622 treeop0 = treeop1 = treeop2 = NULL_TREE;
9623 if (!VL_EXP_CLASS_P (exp))
9624 switch (TREE_CODE_LENGTH (code))
9626 default:
9627 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9628 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9629 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9630 case 0: break;
9632 ops.code = code;
9633 ops.type = type;
9634 ops.op0 = treeop0;
9635 ops.op1 = treeop1;
9636 ops.op2 = treeop2;
9637 ops.location = loc;
9639 ignore = (target == const0_rtx
9640 || ((CONVERT_EXPR_CODE_P (code)
9641 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9642 && TREE_CODE (type) == VOID_TYPE));
9644 /* An operation in what may be a bit-field type needs the
9645 result to be reduced to the precision of the bit-field type,
9646 which is narrower than that of the type's mode. */
9647 reduce_bit_field = (!ignore
9648 && INTEGRAL_TYPE_P (type)
9649 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9651 /* If we are going to ignore this result, we need only do something
9652 if there is a side-effect somewhere in the expression. If there
9653 is, short-circuit the most common cases here. Note that we must
9654 not call expand_expr with anything but const0_rtx in case this
9655 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9657 if (ignore)
9659 if (! TREE_SIDE_EFFECTS (exp))
9660 return const0_rtx;
9662 /* Ensure we reference a volatile object even if value is ignored, but
9663 don't do this if all we are doing is taking its address. */
9664 if (TREE_THIS_VOLATILE (exp)
9665 && TREE_CODE (exp) != FUNCTION_DECL
9666 && mode != VOIDmode && mode != BLKmode
9667 && modifier != EXPAND_CONST_ADDRESS)
9669 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9670 if (MEM_P (temp))
9671 copy_to_reg (temp);
9672 return const0_rtx;
9675 if (TREE_CODE_CLASS (code) == tcc_unary
9676 || code == BIT_FIELD_REF
9677 || code == COMPONENT_REF
9678 || code == INDIRECT_REF)
9679 return expand_expr (treeop0, const0_rtx, VOIDmode,
9680 modifier);
9682 else if (TREE_CODE_CLASS (code) == tcc_binary
9683 || TREE_CODE_CLASS (code) == tcc_comparison
9684 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9686 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9687 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9688 return const0_rtx;
9691 target = 0;
9694 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9695 target = 0;
9697 /* Use subtarget as the target for operand 0 of a binary operation. */
9698 subtarget = get_subtarget (target);
9699 original_target = target;
9701 switch (code)
9703 case LABEL_DECL:
9705 tree function = decl_function_context (exp);
9707 temp = label_rtx (exp);
9708 temp = gen_rtx_LABEL_REF (Pmode, temp);
9710 if (function != current_function_decl
9711 && function != 0)
9712 LABEL_REF_NONLOCAL_P (temp) = 1;
9714 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9715 return temp;
9718 case SSA_NAME:
9719 /* ??? ivopts calls expander, without any preparation from
9720 out-of-ssa. So fake instructions as if this was an access to the
9721 base variable. This unnecessarily allocates a pseudo, see how we can
9722 reuse it, if partition base vars have it set already. */
9723 if (!currently_expanding_to_rtl)
9725 tree var = SSA_NAME_VAR (exp);
9726 if (var && DECL_RTL_SET_P (var))
9727 return DECL_RTL (var);
9728 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9729 LAST_VIRTUAL_REGISTER + 1);
9732 g = get_gimple_for_ssa_name (exp);
9733 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9734 if (g == NULL
9735 && modifier == EXPAND_INITIALIZER
9736 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9737 && (optimize || !SSA_NAME_VAR (exp)
9738 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9739 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9740 g = SSA_NAME_DEF_STMT (exp);
9741 if (g)
9743 rtx r;
9744 location_t saved_loc = curr_insn_location ();
9745 location_t loc = gimple_location (g);
9746 if (loc != UNKNOWN_LOCATION)
9747 set_curr_insn_location (loc);
9748 ops.code = gimple_assign_rhs_code (g);
9749 switch (get_gimple_rhs_class (ops.code))
9751 case GIMPLE_TERNARY_RHS:
9752 ops.op2 = gimple_assign_rhs3 (g);
9753 /* Fallthru */
9754 case GIMPLE_BINARY_RHS:
9755 ops.op1 = gimple_assign_rhs2 (g);
9757 /* Try to expand conditonal compare. */
9758 if (targetm.gen_ccmp_first)
9760 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9761 r = expand_ccmp_expr (g);
9762 if (r)
9763 break;
9765 /* Fallthru */
9766 case GIMPLE_UNARY_RHS:
9767 ops.op0 = gimple_assign_rhs1 (g);
9768 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9769 ops.location = loc;
9770 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9771 break;
9772 case GIMPLE_SINGLE_RHS:
9774 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9775 tmode, modifier, NULL, inner_reference_p);
9776 break;
9778 default:
9779 gcc_unreachable ();
9781 set_curr_insn_location (saved_loc);
9782 if (REG_P (r) && !REG_EXPR (r))
9783 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9784 return r;
9787 ssa_name = exp;
9788 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9789 exp = SSA_NAME_VAR (ssa_name);
9790 goto expand_decl_rtl;
9792 case PARM_DECL:
9793 case VAR_DECL:
9794 /* If a static var's type was incomplete when the decl was written,
9795 but the type is complete now, lay out the decl now. */
9796 if (DECL_SIZE (exp) == 0
9797 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9798 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9799 layout_decl (exp, 0);
9801 /* fall through */
9803 case FUNCTION_DECL:
9804 case RESULT_DECL:
9805 decl_rtl = DECL_RTL (exp);
9806 expand_decl_rtl:
9807 gcc_assert (decl_rtl);
9809 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9810 settings for VECTOR_TYPE_P that might switch for the function. */
9811 if (currently_expanding_to_rtl
9812 && code == VAR_DECL && MEM_P (decl_rtl)
9813 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9814 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9815 else
9816 decl_rtl = copy_rtx (decl_rtl);
9818 /* Record writes to register variables. */
9819 if (modifier == EXPAND_WRITE
9820 && REG_P (decl_rtl)
9821 && HARD_REGISTER_P (decl_rtl))
9822 add_to_hard_reg_set (&crtl->asm_clobbers,
9823 GET_MODE (decl_rtl), REGNO (decl_rtl));
9825 /* Ensure variable marked as used even if it doesn't go through
9826 a parser. If it hasn't be used yet, write out an external
9827 definition. */
9828 if (exp)
9829 TREE_USED (exp) = 1;
9831 /* Show we haven't gotten RTL for this yet. */
9832 temp = 0;
9834 /* Variables inherited from containing functions should have
9835 been lowered by this point. */
9836 if (exp)
9837 context = decl_function_context (exp);
9838 gcc_assert (!exp
9839 || SCOPE_FILE_SCOPE_P (context)
9840 || context == current_function_decl
9841 || TREE_STATIC (exp)
9842 || DECL_EXTERNAL (exp)
9843 /* ??? C++ creates functions that are not TREE_STATIC. */
9844 || TREE_CODE (exp) == FUNCTION_DECL);
9846 /* This is the case of an array whose size is to be determined
9847 from its initializer, while the initializer is still being parsed.
9848 ??? We aren't parsing while expanding anymore. */
9850 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9851 temp = validize_mem (decl_rtl);
9853 /* If DECL_RTL is memory, we are in the normal case and the
9854 address is not valid, get the address into a register. */
9856 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9858 if (alt_rtl)
9859 *alt_rtl = decl_rtl;
9860 decl_rtl = use_anchored_address (decl_rtl);
9861 if (modifier != EXPAND_CONST_ADDRESS
9862 && modifier != EXPAND_SUM
9863 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9864 : GET_MODE (decl_rtl),
9865 XEXP (decl_rtl, 0),
9866 MEM_ADDR_SPACE (decl_rtl)))
9867 temp = replace_equiv_address (decl_rtl,
9868 copy_rtx (XEXP (decl_rtl, 0)));
9871 /* If we got something, return it. But first, set the alignment
9872 if the address is a register. */
9873 if (temp != 0)
9875 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9876 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9878 return temp;
9881 if (exp)
9882 dmode = DECL_MODE (exp);
9883 else
9884 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9886 /* If the mode of DECL_RTL does not match that of the decl,
9887 there are two cases: we are dealing with a BLKmode value
9888 that is returned in a register, or we are dealing with
9889 a promoted value. In the latter case, return a SUBREG
9890 of the wanted mode, but mark it so that we know that it
9891 was already extended. */
9892 if (REG_P (decl_rtl)
9893 && dmode != BLKmode
9894 && GET_MODE (decl_rtl) != dmode)
9896 machine_mode pmode;
9898 /* Get the signedness to be used for this variable. Ensure we get
9899 the same mode we got when the variable was declared. */
9900 if (code != SSA_NAME)
9901 pmode = promote_decl_mode (exp, &unsignedp);
9902 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9903 && gimple_code (g) == GIMPLE_CALL
9904 && !gimple_call_internal_p (g))
9905 pmode = promote_function_mode (type, mode, &unsignedp,
9906 gimple_call_fntype (g),
9908 else
9909 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9910 gcc_assert (GET_MODE (decl_rtl) == pmode);
9912 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9913 SUBREG_PROMOTED_VAR_P (temp) = 1;
9914 SUBREG_PROMOTED_SET (temp, unsignedp);
9915 return temp;
9918 return decl_rtl;
9920 case INTEGER_CST:
9921 /* Given that TYPE_PRECISION (type) is not always equal to
9922 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9923 the former to the latter according to the signedness of the
9924 type. */
9925 temp = immed_wide_int_const (wi::to_wide
9926 (exp,
9927 GET_MODE_PRECISION (TYPE_MODE (type))),
9928 TYPE_MODE (type));
9929 return temp;
9931 case VECTOR_CST:
9933 tree tmp = NULL_TREE;
9934 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9935 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9936 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9937 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9938 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9939 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9940 return const_vector_from_tree (exp);
9941 if (GET_MODE_CLASS (mode) == MODE_INT)
9943 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9944 return const_scalar_mask_from_tree (exp);
9945 else
9947 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9948 if (type_for_mode)
9949 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9950 type_for_mode, exp);
9953 if (!tmp)
9955 vec<constructor_elt, va_gc> *v;
9956 unsigned i;
9957 vec_alloc (v, VECTOR_CST_NELTS (exp));
9958 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9959 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9960 tmp = build_constructor (type, v);
9962 return expand_expr (tmp, ignore ? const0_rtx : target,
9963 tmode, modifier);
9966 case CONST_DECL:
9967 if (modifier == EXPAND_WRITE)
9969 /* Writing into CONST_DECL is always invalid, but handle it
9970 gracefully. */
9971 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9972 machine_mode address_mode = targetm.addr_space.address_mode (as);
9973 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9974 EXPAND_NORMAL, as);
9975 op0 = memory_address_addr_space (mode, op0, as);
9976 temp = gen_rtx_MEM (mode, op0);
9977 set_mem_addr_space (temp, as);
9978 return temp;
9980 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9982 case REAL_CST:
9983 /* If optimized, generate immediate CONST_DOUBLE
9984 which will be turned into memory by reload if necessary.
9986 We used to force a register so that loop.c could see it. But
9987 this does not allow gen_* patterns to perform optimizations with
9988 the constants. It also produces two insns in cases like "x = 1.0;".
9989 On most machines, floating-point constants are not permitted in
9990 many insns, so we'd end up copying it to a register in any case.
9992 Now, we do the copying in expand_binop, if appropriate. */
9993 return const_double_from_real_value (TREE_REAL_CST (exp),
9994 TYPE_MODE (TREE_TYPE (exp)));
9996 case FIXED_CST:
9997 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9998 TYPE_MODE (TREE_TYPE (exp)));
10000 case COMPLEX_CST:
10001 /* Handle evaluating a complex constant in a CONCAT target. */
10002 if (original_target && GET_CODE (original_target) == CONCAT)
10004 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10005 rtx rtarg, itarg;
10007 rtarg = XEXP (original_target, 0);
10008 itarg = XEXP (original_target, 1);
10010 /* Move the real and imaginary parts separately. */
10011 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10012 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10014 if (op0 != rtarg)
10015 emit_move_insn (rtarg, op0);
10016 if (op1 != itarg)
10017 emit_move_insn (itarg, op1);
10019 return original_target;
10022 /* fall through */
10024 case STRING_CST:
10025 temp = expand_expr_constant (exp, 1, modifier);
10027 /* temp contains a constant address.
10028 On RISC machines where a constant address isn't valid,
10029 make some insns to get that address into a register. */
10030 if (modifier != EXPAND_CONST_ADDRESS
10031 && modifier != EXPAND_INITIALIZER
10032 && modifier != EXPAND_SUM
10033 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10034 MEM_ADDR_SPACE (temp)))
10035 return replace_equiv_address (temp,
10036 copy_rtx (XEXP (temp, 0)));
10037 return temp;
10039 case SAVE_EXPR:
10041 tree val = treeop0;
10042 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10043 inner_reference_p);
10045 if (!SAVE_EXPR_RESOLVED_P (exp))
10047 /* We can indeed still hit this case, typically via builtin
10048 expanders calling save_expr immediately before expanding
10049 something. Assume this means that we only have to deal
10050 with non-BLKmode values. */
10051 gcc_assert (GET_MODE (ret) != BLKmode);
10053 val = build_decl (curr_insn_location (),
10054 VAR_DECL, NULL, TREE_TYPE (exp));
10055 DECL_ARTIFICIAL (val) = 1;
10056 DECL_IGNORED_P (val) = 1;
10057 treeop0 = val;
10058 TREE_OPERAND (exp, 0) = treeop0;
10059 SAVE_EXPR_RESOLVED_P (exp) = 1;
10061 if (!CONSTANT_P (ret))
10062 ret = copy_to_reg (ret);
10063 SET_DECL_RTL (val, ret);
10066 return ret;
10070 case CONSTRUCTOR:
10071 /* If we don't need the result, just ensure we evaluate any
10072 subexpressions. */
10073 if (ignore)
10075 unsigned HOST_WIDE_INT idx;
10076 tree value;
10078 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10079 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10081 return const0_rtx;
10084 return expand_constructor (exp, target, modifier, false);
10086 case TARGET_MEM_REF:
10088 addr_space_t as
10089 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10090 enum insn_code icode;
10091 unsigned int align;
10093 op0 = addr_for_mem_ref (exp, as, true);
10094 op0 = memory_address_addr_space (mode, op0, as);
10095 temp = gen_rtx_MEM (mode, op0);
10096 set_mem_attributes (temp, exp, 0);
10097 set_mem_addr_space (temp, as);
10098 align = get_object_alignment (exp);
10099 if (modifier != EXPAND_WRITE
10100 && modifier != EXPAND_MEMORY
10101 && mode != BLKmode
10102 && align < GET_MODE_ALIGNMENT (mode)
10103 /* If the target does not have special handling for unaligned
10104 loads of mode then it can use regular moves for them. */
10105 && ((icode = optab_handler (movmisalign_optab, mode))
10106 != CODE_FOR_nothing))
10108 struct expand_operand ops[2];
10110 /* We've already validated the memory, and we're creating a
10111 new pseudo destination. The predicates really can't fail,
10112 nor can the generator. */
10113 create_output_operand (&ops[0], NULL_RTX, mode);
10114 create_fixed_operand (&ops[1], temp);
10115 expand_insn (icode, 2, ops);
10116 temp = ops[0].value;
10118 return temp;
10121 case MEM_REF:
10123 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10124 addr_space_t as
10125 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10126 machine_mode address_mode;
10127 tree base = TREE_OPERAND (exp, 0);
10128 gimple *def_stmt;
10129 enum insn_code icode;
10130 unsigned align;
10131 /* Handle expansion of non-aliased memory with non-BLKmode. That
10132 might end up in a register. */
10133 if (mem_ref_refers_to_non_mem_p (exp))
10135 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10136 base = TREE_OPERAND (base, 0);
10137 if (offset == 0
10138 && !reverse
10139 && tree_fits_uhwi_p (TYPE_SIZE (type))
10140 && (GET_MODE_BITSIZE (DECL_MODE (base))
10141 == tree_to_uhwi (TYPE_SIZE (type))))
10142 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10143 target, tmode, modifier);
10144 if (TYPE_MODE (type) == BLKmode)
10146 temp = assign_stack_temp (DECL_MODE (base),
10147 GET_MODE_SIZE (DECL_MODE (base)));
10148 store_expr (base, temp, 0, false, false);
10149 temp = adjust_address (temp, BLKmode, offset);
10150 set_mem_size (temp, int_size_in_bytes (type));
10151 return temp;
10153 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10154 bitsize_int (offset * BITS_PER_UNIT));
10155 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10156 return expand_expr (exp, target, tmode, modifier);
10158 address_mode = targetm.addr_space.address_mode (as);
10159 base = TREE_OPERAND (exp, 0);
10160 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10162 tree mask = gimple_assign_rhs2 (def_stmt);
10163 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10164 gimple_assign_rhs1 (def_stmt), mask);
10165 TREE_OPERAND (exp, 0) = base;
10167 align = get_object_alignment (exp);
10168 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10169 op0 = memory_address_addr_space (mode, op0, as);
10170 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10172 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10173 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10174 op0 = memory_address_addr_space (mode, op0, as);
10176 temp = gen_rtx_MEM (mode, op0);
10177 set_mem_attributes (temp, exp, 0);
10178 set_mem_addr_space (temp, as);
10179 if (TREE_THIS_VOLATILE (exp))
10180 MEM_VOLATILE_P (temp) = 1;
10181 if (modifier != EXPAND_WRITE
10182 && modifier != EXPAND_MEMORY
10183 && !inner_reference_p
10184 && mode != BLKmode
10185 && align < GET_MODE_ALIGNMENT (mode))
10187 if ((icode = optab_handler (movmisalign_optab, mode))
10188 != CODE_FOR_nothing)
10190 struct expand_operand ops[2];
10192 /* We've already validated the memory, and we're creating a
10193 new pseudo destination. The predicates really can't fail,
10194 nor can the generator. */
10195 create_output_operand (&ops[0], NULL_RTX, mode);
10196 create_fixed_operand (&ops[1], temp);
10197 expand_insn (icode, 2, ops);
10198 temp = ops[0].value;
10200 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10201 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10202 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10203 (modifier == EXPAND_STACK_PARM
10204 ? NULL_RTX : target),
10205 mode, mode, false);
10207 if (reverse
10208 && modifier != EXPAND_MEMORY
10209 && modifier != EXPAND_WRITE)
10210 temp = flip_storage_order (mode, temp);
10211 return temp;
10214 case ARRAY_REF:
10217 tree array = treeop0;
10218 tree index = treeop1;
10219 tree init;
10221 /* Fold an expression like: "foo"[2].
10222 This is not done in fold so it won't happen inside &.
10223 Don't fold if this is for wide characters since it's too
10224 difficult to do correctly and this is a very rare case. */
10226 if (modifier != EXPAND_CONST_ADDRESS
10227 && modifier != EXPAND_INITIALIZER
10228 && modifier != EXPAND_MEMORY)
10230 tree t = fold_read_from_constant_string (exp);
10232 if (t)
10233 return expand_expr (t, target, tmode, modifier);
10236 /* If this is a constant index into a constant array,
10237 just get the value from the array. Handle both the cases when
10238 we have an explicit constructor and when our operand is a variable
10239 that was declared const. */
10241 if (modifier != EXPAND_CONST_ADDRESS
10242 && modifier != EXPAND_INITIALIZER
10243 && modifier != EXPAND_MEMORY
10244 && TREE_CODE (array) == CONSTRUCTOR
10245 && ! TREE_SIDE_EFFECTS (array)
10246 && TREE_CODE (index) == INTEGER_CST)
10248 unsigned HOST_WIDE_INT ix;
10249 tree field, value;
10251 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10252 field, value)
10253 if (tree_int_cst_equal (field, index))
10255 if (!TREE_SIDE_EFFECTS (value))
10256 return expand_expr (fold (value), target, tmode, modifier);
10257 break;
10261 else if (optimize >= 1
10262 && modifier != EXPAND_CONST_ADDRESS
10263 && modifier != EXPAND_INITIALIZER
10264 && modifier != EXPAND_MEMORY
10265 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10266 && TREE_CODE (index) == INTEGER_CST
10267 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10268 && (init = ctor_for_folding (array)) != error_mark_node)
10270 if (init == NULL_TREE)
10272 tree value = build_zero_cst (type);
10273 if (TREE_CODE (value) == CONSTRUCTOR)
10275 /* If VALUE is a CONSTRUCTOR, this optimization is only
10276 useful if this doesn't store the CONSTRUCTOR into
10277 memory. If it does, it is more efficient to just
10278 load the data from the array directly. */
10279 rtx ret = expand_constructor (value, target,
10280 modifier, true);
10281 if (ret == NULL_RTX)
10282 value = NULL_TREE;
10285 if (value)
10286 return expand_expr (value, target, tmode, modifier);
10288 else if (TREE_CODE (init) == CONSTRUCTOR)
10290 unsigned HOST_WIDE_INT ix;
10291 tree field, value;
10293 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10294 field, value)
10295 if (tree_int_cst_equal (field, index))
10297 if (TREE_SIDE_EFFECTS (value))
10298 break;
10300 if (TREE_CODE (value) == CONSTRUCTOR)
10302 /* If VALUE is a CONSTRUCTOR, this
10303 optimization is only useful if
10304 this doesn't store the CONSTRUCTOR
10305 into memory. If it does, it is more
10306 efficient to just load the data from
10307 the array directly. */
10308 rtx ret = expand_constructor (value, target,
10309 modifier, true);
10310 if (ret == NULL_RTX)
10311 break;
10314 return
10315 expand_expr (fold (value), target, tmode, modifier);
10318 else if (TREE_CODE (init) == STRING_CST)
10320 tree low_bound = array_ref_low_bound (exp);
10321 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10323 /* Optimize the special case of a zero lower bound.
10325 We convert the lower bound to sizetype to avoid problems
10326 with constant folding. E.g. suppose the lower bound is
10327 1 and its mode is QI. Without the conversion
10328 (ARRAY + (INDEX - (unsigned char)1))
10329 becomes
10330 (ARRAY + (-(unsigned char)1) + INDEX)
10331 which becomes
10332 (ARRAY + 255 + INDEX). Oops! */
10333 if (!integer_zerop (low_bound))
10334 index1 = size_diffop_loc (loc, index1,
10335 fold_convert_loc (loc, sizetype,
10336 low_bound));
10338 if (tree_fits_uhwi_p (index1)
10339 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10341 tree type = TREE_TYPE (TREE_TYPE (init));
10342 machine_mode mode = TYPE_MODE (type);
10344 if (GET_MODE_CLASS (mode) == MODE_INT
10345 && GET_MODE_SIZE (mode) == 1)
10346 return gen_int_mode (TREE_STRING_POINTER (init)
10347 [TREE_INT_CST_LOW (index1)],
10348 mode);
10353 goto normal_inner_ref;
10355 case COMPONENT_REF:
10356 /* If the operand is a CONSTRUCTOR, we can just extract the
10357 appropriate field if it is present. */
10358 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10360 unsigned HOST_WIDE_INT idx;
10361 tree field, value;
10363 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10364 idx, field, value)
10365 if (field == treeop1
10366 /* We can normally use the value of the field in the
10367 CONSTRUCTOR. However, if this is a bitfield in
10368 an integral mode that we can fit in a HOST_WIDE_INT,
10369 we must mask only the number of bits in the bitfield,
10370 since this is done implicitly by the constructor. If
10371 the bitfield does not meet either of those conditions,
10372 we can't do this optimization. */
10373 && (! DECL_BIT_FIELD (field)
10374 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10375 && (GET_MODE_PRECISION (DECL_MODE (field))
10376 <= HOST_BITS_PER_WIDE_INT))))
10378 if (DECL_BIT_FIELD (field)
10379 && modifier == EXPAND_STACK_PARM)
10380 target = 0;
10381 op0 = expand_expr (value, target, tmode, modifier);
10382 if (DECL_BIT_FIELD (field))
10384 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10385 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10387 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10389 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10390 imode);
10391 op0 = expand_and (imode, op0, op1, target);
10393 else
10395 int count = GET_MODE_PRECISION (imode) - bitsize;
10397 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10398 target, 0);
10399 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10400 target, 0);
10404 return op0;
10407 goto normal_inner_ref;
10409 case BIT_FIELD_REF:
10410 case ARRAY_RANGE_REF:
10411 normal_inner_ref:
10413 machine_mode mode1, mode2;
10414 HOST_WIDE_INT bitsize, bitpos;
10415 tree offset;
10416 int reversep, volatilep = 0, must_force_mem;
10417 tree tem
10418 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10419 &unsignedp, &reversep, &volatilep);
10420 rtx orig_op0, memloc;
10421 bool clear_mem_expr = false;
10423 /* If we got back the original object, something is wrong. Perhaps
10424 we are evaluating an expression too early. In any event, don't
10425 infinitely recurse. */
10426 gcc_assert (tem != exp);
10428 /* If TEM's type is a union of variable size, pass TARGET to the inner
10429 computation, since it will need a temporary and TARGET is known
10430 to have to do. This occurs in unchecked conversion in Ada. */
10431 orig_op0 = op0
10432 = expand_expr_real (tem,
10433 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10434 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10435 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10436 != INTEGER_CST)
10437 && modifier != EXPAND_STACK_PARM
10438 ? target : NULL_RTX),
10439 VOIDmode,
10440 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10441 NULL, true);
10443 /* If the field has a mode, we want to access it in the
10444 field's mode, not the computed mode.
10445 If a MEM has VOIDmode (external with incomplete type),
10446 use BLKmode for it instead. */
10447 if (MEM_P (op0))
10449 if (mode1 != VOIDmode)
10450 op0 = adjust_address (op0, mode1, 0);
10451 else if (GET_MODE (op0) == VOIDmode)
10452 op0 = adjust_address (op0, BLKmode, 0);
10455 mode2
10456 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10458 /* If we have either an offset, a BLKmode result, or a reference
10459 outside the underlying object, we must force it to memory.
10460 Such a case can occur in Ada if we have unchecked conversion
10461 of an expression from a scalar type to an aggregate type or
10462 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10463 passed a partially uninitialized object or a view-conversion
10464 to a larger size. */
10465 must_force_mem = (offset
10466 || mode1 == BLKmode
10467 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10469 /* Handle CONCAT first. */
10470 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10472 if (bitpos == 0
10473 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10474 && COMPLEX_MODE_P (mode1)
10475 && COMPLEX_MODE_P (GET_MODE (op0))
10476 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10477 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10479 if (reversep)
10480 op0 = flip_storage_order (GET_MODE (op0), op0);
10481 if (mode1 != GET_MODE (op0))
10483 rtx parts[2];
10484 for (int i = 0; i < 2; i++)
10486 rtx op = read_complex_part (op0, i != 0);
10487 if (GET_CODE (op) == SUBREG)
10488 op = force_reg (GET_MODE (op), op);
10489 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10490 op);
10491 if (temp)
10492 op = temp;
10493 else
10495 if (!REG_P (op) && !MEM_P (op))
10496 op = force_reg (GET_MODE (op), op);
10497 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10499 parts[i] = op;
10501 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10503 return op0;
10505 if (bitpos == 0
10506 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10507 && bitsize)
10509 op0 = XEXP (op0, 0);
10510 mode2 = GET_MODE (op0);
10512 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10513 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10514 && bitpos
10515 && bitsize)
10517 op0 = XEXP (op0, 1);
10518 bitpos = 0;
10519 mode2 = GET_MODE (op0);
10521 else
10522 /* Otherwise force into memory. */
10523 must_force_mem = 1;
10526 /* If this is a constant, put it in a register if it is a legitimate
10527 constant and we don't need a memory reference. */
10528 if (CONSTANT_P (op0)
10529 && mode2 != BLKmode
10530 && targetm.legitimate_constant_p (mode2, op0)
10531 && !must_force_mem)
10532 op0 = force_reg (mode2, op0);
10534 /* Otherwise, if this is a constant, try to force it to the constant
10535 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10536 is a legitimate constant. */
10537 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10538 op0 = validize_mem (memloc);
10540 /* Otherwise, if this is a constant or the object is not in memory
10541 and need be, put it there. */
10542 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10544 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10545 emit_move_insn (memloc, op0);
10546 op0 = memloc;
10547 clear_mem_expr = true;
10550 if (offset)
10552 machine_mode address_mode;
10553 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10554 EXPAND_SUM);
10556 gcc_assert (MEM_P (op0));
10558 address_mode = get_address_mode (op0);
10559 if (GET_MODE (offset_rtx) != address_mode)
10561 /* We cannot be sure that the RTL in offset_rtx is valid outside
10562 of a memory address context, so force it into a register
10563 before attempting to convert it to the desired mode. */
10564 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10565 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10568 /* See the comment in expand_assignment for the rationale. */
10569 if (mode1 != VOIDmode
10570 && bitpos != 0
10571 && bitsize > 0
10572 && (bitpos % bitsize) == 0
10573 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10574 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10576 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10577 bitpos = 0;
10580 op0 = offset_address (op0, offset_rtx,
10581 highest_pow2_factor (offset));
10584 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10585 record its alignment as BIGGEST_ALIGNMENT. */
10586 if (MEM_P (op0) && bitpos == 0 && offset != 0
10587 && is_aligning_offset (offset, tem))
10588 set_mem_align (op0, BIGGEST_ALIGNMENT);
10590 /* Don't forget about volatility even if this is a bitfield. */
10591 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10593 if (op0 == orig_op0)
10594 op0 = copy_rtx (op0);
10596 MEM_VOLATILE_P (op0) = 1;
10599 /* In cases where an aligned union has an unaligned object
10600 as a field, we might be extracting a BLKmode value from
10601 an integer-mode (e.g., SImode) object. Handle this case
10602 by doing the extract into an object as wide as the field
10603 (which we know to be the width of a basic mode), then
10604 storing into memory, and changing the mode to BLKmode. */
10605 if (mode1 == VOIDmode
10606 || REG_P (op0) || GET_CODE (op0) == SUBREG
10607 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10608 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10609 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10610 && modifier != EXPAND_CONST_ADDRESS
10611 && modifier != EXPAND_INITIALIZER
10612 && modifier != EXPAND_MEMORY)
10613 /* If the bitfield is volatile and the bitsize
10614 is narrower than the access size of the bitfield,
10615 we need to extract bitfields from the access. */
10616 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10617 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10618 && mode1 != BLKmode
10619 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10620 /* If the field isn't aligned enough to fetch as a memref,
10621 fetch it as a bit field. */
10622 || (mode1 != BLKmode
10623 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10624 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10625 || (MEM_P (op0)
10626 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10627 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10628 && modifier != EXPAND_MEMORY
10629 && ((modifier == EXPAND_CONST_ADDRESS
10630 || modifier == EXPAND_INITIALIZER)
10631 ? STRICT_ALIGNMENT
10632 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10633 || (bitpos % BITS_PER_UNIT != 0)))
10634 /* If the type and the field are a constant size and the
10635 size of the type isn't the same size as the bitfield,
10636 we must use bitfield operations. */
10637 || (bitsize >= 0
10638 && TYPE_SIZE (TREE_TYPE (exp))
10639 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10640 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10641 bitsize)))
10643 machine_mode ext_mode = mode;
10645 if (ext_mode == BLKmode
10646 && ! (target != 0 && MEM_P (op0)
10647 && MEM_P (target)
10648 && bitpos % BITS_PER_UNIT == 0))
10649 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10651 if (ext_mode == BLKmode)
10653 if (target == 0)
10654 target = assign_temp (type, 1, 1);
10656 /* ??? Unlike the similar test a few lines below, this one is
10657 very likely obsolete. */
10658 if (bitsize == 0)
10659 return target;
10661 /* In this case, BITPOS must start at a byte boundary and
10662 TARGET, if specified, must be a MEM. */
10663 gcc_assert (MEM_P (op0)
10664 && (!target || MEM_P (target))
10665 && !(bitpos % BITS_PER_UNIT));
10667 emit_block_move (target,
10668 adjust_address (op0, VOIDmode,
10669 bitpos / BITS_PER_UNIT),
10670 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10671 / BITS_PER_UNIT),
10672 (modifier == EXPAND_STACK_PARM
10673 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10675 return target;
10678 /* If we have nothing to extract, the result will be 0 for targets
10679 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10680 return 0 for the sake of consistency, as reading a zero-sized
10681 bitfield is valid in Ada and the value is fully specified. */
10682 if (bitsize == 0)
10683 return const0_rtx;
10685 op0 = validize_mem (op0);
10687 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10688 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10690 /* If the result has a record type and the extraction is done in
10691 an integral mode, then the field may be not aligned on a byte
10692 boundary; in this case, if it has reverse storage order, it
10693 needs to be extracted as a scalar field with reverse storage
10694 order and put back into memory order afterwards. */
10695 if (TREE_CODE (type) == RECORD_TYPE
10696 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10697 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10699 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10700 (modifier == EXPAND_STACK_PARM
10701 ? NULL_RTX : target),
10702 ext_mode, ext_mode, reversep);
10704 /* If the result has a record type and the mode of OP0 is an
10705 integral mode then, if BITSIZE is narrower than this mode
10706 and this is for big-endian data, we must put the field
10707 into the high-order bits. And we must also put it back
10708 into memory order if it has been previously reversed. */
10709 if (TREE_CODE (type) == RECORD_TYPE
10710 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
10712 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (op0));
10714 if (bitsize < size
10715 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10716 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10717 size - bitsize, op0, 1);
10719 if (reversep)
10720 op0 = flip_storage_order (GET_MODE (op0), op0);
10723 /* If the result type is BLKmode, store the data into a temporary
10724 of the appropriate type, but with the mode corresponding to the
10725 mode for the data we have (op0's mode). */
10726 if (mode == BLKmode)
10728 rtx new_rtx
10729 = assign_stack_temp_for_type (ext_mode,
10730 GET_MODE_BITSIZE (ext_mode),
10731 type);
10732 emit_move_insn (new_rtx, op0);
10733 op0 = copy_rtx (new_rtx);
10734 PUT_MODE (op0, BLKmode);
10737 return op0;
10740 /* If the result is BLKmode, use that to access the object
10741 now as well. */
10742 if (mode == BLKmode)
10743 mode1 = BLKmode;
10745 /* Get a reference to just this component. */
10746 if (modifier == EXPAND_CONST_ADDRESS
10747 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10748 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10749 else
10750 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10752 if (op0 == orig_op0)
10753 op0 = copy_rtx (op0);
10755 /* Don't set memory attributes if the base expression is
10756 SSA_NAME that got expanded as a MEM. In that case, we should
10757 just honor its original memory attributes. */
10758 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10759 set_mem_attributes (op0, exp, 0);
10761 if (REG_P (XEXP (op0, 0)))
10762 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10764 /* If op0 is a temporary because the original expressions was forced
10765 to memory, clear MEM_EXPR so that the original expression cannot
10766 be marked as addressable through MEM_EXPR of the temporary. */
10767 if (clear_mem_expr)
10768 set_mem_expr (op0, NULL_TREE);
10770 MEM_VOLATILE_P (op0) |= volatilep;
10772 if (reversep
10773 && modifier != EXPAND_MEMORY
10774 && modifier != EXPAND_WRITE)
10775 op0 = flip_storage_order (mode1, op0);
10777 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10778 || modifier == EXPAND_CONST_ADDRESS
10779 || modifier == EXPAND_INITIALIZER)
10780 return op0;
10782 if (target == 0)
10783 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10785 convert_move (target, op0, unsignedp);
10786 return target;
10789 case OBJ_TYPE_REF:
10790 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10792 case CALL_EXPR:
10793 /* All valid uses of __builtin_va_arg_pack () are removed during
10794 inlining. */
10795 if (CALL_EXPR_VA_ARG_PACK (exp))
10796 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10798 tree fndecl = get_callee_fndecl (exp), attr;
10800 if (fndecl
10801 && (attr = lookup_attribute ("error",
10802 DECL_ATTRIBUTES (fndecl))) != NULL)
10803 error ("%Kcall to %qs declared with attribute error: %s",
10804 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10805 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10806 if (fndecl
10807 && (attr = lookup_attribute ("warning",
10808 DECL_ATTRIBUTES (fndecl))) != NULL)
10809 warning_at (tree_nonartificial_location (exp),
10810 0, "%Kcall to %qs declared with attribute warning: %s",
10811 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10812 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10814 /* Check for a built-in function. */
10815 if (fndecl && DECL_BUILT_IN (fndecl))
10817 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10818 if (CALL_WITH_BOUNDS_P (exp))
10819 return expand_builtin_with_bounds (exp, target, subtarget,
10820 tmode, ignore);
10821 else
10822 return expand_builtin (exp, target, subtarget, tmode, ignore);
10825 return expand_call (exp, target, ignore);
10827 case VIEW_CONVERT_EXPR:
10828 op0 = NULL_RTX;
10830 /* If we are converting to BLKmode, try to avoid an intermediate
10831 temporary by fetching an inner memory reference. */
10832 if (mode == BLKmode
10833 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10834 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10835 && handled_component_p (treeop0))
10837 machine_mode mode1;
10838 HOST_WIDE_INT bitsize, bitpos;
10839 tree offset;
10840 int unsignedp, reversep, volatilep = 0;
10841 tree tem
10842 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10843 &unsignedp, &reversep, &volatilep);
10844 rtx orig_op0;
10846 /* ??? We should work harder and deal with non-zero offsets. */
10847 if (!offset
10848 && (bitpos % BITS_PER_UNIT) == 0
10849 && !reversep
10850 && bitsize >= 0
10851 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10853 /* See the normal_inner_ref case for the rationale. */
10854 orig_op0
10855 = expand_expr_real (tem,
10856 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10857 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10858 != INTEGER_CST)
10859 && modifier != EXPAND_STACK_PARM
10860 ? target : NULL_RTX),
10861 VOIDmode,
10862 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10863 NULL, true);
10865 if (MEM_P (orig_op0))
10867 op0 = orig_op0;
10869 /* Get a reference to just this component. */
10870 if (modifier == EXPAND_CONST_ADDRESS
10871 || modifier == EXPAND_SUM
10872 || modifier == EXPAND_INITIALIZER)
10873 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10874 else
10875 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10877 if (op0 == orig_op0)
10878 op0 = copy_rtx (op0);
10880 set_mem_attributes (op0, treeop0, 0);
10881 if (REG_P (XEXP (op0, 0)))
10882 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10884 MEM_VOLATILE_P (op0) |= volatilep;
10889 if (!op0)
10890 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10891 NULL, inner_reference_p);
10893 /* If the input and output modes are both the same, we are done. */
10894 if (mode == GET_MODE (op0))
10896 /* If neither mode is BLKmode, and both modes are the same size
10897 then we can use gen_lowpart. */
10898 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10899 && (GET_MODE_PRECISION (mode)
10900 == GET_MODE_PRECISION (GET_MODE (op0)))
10901 && !COMPLEX_MODE_P (GET_MODE (op0)))
10903 if (GET_CODE (op0) == SUBREG)
10904 op0 = force_reg (GET_MODE (op0), op0);
10905 temp = gen_lowpart_common (mode, op0);
10906 if (temp)
10907 op0 = temp;
10908 else
10910 if (!REG_P (op0) && !MEM_P (op0))
10911 op0 = force_reg (GET_MODE (op0), op0);
10912 op0 = gen_lowpart (mode, op0);
10915 /* If both types are integral, convert from one mode to the other. */
10916 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10917 op0 = convert_modes (mode, GET_MODE (op0), op0,
10918 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10919 /* If the output type is a bit-field type, do an extraction. */
10920 else if (reduce_bit_field)
10921 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10922 TYPE_UNSIGNED (type), NULL_RTX,
10923 mode, mode, false);
10924 /* As a last resort, spill op0 to memory, and reload it in a
10925 different mode. */
10926 else if (!MEM_P (op0))
10928 /* If the operand is not a MEM, force it into memory. Since we
10929 are going to be changing the mode of the MEM, don't call
10930 force_const_mem for constants because we don't allow pool
10931 constants to change mode. */
10932 tree inner_type = TREE_TYPE (treeop0);
10934 gcc_assert (!TREE_ADDRESSABLE (exp));
10936 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10937 target
10938 = assign_stack_temp_for_type
10939 (TYPE_MODE (inner_type),
10940 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10942 emit_move_insn (target, op0);
10943 op0 = target;
10946 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10947 output type is such that the operand is known to be aligned, indicate
10948 that it is. Otherwise, we need only be concerned about alignment for
10949 non-BLKmode results. */
10950 if (MEM_P (op0))
10952 enum insn_code icode;
10954 if (modifier != EXPAND_WRITE
10955 && modifier != EXPAND_MEMORY
10956 && !inner_reference_p
10957 && mode != BLKmode
10958 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10960 /* If the target does have special handling for unaligned
10961 loads of mode then use them. */
10962 if ((icode = optab_handler (movmisalign_optab, mode))
10963 != CODE_FOR_nothing)
10965 rtx reg;
10967 op0 = adjust_address (op0, mode, 0);
10968 /* We've already validated the memory, and we're creating a
10969 new pseudo destination. The predicates really can't
10970 fail. */
10971 reg = gen_reg_rtx (mode);
10973 /* Nor can the insn generator. */
10974 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10975 emit_insn (insn);
10976 return reg;
10978 else if (STRICT_ALIGNMENT)
10980 tree inner_type = TREE_TYPE (treeop0);
10981 HOST_WIDE_INT temp_size
10982 = MAX (int_size_in_bytes (inner_type),
10983 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10984 rtx new_rtx
10985 = assign_stack_temp_for_type (mode, temp_size, type);
10986 rtx new_with_op0_mode
10987 = adjust_address (new_rtx, GET_MODE (op0), 0);
10989 gcc_assert (!TREE_ADDRESSABLE (exp));
10991 if (GET_MODE (op0) == BLKmode)
10992 emit_block_move (new_with_op0_mode, op0,
10993 GEN_INT (GET_MODE_SIZE (mode)),
10994 (modifier == EXPAND_STACK_PARM
10995 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10996 else
10997 emit_move_insn (new_with_op0_mode, op0);
10999 op0 = new_rtx;
11003 op0 = adjust_address (op0, mode, 0);
11006 return op0;
11008 case MODIFY_EXPR:
11010 tree lhs = treeop0;
11011 tree rhs = treeop1;
11012 gcc_assert (ignore);
11014 /* Check for |= or &= of a bitfield of size one into another bitfield
11015 of size 1. In this case, (unless we need the result of the
11016 assignment) we can do this more efficiently with a
11017 test followed by an assignment, if necessary.
11019 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11020 things change so we do, this code should be enhanced to
11021 support it. */
11022 if (TREE_CODE (lhs) == COMPONENT_REF
11023 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11024 || TREE_CODE (rhs) == BIT_AND_EXPR)
11025 && TREE_OPERAND (rhs, 0) == lhs
11026 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11027 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11028 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11030 rtx_code_label *label = gen_label_rtx ();
11031 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11032 do_jump (TREE_OPERAND (rhs, 1),
11033 value ? label : 0,
11034 value ? 0 : label, -1);
11035 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11036 false);
11037 do_pending_stack_adjust ();
11038 emit_label (label);
11039 return const0_rtx;
11042 expand_assignment (lhs, rhs, false);
11043 return const0_rtx;
11046 case ADDR_EXPR:
11047 return expand_expr_addr_expr (exp, target, tmode, modifier);
11049 case REALPART_EXPR:
11050 op0 = expand_normal (treeop0);
11051 return read_complex_part (op0, false);
11053 case IMAGPART_EXPR:
11054 op0 = expand_normal (treeop0);
11055 return read_complex_part (op0, true);
11057 case RETURN_EXPR:
11058 case LABEL_EXPR:
11059 case GOTO_EXPR:
11060 case SWITCH_EXPR:
11061 case ASM_EXPR:
11062 /* Expanded in cfgexpand.c. */
11063 gcc_unreachable ();
11065 case TRY_CATCH_EXPR:
11066 case CATCH_EXPR:
11067 case EH_FILTER_EXPR:
11068 case TRY_FINALLY_EXPR:
11069 /* Lowered by tree-eh.c. */
11070 gcc_unreachable ();
11072 case WITH_CLEANUP_EXPR:
11073 case CLEANUP_POINT_EXPR:
11074 case TARGET_EXPR:
11075 case CASE_LABEL_EXPR:
11076 case VA_ARG_EXPR:
11077 case BIND_EXPR:
11078 case INIT_EXPR:
11079 case CONJ_EXPR:
11080 case COMPOUND_EXPR:
11081 case PREINCREMENT_EXPR:
11082 case PREDECREMENT_EXPR:
11083 case POSTINCREMENT_EXPR:
11084 case POSTDECREMENT_EXPR:
11085 case LOOP_EXPR:
11086 case EXIT_EXPR:
11087 case COMPOUND_LITERAL_EXPR:
11088 /* Lowered by gimplify.c. */
11089 gcc_unreachable ();
11091 case FDESC_EXPR:
11092 /* Function descriptors are not valid except for as
11093 initialization constants, and should not be expanded. */
11094 gcc_unreachable ();
11096 case WITH_SIZE_EXPR:
11097 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11098 have pulled out the size to use in whatever context it needed. */
11099 return expand_expr_real (treeop0, original_target, tmode,
11100 modifier, alt_rtl, inner_reference_p);
11102 default:
11103 return expand_expr_real_2 (&ops, target, tmode, modifier);
11107 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11108 signedness of TYPE), possibly returning the result in TARGET. */
11109 static rtx
11110 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11112 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11113 if (target && GET_MODE (target) != GET_MODE (exp))
11114 target = 0;
11115 /* For constant values, reduce using build_int_cst_type. */
11116 if (CONST_INT_P (exp))
11118 HOST_WIDE_INT value = INTVAL (exp);
11119 tree t = build_int_cst_type (type, value);
11120 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11122 else if (TYPE_UNSIGNED (type))
11124 machine_mode mode = GET_MODE (exp);
11125 rtx mask = immed_wide_int_const
11126 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11127 return expand_and (mode, exp, mask, target);
11129 else
11131 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11132 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11133 exp, count, target, 0);
11134 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11135 exp, count, target, 0);
11139 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11140 when applied to the address of EXP produces an address known to be
11141 aligned more than BIGGEST_ALIGNMENT. */
11143 static int
11144 is_aligning_offset (const_tree offset, const_tree exp)
11146 /* Strip off any conversions. */
11147 while (CONVERT_EXPR_P (offset))
11148 offset = TREE_OPERAND (offset, 0);
11150 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11151 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11152 if (TREE_CODE (offset) != BIT_AND_EXPR
11153 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11154 || compare_tree_int (TREE_OPERAND (offset, 1),
11155 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11156 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11157 return 0;
11159 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11160 It must be NEGATE_EXPR. Then strip any more conversions. */
11161 offset = TREE_OPERAND (offset, 0);
11162 while (CONVERT_EXPR_P (offset))
11163 offset = TREE_OPERAND (offset, 0);
11165 if (TREE_CODE (offset) != NEGATE_EXPR)
11166 return 0;
11168 offset = TREE_OPERAND (offset, 0);
11169 while (CONVERT_EXPR_P (offset))
11170 offset = TREE_OPERAND (offset, 0);
11172 /* This must now be the address of EXP. */
11173 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11176 /* Return the tree node if an ARG corresponds to a string constant or zero
11177 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11178 in bytes within the string that ARG is accessing. The type of the
11179 offset will be `sizetype'. */
11181 tree
11182 string_constant (tree arg, tree *ptr_offset)
11184 tree array, offset, lower_bound;
11185 STRIP_NOPS (arg);
11187 if (TREE_CODE (arg) == ADDR_EXPR)
11189 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11191 *ptr_offset = size_zero_node;
11192 return TREE_OPERAND (arg, 0);
11194 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11196 array = TREE_OPERAND (arg, 0);
11197 offset = size_zero_node;
11199 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11201 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11202 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11203 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11204 return 0;
11206 /* Check if the array has a nonzero lower bound. */
11207 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11208 if (!integer_zerop (lower_bound))
11210 /* If the offset and base aren't both constants, return 0. */
11211 if (TREE_CODE (lower_bound) != INTEGER_CST)
11212 return 0;
11213 if (TREE_CODE (offset) != INTEGER_CST)
11214 return 0;
11215 /* Adjust offset by the lower bound. */
11216 offset = size_diffop (fold_convert (sizetype, offset),
11217 fold_convert (sizetype, lower_bound));
11220 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11222 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11223 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11224 if (TREE_CODE (array) != ADDR_EXPR)
11225 return 0;
11226 array = TREE_OPERAND (array, 0);
11227 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11228 return 0;
11230 else
11231 return 0;
11233 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11235 tree arg0 = TREE_OPERAND (arg, 0);
11236 tree arg1 = TREE_OPERAND (arg, 1);
11238 STRIP_NOPS (arg0);
11239 STRIP_NOPS (arg1);
11241 if (TREE_CODE (arg0) == ADDR_EXPR
11242 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11243 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11245 array = TREE_OPERAND (arg0, 0);
11246 offset = arg1;
11248 else if (TREE_CODE (arg1) == ADDR_EXPR
11249 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11250 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11252 array = TREE_OPERAND (arg1, 0);
11253 offset = arg0;
11255 else
11256 return 0;
11258 else
11259 return 0;
11261 if (TREE_CODE (array) == STRING_CST)
11263 *ptr_offset = fold_convert (sizetype, offset);
11264 return array;
11266 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11268 int length;
11269 tree init = ctor_for_folding (array);
11271 /* Variables initialized to string literals can be handled too. */
11272 if (init == error_mark_node
11273 || !init
11274 || TREE_CODE (init) != STRING_CST)
11275 return 0;
11277 /* Avoid const char foo[4] = "abcde"; */
11278 if (DECL_SIZE_UNIT (array) == NULL_TREE
11279 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11280 || (length = TREE_STRING_LENGTH (init)) <= 0
11281 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11282 return 0;
11284 /* If variable is bigger than the string literal, OFFSET must be constant
11285 and inside of the bounds of the string literal. */
11286 offset = fold_convert (sizetype, offset);
11287 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11288 && (! tree_fits_uhwi_p (offset)
11289 || compare_tree_int (offset, length) >= 0))
11290 return 0;
11292 *ptr_offset = offset;
11293 return init;
11296 return 0;
11299 /* Generate code to calculate OPS, and exploded expression
11300 using a store-flag instruction and return an rtx for the result.
11301 OPS reflects a comparison.
11303 If TARGET is nonzero, store the result there if convenient.
11305 Return zero if there is no suitable set-flag instruction
11306 available on this machine.
11308 Once expand_expr has been called on the arguments of the comparison,
11309 we are committed to doing the store flag, since it is not safe to
11310 re-evaluate the expression. We emit the store-flag insn by calling
11311 emit_store_flag, but only expand the arguments if we have a reason
11312 to believe that emit_store_flag will be successful. If we think that
11313 it will, but it isn't, we have to simulate the store-flag with a
11314 set/jump/set sequence. */
11316 static rtx
11317 do_store_flag (sepops ops, rtx target, machine_mode mode)
11319 enum rtx_code code;
11320 tree arg0, arg1, type;
11321 machine_mode operand_mode;
11322 int unsignedp;
11323 rtx op0, op1;
11324 rtx subtarget = target;
11325 location_t loc = ops->location;
11327 arg0 = ops->op0;
11328 arg1 = ops->op1;
11330 /* Don't crash if the comparison was erroneous. */
11331 if (arg0 == error_mark_node || arg1 == error_mark_node)
11332 return const0_rtx;
11334 type = TREE_TYPE (arg0);
11335 operand_mode = TYPE_MODE (type);
11336 unsignedp = TYPE_UNSIGNED (type);
11338 /* We won't bother with BLKmode store-flag operations because it would mean
11339 passing a lot of information to emit_store_flag. */
11340 if (operand_mode == BLKmode)
11341 return 0;
11343 /* We won't bother with store-flag operations involving function pointers
11344 when function pointers must be canonicalized before comparisons. */
11345 if (targetm.have_canonicalize_funcptr_for_compare ()
11346 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11347 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11348 == FUNCTION_TYPE))
11349 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11350 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11351 == FUNCTION_TYPE))))
11352 return 0;
11354 STRIP_NOPS (arg0);
11355 STRIP_NOPS (arg1);
11357 /* For vector typed comparisons emit code to generate the desired
11358 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11359 expander for this. */
11360 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11362 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11363 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11364 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11365 return expand_vec_cmp_expr (ops->type, ifexp, target);
11366 else
11368 tree if_true = constant_boolean_node (true, ops->type);
11369 tree if_false = constant_boolean_node (false, ops->type);
11370 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11371 if_false, target);
11375 /* Get the rtx comparison code to use. We know that EXP is a comparison
11376 operation of some type. Some comparisons against 1 and -1 can be
11377 converted to comparisons with zero. Do so here so that the tests
11378 below will be aware that we have a comparison with zero. These
11379 tests will not catch constants in the first operand, but constants
11380 are rarely passed as the first operand. */
11382 switch (ops->code)
11384 case EQ_EXPR:
11385 code = EQ;
11386 break;
11387 case NE_EXPR:
11388 code = NE;
11389 break;
11390 case LT_EXPR:
11391 if (integer_onep (arg1))
11392 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11393 else
11394 code = unsignedp ? LTU : LT;
11395 break;
11396 case LE_EXPR:
11397 if (! unsignedp && integer_all_onesp (arg1))
11398 arg1 = integer_zero_node, code = LT;
11399 else
11400 code = unsignedp ? LEU : LE;
11401 break;
11402 case GT_EXPR:
11403 if (! unsignedp && integer_all_onesp (arg1))
11404 arg1 = integer_zero_node, code = GE;
11405 else
11406 code = unsignedp ? GTU : GT;
11407 break;
11408 case GE_EXPR:
11409 if (integer_onep (arg1))
11410 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11411 else
11412 code = unsignedp ? GEU : GE;
11413 break;
11415 case UNORDERED_EXPR:
11416 code = UNORDERED;
11417 break;
11418 case ORDERED_EXPR:
11419 code = ORDERED;
11420 break;
11421 case UNLT_EXPR:
11422 code = UNLT;
11423 break;
11424 case UNLE_EXPR:
11425 code = UNLE;
11426 break;
11427 case UNGT_EXPR:
11428 code = UNGT;
11429 break;
11430 case UNGE_EXPR:
11431 code = UNGE;
11432 break;
11433 case UNEQ_EXPR:
11434 code = UNEQ;
11435 break;
11436 case LTGT_EXPR:
11437 code = LTGT;
11438 break;
11440 default:
11441 gcc_unreachable ();
11444 /* Put a constant second. */
11445 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11446 || TREE_CODE (arg0) == FIXED_CST)
11448 std::swap (arg0, arg1);
11449 code = swap_condition (code);
11452 /* If this is an equality or inequality test of a single bit, we can
11453 do this by shifting the bit being tested to the low-order bit and
11454 masking the result with the constant 1. If the condition was EQ,
11455 we xor it with 1. This does not require an scc insn and is faster
11456 than an scc insn even if we have it.
11458 The code to make this transformation was moved into fold_single_bit_test,
11459 so we just call into the folder and expand its result. */
11461 if ((code == NE || code == EQ)
11462 && integer_zerop (arg1)
11463 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11465 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11466 if (srcstmt
11467 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11469 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11470 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11471 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11472 gimple_assign_rhs1 (srcstmt),
11473 gimple_assign_rhs2 (srcstmt));
11474 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11475 if (temp)
11476 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11480 if (! get_subtarget (target)
11481 || GET_MODE (subtarget) != operand_mode)
11482 subtarget = 0;
11484 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11486 if (target == 0)
11487 target = gen_reg_rtx (mode);
11489 /* Try a cstore if possible. */
11490 return emit_store_flag_force (target, code, op0, op1,
11491 operand_mode, unsignedp,
11492 (TYPE_PRECISION (ops->type) == 1
11493 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11496 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11497 0 otherwise (i.e. if there is no casesi instruction).
11499 DEFAULT_PROBABILITY is the probability of jumping to the default
11500 label. */
11502 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11503 rtx table_label, rtx default_label, rtx fallback_label,
11504 int default_probability)
11506 struct expand_operand ops[5];
11507 machine_mode index_mode = SImode;
11508 rtx op1, op2, index;
11510 if (! targetm.have_casesi ())
11511 return 0;
11513 /* Convert the index to SImode. */
11514 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11516 machine_mode omode = TYPE_MODE (index_type);
11517 rtx rangertx = expand_normal (range);
11519 /* We must handle the endpoints in the original mode. */
11520 index_expr = build2 (MINUS_EXPR, index_type,
11521 index_expr, minval);
11522 minval = integer_zero_node;
11523 index = expand_normal (index_expr);
11524 if (default_label)
11525 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11526 omode, 1, default_label,
11527 default_probability);
11528 /* Now we can safely truncate. */
11529 index = convert_to_mode (index_mode, index, 0);
11531 else
11533 if (TYPE_MODE (index_type) != index_mode)
11535 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11536 index_expr = fold_convert (index_type, index_expr);
11539 index = expand_normal (index_expr);
11542 do_pending_stack_adjust ();
11544 op1 = expand_normal (minval);
11545 op2 = expand_normal (range);
11547 create_input_operand (&ops[0], index, index_mode);
11548 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11549 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11550 create_fixed_operand (&ops[3], table_label);
11551 create_fixed_operand (&ops[4], (default_label
11552 ? default_label
11553 : fallback_label));
11554 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11555 return 1;
11558 /* Attempt to generate a tablejump instruction; same concept. */
11559 /* Subroutine of the next function.
11561 INDEX is the value being switched on, with the lowest value
11562 in the table already subtracted.
11563 MODE is its expected mode (needed if INDEX is constant).
11564 RANGE is the length of the jump table.
11565 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11567 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11568 index value is out of range.
11569 DEFAULT_PROBABILITY is the probability of jumping to
11570 the default label. */
11572 static void
11573 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11574 rtx default_label, int default_probability)
11576 rtx temp, vector;
11578 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11579 cfun->cfg->max_jumptable_ents = INTVAL (range);
11581 /* Do an unsigned comparison (in the proper mode) between the index
11582 expression and the value which represents the length of the range.
11583 Since we just finished subtracting the lower bound of the range
11584 from the index expression, this comparison allows us to simultaneously
11585 check that the original index expression value is both greater than
11586 or equal to the minimum value of the range and less than or equal to
11587 the maximum value of the range. */
11589 if (default_label)
11590 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11591 default_label, default_probability);
11594 /* If index is in range, it must fit in Pmode.
11595 Convert to Pmode so we can index with it. */
11596 if (mode != Pmode)
11597 index = convert_to_mode (Pmode, index, 1);
11599 /* Don't let a MEM slip through, because then INDEX that comes
11600 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11601 and break_out_memory_refs will go to work on it and mess it up. */
11602 #ifdef PIC_CASE_VECTOR_ADDRESS
11603 if (flag_pic && !REG_P (index))
11604 index = copy_to_mode_reg (Pmode, index);
11605 #endif
11607 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11608 GET_MODE_SIZE, because this indicates how large insns are. The other
11609 uses should all be Pmode, because they are addresses. This code
11610 could fail if addresses and insns are not the same size. */
11611 index = simplify_gen_binary (MULT, Pmode, index,
11612 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11613 Pmode));
11614 index = simplify_gen_binary (PLUS, Pmode, index,
11615 gen_rtx_LABEL_REF (Pmode, table_label));
11617 #ifdef PIC_CASE_VECTOR_ADDRESS
11618 if (flag_pic)
11619 index = PIC_CASE_VECTOR_ADDRESS (index);
11620 else
11621 #endif
11622 index = memory_address (CASE_VECTOR_MODE, index);
11623 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11624 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11625 convert_move (temp, vector, 0);
11627 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11629 /* If we are generating PIC code or if the table is PC-relative, the
11630 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11631 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11632 emit_barrier ();
11636 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11637 rtx table_label, rtx default_label, int default_probability)
11639 rtx index;
11641 if (! targetm.have_tablejump ())
11642 return 0;
11644 index_expr = fold_build2 (MINUS_EXPR, index_type,
11645 fold_convert (index_type, index_expr),
11646 fold_convert (index_type, minval));
11647 index = expand_normal (index_expr);
11648 do_pending_stack_adjust ();
11650 do_tablejump (index, TYPE_MODE (index_type),
11651 convert_modes (TYPE_MODE (index_type),
11652 TYPE_MODE (TREE_TYPE (range)),
11653 expand_normal (range),
11654 TYPE_UNSIGNED (TREE_TYPE (range))),
11655 table_label, default_label, default_probability);
11656 return 1;
11659 /* Return a CONST_VECTOR rtx representing vector mask for
11660 a VECTOR_CST of booleans. */
11661 static rtx
11662 const_vector_mask_from_tree (tree exp)
11664 rtvec v;
11665 unsigned i;
11666 int units;
11667 tree elt;
11668 machine_mode inner, mode;
11670 mode = TYPE_MODE (TREE_TYPE (exp));
11671 units = GET_MODE_NUNITS (mode);
11672 inner = GET_MODE_INNER (mode);
11674 v = rtvec_alloc (units);
11676 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11678 elt = VECTOR_CST_ELT (exp, i);
11680 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11681 if (integer_zerop (elt))
11682 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11683 else if (integer_onep (elt)
11684 || integer_minus_onep (elt))
11685 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11686 else
11687 gcc_unreachable ();
11690 return gen_rtx_CONST_VECTOR (mode, v);
11693 /* Return a CONST_INT rtx representing vector mask for
11694 a VECTOR_CST of booleans. */
11695 static rtx
11696 const_scalar_mask_from_tree (tree exp)
11698 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11699 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11700 tree elt;
11701 unsigned i;
11703 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11705 elt = VECTOR_CST_ELT (exp, i);
11706 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11707 if (integer_all_onesp (elt))
11708 res = wi::set_bit (res, i);
11709 else
11710 gcc_assert (integer_zerop (elt));
11713 return immed_wide_int_const (res, mode);
11716 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11717 static rtx
11718 const_vector_from_tree (tree exp)
11720 rtvec v;
11721 unsigned i;
11722 int units;
11723 tree elt;
11724 machine_mode inner, mode;
11726 mode = TYPE_MODE (TREE_TYPE (exp));
11728 if (initializer_zerop (exp))
11729 return CONST0_RTX (mode);
11731 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11732 return const_vector_mask_from_tree (exp);
11734 units = GET_MODE_NUNITS (mode);
11735 inner = GET_MODE_INNER (mode);
11737 v = rtvec_alloc (units);
11739 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11741 elt = VECTOR_CST_ELT (exp, i);
11743 if (TREE_CODE (elt) == REAL_CST)
11744 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11745 inner);
11746 else if (TREE_CODE (elt) == FIXED_CST)
11747 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11748 inner);
11749 else
11750 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11753 return gen_rtx_CONST_VECTOR (mode, v);
11756 /* Build a decl for a personality function given a language prefix. */
11758 tree
11759 build_personality_function (const char *lang)
11761 const char *unwind_and_version;
11762 tree decl, type;
11763 char *name;
11765 switch (targetm_common.except_unwind_info (&global_options))
11767 case UI_NONE:
11768 return NULL;
11769 case UI_SJLJ:
11770 unwind_and_version = "_sj0";
11771 break;
11772 case UI_DWARF2:
11773 case UI_TARGET:
11774 unwind_and_version = "_v0";
11775 break;
11776 case UI_SEH:
11777 unwind_and_version = "_seh0";
11778 break;
11779 default:
11780 gcc_unreachable ();
11783 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11785 type = build_function_type_list (integer_type_node, integer_type_node,
11786 long_long_unsigned_type_node,
11787 ptr_type_node, ptr_type_node, NULL_TREE);
11788 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11789 get_identifier (name), type);
11790 DECL_ARTIFICIAL (decl) = 1;
11791 DECL_EXTERNAL (decl) = 1;
11792 TREE_PUBLIC (decl) = 1;
11794 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11795 are the flags assigned by targetm.encode_section_info. */
11796 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11798 return decl;
11801 /* Extracts the personality function of DECL and returns the corresponding
11802 libfunc. */
11805 get_personality_function (tree decl)
11807 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11808 enum eh_personality_kind pk;
11810 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11811 if (pk == eh_personality_none)
11812 return NULL;
11814 if (!personality
11815 && pk == eh_personality_any)
11816 personality = lang_hooks.eh_personality ();
11818 if (pk == eh_personality_lang)
11819 gcc_assert (personality != NULL_TREE);
11821 return XEXP (DECL_RTL (personality), 0);
11824 /* Returns a tree for the size of EXP in bytes. */
11826 static tree
11827 tree_expr_size (const_tree exp)
11829 if (DECL_P (exp)
11830 && DECL_SIZE_UNIT (exp) != 0)
11831 return DECL_SIZE_UNIT (exp);
11832 else
11833 return size_in_bytes (TREE_TYPE (exp));
11836 /* Return an rtx for the size in bytes of the value of EXP. */
11839 expr_size (tree exp)
11841 tree size;
11843 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11844 size = TREE_OPERAND (exp, 1);
11845 else
11847 size = tree_expr_size (exp);
11848 gcc_assert (size);
11849 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11852 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11855 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11856 if the size can vary or is larger than an integer. */
11858 static HOST_WIDE_INT
11859 int_expr_size (tree exp)
11861 tree size;
11863 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11864 size = TREE_OPERAND (exp, 1);
11865 else
11867 size = tree_expr_size (exp);
11868 gcc_assert (size);
11871 if (size == 0 || !tree_fits_shwi_p (size))
11872 return -1;
11874 return tree_to_shwi (size);