[13/77] Make floatn_mode return an opt_scalar_float_mode
[official-gcc.git] / gcc / expr.c
blobc5cc0cbc155ce8a92795e86eeb87cf5a629e3f4c
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 machine_mode full_mode
350 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
352 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
353 != CODE_FOR_nothing);
355 if (full_mode != from_mode)
356 from = convert_to_mode (full_mode, from, unsignedp);
357 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
358 to, from, UNKNOWN);
359 return;
361 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
363 rtx new_from;
364 machine_mode full_mode
365 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
366 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
367 enum insn_code icode;
369 icode = convert_optab_handler (ctab, full_mode, from_mode);
370 gcc_assert (icode != CODE_FOR_nothing);
372 if (to_mode == full_mode)
374 emit_unop_insn (icode, to, from, UNKNOWN);
375 return;
378 new_from = gen_reg_rtx (full_mode);
379 emit_unop_insn (icode, new_from, from, UNKNOWN);
381 /* else proceed to integer conversions below. */
382 from_mode = full_mode;
383 from = new_from;
386 /* Make sure both are fixed-point modes or both are not. */
387 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
388 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
389 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
391 /* If we widen from_mode to to_mode and they are in the same class,
392 we won't saturate the result.
393 Otherwise, always saturate the result to play safe. */
394 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
395 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
396 expand_fixed_convert (to, from, 0, 0);
397 else
398 expand_fixed_convert (to, from, 0, 1);
399 return;
402 /* Now both modes are integers. */
404 /* Handle expanding beyond a word. */
405 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
406 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
408 rtx_insn *insns;
409 rtx lowpart;
410 rtx fill_value;
411 rtx lowfrom;
412 int i;
413 machine_mode lowpart_mode;
414 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
416 /* Try converting directly if the insn is supported. */
417 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
418 != CODE_FOR_nothing)
420 /* If FROM is a SUBREG, put it into a register. Do this
421 so that we always generate the same set of insns for
422 better cse'ing; if an intermediate assignment occurred,
423 we won't be doing the operation directly on the SUBREG. */
424 if (optimize > 0 && GET_CODE (from) == SUBREG)
425 from = force_reg (from_mode, from);
426 emit_unop_insn (code, to, from, equiv_code);
427 return;
429 /* Next, try converting via full word. */
430 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
431 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
432 != CODE_FOR_nothing))
434 rtx word_to = gen_reg_rtx (word_mode);
435 if (REG_P (to))
437 if (reg_overlap_mentioned_p (to, from))
438 from = force_reg (from_mode, from);
439 emit_clobber (to);
441 convert_move (word_to, from, unsignedp);
442 emit_unop_insn (code, to, word_to, equiv_code);
443 return;
446 /* No special multiword conversion insn; do it by hand. */
447 start_sequence ();
449 /* Since we will turn this into a no conflict block, we must ensure
450 the source does not overlap the target so force it into an isolated
451 register when maybe so. Likewise for any MEM input, since the
452 conversion sequence might require several references to it and we
453 must ensure we're getting the same value every time. */
455 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
456 from = force_reg (from_mode, from);
458 /* Get a copy of FROM widened to a word, if necessary. */
459 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
460 lowpart_mode = word_mode;
461 else
462 lowpart_mode = from_mode;
464 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
466 lowpart = gen_lowpart (lowpart_mode, to);
467 emit_move_insn (lowpart, lowfrom);
469 /* Compute the value to put in each remaining word. */
470 if (unsignedp)
471 fill_value = const0_rtx;
472 else
473 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
474 LT, lowfrom, const0_rtx,
475 lowpart_mode, 0, -1);
477 /* Fill the remaining words. */
478 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
480 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
481 rtx subword = operand_subword (to, index, 1, to_mode);
483 gcc_assert (subword);
485 if (fill_value != subword)
486 emit_move_insn (subword, fill_value);
489 insns = get_insns ();
490 end_sequence ();
492 emit_insn (insns);
493 return;
496 /* Truncating multi-word to a word or less. */
497 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
498 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
500 if (!((MEM_P (from)
501 && ! MEM_VOLATILE_P (from)
502 && direct_load[(int) to_mode]
503 && ! mode_dependent_address_p (XEXP (from, 0),
504 MEM_ADDR_SPACE (from)))
505 || REG_P (from)
506 || GET_CODE (from) == SUBREG))
507 from = force_reg (from_mode, from);
508 convert_move (to, gen_lowpart (word_mode, from), 0);
509 return;
512 /* Now follow all the conversions between integers
513 no more than a word long. */
515 /* For truncation, usually we can just refer to FROM in a narrower mode. */
516 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
517 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
519 if (!((MEM_P (from)
520 && ! MEM_VOLATILE_P (from)
521 && direct_load[(int) to_mode]
522 && ! mode_dependent_address_p (XEXP (from, 0),
523 MEM_ADDR_SPACE (from)))
524 || REG_P (from)
525 || GET_CODE (from) == SUBREG))
526 from = force_reg (from_mode, from);
527 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
528 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
529 from = copy_to_reg (from);
530 emit_move_insn (to, gen_lowpart (to_mode, from));
531 return;
534 /* Handle extension. */
535 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
537 /* Convert directly if that works. */
538 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
539 != CODE_FOR_nothing)
541 emit_unop_insn (code, to, from, equiv_code);
542 return;
544 else
546 machine_mode intermediate;
547 rtx tmp;
548 int shift_amount;
550 /* Search for a mode to convert via. */
551 FOR_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;
634 /* If FROM is a SUBREG that indicates that we have already done at least
635 the required extension, strip it. */
637 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
638 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
639 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
640 x = gen_lowpart (mode, SUBREG_REG (x));
642 if (GET_MODE (x) != VOIDmode)
643 oldmode = GET_MODE (x);
645 if (mode == oldmode)
646 return x;
648 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
650 /* If the caller did not tell us the old mode, then there is not
651 much to do with respect to canonicalization. We have to
652 assume that all the bits are significant. */
653 if (GET_MODE_CLASS (oldmode) != MODE_INT)
654 oldmode = MAX_MODE_INT;
655 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
656 GET_MODE_PRECISION (mode),
657 unsignedp ? UNSIGNED : SIGNED);
658 return immed_wide_int_const (w, mode);
661 /* We can do this with a gen_lowpart if both desired and current modes
662 are integer, and this is either a constant integer, a register, or a
663 non-volatile MEM. */
664 if (GET_MODE_CLASS (mode) == MODE_INT
665 && GET_MODE_CLASS (oldmode) == MODE_INT
666 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
667 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
668 || (REG_P (x)
669 && (!HARD_REGISTER_P (x)
670 || HARD_REGNO_MODE_OK (REGNO (x), mode))
671 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
673 return gen_lowpart (mode, x);
675 /* Converting from integer constant into mode is always equivalent to an
676 subreg operation. */
677 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
679 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
680 return simplify_gen_subreg (mode, x, oldmode, 0);
683 temp = gen_reg_rtx (mode);
684 convert_move (temp, x, unsignedp);
685 return temp;
688 /* Return the largest alignment we can use for doing a move (or store)
689 of MAX_PIECES. ALIGN is the largest alignment we could use. */
691 static unsigned int
692 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
694 machine_mode tmode;
696 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
697 if (align >= GET_MODE_ALIGNMENT (tmode))
698 align = GET_MODE_ALIGNMENT (tmode);
699 else
701 machine_mode tmode, xmode;
703 xmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
704 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
706 if (GET_MODE_SIZE (tmode) > max_pieces
707 || SLOW_UNALIGNED_ACCESS (tmode, align))
708 break;
709 xmode = tmode;
712 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
715 return align;
718 /* Return the widest integer mode no wider than SIZE. If no such mode
719 can be found, return VOIDmode. */
721 static machine_mode
722 widest_int_mode_for_size (unsigned int size)
724 machine_mode tmode, mode = VOIDmode;
726 FOR_EACH_MODE_IN_CLASS (tmode, MODE_INT)
727 if (GET_MODE_SIZE (tmode) < size)
728 mode = tmode;
730 return mode;
733 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
734 and should be performed piecewise. */
736 static bool
737 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
738 enum by_pieces_operation op)
740 return targetm.use_by_pieces_infrastructure_p (len, align, op,
741 optimize_insn_for_speed_p ());
744 /* Determine whether the LEN bytes can be moved by using several move
745 instructions. Return nonzero if a call to move_by_pieces should
746 succeed. */
748 bool
749 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
751 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
754 /* Return number of insns required to perform operation OP by pieces
755 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
757 unsigned HOST_WIDE_INT
758 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
759 unsigned int max_size, by_pieces_operation op)
761 unsigned HOST_WIDE_INT n_insns = 0;
763 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
765 while (max_size > 1 && l > 0)
767 machine_mode mode;
768 enum insn_code icode;
770 mode = widest_int_mode_for_size (max_size);
772 if (mode == VOIDmode)
773 break;
774 unsigned int modesize = GET_MODE_SIZE (mode);
776 icode = optab_handler (mov_optab, mode);
777 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
779 unsigned HOST_WIDE_INT n_pieces = l / modesize;
780 l %= modesize;
781 switch (op)
783 default:
784 n_insns += n_pieces;
785 break;
787 case COMPARE_BY_PIECES:
788 int batch = targetm.compare_by_pieces_branch_ratio (mode);
789 int batch_ops = 4 * batch - 1;
790 unsigned HOST_WIDE_INT full = n_pieces / batch;
791 n_insns += full * batch_ops;
792 if (n_pieces % batch != 0)
793 n_insns++;
794 break;
798 max_size = modesize;
801 gcc_assert (!l);
802 return n_insns;
805 /* Used when performing piecewise block operations, holds information
806 about one of the memory objects involved. The member functions
807 can be used to generate code for loading from the object and
808 updating the address when iterating. */
810 class pieces_addr
812 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
813 stack pushes. */
814 rtx m_obj;
815 /* The address of the object. Can differ from that seen in the
816 MEM rtx if we copied the address to a register. */
817 rtx m_addr;
818 /* Nonzero if the address on the object has an autoincrement already,
819 signifies whether that was an increment or decrement. */
820 signed char m_addr_inc;
821 /* Nonzero if we intend to use autoinc without the address already
822 having autoinc form. We will insert add insns around each memory
823 reference, expecting later passes to form autoinc addressing modes.
824 The only supported options are predecrement and postincrement. */
825 signed char m_explicit_inc;
826 /* True if we have either of the two possible cases of using
827 autoincrement. */
828 bool m_auto;
829 /* True if this is an address to be used for load operations rather
830 than stores. */
831 bool m_is_load;
833 /* Optionally, a function to obtain constants for any given offset into
834 the objects, and data associated with it. */
835 by_pieces_constfn m_constfn;
836 void *m_cfndata;
837 public:
838 pieces_addr (rtx, bool, by_pieces_constfn, void *);
839 rtx adjust (machine_mode, HOST_WIDE_INT);
840 void increment_address (HOST_WIDE_INT);
841 void maybe_predec (HOST_WIDE_INT);
842 void maybe_postinc (HOST_WIDE_INT);
843 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
844 int get_addr_inc ()
846 return m_addr_inc;
850 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
851 true if the operation to be performed on this object is a load
852 rather than a store. For stores, OBJ can be NULL, in which case we
853 assume the operation is a stack push. For loads, the optional
854 CONSTFN and its associated CFNDATA can be used in place of the
855 memory load. */
857 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
858 void *cfndata)
859 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
861 m_addr_inc = 0;
862 m_auto = false;
863 if (obj)
865 rtx addr = XEXP (obj, 0);
866 rtx_code code = GET_CODE (addr);
867 m_addr = addr;
868 bool dec = code == PRE_DEC || code == POST_DEC;
869 bool inc = code == PRE_INC || code == POST_INC;
870 m_auto = inc || dec;
871 if (m_auto)
872 m_addr_inc = dec ? -1 : 1;
874 /* While we have always looked for these codes here, the code
875 implementing the memory operation has never handled them.
876 Support could be added later if necessary or beneficial. */
877 gcc_assert (code != PRE_INC && code != POST_DEC);
879 else
881 m_addr = NULL_RTX;
882 if (!is_load)
884 m_auto = true;
885 if (STACK_GROWS_DOWNWARD)
886 m_addr_inc = -1;
887 else
888 m_addr_inc = 1;
890 else
891 gcc_assert (constfn != NULL);
893 m_explicit_inc = 0;
894 if (constfn)
895 gcc_assert (is_load);
898 /* Decide whether to use autoinc for an address involved in a memory op.
899 MODE is the mode of the accesses, REVERSE is true if we've decided to
900 perform the operation starting from the end, and LEN is the length of
901 the operation. Don't override an earlier decision to set m_auto. */
903 void
904 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
905 HOST_WIDE_INT len)
907 if (m_auto || m_obj == NULL_RTX)
908 return;
910 bool use_predec = (m_is_load
911 ? USE_LOAD_PRE_DECREMENT (mode)
912 : USE_STORE_PRE_DECREMENT (mode));
913 bool use_postinc = (m_is_load
914 ? USE_LOAD_POST_INCREMENT (mode)
915 : USE_STORE_POST_INCREMENT (mode));
916 machine_mode addr_mode = get_address_mode (m_obj);
918 if (use_predec && reverse)
920 m_addr = copy_to_mode_reg (addr_mode,
921 plus_constant (addr_mode,
922 m_addr, len));
923 m_auto = true;
924 m_explicit_inc = -1;
926 else if (use_postinc && !reverse)
928 m_addr = copy_to_mode_reg (addr_mode, m_addr);
929 m_auto = true;
930 m_explicit_inc = 1;
932 else if (CONSTANT_P (m_addr))
933 m_addr = copy_to_mode_reg (addr_mode, m_addr);
936 /* Adjust the address to refer to the data at OFFSET in MODE. If we
937 are using autoincrement for this address, we don't add the offset,
938 but we still modify the MEM's properties. */
941 pieces_addr::adjust (machine_mode mode, HOST_WIDE_INT offset)
943 if (m_constfn)
944 return m_constfn (m_cfndata, offset, mode);
945 if (m_obj == NULL_RTX)
946 return NULL_RTX;
947 if (m_auto)
948 return adjust_automodify_address (m_obj, mode, m_addr, offset);
949 else
950 return adjust_address (m_obj, mode, offset);
953 /* Emit an add instruction to increment the address by SIZE. */
955 void
956 pieces_addr::increment_address (HOST_WIDE_INT size)
958 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
959 emit_insn (gen_add2_insn (m_addr, amount));
962 /* If we are supposed to decrement the address after each access, emit code
963 to do so now. Increment by SIZE (which has should have the correct sign
964 already). */
966 void
967 pieces_addr::maybe_predec (HOST_WIDE_INT size)
969 if (m_explicit_inc >= 0)
970 return;
971 gcc_assert (HAVE_PRE_DECREMENT);
972 increment_address (size);
975 /* If we are supposed to decrement the address after each access, emit code
976 to do so now. Increment by SIZE. */
978 void
979 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
981 if (m_explicit_inc <= 0)
982 return;
983 gcc_assert (HAVE_POST_INCREMENT);
984 increment_address (size);
987 /* This structure is used by do_op_by_pieces to describe the operation
988 to be performed. */
990 class op_by_pieces_d
992 protected:
993 pieces_addr m_to, m_from;
994 unsigned HOST_WIDE_INT m_len;
995 HOST_WIDE_INT m_offset;
996 unsigned int m_align;
997 unsigned int m_max_size;
998 bool m_reverse;
1000 /* Virtual functions, overriden by derived classes for the specific
1001 operation. */
1002 virtual void generate (rtx, rtx, machine_mode) = 0;
1003 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1004 virtual void finish_mode (machine_mode)
1008 public:
1009 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1010 unsigned HOST_WIDE_INT, unsigned int);
1011 void run ();
1014 /* The constructor for an op_by_pieces_d structure. We require two
1015 objects named TO and FROM, which are identified as loads or stores
1016 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1017 and its associated FROM_CFN_DATA can be used to replace loads with
1018 constant values. LEN describes the length of the operation. */
1020 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1021 rtx from, bool from_load,
1022 by_pieces_constfn from_cfn,
1023 void *from_cfn_data,
1024 unsigned HOST_WIDE_INT len,
1025 unsigned int align)
1026 : m_to (to, to_load, NULL, NULL),
1027 m_from (from, from_load, from_cfn, from_cfn_data),
1028 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1030 int toi = m_to.get_addr_inc ();
1031 int fromi = m_from.get_addr_inc ();
1032 if (toi >= 0 && fromi >= 0)
1033 m_reverse = false;
1034 else if (toi <= 0 && fromi <= 0)
1035 m_reverse = true;
1036 else
1037 gcc_unreachable ();
1039 m_offset = m_reverse ? len : 0;
1040 align = MIN (to ? MEM_ALIGN (to) : align,
1041 from ? MEM_ALIGN (from) : align);
1043 /* If copying requires more than two move insns,
1044 copy addresses to registers (to make displacements shorter)
1045 and use post-increment if available. */
1046 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1048 /* Find the mode of the largest comparison. */
1049 machine_mode mode = widest_int_mode_for_size (m_max_size);
1051 m_from.decide_autoinc (mode, m_reverse, len);
1052 m_to.decide_autoinc (mode, m_reverse, len);
1055 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1056 m_align = align;
1059 /* This function contains the main loop used for expanding a block
1060 operation. First move what we can in the largest integer mode,
1061 then go to successively smaller modes. For every access, call
1062 GENFUN with the two operands and the EXTRA_DATA. */
1064 void
1065 op_by_pieces_d::run ()
1067 while (m_max_size > 1 && m_len > 0)
1069 machine_mode mode = widest_int_mode_for_size (m_max_size);
1071 if (mode == VOIDmode)
1072 break;
1074 if (prepare_mode (mode, m_align))
1076 unsigned int size = GET_MODE_SIZE (mode);
1077 rtx to1 = NULL_RTX, from1;
1079 while (m_len >= size)
1081 if (m_reverse)
1082 m_offset -= size;
1084 to1 = m_to.adjust (mode, m_offset);
1085 from1 = m_from.adjust (mode, m_offset);
1087 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1088 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1090 generate (to1, from1, mode);
1092 m_to.maybe_postinc (size);
1093 m_from.maybe_postinc (size);
1095 if (!m_reverse)
1096 m_offset += size;
1098 m_len -= size;
1101 finish_mode (mode);
1104 m_max_size = GET_MODE_SIZE (mode);
1107 /* The code above should have handled everything. */
1108 gcc_assert (!m_len);
1111 /* Derived class from op_by_pieces_d, providing support for block move
1112 operations. */
1114 class move_by_pieces_d : public op_by_pieces_d
1116 insn_gen_fn m_gen_fun;
1117 void generate (rtx, rtx, machine_mode);
1118 bool prepare_mode (machine_mode, unsigned int);
1120 public:
1121 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1122 unsigned int align)
1123 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1126 rtx finish_endp (int);
1129 /* Return true if MODE can be used for a set of copies, given an
1130 alignment ALIGN. Prepare whatever data is necessary for later
1131 calls to generate. */
1133 bool
1134 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1136 insn_code icode = optab_handler (mov_optab, mode);
1137 m_gen_fun = GEN_FCN (icode);
1138 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1141 /* A callback used when iterating for a compare_by_pieces_operation.
1142 OP0 and OP1 are the values that have been loaded and should be
1143 compared in MODE. If OP0 is NULL, this means we should generate a
1144 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1145 gen function that should be used to generate the mode. */
1147 void
1148 move_by_pieces_d::generate (rtx op0, rtx op1,
1149 machine_mode mode ATTRIBUTE_UNUSED)
1151 #ifdef PUSH_ROUNDING
1152 if (op0 == NULL_RTX)
1154 emit_single_push_insn (mode, op1, NULL);
1155 return;
1157 #endif
1158 emit_insn (m_gen_fun (op0, op1));
1161 /* Perform the final adjustment at the end of a string to obtain the
1162 correct return value for the block operation. If ENDP is 1 return
1163 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1164 end minus one byte ala stpcpy. */
1167 move_by_pieces_d::finish_endp (int endp)
1169 gcc_assert (!m_reverse);
1170 if (endp == 2)
1172 m_to.maybe_postinc (-1);
1173 --m_offset;
1175 return m_to.adjust (QImode, m_offset);
1178 /* Generate several move instructions to copy LEN bytes from block FROM to
1179 block TO. (These are MEM rtx's with BLKmode).
1181 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1182 used to push FROM to the stack.
1184 ALIGN is maximum stack alignment we can assume.
1186 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1187 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1188 stpcpy. */
1191 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1192 unsigned int align, int endp)
1194 #ifndef PUSH_ROUNDING
1195 if (to == NULL)
1196 gcc_unreachable ();
1197 #endif
1199 move_by_pieces_d data (to, from, len, align);
1201 data.run ();
1203 if (endp)
1204 return data.finish_endp (endp);
1205 else
1206 return to;
1209 /* Derived class from op_by_pieces_d, providing support for block move
1210 operations. */
1212 class store_by_pieces_d : public op_by_pieces_d
1214 insn_gen_fn m_gen_fun;
1215 void generate (rtx, rtx, machine_mode);
1216 bool prepare_mode (machine_mode, unsigned int);
1218 public:
1219 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1220 unsigned HOST_WIDE_INT len, unsigned int align)
1221 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1224 rtx finish_endp (int);
1227 /* Return true if MODE can be used for a set of stores, given an
1228 alignment ALIGN. Prepare whatever data is necessary for later
1229 calls to generate. */
1231 bool
1232 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1234 insn_code icode = optab_handler (mov_optab, mode);
1235 m_gen_fun = GEN_FCN (icode);
1236 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1239 /* A callback used when iterating for a store_by_pieces_operation.
1240 OP0 and OP1 are the values that have been loaded and should be
1241 compared in MODE. If OP0 is NULL, this means we should generate a
1242 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1243 gen function that should be used to generate the mode. */
1245 void
1246 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1248 emit_insn (m_gen_fun (op0, op1));
1251 /* Perform the final adjustment at the end of a string to obtain the
1252 correct return value for the block operation. If ENDP is 1 return
1253 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1254 end minus one byte ala stpcpy. */
1257 store_by_pieces_d::finish_endp (int endp)
1259 gcc_assert (!m_reverse);
1260 if (endp == 2)
1262 m_to.maybe_postinc (-1);
1263 --m_offset;
1265 return m_to.adjust (QImode, m_offset);
1268 /* Determine whether the LEN bytes generated by CONSTFUN can be
1269 stored to memory using several move instructions. CONSTFUNDATA is
1270 a pointer which will be passed as argument in every CONSTFUN call.
1271 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1272 a memset operation and false if it's a copy of a constant string.
1273 Return nonzero if a call to store_by_pieces should succeed. */
1276 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1277 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1278 void *constfundata, unsigned int align, bool memsetp)
1280 unsigned HOST_WIDE_INT l;
1281 unsigned int max_size;
1282 HOST_WIDE_INT offset = 0;
1283 machine_mode mode;
1284 enum insn_code icode;
1285 int reverse;
1286 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1287 rtx cst ATTRIBUTE_UNUSED;
1289 if (len == 0)
1290 return 1;
1292 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1293 memsetp
1294 ? SET_BY_PIECES
1295 : STORE_BY_PIECES,
1296 optimize_insn_for_speed_p ()))
1297 return 0;
1299 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1301 /* We would first store what we can in the largest integer mode, then go to
1302 successively smaller modes. */
1304 for (reverse = 0;
1305 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1306 reverse++)
1308 l = len;
1309 max_size = STORE_MAX_PIECES + 1;
1310 while (max_size > 1 && l > 0)
1312 mode = widest_int_mode_for_size (max_size);
1314 if (mode == VOIDmode)
1315 break;
1317 icode = optab_handler (mov_optab, mode);
1318 if (icode != CODE_FOR_nothing
1319 && align >= GET_MODE_ALIGNMENT (mode))
1321 unsigned int size = GET_MODE_SIZE (mode);
1323 while (l >= size)
1325 if (reverse)
1326 offset -= size;
1328 cst = (*constfun) (constfundata, offset, mode);
1329 if (!targetm.legitimate_constant_p (mode, cst))
1330 return 0;
1332 if (!reverse)
1333 offset += size;
1335 l -= size;
1339 max_size = GET_MODE_SIZE (mode);
1342 /* The code above should have handled everything. */
1343 gcc_assert (!l);
1346 return 1;
1349 /* Generate several move instructions to store LEN bytes generated by
1350 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1351 pointer which will be passed as argument in every CONSTFUN call.
1352 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1353 a memset operation and false if it's a copy of a constant string.
1354 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1355 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1356 stpcpy. */
1359 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1360 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1361 void *constfundata, unsigned int align, bool memsetp, int endp)
1363 if (len == 0)
1365 gcc_assert (endp != 2);
1366 return to;
1369 gcc_assert (targetm.use_by_pieces_infrastructure_p
1370 (len, align,
1371 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1372 optimize_insn_for_speed_p ()));
1374 store_by_pieces_d data (to, constfun, constfundata, len, align);
1375 data.run ();
1377 if (endp)
1378 return data.finish_endp (endp);
1379 else
1380 return to;
1383 /* Callback routine for clear_by_pieces.
1384 Return const0_rtx unconditionally. */
1386 static rtx
1387 clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode)
1389 return const0_rtx;
1392 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1393 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1395 static void
1396 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1398 if (len == 0)
1399 return;
1401 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1402 data.run ();
1405 /* Context used by compare_by_pieces_genfn. It stores the fail label
1406 to jump to in case of miscomparison, and for branch ratios greater than 1,
1407 it stores an accumulator and the current and maximum counts before
1408 emitting another branch. */
1410 class compare_by_pieces_d : public op_by_pieces_d
1412 rtx_code_label *m_fail_label;
1413 rtx m_accumulator;
1414 int m_count, m_batch;
1416 void generate (rtx, rtx, machine_mode);
1417 bool prepare_mode (machine_mode, unsigned int);
1418 void finish_mode (machine_mode);
1419 public:
1420 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1421 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1422 rtx_code_label *fail_label)
1423 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1425 m_fail_label = fail_label;
1429 /* A callback used when iterating for a compare_by_pieces_operation.
1430 OP0 and OP1 are the values that have been loaded and should be
1431 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1432 context structure. */
1434 void
1435 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1437 if (m_batch > 1)
1439 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1440 true, OPTAB_LIB_WIDEN);
1441 if (m_count != 0)
1442 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1443 true, OPTAB_LIB_WIDEN);
1444 m_accumulator = temp;
1446 if (++m_count < m_batch)
1447 return;
1449 m_count = 0;
1450 op0 = m_accumulator;
1451 op1 = const0_rtx;
1452 m_accumulator = NULL_RTX;
1454 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1455 m_fail_label, profile_probability::uninitialized ());
1458 /* Return true if MODE can be used for a set of moves and comparisons,
1459 given an alignment ALIGN. Prepare whatever data is necessary for
1460 later calls to generate. */
1462 bool
1463 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1465 insn_code icode = optab_handler (mov_optab, mode);
1466 if (icode == CODE_FOR_nothing
1467 || align < GET_MODE_ALIGNMENT (mode)
1468 || !can_compare_p (EQ, mode, ccp_jump))
1469 return false;
1470 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1471 if (m_batch < 0)
1472 return false;
1473 m_accumulator = NULL_RTX;
1474 m_count = 0;
1475 return true;
1478 /* Called after expanding a series of comparisons in MODE. If we have
1479 accumulated results for which we haven't emitted a branch yet, do
1480 so now. */
1482 void
1483 compare_by_pieces_d::finish_mode (machine_mode mode)
1485 if (m_accumulator != NULL_RTX)
1486 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1487 NULL_RTX, NULL, m_fail_label,
1488 profile_probability::uninitialized ());
1491 /* Generate several move instructions to compare LEN bytes from blocks
1492 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1494 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1495 used to push FROM to the stack.
1497 ALIGN is maximum stack alignment we can assume.
1499 Optionally, the caller can pass a constfn and associated data in A1_CFN
1500 and A1_CFN_DATA. describing that the second operand being compared is a
1501 known constant and how to obtain its data. */
1503 static rtx
1504 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1505 rtx target, unsigned int align,
1506 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1508 rtx_code_label *fail_label = gen_label_rtx ();
1509 rtx_code_label *end_label = gen_label_rtx ();
1511 if (target == NULL_RTX
1512 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1513 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1515 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1516 fail_label);
1518 data.run ();
1520 emit_move_insn (target, const0_rtx);
1521 emit_jump (end_label);
1522 emit_barrier ();
1523 emit_label (fail_label);
1524 emit_move_insn (target, const1_rtx);
1525 emit_label (end_label);
1527 return target;
1530 /* Emit code to move a block Y to a block X. This may be done with
1531 string-move instructions, with multiple scalar move instructions,
1532 or with a library call.
1534 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1535 SIZE is an rtx that says how long they are.
1536 ALIGN is the maximum alignment we can assume they have.
1537 METHOD describes what kind of copy this is, and what mechanisms may be used.
1538 MIN_SIZE is the minimal size of block to move
1539 MAX_SIZE is the maximal size of block to move, if it can not be represented
1540 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1542 Return the address of the new block, if memcpy is called and returns it,
1543 0 otherwise. */
1546 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1547 unsigned int expected_align, HOST_WIDE_INT expected_size,
1548 unsigned HOST_WIDE_INT min_size,
1549 unsigned HOST_WIDE_INT max_size,
1550 unsigned HOST_WIDE_INT probable_max_size)
1552 bool may_use_call;
1553 rtx retval = 0;
1554 unsigned int align;
1556 gcc_assert (size);
1557 if (CONST_INT_P (size) && INTVAL (size) == 0)
1558 return 0;
1560 switch (method)
1562 case BLOCK_OP_NORMAL:
1563 case BLOCK_OP_TAILCALL:
1564 may_use_call = true;
1565 break;
1567 case BLOCK_OP_CALL_PARM:
1568 may_use_call = block_move_libcall_safe_for_call_parm ();
1570 /* Make inhibit_defer_pop nonzero around the library call
1571 to force it to pop the arguments right away. */
1572 NO_DEFER_POP;
1573 break;
1575 case BLOCK_OP_NO_LIBCALL:
1576 may_use_call = false;
1577 break;
1579 default:
1580 gcc_unreachable ();
1583 gcc_assert (MEM_P (x) && MEM_P (y));
1584 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1585 gcc_assert (align >= BITS_PER_UNIT);
1587 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1588 block copy is more efficient for other large modes, e.g. DCmode. */
1589 x = adjust_address (x, BLKmode, 0);
1590 y = adjust_address (y, BLKmode, 0);
1592 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1593 can be incorrect is coming from __builtin_memcpy. */
1594 if (CONST_INT_P (size))
1596 x = shallow_copy_rtx (x);
1597 y = shallow_copy_rtx (y);
1598 set_mem_size (x, INTVAL (size));
1599 set_mem_size (y, INTVAL (size));
1602 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1603 move_by_pieces (x, y, INTVAL (size), align, 0);
1604 else if (emit_block_move_via_movmem (x, y, size, align,
1605 expected_align, expected_size,
1606 min_size, max_size, probable_max_size))
1608 else if (may_use_call
1609 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1610 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1612 /* Since x and y are passed to a libcall, mark the corresponding
1613 tree EXPR as addressable. */
1614 tree y_expr = MEM_EXPR (y);
1615 tree x_expr = MEM_EXPR (x);
1616 if (y_expr)
1617 mark_addressable (y_expr);
1618 if (x_expr)
1619 mark_addressable (x_expr);
1620 retval = emit_block_copy_via_libcall (x, y, size,
1621 method == BLOCK_OP_TAILCALL);
1624 else
1625 emit_block_move_via_loop (x, y, size, align);
1627 if (method == BLOCK_OP_CALL_PARM)
1628 OK_DEFER_POP;
1630 return retval;
1634 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1636 unsigned HOST_WIDE_INT max, min = 0;
1637 if (GET_CODE (size) == CONST_INT)
1638 min = max = UINTVAL (size);
1639 else
1640 max = GET_MODE_MASK (GET_MODE (size));
1641 return emit_block_move_hints (x, y, size, method, 0, -1,
1642 min, max, max);
1645 /* A subroutine of emit_block_move. Returns true if calling the
1646 block move libcall will not clobber any parameters which may have
1647 already been placed on the stack. */
1649 static bool
1650 block_move_libcall_safe_for_call_parm (void)
1652 #if defined (REG_PARM_STACK_SPACE)
1653 tree fn;
1654 #endif
1656 /* If arguments are pushed on the stack, then they're safe. */
1657 if (PUSH_ARGS)
1658 return true;
1660 /* If registers go on the stack anyway, any argument is sure to clobber
1661 an outgoing argument. */
1662 #if defined (REG_PARM_STACK_SPACE)
1663 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1664 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1665 depend on its argument. */
1666 (void) fn;
1667 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1668 && REG_PARM_STACK_SPACE (fn) != 0)
1669 return false;
1670 #endif
1672 /* If any argument goes in memory, then it might clobber an outgoing
1673 argument. */
1675 CUMULATIVE_ARGS args_so_far_v;
1676 cumulative_args_t args_so_far;
1677 tree fn, arg;
1679 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1680 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1681 args_so_far = pack_cumulative_args (&args_so_far_v);
1683 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1684 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1686 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1687 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1688 NULL_TREE, true);
1689 if (!tmp || !REG_P (tmp))
1690 return false;
1691 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1692 return false;
1693 targetm.calls.function_arg_advance (args_so_far, mode,
1694 NULL_TREE, true);
1697 return true;
1700 /* A subroutine of emit_block_move. Expand a movmem pattern;
1701 return true if successful. */
1703 static bool
1704 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1705 unsigned int expected_align, HOST_WIDE_INT expected_size,
1706 unsigned HOST_WIDE_INT min_size,
1707 unsigned HOST_WIDE_INT max_size,
1708 unsigned HOST_WIDE_INT probable_max_size)
1710 int save_volatile_ok = volatile_ok;
1711 machine_mode mode;
1713 if (expected_align < align)
1714 expected_align = align;
1715 if (expected_size != -1)
1717 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1718 expected_size = probable_max_size;
1719 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1720 expected_size = min_size;
1723 /* Since this is a move insn, we don't care about volatility. */
1724 volatile_ok = 1;
1726 /* Try the most limited insn first, because there's no point
1727 including more than one in the machine description unless
1728 the more limited one has some advantage. */
1730 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
1732 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1734 if (code != CODE_FOR_nothing
1735 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1736 here because if SIZE is less than the mode mask, as it is
1737 returned by the macro, it will definitely be less than the
1738 actual mode mask. Since SIZE is within the Pmode address
1739 space, we limit MODE to Pmode. */
1740 && ((CONST_INT_P (size)
1741 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1742 <= (GET_MODE_MASK (mode) >> 1)))
1743 || max_size <= (GET_MODE_MASK (mode) >> 1)
1744 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1746 struct expand_operand ops[9];
1747 unsigned int nops;
1749 /* ??? When called via emit_block_move_for_call, it'd be
1750 nice if there were some way to inform the backend, so
1751 that it doesn't fail the expansion because it thinks
1752 emitting the libcall would be more efficient. */
1753 nops = insn_data[(int) code].n_generator_args;
1754 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1756 create_fixed_operand (&ops[0], x);
1757 create_fixed_operand (&ops[1], y);
1758 /* The check above guarantees that this size conversion is valid. */
1759 create_convert_operand_to (&ops[2], size, mode, true);
1760 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1761 if (nops >= 6)
1763 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1764 create_integer_operand (&ops[5], expected_size);
1766 if (nops >= 8)
1768 create_integer_operand (&ops[6], min_size);
1769 /* If we can not represent the maximal size,
1770 make parameter NULL. */
1771 if ((HOST_WIDE_INT) max_size != -1)
1772 create_integer_operand (&ops[7], max_size);
1773 else
1774 create_fixed_operand (&ops[7], NULL);
1776 if (nops == 9)
1778 /* If we can not represent the maximal size,
1779 make parameter NULL. */
1780 if ((HOST_WIDE_INT) probable_max_size != -1)
1781 create_integer_operand (&ops[8], probable_max_size);
1782 else
1783 create_fixed_operand (&ops[8], NULL);
1785 if (maybe_expand_insn (code, nops, ops))
1787 volatile_ok = save_volatile_ok;
1788 return true;
1793 volatile_ok = save_volatile_ok;
1794 return false;
1797 /* A subroutine of emit_block_move. Copy the data via an explicit
1798 loop. This is used only when libcalls are forbidden. */
1799 /* ??? It'd be nice to copy in hunks larger than QImode. */
1801 static void
1802 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1803 unsigned int align ATTRIBUTE_UNUSED)
1805 rtx_code_label *cmp_label, *top_label;
1806 rtx iter, x_addr, y_addr, tmp;
1807 machine_mode x_addr_mode = get_address_mode (x);
1808 machine_mode y_addr_mode = get_address_mode (y);
1809 machine_mode iter_mode;
1811 iter_mode = GET_MODE (size);
1812 if (iter_mode == VOIDmode)
1813 iter_mode = word_mode;
1815 top_label = gen_label_rtx ();
1816 cmp_label = gen_label_rtx ();
1817 iter = gen_reg_rtx (iter_mode);
1819 emit_move_insn (iter, const0_rtx);
1821 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1822 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1823 do_pending_stack_adjust ();
1825 emit_jump (cmp_label);
1826 emit_label (top_label);
1828 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1829 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1831 if (x_addr_mode != y_addr_mode)
1832 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1833 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1835 x = change_address (x, QImode, x_addr);
1836 y = change_address (y, QImode, y_addr);
1838 emit_move_insn (x, y);
1840 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1841 true, OPTAB_LIB_WIDEN);
1842 if (tmp != iter)
1843 emit_move_insn (iter, tmp);
1845 emit_label (cmp_label);
1847 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1848 true, top_label,
1849 profile_probability::guessed_always ()
1850 .apply_scale (9, 10));
1853 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1854 TAILCALL is true if this is a tail call. */
1857 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1858 rtx size, bool tailcall)
1860 rtx dst_addr, src_addr;
1861 tree call_expr, dst_tree, src_tree, size_tree;
1862 machine_mode size_mode;
1864 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1865 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1866 dst_tree = make_tree (ptr_type_node, dst_addr);
1868 src_addr = copy_addr_to_reg (XEXP (src, 0));
1869 src_addr = convert_memory_address (ptr_mode, src_addr);
1870 src_tree = make_tree (ptr_type_node, src_addr);
1872 size_mode = TYPE_MODE (sizetype);
1873 size = convert_to_mode (size_mode, size, 1);
1874 size = copy_to_mode_reg (size_mode, size);
1875 size_tree = make_tree (sizetype, size);
1877 /* It is incorrect to use the libcall calling conventions for calls to
1878 memcpy/memmove/memcmp because they can be provided by the user. */
1879 tree fn = builtin_decl_implicit (fncode);
1880 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1881 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1883 return expand_call (call_expr, NULL_RTX, false);
1886 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1887 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1888 otherwise return null. */
1891 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1892 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1893 HOST_WIDE_INT align)
1895 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1897 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1898 target = NULL_RTX;
1900 struct expand_operand ops[5];
1901 create_output_operand (&ops[0], target, insn_mode);
1902 create_fixed_operand (&ops[1], arg1_rtx);
1903 create_fixed_operand (&ops[2], arg2_rtx);
1904 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1905 TYPE_UNSIGNED (arg3_type));
1906 create_integer_operand (&ops[4], align);
1907 if (maybe_expand_insn (icode, 5, ops))
1908 return ops[0].value;
1909 return NULL_RTX;
1912 /* Expand a block compare between X and Y with length LEN using the
1913 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1914 of the expression that was used to calculate the length. ALIGN
1915 gives the known minimum common alignment. */
1917 static rtx
1918 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1919 unsigned align)
1921 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1922 implementing memcmp because it will stop if it encounters two
1923 zero bytes. */
1924 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1926 if (icode == CODE_FOR_nothing)
1927 return NULL_RTX;
1929 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1932 /* Emit code to compare a block Y to a block X. This may be done with
1933 string-compare instructions, with multiple scalar instructions,
1934 or with a library call.
1936 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1937 they are. LEN_TYPE is the type of the expression that was used to
1938 calculate it.
1940 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1941 value of a normal memcmp call, instead we can just compare for equality.
1942 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1943 returning NULL_RTX.
1945 Optionally, the caller can pass a constfn and associated data in Y_CFN
1946 and Y_CFN_DATA. describing that the second operand being compared is a
1947 known constant and how to obtain its data.
1948 Return the result of the comparison, or NULL_RTX if we failed to
1949 perform the operation. */
1952 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1953 bool equality_only, by_pieces_constfn y_cfn,
1954 void *y_cfndata)
1956 rtx result = 0;
1958 if (CONST_INT_P (len) && INTVAL (len) == 0)
1959 return const0_rtx;
1961 gcc_assert (MEM_P (x) && MEM_P (y));
1962 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1963 gcc_assert (align >= BITS_PER_UNIT);
1965 x = adjust_address (x, BLKmode, 0);
1966 y = adjust_address (y, BLKmode, 0);
1968 if (equality_only
1969 && CONST_INT_P (len)
1970 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1971 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1972 y_cfn, y_cfndata);
1973 else
1974 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1976 return result;
1979 /* Copy all or part of a value X into registers starting at REGNO.
1980 The number of registers to be filled is NREGS. */
1982 void
1983 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1985 if (nregs == 0)
1986 return;
1988 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1989 x = validize_mem (force_const_mem (mode, x));
1991 /* See if the machine can do this with a load multiple insn. */
1992 if (targetm.have_load_multiple ())
1994 rtx_insn *last = get_last_insn ();
1995 rtx first = gen_rtx_REG (word_mode, regno);
1996 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1997 GEN_INT (nregs)))
1999 emit_insn (pat);
2000 return;
2002 else
2003 delete_insns_since (last);
2006 for (int i = 0; i < nregs; i++)
2007 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2008 operand_subword_force (x, i, mode));
2011 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2012 The number of registers to be filled is NREGS. */
2014 void
2015 move_block_from_reg (int regno, rtx x, int nregs)
2017 if (nregs == 0)
2018 return;
2020 /* See if the machine can do this with a store multiple insn. */
2021 if (targetm.have_store_multiple ())
2023 rtx_insn *last = get_last_insn ();
2024 rtx first = gen_rtx_REG (word_mode, regno);
2025 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2026 GEN_INT (nregs)))
2028 emit_insn (pat);
2029 return;
2031 else
2032 delete_insns_since (last);
2035 for (int i = 0; i < nregs; i++)
2037 rtx tem = operand_subword (x, i, 1, BLKmode);
2039 gcc_assert (tem);
2041 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2045 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2046 ORIG, where ORIG is a non-consecutive group of registers represented by
2047 a PARALLEL. The clone is identical to the original except in that the
2048 original set of registers is replaced by a new set of pseudo registers.
2049 The new set has the same modes as the original set. */
2052 gen_group_rtx (rtx orig)
2054 int i, length;
2055 rtx *tmps;
2057 gcc_assert (GET_CODE (orig) == PARALLEL);
2059 length = XVECLEN (orig, 0);
2060 tmps = XALLOCAVEC (rtx, length);
2062 /* Skip a NULL entry in first slot. */
2063 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2065 if (i)
2066 tmps[0] = 0;
2068 for (; i < length; i++)
2070 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2071 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2073 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2076 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2079 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2080 except that values are placed in TMPS[i], and must later be moved
2081 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2083 static void
2084 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2086 rtx src;
2087 int start, i;
2088 machine_mode m = GET_MODE (orig_src);
2090 gcc_assert (GET_CODE (dst) == PARALLEL);
2092 if (m != VOIDmode
2093 && !SCALAR_INT_MODE_P (m)
2094 && !MEM_P (orig_src)
2095 && GET_CODE (orig_src) != CONCAT)
2097 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
2098 if (imode == BLKmode)
2099 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2100 else
2101 src = gen_reg_rtx (imode);
2102 if (imode != BLKmode)
2103 src = gen_lowpart (GET_MODE (orig_src), src);
2104 emit_move_insn (src, orig_src);
2105 /* ...and back again. */
2106 if (imode != BLKmode)
2107 src = gen_lowpart (imode, src);
2108 emit_group_load_1 (tmps, dst, src, type, ssize);
2109 return;
2112 /* Check for a NULL entry, used to indicate that the parameter goes
2113 both on the stack and in registers. */
2114 if (XEXP (XVECEXP (dst, 0, 0), 0))
2115 start = 0;
2116 else
2117 start = 1;
2119 /* Process the pieces. */
2120 for (i = start; i < XVECLEN (dst, 0); i++)
2122 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2123 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2124 unsigned int bytelen = GET_MODE_SIZE (mode);
2125 int shift = 0;
2127 /* Handle trailing fragments that run over the size of the struct. */
2128 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2130 /* Arrange to shift the fragment to where it belongs.
2131 extract_bit_field loads to the lsb of the reg. */
2132 if (
2133 #ifdef BLOCK_REG_PADDING
2134 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2135 == (BYTES_BIG_ENDIAN ? upward : downward)
2136 #else
2137 BYTES_BIG_ENDIAN
2138 #endif
2140 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2141 bytelen = ssize - bytepos;
2142 gcc_assert (bytelen > 0);
2145 /* If we won't be loading directly from memory, protect the real source
2146 from strange tricks we might play; but make sure that the source can
2147 be loaded directly into the destination. */
2148 src = orig_src;
2149 if (!MEM_P (orig_src)
2150 && (!CONSTANT_P (orig_src)
2151 || (GET_MODE (orig_src) != mode
2152 && GET_MODE (orig_src) != VOIDmode)))
2154 if (GET_MODE (orig_src) == VOIDmode)
2155 src = gen_reg_rtx (mode);
2156 else
2157 src = gen_reg_rtx (GET_MODE (orig_src));
2159 emit_move_insn (src, orig_src);
2162 /* Optimize the access just a bit. */
2163 if (MEM_P (src)
2164 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
2165 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2166 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2167 && bytelen == GET_MODE_SIZE (mode))
2169 tmps[i] = gen_reg_rtx (mode);
2170 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2172 else if (COMPLEX_MODE_P (mode)
2173 && GET_MODE (src) == mode
2174 && bytelen == GET_MODE_SIZE (mode))
2175 /* Let emit_move_complex do the bulk of the work. */
2176 tmps[i] = src;
2177 else if (GET_CODE (src) == CONCAT)
2179 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2180 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2181 unsigned int elt = bytepos / slen0;
2182 unsigned int subpos = bytepos % slen0;
2184 if (subpos + bytelen <= slen0)
2186 /* The following assumes that the concatenated objects all
2187 have the same size. In this case, a simple calculation
2188 can be used to determine the object and the bit field
2189 to be extracted. */
2190 tmps[i] = XEXP (src, elt);
2191 if (subpos != 0
2192 || subpos + bytelen != slen0
2193 || (!CONSTANT_P (tmps[i])
2194 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2195 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2196 subpos * BITS_PER_UNIT,
2197 1, NULL_RTX, mode, mode, false,
2198 NULL);
2200 else
2202 rtx mem;
2204 gcc_assert (!bytepos);
2205 mem = assign_stack_temp (GET_MODE (src), slen);
2206 emit_move_insn (mem, src);
2207 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2208 0, 1, NULL_RTX, mode, mode, false,
2209 NULL);
2212 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2213 SIMD register, which is currently broken. While we get GCC
2214 to emit proper RTL for these cases, let's dump to memory. */
2215 else if (VECTOR_MODE_P (GET_MODE (dst))
2216 && REG_P (src))
2218 int slen = GET_MODE_SIZE (GET_MODE (src));
2219 rtx mem;
2221 mem = assign_stack_temp (GET_MODE (src), slen);
2222 emit_move_insn (mem, src);
2223 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2225 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2226 && XVECLEN (dst, 0) > 1)
2227 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2228 else if (CONSTANT_P (src))
2230 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2232 if (len == ssize)
2233 tmps[i] = src;
2234 else
2236 rtx first, second;
2238 /* TODO: const_wide_int can have sizes other than this... */
2239 gcc_assert (2 * len == ssize);
2240 split_double (src, &first, &second);
2241 if (i)
2242 tmps[i] = second;
2243 else
2244 tmps[i] = first;
2247 else if (REG_P (src) && GET_MODE (src) == mode)
2248 tmps[i] = src;
2249 else
2250 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2251 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2252 mode, mode, false, NULL);
2254 if (shift)
2255 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2256 shift, tmps[i], 0);
2260 /* Emit code to move a block SRC of type TYPE to a block DST,
2261 where DST is non-consecutive registers represented by a PARALLEL.
2262 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2263 if not known. */
2265 void
2266 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2268 rtx *tmps;
2269 int i;
2271 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2272 emit_group_load_1 (tmps, dst, src, type, ssize);
2274 /* Copy the extracted pieces into the proper (probable) hard regs. */
2275 for (i = 0; i < XVECLEN (dst, 0); i++)
2277 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2278 if (d == NULL)
2279 continue;
2280 emit_move_insn (d, tmps[i]);
2284 /* Similar, but load SRC into new pseudos in a format that looks like
2285 PARALLEL. This can later be fed to emit_group_move to get things
2286 in the right place. */
2289 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2291 rtvec vec;
2292 int i;
2294 vec = rtvec_alloc (XVECLEN (parallel, 0));
2295 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2297 /* Convert the vector to look just like the original PARALLEL, except
2298 with the computed values. */
2299 for (i = 0; i < XVECLEN (parallel, 0); i++)
2301 rtx e = XVECEXP (parallel, 0, i);
2302 rtx d = XEXP (e, 0);
2304 if (d)
2306 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2307 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2309 RTVEC_ELT (vec, i) = e;
2312 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2315 /* Emit code to move a block SRC to block DST, where SRC and DST are
2316 non-consecutive groups of registers, each represented by a PARALLEL. */
2318 void
2319 emit_group_move (rtx dst, rtx src)
2321 int i;
2323 gcc_assert (GET_CODE (src) == PARALLEL
2324 && GET_CODE (dst) == PARALLEL
2325 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2327 /* Skip first entry if NULL. */
2328 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2329 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2330 XEXP (XVECEXP (src, 0, i), 0));
2333 /* Move a group of registers represented by a PARALLEL into pseudos. */
2336 emit_group_move_into_temps (rtx src)
2338 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2339 int i;
2341 for (i = 0; i < XVECLEN (src, 0); i++)
2343 rtx e = XVECEXP (src, 0, i);
2344 rtx d = XEXP (e, 0);
2346 if (d)
2347 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2348 RTVEC_ELT (vec, i) = e;
2351 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2354 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2355 where SRC is non-consecutive registers represented by a PARALLEL.
2356 SSIZE represents the total size of block ORIG_DST, or -1 if not
2357 known. */
2359 void
2360 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2362 rtx *tmps, dst;
2363 int start, finish, i;
2364 machine_mode m = GET_MODE (orig_dst);
2366 gcc_assert (GET_CODE (src) == PARALLEL);
2368 if (!SCALAR_INT_MODE_P (m)
2369 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2371 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
2372 if (imode == BLKmode)
2373 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2374 else
2375 dst = gen_reg_rtx (imode);
2376 emit_group_store (dst, src, type, ssize);
2377 if (imode != BLKmode)
2378 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2379 emit_move_insn (orig_dst, dst);
2380 return;
2383 /* Check for a NULL entry, used to indicate that the parameter goes
2384 both on the stack and in registers. */
2385 if (XEXP (XVECEXP (src, 0, 0), 0))
2386 start = 0;
2387 else
2388 start = 1;
2389 finish = XVECLEN (src, 0);
2391 tmps = XALLOCAVEC (rtx, finish);
2393 /* Copy the (probable) hard regs into pseudos. */
2394 for (i = start; i < finish; i++)
2396 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2397 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2399 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2400 emit_move_insn (tmps[i], reg);
2402 else
2403 tmps[i] = reg;
2406 /* If we won't be storing directly into memory, protect the real destination
2407 from strange tricks we might play. */
2408 dst = orig_dst;
2409 if (GET_CODE (dst) == PARALLEL)
2411 rtx temp;
2413 /* We can get a PARALLEL dst if there is a conditional expression in
2414 a return statement. In that case, the dst and src are the same,
2415 so no action is necessary. */
2416 if (rtx_equal_p (dst, src))
2417 return;
2419 /* It is unclear if we can ever reach here, but we may as well handle
2420 it. Allocate a temporary, and split this into a store/load to/from
2421 the temporary. */
2422 temp = assign_stack_temp (GET_MODE (dst), ssize);
2423 emit_group_store (temp, src, type, ssize);
2424 emit_group_load (dst, temp, type, ssize);
2425 return;
2427 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2429 machine_mode outer = GET_MODE (dst);
2430 machine_mode inner;
2431 HOST_WIDE_INT bytepos;
2432 bool done = false;
2433 rtx temp;
2435 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2436 dst = gen_reg_rtx (outer);
2438 /* Make life a bit easier for combine. */
2439 /* If the first element of the vector is the low part
2440 of the destination mode, use a paradoxical subreg to
2441 initialize the destination. */
2442 if (start < finish)
2444 inner = GET_MODE (tmps[start]);
2445 bytepos = subreg_lowpart_offset (inner, outer);
2446 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2448 temp = simplify_gen_subreg (outer, tmps[start],
2449 inner, 0);
2450 if (temp)
2452 emit_move_insn (dst, temp);
2453 done = true;
2454 start++;
2459 /* If the first element wasn't the low part, try the last. */
2460 if (!done
2461 && start < finish - 1)
2463 inner = GET_MODE (tmps[finish - 1]);
2464 bytepos = subreg_lowpart_offset (inner, outer);
2465 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2467 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2468 inner, 0);
2469 if (temp)
2471 emit_move_insn (dst, temp);
2472 done = true;
2473 finish--;
2478 /* Otherwise, simply initialize the result to zero. */
2479 if (!done)
2480 emit_move_insn (dst, CONST0_RTX (outer));
2483 /* Process the pieces. */
2484 for (i = start; i < finish; i++)
2486 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2487 machine_mode mode = GET_MODE (tmps[i]);
2488 unsigned int bytelen = GET_MODE_SIZE (mode);
2489 unsigned int adj_bytelen;
2490 rtx dest = dst;
2492 /* Handle trailing fragments that run over the size of the struct. */
2493 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2494 adj_bytelen = ssize - bytepos;
2495 else
2496 adj_bytelen = bytelen;
2498 if (GET_CODE (dst) == CONCAT)
2500 if (bytepos + adj_bytelen
2501 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2502 dest = XEXP (dst, 0);
2503 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2505 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2506 dest = XEXP (dst, 1);
2508 else
2510 machine_mode dest_mode = GET_MODE (dest);
2511 machine_mode tmp_mode = GET_MODE (tmps[i]);
2513 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2515 if (GET_MODE_ALIGNMENT (dest_mode)
2516 >= GET_MODE_ALIGNMENT (tmp_mode))
2518 dest = assign_stack_temp (dest_mode,
2519 GET_MODE_SIZE (dest_mode));
2520 emit_move_insn (adjust_address (dest,
2521 tmp_mode,
2522 bytepos),
2523 tmps[i]);
2524 dst = dest;
2526 else
2528 dest = assign_stack_temp (tmp_mode,
2529 GET_MODE_SIZE (tmp_mode));
2530 emit_move_insn (dest, tmps[i]);
2531 dst = adjust_address (dest, dest_mode, bytepos);
2533 break;
2537 /* Handle trailing fragments that run over the size of the struct. */
2538 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2540 /* store_bit_field always takes its value from the lsb.
2541 Move the fragment to the lsb if it's not already there. */
2542 if (
2543 #ifdef BLOCK_REG_PADDING
2544 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2545 == (BYTES_BIG_ENDIAN ? upward : downward)
2546 #else
2547 BYTES_BIG_ENDIAN
2548 #endif
2551 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2552 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2553 shift, tmps[i], 0);
2556 /* Make sure not to write past the end of the struct. */
2557 store_bit_field (dest,
2558 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2559 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2560 VOIDmode, tmps[i], false);
2563 /* Optimize the access just a bit. */
2564 else if (MEM_P (dest)
2565 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2566 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2567 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2568 && bytelen == GET_MODE_SIZE (mode))
2569 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2571 else
2572 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2573 0, 0, mode, tmps[i], false);
2576 /* Copy from the pseudo into the (probable) hard reg. */
2577 if (orig_dst != dst)
2578 emit_move_insn (orig_dst, dst);
2581 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2582 of the value stored in X. */
2585 maybe_emit_group_store (rtx x, tree type)
2587 machine_mode mode = TYPE_MODE (type);
2588 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2589 if (GET_CODE (x) == PARALLEL)
2591 rtx result = gen_reg_rtx (mode);
2592 emit_group_store (result, x, type, int_size_in_bytes (type));
2593 return result;
2595 return x;
2598 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2600 This is used on targets that return BLKmode values in registers. */
2602 static void
2603 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2605 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2606 rtx src = NULL, dst = NULL;
2607 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2608 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2609 machine_mode mode = GET_MODE (srcreg);
2610 machine_mode tmode = GET_MODE (target);
2611 machine_mode copy_mode;
2613 /* BLKmode registers created in the back-end shouldn't have survived. */
2614 gcc_assert (mode != BLKmode);
2616 /* If the structure doesn't take up a whole number of words, see whether
2617 SRCREG is padded on the left or on the right. If it's on the left,
2618 set PADDING_CORRECTION to the number of bits to skip.
2620 In most ABIs, the structure will be returned at the least end of
2621 the register, which translates to right padding on little-endian
2622 targets and left padding on big-endian targets. The opposite
2623 holds if the structure is returned at the most significant
2624 end of the register. */
2625 if (bytes % UNITS_PER_WORD != 0
2626 && (targetm.calls.return_in_msb (type)
2627 ? !BYTES_BIG_ENDIAN
2628 : BYTES_BIG_ENDIAN))
2629 padding_correction
2630 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2632 /* We can use a single move if we have an exact mode for the size. */
2633 else if (MEM_P (target)
2634 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2635 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2636 && bytes == GET_MODE_SIZE (mode))
2638 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2639 return;
2642 /* And if we additionally have the same mode for a register. */
2643 else if (REG_P (target)
2644 && GET_MODE (target) == mode
2645 && bytes == GET_MODE_SIZE (mode))
2647 emit_move_insn (target, srcreg);
2648 return;
2651 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2652 into a new pseudo which is a full word. */
2653 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2655 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2656 mode = word_mode;
2659 /* Copy the structure BITSIZE bits at a time. If the target lives in
2660 memory, take care of not reading/writing past its end by selecting
2661 a copy mode suited to BITSIZE. This should always be possible given
2662 how it is computed.
2664 If the target lives in register, make sure not to select a copy mode
2665 larger than the mode of the register.
2667 We could probably emit more efficient code for machines which do not use
2668 strict alignment, but it doesn't seem worth the effort at the current
2669 time. */
2671 copy_mode = word_mode;
2672 if (MEM_P (target))
2674 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2675 if (mem_mode != BLKmode)
2676 copy_mode = mem_mode;
2678 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2679 copy_mode = tmode;
2681 for (bitpos = 0, xbitpos = padding_correction;
2682 bitpos < bytes * BITS_PER_UNIT;
2683 bitpos += bitsize, xbitpos += bitsize)
2685 /* We need a new source operand each time xbitpos is on a
2686 word boundary and when xbitpos == padding_correction
2687 (the first time through). */
2688 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2689 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2691 /* We need a new destination operand each time bitpos is on
2692 a word boundary. */
2693 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2694 dst = target;
2695 else if (bitpos % BITS_PER_WORD == 0)
2696 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2698 /* Use xbitpos for the source extraction (right justified) and
2699 bitpos for the destination store (left justified). */
2700 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2701 extract_bit_field (src, bitsize,
2702 xbitpos % BITS_PER_WORD, 1,
2703 NULL_RTX, copy_mode, copy_mode,
2704 false, NULL),
2705 false);
2709 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2710 register if it contains any data, otherwise return null.
2712 This is used on targets that return BLKmode values in registers. */
2715 copy_blkmode_to_reg (machine_mode mode, tree src)
2717 int i, n_regs;
2718 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2719 unsigned int bitsize;
2720 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2721 machine_mode dst_mode;
2723 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2725 x = expand_normal (src);
2727 bytes = int_size_in_bytes (TREE_TYPE (src));
2728 if (bytes == 0)
2729 return NULL_RTX;
2731 /* If the structure doesn't take up a whole number of words, see
2732 whether the register value should be padded on the left or on
2733 the right. Set PADDING_CORRECTION to the number of padding
2734 bits needed on the left side.
2736 In most ABIs, the structure will be returned at the least end of
2737 the register, which translates to right padding on little-endian
2738 targets and left padding on big-endian targets. The opposite
2739 holds if the structure is returned at the most significant
2740 end of the register. */
2741 if (bytes % UNITS_PER_WORD != 0
2742 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2743 ? !BYTES_BIG_ENDIAN
2744 : BYTES_BIG_ENDIAN))
2745 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2746 * BITS_PER_UNIT));
2748 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2749 dst_words = XALLOCAVEC (rtx, n_regs);
2750 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2752 /* Copy the structure BITSIZE bits at a time. */
2753 for (bitpos = 0, xbitpos = padding_correction;
2754 bitpos < bytes * BITS_PER_UNIT;
2755 bitpos += bitsize, xbitpos += bitsize)
2757 /* We need a new destination pseudo each time xbitpos is
2758 on a word boundary and when xbitpos == padding_correction
2759 (the first time through). */
2760 if (xbitpos % BITS_PER_WORD == 0
2761 || xbitpos == padding_correction)
2763 /* Generate an appropriate register. */
2764 dst_word = gen_reg_rtx (word_mode);
2765 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2767 /* Clear the destination before we move anything into it. */
2768 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2771 /* We need a new source operand each time bitpos is on a word
2772 boundary. */
2773 if (bitpos % BITS_PER_WORD == 0)
2774 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2776 /* Use bitpos for the source extraction (left justified) and
2777 xbitpos for the destination store (right justified). */
2778 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2779 0, 0, word_mode,
2780 extract_bit_field (src_word, bitsize,
2781 bitpos % BITS_PER_WORD, 1,
2782 NULL_RTX, word_mode, word_mode,
2783 false, NULL),
2784 false);
2787 if (mode == BLKmode)
2789 /* Find the smallest integer mode large enough to hold the
2790 entire structure. */
2791 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
2792 /* Have we found a large enough mode? */
2793 if (GET_MODE_SIZE (mode) >= bytes)
2794 break;
2796 /* A suitable mode should have been found. */
2797 gcc_assert (mode != VOIDmode);
2800 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2801 dst_mode = word_mode;
2802 else
2803 dst_mode = mode;
2804 dst = gen_reg_rtx (dst_mode);
2806 for (i = 0; i < n_regs; i++)
2807 emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2809 if (mode != dst_mode)
2810 dst = gen_lowpart (mode, dst);
2812 return dst;
2815 /* Add a USE expression for REG to the (possibly empty) list pointed
2816 to by CALL_FUSAGE. REG must denote a hard register. */
2818 void
2819 use_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2821 gcc_assert (REG_P (reg));
2823 if (!HARD_REGISTER_P (reg))
2824 return;
2826 *call_fusage
2827 = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2830 /* Add a CLOBBER expression for REG to the (possibly empty) list pointed
2831 to by CALL_FUSAGE. REG must denote a hard register. */
2833 void
2834 clobber_reg_mode (rtx *call_fusage, rtx reg, machine_mode mode)
2836 gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2838 *call_fusage
2839 = gen_rtx_EXPR_LIST (mode, gen_rtx_CLOBBER (VOIDmode, reg), *call_fusage);
2842 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2843 starting at REGNO. All of these registers must be hard registers. */
2845 void
2846 use_regs (rtx *call_fusage, int regno, int nregs)
2848 int i;
2850 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2852 for (i = 0; i < nregs; i++)
2853 use_reg (call_fusage, regno_reg_rtx[regno + i]);
2856 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2857 PARALLEL REGS. This is for calls that pass values in multiple
2858 non-contiguous locations. The Irix 6 ABI has examples of this. */
2860 void
2861 use_group_regs (rtx *call_fusage, rtx regs)
2863 int i;
2865 for (i = 0; i < XVECLEN (regs, 0); i++)
2867 rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2869 /* A NULL entry means the parameter goes both on the stack and in
2870 registers. This can also be a MEM for targets that pass values
2871 partially on the stack and partially in registers. */
2872 if (reg != 0 && REG_P (reg))
2873 use_reg (call_fusage, reg);
2877 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2878 assigment and the code of the expresion on the RHS is CODE. Return
2879 NULL otherwise. */
2881 static gimple *
2882 get_def_for_expr (tree name, enum tree_code code)
2884 gimple *def_stmt;
2886 if (TREE_CODE (name) != SSA_NAME)
2887 return NULL;
2889 def_stmt = get_gimple_for_ssa_name (name);
2890 if (!def_stmt
2891 || gimple_assign_rhs_code (def_stmt) != code)
2892 return NULL;
2894 return def_stmt;
2897 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2898 assigment and the class of the expresion on the RHS is CLASS. Return
2899 NULL otherwise. */
2901 static gimple *
2902 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2904 gimple *def_stmt;
2906 if (TREE_CODE (name) != SSA_NAME)
2907 return NULL;
2909 def_stmt = get_gimple_for_ssa_name (name);
2910 if (!def_stmt
2911 || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2912 return NULL;
2914 return def_stmt;
2917 /* Write zeros through the storage of OBJECT. If OBJECT has BLKmode, SIZE is
2918 its length in bytes. */
2921 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2922 unsigned int expected_align, HOST_WIDE_INT expected_size,
2923 unsigned HOST_WIDE_INT min_size,
2924 unsigned HOST_WIDE_INT max_size,
2925 unsigned HOST_WIDE_INT probable_max_size)
2927 machine_mode mode = GET_MODE (object);
2928 unsigned int align;
2930 gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2932 /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2933 just move a zero. Otherwise, do this a piece at a time. */
2934 if (mode != BLKmode
2935 && CONST_INT_P (size)
2936 && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2938 rtx zero = CONST0_RTX (mode);
2939 if (zero != NULL)
2941 emit_move_insn (object, zero);
2942 return NULL;
2945 if (COMPLEX_MODE_P (mode))
2947 zero = CONST0_RTX (GET_MODE_INNER (mode));
2948 if (zero != NULL)
2950 write_complex_part (object, zero, 0);
2951 write_complex_part (object, zero, 1);
2952 return NULL;
2957 if (size == const0_rtx)
2958 return NULL;
2960 align = MEM_ALIGN (object);
2962 if (CONST_INT_P (size)
2963 && targetm.use_by_pieces_infrastructure_p (INTVAL (size), align,
2964 CLEAR_BY_PIECES,
2965 optimize_insn_for_speed_p ()))
2966 clear_by_pieces (object, INTVAL (size), align);
2967 else if (set_storage_via_setmem (object, size, const0_rtx, align,
2968 expected_align, expected_size,
2969 min_size, max_size, probable_max_size))
2971 else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2972 return set_storage_via_libcall (object, size, const0_rtx,
2973 method == BLOCK_OP_TAILCALL);
2974 else
2975 gcc_unreachable ();
2977 return NULL;
2981 clear_storage (rtx object, rtx size, enum block_op_methods method)
2983 unsigned HOST_WIDE_INT max, min = 0;
2984 if (GET_CODE (size) == CONST_INT)
2985 min = max = UINTVAL (size);
2986 else
2987 max = GET_MODE_MASK (GET_MODE (size));
2988 return clear_storage_hints (object, size, method, 0, -1, min, max, max);
2992 /* A subroutine of clear_storage. Expand a call to memset.
2993 Return the return value of memset, 0 otherwise. */
2996 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2998 tree call_expr, fn, object_tree, size_tree, val_tree;
2999 machine_mode size_mode;
3001 object = copy_addr_to_reg (XEXP (object, 0));
3002 object_tree = make_tree (ptr_type_node, object);
3004 if (!CONST_INT_P (val))
3005 val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
3006 val_tree = make_tree (integer_type_node, val);
3008 size_mode = TYPE_MODE (sizetype);
3009 size = convert_to_mode (size_mode, size, 1);
3010 size = copy_to_mode_reg (size_mode, size);
3011 size_tree = make_tree (sizetype, size);
3013 /* It is incorrect to use the libcall calling conventions for calls to
3014 memset because it can be provided by the user. */
3015 fn = builtin_decl_implicit (BUILT_IN_MEMSET);
3016 call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
3017 CALL_EXPR_TAILCALL (call_expr) = tailcall;
3019 return expand_call (call_expr, NULL_RTX, false);
3022 /* Expand a setmem pattern; return true if successful. */
3024 bool
3025 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
3026 unsigned int expected_align, HOST_WIDE_INT expected_size,
3027 unsigned HOST_WIDE_INT min_size,
3028 unsigned HOST_WIDE_INT max_size,
3029 unsigned HOST_WIDE_INT probable_max_size)
3031 /* Try the most limited insn first, because there's no point
3032 including more than one in the machine description unless
3033 the more limited one has some advantage. */
3035 machine_mode mode;
3037 if (expected_align < align)
3038 expected_align = align;
3039 if (expected_size != -1)
3041 if ((unsigned HOST_WIDE_INT)expected_size > max_size)
3042 expected_size = max_size;
3043 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
3044 expected_size = min_size;
3047 FOR_EACH_MODE_IN_CLASS (mode, MODE_INT)
3049 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3051 if (code != CODE_FOR_nothing
3052 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3053 here because if SIZE is less than the mode mask, as it is
3054 returned by the macro, it will definitely be less than the
3055 actual mode mask. Since SIZE is within the Pmode address
3056 space, we limit MODE to Pmode. */
3057 && ((CONST_INT_P (size)
3058 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3059 <= (GET_MODE_MASK (mode) >> 1)))
3060 || max_size <= (GET_MODE_MASK (mode) >> 1)
3061 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3063 struct expand_operand ops[9];
3064 unsigned int nops;
3066 nops = insn_data[(int) code].n_generator_args;
3067 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3069 create_fixed_operand (&ops[0], object);
3070 /* The check above guarantees that this size conversion is valid. */
3071 create_convert_operand_to (&ops[1], size, mode, true);
3072 create_convert_operand_from (&ops[2], val, byte_mode, true);
3073 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3074 if (nops >= 6)
3076 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3077 create_integer_operand (&ops[5], expected_size);
3079 if (nops >= 8)
3081 create_integer_operand (&ops[6], min_size);
3082 /* If we can not represent the maximal size,
3083 make parameter NULL. */
3084 if ((HOST_WIDE_INT) max_size != -1)
3085 create_integer_operand (&ops[7], max_size);
3086 else
3087 create_fixed_operand (&ops[7], NULL);
3089 if (nops == 9)
3091 /* If we can not represent the maximal size,
3092 make parameter NULL. */
3093 if ((HOST_WIDE_INT) probable_max_size != -1)
3094 create_integer_operand (&ops[8], probable_max_size);
3095 else
3096 create_fixed_operand (&ops[8], NULL);
3098 if (maybe_expand_insn (code, nops, ops))
3099 return true;
3103 return false;
3107 /* Write to one of the components of the complex value CPLX. Write VAL to
3108 the real part if IMAG_P is false, and the imaginary part if its true. */
3110 void
3111 write_complex_part (rtx cplx, rtx val, bool imag_p)
3113 machine_mode cmode;
3114 machine_mode imode;
3115 unsigned ibitsize;
3117 if (GET_CODE (cplx) == CONCAT)
3119 emit_move_insn (XEXP (cplx, imag_p), val);
3120 return;
3123 cmode = GET_MODE (cplx);
3124 imode = GET_MODE_INNER (cmode);
3125 ibitsize = GET_MODE_BITSIZE (imode);
3127 /* For MEMs simplify_gen_subreg may generate an invalid new address
3128 because, e.g., the original address is considered mode-dependent
3129 by the target, which restricts simplify_subreg from invoking
3130 adjust_address_nv. Instead of preparing fallback support for an
3131 invalid address, we call adjust_address_nv directly. */
3132 if (MEM_P (cplx))
3134 emit_move_insn (adjust_address_nv (cplx, imode,
3135 imag_p ? GET_MODE_SIZE (imode) : 0),
3136 val);
3137 return;
3140 /* If the sub-object is at least word sized, then we know that subregging
3141 will work. This special case is important, since store_bit_field
3142 wants to operate on integer modes, and there's rarely an OImode to
3143 correspond to TCmode. */
3144 if (ibitsize >= BITS_PER_WORD
3145 /* For hard regs we have exact predicates. Assume we can split
3146 the original object if it spans an even number of hard regs.
3147 This special case is important for SCmode on 64-bit platforms
3148 where the natural size of floating-point regs is 32-bit. */
3149 || (REG_P (cplx)
3150 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3151 && REG_NREGS (cplx) % 2 == 0))
3153 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3154 imag_p ? GET_MODE_SIZE (imode) : 0);
3155 if (part)
3157 emit_move_insn (part, val);
3158 return;
3160 else
3161 /* simplify_gen_subreg may fail for sub-word MEMs. */
3162 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3165 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3166 false);
3169 /* Extract one of the components of the complex value CPLX. Extract the
3170 real part if IMAG_P is false, and the imaginary part if it's true. */
3173 read_complex_part (rtx cplx, bool imag_p)
3175 machine_mode cmode, imode;
3176 unsigned ibitsize;
3178 if (GET_CODE (cplx) == CONCAT)
3179 return XEXP (cplx, imag_p);
3181 cmode = GET_MODE (cplx);
3182 imode = GET_MODE_INNER (cmode);
3183 ibitsize = GET_MODE_BITSIZE (imode);
3185 /* Special case reads from complex constants that got spilled to memory. */
3186 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3188 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3189 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3191 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3192 if (CONSTANT_CLASS_P (part))
3193 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3197 /* For MEMs simplify_gen_subreg may generate an invalid new address
3198 because, e.g., the original address is considered mode-dependent
3199 by the target, which restricts simplify_subreg from invoking
3200 adjust_address_nv. Instead of preparing fallback support for an
3201 invalid address, we call adjust_address_nv directly. */
3202 if (MEM_P (cplx))
3203 return adjust_address_nv (cplx, imode,
3204 imag_p ? GET_MODE_SIZE (imode) : 0);
3206 /* If the sub-object is at least word sized, then we know that subregging
3207 will work. This special case is important, since extract_bit_field
3208 wants to operate on integer modes, and there's rarely an OImode to
3209 correspond to TCmode. */
3210 if (ibitsize >= BITS_PER_WORD
3211 /* For hard regs we have exact predicates. Assume we can split
3212 the original object if it spans an even number of hard regs.
3213 This special case is important for SCmode on 64-bit platforms
3214 where the natural size of floating-point regs is 32-bit. */
3215 || (REG_P (cplx)
3216 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3217 && REG_NREGS (cplx) % 2 == 0))
3219 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3220 imag_p ? GET_MODE_SIZE (imode) : 0);
3221 if (ret)
3222 return ret;
3223 else
3224 /* simplify_gen_subreg may fail for sub-word MEMs. */
3225 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3228 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3229 true, NULL_RTX, imode, imode, false, NULL);
3232 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3233 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3234 represented in NEW_MODE. If FORCE is true, this will never happen, as
3235 we'll force-create a SUBREG if needed. */
3237 static rtx
3238 emit_move_change_mode (machine_mode new_mode,
3239 machine_mode old_mode, rtx x, bool force)
3241 rtx ret;
3243 if (push_operand (x, GET_MODE (x)))
3245 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3246 MEM_COPY_ATTRIBUTES (ret, x);
3248 else if (MEM_P (x))
3250 /* We don't have to worry about changing the address since the
3251 size in bytes is supposed to be the same. */
3252 if (reload_in_progress)
3254 /* Copy the MEM to change the mode and move any
3255 substitutions from the old MEM to the new one. */
3256 ret = adjust_address_nv (x, new_mode, 0);
3257 copy_replacements (x, ret);
3259 else
3260 ret = adjust_address (x, new_mode, 0);
3262 else
3264 /* Note that we do want simplify_subreg's behavior of validating
3265 that the new mode is ok for a hard register. If we were to use
3266 simplify_gen_subreg, we would create the subreg, but would
3267 probably run into the target not being able to implement it. */
3268 /* Except, of course, when FORCE is true, when this is exactly what
3269 we want. Which is needed for CCmodes on some targets. */
3270 if (force)
3271 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3272 else
3273 ret = simplify_subreg (new_mode, x, old_mode, 0);
3276 return ret;
3279 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3280 an integer mode of the same size as MODE. Returns the instruction
3281 emitted, or NULL if such a move could not be generated. */
3283 static rtx_insn *
3284 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3286 machine_mode imode;
3287 enum insn_code code;
3289 /* There must exist a mode of the exact size we require. */
3290 imode = int_mode_for_mode (mode);
3291 if (imode == BLKmode)
3292 return NULL;
3294 /* The target must support moves in this mode. */
3295 code = optab_handler (mov_optab, imode);
3296 if (code == CODE_FOR_nothing)
3297 return NULL;
3299 x = emit_move_change_mode (imode, mode, x, force);
3300 if (x == NULL_RTX)
3301 return NULL;
3302 y = emit_move_change_mode (imode, mode, y, force);
3303 if (y == NULL_RTX)
3304 return NULL;
3305 return emit_insn (GEN_FCN (code) (x, y));
3308 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3309 Return an equivalent MEM that does not use an auto-increment. */
3312 emit_move_resolve_push (machine_mode mode, rtx x)
3314 enum rtx_code code = GET_CODE (XEXP (x, 0));
3315 HOST_WIDE_INT adjust;
3316 rtx temp;
3318 adjust = GET_MODE_SIZE (mode);
3319 #ifdef PUSH_ROUNDING
3320 adjust = PUSH_ROUNDING (adjust);
3321 #endif
3322 if (code == PRE_DEC || code == POST_DEC)
3323 adjust = -adjust;
3324 else if (code == PRE_MODIFY || code == POST_MODIFY)
3326 rtx expr = XEXP (XEXP (x, 0), 1);
3327 HOST_WIDE_INT val;
3329 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3330 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3331 val = INTVAL (XEXP (expr, 1));
3332 if (GET_CODE (expr) == MINUS)
3333 val = -val;
3334 gcc_assert (adjust == val || adjust == -val);
3335 adjust = val;
3338 /* Do not use anti_adjust_stack, since we don't want to update
3339 stack_pointer_delta. */
3340 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3341 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3342 0, OPTAB_LIB_WIDEN);
3343 if (temp != stack_pointer_rtx)
3344 emit_move_insn (stack_pointer_rtx, temp);
3346 switch (code)
3348 case PRE_INC:
3349 case PRE_DEC:
3350 case PRE_MODIFY:
3351 temp = stack_pointer_rtx;
3352 break;
3353 case POST_INC:
3354 case POST_DEC:
3355 case POST_MODIFY:
3356 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3357 break;
3358 default:
3359 gcc_unreachable ();
3362 return replace_equiv_address (x, temp);
3365 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3366 X is known to satisfy push_operand, and MODE is known to be complex.
3367 Returns the last instruction emitted. */
3369 rtx_insn *
3370 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3372 machine_mode submode = GET_MODE_INNER (mode);
3373 bool imag_first;
3375 #ifdef PUSH_ROUNDING
3376 unsigned int submodesize = GET_MODE_SIZE (submode);
3378 /* In case we output to the stack, but the size is smaller than the
3379 machine can push exactly, we need to use move instructions. */
3380 if (PUSH_ROUNDING (submodesize) != submodesize)
3382 x = emit_move_resolve_push (mode, x);
3383 return emit_move_insn (x, y);
3385 #endif
3387 /* Note that the real part always precedes the imag part in memory
3388 regardless of machine's endianness. */
3389 switch (GET_CODE (XEXP (x, 0)))
3391 case PRE_DEC:
3392 case POST_DEC:
3393 imag_first = true;
3394 break;
3395 case PRE_INC:
3396 case POST_INC:
3397 imag_first = false;
3398 break;
3399 default:
3400 gcc_unreachable ();
3403 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3404 read_complex_part (y, imag_first));
3405 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3406 read_complex_part (y, !imag_first));
3409 /* A subroutine of emit_move_complex. Perform the move from Y to X
3410 via two moves of the parts. Returns the last instruction emitted. */
3412 rtx_insn *
3413 emit_move_complex_parts (rtx x, rtx y)
3415 /* Show the output dies here. This is necessary for SUBREGs
3416 of pseudos since we cannot track their lifetimes correctly;
3417 hard regs shouldn't appear here except as return values. */
3418 if (!reload_completed && !reload_in_progress
3419 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3420 emit_clobber (x);
3422 write_complex_part (x, read_complex_part (y, false), false);
3423 write_complex_part (x, read_complex_part (y, true), true);
3425 return get_last_insn ();
3428 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3429 MODE is known to be complex. Returns the last instruction emitted. */
3431 static rtx_insn *
3432 emit_move_complex (machine_mode mode, rtx x, rtx y)
3434 bool try_int;
3436 /* Need to take special care for pushes, to maintain proper ordering
3437 of the data, and possibly extra padding. */
3438 if (push_operand (x, mode))
3439 return emit_move_complex_push (mode, x, y);
3441 /* See if we can coerce the target into moving both values at once, except
3442 for floating point where we favor moving as parts if this is easy. */
3443 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3444 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3445 && !(REG_P (x)
3446 && HARD_REGISTER_P (x)
3447 && REG_NREGS (x) == 1)
3448 && !(REG_P (y)
3449 && HARD_REGISTER_P (y)
3450 && REG_NREGS (y) == 1))
3451 try_int = false;
3452 /* Not possible if the values are inherently not adjacent. */
3453 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3454 try_int = false;
3455 /* Is possible if both are registers (or subregs of registers). */
3456 else if (register_operand (x, mode) && register_operand (y, mode))
3457 try_int = true;
3458 /* If one of the operands is a memory, and alignment constraints
3459 are friendly enough, we may be able to do combined memory operations.
3460 We do not attempt this if Y is a constant because that combination is
3461 usually better with the by-parts thing below. */
3462 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3463 && (!STRICT_ALIGNMENT
3464 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3465 try_int = true;
3466 else
3467 try_int = false;
3469 if (try_int)
3471 rtx_insn *ret;
3473 /* For memory to memory moves, optimal behavior can be had with the
3474 existing block move logic. */
3475 if (MEM_P (x) && MEM_P (y))
3477 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3478 BLOCK_OP_NO_LIBCALL);
3479 return get_last_insn ();
3482 ret = emit_move_via_integer (mode, x, y, true);
3483 if (ret)
3484 return ret;
3487 return emit_move_complex_parts (x, y);
3490 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3491 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3493 static rtx_insn *
3494 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3496 rtx_insn *ret;
3498 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3499 if (mode != CCmode)
3501 enum insn_code code = optab_handler (mov_optab, CCmode);
3502 if (code != CODE_FOR_nothing)
3504 x = emit_move_change_mode (CCmode, mode, x, true);
3505 y = emit_move_change_mode (CCmode, mode, y, true);
3506 return emit_insn (GEN_FCN (code) (x, y));
3510 /* Otherwise, find the MODE_INT mode of the same width. */
3511 ret = emit_move_via_integer (mode, x, y, false);
3512 gcc_assert (ret != NULL);
3513 return ret;
3516 /* Return true if word I of OP lies entirely in the
3517 undefined bits of a paradoxical subreg. */
3519 static bool
3520 undefined_operand_subword_p (const_rtx op, int i)
3522 machine_mode innermode, innermostmode;
3523 int offset;
3524 if (GET_CODE (op) != SUBREG)
3525 return false;
3526 innermode = GET_MODE (op);
3527 innermostmode = GET_MODE (SUBREG_REG (op));
3528 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3529 /* The SUBREG_BYTE represents offset, as if the value were stored in
3530 memory, except for a paradoxical subreg where we define
3531 SUBREG_BYTE to be 0; undo this exception as in
3532 simplify_subreg. */
3533 if (SUBREG_BYTE (op) == 0
3534 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3536 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3537 if (WORDS_BIG_ENDIAN)
3538 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3539 if (BYTES_BIG_ENDIAN)
3540 offset += difference % UNITS_PER_WORD;
3542 if (offset >= GET_MODE_SIZE (innermostmode)
3543 || offset <= -GET_MODE_SIZE (word_mode))
3544 return true;
3545 return false;
3548 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3549 MODE is any multi-word or full-word mode that lacks a move_insn
3550 pattern. Note that you will get better code if you define such
3551 patterns, even if they must turn into multiple assembler instructions. */
3553 static rtx_insn *
3554 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3556 rtx_insn *last_insn = 0;
3557 rtx_insn *seq;
3558 rtx inner;
3559 bool need_clobber;
3560 int i;
3562 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3564 /* If X is a push on the stack, do the push now and replace
3565 X with a reference to the stack pointer. */
3566 if (push_operand (x, mode))
3567 x = emit_move_resolve_push (mode, x);
3569 /* If we are in reload, see if either operand is a MEM whose address
3570 is scheduled for replacement. */
3571 if (reload_in_progress && MEM_P (x)
3572 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3573 x = replace_equiv_address_nv (x, inner);
3574 if (reload_in_progress && MEM_P (y)
3575 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3576 y = replace_equiv_address_nv (y, inner);
3578 start_sequence ();
3580 need_clobber = false;
3581 for (i = 0;
3582 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3583 i++)
3585 rtx xpart = operand_subword (x, i, 1, mode);
3586 rtx ypart;
3588 /* Do not generate code for a move if it would come entirely
3589 from the undefined bits of a paradoxical subreg. */
3590 if (undefined_operand_subword_p (y, i))
3591 continue;
3593 ypart = operand_subword (y, i, 1, mode);
3595 /* If we can't get a part of Y, put Y into memory if it is a
3596 constant. Otherwise, force it into a register. Then we must
3597 be able to get a part of Y. */
3598 if (ypart == 0 && CONSTANT_P (y))
3600 y = use_anchored_address (force_const_mem (mode, y));
3601 ypart = operand_subword (y, i, 1, mode);
3603 else if (ypart == 0)
3604 ypart = operand_subword_force (y, i, mode);
3606 gcc_assert (xpart && ypart);
3608 need_clobber |= (GET_CODE (xpart) == SUBREG);
3610 last_insn = emit_move_insn (xpart, ypart);
3613 seq = get_insns ();
3614 end_sequence ();
3616 /* Show the output dies here. This is necessary for SUBREGs
3617 of pseudos since we cannot track their lifetimes correctly;
3618 hard regs shouldn't appear here except as return values.
3619 We never want to emit such a clobber after reload. */
3620 if (x != y
3621 && ! (reload_in_progress || reload_completed)
3622 && need_clobber != 0)
3623 emit_clobber (x);
3625 emit_insn (seq);
3627 return last_insn;
3630 /* Low level part of emit_move_insn.
3631 Called just like emit_move_insn, but assumes X and Y
3632 are basically valid. */
3634 rtx_insn *
3635 emit_move_insn_1 (rtx x, rtx y)
3637 machine_mode mode = GET_MODE (x);
3638 enum insn_code code;
3640 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3642 code = optab_handler (mov_optab, mode);
3643 if (code != CODE_FOR_nothing)
3644 return emit_insn (GEN_FCN (code) (x, y));
3646 /* Expand complex moves by moving real part and imag part. */
3647 if (COMPLEX_MODE_P (mode))
3648 return emit_move_complex (mode, x, y);
3650 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3651 || ALL_FIXED_POINT_MODE_P (mode))
3653 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3655 /* If we can't find an integer mode, use multi words. */
3656 if (result)
3657 return result;
3658 else
3659 return emit_move_multi_word (mode, x, y);
3662 if (GET_MODE_CLASS (mode) == MODE_CC)
3663 return emit_move_ccmode (mode, x, y);
3665 /* Try using a move pattern for the corresponding integer mode. This is
3666 only safe when simplify_subreg can convert MODE constants into integer
3667 constants. At present, it can only do this reliably if the value
3668 fits within a HOST_WIDE_INT. */
3669 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3671 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3673 if (ret)
3675 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3676 return ret;
3680 return emit_move_multi_word (mode, x, y);
3683 /* Generate code to copy Y into X.
3684 Both Y and X must have the same mode, except that
3685 Y can be a constant with VOIDmode.
3686 This mode cannot be BLKmode; use emit_block_move for that.
3688 Return the last instruction emitted. */
3690 rtx_insn *
3691 emit_move_insn (rtx x, rtx y)
3693 machine_mode mode = GET_MODE (x);
3694 rtx y_cst = NULL_RTX;
3695 rtx_insn *last_insn;
3696 rtx set;
3698 gcc_assert (mode != BLKmode
3699 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3701 if (CONSTANT_P (y))
3703 if (optimize
3704 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3705 && (last_insn = compress_float_constant (x, y)))
3706 return last_insn;
3708 y_cst = y;
3710 if (!targetm.legitimate_constant_p (mode, y))
3712 y = force_const_mem (mode, y);
3714 /* If the target's cannot_force_const_mem prevented the spill,
3715 assume that the target's move expanders will also take care
3716 of the non-legitimate constant. */
3717 if (!y)
3718 y = y_cst;
3719 else
3720 y = use_anchored_address (y);
3724 /* If X or Y are memory references, verify that their addresses are valid
3725 for the machine. */
3726 if (MEM_P (x)
3727 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3728 MEM_ADDR_SPACE (x))
3729 && ! push_operand (x, GET_MODE (x))))
3730 x = validize_mem (x);
3732 if (MEM_P (y)
3733 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3734 MEM_ADDR_SPACE (y)))
3735 y = validize_mem (y);
3737 gcc_assert (mode != BLKmode);
3739 last_insn = emit_move_insn_1 (x, y);
3741 if (y_cst && REG_P (x)
3742 && (set = single_set (last_insn)) != NULL_RTX
3743 && SET_DEST (set) == x
3744 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3745 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3747 return last_insn;
3750 /* Generate the body of an instruction to copy Y into X.
3751 It may be a list of insns, if one insn isn't enough. */
3753 rtx_insn *
3754 gen_move_insn (rtx x, rtx y)
3756 rtx_insn *seq;
3758 start_sequence ();
3759 emit_move_insn_1 (x, y);
3760 seq = get_insns ();
3761 end_sequence ();
3762 return seq;
3765 /* If Y is representable exactly in a narrower mode, and the target can
3766 perform the extension directly from constant or memory, then emit the
3767 move as an extension. */
3769 static rtx_insn *
3770 compress_float_constant (rtx x, rtx y)
3772 machine_mode dstmode = GET_MODE (x);
3773 machine_mode orig_srcmode = GET_MODE (y);
3774 machine_mode srcmode;
3775 const REAL_VALUE_TYPE *r;
3776 int oldcost, newcost;
3777 bool speed = optimize_insn_for_speed_p ();
3779 r = CONST_DOUBLE_REAL_VALUE (y);
3781 if (targetm.legitimate_constant_p (dstmode, y))
3782 oldcost = set_src_cost (y, orig_srcmode, speed);
3783 else
3784 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3786 FOR_EACH_MODE_UNTIL (srcmode, orig_srcmode)
3788 enum insn_code ic;
3789 rtx trunc_y;
3790 rtx_insn *last_insn;
3792 /* Skip if the target can't extend this way. */
3793 ic = can_extend_p (dstmode, srcmode, 0);
3794 if (ic == CODE_FOR_nothing)
3795 continue;
3797 /* Skip if the narrowed value isn't exact. */
3798 if (! exact_real_truncate (srcmode, r))
3799 continue;
3801 trunc_y = const_double_from_real_value (*r, srcmode);
3803 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3805 /* Skip if the target needs extra instructions to perform
3806 the extension. */
3807 if (!insn_operand_matches (ic, 1, trunc_y))
3808 continue;
3809 /* This is valid, but may not be cheaper than the original. */
3810 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3811 dstmode, speed);
3812 if (oldcost < newcost)
3813 continue;
3815 else if (float_extend_from_mem[dstmode][srcmode])
3817 trunc_y = force_const_mem (srcmode, trunc_y);
3818 /* This is valid, but may not be cheaper than the original. */
3819 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3820 dstmode, speed);
3821 if (oldcost < newcost)
3822 continue;
3823 trunc_y = validize_mem (trunc_y);
3825 else
3826 continue;
3828 /* For CSE's benefit, force the compressed constant pool entry
3829 into a new pseudo. This constant may be used in different modes,
3830 and if not, combine will put things back together for us. */
3831 trunc_y = force_reg (srcmode, trunc_y);
3833 /* If x is a hard register, perform the extension into a pseudo,
3834 so that e.g. stack realignment code is aware of it. */
3835 rtx target = x;
3836 if (REG_P (x) && HARD_REGISTER_P (x))
3837 target = gen_reg_rtx (dstmode);
3839 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3840 last_insn = get_last_insn ();
3842 if (REG_P (target))
3843 set_unique_reg_note (last_insn, REG_EQUAL, y);
3845 if (target != x)
3846 return emit_move_insn (x, target);
3847 return last_insn;
3850 return NULL;
3853 /* Pushing data onto the stack. */
3855 /* Push a block of length SIZE (perhaps variable)
3856 and return an rtx to address the beginning of the block.
3857 The value may be virtual_outgoing_args_rtx.
3859 EXTRA is the number of bytes of padding to push in addition to SIZE.
3860 BELOW nonzero means this padding comes at low addresses;
3861 otherwise, the padding comes at high addresses. */
3864 push_block (rtx size, int extra, int below)
3866 rtx temp;
3868 size = convert_modes (Pmode, ptr_mode, size, 1);
3869 if (CONSTANT_P (size))
3870 anti_adjust_stack (plus_constant (Pmode, size, extra));
3871 else if (REG_P (size) && extra == 0)
3872 anti_adjust_stack (size);
3873 else
3875 temp = copy_to_mode_reg (Pmode, size);
3876 if (extra != 0)
3877 temp = expand_binop (Pmode, add_optab, temp,
3878 gen_int_mode (extra, Pmode),
3879 temp, 0, OPTAB_LIB_WIDEN);
3880 anti_adjust_stack (temp);
3883 if (STACK_GROWS_DOWNWARD)
3885 temp = virtual_outgoing_args_rtx;
3886 if (extra != 0 && below)
3887 temp = plus_constant (Pmode, temp, extra);
3889 else
3891 if (CONST_INT_P (size))
3892 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3893 -INTVAL (size) - (below ? 0 : extra));
3894 else if (extra != 0 && !below)
3895 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3896 negate_rtx (Pmode, plus_constant (Pmode, size,
3897 extra)));
3898 else
3899 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3900 negate_rtx (Pmode, size));
3903 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3906 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3908 static rtx
3909 mem_autoinc_base (rtx mem)
3911 if (MEM_P (mem))
3913 rtx addr = XEXP (mem, 0);
3914 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3915 return XEXP (addr, 0);
3917 return NULL;
3920 /* A utility routine used here, in reload, and in try_split. The insns
3921 after PREV up to and including LAST are known to adjust the stack,
3922 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3923 placing notes as appropriate. PREV may be NULL, indicating the
3924 entire insn sequence prior to LAST should be scanned.
3926 The set of allowed stack pointer modifications is small:
3927 (1) One or more auto-inc style memory references (aka pushes),
3928 (2) One or more addition/subtraction with the SP as destination,
3929 (3) A single move insn with the SP as destination,
3930 (4) A call_pop insn,
3931 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3933 Insns in the sequence that do not modify the SP are ignored,
3934 except for noreturn calls.
3936 The return value is the amount of adjustment that can be trivially
3937 verified, via immediate operand or auto-inc. If the adjustment
3938 cannot be trivially extracted, the return value is INT_MIN. */
3940 HOST_WIDE_INT
3941 find_args_size_adjust (rtx_insn *insn)
3943 rtx dest, set, pat;
3944 int i;
3946 pat = PATTERN (insn);
3947 set = NULL;
3949 /* Look for a call_pop pattern. */
3950 if (CALL_P (insn))
3952 /* We have to allow non-call_pop patterns for the case
3953 of emit_single_push_insn of a TLS address. */
3954 if (GET_CODE (pat) != PARALLEL)
3955 return 0;
3957 /* All call_pop have a stack pointer adjust in the parallel.
3958 The call itself is always first, and the stack adjust is
3959 usually last, so search from the end. */
3960 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3962 set = XVECEXP (pat, 0, i);
3963 if (GET_CODE (set) != SET)
3964 continue;
3965 dest = SET_DEST (set);
3966 if (dest == stack_pointer_rtx)
3967 break;
3969 /* We'd better have found the stack pointer adjust. */
3970 if (i == 0)
3971 return 0;
3972 /* Fall through to process the extracted SET and DEST
3973 as if it was a standalone insn. */
3975 else if (GET_CODE (pat) == SET)
3976 set = pat;
3977 else if ((set = single_set (insn)) != NULL)
3979 else if (GET_CODE (pat) == PARALLEL)
3981 /* ??? Some older ports use a parallel with a stack adjust
3982 and a store for a PUSH_ROUNDING pattern, rather than a
3983 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3984 /* ??? See h8300 and m68k, pushqi1. */
3985 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3987 set = XVECEXP (pat, 0, i);
3988 if (GET_CODE (set) != SET)
3989 continue;
3990 dest = SET_DEST (set);
3991 if (dest == stack_pointer_rtx)
3992 break;
3994 /* We do not expect an auto-inc of the sp in the parallel. */
3995 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3996 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3997 != stack_pointer_rtx);
3999 if (i < 0)
4000 return 0;
4002 else
4003 return 0;
4005 dest = SET_DEST (set);
4007 /* Look for direct modifications of the stack pointer. */
4008 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4010 /* Look for a trivial adjustment, otherwise assume nothing. */
4011 /* Note that the SPU restore_stack_block pattern refers to
4012 the stack pointer in V4SImode. Consider that non-trivial. */
4013 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4014 && GET_CODE (SET_SRC (set)) == PLUS
4015 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4016 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4017 return INTVAL (XEXP (SET_SRC (set), 1));
4018 /* ??? Reload can generate no-op moves, which will be cleaned
4019 up later. Recognize it and continue searching. */
4020 else if (rtx_equal_p (dest, SET_SRC (set)))
4021 return 0;
4022 else
4023 return HOST_WIDE_INT_MIN;
4025 else
4027 rtx mem, addr;
4029 /* Otherwise only think about autoinc patterns. */
4030 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4032 mem = dest;
4033 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4034 != stack_pointer_rtx);
4036 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4037 mem = SET_SRC (set);
4038 else
4039 return 0;
4041 addr = XEXP (mem, 0);
4042 switch (GET_CODE (addr))
4044 case PRE_INC:
4045 case POST_INC:
4046 return GET_MODE_SIZE (GET_MODE (mem));
4047 case PRE_DEC:
4048 case POST_DEC:
4049 return -GET_MODE_SIZE (GET_MODE (mem));
4050 case PRE_MODIFY:
4051 case POST_MODIFY:
4052 addr = XEXP (addr, 1);
4053 gcc_assert (GET_CODE (addr) == PLUS);
4054 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4055 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4056 return INTVAL (XEXP (addr, 1));
4057 default:
4058 gcc_unreachable ();
4064 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4066 int args_size = end_args_size;
4067 bool saw_unknown = false;
4068 rtx_insn *insn;
4070 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4072 HOST_WIDE_INT this_delta;
4074 if (!NONDEBUG_INSN_P (insn))
4075 continue;
4077 this_delta = find_args_size_adjust (insn);
4078 if (this_delta == 0)
4080 if (!CALL_P (insn)
4081 || ACCUMULATE_OUTGOING_ARGS
4082 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4083 continue;
4086 gcc_assert (!saw_unknown);
4087 if (this_delta == HOST_WIDE_INT_MIN)
4088 saw_unknown = true;
4090 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4091 if (STACK_GROWS_DOWNWARD)
4092 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4094 args_size -= this_delta;
4097 return saw_unknown ? INT_MIN : args_size;
4100 #ifdef PUSH_ROUNDING
4101 /* Emit single push insn. */
4103 static void
4104 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4106 rtx dest_addr;
4107 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4108 rtx dest;
4109 enum insn_code icode;
4111 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4112 /* If there is push pattern, use it. Otherwise try old way of throwing
4113 MEM representing push operation to move expander. */
4114 icode = optab_handler (push_optab, mode);
4115 if (icode != CODE_FOR_nothing)
4117 struct expand_operand ops[1];
4119 create_input_operand (&ops[0], x, mode);
4120 if (maybe_expand_insn (icode, 1, ops))
4121 return;
4123 if (GET_MODE_SIZE (mode) == rounded_size)
4124 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4125 /* If we are to pad downward, adjust the stack pointer first and
4126 then store X into the stack location using an offset. This is
4127 because emit_move_insn does not know how to pad; it does not have
4128 access to type. */
4129 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4131 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4132 HOST_WIDE_INT offset;
4134 emit_move_insn (stack_pointer_rtx,
4135 expand_binop (Pmode,
4136 STACK_GROWS_DOWNWARD ? sub_optab
4137 : add_optab,
4138 stack_pointer_rtx,
4139 gen_int_mode (rounded_size, Pmode),
4140 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4142 offset = (HOST_WIDE_INT) padding_size;
4143 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4144 /* We have already decremented the stack pointer, so get the
4145 previous value. */
4146 offset += (HOST_WIDE_INT) rounded_size;
4148 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4149 /* We have already incremented the stack pointer, so get the
4150 previous value. */
4151 offset -= (HOST_WIDE_INT) rounded_size;
4153 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4154 gen_int_mode (offset, Pmode));
4156 else
4158 if (STACK_GROWS_DOWNWARD)
4159 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4160 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4161 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4162 Pmode));
4163 else
4164 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4165 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4166 gen_int_mode (rounded_size, Pmode));
4168 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4171 dest = gen_rtx_MEM (mode, dest_addr);
4173 if (type != 0)
4175 set_mem_attributes (dest, type, 1);
4177 if (cfun->tail_call_marked)
4178 /* Function incoming arguments may overlap with sibling call
4179 outgoing arguments and we cannot allow reordering of reads
4180 from function arguments with stores to outgoing arguments
4181 of sibling calls. */
4182 set_mem_alias_set (dest, 0);
4184 emit_move_insn (dest, x);
4187 /* Emit and annotate a single push insn. */
4189 static void
4190 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4192 int delta, old_delta = stack_pointer_delta;
4193 rtx_insn *prev = get_last_insn ();
4194 rtx_insn *last;
4196 emit_single_push_insn_1 (mode, x, type);
4198 last = get_last_insn ();
4200 /* Notice the common case where we emitted exactly one insn. */
4201 if (PREV_INSN (last) == prev)
4203 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4204 return;
4207 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4208 gcc_assert (delta == INT_MIN || delta == old_delta);
4210 #endif
4212 /* If reading SIZE bytes from X will end up reading from
4213 Y return the number of bytes that overlap. Return -1
4214 if there is no overlap or -2 if we can't determine
4215 (for example when X and Y have different base registers). */
4217 static int
4218 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4220 rtx tmp = plus_constant (Pmode, x, size);
4221 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4223 if (!CONST_INT_P (sub))
4224 return -2;
4226 HOST_WIDE_INT val = INTVAL (sub);
4228 return IN_RANGE (val, 1, size) ? val : -1;
4231 /* Generate code to push X onto the stack, assuming it has mode MODE and
4232 type TYPE.
4233 MODE is redundant except when X is a CONST_INT (since they don't
4234 carry mode info).
4235 SIZE is an rtx for the size of data to be copied (in bytes),
4236 needed only if X is BLKmode.
4237 Return true if successful. May return false if asked to push a
4238 partial argument during a sibcall optimization (as specified by
4239 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4240 to not overlap.
4242 ALIGN (in bits) is maximum alignment we can assume.
4244 If PARTIAL and REG are both nonzero, then copy that many of the first
4245 bytes of X into registers starting with REG, and push the rest of X.
4246 The amount of space pushed is decreased by PARTIAL bytes.
4247 REG must be a hard register in this case.
4248 If REG is zero but PARTIAL is not, take any all others actions for an
4249 argument partially in registers, but do not actually load any
4250 registers.
4252 EXTRA is the amount in bytes of extra space to leave next to this arg.
4253 This is ignored if an argument block has already been allocated.
4255 On a machine that lacks real push insns, ARGS_ADDR is the address of
4256 the bottom of the argument block for this call. We use indexing off there
4257 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4258 argument block has not been preallocated.
4260 ARGS_SO_FAR is the size of args previously pushed for this call.
4262 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4263 for arguments passed in registers. If nonzero, it will be the number
4264 of bytes required. */
4266 bool
4267 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4268 unsigned int align, int partial, rtx reg, int extra,
4269 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4270 rtx alignment_pad, bool sibcall_p)
4272 rtx xinner;
4273 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4275 /* Decide where to pad the argument: `downward' for below,
4276 `upward' for above, or `none' for don't pad it.
4277 Default is below for small data on big-endian machines; else above. */
4278 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4280 /* Invert direction if stack is post-decrement.
4281 FIXME: why? */
4282 if (STACK_PUSH_CODE == POST_DEC)
4283 if (where_pad != none)
4284 where_pad = (where_pad == downward ? upward : downward);
4286 xinner = x;
4288 int nregs = partial / UNITS_PER_WORD;
4289 rtx *tmp_regs = NULL;
4290 int overlapping = 0;
4292 if (mode == BLKmode
4293 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4295 /* Copy a block into the stack, entirely or partially. */
4297 rtx temp;
4298 int used;
4299 int offset;
4300 int skip;
4302 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4303 used = partial - offset;
4305 if (mode != BLKmode)
4307 /* A value is to be stored in an insufficiently aligned
4308 stack slot; copy via a suitably aligned slot if
4309 necessary. */
4310 size = GEN_INT (GET_MODE_SIZE (mode));
4311 if (!MEM_P (xinner))
4313 temp = assign_temp (type, 1, 1);
4314 emit_move_insn (temp, xinner);
4315 xinner = temp;
4319 gcc_assert (size);
4321 /* USED is now the # of bytes we need not copy to the stack
4322 because registers will take care of them. */
4324 if (partial != 0)
4325 xinner = adjust_address (xinner, BLKmode, used);
4327 /* If the partial register-part of the arg counts in its stack size,
4328 skip the part of stack space corresponding to the registers.
4329 Otherwise, start copying to the beginning of the stack space,
4330 by setting SKIP to 0. */
4331 skip = (reg_parm_stack_space == 0) ? 0 : used;
4333 #ifdef PUSH_ROUNDING
4334 /* Do it with several push insns if that doesn't take lots of insns
4335 and if there is no difficulty with push insns that skip bytes
4336 on the stack for alignment purposes. */
4337 if (args_addr == 0
4338 && PUSH_ARGS
4339 && CONST_INT_P (size)
4340 && skip == 0
4341 && MEM_ALIGN (xinner) >= align
4342 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4343 /* Here we avoid the case of a structure whose weak alignment
4344 forces many pushes of a small amount of data,
4345 and such small pushes do rounding that causes trouble. */
4346 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4347 || align >= BIGGEST_ALIGNMENT
4348 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4349 == (align / BITS_PER_UNIT)))
4350 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4352 /* Push padding now if padding above and stack grows down,
4353 or if padding below and stack grows up.
4354 But if space already allocated, this has already been done. */
4355 if (extra && args_addr == 0
4356 && where_pad != none && where_pad != stack_direction)
4357 anti_adjust_stack (GEN_INT (extra));
4359 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4361 else
4362 #endif /* PUSH_ROUNDING */
4364 rtx target;
4366 /* Otherwise make space on the stack and copy the data
4367 to the address of that space. */
4369 /* Deduct words put into registers from the size we must copy. */
4370 if (partial != 0)
4372 if (CONST_INT_P (size))
4373 size = GEN_INT (INTVAL (size) - used);
4374 else
4375 size = expand_binop (GET_MODE (size), sub_optab, size,
4376 gen_int_mode (used, GET_MODE (size)),
4377 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4380 /* Get the address of the stack space.
4381 In this case, we do not deal with EXTRA separately.
4382 A single stack adjust will do. */
4383 if (! args_addr)
4385 temp = push_block (size, extra, where_pad == downward);
4386 extra = 0;
4388 else if (CONST_INT_P (args_so_far))
4389 temp = memory_address (BLKmode,
4390 plus_constant (Pmode, args_addr,
4391 skip + INTVAL (args_so_far)));
4392 else
4393 temp = memory_address (BLKmode,
4394 plus_constant (Pmode,
4395 gen_rtx_PLUS (Pmode,
4396 args_addr,
4397 args_so_far),
4398 skip));
4400 if (!ACCUMULATE_OUTGOING_ARGS)
4402 /* If the source is referenced relative to the stack pointer,
4403 copy it to another register to stabilize it. We do not need
4404 to do this if we know that we won't be changing sp. */
4406 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4407 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4408 temp = copy_to_reg (temp);
4411 target = gen_rtx_MEM (BLKmode, temp);
4413 /* We do *not* set_mem_attributes here, because incoming arguments
4414 may overlap with sibling call outgoing arguments and we cannot
4415 allow reordering of reads from function arguments with stores
4416 to outgoing arguments of sibling calls. We do, however, want
4417 to record the alignment of the stack slot. */
4418 /* ALIGN may well be better aligned than TYPE, e.g. due to
4419 PARM_BOUNDARY. Assume the caller isn't lying. */
4420 set_mem_align (target, align);
4422 /* If part should go in registers and pushing to that part would
4423 overwrite some of the values that need to go into regs, load the
4424 overlapping values into temporary pseudos to be moved into the hard
4425 regs at the end after the stack pushing has completed.
4426 We cannot load them directly into the hard regs here because
4427 they can be clobbered by the block move expansions.
4428 See PR 65358. */
4430 if (partial > 0 && reg != 0 && mode == BLKmode
4431 && GET_CODE (reg) != PARALLEL)
4433 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4434 if (overlapping > 0)
4436 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4437 overlapping /= UNITS_PER_WORD;
4439 tmp_regs = XALLOCAVEC (rtx, overlapping);
4441 for (int i = 0; i < overlapping; i++)
4442 tmp_regs[i] = gen_reg_rtx (word_mode);
4444 for (int i = 0; i < overlapping; i++)
4445 emit_move_insn (tmp_regs[i],
4446 operand_subword_force (target, i, mode));
4448 else if (overlapping == -1)
4449 overlapping = 0;
4450 /* Could not determine whether there is overlap.
4451 Fail the sibcall. */
4452 else
4454 overlapping = 0;
4455 if (sibcall_p)
4456 return false;
4459 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4462 else if (partial > 0)
4464 /* Scalar partly in registers. */
4466 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4467 int i;
4468 int not_stack;
4469 /* # bytes of start of argument
4470 that we must make space for but need not store. */
4471 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4472 int args_offset = INTVAL (args_so_far);
4473 int skip;
4475 /* Push padding now if padding above and stack grows down,
4476 or if padding below and stack grows up.
4477 But if space already allocated, this has already been done. */
4478 if (extra && args_addr == 0
4479 && where_pad != none && where_pad != stack_direction)
4480 anti_adjust_stack (GEN_INT (extra));
4482 /* If we make space by pushing it, we might as well push
4483 the real data. Otherwise, we can leave OFFSET nonzero
4484 and leave the space uninitialized. */
4485 if (args_addr == 0)
4486 offset = 0;
4488 /* Now NOT_STACK gets the number of words that we don't need to
4489 allocate on the stack. Convert OFFSET to words too. */
4490 not_stack = (partial - offset) / UNITS_PER_WORD;
4491 offset /= UNITS_PER_WORD;
4493 /* If the partial register-part of the arg counts in its stack size,
4494 skip the part of stack space corresponding to the registers.
4495 Otherwise, start copying to the beginning of the stack space,
4496 by setting SKIP to 0. */
4497 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4499 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4500 x = validize_mem (force_const_mem (mode, x));
4502 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4503 SUBREGs of such registers are not allowed. */
4504 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4505 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4506 x = copy_to_reg (x);
4508 /* Loop over all the words allocated on the stack for this arg. */
4509 /* We can do it by words, because any scalar bigger than a word
4510 has a size a multiple of a word. */
4511 for (i = size - 1; i >= not_stack; i--)
4512 if (i >= not_stack + offset)
4513 if (!emit_push_insn (operand_subword_force (x, i, mode),
4514 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4515 0, args_addr,
4516 GEN_INT (args_offset + ((i - not_stack + skip)
4517 * UNITS_PER_WORD)),
4518 reg_parm_stack_space, alignment_pad, sibcall_p))
4519 return false;
4521 else
4523 rtx addr;
4524 rtx dest;
4526 /* Push padding now if padding above and stack grows down,
4527 or if padding below and stack grows up.
4528 But if space already allocated, this has already been done. */
4529 if (extra && args_addr == 0
4530 && where_pad != none && where_pad != stack_direction)
4531 anti_adjust_stack (GEN_INT (extra));
4533 #ifdef PUSH_ROUNDING
4534 if (args_addr == 0 && PUSH_ARGS)
4535 emit_single_push_insn (mode, x, type);
4536 else
4537 #endif
4539 if (CONST_INT_P (args_so_far))
4540 addr
4541 = memory_address (mode,
4542 plus_constant (Pmode, args_addr,
4543 INTVAL (args_so_far)));
4544 else
4545 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4546 args_so_far));
4547 dest = gen_rtx_MEM (mode, addr);
4549 /* We do *not* set_mem_attributes here, because incoming arguments
4550 may overlap with sibling call outgoing arguments and we cannot
4551 allow reordering of reads from function arguments with stores
4552 to outgoing arguments of sibling calls. We do, however, want
4553 to record the alignment of the stack slot. */
4554 /* ALIGN may well be better aligned than TYPE, e.g. due to
4555 PARM_BOUNDARY. Assume the caller isn't lying. */
4556 set_mem_align (dest, align);
4558 emit_move_insn (dest, x);
4562 /* Move the partial arguments into the registers and any overlapping
4563 values that we moved into the pseudos in tmp_regs. */
4564 if (partial > 0 && reg != 0)
4566 /* Handle calls that pass values in multiple non-contiguous locations.
4567 The Irix 6 ABI has examples of this. */
4568 if (GET_CODE (reg) == PARALLEL)
4569 emit_group_load (reg, x, type, -1);
4570 else
4572 gcc_assert (partial % UNITS_PER_WORD == 0);
4573 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4575 for (int i = 0; i < overlapping; i++)
4576 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4577 + nregs - overlapping + i),
4578 tmp_regs[i]);
4583 if (extra && args_addr == 0 && where_pad == stack_direction)
4584 anti_adjust_stack (GEN_INT (extra));
4586 if (alignment_pad && args_addr == 0)
4587 anti_adjust_stack (alignment_pad);
4589 return true;
4592 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4593 operations. */
4595 static rtx
4596 get_subtarget (rtx x)
4598 return (optimize
4599 || x == 0
4600 /* Only registers can be subtargets. */
4601 || !REG_P (x)
4602 /* Don't use hard regs to avoid extending their life. */
4603 || REGNO (x) < FIRST_PSEUDO_REGISTER
4604 ? 0 : x);
4607 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4608 FIELD is a bitfield. Returns true if the optimization was successful,
4609 and there's nothing else to do. */
4611 static bool
4612 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4613 unsigned HOST_WIDE_INT bitpos,
4614 unsigned HOST_WIDE_INT bitregion_start,
4615 unsigned HOST_WIDE_INT bitregion_end,
4616 machine_mode mode1, rtx str_rtx,
4617 tree to, tree src, bool reverse)
4619 machine_mode str_mode = GET_MODE (str_rtx);
4620 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4621 tree op0, op1;
4622 rtx value, result;
4623 optab binop;
4624 gimple *srcstmt;
4625 enum tree_code code;
4627 if (mode1 != VOIDmode
4628 || bitsize >= BITS_PER_WORD
4629 || str_bitsize > BITS_PER_WORD
4630 || TREE_SIDE_EFFECTS (to)
4631 || TREE_THIS_VOLATILE (to))
4632 return false;
4634 STRIP_NOPS (src);
4635 if (TREE_CODE (src) != SSA_NAME)
4636 return false;
4637 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4638 return false;
4640 srcstmt = get_gimple_for_ssa_name (src);
4641 if (!srcstmt
4642 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4643 return false;
4645 code = gimple_assign_rhs_code (srcstmt);
4647 op0 = gimple_assign_rhs1 (srcstmt);
4649 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4650 to find its initialization. Hopefully the initialization will
4651 be from a bitfield load. */
4652 if (TREE_CODE (op0) == SSA_NAME)
4654 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4656 /* We want to eventually have OP0 be the same as TO, which
4657 should be a bitfield. */
4658 if (!op0stmt
4659 || !is_gimple_assign (op0stmt)
4660 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4661 return false;
4662 op0 = gimple_assign_rhs1 (op0stmt);
4665 op1 = gimple_assign_rhs2 (srcstmt);
4667 if (!operand_equal_p (to, op0, 0))
4668 return false;
4670 if (MEM_P (str_rtx))
4672 unsigned HOST_WIDE_INT offset1;
4674 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4675 str_mode = word_mode;
4676 str_mode = get_best_mode (bitsize, bitpos,
4677 bitregion_start, bitregion_end,
4678 MEM_ALIGN (str_rtx), str_mode, 0);
4679 if (str_mode == VOIDmode)
4680 return false;
4681 str_bitsize = GET_MODE_BITSIZE (str_mode);
4683 offset1 = bitpos;
4684 bitpos %= str_bitsize;
4685 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4686 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4688 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4689 return false;
4690 else
4691 gcc_assert (!reverse);
4693 /* If the bit field covers the whole REG/MEM, store_field
4694 will likely generate better code. */
4695 if (bitsize >= str_bitsize)
4696 return false;
4698 /* We can't handle fields split across multiple entities. */
4699 if (bitpos + bitsize > str_bitsize)
4700 return false;
4702 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4703 bitpos = str_bitsize - bitpos - bitsize;
4705 switch (code)
4707 case PLUS_EXPR:
4708 case MINUS_EXPR:
4709 /* For now, just optimize the case of the topmost bitfield
4710 where we don't need to do any masking and also
4711 1 bit bitfields where xor can be used.
4712 We might win by one instruction for the other bitfields
4713 too if insv/extv instructions aren't used, so that
4714 can be added later. */
4715 if ((reverse || bitpos + bitsize != str_bitsize)
4716 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4717 break;
4719 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4720 value = convert_modes (str_mode,
4721 TYPE_MODE (TREE_TYPE (op1)), value,
4722 TYPE_UNSIGNED (TREE_TYPE (op1)));
4724 /* We may be accessing data outside the field, which means
4725 we can alias adjacent data. */
4726 if (MEM_P (str_rtx))
4728 str_rtx = shallow_copy_rtx (str_rtx);
4729 set_mem_alias_set (str_rtx, 0);
4730 set_mem_expr (str_rtx, 0);
4733 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4735 value = expand_and (str_mode, value, const1_rtx, NULL);
4736 binop = xor_optab;
4738 else
4739 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4741 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4742 if (reverse)
4743 value = flip_storage_order (str_mode, value);
4744 result = expand_binop (str_mode, binop, str_rtx,
4745 value, str_rtx, 1, OPTAB_WIDEN);
4746 if (result != str_rtx)
4747 emit_move_insn (str_rtx, result);
4748 return true;
4750 case BIT_IOR_EXPR:
4751 case BIT_XOR_EXPR:
4752 if (TREE_CODE (op1) != INTEGER_CST)
4753 break;
4754 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4755 value = convert_modes (str_mode,
4756 TYPE_MODE (TREE_TYPE (op1)), value,
4757 TYPE_UNSIGNED (TREE_TYPE (op1)));
4759 /* We may be accessing data outside the field, which means
4760 we can alias adjacent data. */
4761 if (MEM_P (str_rtx))
4763 str_rtx = shallow_copy_rtx (str_rtx);
4764 set_mem_alias_set (str_rtx, 0);
4765 set_mem_expr (str_rtx, 0);
4768 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4769 if (bitpos + bitsize != str_bitsize)
4771 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4772 str_mode);
4773 value = expand_and (str_mode, value, mask, NULL_RTX);
4775 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4776 if (reverse)
4777 value = flip_storage_order (str_mode, value);
4778 result = expand_binop (str_mode, binop, str_rtx,
4779 value, str_rtx, 1, OPTAB_WIDEN);
4780 if (result != str_rtx)
4781 emit_move_insn (str_rtx, result);
4782 return true;
4784 default:
4785 break;
4788 return false;
4791 /* In the C++ memory model, consecutive bit fields in a structure are
4792 considered one memory location.
4794 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4795 returns the bit range of consecutive bits in which this COMPONENT_REF
4796 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4797 and *OFFSET may be adjusted in the process.
4799 If the access does not need to be restricted, 0 is returned in both
4800 *BITSTART and *BITEND. */
4802 void
4803 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4804 unsigned HOST_WIDE_INT *bitend,
4805 tree exp,
4806 HOST_WIDE_INT *bitpos,
4807 tree *offset)
4809 HOST_WIDE_INT bitoffset;
4810 tree field, repr;
4812 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4814 field = TREE_OPERAND (exp, 1);
4815 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4816 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4817 need to limit the range we can access. */
4818 if (!repr)
4820 *bitstart = *bitend = 0;
4821 return;
4824 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4825 part of a larger bit field, then the representative does not serve any
4826 useful purpose. This can occur in Ada. */
4827 if (handled_component_p (TREE_OPERAND (exp, 0)))
4829 machine_mode rmode;
4830 HOST_WIDE_INT rbitsize, rbitpos;
4831 tree roffset;
4832 int unsignedp, reversep, volatilep = 0;
4833 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4834 &roffset, &rmode, &unsignedp, &reversep,
4835 &volatilep);
4836 if ((rbitpos % BITS_PER_UNIT) != 0)
4838 *bitstart = *bitend = 0;
4839 return;
4843 /* Compute the adjustment to bitpos from the offset of the field
4844 relative to the representative. DECL_FIELD_OFFSET of field and
4845 repr are the same by construction if they are not constants,
4846 see finish_bitfield_layout. */
4847 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4848 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4849 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4850 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4851 else
4852 bitoffset = 0;
4853 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4854 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4856 /* If the adjustment is larger than bitpos, we would have a negative bit
4857 position for the lower bound and this may wreak havoc later. Adjust
4858 offset and bitpos to make the lower bound non-negative in that case. */
4859 if (bitoffset > *bitpos)
4861 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4862 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4864 *bitpos += adjust;
4865 if (*offset == NULL_TREE)
4866 *offset = size_int (-adjust / BITS_PER_UNIT);
4867 else
4868 *offset
4869 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4870 *bitstart = 0;
4872 else
4873 *bitstart = *bitpos - bitoffset;
4875 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4878 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4879 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4880 DECL_RTL was not set yet, return NORTL. */
4882 static inline bool
4883 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4885 if (TREE_CODE (addr) != ADDR_EXPR)
4886 return false;
4888 tree base = TREE_OPERAND (addr, 0);
4890 if (!DECL_P (base)
4891 || TREE_ADDRESSABLE (base)
4892 || DECL_MODE (base) == BLKmode)
4893 return false;
4895 if (!DECL_RTL_SET_P (base))
4896 return nortl;
4898 return (!MEM_P (DECL_RTL (base)));
4901 /* Returns true if the MEM_REF REF refers to an object that does not
4902 reside in memory and has non-BLKmode. */
4904 static inline bool
4905 mem_ref_refers_to_non_mem_p (tree ref)
4907 tree base = TREE_OPERAND (ref, 0);
4908 return addr_expr_of_non_mem_decl_p_1 (base, false);
4911 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4912 is true, try generating a nontemporal store. */
4914 void
4915 expand_assignment (tree to, tree from, bool nontemporal)
4917 rtx to_rtx = 0;
4918 rtx result;
4919 machine_mode mode;
4920 unsigned int align;
4921 enum insn_code icode;
4923 /* Don't crash if the lhs of the assignment was erroneous. */
4924 if (TREE_CODE (to) == ERROR_MARK)
4926 expand_normal (from);
4927 return;
4930 /* Optimize away no-op moves without side-effects. */
4931 if (operand_equal_p (to, from, 0))
4932 return;
4934 /* Handle misaligned stores. */
4935 mode = TYPE_MODE (TREE_TYPE (to));
4936 if ((TREE_CODE (to) == MEM_REF
4937 || TREE_CODE (to) == TARGET_MEM_REF)
4938 && mode != BLKmode
4939 && !mem_ref_refers_to_non_mem_p (to)
4940 && ((align = get_object_alignment (to))
4941 < GET_MODE_ALIGNMENT (mode))
4942 && (((icode = optab_handler (movmisalign_optab, mode))
4943 != CODE_FOR_nothing)
4944 || SLOW_UNALIGNED_ACCESS (mode, align)))
4946 rtx reg, mem;
4948 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4949 reg = force_not_mem (reg);
4950 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4951 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4952 reg = flip_storage_order (mode, reg);
4954 if (icode != CODE_FOR_nothing)
4956 struct expand_operand ops[2];
4958 create_fixed_operand (&ops[0], mem);
4959 create_input_operand (&ops[1], reg, mode);
4960 /* The movmisalign<mode> pattern cannot fail, else the assignment
4961 would silently be omitted. */
4962 expand_insn (icode, 2, ops);
4964 else
4965 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4966 false);
4967 return;
4970 /* Assignment of a structure component needs special treatment
4971 if the structure component's rtx is not simply a MEM.
4972 Assignment of an array element at a constant index, and assignment of
4973 an array element in an unaligned packed structure field, has the same
4974 problem. Same for (partially) storing into a non-memory object. */
4975 if (handled_component_p (to)
4976 || (TREE_CODE (to) == MEM_REF
4977 && (REF_REVERSE_STORAGE_ORDER (to)
4978 || mem_ref_refers_to_non_mem_p (to)))
4979 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4981 machine_mode mode1;
4982 HOST_WIDE_INT bitsize, bitpos;
4983 unsigned HOST_WIDE_INT bitregion_start = 0;
4984 unsigned HOST_WIDE_INT bitregion_end = 0;
4985 tree offset;
4986 int unsignedp, reversep, volatilep = 0;
4987 tree tem;
4989 push_temp_slots ();
4990 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4991 &unsignedp, &reversep, &volatilep);
4993 /* Make sure bitpos is not negative, it can wreak havoc later. */
4994 if (bitpos < 0)
4996 gcc_assert (offset == NULL_TREE);
4997 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
4998 bitpos &= BITS_PER_UNIT - 1;
5001 if (TREE_CODE (to) == COMPONENT_REF
5002 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5003 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5004 /* The C++ memory model naturally applies to byte-aligned fields.
5005 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5006 BITSIZE are not byte-aligned, there is no need to limit the range
5007 we can access. This can occur with packed structures in Ada. */
5008 else if (bitsize > 0
5009 && bitsize % BITS_PER_UNIT == 0
5010 && bitpos % BITS_PER_UNIT == 0)
5012 bitregion_start = bitpos;
5013 bitregion_end = bitpos + bitsize - 1;
5016 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5018 /* If the field has a mode, we want to access it in the
5019 field's mode, not the computed mode.
5020 If a MEM has VOIDmode (external with incomplete type),
5021 use BLKmode for it instead. */
5022 if (MEM_P (to_rtx))
5024 if (mode1 != VOIDmode)
5025 to_rtx = adjust_address (to_rtx, mode1, 0);
5026 else if (GET_MODE (to_rtx) == VOIDmode)
5027 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5030 if (offset != 0)
5032 machine_mode address_mode;
5033 rtx offset_rtx;
5035 if (!MEM_P (to_rtx))
5037 /* We can get constant negative offsets into arrays with broken
5038 user code. Translate this to a trap instead of ICEing. */
5039 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5040 expand_builtin_trap ();
5041 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5044 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5045 address_mode = get_address_mode (to_rtx);
5046 if (GET_MODE (offset_rtx) != address_mode)
5048 /* We cannot be sure that the RTL in offset_rtx is valid outside
5049 of a memory address context, so force it into a register
5050 before attempting to convert it to the desired mode. */
5051 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5052 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5055 /* If we have an expression in OFFSET_RTX and a non-zero
5056 byte offset in BITPOS, adding the byte offset before the
5057 OFFSET_RTX results in better intermediate code, which makes
5058 later rtl optimization passes perform better.
5060 We prefer intermediate code like this:
5062 r124:DI=r123:DI+0x18
5063 [r124:DI]=r121:DI
5065 ... instead of ...
5067 r124:DI=r123:DI+0x10
5068 [r124:DI+0x8]=r121:DI
5070 This is only done for aligned data values, as these can
5071 be expected to result in single move instructions. */
5072 if (mode1 != VOIDmode
5073 && bitpos != 0
5074 && bitsize > 0
5075 && (bitpos % bitsize) == 0
5076 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5077 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5079 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5080 bitregion_start = 0;
5081 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5082 bitregion_end -= bitpos;
5083 bitpos = 0;
5086 to_rtx = offset_address (to_rtx, offset_rtx,
5087 highest_pow2_factor_for_target (to,
5088 offset));
5091 /* No action is needed if the target is not a memory and the field
5092 lies completely outside that target. This can occur if the source
5093 code contains an out-of-bounds access to a small array. */
5094 if (!MEM_P (to_rtx)
5095 && GET_MODE (to_rtx) != BLKmode
5096 && (unsigned HOST_WIDE_INT) bitpos
5097 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5099 expand_normal (from);
5100 result = NULL;
5102 /* Handle expand_expr of a complex value returning a CONCAT. */
5103 else if (GET_CODE (to_rtx) == CONCAT)
5105 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5106 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5107 && bitpos == 0
5108 && bitsize == mode_bitsize)
5109 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5110 else if (bitsize == mode_bitsize / 2
5111 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5112 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5113 nontemporal, reversep);
5114 else if (bitpos + bitsize <= mode_bitsize / 2)
5115 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5116 bitregion_start, bitregion_end,
5117 mode1, from, get_alias_set (to),
5118 nontemporal, reversep);
5119 else if (bitpos >= mode_bitsize / 2)
5120 result = store_field (XEXP (to_rtx, 1), bitsize,
5121 bitpos - mode_bitsize / 2,
5122 bitregion_start, bitregion_end,
5123 mode1, from, get_alias_set (to),
5124 nontemporal, reversep);
5125 else if (bitpos == 0 && bitsize == mode_bitsize)
5127 rtx from_rtx;
5128 result = expand_normal (from);
5129 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5130 TYPE_MODE (TREE_TYPE (from)), 0);
5131 emit_move_insn (XEXP (to_rtx, 0),
5132 read_complex_part (from_rtx, false));
5133 emit_move_insn (XEXP (to_rtx, 1),
5134 read_complex_part (from_rtx, true));
5136 else
5138 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5139 GET_MODE_SIZE (GET_MODE (to_rtx)));
5140 write_complex_part (temp, XEXP (to_rtx, 0), false);
5141 write_complex_part (temp, XEXP (to_rtx, 1), true);
5142 result = store_field (temp, bitsize, bitpos,
5143 bitregion_start, bitregion_end,
5144 mode1, from, get_alias_set (to),
5145 nontemporal, reversep);
5146 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5147 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5150 else
5152 if (MEM_P (to_rtx))
5154 /* If the field is at offset zero, we could have been given the
5155 DECL_RTX of the parent struct. Don't munge it. */
5156 to_rtx = shallow_copy_rtx (to_rtx);
5157 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5158 if (volatilep)
5159 MEM_VOLATILE_P (to_rtx) = 1;
5162 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5163 bitregion_start, bitregion_end,
5164 mode1, to_rtx, to, from,
5165 reversep))
5166 result = NULL;
5167 else
5168 result = store_field (to_rtx, bitsize, bitpos,
5169 bitregion_start, bitregion_end,
5170 mode1, from, get_alias_set (to),
5171 nontemporal, reversep);
5174 if (result)
5175 preserve_temp_slots (result);
5176 pop_temp_slots ();
5177 return;
5180 /* If the rhs is a function call and its value is not an aggregate,
5181 call the function before we start to compute the lhs.
5182 This is needed for correct code for cases such as
5183 val = setjmp (buf) on machines where reference to val
5184 requires loading up part of an address in a separate insn.
5186 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5187 since it might be a promoted variable where the zero- or sign- extension
5188 needs to be done. Handling this in the normal way is safe because no
5189 computation is done before the call. The same is true for SSA names. */
5190 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5191 && COMPLETE_TYPE_P (TREE_TYPE (from))
5192 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5193 && ! (((VAR_P (to)
5194 || TREE_CODE (to) == PARM_DECL
5195 || TREE_CODE (to) == RESULT_DECL)
5196 && REG_P (DECL_RTL (to)))
5197 || TREE_CODE (to) == SSA_NAME))
5199 rtx value;
5200 rtx bounds;
5202 push_temp_slots ();
5203 value = expand_normal (from);
5205 /* Split value and bounds to store them separately. */
5206 chkp_split_slot (value, &value, &bounds);
5208 if (to_rtx == 0)
5209 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5211 /* Handle calls that return values in multiple non-contiguous locations.
5212 The Irix 6 ABI has examples of this. */
5213 if (GET_CODE (to_rtx) == PARALLEL)
5215 if (GET_CODE (value) == PARALLEL)
5216 emit_group_move (to_rtx, value);
5217 else
5218 emit_group_load (to_rtx, value, TREE_TYPE (from),
5219 int_size_in_bytes (TREE_TYPE (from)));
5221 else if (GET_CODE (value) == PARALLEL)
5222 emit_group_store (to_rtx, value, TREE_TYPE (from),
5223 int_size_in_bytes (TREE_TYPE (from)));
5224 else if (GET_MODE (to_rtx) == BLKmode)
5226 /* Handle calls that return BLKmode values in registers. */
5227 if (REG_P (value))
5228 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5229 else
5230 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5232 else
5234 if (POINTER_TYPE_P (TREE_TYPE (to)))
5235 value = convert_memory_address_addr_space
5236 (GET_MODE (to_rtx), value,
5237 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5239 emit_move_insn (to_rtx, value);
5242 /* Store bounds if required. */
5243 if (bounds
5244 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5246 gcc_assert (MEM_P (to_rtx));
5247 chkp_emit_bounds_store (bounds, value, to_rtx);
5250 preserve_temp_slots (to_rtx);
5251 pop_temp_slots ();
5252 return;
5255 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5256 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5258 /* Don't move directly into a return register. */
5259 if (TREE_CODE (to) == RESULT_DECL
5260 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5262 rtx temp;
5264 push_temp_slots ();
5266 /* If the source is itself a return value, it still is in a pseudo at
5267 this point so we can move it back to the return register directly. */
5268 if (REG_P (to_rtx)
5269 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5270 && TREE_CODE (from) != CALL_EXPR)
5271 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5272 else
5273 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5275 /* Handle calls that return values in multiple non-contiguous locations.
5276 The Irix 6 ABI has examples of this. */
5277 if (GET_CODE (to_rtx) == PARALLEL)
5279 if (GET_CODE (temp) == PARALLEL)
5280 emit_group_move (to_rtx, temp);
5281 else
5282 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5283 int_size_in_bytes (TREE_TYPE (from)));
5285 else if (temp)
5286 emit_move_insn (to_rtx, temp);
5288 preserve_temp_slots (to_rtx);
5289 pop_temp_slots ();
5290 return;
5293 /* In case we are returning the contents of an object which overlaps
5294 the place the value is being stored, use a safe function when copying
5295 a value through a pointer into a structure value return block. */
5296 if (TREE_CODE (to) == RESULT_DECL
5297 && TREE_CODE (from) == INDIRECT_REF
5298 && ADDR_SPACE_GENERIC_P
5299 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5300 && refs_may_alias_p (to, from)
5301 && cfun->returns_struct
5302 && !cfun->returns_pcc_struct)
5304 rtx from_rtx, size;
5306 push_temp_slots ();
5307 size = expr_size (from);
5308 from_rtx = expand_normal (from);
5310 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5312 preserve_temp_slots (to_rtx);
5313 pop_temp_slots ();
5314 return;
5317 /* Compute FROM and store the value in the rtx we got. */
5319 push_temp_slots ();
5320 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5321 preserve_temp_slots (result);
5322 pop_temp_slots ();
5323 return;
5326 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5327 succeeded, false otherwise. */
5329 bool
5330 emit_storent_insn (rtx to, rtx from)
5332 struct expand_operand ops[2];
5333 machine_mode mode = GET_MODE (to);
5334 enum insn_code code = optab_handler (storent_optab, mode);
5336 if (code == CODE_FOR_nothing)
5337 return false;
5339 create_fixed_operand (&ops[0], to);
5340 create_input_operand (&ops[1], from, mode);
5341 return maybe_expand_insn (code, 2, ops);
5344 /* Generate code for computing expression EXP,
5345 and storing the value into TARGET.
5347 If the mode is BLKmode then we may return TARGET itself.
5348 It turns out that in BLKmode it doesn't cause a problem.
5349 because C has no operators that could combine two different
5350 assignments into the same BLKmode object with different values
5351 with no sequence point. Will other languages need this to
5352 be more thorough?
5354 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5355 stack, and block moves may need to be treated specially.
5357 If NONTEMPORAL is true, try using a nontemporal store instruction.
5359 If REVERSE is true, the store is to be done in reverse order.
5361 If BTARGET is not NULL then computed bounds of EXP are
5362 associated with BTARGET. */
5365 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5366 bool nontemporal, bool reverse, tree btarget)
5368 rtx temp;
5369 rtx alt_rtl = NULL_RTX;
5370 location_t loc = curr_insn_location ();
5372 if (VOID_TYPE_P (TREE_TYPE (exp)))
5374 /* C++ can generate ?: expressions with a throw expression in one
5375 branch and an rvalue in the other. Here, we resolve attempts to
5376 store the throw expression's nonexistent result. */
5377 gcc_assert (!call_param_p);
5378 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5379 return NULL_RTX;
5381 if (TREE_CODE (exp) == COMPOUND_EXPR)
5383 /* Perform first part of compound expression, then assign from second
5384 part. */
5385 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5386 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5387 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5388 call_param_p, nontemporal, reverse,
5389 btarget);
5391 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5393 /* For conditional expression, get safe form of the target. Then
5394 test the condition, doing the appropriate assignment on either
5395 side. This avoids the creation of unnecessary temporaries.
5396 For non-BLKmode, it is more efficient not to do this. */
5398 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5400 do_pending_stack_adjust ();
5401 NO_DEFER_POP;
5402 jumpifnot (TREE_OPERAND (exp, 0), lab1,
5403 profile_probability::uninitialized ());
5404 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5405 nontemporal, reverse, btarget);
5406 emit_jump_insn (targetm.gen_jump (lab2));
5407 emit_barrier ();
5408 emit_label (lab1);
5409 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5410 nontemporal, reverse, btarget);
5411 emit_label (lab2);
5412 OK_DEFER_POP;
5414 return NULL_RTX;
5416 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5417 /* If this is a scalar in a register that is stored in a wider mode
5418 than the declared mode, compute the result into its declared mode
5419 and then convert to the wider mode. Our value is the computed
5420 expression. */
5422 rtx inner_target = 0;
5424 /* We can do the conversion inside EXP, which will often result
5425 in some optimizations. Do the conversion in two steps: first
5426 change the signedness, if needed, then the extend. But don't
5427 do this if the type of EXP is a subtype of something else
5428 since then the conversion might involve more than just
5429 converting modes. */
5430 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5431 && TREE_TYPE (TREE_TYPE (exp)) == 0
5432 && GET_MODE_PRECISION (GET_MODE (target))
5433 == TYPE_PRECISION (TREE_TYPE (exp)))
5435 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5436 TYPE_UNSIGNED (TREE_TYPE (exp))))
5438 /* Some types, e.g. Fortran's logical*4, won't have a signed
5439 version, so use the mode instead. */
5440 tree ntype
5441 = (signed_or_unsigned_type_for
5442 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5443 if (ntype == NULL)
5444 ntype = lang_hooks.types.type_for_mode
5445 (TYPE_MODE (TREE_TYPE (exp)),
5446 SUBREG_PROMOTED_SIGN (target));
5448 exp = fold_convert_loc (loc, ntype, exp);
5451 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5452 (GET_MODE (SUBREG_REG (target)),
5453 SUBREG_PROMOTED_SIGN (target)),
5454 exp);
5456 inner_target = SUBREG_REG (target);
5459 temp = expand_expr (exp, inner_target, VOIDmode,
5460 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5462 /* Handle bounds returned by call. */
5463 if (TREE_CODE (exp) == CALL_EXPR)
5465 rtx bounds;
5466 chkp_split_slot (temp, &temp, &bounds);
5467 if (bounds && btarget)
5469 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5470 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5471 chkp_set_rtl_bounds (btarget, tmp);
5475 /* If TEMP is a VOIDmode constant, use convert_modes to make
5476 sure that we properly convert it. */
5477 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5479 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5480 temp, SUBREG_PROMOTED_SIGN (target));
5481 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5482 GET_MODE (target), temp,
5483 SUBREG_PROMOTED_SIGN (target));
5486 convert_move (SUBREG_REG (target), temp,
5487 SUBREG_PROMOTED_SIGN (target));
5489 return NULL_RTX;
5491 else if ((TREE_CODE (exp) == STRING_CST
5492 || (TREE_CODE (exp) == MEM_REF
5493 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5494 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5495 == STRING_CST
5496 && integer_zerop (TREE_OPERAND (exp, 1))))
5497 && !nontemporal && !call_param_p
5498 && MEM_P (target))
5500 /* Optimize initialization of an array with a STRING_CST. */
5501 HOST_WIDE_INT exp_len, str_copy_len;
5502 rtx dest_mem;
5503 tree str = TREE_CODE (exp) == STRING_CST
5504 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5506 exp_len = int_expr_size (exp);
5507 if (exp_len <= 0)
5508 goto normal_expr;
5510 if (TREE_STRING_LENGTH (str) <= 0)
5511 goto normal_expr;
5513 str_copy_len = strlen (TREE_STRING_POINTER (str));
5514 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5515 goto normal_expr;
5517 str_copy_len = TREE_STRING_LENGTH (str);
5518 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5519 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5521 str_copy_len += STORE_MAX_PIECES - 1;
5522 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5524 str_copy_len = MIN (str_copy_len, exp_len);
5525 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5526 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5527 MEM_ALIGN (target), false))
5528 goto normal_expr;
5530 dest_mem = target;
5532 dest_mem = store_by_pieces (dest_mem,
5533 str_copy_len, builtin_strncpy_read_str,
5534 CONST_CAST (char *,
5535 TREE_STRING_POINTER (str)),
5536 MEM_ALIGN (target), false,
5537 exp_len > str_copy_len ? 1 : 0);
5538 if (exp_len > str_copy_len)
5539 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5540 GEN_INT (exp_len - str_copy_len),
5541 BLOCK_OP_NORMAL);
5542 return NULL_RTX;
5544 else
5546 rtx tmp_target;
5548 normal_expr:
5549 /* If we want to use a nontemporal or a reverse order store, force the
5550 value into a register first. */
5551 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5552 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5553 (call_param_p
5554 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5555 &alt_rtl, false);
5557 /* Handle bounds returned by call. */
5558 if (TREE_CODE (exp) == CALL_EXPR)
5560 rtx bounds;
5561 chkp_split_slot (temp, &temp, &bounds);
5562 if (bounds && btarget)
5564 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5565 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5566 chkp_set_rtl_bounds (btarget, tmp);
5571 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5572 the same as that of TARGET, adjust the constant. This is needed, for
5573 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5574 only a word-sized value. */
5575 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5576 && TREE_CODE (exp) != ERROR_MARK
5577 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5578 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5579 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5581 /* If value was not generated in the target, store it there.
5582 Convert the value to TARGET's type first if necessary and emit the
5583 pending incrementations that have been queued when expanding EXP.
5584 Note that we cannot emit the whole queue blindly because this will
5585 effectively disable the POST_INC optimization later.
5587 If TEMP and TARGET compare equal according to rtx_equal_p, but
5588 one or both of them are volatile memory refs, we have to distinguish
5589 two cases:
5590 - expand_expr has used TARGET. In this case, we must not generate
5591 another copy. This can be detected by TARGET being equal according
5592 to == .
5593 - expand_expr has not used TARGET - that means that the source just
5594 happens to have the same RTX form. Since temp will have been created
5595 by expand_expr, it will compare unequal according to == .
5596 We must generate a copy in this case, to reach the correct number
5597 of volatile memory references. */
5599 if ((! rtx_equal_p (temp, target)
5600 || (temp != target && (side_effects_p (temp)
5601 || side_effects_p (target))))
5602 && TREE_CODE (exp) != ERROR_MARK
5603 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5604 but TARGET is not valid memory reference, TEMP will differ
5605 from TARGET although it is really the same location. */
5606 && !(alt_rtl
5607 && rtx_equal_p (alt_rtl, target)
5608 && !side_effects_p (alt_rtl)
5609 && !side_effects_p (target))
5610 /* If there's nothing to copy, don't bother. Don't call
5611 expr_size unless necessary, because some front-ends (C++)
5612 expr_size-hook must not be given objects that are not
5613 supposed to be bit-copied or bit-initialized. */
5614 && expr_size (exp) != const0_rtx)
5616 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5618 if (GET_MODE (target) == BLKmode)
5620 /* Handle calls that return BLKmode values in registers. */
5621 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5622 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5623 else
5624 store_bit_field (target,
5625 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5626 0, 0, 0, GET_MODE (temp), temp, reverse);
5628 else
5629 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5632 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5634 /* Handle copying a string constant into an array. The string
5635 constant may be shorter than the array. So copy just the string's
5636 actual length, and clear the rest. First get the size of the data
5637 type of the string, which is actually the size of the target. */
5638 rtx size = expr_size (exp);
5640 if (CONST_INT_P (size)
5641 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5642 emit_block_move (target, temp, size,
5643 (call_param_p
5644 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5645 else
5647 machine_mode pointer_mode
5648 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5649 machine_mode address_mode = get_address_mode (target);
5651 /* Compute the size of the data to copy from the string. */
5652 tree copy_size
5653 = size_binop_loc (loc, MIN_EXPR,
5654 make_tree (sizetype, size),
5655 size_int (TREE_STRING_LENGTH (exp)));
5656 rtx copy_size_rtx
5657 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5658 (call_param_p
5659 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5660 rtx_code_label *label = 0;
5662 /* Copy that much. */
5663 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5664 TYPE_UNSIGNED (sizetype));
5665 emit_block_move (target, temp, copy_size_rtx,
5666 (call_param_p
5667 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5669 /* Figure out how much is left in TARGET that we have to clear.
5670 Do all calculations in pointer_mode. */
5671 if (CONST_INT_P (copy_size_rtx))
5673 size = plus_constant (address_mode, size,
5674 -INTVAL (copy_size_rtx));
5675 target = adjust_address (target, BLKmode,
5676 INTVAL (copy_size_rtx));
5678 else
5680 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5681 copy_size_rtx, NULL_RTX, 0,
5682 OPTAB_LIB_WIDEN);
5684 if (GET_MODE (copy_size_rtx) != address_mode)
5685 copy_size_rtx = convert_to_mode (address_mode,
5686 copy_size_rtx,
5687 TYPE_UNSIGNED (sizetype));
5689 target = offset_address (target, copy_size_rtx,
5690 highest_pow2_factor (copy_size));
5691 label = gen_label_rtx ();
5692 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5693 GET_MODE (size), 0, label);
5696 if (size != const0_rtx)
5697 clear_storage (target, size, BLOCK_OP_NORMAL);
5699 if (label)
5700 emit_label (label);
5703 /* Handle calls that return values in multiple non-contiguous locations.
5704 The Irix 6 ABI has examples of this. */
5705 else if (GET_CODE (target) == PARALLEL)
5707 if (GET_CODE (temp) == PARALLEL)
5708 emit_group_move (target, temp);
5709 else
5710 emit_group_load (target, temp, TREE_TYPE (exp),
5711 int_size_in_bytes (TREE_TYPE (exp)));
5713 else if (GET_CODE (temp) == PARALLEL)
5714 emit_group_store (target, temp, TREE_TYPE (exp),
5715 int_size_in_bytes (TREE_TYPE (exp)));
5716 else if (GET_MODE (temp) == BLKmode)
5717 emit_block_move (target, temp, expr_size (exp),
5718 (call_param_p
5719 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5720 /* If we emit a nontemporal store, there is nothing else to do. */
5721 else if (nontemporal && emit_storent_insn (target, temp))
5723 else
5725 if (reverse)
5726 temp = flip_storage_order (GET_MODE (target), temp);
5727 temp = force_operand (temp, target);
5728 if (temp != target)
5729 emit_move_insn (target, temp);
5733 return NULL_RTX;
5736 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5738 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5739 bool reverse)
5741 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5742 reverse, NULL);
5745 /* Return true if field F of structure TYPE is a flexible array. */
5747 static bool
5748 flexible_array_member_p (const_tree f, const_tree type)
5750 const_tree tf;
5752 tf = TREE_TYPE (f);
5753 return (DECL_CHAIN (f) == NULL
5754 && TREE_CODE (tf) == ARRAY_TYPE
5755 && TYPE_DOMAIN (tf)
5756 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5757 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5758 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5759 && int_size_in_bytes (type) >= 0);
5762 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5763 must have in order for it to completely initialize a value of type TYPE.
5764 Return -1 if the number isn't known.
5766 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5768 static HOST_WIDE_INT
5769 count_type_elements (const_tree type, bool for_ctor_p)
5771 switch (TREE_CODE (type))
5773 case ARRAY_TYPE:
5775 tree nelts;
5777 nelts = array_type_nelts (type);
5778 if (nelts && tree_fits_uhwi_p (nelts))
5780 unsigned HOST_WIDE_INT n;
5782 n = tree_to_uhwi (nelts) + 1;
5783 if (n == 0 || for_ctor_p)
5784 return n;
5785 else
5786 return n * count_type_elements (TREE_TYPE (type), false);
5788 return for_ctor_p ? -1 : 1;
5791 case RECORD_TYPE:
5793 unsigned HOST_WIDE_INT n;
5794 tree f;
5796 n = 0;
5797 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5798 if (TREE_CODE (f) == FIELD_DECL)
5800 if (!for_ctor_p)
5801 n += count_type_elements (TREE_TYPE (f), false);
5802 else if (!flexible_array_member_p (f, type))
5803 /* Don't count flexible arrays, which are not supposed
5804 to be initialized. */
5805 n += 1;
5808 return n;
5811 case UNION_TYPE:
5812 case QUAL_UNION_TYPE:
5814 tree f;
5815 HOST_WIDE_INT n, m;
5817 gcc_assert (!for_ctor_p);
5818 /* Estimate the number of scalars in each field and pick the
5819 maximum. Other estimates would do instead; the idea is simply
5820 to make sure that the estimate is not sensitive to the ordering
5821 of the fields. */
5822 n = 1;
5823 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5824 if (TREE_CODE (f) == FIELD_DECL)
5826 m = count_type_elements (TREE_TYPE (f), false);
5827 /* If the field doesn't span the whole union, add an extra
5828 scalar for the rest. */
5829 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5830 TYPE_SIZE (type)) != 1)
5831 m++;
5832 if (n < m)
5833 n = m;
5835 return n;
5838 case COMPLEX_TYPE:
5839 return 2;
5841 case VECTOR_TYPE:
5842 return TYPE_VECTOR_SUBPARTS (type);
5844 case INTEGER_TYPE:
5845 case REAL_TYPE:
5846 case FIXED_POINT_TYPE:
5847 case ENUMERAL_TYPE:
5848 case BOOLEAN_TYPE:
5849 case POINTER_TYPE:
5850 case OFFSET_TYPE:
5851 case REFERENCE_TYPE:
5852 case NULLPTR_TYPE:
5853 return 1;
5855 case ERROR_MARK:
5856 return 0;
5858 case VOID_TYPE:
5859 case METHOD_TYPE:
5860 case FUNCTION_TYPE:
5861 case LANG_TYPE:
5862 default:
5863 gcc_unreachable ();
5867 /* Helper for categorize_ctor_elements. Identical interface. */
5869 static bool
5870 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5871 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5873 unsigned HOST_WIDE_INT idx;
5874 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5875 tree value, purpose, elt_type;
5877 /* Whether CTOR is a valid constant initializer, in accordance with what
5878 initializer_constant_valid_p does. If inferred from the constructor
5879 elements, true until proven otherwise. */
5880 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5881 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5883 nz_elts = 0;
5884 init_elts = 0;
5885 num_fields = 0;
5886 elt_type = NULL_TREE;
5888 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5890 HOST_WIDE_INT mult = 1;
5892 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5894 tree lo_index = TREE_OPERAND (purpose, 0);
5895 tree hi_index = TREE_OPERAND (purpose, 1);
5897 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5898 mult = (tree_to_uhwi (hi_index)
5899 - tree_to_uhwi (lo_index) + 1);
5901 num_fields += mult;
5902 elt_type = TREE_TYPE (value);
5904 switch (TREE_CODE (value))
5906 case CONSTRUCTOR:
5908 HOST_WIDE_INT nz = 0, ic = 0;
5910 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5911 p_complete);
5913 nz_elts += mult * nz;
5914 init_elts += mult * ic;
5916 if (const_from_elts_p && const_p)
5917 const_p = const_elt_p;
5919 break;
5921 case INTEGER_CST:
5922 case REAL_CST:
5923 case FIXED_CST:
5924 if (!initializer_zerop (value))
5925 nz_elts += mult;
5926 init_elts += mult;
5927 break;
5929 case STRING_CST:
5930 nz_elts += mult * TREE_STRING_LENGTH (value);
5931 init_elts += mult * TREE_STRING_LENGTH (value);
5932 break;
5934 case COMPLEX_CST:
5935 if (!initializer_zerop (TREE_REALPART (value)))
5936 nz_elts += mult;
5937 if (!initializer_zerop (TREE_IMAGPART (value)))
5938 nz_elts += mult;
5939 init_elts += mult;
5940 break;
5942 case VECTOR_CST:
5944 unsigned i;
5945 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5947 tree v = VECTOR_CST_ELT (value, i);
5948 if (!initializer_zerop (v))
5949 nz_elts += mult;
5950 init_elts += mult;
5953 break;
5955 default:
5957 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5958 nz_elts += mult * tc;
5959 init_elts += mult * tc;
5961 if (const_from_elts_p && const_p)
5962 const_p
5963 = initializer_constant_valid_p (value,
5964 elt_type,
5965 TYPE_REVERSE_STORAGE_ORDER
5966 (TREE_TYPE (ctor)))
5967 != NULL_TREE;
5969 break;
5973 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5974 num_fields, elt_type))
5975 *p_complete = false;
5977 *p_nz_elts += nz_elts;
5978 *p_init_elts += init_elts;
5980 return const_p;
5983 /* Examine CTOR to discover:
5984 * how many scalar fields are set to nonzero values,
5985 and place it in *P_NZ_ELTS;
5986 * how many scalar fields in total are in CTOR,
5987 and place it in *P_ELT_COUNT.
5988 * whether the constructor is complete -- in the sense that every
5989 meaningful byte is explicitly given a value --
5990 and place it in *P_COMPLETE.
5992 Return whether or not CTOR is a valid static constant initializer, the same
5993 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5995 bool
5996 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5997 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5999 *p_nz_elts = 0;
6000 *p_init_elts = 0;
6001 *p_complete = true;
6003 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6006 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6007 of which had type LAST_TYPE. Each element was itself a complete
6008 initializer, in the sense that every meaningful byte was explicitly
6009 given a value. Return true if the same is true for the constructor
6010 as a whole. */
6012 bool
6013 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6014 const_tree last_type)
6016 if (TREE_CODE (type) == UNION_TYPE
6017 || TREE_CODE (type) == QUAL_UNION_TYPE)
6019 if (num_elts == 0)
6020 return false;
6022 gcc_assert (num_elts == 1 && last_type);
6024 /* ??? We could look at each element of the union, and find the
6025 largest element. Which would avoid comparing the size of the
6026 initialized element against any tail padding in the union.
6027 Doesn't seem worth the effort... */
6028 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6031 return count_type_elements (type, true) == num_elts;
6034 /* Return 1 if EXP contains mostly (3/4) zeros. */
6036 static int
6037 mostly_zeros_p (const_tree exp)
6039 if (TREE_CODE (exp) == CONSTRUCTOR)
6041 HOST_WIDE_INT nz_elts, init_elts;
6042 bool complete_p;
6044 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6045 return !complete_p || nz_elts < init_elts / 4;
6048 return initializer_zerop (exp);
6051 /* Return 1 if EXP contains all zeros. */
6053 static int
6054 all_zeros_p (const_tree exp)
6056 if (TREE_CODE (exp) == CONSTRUCTOR)
6058 HOST_WIDE_INT nz_elts, init_elts;
6059 bool complete_p;
6061 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6062 return nz_elts == 0;
6065 return initializer_zerop (exp);
6068 /* Helper function for store_constructor.
6069 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6070 CLEARED is as for store_constructor.
6071 ALIAS_SET is the alias set to use for any stores.
6072 If REVERSE is true, the store is to be done in reverse order.
6074 This provides a recursive shortcut back to store_constructor when it isn't
6075 necessary to go through store_field. This is so that we can pass through
6076 the cleared field to let store_constructor know that we may not have to
6077 clear a substructure if the outer structure has already been cleared. */
6079 static void
6080 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6081 HOST_WIDE_INT bitpos,
6082 unsigned HOST_WIDE_INT bitregion_start,
6083 unsigned HOST_WIDE_INT bitregion_end,
6084 machine_mode mode,
6085 tree exp, int cleared,
6086 alias_set_type alias_set, bool reverse)
6088 if (TREE_CODE (exp) == CONSTRUCTOR
6089 /* We can only call store_constructor recursively if the size and
6090 bit position are on a byte boundary. */
6091 && bitpos % BITS_PER_UNIT == 0
6092 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6093 /* If we have a nonzero bitpos for a register target, then we just
6094 let store_field do the bitfield handling. This is unlikely to
6095 generate unnecessary clear instructions anyways. */
6096 && (bitpos == 0 || MEM_P (target)))
6098 if (MEM_P (target))
6099 target
6100 = adjust_address (target,
6101 GET_MODE (target) == BLKmode
6102 || 0 != (bitpos
6103 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6104 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6107 /* Update the alias set, if required. */
6108 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6109 && MEM_ALIAS_SET (target) != 0)
6111 target = copy_rtx (target);
6112 set_mem_alias_set (target, alias_set);
6115 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6116 reverse);
6118 else
6119 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6120 exp, alias_set, false, reverse);
6124 /* Returns the number of FIELD_DECLs in TYPE. */
6126 static int
6127 fields_length (const_tree type)
6129 tree t = TYPE_FIELDS (type);
6130 int count = 0;
6132 for (; t; t = DECL_CHAIN (t))
6133 if (TREE_CODE (t) == FIELD_DECL)
6134 ++count;
6136 return count;
6140 /* Store the value of constructor EXP into the rtx TARGET.
6141 TARGET is either a REG or a MEM; we know it cannot conflict, since
6142 safe_from_p has been called.
6143 CLEARED is true if TARGET is known to have been zero'd.
6144 SIZE is the number of bytes of TARGET we are allowed to modify: this
6145 may not be the same as the size of EXP if we are assigning to a field
6146 which has been packed to exclude padding bits.
6147 If REVERSE is true, the store is to be done in reverse order. */
6149 static void
6150 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6151 bool reverse)
6153 tree type = TREE_TYPE (exp);
6154 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6155 HOST_WIDE_INT bitregion_end = size > 0 ? size * BITS_PER_UNIT - 1 : 0;
6157 switch (TREE_CODE (type))
6159 case RECORD_TYPE:
6160 case UNION_TYPE:
6161 case QUAL_UNION_TYPE:
6163 unsigned HOST_WIDE_INT idx;
6164 tree field, value;
6166 /* The storage order is specified for every aggregate type. */
6167 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6169 /* If size is zero or the target is already cleared, do nothing. */
6170 if (size == 0 || cleared)
6171 cleared = 1;
6172 /* We either clear the aggregate or indicate the value is dead. */
6173 else if ((TREE_CODE (type) == UNION_TYPE
6174 || TREE_CODE (type) == QUAL_UNION_TYPE)
6175 && ! CONSTRUCTOR_ELTS (exp))
6176 /* If the constructor is empty, clear the union. */
6178 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6179 cleared = 1;
6182 /* If we are building a static constructor into a register,
6183 set the initial value as zero so we can fold the value into
6184 a constant. But if more than one register is involved,
6185 this probably loses. */
6186 else if (REG_P (target) && TREE_STATIC (exp)
6187 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6189 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6190 cleared = 1;
6193 /* If the constructor has fewer fields than the structure or
6194 if we are initializing the structure to mostly zeros, clear
6195 the whole structure first. Don't do this if TARGET is a
6196 register whose mode size isn't equal to SIZE since
6197 clear_storage can't handle this case. */
6198 else if (size > 0
6199 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6200 || mostly_zeros_p (exp))
6201 && (!REG_P (target)
6202 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6203 == size)))
6205 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6206 cleared = 1;
6209 if (REG_P (target) && !cleared)
6210 emit_clobber (target);
6212 /* Store each element of the constructor into the
6213 corresponding field of TARGET. */
6214 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6216 machine_mode mode;
6217 HOST_WIDE_INT bitsize;
6218 HOST_WIDE_INT bitpos = 0;
6219 tree offset;
6220 rtx to_rtx = target;
6222 /* Just ignore missing fields. We cleared the whole
6223 structure, above, if any fields are missing. */
6224 if (field == 0)
6225 continue;
6227 if (cleared && initializer_zerop (value))
6228 continue;
6230 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6231 bitsize = tree_to_uhwi (DECL_SIZE (field));
6232 else
6233 gcc_unreachable ();
6235 mode = DECL_MODE (field);
6236 if (DECL_BIT_FIELD (field))
6237 mode = VOIDmode;
6239 offset = DECL_FIELD_OFFSET (field);
6240 if (tree_fits_shwi_p (offset)
6241 && tree_fits_shwi_p (bit_position (field)))
6243 bitpos = int_bit_position (field);
6244 offset = NULL_TREE;
6246 else
6247 gcc_unreachable ();
6249 /* If this initializes a field that is smaller than a
6250 word, at the start of a word, try to widen it to a full
6251 word. This special case allows us to output C++ member
6252 function initializations in a form that the optimizers
6253 can understand. */
6254 if (WORD_REGISTER_OPERATIONS
6255 && REG_P (target)
6256 && bitsize < BITS_PER_WORD
6257 && bitpos % BITS_PER_WORD == 0
6258 && GET_MODE_CLASS (mode) == MODE_INT
6259 && TREE_CODE (value) == INTEGER_CST
6260 && exp_size >= 0
6261 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6263 tree type = TREE_TYPE (value);
6265 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6267 type = lang_hooks.types.type_for_mode
6268 (word_mode, TYPE_UNSIGNED (type));
6269 value = fold_convert (type, value);
6270 /* Make sure the bits beyond the original bitsize are zero
6271 so that we can correctly avoid extra zeroing stores in
6272 later constructor elements. */
6273 tree bitsize_mask
6274 = wide_int_to_tree (type, wi::mask (bitsize, false,
6275 BITS_PER_WORD));
6276 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6279 if (BYTES_BIG_ENDIAN)
6280 value
6281 = fold_build2 (LSHIFT_EXPR, type, value,
6282 build_int_cst (type,
6283 BITS_PER_WORD - bitsize));
6284 bitsize = BITS_PER_WORD;
6285 mode = word_mode;
6288 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6289 && DECL_NONADDRESSABLE_P (field))
6291 to_rtx = copy_rtx (to_rtx);
6292 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6295 store_constructor_field (to_rtx, bitsize, bitpos,
6296 0, bitregion_end, mode,
6297 value, cleared,
6298 get_alias_set (TREE_TYPE (field)),
6299 reverse);
6301 break;
6303 case ARRAY_TYPE:
6305 tree value, index;
6306 unsigned HOST_WIDE_INT i;
6307 int need_to_clear;
6308 tree domain;
6309 tree elttype = TREE_TYPE (type);
6310 int const_bounds_p;
6311 HOST_WIDE_INT minelt = 0;
6312 HOST_WIDE_INT maxelt = 0;
6314 /* The storage order is specified for every aggregate type. */
6315 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6317 domain = TYPE_DOMAIN (type);
6318 const_bounds_p = (TYPE_MIN_VALUE (domain)
6319 && TYPE_MAX_VALUE (domain)
6320 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6321 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6323 /* If we have constant bounds for the range of the type, get them. */
6324 if (const_bounds_p)
6326 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6327 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6330 /* If the constructor has fewer elements than the array, clear
6331 the whole array first. Similarly if this is static
6332 constructor of a non-BLKmode object. */
6333 if (cleared)
6334 need_to_clear = 0;
6335 else if (REG_P (target) && TREE_STATIC (exp))
6336 need_to_clear = 1;
6337 else
6339 unsigned HOST_WIDE_INT idx;
6340 tree index, value;
6341 HOST_WIDE_INT count = 0, zero_count = 0;
6342 need_to_clear = ! const_bounds_p;
6344 /* This loop is a more accurate version of the loop in
6345 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6346 is also needed to check for missing elements. */
6347 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6349 HOST_WIDE_INT this_node_count;
6351 if (need_to_clear)
6352 break;
6354 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6356 tree lo_index = TREE_OPERAND (index, 0);
6357 tree hi_index = TREE_OPERAND (index, 1);
6359 if (! tree_fits_uhwi_p (lo_index)
6360 || ! tree_fits_uhwi_p (hi_index))
6362 need_to_clear = 1;
6363 break;
6366 this_node_count = (tree_to_uhwi (hi_index)
6367 - tree_to_uhwi (lo_index) + 1);
6369 else
6370 this_node_count = 1;
6372 count += this_node_count;
6373 if (mostly_zeros_p (value))
6374 zero_count += this_node_count;
6377 /* Clear the entire array first if there are any missing
6378 elements, or if the incidence of zero elements is >=
6379 75%. */
6380 if (! need_to_clear
6381 && (count < maxelt - minelt + 1
6382 || 4 * zero_count >= 3 * count))
6383 need_to_clear = 1;
6386 if (need_to_clear && size > 0)
6388 if (REG_P (target))
6389 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6390 else
6391 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6392 cleared = 1;
6395 if (!cleared && REG_P (target))
6396 /* Inform later passes that the old value is dead. */
6397 emit_clobber (target);
6399 /* Store each element of the constructor into the
6400 corresponding element of TARGET, determined by counting the
6401 elements. */
6402 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6404 machine_mode mode;
6405 HOST_WIDE_INT bitsize;
6406 HOST_WIDE_INT bitpos;
6407 rtx xtarget = target;
6409 if (cleared && initializer_zerop (value))
6410 continue;
6412 mode = TYPE_MODE (elttype);
6413 if (mode == BLKmode)
6414 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6415 ? tree_to_uhwi (TYPE_SIZE (elttype))
6416 : -1);
6417 else
6418 bitsize = GET_MODE_BITSIZE (mode);
6420 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6422 tree lo_index = TREE_OPERAND (index, 0);
6423 tree hi_index = TREE_OPERAND (index, 1);
6424 rtx index_r, pos_rtx;
6425 HOST_WIDE_INT lo, hi, count;
6426 tree position;
6428 /* If the range is constant and "small", unroll the loop. */
6429 if (const_bounds_p
6430 && tree_fits_shwi_p (lo_index)
6431 && tree_fits_shwi_p (hi_index)
6432 && (lo = tree_to_shwi (lo_index),
6433 hi = tree_to_shwi (hi_index),
6434 count = hi - lo + 1,
6435 (!MEM_P (target)
6436 || count <= 2
6437 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6438 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6439 <= 40 * 8)))))
6441 lo -= minelt; hi -= minelt;
6442 for (; lo <= hi; lo++)
6444 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6446 if (MEM_P (target)
6447 && !MEM_KEEP_ALIAS_SET_P (target)
6448 && TREE_CODE (type) == ARRAY_TYPE
6449 && TYPE_NONALIASED_COMPONENT (type))
6451 target = copy_rtx (target);
6452 MEM_KEEP_ALIAS_SET_P (target) = 1;
6455 store_constructor_field
6456 (target, bitsize, bitpos, 0, bitregion_end,
6457 mode, value, cleared,
6458 get_alias_set (elttype), reverse);
6461 else
6463 rtx_code_label *loop_start = gen_label_rtx ();
6464 rtx_code_label *loop_end = gen_label_rtx ();
6465 tree exit_cond;
6467 expand_normal (hi_index);
6469 index = build_decl (EXPR_LOCATION (exp),
6470 VAR_DECL, NULL_TREE, domain);
6471 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6472 SET_DECL_RTL (index, index_r);
6473 store_expr (lo_index, index_r, 0, false, reverse);
6475 /* Build the head of the loop. */
6476 do_pending_stack_adjust ();
6477 emit_label (loop_start);
6479 /* Assign value to element index. */
6480 position =
6481 fold_convert (ssizetype,
6482 fold_build2 (MINUS_EXPR,
6483 TREE_TYPE (index),
6484 index,
6485 TYPE_MIN_VALUE (domain)));
6487 position =
6488 size_binop (MULT_EXPR, position,
6489 fold_convert (ssizetype,
6490 TYPE_SIZE_UNIT (elttype)));
6492 pos_rtx = expand_normal (position);
6493 xtarget = offset_address (target, pos_rtx,
6494 highest_pow2_factor (position));
6495 xtarget = adjust_address (xtarget, mode, 0);
6496 if (TREE_CODE (value) == CONSTRUCTOR)
6497 store_constructor (value, xtarget, cleared,
6498 bitsize / BITS_PER_UNIT, reverse);
6499 else
6500 store_expr (value, xtarget, 0, false, reverse);
6502 /* Generate a conditional jump to exit the loop. */
6503 exit_cond = build2 (LT_EXPR, integer_type_node,
6504 index, hi_index);
6505 jumpif (exit_cond, loop_end,
6506 profile_probability::uninitialized ());
6508 /* Update the loop counter, and jump to the head of
6509 the loop. */
6510 expand_assignment (index,
6511 build2 (PLUS_EXPR, TREE_TYPE (index),
6512 index, integer_one_node),
6513 false);
6515 emit_jump (loop_start);
6517 /* Build the end of the loop. */
6518 emit_label (loop_end);
6521 else if ((index != 0 && ! tree_fits_shwi_p (index))
6522 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6524 tree position;
6526 if (index == 0)
6527 index = ssize_int (1);
6529 if (minelt)
6530 index = fold_convert (ssizetype,
6531 fold_build2 (MINUS_EXPR,
6532 TREE_TYPE (index),
6533 index,
6534 TYPE_MIN_VALUE (domain)));
6536 position =
6537 size_binop (MULT_EXPR, index,
6538 fold_convert (ssizetype,
6539 TYPE_SIZE_UNIT (elttype)));
6540 xtarget = offset_address (target,
6541 expand_normal (position),
6542 highest_pow2_factor (position));
6543 xtarget = adjust_address (xtarget, mode, 0);
6544 store_expr (value, xtarget, 0, false, reverse);
6546 else
6548 if (index != 0)
6549 bitpos = ((tree_to_shwi (index) - minelt)
6550 * tree_to_uhwi (TYPE_SIZE (elttype)));
6551 else
6552 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6554 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6555 && TREE_CODE (type) == ARRAY_TYPE
6556 && TYPE_NONALIASED_COMPONENT (type))
6558 target = copy_rtx (target);
6559 MEM_KEEP_ALIAS_SET_P (target) = 1;
6561 store_constructor_field (target, bitsize, bitpos, 0,
6562 bitregion_end, mode, value,
6563 cleared, get_alias_set (elttype),
6564 reverse);
6567 break;
6570 case VECTOR_TYPE:
6572 unsigned HOST_WIDE_INT idx;
6573 constructor_elt *ce;
6574 int i;
6575 int need_to_clear;
6576 int icode = CODE_FOR_nothing;
6577 tree elttype = TREE_TYPE (type);
6578 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6579 machine_mode eltmode = TYPE_MODE (elttype);
6580 HOST_WIDE_INT bitsize;
6581 HOST_WIDE_INT bitpos;
6582 rtvec vector = NULL;
6583 unsigned n_elts;
6584 alias_set_type alias;
6585 bool vec_vec_init_p = false;
6587 gcc_assert (eltmode != BLKmode);
6589 n_elts = TYPE_VECTOR_SUBPARTS (type);
6590 if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6592 machine_mode mode = GET_MODE (target);
6593 machine_mode emode = eltmode;
6595 if (CONSTRUCTOR_NELTS (exp)
6596 && (TREE_CODE (TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value))
6597 == VECTOR_TYPE))
6599 tree etype = TREE_TYPE (CONSTRUCTOR_ELT (exp, 0)->value);
6600 gcc_assert (CONSTRUCTOR_NELTS (exp) * TYPE_VECTOR_SUBPARTS (etype)
6601 == n_elts);
6602 emode = TYPE_MODE (etype);
6604 icode = (int) convert_optab_handler (vec_init_optab, mode, emode);
6605 if (icode != CODE_FOR_nothing)
6607 unsigned int i, n = n_elts;
6609 if (emode != eltmode)
6611 n = CONSTRUCTOR_NELTS (exp);
6612 vec_vec_init_p = true;
6614 vector = rtvec_alloc (n);
6615 for (i = 0; i < n; i++)
6616 RTVEC_ELT (vector, i) = CONST0_RTX (emode);
6620 /* If the constructor has fewer elements than the vector,
6621 clear the whole array first. Similarly if this is static
6622 constructor of a non-BLKmode object. */
6623 if (cleared)
6624 need_to_clear = 0;
6625 else if (REG_P (target) && TREE_STATIC (exp))
6626 need_to_clear = 1;
6627 else
6629 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6630 tree value;
6632 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6634 tree sz = TYPE_SIZE (TREE_TYPE (value));
6635 int n_elts_here
6636 = tree_to_uhwi (int_const_binop (TRUNC_DIV_EXPR, sz,
6637 TYPE_SIZE (elttype)));
6639 count += n_elts_here;
6640 if (mostly_zeros_p (value))
6641 zero_count += n_elts_here;
6644 /* Clear the entire vector first if there are any missing elements,
6645 or if the incidence of zero elements is >= 75%. */
6646 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6649 if (need_to_clear && size > 0 && !vector)
6651 if (REG_P (target))
6652 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6653 else
6654 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6655 cleared = 1;
6658 /* Inform later passes that the old value is dead. */
6659 if (!cleared && !vector && REG_P (target))
6660 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6662 if (MEM_P (target))
6663 alias = MEM_ALIAS_SET (target);
6664 else
6665 alias = get_alias_set (elttype);
6667 /* Store each element of the constructor into the corresponding
6668 element of TARGET, determined by counting the elements. */
6669 for (idx = 0, i = 0;
6670 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6671 idx++, i += bitsize / elt_size)
6673 HOST_WIDE_INT eltpos;
6674 tree value = ce->value;
6676 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6677 if (cleared && initializer_zerop (value))
6678 continue;
6680 if (ce->index)
6681 eltpos = tree_to_uhwi (ce->index);
6682 else
6683 eltpos = i;
6685 if (vector)
6687 if (vec_vec_init_p)
6689 gcc_assert (ce->index == NULL_TREE);
6690 gcc_assert (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE);
6691 eltpos = idx;
6693 else
6694 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6695 RTVEC_ELT (vector, eltpos) = expand_normal (value);
6697 else
6699 machine_mode value_mode
6700 = (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6701 ? TYPE_MODE (TREE_TYPE (value)) : eltmode);
6702 bitpos = eltpos * elt_size;
6703 store_constructor_field (target, bitsize, bitpos, 0,
6704 bitregion_end, value_mode,
6705 value, cleared, alias, reverse);
6709 if (vector)
6710 emit_insn (GEN_FCN (icode) (target,
6711 gen_rtx_PARALLEL (GET_MODE (target),
6712 vector)));
6713 break;
6716 default:
6717 gcc_unreachable ();
6721 /* Store the value of EXP (an expression tree)
6722 into a subfield of TARGET which has mode MODE and occupies
6723 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6724 If MODE is VOIDmode, it means that we are storing into a bit-field.
6726 BITREGION_START is bitpos of the first bitfield in this region.
6727 BITREGION_END is the bitpos of the ending bitfield in this region.
6728 These two fields are 0, if the C++ memory model does not apply,
6729 or we are not interested in keeping track of bitfield regions.
6731 Always return const0_rtx unless we have something particular to
6732 return.
6734 ALIAS_SET is the alias set for the destination. This value will
6735 (in general) be different from that for TARGET, since TARGET is a
6736 reference to the containing structure.
6738 If NONTEMPORAL is true, try generating a nontemporal store.
6740 If REVERSE is true, the store is to be done in reverse order. */
6742 static rtx
6743 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6744 unsigned HOST_WIDE_INT bitregion_start,
6745 unsigned HOST_WIDE_INT bitregion_end,
6746 machine_mode mode, tree exp,
6747 alias_set_type alias_set, bool nontemporal, bool reverse)
6749 if (TREE_CODE (exp) == ERROR_MARK)
6750 return const0_rtx;
6752 /* If we have nothing to store, do nothing unless the expression has
6753 side-effects. */
6754 if (bitsize == 0)
6755 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6757 if (GET_CODE (target) == CONCAT)
6759 /* We're storing into a struct containing a single __complex. */
6761 gcc_assert (!bitpos);
6762 return store_expr (exp, target, 0, nontemporal, reverse);
6765 /* If the structure is in a register or if the component
6766 is a bit field, we cannot use addressing to access it.
6767 Use bit-field techniques or SUBREG to store in it. */
6769 if (mode == VOIDmode
6770 || (mode != BLKmode && ! direct_store[(int) mode]
6771 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6772 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6773 || REG_P (target)
6774 || GET_CODE (target) == SUBREG
6775 /* If the field isn't aligned enough to store as an ordinary memref,
6776 store it as a bit field. */
6777 || (mode != BLKmode
6778 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6779 || bitpos % GET_MODE_ALIGNMENT (mode))
6780 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6781 || (bitpos % BITS_PER_UNIT != 0)))
6782 || (bitsize >= 0 && mode != BLKmode
6783 && GET_MODE_BITSIZE (mode) > bitsize)
6784 /* If the RHS and field are a constant size and the size of the
6785 RHS isn't the same size as the bitfield, we must use bitfield
6786 operations. */
6787 || (bitsize >= 0
6788 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6789 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6790 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6791 we will handle specially below. */
6792 && !(TREE_CODE (exp) == CONSTRUCTOR
6793 && bitsize % BITS_PER_UNIT == 0)
6794 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6795 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6796 includes some extra padding. store_expr / expand_expr will in
6797 that case call get_inner_reference that will have the bitsize
6798 we check here and thus the block move will not clobber the
6799 padding that shouldn't be clobbered. In the future we could
6800 replace the TREE_ADDRESSABLE check with a check that
6801 get_base_address needs to live in memory. */
6802 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6803 || TREE_CODE (exp) != COMPONENT_REF
6804 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6805 || (bitsize % BITS_PER_UNIT != 0)
6806 || (bitpos % BITS_PER_UNIT != 0)
6807 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6808 != 0)))
6809 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6810 decl we must use bitfield operations. */
6811 || (bitsize >= 0
6812 && TREE_CODE (exp) == MEM_REF
6813 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6814 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6815 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6816 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6818 rtx temp;
6819 gimple *nop_def;
6821 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6822 implies a mask operation. If the precision is the same size as
6823 the field we're storing into, that mask is redundant. This is
6824 particularly common with bit field assignments generated by the
6825 C front end. */
6826 nop_def = get_def_for_expr (exp, NOP_EXPR);
6827 if (nop_def)
6829 tree type = TREE_TYPE (exp);
6830 if (INTEGRAL_TYPE_P (type)
6831 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6832 && bitsize == TYPE_PRECISION (type))
6834 tree op = gimple_assign_rhs1 (nop_def);
6835 type = TREE_TYPE (op);
6836 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6837 exp = op;
6841 temp = expand_normal (exp);
6843 /* Handle calls that return values in multiple non-contiguous locations.
6844 The Irix 6 ABI has examples of this. */
6845 if (GET_CODE (temp) == PARALLEL)
6847 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6848 machine_mode temp_mode
6849 = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6850 rtx temp_target = gen_reg_rtx (temp_mode);
6851 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6852 temp = temp_target;
6855 /* Handle calls that return BLKmode values in registers. */
6856 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6858 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6859 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6860 temp = temp_target;
6863 /* If the value has aggregate type and an integral mode then, if BITSIZE
6864 is narrower than this mode and this is for big-endian data, we first
6865 need to put the value into the low-order bits for store_bit_field,
6866 except when MODE is BLKmode and BITSIZE larger than the word size
6867 (see the handling of fields larger than a word in store_bit_field).
6868 Moreover, the field may be not aligned on a byte boundary; in this
6869 case, if it has reverse storage order, it needs to be accessed as a
6870 scalar field with reverse storage order and we must first put the
6871 value into target order. */
6872 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6873 && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT)
6875 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (temp));
6877 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6879 if (reverse)
6880 temp = flip_storage_order (GET_MODE (temp), temp);
6882 if (bitsize < size
6883 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6884 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6885 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6886 size - bitsize, NULL_RTX, 1);
6889 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6890 if (mode != VOIDmode && mode != BLKmode
6891 && mode != TYPE_MODE (TREE_TYPE (exp)))
6892 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6894 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6895 and BITPOS must be aligned on a byte boundary. If so, we simply do
6896 a block copy. Likewise for a BLKmode-like TARGET. */
6897 if (GET_MODE (temp) == BLKmode
6898 && (GET_MODE (target) == BLKmode
6899 || (MEM_P (target)
6900 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6901 && (bitpos % BITS_PER_UNIT) == 0
6902 && (bitsize % BITS_PER_UNIT) == 0)))
6904 gcc_assert (MEM_P (target) && MEM_P (temp)
6905 && (bitpos % BITS_PER_UNIT) == 0);
6907 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6908 emit_block_move (target, temp,
6909 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6910 / BITS_PER_UNIT),
6911 BLOCK_OP_NORMAL);
6913 return const0_rtx;
6916 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6917 word size, we need to load the value (see again store_bit_field). */
6918 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6920 machine_mode temp_mode = smallest_mode_for_size (bitsize, MODE_INT);
6921 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6922 temp_mode, false, NULL);
6925 /* Store the value in the bitfield. */
6926 store_bit_field (target, bitsize, bitpos,
6927 bitregion_start, bitregion_end,
6928 mode, temp, reverse);
6930 return const0_rtx;
6932 else
6934 /* Now build a reference to just the desired component. */
6935 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6937 if (to_rtx == target)
6938 to_rtx = copy_rtx (to_rtx);
6940 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6941 set_mem_alias_set (to_rtx, alias_set);
6943 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6944 into a target smaller than its type; handle that case now. */
6945 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6947 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6948 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6949 return to_rtx;
6952 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6956 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6957 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6958 codes and find the ultimate containing object, which we return.
6960 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6961 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6962 storage order of the field.
6963 If the position of the field is variable, we store a tree
6964 giving the variable offset (in units) in *POFFSET.
6965 This offset is in addition to the bit position.
6966 If the position is not variable, we store 0 in *POFFSET.
6968 If any of the extraction expressions is volatile,
6969 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6971 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6972 Otherwise, it is a mode that can be used to access the field.
6974 If the field describes a variable-sized object, *PMODE is set to
6975 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6976 this case, but the address of the object can be found. */
6978 tree
6979 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6980 HOST_WIDE_INT *pbitpos, tree *poffset,
6981 machine_mode *pmode, int *punsignedp,
6982 int *preversep, int *pvolatilep)
6984 tree size_tree = 0;
6985 machine_mode mode = VOIDmode;
6986 bool blkmode_bitfield = false;
6987 tree offset = size_zero_node;
6988 offset_int bit_offset = 0;
6990 /* First get the mode, signedness, storage order and size. We do this from
6991 just the outermost expression. */
6992 *pbitsize = -1;
6993 if (TREE_CODE (exp) == COMPONENT_REF)
6995 tree field = TREE_OPERAND (exp, 1);
6996 size_tree = DECL_SIZE (field);
6997 if (flag_strict_volatile_bitfields > 0
6998 && TREE_THIS_VOLATILE (exp)
6999 && DECL_BIT_FIELD_TYPE (field)
7000 && DECL_MODE (field) != BLKmode)
7001 /* Volatile bitfields should be accessed in the mode of the
7002 field's type, not the mode computed based on the bit
7003 size. */
7004 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
7005 else if (!DECL_BIT_FIELD (field))
7006 mode = DECL_MODE (field);
7007 else if (DECL_MODE (field) == BLKmode)
7008 blkmode_bitfield = true;
7010 *punsignedp = DECL_UNSIGNED (field);
7012 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7014 size_tree = TREE_OPERAND (exp, 1);
7015 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7016 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7018 /* For vector types, with the correct size of access, use the mode of
7019 inner type. */
7020 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7021 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7022 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7023 mode = TYPE_MODE (TREE_TYPE (exp));
7025 else
7027 mode = TYPE_MODE (TREE_TYPE (exp));
7028 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7030 if (mode == BLKmode)
7031 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7032 else
7033 *pbitsize = GET_MODE_BITSIZE (mode);
7036 if (size_tree != 0)
7038 if (! tree_fits_uhwi_p (size_tree))
7039 mode = BLKmode, *pbitsize = -1;
7040 else
7041 *pbitsize = tree_to_uhwi (size_tree);
7044 *preversep = reverse_storage_order_for_component_p (exp);
7046 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7047 and find the ultimate containing object. */
7048 while (1)
7050 switch (TREE_CODE (exp))
7052 case BIT_FIELD_REF:
7053 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7054 break;
7056 case COMPONENT_REF:
7058 tree field = TREE_OPERAND (exp, 1);
7059 tree this_offset = component_ref_field_offset (exp);
7061 /* If this field hasn't been filled in yet, don't go past it.
7062 This should only happen when folding expressions made during
7063 type construction. */
7064 if (this_offset == 0)
7065 break;
7067 offset = size_binop (PLUS_EXPR, offset, this_offset);
7068 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7070 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7072 break;
7074 case ARRAY_REF:
7075 case ARRAY_RANGE_REF:
7077 tree index = TREE_OPERAND (exp, 1);
7078 tree low_bound = array_ref_low_bound (exp);
7079 tree unit_size = array_ref_element_size (exp);
7081 /* We assume all arrays have sizes that are a multiple of a byte.
7082 First subtract the lower bound, if any, in the type of the
7083 index, then convert to sizetype and multiply by the size of
7084 the array element. */
7085 if (! integer_zerop (low_bound))
7086 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7087 index, low_bound);
7089 offset = size_binop (PLUS_EXPR, offset,
7090 size_binop (MULT_EXPR,
7091 fold_convert (sizetype, index),
7092 unit_size));
7094 break;
7096 case REALPART_EXPR:
7097 break;
7099 case IMAGPART_EXPR:
7100 bit_offset += *pbitsize;
7101 break;
7103 case VIEW_CONVERT_EXPR:
7104 break;
7106 case MEM_REF:
7107 /* Hand back the decl for MEM[&decl, off]. */
7108 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7110 tree off = TREE_OPERAND (exp, 1);
7111 if (!integer_zerop (off))
7113 offset_int boff, coff = mem_ref_offset (exp);
7114 boff = coff << LOG2_BITS_PER_UNIT;
7115 bit_offset += boff;
7117 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7119 goto done;
7121 default:
7122 goto done;
7125 /* If any reference in the chain is volatile, the effect is volatile. */
7126 if (TREE_THIS_VOLATILE (exp))
7127 *pvolatilep = 1;
7129 exp = TREE_OPERAND (exp, 0);
7131 done:
7133 /* If OFFSET is constant, see if we can return the whole thing as a
7134 constant bit position. Make sure to handle overflow during
7135 this conversion. */
7136 if (TREE_CODE (offset) == INTEGER_CST)
7138 offset_int tem = wi::sext (wi::to_offset (offset),
7139 TYPE_PRECISION (sizetype));
7140 tem <<= LOG2_BITS_PER_UNIT;
7141 tem += bit_offset;
7142 if (wi::fits_shwi_p (tem))
7144 *pbitpos = tem.to_shwi ();
7145 *poffset = offset = NULL_TREE;
7149 /* Otherwise, split it up. */
7150 if (offset)
7152 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7153 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7155 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7156 offset_int tem = bit_offset.and_not (mask);
7157 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7158 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7159 bit_offset -= tem;
7160 tem >>= LOG2_BITS_PER_UNIT;
7161 offset = size_binop (PLUS_EXPR, offset,
7162 wide_int_to_tree (sizetype, tem));
7165 *pbitpos = bit_offset.to_shwi ();
7166 *poffset = offset;
7169 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7170 if (mode == VOIDmode
7171 && blkmode_bitfield
7172 && (*pbitpos % BITS_PER_UNIT) == 0
7173 && (*pbitsize % BITS_PER_UNIT) == 0)
7174 *pmode = BLKmode;
7175 else
7176 *pmode = mode;
7178 return exp;
7181 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7183 static unsigned HOST_WIDE_INT
7184 target_align (const_tree target)
7186 /* We might have a chain of nested references with intermediate misaligning
7187 bitfields components, so need to recurse to find out. */
7189 unsigned HOST_WIDE_INT this_align, outer_align;
7191 switch (TREE_CODE (target))
7193 case BIT_FIELD_REF:
7194 return 1;
7196 case COMPONENT_REF:
7197 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7198 outer_align = target_align (TREE_OPERAND (target, 0));
7199 return MIN (this_align, outer_align);
7201 case ARRAY_REF:
7202 case ARRAY_RANGE_REF:
7203 this_align = TYPE_ALIGN (TREE_TYPE (target));
7204 outer_align = target_align (TREE_OPERAND (target, 0));
7205 return MIN (this_align, outer_align);
7207 CASE_CONVERT:
7208 case NON_LVALUE_EXPR:
7209 case VIEW_CONVERT_EXPR:
7210 this_align = TYPE_ALIGN (TREE_TYPE (target));
7211 outer_align = target_align (TREE_OPERAND (target, 0));
7212 return MAX (this_align, outer_align);
7214 default:
7215 return TYPE_ALIGN (TREE_TYPE (target));
7220 /* Given an rtx VALUE that may contain additions and multiplications, return
7221 an equivalent value that just refers to a register, memory, or constant.
7222 This is done by generating instructions to perform the arithmetic and
7223 returning a pseudo-register containing the value.
7225 The returned value may be a REG, SUBREG, MEM or constant. */
7228 force_operand (rtx value, rtx target)
7230 rtx op1, op2;
7231 /* Use subtarget as the target for operand 0 of a binary operation. */
7232 rtx subtarget = get_subtarget (target);
7233 enum rtx_code code = GET_CODE (value);
7235 /* Check for subreg applied to an expression produced by loop optimizer. */
7236 if (code == SUBREG
7237 && !REG_P (SUBREG_REG (value))
7238 && !MEM_P (SUBREG_REG (value)))
7240 value
7241 = simplify_gen_subreg (GET_MODE (value),
7242 force_reg (GET_MODE (SUBREG_REG (value)),
7243 force_operand (SUBREG_REG (value),
7244 NULL_RTX)),
7245 GET_MODE (SUBREG_REG (value)),
7246 SUBREG_BYTE (value));
7247 code = GET_CODE (value);
7250 /* Check for a PIC address load. */
7251 if ((code == PLUS || code == MINUS)
7252 && XEXP (value, 0) == pic_offset_table_rtx
7253 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7254 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7255 || GET_CODE (XEXP (value, 1)) == CONST))
7257 if (!subtarget)
7258 subtarget = gen_reg_rtx (GET_MODE (value));
7259 emit_move_insn (subtarget, value);
7260 return subtarget;
7263 if (ARITHMETIC_P (value))
7265 op2 = XEXP (value, 1);
7266 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7267 subtarget = 0;
7268 if (code == MINUS && CONST_INT_P (op2))
7270 code = PLUS;
7271 op2 = negate_rtx (GET_MODE (value), op2);
7274 /* Check for an addition with OP2 a constant integer and our first
7275 operand a PLUS of a virtual register and something else. In that
7276 case, we want to emit the sum of the virtual register and the
7277 constant first and then add the other value. This allows virtual
7278 register instantiation to simply modify the constant rather than
7279 creating another one around this addition. */
7280 if (code == PLUS && CONST_INT_P (op2)
7281 && GET_CODE (XEXP (value, 0)) == PLUS
7282 && REG_P (XEXP (XEXP (value, 0), 0))
7283 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7284 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7286 rtx temp = expand_simple_binop (GET_MODE (value), code,
7287 XEXP (XEXP (value, 0), 0), op2,
7288 subtarget, 0, OPTAB_LIB_WIDEN);
7289 return expand_simple_binop (GET_MODE (value), code, temp,
7290 force_operand (XEXP (XEXP (value,
7291 0), 1), 0),
7292 target, 0, OPTAB_LIB_WIDEN);
7295 op1 = force_operand (XEXP (value, 0), subtarget);
7296 op2 = force_operand (op2, NULL_RTX);
7297 switch (code)
7299 case MULT:
7300 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7301 case DIV:
7302 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7303 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7304 target, 1, OPTAB_LIB_WIDEN);
7305 else
7306 return expand_divmod (0,
7307 FLOAT_MODE_P (GET_MODE (value))
7308 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7309 GET_MODE (value), op1, op2, target, 0);
7310 case MOD:
7311 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7312 target, 0);
7313 case UDIV:
7314 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7315 target, 1);
7316 case UMOD:
7317 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7318 target, 1);
7319 case ASHIFTRT:
7320 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7321 target, 0, OPTAB_LIB_WIDEN);
7322 default:
7323 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7324 target, 1, OPTAB_LIB_WIDEN);
7327 if (UNARY_P (value))
7329 if (!target)
7330 target = gen_reg_rtx (GET_MODE (value));
7331 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7332 switch (code)
7334 case ZERO_EXTEND:
7335 case SIGN_EXTEND:
7336 case TRUNCATE:
7337 case FLOAT_EXTEND:
7338 case FLOAT_TRUNCATE:
7339 convert_move (target, op1, code == ZERO_EXTEND);
7340 return target;
7342 case FIX:
7343 case UNSIGNED_FIX:
7344 expand_fix (target, op1, code == UNSIGNED_FIX);
7345 return target;
7347 case FLOAT:
7348 case UNSIGNED_FLOAT:
7349 expand_float (target, op1, code == UNSIGNED_FLOAT);
7350 return target;
7352 default:
7353 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7357 #ifdef INSN_SCHEDULING
7358 /* On machines that have insn scheduling, we want all memory reference to be
7359 explicit, so we need to deal with such paradoxical SUBREGs. */
7360 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7361 value
7362 = simplify_gen_subreg (GET_MODE (value),
7363 force_reg (GET_MODE (SUBREG_REG (value)),
7364 force_operand (SUBREG_REG (value),
7365 NULL_RTX)),
7366 GET_MODE (SUBREG_REG (value)),
7367 SUBREG_BYTE (value));
7368 #endif
7370 return value;
7373 /* Subroutine of expand_expr: return nonzero iff there is no way that
7374 EXP can reference X, which is being modified. TOP_P is nonzero if this
7375 call is going to be used to determine whether we need a temporary
7376 for EXP, as opposed to a recursive call to this function.
7378 It is always safe for this routine to return zero since it merely
7379 searches for optimization opportunities. */
7382 safe_from_p (const_rtx x, tree exp, int top_p)
7384 rtx exp_rtl = 0;
7385 int i, nops;
7387 if (x == 0
7388 /* If EXP has varying size, we MUST use a target since we currently
7389 have no way of allocating temporaries of variable size
7390 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7391 So we assume here that something at a higher level has prevented a
7392 clash. This is somewhat bogus, but the best we can do. Only
7393 do this when X is BLKmode and when we are at the top level. */
7394 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7395 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7396 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7397 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7398 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7399 != INTEGER_CST)
7400 && GET_MODE (x) == BLKmode)
7401 /* If X is in the outgoing argument area, it is always safe. */
7402 || (MEM_P (x)
7403 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7404 || (GET_CODE (XEXP (x, 0)) == PLUS
7405 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7406 return 1;
7408 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7409 find the underlying pseudo. */
7410 if (GET_CODE (x) == SUBREG)
7412 x = SUBREG_REG (x);
7413 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7414 return 0;
7417 /* Now look at our tree code and possibly recurse. */
7418 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7420 case tcc_declaration:
7421 exp_rtl = DECL_RTL_IF_SET (exp);
7422 break;
7424 case tcc_constant:
7425 return 1;
7427 case tcc_exceptional:
7428 if (TREE_CODE (exp) == TREE_LIST)
7430 while (1)
7432 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7433 return 0;
7434 exp = TREE_CHAIN (exp);
7435 if (!exp)
7436 return 1;
7437 if (TREE_CODE (exp) != TREE_LIST)
7438 return safe_from_p (x, exp, 0);
7441 else if (TREE_CODE (exp) == CONSTRUCTOR)
7443 constructor_elt *ce;
7444 unsigned HOST_WIDE_INT idx;
7446 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7447 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7448 || !safe_from_p (x, ce->value, 0))
7449 return 0;
7450 return 1;
7452 else if (TREE_CODE (exp) == ERROR_MARK)
7453 return 1; /* An already-visited SAVE_EXPR? */
7454 else
7455 return 0;
7457 case tcc_statement:
7458 /* The only case we look at here is the DECL_INITIAL inside a
7459 DECL_EXPR. */
7460 return (TREE_CODE (exp) != DECL_EXPR
7461 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7462 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7463 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7465 case tcc_binary:
7466 case tcc_comparison:
7467 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7468 return 0;
7469 /* Fall through. */
7471 case tcc_unary:
7472 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7474 case tcc_expression:
7475 case tcc_reference:
7476 case tcc_vl_exp:
7477 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7478 the expression. If it is set, we conflict iff we are that rtx or
7479 both are in memory. Otherwise, we check all operands of the
7480 expression recursively. */
7482 switch (TREE_CODE (exp))
7484 case ADDR_EXPR:
7485 /* If the operand is static or we are static, we can't conflict.
7486 Likewise if we don't conflict with the operand at all. */
7487 if (staticp (TREE_OPERAND (exp, 0))
7488 || TREE_STATIC (exp)
7489 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7490 return 1;
7492 /* Otherwise, the only way this can conflict is if we are taking
7493 the address of a DECL a that address if part of X, which is
7494 very rare. */
7495 exp = TREE_OPERAND (exp, 0);
7496 if (DECL_P (exp))
7498 if (!DECL_RTL_SET_P (exp)
7499 || !MEM_P (DECL_RTL (exp)))
7500 return 0;
7501 else
7502 exp_rtl = XEXP (DECL_RTL (exp), 0);
7504 break;
7506 case MEM_REF:
7507 if (MEM_P (x)
7508 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7509 get_alias_set (exp)))
7510 return 0;
7511 break;
7513 case CALL_EXPR:
7514 /* Assume that the call will clobber all hard registers and
7515 all of memory. */
7516 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7517 || MEM_P (x))
7518 return 0;
7519 break;
7521 case WITH_CLEANUP_EXPR:
7522 case CLEANUP_POINT_EXPR:
7523 /* Lowered by gimplify.c. */
7524 gcc_unreachable ();
7526 case SAVE_EXPR:
7527 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7529 default:
7530 break;
7533 /* If we have an rtx, we do not need to scan our operands. */
7534 if (exp_rtl)
7535 break;
7537 nops = TREE_OPERAND_LENGTH (exp);
7538 for (i = 0; i < nops; i++)
7539 if (TREE_OPERAND (exp, i) != 0
7540 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7541 return 0;
7543 break;
7545 case tcc_type:
7546 /* Should never get a type here. */
7547 gcc_unreachable ();
7550 /* If we have an rtl, find any enclosed object. Then see if we conflict
7551 with it. */
7552 if (exp_rtl)
7554 if (GET_CODE (exp_rtl) == SUBREG)
7556 exp_rtl = SUBREG_REG (exp_rtl);
7557 if (REG_P (exp_rtl)
7558 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7559 return 0;
7562 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7563 are memory and they conflict. */
7564 return ! (rtx_equal_p (x, exp_rtl)
7565 || (MEM_P (x) && MEM_P (exp_rtl)
7566 && true_dependence (exp_rtl, VOIDmode, x)));
7569 /* If we reach here, it is safe. */
7570 return 1;
7574 /* Return the highest power of two that EXP is known to be a multiple of.
7575 This is used in updating alignment of MEMs in array references. */
7577 unsigned HOST_WIDE_INT
7578 highest_pow2_factor (const_tree exp)
7580 unsigned HOST_WIDE_INT ret;
7581 int trailing_zeros = tree_ctz (exp);
7582 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7583 return BIGGEST_ALIGNMENT;
7584 ret = HOST_WIDE_INT_1U << trailing_zeros;
7585 if (ret > BIGGEST_ALIGNMENT)
7586 return BIGGEST_ALIGNMENT;
7587 return ret;
7590 /* Similar, except that the alignment requirements of TARGET are
7591 taken into account. Assume it is at least as aligned as its
7592 type, unless it is a COMPONENT_REF in which case the layout of
7593 the structure gives the alignment. */
7595 static unsigned HOST_WIDE_INT
7596 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7598 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7599 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7601 return MAX (factor, talign);
7604 /* Convert the tree comparison code TCODE to the rtl one where the
7605 signedness is UNSIGNEDP. */
7607 static enum rtx_code
7608 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7610 enum rtx_code code;
7611 switch (tcode)
7613 case EQ_EXPR:
7614 code = EQ;
7615 break;
7616 case NE_EXPR:
7617 code = NE;
7618 break;
7619 case LT_EXPR:
7620 code = unsignedp ? LTU : LT;
7621 break;
7622 case LE_EXPR:
7623 code = unsignedp ? LEU : LE;
7624 break;
7625 case GT_EXPR:
7626 code = unsignedp ? GTU : GT;
7627 break;
7628 case GE_EXPR:
7629 code = unsignedp ? GEU : GE;
7630 break;
7631 case UNORDERED_EXPR:
7632 code = UNORDERED;
7633 break;
7634 case ORDERED_EXPR:
7635 code = ORDERED;
7636 break;
7637 case UNLT_EXPR:
7638 code = UNLT;
7639 break;
7640 case UNLE_EXPR:
7641 code = UNLE;
7642 break;
7643 case UNGT_EXPR:
7644 code = UNGT;
7645 break;
7646 case UNGE_EXPR:
7647 code = UNGE;
7648 break;
7649 case UNEQ_EXPR:
7650 code = UNEQ;
7651 break;
7652 case LTGT_EXPR:
7653 code = LTGT;
7654 break;
7656 default:
7657 gcc_unreachable ();
7659 return code;
7662 /* Subroutine of expand_expr. Expand the two operands of a binary
7663 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7664 The value may be stored in TARGET if TARGET is nonzero. The
7665 MODIFIER argument is as documented by expand_expr. */
7667 void
7668 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7669 enum expand_modifier modifier)
7671 if (! safe_from_p (target, exp1, 1))
7672 target = 0;
7673 if (operand_equal_p (exp0, exp1, 0))
7675 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7676 *op1 = copy_rtx (*op0);
7678 else
7680 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7681 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7686 /* Return a MEM that contains constant EXP. DEFER is as for
7687 output_constant_def and MODIFIER is as for expand_expr. */
7689 static rtx
7690 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7692 rtx mem;
7694 mem = output_constant_def (exp, defer);
7695 if (modifier != EXPAND_INITIALIZER)
7696 mem = use_anchored_address (mem);
7697 return mem;
7700 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7701 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7703 static rtx
7704 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7705 enum expand_modifier modifier, addr_space_t as)
7707 rtx result, subtarget;
7708 tree inner, offset;
7709 HOST_WIDE_INT bitsize, bitpos;
7710 int unsignedp, reversep, volatilep = 0;
7711 machine_mode mode1;
7713 /* If we are taking the address of a constant and are at the top level,
7714 we have to use output_constant_def since we can't call force_const_mem
7715 at top level. */
7716 /* ??? This should be considered a front-end bug. We should not be
7717 generating ADDR_EXPR of something that isn't an LVALUE. The only
7718 exception here is STRING_CST. */
7719 if (CONSTANT_CLASS_P (exp))
7721 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7722 if (modifier < EXPAND_SUM)
7723 result = force_operand (result, target);
7724 return result;
7727 /* Everything must be something allowed by is_gimple_addressable. */
7728 switch (TREE_CODE (exp))
7730 case INDIRECT_REF:
7731 /* This case will happen via recursion for &a->b. */
7732 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7734 case MEM_REF:
7736 tree tem = TREE_OPERAND (exp, 0);
7737 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7738 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7739 return expand_expr (tem, target, tmode, modifier);
7742 case CONST_DECL:
7743 /* Expand the initializer like constants above. */
7744 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7745 0, modifier), 0);
7746 if (modifier < EXPAND_SUM)
7747 result = force_operand (result, target);
7748 return result;
7750 case REALPART_EXPR:
7751 /* The real part of the complex number is always first, therefore
7752 the address is the same as the address of the parent object. */
7753 offset = 0;
7754 bitpos = 0;
7755 inner = TREE_OPERAND (exp, 0);
7756 break;
7758 case IMAGPART_EXPR:
7759 /* The imaginary part of the complex number is always second.
7760 The expression is therefore always offset by the size of the
7761 scalar type. */
7762 offset = 0;
7763 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7764 inner = TREE_OPERAND (exp, 0);
7765 break;
7767 case COMPOUND_LITERAL_EXPR:
7768 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7769 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7770 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7771 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7772 the initializers aren't gimplified. */
7773 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7774 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7775 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7776 target, tmode, modifier, as);
7777 /* FALLTHRU */
7778 default:
7779 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7780 expand_expr, as that can have various side effects; LABEL_DECLs for
7781 example, may not have their DECL_RTL set yet. Expand the rtl of
7782 CONSTRUCTORs too, which should yield a memory reference for the
7783 constructor's contents. Assume language specific tree nodes can
7784 be expanded in some interesting way. */
7785 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7786 if (DECL_P (exp)
7787 || TREE_CODE (exp) == CONSTRUCTOR
7788 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7790 result = expand_expr (exp, target, tmode,
7791 modifier == EXPAND_INITIALIZER
7792 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7794 /* If the DECL isn't in memory, then the DECL wasn't properly
7795 marked TREE_ADDRESSABLE, which will be either a front-end
7796 or a tree optimizer bug. */
7798 gcc_assert (MEM_P (result));
7799 result = XEXP (result, 0);
7801 /* ??? Is this needed anymore? */
7802 if (DECL_P (exp))
7803 TREE_USED (exp) = 1;
7805 if (modifier != EXPAND_INITIALIZER
7806 && modifier != EXPAND_CONST_ADDRESS
7807 && modifier != EXPAND_SUM)
7808 result = force_operand (result, target);
7809 return result;
7812 /* Pass FALSE as the last argument to get_inner_reference although
7813 we are expanding to RTL. The rationale is that we know how to
7814 handle "aligning nodes" here: we can just bypass them because
7815 they won't change the final object whose address will be returned
7816 (they actually exist only for that purpose). */
7817 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7818 &unsignedp, &reversep, &volatilep);
7819 break;
7822 /* We must have made progress. */
7823 gcc_assert (inner != exp);
7825 subtarget = offset || bitpos ? NULL_RTX : target;
7826 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7827 inner alignment, force the inner to be sufficiently aligned. */
7828 if (CONSTANT_CLASS_P (inner)
7829 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7831 inner = copy_node (inner);
7832 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7833 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7834 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7836 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7838 if (offset)
7840 rtx tmp;
7842 if (modifier != EXPAND_NORMAL)
7843 result = force_operand (result, NULL);
7844 tmp = expand_expr (offset, NULL_RTX, tmode,
7845 modifier == EXPAND_INITIALIZER
7846 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7848 /* expand_expr is allowed to return an object in a mode other
7849 than TMODE. If it did, we need to convert. */
7850 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7851 tmp = convert_modes (tmode, GET_MODE (tmp),
7852 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7853 result = convert_memory_address_addr_space (tmode, result, as);
7854 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7856 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7857 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7858 else
7860 subtarget = bitpos ? NULL_RTX : target;
7861 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7862 1, OPTAB_LIB_WIDEN);
7866 if (bitpos)
7868 /* Someone beforehand should have rejected taking the address
7869 of such an object. */
7870 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7872 result = convert_memory_address_addr_space (tmode, result, as);
7873 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7874 if (modifier < EXPAND_SUM)
7875 result = force_operand (result, target);
7878 return result;
7881 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7882 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7884 static rtx
7885 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7886 enum expand_modifier modifier)
7888 addr_space_t as = ADDR_SPACE_GENERIC;
7889 machine_mode address_mode = Pmode;
7890 machine_mode pointer_mode = ptr_mode;
7891 machine_mode rmode;
7892 rtx result;
7894 /* Target mode of VOIDmode says "whatever's natural". */
7895 if (tmode == VOIDmode)
7896 tmode = TYPE_MODE (TREE_TYPE (exp));
7898 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7900 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7901 address_mode = targetm.addr_space.address_mode (as);
7902 pointer_mode = targetm.addr_space.pointer_mode (as);
7905 /* We can get called with some Weird Things if the user does silliness
7906 like "(short) &a". In that case, convert_memory_address won't do
7907 the right thing, so ignore the given target mode. */
7908 if (tmode != address_mode && tmode != pointer_mode)
7909 tmode = address_mode;
7911 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7912 tmode, modifier, as);
7914 /* Despite expand_expr claims concerning ignoring TMODE when not
7915 strictly convenient, stuff breaks if we don't honor it. Note
7916 that combined with the above, we only do this for pointer modes. */
7917 rmode = GET_MODE (result);
7918 if (rmode == VOIDmode)
7919 rmode = tmode;
7920 if (rmode != tmode)
7921 result = convert_memory_address_addr_space (tmode, result, as);
7923 return result;
7926 /* Generate code for computing CONSTRUCTOR EXP.
7927 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7928 is TRUE, instead of creating a temporary variable in memory
7929 NULL is returned and the caller needs to handle it differently. */
7931 static rtx
7932 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7933 bool avoid_temp_mem)
7935 tree type = TREE_TYPE (exp);
7936 machine_mode mode = TYPE_MODE (type);
7938 /* Try to avoid creating a temporary at all. This is possible
7939 if all of the initializer is zero.
7940 FIXME: try to handle all [0..255] initializers we can handle
7941 with memset. */
7942 if (TREE_STATIC (exp)
7943 && !TREE_ADDRESSABLE (exp)
7944 && target != 0 && mode == BLKmode
7945 && all_zeros_p (exp))
7947 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7948 return target;
7951 /* All elts simple constants => refer to a constant in memory. But
7952 if this is a non-BLKmode mode, let it store a field at a time
7953 since that should make a CONST_INT, CONST_WIDE_INT or
7954 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7955 use, it is best to store directly into the target unless the type
7956 is large enough that memcpy will be used. If we are making an
7957 initializer and all operands are constant, put it in memory as
7958 well.
7960 FIXME: Avoid trying to fill vector constructors piece-meal.
7961 Output them with output_constant_def below unless we're sure
7962 they're zeros. This should go away when vector initializers
7963 are treated like VECTOR_CST instead of arrays. */
7964 if ((TREE_STATIC (exp)
7965 && ((mode == BLKmode
7966 && ! (target != 0 && safe_from_p (target, exp, 1)))
7967 || TREE_ADDRESSABLE (exp)
7968 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7969 && (! can_move_by_pieces
7970 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7971 TYPE_ALIGN (type)))
7972 && ! mostly_zeros_p (exp))))
7973 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7974 && TREE_CONSTANT (exp)))
7976 rtx constructor;
7978 if (avoid_temp_mem)
7979 return NULL_RTX;
7981 constructor = expand_expr_constant (exp, 1, modifier);
7983 if (modifier != EXPAND_CONST_ADDRESS
7984 && modifier != EXPAND_INITIALIZER
7985 && modifier != EXPAND_SUM)
7986 constructor = validize_mem (constructor);
7988 return constructor;
7991 /* Handle calls that pass values in multiple non-contiguous
7992 locations. The Irix 6 ABI has examples of this. */
7993 if (target == 0 || ! safe_from_p (target, exp, 1)
7994 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7996 if (avoid_temp_mem)
7997 return NULL_RTX;
7999 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
8002 store_constructor (exp, target, 0, int_expr_size (exp), false);
8003 return target;
8007 /* expand_expr: generate code for computing expression EXP.
8008 An rtx for the computed value is returned. The value is never null.
8009 In the case of a void EXP, const0_rtx is returned.
8011 The value may be stored in TARGET if TARGET is nonzero.
8012 TARGET is just a suggestion; callers must assume that
8013 the rtx returned may not be the same as TARGET.
8015 If TARGET is CONST0_RTX, it means that the value will be ignored.
8017 If TMODE is not VOIDmode, it suggests generating the
8018 result in mode TMODE. But this is done only when convenient.
8019 Otherwise, TMODE is ignored and the value generated in its natural mode.
8020 TMODE is just a suggestion; callers must assume that
8021 the rtx returned may not have mode TMODE.
8023 Note that TARGET may have neither TMODE nor MODE. In that case, it
8024 probably will not be used.
8026 If MODIFIER is EXPAND_SUM then when EXP is an addition
8027 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8028 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8029 products as above, or REG or MEM, or constant.
8030 Ordinarily in such cases we would output mul or add instructions
8031 and then return a pseudo reg containing the sum.
8033 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8034 it also marks a label as absolutely required (it can't be dead).
8035 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8036 This is used for outputting expressions used in initializers.
8038 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8039 with a constant address even if that address is not normally legitimate.
8040 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8042 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8043 a call parameter. Such targets require special care as we haven't yet
8044 marked TARGET so that it's safe from being trashed by libcalls. We
8045 don't want to use TARGET for anything but the final result;
8046 Intermediate values must go elsewhere. Additionally, calls to
8047 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8049 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8050 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8051 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8052 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8053 recursively.
8055 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8056 In this case, we don't adjust a returned MEM rtx that wouldn't be
8057 sufficiently aligned for its mode; instead, it's up to the caller
8058 to deal with it afterwards. This is used to make sure that unaligned
8059 base objects for which out-of-bounds accesses are supported, for
8060 example record types with trailing arrays, aren't realigned behind
8061 the back of the caller.
8062 The normal operating mode is to pass FALSE for this parameter. */
8065 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8066 enum expand_modifier modifier, rtx *alt_rtl,
8067 bool inner_reference_p)
8069 rtx ret;
8071 /* Handle ERROR_MARK before anybody tries to access its type. */
8072 if (TREE_CODE (exp) == ERROR_MARK
8073 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8075 ret = CONST0_RTX (tmode);
8076 return ret ? ret : const0_rtx;
8079 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8080 inner_reference_p);
8081 return ret;
8084 /* Try to expand the conditional expression which is represented by
8085 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8086 return the rtl reg which represents the result. Otherwise return
8087 NULL_RTX. */
8089 static rtx
8090 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8091 tree treeop1 ATTRIBUTE_UNUSED,
8092 tree treeop2 ATTRIBUTE_UNUSED)
8094 rtx insn;
8095 rtx op00, op01, op1, op2;
8096 enum rtx_code comparison_code;
8097 machine_mode comparison_mode;
8098 gimple *srcstmt;
8099 rtx temp;
8100 tree type = TREE_TYPE (treeop1);
8101 int unsignedp = TYPE_UNSIGNED (type);
8102 machine_mode mode = TYPE_MODE (type);
8103 machine_mode orig_mode = mode;
8104 static bool expanding_cond_expr_using_cmove = false;
8106 /* Conditional move expansion can end up TERing two operands which,
8107 when recursively hitting conditional expressions can result in
8108 exponential behavior if the cmove expansion ultimatively fails.
8109 It's hardly profitable to TER a cmove into a cmove so avoid doing
8110 that by failing early if we end up recursing. */
8111 if (expanding_cond_expr_using_cmove)
8112 return NULL_RTX;
8114 /* If we cannot do a conditional move on the mode, try doing it
8115 with the promoted mode. */
8116 if (!can_conditionally_move_p (mode))
8118 mode = promote_mode (type, mode, &unsignedp);
8119 if (!can_conditionally_move_p (mode))
8120 return NULL_RTX;
8121 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8123 else
8124 temp = assign_temp (type, 0, 1);
8126 expanding_cond_expr_using_cmove = true;
8127 start_sequence ();
8128 expand_operands (treeop1, treeop2,
8129 temp, &op1, &op2, EXPAND_NORMAL);
8131 if (TREE_CODE (treeop0) == SSA_NAME
8132 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8134 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8135 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8136 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8137 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8138 comparison_mode = TYPE_MODE (type);
8139 unsignedp = TYPE_UNSIGNED (type);
8140 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8142 else if (COMPARISON_CLASS_P (treeop0))
8144 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8145 enum tree_code cmpcode = TREE_CODE (treeop0);
8146 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8147 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8148 unsignedp = TYPE_UNSIGNED (type);
8149 comparison_mode = TYPE_MODE (type);
8150 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8152 else
8154 op00 = expand_normal (treeop0);
8155 op01 = const0_rtx;
8156 comparison_code = NE;
8157 comparison_mode = GET_MODE (op00);
8158 if (comparison_mode == VOIDmode)
8159 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8161 expanding_cond_expr_using_cmove = false;
8163 if (GET_MODE (op1) != mode)
8164 op1 = gen_lowpart (mode, op1);
8166 if (GET_MODE (op2) != mode)
8167 op2 = gen_lowpart (mode, op2);
8169 /* Try to emit the conditional move. */
8170 insn = emit_conditional_move (temp, comparison_code,
8171 op00, op01, comparison_mode,
8172 op1, op2, mode,
8173 unsignedp);
8175 /* If we could do the conditional move, emit the sequence,
8176 and return. */
8177 if (insn)
8179 rtx_insn *seq = get_insns ();
8180 end_sequence ();
8181 emit_insn (seq);
8182 return convert_modes (orig_mode, mode, temp, 0);
8185 /* Otherwise discard the sequence and fall back to code with
8186 branches. */
8187 end_sequence ();
8188 return NULL_RTX;
8192 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8193 enum expand_modifier modifier)
8195 rtx op0, op1, op2, temp;
8196 rtx_code_label *lab;
8197 tree type;
8198 int unsignedp;
8199 machine_mode mode;
8200 enum tree_code code = ops->code;
8201 optab this_optab;
8202 rtx subtarget, original_target;
8203 int ignore;
8204 bool reduce_bit_field;
8205 location_t loc = ops->location;
8206 tree treeop0, treeop1, treeop2;
8207 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8208 ? reduce_to_bit_field_precision ((expr), \
8209 target, \
8210 type) \
8211 : (expr))
8213 type = ops->type;
8214 mode = TYPE_MODE (type);
8215 unsignedp = TYPE_UNSIGNED (type);
8217 treeop0 = ops->op0;
8218 treeop1 = ops->op1;
8219 treeop2 = ops->op2;
8221 /* We should be called only on simple (binary or unary) expressions,
8222 exactly those that are valid in gimple expressions that aren't
8223 GIMPLE_SINGLE_RHS (or invalid). */
8224 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8225 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8226 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8228 ignore = (target == const0_rtx
8229 || ((CONVERT_EXPR_CODE_P (code)
8230 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8231 && TREE_CODE (type) == VOID_TYPE));
8233 /* We should be called only if we need the result. */
8234 gcc_assert (!ignore);
8236 /* An operation in what may be a bit-field type needs the
8237 result to be reduced to the precision of the bit-field type,
8238 which is narrower than that of the type's mode. */
8239 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8240 && !type_has_mode_precision_p (type));
8242 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8243 target = 0;
8245 /* Use subtarget as the target for operand 0 of a binary operation. */
8246 subtarget = get_subtarget (target);
8247 original_target = target;
8249 switch (code)
8251 case NON_LVALUE_EXPR:
8252 case PAREN_EXPR:
8253 CASE_CONVERT:
8254 if (treeop0 == error_mark_node)
8255 return const0_rtx;
8257 if (TREE_CODE (type) == UNION_TYPE)
8259 tree valtype = TREE_TYPE (treeop0);
8261 /* If both input and output are BLKmode, this conversion isn't doing
8262 anything except possibly changing memory attribute. */
8263 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8265 rtx result = expand_expr (treeop0, target, tmode,
8266 modifier);
8268 result = copy_rtx (result);
8269 set_mem_attributes (result, type, 0);
8270 return result;
8273 if (target == 0)
8275 if (TYPE_MODE (type) != BLKmode)
8276 target = gen_reg_rtx (TYPE_MODE (type));
8277 else
8278 target = assign_temp (type, 1, 1);
8281 if (MEM_P (target))
8282 /* Store data into beginning of memory target. */
8283 store_expr (treeop0,
8284 adjust_address (target, TYPE_MODE (valtype), 0),
8285 modifier == EXPAND_STACK_PARM,
8286 false, TYPE_REVERSE_STORAGE_ORDER (type));
8288 else
8290 gcc_assert (REG_P (target)
8291 && !TYPE_REVERSE_STORAGE_ORDER (type));
8293 /* Store this field into a union of the proper type. */
8294 store_field (target,
8295 MIN ((int_size_in_bytes (TREE_TYPE
8296 (treeop0))
8297 * BITS_PER_UNIT),
8298 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8299 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8300 false, false);
8303 /* Return the entire union. */
8304 return target;
8307 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8309 op0 = expand_expr (treeop0, target, VOIDmode,
8310 modifier);
8312 /* If the signedness of the conversion differs and OP0 is
8313 a promoted SUBREG, clear that indication since we now
8314 have to do the proper extension. */
8315 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8316 && GET_CODE (op0) == SUBREG)
8317 SUBREG_PROMOTED_VAR_P (op0) = 0;
8319 return REDUCE_BIT_FIELD (op0);
8322 op0 = expand_expr (treeop0, NULL_RTX, mode,
8323 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8324 if (GET_MODE (op0) == mode)
8327 /* If OP0 is a constant, just convert it into the proper mode. */
8328 else if (CONSTANT_P (op0))
8330 tree inner_type = TREE_TYPE (treeop0);
8331 machine_mode inner_mode = GET_MODE (op0);
8333 if (inner_mode == VOIDmode)
8334 inner_mode = TYPE_MODE (inner_type);
8336 if (modifier == EXPAND_INITIALIZER)
8337 op0 = lowpart_subreg (mode, op0, inner_mode);
8338 else
8339 op0= convert_modes (mode, inner_mode, op0,
8340 TYPE_UNSIGNED (inner_type));
8343 else if (modifier == EXPAND_INITIALIZER)
8344 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8345 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8347 else if (target == 0)
8348 op0 = convert_to_mode (mode, op0,
8349 TYPE_UNSIGNED (TREE_TYPE
8350 (treeop0)));
8351 else
8353 convert_move (target, op0,
8354 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8355 op0 = target;
8358 return REDUCE_BIT_FIELD (op0);
8360 case ADDR_SPACE_CONVERT_EXPR:
8362 tree treeop0_type = TREE_TYPE (treeop0);
8364 gcc_assert (POINTER_TYPE_P (type));
8365 gcc_assert (POINTER_TYPE_P (treeop0_type));
8367 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8368 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8370 /* Conversions between pointers to the same address space should
8371 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8372 gcc_assert (as_to != as_from);
8374 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8376 /* Ask target code to handle conversion between pointers
8377 to overlapping address spaces. */
8378 if (targetm.addr_space.subset_p (as_to, as_from)
8379 || targetm.addr_space.subset_p (as_from, as_to))
8381 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8383 else
8385 /* For disjoint address spaces, converting anything but a null
8386 pointer invokes undefined behavior. We truncate or extend the
8387 value as if we'd converted via integers, which handles 0 as
8388 required, and all others as the programmer likely expects. */
8389 #ifndef POINTERS_EXTEND_UNSIGNED
8390 const int POINTERS_EXTEND_UNSIGNED = 1;
8391 #endif
8392 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8393 op0, POINTERS_EXTEND_UNSIGNED);
8395 gcc_assert (op0);
8396 return op0;
8399 case POINTER_PLUS_EXPR:
8400 /* Even though the sizetype mode and the pointer's mode can be different
8401 expand is able to handle this correctly and get the correct result out
8402 of the PLUS_EXPR code. */
8403 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8404 if sizetype precision is smaller than pointer precision. */
8405 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8406 treeop1 = fold_convert_loc (loc, type,
8407 fold_convert_loc (loc, ssizetype,
8408 treeop1));
8409 /* If sizetype precision is larger than pointer precision, truncate the
8410 offset to have matching modes. */
8411 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8412 treeop1 = fold_convert_loc (loc, type, treeop1);
8413 /* FALLTHRU */
8415 case PLUS_EXPR:
8416 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8417 something else, make sure we add the register to the constant and
8418 then to the other thing. This case can occur during strength
8419 reduction and doing it this way will produce better code if the
8420 frame pointer or argument pointer is eliminated.
8422 fold-const.c will ensure that the constant is always in the inner
8423 PLUS_EXPR, so the only case we need to do anything about is if
8424 sp, ap, or fp is our second argument, in which case we must swap
8425 the innermost first argument and our second argument. */
8427 if (TREE_CODE (treeop0) == PLUS_EXPR
8428 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8429 && VAR_P (treeop1)
8430 && (DECL_RTL (treeop1) == frame_pointer_rtx
8431 || DECL_RTL (treeop1) == stack_pointer_rtx
8432 || DECL_RTL (treeop1) == arg_pointer_rtx))
8434 gcc_unreachable ();
8437 /* If the result is to be ptr_mode and we are adding an integer to
8438 something, we might be forming a constant. So try to use
8439 plus_constant. If it produces a sum and we can't accept it,
8440 use force_operand. This allows P = &ARR[const] to generate
8441 efficient code on machines where a SYMBOL_REF is not a valid
8442 address.
8444 If this is an EXPAND_SUM call, always return the sum. */
8445 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8446 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8448 if (modifier == EXPAND_STACK_PARM)
8449 target = 0;
8450 if (TREE_CODE (treeop0) == INTEGER_CST
8451 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8452 && TREE_CONSTANT (treeop1))
8454 rtx constant_part;
8455 HOST_WIDE_INT wc;
8456 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8458 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8459 EXPAND_SUM);
8460 /* Use wi::shwi to ensure that the constant is
8461 truncated according to the mode of OP1, then sign extended
8462 to a HOST_WIDE_INT. Using the constant directly can result
8463 in non-canonical RTL in a 64x32 cross compile. */
8464 wc = TREE_INT_CST_LOW (treeop0);
8465 constant_part =
8466 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8467 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8468 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8469 op1 = force_operand (op1, target);
8470 return REDUCE_BIT_FIELD (op1);
8473 else if (TREE_CODE (treeop1) == INTEGER_CST
8474 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8475 && TREE_CONSTANT (treeop0))
8477 rtx constant_part;
8478 HOST_WIDE_INT wc;
8479 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8481 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8482 (modifier == EXPAND_INITIALIZER
8483 ? EXPAND_INITIALIZER : EXPAND_SUM));
8484 if (! CONSTANT_P (op0))
8486 op1 = expand_expr (treeop1, NULL_RTX,
8487 VOIDmode, modifier);
8488 /* Return a PLUS if modifier says it's OK. */
8489 if (modifier == EXPAND_SUM
8490 || modifier == EXPAND_INITIALIZER)
8491 return simplify_gen_binary (PLUS, mode, op0, op1);
8492 goto binop2;
8494 /* Use wi::shwi to ensure that the constant is
8495 truncated according to the mode of OP1, then sign extended
8496 to a HOST_WIDE_INT. Using the constant directly can result
8497 in non-canonical RTL in a 64x32 cross compile. */
8498 wc = TREE_INT_CST_LOW (treeop1);
8499 constant_part
8500 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8501 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8502 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8503 op0 = force_operand (op0, target);
8504 return REDUCE_BIT_FIELD (op0);
8508 /* Use TER to expand pointer addition of a negated value
8509 as pointer subtraction. */
8510 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8511 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8512 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8513 && TREE_CODE (treeop1) == SSA_NAME
8514 && TYPE_MODE (TREE_TYPE (treeop0))
8515 == TYPE_MODE (TREE_TYPE (treeop1)))
8517 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8518 if (def)
8520 treeop1 = gimple_assign_rhs1 (def);
8521 code = MINUS_EXPR;
8522 goto do_minus;
8526 /* No sense saving up arithmetic to be done
8527 if it's all in the wrong mode to form part of an address.
8528 And force_operand won't know whether to sign-extend or
8529 zero-extend. */
8530 if (modifier != EXPAND_INITIALIZER
8531 && (modifier != EXPAND_SUM || mode != ptr_mode))
8533 expand_operands (treeop0, treeop1,
8534 subtarget, &op0, &op1, modifier);
8535 if (op0 == const0_rtx)
8536 return op1;
8537 if (op1 == const0_rtx)
8538 return op0;
8539 goto binop2;
8542 expand_operands (treeop0, treeop1,
8543 subtarget, &op0, &op1, modifier);
8544 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8546 case MINUS_EXPR:
8547 do_minus:
8548 /* For initializers, we are allowed to return a MINUS of two
8549 symbolic constants. Here we handle all cases when both operands
8550 are constant. */
8551 /* Handle difference of two symbolic constants,
8552 for the sake of an initializer. */
8553 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8554 && really_constant_p (treeop0)
8555 && really_constant_p (treeop1))
8557 expand_operands (treeop0, treeop1,
8558 NULL_RTX, &op0, &op1, modifier);
8560 /* If the last operand is a CONST_INT, use plus_constant of
8561 the negated constant. Else make the MINUS. */
8562 if (CONST_INT_P (op1))
8563 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8564 -INTVAL (op1)));
8565 else
8566 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8569 /* No sense saving up arithmetic to be done
8570 if it's all in the wrong mode to form part of an address.
8571 And force_operand won't know whether to sign-extend or
8572 zero-extend. */
8573 if (modifier != EXPAND_INITIALIZER
8574 && (modifier != EXPAND_SUM || mode != ptr_mode))
8575 goto binop;
8577 expand_operands (treeop0, treeop1,
8578 subtarget, &op0, &op1, modifier);
8580 /* Convert A - const to A + (-const). */
8581 if (CONST_INT_P (op1))
8583 op1 = negate_rtx (mode, op1);
8584 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8587 goto binop2;
8589 case WIDEN_MULT_PLUS_EXPR:
8590 case WIDEN_MULT_MINUS_EXPR:
8591 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8592 op2 = expand_normal (treeop2);
8593 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8594 target, unsignedp);
8595 return target;
8597 case WIDEN_MULT_EXPR:
8598 /* If first operand is constant, swap them.
8599 Thus the following special case checks need only
8600 check the second operand. */
8601 if (TREE_CODE (treeop0) == INTEGER_CST)
8602 std::swap (treeop0, treeop1);
8604 /* First, check if we have a multiplication of one signed and one
8605 unsigned operand. */
8606 if (TREE_CODE (treeop1) != INTEGER_CST
8607 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8608 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8610 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8611 this_optab = usmul_widen_optab;
8612 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8613 != CODE_FOR_nothing)
8615 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8616 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8617 EXPAND_NORMAL);
8618 else
8619 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8620 EXPAND_NORMAL);
8621 /* op0 and op1 might still be constant, despite the above
8622 != INTEGER_CST check. Handle it. */
8623 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8625 op0 = convert_modes (innermode, mode, op0, true);
8626 op1 = convert_modes (innermode, mode, op1, false);
8627 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8628 target, unsignedp));
8630 goto binop3;
8633 /* Check for a multiplication with matching signedness. */
8634 else if ((TREE_CODE (treeop1) == INTEGER_CST
8635 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8636 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8637 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8639 tree op0type = TREE_TYPE (treeop0);
8640 machine_mode innermode = TYPE_MODE (op0type);
8641 bool zextend_p = TYPE_UNSIGNED (op0type);
8642 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8643 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8645 if (TREE_CODE (treeop0) != INTEGER_CST)
8647 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8648 != CODE_FOR_nothing)
8650 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8651 EXPAND_NORMAL);
8652 /* op0 and op1 might still be constant, despite the above
8653 != INTEGER_CST check. Handle it. */
8654 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8656 widen_mult_const:
8657 op0 = convert_modes (innermode, mode, op0, zextend_p);
8659 = convert_modes (innermode, mode, op1,
8660 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8661 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8662 target,
8663 unsignedp));
8665 temp = expand_widening_mult (mode, op0, op1, target,
8666 unsignedp, this_optab);
8667 return REDUCE_BIT_FIELD (temp);
8669 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8670 != CODE_FOR_nothing
8671 && innermode == word_mode)
8673 rtx htem, hipart;
8674 op0 = expand_normal (treeop0);
8675 if (TREE_CODE (treeop1) == INTEGER_CST)
8676 op1 = convert_modes (innermode, mode,
8677 expand_normal (treeop1),
8678 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8679 else
8680 op1 = expand_normal (treeop1);
8681 /* op0 and op1 might still be constant, despite the above
8682 != INTEGER_CST check. Handle it. */
8683 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8684 goto widen_mult_const;
8685 temp = expand_binop (mode, other_optab, op0, op1, target,
8686 unsignedp, OPTAB_LIB_WIDEN);
8687 hipart = gen_highpart (innermode, temp);
8688 htem = expand_mult_highpart_adjust (innermode, hipart,
8689 op0, op1, hipart,
8690 zextend_p);
8691 if (htem != hipart)
8692 emit_move_insn (hipart, htem);
8693 return REDUCE_BIT_FIELD (temp);
8697 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8698 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8699 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8700 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8702 case FMA_EXPR:
8704 optab opt = fma_optab;
8705 gimple *def0, *def2;
8707 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8708 call. */
8709 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8711 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8712 tree call_expr;
8714 gcc_assert (fn != NULL_TREE);
8715 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8716 return expand_builtin (call_expr, target, subtarget, mode, false);
8719 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8720 /* The multiplication is commutative - look at its 2nd operand
8721 if the first isn't fed by a negate. */
8722 if (!def0)
8724 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8725 /* Swap operands if the 2nd operand is fed by a negate. */
8726 if (def0)
8727 std::swap (treeop0, treeop1);
8729 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8731 op0 = op2 = NULL;
8733 if (def0 && def2
8734 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8736 opt = fnms_optab;
8737 op0 = expand_normal (gimple_assign_rhs1 (def0));
8738 op2 = expand_normal (gimple_assign_rhs1 (def2));
8740 else if (def0
8741 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8743 opt = fnma_optab;
8744 op0 = expand_normal (gimple_assign_rhs1 (def0));
8746 else if (def2
8747 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8749 opt = fms_optab;
8750 op2 = expand_normal (gimple_assign_rhs1 (def2));
8753 if (op0 == NULL)
8754 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8755 if (op2 == NULL)
8756 op2 = expand_normal (treeop2);
8757 op1 = expand_normal (treeop1);
8759 return expand_ternary_op (TYPE_MODE (type), opt,
8760 op0, op1, op2, target, 0);
8763 case MULT_EXPR:
8764 /* If this is a fixed-point operation, then we cannot use the code
8765 below because "expand_mult" doesn't support sat/no-sat fixed-point
8766 multiplications. */
8767 if (ALL_FIXED_POINT_MODE_P (mode))
8768 goto binop;
8770 /* If first operand is constant, swap them.
8771 Thus the following special case checks need only
8772 check the second operand. */
8773 if (TREE_CODE (treeop0) == INTEGER_CST)
8774 std::swap (treeop0, treeop1);
8776 /* Attempt to return something suitable for generating an
8777 indexed address, for machines that support that. */
8779 if (modifier == EXPAND_SUM && mode == ptr_mode
8780 && tree_fits_shwi_p (treeop1))
8782 tree exp1 = treeop1;
8784 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8785 EXPAND_SUM);
8787 if (!REG_P (op0))
8788 op0 = force_operand (op0, NULL_RTX);
8789 if (!REG_P (op0))
8790 op0 = copy_to_mode_reg (mode, op0);
8792 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8793 gen_int_mode (tree_to_shwi (exp1),
8794 TYPE_MODE (TREE_TYPE (exp1)))));
8797 if (modifier == EXPAND_STACK_PARM)
8798 target = 0;
8800 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8801 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8803 case TRUNC_MOD_EXPR:
8804 case FLOOR_MOD_EXPR:
8805 case CEIL_MOD_EXPR:
8806 case ROUND_MOD_EXPR:
8808 case TRUNC_DIV_EXPR:
8809 case FLOOR_DIV_EXPR:
8810 case CEIL_DIV_EXPR:
8811 case ROUND_DIV_EXPR:
8812 case EXACT_DIV_EXPR:
8814 /* If this is a fixed-point operation, then we cannot use the code
8815 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8816 divisions. */
8817 if (ALL_FIXED_POINT_MODE_P (mode))
8818 goto binop;
8820 if (modifier == EXPAND_STACK_PARM)
8821 target = 0;
8822 /* Possible optimization: compute the dividend with EXPAND_SUM
8823 then if the divisor is constant can optimize the case
8824 where some terms of the dividend have coeffs divisible by it. */
8825 expand_operands (treeop0, treeop1,
8826 subtarget, &op0, &op1, EXPAND_NORMAL);
8827 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8828 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8829 if (SCALAR_INT_MODE_P (mode)
8830 && optimize >= 2
8831 && get_range_pos_neg (treeop0) == 1
8832 && get_range_pos_neg (treeop1) == 1)
8834 /* If both arguments are known to be positive when interpreted
8835 as signed, we can expand it as both signed and unsigned
8836 division or modulo. Choose the cheaper sequence in that case. */
8837 bool speed_p = optimize_insn_for_speed_p ();
8838 do_pending_stack_adjust ();
8839 start_sequence ();
8840 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8841 rtx_insn *uns_insns = get_insns ();
8842 end_sequence ();
8843 start_sequence ();
8844 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8845 rtx_insn *sgn_insns = get_insns ();
8846 end_sequence ();
8847 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8848 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8850 /* If costs are the same then use as tie breaker the other
8851 other factor. */
8852 if (uns_cost == sgn_cost)
8854 uns_cost = seq_cost (uns_insns, !speed_p);
8855 sgn_cost = seq_cost (sgn_insns, !speed_p);
8858 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8860 emit_insn (uns_insns);
8861 return uns_ret;
8863 emit_insn (sgn_insns);
8864 return sgn_ret;
8866 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8868 case RDIV_EXPR:
8869 goto binop;
8871 case MULT_HIGHPART_EXPR:
8872 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8873 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8874 gcc_assert (temp);
8875 return temp;
8877 case FIXED_CONVERT_EXPR:
8878 op0 = expand_normal (treeop0);
8879 if (target == 0 || modifier == EXPAND_STACK_PARM)
8880 target = gen_reg_rtx (mode);
8882 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8883 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8884 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8885 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8886 else
8887 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8888 return target;
8890 case FIX_TRUNC_EXPR:
8891 op0 = expand_normal (treeop0);
8892 if (target == 0 || modifier == EXPAND_STACK_PARM)
8893 target = gen_reg_rtx (mode);
8894 expand_fix (target, op0, unsignedp);
8895 return target;
8897 case FLOAT_EXPR:
8898 op0 = expand_normal (treeop0);
8899 if (target == 0 || modifier == EXPAND_STACK_PARM)
8900 target = gen_reg_rtx (mode);
8901 /* expand_float can't figure out what to do if FROM has VOIDmode.
8902 So give it the correct mode. With -O, cse will optimize this. */
8903 if (GET_MODE (op0) == VOIDmode)
8904 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8905 op0);
8906 expand_float (target, op0,
8907 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8908 return target;
8910 case NEGATE_EXPR:
8911 op0 = expand_expr (treeop0, subtarget,
8912 VOIDmode, EXPAND_NORMAL);
8913 if (modifier == EXPAND_STACK_PARM)
8914 target = 0;
8915 temp = expand_unop (mode,
8916 optab_for_tree_code (NEGATE_EXPR, type,
8917 optab_default),
8918 op0, target, 0);
8919 gcc_assert (temp);
8920 return REDUCE_BIT_FIELD (temp);
8922 case ABS_EXPR:
8923 op0 = expand_expr (treeop0, subtarget,
8924 VOIDmode, EXPAND_NORMAL);
8925 if (modifier == EXPAND_STACK_PARM)
8926 target = 0;
8928 /* ABS_EXPR is not valid for complex arguments. */
8929 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8930 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8932 /* Unsigned abs is simply the operand. Testing here means we don't
8933 risk generating incorrect code below. */
8934 if (TYPE_UNSIGNED (type))
8935 return op0;
8937 return expand_abs (mode, op0, target, unsignedp,
8938 safe_from_p (target, treeop0, 1));
8940 case MAX_EXPR:
8941 case MIN_EXPR:
8942 target = original_target;
8943 if (target == 0
8944 || modifier == EXPAND_STACK_PARM
8945 || (MEM_P (target) && MEM_VOLATILE_P (target))
8946 || GET_MODE (target) != mode
8947 || (REG_P (target)
8948 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8949 target = gen_reg_rtx (mode);
8950 expand_operands (treeop0, treeop1,
8951 target, &op0, &op1, EXPAND_NORMAL);
8953 /* First try to do it with a special MIN or MAX instruction.
8954 If that does not win, use a conditional jump to select the proper
8955 value. */
8956 this_optab = optab_for_tree_code (code, type, optab_default);
8957 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8958 OPTAB_WIDEN);
8959 if (temp != 0)
8960 return temp;
8962 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8963 and similarly for MAX <x, y>. */
8964 if (VECTOR_TYPE_P (type))
8966 tree t0 = make_tree (type, op0);
8967 tree t1 = make_tree (type, op1);
8968 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
8969 type, t0, t1);
8970 return expand_vec_cond_expr (type, comparison, t0, t1,
8971 original_target);
8974 /* At this point, a MEM target is no longer useful; we will get better
8975 code without it. */
8977 if (! REG_P (target))
8978 target = gen_reg_rtx (mode);
8980 /* If op1 was placed in target, swap op0 and op1. */
8981 if (target != op0 && target == op1)
8982 std::swap (op0, op1);
8984 /* We generate better code and avoid problems with op1 mentioning
8985 target by forcing op1 into a pseudo if it isn't a constant. */
8986 if (! CONSTANT_P (op1))
8987 op1 = force_reg (mode, op1);
8990 enum rtx_code comparison_code;
8991 rtx cmpop1 = op1;
8993 if (code == MAX_EXPR)
8994 comparison_code = unsignedp ? GEU : GE;
8995 else
8996 comparison_code = unsignedp ? LEU : LE;
8998 /* Canonicalize to comparisons against 0. */
8999 if (op1 == const1_rtx)
9001 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
9002 or (a != 0 ? a : 1) for unsigned.
9003 For MIN we are safe converting (a <= 1 ? a : 1)
9004 into (a <= 0 ? a : 1) */
9005 cmpop1 = const0_rtx;
9006 if (code == MAX_EXPR)
9007 comparison_code = unsignedp ? NE : GT;
9009 if (op1 == constm1_rtx && !unsignedp)
9011 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9012 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9013 cmpop1 = const0_rtx;
9014 if (code == MIN_EXPR)
9015 comparison_code = LT;
9018 /* Use a conditional move if possible. */
9019 if (can_conditionally_move_p (mode))
9021 rtx insn;
9023 start_sequence ();
9025 /* Try to emit the conditional move. */
9026 insn = emit_conditional_move (target, comparison_code,
9027 op0, cmpop1, mode,
9028 op0, op1, mode,
9029 unsignedp);
9031 /* If we could do the conditional move, emit the sequence,
9032 and return. */
9033 if (insn)
9035 rtx_insn *seq = get_insns ();
9036 end_sequence ();
9037 emit_insn (seq);
9038 return target;
9041 /* Otherwise discard the sequence and fall back to code with
9042 branches. */
9043 end_sequence ();
9046 if (target != op0)
9047 emit_move_insn (target, op0);
9049 lab = gen_label_rtx ();
9050 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9051 unsignedp, mode, NULL_RTX, NULL, lab,
9052 profile_probability::uninitialized ());
9054 emit_move_insn (target, op1);
9055 emit_label (lab);
9056 return target;
9058 case BIT_NOT_EXPR:
9059 op0 = expand_expr (treeop0, subtarget,
9060 VOIDmode, EXPAND_NORMAL);
9061 if (modifier == EXPAND_STACK_PARM)
9062 target = 0;
9063 /* In case we have to reduce the result to bitfield precision
9064 for unsigned bitfield expand this as XOR with a proper constant
9065 instead. */
9066 if (reduce_bit_field && TYPE_UNSIGNED (type))
9068 wide_int mask = wi::mask (TYPE_PRECISION (type),
9069 false, GET_MODE_PRECISION (mode));
9071 temp = expand_binop (mode, xor_optab, op0,
9072 immed_wide_int_const (mask, mode),
9073 target, 1, OPTAB_LIB_WIDEN);
9075 else
9076 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9077 gcc_assert (temp);
9078 return temp;
9080 /* ??? Can optimize bitwise operations with one arg constant.
9081 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9082 and (a bitwise1 b) bitwise2 b (etc)
9083 but that is probably not worth while. */
9085 case BIT_AND_EXPR:
9086 case BIT_IOR_EXPR:
9087 case BIT_XOR_EXPR:
9088 goto binop;
9090 case LROTATE_EXPR:
9091 case RROTATE_EXPR:
9092 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9093 || type_has_mode_precision_p (type));
9094 /* fall through */
9096 case LSHIFT_EXPR:
9097 case RSHIFT_EXPR:
9099 /* If this is a fixed-point operation, then we cannot use the code
9100 below because "expand_shift" doesn't support sat/no-sat fixed-point
9101 shifts. */
9102 if (ALL_FIXED_POINT_MODE_P (mode))
9103 goto binop;
9105 if (! safe_from_p (subtarget, treeop1, 1))
9106 subtarget = 0;
9107 if (modifier == EXPAND_STACK_PARM)
9108 target = 0;
9109 op0 = expand_expr (treeop0, subtarget,
9110 VOIDmode, EXPAND_NORMAL);
9112 /* Left shift optimization when shifting across word_size boundary.
9114 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9115 there isn't native instruction to support this wide mode
9116 left shift. Given below scenario:
9118 Type A = (Type) B << C
9120 |< T >|
9121 | dest_high | dest_low |
9123 | word_size |
9125 If the shift amount C caused we shift B to across the word
9126 size boundary, i.e part of B shifted into high half of
9127 destination register, and part of B remains in the low
9128 half, then GCC will use the following left shift expand
9129 logic:
9131 1. Initialize dest_low to B.
9132 2. Initialize every bit of dest_high to the sign bit of B.
9133 3. Logic left shift dest_low by C bit to finalize dest_low.
9134 The value of dest_low before this shift is kept in a temp D.
9135 4. Logic left shift dest_high by C.
9136 5. Logic right shift D by (word_size - C).
9137 6. Or the result of 4 and 5 to finalize dest_high.
9139 While, by checking gimple statements, if operand B is
9140 coming from signed extension, then we can simplify above
9141 expand logic into:
9143 1. dest_high = src_low >> (word_size - C).
9144 2. dest_low = src_low << C.
9146 We can use one arithmetic right shift to finish all the
9147 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9148 needed from 6 into 2.
9150 The case is similar for zero extension, except that we
9151 initialize dest_high to zero rather than copies of the sign
9152 bit from B. Furthermore, we need to use a logical right shift
9153 in this case.
9155 The choice of sign-extension versus zero-extension is
9156 determined entirely by whether or not B is signed and is
9157 independent of the current setting of unsignedp. */
9159 temp = NULL_RTX;
9160 if (code == LSHIFT_EXPR
9161 && target
9162 && REG_P (target)
9163 && mode == GET_MODE_WIDER_MODE (word_mode).else_void ()
9164 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
9165 && TREE_CONSTANT (treeop1)
9166 && TREE_CODE (treeop0) == SSA_NAME)
9168 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9169 if (is_gimple_assign (def)
9170 && gimple_assign_rhs_code (def) == NOP_EXPR)
9172 machine_mode rmode = TYPE_MODE
9173 (TREE_TYPE (gimple_assign_rhs1 (def)));
9175 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
9176 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9177 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9178 >= GET_MODE_BITSIZE (word_mode)))
9180 rtx_insn *seq, *seq_old;
9181 unsigned int high_off = subreg_highpart_offset (word_mode,
9182 mode);
9183 bool extend_unsigned
9184 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9185 rtx low = lowpart_subreg (word_mode, op0, mode);
9186 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9187 rtx dest_high = simplify_gen_subreg (word_mode, target,
9188 mode, high_off);
9189 HOST_WIDE_INT ramount = (BITS_PER_WORD
9190 - TREE_INT_CST_LOW (treeop1));
9191 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9193 start_sequence ();
9194 /* dest_high = src_low >> (word_size - C). */
9195 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9196 rshift, dest_high,
9197 extend_unsigned);
9198 if (temp != dest_high)
9199 emit_move_insn (dest_high, temp);
9201 /* dest_low = src_low << C. */
9202 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9203 treeop1, dest_low, unsignedp);
9204 if (temp != dest_low)
9205 emit_move_insn (dest_low, temp);
9207 seq = get_insns ();
9208 end_sequence ();
9209 temp = target ;
9211 if (have_insn_for (ASHIFT, mode))
9213 bool speed_p = optimize_insn_for_speed_p ();
9214 start_sequence ();
9215 rtx ret_old = expand_variable_shift (code, mode, op0,
9216 treeop1, target,
9217 unsignedp);
9219 seq_old = get_insns ();
9220 end_sequence ();
9221 if (seq_cost (seq, speed_p)
9222 >= seq_cost (seq_old, speed_p))
9224 seq = seq_old;
9225 temp = ret_old;
9228 emit_insn (seq);
9233 if (temp == NULL_RTX)
9234 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9235 unsignedp);
9236 if (code == LSHIFT_EXPR)
9237 temp = REDUCE_BIT_FIELD (temp);
9238 return temp;
9241 /* Could determine the answer when only additive constants differ. Also,
9242 the addition of one can be handled by changing the condition. */
9243 case LT_EXPR:
9244 case LE_EXPR:
9245 case GT_EXPR:
9246 case GE_EXPR:
9247 case EQ_EXPR:
9248 case NE_EXPR:
9249 case UNORDERED_EXPR:
9250 case ORDERED_EXPR:
9251 case UNLT_EXPR:
9252 case UNLE_EXPR:
9253 case UNGT_EXPR:
9254 case UNGE_EXPR:
9255 case UNEQ_EXPR:
9256 case LTGT_EXPR:
9258 temp = do_store_flag (ops,
9259 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9260 tmode != VOIDmode ? tmode : mode);
9261 if (temp)
9262 return temp;
9264 /* Use a compare and a jump for BLKmode comparisons, or for function
9265 type comparisons is have_canonicalize_funcptr_for_compare. */
9267 if ((target == 0
9268 || modifier == EXPAND_STACK_PARM
9269 || ! safe_from_p (target, treeop0, 1)
9270 || ! safe_from_p (target, treeop1, 1)
9271 /* Make sure we don't have a hard reg (such as function's return
9272 value) live across basic blocks, if not optimizing. */
9273 || (!optimize && REG_P (target)
9274 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9275 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9277 emit_move_insn (target, const0_rtx);
9279 rtx_code_label *lab1 = gen_label_rtx ();
9280 jumpifnot_1 (code, treeop0, treeop1, lab1,
9281 profile_probability::uninitialized ());
9283 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9284 emit_move_insn (target, constm1_rtx);
9285 else
9286 emit_move_insn (target, const1_rtx);
9288 emit_label (lab1);
9289 return target;
9291 case COMPLEX_EXPR:
9292 /* Get the rtx code of the operands. */
9293 op0 = expand_normal (treeop0);
9294 op1 = expand_normal (treeop1);
9296 if (!target)
9297 target = gen_reg_rtx (TYPE_MODE (type));
9298 else
9299 /* If target overlaps with op1, then either we need to force
9300 op1 into a pseudo (if target also overlaps with op0),
9301 or write the complex parts in reverse order. */
9302 switch (GET_CODE (target))
9304 case CONCAT:
9305 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9307 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9309 complex_expr_force_op1:
9310 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9311 emit_move_insn (temp, op1);
9312 op1 = temp;
9313 break;
9315 complex_expr_swap_order:
9316 /* Move the imaginary (op1) and real (op0) parts to their
9317 location. */
9318 write_complex_part (target, op1, true);
9319 write_complex_part (target, op0, false);
9321 return target;
9323 break;
9324 case MEM:
9325 temp = adjust_address_nv (target,
9326 GET_MODE_INNER (GET_MODE (target)), 0);
9327 if (reg_overlap_mentioned_p (temp, op1))
9329 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9330 temp = adjust_address_nv (target, imode,
9331 GET_MODE_SIZE (imode));
9332 if (reg_overlap_mentioned_p (temp, op0))
9333 goto complex_expr_force_op1;
9334 goto complex_expr_swap_order;
9336 break;
9337 default:
9338 if (reg_overlap_mentioned_p (target, op1))
9340 if (reg_overlap_mentioned_p (target, op0))
9341 goto complex_expr_force_op1;
9342 goto complex_expr_swap_order;
9344 break;
9347 /* Move the real (op0) and imaginary (op1) parts to their location. */
9348 write_complex_part (target, op0, false);
9349 write_complex_part (target, op1, true);
9351 return target;
9353 case WIDEN_SUM_EXPR:
9355 tree oprnd0 = treeop0;
9356 tree oprnd1 = treeop1;
9358 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9359 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9360 target, unsignedp);
9361 return target;
9364 case REDUC_MAX_EXPR:
9365 case REDUC_MIN_EXPR:
9366 case REDUC_PLUS_EXPR:
9368 op0 = expand_normal (treeop0);
9369 this_optab = optab_for_tree_code (code, type, optab_default);
9370 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9372 struct expand_operand ops[2];
9373 enum insn_code icode = optab_handler (this_optab, vec_mode);
9375 create_output_operand (&ops[0], target, mode);
9376 create_input_operand (&ops[1], op0, vec_mode);
9377 expand_insn (icode, 2, ops);
9378 target = ops[0].value;
9379 if (GET_MODE (target) != mode)
9380 return gen_lowpart (tmode, target);
9381 return target;
9384 case VEC_UNPACK_HI_EXPR:
9385 case VEC_UNPACK_LO_EXPR:
9387 op0 = expand_normal (treeop0);
9388 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9389 target, unsignedp);
9390 gcc_assert (temp);
9391 return temp;
9394 case VEC_UNPACK_FLOAT_HI_EXPR:
9395 case VEC_UNPACK_FLOAT_LO_EXPR:
9397 op0 = expand_normal (treeop0);
9398 /* The signedness is determined from input operand. */
9399 temp = expand_widen_pattern_expr
9400 (ops, op0, NULL_RTX, NULL_RTX,
9401 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9403 gcc_assert (temp);
9404 return temp;
9407 case VEC_WIDEN_MULT_HI_EXPR:
9408 case VEC_WIDEN_MULT_LO_EXPR:
9409 case VEC_WIDEN_MULT_EVEN_EXPR:
9410 case VEC_WIDEN_MULT_ODD_EXPR:
9411 case VEC_WIDEN_LSHIFT_HI_EXPR:
9412 case VEC_WIDEN_LSHIFT_LO_EXPR:
9413 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9414 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9415 target, unsignedp);
9416 gcc_assert (target);
9417 return target;
9419 case VEC_PACK_TRUNC_EXPR:
9420 case VEC_PACK_SAT_EXPR:
9421 case VEC_PACK_FIX_TRUNC_EXPR:
9422 mode = TYPE_MODE (TREE_TYPE (treeop0));
9423 goto binop;
9425 case VEC_PERM_EXPR:
9426 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9427 op2 = expand_normal (treeop2);
9429 /* Careful here: if the target doesn't support integral vector modes,
9430 a constant selection vector could wind up smooshed into a normal
9431 integral constant. */
9432 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9434 tree sel_type = TREE_TYPE (treeop2);
9435 machine_mode vmode
9436 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9437 TYPE_VECTOR_SUBPARTS (sel_type));
9438 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9439 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9440 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9442 else
9443 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9445 temp = expand_vec_perm (mode, op0, op1, op2, target);
9446 gcc_assert (temp);
9447 return temp;
9449 case DOT_PROD_EXPR:
9451 tree oprnd0 = treeop0;
9452 tree oprnd1 = treeop1;
9453 tree oprnd2 = treeop2;
9454 rtx op2;
9456 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9457 op2 = expand_normal (oprnd2);
9458 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9459 target, unsignedp);
9460 return target;
9463 case SAD_EXPR:
9465 tree oprnd0 = treeop0;
9466 tree oprnd1 = treeop1;
9467 tree oprnd2 = treeop2;
9468 rtx op2;
9470 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9471 op2 = expand_normal (oprnd2);
9472 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9473 target, unsignedp);
9474 return target;
9477 case REALIGN_LOAD_EXPR:
9479 tree oprnd0 = treeop0;
9480 tree oprnd1 = treeop1;
9481 tree oprnd2 = treeop2;
9482 rtx op2;
9484 this_optab = optab_for_tree_code (code, type, optab_default);
9485 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9486 op2 = expand_normal (oprnd2);
9487 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9488 target, unsignedp);
9489 gcc_assert (temp);
9490 return temp;
9493 case COND_EXPR:
9495 /* A COND_EXPR with its type being VOID_TYPE represents a
9496 conditional jump and is handled in
9497 expand_gimple_cond_expr. */
9498 gcc_assert (!VOID_TYPE_P (type));
9500 /* Note that COND_EXPRs whose type is a structure or union
9501 are required to be constructed to contain assignments of
9502 a temporary variable, so that we can evaluate them here
9503 for side effect only. If type is void, we must do likewise. */
9505 gcc_assert (!TREE_ADDRESSABLE (type)
9506 && !ignore
9507 && TREE_TYPE (treeop1) != void_type_node
9508 && TREE_TYPE (treeop2) != void_type_node);
9510 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9511 if (temp)
9512 return temp;
9514 /* If we are not to produce a result, we have no target. Otherwise,
9515 if a target was specified use it; it will not be used as an
9516 intermediate target unless it is safe. If no target, use a
9517 temporary. */
9519 if (modifier != EXPAND_STACK_PARM
9520 && original_target
9521 && safe_from_p (original_target, treeop0, 1)
9522 && GET_MODE (original_target) == mode
9523 && !MEM_P (original_target))
9524 temp = original_target;
9525 else
9526 temp = assign_temp (type, 0, 1);
9528 do_pending_stack_adjust ();
9529 NO_DEFER_POP;
9530 rtx_code_label *lab0 = gen_label_rtx ();
9531 rtx_code_label *lab1 = gen_label_rtx ();
9532 jumpifnot (treeop0, lab0,
9533 profile_probability::uninitialized ());
9534 store_expr (treeop1, temp,
9535 modifier == EXPAND_STACK_PARM,
9536 false, false);
9538 emit_jump_insn (targetm.gen_jump (lab1));
9539 emit_barrier ();
9540 emit_label (lab0);
9541 store_expr (treeop2, temp,
9542 modifier == EXPAND_STACK_PARM,
9543 false, false);
9545 emit_label (lab1);
9546 OK_DEFER_POP;
9547 return temp;
9550 case VEC_COND_EXPR:
9551 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9552 return target;
9554 case BIT_INSERT_EXPR:
9556 unsigned bitpos = tree_to_uhwi (treeop2);
9557 unsigned bitsize;
9558 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9559 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9560 else
9561 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9562 rtx op0 = expand_normal (treeop0);
9563 rtx op1 = expand_normal (treeop1);
9564 rtx dst = gen_reg_rtx (mode);
9565 emit_move_insn (dst, op0);
9566 store_bit_field (dst, bitsize, bitpos, 0, 0,
9567 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9568 return dst;
9571 default:
9572 gcc_unreachable ();
9575 /* Here to do an ordinary binary operator. */
9576 binop:
9577 expand_operands (treeop0, treeop1,
9578 subtarget, &op0, &op1, EXPAND_NORMAL);
9579 binop2:
9580 this_optab = optab_for_tree_code (code, type, optab_default);
9581 binop3:
9582 if (modifier == EXPAND_STACK_PARM)
9583 target = 0;
9584 temp = expand_binop (mode, this_optab, op0, op1, target,
9585 unsignedp, OPTAB_LIB_WIDEN);
9586 gcc_assert (temp);
9587 /* Bitwise operations do not need bitfield reduction as we expect their
9588 operands being properly truncated. */
9589 if (code == BIT_XOR_EXPR
9590 || code == BIT_AND_EXPR
9591 || code == BIT_IOR_EXPR)
9592 return temp;
9593 return REDUCE_BIT_FIELD (temp);
9595 #undef REDUCE_BIT_FIELD
9598 /* Return TRUE if expression STMT is suitable for replacement.
9599 Never consider memory loads as replaceable, because those don't ever lead
9600 into constant expressions. */
9602 static bool
9603 stmt_is_replaceable_p (gimple *stmt)
9605 if (ssa_is_replaceable_p (stmt))
9607 /* Don't move around loads. */
9608 if (!gimple_assign_single_p (stmt)
9609 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9610 return true;
9612 return false;
9616 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9617 enum expand_modifier modifier, rtx *alt_rtl,
9618 bool inner_reference_p)
9620 rtx op0, op1, temp, decl_rtl;
9621 tree type;
9622 int unsignedp;
9623 machine_mode mode, dmode;
9624 enum tree_code code = TREE_CODE (exp);
9625 rtx subtarget, original_target;
9626 int ignore;
9627 tree context;
9628 bool reduce_bit_field;
9629 location_t loc = EXPR_LOCATION (exp);
9630 struct separate_ops ops;
9631 tree treeop0, treeop1, treeop2;
9632 tree ssa_name = NULL_TREE;
9633 gimple *g;
9635 type = TREE_TYPE (exp);
9636 mode = TYPE_MODE (type);
9637 unsignedp = TYPE_UNSIGNED (type);
9639 treeop0 = treeop1 = treeop2 = NULL_TREE;
9640 if (!VL_EXP_CLASS_P (exp))
9641 switch (TREE_CODE_LENGTH (code))
9643 default:
9644 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9645 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9646 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9647 case 0: break;
9649 ops.code = code;
9650 ops.type = type;
9651 ops.op0 = treeop0;
9652 ops.op1 = treeop1;
9653 ops.op2 = treeop2;
9654 ops.location = loc;
9656 ignore = (target == const0_rtx
9657 || ((CONVERT_EXPR_CODE_P (code)
9658 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9659 && TREE_CODE (type) == VOID_TYPE));
9661 /* An operation in what may be a bit-field type needs the
9662 result to be reduced to the precision of the bit-field type,
9663 which is narrower than that of the type's mode. */
9664 reduce_bit_field = (!ignore
9665 && INTEGRAL_TYPE_P (type)
9666 && !type_has_mode_precision_p (type));
9668 /* If we are going to ignore this result, we need only do something
9669 if there is a side-effect somewhere in the expression. If there
9670 is, short-circuit the most common cases here. Note that we must
9671 not call expand_expr with anything but const0_rtx in case this
9672 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9674 if (ignore)
9676 if (! TREE_SIDE_EFFECTS (exp))
9677 return const0_rtx;
9679 /* Ensure we reference a volatile object even if value is ignored, but
9680 don't do this if all we are doing is taking its address. */
9681 if (TREE_THIS_VOLATILE (exp)
9682 && TREE_CODE (exp) != FUNCTION_DECL
9683 && mode != VOIDmode && mode != BLKmode
9684 && modifier != EXPAND_CONST_ADDRESS)
9686 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9687 if (MEM_P (temp))
9688 copy_to_reg (temp);
9689 return const0_rtx;
9692 if (TREE_CODE_CLASS (code) == tcc_unary
9693 || code == BIT_FIELD_REF
9694 || code == COMPONENT_REF
9695 || code == INDIRECT_REF)
9696 return expand_expr (treeop0, const0_rtx, VOIDmode,
9697 modifier);
9699 else if (TREE_CODE_CLASS (code) == tcc_binary
9700 || TREE_CODE_CLASS (code) == tcc_comparison
9701 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9703 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9704 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9705 return const0_rtx;
9708 target = 0;
9711 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9712 target = 0;
9714 /* Use subtarget as the target for operand 0 of a binary operation. */
9715 subtarget = get_subtarget (target);
9716 original_target = target;
9718 switch (code)
9720 case LABEL_DECL:
9722 tree function = decl_function_context (exp);
9724 temp = label_rtx (exp);
9725 temp = gen_rtx_LABEL_REF (Pmode, temp);
9727 if (function != current_function_decl
9728 && function != 0)
9729 LABEL_REF_NONLOCAL_P (temp) = 1;
9731 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9732 return temp;
9735 case SSA_NAME:
9736 /* ??? ivopts calls expander, without any preparation from
9737 out-of-ssa. So fake instructions as if this was an access to the
9738 base variable. This unnecessarily allocates a pseudo, see how we can
9739 reuse it, if partition base vars have it set already. */
9740 if (!currently_expanding_to_rtl)
9742 tree var = SSA_NAME_VAR (exp);
9743 if (var && DECL_RTL_SET_P (var))
9744 return DECL_RTL (var);
9745 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9746 LAST_VIRTUAL_REGISTER + 1);
9749 g = get_gimple_for_ssa_name (exp);
9750 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9751 if (g == NULL
9752 && modifier == EXPAND_INITIALIZER
9753 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9754 && (optimize || !SSA_NAME_VAR (exp)
9755 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9756 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9757 g = SSA_NAME_DEF_STMT (exp);
9758 if (g)
9760 rtx r;
9761 location_t saved_loc = curr_insn_location ();
9762 location_t loc = gimple_location (g);
9763 if (loc != UNKNOWN_LOCATION)
9764 set_curr_insn_location (loc);
9765 ops.code = gimple_assign_rhs_code (g);
9766 switch (get_gimple_rhs_class (ops.code))
9768 case GIMPLE_TERNARY_RHS:
9769 ops.op2 = gimple_assign_rhs3 (g);
9770 /* Fallthru */
9771 case GIMPLE_BINARY_RHS:
9772 ops.op1 = gimple_assign_rhs2 (g);
9774 /* Try to expand conditonal compare. */
9775 if (targetm.gen_ccmp_first)
9777 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9778 r = expand_ccmp_expr (g, mode);
9779 if (r)
9780 break;
9782 /* Fallthru */
9783 case GIMPLE_UNARY_RHS:
9784 ops.op0 = gimple_assign_rhs1 (g);
9785 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9786 ops.location = loc;
9787 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9788 break;
9789 case GIMPLE_SINGLE_RHS:
9791 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9792 tmode, modifier, alt_rtl,
9793 inner_reference_p);
9794 break;
9796 default:
9797 gcc_unreachable ();
9799 set_curr_insn_location (saved_loc);
9800 if (REG_P (r) && !REG_EXPR (r))
9801 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9802 return r;
9805 ssa_name = exp;
9806 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9807 exp = SSA_NAME_VAR (ssa_name);
9808 goto expand_decl_rtl;
9810 case PARM_DECL:
9811 case VAR_DECL:
9812 /* If a static var's type was incomplete when the decl was written,
9813 but the type is complete now, lay out the decl now. */
9814 if (DECL_SIZE (exp) == 0
9815 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9816 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9817 layout_decl (exp, 0);
9819 /* fall through */
9821 case FUNCTION_DECL:
9822 case RESULT_DECL:
9823 decl_rtl = DECL_RTL (exp);
9824 expand_decl_rtl:
9825 gcc_assert (decl_rtl);
9827 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9828 settings for VECTOR_TYPE_P that might switch for the function. */
9829 if (currently_expanding_to_rtl
9830 && code == VAR_DECL && MEM_P (decl_rtl)
9831 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9832 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9833 else
9834 decl_rtl = copy_rtx (decl_rtl);
9836 /* Record writes to register variables. */
9837 if (modifier == EXPAND_WRITE
9838 && REG_P (decl_rtl)
9839 && HARD_REGISTER_P (decl_rtl))
9840 add_to_hard_reg_set (&crtl->asm_clobbers,
9841 GET_MODE (decl_rtl), REGNO (decl_rtl));
9843 /* Ensure variable marked as used even if it doesn't go through
9844 a parser. If it hasn't be used yet, write out an external
9845 definition. */
9846 if (exp)
9847 TREE_USED (exp) = 1;
9849 /* Show we haven't gotten RTL for this yet. */
9850 temp = 0;
9852 /* Variables inherited from containing functions should have
9853 been lowered by this point. */
9854 if (exp)
9855 context = decl_function_context (exp);
9856 gcc_assert (!exp
9857 || SCOPE_FILE_SCOPE_P (context)
9858 || context == current_function_decl
9859 || TREE_STATIC (exp)
9860 || DECL_EXTERNAL (exp)
9861 /* ??? C++ creates functions that are not TREE_STATIC. */
9862 || TREE_CODE (exp) == FUNCTION_DECL);
9864 /* This is the case of an array whose size is to be determined
9865 from its initializer, while the initializer is still being parsed.
9866 ??? We aren't parsing while expanding anymore. */
9868 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9869 temp = validize_mem (decl_rtl);
9871 /* If DECL_RTL is memory, we are in the normal case and the
9872 address is not valid, get the address into a register. */
9874 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9876 if (alt_rtl)
9877 *alt_rtl = decl_rtl;
9878 decl_rtl = use_anchored_address (decl_rtl);
9879 if (modifier != EXPAND_CONST_ADDRESS
9880 && modifier != EXPAND_SUM
9881 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9882 : GET_MODE (decl_rtl),
9883 XEXP (decl_rtl, 0),
9884 MEM_ADDR_SPACE (decl_rtl)))
9885 temp = replace_equiv_address (decl_rtl,
9886 copy_rtx (XEXP (decl_rtl, 0)));
9889 /* If we got something, return it. But first, set the alignment
9890 if the address is a register. */
9891 if (temp != 0)
9893 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9894 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9896 return temp;
9899 if (exp)
9900 dmode = DECL_MODE (exp);
9901 else
9902 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9904 /* If the mode of DECL_RTL does not match that of the decl,
9905 there are two cases: we are dealing with a BLKmode value
9906 that is returned in a register, or we are dealing with
9907 a promoted value. In the latter case, return a SUBREG
9908 of the wanted mode, but mark it so that we know that it
9909 was already extended. */
9910 if (REG_P (decl_rtl)
9911 && dmode != BLKmode
9912 && GET_MODE (decl_rtl) != dmode)
9914 machine_mode pmode;
9916 /* Get the signedness to be used for this variable. Ensure we get
9917 the same mode we got when the variable was declared. */
9918 if (code != SSA_NAME)
9919 pmode = promote_decl_mode (exp, &unsignedp);
9920 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9921 && gimple_code (g) == GIMPLE_CALL
9922 && !gimple_call_internal_p (g))
9923 pmode = promote_function_mode (type, mode, &unsignedp,
9924 gimple_call_fntype (g),
9926 else
9927 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9928 gcc_assert (GET_MODE (decl_rtl) == pmode);
9930 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9931 SUBREG_PROMOTED_VAR_P (temp) = 1;
9932 SUBREG_PROMOTED_SET (temp, unsignedp);
9933 return temp;
9936 return decl_rtl;
9938 case INTEGER_CST:
9939 /* Given that TYPE_PRECISION (type) is not always equal to
9940 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9941 the former to the latter according to the signedness of the
9942 type. */
9943 temp = immed_wide_int_const (wi::to_wide
9944 (exp,
9945 GET_MODE_PRECISION (TYPE_MODE (type))),
9946 TYPE_MODE (type));
9947 return temp;
9949 case VECTOR_CST:
9951 tree tmp = NULL_TREE;
9952 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9953 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9954 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9955 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9956 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9957 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9958 return const_vector_from_tree (exp);
9959 if (GET_MODE_CLASS (mode) == MODE_INT)
9961 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9962 return const_scalar_mask_from_tree (exp);
9963 else
9965 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9966 if (type_for_mode)
9967 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9968 type_for_mode, exp);
9971 if (!tmp)
9973 vec<constructor_elt, va_gc> *v;
9974 unsigned i;
9975 vec_alloc (v, VECTOR_CST_NELTS (exp));
9976 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9977 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9978 tmp = build_constructor (type, v);
9980 return expand_expr (tmp, ignore ? const0_rtx : target,
9981 tmode, modifier);
9984 case CONST_DECL:
9985 if (modifier == EXPAND_WRITE)
9987 /* Writing into CONST_DECL is always invalid, but handle it
9988 gracefully. */
9989 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9990 machine_mode address_mode = targetm.addr_space.address_mode (as);
9991 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9992 EXPAND_NORMAL, as);
9993 op0 = memory_address_addr_space (mode, op0, as);
9994 temp = gen_rtx_MEM (mode, op0);
9995 set_mem_addr_space (temp, as);
9996 return temp;
9998 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
10000 case REAL_CST:
10001 /* If optimized, generate immediate CONST_DOUBLE
10002 which will be turned into memory by reload if necessary.
10004 We used to force a register so that loop.c could see it. But
10005 this does not allow gen_* patterns to perform optimizations with
10006 the constants. It also produces two insns in cases like "x = 1.0;".
10007 On most machines, floating-point constants are not permitted in
10008 many insns, so we'd end up copying it to a register in any case.
10010 Now, we do the copying in expand_binop, if appropriate. */
10011 return const_double_from_real_value (TREE_REAL_CST (exp),
10012 TYPE_MODE (TREE_TYPE (exp)));
10014 case FIXED_CST:
10015 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10016 TYPE_MODE (TREE_TYPE (exp)));
10018 case COMPLEX_CST:
10019 /* Handle evaluating a complex constant in a CONCAT target. */
10020 if (original_target && GET_CODE (original_target) == CONCAT)
10022 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10023 rtx rtarg, itarg;
10025 rtarg = XEXP (original_target, 0);
10026 itarg = XEXP (original_target, 1);
10028 /* Move the real and imaginary parts separately. */
10029 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10030 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10032 if (op0 != rtarg)
10033 emit_move_insn (rtarg, op0);
10034 if (op1 != itarg)
10035 emit_move_insn (itarg, op1);
10037 return original_target;
10040 /* fall through */
10042 case STRING_CST:
10043 temp = expand_expr_constant (exp, 1, modifier);
10045 /* temp contains a constant address.
10046 On RISC machines where a constant address isn't valid,
10047 make some insns to get that address into a register. */
10048 if (modifier != EXPAND_CONST_ADDRESS
10049 && modifier != EXPAND_INITIALIZER
10050 && modifier != EXPAND_SUM
10051 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10052 MEM_ADDR_SPACE (temp)))
10053 return replace_equiv_address (temp,
10054 copy_rtx (XEXP (temp, 0)));
10055 return temp;
10057 case SAVE_EXPR:
10059 tree val = treeop0;
10060 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10061 inner_reference_p);
10063 if (!SAVE_EXPR_RESOLVED_P (exp))
10065 /* We can indeed still hit this case, typically via builtin
10066 expanders calling save_expr immediately before expanding
10067 something. Assume this means that we only have to deal
10068 with non-BLKmode values. */
10069 gcc_assert (GET_MODE (ret) != BLKmode);
10071 val = build_decl (curr_insn_location (),
10072 VAR_DECL, NULL, TREE_TYPE (exp));
10073 DECL_ARTIFICIAL (val) = 1;
10074 DECL_IGNORED_P (val) = 1;
10075 treeop0 = val;
10076 TREE_OPERAND (exp, 0) = treeop0;
10077 SAVE_EXPR_RESOLVED_P (exp) = 1;
10079 if (!CONSTANT_P (ret))
10080 ret = copy_to_reg (ret);
10081 SET_DECL_RTL (val, ret);
10084 return ret;
10088 case CONSTRUCTOR:
10089 /* If we don't need the result, just ensure we evaluate any
10090 subexpressions. */
10091 if (ignore)
10093 unsigned HOST_WIDE_INT idx;
10094 tree value;
10096 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10097 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10099 return const0_rtx;
10102 return expand_constructor (exp, target, modifier, false);
10104 case TARGET_MEM_REF:
10106 addr_space_t as
10107 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10108 enum insn_code icode;
10109 unsigned int align;
10111 op0 = addr_for_mem_ref (exp, as, true);
10112 op0 = memory_address_addr_space (mode, op0, as);
10113 temp = gen_rtx_MEM (mode, op0);
10114 set_mem_attributes (temp, exp, 0);
10115 set_mem_addr_space (temp, as);
10116 align = get_object_alignment (exp);
10117 if (modifier != EXPAND_WRITE
10118 && modifier != EXPAND_MEMORY
10119 && mode != BLKmode
10120 && align < GET_MODE_ALIGNMENT (mode)
10121 /* If the target does not have special handling for unaligned
10122 loads of mode then it can use regular moves for them. */
10123 && ((icode = optab_handler (movmisalign_optab, mode))
10124 != CODE_FOR_nothing))
10126 struct expand_operand ops[2];
10128 /* We've already validated the memory, and we're creating a
10129 new pseudo destination. The predicates really can't fail,
10130 nor can the generator. */
10131 create_output_operand (&ops[0], NULL_RTX, mode);
10132 create_fixed_operand (&ops[1], temp);
10133 expand_insn (icode, 2, ops);
10134 temp = ops[0].value;
10136 return temp;
10139 case MEM_REF:
10141 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10142 addr_space_t as
10143 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10144 machine_mode address_mode;
10145 tree base = TREE_OPERAND (exp, 0);
10146 gimple *def_stmt;
10147 enum insn_code icode;
10148 unsigned align;
10149 /* Handle expansion of non-aliased memory with non-BLKmode. That
10150 might end up in a register. */
10151 if (mem_ref_refers_to_non_mem_p (exp))
10153 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10154 base = TREE_OPERAND (base, 0);
10155 if (offset == 0
10156 && !reverse
10157 && tree_fits_uhwi_p (TYPE_SIZE (type))
10158 && (GET_MODE_BITSIZE (DECL_MODE (base))
10159 == tree_to_uhwi (TYPE_SIZE (type))))
10160 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10161 target, tmode, modifier);
10162 if (TYPE_MODE (type) == BLKmode)
10164 temp = assign_stack_temp (DECL_MODE (base),
10165 GET_MODE_SIZE (DECL_MODE (base)));
10166 store_expr (base, temp, 0, false, false);
10167 temp = adjust_address (temp, BLKmode, offset);
10168 set_mem_size (temp, int_size_in_bytes (type));
10169 return temp;
10171 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10172 bitsize_int (offset * BITS_PER_UNIT));
10173 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10174 return expand_expr (exp, target, tmode, modifier);
10176 address_mode = targetm.addr_space.address_mode (as);
10177 base = TREE_OPERAND (exp, 0);
10178 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10180 tree mask = gimple_assign_rhs2 (def_stmt);
10181 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10182 gimple_assign_rhs1 (def_stmt), mask);
10183 TREE_OPERAND (exp, 0) = base;
10185 align = get_object_alignment (exp);
10186 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10187 op0 = memory_address_addr_space (mode, op0, as);
10188 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10190 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10191 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10192 op0 = memory_address_addr_space (mode, op0, as);
10194 temp = gen_rtx_MEM (mode, op0);
10195 set_mem_attributes (temp, exp, 0);
10196 set_mem_addr_space (temp, as);
10197 if (TREE_THIS_VOLATILE (exp))
10198 MEM_VOLATILE_P (temp) = 1;
10199 if (modifier != EXPAND_WRITE
10200 && modifier != EXPAND_MEMORY
10201 && !inner_reference_p
10202 && mode != BLKmode
10203 && align < GET_MODE_ALIGNMENT (mode))
10205 if ((icode = optab_handler (movmisalign_optab, mode))
10206 != CODE_FOR_nothing)
10208 struct expand_operand ops[2];
10210 /* We've already validated the memory, and we're creating a
10211 new pseudo destination. The predicates really can't fail,
10212 nor can the generator. */
10213 create_output_operand (&ops[0], NULL_RTX, mode);
10214 create_fixed_operand (&ops[1], temp);
10215 expand_insn (icode, 2, ops);
10216 temp = ops[0].value;
10218 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10219 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10220 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10221 (modifier == EXPAND_STACK_PARM
10222 ? NULL_RTX : target),
10223 mode, mode, false, alt_rtl);
10225 if (reverse
10226 && modifier != EXPAND_MEMORY
10227 && modifier != EXPAND_WRITE)
10228 temp = flip_storage_order (mode, temp);
10229 return temp;
10232 case ARRAY_REF:
10235 tree array = treeop0;
10236 tree index = treeop1;
10237 tree init;
10239 /* Fold an expression like: "foo"[2].
10240 This is not done in fold so it won't happen inside &.
10241 Don't fold if this is for wide characters since it's too
10242 difficult to do correctly and this is a very rare case. */
10244 if (modifier != EXPAND_CONST_ADDRESS
10245 && modifier != EXPAND_INITIALIZER
10246 && modifier != EXPAND_MEMORY)
10248 tree t = fold_read_from_constant_string (exp);
10250 if (t)
10251 return expand_expr (t, target, tmode, modifier);
10254 /* If this is a constant index into a constant array,
10255 just get the value from the array. Handle both the cases when
10256 we have an explicit constructor and when our operand is a variable
10257 that was declared const. */
10259 if (modifier != EXPAND_CONST_ADDRESS
10260 && modifier != EXPAND_INITIALIZER
10261 && modifier != EXPAND_MEMORY
10262 && TREE_CODE (array) == CONSTRUCTOR
10263 && ! TREE_SIDE_EFFECTS (array)
10264 && TREE_CODE (index) == INTEGER_CST)
10266 unsigned HOST_WIDE_INT ix;
10267 tree field, value;
10269 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10270 field, value)
10271 if (tree_int_cst_equal (field, index))
10273 if (!TREE_SIDE_EFFECTS (value))
10274 return expand_expr (fold (value), target, tmode, modifier);
10275 break;
10279 else if (optimize >= 1
10280 && modifier != EXPAND_CONST_ADDRESS
10281 && modifier != EXPAND_INITIALIZER
10282 && modifier != EXPAND_MEMORY
10283 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10284 && TREE_CODE (index) == INTEGER_CST
10285 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10286 && (init = ctor_for_folding (array)) != error_mark_node)
10288 if (init == NULL_TREE)
10290 tree value = build_zero_cst (type);
10291 if (TREE_CODE (value) == CONSTRUCTOR)
10293 /* If VALUE is a CONSTRUCTOR, this optimization is only
10294 useful if this doesn't store the CONSTRUCTOR into
10295 memory. If it does, it is more efficient to just
10296 load the data from the array directly. */
10297 rtx ret = expand_constructor (value, target,
10298 modifier, true);
10299 if (ret == NULL_RTX)
10300 value = NULL_TREE;
10303 if (value)
10304 return expand_expr (value, target, tmode, modifier);
10306 else if (TREE_CODE (init) == CONSTRUCTOR)
10308 unsigned HOST_WIDE_INT ix;
10309 tree field, value;
10311 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10312 field, value)
10313 if (tree_int_cst_equal (field, index))
10315 if (TREE_SIDE_EFFECTS (value))
10316 break;
10318 if (TREE_CODE (value) == CONSTRUCTOR)
10320 /* If VALUE is a CONSTRUCTOR, this
10321 optimization is only useful if
10322 this doesn't store the CONSTRUCTOR
10323 into memory. If it does, it is more
10324 efficient to just load the data from
10325 the array directly. */
10326 rtx ret = expand_constructor (value, target,
10327 modifier, true);
10328 if (ret == NULL_RTX)
10329 break;
10332 return
10333 expand_expr (fold (value), target, tmode, modifier);
10336 else if (TREE_CODE (init) == STRING_CST)
10338 tree low_bound = array_ref_low_bound (exp);
10339 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10341 /* Optimize the special case of a zero lower bound.
10343 We convert the lower bound to sizetype to avoid problems
10344 with constant folding. E.g. suppose the lower bound is
10345 1 and its mode is QI. Without the conversion
10346 (ARRAY + (INDEX - (unsigned char)1))
10347 becomes
10348 (ARRAY + (-(unsigned char)1) + INDEX)
10349 which becomes
10350 (ARRAY + 255 + INDEX). Oops! */
10351 if (!integer_zerop (low_bound))
10352 index1 = size_diffop_loc (loc, index1,
10353 fold_convert_loc (loc, sizetype,
10354 low_bound));
10356 if (tree_fits_uhwi_p (index1)
10357 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10359 tree type = TREE_TYPE (TREE_TYPE (init));
10360 machine_mode mode = TYPE_MODE (type);
10362 if (GET_MODE_CLASS (mode) == MODE_INT
10363 && GET_MODE_SIZE (mode) == 1)
10364 return gen_int_mode (TREE_STRING_POINTER (init)
10365 [TREE_INT_CST_LOW (index1)],
10366 mode);
10371 goto normal_inner_ref;
10373 case COMPONENT_REF:
10374 /* If the operand is a CONSTRUCTOR, we can just extract the
10375 appropriate field if it is present. */
10376 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10378 unsigned HOST_WIDE_INT idx;
10379 tree field, value;
10381 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10382 idx, field, value)
10383 if (field == treeop1
10384 /* We can normally use the value of the field in the
10385 CONSTRUCTOR. However, if this is a bitfield in
10386 an integral mode that we can fit in a HOST_WIDE_INT,
10387 we must mask only the number of bits in the bitfield,
10388 since this is done implicitly by the constructor. If
10389 the bitfield does not meet either of those conditions,
10390 we can't do this optimization. */
10391 && (! DECL_BIT_FIELD (field)
10392 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10393 && (GET_MODE_PRECISION (DECL_MODE (field))
10394 <= HOST_BITS_PER_WIDE_INT))))
10396 if (DECL_BIT_FIELD (field)
10397 && modifier == EXPAND_STACK_PARM)
10398 target = 0;
10399 op0 = expand_expr (value, target, tmode, modifier);
10400 if (DECL_BIT_FIELD (field))
10402 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10403 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10405 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10407 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10408 imode);
10409 op0 = expand_and (imode, op0, op1, target);
10411 else
10413 int count = GET_MODE_PRECISION (imode) - bitsize;
10415 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10416 target, 0);
10417 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10418 target, 0);
10422 return op0;
10425 goto normal_inner_ref;
10427 case BIT_FIELD_REF:
10428 case ARRAY_RANGE_REF:
10429 normal_inner_ref:
10431 machine_mode mode1, mode2;
10432 HOST_WIDE_INT bitsize, bitpos;
10433 tree offset;
10434 int reversep, volatilep = 0, must_force_mem;
10435 tree tem
10436 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10437 &unsignedp, &reversep, &volatilep);
10438 rtx orig_op0, memloc;
10439 bool clear_mem_expr = false;
10441 /* If we got back the original object, something is wrong. Perhaps
10442 we are evaluating an expression too early. In any event, don't
10443 infinitely recurse. */
10444 gcc_assert (tem != exp);
10446 /* If TEM's type is a union of variable size, pass TARGET to the inner
10447 computation, since it will need a temporary and TARGET is known
10448 to have to do. This occurs in unchecked conversion in Ada. */
10449 orig_op0 = op0
10450 = expand_expr_real (tem,
10451 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10452 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10453 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10454 != INTEGER_CST)
10455 && modifier != EXPAND_STACK_PARM
10456 ? target : NULL_RTX),
10457 VOIDmode,
10458 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10459 NULL, true);
10461 /* If the field has a mode, we want to access it in the
10462 field's mode, not the computed mode.
10463 If a MEM has VOIDmode (external with incomplete type),
10464 use BLKmode for it instead. */
10465 if (MEM_P (op0))
10467 if (mode1 != VOIDmode)
10468 op0 = adjust_address (op0, mode1, 0);
10469 else if (GET_MODE (op0) == VOIDmode)
10470 op0 = adjust_address (op0, BLKmode, 0);
10473 mode2
10474 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10476 /* If we have either an offset, a BLKmode result, or a reference
10477 outside the underlying object, we must force it to memory.
10478 Such a case can occur in Ada if we have unchecked conversion
10479 of an expression from a scalar type to an aggregate type or
10480 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10481 passed a partially uninitialized object or a view-conversion
10482 to a larger size. */
10483 must_force_mem = (offset
10484 || mode1 == BLKmode
10485 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10487 /* Handle CONCAT first. */
10488 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10490 if (bitpos == 0
10491 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10492 && COMPLEX_MODE_P (mode1)
10493 && COMPLEX_MODE_P (GET_MODE (op0))
10494 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10495 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10497 if (reversep)
10498 op0 = flip_storage_order (GET_MODE (op0), op0);
10499 if (mode1 != GET_MODE (op0))
10501 rtx parts[2];
10502 for (int i = 0; i < 2; i++)
10504 rtx op = read_complex_part (op0, i != 0);
10505 if (GET_CODE (op) == SUBREG)
10506 op = force_reg (GET_MODE (op), op);
10507 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10508 op);
10509 if (temp)
10510 op = temp;
10511 else
10513 if (!REG_P (op) && !MEM_P (op))
10514 op = force_reg (GET_MODE (op), op);
10515 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10517 parts[i] = op;
10519 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10521 return op0;
10523 if (bitpos == 0
10524 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10525 && bitsize)
10527 op0 = XEXP (op0, 0);
10528 mode2 = GET_MODE (op0);
10530 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10531 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10532 && bitpos
10533 && bitsize)
10535 op0 = XEXP (op0, 1);
10536 bitpos = 0;
10537 mode2 = GET_MODE (op0);
10539 else
10540 /* Otherwise force into memory. */
10541 must_force_mem = 1;
10544 /* If this is a constant, put it in a register if it is a legitimate
10545 constant and we don't need a memory reference. */
10546 if (CONSTANT_P (op0)
10547 && mode2 != BLKmode
10548 && targetm.legitimate_constant_p (mode2, op0)
10549 && !must_force_mem)
10550 op0 = force_reg (mode2, op0);
10552 /* Otherwise, if this is a constant, try to force it to the constant
10553 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10554 is a legitimate constant. */
10555 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10556 op0 = validize_mem (memloc);
10558 /* Otherwise, if this is a constant or the object is not in memory
10559 and need be, put it there. */
10560 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10562 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10563 emit_move_insn (memloc, op0);
10564 op0 = memloc;
10565 clear_mem_expr = true;
10568 if (offset)
10570 machine_mode address_mode;
10571 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10572 EXPAND_SUM);
10574 gcc_assert (MEM_P (op0));
10576 address_mode = get_address_mode (op0);
10577 if (GET_MODE (offset_rtx) != address_mode)
10579 /* We cannot be sure that the RTL in offset_rtx is valid outside
10580 of a memory address context, so force it into a register
10581 before attempting to convert it to the desired mode. */
10582 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10583 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10586 /* See the comment in expand_assignment for the rationale. */
10587 if (mode1 != VOIDmode
10588 && bitpos != 0
10589 && bitsize > 0
10590 && (bitpos % bitsize) == 0
10591 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10592 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10594 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10595 bitpos = 0;
10598 op0 = offset_address (op0, offset_rtx,
10599 highest_pow2_factor (offset));
10602 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10603 record its alignment as BIGGEST_ALIGNMENT. */
10604 if (MEM_P (op0) && bitpos == 0 && offset != 0
10605 && is_aligning_offset (offset, tem))
10606 set_mem_align (op0, BIGGEST_ALIGNMENT);
10608 /* Don't forget about volatility even if this is a bitfield. */
10609 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10611 if (op0 == orig_op0)
10612 op0 = copy_rtx (op0);
10614 MEM_VOLATILE_P (op0) = 1;
10617 /* In cases where an aligned union has an unaligned object
10618 as a field, we might be extracting a BLKmode value from
10619 an integer-mode (e.g., SImode) object. Handle this case
10620 by doing the extract into an object as wide as the field
10621 (which we know to be the width of a basic mode), then
10622 storing into memory, and changing the mode to BLKmode. */
10623 if (mode1 == VOIDmode
10624 || REG_P (op0) || GET_CODE (op0) == SUBREG
10625 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10626 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10627 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10628 && modifier != EXPAND_CONST_ADDRESS
10629 && modifier != EXPAND_INITIALIZER
10630 && modifier != EXPAND_MEMORY)
10631 /* If the bitfield is volatile and the bitsize
10632 is narrower than the access size of the bitfield,
10633 we need to extract bitfields from the access. */
10634 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10635 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10636 && mode1 != BLKmode
10637 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10638 /* If the field isn't aligned enough to fetch as a memref,
10639 fetch it as a bit field. */
10640 || (mode1 != BLKmode
10641 && (((MEM_P (op0)
10642 ? MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10643 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0)
10644 : TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10645 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0))
10646 && modifier != EXPAND_MEMORY
10647 && ((modifier == EXPAND_CONST_ADDRESS
10648 || modifier == EXPAND_INITIALIZER)
10649 ? STRICT_ALIGNMENT
10650 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10651 || (bitpos % BITS_PER_UNIT != 0)))
10652 /* If the type and the field are a constant size and the
10653 size of the type isn't the same size as the bitfield,
10654 we must use bitfield operations. */
10655 || (bitsize >= 0
10656 && TYPE_SIZE (TREE_TYPE (exp))
10657 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10658 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10659 bitsize)))
10661 machine_mode ext_mode = mode;
10663 if (ext_mode == BLKmode
10664 && ! (target != 0 && MEM_P (op0)
10665 && MEM_P (target)
10666 && bitpos % BITS_PER_UNIT == 0))
10667 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10669 if (ext_mode == BLKmode)
10671 if (target == 0)
10672 target = assign_temp (type, 1, 1);
10674 /* ??? Unlike the similar test a few lines below, this one is
10675 very likely obsolete. */
10676 if (bitsize == 0)
10677 return target;
10679 /* In this case, BITPOS must start at a byte boundary and
10680 TARGET, if specified, must be a MEM. */
10681 gcc_assert (MEM_P (op0)
10682 && (!target || MEM_P (target))
10683 && !(bitpos % BITS_PER_UNIT));
10685 emit_block_move (target,
10686 adjust_address (op0, VOIDmode,
10687 bitpos / BITS_PER_UNIT),
10688 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10689 / BITS_PER_UNIT),
10690 (modifier == EXPAND_STACK_PARM
10691 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10693 return target;
10696 /* If we have nothing to extract, the result will be 0 for targets
10697 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10698 return 0 for the sake of consistency, as reading a zero-sized
10699 bitfield is valid in Ada and the value is fully specified. */
10700 if (bitsize == 0)
10701 return const0_rtx;
10703 op0 = validize_mem (op0);
10705 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10706 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10708 /* If the result has a record type and the extraction is done in
10709 an integral mode, then the field may be not aligned on a byte
10710 boundary; in this case, if it has reverse storage order, it
10711 needs to be extracted as a scalar field with reverse storage
10712 order and put back into memory order afterwards. */
10713 if (TREE_CODE (type) == RECORD_TYPE
10714 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10715 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10717 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10718 (modifier == EXPAND_STACK_PARM
10719 ? NULL_RTX : target),
10720 ext_mode, ext_mode, reversep, alt_rtl);
10722 /* If the result has a record type and the mode of OP0 is an
10723 integral mode then, if BITSIZE is narrower than this mode
10724 and this is for big-endian data, we must put the field
10725 into the high-order bits. And we must also put it back
10726 into memory order if it has been previously reversed. */
10727 if (TREE_CODE (type) == RECORD_TYPE
10728 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
10730 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (op0));
10732 if (bitsize < size
10733 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10734 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10735 size - bitsize, op0, 1);
10737 if (reversep)
10738 op0 = flip_storage_order (GET_MODE (op0), op0);
10741 /* If the result type is BLKmode, store the data into a temporary
10742 of the appropriate type, but with the mode corresponding to the
10743 mode for the data we have (op0's mode). */
10744 if (mode == BLKmode)
10746 rtx new_rtx
10747 = assign_stack_temp_for_type (ext_mode,
10748 GET_MODE_BITSIZE (ext_mode),
10749 type);
10750 emit_move_insn (new_rtx, op0);
10751 op0 = copy_rtx (new_rtx);
10752 PUT_MODE (op0, BLKmode);
10755 return op0;
10758 /* If the result is BLKmode, use that to access the object
10759 now as well. */
10760 if (mode == BLKmode)
10761 mode1 = BLKmode;
10763 /* Get a reference to just this component. */
10764 if (modifier == EXPAND_CONST_ADDRESS
10765 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10766 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10767 else
10768 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10770 if (op0 == orig_op0)
10771 op0 = copy_rtx (op0);
10773 /* Don't set memory attributes if the base expression is
10774 SSA_NAME that got expanded as a MEM. In that case, we should
10775 just honor its original memory attributes. */
10776 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10777 set_mem_attributes (op0, exp, 0);
10779 if (REG_P (XEXP (op0, 0)))
10780 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10782 /* If op0 is a temporary because the original expressions was forced
10783 to memory, clear MEM_EXPR so that the original expression cannot
10784 be marked as addressable through MEM_EXPR of the temporary. */
10785 if (clear_mem_expr)
10786 set_mem_expr (op0, NULL_TREE);
10788 MEM_VOLATILE_P (op0) |= volatilep;
10790 if (reversep
10791 && modifier != EXPAND_MEMORY
10792 && modifier != EXPAND_WRITE)
10793 op0 = flip_storage_order (mode1, op0);
10795 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10796 || modifier == EXPAND_CONST_ADDRESS
10797 || modifier == EXPAND_INITIALIZER)
10798 return op0;
10800 if (target == 0)
10801 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10803 convert_move (target, op0, unsignedp);
10804 return target;
10807 case OBJ_TYPE_REF:
10808 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10810 case CALL_EXPR:
10811 /* All valid uses of __builtin_va_arg_pack () are removed during
10812 inlining. */
10813 if (CALL_EXPR_VA_ARG_PACK (exp))
10814 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10816 tree fndecl = get_callee_fndecl (exp), attr;
10818 if (fndecl
10819 && (attr = lookup_attribute ("error",
10820 DECL_ATTRIBUTES (fndecl))) != NULL)
10821 error ("%Kcall to %qs declared with attribute error: %s",
10822 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10823 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10824 if (fndecl
10825 && (attr = lookup_attribute ("warning",
10826 DECL_ATTRIBUTES (fndecl))) != NULL)
10827 warning_at (tree_nonartificial_location (exp),
10828 0, "%Kcall to %qs declared with attribute warning: %s",
10829 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10830 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10832 /* Check for a built-in function. */
10833 if (fndecl && DECL_BUILT_IN (fndecl))
10835 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10836 if (CALL_WITH_BOUNDS_P (exp))
10837 return expand_builtin_with_bounds (exp, target, subtarget,
10838 tmode, ignore);
10839 else
10840 return expand_builtin (exp, target, subtarget, tmode, ignore);
10843 return expand_call (exp, target, ignore);
10845 case VIEW_CONVERT_EXPR:
10846 op0 = NULL_RTX;
10848 /* If we are converting to BLKmode, try to avoid an intermediate
10849 temporary by fetching an inner memory reference. */
10850 if (mode == BLKmode
10851 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10852 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10853 && handled_component_p (treeop0))
10855 machine_mode mode1;
10856 HOST_WIDE_INT bitsize, bitpos;
10857 tree offset;
10858 int unsignedp, reversep, volatilep = 0;
10859 tree tem
10860 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10861 &unsignedp, &reversep, &volatilep);
10862 rtx orig_op0;
10864 /* ??? We should work harder and deal with non-zero offsets. */
10865 if (!offset
10866 && (bitpos % BITS_PER_UNIT) == 0
10867 && !reversep
10868 && bitsize >= 0
10869 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10871 /* See the normal_inner_ref case for the rationale. */
10872 orig_op0
10873 = expand_expr_real (tem,
10874 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10875 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10876 != INTEGER_CST)
10877 && modifier != EXPAND_STACK_PARM
10878 ? target : NULL_RTX),
10879 VOIDmode,
10880 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10881 NULL, true);
10883 if (MEM_P (orig_op0))
10885 op0 = orig_op0;
10887 /* Get a reference to just this component. */
10888 if (modifier == EXPAND_CONST_ADDRESS
10889 || modifier == EXPAND_SUM
10890 || modifier == EXPAND_INITIALIZER)
10891 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10892 else
10893 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10895 if (op0 == orig_op0)
10896 op0 = copy_rtx (op0);
10898 set_mem_attributes (op0, treeop0, 0);
10899 if (REG_P (XEXP (op0, 0)))
10900 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10902 MEM_VOLATILE_P (op0) |= volatilep;
10907 if (!op0)
10908 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10909 NULL, inner_reference_p);
10911 /* If the input and output modes are both the same, we are done. */
10912 if (mode == GET_MODE (op0))
10914 /* If neither mode is BLKmode, and both modes are the same size
10915 then we can use gen_lowpart. */
10916 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10917 && (GET_MODE_PRECISION (mode)
10918 == GET_MODE_PRECISION (GET_MODE (op0)))
10919 && !COMPLEX_MODE_P (GET_MODE (op0)))
10921 if (GET_CODE (op0) == SUBREG)
10922 op0 = force_reg (GET_MODE (op0), op0);
10923 temp = gen_lowpart_common (mode, op0);
10924 if (temp)
10925 op0 = temp;
10926 else
10928 if (!REG_P (op0) && !MEM_P (op0))
10929 op0 = force_reg (GET_MODE (op0), op0);
10930 op0 = gen_lowpart (mode, op0);
10933 /* If both types are integral, convert from one mode to the other. */
10934 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10935 op0 = convert_modes (mode, GET_MODE (op0), op0,
10936 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10937 /* If the output type is a bit-field type, do an extraction. */
10938 else if (reduce_bit_field)
10939 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10940 TYPE_UNSIGNED (type), NULL_RTX,
10941 mode, mode, false, NULL);
10942 /* As a last resort, spill op0 to memory, and reload it in a
10943 different mode. */
10944 else if (!MEM_P (op0))
10946 /* If the operand is not a MEM, force it into memory. Since we
10947 are going to be changing the mode of the MEM, don't call
10948 force_const_mem for constants because we don't allow pool
10949 constants to change mode. */
10950 tree inner_type = TREE_TYPE (treeop0);
10952 gcc_assert (!TREE_ADDRESSABLE (exp));
10954 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10955 target
10956 = assign_stack_temp_for_type
10957 (TYPE_MODE (inner_type),
10958 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10960 emit_move_insn (target, op0);
10961 op0 = target;
10964 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10965 output type is such that the operand is known to be aligned, indicate
10966 that it is. Otherwise, we need only be concerned about alignment for
10967 non-BLKmode results. */
10968 if (MEM_P (op0))
10970 enum insn_code icode;
10972 if (modifier != EXPAND_WRITE
10973 && modifier != EXPAND_MEMORY
10974 && !inner_reference_p
10975 && mode != BLKmode
10976 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10978 /* If the target does have special handling for unaligned
10979 loads of mode then use them. */
10980 if ((icode = optab_handler (movmisalign_optab, mode))
10981 != CODE_FOR_nothing)
10983 rtx reg;
10985 op0 = adjust_address (op0, mode, 0);
10986 /* We've already validated the memory, and we're creating a
10987 new pseudo destination. The predicates really can't
10988 fail. */
10989 reg = gen_reg_rtx (mode);
10991 /* Nor can the insn generator. */
10992 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10993 emit_insn (insn);
10994 return reg;
10996 else if (STRICT_ALIGNMENT)
10998 tree inner_type = TREE_TYPE (treeop0);
10999 HOST_WIDE_INT temp_size
11000 = MAX (int_size_in_bytes (inner_type),
11001 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
11002 rtx new_rtx
11003 = assign_stack_temp_for_type (mode, temp_size, type);
11004 rtx new_with_op0_mode
11005 = adjust_address (new_rtx, GET_MODE (op0), 0);
11007 gcc_assert (!TREE_ADDRESSABLE (exp));
11009 if (GET_MODE (op0) == BLKmode)
11010 emit_block_move (new_with_op0_mode, op0,
11011 GEN_INT (GET_MODE_SIZE (mode)),
11012 (modifier == EXPAND_STACK_PARM
11013 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11014 else
11015 emit_move_insn (new_with_op0_mode, op0);
11017 op0 = new_rtx;
11021 op0 = adjust_address (op0, mode, 0);
11024 return op0;
11026 case MODIFY_EXPR:
11028 tree lhs = treeop0;
11029 tree rhs = treeop1;
11030 gcc_assert (ignore);
11032 /* Check for |= or &= of a bitfield of size one into another bitfield
11033 of size 1. In this case, (unless we need the result of the
11034 assignment) we can do this more efficiently with a
11035 test followed by an assignment, if necessary.
11037 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11038 things change so we do, this code should be enhanced to
11039 support it. */
11040 if (TREE_CODE (lhs) == COMPONENT_REF
11041 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11042 || TREE_CODE (rhs) == BIT_AND_EXPR)
11043 && TREE_OPERAND (rhs, 0) == lhs
11044 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11045 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11046 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11048 rtx_code_label *label = gen_label_rtx ();
11049 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11050 do_jump (TREE_OPERAND (rhs, 1),
11051 value ? label : 0,
11052 value ? 0 : label,
11053 profile_probability::uninitialized ());
11054 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11055 false);
11056 do_pending_stack_adjust ();
11057 emit_label (label);
11058 return const0_rtx;
11061 expand_assignment (lhs, rhs, false);
11062 return const0_rtx;
11065 case ADDR_EXPR:
11066 return expand_expr_addr_expr (exp, target, tmode, modifier);
11068 case REALPART_EXPR:
11069 op0 = expand_normal (treeop0);
11070 return read_complex_part (op0, false);
11072 case IMAGPART_EXPR:
11073 op0 = expand_normal (treeop0);
11074 return read_complex_part (op0, true);
11076 case RETURN_EXPR:
11077 case LABEL_EXPR:
11078 case GOTO_EXPR:
11079 case SWITCH_EXPR:
11080 case ASM_EXPR:
11081 /* Expanded in cfgexpand.c. */
11082 gcc_unreachable ();
11084 case TRY_CATCH_EXPR:
11085 case CATCH_EXPR:
11086 case EH_FILTER_EXPR:
11087 case TRY_FINALLY_EXPR:
11088 /* Lowered by tree-eh.c. */
11089 gcc_unreachable ();
11091 case WITH_CLEANUP_EXPR:
11092 case CLEANUP_POINT_EXPR:
11093 case TARGET_EXPR:
11094 case CASE_LABEL_EXPR:
11095 case VA_ARG_EXPR:
11096 case BIND_EXPR:
11097 case INIT_EXPR:
11098 case CONJ_EXPR:
11099 case COMPOUND_EXPR:
11100 case PREINCREMENT_EXPR:
11101 case PREDECREMENT_EXPR:
11102 case POSTINCREMENT_EXPR:
11103 case POSTDECREMENT_EXPR:
11104 case LOOP_EXPR:
11105 case EXIT_EXPR:
11106 case COMPOUND_LITERAL_EXPR:
11107 /* Lowered by gimplify.c. */
11108 gcc_unreachable ();
11110 case FDESC_EXPR:
11111 /* Function descriptors are not valid except for as
11112 initialization constants, and should not be expanded. */
11113 gcc_unreachable ();
11115 case WITH_SIZE_EXPR:
11116 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11117 have pulled out the size to use in whatever context it needed. */
11118 return expand_expr_real (treeop0, original_target, tmode,
11119 modifier, alt_rtl, inner_reference_p);
11121 default:
11122 return expand_expr_real_2 (&ops, target, tmode, modifier);
11126 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11127 signedness of TYPE), possibly returning the result in TARGET. */
11128 static rtx
11129 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11131 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11132 if (target && GET_MODE (target) != GET_MODE (exp))
11133 target = 0;
11134 /* For constant values, reduce using build_int_cst_type. */
11135 if (CONST_INT_P (exp))
11137 HOST_WIDE_INT value = INTVAL (exp);
11138 tree t = build_int_cst_type (type, value);
11139 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11141 else if (TYPE_UNSIGNED (type))
11143 machine_mode mode = GET_MODE (exp);
11144 rtx mask = immed_wide_int_const
11145 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11146 return expand_and (mode, exp, mask, target);
11148 else
11150 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11151 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11152 exp, count, target, 0);
11153 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11154 exp, count, target, 0);
11158 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11159 when applied to the address of EXP produces an address known to be
11160 aligned more than BIGGEST_ALIGNMENT. */
11162 static int
11163 is_aligning_offset (const_tree offset, const_tree exp)
11165 /* Strip off any conversions. */
11166 while (CONVERT_EXPR_P (offset))
11167 offset = TREE_OPERAND (offset, 0);
11169 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11170 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11171 if (TREE_CODE (offset) != BIT_AND_EXPR
11172 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11173 || compare_tree_int (TREE_OPERAND (offset, 1),
11174 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11175 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11176 return 0;
11178 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11179 It must be NEGATE_EXPR. Then strip any more conversions. */
11180 offset = TREE_OPERAND (offset, 0);
11181 while (CONVERT_EXPR_P (offset))
11182 offset = TREE_OPERAND (offset, 0);
11184 if (TREE_CODE (offset) != NEGATE_EXPR)
11185 return 0;
11187 offset = TREE_OPERAND (offset, 0);
11188 while (CONVERT_EXPR_P (offset))
11189 offset = TREE_OPERAND (offset, 0);
11191 /* This must now be the address of EXP. */
11192 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11195 /* Return the tree node if an ARG corresponds to a string constant or zero
11196 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11197 in bytes within the string that ARG is accessing. The type of the
11198 offset will be `sizetype'. */
11200 tree
11201 string_constant (tree arg, tree *ptr_offset)
11203 tree array, offset, lower_bound;
11204 STRIP_NOPS (arg);
11206 if (TREE_CODE (arg) == ADDR_EXPR)
11208 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11210 *ptr_offset = size_zero_node;
11211 return TREE_OPERAND (arg, 0);
11213 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11215 array = TREE_OPERAND (arg, 0);
11216 offset = size_zero_node;
11218 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11220 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11221 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11222 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11223 return 0;
11225 /* Check if the array has a nonzero lower bound. */
11226 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11227 if (!integer_zerop (lower_bound))
11229 /* If the offset and base aren't both constants, return 0. */
11230 if (TREE_CODE (lower_bound) != INTEGER_CST)
11231 return 0;
11232 if (TREE_CODE (offset) != INTEGER_CST)
11233 return 0;
11234 /* Adjust offset by the lower bound. */
11235 offset = size_diffop (fold_convert (sizetype, offset),
11236 fold_convert (sizetype, lower_bound));
11239 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11241 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11242 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11243 if (TREE_CODE (array) != ADDR_EXPR)
11244 return 0;
11245 array = TREE_OPERAND (array, 0);
11246 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11247 return 0;
11249 else
11250 return 0;
11252 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11254 tree arg0 = TREE_OPERAND (arg, 0);
11255 tree arg1 = TREE_OPERAND (arg, 1);
11257 STRIP_NOPS (arg0);
11258 STRIP_NOPS (arg1);
11260 if (TREE_CODE (arg0) == ADDR_EXPR
11261 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11262 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11264 array = TREE_OPERAND (arg0, 0);
11265 offset = arg1;
11267 else if (TREE_CODE (arg1) == ADDR_EXPR
11268 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11269 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11271 array = TREE_OPERAND (arg1, 0);
11272 offset = arg0;
11274 else
11275 return 0;
11277 else
11278 return 0;
11280 if (TREE_CODE (array) == STRING_CST)
11282 *ptr_offset = fold_convert (sizetype, offset);
11283 return array;
11285 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11287 int length;
11288 tree init = ctor_for_folding (array);
11290 /* Variables initialized to string literals can be handled too. */
11291 if (init == error_mark_node
11292 || !init
11293 || TREE_CODE (init) != STRING_CST)
11294 return 0;
11296 /* Avoid const char foo[4] = "abcde"; */
11297 if (DECL_SIZE_UNIT (array) == NULL_TREE
11298 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11299 || (length = TREE_STRING_LENGTH (init)) <= 0
11300 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11301 return 0;
11303 /* If variable is bigger than the string literal, OFFSET must be constant
11304 and inside of the bounds of the string literal. */
11305 offset = fold_convert (sizetype, offset);
11306 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11307 && (! tree_fits_uhwi_p (offset)
11308 || compare_tree_int (offset, length) >= 0))
11309 return 0;
11311 *ptr_offset = offset;
11312 return init;
11315 return 0;
11318 /* Generate code to calculate OPS, and exploded expression
11319 using a store-flag instruction and return an rtx for the result.
11320 OPS reflects a comparison.
11322 If TARGET is nonzero, store the result there if convenient.
11324 Return zero if there is no suitable set-flag instruction
11325 available on this machine.
11327 Once expand_expr has been called on the arguments of the comparison,
11328 we are committed to doing the store flag, since it is not safe to
11329 re-evaluate the expression. We emit the store-flag insn by calling
11330 emit_store_flag, but only expand the arguments if we have a reason
11331 to believe that emit_store_flag will be successful. If we think that
11332 it will, but it isn't, we have to simulate the store-flag with a
11333 set/jump/set sequence. */
11335 static rtx
11336 do_store_flag (sepops ops, rtx target, machine_mode mode)
11338 enum rtx_code code;
11339 tree arg0, arg1, type;
11340 machine_mode operand_mode;
11341 int unsignedp;
11342 rtx op0, op1;
11343 rtx subtarget = target;
11344 location_t loc = ops->location;
11346 arg0 = ops->op0;
11347 arg1 = ops->op1;
11349 /* Don't crash if the comparison was erroneous. */
11350 if (arg0 == error_mark_node || arg1 == error_mark_node)
11351 return const0_rtx;
11353 type = TREE_TYPE (arg0);
11354 operand_mode = TYPE_MODE (type);
11355 unsignedp = TYPE_UNSIGNED (type);
11357 /* We won't bother with BLKmode store-flag operations because it would mean
11358 passing a lot of information to emit_store_flag. */
11359 if (operand_mode == BLKmode)
11360 return 0;
11362 /* We won't bother with store-flag operations involving function pointers
11363 when function pointers must be canonicalized before comparisons. */
11364 if (targetm.have_canonicalize_funcptr_for_compare ()
11365 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11366 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11367 == FUNCTION_TYPE))
11368 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11369 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11370 == FUNCTION_TYPE))))
11371 return 0;
11373 STRIP_NOPS (arg0);
11374 STRIP_NOPS (arg1);
11376 /* For vector typed comparisons emit code to generate the desired
11377 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11378 expander for this. */
11379 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11381 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11382 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11383 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11384 return expand_vec_cmp_expr (ops->type, ifexp, target);
11385 else
11387 tree if_true = constant_boolean_node (true, ops->type);
11388 tree if_false = constant_boolean_node (false, ops->type);
11389 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11390 if_false, target);
11394 /* Get the rtx comparison code to use. We know that EXP is a comparison
11395 operation of some type. Some comparisons against 1 and -1 can be
11396 converted to comparisons with zero. Do so here so that the tests
11397 below will be aware that we have a comparison with zero. These
11398 tests will not catch constants in the first operand, but constants
11399 are rarely passed as the first operand. */
11401 switch (ops->code)
11403 case EQ_EXPR:
11404 code = EQ;
11405 break;
11406 case NE_EXPR:
11407 code = NE;
11408 break;
11409 case LT_EXPR:
11410 if (integer_onep (arg1))
11411 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11412 else
11413 code = unsignedp ? LTU : LT;
11414 break;
11415 case LE_EXPR:
11416 if (! unsignedp && integer_all_onesp (arg1))
11417 arg1 = integer_zero_node, code = LT;
11418 else
11419 code = unsignedp ? LEU : LE;
11420 break;
11421 case GT_EXPR:
11422 if (! unsignedp && integer_all_onesp (arg1))
11423 arg1 = integer_zero_node, code = GE;
11424 else
11425 code = unsignedp ? GTU : GT;
11426 break;
11427 case GE_EXPR:
11428 if (integer_onep (arg1))
11429 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11430 else
11431 code = unsignedp ? GEU : GE;
11432 break;
11434 case UNORDERED_EXPR:
11435 code = UNORDERED;
11436 break;
11437 case ORDERED_EXPR:
11438 code = ORDERED;
11439 break;
11440 case UNLT_EXPR:
11441 code = UNLT;
11442 break;
11443 case UNLE_EXPR:
11444 code = UNLE;
11445 break;
11446 case UNGT_EXPR:
11447 code = UNGT;
11448 break;
11449 case UNGE_EXPR:
11450 code = UNGE;
11451 break;
11452 case UNEQ_EXPR:
11453 code = UNEQ;
11454 break;
11455 case LTGT_EXPR:
11456 code = LTGT;
11457 break;
11459 default:
11460 gcc_unreachable ();
11463 /* Put a constant second. */
11464 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11465 || TREE_CODE (arg0) == FIXED_CST)
11467 std::swap (arg0, arg1);
11468 code = swap_condition (code);
11471 /* If this is an equality or inequality test of a single bit, we can
11472 do this by shifting the bit being tested to the low-order bit and
11473 masking the result with the constant 1. If the condition was EQ,
11474 we xor it with 1. This does not require an scc insn and is faster
11475 than an scc insn even if we have it.
11477 The code to make this transformation was moved into fold_single_bit_test,
11478 so we just call into the folder and expand its result. */
11480 if ((code == NE || code == EQ)
11481 && integer_zerop (arg1)
11482 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11484 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11485 if (srcstmt
11486 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11488 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11489 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11490 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11491 gimple_assign_rhs1 (srcstmt),
11492 gimple_assign_rhs2 (srcstmt));
11493 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11494 if (temp)
11495 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11499 if (! get_subtarget (target)
11500 || GET_MODE (subtarget) != operand_mode)
11501 subtarget = 0;
11503 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11505 if (target == 0)
11506 target = gen_reg_rtx (mode);
11508 /* Try a cstore if possible. */
11509 return emit_store_flag_force (target, code, op0, op1,
11510 operand_mode, unsignedp,
11511 (TYPE_PRECISION (ops->type) == 1
11512 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11515 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11516 0 otherwise (i.e. if there is no casesi instruction).
11518 DEFAULT_PROBABILITY is the probability of jumping to the default
11519 label. */
11521 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11522 rtx table_label, rtx default_label, rtx fallback_label,
11523 profile_probability default_probability)
11525 struct expand_operand ops[5];
11526 machine_mode index_mode = SImode;
11527 rtx op1, op2, index;
11529 if (! targetm.have_casesi ())
11530 return 0;
11532 /* Convert the index to SImode. */
11533 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11535 machine_mode omode = TYPE_MODE (index_type);
11536 rtx rangertx = expand_normal (range);
11538 /* We must handle the endpoints in the original mode. */
11539 index_expr = build2 (MINUS_EXPR, index_type,
11540 index_expr, minval);
11541 minval = integer_zero_node;
11542 index = expand_normal (index_expr);
11543 if (default_label)
11544 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11545 omode, 1, default_label,
11546 default_probability);
11547 /* Now we can safely truncate. */
11548 index = convert_to_mode (index_mode, index, 0);
11550 else
11552 if (TYPE_MODE (index_type) != index_mode)
11554 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11555 index_expr = fold_convert (index_type, index_expr);
11558 index = expand_normal (index_expr);
11561 do_pending_stack_adjust ();
11563 op1 = expand_normal (minval);
11564 op2 = expand_normal (range);
11566 create_input_operand (&ops[0], index, index_mode);
11567 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11568 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11569 create_fixed_operand (&ops[3], table_label);
11570 create_fixed_operand (&ops[4], (default_label
11571 ? default_label
11572 : fallback_label));
11573 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11574 return 1;
11577 /* Attempt to generate a tablejump instruction; same concept. */
11578 /* Subroutine of the next function.
11580 INDEX is the value being switched on, with the lowest value
11581 in the table already subtracted.
11582 MODE is its expected mode (needed if INDEX is constant).
11583 RANGE is the length of the jump table.
11584 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11586 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11587 index value is out of range.
11588 DEFAULT_PROBABILITY is the probability of jumping to
11589 the default label. */
11591 static void
11592 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11593 rtx default_label, profile_probability default_probability)
11595 rtx temp, vector;
11597 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11598 cfun->cfg->max_jumptable_ents = INTVAL (range);
11600 /* Do an unsigned comparison (in the proper mode) between the index
11601 expression and the value which represents the length of the range.
11602 Since we just finished subtracting the lower bound of the range
11603 from the index expression, this comparison allows us to simultaneously
11604 check that the original index expression value is both greater than
11605 or equal to the minimum value of the range and less than or equal to
11606 the maximum value of the range. */
11608 if (default_label)
11609 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11610 default_label, default_probability);
11613 /* If index is in range, it must fit in Pmode.
11614 Convert to Pmode so we can index with it. */
11615 if (mode != Pmode)
11616 index = convert_to_mode (Pmode, index, 1);
11618 /* Don't let a MEM slip through, because then INDEX that comes
11619 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11620 and break_out_memory_refs will go to work on it and mess it up. */
11621 #ifdef PIC_CASE_VECTOR_ADDRESS
11622 if (flag_pic && !REG_P (index))
11623 index = copy_to_mode_reg (Pmode, index);
11624 #endif
11626 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11627 GET_MODE_SIZE, because this indicates how large insns are. The other
11628 uses should all be Pmode, because they are addresses. This code
11629 could fail if addresses and insns are not the same size. */
11630 index = simplify_gen_binary (MULT, Pmode, index,
11631 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11632 Pmode));
11633 index = simplify_gen_binary (PLUS, Pmode, index,
11634 gen_rtx_LABEL_REF (Pmode, table_label));
11636 #ifdef PIC_CASE_VECTOR_ADDRESS
11637 if (flag_pic)
11638 index = PIC_CASE_VECTOR_ADDRESS (index);
11639 else
11640 #endif
11641 index = memory_address (CASE_VECTOR_MODE, index);
11642 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11643 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11644 convert_move (temp, vector, 0);
11646 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11648 /* If we are generating PIC code or if the table is PC-relative, the
11649 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11650 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11651 emit_barrier ();
11655 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11656 rtx table_label, rtx default_label,
11657 profile_probability default_probability)
11659 rtx index;
11661 if (! targetm.have_tablejump ())
11662 return 0;
11664 index_expr = fold_build2 (MINUS_EXPR, index_type,
11665 fold_convert (index_type, index_expr),
11666 fold_convert (index_type, minval));
11667 index = expand_normal (index_expr);
11668 do_pending_stack_adjust ();
11670 do_tablejump (index, TYPE_MODE (index_type),
11671 convert_modes (TYPE_MODE (index_type),
11672 TYPE_MODE (TREE_TYPE (range)),
11673 expand_normal (range),
11674 TYPE_UNSIGNED (TREE_TYPE (range))),
11675 table_label, default_label, default_probability);
11676 return 1;
11679 /* Return a CONST_VECTOR rtx representing vector mask for
11680 a VECTOR_CST of booleans. */
11681 static rtx
11682 const_vector_mask_from_tree (tree exp)
11684 rtvec v;
11685 unsigned i;
11686 int units;
11687 tree elt;
11688 machine_mode inner, mode;
11690 mode = TYPE_MODE (TREE_TYPE (exp));
11691 units = GET_MODE_NUNITS (mode);
11692 inner = GET_MODE_INNER (mode);
11694 v = rtvec_alloc (units);
11696 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11698 elt = VECTOR_CST_ELT (exp, i);
11700 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11701 if (integer_zerop (elt))
11702 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11703 else if (integer_onep (elt)
11704 || integer_minus_onep (elt))
11705 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11706 else
11707 gcc_unreachable ();
11710 return gen_rtx_CONST_VECTOR (mode, v);
11713 /* Return a CONST_INT rtx representing vector mask for
11714 a VECTOR_CST of booleans. */
11715 static rtx
11716 const_scalar_mask_from_tree (tree exp)
11718 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11719 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11720 tree elt;
11721 unsigned i;
11723 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11725 elt = VECTOR_CST_ELT (exp, i);
11726 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11727 if (integer_all_onesp (elt))
11728 res = wi::set_bit (res, i);
11729 else
11730 gcc_assert (integer_zerop (elt));
11733 return immed_wide_int_const (res, mode);
11736 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11737 static rtx
11738 const_vector_from_tree (tree exp)
11740 rtvec v;
11741 unsigned i;
11742 int units;
11743 tree elt;
11744 machine_mode inner, mode;
11746 mode = TYPE_MODE (TREE_TYPE (exp));
11748 if (initializer_zerop (exp))
11749 return CONST0_RTX (mode);
11751 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11752 return const_vector_mask_from_tree (exp);
11754 units = GET_MODE_NUNITS (mode);
11755 inner = GET_MODE_INNER (mode);
11757 v = rtvec_alloc (units);
11759 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11761 elt = VECTOR_CST_ELT (exp, i);
11763 if (TREE_CODE (elt) == REAL_CST)
11764 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11765 inner);
11766 else if (TREE_CODE (elt) == FIXED_CST)
11767 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11768 inner);
11769 else
11770 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11773 return gen_rtx_CONST_VECTOR (mode, v);
11776 /* Build a decl for a personality function given a language prefix. */
11778 tree
11779 build_personality_function (const char *lang)
11781 const char *unwind_and_version;
11782 tree decl, type;
11783 char *name;
11785 switch (targetm_common.except_unwind_info (&global_options))
11787 case UI_NONE:
11788 return NULL;
11789 case UI_SJLJ:
11790 unwind_and_version = "_sj0";
11791 break;
11792 case UI_DWARF2:
11793 case UI_TARGET:
11794 unwind_and_version = "_v0";
11795 break;
11796 case UI_SEH:
11797 unwind_and_version = "_seh0";
11798 break;
11799 default:
11800 gcc_unreachable ();
11803 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11805 type = build_function_type_list (integer_type_node, integer_type_node,
11806 long_long_unsigned_type_node,
11807 ptr_type_node, ptr_type_node, NULL_TREE);
11808 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11809 get_identifier (name), type);
11810 DECL_ARTIFICIAL (decl) = 1;
11811 DECL_EXTERNAL (decl) = 1;
11812 TREE_PUBLIC (decl) = 1;
11814 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11815 are the flags assigned by targetm.encode_section_info. */
11816 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11818 return decl;
11821 /* Extracts the personality function of DECL and returns the corresponding
11822 libfunc. */
11825 get_personality_function (tree decl)
11827 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11828 enum eh_personality_kind pk;
11830 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11831 if (pk == eh_personality_none)
11832 return NULL;
11834 if (!personality
11835 && pk == eh_personality_any)
11836 personality = lang_hooks.eh_personality ();
11838 if (pk == eh_personality_lang)
11839 gcc_assert (personality != NULL_TREE);
11841 return XEXP (DECL_RTL (personality), 0);
11844 /* Returns a tree for the size of EXP in bytes. */
11846 static tree
11847 tree_expr_size (const_tree exp)
11849 if (DECL_P (exp)
11850 && DECL_SIZE_UNIT (exp) != 0)
11851 return DECL_SIZE_UNIT (exp);
11852 else
11853 return size_in_bytes (TREE_TYPE (exp));
11856 /* Return an rtx for the size in bytes of the value of EXP. */
11859 expr_size (tree exp)
11861 tree size;
11863 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11864 size = TREE_OPERAND (exp, 1);
11865 else
11867 size = tree_expr_size (exp);
11868 gcc_assert (size);
11869 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11872 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11875 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11876 if the size can vary or is larger than an integer. */
11878 static HOST_WIDE_INT
11879 int_expr_size (tree exp)
11881 tree size;
11883 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11884 size = TREE_OPERAND (exp, 1);
11885 else
11887 size = tree_expr_size (exp);
11888 gcc_assert (size);
11891 if (size == 0 || !tree_fits_shwi_p (size))
11892 return -1;
11894 return tree_to_shwi (size);