[20/77] Replace MODE_INT checks with is_int_mode
[official-gcc.git] / gcc / expr.c
blob225b8c2925ecd7f939a99c76561333624984c178
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,
100 profile_probability);
101 static rtx const_vector_from_tree (tree);
102 static rtx const_scalar_mask_from_tree (tree);
103 static tree tree_expr_size (const_tree);
104 static HOST_WIDE_INT int_expr_size (tree);
107 /* This is run to set up which modes can be used
108 directly in memory and to initialize the block move optab. It is run
109 at the beginning of compilation and when the target is reinitialized. */
111 void
112 init_expr_target (void)
114 rtx pat;
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 (machine_mode 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 opt_scalar_float_mode mode_iter;
180 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_FLOAT)
182 scalar_float_mode mode = mode_iter.require ();
183 scalar_float_mode srcmode;
184 FOR_EACH_MODE_UNTIL (srcmode, mode)
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 scalar_int_mode full_mode
350 = smallest_int_mode_for_size (GET_MODE_BITSIZE (to_mode));
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 scalar_int_mode full_mode
365 = smallest_int_mode_for_size (GET_MODE_BITSIZE (from_mode));
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_EACH_MODE_FROM (intermediate, from_mode)
552 if (((can_extend_p (to_mode, intermediate, unsignedp)
553 != CODE_FOR_nothing)
554 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
555 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
556 && (can_extend_p (intermediate, from_mode, unsignedp)
557 != CODE_FOR_nothing))
559 convert_move (to, convert_to_mode (intermediate, from,
560 unsignedp), unsignedp);
561 return;
564 /* No suitable intermediate mode.
565 Generate what we need with shifts. */
566 shift_amount = (GET_MODE_PRECISION (to_mode)
567 - GET_MODE_PRECISION (from_mode));
568 from = gen_lowpart (to_mode, force_reg (from_mode, from));
569 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
570 to, unsignedp);
571 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
572 to, unsignedp);
573 if (tmp != to)
574 emit_move_insn (to, tmp);
575 return;
579 /* Support special truncate insns for certain modes. */
580 if (convert_optab_handler (trunc_optab, to_mode,
581 from_mode) != CODE_FOR_nothing)
583 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
584 to, from, UNKNOWN);
585 return;
588 /* Handle truncation of volatile memrefs, and so on;
589 the things that couldn't be truncated directly,
590 and for which there was no special instruction.
592 ??? Code above formerly short-circuited this, for most integer
593 mode pairs, with a force_reg in from_mode followed by a recursive
594 call to this routine. Appears always to have been wrong. */
595 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
597 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
598 emit_move_insn (to, temp);
599 return;
602 /* Mode combination is not recognized. */
603 gcc_unreachable ();
606 /* Return an rtx for a value that would result
607 from converting X to mode MODE.
608 Both X and MODE may be floating, or both integer.
609 UNSIGNEDP is nonzero if X is an unsigned value.
610 This can be done by referring to a part of X in place
611 or by copying to a new temporary with conversion. */
614 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
616 return convert_modes (mode, VOIDmode, x, unsignedp);
619 /* Return an rtx for a value that would result
620 from converting X from mode OLDMODE to mode MODE.
621 Both modes may be floating, or both integer.
622 UNSIGNEDP is nonzero if X is an unsigned value.
624 This can be done by referring to a part of X in place
625 or by copying to a new temporary with conversion.
627 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
630 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
632 rtx temp;
633 scalar_int_mode int_mode;
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)
650 && is_int_mode (mode, &int_mode))
652 /* If the caller did not tell us the old mode, then there is not
653 much to do with respect to canonicalization. We have to
654 assume that all the bits are significant. */
655 if (GET_MODE_CLASS (oldmode) != MODE_INT)
656 oldmode = MAX_MODE_INT;
657 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
658 GET_MODE_PRECISION (int_mode),
659 unsignedp ? UNSIGNED : SIGNED);
660 return immed_wide_int_const (w, int_mode);
663 /* We can do this with a gen_lowpart if both desired and current modes
664 are integer, and this is either a constant integer, a register, or a
665 non-volatile MEM. */
666 scalar_int_mode int_oldmode;
667 if (is_int_mode (mode, &int_mode)
668 && is_int_mode (oldmode, &int_oldmode)
669 && GET_MODE_PRECISION (int_mode) <= GET_MODE_PRECISION (int_oldmode)
670 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) int_mode])
671 || (REG_P (x)
672 && (!HARD_REGISTER_P (x)
673 || HARD_REGNO_MODE_OK (REGNO (x), int_mode))
674 && TRULY_NOOP_TRUNCATION_MODES_P (int_mode, GET_MODE (x)))))
675 return gen_lowpart (int_mode, x);
677 /* Converting from integer constant into mode is always equivalent to an
678 subreg operation. */
679 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
681 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
682 return simplify_gen_subreg (mode, x, oldmode, 0);
685 temp = gen_reg_rtx (mode);
686 convert_move (temp, x, unsignedp);
687 return temp;
690 /* Return the largest alignment we can use for doing a move (or store)
691 of MAX_PIECES. ALIGN is the largest alignment we could use. */
693 static unsigned int
694 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
696 machine_mode tmode;
698 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
699 if (align >= GET_MODE_ALIGNMENT (tmode))
700 align = GET_MODE_ALIGNMENT (tmode);
701 else
703 machine_mode tmode, xmode;
705 xmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
706 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
708 if (GET_MODE_SIZE (tmode) > max_pieces
709 || SLOW_UNALIGNED_ACCESS (tmode, align))
710 break;
711 xmode = tmode;
714 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
717 return align;
720 /* Return the widest integer mode no wider than SIZE. If no such mode
721 can be found, return VOIDmode. */
723 static machine_mode
724 widest_int_mode_for_size (unsigned int size)
726 machine_mode tmode, mode = VOIDmode;
728 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
729 if (GET_MODE_SIZE (tmode) < size)
730 mode = tmode;
732 return mode;
735 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
736 and should be performed piecewise. */
738 static bool
739 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
740 enum by_pieces_operation op)
742 return targetm.use_by_pieces_infrastructure_p (len, align, op,
743 optimize_insn_for_speed_p ());
746 /* Determine whether the LEN bytes can be moved by using several move
747 instructions. Return nonzero if a call to move_by_pieces should
748 succeed. */
750 bool
751 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
753 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
756 /* Return number of insns required to perform operation OP by pieces
757 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
759 unsigned HOST_WIDE_INT
760 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
761 unsigned int max_size, by_pieces_operation op)
763 unsigned HOST_WIDE_INT n_insns = 0;
765 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
767 while (max_size > 1 && l > 0)
769 machine_mode mode;
770 enum insn_code icode;
772 mode = widest_int_mode_for_size (max_size);
774 if (mode == VOIDmode)
775 break;
776 unsigned int modesize = GET_MODE_SIZE (mode);
778 icode = optab_handler (mov_optab, mode);
779 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
781 unsigned HOST_WIDE_INT n_pieces = l / modesize;
782 l %= modesize;
783 switch (op)
785 default:
786 n_insns += n_pieces;
787 break;
789 case COMPARE_BY_PIECES:
790 int batch = targetm.compare_by_pieces_branch_ratio (mode);
791 int batch_ops = 4 * batch - 1;
792 unsigned HOST_WIDE_INT full = n_pieces / batch;
793 n_insns += full * batch_ops;
794 if (n_pieces % batch != 0)
795 n_insns++;
796 break;
800 max_size = modesize;
803 gcc_assert (!l);
804 return n_insns;
807 /* Used when performing piecewise block operations, holds information
808 about one of the memory objects involved. The member functions
809 can be used to generate code for loading from the object and
810 updating the address when iterating. */
812 class pieces_addr
814 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
815 stack pushes. */
816 rtx m_obj;
817 /* The address of the object. Can differ from that seen in the
818 MEM rtx if we copied the address to a register. */
819 rtx m_addr;
820 /* Nonzero if the address on the object has an autoincrement already,
821 signifies whether that was an increment or decrement. */
822 signed char m_addr_inc;
823 /* Nonzero if we intend to use autoinc without the address already
824 having autoinc form. We will insert add insns around each memory
825 reference, expecting later passes to form autoinc addressing modes.
826 The only supported options are predecrement and postincrement. */
827 signed char m_explicit_inc;
828 /* True if we have either of the two possible cases of using
829 autoincrement. */
830 bool m_auto;
831 /* True if this is an address to be used for load operations rather
832 than stores. */
833 bool m_is_load;
835 /* Optionally, a function to obtain constants for any given offset into
836 the objects, and data associated with it. */
837 by_pieces_constfn m_constfn;
838 void *m_cfndata;
839 public:
840 pieces_addr (rtx, bool, by_pieces_constfn, void *);
841 rtx adjust (machine_mode, HOST_WIDE_INT);
842 void increment_address (HOST_WIDE_INT);
843 void maybe_predec (HOST_WIDE_INT);
844 void maybe_postinc (HOST_WIDE_INT);
845 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
846 int get_addr_inc ()
848 return m_addr_inc;
852 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
853 true if the operation to be performed on this object is a load
854 rather than a store. For stores, OBJ can be NULL, in which case we
855 assume the operation is a stack push. For loads, the optional
856 CONSTFN and its associated CFNDATA can be used in place of the
857 memory load. */
859 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
860 void *cfndata)
861 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
863 m_addr_inc = 0;
864 m_auto = false;
865 if (obj)
867 rtx addr = XEXP (obj, 0);
868 rtx_code code = GET_CODE (addr);
869 m_addr = addr;
870 bool dec = code == PRE_DEC || code == POST_DEC;
871 bool inc = code == PRE_INC || code == POST_INC;
872 m_auto = inc || dec;
873 if (m_auto)
874 m_addr_inc = dec ? -1 : 1;
876 /* While we have always looked for these codes here, the code
877 implementing the memory operation has never handled them.
878 Support could be added later if necessary or beneficial. */
879 gcc_assert (code != PRE_INC && code != POST_DEC);
881 else
883 m_addr = NULL_RTX;
884 if (!is_load)
886 m_auto = true;
887 if (STACK_GROWS_DOWNWARD)
888 m_addr_inc = -1;
889 else
890 m_addr_inc = 1;
892 else
893 gcc_assert (constfn != NULL);
895 m_explicit_inc = 0;
896 if (constfn)
897 gcc_assert (is_load);
900 /* Decide whether to use autoinc for an address involved in a memory op.
901 MODE is the mode of the accesses, REVERSE is true if we've decided to
902 perform the operation starting from the end, and LEN is the length of
903 the operation. Don't override an earlier decision to set m_auto. */
905 void
906 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
907 HOST_WIDE_INT len)
909 if (m_auto || m_obj == NULL_RTX)
910 return;
912 bool use_predec = (m_is_load
913 ? USE_LOAD_PRE_DECREMENT (mode)
914 : USE_STORE_PRE_DECREMENT (mode));
915 bool use_postinc = (m_is_load
916 ? USE_LOAD_POST_INCREMENT (mode)
917 : USE_STORE_POST_INCREMENT (mode));
918 machine_mode addr_mode = get_address_mode (m_obj);
920 if (use_predec && reverse)
922 m_addr = copy_to_mode_reg (addr_mode,
923 plus_constant (addr_mode,
924 m_addr, len));
925 m_auto = true;
926 m_explicit_inc = -1;
928 else if (use_postinc && !reverse)
930 m_addr = copy_to_mode_reg (addr_mode, m_addr);
931 m_auto = true;
932 m_explicit_inc = 1;
934 else if (CONSTANT_P (m_addr))
935 m_addr = copy_to_mode_reg (addr_mode, m_addr);
938 /* Adjust the address to refer to the data at OFFSET in MODE. If we
939 are using autoincrement for this address, we don't add the offset,
940 but we still modify the MEM's properties. */
943 pieces_addr::adjust (machine_mode mode, HOST_WIDE_INT offset)
945 if (m_constfn)
946 return m_constfn (m_cfndata, offset, mode);
947 if (m_obj == NULL_RTX)
948 return NULL_RTX;
949 if (m_auto)
950 return adjust_automodify_address (m_obj, mode, m_addr, offset);
951 else
952 return adjust_address (m_obj, mode, offset);
955 /* Emit an add instruction to increment the address by SIZE. */
957 void
958 pieces_addr::increment_address (HOST_WIDE_INT size)
960 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
961 emit_insn (gen_add2_insn (m_addr, amount));
964 /* If we are supposed to decrement the address after each access, emit code
965 to do so now. Increment by SIZE (which has should have the correct sign
966 already). */
968 void
969 pieces_addr::maybe_predec (HOST_WIDE_INT size)
971 if (m_explicit_inc >= 0)
972 return;
973 gcc_assert (HAVE_PRE_DECREMENT);
974 increment_address (size);
977 /* If we are supposed to decrement the address after each access, emit code
978 to do so now. Increment by SIZE. */
980 void
981 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
983 if (m_explicit_inc <= 0)
984 return;
985 gcc_assert (HAVE_POST_INCREMENT);
986 increment_address (size);
989 /* This structure is used by do_op_by_pieces to describe the operation
990 to be performed. */
992 class op_by_pieces_d
994 protected:
995 pieces_addr m_to, m_from;
996 unsigned HOST_WIDE_INT m_len;
997 HOST_WIDE_INT m_offset;
998 unsigned int m_align;
999 unsigned int m_max_size;
1000 bool m_reverse;
1002 /* Virtual functions, overriden by derived classes for the specific
1003 operation. */
1004 virtual void generate (rtx, rtx, machine_mode) = 0;
1005 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1006 virtual void finish_mode (machine_mode)
1010 public:
1011 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1012 unsigned HOST_WIDE_INT, unsigned int);
1013 void run ();
1016 /* The constructor for an op_by_pieces_d structure. We require two
1017 objects named TO and FROM, which are identified as loads or stores
1018 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1019 and its associated FROM_CFN_DATA can be used to replace loads with
1020 constant values. LEN describes the length of the operation. */
1022 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1023 rtx from, bool from_load,
1024 by_pieces_constfn from_cfn,
1025 void *from_cfn_data,
1026 unsigned HOST_WIDE_INT len,
1027 unsigned int align)
1028 : m_to (to, to_load, NULL, NULL),
1029 m_from (from, from_load, from_cfn, from_cfn_data),
1030 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1032 int toi = m_to.get_addr_inc ();
1033 int fromi = m_from.get_addr_inc ();
1034 if (toi >= 0 && fromi >= 0)
1035 m_reverse = false;
1036 else if (toi <= 0 && fromi <= 0)
1037 m_reverse = true;
1038 else
1039 gcc_unreachable ();
1041 m_offset = m_reverse ? len : 0;
1042 align = MIN (to ? MEM_ALIGN (to) : align,
1043 from ? MEM_ALIGN (from) : align);
1045 /* If copying requires more than two move insns,
1046 copy addresses to registers (to make displacements shorter)
1047 and use post-increment if available. */
1048 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1050 /* Find the mode of the largest comparison. */
1051 machine_mode mode = widest_int_mode_for_size (m_max_size);
1053 m_from.decide_autoinc (mode, m_reverse, len);
1054 m_to.decide_autoinc (mode, m_reverse, len);
1057 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1058 m_align = align;
1061 /* This function contains the main loop used for expanding a block
1062 operation. First move what we can in the largest integer mode,
1063 then go to successively smaller modes. For every access, call
1064 GENFUN with the two operands and the EXTRA_DATA. */
1066 void
1067 op_by_pieces_d::run ()
1069 while (m_max_size > 1 && m_len > 0)
1071 machine_mode mode = widest_int_mode_for_size (m_max_size);
1073 if (mode == VOIDmode)
1074 break;
1076 if (prepare_mode (mode, m_align))
1078 unsigned int size = GET_MODE_SIZE (mode);
1079 rtx to1 = NULL_RTX, from1;
1081 while (m_len >= size)
1083 if (m_reverse)
1084 m_offset -= size;
1086 to1 = m_to.adjust (mode, m_offset);
1087 from1 = m_from.adjust (mode, m_offset);
1089 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1090 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1092 generate (to1, from1, mode);
1094 m_to.maybe_postinc (size);
1095 m_from.maybe_postinc (size);
1097 if (!m_reverse)
1098 m_offset += size;
1100 m_len -= size;
1103 finish_mode (mode);
1106 m_max_size = GET_MODE_SIZE (mode);
1109 /* The code above should have handled everything. */
1110 gcc_assert (!m_len);
1113 /* Derived class from op_by_pieces_d, providing support for block move
1114 operations. */
1116 class move_by_pieces_d : public op_by_pieces_d
1118 insn_gen_fn m_gen_fun;
1119 void generate (rtx, rtx, machine_mode);
1120 bool prepare_mode (machine_mode, unsigned int);
1122 public:
1123 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1124 unsigned int align)
1125 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1128 rtx finish_endp (int);
1131 /* Return true if MODE can be used for a set of copies, given an
1132 alignment ALIGN. Prepare whatever data is necessary for later
1133 calls to generate. */
1135 bool
1136 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1138 insn_code icode = optab_handler (mov_optab, mode);
1139 m_gen_fun = GEN_FCN (icode);
1140 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1143 /* A callback used when iterating for a compare_by_pieces_operation.
1144 OP0 and OP1 are the values that have been loaded and should be
1145 compared in MODE. If OP0 is NULL, this means we should generate a
1146 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1147 gen function that should be used to generate the mode. */
1149 void
1150 move_by_pieces_d::generate (rtx op0, rtx op1,
1151 machine_mode mode ATTRIBUTE_UNUSED)
1153 #ifdef PUSH_ROUNDING
1154 if (op0 == NULL_RTX)
1156 emit_single_push_insn (mode, op1, NULL);
1157 return;
1159 #endif
1160 emit_insn (m_gen_fun (op0, op1));
1163 /* Perform the final adjustment at the end of a string to obtain the
1164 correct return value for the block operation. If ENDP is 1 return
1165 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1166 end minus one byte ala stpcpy. */
1169 move_by_pieces_d::finish_endp (int endp)
1171 gcc_assert (!m_reverse);
1172 if (endp == 2)
1174 m_to.maybe_postinc (-1);
1175 --m_offset;
1177 return m_to.adjust (QImode, m_offset);
1180 /* Generate several move instructions to copy LEN bytes from block FROM to
1181 block TO. (These are MEM rtx's with BLKmode).
1183 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1184 used to push FROM to the stack.
1186 ALIGN is maximum stack alignment we can assume.
1188 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1189 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1190 stpcpy. */
1193 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1194 unsigned int align, int endp)
1196 #ifndef PUSH_ROUNDING
1197 if (to == NULL)
1198 gcc_unreachable ();
1199 #endif
1201 move_by_pieces_d data (to, from, len, align);
1203 data.run ();
1205 if (endp)
1206 return data.finish_endp (endp);
1207 else
1208 return to;
1211 /* Derived class from op_by_pieces_d, providing support for block move
1212 operations. */
1214 class store_by_pieces_d : public op_by_pieces_d
1216 insn_gen_fn m_gen_fun;
1217 void generate (rtx, rtx, machine_mode);
1218 bool prepare_mode (machine_mode, unsigned int);
1220 public:
1221 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1222 unsigned HOST_WIDE_INT len, unsigned int align)
1223 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1226 rtx finish_endp (int);
1229 /* Return true if MODE can be used for a set of stores, given an
1230 alignment ALIGN. Prepare whatever data is necessary for later
1231 calls to generate. */
1233 bool
1234 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1236 insn_code icode = optab_handler (mov_optab, mode);
1237 m_gen_fun = GEN_FCN (icode);
1238 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1241 /* A callback used when iterating for a store_by_pieces_operation.
1242 OP0 and OP1 are the values that have been loaded and should be
1243 compared in MODE. If OP0 is NULL, this means we should generate a
1244 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1245 gen function that should be used to generate the mode. */
1247 void
1248 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1250 emit_insn (m_gen_fun (op0, op1));
1253 /* Perform the final adjustment at the end of a string to obtain the
1254 correct return value for the block operation. If ENDP is 1 return
1255 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1256 end minus one byte ala stpcpy. */
1259 store_by_pieces_d::finish_endp (int endp)
1261 gcc_assert (!m_reverse);
1262 if (endp == 2)
1264 m_to.maybe_postinc (-1);
1265 --m_offset;
1267 return m_to.adjust (QImode, m_offset);
1270 /* Determine whether the LEN bytes generated by CONSTFUN can be
1271 stored to memory using several move instructions. CONSTFUNDATA is
1272 a pointer which will be passed as argument in every CONSTFUN call.
1273 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1274 a memset operation and false if it's a copy of a constant string.
1275 Return nonzero if a call to store_by_pieces should succeed. */
1278 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1279 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1280 void *constfundata, unsigned int align, bool memsetp)
1282 unsigned HOST_WIDE_INT l;
1283 unsigned int max_size;
1284 HOST_WIDE_INT offset = 0;
1285 machine_mode mode;
1286 enum insn_code icode;
1287 int reverse;
1288 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1289 rtx cst ATTRIBUTE_UNUSED;
1291 if (len == 0)
1292 return 1;
1294 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1295 memsetp
1296 ? SET_BY_PIECES
1297 : STORE_BY_PIECES,
1298 optimize_insn_for_speed_p ()))
1299 return 0;
1301 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1303 /* We would first store what we can in the largest integer mode, then go to
1304 successively smaller modes. */
1306 for (reverse = 0;
1307 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1308 reverse++)
1310 l = len;
1311 max_size = STORE_MAX_PIECES + 1;
1312 while (max_size > 1 && l > 0)
1314 mode = widest_int_mode_for_size (max_size);
1316 if (mode == VOIDmode)
1317 break;
1319 icode = optab_handler (mov_optab, mode);
1320 if (icode != CODE_FOR_nothing
1321 && align >= GET_MODE_ALIGNMENT (mode))
1323 unsigned int size = GET_MODE_SIZE (mode);
1325 while (l >= size)
1327 if (reverse)
1328 offset -= size;
1330 cst = (*constfun) (constfundata, offset, mode);
1331 if (!targetm.legitimate_constant_p (mode, cst))
1332 return 0;
1334 if (!reverse)
1335 offset += size;
1337 l -= size;
1341 max_size = GET_MODE_SIZE (mode);
1344 /* The code above should have handled everything. */
1345 gcc_assert (!l);
1348 return 1;
1351 /* Generate several move instructions to store LEN bytes generated by
1352 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1353 pointer which will be passed as argument in every CONSTFUN call.
1354 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1355 a memset operation and false if it's a copy of a constant string.
1356 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1357 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1358 stpcpy. */
1361 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1362 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1363 void *constfundata, unsigned int align, bool memsetp, int endp)
1365 if (len == 0)
1367 gcc_assert (endp != 2);
1368 return to;
1371 gcc_assert (targetm.use_by_pieces_infrastructure_p
1372 (len, align,
1373 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1374 optimize_insn_for_speed_p ()));
1376 store_by_pieces_d data (to, constfun, constfundata, len, align);
1377 data.run ();
1379 if (endp)
1380 return data.finish_endp (endp);
1381 else
1382 return to;
1385 /* Callback routine for clear_by_pieces.
1386 Return const0_rtx unconditionally. */
1388 static rtx
1389 clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode)
1391 return const0_rtx;
1394 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1395 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1397 static void
1398 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1400 if (len == 0)
1401 return;
1403 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1404 data.run ();
1407 /* Context used by compare_by_pieces_genfn. It stores the fail label
1408 to jump to in case of miscomparison, and for branch ratios greater than 1,
1409 it stores an accumulator and the current and maximum counts before
1410 emitting another branch. */
1412 class compare_by_pieces_d : public op_by_pieces_d
1414 rtx_code_label *m_fail_label;
1415 rtx m_accumulator;
1416 int m_count, m_batch;
1418 void generate (rtx, rtx, machine_mode);
1419 bool prepare_mode (machine_mode, unsigned int);
1420 void finish_mode (machine_mode);
1421 public:
1422 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1423 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1424 rtx_code_label *fail_label)
1425 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1427 m_fail_label = fail_label;
1431 /* A callback used when iterating for a compare_by_pieces_operation.
1432 OP0 and OP1 are the values that have been loaded and should be
1433 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1434 context structure. */
1436 void
1437 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1439 if (m_batch > 1)
1441 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1442 true, OPTAB_LIB_WIDEN);
1443 if (m_count != 0)
1444 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1445 true, OPTAB_LIB_WIDEN);
1446 m_accumulator = temp;
1448 if (++m_count < m_batch)
1449 return;
1451 m_count = 0;
1452 op0 = m_accumulator;
1453 op1 = const0_rtx;
1454 m_accumulator = NULL_RTX;
1456 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1457 m_fail_label, profile_probability::uninitialized ());
1460 /* Return true if MODE can be used for a set of moves and comparisons,
1461 given an alignment ALIGN. Prepare whatever data is necessary for
1462 later calls to generate. */
1464 bool
1465 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1467 insn_code icode = optab_handler (mov_optab, mode);
1468 if (icode == CODE_FOR_nothing
1469 || align < GET_MODE_ALIGNMENT (mode)
1470 || !can_compare_p (EQ, mode, ccp_jump))
1471 return false;
1472 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1473 if (m_batch < 0)
1474 return false;
1475 m_accumulator = NULL_RTX;
1476 m_count = 0;
1477 return true;
1480 /* Called after expanding a series of comparisons in MODE. If we have
1481 accumulated results for which we haven't emitted a branch yet, do
1482 so now. */
1484 void
1485 compare_by_pieces_d::finish_mode (machine_mode mode)
1487 if (m_accumulator != NULL_RTX)
1488 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1489 NULL_RTX, NULL, m_fail_label,
1490 profile_probability::uninitialized ());
1493 /* Generate several move instructions to compare LEN bytes from blocks
1494 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1496 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1497 used to push FROM to the stack.
1499 ALIGN is maximum stack alignment we can assume.
1501 Optionally, the caller can pass a constfn and associated data in A1_CFN
1502 and A1_CFN_DATA. describing that the second operand being compared is a
1503 known constant and how to obtain its data. */
1505 static rtx
1506 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1507 rtx target, unsigned int align,
1508 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1510 rtx_code_label *fail_label = gen_label_rtx ();
1511 rtx_code_label *end_label = gen_label_rtx ();
1513 if (target == NULL_RTX
1514 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1515 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1517 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1518 fail_label);
1520 data.run ();
1522 emit_move_insn (target, const0_rtx);
1523 emit_jump (end_label);
1524 emit_barrier ();
1525 emit_label (fail_label);
1526 emit_move_insn (target, const1_rtx);
1527 emit_label (end_label);
1529 return target;
1532 /* Emit code to move a block Y to a block X. This may be done with
1533 string-move instructions, with multiple scalar move instructions,
1534 or with a library call.
1536 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1537 SIZE is an rtx that says how long they are.
1538 ALIGN is the maximum alignment we can assume they have.
1539 METHOD describes what kind of copy this is, and what mechanisms may be used.
1540 MIN_SIZE is the minimal size of block to move
1541 MAX_SIZE is the maximal size of block to move, if it can not be represented
1542 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1544 Return the address of the new block, if memcpy is called and returns it,
1545 0 otherwise. */
1548 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1549 unsigned int expected_align, HOST_WIDE_INT expected_size,
1550 unsigned HOST_WIDE_INT min_size,
1551 unsigned HOST_WIDE_INT max_size,
1552 unsigned HOST_WIDE_INT probable_max_size)
1554 bool may_use_call;
1555 rtx retval = 0;
1556 unsigned int align;
1558 gcc_assert (size);
1559 if (CONST_INT_P (size) && INTVAL (size) == 0)
1560 return 0;
1562 switch (method)
1564 case BLOCK_OP_NORMAL:
1565 case BLOCK_OP_TAILCALL:
1566 may_use_call = true;
1567 break;
1569 case BLOCK_OP_CALL_PARM:
1570 may_use_call = block_move_libcall_safe_for_call_parm ();
1572 /* Make inhibit_defer_pop nonzero around the library call
1573 to force it to pop the arguments right away. */
1574 NO_DEFER_POP;
1575 break;
1577 case BLOCK_OP_NO_LIBCALL:
1578 may_use_call = false;
1579 break;
1581 default:
1582 gcc_unreachable ();
1585 gcc_assert (MEM_P (x) && MEM_P (y));
1586 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1587 gcc_assert (align >= BITS_PER_UNIT);
1589 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1590 block copy is more efficient for other large modes, e.g. DCmode. */
1591 x = adjust_address (x, BLKmode, 0);
1592 y = adjust_address (y, BLKmode, 0);
1594 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1595 can be incorrect is coming from __builtin_memcpy. */
1596 if (CONST_INT_P (size))
1598 x = shallow_copy_rtx (x);
1599 y = shallow_copy_rtx (y);
1600 set_mem_size (x, INTVAL (size));
1601 set_mem_size (y, INTVAL (size));
1604 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1605 move_by_pieces (x, y, INTVAL (size), align, 0);
1606 else if (emit_block_move_via_movmem (x, y, size, align,
1607 expected_align, expected_size,
1608 min_size, max_size, probable_max_size))
1610 else if (may_use_call
1611 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1612 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1614 /* Since x and y are passed to a libcall, mark the corresponding
1615 tree EXPR as addressable. */
1616 tree y_expr = MEM_EXPR (y);
1617 tree x_expr = MEM_EXPR (x);
1618 if (y_expr)
1619 mark_addressable (y_expr);
1620 if (x_expr)
1621 mark_addressable (x_expr);
1622 retval = emit_block_copy_via_libcall (x, y, size,
1623 method == BLOCK_OP_TAILCALL);
1626 else
1627 emit_block_move_via_loop (x, y, size, align);
1629 if (method == BLOCK_OP_CALL_PARM)
1630 OK_DEFER_POP;
1632 return retval;
1636 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1638 unsigned HOST_WIDE_INT max, min = 0;
1639 if (GET_CODE (size) == CONST_INT)
1640 min = max = UINTVAL (size);
1641 else
1642 max = GET_MODE_MASK (GET_MODE (size));
1643 return emit_block_move_hints (x, y, size, method, 0, -1,
1644 min, max, max);
1647 /* A subroutine of emit_block_move. Returns true if calling the
1648 block move libcall will not clobber any parameters which may have
1649 already been placed on the stack. */
1651 static bool
1652 block_move_libcall_safe_for_call_parm (void)
1654 #if defined (REG_PARM_STACK_SPACE)
1655 tree fn;
1656 #endif
1658 /* If arguments are pushed on the stack, then they're safe. */
1659 if (PUSH_ARGS)
1660 return true;
1662 /* If registers go on the stack anyway, any argument is sure to clobber
1663 an outgoing argument. */
1664 #if defined (REG_PARM_STACK_SPACE)
1665 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1666 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1667 depend on its argument. */
1668 (void) fn;
1669 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1670 && REG_PARM_STACK_SPACE (fn) != 0)
1671 return false;
1672 #endif
1674 /* If any argument goes in memory, then it might clobber an outgoing
1675 argument. */
1677 CUMULATIVE_ARGS args_so_far_v;
1678 cumulative_args_t args_so_far;
1679 tree fn, arg;
1681 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1682 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1683 args_so_far = pack_cumulative_args (&args_so_far_v);
1685 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1686 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1688 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1689 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1690 NULL_TREE, true);
1691 if (!tmp || !REG_P (tmp))
1692 return false;
1693 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1694 return false;
1695 targetm.calls.function_arg_advance (args_so_far, mode,
1696 NULL_TREE, true);
1699 return true;
1702 /* A subroutine of emit_block_move. Expand a movmem pattern;
1703 return true if successful. */
1705 static bool
1706 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1707 unsigned int expected_align, HOST_WIDE_INT expected_size,
1708 unsigned HOST_WIDE_INT min_size,
1709 unsigned HOST_WIDE_INT max_size,
1710 unsigned HOST_WIDE_INT probable_max_size)
1712 int save_volatile_ok = volatile_ok;
1713 machine_mode mode;
1715 if (expected_align < align)
1716 expected_align = align;
1717 if (expected_size != -1)
1719 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1720 expected_size = probable_max_size;
1721 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1722 expected_size = min_size;
1725 /* Since this is a move insn, we don't care about volatility. */
1726 volatile_ok = 1;
1728 /* Try the most limited insn first, because there's no point
1729 including more than one in the machine description unless
1730 the more limited one has some advantage. */
1732 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
1734 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1736 if (code != CODE_FOR_nothing
1737 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1738 here because if SIZE is less than the mode mask, as it is
1739 returned by the macro, it will definitely be less than the
1740 actual mode mask. Since SIZE is within the Pmode address
1741 space, we limit MODE to Pmode. */
1742 && ((CONST_INT_P (size)
1743 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1744 <= (GET_MODE_MASK (mode) >> 1)))
1745 || max_size <= (GET_MODE_MASK (mode) >> 1)
1746 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1748 struct expand_operand ops[9];
1749 unsigned int nops;
1751 /* ??? When called via emit_block_move_for_call, it'd be
1752 nice if there were some way to inform the backend, so
1753 that it doesn't fail the expansion because it thinks
1754 emitting the libcall would be more efficient. */
1755 nops = insn_data[(int) code].n_generator_args;
1756 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1758 create_fixed_operand (&ops[0], x);
1759 create_fixed_operand (&ops[1], y);
1760 /* The check above guarantees that this size conversion is valid. */
1761 create_convert_operand_to (&ops[2], size, mode, true);
1762 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1763 if (nops >= 6)
1765 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1766 create_integer_operand (&ops[5], expected_size);
1768 if (nops >= 8)
1770 create_integer_operand (&ops[6], min_size);
1771 /* If we can not represent the maximal size,
1772 make parameter NULL. */
1773 if ((HOST_WIDE_INT) max_size != -1)
1774 create_integer_operand (&ops[7], max_size);
1775 else
1776 create_fixed_operand (&ops[7], NULL);
1778 if (nops == 9)
1780 /* If we can not represent the maximal size,
1781 make parameter NULL. */
1782 if ((HOST_WIDE_INT) probable_max_size != -1)
1783 create_integer_operand (&ops[8], probable_max_size);
1784 else
1785 create_fixed_operand (&ops[8], NULL);
1787 if (maybe_expand_insn (code, nops, ops))
1789 volatile_ok = save_volatile_ok;
1790 return true;
1795 volatile_ok = save_volatile_ok;
1796 return false;
1799 /* A subroutine of emit_block_move. Copy the data via an explicit
1800 loop. This is used only when libcalls are forbidden. */
1801 /* ??? It'd be nice to copy in hunks larger than QImode. */
1803 static void
1804 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1805 unsigned int align ATTRIBUTE_UNUSED)
1807 rtx_code_label *cmp_label, *top_label;
1808 rtx iter, x_addr, y_addr, tmp;
1809 machine_mode x_addr_mode = get_address_mode (x);
1810 machine_mode y_addr_mode = get_address_mode (y);
1811 machine_mode iter_mode;
1813 iter_mode = GET_MODE (size);
1814 if (iter_mode == VOIDmode)
1815 iter_mode = word_mode;
1817 top_label = gen_label_rtx ();
1818 cmp_label = gen_label_rtx ();
1819 iter = gen_reg_rtx (iter_mode);
1821 emit_move_insn (iter, const0_rtx);
1823 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1824 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1825 do_pending_stack_adjust ();
1827 emit_jump (cmp_label);
1828 emit_label (top_label);
1830 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1831 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1833 if (x_addr_mode != y_addr_mode)
1834 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1835 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1837 x = change_address (x, QImode, x_addr);
1838 y = change_address (y, QImode, y_addr);
1840 emit_move_insn (x, y);
1842 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1843 true, OPTAB_LIB_WIDEN);
1844 if (tmp != iter)
1845 emit_move_insn (iter, tmp);
1847 emit_label (cmp_label);
1849 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1850 true, top_label,
1851 profile_probability::guessed_always ()
1852 .apply_scale (9, 10));
1855 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1856 TAILCALL is true if this is a tail call. */
1859 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1860 rtx size, bool tailcall)
1862 rtx dst_addr, src_addr;
1863 tree call_expr, dst_tree, src_tree, size_tree;
1864 machine_mode size_mode;
1866 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1867 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1868 dst_tree = make_tree (ptr_type_node, dst_addr);
1870 src_addr = copy_addr_to_reg (XEXP (src, 0));
1871 src_addr = convert_memory_address (ptr_mode, src_addr);
1872 src_tree = make_tree (ptr_type_node, src_addr);
1874 size_mode = TYPE_MODE (sizetype);
1875 size = convert_to_mode (size_mode, size, 1);
1876 size = copy_to_mode_reg (size_mode, size);
1877 size_tree = make_tree (sizetype, size);
1879 /* It is incorrect to use the libcall calling conventions for calls to
1880 memcpy/memmove/memcmp because they can be provided by the user. */
1881 tree fn = builtin_decl_implicit (fncode);
1882 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1883 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1885 return expand_call (call_expr, NULL_RTX, false);
1888 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1889 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1890 otherwise return null. */
1893 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1894 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1895 HOST_WIDE_INT align)
1897 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1899 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1900 target = NULL_RTX;
1902 struct expand_operand ops[5];
1903 create_output_operand (&ops[0], target, insn_mode);
1904 create_fixed_operand (&ops[1], arg1_rtx);
1905 create_fixed_operand (&ops[2], arg2_rtx);
1906 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1907 TYPE_UNSIGNED (arg3_type));
1908 create_integer_operand (&ops[4], align);
1909 if (maybe_expand_insn (icode, 5, ops))
1910 return ops[0].value;
1911 return NULL_RTX;
1914 /* Expand a block compare between X and Y with length LEN using the
1915 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1916 of the expression that was used to calculate the length. ALIGN
1917 gives the known minimum common alignment. */
1919 static rtx
1920 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1921 unsigned align)
1923 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1924 implementing memcmp because it will stop if it encounters two
1925 zero bytes. */
1926 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1928 if (icode == CODE_FOR_nothing)
1929 return NULL_RTX;
1931 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1934 /* Emit code to compare a block Y to a block X. This may be done with
1935 string-compare instructions, with multiple scalar instructions,
1936 or with a library call.
1938 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1939 they are. LEN_TYPE is the type of the expression that was used to
1940 calculate it.
1942 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1943 value of a normal memcmp call, instead we can just compare for equality.
1944 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1945 returning NULL_RTX.
1947 Optionally, the caller can pass a constfn and associated data in Y_CFN
1948 and Y_CFN_DATA. describing that the second operand being compared is a
1949 known constant and how to obtain its data.
1950 Return the result of the comparison, or NULL_RTX if we failed to
1951 perform the operation. */
1954 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1955 bool equality_only, by_pieces_constfn y_cfn,
1956 void *y_cfndata)
1958 rtx result = 0;
1960 if (CONST_INT_P (len) && INTVAL (len) == 0)
1961 return const0_rtx;
1963 gcc_assert (MEM_P (x) && MEM_P (y));
1964 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1965 gcc_assert (align >= BITS_PER_UNIT);
1967 x = adjust_address (x, BLKmode, 0);
1968 y = adjust_address (y, BLKmode, 0);
1970 if (equality_only
1971 && CONST_INT_P (len)
1972 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1973 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1974 y_cfn, y_cfndata);
1975 else
1976 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1978 return result;
1981 /* Copy all or part of a value X into registers starting at REGNO.
1982 The number of registers to be filled is NREGS. */
1984 void
1985 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1987 if (nregs == 0)
1988 return;
1990 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1991 x = validize_mem (force_const_mem (mode, x));
1993 /* See if the machine can do this with a load multiple insn. */
1994 if (targetm.have_load_multiple ())
1996 rtx_insn *last = get_last_insn ();
1997 rtx first = gen_rtx_REG (word_mode, regno);
1998 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1999 GEN_INT (nregs)))
2001 emit_insn (pat);
2002 return;
2004 else
2005 delete_insns_since (last);
2008 for (int i = 0; i < nregs; i++)
2009 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2010 operand_subword_force (x, i, mode));
2013 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2014 The number of registers to be filled is NREGS. */
2016 void
2017 move_block_from_reg (int regno, rtx x, int nregs)
2019 if (nregs == 0)
2020 return;
2022 /* See if the machine can do this with a store multiple insn. */
2023 if (targetm.have_store_multiple ())
2025 rtx_insn *last = get_last_insn ();
2026 rtx first = gen_rtx_REG (word_mode, regno);
2027 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2028 GEN_INT (nregs)))
2030 emit_insn (pat);
2031 return;
2033 else
2034 delete_insns_since (last);
2037 for (int i = 0; i < nregs; i++)
2039 rtx tem = operand_subword (x, i, 1, BLKmode);
2041 gcc_assert (tem);
2043 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2047 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2048 ORIG, where ORIG is a non-consecutive group of registers represented by
2049 a PARALLEL. The clone is identical to the original except in that the
2050 original set of registers is replaced by a new set of pseudo registers.
2051 The new set has the same modes as the original set. */
2054 gen_group_rtx (rtx orig)
2056 int i, length;
2057 rtx *tmps;
2059 gcc_assert (GET_CODE (orig) == PARALLEL);
2061 length = XVECLEN (orig, 0);
2062 tmps = XALLOCAVEC (rtx, length);
2064 /* Skip a NULL entry in first slot. */
2065 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2067 if (i)
2068 tmps[0] = 0;
2070 for (; i < length; i++)
2072 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2073 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2075 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2078 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2081 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2082 except that values are placed in TMPS[i], and must later be moved
2083 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2085 static void
2086 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2088 rtx src;
2089 int start, i;
2090 machine_mode m = GET_MODE (orig_src);
2092 gcc_assert (GET_CODE (dst) == PARALLEL);
2094 if (m != VOIDmode
2095 && !SCALAR_INT_MODE_P (m)
2096 && !MEM_P (orig_src)
2097 && GET_CODE (orig_src) != CONCAT)
2099 scalar_int_mode imode;
2100 if (int_mode_for_mode (GET_MODE (orig_src)).exists (&imode))
2102 src = gen_reg_rtx (imode);
2103 emit_move_insn (gen_lowpart (GET_MODE (orig_src), src), orig_src);
2105 else
2107 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2108 emit_move_insn (src, orig_src);
2110 emit_group_load_1 (tmps, dst, src, type, ssize);
2111 return;
2114 /* Check for a NULL entry, used to indicate that the parameter goes
2115 both on the stack and in registers. */
2116 if (XEXP (XVECEXP (dst, 0, 0), 0))
2117 start = 0;
2118 else
2119 start = 1;
2121 /* Process the pieces. */
2122 for (i = start; i < XVECLEN (dst, 0); i++)
2124 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2125 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2126 unsigned int bytelen = GET_MODE_SIZE (mode);
2127 int shift = 0;
2129 /* Handle trailing fragments that run over the size of the struct. */
2130 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2132 /* Arrange to shift the fragment to where it belongs.
2133 extract_bit_field loads to the lsb of the reg. */
2134 if (
2135 #ifdef BLOCK_REG_PADDING
2136 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2137 == (BYTES_BIG_ENDIAN ? upward : downward)
2138 #else
2139 BYTES_BIG_ENDIAN
2140 #endif
2142 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2143 bytelen = ssize - bytepos;
2144 gcc_assert (bytelen > 0);
2147 /* If we won't be loading directly from memory, protect the real source
2148 from strange tricks we might play; but make sure that the source can
2149 be loaded directly into the destination. */
2150 src = orig_src;
2151 if (!MEM_P (orig_src)
2152 && (!CONSTANT_P (orig_src)
2153 || (GET_MODE (orig_src) != mode
2154 && GET_MODE (orig_src) != VOIDmode)))
2156 if (GET_MODE (orig_src) == VOIDmode)
2157 src = gen_reg_rtx (mode);
2158 else
2159 src = gen_reg_rtx (GET_MODE (orig_src));
2161 emit_move_insn (src, orig_src);
2164 /* Optimize the access just a bit. */
2165 if (MEM_P (src)
2166 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
2167 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2168 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2169 && bytelen == GET_MODE_SIZE (mode))
2171 tmps[i] = gen_reg_rtx (mode);
2172 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2174 else if (COMPLEX_MODE_P (mode)
2175 && GET_MODE (src) == mode
2176 && bytelen == GET_MODE_SIZE (mode))
2177 /* Let emit_move_complex do the bulk of the work. */
2178 tmps[i] = src;
2179 else if (GET_CODE (src) == CONCAT)
2181 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2182 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2183 unsigned int elt = bytepos / slen0;
2184 unsigned int subpos = bytepos % slen0;
2186 if (subpos + bytelen <= slen0)
2188 /* The following assumes that the concatenated objects all
2189 have the same size. In this case, a simple calculation
2190 can be used to determine the object and the bit field
2191 to be extracted. */
2192 tmps[i] = XEXP (src, elt);
2193 if (subpos != 0
2194 || subpos + bytelen != slen0
2195 || (!CONSTANT_P (tmps[i])
2196 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2197 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2198 subpos * BITS_PER_UNIT,
2199 1, NULL_RTX, mode, mode, false,
2200 NULL);
2202 else
2204 rtx mem;
2206 gcc_assert (!bytepos);
2207 mem = assign_stack_temp (GET_MODE (src), slen);
2208 emit_move_insn (mem, src);
2209 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2210 0, 1, NULL_RTX, mode, mode, false,
2211 NULL);
2214 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2215 SIMD register, which is currently broken. While we get GCC
2216 to emit proper RTL for these cases, let's dump to memory. */
2217 else if (VECTOR_MODE_P (GET_MODE (dst))
2218 && REG_P (src))
2220 int slen = GET_MODE_SIZE (GET_MODE (src));
2221 rtx mem;
2223 mem = assign_stack_temp (GET_MODE (src), slen);
2224 emit_move_insn (mem, src);
2225 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2227 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2228 && XVECLEN (dst, 0) > 1)
2229 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2230 else if (CONSTANT_P (src))
2232 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2234 if (len == ssize)
2235 tmps[i] = src;
2236 else
2238 rtx first, second;
2240 /* TODO: const_wide_int can have sizes other than this... */
2241 gcc_assert (2 * len == ssize);
2242 split_double (src, &first, &second);
2243 if (i)
2244 tmps[i] = second;
2245 else
2246 tmps[i] = first;
2249 else if (REG_P (src) && GET_MODE (src) == mode)
2250 tmps[i] = src;
2251 else
2252 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2253 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2254 mode, mode, false, NULL);
2256 if (shift)
2257 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2258 shift, tmps[i], 0);
2262 /* Emit code to move a block SRC of type TYPE to a block DST,
2263 where DST is non-consecutive registers represented by a PARALLEL.
2264 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2265 if not known. */
2267 void
2268 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2270 rtx *tmps;
2271 int i;
2273 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2274 emit_group_load_1 (tmps, dst, src, type, ssize);
2276 /* Copy the extracted pieces into the proper (probable) hard regs. */
2277 for (i = 0; i < XVECLEN (dst, 0); i++)
2279 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2280 if (d == NULL)
2281 continue;
2282 emit_move_insn (d, tmps[i]);
2286 /* Similar, but load SRC into new pseudos in a format that looks like
2287 PARALLEL. This can later be fed to emit_group_move to get things
2288 in the right place. */
2291 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2293 rtvec vec;
2294 int i;
2296 vec = rtvec_alloc (XVECLEN (parallel, 0));
2297 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2299 /* Convert the vector to look just like the original PARALLEL, except
2300 with the computed values. */
2301 for (i = 0; i < XVECLEN (parallel, 0); i++)
2303 rtx e = XVECEXP (parallel, 0, i);
2304 rtx d = XEXP (e, 0);
2306 if (d)
2308 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2309 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2311 RTVEC_ELT (vec, i) = e;
2314 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2317 /* Emit code to move a block SRC to block DST, where SRC and DST are
2318 non-consecutive groups of registers, each represented by a PARALLEL. */
2320 void
2321 emit_group_move (rtx dst, rtx src)
2323 int i;
2325 gcc_assert (GET_CODE (src) == PARALLEL
2326 && GET_CODE (dst) == PARALLEL
2327 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2329 /* Skip first entry if NULL. */
2330 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2331 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2332 XEXP (XVECEXP (src, 0, i), 0));
2335 /* Move a group of registers represented by a PARALLEL into pseudos. */
2338 emit_group_move_into_temps (rtx src)
2340 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2341 int i;
2343 for (i = 0; i < XVECLEN (src, 0); i++)
2345 rtx e = XVECEXP (src, 0, i);
2346 rtx d = XEXP (e, 0);
2348 if (d)
2349 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2350 RTVEC_ELT (vec, i) = e;
2353 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2356 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2357 where SRC is non-consecutive registers represented by a PARALLEL.
2358 SSIZE represents the total size of block ORIG_DST, or -1 if not
2359 known. */
2361 void
2362 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2364 rtx *tmps, dst;
2365 int start, finish, i;
2366 machine_mode m = GET_MODE (orig_dst);
2368 gcc_assert (GET_CODE (src) == PARALLEL);
2370 if (!SCALAR_INT_MODE_P (m)
2371 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2373 scalar_int_mode imode;
2374 if (int_mode_for_mode (GET_MODE (orig_dst)).exists (&imode))
2376 dst = gen_reg_rtx (imode);
2377 emit_group_store (dst, src, type, ssize);
2378 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2380 else
2382 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2383 emit_group_store (dst, src, type, ssize);
2385 emit_move_insn (orig_dst, dst);
2386 return;
2389 /* Check for a NULL entry, used to indicate that the parameter goes
2390 both on the stack and in registers. */
2391 if (XEXP (XVECEXP (src, 0, 0), 0))
2392 start = 0;
2393 else
2394 start = 1;
2395 finish = XVECLEN (src, 0);
2397 tmps = XALLOCAVEC (rtx, finish);
2399 /* Copy the (probable) hard regs into pseudos. */
2400 for (i = start; i < finish; i++)
2402 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2403 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2405 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2406 emit_move_insn (tmps[i], reg);
2408 else
2409 tmps[i] = reg;
2412 /* If we won't be storing directly into memory, protect the real destination
2413 from strange tricks we might play. */
2414 dst = orig_dst;
2415 if (GET_CODE (dst) == PARALLEL)
2417 rtx temp;
2419 /* We can get a PARALLEL dst if there is a conditional expression in
2420 a return statement. In that case, the dst and src are the same,
2421 so no action is necessary. */
2422 if (rtx_equal_p (dst, src))
2423 return;
2425 /* It is unclear if we can ever reach here, but we may as well handle
2426 it. Allocate a temporary, and split this into a store/load to/from
2427 the temporary. */
2428 temp = assign_stack_temp (GET_MODE (dst), ssize);
2429 emit_group_store (temp, src, type, ssize);
2430 emit_group_load (dst, temp, type, ssize);
2431 return;
2433 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2435 machine_mode outer = GET_MODE (dst);
2436 machine_mode inner;
2437 HOST_WIDE_INT bytepos;
2438 bool done = false;
2439 rtx temp;
2441 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2442 dst = gen_reg_rtx (outer);
2444 /* Make life a bit easier for combine. */
2445 /* If the first element of the vector is the low part
2446 of the destination mode, use a paradoxical subreg to
2447 initialize the destination. */
2448 if (start < finish)
2450 inner = GET_MODE (tmps[start]);
2451 bytepos = subreg_lowpart_offset (inner, outer);
2452 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2454 temp = simplify_gen_subreg (outer, tmps[start],
2455 inner, 0);
2456 if (temp)
2458 emit_move_insn (dst, temp);
2459 done = true;
2460 start++;
2465 /* If the first element wasn't the low part, try the last. */
2466 if (!done
2467 && start < finish - 1)
2469 inner = GET_MODE (tmps[finish - 1]);
2470 bytepos = subreg_lowpart_offset (inner, outer);
2471 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2473 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2474 inner, 0);
2475 if (temp)
2477 emit_move_insn (dst, temp);
2478 done = true;
2479 finish--;
2484 /* Otherwise, simply initialize the result to zero. */
2485 if (!done)
2486 emit_move_insn (dst, CONST0_RTX (outer));
2489 /* Process the pieces. */
2490 for (i = start; i < finish; i++)
2492 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2493 machine_mode mode = GET_MODE (tmps[i]);
2494 unsigned int bytelen = GET_MODE_SIZE (mode);
2495 unsigned int adj_bytelen;
2496 rtx dest = dst;
2498 /* Handle trailing fragments that run over the size of the struct. */
2499 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2500 adj_bytelen = ssize - bytepos;
2501 else
2502 adj_bytelen = bytelen;
2504 if (GET_CODE (dst) == CONCAT)
2506 if (bytepos + adj_bytelen
2507 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2508 dest = XEXP (dst, 0);
2509 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2511 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2512 dest = XEXP (dst, 1);
2514 else
2516 machine_mode dest_mode = GET_MODE (dest);
2517 machine_mode tmp_mode = GET_MODE (tmps[i]);
2519 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2521 if (GET_MODE_ALIGNMENT (dest_mode)
2522 >= GET_MODE_ALIGNMENT (tmp_mode))
2524 dest = assign_stack_temp (dest_mode,
2525 GET_MODE_SIZE (dest_mode));
2526 emit_move_insn (adjust_address (dest,
2527 tmp_mode,
2528 bytepos),
2529 tmps[i]);
2530 dst = dest;
2532 else
2534 dest = assign_stack_temp (tmp_mode,
2535 GET_MODE_SIZE (tmp_mode));
2536 emit_move_insn (dest, tmps[i]);
2537 dst = adjust_address (dest, dest_mode, bytepos);
2539 break;
2543 /* Handle trailing fragments that run over the size of the struct. */
2544 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2546 /* store_bit_field always takes its value from the lsb.
2547 Move the fragment to the lsb if it's not already there. */
2548 if (
2549 #ifdef BLOCK_REG_PADDING
2550 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2551 == (BYTES_BIG_ENDIAN ? upward : downward)
2552 #else
2553 BYTES_BIG_ENDIAN
2554 #endif
2557 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2558 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2559 shift, tmps[i], 0);
2562 /* Make sure not to write past the end of the struct. */
2563 store_bit_field (dest,
2564 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2565 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2566 VOIDmode, tmps[i], false);
2569 /* Optimize the access just a bit. */
2570 else if (MEM_P (dest)
2571 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2572 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2573 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2574 && bytelen == GET_MODE_SIZE (mode))
2575 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2577 else
2578 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2579 0, 0, mode, tmps[i], false);
2582 /* Copy from the pseudo into the (probable) hard reg. */
2583 if (orig_dst != dst)
2584 emit_move_insn (orig_dst, dst);
2587 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2588 of the value stored in X. */
2591 maybe_emit_group_store (rtx x, tree type)
2593 machine_mode mode = TYPE_MODE (type);
2594 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2595 if (GET_CODE (x) == PARALLEL)
2597 rtx result = gen_reg_rtx (mode);
2598 emit_group_store (result, x, type, int_size_in_bytes (type));
2599 return result;
2601 return x;
2604 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2606 This is used on targets that return BLKmode values in registers. */
2608 static void
2609 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2611 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2612 rtx src = NULL, dst = NULL;
2613 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2614 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2615 machine_mode mode = GET_MODE (srcreg);
2616 machine_mode tmode = GET_MODE (target);
2617 machine_mode copy_mode;
2619 /* BLKmode registers created in the back-end shouldn't have survived. */
2620 gcc_assert (mode != BLKmode);
2622 /* If the structure doesn't take up a whole number of words, see whether
2623 SRCREG is padded on the left or on the right. If it's on the left,
2624 set PADDING_CORRECTION to the number of bits to skip.
2626 In most ABIs, the structure will be returned at the least end of
2627 the register, which translates to right padding on little-endian
2628 targets and left padding on big-endian targets. The opposite
2629 holds if the structure is returned at the most significant
2630 end of the register. */
2631 if (bytes % UNITS_PER_WORD != 0
2632 && (targetm.calls.return_in_msb (type)
2633 ? !BYTES_BIG_ENDIAN
2634 : BYTES_BIG_ENDIAN))
2635 padding_correction
2636 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2638 /* We can use a single move if we have an exact mode for the size. */
2639 else if (MEM_P (target)
2640 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2641 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2642 && bytes == GET_MODE_SIZE (mode))
2644 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2645 return;
2648 /* And if we additionally have the same mode for a register. */
2649 else if (REG_P (target)
2650 && GET_MODE (target) == mode
2651 && bytes == GET_MODE_SIZE (mode))
2653 emit_move_insn (target, srcreg);
2654 return;
2657 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2658 into a new pseudo which is a full word. */
2659 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2661 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2662 mode = word_mode;
2665 /* Copy the structure BITSIZE bits at a time. If the target lives in
2666 memory, take care of not reading/writing past its end by selecting
2667 a copy mode suited to BITSIZE. This should always be possible given
2668 how it is computed.
2670 If the target lives in register, make sure not to select a copy mode
2671 larger than the mode of the register.
2673 We could probably emit more efficient code for machines which do not use
2674 strict alignment, but it doesn't seem worth the effort at the current
2675 time. */
2677 copy_mode = word_mode;
2678 if (MEM_P (target))
2680 opt_scalar_int_mode mem_mode = int_mode_for_size (bitsize, 1);
2681 if (mem_mode.exists ())
2682 copy_mode = mem_mode.require ();
2684 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2685 copy_mode = tmode;
2687 for (bitpos = 0, xbitpos = padding_correction;
2688 bitpos < bytes * BITS_PER_UNIT;
2689 bitpos += bitsize, xbitpos += bitsize)
2691 /* We need a new source operand each time xbitpos is on a
2692 word boundary and when xbitpos == padding_correction
2693 (the first time through). */
2694 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2695 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2697 /* We need a new destination operand each time bitpos is on
2698 a word boundary. */
2699 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2700 dst = target;
2701 else if (bitpos % BITS_PER_WORD == 0)
2702 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2704 /* Use xbitpos for the source extraction (right justified) and
2705 bitpos for the destination store (left justified). */
2706 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2707 extract_bit_field (src, bitsize,
2708 xbitpos % BITS_PER_WORD, 1,
2709 NULL_RTX, copy_mode, copy_mode,
2710 false, NULL),
2711 false);
2715 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2716 register if it contains any data, otherwise return null.
2718 This is used on targets that return BLKmode values in registers. */
2721 copy_blkmode_to_reg (machine_mode mode, tree src)
2723 int i, n_regs;
2724 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2725 unsigned int bitsize;
2726 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2727 machine_mode dst_mode;
2729 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2731 x = expand_normal (src);
2733 bytes = int_size_in_bytes (TREE_TYPE (src));
2734 if (bytes == 0)
2735 return NULL_RTX;
2737 /* If the structure doesn't take up a whole number of words, see
2738 whether the register value should be padded on the left or on
2739 the right. Set PADDING_CORRECTION to the number of padding
2740 bits needed on the left side.
2742 In most ABIs, the structure will be returned at the least end of
2743 the register, which translates to right padding on little-endian
2744 targets and left padding on big-endian targets. The opposite
2745 holds if the structure is returned at the most significant
2746 end of the register. */
2747 if (bytes % UNITS_PER_WORD != 0
2748 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2749 ? !BYTES_BIG_ENDIAN
2750 : BYTES_BIG_ENDIAN))
2751 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2752 * BITS_PER_UNIT));
2754 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2755 dst_words = XALLOCAVEC (rtx, n_regs);
2756 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2758 /* Copy the structure BITSIZE bits at a time. */
2759 for (bitpos = 0, xbitpos = padding_correction;
2760 bitpos < bytes * BITS_PER_UNIT;
2761 bitpos += bitsize, xbitpos += bitsize)
2763 /* We need a new destination pseudo each time xbitpos is
2764 on a word boundary and when xbitpos == padding_correction
2765 (the first time through). */
2766 if (xbitpos % BITS_PER_WORD == 0
2767 || xbitpos == padding_correction)
2769 /* Generate an appropriate register. */
2770 dst_word = gen_reg_rtx (word_mode);
2771 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2773 /* Clear the destination before we move anything into it. */
2774 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2777 /* We need a new source operand each time bitpos is on a word
2778 boundary. */
2779 if (bitpos % BITS_PER_WORD == 0)
2780 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2782 /* Use bitpos for the source extraction (left justified) and
2783 xbitpos for the destination store (right justified). */
2784 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2785 0, 0, word_mode,
2786 extract_bit_field (src_word, bitsize,
2787 bitpos % BITS_PER_WORD, 1,
2788 NULL_RTX, word_mode, word_mode,
2789 false, NULL),
2790 false);
2793 if (mode == BLKmode)
2795 /* Find the smallest integer mode large enough to hold the
2796 entire structure. */
2797 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
2798 /* Have we found a large enough mode? */
2799 if (GET_MODE_SIZE (mode) >= bytes)
2800 break;
2802 /* A suitable mode should have been found. */
2803 gcc_assert (mode != VOIDmode);
2806 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2807 dst_mode = word_mode;
2808 else
2809 dst_mode = mode;
2810 dst = gen_reg_rtx (dst_mode);
2812 for (i = 0; i < n_regs; i++)
2813 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2815 if (mode != dst_mode)
2816 dst = gen_lowpart (mode, dst);
2818 return dst;
2821 /* Add a USE expression for REG to the (possibly empty) list pointed
2822 to by CALL_FUSAGE. REG must denote a hard register. */
2824 void
2825 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2827 gcc_assert (REG_P (reg));
2829 if (!HARD_REGISTER_P (reg))
2830 return;
2832 *call_fusage
2833 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2836 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2837 to by CALL_FUSAGE. REG must denote a hard register. */
2839 void
2840 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2842 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2844 *call_fusage
2845 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2848 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2849 starting at REGNO. All of these registers must be hard registers. */
2851 void
2852 use_regs (rtx *call_fusage, int regno, int nregs)
2854 int i;
2856 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2858 for (i = 0; i < nregs; i++)
2859 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2862 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2863 PARALLEL REGS. This is for calls that pass values in multiple
2864 non-contiguous locations. The Irix 6 ABI has examples of this. */
2866 void
2867 use_group_regs (rtx *call_fusage, rtx regs)
2869 int i;
2871 for (i = 0; i < XVECLEN (regs, 0); i++)
2873 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2875 /* A NULL entry means the parameter goes both on the stack and in
2876 registers. This can also be a MEM for targets that pass values
2877 partially on the stack and partially in registers. */
2878 if (reg != 0 && REG_P (reg))
2879 use_reg (call_fusage, reg);
2883 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2884 assigment and the code of the expresion on the RHS is CODE. Return
2885 NULL otherwise. */
2887 static gimple *
2888 get_def_for_expr (tree name, enum tree_code code)
2890 gimple *def_stmt;
2892 if (TREE_CODE (name) != SSA_NAME)
2893 return NULL;
2895 def_stmt = get_gimple_for_ssa_name (name);
2896 if (!def_stmt
2897 || gimple_assign_rhs_code (def_stmt) != code)
2898 return NULL;
2900 return def_stmt;
2903 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2904 assigment and the class of the expresion on the RHS is CLASS. Return
2905 NULL otherwise. */
2907 static gimple *
2908 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2910 gimple *def_stmt;
2912 if (TREE_CODE (name) != SSA_NAME)
2913 return NULL;
2915 def_stmt = get_gimple_for_ssa_name (name);
2916 if (!def_stmt
2917 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2918 return NULL;
2920 return def_stmt;
2923 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2924 its length in bytes. */
2927 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2928 unsigned int expected_align, HOST_WIDE_INT expected_size,
2929 unsigned HOST_WIDE_INT min_size,
2930 unsigned HOST_WIDE_INT max_size,
2931 unsigned HOST_WIDE_INT probable_max_size)
2933 machine_mode mode = GET_MODE (object);
2934 unsigned int align;
2936 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2938 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2939 just move a zero. Otherwise, do this a piece at a time. */
2940 if (mode != BLKmode
2941 && CONST_INT_P (size)
2942 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2944 rtx zero = CONST0_RTX (mode);
2945 if (zero != NULL)
2947 emit_move_insn (object, zero);
2948 return NULL;
2951 if (COMPLEX_MODE_P (mode))
2953 zero = CONST0_RTX (GET_MODE_INNER (mode));
2954 if (zero != NULL)
2956 write_complex_part (object, zero, 0);
2957 write_complex_part (object, zero, 1);
2958 return NULL;
2963 if (size == const0_rtx)
2964 return NULL;
2966 align = MEM_ALIGN (object);
2968 if (CONST_INT_P (size)
2969 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2970 CLEAR_BY_PIECES,
2971 optimize_insn_for_speed_p ()))
2972 clear_by_pieces (object, INTVAL (size), align);
2973 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2974 expected_align, expected_size,
2975 min_size, max_size, probable_max_size))
2977 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2978 return set_storage_via_libcall (object, size, const0_rtx,
2979 method == BLOCK_OP_TAILCALL);
2980 else
2981 gcc_unreachable ();
2983 return NULL;
2987 clear_storage (rtx object, rtx size, enum block_op_methods method)
2989 unsigned HOST_WIDE_INT max, min = 0;
2990 if (GET_CODE (size) == CONST_INT)
2991 min = max = UINTVAL (size);
2992 else
2993 max = GET_MODE_MASK (GET_MODE (size));
2994 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2998 /* A subroutine of clear_storage. Expand a call to memset.
2999 Return the return value of memset, 0 otherwise. */
3002 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
3004 tree call_expr, fn, object_tree, size_tree, val_tree;
3005 machine_mode size_mode;
3007 object = copy_addr_to_reg (XEXP (object, 0));
3008 object_tree = make_tree (ptr_type_node, object);
3010 if (!CONST_INT_P (val))
3011 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3012 val_tree = make_tree (integer_type_node, val);
3014 size_mode = TYPE_MODE (sizetype);
3015 size = convert_to_mode (size_mode, size, 1);
3016 size = copy_to_mode_reg (size_mode, size);
3017 size_tree = make_tree (sizetype, size);
3019 /* It is incorrect to use the libcall calling conventions for calls to
3020 memset because it can be provided by the user. */
3021 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3022 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3023 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3025 return expand_call (call_expr, NULL_RTX, false);
3028 /* Expand a setmem pattern; return true if successful. */
3030 bool
3031 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3032 unsigned int expected_align, HOST_WIDE_INT expected_size,
3033 unsigned HOST_WIDE_INT min_size,
3034 unsigned HOST_WIDE_INT max_size,
3035 unsigned HOST_WIDE_INT probable_max_size)
3037 /* Try the most limited insn first, because there's no point
3038 including more than one in the machine description unless
3039 the more limited one has some advantage. */
3041 machine_mode mode;
3043 if (expected_align < align)
3044 expected_align = align;
3045 if (expected_size != -1)
3047 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3048 expected_size = max_size;
3049 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3050 expected_size = min_size;
3053 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
3055 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3057 if (code != CODE_FOR_nothing
3058 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3059 here because if SIZE is less than the mode mask, as it is
3060 returned by the macro, it will definitely be less than the
3061 actual mode mask. Since SIZE is within the Pmode address
3062 space, we limit MODE to Pmode. */
3063 && ((CONST_INT_P (size)
3064 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3065 <= (GET_MODE_MASK (mode) >> 1)))
3066 || max_size <= (GET_MODE_MASK (mode) >> 1)
3067 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3069 struct expand_operand ops[9];
3070 unsigned int nops;
3072 nops = insn_data[(int) code].n_generator_args;
3073 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3075 create_fixed_operand (&ops[0], object);
3076 /* The check above guarantees that this size conversion is valid. */
3077 create_convert_operand_to (&ops[1], size, mode, true);
3078 create_convert_operand_from (&ops[2], val, byte_mode, true);
3079 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3080 if (nops >= 6)
3082 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3083 create_integer_operand (&ops[5], expected_size);
3085 if (nops >= 8)
3087 create_integer_operand (&ops[6], min_size);
3088 /* If we can not represent the maximal size,
3089 make parameter NULL. */
3090 if ((HOST_WIDE_INT) max_size != -1)
3091 create_integer_operand (&ops[7], max_size);
3092 else
3093 create_fixed_operand (&ops[7], NULL);
3095 if (nops == 9)
3097 /* If we can not represent the maximal size,
3098 make parameter NULL. */
3099 if ((HOST_WIDE_INT) probable_max_size != -1)
3100 create_integer_operand (&ops[8], probable_max_size);
3101 else
3102 create_fixed_operand (&ops[8], NULL);
3104 if (maybe_expand_insn (code, nops, ops))
3105 return true;
3109 return false;
3113 /* Write to one of the components of the complex value CPLX. Write VAL to
3114 the real part if IMAG_P is false, and the imaginary part if its true. */
3116 void
3117 write_complex_part (rtx cplx, rtx val, bool imag_p)
3119 machine_mode cmode;
3120 machine_mode imode;
3121 unsigned ibitsize;
3123 if (GET_CODE (cplx) == CONCAT)
3125 emit_move_insn (XEXP (cplx, imag_p), val);
3126 return;
3129 cmode = GET_MODE (cplx);
3130 imode = GET_MODE_INNER (cmode);
3131 ibitsize = GET_MODE_BITSIZE (imode);
3133 /* For MEMs simplify_gen_subreg may generate an invalid new address
3134 because, e.g., the original address is considered mode-dependent
3135 by the target, which restricts simplify_subreg from invoking
3136 adjust_address_nv. Instead of preparing fallback support for an
3137 invalid address, we call adjust_address_nv directly. */
3138 if (MEM_P (cplx))
3140 emit_move_insn (adjust_address_nv (cplx, imode,
3141 imag_p ? GET_MODE_SIZE (imode) : 0),
3142 val);
3143 return;
3146 /* If the sub-object is at least word sized, then we know that subregging
3147 will work. This special case is important, since store_bit_field
3148 wants to operate on integer modes, and there's rarely an OImode to
3149 correspond to TCmode. */
3150 if (ibitsize >= BITS_PER_WORD
3151 /* For hard regs we have exact predicates. Assume we can split
3152 the original object if it spans an even number of hard regs.
3153 This special case is important for SCmode on 64-bit platforms
3154 where the natural size of floating-point regs is 32-bit. */
3155 || (REG_P (cplx)
3156 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3157 && REG_NREGS (cplx) % 2 == 0))
3159 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3160 imag_p ? GET_MODE_SIZE (imode) : 0);
3161 if (part)
3163 emit_move_insn (part, val);
3164 return;
3166 else
3167 /* simplify_gen_subreg may fail for sub-word MEMs. */
3168 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3171 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3172 false);
3175 /* Extract one of the components of the complex value CPLX. Extract the
3176 real part if IMAG_P is false, and the imaginary part if it's true. */
3179 read_complex_part (rtx cplx, bool imag_p)
3181 machine_mode cmode, imode;
3182 unsigned ibitsize;
3184 if (GET_CODE (cplx) == CONCAT)
3185 return XEXP (cplx, imag_p);
3187 cmode = GET_MODE (cplx);
3188 imode = GET_MODE_INNER (cmode);
3189 ibitsize = GET_MODE_BITSIZE (imode);
3191 /* Special case reads from complex constants that got spilled to memory. */
3192 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3194 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3195 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3197 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3198 if (CONSTANT_CLASS_P (part))
3199 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3203 /* For MEMs simplify_gen_subreg may generate an invalid new address
3204 because, e.g., the original address is considered mode-dependent
3205 by the target, which restricts simplify_subreg from invoking
3206 adjust_address_nv. Instead of preparing fallback support for an
3207 invalid address, we call adjust_address_nv directly. */
3208 if (MEM_P (cplx))
3209 return adjust_address_nv (cplx, imode,
3210 imag_p ? GET_MODE_SIZE (imode) : 0);
3212 /* If the sub-object is at least word sized, then we know that subregging
3213 will work. This special case is important, since extract_bit_field
3214 wants to operate on integer modes, and there's rarely an OImode to
3215 correspond to TCmode. */
3216 if (ibitsize >= BITS_PER_WORD
3217 /* For hard regs we have exact predicates. Assume we can split
3218 the original object if it spans an even number of hard regs.
3219 This special case is important for SCmode on 64-bit platforms
3220 where the natural size of floating-point regs is 32-bit. */
3221 || (REG_P (cplx)
3222 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3223 && REG_NREGS (cplx) % 2 == 0))
3225 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3226 imag_p ? GET_MODE_SIZE (imode) : 0);
3227 if (ret)
3228 return ret;
3229 else
3230 /* simplify_gen_subreg may fail for sub-word MEMs. */
3231 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3234 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3235 true, NULL_RTX, imode, imode, false, NULL);
3238 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3239 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3240 represented in NEW_MODE. If FORCE is true, this will never happen, as
3241 we'll force-create a SUBREG if needed. */
3243 static rtx
3244 emit_move_change_mode (machine_mode new_mode,
3245 machine_mode old_mode, rtx x, bool force)
3247 rtx ret;
3249 if (push_operand (x, GET_MODE (x)))
3251 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3252 MEM_COPY_ATTRIBUTES (ret, x);
3254 else if (MEM_P (x))
3256 /* We don't have to worry about changing the address since the
3257 size in bytes is supposed to be the same. */
3258 if (reload_in_progress)
3260 /* Copy the MEM to change the mode and move any
3261 substitutions from the old MEM to the new one. */
3262 ret = adjust_address_nv (x, new_mode, 0);
3263 copy_replacements (x, ret);
3265 else
3266 ret = adjust_address (x, new_mode, 0);
3268 else
3270 /* Note that we do want simplify_subreg's behavior of validating
3271 that the new mode is ok for a hard register. If we were to use
3272 simplify_gen_subreg, we would create the subreg, but would
3273 probably run into the target not being able to implement it. */
3274 /* Except, of course, when FORCE is true, when this is exactly what
3275 we want. Which is needed for CCmodes on some targets. */
3276 if (force)
3277 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3278 else
3279 ret = simplify_subreg (new_mode, x, old_mode, 0);
3282 return ret;
3285 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3286 an integer mode of the same size as MODE. Returns the instruction
3287 emitted, or NULL if such a move could not be generated. */
3289 static rtx_insn *
3290 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3292 scalar_int_mode imode;
3293 enum insn_code code;
3295 /* There must exist a mode of the exact size we require. */
3296 if (!int_mode_for_mode (mode).exists (&imode))
3297 return NULL;
3299 /* The target must support moves in this mode. */
3300 code = optab_handler (mov_optab, imode);
3301 if (code == CODE_FOR_nothing)
3302 return NULL;
3304 x = emit_move_change_mode (imode, mode, x, force);
3305 if (x == NULL_RTX)
3306 return NULL;
3307 y = emit_move_change_mode (imode, mode, y, force);
3308 if (y == NULL_RTX)
3309 return NULL;
3310 return emit_insn (GEN_FCN (code) (x, y));
3313 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3314 Return an equivalent MEM that does not use an auto-increment. */
3317 emit_move_resolve_push (machine_mode mode, rtx x)
3319 enum rtx_code code = GET_CODE (XEXP (x, 0));
3320 HOST_WIDE_INT adjust;
3321 rtx temp;
3323 adjust = GET_MODE_SIZE (mode);
3324 #ifdef PUSH_ROUNDING
3325 adjust = PUSH_ROUNDING (adjust);
3326 #endif
3327 if (code == PRE_DEC || code == POST_DEC)
3328 adjust = -adjust;
3329 else if (code == PRE_MODIFY || code == POST_MODIFY)
3331 rtx expr = XEXP (XEXP (x, 0), 1);
3332 HOST_WIDE_INT val;
3334 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3335 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3336 val = INTVAL (XEXP (expr, 1));
3337 if (GET_CODE (expr) == MINUS)
3338 val = -val;
3339 gcc_assert (adjust == val || adjust == -val);
3340 adjust = val;
3343 /* Do not use anti_adjust_stack, since we don't want to update
3344 stack_pointer_delta. */
3345 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3346 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3347 0, OPTAB_LIB_WIDEN);
3348 if (temp != stack_pointer_rtx)
3349 emit_move_insn (stack_pointer_rtx, temp);
3351 switch (code)
3353 case PRE_INC:
3354 case PRE_DEC:
3355 case PRE_MODIFY:
3356 temp = stack_pointer_rtx;
3357 break;
3358 case POST_INC:
3359 case POST_DEC:
3360 case POST_MODIFY:
3361 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3362 break;
3363 default:
3364 gcc_unreachable ();
3367 return replace_equiv_address (x, temp);
3370 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3371 X is known to satisfy push_operand, and MODE is known to be complex.
3372 Returns the last instruction emitted. */
3374 rtx_insn *
3375 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3377 machine_mode submode = GET_MODE_INNER (mode);
3378 bool imag_first;
3380 #ifdef PUSH_ROUNDING
3381 unsigned int submodesize = GET_MODE_SIZE (submode);
3383 /* In case we output to the stack, but the size is smaller than the
3384 machine can push exactly, we need to use move instructions. */
3385 if (PUSH_ROUNDING (submodesize) != submodesize)
3387 x = emit_move_resolve_push (mode, x);
3388 return emit_move_insn (x, y);
3390 #endif
3392 /* Note that the real part always precedes the imag part in memory
3393 regardless of machine's endianness. */
3394 switch (GET_CODE (XEXP (x, 0)))
3396 case PRE_DEC:
3397 case POST_DEC:
3398 imag_first = true;
3399 break;
3400 case PRE_INC:
3401 case POST_INC:
3402 imag_first = false;
3403 break;
3404 default:
3405 gcc_unreachable ();
3408 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3409 read_complex_part (y, imag_first));
3410 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3411 read_complex_part (y, !imag_first));
3414 /* A subroutine of emit_move_complex. Perform the move from Y to X
3415 via two moves of the parts. Returns the last instruction emitted. */
3417 rtx_insn *
3418 emit_move_complex_parts (rtx x, rtx y)
3420 /* Show the output dies here. This is necessary for SUBREGs
3421 of pseudos since we cannot track their lifetimes correctly;
3422 hard regs shouldn't appear here except as return values. */
3423 if (!reload_completed && !reload_in_progress
3424 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3425 emit_clobber (x);
3427 write_complex_part (x, read_complex_part (y, false), false);
3428 write_complex_part (x, read_complex_part (y, true), true);
3430 return get_last_insn ();
3433 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3434 MODE is known to be complex. Returns the last instruction emitted. */
3436 static rtx_insn *
3437 emit_move_complex (machine_mode mode, rtx x, rtx y)
3439 bool try_int;
3441 /* Need to take special care for pushes, to maintain proper ordering
3442 of the data, and possibly extra padding. */
3443 if (push_operand (x, mode))
3444 return emit_move_complex_push (mode, x, y);
3446 /* See if we can coerce the target into moving both values at once, except
3447 for floating point where we favor moving as parts if this is easy. */
3448 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3449 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3450 && !(REG_P (x)
3451 && HARD_REGISTER_P (x)
3452 && REG_NREGS (x) == 1)
3453 && !(REG_P (y)
3454 && HARD_REGISTER_P (y)
3455 && REG_NREGS (y) == 1))
3456 try_int = false;
3457 /* Not possible if the values are inherently not adjacent. */
3458 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3459 try_int = false;
3460 /* Is possible if both are registers (or subregs of registers). */
3461 else if (register_operand (x, mode) && register_operand (y, mode))
3462 try_int = true;
3463 /* If one of the operands is a memory, and alignment constraints
3464 are friendly enough, we may be able to do combined memory operations.
3465 We do not attempt this if Y is a constant because that combination is
3466 usually better with the by-parts thing below. */
3467 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3468 && (!STRICT_ALIGNMENT
3469 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3470 try_int = true;
3471 else
3472 try_int = false;
3474 if (try_int)
3476 rtx_insn *ret;
3478 /* For memory to memory moves, optimal behavior can be had with the
3479 existing block move logic. */
3480 if (MEM_P (x) && MEM_P (y))
3482 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3483 BLOCK_OP_NO_LIBCALL);
3484 return get_last_insn ();
3487 ret = emit_move_via_integer (mode, x, y, true);
3488 if (ret)
3489 return ret;
3492 return emit_move_complex_parts (x, y);
3495 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3496 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3498 static rtx_insn *
3499 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3501 rtx_insn *ret;
3503 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3504 if (mode != CCmode)
3506 enum insn_code code = optab_handler (mov_optab, CCmode);
3507 if (code != CODE_FOR_nothing)
3509 x = emit_move_change_mode (CCmode, mode, x, true);
3510 y = emit_move_change_mode (CCmode, mode, y, true);
3511 return emit_insn (GEN_FCN (code) (x, y));
3515 /* Otherwise, find the MODE_INT mode of the same width. */
3516 ret = emit_move_via_integer (mode, x, y, false);
3517 gcc_assert (ret != NULL);
3518 return ret;
3521 /* Return true if word I of OP lies entirely in the
3522 undefined bits of a paradoxical subreg. */
3524 static bool
3525 undefined_operand_subword_p (const_rtx op, int i)
3527 machine_mode innermode, innermostmode;
3528 int offset;
3529 if (GET_CODE (op) != SUBREG)
3530 return false;
3531 innermode = GET_MODE (op);
3532 innermostmode = GET_MODE (SUBREG_REG (op));
3533 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3534 /* The SUBREG_BYTE represents offset, as if the value were stored in
3535 memory, except for a paradoxical subreg where we define
3536 SUBREG_BYTE to be 0; undo this exception as in
3537 simplify_subreg. */
3538 if (SUBREG_BYTE (op) == 0
3539 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3541 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3542 if (WORDS_BIG_ENDIAN)
3543 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3544 if (BYTES_BIG_ENDIAN)
3545 offset += difference % UNITS_PER_WORD;
3547 if (offset >= GET_MODE_SIZE (innermostmode)
3548 || offset <= -GET_MODE_SIZE (word_mode))
3549 return true;
3550 return false;
3553 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3554 MODE is any multi-word or full-word mode that lacks a move_insn
3555 pattern. Note that you will get better code if you define such
3556 patterns, even if they must turn into multiple assembler instructions. */
3558 static rtx_insn *
3559 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3561 rtx_insn *last_insn = 0;
3562 rtx_insn *seq;
3563 rtx inner;
3564 bool need_clobber;
3565 int i;
3567 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3569 /* If X is a push on the stack, do the push now and replace
3570 X with a reference to the stack pointer. */
3571 if (push_operand (x, mode))
3572 x = emit_move_resolve_push (mode, x);
3574 /* If we are in reload, see if either operand is a MEM whose address
3575 is scheduled for replacement. */
3576 if (reload_in_progress && MEM_P (x)
3577 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3578 x = replace_equiv_address_nv (x, inner);
3579 if (reload_in_progress && MEM_P (y)
3580 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3581 y = replace_equiv_address_nv (y, inner);
3583 start_sequence ();
3585 need_clobber = false;
3586 for (i = 0;
3587 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3588 i++)
3590 rtx xpart = operand_subword (x, i, 1, mode);
3591 rtx ypart;
3593 /* Do not generate code for a move if it would come entirely
3594 from the undefined bits of a paradoxical subreg. */
3595 if (undefined_operand_subword_p (y, i))
3596 continue;
3598 ypart = operand_subword (y, i, 1, mode);
3600 /* If we can't get a part of Y, put Y into memory if it is a
3601 constant. Otherwise, force it into a register. Then we must
3602 be able to get a part of Y. */
3603 if (ypart == 0 && CONSTANT_P (y))
3605 y = use_anchored_address (force_const_mem (mode, y));
3606 ypart = operand_subword (y, i, 1, mode);
3608 else if (ypart == 0)
3609 ypart = operand_subword_force (y, i, mode);
3611 gcc_assert (xpart && ypart);
3613 need_clobber |= (GET_CODE (xpart) == SUBREG);
3615 last_insn = emit_move_insn (xpart, ypart);
3618 seq = get_insns ();
3619 end_sequence ();
3621 /* Show the output dies here. This is necessary for SUBREGs
3622 of pseudos since we cannot track their lifetimes correctly;
3623 hard regs shouldn't appear here except as return values.
3624 We never want to emit such a clobber after reload. */
3625 if (x != y
3626 && ! (reload_in_progress || reload_completed)
3627 && need_clobber != 0)
3628 emit_clobber (x);
3630 emit_insn (seq);
3632 return last_insn;
3635 /* Low level part of emit_move_insn.
3636 Called just like emit_move_insn, but assumes X and Y
3637 are basically valid. */
3639 rtx_insn *
3640 emit_move_insn_1 (rtx x, rtx y)
3642 machine_mode mode = GET_MODE (x);
3643 enum insn_code code;
3645 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3647 code = optab_handler (mov_optab, mode);
3648 if (code != CODE_FOR_nothing)
3649 return emit_insn (GEN_FCN (code) (x, y));
3651 /* Expand complex moves by moving real part and imag part. */
3652 if (COMPLEX_MODE_P (mode))
3653 return emit_move_complex (mode, x, y);
3655 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3656 || ALL_FIXED_POINT_MODE_P (mode))
3658 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3660 /* If we can't find an integer mode, use multi words. */
3661 if (result)
3662 return result;
3663 else
3664 return emit_move_multi_word (mode, x, y);
3667 if (GET_MODE_CLASS (mode) == MODE_CC)
3668 return emit_move_ccmode (mode, x, y);
3670 /* Try using a move pattern for the corresponding integer mode. This is
3671 only safe when simplify_subreg can convert MODE constants into integer
3672 constants. At present, it can only do this reliably if the value
3673 fits within a HOST_WIDE_INT. */
3674 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3676 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3678 if (ret)
3680 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3681 return ret;
3685 return emit_move_multi_word (mode, x, y);
3688 /* Generate code to copy Y into X.
3689 Both Y and X must have the same mode, except that
3690 Y can be a constant with VOIDmode.
3691 This mode cannot be BLKmode; use emit_block_move for that.
3693 Return the last instruction emitted. */
3695 rtx_insn *
3696 emit_move_insn (rtx x, rtx y)
3698 machine_mode mode = GET_MODE (x);
3699 rtx y_cst = NULL_RTX;
3700 rtx_insn *last_insn;
3701 rtx set;
3703 gcc_assert (mode != BLKmode
3704 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3706 if (CONSTANT_P (y))
3708 if (optimize
3709 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3710 && (last_insn = compress_float_constant (x, y)))
3711 return last_insn;
3713 y_cst = y;
3715 if (!targetm.legitimate_constant_p (mode, y))
3717 y = force_const_mem (mode, y);
3719 /* If the target's cannot_force_const_mem prevented the spill,
3720 assume that the target's move expanders will also take care
3721 of the non-legitimate constant. */
3722 if (!y)
3723 y = y_cst;
3724 else
3725 y = use_anchored_address (y);
3729 /* If X or Y are memory references, verify that their addresses are valid
3730 for the machine. */
3731 if (MEM_P (x)
3732 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3733 MEM_ADDR_SPACE (x))
3734 && ! push_operand (x, GET_MODE (x))))
3735 x = validize_mem (x);
3737 if (MEM_P (y)
3738 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3739 MEM_ADDR_SPACE (y)))
3740 y = validize_mem (y);
3742 gcc_assert (mode != BLKmode);
3744 last_insn = emit_move_insn_1 (x, y);
3746 if (y_cst && REG_P (x)
3747 && (set = single_set (last_insn)) != NULL_RTX
3748 && SET_DEST (set) == x
3749 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3750 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3752 return last_insn;
3755 /* Generate the body of an instruction to copy Y into X.
3756 It may be a list of insns, if one insn isn't enough. */
3758 rtx_insn *
3759 gen_move_insn (rtx x, rtx y)
3761 rtx_insn *seq;
3763 start_sequence ();
3764 emit_move_insn_1 (x, y);
3765 seq = get_insns ();
3766 end_sequence ();
3767 return seq;
3770 /* If Y is representable exactly in a narrower mode, and the target can
3771 perform the extension directly from constant or memory, then emit the
3772 move as an extension. */
3774 static rtx_insn *
3775 compress_float_constant (rtx x, rtx y)
3777 machine_mode dstmode = GET_MODE (x);
3778 machine_mode orig_srcmode = GET_MODE (y);
3779 machine_mode srcmode;
3780 const REAL_VALUE_TYPE *r;
3781 int oldcost, newcost;
3782 bool speed = optimize_insn_for_speed_p ();
3784 r = CONST_DOUBLE_REAL_VALUE (y);
3786 if (targetm.legitimate_constant_p (dstmode, y))
3787 oldcost = set_src_cost (y, orig_srcmode, speed);
3788 else
3789 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3791 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3793 enum insn_code ic;
3794 rtx trunc_y;
3795 rtx_insn *last_insn;
3797 /* Skip if the target can't extend this way. */
3798 ic = can_extend_p (dstmode, srcmode, 0);
3799 if (ic == CODE_FOR_nothing)
3800 continue;
3802 /* Skip if the narrowed value isn't exact. */
3803 if (! exact_real_truncate (srcmode, r))
3804 continue;
3806 trunc_y = const_double_from_real_value (*r, srcmode);
3808 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3810 /* Skip if the target needs extra instructions to perform
3811 the extension. */
3812 if (!insn_operand_matches (ic, 1, trunc_y))
3813 continue;
3814 /* This is valid, but may not be cheaper than the original. */
3815 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3816 dstmode, speed);
3817 if (oldcost < newcost)
3818 continue;
3820 else if (float_extend_from_mem[dstmode][srcmode])
3822 trunc_y = force_const_mem (srcmode, trunc_y);
3823 /* This is valid, but may not be cheaper than the original. */
3824 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3825 dstmode, speed);
3826 if (oldcost < newcost)
3827 continue;
3828 trunc_y = validize_mem (trunc_y);
3830 else
3831 continue;
3833 /* For CSE's benefit, force the compressed constant pool entry
3834 into a new pseudo. This constant may be used in different modes,
3835 and if not, combine will put things back together for us. */
3836 trunc_y = force_reg (srcmode, trunc_y);
3838 /* If x is a hard register, perform the extension into a pseudo,
3839 so that e.g. stack realignment code is aware of it. */
3840 rtx target = x;
3841 if (REG_P (x) && HARD_REGISTER_P (x))
3842 target = gen_reg_rtx (dstmode);
3844 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3845 last_insn = get_last_insn ();
3847 if (REG_P (target))
3848 set_unique_reg_note (last_insn, REG_EQUAL, y);
3850 if (target != x)
3851 return emit_move_insn (x, target);
3852 return last_insn;
3855 return NULL;
3858 /* Pushing data onto the stack. */
3860 /* Push a block of length SIZE (perhaps variable)
3861 and return an rtx to address the beginning of the block.
3862 The value may be virtual_outgoing_args_rtx.
3864 EXTRA is the number of bytes of padding to push in addition to SIZE.
3865 BELOW nonzero means this padding comes at low addresses;
3866 otherwise, the padding comes at high addresses. */
3869 push_block (rtx size, int extra, int below)
3871 rtx temp;
3873 size = convert_modes (Pmode, ptr_mode, size, 1);
3874 if (CONSTANT_P (size))
3875 anti_adjust_stack (plus_constant (Pmode, size, extra));
3876 else if (REG_P (size) && extra == 0)
3877 anti_adjust_stack (size);
3878 else
3880 temp = copy_to_mode_reg (Pmode, size);
3881 if (extra != 0)
3882 temp = expand_binop (Pmode, add_optab, temp,
3883 gen_int_mode (extra, Pmode),
3884 temp, 0, OPTAB_LIB_WIDEN);
3885 anti_adjust_stack (temp);
3888 if (STACK_GROWS_DOWNWARD)
3890 temp = virtual_outgoing_args_rtx;
3891 if (extra != 0 && below)
3892 temp = plus_constant (Pmode, temp, extra);
3894 else
3896 if (CONST_INT_P (size))
3897 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3898 -INTVAL (size) - (below ? 0 : extra));
3899 else if (extra != 0 && !below)
3900 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3901 negate_rtx (Pmode, plus_constant (Pmode, size,
3902 extra)));
3903 else
3904 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3905 negate_rtx (Pmode, size));
3908 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3911 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3913 static rtx
3914 mem_autoinc_base (rtx mem)
3916 if (MEM_P (mem))
3918 rtx addr = XEXP (mem, 0);
3919 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3920 return XEXP (addr, 0);
3922 return NULL;
3925 /* A utility routine used here, in reload, and in try_split. The insns
3926 after PREV up to and including LAST are known to adjust the stack,
3927 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3928 placing notes as appropriate. PREV may be NULL, indicating the
3929 entire insn sequence prior to LAST should be scanned.
3931 The set of allowed stack pointer modifications is small:
3932 (1) One or more auto-inc style memory references (aka pushes),
3933 (2) One or more addition/subtraction with the SP as destination,
3934 (3) A single move insn with the SP as destination,
3935 (4) A call_pop insn,
3936 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3938 Insns in the sequence that do not modify the SP are ignored,
3939 except for noreturn calls.
3941 The return value is the amount of adjustment that can be trivially
3942 verified, via immediate operand or auto-inc. If the adjustment
3943 cannot be trivially extracted, the return value is INT_MIN. */
3945 HOST_WIDE_INT
3946 find_args_size_adjust (rtx_insn *insn)
3948 rtx dest, set, pat;
3949 int i;
3951 pat = PATTERN (insn);
3952 set = NULL;
3954 /* Look for a call_pop pattern. */
3955 if (CALL_P (insn))
3957 /* We have to allow non-call_pop patterns for the case
3958 of emit_single_push_insn of a TLS address. */
3959 if (GET_CODE (pat) != PARALLEL)
3960 return 0;
3962 /* All call_pop have a stack pointer adjust in the parallel.
3963 The call itself is always first, and the stack adjust is
3964 usually last, so search from the end. */
3965 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3967 set = XVECEXP (pat, 0, i);
3968 if (GET_CODE (set) != SET)
3969 continue;
3970 dest = SET_DEST (set);
3971 if (dest == stack_pointer_rtx)
3972 break;
3974 /* We'd better have found the stack pointer adjust. */
3975 if (i == 0)
3976 return 0;
3977 /* Fall through to process the extracted SET and DEST
3978 as if it was a standalone insn. */
3980 else if (GET_CODE (pat) == SET)
3981 set = pat;
3982 else if ((set = single_set (insn)) != NULL)
3984 else if (GET_CODE (pat) == PARALLEL)
3986 /* ??? Some older ports use a parallel with a stack adjust
3987 and a store for a PUSH_ROUNDING pattern, rather than a
3988 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3989 /* ??? See h8300 and m68k, pushqi1. */
3990 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3992 set = XVECEXP (pat, 0, i);
3993 if (GET_CODE (set) != SET)
3994 continue;
3995 dest = SET_DEST (set);
3996 if (dest == stack_pointer_rtx)
3997 break;
3999 /* We do not expect an auto-inc of the sp in the parallel. */
4000 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
4001 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4002 != stack_pointer_rtx);
4004 if (i < 0)
4005 return 0;
4007 else
4008 return 0;
4010 dest = SET_DEST (set);
4012 /* Look for direct modifications of the stack pointer. */
4013 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4015 /* Look for a trivial adjustment, otherwise assume nothing. */
4016 /* Note that the SPU restore_stack_block pattern refers to
4017 the stack pointer in V4SImode. Consider that non-trivial. */
4018 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4019 && GET_CODE (SET_SRC (set)) == PLUS
4020 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4021 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4022 return INTVAL (XEXP (SET_SRC (set), 1));
4023 /* ??? Reload can generate no-op moves, which will be cleaned
4024 up later. Recognize it and continue searching. */
4025 else if (rtx_equal_p (dest, SET_SRC (set)))
4026 return 0;
4027 else
4028 return HOST_WIDE_INT_MIN;
4030 else
4032 rtx mem, addr;
4034 /* Otherwise only think about autoinc patterns. */
4035 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4037 mem = dest;
4038 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4039 != stack_pointer_rtx);
4041 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4042 mem = SET_SRC (set);
4043 else
4044 return 0;
4046 addr = XEXP (mem, 0);
4047 switch (GET_CODE (addr))
4049 case PRE_INC:
4050 case POST_INC:
4051 return GET_MODE_SIZE (GET_MODE (mem));
4052 case PRE_DEC:
4053 case POST_DEC:
4054 return -GET_MODE_SIZE (GET_MODE (mem));
4055 case PRE_MODIFY:
4056 case POST_MODIFY:
4057 addr = XEXP (addr, 1);
4058 gcc_assert (GET_CODE (addr) == PLUS);
4059 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4060 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4061 return INTVAL (XEXP (addr, 1));
4062 default:
4063 gcc_unreachable ();
4069 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4071 int args_size = end_args_size;
4072 bool saw_unknown = false;
4073 rtx_insn *insn;
4075 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4077 HOST_WIDE_INT this_delta;
4079 if (!NONDEBUG_INSN_P (insn))
4080 continue;
4082 this_delta = find_args_size_adjust (insn);
4083 if (this_delta == 0)
4085 if (!CALL_P (insn)
4086 || ACCUMULATE_OUTGOING_ARGS
4087 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4088 continue;
4091 gcc_assert (!saw_unknown);
4092 if (this_delta == HOST_WIDE_INT_MIN)
4093 saw_unknown = true;
4095 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4096 if (STACK_GROWS_DOWNWARD)
4097 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4099 args_size -= this_delta;
4102 return saw_unknown ? INT_MIN : args_size;
4105 #ifdef PUSH_ROUNDING
4106 /* Emit single push insn. */
4108 static void
4109 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4111 rtx dest_addr;
4112 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4113 rtx dest;
4114 enum insn_code icode;
4116 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4117 /* If there is push pattern, use it. Otherwise try old way of throwing
4118 MEM representing push operation to move expander. */
4119 icode = optab_handler (push_optab, mode);
4120 if (icode != CODE_FOR_nothing)
4122 struct expand_operand ops[1];
4124 create_input_operand (&ops[0], x, mode);
4125 if (maybe_expand_insn (icode, 1, ops))
4126 return;
4128 if (GET_MODE_SIZE (mode) == rounded_size)
4129 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4130 /* If we are to pad downward, adjust the stack pointer first and
4131 then store X into the stack location using an offset. This is
4132 because emit_move_insn does not know how to pad; it does not have
4133 access to type. */
4134 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4136 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4137 HOST_WIDE_INT offset;
4139 emit_move_insn (stack_pointer_rtx,
4140 expand_binop (Pmode,
4141 STACK_GROWS_DOWNWARD ? sub_optab
4142 : add_optab,
4143 stack_pointer_rtx,
4144 gen_int_mode (rounded_size, Pmode),
4145 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4147 offset = (HOST_WIDE_INT) padding_size;
4148 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4149 /* We have already decremented the stack pointer, so get the
4150 previous value. */
4151 offset += (HOST_WIDE_INT) rounded_size;
4153 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4154 /* We have already incremented the stack pointer, so get the
4155 previous value. */
4156 offset -= (HOST_WIDE_INT) rounded_size;
4158 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4159 gen_int_mode (offset, Pmode));
4161 else
4163 if (STACK_GROWS_DOWNWARD)
4164 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4165 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4166 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4167 Pmode));
4168 else
4169 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4170 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4171 gen_int_mode (rounded_size, Pmode));
4173 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4176 dest = gen_rtx_MEM (mode, dest_addr);
4178 if (type != 0)
4180 set_mem_attributes (dest, type, 1);
4182 if (cfun->tail_call_marked)
4183 /* Function incoming arguments may overlap with sibling call
4184 outgoing arguments and we cannot allow reordering of reads
4185 from function arguments with stores to outgoing arguments
4186 of sibling calls. */
4187 set_mem_alias_set (dest, 0);
4189 emit_move_insn (dest, x);
4192 /* Emit and annotate a single push insn. */
4194 static void
4195 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4197 int delta, old_delta = stack_pointer_delta;
4198 rtx_insn *prev = get_last_insn ();
4199 rtx_insn *last;
4201 emit_single_push_insn_1 (mode, x, type);
4203 last = get_last_insn ();
4205 /* Notice the common case where we emitted exactly one insn. */
4206 if (PREV_INSN (last) == prev)
4208 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4209 return;
4212 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4213 gcc_assert (delta == INT_MIN || delta == old_delta);
4215 #endif
4217 /* If reading SIZE bytes from X will end up reading from
4218 Y return the number of bytes that overlap. Return -1
4219 if there is no overlap or -2 if we can't determine
4220 (for example when X and Y have different base registers). */
4222 static int
4223 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4225 rtx tmp = plus_constant (Pmode, x, size);
4226 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4228 if (!CONST_INT_P (sub))
4229 return -2;
4231 HOST_WIDE_INT val = INTVAL (sub);
4233 return IN_RANGE (val, 1, size) ? val : -1;
4236 /* Generate code to push X onto the stack, assuming it has mode MODE and
4237 type TYPE.
4238 MODE is redundant except when X is a CONST_INT (since they don't
4239 carry mode info).
4240 SIZE is an rtx for the size of data to be copied (in bytes),
4241 needed only if X is BLKmode.
4242 Return true if successful. May return false if asked to push a
4243 partial argument during a sibcall optimization (as specified by
4244 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4245 to not overlap.
4247 ALIGN (in bits) is maximum alignment we can assume.
4249 If PARTIAL and REG are both nonzero, then copy that many of the first
4250 bytes of X into registers starting with REG, and push the rest of X.
4251 The amount of space pushed is decreased by PARTIAL bytes.
4252 REG must be a hard register in this case.
4253 If REG is zero but PARTIAL is not, take any all others actions for an
4254 argument partially in registers, but do not actually load any
4255 registers.
4257 EXTRA is the amount in bytes of extra space to leave next to this arg.
4258 This is ignored if an argument block has already been allocated.
4260 On a machine that lacks real push insns, ARGS_ADDR is the address of
4261 the bottom of the argument block for this call. We use indexing off there
4262 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4263 argument block has not been preallocated.
4265 ARGS_SO_FAR is the size of args previously pushed for this call.
4267 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4268 for arguments passed in registers. If nonzero, it will be the number
4269 of bytes required. */
4271 bool
4272 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4273 unsigned int align, int partial, rtx reg, int extra,
4274 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4275 rtx alignment_pad, bool sibcall_p)
4277 rtx xinner;
4278 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4280 /* Decide where to pad the argument: `downward' for below,
4281 `upward' for above, or `none' for don't pad it.
4282 Default is below for small data on big-endian machines; else above. */
4283 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4285 /* Invert direction if stack is post-decrement.
4286 FIXME: why? */
4287 if (STACK_PUSH_CODE == POST_DEC)
4288 if (where_pad != none)
4289 where_pad = (where_pad == downward ? upward : downward);
4291 xinner = x;
4293 int nregs = partial / UNITS_PER_WORD;
4294 rtx *tmp_regs = NULL;
4295 int overlapping = 0;
4297 if (mode == BLKmode
4298 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4300 /* Copy a block into the stack, entirely or partially. */
4302 rtx temp;
4303 int used;
4304 int offset;
4305 int skip;
4307 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4308 used = partial - offset;
4310 if (mode != BLKmode)
4312 /* A value is to be stored in an insufficiently aligned
4313 stack slot; copy via a suitably aligned slot if
4314 necessary. */
4315 size = GEN_INT (GET_MODE_SIZE (mode));
4316 if (!MEM_P (xinner))
4318 temp = assign_temp (type, 1, 1);
4319 emit_move_insn (temp, xinner);
4320 xinner = temp;
4324 gcc_assert (size);
4326 /* USED is now the # of bytes we need not copy to the stack
4327 because registers will take care of them. */
4329 if (partial != 0)
4330 xinner = adjust_address (xinner, BLKmode, used);
4332 /* If the partial register-part of the arg counts in its stack size,
4333 skip the part of stack space corresponding to the registers.
4334 Otherwise, start copying to the beginning of the stack space,
4335 by setting SKIP to 0. */
4336 skip = (reg_parm_stack_space == 0) ? 0 : used;
4338 #ifdef PUSH_ROUNDING
4339 /* Do it with several push insns if that doesn't take lots of insns
4340 and if there is no difficulty with push insns that skip bytes
4341 on the stack for alignment purposes. */
4342 if (args_addr == 0
4343 && PUSH_ARGS
4344 && CONST_INT_P (size)
4345 && skip == 0
4346 && MEM_ALIGN (xinner) >= align
4347 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4348 /* Here we avoid the case of a structure whose weak alignment
4349 forces many pushes of a small amount of data,
4350 and such small pushes do rounding that causes trouble. */
4351 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4352 || align >= BIGGEST_ALIGNMENT
4353 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4354 == (align / BITS_PER_UNIT)))
4355 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4357 /* Push padding now if padding above and stack grows down,
4358 or if padding below and stack grows up.
4359 But if space already allocated, this has already been done. */
4360 if (extra && args_addr == 0
4361 && where_pad != none && where_pad != stack_direction)
4362 anti_adjust_stack (GEN_INT (extra));
4364 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4366 else
4367 #endif /* PUSH_ROUNDING */
4369 rtx target;
4371 /* Otherwise make space on the stack and copy the data
4372 to the address of that space. */
4374 /* Deduct words put into registers from the size we must copy. */
4375 if (partial != 0)
4377 if (CONST_INT_P (size))
4378 size = GEN_INT (INTVAL (size) - used);
4379 else
4380 size = expand_binop (GET_MODE (size), sub_optab, size,
4381 gen_int_mode (used, GET_MODE (size)),
4382 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4385 /* Get the address of the stack space.
4386 In this case, we do not deal with EXTRA separately.
4387 A single stack adjust will do. */
4388 if (! args_addr)
4390 temp = push_block (size, extra, where_pad == downward);
4391 extra = 0;
4393 else if (CONST_INT_P (args_so_far))
4394 temp = memory_address (BLKmode,
4395 plus_constant (Pmode, args_addr,
4396 skip + INTVAL (args_so_far)));
4397 else
4398 temp = memory_address (BLKmode,
4399 plus_constant (Pmode,
4400 gen_rtx_PLUS (Pmode,
4401 args_addr,
4402 args_so_far),
4403 skip));
4405 if (!ACCUMULATE_OUTGOING_ARGS)
4407 /* If the source is referenced relative to the stack pointer,
4408 copy it to another register to stabilize it. We do not need
4409 to do this if we know that we won't be changing sp. */
4411 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4412 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4413 temp = copy_to_reg (temp);
4416 target = gen_rtx_MEM (BLKmode, temp);
4418 /* We do *not* set_mem_attributes here, because incoming arguments
4419 may overlap with sibling call outgoing arguments and we cannot
4420 allow reordering of reads from function arguments with stores
4421 to outgoing arguments of sibling calls. We do, however, want
4422 to record the alignment of the stack slot. */
4423 /* ALIGN may well be better aligned than TYPE, e.g. due to
4424 PARM_BOUNDARY. Assume the caller isn't lying. */
4425 set_mem_align (target, align);
4427 /* If part should go in registers and pushing to that part would
4428 overwrite some of the values that need to go into regs, load the
4429 overlapping values into temporary pseudos to be moved into the hard
4430 regs at the end after the stack pushing has completed.
4431 We cannot load them directly into the hard regs here because
4432 they can be clobbered by the block move expansions.
4433 See PR 65358. */
4435 if (partial > 0 && reg != 0 && mode == BLKmode
4436 && GET_CODE (reg) != PARALLEL)
4438 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4439 if (overlapping > 0)
4441 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4442 overlapping /= UNITS_PER_WORD;
4444 tmp_regs = XALLOCAVEC (rtx, overlapping);
4446 for (int i = 0; i < overlapping; i++)
4447 tmp_regs[i] = gen_reg_rtx (word_mode);
4449 for (int i = 0; i < overlapping; i++)
4450 emit_move_insn (tmp_regs[i],
4451 operand_subword_force (target, i, mode));
4453 else if (overlapping == -1)
4454 overlapping = 0;
4455 /* Could not determine whether there is overlap.
4456 Fail the sibcall. */
4457 else
4459 overlapping = 0;
4460 if (sibcall_p)
4461 return false;
4464 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4467 else if (partial > 0)
4469 /* Scalar partly in registers. */
4471 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4472 int i;
4473 int not_stack;
4474 /* # bytes of start of argument
4475 that we must make space for but need not store. */
4476 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4477 int args_offset = INTVAL (args_so_far);
4478 int skip;
4480 /* Push padding now if padding above and stack grows down,
4481 or if padding below and stack grows up.
4482 But if space already allocated, this has already been done. */
4483 if (extra && args_addr == 0
4484 && where_pad != none && where_pad != stack_direction)
4485 anti_adjust_stack (GEN_INT (extra));
4487 /* If we make space by pushing it, we might as well push
4488 the real data. Otherwise, we can leave OFFSET nonzero
4489 and leave the space uninitialized. */
4490 if (args_addr == 0)
4491 offset = 0;
4493 /* Now NOT_STACK gets the number of words that we don't need to
4494 allocate on the stack. Convert OFFSET to words too. */
4495 not_stack = (partial - offset) / UNITS_PER_WORD;
4496 offset /= UNITS_PER_WORD;
4498 /* If the partial register-part of the arg counts in its stack size,
4499 skip the part of stack space corresponding to the registers.
4500 Otherwise, start copying to the beginning of the stack space,
4501 by setting SKIP to 0. */
4502 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4504 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4505 x = validize_mem (force_const_mem (mode, x));
4507 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4508 SUBREGs of such registers are not allowed. */
4509 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4510 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4511 x = copy_to_reg (x);
4513 /* Loop over all the words allocated on the stack for this arg. */
4514 /* We can do it by words, because any scalar bigger than a word
4515 has a size a multiple of a word. */
4516 for (i = size - 1; i >= not_stack; i--)
4517 if (i >= not_stack + offset)
4518 if (!emit_push_insn (operand_subword_force (x, i, mode),
4519 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4520 0, args_addr,
4521 GEN_INT (args_offset + ((i - not_stack + skip)
4522 * UNITS_PER_WORD)),
4523 reg_parm_stack_space, alignment_pad, sibcall_p))
4524 return false;
4526 else
4528 rtx addr;
4529 rtx dest;
4531 /* Push padding now if padding above and stack grows down,
4532 or if padding below and stack grows up.
4533 But if space already allocated, this has already been done. */
4534 if (extra && args_addr == 0
4535 && where_pad != none && where_pad != stack_direction)
4536 anti_adjust_stack (GEN_INT (extra));
4538 #ifdef PUSH_ROUNDING
4539 if (args_addr == 0 && PUSH_ARGS)
4540 emit_single_push_insn (mode, x, type);
4541 else
4542 #endif
4544 if (CONST_INT_P (args_so_far))
4545 addr
4546 = memory_address (mode,
4547 plus_constant (Pmode, args_addr,
4548 INTVAL (args_so_far)));
4549 else
4550 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4551 args_so_far));
4552 dest = gen_rtx_MEM (mode, addr);
4554 /* We do *not* set_mem_attributes here, because incoming arguments
4555 may overlap with sibling call outgoing arguments and we cannot
4556 allow reordering of reads from function arguments with stores
4557 to outgoing arguments of sibling calls. We do, however, want
4558 to record the alignment of the stack slot. */
4559 /* ALIGN may well be better aligned than TYPE, e.g. due to
4560 PARM_BOUNDARY. Assume the caller isn't lying. */
4561 set_mem_align (dest, align);
4563 emit_move_insn (dest, x);
4567 /* Move the partial arguments into the registers and any overlapping
4568 values that we moved into the pseudos in tmp_regs. */
4569 if (partial > 0 && reg != 0)
4571 /* Handle calls that pass values in multiple non-contiguous locations.
4572 The Irix 6 ABI has examples of this. */
4573 if (GET_CODE (reg) == PARALLEL)
4574 emit_group_load (reg, x, type, -1);
4575 else
4577 gcc_assert (partial % UNITS_PER_WORD == 0);
4578 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4580 for (int i = 0; i < overlapping; i++)
4581 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4582 + nregs - overlapping + i),
4583 tmp_regs[i]);
4588 if (extra && args_addr == 0 && where_pad == stack_direction)
4589 anti_adjust_stack (GEN_INT (extra));
4591 if (alignment_pad && args_addr == 0)
4592 anti_adjust_stack (alignment_pad);
4594 return true;
4597 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4598 operations. */
4600 static rtx
4601 get_subtarget (rtx x)
4603 return (optimize
4604 || x == 0
4605 /* Only registers can be subtargets. */
4606 || !REG_P (x)
4607 /* Don't use hard regs to avoid extending their life. */
4608 || REGNO (x) < FIRST_PSEUDO_REGISTER
4609 ? 0 : x);
4612 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4613 FIELD is a bitfield. Returns true if the optimization was successful,
4614 and there's nothing else to do. */
4616 static bool
4617 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4618 unsigned HOST_WIDE_INT bitpos,
4619 unsigned HOST_WIDE_INT bitregion_start,
4620 unsigned HOST_WIDE_INT bitregion_end,
4621 machine_mode mode1, rtx str_rtx,
4622 tree to, tree src, bool reverse)
4624 machine_mode str_mode = GET_MODE (str_rtx);
4625 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4626 tree op0, op1;
4627 rtx value, result;
4628 optab binop;
4629 gimple *srcstmt;
4630 enum tree_code code;
4632 if (mode1 != VOIDmode
4633 || bitsize >= BITS_PER_WORD
4634 || str_bitsize > BITS_PER_WORD
4635 || TREE_SIDE_EFFECTS (to)
4636 || TREE_THIS_VOLATILE (to))
4637 return false;
4639 STRIP_NOPS (src);
4640 if (TREE_CODE (src) != SSA_NAME)
4641 return false;
4642 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4643 return false;
4645 srcstmt = get_gimple_for_ssa_name (src);
4646 if (!srcstmt
4647 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4648 return false;
4650 code = gimple_assign_rhs_code (srcstmt);
4652 op0 = gimple_assign_rhs1 (srcstmt);
4654 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4655 to find its initialization. Hopefully the initialization will
4656 be from a bitfield load. */
4657 if (TREE_CODE (op0) == SSA_NAME)
4659 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4661 /* We want to eventually have OP0 be the same as TO, which
4662 should be a bitfield. */
4663 if (!op0stmt
4664 || !is_gimple_assign (op0stmt)
4665 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4666 return false;
4667 op0 = gimple_assign_rhs1 (op0stmt);
4670 op1 = gimple_assign_rhs2 (srcstmt);
4672 if (!operand_equal_p (to, op0, 0))
4673 return false;
4675 if (MEM_P (str_rtx))
4677 unsigned HOST_WIDE_INT offset1;
4679 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4680 str_mode = word_mode;
4681 str_mode = get_best_mode (bitsize, bitpos,
4682 bitregion_start, bitregion_end,
4683 MEM_ALIGN (str_rtx), str_mode, 0);
4684 if (str_mode == VOIDmode)
4685 return false;
4686 str_bitsize = GET_MODE_BITSIZE (str_mode);
4688 offset1 = bitpos;
4689 bitpos %= str_bitsize;
4690 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4691 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4693 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4694 return false;
4695 else
4696 gcc_assert (!reverse);
4698 /* If the bit field covers the whole REG/MEM, store_field
4699 will likely generate better code. */
4700 if (bitsize >= str_bitsize)
4701 return false;
4703 /* We can't handle fields split across multiple entities. */
4704 if (bitpos + bitsize > str_bitsize)
4705 return false;
4707 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4708 bitpos = str_bitsize - bitpos - bitsize;
4710 switch (code)
4712 case PLUS_EXPR:
4713 case MINUS_EXPR:
4714 /* For now, just optimize the case of the topmost bitfield
4715 where we don't need to do any masking and also
4716 1 bit bitfields where xor can be used.
4717 We might win by one instruction for the other bitfields
4718 too if insv/extv instructions aren't used, so that
4719 can be added later. */
4720 if ((reverse || bitpos + bitsize != str_bitsize)
4721 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4722 break;
4724 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4725 value = convert_modes (str_mode,
4726 TYPE_MODE (TREE_TYPE (op1)), value,
4727 TYPE_UNSIGNED (TREE_TYPE (op1)));
4729 /* We may be accessing data outside the field, which means
4730 we can alias adjacent data. */
4731 if (MEM_P (str_rtx))
4733 str_rtx = shallow_copy_rtx (str_rtx);
4734 set_mem_alias_set (str_rtx, 0);
4735 set_mem_expr (str_rtx, 0);
4738 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4740 value = expand_and (str_mode, value, const1_rtx, NULL);
4741 binop = xor_optab;
4743 else
4744 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4746 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4747 if (reverse)
4748 value = flip_storage_order (str_mode, value);
4749 result = expand_binop (str_mode, binop, str_rtx,
4750 value, str_rtx, 1, OPTAB_WIDEN);
4751 if (result != str_rtx)
4752 emit_move_insn (str_rtx, result);
4753 return true;
4755 case BIT_IOR_EXPR:
4756 case BIT_XOR_EXPR:
4757 if (TREE_CODE (op1) != INTEGER_CST)
4758 break;
4759 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4760 value = convert_modes (str_mode,
4761 TYPE_MODE (TREE_TYPE (op1)), value,
4762 TYPE_UNSIGNED (TREE_TYPE (op1)));
4764 /* We may be accessing data outside the field, which means
4765 we can alias adjacent data. */
4766 if (MEM_P (str_rtx))
4768 str_rtx = shallow_copy_rtx (str_rtx);
4769 set_mem_alias_set (str_rtx, 0);
4770 set_mem_expr (str_rtx, 0);
4773 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4774 if (bitpos + bitsize != str_bitsize)
4776 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4777 str_mode);
4778 value = expand_and (str_mode, value, mask, NULL_RTX);
4780 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4781 if (reverse)
4782 value = flip_storage_order (str_mode, value);
4783 result = expand_binop (str_mode, binop, str_rtx,
4784 value, str_rtx, 1, OPTAB_WIDEN);
4785 if (result != str_rtx)
4786 emit_move_insn (str_rtx, result);
4787 return true;
4789 default:
4790 break;
4793 return false;
4796 /* In the C++ memory model, consecutive bit fields in a structure are
4797 considered one memory location.
4799 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4800 returns the bit range of consecutive bits in which this COMPONENT_REF
4801 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4802 and *OFFSET may be adjusted in the process.
4804 If the access does not need to be restricted, 0 is returned in both
4805 *BITSTART and *BITEND. */
4807 void
4808 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4809 unsigned HOST_WIDE_INT *bitend,
4810 tree exp,
4811 HOST_WIDE_INT *bitpos,
4812 tree *offset)
4814 HOST_WIDE_INT bitoffset;
4815 tree field, repr;
4817 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4819 field = TREE_OPERAND (exp, 1);
4820 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4821 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4822 need to limit the range we can access. */
4823 if (!repr)
4825 *bitstart = *bitend = 0;
4826 return;
4829 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4830 part of a larger bit field, then the representative does not serve any
4831 useful purpose. This can occur in Ada. */
4832 if (handled_component_p (TREE_OPERAND (exp, 0)))
4834 machine_mode rmode;
4835 HOST_WIDE_INT rbitsize, rbitpos;
4836 tree roffset;
4837 int unsignedp, reversep, volatilep = 0;
4838 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4839 &roffset, &rmode, &unsignedp, &reversep,
4840 &volatilep);
4841 if ((rbitpos % BITS_PER_UNIT) != 0)
4843 *bitstart = *bitend = 0;
4844 return;
4848 /* Compute the adjustment to bitpos from the offset of the field
4849 relative to the representative. DECL_FIELD_OFFSET of field and
4850 repr are the same by construction if they are not constants,
4851 see finish_bitfield_layout. */
4852 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4853 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4854 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4855 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4856 else
4857 bitoffset = 0;
4858 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4859 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4861 /* If the adjustment is larger than bitpos, we would have a negative bit
4862 position for the lower bound and this may wreak havoc later. Adjust
4863 offset and bitpos to make the lower bound non-negative in that case. */
4864 if (bitoffset > *bitpos)
4866 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4867 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4869 *bitpos += adjust;
4870 if (*offset == NULL_TREE)
4871 *offset = size_int (-adjust / BITS_PER_UNIT);
4872 else
4873 *offset
4874 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4875 *bitstart = 0;
4877 else
4878 *bitstart = *bitpos - bitoffset;
4880 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4883 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4884 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4885 DECL_RTL was not set yet, return NORTL. */
4887 static inline bool
4888 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4890 if (TREE_CODE (addr) != ADDR_EXPR)
4891 return false;
4893 tree base = TREE_OPERAND (addr, 0);
4895 if (!DECL_P (base)
4896 || TREE_ADDRESSABLE (base)
4897 || DECL_MODE (base) == BLKmode)
4898 return false;
4900 if (!DECL_RTL_SET_P (base))
4901 return nortl;
4903 return (!MEM_P (DECL_RTL (base)));
4906 /* Returns true if the MEM_REF REF refers to an object that does not
4907 reside in memory and has non-BLKmode. */
4909 static inline bool
4910 mem_ref_refers_to_non_mem_p (tree ref)
4912 tree base = TREE_OPERAND (ref, 0);
4913 return addr_expr_of_non_mem_decl_p_1 (base, false);
4916 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4917 is true, try generating a nontemporal store. */
4919 void
4920 expand_assignment (tree to, tree from, bool nontemporal)
4922 rtx to_rtx = 0;
4923 rtx result;
4924 machine_mode mode;
4925 unsigned int align;
4926 enum insn_code icode;
4928 /* Don't crash if the lhs of the assignment was erroneous. */
4929 if (TREE_CODE (to) == ERROR_MARK)
4931 expand_normal (from);
4932 return;
4935 /* Optimize away no-op moves without side-effects. */
4936 if (operand_equal_p (to, from, 0))
4937 return;
4939 /* Handle misaligned stores. */
4940 mode = TYPE_MODE (TREE_TYPE (to));
4941 if ((TREE_CODE (to) == MEM_REF
4942 || TREE_CODE (to) == TARGET_MEM_REF)
4943 && mode != BLKmode
4944 && !mem_ref_refers_to_non_mem_p (to)
4945 && ((align = get_object_alignment (to))
4946 < GET_MODE_ALIGNMENT (mode))
4947 && (((icode = optab_handler (movmisalign_optab, mode))
4948 != CODE_FOR_nothing)
4949 || SLOW_UNALIGNED_ACCESS (mode, align)))
4951 rtx reg, mem;
4953 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4954 reg = force_not_mem (reg);
4955 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4956 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4957 reg = flip_storage_order (mode, reg);
4959 if (icode != CODE_FOR_nothing)
4961 struct expand_operand ops[2];
4963 create_fixed_operand (&ops[0], mem);
4964 create_input_operand (&ops[1], reg, mode);
4965 /* The movmisalign<mode> pattern cannot fail, else the assignment
4966 would silently be omitted. */
4967 expand_insn (icode, 2, ops);
4969 else
4970 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4971 false);
4972 return;
4975 /* Assignment of a structure component needs special treatment
4976 if the structure component's rtx is not simply a MEM.
4977 Assignment of an array element at a constant index, and assignment of
4978 an array element in an unaligned packed structure field, has the same
4979 problem. Same for (partially) storing into a non-memory object. */
4980 if (handled_component_p (to)
4981 || (TREE_CODE (to) == MEM_REF
4982 && (REF_REVERSE_STORAGE_ORDER (to)
4983 || mem_ref_refers_to_non_mem_p (to)))
4984 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4986 machine_mode mode1;
4987 HOST_WIDE_INT bitsize, bitpos;
4988 unsigned HOST_WIDE_INT bitregion_start = 0;
4989 unsigned HOST_WIDE_INT bitregion_end = 0;
4990 tree offset;
4991 int unsignedp, reversep, volatilep = 0;
4992 tree tem;
4994 push_temp_slots ();
4995 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4996 &unsignedp, &reversep, &volatilep);
4998 /* Make sure bitpos is not negative, it can wreak havoc later. */
4999 if (bitpos < 0)
5001 gcc_assert (offset == NULL_TREE);
5002 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
5003 bitpos &= BITS_PER_UNIT - 1;
5006 if (TREE_CODE (to) == COMPONENT_REF
5007 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5008 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5009 /* The C++ memory model naturally applies to byte-aligned fields.
5010 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5011 BITSIZE are not byte-aligned, there is no need to limit the range
5012 we can access. This can occur with packed structures in Ada. */
5013 else if (bitsize > 0
5014 && bitsize % BITS_PER_UNIT == 0
5015 && bitpos % BITS_PER_UNIT == 0)
5017 bitregion_start = bitpos;
5018 bitregion_end = bitpos + bitsize - 1;
5021 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5023 /* If the field has a mode, we want to access it in the
5024 field's mode, not the computed mode.
5025 If a MEM has VOIDmode (external with incomplete type),
5026 use BLKmode for it instead. */
5027 if (MEM_P (to_rtx))
5029 if (mode1 != VOIDmode)
5030 to_rtx = adjust_address (to_rtx, mode1, 0);
5031 else if (GET_MODE (to_rtx) == VOIDmode)
5032 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5035 if (offset != 0)
5037 machine_mode address_mode;
5038 rtx offset_rtx;
5040 if (!MEM_P (to_rtx))
5042 /* We can get constant negative offsets into arrays with broken
5043 user code. Translate this to a trap instead of ICEing. */
5044 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5045 expand_builtin_trap ();
5046 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5049 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5050 address_mode = get_address_mode (to_rtx);
5051 if (GET_MODE (offset_rtx) != address_mode)
5053 /* We cannot be sure that the RTL in offset_rtx is valid outside
5054 of a memory address context, so force it into a register
5055 before attempting to convert it to the desired mode. */
5056 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5057 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5060 /* If we have an expression in OFFSET_RTX and a non-zero
5061 byte offset in BITPOS, adding the byte offset before the
5062 OFFSET_RTX results in better intermediate code, which makes
5063 later rtl optimization passes perform better.
5065 We prefer intermediate code like this:
5067 r124:DI=r123:DI+0x18
5068 [r124:DI]=r121:DI
5070 ... instead of ...
5072 r124:DI=r123:DI+0x10
5073 [r124:DI+0x8]=r121:DI
5075 This is only done for aligned data values, as these can
5076 be expected to result in single move instructions. */
5077 if (mode1 != VOIDmode
5078 && bitpos != 0
5079 && bitsize > 0
5080 && (bitpos % bitsize) == 0
5081 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5082 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5084 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5085 bitregion_start = 0;
5086 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5087 bitregion_end -= bitpos;
5088 bitpos = 0;
5091 to_rtx = offset_address (to_rtx, offset_rtx,
5092 highest_pow2_factor_for_target (to,
5093 offset));
5096 /* No action is needed if the target is not a memory and the field
5097 lies completely outside that target. This can occur if the source
5098 code contains an out-of-bounds access to a small array. */
5099 if (!MEM_P (to_rtx)
5100 && GET_MODE (to_rtx) != BLKmode
5101 && (unsigned HOST_WIDE_INT) bitpos
5102 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5104 expand_normal (from);
5105 result = NULL;
5107 /* Handle expand_expr of a complex value returning a CONCAT. */
5108 else if (GET_CODE (to_rtx) == CONCAT)
5110 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5111 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5112 && bitpos == 0
5113 && bitsize == mode_bitsize)
5114 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5115 else if (bitsize == mode_bitsize / 2
5116 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5117 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5118 nontemporal, reversep);
5119 else if (bitpos + bitsize <= mode_bitsize / 2)
5120 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5121 bitregion_start, bitregion_end,
5122 mode1, from, get_alias_set (to),
5123 nontemporal, reversep);
5124 else if (bitpos >= mode_bitsize / 2)
5125 result = store_field (XEXP (to_rtx, 1), bitsize,
5126 bitpos - mode_bitsize / 2,
5127 bitregion_start, bitregion_end,
5128 mode1, from, get_alias_set (to),
5129 nontemporal, reversep);
5130 else if (bitpos == 0 && bitsize == mode_bitsize)
5132 rtx from_rtx;
5133 result = expand_normal (from);
5134 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5135 TYPE_MODE (TREE_TYPE (from)), 0);
5136 emit_move_insn (XEXP (to_rtx, 0),
5137 read_complex_part (from_rtx, false));
5138 emit_move_insn (XEXP (to_rtx, 1),
5139 read_complex_part (from_rtx, true));
5141 else
5143 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5144 GET_MODE_SIZE (GET_MODE (to_rtx)));
5145 write_complex_part (temp, XEXP (to_rtx, 0), false);
5146 write_complex_part (temp, XEXP (to_rtx, 1), true);
5147 result = store_field (temp, bitsize, bitpos,
5148 bitregion_start, bitregion_end,
5149 mode1, from, get_alias_set (to),
5150 nontemporal, reversep);
5151 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5152 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5155 else
5157 if (MEM_P (to_rtx))
5159 /* If the field is at offset zero, we could have been given the
5160 DECL_RTX of the parent struct. Don't munge it. */
5161 to_rtx = shallow_copy_rtx (to_rtx);
5162 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5163 if (volatilep)
5164 MEM_VOLATILE_P (to_rtx) = 1;
5167 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5168 bitregion_start, bitregion_end,
5169 mode1, to_rtx, to, from,
5170 reversep))
5171 result = NULL;
5172 else
5173 result = store_field (to_rtx, bitsize, bitpos,
5174 bitregion_start, bitregion_end,
5175 mode1, from, get_alias_set (to),
5176 nontemporal, reversep);
5179 if (result)
5180 preserve_temp_slots (result);
5181 pop_temp_slots ();
5182 return;
5185 /* If the rhs is a function call and its value is not an aggregate,
5186 call the function before we start to compute the lhs.
5187 This is needed for correct code for cases such as
5188 val = setjmp (buf) on machines where reference to val
5189 requires loading up part of an address in a separate insn.
5191 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5192 since it might be a promoted variable where the zero- or sign- extension
5193 needs to be done. Handling this in the normal way is safe because no
5194 computation is done before the call. The same is true for SSA names. */
5195 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5196 && COMPLETE_TYPE_P (TREE_TYPE (from))
5197 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5198 && ! (((VAR_P (to)
5199 || TREE_CODE (to) == PARM_DECL
5200 || TREE_CODE (to) == RESULT_DECL)
5201 && REG_P (DECL_RTL (to)))
5202 || TREE_CODE (to) == SSA_NAME))
5204 rtx value;
5205 rtx bounds;
5207 push_temp_slots ();
5208 value = expand_normal (from);
5210 /* Split value and bounds to store them separately. */
5211 chkp_split_slot (value, &value, &bounds);
5213 if (to_rtx == 0)
5214 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5216 /* Handle calls that return values in multiple non-contiguous locations.
5217 The Irix 6 ABI has examples of this. */
5218 if (GET_CODE (to_rtx) == PARALLEL)
5220 if (GET_CODE (value) == PARALLEL)
5221 emit_group_move (to_rtx, value);
5222 else
5223 emit_group_load (to_rtx, value, TREE_TYPE (from),
5224 int_size_in_bytes (TREE_TYPE (from)));
5226 else if (GET_CODE (value) == PARALLEL)
5227 emit_group_store (to_rtx, value, TREE_TYPE (from),
5228 int_size_in_bytes (TREE_TYPE (from)));
5229 else if (GET_MODE (to_rtx) == BLKmode)
5231 /* Handle calls that return BLKmode values in registers. */
5232 if (REG_P (value))
5233 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5234 else
5235 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5237 else
5239 if (POINTER_TYPE_P (TREE_TYPE (to)))
5240 value = convert_memory_address_addr_space
5241 (GET_MODE (to_rtx), value,
5242 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5244 emit_move_insn (to_rtx, value);
5247 /* Store bounds if required. */
5248 if (bounds
5249 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5251 gcc_assert (MEM_P (to_rtx));
5252 chkp_emit_bounds_store (bounds, value, to_rtx);
5255 preserve_temp_slots (to_rtx);
5256 pop_temp_slots ();
5257 return;
5260 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5261 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5263 /* Don't move directly into a return register. */
5264 if (TREE_CODE (to) == RESULT_DECL
5265 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5267 rtx temp;
5269 push_temp_slots ();
5271 /* If the source is itself a return value, it still is in a pseudo at
5272 this point so we can move it back to the return register directly. */
5273 if (REG_P (to_rtx)
5274 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5275 && TREE_CODE (from) != CALL_EXPR)
5276 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5277 else
5278 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5280 /* Handle calls that return values in multiple non-contiguous locations.
5281 The Irix 6 ABI has examples of this. */
5282 if (GET_CODE (to_rtx) == PARALLEL)
5284 if (GET_CODE (temp) == PARALLEL)
5285 emit_group_move (to_rtx, temp);
5286 else
5287 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5288 int_size_in_bytes (TREE_TYPE (from)));
5290 else if (temp)
5291 emit_move_insn (to_rtx, temp);
5293 preserve_temp_slots (to_rtx);
5294 pop_temp_slots ();
5295 return;
5298 /* In case we are returning the contents of an object which overlaps
5299 the place the value is being stored, use a safe function when copying
5300 a value through a pointer into a structure value return block. */
5301 if (TREE_CODE (to) == RESULT_DECL
5302 && TREE_CODE (from) == INDIRECT_REF
5303 && ADDR_SPACE_GENERIC_P
5304 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5305 && refs_may_alias_p (to, from)
5306 && cfun->returns_struct
5307 && !cfun->returns_pcc_struct)
5309 rtx from_rtx, size;
5311 push_temp_slots ();
5312 size = expr_size (from);
5313 from_rtx = expand_normal (from);
5315 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5317 preserve_temp_slots (to_rtx);
5318 pop_temp_slots ();
5319 return;
5322 /* Compute FROM and store the value in the rtx we got. */
5324 push_temp_slots ();
5325 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5326 preserve_temp_slots (result);
5327 pop_temp_slots ();
5328 return;
5331 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5332 succeeded, false otherwise. */
5334 bool
5335 emit_storent_insn (rtx to, rtx from)
5337 struct expand_operand ops[2];
5338 machine_mode mode = GET_MODE (to);
5339 enum insn_code code = optab_handler (storent_optab, mode);
5341 if (code == CODE_FOR_nothing)
5342 return false;
5344 create_fixed_operand (&ops[0], to);
5345 create_input_operand (&ops[1], from, mode);
5346 return maybe_expand_insn (code, 2, ops);
5349 /* Generate code for computing expression EXP,
5350 and storing the value into TARGET.
5352 If the mode is BLKmode then we may return TARGET itself.
5353 It turns out that in BLKmode it doesn't cause a problem.
5354 because C has no operators that could combine two different
5355 assignments into the same BLKmode object with different values
5356 with no sequence point. Will other languages need this to
5357 be more thorough?
5359 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5360 stack, and block moves may need to be treated specially.
5362 If NONTEMPORAL is true, try using a nontemporal store instruction.
5364 If REVERSE is true, the store is to be done in reverse order.
5366 If BTARGET is not NULL then computed bounds of EXP are
5367 associated with BTARGET. */
5370 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5371 bool nontemporal, bool reverse, tree btarget)
5373 rtx temp;
5374 rtx alt_rtl = NULL_RTX;
5375 location_t loc = curr_insn_location ();
5377 if (VOID_TYPE_P (TREE_TYPE (exp)))
5379 /* C++ can generate ?: expressions with a throw expression in one
5380 branch and an rvalue in the other. Here, we resolve attempts to
5381 store the throw expression's nonexistent result. */
5382 gcc_assert (!call_param_p);
5383 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5384 return NULL_RTX;
5386 if (TREE_CODE (exp) == COMPOUND_EXPR)
5388 /* Perform first part of compound expression, then assign from second
5389 part. */
5390 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5391 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5392 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5393 call_param_p, nontemporal, reverse,
5394 btarget);
5396 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5398 /* For conditional expression, get safe form of the target. Then
5399 test the condition, doing the appropriate assignment on either
5400 side. This avoids the creation of unnecessary temporaries.
5401 For non-BLKmode, it is more efficient not to do this. */
5403 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5405 do_pending_stack_adjust ();
5406 NO_DEFER_POP;
5407 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5408 profile_probability::uninitialized ());
5409 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5410 nontemporal, reverse, btarget);
5411 emit_jump_insn (targetm.gen_jump (lab2));
5412 emit_barrier ();
5413 emit_label (lab1);
5414 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5415 nontemporal, reverse, btarget);
5416 emit_label (lab2);
5417 OK_DEFER_POP;
5419 return NULL_RTX;
5421 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5422 /* If this is a scalar in a register that is stored in a wider mode
5423 than the declared mode, compute the result into its declared mode
5424 and then convert to the wider mode. Our value is the computed
5425 expression. */
5427 rtx inner_target = 0;
5429 /* We can do the conversion inside EXP, which will often result
5430 in some optimizations. Do the conversion in two steps: first
5431 change the signedness, if needed, then the extend. But don't
5432 do this if the type of EXP is a subtype of something else
5433 since then the conversion might involve more than just
5434 converting modes. */
5435 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5436 && TREE_TYPE (TREE_TYPE (exp)) == 0
5437 && GET_MODE_PRECISION (GET_MODE (target))
5438 == TYPE_PRECISION (TREE_TYPE (exp)))
5440 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5441 TYPE_UNSIGNED (TREE_TYPE (exp))))
5443 /* Some types, e.g. Fortran's logical*4, won't have a signed
5444 version, so use the mode instead. */
5445 tree ntype
5446 = (signed_or_unsigned_type_for
5447 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5448 if (ntype == NULL)
5449 ntype = lang_hooks.types.type_for_mode
5450 (TYPE_MODE (TREE_TYPE (exp)),
5451 SUBREG_PROMOTED_SIGN (target));
5453 exp = fold_convert_loc (loc, ntype, exp);
5456 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5457 (GET_MODE (SUBREG_REG (target)),
5458 SUBREG_PROMOTED_SIGN (target)),
5459 exp);
5461 inner_target = SUBREG_REG (target);
5464 temp = expand_expr (exp, inner_target, VOIDmode,
5465 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5467 /* Handle bounds returned by call. */
5468 if (TREE_CODE (exp) == CALL_EXPR)
5470 rtx bounds;
5471 chkp_split_slot (temp, &temp, &bounds);
5472 if (bounds && btarget)
5474 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5475 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5476 chkp_set_rtl_bounds (btarget, tmp);
5480 /* If TEMP is a VOIDmode constant, use convert_modes to make
5481 sure that we properly convert it. */
5482 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5484 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5485 temp, SUBREG_PROMOTED_SIGN (target));
5486 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5487 GET_MODE (target), temp,
5488 SUBREG_PROMOTED_SIGN (target));
5491 convert_move (SUBREG_REG (target), temp,
5492 SUBREG_PROMOTED_SIGN (target));
5494 return NULL_RTX;
5496 else if ((TREE_CODE (exp) == STRING_CST
5497 || (TREE_CODE (exp) == MEM_REF
5498 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5499 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5500 == STRING_CST
5501 && integer_zerop (TREE_OPERAND (exp, 1))))
5502 && !nontemporal && !call_param_p
5503 && MEM_P (target))
5505 /* Optimize initialization of an array with a STRING_CST. */
5506 HOST_WIDE_INT exp_len, str_copy_len;
5507 rtx dest_mem;
5508 tree str = TREE_CODE (exp) == STRING_CST
5509 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5511 exp_len = int_expr_size (exp);
5512 if (exp_len <= 0)
5513 goto normal_expr;
5515 if (TREE_STRING_LENGTH (str) <= 0)
5516 goto normal_expr;
5518 str_copy_len = strlen (TREE_STRING_POINTER (str));
5519 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5520 goto normal_expr;
5522 str_copy_len = TREE_STRING_LENGTH (str);
5523 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5524 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5526 str_copy_len += STORE_MAX_PIECES - 1;
5527 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5529 str_copy_len = MIN (str_copy_len, exp_len);
5530 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5531 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5532 MEM_ALIGN (target), false))
5533 goto normal_expr;
5535 dest_mem = target;
5537 dest_mem = store_by_pieces (dest_mem,
5538 str_copy_len, builtin_strncpy_read_str,
5539 CONST_CAST (char *,
5540 TREE_STRING_POINTER (str)),
5541 MEM_ALIGN (target), false,
5542 exp_len > str_copy_len ? 1 : 0);
5543 if (exp_len > str_copy_len)
5544 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5545 GEN_INT (exp_len - str_copy_len),
5546 BLOCK_OP_NORMAL);
5547 return NULL_RTX;
5549 else
5551 rtx tmp_target;
5553 normal_expr:
5554 /* If we want to use a nontemporal or a reverse order store, force the
5555 value into a register first. */
5556 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5557 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5558 (call_param_p
5559 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5560 &alt_rtl, false);
5562 /* Handle bounds returned by call. */
5563 if (TREE_CODE (exp) == CALL_EXPR)
5565 rtx bounds;
5566 chkp_split_slot (temp, &temp, &bounds);
5567 if (bounds && btarget)
5569 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5570 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5571 chkp_set_rtl_bounds (btarget, tmp);
5576 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5577 the same as that of TARGET, adjust the constant. This is needed, for
5578 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5579 only a word-sized value. */
5580 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5581 && TREE_CODE (exp) != ERROR_MARK
5582 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5583 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5584 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5586 /* If value was not generated in the target, store it there.
5587 Convert the value to TARGET's type first if necessary and emit the
5588 pending incrementations that have been queued when expanding EXP.
5589 Note that we cannot emit the whole queue blindly because this will
5590 effectively disable the POST_INC optimization later.
5592 If TEMP and TARGET compare equal according to rtx_equal_p, but
5593 one or both of them are volatile memory refs, we have to distinguish
5594 two cases:
5595 - expand_expr has used TARGET. In this case, we must not generate
5596 another copy. This can be detected by TARGET being equal according
5597 to == .
5598 - expand_expr has not used TARGET - that means that the source just
5599 happens to have the same RTX form. Since temp will have been created
5600 by expand_expr, it will compare unequal according to == .
5601 We must generate a copy in this case, to reach the correct number
5602 of volatile memory references. */
5604 if ((! rtx_equal_p (temp, target)
5605 || (temp != target && (side_effects_p (temp)
5606 || side_effects_p (target))))
5607 && TREE_CODE (exp) != ERROR_MARK
5608 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5609 but TARGET is not valid memory reference, TEMP will differ
5610 from TARGET although it is really the same location. */
5611 && !(alt_rtl
5612 && rtx_equal_p (alt_rtl, target)
5613 && !side_effects_p (alt_rtl)
5614 && !side_effects_p (target))
5615 /* If there's nothing to copy, don't bother. Don't call
5616 expr_size unless necessary, because some front-ends (C++)
5617 expr_size-hook must not be given objects that are not
5618 supposed to be bit-copied or bit-initialized. */
5619 && expr_size (exp) != const0_rtx)
5621 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5623 if (GET_MODE (target) == BLKmode)
5625 /* Handle calls that return BLKmode values in registers. */
5626 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5627 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5628 else
5629 store_bit_field (target,
5630 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5631 0, 0, 0, GET_MODE (temp), temp, reverse);
5633 else
5634 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5637 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5639 /* Handle copying a string constant into an array. The string
5640 constant may be shorter than the array. So copy just the string's
5641 actual length, and clear the rest. First get the size of the data
5642 type of the string, which is actually the size of the target. */
5643 rtx size = expr_size (exp);
5645 if (CONST_INT_P (size)
5646 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5647 emit_block_move (target, temp, size,
5648 (call_param_p
5649 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5650 else
5652 machine_mode pointer_mode
5653 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5654 machine_mode address_mode = get_address_mode (target);
5656 /* Compute the size of the data to copy from the string. */
5657 tree copy_size
5658 = size_binop_loc (loc, MIN_EXPR,
5659 make_tree (sizetype, size),
5660 size_int (TREE_STRING_LENGTH (exp)));
5661 rtx copy_size_rtx
5662 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5663 (call_param_p
5664 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5665 rtx_code_label *label = 0;
5667 /* Copy that much. */
5668 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5669 TYPE_UNSIGNED (sizetype));
5670 emit_block_move (target, temp, copy_size_rtx,
5671 (call_param_p
5672 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5674 /* Figure out how much is left in TARGET that we have to clear.
5675 Do all calculations in pointer_mode. */
5676 if (CONST_INT_P (copy_size_rtx))
5678 size = plus_constant (address_mode, size,
5679 -INTVAL (copy_size_rtx));
5680 target = adjust_address (target, BLKmode,
5681 INTVAL (copy_size_rtx));
5683 else
5685 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5686 copy_size_rtx, NULL_RTX, 0,
5687 OPTAB_LIB_WIDEN);
5689 if (GET_MODE (copy_size_rtx) != address_mode)
5690 copy_size_rtx = convert_to_mode (address_mode,
5691 copy_size_rtx,
5692 TYPE_UNSIGNED (sizetype));
5694 target = offset_address (target, copy_size_rtx,
5695 highest_pow2_factor (copy_size));
5696 label = gen_label_rtx ();
5697 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5698 GET_MODE (size), 0, label);
5701 if (size != const0_rtx)
5702 clear_storage (target, size, BLOCK_OP_NORMAL);
5704 if (label)
5705 emit_label (label);
5708 /* Handle calls that return values in multiple non-contiguous locations.
5709 The Irix 6 ABI has examples of this. */
5710 else if (GET_CODE (target) == PARALLEL)
5712 if (GET_CODE (temp) == PARALLEL)
5713 emit_group_move (target, temp);
5714 else
5715 emit_group_load (target, temp, TREE_TYPE (exp),
5716 int_size_in_bytes (TREE_TYPE (exp)));
5718 else if (GET_CODE (temp) == PARALLEL)
5719 emit_group_store (target, temp, TREE_TYPE (exp),
5720 int_size_in_bytes (TREE_TYPE (exp)));
5721 else if (GET_MODE (temp) == BLKmode)
5722 emit_block_move (target, temp, expr_size (exp),
5723 (call_param_p
5724 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5725 /* If we emit a nontemporal store, there is nothing else to do. */
5726 else if (nontemporal && emit_storent_insn (target, temp))
5728 else
5730 if (reverse)
5731 temp = flip_storage_order (GET_MODE (target), temp);
5732 temp = force_operand (temp, target);
5733 if (temp != target)
5734 emit_move_insn (target, temp);
5738 return NULL_RTX;
5741 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5743 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5744 bool reverse)
5746 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5747 reverse, NULL);
5750 /* Return true if field F of structure TYPE is a flexible array. */
5752 static bool
5753 flexible_array_member_p (const_tree f, const_tree type)
5755 const_tree tf;
5757 tf = TREE_TYPE (f);
5758 return (DECL_CHAIN (f) == NULL
5759 && TREE_CODE (tf) == ARRAY_TYPE
5760 && TYPE_DOMAIN (tf)
5761 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5762 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5763 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5764 && int_size_in_bytes (type) >= 0);
5767 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5768 must have in order for it to completely initialize a value of type TYPE.
5769 Return -1 if the number isn't known.
5771 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5773 static HOST_WIDE_INT
5774 count_type_elements (const_tree type, bool for_ctor_p)
5776 switch (TREE_CODE (type))
5778 case ARRAY_TYPE:
5780 tree nelts;
5782 nelts = array_type_nelts (type);
5783 if (nelts && tree_fits_uhwi_p (nelts))
5785 unsigned HOST_WIDE_INT n;
5787 n = tree_to_uhwi (nelts) + 1;
5788 if (n == 0 || for_ctor_p)
5789 return n;
5790 else
5791 return n * count_type_elements (TREE_TYPE (type), false);
5793 return for_ctor_p ? -1 : 1;
5796 case RECORD_TYPE:
5798 unsigned HOST_WIDE_INT n;
5799 tree f;
5801 n = 0;
5802 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5803 if (TREE_CODE (f) == FIELD_DECL)
5805 if (!for_ctor_p)
5806 n += count_type_elements (TREE_TYPE (f), false);
5807 else if (!flexible_array_member_p (f, type))
5808 /* Don't count flexible arrays, which are not supposed
5809 to be initialized. */
5810 n += 1;
5813 return n;
5816 case UNION_TYPE:
5817 case QUAL_UNION_TYPE:
5819 tree f;
5820 HOST_WIDE_INT n, m;
5822 gcc_assert (!for_ctor_p);
5823 /* Estimate the number of scalars in each field and pick the
5824 maximum. Other estimates would do instead; the idea is simply
5825 to make sure that the estimate is not sensitive to the ordering
5826 of the fields. */
5827 n = 1;
5828 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5829 if (TREE_CODE (f) == FIELD_DECL)
5831 m = count_type_elements (TREE_TYPE (f), false);
5832 /* If the field doesn't span the whole union, add an extra
5833 scalar for the rest. */
5834 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5835 TYPE_SIZE (type)) != 1)
5836 m++;
5837 if (n < m)
5838 n = m;
5840 return n;
5843 case COMPLEX_TYPE:
5844 return 2;
5846 case VECTOR_TYPE:
5847 return TYPE_VECTOR_SUBPARTS (type);
5849 case INTEGER_TYPE:
5850 case REAL_TYPE:
5851 case FIXED_POINT_TYPE:
5852 case ENUMERAL_TYPE:
5853 case BOOLEAN_TYPE:
5854 case POINTER_TYPE:
5855 case OFFSET_TYPE:
5856 case REFERENCE_TYPE:
5857 case NULLPTR_TYPE:
5858 return 1;
5860 case ERROR_MARK:
5861 return 0;
5863 case VOID_TYPE:
5864 case METHOD_TYPE:
5865 case FUNCTION_TYPE:
5866 case LANG_TYPE:
5867 default:
5868 gcc_unreachable ();
5872 /* Helper for categorize_ctor_elements. Identical interface. */
5874 static bool
5875 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5876 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5878 unsigned HOST_WIDE_INT idx;
5879 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5880 tree value, purpose, elt_type;
5882 /* Whether CTOR is a valid constant initializer, in accordance with what
5883 initializer_constant_valid_p does. If inferred from the constructor
5884 elements, true until proven otherwise. */
5885 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5886 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5888 nz_elts = 0;
5889 init_elts = 0;
5890 num_fields = 0;
5891 elt_type = NULL_TREE;
5893 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5895 HOST_WIDE_INT mult = 1;
5897 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5899 tree lo_index = TREE_OPERAND (purpose, 0);
5900 tree hi_index = TREE_OPERAND (purpose, 1);
5902 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5903 mult = (tree_to_uhwi (hi_index)
5904 - tree_to_uhwi (lo_index) + 1);
5906 num_fields += mult;
5907 elt_type = TREE_TYPE (value);
5909 switch (TREE_CODE (value))
5911 case CONSTRUCTOR:
5913 HOST_WIDE_INT nz = 0, ic = 0;
5915 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5916 p_complete);
5918 nz_elts += mult * nz;
5919 init_elts += mult * ic;
5921 if (const_from_elts_p && const_p)
5922 const_p = const_elt_p;
5924 break;
5926 case INTEGER_CST:
5927 case REAL_CST:
5928 case FIXED_CST:
5929 if (!initializer_zerop (value))
5930 nz_elts += mult;
5931 init_elts += mult;
5932 break;
5934 case STRING_CST:
5935 nz_elts += mult * TREE_STRING_LENGTH (value);
5936 init_elts += mult * TREE_STRING_LENGTH (value);
5937 break;
5939 case COMPLEX_CST:
5940 if (!initializer_zerop (TREE_REALPART (value)))
5941 nz_elts += mult;
5942 if (!initializer_zerop (TREE_IMAGPART (value)))
5943 nz_elts += mult;
5944 init_elts += mult;
5945 break;
5947 case VECTOR_CST:
5949 unsigned i;
5950 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5952 tree v = VECTOR_CST_ELT (value, i);
5953 if (!initializer_zerop (v))
5954 nz_elts += mult;
5955 init_elts += mult;
5958 break;
5960 default:
5962 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5963 nz_elts += mult * tc;
5964 init_elts += mult * tc;
5966 if (const_from_elts_p && const_p)
5967 const_p
5968 = initializer_constant_valid_p (value,
5969 elt_type,
5970 TYPE_REVERSE_STORAGE_ORDER
5971 (TREE_TYPE (ctor)))
5972 != NULL_TREE;
5974 break;
5978 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5979 num_fields, elt_type))
5980 *p_complete = false;
5982 *p_nz_elts += nz_elts;
5983 *p_init_elts += init_elts;
5985 return const_p;
5988 /* Examine CTOR to discover:
5989 * how many scalar fields are set to nonzero values,
5990 and place it in *P_NZ_ELTS;
5991 * how many scalar fields in total are in CTOR,
5992 and place it in *P_ELT_COUNT.
5993 * whether the constructor is complete -- in the sense that every
5994 meaningful byte is explicitly given a value --
5995 and place it in *P_COMPLETE.
5997 Return whether or not CTOR is a valid static constant initializer, the same
5998 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
6000 bool
6001 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
6002 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6004 *p_nz_elts = 0;
6005 *p_init_elts = 0;
6006 *p_complete = true;
6008 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6011 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6012 of which had type LAST_TYPE. Each element was itself a complete
6013 initializer, in the sense that every meaningful byte was explicitly
6014 given a value. Return true if the same is true for the constructor
6015 as a whole. */
6017 bool
6018 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6019 const_tree last_type)
6021 if (TREE_CODE (type) == UNION_TYPE
6022 || TREE_CODE (type) == QUAL_UNION_TYPE)
6024 if (num_elts == 0)
6025 return false;
6027 gcc_assert (num_elts == 1 && last_type);
6029 /* ??? We could look at each element of the union, and find the
6030 largest element. Which would avoid comparing the size of the
6031 initialized element against any tail padding in the union.
6032 Doesn't seem worth the effort... */
6033 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6036 return count_type_elements (type, true) == num_elts;
6039 /* Return 1 if EXP contains mostly (3/4) zeros. */
6041 static int
6042 mostly_zeros_p (const_tree exp)
6044 if (TREE_CODE (exp) == CONSTRUCTOR)
6046 HOST_WIDE_INT nz_elts, init_elts;
6047 bool complete_p;
6049 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6050 return !complete_p || nz_elts < init_elts / 4;
6053 return initializer_zerop (exp);
6056 /* Return 1 if EXP contains all zeros. */
6058 static int
6059 all_zeros_p (const_tree exp)
6061 if (TREE_CODE (exp) == CONSTRUCTOR)
6063 HOST_WIDE_INT nz_elts, init_elts;
6064 bool complete_p;
6066 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6067 return nz_elts == 0;
6070 return initializer_zerop (exp);
6073 /* Helper function for store_constructor.
6074 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6075 CLEARED is as for store_constructor.
6076 ALIAS_SET is the alias set to use for any stores.
6077 If REVERSE is true, the store is to be done in reverse order.
6079 This provides a recursive shortcut back to store_constructor when it isn't
6080 necessary to go through store_field. This is so that we can pass through
6081 the cleared field to let store_constructor know that we may not have to
6082 clear a substructure if the outer structure has already been cleared. */
6084 static void
6085 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6086 HOST_WIDE_INT bitpos,
6087 unsigned HOST_WIDE_INT bitregion_start,
6088 unsigned HOST_WIDE_INT bitregion_end,
6089 machine_mode mode,
6090 tree exp, int cleared,
6091 alias_set_type alias_set, bool reverse)
6093 if (TREE_CODE (exp) == CONSTRUCTOR
6094 /* We can only call store_constructor recursively if the size and
6095 bit position are on a byte boundary. */
6096 && bitpos % BITS_PER_UNIT == 0
6097 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6098 /* If we have a nonzero bitpos for a register target, then we just
6099 let store_field do the bitfield handling. This is unlikely to
6100 generate unnecessary clear instructions anyways. */
6101 && (bitpos == 0 || MEM_P (target)))
6103 if (MEM_P (target))
6104 target
6105 = adjust_address (target,
6106 GET_MODE (target) == BLKmode
6107 || 0 != (bitpos
6108 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6109 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6112 /* Update the alias set, if required. */
6113 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6114 && MEM_ALIAS_SET (target) != 0)
6116 target = copy_rtx (target);
6117 set_mem_alias_set (target, alias_set);
6120 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6121 reverse);
6123 else
6124 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6125 exp, alias_set, false, reverse);
6129 /* Returns the number of FIELD_DECLs in TYPE. */
6131 static int
6132 fields_length (const_tree type)
6134 tree t = TYPE_FIELDS (type);
6135 int count = 0;
6137 for (; t; t = DECL_CHAIN (t))
6138 if (TREE_CODE (t) == FIELD_DECL)
6139 ++count;
6141 return count;
6145 /* Store the value of constructor EXP into the rtx TARGET.
6146 TARGET is either a REG or a MEM; we know it cannot conflict, since
6147 safe_from_p has been called.
6148 CLEARED is true if TARGET is known to have been zero'd.
6149 SIZE is the number of bytes of TARGET we are allowed to modify: this
6150 may not be the same as the size of EXP if we are assigning to a field
6151 which has been packed to exclude padding bits.
6152 If REVERSE is true, the store is to be done in reverse order. */
6154 static void
6155 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6156 bool reverse)
6158 tree type = TREE_TYPE (exp);
6159 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6160 HOST_WIDE_INT bitregion_end = size > 0 ? size * BITS_PER_UNIT - 1 : 0;
6162 switch (TREE_CODE (type))
6164 case RECORD_TYPE:
6165 case UNION_TYPE:
6166 case QUAL_UNION_TYPE:
6168 unsigned HOST_WIDE_INT idx;
6169 tree field, value;
6171 /* The storage order is specified for every aggregate type. */
6172 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6174 /* If size is zero or the target is already cleared, do nothing. */
6175 if (size == 0 || cleared)
6176 cleared = 1;
6177 /* We either clear the aggregate or indicate the value is dead. */
6178 else if ((TREE_CODE (type) == UNION_TYPE
6179 || TREE_CODE (type) == QUAL_UNION_TYPE)
6180 && ! CONSTRUCTOR_ELTS (exp))
6181 /* If the constructor is empty, clear the union. */
6183 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6184 cleared = 1;
6187 /* If we are building a static constructor into a register,
6188 set the initial value as zero so we can fold the value into
6189 a constant. But if more than one register is involved,
6190 this probably loses. */
6191 else if (REG_P (target) && TREE_STATIC (exp)
6192 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6194 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6195 cleared = 1;
6198 /* If the constructor has fewer fields than the structure or
6199 if we are initializing the structure to mostly zeros, clear
6200 the whole structure first. Don't do this if TARGET is a
6201 register whose mode size isn't equal to SIZE since
6202 clear_storage can't handle this case. */
6203 else if (size > 0
6204 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6205 || mostly_zeros_p (exp))
6206 && (!REG_P (target)
6207 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6208 == size)))
6210 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6211 cleared = 1;
6214 if (REG_P (target) && !cleared)
6215 emit_clobber (target);
6217 /* Store each element of the constructor into the
6218 corresponding field of TARGET. */
6219 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6221 machine_mode mode;
6222 HOST_WIDE_INT bitsize;
6223 HOST_WIDE_INT bitpos = 0;
6224 tree offset;
6225 rtx to_rtx = target;
6227 /* Just ignore missing fields. We cleared the whole
6228 structure, above, if any fields are missing. */
6229 if (field == 0)
6230 continue;
6232 if (cleared && initializer_zerop (value))
6233 continue;
6235 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6236 bitsize = tree_to_uhwi (DECL_SIZE (field));
6237 else
6238 gcc_unreachable ();
6240 mode = DECL_MODE (field);
6241 if (DECL_BIT_FIELD (field))
6242 mode = VOIDmode;
6244 offset = DECL_FIELD_OFFSET (field);
6245 if (tree_fits_shwi_p (offset)
6246 && tree_fits_shwi_p (bit_position (field)))
6248 bitpos = int_bit_position (field);
6249 offset = NULL_TREE;
6251 else
6252 gcc_unreachable ();
6254 /* If this initializes a field that is smaller than a
6255 word, at the start of a word, try to widen it to a full
6256 word. This special case allows us to output C++ member
6257 function initializations in a form that the optimizers
6258 can understand. */
6259 if (WORD_REGISTER_OPERATIONS
6260 && REG_P (target)
6261 && bitsize < BITS_PER_WORD
6262 && bitpos % BITS_PER_WORD == 0
6263 && GET_MODE_CLASS (mode) == MODE_INT
6264 && TREE_CODE (value) == INTEGER_CST
6265 && exp_size >= 0
6266 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6268 tree type = TREE_TYPE (value);
6270 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6272 type = lang_hooks.types.type_for_mode
6273 (word_mode, TYPE_UNSIGNED (type));
6274 value = fold_convert (type, value);
6275 /* Make sure the bits beyond the original bitsize are zero
6276 so that we can correctly avoid extra zeroing stores in
6277 later constructor elements. */
6278 tree bitsize_mask
6279 = wide_int_to_tree (type, wi::mask (bitsize, false,
6280 BITS_PER_WORD));
6281 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6284 if (BYTES_BIG_ENDIAN)
6285 value
6286 = fold_build2 (LSHIFT_EXPR, type, value,
6287 build_int_cst (type,
6288 BITS_PER_WORD - bitsize));
6289 bitsize = BITS_PER_WORD;
6290 mode = word_mode;
6293 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6294 && DECL_NONADDRESSABLE_P (field))
6296 to_rtx = copy_rtx (to_rtx);
6297 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6300 store_constructor_field (to_rtx, bitsize, bitpos,
6301 0, bitregion_end, mode,
6302 value, cleared,
6303 get_alias_set (TREE_TYPE (field)),
6304 reverse);
6306 break;
6308 case ARRAY_TYPE:
6310 tree value, index;
6311 unsigned HOST_WIDE_INT i;
6312 int need_to_clear;
6313 tree domain;
6314 tree elttype = TREE_TYPE (type);
6315 int const_bounds_p;
6316 HOST_WIDE_INT minelt = 0;
6317 HOST_WIDE_INT maxelt = 0;
6319 /* The storage order is specified for every aggregate type. */
6320 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6322 domain = TYPE_DOMAIN (type);
6323 const_bounds_p = (TYPE_MIN_VALUE (domain)
6324 && TYPE_MAX_VALUE (domain)
6325 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6326 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6328 /* If we have constant bounds for the range of the type, get them. */
6329 if (const_bounds_p)
6331 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6332 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6335 /* If the constructor has fewer elements than the array, clear
6336 the whole array first. Similarly if this is static
6337 constructor of a non-BLKmode object. */
6338 if (cleared)
6339 need_to_clear = 0;
6340 else if (REG_P (target) && TREE_STATIC (exp))
6341 need_to_clear = 1;
6342 else
6344 unsigned HOST_WIDE_INT idx;
6345 tree index, value;
6346 HOST_WIDE_INT count = 0, zero_count = 0;
6347 need_to_clear = ! const_bounds_p;
6349 /* This loop is a more accurate version of the loop in
6350 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6351 is also needed to check for missing elements. */
6352 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6354 HOST_WIDE_INT this_node_count;
6356 if (need_to_clear)
6357 break;
6359 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6361 tree lo_index = TREE_OPERAND (index, 0);
6362 tree hi_index = TREE_OPERAND (index, 1);
6364 if (! tree_fits_uhwi_p (lo_index)
6365 || ! tree_fits_uhwi_p (hi_index))
6367 need_to_clear = 1;
6368 break;
6371 this_node_count = (tree_to_uhwi (hi_index)
6372 - tree_to_uhwi (lo_index) + 1);
6374 else
6375 this_node_count = 1;
6377 count += this_node_count;
6378 if (mostly_zeros_p (value))
6379 zero_count += this_node_count;
6382 /* Clear the entire array first if there are any missing
6383 elements, or if the incidence of zero elements is >=
6384 75%. */
6385 if (! need_to_clear
6386 && (count < maxelt - minelt + 1
6387 || 4 * zero_count >= 3 * count))
6388 need_to_clear = 1;
6391 if (need_to_clear && size > 0)
6393 if (REG_P (target))
6394 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6395 else
6396 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6397 cleared = 1;
6400 if (!cleared && REG_P (target))
6401 /* Inform later passes that the old value is dead. */
6402 emit_clobber (target);
6404 /* Store each element of the constructor into the
6405 corresponding element of TARGET, determined by counting the
6406 elements. */
6407 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6409 machine_mode mode;
6410 HOST_WIDE_INT bitsize;
6411 HOST_WIDE_INT bitpos;
6412 rtx xtarget = target;
6414 if (cleared && initializer_zerop (value))
6415 continue;
6417 mode = TYPE_MODE (elttype);
6418 if (mode == BLKmode)
6419 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6420 ? tree_to_uhwi (TYPE_SIZE (elttype))
6421 : -1);
6422 else
6423 bitsize = GET_MODE_BITSIZE (mode);
6425 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6427 tree lo_index = TREE_OPERAND (index, 0);
6428 tree hi_index = TREE_OPERAND (index, 1);
6429 rtx index_r, pos_rtx;
6430 HOST_WIDE_INT lo, hi, count;
6431 tree position;
6433 /* If the range is constant and "small", unroll the loop. */
6434 if (const_bounds_p
6435 && tree_fits_shwi_p (lo_index)
6436 && tree_fits_shwi_p (hi_index)
6437 && (lo = tree_to_shwi (lo_index),
6438 hi = tree_to_shwi (hi_index),
6439 count = hi - lo + 1,
6440 (!MEM_P (target)
6441 || count <= 2
6442 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6443 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6444 <= 40 * 8)))))
6446 lo -= minelt; hi -= minelt;
6447 for (; lo <= hi; lo++)
6449 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6451 if (MEM_P (target)
6452 && !MEM_KEEP_ALIAS_SET_P (target)
6453 && TREE_CODE (type) == ARRAY_TYPE
6454 && TYPE_NONALIASED_COMPONENT (type))
6456 target = copy_rtx (target);
6457 MEM_KEEP_ALIAS_SET_P (target) = 1;
6460 store_constructor_field
6461 (target, bitsize, bitpos, 0, bitregion_end,
6462 mode, value, cleared,
6463 get_alias_set (elttype), reverse);
6466 else
6468 rtx_code_label *loop_start = gen_label_rtx ();
6469 rtx_code_label *loop_end = gen_label_rtx ();
6470 tree exit_cond;
6472 expand_normal (hi_index);
6474 index = build_decl (EXPR_LOCATION (exp),
6475 VAR_DECL, NULL_TREE, domain);
6476 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6477 SET_DECL_RTL (index, index_r);
6478 store_expr (lo_index, index_r, 0, false, reverse);
6480 /* Build the head of the loop. */
6481 do_pending_stack_adjust ();
6482 emit_label (loop_start);
6484 /* Assign value to element index. */
6485 position =
6486 fold_convert (ssizetype,
6487 fold_build2 (MINUS_EXPR,
6488 TREE_TYPE (index),
6489 index,
6490 TYPE_MIN_VALUE (domain)));
6492 position =
6493 size_binop (MULT_EXPR, position,
6494 fold_convert (ssizetype,
6495 TYPE_SIZE_UNIT (elttype)));
6497 pos_rtx = expand_normal (position);
6498 xtarget = offset_address (target, pos_rtx,
6499 highest_pow2_factor (position));
6500 xtarget = adjust_address (xtarget, mode, 0);
6501 if (TREE_CODE (value) == CONSTRUCTOR)
6502 store_constructor (value, xtarget, cleared,
6503 bitsize / BITS_PER_UNIT, reverse);
6504 else
6505 store_expr (value, xtarget, 0, false, reverse);
6507 /* Generate a conditional jump to exit the loop. */
6508 exit_cond = build2 (LT_EXPR, integer_type_node,
6509 index, hi_index);
6510 jumpif (exit_cond, loop_end,
6511 profile_probability::uninitialized ());
6513 /* Update the loop counter, and jump to the head of
6514 the loop. */
6515 expand_assignment (index,
6516 build2 (PLUS_EXPR, TREE_TYPE (index),
6517 index, integer_one_node),
6518 false);
6520 emit_jump (loop_start);
6522 /* Build the end of the loop. */
6523 emit_label (loop_end);
6526 else if ((index != 0 && ! tree_fits_shwi_p (index))
6527 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6529 tree position;
6531 if (index == 0)
6532 index = ssize_int (1);
6534 if (minelt)
6535 index = fold_convert (ssizetype,
6536 fold_build2 (MINUS_EXPR,
6537 TREE_TYPE (index),
6538 index,
6539 TYPE_MIN_VALUE (domain)));
6541 position =
6542 size_binop (MULT_EXPR, index,
6543 fold_convert (ssizetype,
6544 TYPE_SIZE_UNIT (elttype)));
6545 xtarget = offset_address (target,
6546 expand_normal (position),
6547 highest_pow2_factor (position));
6548 xtarget = adjust_address (xtarget, mode, 0);
6549 store_expr (value, xtarget, 0, false, reverse);
6551 else
6553 if (index != 0)
6554 bitpos = ((tree_to_shwi (index) - minelt)
6555 * tree_to_uhwi (TYPE_SIZE (elttype)));
6556 else
6557 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6559 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6560 && TREE_CODE (type) == ARRAY_TYPE
6561 && TYPE_NONALIASED_COMPONENT (type))
6563 target = copy_rtx (target);
6564 MEM_KEEP_ALIAS_SET_P (target) = 1;
6566 store_constructor_field (target, bitsize, bitpos, 0,
6567 bitregion_end, mode, value,
6568 cleared, get_alias_set (elttype),
6569 reverse);
6572 break;
6575 case VECTOR_TYPE:
6577 unsigned HOST_WIDE_INT idx;
6578 constructor_elt *ce;
6579 int i;
6580 int need_to_clear;
6581 int icode = CODE_FOR_nothing;
6582 tree elttype = TREE_TYPE (type);
6583 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6584 machine_mode eltmode = TYPE_MODE (elttype);
6585 HOST_WIDE_INT bitsize;
6586 HOST_WIDE_INT bitpos;
6587 rtvec vector = NULL;
6588 unsigned n_elts;
6589 alias_set_type alias;
6590 bool vec_vec_init_p = false;
6592 gcc_assert (eltmode != BLKmode);
6594 n_elts = TYPE_VECTOR_SUBPARTS (type);
6595 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6597 machine_mode mode = GET_MODE (target);
6598 machine_mode emode = eltmode;
6600 if (CONSTRUCTOR_NELTS (exp)
6601 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6602 == VECTOR_TYPE))
6604 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6605 gcc_assert (CONSTRUCTOR_NELTS (exp) * TYPE_VECTOR_SUBPARTS (etype)
6606 == n_elts);
6607 emode = TYPE_MODE (etype);
6609 icode = (int) convert_optab_handler (vec_init_optab, mode, emode);
6610 if (icode != CODE_FOR_nothing)
6612 unsigned int i, n = n_elts;
6614 if (emode != eltmode)
6616 n = CONSTRUCTOR_NELTS (exp);
6617 vec_vec_init_p = true;
6619 vector = rtvec_alloc (n);
6620 for (i = 0; i < n; i++)
6621 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6625 /* If the constructor has fewer elements than the vector,
6626 clear the whole array first. Similarly if this is static
6627 constructor of a non-BLKmode object. */
6628 if (cleared)
6629 need_to_clear = 0;
6630 else if (REG_P (target) && TREE_STATIC (exp))
6631 need_to_clear = 1;
6632 else
6634 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6635 tree value;
6637 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6639 tree sz = TYPE_SIZE (TREE_TYPE (value));
6640 int n_elts_here
6641 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6642 TYPE_SIZE (elttype)));
6644 count += n_elts_here;
6645 if (mostly_zeros_p (value))
6646 zero_count += n_elts_here;
6649 /* Clear the entire vector first if there are any missing elements,
6650 or if the incidence of zero elements is >= 75%. */
6651 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6654 if (need_to_clear && size > 0 && !vector)
6656 if (REG_P (target))
6657 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6658 else
6659 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6660 cleared = 1;
6663 /* Inform later passes that the old value is dead. */
6664 if (!cleared && !vector && REG_P (target))
6665 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6667 if (MEM_P (target))
6668 alias = MEM_ALIAS_SET (target);
6669 else
6670 alias = get_alias_set (elttype);
6672 /* Store each element of the constructor into the corresponding
6673 element of TARGET, determined by counting the elements. */
6674 for (idx = 0, i = 0;
6675 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6676 idx++, i += bitsize / elt_size)
6678 HOST_WIDE_INT eltpos;
6679 tree value = ce->value;
6681 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6682 if (cleared && initializer_zerop (value))
6683 continue;
6685 if (ce->index)
6686 eltpos = tree_to_uhwi (ce->index);
6687 else
6688 eltpos = i;
6690 if (vector)
6692 if (vec_vec_init_p)
6694 gcc_assert (ce->index == NULL_TREE);
6695 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6696 eltpos = idx;
6698 else
6699 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6700 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6702 else
6704 machine_mode value_mode
6705 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6706 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6707 bitpos = eltpos * elt_size;
6708 store_constructor_field (target, bitsize, bitpos, 0,
6709 bitregion_end, value_mode,
6710 value, cleared, alias, reverse);
6714 if (vector)
6715 emit_insn (GEN_FCN (icode) (target,
6716 gen_rtx_PARALLEL (GET_MODE (target),
6717 vector)));
6718 break;
6721 default:
6722 gcc_unreachable ();
6726 /* Store the value of EXP (an expression tree)
6727 into a subfield of TARGET which has mode MODE and occupies
6728 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6729 If MODE is VOIDmode, it means that we are storing into a bit-field.
6731 BITREGION_START is bitpos of the first bitfield in this region.
6732 BITREGION_END is the bitpos of the ending bitfield in this region.
6733 These two fields are 0, if the C++ memory model does not apply,
6734 or we are not interested in keeping track of bitfield regions.
6736 Always return const0_rtx unless we have something particular to
6737 return.
6739 ALIAS_SET is the alias set for the destination. This value will
6740 (in general) be different from that for TARGET, since TARGET is a
6741 reference to the containing structure.
6743 If NONTEMPORAL is true, try generating a nontemporal store.
6745 If REVERSE is true, the store is to be done in reverse order. */
6747 static rtx
6748 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6749 unsigned HOST_WIDE_INT bitregion_start,
6750 unsigned HOST_WIDE_INT bitregion_end,
6751 machine_mode mode, tree exp,
6752 alias_set_type alias_set, bool nontemporal, bool reverse)
6754 if (TREE_CODE (exp) == ERROR_MARK)
6755 return const0_rtx;
6757 /* If we have nothing to store, do nothing unless the expression has
6758 side-effects. */
6759 if (bitsize == 0)
6760 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6762 if (GET_CODE (target) == CONCAT)
6764 /* We're storing into a struct containing a single __complex. */
6766 gcc_assert (!bitpos);
6767 return store_expr (exp, target, 0, nontemporal, reverse);
6770 /* If the structure is in a register or if the component
6771 is a bit field, we cannot use addressing to access it.
6772 Use bit-field techniques or SUBREG to store in it. */
6774 if (mode == VOIDmode
6775 || (mode != BLKmode && ! direct_store[(int) mode]
6776 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6777 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6778 || REG_P (target)
6779 || GET_CODE (target) == SUBREG
6780 /* If the field isn't aligned enough to store as an ordinary memref,
6781 store it as a bit field. */
6782 || (mode != BLKmode
6783 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6784 || bitpos % GET_MODE_ALIGNMENT (mode))
6785 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6786 || (bitpos % BITS_PER_UNIT != 0)))
6787 || (bitsize >= 0 && mode != BLKmode
6788 && GET_MODE_BITSIZE (mode) > bitsize)
6789 /* If the RHS and field are a constant size and the size of the
6790 RHS isn't the same size as the bitfield, we must use bitfield
6791 operations. */
6792 || (bitsize >= 0
6793 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6794 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6795 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6796 we will handle specially below. */
6797 && !(TREE_CODE (exp) == CONSTRUCTOR
6798 && bitsize % BITS_PER_UNIT == 0)
6799 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6800 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6801 includes some extra padding. store_expr / expand_expr will in
6802 that case call get_inner_reference that will have the bitsize
6803 we check here and thus the block move will not clobber the
6804 padding that shouldn't be clobbered. In the future we could
6805 replace the TREE_ADDRESSABLE check with a check that
6806 get_base_address needs to live in memory. */
6807 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6808 || TREE_CODE (exp) != COMPONENT_REF
6809 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6810 || (bitsize % BITS_PER_UNIT != 0)
6811 || (bitpos % BITS_PER_UNIT != 0)
6812 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6813 != 0)))
6814 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6815 decl we must use bitfield operations. */
6816 || (bitsize >= 0
6817 && TREE_CODE (exp) == MEM_REF
6818 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6819 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6820 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6821 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6823 rtx temp;
6824 gimple *nop_def;
6826 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6827 implies a mask operation. If the precision is the same size as
6828 the field we're storing into, that mask is redundant. This is
6829 particularly common with bit field assignments generated by the
6830 C front end. */
6831 nop_def = get_def_for_expr (exp, NOP_EXPR);
6832 if (nop_def)
6834 tree type = TREE_TYPE (exp);
6835 if (INTEGRAL_TYPE_P (type)
6836 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6837 && bitsize == TYPE_PRECISION (type))
6839 tree op = gimple_assign_rhs1 (nop_def);
6840 type = TREE_TYPE (op);
6841 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6842 exp = op;
6846 temp = expand_normal (exp);
6848 /* Handle calls that return values in multiple non-contiguous locations.
6849 The Irix 6 ABI has examples of this. */
6850 if (GET_CODE (temp) == PARALLEL)
6852 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6853 scalar_int_mode temp_mode
6854 = smallest_int_mode_for_size (size * BITS_PER_UNIT);
6855 rtx temp_target = gen_reg_rtx (temp_mode);
6856 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6857 temp = temp_target;
6860 /* Handle calls that return BLKmode values in registers. */
6861 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6863 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6864 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6865 temp = temp_target;
6868 /* If the value has aggregate type and an integral mode then, if BITSIZE
6869 is narrower than this mode and this is for big-endian data, we first
6870 need to put the value into the low-order bits for store_bit_field,
6871 except when MODE is BLKmode and BITSIZE larger than the word size
6872 (see the handling of fields larger than a word in store_bit_field).
6873 Moreover, the field may be not aligned on a byte boundary; in this
6874 case, if it has reverse storage order, it needs to be accessed as a
6875 scalar field with reverse storage order and we must first put the
6876 value into target order. */
6877 scalar_int_mode temp_mode;
6878 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6879 && is_int_mode (GET_MODE (temp), &temp_mode))
6881 HOST_WIDE_INT size = GET_MODE_BITSIZE (temp_mode);
6883 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6885 if (reverse)
6886 temp = flip_storage_order (temp_mode, temp);
6888 if (bitsize < size
6889 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6890 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6891 temp = expand_shift (RSHIFT_EXPR, temp_mode, temp,
6892 size - bitsize, NULL_RTX, 1);
6895 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6896 if (mode != VOIDmode && mode != BLKmode
6897 && mode != TYPE_MODE (TREE_TYPE (exp)))
6898 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6900 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6901 and BITPOS must be aligned on a byte boundary. If so, we simply do
6902 a block copy. Likewise for a BLKmode-like TARGET. */
6903 if (GET_MODE (temp) == BLKmode
6904 && (GET_MODE (target) == BLKmode
6905 || (MEM_P (target)
6906 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6907 && (bitpos % BITS_PER_UNIT) == 0
6908 && (bitsize % BITS_PER_UNIT) == 0)))
6910 gcc_assert (MEM_P (target) && MEM_P (temp)
6911 && (bitpos % BITS_PER_UNIT) == 0);
6913 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6914 emit_block_move (target, temp,
6915 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6916 / BITS_PER_UNIT),
6917 BLOCK_OP_NORMAL);
6919 return const0_rtx;
6922 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6923 word size, we need to load the value (see again store_bit_field). */
6924 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6926 scalar_int_mode temp_mode = smallest_int_mode_for_size (bitsize);
6927 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6928 temp_mode, false, NULL);
6931 /* Store the value in the bitfield. */
6932 store_bit_field (target, bitsize, bitpos,
6933 bitregion_start, bitregion_end,
6934 mode, temp, reverse);
6936 return const0_rtx;
6938 else
6940 /* Now build a reference to just the desired component. */
6941 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6943 if (to_rtx == target)
6944 to_rtx = copy_rtx (to_rtx);
6946 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6947 set_mem_alias_set (to_rtx, alias_set);
6949 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6950 into a target smaller than its type; handle that case now. */
6951 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6953 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6954 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6955 return to_rtx;
6958 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6962 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6963 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6964 codes and find the ultimate containing object, which we return.
6966 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6967 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6968 storage order of the field.
6969 If the position of the field is variable, we store a tree
6970 giving the variable offset (in units) in *POFFSET.
6971 This offset is in addition to the bit position.
6972 If the position is not variable, we store 0 in *POFFSET.
6974 If any of the extraction expressions is volatile,
6975 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6977 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6978 Otherwise, it is a mode that can be used to access the field.
6980 If the field describes a variable-sized object, *PMODE is set to
6981 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6982 this case, but the address of the object can be found. */
6984 tree
6985 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6986 HOST_WIDE_INT *pbitpos, tree *poffset,
6987 machine_mode *pmode, int *punsignedp,
6988 int *preversep, int *pvolatilep)
6990 tree size_tree = 0;
6991 machine_mode mode = VOIDmode;
6992 bool blkmode_bitfield = false;
6993 tree offset = size_zero_node;
6994 offset_int bit_offset = 0;
6996 /* First get the mode, signedness, storage order and size. We do this from
6997 just the outermost expression. */
6998 *pbitsize = -1;
6999 if (TREE_CODE (exp) == COMPONENT_REF)
7001 tree field = TREE_OPERAND (exp, 1);
7002 size_tree = DECL_SIZE (field);
7003 if (flag_strict_volatile_bitfields > 0
7004 && TREE_THIS_VOLATILE (exp)
7005 && DECL_BIT_FIELD_TYPE (field)
7006 && DECL_MODE (field) != BLKmode)
7007 /* Volatile bitfields should be accessed in the mode of the
7008 field's type, not the mode computed based on the bit
7009 size. */
7010 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7011 else if (!DECL_BIT_FIELD (field))
7012 mode = DECL_MODE (field);
7013 else if (DECL_MODE (field) == BLKmode)
7014 blkmode_bitfield = true;
7016 *punsignedp = DECL_UNSIGNED (field);
7018 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7020 size_tree = TREE_OPERAND (exp, 1);
7021 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7022 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7024 /* For vector types, with the correct size of access, use the mode of
7025 inner type. */
7026 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7027 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7028 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7029 mode = TYPE_MODE (TREE_TYPE (exp));
7031 else
7033 mode = TYPE_MODE (TREE_TYPE (exp));
7034 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7036 if (mode == BLKmode)
7037 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7038 else
7039 *pbitsize = GET_MODE_BITSIZE (mode);
7042 if (size_tree != 0)
7044 if (! tree_fits_uhwi_p (size_tree))
7045 mode = BLKmode, *pbitsize = -1;
7046 else
7047 *pbitsize = tree_to_uhwi (size_tree);
7050 *preversep = reverse_storage_order_for_component_p (exp);
7052 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7053 and find the ultimate containing object. */
7054 while (1)
7056 switch (TREE_CODE (exp))
7058 case BIT_FIELD_REF:
7059 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7060 break;
7062 case COMPONENT_REF:
7064 tree field = TREE_OPERAND (exp, 1);
7065 tree this_offset = component_ref_field_offset (exp);
7067 /* If this field hasn't been filled in yet, don't go past it.
7068 This should only happen when folding expressions made during
7069 type construction. */
7070 if (this_offset == 0)
7071 break;
7073 offset = size_binop (PLUS_EXPR, offset, this_offset);
7074 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7076 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7078 break;
7080 case ARRAY_REF:
7081 case ARRAY_RANGE_REF:
7083 tree index = TREE_OPERAND (exp, 1);
7084 tree low_bound = array_ref_low_bound (exp);
7085 tree unit_size = array_ref_element_size (exp);
7087 /* We assume all arrays have sizes that are a multiple of a byte.
7088 First subtract the lower bound, if any, in the type of the
7089 index, then convert to sizetype and multiply by the size of
7090 the array element. */
7091 if (! integer_zerop (low_bound))
7092 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7093 index, low_bound);
7095 offset = size_binop (PLUS_EXPR, offset,
7096 size_binop (MULT_EXPR,
7097 fold_convert (sizetype, index),
7098 unit_size));
7100 break;
7102 case REALPART_EXPR:
7103 break;
7105 case IMAGPART_EXPR:
7106 bit_offset += *pbitsize;
7107 break;
7109 case VIEW_CONVERT_EXPR:
7110 break;
7112 case MEM_REF:
7113 /* Hand back the decl for MEM[&decl, off]. */
7114 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7116 tree off = TREE_OPERAND (exp, 1);
7117 if (!integer_zerop (off))
7119 offset_int boff, coff = mem_ref_offset (exp);
7120 boff = coff << LOG2_BITS_PER_UNIT;
7121 bit_offset += boff;
7123 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7125 goto done;
7127 default:
7128 goto done;
7131 /* If any reference in the chain is volatile, the effect is volatile. */
7132 if (TREE_THIS_VOLATILE (exp))
7133 *pvolatilep = 1;
7135 exp = TREE_OPERAND (exp, 0);
7137 done:
7139 /* If OFFSET is constant, see if we can return the whole thing as a
7140 constant bit position. Make sure to handle overflow during
7141 this conversion. */
7142 if (TREE_CODE (offset) == INTEGER_CST)
7144 offset_int tem = wi::sext (wi::to_offset (offset),
7145 TYPE_PRECISION (sizetype));
7146 tem <<= LOG2_BITS_PER_UNIT;
7147 tem += bit_offset;
7148 if (wi::fits_shwi_p (tem))
7150 *pbitpos = tem.to_shwi ();
7151 *poffset = offset = NULL_TREE;
7155 /* Otherwise, split it up. */
7156 if (offset)
7158 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7159 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7161 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7162 offset_int tem = bit_offset.and_not (mask);
7163 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7164 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7165 bit_offset -= tem;
7166 tem >>= LOG2_BITS_PER_UNIT;
7167 offset = size_binop (PLUS_EXPR, offset,
7168 wide_int_to_tree (sizetype, tem));
7171 *pbitpos = bit_offset.to_shwi ();
7172 *poffset = offset;
7175 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7176 if (mode == VOIDmode
7177 && blkmode_bitfield
7178 && (*pbitpos % BITS_PER_UNIT) == 0
7179 && (*pbitsize % BITS_PER_UNIT) == 0)
7180 *pmode = BLKmode;
7181 else
7182 *pmode = mode;
7184 return exp;
7187 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7189 static unsigned HOST_WIDE_INT
7190 target_align (const_tree target)
7192 /* We might have a chain of nested references with intermediate misaligning
7193 bitfields components, so need to recurse to find out. */
7195 unsigned HOST_WIDE_INT this_align, outer_align;
7197 switch (TREE_CODE (target))
7199 case BIT_FIELD_REF:
7200 return 1;
7202 case COMPONENT_REF:
7203 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7204 outer_align = target_align (TREE_OPERAND (target, 0));
7205 return MIN (this_align, outer_align);
7207 case ARRAY_REF:
7208 case ARRAY_RANGE_REF:
7209 this_align = TYPE_ALIGN (TREE_TYPE (target));
7210 outer_align = target_align (TREE_OPERAND (target, 0));
7211 return MIN (this_align, outer_align);
7213 CASE_CONVERT:
7214 case NON_LVALUE_EXPR:
7215 case VIEW_CONVERT_EXPR:
7216 this_align = TYPE_ALIGN (TREE_TYPE (target));
7217 outer_align = target_align (TREE_OPERAND (target, 0));
7218 return MAX (this_align, outer_align);
7220 default:
7221 return TYPE_ALIGN (TREE_TYPE (target));
7226 /* Given an rtx VALUE that may contain additions and multiplications, return
7227 an equivalent value that just refers to a register, memory, or constant.
7228 This is done by generating instructions to perform the arithmetic and
7229 returning a pseudo-register containing the value.
7231 The returned value may be a REG, SUBREG, MEM or constant. */
7234 force_operand (rtx value, rtx target)
7236 rtx op1, op2;
7237 /* Use subtarget as the target for operand 0 of a binary operation. */
7238 rtx subtarget = get_subtarget (target);
7239 enum rtx_code code = GET_CODE (value);
7241 /* Check for subreg applied to an expression produced by loop optimizer. */
7242 if (code == SUBREG
7243 && !REG_P (SUBREG_REG (value))
7244 && !MEM_P (SUBREG_REG (value)))
7246 value
7247 = simplify_gen_subreg (GET_MODE (value),
7248 force_reg (GET_MODE (SUBREG_REG (value)),
7249 force_operand (SUBREG_REG (value),
7250 NULL_RTX)),
7251 GET_MODE (SUBREG_REG (value)),
7252 SUBREG_BYTE (value));
7253 code = GET_CODE (value);
7256 /* Check for a PIC address load. */
7257 if ((code == PLUS || code == MINUS)
7258 && XEXP (value, 0) == pic_offset_table_rtx
7259 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7260 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7261 || GET_CODE (XEXP (value, 1)) == CONST))
7263 if (!subtarget)
7264 subtarget = gen_reg_rtx (GET_MODE (value));
7265 emit_move_insn (subtarget, value);
7266 return subtarget;
7269 if (ARITHMETIC_P (value))
7271 op2 = XEXP (value, 1);
7272 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7273 subtarget = 0;
7274 if (code == MINUS && CONST_INT_P (op2))
7276 code = PLUS;
7277 op2 = negate_rtx (GET_MODE (value), op2);
7280 /* Check for an addition with OP2 a constant integer and our first
7281 operand a PLUS of a virtual register and something else. In that
7282 case, we want to emit the sum of the virtual register and the
7283 constant first and then add the other value. This allows virtual
7284 register instantiation to simply modify the constant rather than
7285 creating another one around this addition. */
7286 if (code == PLUS && CONST_INT_P (op2)
7287 && GET_CODE (XEXP (value, 0)) == PLUS
7288 && REG_P (XEXP (XEXP (value, 0), 0))
7289 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7290 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7292 rtx temp = expand_simple_binop (GET_MODE (value), code,
7293 XEXP (XEXP (value, 0), 0), op2,
7294 subtarget, 0, OPTAB_LIB_WIDEN);
7295 return expand_simple_binop (GET_MODE (value), code, temp,
7296 force_operand (XEXP (XEXP (value,
7297 0), 1), 0),
7298 target, 0, OPTAB_LIB_WIDEN);
7301 op1 = force_operand (XEXP (value, 0), subtarget);
7302 op2 = force_operand (op2, NULL_RTX);
7303 switch (code)
7305 case MULT:
7306 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7307 case DIV:
7308 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7309 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7310 target, 1, OPTAB_LIB_WIDEN);
7311 else
7312 return expand_divmod (0,
7313 FLOAT_MODE_P (GET_MODE (value))
7314 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7315 GET_MODE (value), op1, op2, target, 0);
7316 case MOD:
7317 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7318 target, 0);
7319 case UDIV:
7320 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7321 target, 1);
7322 case UMOD:
7323 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7324 target, 1);
7325 case ASHIFTRT:
7326 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7327 target, 0, OPTAB_LIB_WIDEN);
7328 default:
7329 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7330 target, 1, OPTAB_LIB_WIDEN);
7333 if (UNARY_P (value))
7335 if (!target)
7336 target = gen_reg_rtx (GET_MODE (value));
7337 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7338 switch (code)
7340 case ZERO_EXTEND:
7341 case SIGN_EXTEND:
7342 case TRUNCATE:
7343 case FLOAT_EXTEND:
7344 case FLOAT_TRUNCATE:
7345 convert_move (target, op1, code == ZERO_EXTEND);
7346 return target;
7348 case FIX:
7349 case UNSIGNED_FIX:
7350 expand_fix (target, op1, code == UNSIGNED_FIX);
7351 return target;
7353 case FLOAT:
7354 case UNSIGNED_FLOAT:
7355 expand_float (target, op1, code == UNSIGNED_FLOAT);
7356 return target;
7358 default:
7359 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7363 #ifdef INSN_SCHEDULING
7364 /* On machines that have insn scheduling, we want all memory reference to be
7365 explicit, so we need to deal with such paradoxical SUBREGs. */
7366 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7367 value
7368 = simplify_gen_subreg (GET_MODE (value),
7369 force_reg (GET_MODE (SUBREG_REG (value)),
7370 force_operand (SUBREG_REG (value),
7371 NULL_RTX)),
7372 GET_MODE (SUBREG_REG (value)),
7373 SUBREG_BYTE (value));
7374 #endif
7376 return value;
7379 /* Subroutine of expand_expr: return nonzero iff there is no way that
7380 EXP can reference X, which is being modified. TOP_P is nonzero if this
7381 call is going to be used to determine whether we need a temporary
7382 for EXP, as opposed to a recursive call to this function.
7384 It is always safe for this routine to return zero since it merely
7385 searches for optimization opportunities. */
7388 safe_from_p (const_rtx x, tree exp, int top_p)
7390 rtx exp_rtl = 0;
7391 int i, nops;
7393 if (x == 0
7394 /* If EXP has varying size, we MUST use a target since we currently
7395 have no way of allocating temporaries of variable size
7396 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7397 So we assume here that something at a higher level has prevented a
7398 clash. This is somewhat bogus, but the best we can do. Only
7399 do this when X is BLKmode and when we are at the top level. */
7400 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7401 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7402 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7403 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7404 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7405 != INTEGER_CST)
7406 && GET_MODE (x) == BLKmode)
7407 /* If X is in the outgoing argument area, it is always safe. */
7408 || (MEM_P (x)
7409 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7410 || (GET_CODE (XEXP (x, 0)) == PLUS
7411 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7412 return 1;
7414 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7415 find the underlying pseudo. */
7416 if (GET_CODE (x) == SUBREG)
7418 x = SUBREG_REG (x);
7419 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7420 return 0;
7423 /* Now look at our tree code and possibly recurse. */
7424 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7426 case tcc_declaration:
7427 exp_rtl = DECL_RTL_IF_SET (exp);
7428 break;
7430 case tcc_constant:
7431 return 1;
7433 case tcc_exceptional:
7434 if (TREE_CODE (exp) == TREE_LIST)
7436 while (1)
7438 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7439 return 0;
7440 exp = TREE_CHAIN (exp);
7441 if (!exp)
7442 return 1;
7443 if (TREE_CODE (exp) != TREE_LIST)
7444 return safe_from_p (x, exp, 0);
7447 else if (TREE_CODE (exp) == CONSTRUCTOR)
7449 constructor_elt *ce;
7450 unsigned HOST_WIDE_INT idx;
7452 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7453 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7454 || !safe_from_p (x, ce->value, 0))
7455 return 0;
7456 return 1;
7458 else if (TREE_CODE (exp) == ERROR_MARK)
7459 return 1; /* An already-visited SAVE_EXPR? */
7460 else
7461 return 0;
7463 case tcc_statement:
7464 /* The only case we look at here is the DECL_INITIAL inside a
7465 DECL_EXPR. */
7466 return (TREE_CODE (exp) != DECL_EXPR
7467 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7468 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7469 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7471 case tcc_binary:
7472 case tcc_comparison:
7473 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7474 return 0;
7475 /* Fall through. */
7477 case tcc_unary:
7478 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7480 case tcc_expression:
7481 case tcc_reference:
7482 case tcc_vl_exp:
7483 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7484 the expression. If it is set, we conflict iff we are that rtx or
7485 both are in memory. Otherwise, we check all operands of the
7486 expression recursively. */
7488 switch (TREE_CODE (exp))
7490 case ADDR_EXPR:
7491 /* If the operand is static or we are static, we can't conflict.
7492 Likewise if we don't conflict with the operand at all. */
7493 if (staticp (TREE_OPERAND (exp, 0))
7494 || TREE_STATIC (exp)
7495 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7496 return 1;
7498 /* Otherwise, the only way this can conflict is if we are taking
7499 the address of a DECL a that address if part of X, which is
7500 very rare. */
7501 exp = TREE_OPERAND (exp, 0);
7502 if (DECL_P (exp))
7504 if (!DECL_RTL_SET_P (exp)
7505 || !MEM_P (DECL_RTL (exp)))
7506 return 0;
7507 else
7508 exp_rtl = XEXP (DECL_RTL (exp), 0);
7510 break;
7512 case MEM_REF:
7513 if (MEM_P (x)
7514 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7515 get_alias_set (exp)))
7516 return 0;
7517 break;
7519 case CALL_EXPR:
7520 /* Assume that the call will clobber all hard registers and
7521 all of memory. */
7522 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7523 || MEM_P (x))
7524 return 0;
7525 break;
7527 case WITH_CLEANUP_EXPR:
7528 case CLEANUP_POINT_EXPR:
7529 /* Lowered by gimplify.c. */
7530 gcc_unreachable ();
7532 case SAVE_EXPR:
7533 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7535 default:
7536 break;
7539 /* If we have an rtx, we do not need to scan our operands. */
7540 if (exp_rtl)
7541 break;
7543 nops = TREE_OPERAND_LENGTH (exp);
7544 for (i = 0; i < nops; i++)
7545 if (TREE_OPERAND (exp, i) != 0
7546 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7547 return 0;
7549 break;
7551 case tcc_type:
7552 /* Should never get a type here. */
7553 gcc_unreachable ();
7556 /* If we have an rtl, find any enclosed object. Then see if we conflict
7557 with it. */
7558 if (exp_rtl)
7560 if (GET_CODE (exp_rtl) == SUBREG)
7562 exp_rtl = SUBREG_REG (exp_rtl);
7563 if (REG_P (exp_rtl)
7564 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7565 return 0;
7568 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7569 are memory and they conflict. */
7570 return ! (rtx_equal_p (x, exp_rtl)
7571 || (MEM_P (x) && MEM_P (exp_rtl)
7572 && true_dependence (exp_rtl, VOIDmode, x)));
7575 /* If we reach here, it is safe. */
7576 return 1;
7580 /* Return the highest power of two that EXP is known to be a multiple of.
7581 This is used in updating alignment of MEMs in array references. */
7583 unsigned HOST_WIDE_INT
7584 highest_pow2_factor (const_tree exp)
7586 unsigned HOST_WIDE_INT ret;
7587 int trailing_zeros = tree_ctz (exp);
7588 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7589 return BIGGEST_ALIGNMENT;
7590 ret = HOST_WIDE_INT_1U << trailing_zeros;
7591 if (ret > BIGGEST_ALIGNMENT)
7592 return BIGGEST_ALIGNMENT;
7593 return ret;
7596 /* Similar, except that the alignment requirements of TARGET are
7597 taken into account. Assume it is at least as aligned as its
7598 type, unless it is a COMPONENT_REF in which case the layout of
7599 the structure gives the alignment. */
7601 static unsigned HOST_WIDE_INT
7602 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7604 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7605 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7607 return MAX (factor, talign);
7610 /* Convert the tree comparison code TCODE to the rtl one where the
7611 signedness is UNSIGNEDP. */
7613 static enum rtx_code
7614 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7616 enum rtx_code code;
7617 switch (tcode)
7619 case EQ_EXPR:
7620 code = EQ;
7621 break;
7622 case NE_EXPR:
7623 code = NE;
7624 break;
7625 case LT_EXPR:
7626 code = unsignedp ? LTU : LT;
7627 break;
7628 case LE_EXPR:
7629 code = unsignedp ? LEU : LE;
7630 break;
7631 case GT_EXPR:
7632 code = unsignedp ? GTU : GT;
7633 break;
7634 case GE_EXPR:
7635 code = unsignedp ? GEU : GE;
7636 break;
7637 case UNORDERED_EXPR:
7638 code = UNORDERED;
7639 break;
7640 case ORDERED_EXPR:
7641 code = ORDERED;
7642 break;
7643 case UNLT_EXPR:
7644 code = UNLT;
7645 break;
7646 case UNLE_EXPR:
7647 code = UNLE;
7648 break;
7649 case UNGT_EXPR:
7650 code = UNGT;
7651 break;
7652 case UNGE_EXPR:
7653 code = UNGE;
7654 break;
7655 case UNEQ_EXPR:
7656 code = UNEQ;
7657 break;
7658 case LTGT_EXPR:
7659 code = LTGT;
7660 break;
7662 default:
7663 gcc_unreachable ();
7665 return code;
7668 /* Subroutine of expand_expr. Expand the two operands of a binary
7669 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7670 The value may be stored in TARGET if TARGET is nonzero. The
7671 MODIFIER argument is as documented by expand_expr. */
7673 void
7674 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7675 enum expand_modifier modifier)
7677 if (! safe_from_p (target, exp1, 1))
7678 target = 0;
7679 if (operand_equal_p (exp0, exp1, 0))
7681 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7682 *op1 = copy_rtx (*op0);
7684 else
7686 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7687 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7692 /* Return a MEM that contains constant EXP. DEFER is as for
7693 output_constant_def and MODIFIER is as for expand_expr. */
7695 static rtx
7696 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7698 rtx mem;
7700 mem = output_constant_def (exp, defer);
7701 if (modifier != EXPAND_INITIALIZER)
7702 mem = use_anchored_address (mem);
7703 return mem;
7706 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7707 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7709 static rtx
7710 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7711 enum expand_modifier modifier, addr_space_t as)
7713 rtx result, subtarget;
7714 tree inner, offset;
7715 HOST_WIDE_INT bitsize, bitpos;
7716 int unsignedp, reversep, volatilep = 0;
7717 machine_mode mode1;
7719 /* If we are taking the address of a constant and are at the top level,
7720 we have to use output_constant_def since we can't call force_const_mem
7721 at top level. */
7722 /* ??? This should be considered a front-end bug. We should not be
7723 generating ADDR_EXPR of something that isn't an LVALUE. The only
7724 exception here is STRING_CST. */
7725 if (CONSTANT_CLASS_P (exp))
7727 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7728 if (modifier < EXPAND_SUM)
7729 result = force_operand (result, target);
7730 return result;
7733 /* Everything must be something allowed by is_gimple_addressable. */
7734 switch (TREE_CODE (exp))
7736 case INDIRECT_REF:
7737 /* This case will happen via recursion for &a->b. */
7738 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7740 case MEM_REF:
7742 tree tem = TREE_OPERAND (exp, 0);
7743 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7744 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7745 return expand_expr (tem, target, tmode, modifier);
7748 case CONST_DECL:
7749 /* Expand the initializer like constants above. */
7750 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7751 0, modifier), 0);
7752 if (modifier < EXPAND_SUM)
7753 result = force_operand (result, target);
7754 return result;
7756 case REALPART_EXPR:
7757 /* The real part of the complex number is always first, therefore
7758 the address is the same as the address of the parent object. */
7759 offset = 0;
7760 bitpos = 0;
7761 inner = TREE_OPERAND (exp, 0);
7762 break;
7764 case IMAGPART_EXPR:
7765 /* The imaginary part of the complex number is always second.
7766 The expression is therefore always offset by the size of the
7767 scalar type. */
7768 offset = 0;
7769 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7770 inner = TREE_OPERAND (exp, 0);
7771 break;
7773 case COMPOUND_LITERAL_EXPR:
7774 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7775 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7776 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7777 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7778 the initializers aren't gimplified. */
7779 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7780 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7781 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7782 target, tmode, modifier, as);
7783 /* FALLTHRU */
7784 default:
7785 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7786 expand_expr, as that can have various side effects; LABEL_DECLs for
7787 example, may not have their DECL_RTL set yet. Expand the rtl of
7788 CONSTRUCTORs too, which should yield a memory reference for the
7789 constructor's contents. Assume language specific tree nodes can
7790 be expanded in some interesting way. */
7791 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7792 if (DECL_P (exp)
7793 || TREE_CODE (exp) == CONSTRUCTOR
7794 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7796 result = expand_expr (exp, target, tmode,
7797 modifier == EXPAND_INITIALIZER
7798 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7800 /* If the DECL isn't in memory, then the DECL wasn't properly
7801 marked TREE_ADDRESSABLE, which will be either a front-end
7802 or a tree optimizer bug. */
7804 gcc_assert (MEM_P (result));
7805 result = XEXP (result, 0);
7807 /* ??? Is this needed anymore? */
7808 if (DECL_P (exp))
7809 TREE_USED (exp) = 1;
7811 if (modifier != EXPAND_INITIALIZER
7812 && modifier != EXPAND_CONST_ADDRESS
7813 && modifier != EXPAND_SUM)
7814 result = force_operand (result, target);
7815 return result;
7818 /* Pass FALSE as the last argument to get_inner_reference although
7819 we are expanding to RTL. The rationale is that we know how to
7820 handle "aligning nodes" here: we can just bypass them because
7821 they won't change the final object whose address will be returned
7822 (they actually exist only for that purpose). */
7823 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7824 &unsignedp, &reversep, &volatilep);
7825 break;
7828 /* We must have made progress. */
7829 gcc_assert (inner != exp);
7831 subtarget = offset || bitpos ? NULL_RTX : target;
7832 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7833 inner alignment, force the inner to be sufficiently aligned. */
7834 if (CONSTANT_CLASS_P (inner)
7835 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7837 inner = copy_node (inner);
7838 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7839 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7840 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7842 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7844 if (offset)
7846 rtx tmp;
7848 if (modifier != EXPAND_NORMAL)
7849 result = force_operand (result, NULL);
7850 tmp = expand_expr (offset, NULL_RTX, tmode,
7851 modifier == EXPAND_INITIALIZER
7852 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7854 /* expand_expr is allowed to return an object in a mode other
7855 than TMODE. If it did, we need to convert. */
7856 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7857 tmp = convert_modes (tmode, GET_MODE (tmp),
7858 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7859 result = convert_memory_address_addr_space (tmode, result, as);
7860 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7862 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7863 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7864 else
7866 subtarget = bitpos ? NULL_RTX : target;
7867 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7868 1, OPTAB_LIB_WIDEN);
7872 if (bitpos)
7874 /* Someone beforehand should have rejected taking the address
7875 of such an object. */
7876 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7878 result = convert_memory_address_addr_space (tmode, result, as);
7879 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7880 if (modifier < EXPAND_SUM)
7881 result = force_operand (result, target);
7884 return result;
7887 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7888 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7890 static rtx
7891 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7892 enum expand_modifier modifier)
7894 addr_space_t as = ADDR_SPACE_GENERIC;
7895 machine_mode address_mode = Pmode;
7896 machine_mode pointer_mode = ptr_mode;
7897 machine_mode rmode;
7898 rtx result;
7900 /* Target mode of VOIDmode says "whatever's natural". */
7901 if (tmode == VOIDmode)
7902 tmode = TYPE_MODE (TREE_TYPE (exp));
7904 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7906 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7907 address_mode = targetm.addr_space.address_mode (as);
7908 pointer_mode = targetm.addr_space.pointer_mode (as);
7911 /* We can get called with some Weird Things if the user does silliness
7912 like "(short) &a". In that case, convert_memory_address won't do
7913 the right thing, so ignore the given target mode. */
7914 if (tmode != address_mode && tmode != pointer_mode)
7915 tmode = address_mode;
7917 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7918 tmode, modifier, as);
7920 /* Despite expand_expr claims concerning ignoring TMODE when not
7921 strictly convenient, stuff breaks if we don't honor it. Note
7922 that combined with the above, we only do this for pointer modes. */
7923 rmode = GET_MODE (result);
7924 if (rmode == VOIDmode)
7925 rmode = tmode;
7926 if (rmode != tmode)
7927 result = convert_memory_address_addr_space (tmode, result, as);
7929 return result;
7932 /* Generate code for computing CONSTRUCTOR EXP.
7933 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7934 is TRUE, instead of creating a temporary variable in memory
7935 NULL is returned and the caller needs to handle it differently. */
7937 static rtx
7938 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7939 bool avoid_temp_mem)
7941 tree type = TREE_TYPE (exp);
7942 machine_mode mode = TYPE_MODE (type);
7944 /* Try to avoid creating a temporary at all. This is possible
7945 if all of the initializer is zero.
7946 FIXME: try to handle all [0..255] initializers we can handle
7947 with memset. */
7948 if (TREE_STATIC (exp)
7949 && !TREE_ADDRESSABLE (exp)
7950 && target != 0 && mode == BLKmode
7951 && all_zeros_p (exp))
7953 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7954 return target;
7957 /* All elts simple constants => refer to a constant in memory. But
7958 if this is a non-BLKmode mode, let it store a field at a time
7959 since that should make a CONST_INT, CONST_WIDE_INT or
7960 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7961 use, it is best to store directly into the target unless the type
7962 is large enough that memcpy will be used. If we are making an
7963 initializer and all operands are constant, put it in memory as
7964 well.
7966 FIXME: Avoid trying to fill vector constructors piece-meal.
7967 Output them with output_constant_def below unless we're sure
7968 they're zeros. This should go away when vector initializers
7969 are treated like VECTOR_CST instead of arrays. */
7970 if ((TREE_STATIC (exp)
7971 && ((mode == BLKmode
7972 && ! (target != 0 && safe_from_p (target, exp, 1)))
7973 || TREE_ADDRESSABLE (exp)
7974 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7975 && (! can_move_by_pieces
7976 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7977 TYPE_ALIGN (type)))
7978 && ! mostly_zeros_p (exp))))
7979 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7980 && TREE_CONSTANT (exp)))
7982 rtx constructor;
7984 if (avoid_temp_mem)
7985 return NULL_RTX;
7987 constructor = expand_expr_constant (exp, 1, modifier);
7989 if (modifier != EXPAND_CONST_ADDRESS
7990 && modifier != EXPAND_INITIALIZER
7991 && modifier != EXPAND_SUM)
7992 constructor = validize_mem (constructor);
7994 return constructor;
7997 /* Handle calls that pass values in multiple non-contiguous
7998 locations. The Irix 6 ABI has examples of this. */
7999 if (target == 0 || ! safe_from_p (target, exp, 1)
8000 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
8002 if (avoid_temp_mem)
8003 return NULL_RTX;
8005 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8008 store_constructor (exp, target, 0, int_expr_size (exp), false);
8009 return target;
8013 /* expand_expr: generate code for computing expression EXP.
8014 An rtx for the computed value is returned. The value is never null.
8015 In the case of a void EXP, const0_rtx is returned.
8017 The value may be stored in TARGET if TARGET is nonzero.
8018 TARGET is just a suggestion; callers must assume that
8019 the rtx returned may not be the same as TARGET.
8021 If TARGET is CONST0_RTX, it means that the value will be ignored.
8023 If TMODE is not VOIDmode, it suggests generating the
8024 result in mode TMODE. But this is done only when convenient.
8025 Otherwise, TMODE is ignored and the value generated in its natural mode.
8026 TMODE is just a suggestion; callers must assume that
8027 the rtx returned may not have mode TMODE.
8029 Note that TARGET may have neither TMODE nor MODE. In that case, it
8030 probably will not be used.
8032 If MODIFIER is EXPAND_SUM then when EXP is an addition
8033 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8034 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8035 products as above, or REG or MEM, or constant.
8036 Ordinarily in such cases we would output mul or add instructions
8037 and then return a pseudo reg containing the sum.
8039 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8040 it also marks a label as absolutely required (it can't be dead).
8041 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8042 This is used for outputting expressions used in initializers.
8044 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8045 with a constant address even if that address is not normally legitimate.
8046 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8048 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8049 a call parameter. Such targets require special care as we haven't yet
8050 marked TARGET so that it's safe from being trashed by libcalls. We
8051 don't want to use TARGET for anything but the final result;
8052 Intermediate values must go elsewhere. Additionally, calls to
8053 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8055 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8056 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8057 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8058 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8059 recursively.
8061 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8062 In this case, we don't adjust a returned MEM rtx that wouldn't be
8063 sufficiently aligned for its mode; instead, it's up to the caller
8064 to deal with it afterwards. This is used to make sure that unaligned
8065 base objects for which out-of-bounds accesses are supported, for
8066 example record types with trailing arrays, aren't realigned behind
8067 the back of the caller.
8068 The normal operating mode is to pass FALSE for this parameter. */
8071 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8072 enum expand_modifier modifier, rtx *alt_rtl,
8073 bool inner_reference_p)
8075 rtx ret;
8077 /* Handle ERROR_MARK before anybody tries to access its type. */
8078 if (TREE_CODE (exp) == ERROR_MARK
8079 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8081 ret = CONST0_RTX (tmode);
8082 return ret ? ret : const0_rtx;
8085 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8086 inner_reference_p);
8087 return ret;
8090 /* Try to expand the conditional expression which is represented by
8091 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8092 return the rtl reg which represents the result. Otherwise return
8093 NULL_RTX. */
8095 static rtx
8096 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8097 tree treeop1 ATTRIBUTE_UNUSED,
8098 tree treeop2 ATTRIBUTE_UNUSED)
8100 rtx insn;
8101 rtx op00, op01, op1, op2;
8102 enum rtx_code comparison_code;
8103 machine_mode comparison_mode;
8104 gimple *srcstmt;
8105 rtx temp;
8106 tree type = TREE_TYPE (treeop1);
8107 int unsignedp = TYPE_UNSIGNED (type);
8108 machine_mode mode = TYPE_MODE (type);
8109 machine_mode orig_mode = mode;
8110 static bool expanding_cond_expr_using_cmove = false;
8112 /* Conditional move expansion can end up TERing two operands which,
8113 when recursively hitting conditional expressions can result in
8114 exponential behavior if the cmove expansion ultimatively fails.
8115 It's hardly profitable to TER a cmove into a cmove so avoid doing
8116 that by failing early if we end up recursing. */
8117 if (expanding_cond_expr_using_cmove)
8118 return NULL_RTX;
8120 /* If we cannot do a conditional move on the mode, try doing it
8121 with the promoted mode. */
8122 if (!can_conditionally_move_p (mode))
8124 mode = promote_mode (type, mode, &unsignedp);
8125 if (!can_conditionally_move_p (mode))
8126 return NULL_RTX;
8127 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8129 else
8130 temp = assign_temp (type, 0, 1);
8132 expanding_cond_expr_using_cmove = true;
8133 start_sequence ();
8134 expand_operands (treeop1, treeop2,
8135 temp, &op1, &op2, EXPAND_NORMAL);
8137 if (TREE_CODE (treeop0) == SSA_NAME
8138 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8140 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8141 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8142 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8143 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8144 comparison_mode = TYPE_MODE (type);
8145 unsignedp = TYPE_UNSIGNED (type);
8146 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8148 else if (COMPARISON_CLASS_P (treeop0))
8150 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8151 enum tree_code cmpcode = TREE_CODE (treeop0);
8152 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8153 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8154 unsignedp = TYPE_UNSIGNED (type);
8155 comparison_mode = TYPE_MODE (type);
8156 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8158 else
8160 op00 = expand_normal (treeop0);
8161 op01 = const0_rtx;
8162 comparison_code = NE;
8163 comparison_mode = GET_MODE (op00);
8164 if (comparison_mode == VOIDmode)
8165 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8167 expanding_cond_expr_using_cmove = false;
8169 if (GET_MODE (op1) != mode)
8170 op1 = gen_lowpart (mode, op1);
8172 if (GET_MODE (op2) != mode)
8173 op2 = gen_lowpart (mode, op2);
8175 /* Try to emit the conditional move. */
8176 insn = emit_conditional_move (temp, comparison_code,
8177 op00, op01, comparison_mode,
8178 op1, op2, mode,
8179 unsignedp);
8181 /* If we could do the conditional move, emit the sequence,
8182 and return. */
8183 if (insn)
8185 rtx_insn *seq = get_insns ();
8186 end_sequence ();
8187 emit_insn (seq);
8188 return convert_modes (orig_mode, mode, temp, 0);
8191 /* Otherwise discard the sequence and fall back to code with
8192 branches. */
8193 end_sequence ();
8194 return NULL_RTX;
8198 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8199 enum expand_modifier modifier)
8201 rtx op0, op1, op2, temp;
8202 rtx_code_label *lab;
8203 tree type;
8204 int unsignedp;
8205 machine_mode mode;
8206 enum tree_code code = ops->code;
8207 optab this_optab;
8208 rtx subtarget, original_target;
8209 int ignore;
8210 bool reduce_bit_field;
8211 location_t loc = ops->location;
8212 tree treeop0, treeop1, treeop2;
8213 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8214 ? reduce_to_bit_field_precision ((expr), \
8215 target, \
8216 type) \
8217 : (expr))
8219 type = ops->type;
8220 mode = TYPE_MODE (type);
8221 unsignedp = TYPE_UNSIGNED (type);
8223 treeop0 = ops->op0;
8224 treeop1 = ops->op1;
8225 treeop2 = ops->op2;
8227 /* We should be called only on simple (binary or unary) expressions,
8228 exactly those that are valid in gimple expressions that aren't
8229 GIMPLE_SINGLE_RHS (or invalid). */
8230 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8231 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8232 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8234 ignore = (target == const0_rtx
8235 || ((CONVERT_EXPR_CODE_P (code)
8236 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8237 && TREE_CODE (type) == VOID_TYPE));
8239 /* We should be called only if we need the result. */
8240 gcc_assert (!ignore);
8242 /* An operation in what may be a bit-field type needs the
8243 result to be reduced to the precision of the bit-field type,
8244 which is narrower than that of the type's mode. */
8245 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8246 && !type_has_mode_precision_p (type));
8248 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8249 target = 0;
8251 /* Use subtarget as the target for operand 0 of a binary operation. */
8252 subtarget = get_subtarget (target);
8253 original_target = target;
8255 switch (code)
8257 case NON_LVALUE_EXPR:
8258 case PAREN_EXPR:
8259 CASE_CONVERT:
8260 if (treeop0 == error_mark_node)
8261 return const0_rtx;
8263 if (TREE_CODE (type) == UNION_TYPE)
8265 tree valtype = TREE_TYPE (treeop0);
8267 /* If both input and output are BLKmode, this conversion isn't doing
8268 anything except possibly changing memory attribute. */
8269 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8271 rtx result = expand_expr (treeop0, target, tmode,
8272 modifier);
8274 result = copy_rtx (result);
8275 set_mem_attributes (result, type, 0);
8276 return result;
8279 if (target == 0)
8281 if (TYPE_MODE (type) != BLKmode)
8282 target = gen_reg_rtx (TYPE_MODE (type));
8283 else
8284 target = assign_temp (type, 1, 1);
8287 if (MEM_P (target))
8288 /* Store data into beginning of memory target. */
8289 store_expr (treeop0,
8290 adjust_address (target, TYPE_MODE (valtype), 0),
8291 modifier == EXPAND_STACK_PARM,
8292 false, TYPE_REVERSE_STORAGE_ORDER (type));
8294 else
8296 gcc_assert (REG_P (target)
8297 && !TYPE_REVERSE_STORAGE_ORDER (type));
8299 /* Store this field into a union of the proper type. */
8300 store_field (target,
8301 MIN ((int_size_in_bytes (TREE_TYPE
8302 (treeop0))
8303 * BITS_PER_UNIT),
8304 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8305 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8306 false, false);
8309 /* Return the entire union. */
8310 return target;
8313 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8315 op0 = expand_expr (treeop0, target, VOIDmode,
8316 modifier);
8318 /* If the signedness of the conversion differs and OP0 is
8319 a promoted SUBREG, clear that indication since we now
8320 have to do the proper extension. */
8321 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8322 && GET_CODE (op0) == SUBREG)
8323 SUBREG_PROMOTED_VAR_P (op0) = 0;
8325 return REDUCE_BIT_FIELD (op0);
8328 op0 = expand_expr (treeop0, NULL_RTX, mode,
8329 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8330 if (GET_MODE (op0) == mode)
8333 /* If OP0 is a constant, just convert it into the proper mode. */
8334 else if (CONSTANT_P (op0))
8336 tree inner_type = TREE_TYPE (treeop0);
8337 machine_mode inner_mode = GET_MODE (op0);
8339 if (inner_mode == VOIDmode)
8340 inner_mode = TYPE_MODE (inner_type);
8342 if (modifier == EXPAND_INITIALIZER)
8343 op0 = lowpart_subreg (mode, op0, inner_mode);
8344 else
8345 op0= convert_modes (mode, inner_mode, op0,
8346 TYPE_UNSIGNED (inner_type));
8349 else if (modifier == EXPAND_INITIALIZER)
8350 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8351 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8353 else if (target == 0)
8354 op0 = convert_to_mode (mode, op0,
8355 TYPE_UNSIGNED (TREE_TYPE
8356 (treeop0)));
8357 else
8359 convert_move (target, op0,
8360 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8361 op0 = target;
8364 return REDUCE_BIT_FIELD (op0);
8366 case ADDR_SPACE_CONVERT_EXPR:
8368 tree treeop0_type = TREE_TYPE (treeop0);
8370 gcc_assert (POINTER_TYPE_P (type));
8371 gcc_assert (POINTER_TYPE_P (treeop0_type));
8373 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8374 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8376 /* Conversions between pointers to the same address space should
8377 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8378 gcc_assert (as_to != as_from);
8380 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8382 /* Ask target code to handle conversion between pointers
8383 to overlapping address spaces. */
8384 if (targetm.addr_space.subset_p (as_to, as_from)
8385 || targetm.addr_space.subset_p (as_from, as_to))
8387 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8389 else
8391 /* For disjoint address spaces, converting anything but a null
8392 pointer invokes undefined behavior. We truncate or extend the
8393 value as if we'd converted via integers, which handles 0 as
8394 required, and all others as the programmer likely expects. */
8395 #ifndef POINTERS_EXTEND_UNSIGNED
8396 const int POINTERS_EXTEND_UNSIGNED = 1;
8397 #endif
8398 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8399 op0, POINTERS_EXTEND_UNSIGNED);
8401 gcc_assert (op0);
8402 return op0;
8405 case POINTER_PLUS_EXPR:
8406 /* Even though the sizetype mode and the pointer's mode can be different
8407 expand is able to handle this correctly and get the correct result out
8408 of the PLUS_EXPR code. */
8409 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8410 if sizetype precision is smaller than pointer precision. */
8411 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8412 treeop1 = fold_convert_loc (loc, type,
8413 fold_convert_loc (loc, ssizetype,
8414 treeop1));
8415 /* If sizetype precision is larger than pointer precision, truncate the
8416 offset to have matching modes. */
8417 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8418 treeop1 = fold_convert_loc (loc, type, treeop1);
8419 /* FALLTHRU */
8421 case PLUS_EXPR:
8422 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8423 something else, make sure we add the register to the constant and
8424 then to the other thing. This case can occur during strength
8425 reduction and doing it this way will produce better code if the
8426 frame pointer or argument pointer is eliminated.
8428 fold-const.c will ensure that the constant is always in the inner
8429 PLUS_EXPR, so the only case we need to do anything about is if
8430 sp, ap, or fp is our second argument, in which case we must swap
8431 the innermost first argument and our second argument. */
8433 if (TREE_CODE (treeop0) == PLUS_EXPR
8434 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8435 && VAR_P (treeop1)
8436 && (DECL_RTL (treeop1) == frame_pointer_rtx
8437 || DECL_RTL (treeop1) == stack_pointer_rtx
8438 || DECL_RTL (treeop1) == arg_pointer_rtx))
8440 gcc_unreachable ();
8443 /* If the result is to be ptr_mode and we are adding an integer to
8444 something, we might be forming a constant. So try to use
8445 plus_constant. If it produces a sum and we can't accept it,
8446 use force_operand. This allows P = &ARR[const] to generate
8447 efficient code on machines where a SYMBOL_REF is not a valid
8448 address.
8450 If this is an EXPAND_SUM call, always return the sum. */
8451 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8452 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8454 if (modifier == EXPAND_STACK_PARM)
8455 target = 0;
8456 if (TREE_CODE (treeop0) == INTEGER_CST
8457 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8458 && TREE_CONSTANT (treeop1))
8460 rtx constant_part;
8461 HOST_WIDE_INT wc;
8462 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8464 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8465 EXPAND_SUM);
8466 /* Use wi::shwi to ensure that the constant is
8467 truncated according to the mode of OP1, then sign extended
8468 to a HOST_WIDE_INT. Using the constant directly can result
8469 in non-canonical RTL in a 64x32 cross compile. */
8470 wc = TREE_INT_CST_LOW (treeop0);
8471 constant_part =
8472 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8473 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8474 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8475 op1 = force_operand (op1, target);
8476 return REDUCE_BIT_FIELD (op1);
8479 else if (TREE_CODE (treeop1) == INTEGER_CST
8480 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8481 && TREE_CONSTANT (treeop0))
8483 rtx constant_part;
8484 HOST_WIDE_INT wc;
8485 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8487 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8488 (modifier == EXPAND_INITIALIZER
8489 ? EXPAND_INITIALIZER : EXPAND_SUM));
8490 if (! CONSTANT_P (op0))
8492 op1 = expand_expr (treeop1, NULL_RTX,
8493 VOIDmode, modifier);
8494 /* Return a PLUS if modifier says it's OK. */
8495 if (modifier == EXPAND_SUM
8496 || modifier == EXPAND_INITIALIZER)
8497 return simplify_gen_binary (PLUS, mode, op0, op1);
8498 goto binop2;
8500 /* Use wi::shwi to ensure that the constant is
8501 truncated according to the mode of OP1, then sign extended
8502 to a HOST_WIDE_INT. Using the constant directly can result
8503 in non-canonical RTL in a 64x32 cross compile. */
8504 wc = TREE_INT_CST_LOW (treeop1);
8505 constant_part
8506 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8507 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8508 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8509 op0 = force_operand (op0, target);
8510 return REDUCE_BIT_FIELD (op0);
8514 /* Use TER to expand pointer addition of a negated value
8515 as pointer subtraction. */
8516 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8517 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8518 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8519 && TREE_CODE (treeop1) == SSA_NAME
8520 && TYPE_MODE (TREE_TYPE (treeop0))
8521 == TYPE_MODE (TREE_TYPE (treeop1)))
8523 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8524 if (def)
8526 treeop1 = gimple_assign_rhs1 (def);
8527 code = MINUS_EXPR;
8528 goto do_minus;
8532 /* No sense saving up arithmetic to be done
8533 if it's all in the wrong mode to form part of an address.
8534 And force_operand won't know whether to sign-extend or
8535 zero-extend. */
8536 if (modifier != EXPAND_INITIALIZER
8537 && (modifier != EXPAND_SUM || mode != ptr_mode))
8539 expand_operands (treeop0, treeop1,
8540 subtarget, &op0, &op1, modifier);
8541 if (op0 == const0_rtx)
8542 return op1;
8543 if (op1 == const0_rtx)
8544 return op0;
8545 goto binop2;
8548 expand_operands (treeop0, treeop1,
8549 subtarget, &op0, &op1, modifier);
8550 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8552 case MINUS_EXPR:
8553 do_minus:
8554 /* For initializers, we are allowed to return a MINUS of two
8555 symbolic constants. Here we handle all cases when both operands
8556 are constant. */
8557 /* Handle difference of two symbolic constants,
8558 for the sake of an initializer. */
8559 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8560 && really_constant_p (treeop0)
8561 && really_constant_p (treeop1))
8563 expand_operands (treeop0, treeop1,
8564 NULL_RTX, &op0, &op1, modifier);
8566 /* If the last operand is a CONST_INT, use plus_constant of
8567 the negated constant. Else make the MINUS. */
8568 if (CONST_INT_P (op1))
8569 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8570 -INTVAL (op1)));
8571 else
8572 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8575 /* No sense saving up arithmetic to be done
8576 if it's all in the wrong mode to form part of an address.
8577 And force_operand won't know whether to sign-extend or
8578 zero-extend. */
8579 if (modifier != EXPAND_INITIALIZER
8580 && (modifier != EXPAND_SUM || mode != ptr_mode))
8581 goto binop;
8583 expand_operands (treeop0, treeop1,
8584 subtarget, &op0, &op1, modifier);
8586 /* Convert A - const to A + (-const). */
8587 if (CONST_INT_P (op1))
8589 op1 = negate_rtx (mode, op1);
8590 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8593 goto binop2;
8595 case WIDEN_MULT_PLUS_EXPR:
8596 case WIDEN_MULT_MINUS_EXPR:
8597 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8598 op2 = expand_normal (treeop2);
8599 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8600 target, unsignedp);
8601 return target;
8603 case WIDEN_MULT_EXPR:
8604 /* If first operand is constant, swap them.
8605 Thus the following special case checks need only
8606 check the second operand. */
8607 if (TREE_CODE (treeop0) == INTEGER_CST)
8608 std::swap (treeop0, treeop1);
8610 /* First, check if we have a multiplication of one signed and one
8611 unsigned operand. */
8612 if (TREE_CODE (treeop1) != INTEGER_CST
8613 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8614 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8616 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8617 this_optab = usmul_widen_optab;
8618 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8619 != CODE_FOR_nothing)
8621 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8622 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8623 EXPAND_NORMAL);
8624 else
8625 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8626 EXPAND_NORMAL);
8627 /* op0 and op1 might still be constant, despite the above
8628 != INTEGER_CST check. Handle it. */
8629 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8631 op0 = convert_modes (innermode, mode, op0, true);
8632 op1 = convert_modes (innermode, mode, op1, false);
8633 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8634 target, unsignedp));
8636 goto binop3;
8639 /* Check for a multiplication with matching signedness. */
8640 else if ((TREE_CODE (treeop1) == INTEGER_CST
8641 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8642 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8643 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8645 tree op0type = TREE_TYPE (treeop0);
8646 machine_mode innermode = TYPE_MODE (op0type);
8647 bool zextend_p = TYPE_UNSIGNED (op0type);
8648 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8649 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8651 if (TREE_CODE (treeop0) != INTEGER_CST)
8653 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8654 != CODE_FOR_nothing)
8656 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8657 EXPAND_NORMAL);
8658 /* op0 and op1 might still be constant, despite the above
8659 != INTEGER_CST check. Handle it. */
8660 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8662 widen_mult_const:
8663 op0 = convert_modes (innermode, mode, op0, zextend_p);
8665 = convert_modes (innermode, mode, op1,
8666 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8667 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8668 target,
8669 unsignedp));
8671 temp = expand_widening_mult (mode, op0, op1, target,
8672 unsignedp, this_optab);
8673 return REDUCE_BIT_FIELD (temp);
8675 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8676 != CODE_FOR_nothing
8677 && innermode == word_mode)
8679 rtx htem, hipart;
8680 op0 = expand_normal (treeop0);
8681 if (TREE_CODE (treeop1) == INTEGER_CST)
8682 op1 = convert_modes (innermode, mode,
8683 expand_normal (treeop1),
8684 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8685 else
8686 op1 = expand_normal (treeop1);
8687 /* op0 and op1 might still be constant, despite the above
8688 != INTEGER_CST check. Handle it. */
8689 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8690 goto widen_mult_const;
8691 temp = expand_binop (mode, other_optab, op0, op1, target,
8692 unsignedp, OPTAB_LIB_WIDEN);
8693 hipart = gen_highpart (innermode, temp);
8694 htem = expand_mult_highpart_adjust (innermode, hipart,
8695 op0, op1, hipart,
8696 zextend_p);
8697 if (htem != hipart)
8698 emit_move_insn (hipart, htem);
8699 return REDUCE_BIT_FIELD (temp);
8703 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8704 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8705 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8706 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8708 case FMA_EXPR:
8710 optab opt = fma_optab;
8711 gimple *def0, *def2;
8713 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8714 call. */
8715 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8717 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8718 tree call_expr;
8720 gcc_assert (fn != NULL_TREE);
8721 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8722 return expand_builtin (call_expr, target, subtarget, mode, false);
8725 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8726 /* The multiplication is commutative - look at its 2nd operand
8727 if the first isn't fed by a negate. */
8728 if (!def0)
8730 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8731 /* Swap operands if the 2nd operand is fed by a negate. */
8732 if (def0)
8733 std::swap (treeop0, treeop1);
8735 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8737 op0 = op2 = NULL;
8739 if (def0 && def2
8740 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8742 opt = fnms_optab;
8743 op0 = expand_normal (gimple_assign_rhs1 (def0));
8744 op2 = expand_normal (gimple_assign_rhs1 (def2));
8746 else if (def0
8747 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8749 opt = fnma_optab;
8750 op0 = expand_normal (gimple_assign_rhs1 (def0));
8752 else if (def2
8753 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8755 opt = fms_optab;
8756 op2 = expand_normal (gimple_assign_rhs1 (def2));
8759 if (op0 == NULL)
8760 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8761 if (op2 == NULL)
8762 op2 = expand_normal (treeop2);
8763 op1 = expand_normal (treeop1);
8765 return expand_ternary_op (TYPE_MODE (type), opt,
8766 op0, op1, op2, target, 0);
8769 case MULT_EXPR:
8770 /* If this is a fixed-point operation, then we cannot use the code
8771 below because "expand_mult" doesn't support sat/no-sat fixed-point
8772 multiplications. */
8773 if (ALL_FIXED_POINT_MODE_P (mode))
8774 goto binop;
8776 /* If first operand is constant, swap them.
8777 Thus the following special case checks need only
8778 check the second operand. */
8779 if (TREE_CODE (treeop0) == INTEGER_CST)
8780 std::swap (treeop0, treeop1);
8782 /* Attempt to return something suitable for generating an
8783 indexed address, for machines that support that. */
8785 if (modifier == EXPAND_SUM && mode == ptr_mode
8786 && tree_fits_shwi_p (treeop1))
8788 tree exp1 = treeop1;
8790 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8791 EXPAND_SUM);
8793 if (!REG_P (op0))
8794 op0 = force_operand (op0, NULL_RTX);
8795 if (!REG_P (op0))
8796 op0 = copy_to_mode_reg (mode, op0);
8798 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8799 gen_int_mode (tree_to_shwi (exp1),
8800 TYPE_MODE (TREE_TYPE (exp1)))));
8803 if (modifier == EXPAND_STACK_PARM)
8804 target = 0;
8806 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8807 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8809 case TRUNC_MOD_EXPR:
8810 case FLOOR_MOD_EXPR:
8811 case CEIL_MOD_EXPR:
8812 case ROUND_MOD_EXPR:
8814 case TRUNC_DIV_EXPR:
8815 case FLOOR_DIV_EXPR:
8816 case CEIL_DIV_EXPR:
8817 case ROUND_DIV_EXPR:
8818 case EXACT_DIV_EXPR:
8820 /* If this is a fixed-point operation, then we cannot use the code
8821 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8822 divisions. */
8823 if (ALL_FIXED_POINT_MODE_P (mode))
8824 goto binop;
8826 if (modifier == EXPAND_STACK_PARM)
8827 target = 0;
8828 /* Possible optimization: compute the dividend with EXPAND_SUM
8829 then if the divisor is constant can optimize the case
8830 where some terms of the dividend have coeffs divisible by it. */
8831 expand_operands (treeop0, treeop1,
8832 subtarget, &op0, &op1, EXPAND_NORMAL);
8833 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8834 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8835 if (SCALAR_INT_MODE_P (mode)
8836 && optimize >= 2
8837 && get_range_pos_neg (treeop0) == 1
8838 && get_range_pos_neg (treeop1) == 1)
8840 /* If both arguments are known to be positive when interpreted
8841 as signed, we can expand it as both signed and unsigned
8842 division or modulo. Choose the cheaper sequence in that case. */
8843 bool speed_p = optimize_insn_for_speed_p ();
8844 do_pending_stack_adjust ();
8845 start_sequence ();
8846 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8847 rtx_insn *uns_insns = get_insns ();
8848 end_sequence ();
8849 start_sequence ();
8850 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8851 rtx_insn *sgn_insns = get_insns ();
8852 end_sequence ();
8853 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8854 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8856 /* If costs are the same then use as tie breaker the other
8857 other factor. */
8858 if (uns_cost == sgn_cost)
8860 uns_cost = seq_cost (uns_insns, !speed_p);
8861 sgn_cost = seq_cost (sgn_insns, !speed_p);
8864 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8866 emit_insn (uns_insns);
8867 return uns_ret;
8869 emit_insn (sgn_insns);
8870 return sgn_ret;
8872 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8874 case RDIV_EXPR:
8875 goto binop;
8877 case MULT_HIGHPART_EXPR:
8878 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8879 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8880 gcc_assert (temp);
8881 return temp;
8883 case FIXED_CONVERT_EXPR:
8884 op0 = expand_normal (treeop0);
8885 if (target == 0 || modifier == EXPAND_STACK_PARM)
8886 target = gen_reg_rtx (mode);
8888 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8889 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8890 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8891 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8892 else
8893 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8894 return target;
8896 case FIX_TRUNC_EXPR:
8897 op0 = expand_normal (treeop0);
8898 if (target == 0 || modifier == EXPAND_STACK_PARM)
8899 target = gen_reg_rtx (mode);
8900 expand_fix (target, op0, unsignedp);
8901 return target;
8903 case FLOAT_EXPR:
8904 op0 = expand_normal (treeop0);
8905 if (target == 0 || modifier == EXPAND_STACK_PARM)
8906 target = gen_reg_rtx (mode);
8907 /* expand_float can't figure out what to do if FROM has VOIDmode.
8908 So give it the correct mode. With -O, cse will optimize this. */
8909 if (GET_MODE (op0) == VOIDmode)
8910 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8911 op0);
8912 expand_float (target, op0,
8913 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8914 return target;
8916 case NEGATE_EXPR:
8917 op0 = expand_expr (treeop0, subtarget,
8918 VOIDmode, EXPAND_NORMAL);
8919 if (modifier == EXPAND_STACK_PARM)
8920 target = 0;
8921 temp = expand_unop (mode,
8922 optab_for_tree_code (NEGATE_EXPR, type,
8923 optab_default),
8924 op0, target, 0);
8925 gcc_assert (temp);
8926 return REDUCE_BIT_FIELD (temp);
8928 case ABS_EXPR:
8929 op0 = expand_expr (treeop0, subtarget,
8930 VOIDmode, EXPAND_NORMAL);
8931 if (modifier == EXPAND_STACK_PARM)
8932 target = 0;
8934 /* ABS_EXPR is not valid for complex arguments. */
8935 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8936 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8938 /* Unsigned abs is simply the operand. Testing here means we don't
8939 risk generating incorrect code below. */
8940 if (TYPE_UNSIGNED (type))
8941 return op0;
8943 return expand_abs (mode, op0, target, unsignedp,
8944 safe_from_p (target, treeop0, 1));
8946 case MAX_EXPR:
8947 case MIN_EXPR:
8948 target = original_target;
8949 if (target == 0
8950 || modifier == EXPAND_STACK_PARM
8951 || (MEM_P (target) && MEM_VOLATILE_P (target))
8952 || GET_MODE (target) != mode
8953 || (REG_P (target)
8954 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8955 target = gen_reg_rtx (mode);
8956 expand_operands (treeop0, treeop1,
8957 target, &op0, &op1, EXPAND_NORMAL);
8959 /* First try to do it with a special MIN or MAX instruction.
8960 If that does not win, use a conditional jump to select the proper
8961 value. */
8962 this_optab = optab_for_tree_code (code, type, optab_default);
8963 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8964 OPTAB_WIDEN);
8965 if (temp != 0)
8966 return temp;
8968 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8969 and similarly for MAX <x, y>. */
8970 if (VECTOR_TYPE_P (type))
8972 tree t0 = make_tree (type, op0);
8973 tree t1 = make_tree (type, op1);
8974 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
8975 type, t0, t1);
8976 return expand_vec_cond_expr (type, comparison, t0, t1,
8977 original_target);
8980 /* At this point, a MEM target is no longer useful; we will get better
8981 code without it. */
8983 if (! REG_P (target))
8984 target = gen_reg_rtx (mode);
8986 /* If op1 was placed in target, swap op0 and op1. */
8987 if (target != op0 && target == op1)
8988 std::swap (op0, op1);
8990 /* We generate better code and avoid problems with op1 mentioning
8991 target by forcing op1 into a pseudo if it isn't a constant. */
8992 if (! CONSTANT_P (op1))
8993 op1 = force_reg (mode, op1);
8996 enum rtx_code comparison_code;
8997 rtx cmpop1 = op1;
8999 if (code == MAX_EXPR)
9000 comparison_code = unsignedp ? GEU : GE;
9001 else
9002 comparison_code = unsignedp ? LEU : LE;
9004 /* Canonicalize to comparisons against 0. */
9005 if (op1 == const1_rtx)
9007 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9008 or (a != 0 ? a : 1) for unsigned.
9009 For MIN we are safe converting (a <= 1 ? a : 1)
9010 into (a <= 0 ? a : 1) */
9011 cmpop1 = const0_rtx;
9012 if (code == MAX_EXPR)
9013 comparison_code = unsignedp ? NE : GT;
9015 if (op1 == constm1_rtx && !unsignedp)
9017 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9018 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9019 cmpop1 = const0_rtx;
9020 if (code == MIN_EXPR)
9021 comparison_code = LT;
9024 /* Use a conditional move if possible. */
9025 if (can_conditionally_move_p (mode))
9027 rtx insn;
9029 start_sequence ();
9031 /* Try to emit the conditional move. */
9032 insn = emit_conditional_move (target, comparison_code,
9033 op0, cmpop1, mode,
9034 op0, op1, mode,
9035 unsignedp);
9037 /* If we could do the conditional move, emit the sequence,
9038 and return. */
9039 if (insn)
9041 rtx_insn *seq = get_insns ();
9042 end_sequence ();
9043 emit_insn (seq);
9044 return target;
9047 /* Otherwise discard the sequence and fall back to code with
9048 branches. */
9049 end_sequence ();
9052 if (target != op0)
9053 emit_move_insn (target, op0);
9055 lab = gen_label_rtx ();
9056 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9057 unsignedp, mode, NULL_RTX, NULL, lab,
9058 profile_probability::uninitialized ());
9060 emit_move_insn (target, op1);
9061 emit_label (lab);
9062 return target;
9064 case BIT_NOT_EXPR:
9065 op0 = expand_expr (treeop0, subtarget,
9066 VOIDmode, EXPAND_NORMAL);
9067 if (modifier == EXPAND_STACK_PARM)
9068 target = 0;
9069 /* In case we have to reduce the result to bitfield precision
9070 for unsigned bitfield expand this as XOR with a proper constant
9071 instead. */
9072 if (reduce_bit_field && TYPE_UNSIGNED (type))
9074 wide_int mask = wi::mask (TYPE_PRECISION (type),
9075 false, GET_MODE_PRECISION (mode));
9077 temp = expand_binop (mode, xor_optab, op0,
9078 immed_wide_int_const (mask, mode),
9079 target, 1, OPTAB_LIB_WIDEN);
9081 else
9082 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9083 gcc_assert (temp);
9084 return temp;
9086 /* ??? Can optimize bitwise operations with one arg constant.
9087 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9088 and (a bitwise1 b) bitwise2 b (etc)
9089 but that is probably not worth while. */
9091 case BIT_AND_EXPR:
9092 case BIT_IOR_EXPR:
9093 case BIT_XOR_EXPR:
9094 goto binop;
9096 case LROTATE_EXPR:
9097 case RROTATE_EXPR:
9098 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9099 || type_has_mode_precision_p (type));
9100 /* fall through */
9102 case LSHIFT_EXPR:
9103 case RSHIFT_EXPR:
9105 /* If this is a fixed-point operation, then we cannot use the code
9106 below because "expand_shift" doesn't support sat/no-sat fixed-point
9107 shifts. */
9108 if (ALL_FIXED_POINT_MODE_P (mode))
9109 goto binop;
9111 if (! safe_from_p (subtarget, treeop1, 1))
9112 subtarget = 0;
9113 if (modifier == EXPAND_STACK_PARM)
9114 target = 0;
9115 op0 = expand_expr (treeop0, subtarget,
9116 VOIDmode, EXPAND_NORMAL);
9118 /* Left shift optimization when shifting across word_size boundary.
9120 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9121 there isn't native instruction to support this wide mode
9122 left shift. Given below scenario:
9124 Type A = (Type) B << C
9126 |< T >|
9127 | dest_high | dest_low |
9129 | word_size |
9131 If the shift amount C caused we shift B to across the word
9132 size boundary, i.e part of B shifted into high half of
9133 destination register, and part of B remains in the low
9134 half, then GCC will use the following left shift expand
9135 logic:
9137 1. Initialize dest_low to B.
9138 2. Initialize every bit of dest_high to the sign bit of B.
9139 3. Logic left shift dest_low by C bit to finalize dest_low.
9140 The value of dest_low before this shift is kept in a temp D.
9141 4. Logic left shift dest_high by C.
9142 5. Logic right shift D by (word_size - C).
9143 6. Or the result of 4 and 5 to finalize dest_high.
9145 While, by checking gimple statements, if operand B is
9146 coming from signed extension, then we can simplify above
9147 expand logic into:
9149 1. dest_high = src_low >> (word_size - C).
9150 2. dest_low = src_low << C.
9152 We can use one arithmetic right shift to finish all the
9153 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9154 needed from 6 into 2.
9156 The case is similar for zero extension, except that we
9157 initialize dest_high to zero rather than copies of the sign
9158 bit from B. Furthermore, we need to use a logical right shift
9159 in this case.
9161 The choice of sign-extension versus zero-extension is
9162 determined entirely by whether or not B is signed and is
9163 independent of the current setting of unsignedp. */
9165 temp = NULL_RTX;
9166 if (code == LSHIFT_EXPR
9167 && target
9168 && REG_P (target)
9169 && mode == GET_MODE_WIDER_MODE (word_mode).else_void ()
9170 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
9171 && TREE_CONSTANT (treeop1)
9172 && TREE_CODE (treeop0) == SSA_NAME)
9174 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9175 if (is_gimple_assign (def)
9176 && gimple_assign_rhs_code (def) == NOP_EXPR)
9178 machine_mode rmode = TYPE_MODE
9179 (TREE_TYPE (gimple_assign_rhs1 (def)));
9181 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
9182 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9183 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9184 >= GET_MODE_BITSIZE (word_mode)))
9186 rtx_insn *seq, *seq_old;
9187 unsigned int high_off = subreg_highpart_offset (word_mode,
9188 mode);
9189 bool extend_unsigned
9190 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9191 rtx low = lowpart_subreg (word_mode, op0, mode);
9192 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9193 rtx dest_high = simplify_gen_subreg (word_mode, target,
9194 mode, high_off);
9195 HOST_WIDE_INT ramount = (BITS_PER_WORD
9196 - TREE_INT_CST_LOW (treeop1));
9197 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9199 start_sequence ();
9200 /* dest_high = src_low >> (word_size - C). */
9201 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9202 rshift, dest_high,
9203 extend_unsigned);
9204 if (temp != dest_high)
9205 emit_move_insn (dest_high, temp);
9207 /* dest_low = src_low << C. */
9208 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9209 treeop1, dest_low, unsignedp);
9210 if (temp != dest_low)
9211 emit_move_insn (dest_low, temp);
9213 seq = get_insns ();
9214 end_sequence ();
9215 temp = target ;
9217 if (have_insn_for (ASHIFT, mode))
9219 bool speed_p = optimize_insn_for_speed_p ();
9220 start_sequence ();
9221 rtx ret_old = expand_variable_shift (code, mode, op0,
9222 treeop1, target,
9223 unsignedp);
9225 seq_old = get_insns ();
9226 end_sequence ();
9227 if (seq_cost (seq, speed_p)
9228 >= seq_cost (seq_old, speed_p))
9230 seq = seq_old;
9231 temp = ret_old;
9234 emit_insn (seq);
9239 if (temp == NULL_RTX)
9240 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9241 unsignedp);
9242 if (code == LSHIFT_EXPR)
9243 temp = REDUCE_BIT_FIELD (temp);
9244 return temp;
9247 /* Could determine the answer when only additive constants differ. Also,
9248 the addition of one can be handled by changing the condition. */
9249 case LT_EXPR:
9250 case LE_EXPR:
9251 case GT_EXPR:
9252 case GE_EXPR:
9253 case EQ_EXPR:
9254 case NE_EXPR:
9255 case UNORDERED_EXPR:
9256 case ORDERED_EXPR:
9257 case UNLT_EXPR:
9258 case UNLE_EXPR:
9259 case UNGT_EXPR:
9260 case UNGE_EXPR:
9261 case UNEQ_EXPR:
9262 case LTGT_EXPR:
9264 temp = do_store_flag (ops,
9265 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9266 tmode != VOIDmode ? tmode : mode);
9267 if (temp)
9268 return temp;
9270 /* Use a compare and a jump for BLKmode comparisons, or for function
9271 type comparisons is have_canonicalize_funcptr_for_compare. */
9273 if ((target == 0
9274 || modifier == EXPAND_STACK_PARM
9275 || ! safe_from_p (target, treeop0, 1)
9276 || ! safe_from_p (target, treeop1, 1)
9277 /* Make sure we don't have a hard reg (such as function's return
9278 value) live across basic blocks, if not optimizing. */
9279 || (!optimize && REG_P (target)
9280 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9281 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9283 emit_move_insn (target, const0_rtx);
9285 rtx_code_label *lab1 = gen_label_rtx ();
9286 jumpifnot_1 (code, treeop0, treeop1, lab1,
9287 profile_probability::uninitialized ());
9289 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9290 emit_move_insn (target, constm1_rtx);
9291 else
9292 emit_move_insn (target, const1_rtx);
9294 emit_label (lab1);
9295 return target;
9297 case COMPLEX_EXPR:
9298 /* Get the rtx code of the operands. */
9299 op0 = expand_normal (treeop0);
9300 op1 = expand_normal (treeop1);
9302 if (!target)
9303 target = gen_reg_rtx (TYPE_MODE (type));
9304 else
9305 /* If target overlaps with op1, then either we need to force
9306 op1 into a pseudo (if target also overlaps with op0),
9307 or write the complex parts in reverse order. */
9308 switch (GET_CODE (target))
9310 case CONCAT:
9311 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9313 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9315 complex_expr_force_op1:
9316 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9317 emit_move_insn (temp, op1);
9318 op1 = temp;
9319 break;
9321 complex_expr_swap_order:
9322 /* Move the imaginary (op1) and real (op0) parts to their
9323 location. */
9324 write_complex_part (target, op1, true);
9325 write_complex_part (target, op0, false);
9327 return target;
9329 break;
9330 case MEM:
9331 temp = adjust_address_nv (target,
9332 GET_MODE_INNER (GET_MODE (target)), 0);
9333 if (reg_overlap_mentioned_p (temp, op1))
9335 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9336 temp = adjust_address_nv (target, imode,
9337 GET_MODE_SIZE (imode));
9338 if (reg_overlap_mentioned_p (temp, op0))
9339 goto complex_expr_force_op1;
9340 goto complex_expr_swap_order;
9342 break;
9343 default:
9344 if (reg_overlap_mentioned_p (target, op1))
9346 if (reg_overlap_mentioned_p (target, op0))
9347 goto complex_expr_force_op1;
9348 goto complex_expr_swap_order;
9350 break;
9353 /* Move the real (op0) and imaginary (op1) parts to their location. */
9354 write_complex_part (target, op0, false);
9355 write_complex_part (target, op1, true);
9357 return target;
9359 case WIDEN_SUM_EXPR:
9361 tree oprnd0 = treeop0;
9362 tree oprnd1 = treeop1;
9364 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9365 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9366 target, unsignedp);
9367 return target;
9370 case REDUC_MAX_EXPR:
9371 case REDUC_MIN_EXPR:
9372 case REDUC_PLUS_EXPR:
9374 op0 = expand_normal (treeop0);
9375 this_optab = optab_for_tree_code (code, type, optab_default);
9376 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9378 struct expand_operand ops[2];
9379 enum insn_code icode = optab_handler (this_optab, vec_mode);
9381 create_output_operand (&ops[0], target, mode);
9382 create_input_operand (&ops[1], op0, vec_mode);
9383 expand_insn (icode, 2, ops);
9384 target = ops[0].value;
9385 if (GET_MODE (target) != mode)
9386 return gen_lowpart (tmode, target);
9387 return target;
9390 case VEC_UNPACK_HI_EXPR:
9391 case VEC_UNPACK_LO_EXPR:
9393 op0 = expand_normal (treeop0);
9394 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9395 target, unsignedp);
9396 gcc_assert (temp);
9397 return temp;
9400 case VEC_UNPACK_FLOAT_HI_EXPR:
9401 case VEC_UNPACK_FLOAT_LO_EXPR:
9403 op0 = expand_normal (treeop0);
9404 /* The signedness is determined from input operand. */
9405 temp = expand_widen_pattern_expr
9406 (ops, op0, NULL_RTX, NULL_RTX,
9407 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9409 gcc_assert (temp);
9410 return temp;
9413 case VEC_WIDEN_MULT_HI_EXPR:
9414 case VEC_WIDEN_MULT_LO_EXPR:
9415 case VEC_WIDEN_MULT_EVEN_EXPR:
9416 case VEC_WIDEN_MULT_ODD_EXPR:
9417 case VEC_WIDEN_LSHIFT_HI_EXPR:
9418 case VEC_WIDEN_LSHIFT_LO_EXPR:
9419 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9420 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9421 target, unsignedp);
9422 gcc_assert (target);
9423 return target;
9425 case VEC_PACK_TRUNC_EXPR:
9426 case VEC_PACK_SAT_EXPR:
9427 case VEC_PACK_FIX_TRUNC_EXPR:
9428 mode = TYPE_MODE (TREE_TYPE (treeop0));
9429 goto binop;
9431 case VEC_PERM_EXPR:
9432 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9433 op2 = expand_normal (treeop2);
9435 /* Careful here: if the target doesn't support integral vector modes,
9436 a constant selection vector could wind up smooshed into a normal
9437 integral constant. */
9438 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9440 tree sel_type = TREE_TYPE (treeop2);
9441 machine_mode vmode
9442 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9443 TYPE_VECTOR_SUBPARTS (sel_type));
9444 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9445 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9446 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9448 else
9449 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9451 temp = expand_vec_perm (mode, op0, op1, op2, target);
9452 gcc_assert (temp);
9453 return temp;
9455 case DOT_PROD_EXPR:
9457 tree oprnd0 = treeop0;
9458 tree oprnd1 = treeop1;
9459 tree oprnd2 = treeop2;
9460 rtx op2;
9462 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9463 op2 = expand_normal (oprnd2);
9464 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9465 target, unsignedp);
9466 return target;
9469 case SAD_EXPR:
9471 tree oprnd0 = treeop0;
9472 tree oprnd1 = treeop1;
9473 tree oprnd2 = treeop2;
9474 rtx op2;
9476 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9477 op2 = expand_normal (oprnd2);
9478 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9479 target, unsignedp);
9480 return target;
9483 case REALIGN_LOAD_EXPR:
9485 tree oprnd0 = treeop0;
9486 tree oprnd1 = treeop1;
9487 tree oprnd2 = treeop2;
9488 rtx op2;
9490 this_optab = optab_for_tree_code (code, type, optab_default);
9491 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9492 op2 = expand_normal (oprnd2);
9493 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9494 target, unsignedp);
9495 gcc_assert (temp);
9496 return temp;
9499 case COND_EXPR:
9501 /* A COND_EXPR with its type being VOID_TYPE represents a
9502 conditional jump and is handled in
9503 expand_gimple_cond_expr. */
9504 gcc_assert (!VOID_TYPE_P (type));
9506 /* Note that COND_EXPRs whose type is a structure or union
9507 are required to be constructed to contain assignments of
9508 a temporary variable, so that we can evaluate them here
9509 for side effect only. If type is void, we must do likewise. */
9511 gcc_assert (!TREE_ADDRESSABLE (type)
9512 && !ignore
9513 && TREE_TYPE (treeop1) != void_type_node
9514 && TREE_TYPE (treeop2) != void_type_node);
9516 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9517 if (temp)
9518 return temp;
9520 /* If we are not to produce a result, we have no target. Otherwise,
9521 if a target was specified use it; it will not be used as an
9522 intermediate target unless it is safe. If no target, use a
9523 temporary. */
9525 if (modifier != EXPAND_STACK_PARM
9526 && original_target
9527 && safe_from_p (original_target, treeop0, 1)
9528 && GET_MODE (original_target) == mode
9529 && !MEM_P (original_target))
9530 temp = original_target;
9531 else
9532 temp = assign_temp (type, 0, 1);
9534 do_pending_stack_adjust ();
9535 NO_DEFER_POP;
9536 rtx_code_label *lab0 = gen_label_rtx ();
9537 rtx_code_label *lab1 = gen_label_rtx ();
9538 jumpifnot (treeop0, lab0,
9539 profile_probability::uninitialized ());
9540 store_expr (treeop1, temp,
9541 modifier == EXPAND_STACK_PARM,
9542 false, false);
9544 emit_jump_insn (targetm.gen_jump (lab1));
9545 emit_barrier ();
9546 emit_label (lab0);
9547 store_expr (treeop2, temp,
9548 modifier == EXPAND_STACK_PARM,
9549 false, false);
9551 emit_label (lab1);
9552 OK_DEFER_POP;
9553 return temp;
9556 case VEC_COND_EXPR:
9557 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9558 return target;
9560 case BIT_INSERT_EXPR:
9562 unsigned bitpos = tree_to_uhwi (treeop2);
9563 unsigned bitsize;
9564 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9565 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9566 else
9567 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9568 rtx op0 = expand_normal (treeop0);
9569 rtx op1 = expand_normal (treeop1);
9570 rtx dst = gen_reg_rtx (mode);
9571 emit_move_insn (dst, op0);
9572 store_bit_field (dst, bitsize, bitpos, 0, 0,
9573 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9574 return dst;
9577 default:
9578 gcc_unreachable ();
9581 /* Here to do an ordinary binary operator. */
9582 binop:
9583 expand_operands (treeop0, treeop1,
9584 subtarget, &op0, &op1, EXPAND_NORMAL);
9585 binop2:
9586 this_optab = optab_for_tree_code (code, type, optab_default);
9587 binop3:
9588 if (modifier == EXPAND_STACK_PARM)
9589 target = 0;
9590 temp = expand_binop (mode, this_optab, op0, op1, target,
9591 unsignedp, OPTAB_LIB_WIDEN);
9592 gcc_assert (temp);
9593 /* Bitwise operations do not need bitfield reduction as we expect their
9594 operands being properly truncated. */
9595 if (code == BIT_XOR_EXPR
9596 || code == BIT_AND_EXPR
9597 || code == BIT_IOR_EXPR)
9598 return temp;
9599 return REDUCE_BIT_FIELD (temp);
9601 #undef REDUCE_BIT_FIELD
9604 /* Return TRUE if expression STMT is suitable for replacement.
9605 Never consider memory loads as replaceable, because those don't ever lead
9606 into constant expressions. */
9608 static bool
9609 stmt_is_replaceable_p (gimple *stmt)
9611 if (ssa_is_replaceable_p (stmt))
9613 /* Don't move around loads. */
9614 if (!gimple_assign_single_p (stmt)
9615 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9616 return true;
9618 return false;
9622 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9623 enum expand_modifier modifier, rtx *alt_rtl,
9624 bool inner_reference_p)
9626 rtx op0, op1, temp, decl_rtl;
9627 tree type;
9628 int unsignedp;
9629 machine_mode mode, dmode;
9630 enum tree_code code = TREE_CODE (exp);
9631 rtx subtarget, original_target;
9632 int ignore;
9633 tree context;
9634 bool reduce_bit_field;
9635 location_t loc = EXPR_LOCATION (exp);
9636 struct separate_ops ops;
9637 tree treeop0, treeop1, treeop2;
9638 tree ssa_name = NULL_TREE;
9639 gimple *g;
9641 type = TREE_TYPE (exp);
9642 mode = TYPE_MODE (type);
9643 unsignedp = TYPE_UNSIGNED (type);
9645 treeop0 = treeop1 = treeop2 = NULL_TREE;
9646 if (!VL_EXP_CLASS_P (exp))
9647 switch (TREE_CODE_LENGTH (code))
9649 default:
9650 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9651 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9652 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9653 case 0: break;
9655 ops.code = code;
9656 ops.type = type;
9657 ops.op0 = treeop0;
9658 ops.op1 = treeop1;
9659 ops.op2 = treeop2;
9660 ops.location = loc;
9662 ignore = (target == const0_rtx
9663 || ((CONVERT_EXPR_CODE_P (code)
9664 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9665 && TREE_CODE (type) == VOID_TYPE));
9667 /* An operation in what may be a bit-field type needs the
9668 result to be reduced to the precision of the bit-field type,
9669 which is narrower than that of the type's mode. */
9670 reduce_bit_field = (!ignore
9671 && INTEGRAL_TYPE_P (type)
9672 && !type_has_mode_precision_p (type));
9674 /* If we are going to ignore this result, we need only do something
9675 if there is a side-effect somewhere in the expression. If there
9676 is, short-circuit the most common cases here. Note that we must
9677 not call expand_expr with anything but const0_rtx in case this
9678 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9680 if (ignore)
9682 if (! TREE_SIDE_EFFECTS (exp))
9683 return const0_rtx;
9685 /* Ensure we reference a volatile object even if value is ignored, but
9686 don't do this if all we are doing is taking its address. */
9687 if (TREE_THIS_VOLATILE (exp)
9688 && TREE_CODE (exp) != FUNCTION_DECL
9689 && mode != VOIDmode && mode != BLKmode
9690 && modifier != EXPAND_CONST_ADDRESS)
9692 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9693 if (MEM_P (temp))
9694 copy_to_reg (temp);
9695 return const0_rtx;
9698 if (TREE_CODE_CLASS (code) == tcc_unary
9699 || code == BIT_FIELD_REF
9700 || code == COMPONENT_REF
9701 || code == INDIRECT_REF)
9702 return expand_expr (treeop0, const0_rtx, VOIDmode,
9703 modifier);
9705 else if (TREE_CODE_CLASS (code) == tcc_binary
9706 || TREE_CODE_CLASS (code) == tcc_comparison
9707 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9709 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9710 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9711 return const0_rtx;
9714 target = 0;
9717 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9718 target = 0;
9720 /* Use subtarget as the target for operand 0 of a binary operation. */
9721 subtarget = get_subtarget (target);
9722 original_target = target;
9724 switch (code)
9726 case LABEL_DECL:
9728 tree function = decl_function_context (exp);
9730 temp = label_rtx (exp);
9731 temp = gen_rtx_LABEL_REF (Pmode, temp);
9733 if (function != current_function_decl
9734 && function != 0)
9735 LABEL_REF_NONLOCAL_P (temp) = 1;
9737 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9738 return temp;
9741 case SSA_NAME:
9742 /* ??? ivopts calls expander, without any preparation from
9743 out-of-ssa. So fake instructions as if this was an access to the
9744 base variable. This unnecessarily allocates a pseudo, see how we can
9745 reuse it, if partition base vars have it set already. */
9746 if (!currently_expanding_to_rtl)
9748 tree var = SSA_NAME_VAR (exp);
9749 if (var && DECL_RTL_SET_P (var))
9750 return DECL_RTL (var);
9751 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9752 LAST_VIRTUAL_REGISTER + 1);
9755 g = get_gimple_for_ssa_name (exp);
9756 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9757 if (g == NULL
9758 && modifier == EXPAND_INITIALIZER
9759 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9760 && (optimize || !SSA_NAME_VAR (exp)
9761 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9762 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9763 g = SSA_NAME_DEF_STMT (exp);
9764 if (g)
9766 rtx r;
9767 location_t saved_loc = curr_insn_location ();
9768 location_t loc = gimple_location (g);
9769 if (loc != UNKNOWN_LOCATION)
9770 set_curr_insn_location (loc);
9771 ops.code = gimple_assign_rhs_code (g);
9772 switch (get_gimple_rhs_class (ops.code))
9774 case GIMPLE_TERNARY_RHS:
9775 ops.op2 = gimple_assign_rhs3 (g);
9776 /* Fallthru */
9777 case GIMPLE_BINARY_RHS:
9778 ops.op1 = gimple_assign_rhs2 (g);
9780 /* Try to expand conditonal compare. */
9781 if (targetm.gen_ccmp_first)
9783 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9784 r = expand_ccmp_expr (g, mode);
9785 if (r)
9786 break;
9788 /* Fallthru */
9789 case GIMPLE_UNARY_RHS:
9790 ops.op0 = gimple_assign_rhs1 (g);
9791 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9792 ops.location = loc;
9793 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9794 break;
9795 case GIMPLE_SINGLE_RHS:
9797 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9798 tmode, modifier, alt_rtl,
9799 inner_reference_p);
9800 break;
9802 default:
9803 gcc_unreachable ();
9805 set_curr_insn_location (saved_loc);
9806 if (REG_P (r) && !REG_EXPR (r))
9807 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9808 return r;
9811 ssa_name = exp;
9812 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9813 exp = SSA_NAME_VAR (ssa_name);
9814 goto expand_decl_rtl;
9816 case PARM_DECL:
9817 case VAR_DECL:
9818 /* If a static var's type was incomplete when the decl was written,
9819 but the type is complete now, lay out the decl now. */
9820 if (DECL_SIZE (exp) == 0
9821 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9822 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9823 layout_decl (exp, 0);
9825 /* fall through */
9827 case FUNCTION_DECL:
9828 case RESULT_DECL:
9829 decl_rtl = DECL_RTL (exp);
9830 expand_decl_rtl:
9831 gcc_assert (decl_rtl);
9833 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9834 settings for VECTOR_TYPE_P that might switch for the function. */
9835 if (currently_expanding_to_rtl
9836 && code == VAR_DECL && MEM_P (decl_rtl)
9837 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9838 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9839 else
9840 decl_rtl = copy_rtx (decl_rtl);
9842 /* Record writes to register variables. */
9843 if (modifier == EXPAND_WRITE
9844 && REG_P (decl_rtl)
9845 && HARD_REGISTER_P (decl_rtl))
9846 add_to_hard_reg_set (&crtl->asm_clobbers,
9847 GET_MODE (decl_rtl), REGNO (decl_rtl));
9849 /* Ensure variable marked as used even if it doesn't go through
9850 a parser. If it hasn't be used yet, write out an external
9851 definition. */
9852 if (exp)
9853 TREE_USED (exp) = 1;
9855 /* Show we haven't gotten RTL for this yet. */
9856 temp = 0;
9858 /* Variables inherited from containing functions should have
9859 been lowered by this point. */
9860 if (exp)
9861 context = decl_function_context (exp);
9862 gcc_assert (!exp
9863 || SCOPE_FILE_SCOPE_P (context)
9864 || context == current_function_decl
9865 || TREE_STATIC (exp)
9866 || DECL_EXTERNAL (exp)
9867 /* ??? C++ creates functions that are not TREE_STATIC. */
9868 || TREE_CODE (exp) == FUNCTION_DECL);
9870 /* This is the case of an array whose size is to be determined
9871 from its initializer, while the initializer is still being parsed.
9872 ??? We aren't parsing while expanding anymore. */
9874 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9875 temp = validize_mem (decl_rtl);
9877 /* If DECL_RTL is memory, we are in the normal case and the
9878 address is not valid, get the address into a register. */
9880 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9882 if (alt_rtl)
9883 *alt_rtl = decl_rtl;
9884 decl_rtl = use_anchored_address (decl_rtl);
9885 if (modifier != EXPAND_CONST_ADDRESS
9886 && modifier != EXPAND_SUM
9887 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9888 : GET_MODE (decl_rtl),
9889 XEXP (decl_rtl, 0),
9890 MEM_ADDR_SPACE (decl_rtl)))
9891 temp = replace_equiv_address (decl_rtl,
9892 copy_rtx (XEXP (decl_rtl, 0)));
9895 /* If we got something, return it. But first, set the alignment
9896 if the address is a register. */
9897 if (temp != 0)
9899 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9900 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9902 return temp;
9905 if (exp)
9906 dmode = DECL_MODE (exp);
9907 else
9908 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9910 /* If the mode of DECL_RTL does not match that of the decl,
9911 there are two cases: we are dealing with a BLKmode value
9912 that is returned in a register, or we are dealing with
9913 a promoted value. In the latter case, return a SUBREG
9914 of the wanted mode, but mark it so that we know that it
9915 was already extended. */
9916 if (REG_P (decl_rtl)
9917 && dmode != BLKmode
9918 && GET_MODE (decl_rtl) != dmode)
9920 machine_mode pmode;
9922 /* Get the signedness to be used for this variable. Ensure we get
9923 the same mode we got when the variable was declared. */
9924 if (code != SSA_NAME)
9925 pmode = promote_decl_mode (exp, &unsignedp);
9926 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9927 && gimple_code (g) == GIMPLE_CALL
9928 && !gimple_call_internal_p (g))
9929 pmode = promote_function_mode (type, mode, &unsignedp,
9930 gimple_call_fntype (g),
9932 else
9933 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9934 gcc_assert (GET_MODE (decl_rtl) == pmode);
9936 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9937 SUBREG_PROMOTED_VAR_P (temp) = 1;
9938 SUBREG_PROMOTED_SET (temp, unsignedp);
9939 return temp;
9942 return decl_rtl;
9944 case INTEGER_CST:
9945 /* Given that TYPE_PRECISION (type) is not always equal to
9946 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9947 the former to the latter according to the signedness of the
9948 type. */
9949 temp = immed_wide_int_const (wi::to_wide
9950 (exp,
9951 GET_MODE_PRECISION (TYPE_MODE (type))),
9952 TYPE_MODE (type));
9953 return temp;
9955 case VECTOR_CST:
9957 tree tmp = NULL_TREE;
9958 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9959 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9960 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9961 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9962 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9963 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9964 return const_vector_from_tree (exp);
9965 scalar_int_mode int_mode;
9966 if (is_int_mode (mode, &int_mode))
9968 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9969 return const_scalar_mask_from_tree (exp);
9970 else
9972 tree type_for_mode
9973 = lang_hooks.types.type_for_mode (int_mode, 1);
9974 if (type_for_mode)
9975 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9976 type_for_mode, exp);
9979 if (!tmp)
9981 vec<constructor_elt, va_gc> *v;
9982 unsigned i;
9983 vec_alloc (v, VECTOR_CST_NELTS (exp));
9984 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9985 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9986 tmp = build_constructor (type, v);
9988 return expand_expr (tmp, ignore ? const0_rtx : target,
9989 tmode, modifier);
9992 case CONST_DECL:
9993 if (modifier == EXPAND_WRITE)
9995 /* Writing into CONST_DECL is always invalid, but handle it
9996 gracefully. */
9997 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9998 machine_mode address_mode = targetm.addr_space.address_mode (as);
9999 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
10000 EXPAND_NORMAL, as);
10001 op0 = memory_address_addr_space (mode, op0, as);
10002 temp = gen_rtx_MEM (mode, op0);
10003 set_mem_addr_space (temp, as);
10004 return temp;
10006 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10008 case REAL_CST:
10009 /* If optimized, generate immediate CONST_DOUBLE
10010 which will be turned into memory by reload if necessary.
10012 We used to force a register so that loop.c could see it. But
10013 this does not allow gen_* patterns to perform optimizations with
10014 the constants. It also produces two insns in cases like "x = 1.0;".
10015 On most machines, floating-point constants are not permitted in
10016 many insns, so we'd end up copying it to a register in any case.
10018 Now, we do the copying in expand_binop, if appropriate. */
10019 return const_double_from_real_value (TREE_REAL_CST (exp),
10020 TYPE_MODE (TREE_TYPE (exp)));
10022 case FIXED_CST:
10023 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10024 TYPE_MODE (TREE_TYPE (exp)));
10026 case COMPLEX_CST:
10027 /* Handle evaluating a complex constant in a CONCAT target. */
10028 if (original_target && GET_CODE (original_target) == CONCAT)
10030 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10031 rtx rtarg, itarg;
10033 rtarg = XEXP (original_target, 0);
10034 itarg = XEXP (original_target, 1);
10036 /* Move the real and imaginary parts separately. */
10037 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10038 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10040 if (op0 != rtarg)
10041 emit_move_insn (rtarg, op0);
10042 if (op1 != itarg)
10043 emit_move_insn (itarg, op1);
10045 return original_target;
10048 /* fall through */
10050 case STRING_CST:
10051 temp = expand_expr_constant (exp, 1, modifier);
10053 /* temp contains a constant address.
10054 On RISC machines where a constant address isn't valid,
10055 make some insns to get that address into a register. */
10056 if (modifier != EXPAND_CONST_ADDRESS
10057 && modifier != EXPAND_INITIALIZER
10058 && modifier != EXPAND_SUM
10059 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10060 MEM_ADDR_SPACE (temp)))
10061 return replace_equiv_address (temp,
10062 copy_rtx (XEXP (temp, 0)));
10063 return temp;
10065 case SAVE_EXPR:
10067 tree val = treeop0;
10068 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10069 inner_reference_p);
10071 if (!SAVE_EXPR_RESOLVED_P (exp))
10073 /* We can indeed still hit this case, typically via builtin
10074 expanders calling save_expr immediately before expanding
10075 something. Assume this means that we only have to deal
10076 with non-BLKmode values. */
10077 gcc_assert (GET_MODE (ret) != BLKmode);
10079 val = build_decl (curr_insn_location (),
10080 VAR_DECL, NULL, TREE_TYPE (exp));
10081 DECL_ARTIFICIAL (val) = 1;
10082 DECL_IGNORED_P (val) = 1;
10083 treeop0 = val;
10084 TREE_OPERAND (exp, 0) = treeop0;
10085 SAVE_EXPR_RESOLVED_P (exp) = 1;
10087 if (!CONSTANT_P (ret))
10088 ret = copy_to_reg (ret);
10089 SET_DECL_RTL (val, ret);
10092 return ret;
10096 case CONSTRUCTOR:
10097 /* If we don't need the result, just ensure we evaluate any
10098 subexpressions. */
10099 if (ignore)
10101 unsigned HOST_WIDE_INT idx;
10102 tree value;
10104 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10105 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10107 return const0_rtx;
10110 return expand_constructor (exp, target, modifier, false);
10112 case TARGET_MEM_REF:
10114 addr_space_t as
10115 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10116 enum insn_code icode;
10117 unsigned int align;
10119 op0 = addr_for_mem_ref (exp, as, true);
10120 op0 = memory_address_addr_space (mode, op0, as);
10121 temp = gen_rtx_MEM (mode, op0);
10122 set_mem_attributes (temp, exp, 0);
10123 set_mem_addr_space (temp, as);
10124 align = get_object_alignment (exp);
10125 if (modifier != EXPAND_WRITE
10126 && modifier != EXPAND_MEMORY
10127 && mode != BLKmode
10128 && align < GET_MODE_ALIGNMENT (mode)
10129 /* If the target does not have special handling for unaligned
10130 loads of mode then it can use regular moves for them. */
10131 && ((icode = optab_handler (movmisalign_optab, mode))
10132 != CODE_FOR_nothing))
10134 struct expand_operand ops[2];
10136 /* We've already validated the memory, and we're creating a
10137 new pseudo destination. The predicates really can't fail,
10138 nor can the generator. */
10139 create_output_operand (&ops[0], NULL_RTX, mode);
10140 create_fixed_operand (&ops[1], temp);
10141 expand_insn (icode, 2, ops);
10142 temp = ops[0].value;
10144 return temp;
10147 case MEM_REF:
10149 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10150 addr_space_t as
10151 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10152 machine_mode address_mode;
10153 tree base = TREE_OPERAND (exp, 0);
10154 gimple *def_stmt;
10155 enum insn_code icode;
10156 unsigned align;
10157 /* Handle expansion of non-aliased memory with non-BLKmode. That
10158 might end up in a register. */
10159 if (mem_ref_refers_to_non_mem_p (exp))
10161 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10162 base = TREE_OPERAND (base, 0);
10163 if (offset == 0
10164 && !reverse
10165 && tree_fits_uhwi_p (TYPE_SIZE (type))
10166 && (GET_MODE_BITSIZE (DECL_MODE (base))
10167 == tree_to_uhwi (TYPE_SIZE (type))))
10168 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10169 target, tmode, modifier);
10170 if (TYPE_MODE (type) == BLKmode)
10172 temp = assign_stack_temp (DECL_MODE (base),
10173 GET_MODE_SIZE (DECL_MODE (base)));
10174 store_expr (base, temp, 0, false, false);
10175 temp = adjust_address (temp, BLKmode, offset);
10176 set_mem_size (temp, int_size_in_bytes (type));
10177 return temp;
10179 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10180 bitsize_int (offset * BITS_PER_UNIT));
10181 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10182 return expand_expr (exp, target, tmode, modifier);
10184 address_mode = targetm.addr_space.address_mode (as);
10185 base = TREE_OPERAND (exp, 0);
10186 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10188 tree mask = gimple_assign_rhs2 (def_stmt);
10189 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10190 gimple_assign_rhs1 (def_stmt), mask);
10191 TREE_OPERAND (exp, 0) = base;
10193 align = get_object_alignment (exp);
10194 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10195 op0 = memory_address_addr_space (mode, op0, as);
10196 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10198 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10199 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10200 op0 = memory_address_addr_space (mode, op0, as);
10202 temp = gen_rtx_MEM (mode, op0);
10203 set_mem_attributes (temp, exp, 0);
10204 set_mem_addr_space (temp, as);
10205 if (TREE_THIS_VOLATILE (exp))
10206 MEM_VOLATILE_P (temp) = 1;
10207 if (modifier != EXPAND_WRITE
10208 && modifier != EXPAND_MEMORY
10209 && !inner_reference_p
10210 && mode != BLKmode
10211 && align < GET_MODE_ALIGNMENT (mode))
10213 if ((icode = optab_handler (movmisalign_optab, mode))
10214 != CODE_FOR_nothing)
10216 struct expand_operand ops[2];
10218 /* We've already validated the memory, and we're creating a
10219 new pseudo destination. The predicates really can't fail,
10220 nor can the generator. */
10221 create_output_operand (&ops[0], NULL_RTX, mode);
10222 create_fixed_operand (&ops[1], temp);
10223 expand_insn (icode, 2, ops);
10224 temp = ops[0].value;
10226 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10227 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10228 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10229 (modifier == EXPAND_STACK_PARM
10230 ? NULL_RTX : target),
10231 mode, mode, false, alt_rtl);
10233 if (reverse
10234 && modifier != EXPAND_MEMORY
10235 && modifier != EXPAND_WRITE)
10236 temp = flip_storage_order (mode, temp);
10237 return temp;
10240 case ARRAY_REF:
10243 tree array = treeop0;
10244 tree index = treeop1;
10245 tree init;
10247 /* Fold an expression like: "foo"[2].
10248 This is not done in fold so it won't happen inside &.
10249 Don't fold if this is for wide characters since it's too
10250 difficult to do correctly and this is a very rare case. */
10252 if (modifier != EXPAND_CONST_ADDRESS
10253 && modifier != EXPAND_INITIALIZER
10254 && modifier != EXPAND_MEMORY)
10256 tree t = fold_read_from_constant_string (exp);
10258 if (t)
10259 return expand_expr (t, target, tmode, modifier);
10262 /* If this is a constant index into a constant array,
10263 just get the value from the array. Handle both the cases when
10264 we have an explicit constructor and when our operand is a variable
10265 that was declared const. */
10267 if (modifier != EXPAND_CONST_ADDRESS
10268 && modifier != EXPAND_INITIALIZER
10269 && modifier != EXPAND_MEMORY
10270 && TREE_CODE (array) == CONSTRUCTOR
10271 && ! TREE_SIDE_EFFECTS (array)
10272 && TREE_CODE (index) == INTEGER_CST)
10274 unsigned HOST_WIDE_INT ix;
10275 tree field, value;
10277 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10278 field, value)
10279 if (tree_int_cst_equal (field, index))
10281 if (!TREE_SIDE_EFFECTS (value))
10282 return expand_expr (fold (value), target, tmode, modifier);
10283 break;
10287 else if (optimize >= 1
10288 && modifier != EXPAND_CONST_ADDRESS
10289 && modifier != EXPAND_INITIALIZER
10290 && modifier != EXPAND_MEMORY
10291 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10292 && TREE_CODE (index) == INTEGER_CST
10293 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10294 && (init = ctor_for_folding (array)) != error_mark_node)
10296 if (init == NULL_TREE)
10298 tree value = build_zero_cst (type);
10299 if (TREE_CODE (value) == CONSTRUCTOR)
10301 /* If VALUE is a CONSTRUCTOR, this optimization is only
10302 useful if this doesn't store the CONSTRUCTOR into
10303 memory. If it does, it is more efficient to just
10304 load the data from the array directly. */
10305 rtx ret = expand_constructor (value, target,
10306 modifier, true);
10307 if (ret == NULL_RTX)
10308 value = NULL_TREE;
10311 if (value)
10312 return expand_expr (value, target, tmode, modifier);
10314 else if (TREE_CODE (init) == CONSTRUCTOR)
10316 unsigned HOST_WIDE_INT ix;
10317 tree field, value;
10319 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10320 field, value)
10321 if (tree_int_cst_equal (field, index))
10323 if (TREE_SIDE_EFFECTS (value))
10324 break;
10326 if (TREE_CODE (value) == CONSTRUCTOR)
10328 /* If VALUE is a CONSTRUCTOR, this
10329 optimization is only useful if
10330 this doesn't store the CONSTRUCTOR
10331 into memory. If it does, it is more
10332 efficient to just load the data from
10333 the array directly. */
10334 rtx ret = expand_constructor (value, target,
10335 modifier, true);
10336 if (ret == NULL_RTX)
10337 break;
10340 return
10341 expand_expr (fold (value), target, tmode, modifier);
10344 else if (TREE_CODE (init) == STRING_CST)
10346 tree low_bound = array_ref_low_bound (exp);
10347 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10349 /* Optimize the special case of a zero lower bound.
10351 We convert the lower bound to sizetype to avoid problems
10352 with constant folding. E.g. suppose the lower bound is
10353 1 and its mode is QI. Without the conversion
10354 (ARRAY + (INDEX - (unsigned char)1))
10355 becomes
10356 (ARRAY + (-(unsigned char)1) + INDEX)
10357 which becomes
10358 (ARRAY + 255 + INDEX). Oops! */
10359 if (!integer_zerop (low_bound))
10360 index1 = size_diffop_loc (loc, index1,
10361 fold_convert_loc (loc, sizetype,
10362 low_bound));
10364 if (tree_fits_uhwi_p (index1)
10365 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10367 tree type = TREE_TYPE (TREE_TYPE (init));
10368 scalar_int_mode mode;
10370 if (is_int_mode (TYPE_MODE (type), &mode)
10371 && GET_MODE_SIZE (mode) == 1)
10372 return gen_int_mode (TREE_STRING_POINTER (init)
10373 [TREE_INT_CST_LOW (index1)],
10374 mode);
10379 goto normal_inner_ref;
10381 case COMPONENT_REF:
10382 /* If the operand is a CONSTRUCTOR, we can just extract the
10383 appropriate field if it is present. */
10384 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10386 unsigned HOST_WIDE_INT idx;
10387 tree field, value;
10388 scalar_int_mode field_mode;
10390 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10391 idx, field, value)
10392 if (field == treeop1
10393 /* We can normally use the value of the field in the
10394 CONSTRUCTOR. However, if this is a bitfield in
10395 an integral mode that we can fit in a HOST_WIDE_INT,
10396 we must mask only the number of bits in the bitfield,
10397 since this is done implicitly by the constructor. If
10398 the bitfield does not meet either of those conditions,
10399 we can't do this optimization. */
10400 && (! DECL_BIT_FIELD (field)
10401 || (is_int_mode (DECL_MODE (field), &field_mode)
10402 && (GET_MODE_PRECISION (field_mode)
10403 <= HOST_BITS_PER_WIDE_INT))))
10405 if (DECL_BIT_FIELD (field)
10406 && modifier == EXPAND_STACK_PARM)
10407 target = 0;
10408 op0 = expand_expr (value, target, tmode, modifier);
10409 if (DECL_BIT_FIELD (field))
10411 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10412 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10414 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10416 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10417 imode);
10418 op0 = expand_and (imode, op0, op1, target);
10420 else
10422 int count = GET_MODE_PRECISION (imode) - bitsize;
10424 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10425 target, 0);
10426 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10427 target, 0);
10431 return op0;
10434 goto normal_inner_ref;
10436 case BIT_FIELD_REF:
10437 case ARRAY_RANGE_REF:
10438 normal_inner_ref:
10440 machine_mode mode1, mode2;
10441 HOST_WIDE_INT bitsize, bitpos;
10442 tree offset;
10443 int reversep, volatilep = 0, must_force_mem;
10444 tree tem
10445 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10446 &unsignedp, &reversep, &volatilep);
10447 rtx orig_op0, memloc;
10448 bool clear_mem_expr = false;
10450 /* If we got back the original object, something is wrong. Perhaps
10451 we are evaluating an expression too early. In any event, don't
10452 infinitely recurse. */
10453 gcc_assert (tem != exp);
10455 /* If TEM's type is a union of variable size, pass TARGET to the inner
10456 computation, since it will need a temporary and TARGET is known
10457 to have to do. This occurs in unchecked conversion in Ada. */
10458 orig_op0 = op0
10459 = expand_expr_real (tem,
10460 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10461 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10462 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10463 != INTEGER_CST)
10464 && modifier != EXPAND_STACK_PARM
10465 ? target : NULL_RTX),
10466 VOIDmode,
10467 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10468 NULL, true);
10470 /* If the field has a mode, we want to access it in the
10471 field's mode, not the computed mode.
10472 If a MEM has VOIDmode (external with incomplete type),
10473 use BLKmode for it instead. */
10474 if (MEM_P (op0))
10476 if (mode1 != VOIDmode)
10477 op0 = adjust_address (op0, mode1, 0);
10478 else if (GET_MODE (op0) == VOIDmode)
10479 op0 = adjust_address (op0, BLKmode, 0);
10482 mode2
10483 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10485 /* If we have either an offset, a BLKmode result, or a reference
10486 outside the underlying object, we must force it to memory.
10487 Such a case can occur in Ada if we have unchecked conversion
10488 of an expression from a scalar type to an aggregate type or
10489 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10490 passed a partially uninitialized object or a view-conversion
10491 to a larger size. */
10492 must_force_mem = (offset
10493 || mode1 == BLKmode
10494 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10496 /* Handle CONCAT first. */
10497 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10499 if (bitpos == 0
10500 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10501 && COMPLEX_MODE_P (mode1)
10502 && COMPLEX_MODE_P (GET_MODE (op0))
10503 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10504 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10506 if (reversep)
10507 op0 = flip_storage_order (GET_MODE (op0), op0);
10508 if (mode1 != GET_MODE (op0))
10510 rtx parts[2];
10511 for (int i = 0; i < 2; i++)
10513 rtx op = read_complex_part (op0, i != 0);
10514 if (GET_CODE (op) == SUBREG)
10515 op = force_reg (GET_MODE (op), op);
10516 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10517 op);
10518 if (temp)
10519 op = temp;
10520 else
10522 if (!REG_P (op) && !MEM_P (op))
10523 op = force_reg (GET_MODE (op), op);
10524 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10526 parts[i] = op;
10528 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10530 return op0;
10532 if (bitpos == 0
10533 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10534 && bitsize)
10536 op0 = XEXP (op0, 0);
10537 mode2 = GET_MODE (op0);
10539 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10540 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10541 && bitpos
10542 && bitsize)
10544 op0 = XEXP (op0, 1);
10545 bitpos = 0;
10546 mode2 = GET_MODE (op0);
10548 else
10549 /* Otherwise force into memory. */
10550 must_force_mem = 1;
10553 /* If this is a constant, put it in a register if it is a legitimate
10554 constant and we don't need a memory reference. */
10555 if (CONSTANT_P (op0)
10556 && mode2 != BLKmode
10557 && targetm.legitimate_constant_p (mode2, op0)
10558 && !must_force_mem)
10559 op0 = force_reg (mode2, op0);
10561 /* Otherwise, if this is a constant, try to force it to the constant
10562 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10563 is a legitimate constant. */
10564 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10565 op0 = validize_mem (memloc);
10567 /* Otherwise, if this is a constant or the object is not in memory
10568 and need be, put it there. */
10569 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10571 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10572 emit_move_insn (memloc, op0);
10573 op0 = memloc;
10574 clear_mem_expr = true;
10577 if (offset)
10579 machine_mode address_mode;
10580 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10581 EXPAND_SUM);
10583 gcc_assert (MEM_P (op0));
10585 address_mode = get_address_mode (op0);
10586 if (GET_MODE (offset_rtx) != address_mode)
10588 /* We cannot be sure that the RTL in offset_rtx is valid outside
10589 of a memory address context, so force it into a register
10590 before attempting to convert it to the desired mode. */
10591 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10592 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10595 /* See the comment in expand_assignment for the rationale. */
10596 if (mode1 != VOIDmode
10597 && bitpos != 0
10598 && bitsize > 0
10599 && (bitpos % bitsize) == 0
10600 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10601 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10603 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10604 bitpos = 0;
10607 op0 = offset_address (op0, offset_rtx,
10608 highest_pow2_factor (offset));
10611 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10612 record its alignment as BIGGEST_ALIGNMENT. */
10613 if (MEM_P (op0) && bitpos == 0 && offset != 0
10614 && is_aligning_offset (offset, tem))
10615 set_mem_align (op0, BIGGEST_ALIGNMENT);
10617 /* Don't forget about volatility even if this is a bitfield. */
10618 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10620 if (op0 == orig_op0)
10621 op0 = copy_rtx (op0);
10623 MEM_VOLATILE_P (op0) = 1;
10626 /* In cases where an aligned union has an unaligned object
10627 as a field, we might be extracting a BLKmode value from
10628 an integer-mode (e.g., SImode) object. Handle this case
10629 by doing the extract into an object as wide as the field
10630 (which we know to be the width of a basic mode), then
10631 storing into memory, and changing the mode to BLKmode. */
10632 if (mode1 == VOIDmode
10633 || REG_P (op0) || GET_CODE (op0) == SUBREG
10634 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10635 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10636 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10637 && modifier != EXPAND_CONST_ADDRESS
10638 && modifier != EXPAND_INITIALIZER
10639 && modifier != EXPAND_MEMORY)
10640 /* If the bitfield is volatile and the bitsize
10641 is narrower than the access size of the bitfield,
10642 we need to extract bitfields from the access. */
10643 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10644 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10645 && mode1 != BLKmode
10646 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10647 /* If the field isn't aligned enough to fetch as a memref,
10648 fetch it as a bit field. */
10649 || (mode1 != BLKmode
10650 && (((MEM_P (op0)
10651 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10652 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0)
10653 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10654 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0))
10655 && modifier != EXPAND_MEMORY
10656 && ((modifier == EXPAND_CONST_ADDRESS
10657 || modifier == EXPAND_INITIALIZER)
10658 ? STRICT_ALIGNMENT
10659 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10660 || (bitpos % BITS_PER_UNIT != 0)))
10661 /* If the type and the field are a constant size and the
10662 size of the type isn't the same size as the bitfield,
10663 we must use bitfield operations. */
10664 || (bitsize >= 0
10665 && TYPE_SIZE (TREE_TYPE (exp))
10666 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10667 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10668 bitsize)))
10670 machine_mode ext_mode = mode;
10672 if (ext_mode == BLKmode
10673 && ! (target != 0 && MEM_P (op0)
10674 && MEM_P (target)
10675 && bitpos % BITS_PER_UNIT == 0))
10676 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10678 if (ext_mode == BLKmode)
10680 if (target == 0)
10681 target = assign_temp (type, 1, 1);
10683 /* ??? Unlike the similar test a few lines below, this one is
10684 very likely obsolete. */
10685 if (bitsize == 0)
10686 return target;
10688 /* In this case, BITPOS must start at a byte boundary and
10689 TARGET, if specified, must be a MEM. */
10690 gcc_assert (MEM_P (op0)
10691 && (!target || MEM_P (target))
10692 && !(bitpos % BITS_PER_UNIT));
10694 emit_block_move (target,
10695 adjust_address (op0, VOIDmode,
10696 bitpos / BITS_PER_UNIT),
10697 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10698 / BITS_PER_UNIT),
10699 (modifier == EXPAND_STACK_PARM
10700 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10702 return target;
10705 /* If we have nothing to extract, the result will be 0 for targets
10706 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10707 return 0 for the sake of consistency, as reading a zero-sized
10708 bitfield is valid in Ada and the value is fully specified. */
10709 if (bitsize == 0)
10710 return const0_rtx;
10712 op0 = validize_mem (op0);
10714 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10715 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10717 /* If the result has a record type and the extraction is done in
10718 an integral mode, then the field may be not aligned on a byte
10719 boundary; in this case, if it has reverse storage order, it
10720 needs to be extracted as a scalar field with reverse storage
10721 order and put back into memory order afterwards. */
10722 if (TREE_CODE (type) == RECORD_TYPE
10723 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10724 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10726 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10727 (modifier == EXPAND_STACK_PARM
10728 ? NULL_RTX : target),
10729 ext_mode, ext_mode, reversep, alt_rtl);
10731 /* If the result has a record type and the mode of OP0 is an
10732 integral mode then, if BITSIZE is narrower than this mode
10733 and this is for big-endian data, we must put the field
10734 into the high-order bits. And we must also put it back
10735 into memory order if it has been previously reversed. */
10736 scalar_int_mode op0_mode;
10737 if (TREE_CODE (type) == RECORD_TYPE
10738 && is_int_mode (GET_MODE (op0), &op0_mode))
10740 HOST_WIDE_INT size = GET_MODE_BITSIZE (op0_mode);
10742 if (bitsize < size
10743 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10744 op0 = expand_shift (LSHIFT_EXPR, op0_mode, op0,
10745 size - bitsize, op0, 1);
10747 if (reversep)
10748 op0 = flip_storage_order (op0_mode, op0);
10751 /* If the result type is BLKmode, store the data into a temporary
10752 of the appropriate type, but with the mode corresponding to the
10753 mode for the data we have (op0's mode). */
10754 if (mode == BLKmode)
10756 rtx new_rtx
10757 = assign_stack_temp_for_type (ext_mode,
10758 GET_MODE_BITSIZE (ext_mode),
10759 type);
10760 emit_move_insn (new_rtx, op0);
10761 op0 = copy_rtx (new_rtx);
10762 PUT_MODE (op0, BLKmode);
10765 return op0;
10768 /* If the result is BLKmode, use that to access the object
10769 now as well. */
10770 if (mode == BLKmode)
10771 mode1 = BLKmode;
10773 /* Get a reference to just this component. */
10774 if (modifier == EXPAND_CONST_ADDRESS
10775 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10776 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10777 else
10778 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10780 if (op0 == orig_op0)
10781 op0 = copy_rtx (op0);
10783 /* Don't set memory attributes if the base expression is
10784 SSA_NAME that got expanded as a MEM. In that case, we should
10785 just honor its original memory attributes. */
10786 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10787 set_mem_attributes (op0, exp, 0);
10789 if (REG_P (XEXP (op0, 0)))
10790 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10792 /* If op0 is a temporary because the original expressions was forced
10793 to memory, clear MEM_EXPR so that the original expression cannot
10794 be marked as addressable through MEM_EXPR of the temporary. */
10795 if (clear_mem_expr)
10796 set_mem_expr (op0, NULL_TREE);
10798 MEM_VOLATILE_P (op0) |= volatilep;
10800 if (reversep
10801 && modifier != EXPAND_MEMORY
10802 && modifier != EXPAND_WRITE)
10803 op0 = flip_storage_order (mode1, op0);
10805 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10806 || modifier == EXPAND_CONST_ADDRESS
10807 || modifier == EXPAND_INITIALIZER)
10808 return op0;
10810 if (target == 0)
10811 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10813 convert_move (target, op0, unsignedp);
10814 return target;
10817 case OBJ_TYPE_REF:
10818 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10820 case CALL_EXPR:
10821 /* All valid uses of __builtin_va_arg_pack () are removed during
10822 inlining. */
10823 if (CALL_EXPR_VA_ARG_PACK (exp))
10824 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10826 tree fndecl = get_callee_fndecl (exp), attr;
10828 if (fndecl
10829 && (attr = lookup_attribute ("error",
10830 DECL_ATTRIBUTES (fndecl))) != NULL)
10831 error ("%Kcall to %qs declared with attribute error: %s",
10832 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10833 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10834 if (fndecl
10835 && (attr = lookup_attribute ("warning",
10836 DECL_ATTRIBUTES (fndecl))) != NULL)
10837 warning_at (tree_nonartificial_location (exp),
10838 0, "%Kcall to %qs declared with attribute warning: %s",
10839 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10840 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10842 /* Check for a built-in function. */
10843 if (fndecl && DECL_BUILT_IN (fndecl))
10845 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10846 if (CALL_WITH_BOUNDS_P (exp))
10847 return expand_builtin_with_bounds (exp, target, subtarget,
10848 tmode, ignore);
10849 else
10850 return expand_builtin (exp, target, subtarget, tmode, ignore);
10853 return expand_call (exp, target, ignore);
10855 case VIEW_CONVERT_EXPR:
10856 op0 = NULL_RTX;
10858 /* If we are converting to BLKmode, try to avoid an intermediate
10859 temporary by fetching an inner memory reference. */
10860 if (mode == BLKmode
10861 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10862 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10863 && handled_component_p (treeop0))
10865 machine_mode mode1;
10866 HOST_WIDE_INT bitsize, bitpos;
10867 tree offset;
10868 int unsignedp, reversep, volatilep = 0;
10869 tree tem
10870 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10871 &unsignedp, &reversep, &volatilep);
10872 rtx orig_op0;
10874 /* ??? We should work harder and deal with non-zero offsets. */
10875 if (!offset
10876 && (bitpos % BITS_PER_UNIT) == 0
10877 && !reversep
10878 && bitsize >= 0
10879 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10881 /* See the normal_inner_ref case for the rationale. */
10882 orig_op0
10883 = expand_expr_real (tem,
10884 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10885 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10886 != INTEGER_CST)
10887 && modifier != EXPAND_STACK_PARM
10888 ? target : NULL_RTX),
10889 VOIDmode,
10890 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10891 NULL, true);
10893 if (MEM_P (orig_op0))
10895 op0 = orig_op0;
10897 /* Get a reference to just this component. */
10898 if (modifier == EXPAND_CONST_ADDRESS
10899 || modifier == EXPAND_SUM
10900 || modifier == EXPAND_INITIALIZER)
10901 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10902 else
10903 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10905 if (op0 == orig_op0)
10906 op0 = copy_rtx (op0);
10908 set_mem_attributes (op0, treeop0, 0);
10909 if (REG_P (XEXP (op0, 0)))
10910 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10912 MEM_VOLATILE_P (op0) |= volatilep;
10917 if (!op0)
10918 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10919 NULL, inner_reference_p);
10921 /* If the input and output modes are both the same, we are done. */
10922 if (mode == GET_MODE (op0))
10924 /* If neither mode is BLKmode, and both modes are the same size
10925 then we can use gen_lowpart. */
10926 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10927 && (GET_MODE_PRECISION (mode)
10928 == GET_MODE_PRECISION (GET_MODE (op0)))
10929 && !COMPLEX_MODE_P (GET_MODE (op0)))
10931 if (GET_CODE (op0) == SUBREG)
10932 op0 = force_reg (GET_MODE (op0), op0);
10933 temp = gen_lowpart_common (mode, op0);
10934 if (temp)
10935 op0 = temp;
10936 else
10938 if (!REG_P (op0) && !MEM_P (op0))
10939 op0 = force_reg (GET_MODE (op0), op0);
10940 op0 = gen_lowpart (mode, op0);
10943 /* If both types are integral, convert from one mode to the other. */
10944 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10945 op0 = convert_modes (mode, GET_MODE (op0), op0,
10946 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10947 /* If the output type is a bit-field type, do an extraction. */
10948 else if (reduce_bit_field)
10949 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10950 TYPE_UNSIGNED (type), NULL_RTX,
10951 mode, mode, false, NULL);
10952 /* As a last resort, spill op0 to memory, and reload it in a
10953 different mode. */
10954 else if (!MEM_P (op0))
10956 /* If the operand is not a MEM, force it into memory. Since we
10957 are going to be changing the mode of the MEM, don't call
10958 force_const_mem for constants because we don't allow pool
10959 constants to change mode. */
10960 tree inner_type = TREE_TYPE (treeop0);
10962 gcc_assert (!TREE_ADDRESSABLE (exp));
10964 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10965 target
10966 = assign_stack_temp_for_type
10967 (TYPE_MODE (inner_type),
10968 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10970 emit_move_insn (target, op0);
10971 op0 = target;
10974 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10975 output type is such that the operand is known to be aligned, indicate
10976 that it is. Otherwise, we need only be concerned about alignment for
10977 non-BLKmode results. */
10978 if (MEM_P (op0))
10980 enum insn_code icode;
10982 if (modifier != EXPAND_WRITE
10983 && modifier != EXPAND_MEMORY
10984 && !inner_reference_p
10985 && mode != BLKmode
10986 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10988 /* If the target does have special handling for unaligned
10989 loads of mode then use them. */
10990 if ((icode = optab_handler (movmisalign_optab, mode))
10991 != CODE_FOR_nothing)
10993 rtx reg;
10995 op0 = adjust_address (op0, mode, 0);
10996 /* We've already validated the memory, and we're creating a
10997 new pseudo destination. The predicates really can't
10998 fail. */
10999 reg = gen_reg_rtx (mode);
11001 /* Nor can the insn generator. */
11002 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
11003 emit_insn (insn);
11004 return reg;
11006 else if (STRICT_ALIGNMENT)
11008 tree inner_type = TREE_TYPE (treeop0);
11009 HOST_WIDE_INT temp_size
11010 = MAX (int_size_in_bytes (inner_type),
11011 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
11012 rtx new_rtx
11013 = assign_stack_temp_for_type (mode, temp_size, type);
11014 rtx new_with_op0_mode
11015 = adjust_address (new_rtx, GET_MODE (op0), 0);
11017 gcc_assert (!TREE_ADDRESSABLE (exp));
11019 if (GET_MODE (op0) == BLKmode)
11020 emit_block_move (new_with_op0_mode, op0,
11021 GEN_INT (GET_MODE_SIZE (mode)),
11022 (modifier == EXPAND_STACK_PARM
11023 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11024 else
11025 emit_move_insn (new_with_op0_mode, op0);
11027 op0 = new_rtx;
11031 op0 = adjust_address (op0, mode, 0);
11034 return op0;
11036 case MODIFY_EXPR:
11038 tree lhs = treeop0;
11039 tree rhs = treeop1;
11040 gcc_assert (ignore);
11042 /* Check for |= or &= of a bitfield of size one into another bitfield
11043 of size 1. In this case, (unless we need the result of the
11044 assignment) we can do this more efficiently with a
11045 test followed by an assignment, if necessary.
11047 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11048 things change so we do, this code should be enhanced to
11049 support it. */
11050 if (TREE_CODE (lhs) == COMPONENT_REF
11051 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11052 || TREE_CODE (rhs) == BIT_AND_EXPR)
11053 && TREE_OPERAND (rhs, 0) == lhs
11054 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11055 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11056 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11058 rtx_code_label *label = gen_label_rtx ();
11059 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11060 do_jump (TREE_OPERAND (rhs, 1),
11061 value ? label : 0,
11062 value ? 0 : label,
11063 profile_probability::uninitialized ());
11064 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11065 false);
11066 do_pending_stack_adjust ();
11067 emit_label (label);
11068 return const0_rtx;
11071 expand_assignment (lhs, rhs, false);
11072 return const0_rtx;
11075 case ADDR_EXPR:
11076 return expand_expr_addr_expr (exp, target, tmode, modifier);
11078 case REALPART_EXPR:
11079 op0 = expand_normal (treeop0);
11080 return read_complex_part (op0, false);
11082 case IMAGPART_EXPR:
11083 op0 = expand_normal (treeop0);
11084 return read_complex_part (op0, true);
11086 case RETURN_EXPR:
11087 case LABEL_EXPR:
11088 case GOTO_EXPR:
11089 case SWITCH_EXPR:
11090 case ASM_EXPR:
11091 /* Expanded in cfgexpand.c. */
11092 gcc_unreachable ();
11094 case TRY_CATCH_EXPR:
11095 case CATCH_EXPR:
11096 case EH_FILTER_EXPR:
11097 case TRY_FINALLY_EXPR:
11098 /* Lowered by tree-eh.c. */
11099 gcc_unreachable ();
11101 case WITH_CLEANUP_EXPR:
11102 case CLEANUP_POINT_EXPR:
11103 case TARGET_EXPR:
11104 case CASE_LABEL_EXPR:
11105 case VA_ARG_EXPR:
11106 case BIND_EXPR:
11107 case INIT_EXPR:
11108 case CONJ_EXPR:
11109 case COMPOUND_EXPR:
11110 case PREINCREMENT_EXPR:
11111 case PREDECREMENT_EXPR:
11112 case POSTINCREMENT_EXPR:
11113 case POSTDECREMENT_EXPR:
11114 case LOOP_EXPR:
11115 case EXIT_EXPR:
11116 case COMPOUND_LITERAL_EXPR:
11117 /* Lowered by gimplify.c. */
11118 gcc_unreachable ();
11120 case FDESC_EXPR:
11121 /* Function descriptors are not valid except for as
11122 initialization constants, and should not be expanded. */
11123 gcc_unreachable ();
11125 case WITH_SIZE_EXPR:
11126 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11127 have pulled out the size to use in whatever context it needed. */
11128 return expand_expr_real (treeop0, original_target, tmode,
11129 modifier, alt_rtl, inner_reference_p);
11131 default:
11132 return expand_expr_real_2 (&ops, target, tmode, modifier);
11136 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11137 signedness of TYPE), possibly returning the result in TARGET. */
11138 static rtx
11139 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11141 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11142 if (target && GET_MODE (target) != GET_MODE (exp))
11143 target = 0;
11144 /* For constant values, reduce using build_int_cst_type. */
11145 if (CONST_INT_P (exp))
11147 HOST_WIDE_INT value = INTVAL (exp);
11148 tree t = build_int_cst_type (type, value);
11149 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11151 else if (TYPE_UNSIGNED (type))
11153 machine_mode mode = GET_MODE (exp);
11154 rtx mask = immed_wide_int_const
11155 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11156 return expand_and (mode, exp, mask, target);
11158 else
11160 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11161 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11162 exp, count, target, 0);
11163 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11164 exp, count, target, 0);
11168 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11169 when applied to the address of EXP produces an address known to be
11170 aligned more than BIGGEST_ALIGNMENT. */
11172 static int
11173 is_aligning_offset (const_tree offset, const_tree exp)
11175 /* Strip off any conversions. */
11176 while (CONVERT_EXPR_P (offset))
11177 offset = TREE_OPERAND (offset, 0);
11179 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11180 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11181 if (TREE_CODE (offset) != BIT_AND_EXPR
11182 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11183 || compare_tree_int (TREE_OPERAND (offset, 1),
11184 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11185 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11186 return 0;
11188 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11189 It must be NEGATE_EXPR. Then strip any more conversions. */
11190 offset = TREE_OPERAND (offset, 0);
11191 while (CONVERT_EXPR_P (offset))
11192 offset = TREE_OPERAND (offset, 0);
11194 if (TREE_CODE (offset) != NEGATE_EXPR)
11195 return 0;
11197 offset = TREE_OPERAND (offset, 0);
11198 while (CONVERT_EXPR_P (offset))
11199 offset = TREE_OPERAND (offset, 0);
11201 /* This must now be the address of EXP. */
11202 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11205 /* Return the tree node if an ARG corresponds to a string constant or zero
11206 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11207 in bytes within the string that ARG is accessing. The type of the
11208 offset will be `sizetype'. */
11210 tree
11211 string_constant (tree arg, tree *ptr_offset)
11213 tree array, offset, lower_bound;
11214 STRIP_NOPS (arg);
11216 if (TREE_CODE (arg) == ADDR_EXPR)
11218 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11220 *ptr_offset = size_zero_node;
11221 return TREE_OPERAND (arg, 0);
11223 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11225 array = TREE_OPERAND (arg, 0);
11226 offset = size_zero_node;
11228 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11230 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11231 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11232 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11233 return 0;
11235 /* Check if the array has a nonzero lower bound. */
11236 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11237 if (!integer_zerop (lower_bound))
11239 /* If the offset and base aren't both constants, return 0. */
11240 if (TREE_CODE (lower_bound) != INTEGER_CST)
11241 return 0;
11242 if (TREE_CODE (offset) != INTEGER_CST)
11243 return 0;
11244 /* Adjust offset by the lower bound. */
11245 offset = size_diffop (fold_convert (sizetype, offset),
11246 fold_convert (sizetype, lower_bound));
11249 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11251 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11252 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11253 if (TREE_CODE (array) != ADDR_EXPR)
11254 return 0;
11255 array = TREE_OPERAND (array, 0);
11256 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11257 return 0;
11259 else
11260 return 0;
11262 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11264 tree arg0 = TREE_OPERAND (arg, 0);
11265 tree arg1 = TREE_OPERAND (arg, 1);
11267 STRIP_NOPS (arg0);
11268 STRIP_NOPS (arg1);
11270 if (TREE_CODE (arg0) == ADDR_EXPR
11271 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11272 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11274 array = TREE_OPERAND (arg0, 0);
11275 offset = arg1;
11277 else if (TREE_CODE (arg1) == ADDR_EXPR
11278 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11279 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11281 array = TREE_OPERAND (arg1, 0);
11282 offset = arg0;
11284 else
11285 return 0;
11287 else
11288 return 0;
11290 if (TREE_CODE (array) == STRING_CST)
11292 *ptr_offset = fold_convert (sizetype, offset);
11293 return array;
11295 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11297 int length;
11298 tree init = ctor_for_folding (array);
11300 /* Variables initialized to string literals can be handled too. */
11301 if (init == error_mark_node
11302 || !init
11303 || TREE_CODE (init) != STRING_CST)
11304 return 0;
11306 /* Avoid const char foo[4] = "abcde"; */
11307 if (DECL_SIZE_UNIT (array) == NULL_TREE
11308 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11309 || (length = TREE_STRING_LENGTH (init)) <= 0
11310 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11311 return 0;
11313 /* If variable is bigger than the string literal, OFFSET must be constant
11314 and inside of the bounds of the string literal. */
11315 offset = fold_convert (sizetype, offset);
11316 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11317 && (! tree_fits_uhwi_p (offset)
11318 || compare_tree_int (offset, length) >= 0))
11319 return 0;
11321 *ptr_offset = offset;
11322 return init;
11325 return 0;
11328 /* Generate code to calculate OPS, and exploded expression
11329 using a store-flag instruction and return an rtx for the result.
11330 OPS reflects a comparison.
11332 If TARGET is nonzero, store the result there if convenient.
11334 Return zero if there is no suitable set-flag instruction
11335 available on this machine.
11337 Once expand_expr has been called on the arguments of the comparison,
11338 we are committed to doing the store flag, since it is not safe to
11339 re-evaluate the expression. We emit the store-flag insn by calling
11340 emit_store_flag, but only expand the arguments if we have a reason
11341 to believe that emit_store_flag will be successful. If we think that
11342 it will, but it isn't, we have to simulate the store-flag with a
11343 set/jump/set sequence. */
11345 static rtx
11346 do_store_flag (sepops ops, rtx target, machine_mode mode)
11348 enum rtx_code code;
11349 tree arg0, arg1, type;
11350 machine_mode operand_mode;
11351 int unsignedp;
11352 rtx op0, op1;
11353 rtx subtarget = target;
11354 location_t loc = ops->location;
11356 arg0 = ops->op0;
11357 arg1 = ops->op1;
11359 /* Don't crash if the comparison was erroneous. */
11360 if (arg0 == error_mark_node || arg1 == error_mark_node)
11361 return const0_rtx;
11363 type = TREE_TYPE (arg0);
11364 operand_mode = TYPE_MODE (type);
11365 unsignedp = TYPE_UNSIGNED (type);
11367 /* We won't bother with BLKmode store-flag operations because it would mean
11368 passing a lot of information to emit_store_flag. */
11369 if (operand_mode == BLKmode)
11370 return 0;
11372 /* We won't bother with store-flag operations involving function pointers
11373 when function pointers must be canonicalized before comparisons. */
11374 if (targetm.have_canonicalize_funcptr_for_compare ()
11375 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11376 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11377 == FUNCTION_TYPE))
11378 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11379 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11380 == FUNCTION_TYPE))))
11381 return 0;
11383 STRIP_NOPS (arg0);
11384 STRIP_NOPS (arg1);
11386 /* For vector typed comparisons emit code to generate the desired
11387 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11388 expander for this. */
11389 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11391 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11392 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11393 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11394 return expand_vec_cmp_expr (ops->type, ifexp, target);
11395 else
11397 tree if_true = constant_boolean_node (true, ops->type);
11398 tree if_false = constant_boolean_node (false, ops->type);
11399 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11400 if_false, target);
11404 /* Get the rtx comparison code to use. We know that EXP is a comparison
11405 operation of some type. Some comparisons against 1 and -1 can be
11406 converted to comparisons with zero. Do so here so that the tests
11407 below will be aware that we have a comparison with zero. These
11408 tests will not catch constants in the first operand, but constants
11409 are rarely passed as the first operand. */
11411 switch (ops->code)
11413 case EQ_EXPR:
11414 code = EQ;
11415 break;
11416 case NE_EXPR:
11417 code = NE;
11418 break;
11419 case LT_EXPR:
11420 if (integer_onep (arg1))
11421 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11422 else
11423 code = unsignedp ? LTU : LT;
11424 break;
11425 case LE_EXPR:
11426 if (! unsignedp && integer_all_onesp (arg1))
11427 arg1 = integer_zero_node, code = LT;
11428 else
11429 code = unsignedp ? LEU : LE;
11430 break;
11431 case GT_EXPR:
11432 if (! unsignedp && integer_all_onesp (arg1))
11433 arg1 = integer_zero_node, code = GE;
11434 else
11435 code = unsignedp ? GTU : GT;
11436 break;
11437 case GE_EXPR:
11438 if (integer_onep (arg1))
11439 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11440 else
11441 code = unsignedp ? GEU : GE;
11442 break;
11444 case UNORDERED_EXPR:
11445 code = UNORDERED;
11446 break;
11447 case ORDERED_EXPR:
11448 code = ORDERED;
11449 break;
11450 case UNLT_EXPR:
11451 code = UNLT;
11452 break;
11453 case UNLE_EXPR:
11454 code = UNLE;
11455 break;
11456 case UNGT_EXPR:
11457 code = UNGT;
11458 break;
11459 case UNGE_EXPR:
11460 code = UNGE;
11461 break;
11462 case UNEQ_EXPR:
11463 code = UNEQ;
11464 break;
11465 case LTGT_EXPR:
11466 code = LTGT;
11467 break;
11469 default:
11470 gcc_unreachable ();
11473 /* Put a constant second. */
11474 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11475 || TREE_CODE (arg0) == FIXED_CST)
11477 std::swap (arg0, arg1);
11478 code = swap_condition (code);
11481 /* If this is an equality or inequality test of a single bit, we can
11482 do this by shifting the bit being tested to the low-order bit and
11483 masking the result with the constant 1. If the condition was EQ,
11484 we xor it with 1. This does not require an scc insn and is faster
11485 than an scc insn even if we have it.
11487 The code to make this transformation was moved into fold_single_bit_test,
11488 so we just call into the folder and expand its result. */
11490 if ((code == NE || code == EQ)
11491 && integer_zerop (arg1)
11492 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11494 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11495 if (srcstmt
11496 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11498 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11499 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11500 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11501 gimple_assign_rhs1 (srcstmt),
11502 gimple_assign_rhs2 (srcstmt));
11503 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11504 if (temp)
11505 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11509 if (! get_subtarget (target)
11510 || GET_MODE (subtarget) != operand_mode)
11511 subtarget = 0;
11513 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11515 if (target == 0)
11516 target = gen_reg_rtx (mode);
11518 /* Try a cstore if possible. */
11519 return emit_store_flag_force (target, code, op0, op1,
11520 operand_mode, unsignedp,
11521 (TYPE_PRECISION (ops->type) == 1
11522 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11525 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11526 0 otherwise (i.e. if there is no casesi instruction).
11528 DEFAULT_PROBABILITY is the probability of jumping to the default
11529 label. */
11531 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11532 rtx table_label, rtx default_label, rtx fallback_label,
11533 profile_probability default_probability)
11535 struct expand_operand ops[5];
11536 machine_mode index_mode = SImode;
11537 rtx op1, op2, index;
11539 if (! targetm.have_casesi ())
11540 return 0;
11542 /* Convert the index to SImode. */
11543 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11545 machine_mode omode = TYPE_MODE (index_type);
11546 rtx rangertx = expand_normal (range);
11548 /* We must handle the endpoints in the original mode. */
11549 index_expr = build2 (MINUS_EXPR, index_type,
11550 index_expr, minval);
11551 minval = integer_zero_node;
11552 index = expand_normal (index_expr);
11553 if (default_label)
11554 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11555 omode, 1, default_label,
11556 default_probability);
11557 /* Now we can safely truncate. */
11558 index = convert_to_mode (index_mode, index, 0);
11560 else
11562 if (TYPE_MODE (index_type) != index_mode)
11564 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11565 index_expr = fold_convert (index_type, index_expr);
11568 index = expand_normal (index_expr);
11571 do_pending_stack_adjust ();
11573 op1 = expand_normal (minval);
11574 op2 = expand_normal (range);
11576 create_input_operand (&ops[0], index, index_mode);
11577 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11578 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11579 create_fixed_operand (&ops[3], table_label);
11580 create_fixed_operand (&ops[4], (default_label
11581 ? default_label
11582 : fallback_label));
11583 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11584 return 1;
11587 /* Attempt to generate a tablejump instruction; same concept. */
11588 /* Subroutine of the next function.
11590 INDEX is the value being switched on, with the lowest value
11591 in the table already subtracted.
11592 MODE is its expected mode (needed if INDEX is constant).
11593 RANGE is the length of the jump table.
11594 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11596 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11597 index value is out of range.
11598 DEFAULT_PROBABILITY is the probability of jumping to
11599 the default label. */
11601 static void
11602 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11603 rtx default_label, profile_probability default_probability)
11605 rtx temp, vector;
11607 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11608 cfun->cfg->max_jumptable_ents = INTVAL (range);
11610 /* Do an unsigned comparison (in the proper mode) between the index
11611 expression and the value which represents the length of the range.
11612 Since we just finished subtracting the lower bound of the range
11613 from the index expression, this comparison allows us to simultaneously
11614 check that the original index expression value is both greater than
11615 or equal to the minimum value of the range and less than or equal to
11616 the maximum value of the range. */
11618 if (default_label)
11619 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11620 default_label, default_probability);
11623 /* If index is in range, it must fit in Pmode.
11624 Convert to Pmode so we can index with it. */
11625 if (mode != Pmode)
11626 index = convert_to_mode (Pmode, index, 1);
11628 /* Don't let a MEM slip through, because then INDEX that comes
11629 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11630 and break_out_memory_refs will go to work on it and mess it up. */
11631 #ifdef PIC_CASE_VECTOR_ADDRESS
11632 if (flag_pic && !REG_P (index))
11633 index = copy_to_mode_reg (Pmode, index);
11634 #endif
11636 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11637 GET_MODE_SIZE, because this indicates how large insns are. The other
11638 uses should all be Pmode, because they are addresses. This code
11639 could fail if addresses and insns are not the same size. */
11640 index = simplify_gen_binary (MULT, Pmode, index,
11641 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11642 Pmode));
11643 index = simplify_gen_binary (PLUS, Pmode, index,
11644 gen_rtx_LABEL_REF (Pmode, table_label));
11646 #ifdef PIC_CASE_VECTOR_ADDRESS
11647 if (flag_pic)
11648 index = PIC_CASE_VECTOR_ADDRESS (index);
11649 else
11650 #endif
11651 index = memory_address (CASE_VECTOR_MODE, index);
11652 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11653 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11654 convert_move (temp, vector, 0);
11656 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11658 /* If we are generating PIC code or if the table is PC-relative, the
11659 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11660 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11661 emit_barrier ();
11665 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11666 rtx table_label, rtx default_label,
11667 profile_probability default_probability)
11669 rtx index;
11671 if (! targetm.have_tablejump ())
11672 return 0;
11674 index_expr = fold_build2 (MINUS_EXPR, index_type,
11675 fold_convert (index_type, index_expr),
11676 fold_convert (index_type, minval));
11677 index = expand_normal (index_expr);
11678 do_pending_stack_adjust ();
11680 do_tablejump (index, TYPE_MODE (index_type),
11681 convert_modes (TYPE_MODE (index_type),
11682 TYPE_MODE (TREE_TYPE (range)),
11683 expand_normal (range),
11684 TYPE_UNSIGNED (TREE_TYPE (range))),
11685 table_label, default_label, default_probability);
11686 return 1;
11689 /* Return a CONST_VECTOR rtx representing vector mask for
11690 a VECTOR_CST of booleans. */
11691 static rtx
11692 const_vector_mask_from_tree (tree exp)
11694 rtvec v;
11695 unsigned i;
11696 int units;
11697 tree elt;
11698 machine_mode inner, mode;
11700 mode = TYPE_MODE (TREE_TYPE (exp));
11701 units = GET_MODE_NUNITS (mode);
11702 inner = GET_MODE_INNER (mode);
11704 v = rtvec_alloc (units);
11706 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11708 elt = VECTOR_CST_ELT (exp, i);
11710 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11711 if (integer_zerop (elt))
11712 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11713 else if (integer_onep (elt)
11714 || integer_minus_onep (elt))
11715 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11716 else
11717 gcc_unreachable ();
11720 return gen_rtx_CONST_VECTOR (mode, v);
11723 /* Return a CONST_INT rtx representing vector mask for
11724 a VECTOR_CST of booleans. */
11725 static rtx
11726 const_scalar_mask_from_tree (tree exp)
11728 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11729 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11730 tree elt;
11731 unsigned i;
11733 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11735 elt = VECTOR_CST_ELT (exp, i);
11736 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11737 if (integer_all_onesp (elt))
11738 res = wi::set_bit (res, i);
11739 else
11740 gcc_assert (integer_zerop (elt));
11743 return immed_wide_int_const (res, mode);
11746 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11747 static rtx
11748 const_vector_from_tree (tree exp)
11750 rtvec v;
11751 unsigned i;
11752 int units;
11753 tree elt;
11754 machine_mode inner, mode;
11756 mode = TYPE_MODE (TREE_TYPE (exp));
11758 if (initializer_zerop (exp))
11759 return CONST0_RTX (mode);
11761 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11762 return const_vector_mask_from_tree (exp);
11764 units = GET_MODE_NUNITS (mode);
11765 inner = GET_MODE_INNER (mode);
11767 v = rtvec_alloc (units);
11769 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11771 elt = VECTOR_CST_ELT (exp, i);
11773 if (TREE_CODE (elt) == REAL_CST)
11774 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11775 inner);
11776 else if (TREE_CODE (elt) == FIXED_CST)
11777 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11778 inner);
11779 else
11780 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11783 return gen_rtx_CONST_VECTOR (mode, v);
11786 /* Build a decl for a personality function given a language prefix. */
11788 tree
11789 build_personality_function (const char *lang)
11791 const char *unwind_and_version;
11792 tree decl, type;
11793 char *name;
11795 switch (targetm_common.except_unwind_info (&global_options))
11797 case UI_NONE:
11798 return NULL;
11799 case UI_SJLJ:
11800 unwind_and_version = "_sj0";
11801 break;
11802 case UI_DWARF2:
11803 case UI_TARGET:
11804 unwind_and_version = "_v0";
11805 break;
11806 case UI_SEH:
11807 unwind_and_version = "_seh0";
11808 break;
11809 default:
11810 gcc_unreachable ();
11813 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11815 type = build_function_type_list (integer_type_node, integer_type_node,
11816 long_long_unsigned_type_node,
11817 ptr_type_node, ptr_type_node, NULL_TREE);
11818 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11819 get_identifier (name), type);
11820 DECL_ARTIFICIAL (decl) = 1;
11821 DECL_EXTERNAL (decl) = 1;
11822 TREE_PUBLIC (decl) = 1;
11824 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11825 are the flags assigned by targetm.encode_section_info. */
11826 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11828 return decl;
11831 /* Extracts the personality function of DECL and returns the corresponding
11832 libfunc. */
11835 get_personality_function (tree decl)
11837 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11838 enum eh_personality_kind pk;
11840 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11841 if (pk == eh_personality_none)
11842 return NULL;
11844 if (!personality
11845 && pk == eh_personality_any)
11846 personality = lang_hooks.eh_personality ();
11848 if (pk == eh_personality_lang)
11849 gcc_assert (personality != NULL_TREE);
11851 return XEXP (DECL_RTL (personality), 0);
11854 /* Returns a tree for the size of EXP in bytes. */
11856 static tree
11857 tree_expr_size (const_tree exp)
11859 if (DECL_P (exp)
11860 && DECL_SIZE_UNIT (exp) != 0)
11861 return DECL_SIZE_UNIT (exp);
11862 else
11863 return size_in_bytes (TREE_TYPE (exp));
11866 /* Return an rtx for the size in bytes of the value of EXP. */
11869 expr_size (tree exp)
11871 tree size;
11873 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11874 size = TREE_OPERAND (exp, 1);
11875 else
11877 size = tree_expr_size (exp);
11878 gcc_assert (size);
11879 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11882 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11885 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11886 if the size can vary or is larger than an integer. */
11888 static HOST_WIDE_INT
11889 int_expr_size (tree exp)
11891 tree size;
11893 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11894 size = TREE_OPERAND (exp, 1);
11895 else
11897 size = tree_expr_size (exp);
11898 gcc_assert (size);
11901 if (size == 0 || !tree_fits_shwi_p (size))
11902 return -1;
11904 return tree_to_shwi (size);