libgo: add misc/cgo files
[official-gcc.git] / gcc / expr.c
blob5febf07929d0add0ad0ae1356baef008524f0c7c
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "predict.h"
29 #include "memmodel.h"
30 #include "tm_p.h"
31 #include "ssa.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "regs.h"
35 #include "emit-rtl.h"
36 #include "recog.h"
37 #include "cgraph.h"
38 #include "diagnostic.h"
39 #include "alias.h"
40 #include "fold-const.h"
41 #include "stor-layout.h"
42 #include "attribs.h"
43 #include "varasm.h"
44 #include "except.h"
45 #include "insn-attr.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "stmt.h"
50 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
51 #include "expr.h"
52 #include "optabs-tree.h"
53 #include "libfuncs.h"
54 #include "reload.h"
55 #include "langhooks.h"
56 #include "common/common-target.h"
57 #include "tree-ssa-live.h"
58 #include "tree-outof-ssa.h"
59 #include "tree-ssa-address.h"
60 #include "builtins.h"
61 #include "tree-chkp.h"
62 #include "rtl-chkp.h"
63 #include "ccmp.h"
66 /* If this is nonzero, we do not bother generating VOLATILE
67 around volatile memory references, and we are willing to
68 output indirect addresses. If cse is to follow, we reject
69 indirect addresses so a useful potential cse is generated;
70 if it is used only once, instruction combination will produce
71 the same indirect address eventually. */
72 int cse_not_expected;
74 static bool block_move_libcall_safe_for_call_parm (void);
75 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT,
76 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
77 unsigned HOST_WIDE_INT);
78 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
79 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
80 static rtx_insn *compress_float_constant (rtx, rtx);
81 static rtx get_subtarget (rtx);
82 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
83 HOST_WIDE_INT, unsigned HOST_WIDE_INT,
84 unsigned HOST_WIDE_INT, machine_mode,
85 tree, int, alias_set_type, bool);
86 static void store_constructor (tree, rtx, int, HOST_WIDE_INT, bool);
87 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
88 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
89 machine_mode, tree, alias_set_type, bool, bool);
91 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
93 static int is_aligning_offset (const_tree, const_tree);
94 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
95 static rtx do_store_flag (sepops, rtx, machine_mode);
96 #ifdef PUSH_ROUNDING
97 static void emit_single_push_insn (machine_mode, rtx, tree);
98 #endif
99 static void do_tablejump (rtx, machine_mode, rtx, rtx, rtx, int);
100 static rtx const_vector_from_tree (tree);
101 static rtx const_scalar_mask_from_tree (tree);
102 static tree tree_expr_size (const_tree);
103 static HOST_WIDE_INT int_expr_size (tree);
106 /* This is run to set up which modes can be used
107 directly in memory and to initialize the block move optab. It is run
108 at the beginning of compilation and when the target is reinitialized. */
110 void
111 init_expr_target (void)
113 rtx pat;
114 machine_mode mode;
115 int num_clobbers;
116 rtx mem, mem1;
117 rtx reg;
119 /* Try indexing by frame ptr and try by stack ptr.
120 It is known that on the Convex the stack ptr isn't a valid index.
121 With luck, one or the other is valid on any machine. */
122 mem = gen_rtx_MEM (word_mode, stack_pointer_rtx);
123 mem1 = gen_rtx_MEM (word_mode, frame_pointer_rtx);
125 /* A scratch register we can modify in-place below to avoid
126 useless RTL allocations. */
127 reg = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
129 rtx_insn *insn = as_a<rtx_insn *> (rtx_alloc (INSN));
130 pat = gen_rtx_SET (NULL_RTX, NULL_RTX);
131 PATTERN (insn) = pat;
133 for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
134 mode = (machine_mode) ((int) mode + 1))
136 int regno;
138 direct_load[(int) mode] = direct_store[(int) mode] = 0;
139 PUT_MODE (mem, mode);
140 PUT_MODE (mem1, mode);
142 /* See if there is some register that can be used in this mode and
143 directly loaded or stored from memory. */
145 if (mode != VOIDmode && mode != BLKmode)
146 for (regno = 0; regno < FIRST_PSEUDO_REGISTER
147 && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
148 regno++)
150 if (! HARD_REGNO_MODE_OK (regno, mode))
151 continue;
153 set_mode_and_regno (reg, mode, regno);
155 SET_SRC (pat) = mem;
156 SET_DEST (pat) = reg;
157 if (recog (pat, insn, &num_clobbers) >= 0)
158 direct_load[(int) mode] = 1;
160 SET_SRC (pat) = mem1;
161 SET_DEST (pat) = reg;
162 if (recog (pat, insn, &num_clobbers) >= 0)
163 direct_load[(int) mode] = 1;
165 SET_SRC (pat) = reg;
166 SET_DEST (pat) = mem;
167 if (recog (pat, insn, &num_clobbers) >= 0)
168 direct_store[(int) mode] = 1;
170 SET_SRC (pat) = reg;
171 SET_DEST (pat) = mem1;
172 if (recog (pat, insn, &num_clobbers) >= 0)
173 direct_store[(int) mode] = 1;
177 mem = gen_rtx_MEM (VOIDmode, gen_raw_REG (Pmode, LAST_VIRTUAL_REGISTER + 1));
179 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
180 mode = GET_MODE_WIDER_MODE (mode))
182 machine_mode srcmode;
183 for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
184 srcmode = GET_MODE_WIDER_MODE (srcmode))
186 enum insn_code ic;
188 ic = can_extend_p (mode, srcmode, 0);
189 if (ic == CODE_FOR_nothing)
190 continue;
192 PUT_MODE (mem, srcmode);
194 if (insn_operand_matches (ic, 1, mem))
195 float_extend_from_mem[mode][srcmode] = true;
200 /* This is run at the start of compiling a function. */
202 void
203 init_expr (void)
205 memset (&crtl->expr, 0, sizeof (crtl->expr));
208 /* Copy data from FROM to TO, where the machine modes are not the same.
209 Both modes may be integer, or both may be floating, or both may be
210 fixed-point.
211 UNSIGNEDP should be nonzero if FROM is an unsigned type.
212 This causes zero-extension instead of sign-extension. */
214 void
215 convert_move (rtx to, rtx from, int unsignedp)
217 machine_mode to_mode = GET_MODE (to);
218 machine_mode from_mode = GET_MODE (from);
219 int to_real = SCALAR_FLOAT_MODE_P (to_mode);
220 int from_real = SCALAR_FLOAT_MODE_P (from_mode);
221 enum insn_code code;
222 rtx libcall;
224 /* rtx code for making an equivalent value. */
225 enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
226 : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
229 gcc_assert (to_real == from_real);
230 gcc_assert (to_mode != BLKmode);
231 gcc_assert (from_mode != BLKmode);
233 /* If the source and destination are already the same, then there's
234 nothing to do. */
235 if (to == from)
236 return;
238 /* If FROM is a SUBREG that indicates that we have already done at least
239 the required extension, strip it. We don't handle such SUBREGs as
240 TO here. */
242 if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
243 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
244 >= GET_MODE_PRECISION (to_mode))
245 && SUBREG_CHECK_PROMOTED_SIGN (from, unsignedp))
246 from = gen_lowpart (to_mode, from), from_mode = to_mode;
248 gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
250 if (to_mode == from_mode
251 || (from_mode == VOIDmode && CONSTANT_P (from)))
253 emit_move_insn (to, from);
254 return;
257 if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
259 gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
261 if (VECTOR_MODE_P (to_mode))
262 from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
263 else
264 to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
266 emit_move_insn (to, from);
267 return;
270 if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
272 convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
273 convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
274 return;
277 if (to_real)
279 rtx value;
280 rtx_insn *insns;
281 convert_optab tab;
283 gcc_assert ((GET_MODE_PRECISION (from_mode)
284 != GET_MODE_PRECISION (to_mode))
285 || (DECIMAL_FLOAT_MODE_P (from_mode)
286 != DECIMAL_FLOAT_MODE_P (to_mode)));
288 if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
289 /* Conversion between decimal float and binary float, same size. */
290 tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
291 else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
292 tab = sext_optab;
293 else
294 tab = trunc_optab;
296 /* Try converting directly if the insn is supported. */
298 code = convert_optab_handler (tab, to_mode, from_mode);
299 if (code != CODE_FOR_nothing)
301 emit_unop_insn (code, to, from,
302 tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
303 return;
306 /* Otherwise use a libcall. */
307 libcall = convert_optab_libfunc (tab, to_mode, from_mode);
309 /* Is this conversion implemented yet? */
310 gcc_assert (libcall);
312 start_sequence ();
313 value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
314 1, from, from_mode);
315 insns = get_insns ();
316 end_sequence ();
317 emit_libcall_block (insns, to, value,
318 tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
319 from)
320 : gen_rtx_FLOAT_EXTEND (to_mode, from));
321 return;
324 /* Handle pointer conversion. */ /* SPEE 900220. */
325 /* If the target has a converter from FROM_MODE to TO_MODE, use it. */
327 convert_optab ctab;
329 if (GET_MODE_PRECISION (from_mode) > GET_MODE_PRECISION (to_mode))
330 ctab = trunc_optab;
331 else if (unsignedp)
332 ctab = zext_optab;
333 else
334 ctab = sext_optab;
336 if (convert_optab_handler (ctab, to_mode, from_mode)
337 != CODE_FOR_nothing)
339 emit_unop_insn (convert_optab_handler (ctab, to_mode, from_mode),
340 to, from, UNKNOWN);
341 return;
345 /* Targets are expected to provide conversion insns between PxImode and
346 xImode for all MODE_PARTIAL_INT modes they use, but no others. */
347 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
349 machine_mode full_mode
350 = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
352 gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
353 != CODE_FOR_nothing);
355 if (full_mode != from_mode)
356 from = convert_to_mode (full_mode, from, unsignedp);
357 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
358 to, from, UNKNOWN);
359 return;
361 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
363 rtx new_from;
364 machine_mode full_mode
365 = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
366 convert_optab ctab = unsignedp ? zext_optab : sext_optab;
367 enum insn_code icode;
369 icode = convert_optab_handler (ctab, full_mode, from_mode);
370 gcc_assert (icode != CODE_FOR_nothing);
372 if (to_mode == full_mode)
374 emit_unop_insn (icode, to, from, UNKNOWN);
375 return;
378 new_from = gen_reg_rtx (full_mode);
379 emit_unop_insn (icode, new_from, from, UNKNOWN);
381 /* else proceed to integer conversions below. */
382 from_mode = full_mode;
383 from = new_from;
386 /* Make sure both are fixed-point modes or both are not. */
387 gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
388 ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
389 if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
391 /* If we widen from_mode to to_mode and they are in the same class,
392 we won't saturate the result.
393 Otherwise, always saturate the result to play safe. */
394 if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
395 && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
396 expand_fixed_convert (to, from, 0, 0);
397 else
398 expand_fixed_convert (to, from, 0, 1);
399 return;
402 /* Now both modes are integers. */
404 /* Handle expanding beyond a word. */
405 if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
406 && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
408 rtx_insn *insns;
409 rtx lowpart;
410 rtx fill_value;
411 rtx lowfrom;
412 int i;
413 machine_mode lowpart_mode;
414 int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
416 /* Try converting directly if the insn is supported. */
417 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
418 != CODE_FOR_nothing)
420 /* If FROM is a SUBREG, put it into a register. Do this
421 so that we always generate the same set of insns for
422 better cse'ing; if an intermediate assignment occurred,
423 we won't be doing the operation directly on the SUBREG. */
424 if (optimize > 0 && GET_CODE (from) == SUBREG)
425 from = force_reg (from_mode, from);
426 emit_unop_insn (code, to, from, equiv_code);
427 return;
429 /* Next, try converting via full word. */
430 else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
431 && ((code = can_extend_p (to_mode, word_mode, unsignedp))
432 != CODE_FOR_nothing))
434 rtx word_to = gen_reg_rtx (word_mode);
435 if (REG_P (to))
437 if (reg_overlap_mentioned_p (to, from))
438 from = force_reg (from_mode, from);
439 emit_clobber (to);
441 convert_move (word_to, from, unsignedp);
442 emit_unop_insn (code, to, word_to, equiv_code);
443 return;
446 /* No special multiword conversion insn; do it by hand. */
447 start_sequence ();
449 /* Since we will turn this into a no conflict block, we must ensure
450 the source does not overlap the target so force it into an isolated
451 register when maybe so. Likewise for any MEM input, since the
452 conversion sequence might require several references to it and we
453 must ensure we're getting the same value every time. */
455 if (MEM_P (from) || reg_overlap_mentioned_p (to, from))
456 from = force_reg (from_mode, from);
458 /* Get a copy of FROM widened to a word, if necessary. */
459 if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
460 lowpart_mode = word_mode;
461 else
462 lowpart_mode = from_mode;
464 lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
466 lowpart = gen_lowpart (lowpart_mode, to);
467 emit_move_insn (lowpart, lowfrom);
469 /* Compute the value to put in each remaining word. */
470 if (unsignedp)
471 fill_value = const0_rtx;
472 else
473 fill_value = emit_store_flag_force (gen_reg_rtx (word_mode),
474 LT, lowfrom, const0_rtx,
475 lowpart_mode, 0, -1);
477 /* Fill the remaining words. */
478 for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
480 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
481 rtx subword = operand_subword (to, index, 1, to_mode);
483 gcc_assert (subword);
485 if (fill_value != subword)
486 emit_move_insn (subword, fill_value);
489 insns = get_insns ();
490 end_sequence ();
492 emit_insn (insns);
493 return;
496 /* Truncating multi-word to a word or less. */
497 if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
498 && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
500 if (!((MEM_P (from)
501 && ! MEM_VOLATILE_P (from)
502 && direct_load[(int) to_mode]
503 && ! mode_dependent_address_p (XEXP (from, 0),
504 MEM_ADDR_SPACE (from)))
505 || REG_P (from)
506 || GET_CODE (from) == SUBREG))
507 from = force_reg (from_mode, from);
508 convert_move (to, gen_lowpart (word_mode, from), 0);
509 return;
512 /* Now follow all the conversions between integers
513 no more than a word long. */
515 /* For truncation, usually we can just refer to FROM in a narrower mode. */
516 if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
517 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
519 if (!((MEM_P (from)
520 && ! MEM_VOLATILE_P (from)
521 && direct_load[(int) to_mode]
522 && ! mode_dependent_address_p (XEXP (from, 0),
523 MEM_ADDR_SPACE (from)))
524 || REG_P (from)
525 || GET_CODE (from) == SUBREG))
526 from = force_reg (from_mode, from);
527 if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
528 && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
529 from = copy_to_reg (from);
530 emit_move_insn (to, gen_lowpart (to_mode, from));
531 return;
534 /* Handle extension. */
535 if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
537 /* Convert directly if that works. */
538 if ((code = can_extend_p (to_mode, from_mode, unsignedp))
539 != CODE_FOR_nothing)
541 emit_unop_insn (code, to, from, equiv_code);
542 return;
544 else
546 machine_mode intermediate;
547 rtx tmp;
548 int shift_amount;
550 /* Search for a mode to convert via. */
551 for (intermediate = from_mode; intermediate != VOIDmode;
552 intermediate = GET_MODE_WIDER_MODE (intermediate))
553 if (((can_extend_p (to_mode, intermediate, unsignedp)
554 != CODE_FOR_nothing)
555 || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
556 && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
557 && (can_extend_p (intermediate, from_mode, unsignedp)
558 != CODE_FOR_nothing))
560 convert_move (to, convert_to_mode (intermediate, from,
561 unsignedp), unsignedp);
562 return;
565 /* No suitable intermediate mode.
566 Generate what we need with shifts. */
567 shift_amount = (GET_MODE_PRECISION (to_mode)
568 - GET_MODE_PRECISION (from_mode));
569 from = gen_lowpart (to_mode, force_reg (from_mode, from));
570 tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
571 to, unsignedp);
572 tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
573 to, unsignedp);
574 if (tmp != to)
575 emit_move_insn (to, tmp);
576 return;
580 /* Support special truncate insns for certain modes. */
581 if (convert_optab_handler (trunc_optab, to_mode,
582 from_mode) != CODE_FOR_nothing)
584 emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
585 to, from, UNKNOWN);
586 return;
589 /* Handle truncation of volatile memrefs, and so on;
590 the things that couldn't be truncated directly,
591 and for which there was no special instruction.
593 ??? Code above formerly short-circuited this, for most integer
594 mode pairs, with a force_reg in from_mode followed by a recursive
595 call to this routine. Appears always to have been wrong. */
596 if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
598 rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
599 emit_move_insn (to, temp);
600 return;
603 /* Mode combination is not recognized. */
604 gcc_unreachable ();
607 /* Return an rtx for a value that would result
608 from converting X to mode MODE.
609 Both X and MODE may be floating, or both integer.
610 UNSIGNEDP is nonzero if X is an unsigned value.
611 This can be done by referring to a part of X in place
612 or by copying to a new temporary with conversion. */
615 convert_to_mode (machine_mode mode, rtx x, int unsignedp)
617 return convert_modes (mode, VOIDmode, x, unsignedp);
620 /* Return an rtx for a value that would result
621 from converting X from mode OLDMODE to mode MODE.
622 Both modes may be floating, or both integer.
623 UNSIGNEDP is nonzero if X is an unsigned value.
625 This can be done by referring to a part of X in place
626 or by copying to a new temporary with conversion.
628 You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode. */
631 convert_modes (machine_mode mode, machine_mode oldmode, rtx x, int unsignedp)
633 rtx temp;
635 /* If FROM is a SUBREG that indicates that we have already done at least
636 the required extension, strip it. */
638 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
639 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
640 && SUBREG_CHECK_PROMOTED_SIGN (x, unsignedp))
641 x = gen_lowpart (mode, SUBREG_REG (x));
643 if (GET_MODE (x) != VOIDmode)
644 oldmode = GET_MODE (x);
646 if (mode == oldmode)
647 return x;
649 if (CONST_SCALAR_INT_P (x) && GET_MODE_CLASS (mode) == MODE_INT)
651 /* If the caller did not tell us the old mode, then there is not
652 much to do with respect to canonicalization. We have to
653 assume that all the bits are significant. */
654 if (GET_MODE_CLASS (oldmode) != MODE_INT)
655 oldmode = MAX_MODE_INT;
656 wide_int w = wide_int::from (rtx_mode_t (x, oldmode),
657 GET_MODE_PRECISION (mode),
658 unsignedp ? UNSIGNED : SIGNED);
659 return immed_wide_int_const (w, mode);
662 /* We can do this with a gen_lowpart if both desired and current modes
663 are integer, and this is either a constant integer, a register, or a
664 non-volatile MEM. */
665 if (GET_MODE_CLASS (mode) == MODE_INT
666 && GET_MODE_CLASS (oldmode) == MODE_INT
667 && GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
668 && ((MEM_P (x) && !MEM_VOLATILE_P (x) && direct_load[(int) mode])
669 || (REG_P (x)
670 && (!HARD_REGISTER_P (x)
671 || HARD_REGNO_MODE_OK (REGNO (x), mode))
672 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x)))))
674 return gen_lowpart (mode, x);
676 /* Converting from integer constant into mode is always equivalent to an
677 subreg operation. */
678 if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
680 gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
681 return simplify_gen_subreg (mode, x, oldmode, 0);
684 temp = gen_reg_rtx (mode);
685 convert_move (temp, x, unsignedp);
686 return temp;
689 /* Return the largest alignment we can use for doing a move (or store)
690 of MAX_PIECES. ALIGN is the largest alignment we could use. */
692 static unsigned int
693 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
695 machine_mode tmode;
697 tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
698 if (align >= GET_MODE_ALIGNMENT (tmode))
699 align = GET_MODE_ALIGNMENT (tmode);
700 else
702 machine_mode tmode, xmode;
704 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
705 tmode != VOIDmode;
706 xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
707 if (GET_MODE_SIZE (tmode) > max_pieces
708 || SLOW_UNALIGNED_ACCESS (tmode, align))
709 break;
711 align = MAX (align, GET_MODE_ALIGNMENT (xmode));
714 return align;
717 /* Return the widest integer mode no wider than SIZE. If no such mode
718 can be found, return VOIDmode. */
720 static machine_mode
721 widest_int_mode_for_size (unsigned int size)
723 machine_mode tmode, mode = VOIDmode;
725 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
726 tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
727 if (GET_MODE_SIZE (tmode) < size)
728 mode = tmode;
730 return mode;
733 /* Determine whether an operation OP on LEN bytes with alignment ALIGN can
734 and should be performed piecewise. */
736 static bool
737 can_do_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align,
738 enum by_pieces_operation op)
740 return targetm.use_by_pieces_infrastructure_p (len, align, op,
741 optimize_insn_for_speed_p ());
744 /* Determine whether the LEN bytes can be moved by using several move
745 instructions. Return nonzero if a call to move_by_pieces should
746 succeed. */
748 bool
749 can_move_by_pieces (unsigned HOST_WIDE_INT len, unsigned int align)
751 return can_do_by_pieces (len, align, MOVE_BY_PIECES);
754 /* Return number of insns required to perform operation OP by pieces
755 for L bytes. ALIGN (in bits) is maximum alignment we can assume. */
757 unsigned HOST_WIDE_INT
758 by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
759 unsigned int max_size, by_pieces_operation op)
761 unsigned HOST_WIDE_INT n_insns = 0;
763 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
765 while (max_size > 1 && l > 0)
767 machine_mode mode;
768 enum insn_code icode;
770 mode = widest_int_mode_for_size (max_size);
772 if (mode == VOIDmode)
773 break;
774 unsigned int modesize = GET_MODE_SIZE (mode);
776 icode = optab_handler (mov_optab, mode);
777 if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
779 unsigned HOST_WIDE_INT n_pieces = l / modesize;
780 l %= modesize;
781 switch (op)
783 default:
784 n_insns += n_pieces;
785 break;
787 case COMPARE_BY_PIECES:
788 int batch = targetm.compare_by_pieces_branch_ratio (mode);
789 int batch_ops = 4 * batch - 1;
790 unsigned HOST_WIDE_INT full = n_pieces / batch;
791 n_insns += full * batch_ops;
792 if (n_pieces % batch != 0)
793 n_insns++;
794 break;
798 max_size = modesize;
801 gcc_assert (!l);
802 return n_insns;
805 /* Used when performing piecewise block operations, holds information
806 about one of the memory objects involved. The member functions
807 can be used to generate code for loading from the object and
808 updating the address when iterating. */
810 class pieces_addr
812 /* The object being referenced, a MEM. Can be NULL_RTX to indicate
813 stack pushes. */
814 rtx m_obj;
815 /* The address of the object. Can differ from that seen in the
816 MEM rtx if we copied the address to a register. */
817 rtx m_addr;
818 /* Nonzero if the address on the object has an autoincrement already,
819 signifies whether that was an increment or decrement. */
820 signed char m_addr_inc;
821 /* Nonzero if we intend to use autoinc without the address already
822 having autoinc form. We will insert add insns around each memory
823 reference, expecting later passes to form autoinc addressing modes.
824 The only supported options are predecrement and postincrement. */
825 signed char m_explicit_inc;
826 /* True if we have either of the two possible cases of using
827 autoincrement. */
828 bool m_auto;
829 /* True if this is an address to be used for load operations rather
830 than stores. */
831 bool m_is_load;
833 /* Optionally, a function to obtain constants for any given offset into
834 the objects, and data associated with it. */
835 by_pieces_constfn m_constfn;
836 void *m_cfndata;
837 public:
838 pieces_addr (rtx, bool, by_pieces_constfn, void *);
839 rtx adjust (machine_mode, HOST_WIDE_INT);
840 void increment_address (HOST_WIDE_INT);
841 void maybe_predec (HOST_WIDE_INT);
842 void maybe_postinc (HOST_WIDE_INT);
843 void decide_autoinc (machine_mode, bool, HOST_WIDE_INT);
844 int get_addr_inc ()
846 return m_addr_inc;
850 /* Initialize a pieces_addr structure from an object OBJ. IS_LOAD is
851 true if the operation to be performed on this object is a load
852 rather than a store. For stores, OBJ can be NULL, in which case we
853 assume the operation is a stack push. For loads, the optional
854 CONSTFN and its associated CFNDATA can be used in place of the
855 memory load. */
857 pieces_addr::pieces_addr (rtx obj, bool is_load, by_pieces_constfn constfn,
858 void *cfndata)
859 : m_obj (obj), m_is_load (is_load), m_constfn (constfn), m_cfndata (cfndata)
861 m_addr_inc = 0;
862 m_auto = false;
863 if (obj)
865 rtx addr = XEXP (obj, 0);
866 rtx_code code = GET_CODE (addr);
867 m_addr = addr;
868 bool dec = code == PRE_DEC || code == POST_DEC;
869 bool inc = code == PRE_INC || code == POST_INC;
870 m_auto = inc || dec;
871 if (m_auto)
872 m_addr_inc = dec ? -1 : 1;
874 /* While we have always looked for these codes here, the code
875 implementing the memory operation has never handled them.
876 Support could be added later if necessary or beneficial. */
877 gcc_assert (code != PRE_INC && code != POST_DEC);
879 else
881 m_addr = NULL_RTX;
882 if (!is_load)
884 m_auto = true;
885 if (STACK_GROWS_DOWNWARD)
886 m_addr_inc = -1;
887 else
888 m_addr_inc = 1;
890 else
891 gcc_assert (constfn != NULL);
893 m_explicit_inc = 0;
894 if (constfn)
895 gcc_assert (is_load);
898 /* Decide whether to use autoinc for an address involved in a memory op.
899 MODE is the mode of the accesses, REVERSE is true if we've decided to
900 perform the operation starting from the end, and LEN is the length of
901 the operation. Don't override an earlier decision to set m_auto. */
903 void
904 pieces_addr::decide_autoinc (machine_mode ARG_UNUSED (mode), bool reverse,
905 HOST_WIDE_INT len)
907 if (m_auto || m_obj == NULL_RTX)
908 return;
910 bool use_predec = (m_is_load
911 ? USE_LOAD_PRE_DECREMENT (mode)
912 : USE_STORE_PRE_DECREMENT (mode));
913 bool use_postinc = (m_is_load
914 ? USE_LOAD_POST_INCREMENT (mode)
915 : USE_STORE_POST_INCREMENT (mode));
916 machine_mode addr_mode = get_address_mode (m_obj);
918 if (use_predec && reverse)
920 m_addr = copy_to_mode_reg (addr_mode,
921 plus_constant (addr_mode,
922 m_addr, len));
923 m_auto = true;
924 m_explicit_inc = -1;
926 else if (use_postinc && !reverse)
928 m_addr = copy_to_mode_reg (addr_mode, m_addr);
929 m_auto = true;
930 m_explicit_inc = 1;
932 else if (CONSTANT_P (m_addr))
933 m_addr = copy_to_mode_reg (addr_mode, m_addr);
936 /* Adjust the address to refer to the data at OFFSET in MODE. If we
937 are using autoincrement for this address, we don't add the offset,
938 but we still modify the MEM's properties. */
941 pieces_addr::adjust (machine_mode mode, HOST_WIDE_INT offset)
943 if (m_constfn)
944 return m_constfn (m_cfndata, offset, mode);
945 if (m_obj == NULL_RTX)
946 return NULL_RTX;
947 if (m_auto)
948 return adjust_automodify_address (m_obj, mode, m_addr, offset);
949 else
950 return adjust_address (m_obj, mode, offset);
953 /* Emit an add instruction to increment the address by SIZE. */
955 void
956 pieces_addr::increment_address (HOST_WIDE_INT size)
958 rtx amount = gen_int_mode (size, GET_MODE (m_addr));
959 emit_insn (gen_add2_insn (m_addr, amount));
962 /* If we are supposed to decrement the address after each access, emit code
963 to do so now. Increment by SIZE (which has should have the correct sign
964 already). */
966 void
967 pieces_addr::maybe_predec (HOST_WIDE_INT size)
969 if (m_explicit_inc >= 0)
970 return;
971 gcc_assert (HAVE_PRE_DECREMENT);
972 increment_address (size);
975 /* If we are supposed to decrement the address after each access, emit code
976 to do so now. Increment by SIZE. */
978 void
979 pieces_addr::maybe_postinc (HOST_WIDE_INT size)
981 if (m_explicit_inc <= 0)
982 return;
983 gcc_assert (HAVE_POST_INCREMENT);
984 increment_address (size);
987 /* This structure is used by do_op_by_pieces to describe the operation
988 to be performed. */
990 class op_by_pieces_d
992 protected:
993 pieces_addr m_to, m_from;
994 unsigned HOST_WIDE_INT m_len;
995 HOST_WIDE_INT m_offset;
996 unsigned int m_align;
997 unsigned int m_max_size;
998 bool m_reverse;
1000 /* Virtual functions, overriden by derived classes for the specific
1001 operation. */
1002 virtual void generate (rtx, rtx, machine_mode) = 0;
1003 virtual bool prepare_mode (machine_mode, unsigned int) = 0;
1004 virtual void finish_mode (machine_mode)
1008 public:
1009 op_by_pieces_d (rtx, bool, rtx, bool, by_pieces_constfn, void *,
1010 unsigned HOST_WIDE_INT, unsigned int);
1011 void run ();
1014 /* The constructor for an op_by_pieces_d structure. We require two
1015 objects named TO and FROM, which are identified as loads or stores
1016 by TO_LOAD and FROM_LOAD. If FROM is a load, the optional FROM_CFN
1017 and its associated FROM_CFN_DATA can be used to replace loads with
1018 constant values. LEN describes the length of the operation. */
1020 op_by_pieces_d::op_by_pieces_d (rtx to, bool to_load,
1021 rtx from, bool from_load,
1022 by_pieces_constfn from_cfn,
1023 void *from_cfn_data,
1024 unsigned HOST_WIDE_INT len,
1025 unsigned int align)
1026 : m_to (to, to_load, NULL, NULL),
1027 m_from (from, from_load, from_cfn, from_cfn_data),
1028 m_len (len), m_max_size (MOVE_MAX_PIECES + 1)
1030 int toi = m_to.get_addr_inc ();
1031 int fromi = m_from.get_addr_inc ();
1032 if (toi >= 0 && fromi >= 0)
1033 m_reverse = false;
1034 else if (toi <= 0 && fromi <= 0)
1035 m_reverse = true;
1036 else
1037 gcc_unreachable ();
1039 m_offset = m_reverse ? len : 0;
1040 align = MIN (to ? MEM_ALIGN (to) : align,
1041 from ? MEM_ALIGN (from) : align);
1043 /* If copying requires more than two move insns,
1044 copy addresses to registers (to make displacements shorter)
1045 and use post-increment if available. */
1046 if (by_pieces_ninsns (len, align, m_max_size, MOVE_BY_PIECES) > 2)
1048 /* Find the mode of the largest comparison. */
1049 machine_mode mode = widest_int_mode_for_size (m_max_size);
1051 m_from.decide_autoinc (mode, m_reverse, len);
1052 m_to.decide_autoinc (mode, m_reverse, len);
1055 align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1056 m_align = align;
1059 /* This function contains the main loop used for expanding a block
1060 operation. First move what we can in the largest integer mode,
1061 then go to successively smaller modes. For every access, call
1062 GENFUN with the two operands and the EXTRA_DATA. */
1064 void
1065 op_by_pieces_d::run ()
1067 while (m_max_size > 1 && m_len > 0)
1069 machine_mode mode = widest_int_mode_for_size (m_max_size);
1071 if (mode == VOIDmode)
1072 break;
1074 if (prepare_mode (mode, m_align))
1076 unsigned int size = GET_MODE_SIZE (mode);
1077 rtx to1 = NULL_RTX, from1;
1079 while (m_len >= size)
1081 if (m_reverse)
1082 m_offset -= size;
1084 to1 = m_to.adjust (mode, m_offset);
1085 from1 = m_from.adjust (mode, m_offset);
1087 m_to.maybe_predec (-(HOST_WIDE_INT)size);
1088 m_from.maybe_predec (-(HOST_WIDE_INT)size);
1090 generate (to1, from1, mode);
1092 m_to.maybe_postinc (size);
1093 m_from.maybe_postinc (size);
1095 if (!m_reverse)
1096 m_offset += size;
1098 m_len -= size;
1101 finish_mode (mode);
1104 m_max_size = GET_MODE_SIZE (mode);
1107 /* The code above should have handled everything. */
1108 gcc_assert (!m_len);
1111 /* Derived class from op_by_pieces_d, providing support for block move
1112 operations. */
1114 class move_by_pieces_d : public op_by_pieces_d
1116 insn_gen_fn m_gen_fun;
1117 void generate (rtx, rtx, machine_mode);
1118 bool prepare_mode (machine_mode, unsigned int);
1120 public:
1121 move_by_pieces_d (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1122 unsigned int align)
1123 : op_by_pieces_d (to, false, from, true, NULL, NULL, len, align)
1126 rtx finish_endp (int);
1129 /* Return true if MODE can be used for a set of copies, given an
1130 alignment ALIGN. Prepare whatever data is necessary for later
1131 calls to generate. */
1133 bool
1134 move_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1136 insn_code icode = optab_handler (mov_optab, mode);
1137 m_gen_fun = GEN_FCN (icode);
1138 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1141 /* A callback used when iterating for a compare_by_pieces_operation.
1142 OP0 and OP1 are the values that have been loaded and should be
1143 compared in MODE. If OP0 is NULL, this means we should generate a
1144 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1145 gen function that should be used to generate the mode. */
1147 void
1148 move_by_pieces_d::generate (rtx op0, rtx op1,
1149 machine_mode mode ATTRIBUTE_UNUSED)
1151 #ifdef PUSH_ROUNDING
1152 if (op0 == NULL_RTX)
1154 emit_single_push_insn (mode, op1, NULL);
1155 return;
1157 #endif
1158 emit_insn (m_gen_fun (op0, op1));
1161 /* Perform the final adjustment at the end of a string to obtain the
1162 correct return value for the block operation. If ENDP is 1 return
1163 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1164 end minus one byte ala stpcpy. */
1167 move_by_pieces_d::finish_endp (int endp)
1169 gcc_assert (!m_reverse);
1170 if (endp == 2)
1172 m_to.maybe_postinc (-1);
1173 --m_offset;
1175 return m_to.adjust (QImode, m_offset);
1178 /* Generate several move instructions to copy LEN bytes from block FROM to
1179 block TO. (These are MEM rtx's with BLKmode).
1181 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1182 used to push FROM to the stack.
1184 ALIGN is maximum stack alignment we can assume.
1186 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1187 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1188 stpcpy. */
1191 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
1192 unsigned int align, int endp)
1194 #ifndef PUSH_ROUNDING
1195 if (to == NULL)
1196 gcc_unreachable ();
1197 #endif
1199 move_by_pieces_d data (to, from, len, align);
1201 data.run ();
1203 if (endp)
1204 return data.finish_endp (endp);
1205 else
1206 return to;
1209 /* Derived class from op_by_pieces_d, providing support for block move
1210 operations. */
1212 class store_by_pieces_d : public op_by_pieces_d
1214 insn_gen_fn m_gen_fun;
1215 void generate (rtx, rtx, machine_mode);
1216 bool prepare_mode (machine_mode, unsigned int);
1218 public:
1219 store_by_pieces_d (rtx to, by_pieces_constfn cfn, void *cfn_data,
1220 unsigned HOST_WIDE_INT len, unsigned int align)
1221 : op_by_pieces_d (to, false, NULL_RTX, true, cfn, cfn_data, len, align)
1224 rtx finish_endp (int);
1227 /* Return true if MODE can be used for a set of stores, given an
1228 alignment ALIGN. Prepare whatever data is necessary for later
1229 calls to generate. */
1231 bool
1232 store_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1234 insn_code icode = optab_handler (mov_optab, mode);
1235 m_gen_fun = GEN_FCN (icode);
1236 return icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode);
1239 /* A callback used when iterating for a store_by_pieces_operation.
1240 OP0 and OP1 are the values that have been loaded and should be
1241 compared in MODE. If OP0 is NULL, this means we should generate a
1242 push; otherwise EXTRA_DATA holds a pointer to a pointer to the insn
1243 gen function that should be used to generate the mode. */
1245 void
1246 store_by_pieces_d::generate (rtx op0, rtx op1, machine_mode)
1248 emit_insn (m_gen_fun (op0, op1));
1251 /* Perform the final adjustment at the end of a string to obtain the
1252 correct return value for the block operation. If ENDP is 1 return
1253 memory at the end ala mempcpy, and if ENDP is 2 return memory the
1254 end minus one byte ala stpcpy. */
1257 store_by_pieces_d::finish_endp (int endp)
1259 gcc_assert (!m_reverse);
1260 if (endp == 2)
1262 m_to.maybe_postinc (-1);
1263 --m_offset;
1265 return m_to.adjust (QImode, m_offset);
1268 /* Determine whether the LEN bytes generated by CONSTFUN can be
1269 stored to memory using several move instructions. CONSTFUNDATA is
1270 a pointer which will be passed as argument in every CONSTFUN call.
1271 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1272 a memset operation and false if it's a copy of a constant string.
1273 Return nonzero if a call to store_by_pieces should succeed. */
1276 can_store_by_pieces (unsigned HOST_WIDE_INT len,
1277 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1278 void *constfundata, unsigned int align, bool memsetp)
1280 unsigned HOST_WIDE_INT l;
1281 unsigned int max_size;
1282 HOST_WIDE_INT offset = 0;
1283 machine_mode mode;
1284 enum insn_code icode;
1285 int reverse;
1286 /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it. */
1287 rtx cst ATTRIBUTE_UNUSED;
1289 if (len == 0)
1290 return 1;
1292 if (!targetm.use_by_pieces_infrastructure_p (len, align,
1293 memsetp
1294 ? SET_BY_PIECES
1295 : STORE_BY_PIECES,
1296 optimize_insn_for_speed_p ()))
1297 return 0;
1299 align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
1301 /* We would first store what we can in the largest integer mode, then go to
1302 successively smaller modes. */
1304 for (reverse = 0;
1305 reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
1306 reverse++)
1308 l = len;
1309 max_size = STORE_MAX_PIECES + 1;
1310 while (max_size > 1 && l > 0)
1312 mode = widest_int_mode_for_size (max_size);
1314 if (mode == VOIDmode)
1315 break;
1317 icode = optab_handler (mov_optab, mode);
1318 if (icode != CODE_FOR_nothing
1319 && align >= GET_MODE_ALIGNMENT (mode))
1321 unsigned int size = GET_MODE_SIZE (mode);
1323 while (l >= size)
1325 if (reverse)
1326 offset -= size;
1328 cst = (*constfun) (constfundata, offset, mode);
1329 if (!targetm.legitimate_constant_p (mode, cst))
1330 return 0;
1332 if (!reverse)
1333 offset += size;
1335 l -= size;
1339 max_size = GET_MODE_SIZE (mode);
1342 /* The code above should have handled everything. */
1343 gcc_assert (!l);
1346 return 1;
1349 /* Generate several move instructions to store LEN bytes generated by
1350 CONSTFUN to block TO. (A MEM rtx with BLKmode). CONSTFUNDATA is a
1351 pointer which will be passed as argument in every CONSTFUN call.
1352 ALIGN is maximum alignment we can assume. MEMSETP is true if this is
1353 a memset operation and false if it's a copy of a constant string.
1354 If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
1355 mempcpy, and if ENDP is 2 return memory the end minus one byte ala
1356 stpcpy. */
1359 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
1360 rtx (*constfun) (void *, HOST_WIDE_INT, machine_mode),
1361 void *constfundata, unsigned int align, bool memsetp, int endp)
1363 if (len == 0)
1365 gcc_assert (endp != 2);
1366 return to;
1369 gcc_assert (targetm.use_by_pieces_infrastructure_p
1370 (len, align,
1371 memsetp ? SET_BY_PIECES : STORE_BY_PIECES,
1372 optimize_insn_for_speed_p ()));
1374 store_by_pieces_d data (to, constfun, constfundata, len, align);
1375 data.run ();
1377 if (endp)
1378 return data.finish_endp (endp);
1379 else
1380 return to;
1383 /* Callback routine for clear_by_pieces.
1384 Return const0_rtx unconditionally. */
1386 static rtx
1387 clear_by_pieces_1 (void *, HOST_WIDE_INT, machine_mode)
1389 return const0_rtx;
1392 /* Generate several move instructions to clear LEN bytes of block TO. (A MEM
1393 rtx with BLKmode). ALIGN is maximum alignment we can assume. */
1395 static void
1396 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
1398 if (len == 0)
1399 return;
1401 store_by_pieces_d data (to, clear_by_pieces_1, NULL, len, align);
1402 data.run ();
1405 /* Context used by compare_by_pieces_genfn. It stores the fail label
1406 to jump to in case of miscomparison, and for branch ratios greater than 1,
1407 it stores an accumulator and the current and maximum counts before
1408 emitting another branch. */
1410 class compare_by_pieces_d : public op_by_pieces_d
1412 rtx_code_label *m_fail_label;
1413 rtx m_accumulator;
1414 int m_count, m_batch;
1416 void generate (rtx, rtx, machine_mode);
1417 bool prepare_mode (machine_mode, unsigned int);
1418 void finish_mode (machine_mode);
1419 public:
1420 compare_by_pieces_d (rtx op0, rtx op1, by_pieces_constfn op1_cfn,
1421 void *op1_cfn_data, HOST_WIDE_INT len, int align,
1422 rtx_code_label *fail_label)
1423 : op_by_pieces_d (op0, true, op1, true, op1_cfn, op1_cfn_data, len, align)
1425 m_fail_label = fail_label;
1429 /* A callback used when iterating for a compare_by_pieces_operation.
1430 OP0 and OP1 are the values that have been loaded and should be
1431 compared in MODE. DATA holds a pointer to the compare_by_pieces_data
1432 context structure. */
1434 void
1435 compare_by_pieces_d::generate (rtx op0, rtx op1, machine_mode mode)
1437 if (m_batch > 1)
1439 rtx temp = expand_binop (mode, sub_optab, op0, op1, NULL_RTX,
1440 true, OPTAB_LIB_WIDEN);
1441 if (m_count != 0)
1442 temp = expand_binop (mode, ior_optab, m_accumulator, temp, temp,
1443 true, OPTAB_LIB_WIDEN);
1444 m_accumulator = temp;
1446 if (++m_count < m_batch)
1447 return;
1449 m_count = 0;
1450 op0 = m_accumulator;
1451 op1 = const0_rtx;
1452 m_accumulator = NULL_RTX;
1454 do_compare_rtx_and_jump (op0, op1, NE, true, mode, NULL_RTX, NULL,
1455 m_fail_label, -1);
1458 /* Return true if MODE can be used for a set of moves and comparisons,
1459 given an alignment ALIGN. Prepare whatever data is necessary for
1460 later calls to generate. */
1462 bool
1463 compare_by_pieces_d::prepare_mode (machine_mode mode, unsigned int align)
1465 insn_code icode = optab_handler (mov_optab, mode);
1466 if (icode == CODE_FOR_nothing
1467 || align < GET_MODE_ALIGNMENT (mode)
1468 || !can_compare_p (EQ, mode, ccp_jump))
1469 return false;
1470 m_batch = targetm.compare_by_pieces_branch_ratio (mode);
1471 if (m_batch < 0)
1472 return false;
1473 m_accumulator = NULL_RTX;
1474 m_count = 0;
1475 return true;
1478 /* Called after expanding a series of comparisons in MODE. If we have
1479 accumulated results for which we haven't emitted a branch yet, do
1480 so now. */
1482 void
1483 compare_by_pieces_d::finish_mode (machine_mode mode)
1485 if (m_accumulator != NULL_RTX)
1486 do_compare_rtx_and_jump (m_accumulator, const0_rtx, NE, true, mode,
1487 NULL_RTX, NULL, m_fail_label, -1);
1490 /* Generate several move instructions to compare LEN bytes from blocks
1491 ARG0 and ARG1. (These are MEM rtx's with BLKmode).
1493 If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
1494 used to push FROM to the stack.
1496 ALIGN is maximum stack alignment we can assume.
1498 Optionally, the caller can pass a constfn and associated data in A1_CFN
1499 and A1_CFN_DATA. describing that the second operand being compared is a
1500 known constant and how to obtain its data. */
1502 static rtx
1503 compare_by_pieces (rtx arg0, rtx arg1, unsigned HOST_WIDE_INT len,
1504 rtx target, unsigned int align,
1505 by_pieces_constfn a1_cfn, void *a1_cfn_data)
1507 rtx_code_label *fail_label = gen_label_rtx ();
1508 rtx_code_label *end_label = gen_label_rtx ();
1510 if (target == NULL_RTX
1511 || !REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
1512 target = gen_reg_rtx (TYPE_MODE (integer_type_node));
1514 compare_by_pieces_d data (arg0, arg1, a1_cfn, a1_cfn_data, len, align,
1515 fail_label);
1517 data.run ();
1519 emit_move_insn (target, const0_rtx);
1520 emit_jump (end_label);
1521 emit_barrier ();
1522 emit_label (fail_label);
1523 emit_move_insn (target, const1_rtx);
1524 emit_label (end_label);
1526 return target;
1529 /* Emit code to move a block Y to a block X. This may be done with
1530 string-move instructions, with multiple scalar move instructions,
1531 or with a library call.
1533 Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1534 SIZE is an rtx that says how long they are.
1535 ALIGN is the maximum alignment we can assume they have.
1536 METHOD describes what kind of copy this is, and what mechanisms may be used.
1537 MIN_SIZE is the minimal size of block to move
1538 MAX_SIZE is the maximal size of block to move, if it can not be represented
1539 in unsigned HOST_WIDE_INT, than it is mask of all ones.
1541 Return the address of the new block, if memcpy is called and returns it,
1542 0 otherwise. */
1545 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1546 unsigned int expected_align, HOST_WIDE_INT expected_size,
1547 unsigned HOST_WIDE_INT min_size,
1548 unsigned HOST_WIDE_INT max_size,
1549 unsigned HOST_WIDE_INT probable_max_size)
1551 bool may_use_call;
1552 rtx retval = 0;
1553 unsigned int align;
1555 gcc_assert (size);
1556 if (CONST_INT_P (size) && INTVAL (size) == 0)
1557 return 0;
1559 switch (method)
1561 case BLOCK_OP_NORMAL:
1562 case BLOCK_OP_TAILCALL:
1563 may_use_call = true;
1564 break;
1566 case BLOCK_OP_CALL_PARM:
1567 may_use_call = block_move_libcall_safe_for_call_parm ();
1569 /* Make inhibit_defer_pop nonzero around the library call
1570 to force it to pop the arguments right away. */
1571 NO_DEFER_POP;
1572 break;
1574 case BLOCK_OP_NO_LIBCALL:
1575 may_use_call = false;
1576 break;
1578 default:
1579 gcc_unreachable ();
1582 gcc_assert (MEM_P (x) && MEM_P (y));
1583 align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1584 gcc_assert (align >= BITS_PER_UNIT);
1586 /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1587 block copy is more efficient for other large modes, e.g. DCmode. */
1588 x = adjust_address (x, BLKmode, 0);
1589 y = adjust_address (y, BLKmode, 0);
1591 /* Set MEM_SIZE as appropriate for this block copy. The main place this
1592 can be incorrect is coming from __builtin_memcpy. */
1593 if (CONST_INT_P (size))
1595 x = shallow_copy_rtx (x);
1596 y = shallow_copy_rtx (y);
1597 set_mem_size (x, INTVAL (size));
1598 set_mem_size (y, INTVAL (size));
1601 if (CONST_INT_P (size) && can_move_by_pieces (INTVAL (size), align))
1602 move_by_pieces (x, y, INTVAL (size), align, 0);
1603 else if (emit_block_move_via_movmem (x, y, size, align,
1604 expected_align, expected_size,
1605 min_size, max_size, probable_max_size))
1607 else if (may_use_call
1608 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1609 && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1611 /* Since x and y are passed to a libcall, mark the corresponding
1612 tree EXPR as addressable. */
1613 tree y_expr = MEM_EXPR (y);
1614 tree x_expr = MEM_EXPR (x);
1615 if (y_expr)
1616 mark_addressable (y_expr);
1617 if (x_expr)
1618 mark_addressable (x_expr);
1619 retval = emit_block_copy_via_libcall (x, y, size,
1620 method == BLOCK_OP_TAILCALL);
1623 else
1624 emit_block_move_via_loop (x, y, size, align);
1626 if (method == BLOCK_OP_CALL_PARM)
1627 OK_DEFER_POP;
1629 return retval;
1633 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1635 unsigned HOST_WIDE_INT max, min = 0;
1636 if (GET_CODE (size) == CONST_INT)
1637 min = max = UINTVAL (size);
1638 else
1639 max = GET_MODE_MASK (GET_MODE (size));
1640 return emit_block_move_hints (x, y, size, method, 0, -1,
1641 min, max, max);
1644 /* A subroutine of emit_block_move. Returns true if calling the
1645 block move libcall will not clobber any parameters which may have
1646 already been placed on the stack. */
1648 static bool
1649 block_move_libcall_safe_for_call_parm (void)
1651 #if defined (REG_PARM_STACK_SPACE)
1652 tree fn;
1653 #endif
1655 /* If arguments are pushed on the stack, then they're safe. */
1656 if (PUSH_ARGS)
1657 return true;
1659 /* If registers go on the stack anyway, any argument is sure to clobber
1660 an outgoing argument. */
1661 #if defined (REG_PARM_STACK_SPACE)
1662 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1663 /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1664 depend on its argument. */
1665 (void) fn;
1666 if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1667 && REG_PARM_STACK_SPACE (fn) != 0)
1668 return false;
1669 #endif
1671 /* If any argument goes in memory, then it might clobber an outgoing
1672 argument. */
1674 CUMULATIVE_ARGS args_so_far_v;
1675 cumulative_args_t args_so_far;
1676 tree fn, arg;
1678 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1679 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1680 args_so_far = pack_cumulative_args (&args_so_far_v);
1682 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1683 for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1685 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1686 rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1687 NULL_TREE, true);
1688 if (!tmp || !REG_P (tmp))
1689 return false;
1690 if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1691 return false;
1692 targetm.calls.function_arg_advance (args_so_far, mode,
1693 NULL_TREE, true);
1696 return true;
1699 /* A subroutine of emit_block_move. Expand a movmem pattern;
1700 return true if successful. */
1702 static bool
1703 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1704 unsigned int expected_align, HOST_WIDE_INT expected_size,
1705 unsigned HOST_WIDE_INT min_size,
1706 unsigned HOST_WIDE_INT max_size,
1707 unsigned HOST_WIDE_INT probable_max_size)
1709 int save_volatile_ok = volatile_ok;
1710 machine_mode mode;
1712 if (expected_align < align)
1713 expected_align = align;
1714 if (expected_size != -1)
1716 if ((unsigned HOST_WIDE_INT)expected_size > probable_max_size)
1717 expected_size = probable_max_size;
1718 if ((unsigned HOST_WIDE_INT)expected_size < min_size)
1719 expected_size = min_size;
1722 /* Since this is a move insn, we don't care about volatility. */
1723 volatile_ok = 1;
1725 /* Try the most limited insn first, because there's no point
1726 including more than one in the machine description unless
1727 the more limited one has some advantage. */
1729 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1730 mode = GET_MODE_WIDER_MODE (mode))
1732 enum insn_code code = direct_optab_handler (movmem_optab, mode);
1734 if (code != CODE_FOR_nothing
1735 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1736 here because if SIZE is less than the mode mask, as it is
1737 returned by the macro, it will definitely be less than the
1738 actual mode mask. Since SIZE is within the Pmode address
1739 space, we limit MODE to Pmode. */
1740 && ((CONST_INT_P (size)
1741 && ((unsigned HOST_WIDE_INT) INTVAL (size)
1742 <= (GET_MODE_MASK (mode) >> 1)))
1743 || max_size <= (GET_MODE_MASK (mode) >> 1)
1744 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
1746 struct expand_operand ops[9];
1747 unsigned int nops;
1749 /* ??? When called via emit_block_move_for_call, it'd be
1750 nice if there were some way to inform the backend, so
1751 that it doesn't fail the expansion because it thinks
1752 emitting the libcall would be more efficient. */
1753 nops = insn_data[(int) code].n_generator_args;
1754 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
1756 create_fixed_operand (&ops[0], x);
1757 create_fixed_operand (&ops[1], y);
1758 /* The check above guarantees that this size conversion is valid. */
1759 create_convert_operand_to (&ops[2], size, mode, true);
1760 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1761 if (nops >= 6)
1763 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1764 create_integer_operand (&ops[5], expected_size);
1766 if (nops >= 8)
1768 create_integer_operand (&ops[6], min_size);
1769 /* If we can not represent the maximal size,
1770 make parameter NULL. */
1771 if ((HOST_WIDE_INT) max_size != -1)
1772 create_integer_operand (&ops[7], max_size);
1773 else
1774 create_fixed_operand (&ops[7], NULL);
1776 if (nops == 9)
1778 /* If we can not represent the maximal size,
1779 make parameter NULL. */
1780 if ((HOST_WIDE_INT) probable_max_size != -1)
1781 create_integer_operand (&ops[8], probable_max_size);
1782 else
1783 create_fixed_operand (&ops[8], NULL);
1785 if (maybe_expand_insn (code, nops, ops))
1787 volatile_ok = save_volatile_ok;
1788 return true;
1793 volatile_ok = save_volatile_ok;
1794 return false;
1797 /* A subroutine of emit_block_move. Copy the data via an explicit
1798 loop. This is used only when libcalls are forbidden. */
1799 /* ??? It'd be nice to copy in hunks larger than QImode. */
1801 static void
1802 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1803 unsigned int align ATTRIBUTE_UNUSED)
1805 rtx_code_label *cmp_label, *top_label;
1806 rtx iter, x_addr, y_addr, tmp;
1807 machine_mode x_addr_mode = get_address_mode (x);
1808 machine_mode y_addr_mode = get_address_mode (y);
1809 machine_mode iter_mode;
1811 iter_mode = GET_MODE (size);
1812 if (iter_mode == VOIDmode)
1813 iter_mode = word_mode;
1815 top_label = gen_label_rtx ();
1816 cmp_label = gen_label_rtx ();
1817 iter = gen_reg_rtx (iter_mode);
1819 emit_move_insn (iter, const0_rtx);
1821 x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1822 y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1823 do_pending_stack_adjust ();
1825 emit_jump (cmp_label);
1826 emit_label (top_label);
1828 tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1829 x_addr = simplify_gen_binary (PLUS, x_addr_mode, x_addr, tmp);
1831 if (x_addr_mode != y_addr_mode)
1832 tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1833 y_addr = simplify_gen_binary (PLUS, y_addr_mode, y_addr, tmp);
1835 x = change_address (x, QImode, x_addr);
1836 y = change_address (y, QImode, y_addr);
1838 emit_move_insn (x, y);
1840 tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1841 true, OPTAB_LIB_WIDEN);
1842 if (tmp != iter)
1843 emit_move_insn (iter, tmp);
1845 emit_label (cmp_label);
1847 emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1848 true, top_label, REG_BR_PROB_BASE * 90 / 100);
1851 /* Expand a call to memcpy or memmove or memcmp, and return the result.
1852 TAILCALL is true if this is a tail call. */
1855 emit_block_op_via_libcall (enum built_in_function fncode, rtx dst, rtx src,
1856 rtx size, bool tailcall)
1858 rtx dst_addr, src_addr;
1859 tree call_expr, dst_tree, src_tree, size_tree;
1860 machine_mode size_mode;
1862 dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1863 dst_addr = convert_memory_address (ptr_mode, dst_addr);
1864 dst_tree = make_tree (ptr_type_node, dst_addr);
1866 src_addr = copy_addr_to_reg (XEXP (src, 0));
1867 src_addr = convert_memory_address (ptr_mode, src_addr);
1868 src_tree = make_tree (ptr_type_node, src_addr);
1870 size_mode = TYPE_MODE (sizetype);
1871 size = convert_to_mode (size_mode, size, 1);
1872 size = copy_to_mode_reg (size_mode, size);
1873 size_tree = make_tree (sizetype, size);
1875 /* It is incorrect to use the libcall calling conventions for calls to
1876 memcpy/memmove/memcmp because they can be provided by the user. */
1877 tree fn = builtin_decl_implicit (fncode);
1878 call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1879 CALL_EXPR_TAILCALL (call_expr) = tailcall;
1881 return expand_call (call_expr, NULL_RTX, false);
1884 /* Try to expand cmpstrn or cmpmem operation ICODE with the given operands.
1885 ARG3_TYPE is the type of ARG3_RTX. Return the result rtx on success,
1886 otherwise return null. */
1889 expand_cmpstrn_or_cmpmem (insn_code icode, rtx target, rtx arg1_rtx,
1890 rtx arg2_rtx, tree arg3_type, rtx arg3_rtx,
1891 HOST_WIDE_INT align)
1893 machine_mode insn_mode = insn_data[icode].operand[0].mode;
1895 if (target && (!REG_P (target) || HARD_REGISTER_P (target)))
1896 target = NULL_RTX;
1898 struct expand_operand ops[5];
1899 create_output_operand (&ops[0], target, insn_mode);
1900 create_fixed_operand (&ops[1], arg1_rtx);
1901 create_fixed_operand (&ops[2], arg2_rtx);
1902 create_convert_operand_from (&ops[3], arg3_rtx, TYPE_MODE (arg3_type),
1903 TYPE_UNSIGNED (arg3_type));
1904 create_integer_operand (&ops[4], align);
1905 if (maybe_expand_insn (icode, 5, ops))
1906 return ops[0].value;
1907 return NULL_RTX;
1910 /* Expand a block compare between X and Y with length LEN using the
1911 cmpmem optab, placing the result in TARGET. LEN_TYPE is the type
1912 of the expression that was used to calculate the length. ALIGN
1913 gives the known minimum common alignment. */
1915 static rtx
1916 emit_block_cmp_via_cmpmem (rtx x, rtx y, rtx len, tree len_type, rtx target,
1917 unsigned align)
1919 /* Note: The cmpstrnsi pattern, if it exists, is not suitable for
1920 implementing memcmp because it will stop if it encounters two
1921 zero bytes. */
1922 insn_code icode = direct_optab_handler (cmpmem_optab, SImode);
1924 if (icode == CODE_FOR_nothing)
1925 return NULL_RTX;
1927 return expand_cmpstrn_or_cmpmem (icode, target, x, y, len_type, len, align);
1930 /* Emit code to compare a block Y to a block X. This may be done with
1931 string-compare instructions, with multiple scalar instructions,
1932 or with a library call.
1934 Both X and Y must be MEM rtx's. LEN is an rtx that says how long
1935 they are. LEN_TYPE is the type of the expression that was used to
1936 calculate it.
1938 If EQUALITY_ONLY is true, it means we don't have to return the tri-state
1939 value of a normal memcmp call, instead we can just compare for equality.
1940 If FORCE_LIBCALL is true, we should emit a call to memcmp rather than
1941 returning NULL_RTX.
1943 Optionally, the caller can pass a constfn and associated data in Y_CFN
1944 and Y_CFN_DATA. describing that the second operand being compared is a
1945 known constant and how to obtain its data.
1946 Return the result of the comparison, or NULL_RTX if we failed to
1947 perform the operation. */
1950 emit_block_cmp_hints (rtx x, rtx y, rtx len, tree len_type, rtx target,
1951 bool equality_only, by_pieces_constfn y_cfn,
1952 void *y_cfndata)
1954 rtx result = 0;
1956 if (CONST_INT_P (len) && INTVAL (len) == 0)
1957 return const0_rtx;
1959 gcc_assert (MEM_P (x) && MEM_P (y));
1960 unsigned int align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1961 gcc_assert (align >= BITS_PER_UNIT);
1963 x = adjust_address (x, BLKmode, 0);
1964 y = adjust_address (y, BLKmode, 0);
1966 if (equality_only
1967 && CONST_INT_P (len)
1968 && can_do_by_pieces (INTVAL (len), align, COMPARE_BY_PIECES))
1969 result = compare_by_pieces (x, y, INTVAL (len), target, align,
1970 y_cfn, y_cfndata);
1971 else
1972 result = emit_block_cmp_via_cmpmem (x, y, len, len_type, target, align);
1974 return result;
1977 /* Copy all or part of a value X into registers starting at REGNO.
1978 The number of registers to be filled is NREGS. */
1980 void
1981 move_block_to_reg (int regno, rtx x, int nregs, machine_mode mode)
1983 if (nregs == 0)
1984 return;
1986 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1987 x = validize_mem (force_const_mem (mode, x));
1989 /* See if the machine can do this with a load multiple insn. */
1990 if (targetm.have_load_multiple ())
1992 rtx_insn *last = get_last_insn ();
1993 rtx first = gen_rtx_REG (word_mode, regno);
1994 if (rtx_insn *pat = targetm.gen_load_multiple (first, x,
1995 GEN_INT (nregs)))
1997 emit_insn (pat);
1998 return;
2000 else
2001 delete_insns_since (last);
2004 for (int i = 0; i < nregs; i++)
2005 emit_move_insn (gen_rtx_REG (word_mode, regno + i),
2006 operand_subword_force (x, i, mode));
2009 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
2010 The number of registers to be filled is NREGS. */
2012 void
2013 move_block_from_reg (int regno, rtx x, int nregs)
2015 if (nregs == 0)
2016 return;
2018 /* See if the machine can do this with a store multiple insn. */
2019 if (targetm.have_store_multiple ())
2021 rtx_insn *last = get_last_insn ();
2022 rtx first = gen_rtx_REG (word_mode, regno);
2023 if (rtx_insn *pat = targetm.gen_store_multiple (x, first,
2024 GEN_INT (nregs)))
2026 emit_insn (pat);
2027 return;
2029 else
2030 delete_insns_since (last);
2033 for (int i = 0; i < nregs; i++)
2035 rtx tem = operand_subword (x, i, 1, BLKmode);
2037 gcc_assert (tem);
2039 emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
2043 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
2044 ORIG, where ORIG is a non-consecutive group of registers represented by
2045 a PARALLEL. The clone is identical to the original except in that the
2046 original set of registers is replaced by a new set of pseudo registers.
2047 The new set has the same modes as the original set. */
2050 gen_group_rtx (rtx orig)
2052 int i, length;
2053 rtx *tmps;
2055 gcc_assert (GET_CODE (orig) == PARALLEL);
2057 length = XVECLEN (orig, 0);
2058 tmps = XALLOCAVEC (rtx, length);
2060 /* Skip a NULL entry in first slot. */
2061 i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
2063 if (i)
2064 tmps[0] = 0;
2066 for (; i < length; i++)
2068 machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
2069 rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
2071 tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
2074 return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
2077 /* A subroutine of emit_group_load. Arguments as for emit_group_load,
2078 except that values are placed in TMPS[i], and must later be moved
2079 into corresponding XEXP (XVECEXP (DST, 0, i), 0) element. */
2081 static void
2082 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
2084 rtx src;
2085 int start, i;
2086 machine_mode m = GET_MODE (orig_src);
2088 gcc_assert (GET_CODE (dst) == PARALLEL);
2090 if (m != VOIDmode
2091 && !SCALAR_INT_MODE_P (m)
2092 && !MEM_P (orig_src)
2093 && GET_CODE (orig_src) != CONCAT)
2095 machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
2096 if (imode == BLKmode)
2097 src = assign_stack_temp (GET_MODE (orig_src), ssize);
2098 else
2099 src = gen_reg_rtx (imode);
2100 if (imode != BLKmode)
2101 src = gen_lowpart (GET_MODE (orig_src), src);
2102 emit_move_insn (src, orig_src);
2103 /* ...and back again. */
2104 if (imode != BLKmode)
2105 src = gen_lowpart (imode, src);
2106 emit_group_load_1 (tmps, dst, src, type, ssize);
2107 return;
2110 /* Check for a NULL entry, used to indicate that the parameter goes
2111 both on the stack and in registers. */
2112 if (XEXP (XVECEXP (dst, 0, 0), 0))
2113 start = 0;
2114 else
2115 start = 1;
2117 /* Process the pieces. */
2118 for (i = start; i < XVECLEN (dst, 0); i++)
2120 machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
2121 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
2122 unsigned int bytelen = GET_MODE_SIZE (mode);
2123 int shift = 0;
2125 /* Handle trailing fragments that run over the size of the struct. */
2126 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2128 /* Arrange to shift the fragment to where it belongs.
2129 extract_bit_field loads to the lsb of the reg. */
2130 if (
2131 #ifdef BLOCK_REG_PADDING
2132 BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
2133 == (BYTES_BIG_ENDIAN ? upward : downward)
2134 #else
2135 BYTES_BIG_ENDIAN
2136 #endif
2138 shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2139 bytelen = ssize - bytepos;
2140 gcc_assert (bytelen > 0);
2143 /* If we won't be loading directly from memory, protect the real source
2144 from strange tricks we might play; but make sure that the source can
2145 be loaded directly into the destination. */
2146 src = orig_src;
2147 if (!MEM_P (orig_src)
2148 && (!CONSTANT_P (orig_src)
2149 || (GET_MODE (orig_src) != mode
2150 && GET_MODE (orig_src) != VOIDmode)))
2152 if (GET_MODE (orig_src) == VOIDmode)
2153 src = gen_reg_rtx (mode);
2154 else
2155 src = gen_reg_rtx (GET_MODE (orig_src));
2157 emit_move_insn (src, orig_src);
2160 /* Optimize the access just a bit. */
2161 if (MEM_P (src)
2162 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
2163 || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
2164 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2165 && bytelen == GET_MODE_SIZE (mode))
2167 tmps[i] = gen_reg_rtx (mode);
2168 emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
2170 else if (COMPLEX_MODE_P (mode)
2171 && GET_MODE (src) == mode
2172 && bytelen == GET_MODE_SIZE (mode))
2173 /* Let emit_move_complex do the bulk of the work. */
2174 tmps[i] = src;
2175 else if (GET_CODE (src) == CONCAT)
2177 unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
2178 unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
2179 unsigned int elt = bytepos / slen0;
2180 unsigned int subpos = bytepos % slen0;
2182 if (subpos + bytelen <= slen0)
2184 /* The following assumes that the concatenated objects all
2185 have the same size. In this case, a simple calculation
2186 can be used to determine the object and the bit field
2187 to be extracted. */
2188 tmps[i] = XEXP (src, elt);
2189 if (subpos != 0
2190 || subpos + bytelen != slen0
2191 || (!CONSTANT_P (tmps[i])
2192 && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode)))
2193 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
2194 subpos * BITS_PER_UNIT,
2195 1, NULL_RTX, mode, mode, false,
2196 NULL);
2198 else
2200 rtx mem;
2202 gcc_assert (!bytepos);
2203 mem = assign_stack_temp (GET_MODE (src), slen);
2204 emit_move_insn (mem, src);
2205 tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
2206 0, 1, NULL_RTX, mode, mode, false,
2207 NULL);
2210 /* FIXME: A SIMD parallel will eventually lead to a subreg of a
2211 SIMD register, which is currently broken. While we get GCC
2212 to emit proper RTL for these cases, let's dump to memory. */
2213 else if (VECTOR_MODE_P (GET_MODE (dst))
2214 && REG_P (src))
2216 int slen = GET_MODE_SIZE (GET_MODE (src));
2217 rtx mem;
2219 mem = assign_stack_temp (GET_MODE (src), slen);
2220 emit_move_insn (mem, src);
2221 tmps[i] = adjust_address (mem, mode, (int) bytepos);
2223 else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
2224 && XVECLEN (dst, 0) > 1)
2225 tmps[i] = simplify_gen_subreg (mode, src, GET_MODE (dst), bytepos);
2226 else if (CONSTANT_P (src))
2228 HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
2230 if (len == ssize)
2231 tmps[i] = src;
2232 else
2234 rtx first, second;
2236 /* TODO: const_wide_int can have sizes other than this... */
2237 gcc_assert (2 * len == ssize);
2238 split_double (src, &first, &second);
2239 if (i)
2240 tmps[i] = second;
2241 else
2242 tmps[i] = first;
2245 else if (REG_P (src) && GET_MODE (src) == mode)
2246 tmps[i] = src;
2247 else
2248 tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
2249 bytepos * BITS_PER_UNIT, 1, NULL_RTX,
2250 mode, mode, false, NULL);
2252 if (shift)
2253 tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
2254 shift, tmps[i], 0);
2258 /* Emit code to move a block SRC of type TYPE to a block DST,
2259 where DST is non-consecutive registers represented by a PARALLEL.
2260 SSIZE represents the total size of block ORIG_SRC in bytes, or -1
2261 if not known. */
2263 void
2264 emit_group_load (rtx dst, rtx src, tree type, int ssize)
2266 rtx *tmps;
2267 int i;
2269 tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
2270 emit_group_load_1 (tmps, dst, src, type, ssize);
2272 /* Copy the extracted pieces into the proper (probable) hard regs. */
2273 for (i = 0; i < XVECLEN (dst, 0); i++)
2275 rtx d = XEXP (XVECEXP (dst, 0, i), 0);
2276 if (d == NULL)
2277 continue;
2278 emit_move_insn (d, tmps[i]);
2282 /* Similar, but load SRC into new pseudos in a format that looks like
2283 PARALLEL. This can later be fed to emit_group_move to get things
2284 in the right place. */
2287 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
2289 rtvec vec;
2290 int i;
2292 vec = rtvec_alloc (XVECLEN (parallel, 0));
2293 emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
2295 /* Convert the vector to look just like the original PARALLEL, except
2296 with the computed values. */
2297 for (i = 0; i < XVECLEN (parallel, 0); i++)
2299 rtx e = XVECEXP (parallel, 0, i);
2300 rtx d = XEXP (e, 0);
2302 if (d)
2304 d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
2305 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
2307 RTVEC_ELT (vec, i) = e;
2310 return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
2313 /* Emit code to move a block SRC to block DST, where SRC and DST are
2314 non-consecutive groups of registers, each represented by a PARALLEL. */
2316 void
2317 emit_group_move (rtx dst, rtx src)
2319 int i;
2321 gcc_assert (GET_CODE (src) == PARALLEL
2322 && GET_CODE (dst) == PARALLEL
2323 && XVECLEN (src, 0) == XVECLEN (dst, 0));
2325 /* Skip first entry if NULL. */
2326 for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
2327 emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
2328 XEXP (XVECEXP (src, 0, i), 0));
2331 /* Move a group of registers represented by a PARALLEL into pseudos. */
2334 emit_group_move_into_temps (rtx src)
2336 rtvec vec = rtvec_alloc (XVECLEN (src, 0));
2337 int i;
2339 for (i = 0; i < XVECLEN (src, 0); i++)
2341 rtx e = XVECEXP (src, 0, i);
2342 rtx d = XEXP (e, 0);
2344 if (d)
2345 e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
2346 RTVEC_ELT (vec, i) = e;
2349 return gen_rtx_PARALLEL (GET_MODE (src), vec);
2352 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
2353 where SRC is non-consecutive registers represented by a PARALLEL.
2354 SSIZE represents the total size of block ORIG_DST, or -1 if not
2355 known. */
2357 void
2358 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
2360 rtx *tmps, dst;
2361 int start, finish, i;
2362 machine_mode m = GET_MODE (orig_dst);
2364 gcc_assert (GET_CODE (src) == PARALLEL);
2366 if (!SCALAR_INT_MODE_P (m)
2367 && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
2369 machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
2370 if (imode == BLKmode)
2371 dst = assign_stack_temp (GET_MODE (orig_dst), ssize);
2372 else
2373 dst = gen_reg_rtx (imode);
2374 emit_group_store (dst, src, type, ssize);
2375 if (imode != BLKmode)
2376 dst = gen_lowpart (GET_MODE (orig_dst), dst);
2377 emit_move_insn (orig_dst, dst);
2378 return;
2381 /* Check for a NULL entry, used to indicate that the parameter goes
2382 both on the stack and in registers. */
2383 if (XEXP (XVECEXP (src, 0, 0), 0))
2384 start = 0;
2385 else
2386 start = 1;
2387 finish = XVECLEN (src, 0);
2389 tmps = XALLOCAVEC (rtx, finish);
2391 /* Copy the (probable) hard regs into pseudos. */
2392 for (i = start; i < finish; i++)
2394 rtx reg = XEXP (XVECEXP (src, 0, i), 0);
2395 if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2397 tmps[i] = gen_reg_rtx (GET_MODE (reg));
2398 emit_move_insn (tmps[i], reg);
2400 else
2401 tmps[i] = reg;
2404 /* If we won't be storing directly into memory, protect the real destination
2405 from strange tricks we might play. */
2406 dst = orig_dst;
2407 if (GET_CODE (dst) == PARALLEL)
2409 rtx temp;
2411 /* We can get a PARALLEL dst if there is a conditional expression in
2412 a return statement. In that case, the dst and src are the same,
2413 so no action is necessary. */
2414 if (rtx_equal_p (dst, src))
2415 return;
2417 /* It is unclear if we can ever reach here, but we may as well handle
2418 it. Allocate a temporary, and split this into a store/load to/from
2419 the temporary. */
2420 temp = assign_stack_temp (GET_MODE (dst), ssize);
2421 emit_group_store (temp, src, type, ssize);
2422 emit_group_load (dst, temp, type, ssize);
2423 return;
2425 else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
2427 machine_mode outer = GET_MODE (dst);
2428 machine_mode inner;
2429 HOST_WIDE_INT bytepos;
2430 bool done = false;
2431 rtx temp;
2433 if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
2434 dst = gen_reg_rtx (outer);
2436 /* Make life a bit easier for combine. */
2437 /* If the first element of the vector is the low part
2438 of the destination mode, use a paradoxical subreg to
2439 initialize the destination. */
2440 if (start < finish)
2442 inner = GET_MODE (tmps[start]);
2443 bytepos = subreg_lowpart_offset (inner, outer);
2444 if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
2446 temp = simplify_gen_subreg (outer, tmps[start],
2447 inner, 0);
2448 if (temp)
2450 emit_move_insn (dst, temp);
2451 done = true;
2452 start++;
2457 /* If the first element wasn't the low part, try the last. */
2458 if (!done
2459 && start < finish - 1)
2461 inner = GET_MODE (tmps[finish - 1]);
2462 bytepos = subreg_lowpart_offset (inner, outer);
2463 if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
2465 temp = simplify_gen_subreg (outer, tmps[finish - 1],
2466 inner, 0);
2467 if (temp)
2469 emit_move_insn (dst, temp);
2470 done = true;
2471 finish--;
2476 /* Otherwise, simply initialize the result to zero. */
2477 if (!done)
2478 emit_move_insn (dst, CONST0_RTX (outer));
2481 /* Process the pieces. */
2482 for (i = start; i < finish; i++)
2484 HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
2485 machine_mode mode = GET_MODE (tmps[i]);
2486 unsigned int bytelen = GET_MODE_SIZE (mode);
2487 unsigned int adj_bytelen;
2488 rtx dest = dst;
2490 /* Handle trailing fragments that run over the size of the struct. */
2491 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2492 adj_bytelen = ssize - bytepos;
2493 else
2494 adj_bytelen = bytelen;
2496 if (GET_CODE (dst) == CONCAT)
2498 if (bytepos + adj_bytelen
2499 <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2500 dest = XEXP (dst, 0);
2501 else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2503 bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2504 dest = XEXP (dst, 1);
2506 else
2508 machine_mode dest_mode = GET_MODE (dest);
2509 machine_mode tmp_mode = GET_MODE (tmps[i]);
2511 gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2513 if (GET_MODE_ALIGNMENT (dest_mode)
2514 >= GET_MODE_ALIGNMENT (tmp_mode))
2516 dest = assign_stack_temp (dest_mode,
2517 GET_MODE_SIZE (dest_mode));
2518 emit_move_insn (adjust_address (dest,
2519 tmp_mode,
2520 bytepos),
2521 tmps[i]);
2522 dst = dest;
2524 else
2526 dest = assign_stack_temp (tmp_mode,
2527 GET_MODE_SIZE (tmp_mode));
2528 emit_move_insn (dest, tmps[i]);
2529 dst = adjust_address (dest, dest_mode, bytepos);
2531 break;
2535 /* Handle trailing fragments that run over the size of the struct. */
2536 if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2538 /* store_bit_field always takes its value from the lsb.
2539 Move the fragment to the lsb if it's not already there. */
2540 if (
2541 #ifdef BLOCK_REG_PADDING
2542 BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2543 == (BYTES_BIG_ENDIAN ? upward : downward)
2544 #else
2545 BYTES_BIG_ENDIAN
2546 #endif
2549 int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2550 tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2551 shift, tmps[i], 0);
2554 /* Make sure not to write past the end of the struct. */
2555 store_bit_field (dest,
2556 adj_bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2557 bytepos * BITS_PER_UNIT, ssize * BITS_PER_UNIT - 1,
2558 VOIDmode, tmps[i], false);
2561 /* Optimize the access just a bit. */
2562 else if (MEM_P (dest)
2563 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2564 || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2565 && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2566 && bytelen == GET_MODE_SIZE (mode))
2567 emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2569 else
2570 store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2571 0, 0, mode, tmps[i], false);
2574 /* Copy from the pseudo into the (probable) hard reg. */
2575 if (orig_dst != dst)
2576 emit_move_insn (orig_dst, dst);
2579 /* Return a form of X that does not use a PARALLEL. TYPE is the type
2580 of the value stored in X. */
2583 maybe_emit_group_store (rtx x, tree type)
2585 machine_mode mode = TYPE_MODE (type);
2586 gcc_checking_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
2587 if (GET_CODE (x) == PARALLEL)
2589 rtx result = gen_reg_rtx (mode);
2590 emit_group_store (result, x, type, int_size_in_bytes (type));
2591 return result;
2593 return x;
2596 /* Copy a BLKmode object of TYPE out of a register SRCREG into TARGET.
2598 This is used on targets that return BLKmode values in registers. */
2600 static void
2601 copy_blkmode_from_reg (rtx target, rtx srcreg, tree type)
2603 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2604 rtx src = NULL, dst = NULL;
2605 unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2606 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2607 machine_mode mode = GET_MODE (srcreg);
2608 machine_mode tmode = GET_MODE (target);
2609 machine_mode copy_mode;
2611 /* BLKmode registers created in the back-end shouldn't have survived. */
2612 gcc_assert (mode != BLKmode);
2614 /* If the structure doesn't take up a whole number of words, see whether
2615 SRCREG is padded on the left or on the right. If it's on the left,
2616 set PADDING_CORRECTION to the number of bits to skip.
2618 In most ABIs, the structure will be returned at the least end of
2619 the register, which translates to right padding on little-endian
2620 targets and left padding on big-endian targets. The opposite
2621 holds if the structure is returned at the most significant
2622 end of the register. */
2623 if (bytes % UNITS_PER_WORD != 0
2624 && (targetm.calls.return_in_msb (type)
2625 ? !BYTES_BIG_ENDIAN
2626 : BYTES_BIG_ENDIAN))
2627 padding_correction
2628 = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2630 /* We can use a single move if we have an exact mode for the size. */
2631 else if (MEM_P (target)
2632 && (!SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target))
2633 || MEM_ALIGN (target) >= GET_MODE_ALIGNMENT (mode))
2634 && bytes == GET_MODE_SIZE (mode))
2636 emit_move_insn (adjust_address (target, mode, 0), srcreg);
2637 return;
2640 /* And if we additionally have the same mode for a register. */
2641 else if (REG_P (target)
2642 && GET_MODE (target) == mode
2643 && bytes == GET_MODE_SIZE (mode))
2645 emit_move_insn (target, srcreg);
2646 return;
2649 /* This code assumes srcreg is at least a full word. If it isn't, copy it
2650 into a new pseudo which is a full word. */
2651 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
2653 srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2654 mode = word_mode;
2657 /* Copy the structure BITSIZE bits at a time. If the target lives in
2658 memory, take care of not reading/writing past its end by selecting
2659 a copy mode suited to BITSIZE. This should always be possible given
2660 how it is computed.
2662 If the target lives in register, make sure not to select a copy mode
2663 larger than the mode of the register.
2665 We could probably emit more efficient code for machines which do not use
2666 strict alignment, but it doesn't seem worth the effort at the current
2667 time. */
2669 copy_mode = word_mode;
2670 if (MEM_P (target))
2672 machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2673 if (mem_mode != BLKmode)
2674 copy_mode = mem_mode;
2676 else if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2677 copy_mode = tmode;
2679 for (bitpos = 0, xbitpos = padding_correction;
2680 bitpos < bytes * BITS_PER_UNIT;
2681 bitpos += bitsize, xbitpos += bitsize)
2683 /* We need a new source operand each time xbitpos is on a
2684 word boundary and when xbitpos == padding_correction
2685 (the first time through). */
2686 if (xbitpos % BITS_PER_WORD == 0 || xbitpos == padding_correction)
2687 src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD, mode);
2689 /* We need a new destination operand each time bitpos is on
2690 a word boundary. */
2691 if (REG_P (target) && GET_MODE_BITSIZE (tmode) < BITS_PER_WORD)
2692 dst = target;
2693 else if (bitpos % BITS_PER_WORD == 0)
2694 dst = operand_subword (target, bitpos / BITS_PER_WORD, 1, tmode);
2696 /* Use xbitpos for the source extraction (right justified) and
2697 bitpos for the destination store (left justified). */
2698 store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2699 extract_bit_field (src, bitsize,
2700 xbitpos % BITS_PER_WORD, 1,
2701 NULL_RTX, copy_mode, copy_mode,
2702 false, NULL),
2703 false);
2707 /* Copy BLKmode value SRC into a register of mode MODE. Return the
2708 register if it contains any data, otherwise return null.
2710 This is used on targets that return BLKmode values in registers. */
2713 copy_blkmode_to_reg (machine_mode mode, tree src)
2715 int i, n_regs;
2716 unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2717 unsigned int bitsize;
2718 rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2719 machine_mode dst_mode;
2721 gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2723 x = expand_normal (src);
2725 bytes = int_size_in_bytes (TREE_TYPE (src));
2726 if (bytes == 0)
2727 return NULL_RTX;
2729 /* If the structure doesn't take up a whole number of words, see
2730 whether the register value should be padded on the left or on
2731 the right. Set PADDING_CORRECTION to the number of padding
2732 bits needed on the left side.
2734 In most ABIs, the structure will be returned at the least end of
2735 the register, which translates to right padding on little-endian
2736 targets and left padding on big-endian targets. The opposite
2737 holds if the structure is returned at the most significant
2738 end of the register. */
2739 if (bytes % UNITS_PER_WORD != 0
2740 && (targetm.calls.return_in_msb (TREE_TYPE (src))
2741 ? !BYTES_BIG_ENDIAN
2742 : BYTES_BIG_ENDIAN))
2743 padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2744 * BITS_PER_UNIT));
2746 n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2747 dst_words = XALLOCAVEC (rtx, n_regs);
2748 bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2750 /* Copy the structure BITSIZE bits at a time. */
2751 for (bitpos = 0, xbitpos = padding_correction;
2752 bitpos < bytes * BITS_PER_UNIT;
2753 bitpos += bitsize, xbitpos += bitsize)
2755 /* We need a new destination pseudo each time xbitpos is
2756 on a word boundary and when xbitpos == padding_correction
2757 (the first time through). */
2758 if (xbitpos % BITS_PER_WORD == 0
2759 || xbitpos == padding_correction)
2761 /* Generate an appropriate register. */
2762 dst_word = gen_reg_rtx (word_mode);
2763 dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2765 /* Clear the destination before we move anything into it. */
2766 emit_move_insn (dst_word, CONST0_RTX (word_mode));
2769 /* We need a new source operand each time bitpos is on a word
2770 boundary. */
2771 if (bitpos % BITS_PER_WORD == 0)
2772 src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2774 /* Use bitpos for the source extraction (left justified) and
2775 xbitpos for the destination store (right justified). */
2776 store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2777 0, 0, word_mode,
2778 extract_bit_field (src_word, bitsize,
2779 bitpos % BITS_PER_WORD, 1,
2780 NULL_RTX, word_mode, word_mode,
2781 false, NULL),
2782 false);
2785 if (mode == BLKmode)
2787 /* Find the smallest integer mode large enough to hold the
2788 entire structure. */
2789 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2790 mode != VOIDmode;
2791 mode = GET_MODE_WIDER_MODE (mode))
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 (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3048 mode = GET_MODE_WIDER_MODE (mode))
3050 enum insn_code code = direct_optab_handler (setmem_optab, mode);
3052 if (code != CODE_FOR_nothing
3053 /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
3054 here because if SIZE is less than the mode mask, as it is
3055 returned by the macro, it will definitely be less than the
3056 actual mode mask. Since SIZE is within the Pmode address
3057 space, we limit MODE to Pmode. */
3058 && ((CONST_INT_P (size)
3059 && ((unsigned HOST_WIDE_INT) INTVAL (size)
3060 <= (GET_MODE_MASK (mode) >> 1)))
3061 || max_size <= (GET_MODE_MASK (mode) >> 1)
3062 || GET_MODE_BITSIZE (mode) >= GET_MODE_BITSIZE (Pmode)))
3064 struct expand_operand ops[9];
3065 unsigned int nops;
3067 nops = insn_data[(int) code].n_generator_args;
3068 gcc_assert (nops == 4 || nops == 6 || nops == 8 || nops == 9);
3070 create_fixed_operand (&ops[0], object);
3071 /* The check above guarantees that this size conversion is valid. */
3072 create_convert_operand_to (&ops[1], size, mode, true);
3073 create_convert_operand_from (&ops[2], val, byte_mode, true);
3074 create_integer_operand (&ops[3], align / BITS_PER_UNIT);
3075 if (nops >= 6)
3077 create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
3078 create_integer_operand (&ops[5], expected_size);
3080 if (nops >= 8)
3082 create_integer_operand (&ops[6], min_size);
3083 /* If we can not represent the maximal size,
3084 make parameter NULL. */
3085 if ((HOST_WIDE_INT) max_size != -1)
3086 create_integer_operand (&ops[7], max_size);
3087 else
3088 create_fixed_operand (&ops[7], NULL);
3090 if (nops == 9)
3092 /* If we can not represent the maximal size,
3093 make parameter NULL. */
3094 if ((HOST_WIDE_INT) probable_max_size != -1)
3095 create_integer_operand (&ops[8], probable_max_size);
3096 else
3097 create_fixed_operand (&ops[8], NULL);
3099 if (maybe_expand_insn (code, nops, ops))
3100 return true;
3104 return false;
3108 /* Write to one of the components of the complex value CPLX. Write VAL to
3109 the real part if IMAG_P is false, and the imaginary part if its true. */
3111 void
3112 write_complex_part (rtx cplx, rtx val, bool imag_p)
3114 machine_mode cmode;
3115 machine_mode imode;
3116 unsigned ibitsize;
3118 if (GET_CODE (cplx) == CONCAT)
3120 emit_move_insn (XEXP (cplx, imag_p), val);
3121 return;
3124 cmode = GET_MODE (cplx);
3125 imode = GET_MODE_INNER (cmode);
3126 ibitsize = GET_MODE_BITSIZE (imode);
3128 /* For MEMs simplify_gen_subreg may generate an invalid new address
3129 because, e.g., the original address is considered mode-dependent
3130 by the target, which restricts simplify_subreg from invoking
3131 adjust_address_nv. Instead of preparing fallback support for an
3132 invalid address, we call adjust_address_nv directly. */
3133 if (MEM_P (cplx))
3135 emit_move_insn (adjust_address_nv (cplx, imode,
3136 imag_p ? GET_MODE_SIZE (imode) : 0),
3137 val);
3138 return;
3141 /* If the sub-object is at least word sized, then we know that subregging
3142 will work. This special case is important, since store_bit_field
3143 wants to operate on integer modes, and there's rarely an OImode to
3144 correspond to TCmode. */
3145 if (ibitsize >= BITS_PER_WORD
3146 /* For hard regs we have exact predicates. Assume we can split
3147 the original object if it spans an even number of hard regs.
3148 This special case is important for SCmode on 64-bit platforms
3149 where the natural size of floating-point regs is 32-bit. */
3150 || (REG_P (cplx)
3151 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3152 && REG_NREGS (cplx) % 2 == 0))
3154 rtx part = simplify_gen_subreg (imode, cplx, cmode,
3155 imag_p ? GET_MODE_SIZE (imode) : 0);
3156 if (part)
3158 emit_move_insn (part, val);
3159 return;
3161 else
3162 /* simplify_gen_subreg may fail for sub-word MEMs. */
3163 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3166 store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val,
3167 false);
3170 /* Extract one of the components of the complex value CPLX. Extract the
3171 real part if IMAG_P is false, and the imaginary part if it's true. */
3174 read_complex_part (rtx cplx, bool imag_p)
3176 machine_mode cmode, imode;
3177 unsigned ibitsize;
3179 if (GET_CODE (cplx) == CONCAT)
3180 return XEXP (cplx, imag_p);
3182 cmode = GET_MODE (cplx);
3183 imode = GET_MODE_INNER (cmode);
3184 ibitsize = GET_MODE_BITSIZE (imode);
3186 /* Special case reads from complex constants that got spilled to memory. */
3187 if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
3189 tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
3190 if (decl && TREE_CODE (decl) == COMPLEX_CST)
3192 tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
3193 if (CONSTANT_CLASS_P (part))
3194 return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
3198 /* For MEMs simplify_gen_subreg may generate an invalid new address
3199 because, e.g., the original address is considered mode-dependent
3200 by the target, which restricts simplify_subreg from invoking
3201 adjust_address_nv. Instead of preparing fallback support for an
3202 invalid address, we call adjust_address_nv directly. */
3203 if (MEM_P (cplx))
3204 return adjust_address_nv (cplx, imode,
3205 imag_p ? GET_MODE_SIZE (imode) : 0);
3207 /* If the sub-object is at least word sized, then we know that subregging
3208 will work. This special case is important, since extract_bit_field
3209 wants to operate on integer modes, and there's rarely an OImode to
3210 correspond to TCmode. */
3211 if (ibitsize >= BITS_PER_WORD
3212 /* For hard regs we have exact predicates. Assume we can split
3213 the original object if it spans an even number of hard regs.
3214 This special case is important for SCmode on 64-bit platforms
3215 where the natural size of floating-point regs is 32-bit. */
3216 || (REG_P (cplx)
3217 && REGNO (cplx) < FIRST_PSEUDO_REGISTER
3218 && REG_NREGS (cplx) % 2 == 0))
3220 rtx ret = simplify_gen_subreg (imode, cplx, cmode,
3221 imag_p ? GET_MODE_SIZE (imode) : 0);
3222 if (ret)
3223 return ret;
3224 else
3225 /* simplify_gen_subreg may fail for sub-word MEMs. */
3226 gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
3229 return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
3230 true, NULL_RTX, imode, imode, false, NULL);
3233 /* A subroutine of emit_move_insn_1. Yet another lowpart generator.
3234 NEW_MODE and OLD_MODE are the same size. Return NULL if X cannot be
3235 represented in NEW_MODE. If FORCE is true, this will never happen, as
3236 we'll force-create a SUBREG if needed. */
3238 static rtx
3239 emit_move_change_mode (machine_mode new_mode,
3240 machine_mode old_mode, rtx x, bool force)
3242 rtx ret;
3244 if (push_operand (x, GET_MODE (x)))
3246 ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3247 MEM_COPY_ATTRIBUTES (ret, x);
3249 else if (MEM_P (x))
3251 /* We don't have to worry about changing the address since the
3252 size in bytes is supposed to be the same. */
3253 if (reload_in_progress)
3255 /* Copy the MEM to change the mode and move any
3256 substitutions from the old MEM to the new one. */
3257 ret = adjust_address_nv (x, new_mode, 0);
3258 copy_replacements (x, ret);
3260 else
3261 ret = adjust_address (x, new_mode, 0);
3263 else
3265 /* Note that we do want simplify_subreg's behavior of validating
3266 that the new mode is ok for a hard register. If we were to use
3267 simplify_gen_subreg, we would create the subreg, but would
3268 probably run into the target not being able to implement it. */
3269 /* Except, of course, when FORCE is true, when this is exactly what
3270 we want. Which is needed for CCmodes on some targets. */
3271 if (force)
3272 ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3273 else
3274 ret = simplify_subreg (new_mode, x, old_mode, 0);
3277 return ret;
3280 /* A subroutine of emit_move_insn_1. Generate a move from Y into X using
3281 an integer mode of the same size as MODE. Returns the instruction
3282 emitted, or NULL if such a move could not be generated. */
3284 static rtx_insn *
3285 emit_move_via_integer (machine_mode mode, rtx x, rtx y, bool force)
3287 machine_mode imode;
3288 enum insn_code code;
3290 /* There must exist a mode of the exact size we require. */
3291 imode = int_mode_for_mode (mode);
3292 if (imode == BLKmode)
3293 return NULL;
3295 /* The target must support moves in this mode. */
3296 code = optab_handler (mov_optab, imode);
3297 if (code == CODE_FOR_nothing)
3298 return NULL;
3300 x = emit_move_change_mode (imode, mode, x, force);
3301 if (x == NULL_RTX)
3302 return NULL;
3303 y = emit_move_change_mode (imode, mode, y, force);
3304 if (y == NULL_RTX)
3305 return NULL;
3306 return emit_insn (GEN_FCN (code) (x, y));
3309 /* A subroutine of emit_move_insn_1. X is a push_operand in MODE.
3310 Return an equivalent MEM that does not use an auto-increment. */
3313 emit_move_resolve_push (machine_mode mode, rtx x)
3315 enum rtx_code code = GET_CODE (XEXP (x, 0));
3316 HOST_WIDE_INT adjust;
3317 rtx temp;
3319 adjust = GET_MODE_SIZE (mode);
3320 #ifdef PUSH_ROUNDING
3321 adjust = PUSH_ROUNDING (adjust);
3322 #endif
3323 if (code == PRE_DEC || code == POST_DEC)
3324 adjust = -adjust;
3325 else if (code == PRE_MODIFY || code == POST_MODIFY)
3327 rtx expr = XEXP (XEXP (x, 0), 1);
3328 HOST_WIDE_INT val;
3330 gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3331 gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3332 val = INTVAL (XEXP (expr, 1));
3333 if (GET_CODE (expr) == MINUS)
3334 val = -val;
3335 gcc_assert (adjust == val || adjust == -val);
3336 adjust = val;
3339 /* Do not use anti_adjust_stack, since we don't want to update
3340 stack_pointer_delta. */
3341 temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3342 gen_int_mode (adjust, Pmode), stack_pointer_rtx,
3343 0, OPTAB_LIB_WIDEN);
3344 if (temp != stack_pointer_rtx)
3345 emit_move_insn (stack_pointer_rtx, temp);
3347 switch (code)
3349 case PRE_INC:
3350 case PRE_DEC:
3351 case PRE_MODIFY:
3352 temp = stack_pointer_rtx;
3353 break;
3354 case POST_INC:
3355 case POST_DEC:
3356 case POST_MODIFY:
3357 temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3358 break;
3359 default:
3360 gcc_unreachable ();
3363 return replace_equiv_address (x, temp);
3366 /* A subroutine of emit_move_complex. Generate a move from Y into X.
3367 X is known to satisfy push_operand, and MODE is known to be complex.
3368 Returns the last instruction emitted. */
3370 rtx_insn *
3371 emit_move_complex_push (machine_mode mode, rtx x, rtx y)
3373 machine_mode submode = GET_MODE_INNER (mode);
3374 bool imag_first;
3376 #ifdef PUSH_ROUNDING
3377 unsigned int submodesize = GET_MODE_SIZE (submode);
3379 /* In case we output to the stack, but the size is smaller than the
3380 machine can push exactly, we need to use move instructions. */
3381 if (PUSH_ROUNDING (submodesize) != submodesize)
3383 x = emit_move_resolve_push (mode, x);
3384 return emit_move_insn (x, y);
3386 #endif
3388 /* Note that the real part always precedes the imag part in memory
3389 regardless of machine's endianness. */
3390 switch (GET_CODE (XEXP (x, 0)))
3392 case PRE_DEC:
3393 case POST_DEC:
3394 imag_first = true;
3395 break;
3396 case PRE_INC:
3397 case POST_INC:
3398 imag_first = false;
3399 break;
3400 default:
3401 gcc_unreachable ();
3404 emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3405 read_complex_part (y, imag_first));
3406 return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3407 read_complex_part (y, !imag_first));
3410 /* A subroutine of emit_move_complex. Perform the move from Y to X
3411 via two moves of the parts. Returns the last instruction emitted. */
3413 rtx_insn *
3414 emit_move_complex_parts (rtx x, rtx y)
3416 /* Show the output dies here. This is necessary for SUBREGs
3417 of pseudos since we cannot track their lifetimes correctly;
3418 hard regs shouldn't appear here except as return values. */
3419 if (!reload_completed && !reload_in_progress
3420 && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3421 emit_clobber (x);
3423 write_complex_part (x, read_complex_part (y, false), false);
3424 write_complex_part (x, read_complex_part (y, true), true);
3426 return get_last_insn ();
3429 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3430 MODE is known to be complex. Returns the last instruction emitted. */
3432 static rtx_insn *
3433 emit_move_complex (machine_mode mode, rtx x, rtx y)
3435 bool try_int;
3437 /* Need to take special care for pushes, to maintain proper ordering
3438 of the data, and possibly extra padding. */
3439 if (push_operand (x, mode))
3440 return emit_move_complex_push (mode, x, y);
3442 /* See if we can coerce the target into moving both values at once, except
3443 for floating point where we favor moving as parts if this is easy. */
3444 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3445 && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing
3446 && !(REG_P (x)
3447 && HARD_REGISTER_P (x)
3448 && REG_NREGS (x) == 1)
3449 && !(REG_P (y)
3450 && HARD_REGISTER_P (y)
3451 && REG_NREGS (y) == 1))
3452 try_int = false;
3453 /* Not possible if the values are inherently not adjacent. */
3454 else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3455 try_int = false;
3456 /* Is possible if both are registers (or subregs of registers). */
3457 else if (register_operand (x, mode) && register_operand (y, mode))
3458 try_int = true;
3459 /* If one of the operands is a memory, and alignment constraints
3460 are friendly enough, we may be able to do combined memory operations.
3461 We do not attempt this if Y is a constant because that combination is
3462 usually better with the by-parts thing below. */
3463 else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3464 && (!STRICT_ALIGNMENT
3465 || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3466 try_int = true;
3467 else
3468 try_int = false;
3470 if (try_int)
3472 rtx_insn *ret;
3474 /* For memory to memory moves, optimal behavior can be had with the
3475 existing block move logic. */
3476 if (MEM_P (x) && MEM_P (y))
3478 emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3479 BLOCK_OP_NO_LIBCALL);
3480 return get_last_insn ();
3483 ret = emit_move_via_integer (mode, x, y, true);
3484 if (ret)
3485 return ret;
3488 return emit_move_complex_parts (x, y);
3491 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3492 MODE is known to be MODE_CC. Returns the last instruction emitted. */
3494 static rtx_insn *
3495 emit_move_ccmode (machine_mode mode, rtx x, rtx y)
3497 rtx_insn *ret;
3499 /* Assume all MODE_CC modes are equivalent; if we have movcc, use it. */
3500 if (mode != CCmode)
3502 enum insn_code code = optab_handler (mov_optab, CCmode);
3503 if (code != CODE_FOR_nothing)
3505 x = emit_move_change_mode (CCmode, mode, x, true);
3506 y = emit_move_change_mode (CCmode, mode, y, true);
3507 return emit_insn (GEN_FCN (code) (x, y));
3511 /* Otherwise, find the MODE_INT mode of the same width. */
3512 ret = emit_move_via_integer (mode, x, y, false);
3513 gcc_assert (ret != NULL);
3514 return ret;
3517 /* Return true if word I of OP lies entirely in the
3518 undefined bits of a paradoxical subreg. */
3520 static bool
3521 undefined_operand_subword_p (const_rtx op, int i)
3523 machine_mode innermode, innermostmode;
3524 int offset;
3525 if (GET_CODE (op) != SUBREG)
3526 return false;
3527 innermode = GET_MODE (op);
3528 innermostmode = GET_MODE (SUBREG_REG (op));
3529 offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3530 /* The SUBREG_BYTE represents offset, as if the value were stored in
3531 memory, except for a paradoxical subreg where we define
3532 SUBREG_BYTE to be 0; undo this exception as in
3533 simplify_subreg. */
3534 if (SUBREG_BYTE (op) == 0
3535 && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3537 int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3538 if (WORDS_BIG_ENDIAN)
3539 offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3540 if (BYTES_BIG_ENDIAN)
3541 offset += difference % UNITS_PER_WORD;
3543 if (offset >= GET_MODE_SIZE (innermostmode)
3544 || offset <= -GET_MODE_SIZE (word_mode))
3545 return true;
3546 return false;
3549 /* A subroutine of emit_move_insn_1. Generate a move from Y into X.
3550 MODE is any multi-word or full-word mode that lacks a move_insn
3551 pattern. Note that you will get better code if you define such
3552 patterns, even if they must turn into multiple assembler instructions. */
3554 static rtx_insn *
3555 emit_move_multi_word (machine_mode mode, rtx x, rtx y)
3557 rtx_insn *last_insn = 0;
3558 rtx_insn *seq;
3559 rtx inner;
3560 bool need_clobber;
3561 int i;
3563 gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3565 /* If X is a push on the stack, do the push now and replace
3566 X with a reference to the stack pointer. */
3567 if (push_operand (x, mode))
3568 x = emit_move_resolve_push (mode, x);
3570 /* If we are in reload, see if either operand is a MEM whose address
3571 is scheduled for replacement. */
3572 if (reload_in_progress && MEM_P (x)
3573 && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3574 x = replace_equiv_address_nv (x, inner);
3575 if (reload_in_progress && MEM_P (y)
3576 && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3577 y = replace_equiv_address_nv (y, inner);
3579 start_sequence ();
3581 need_clobber = false;
3582 for (i = 0;
3583 i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3584 i++)
3586 rtx xpart = operand_subword (x, i, 1, mode);
3587 rtx ypart;
3589 /* Do not generate code for a move if it would come entirely
3590 from the undefined bits of a paradoxical subreg. */
3591 if (undefined_operand_subword_p (y, i))
3592 continue;
3594 ypart = operand_subword (y, i, 1, mode);
3596 /* If we can't get a part of Y, put Y into memory if it is a
3597 constant. Otherwise, force it into a register. Then we must
3598 be able to get a part of Y. */
3599 if (ypart == 0 && CONSTANT_P (y))
3601 y = use_anchored_address (force_const_mem (mode, y));
3602 ypart = operand_subword (y, i, 1, mode);
3604 else if (ypart == 0)
3605 ypart = operand_subword_force (y, i, mode);
3607 gcc_assert (xpart && ypart);
3609 need_clobber |= (GET_CODE (xpart) == SUBREG);
3611 last_insn = emit_move_insn (xpart, ypart);
3614 seq = get_insns ();
3615 end_sequence ();
3617 /* Show the output dies here. This is necessary for SUBREGs
3618 of pseudos since we cannot track their lifetimes correctly;
3619 hard regs shouldn't appear here except as return values.
3620 We never want to emit such a clobber after reload. */
3621 if (x != y
3622 && ! (reload_in_progress || reload_completed)
3623 && need_clobber != 0)
3624 emit_clobber (x);
3626 emit_insn (seq);
3628 return last_insn;
3631 /* Low level part of emit_move_insn.
3632 Called just like emit_move_insn, but assumes X and Y
3633 are basically valid. */
3635 rtx_insn *
3636 emit_move_insn_1 (rtx x, rtx y)
3638 machine_mode mode = GET_MODE (x);
3639 enum insn_code code;
3641 gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3643 code = optab_handler (mov_optab, mode);
3644 if (code != CODE_FOR_nothing)
3645 return emit_insn (GEN_FCN (code) (x, y));
3647 /* Expand complex moves by moving real part and imag part. */
3648 if (COMPLEX_MODE_P (mode))
3649 return emit_move_complex (mode, x, y);
3651 if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3652 || ALL_FIXED_POINT_MODE_P (mode))
3654 rtx_insn *result = emit_move_via_integer (mode, x, y, true);
3656 /* If we can't find an integer mode, use multi words. */
3657 if (result)
3658 return result;
3659 else
3660 return emit_move_multi_word (mode, x, y);
3663 if (GET_MODE_CLASS (mode) == MODE_CC)
3664 return emit_move_ccmode (mode, x, y);
3666 /* Try using a move pattern for the corresponding integer mode. This is
3667 only safe when simplify_subreg can convert MODE constants into integer
3668 constants. At present, it can only do this reliably if the value
3669 fits within a HOST_WIDE_INT. */
3670 if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3672 rtx_insn *ret = emit_move_via_integer (mode, x, y, lra_in_progress);
3674 if (ret)
3676 if (! lra_in_progress || recog (PATTERN (ret), ret, 0) >= 0)
3677 return ret;
3681 return emit_move_multi_word (mode, x, y);
3684 /* Generate code to copy Y into X.
3685 Both Y and X must have the same mode, except that
3686 Y can be a constant with VOIDmode.
3687 This mode cannot be BLKmode; use emit_block_move for that.
3689 Return the last instruction emitted. */
3691 rtx_insn *
3692 emit_move_insn (rtx x, rtx y)
3694 machine_mode mode = GET_MODE (x);
3695 rtx y_cst = NULL_RTX;
3696 rtx_insn *last_insn;
3697 rtx set;
3699 gcc_assert (mode != BLKmode
3700 && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3702 if (CONSTANT_P (y))
3704 if (optimize
3705 && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3706 && (last_insn = compress_float_constant (x, y)))
3707 return last_insn;
3709 y_cst = y;
3711 if (!targetm.legitimate_constant_p (mode, y))
3713 y = force_const_mem (mode, y);
3715 /* If the target's cannot_force_const_mem prevented the spill,
3716 assume that the target's move expanders will also take care
3717 of the non-legitimate constant. */
3718 if (!y)
3719 y = y_cst;
3720 else
3721 y = use_anchored_address (y);
3725 /* If X or Y are memory references, verify that their addresses are valid
3726 for the machine. */
3727 if (MEM_P (x)
3728 && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3729 MEM_ADDR_SPACE (x))
3730 && ! push_operand (x, GET_MODE (x))))
3731 x = validize_mem (x);
3733 if (MEM_P (y)
3734 && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3735 MEM_ADDR_SPACE (y)))
3736 y = validize_mem (y);
3738 gcc_assert (mode != BLKmode);
3740 last_insn = emit_move_insn_1 (x, y);
3742 if (y_cst && REG_P (x)
3743 && (set = single_set (last_insn)) != NULL_RTX
3744 && SET_DEST (set) == x
3745 && ! rtx_equal_p (y_cst, SET_SRC (set)))
3746 set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3748 return last_insn;
3751 /* Generate the body of an instruction to copy Y into X.
3752 It may be a list of insns, if one insn isn't enough. */
3754 rtx_insn *
3755 gen_move_insn (rtx x, rtx y)
3757 rtx_insn *seq;
3759 start_sequence ();
3760 emit_move_insn_1 (x, y);
3761 seq = get_insns ();
3762 end_sequence ();
3763 return seq;
3766 /* If Y is representable exactly in a narrower mode, and the target can
3767 perform the extension directly from constant or memory, then emit the
3768 move as an extension. */
3770 static rtx_insn *
3771 compress_float_constant (rtx x, rtx y)
3773 machine_mode dstmode = GET_MODE (x);
3774 machine_mode orig_srcmode = GET_MODE (y);
3775 machine_mode srcmode;
3776 const REAL_VALUE_TYPE *r;
3777 int oldcost, newcost;
3778 bool speed = optimize_insn_for_speed_p ();
3780 r = CONST_DOUBLE_REAL_VALUE (y);
3782 if (targetm.legitimate_constant_p (dstmode, y))
3783 oldcost = set_src_cost (y, orig_srcmode, speed);
3784 else
3785 oldcost = set_src_cost (force_const_mem (dstmode, y), dstmode, speed);
3787 for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3788 srcmode != orig_srcmode;
3789 srcmode = GET_MODE_WIDER_MODE (srcmode))
3791 enum insn_code ic;
3792 rtx trunc_y;
3793 rtx_insn *last_insn;
3795 /* Skip if the target can't extend this way. */
3796 ic = can_extend_p (dstmode, srcmode, 0);
3797 if (ic == CODE_FOR_nothing)
3798 continue;
3800 /* Skip if the narrowed value isn't exact. */
3801 if (! exact_real_truncate (srcmode, r))
3802 continue;
3804 trunc_y = const_double_from_real_value (*r, srcmode);
3806 if (targetm.legitimate_constant_p (srcmode, trunc_y))
3808 /* Skip if the target needs extra instructions to perform
3809 the extension. */
3810 if (!insn_operand_matches (ic, 1, trunc_y))
3811 continue;
3812 /* This is valid, but may not be cheaper than the original. */
3813 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3814 dstmode, speed);
3815 if (oldcost < newcost)
3816 continue;
3818 else if (float_extend_from_mem[dstmode][srcmode])
3820 trunc_y = force_const_mem (srcmode, trunc_y);
3821 /* This is valid, but may not be cheaper than the original. */
3822 newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3823 dstmode, speed);
3824 if (oldcost < newcost)
3825 continue;
3826 trunc_y = validize_mem (trunc_y);
3828 else
3829 continue;
3831 /* For CSE's benefit, force the compressed constant pool entry
3832 into a new pseudo. This constant may be used in different modes,
3833 and if not, combine will put things back together for us. */
3834 trunc_y = force_reg (srcmode, trunc_y);
3836 /* If x is a hard register, perform the extension into a pseudo,
3837 so that e.g. stack realignment code is aware of it. */
3838 rtx target = x;
3839 if (REG_P (x) && HARD_REGISTER_P (x))
3840 target = gen_reg_rtx (dstmode);
3842 emit_unop_insn (ic, target, trunc_y, UNKNOWN);
3843 last_insn = get_last_insn ();
3845 if (REG_P (target))
3846 set_unique_reg_note (last_insn, REG_EQUAL, y);
3848 if (target != x)
3849 return emit_move_insn (x, target);
3850 return last_insn;
3853 return NULL;
3856 /* Pushing data onto the stack. */
3858 /* Push a block of length SIZE (perhaps variable)
3859 and return an rtx to address the beginning of the block.
3860 The value may be virtual_outgoing_args_rtx.
3862 EXTRA is the number of bytes of padding to push in addition to SIZE.
3863 BELOW nonzero means this padding comes at low addresses;
3864 otherwise, the padding comes at high addresses. */
3867 push_block (rtx size, int extra, int below)
3869 rtx temp;
3871 size = convert_modes (Pmode, ptr_mode, size, 1);
3872 if (CONSTANT_P (size))
3873 anti_adjust_stack (plus_constant (Pmode, size, extra));
3874 else if (REG_P (size) && extra == 0)
3875 anti_adjust_stack (size);
3876 else
3878 temp = copy_to_mode_reg (Pmode, size);
3879 if (extra != 0)
3880 temp = expand_binop (Pmode, add_optab, temp,
3881 gen_int_mode (extra, Pmode),
3882 temp, 0, OPTAB_LIB_WIDEN);
3883 anti_adjust_stack (temp);
3886 if (STACK_GROWS_DOWNWARD)
3888 temp = virtual_outgoing_args_rtx;
3889 if (extra != 0 && below)
3890 temp = plus_constant (Pmode, temp, extra);
3892 else
3894 if (CONST_INT_P (size))
3895 temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3896 -INTVAL (size) - (below ? 0 : extra));
3897 else if (extra != 0 && !below)
3898 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3899 negate_rtx (Pmode, plus_constant (Pmode, size,
3900 extra)));
3901 else
3902 temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3903 negate_rtx (Pmode, size));
3906 return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3909 /* A utility routine that returns the base of an auto-inc memory, or NULL. */
3911 static rtx
3912 mem_autoinc_base (rtx mem)
3914 if (MEM_P (mem))
3916 rtx addr = XEXP (mem, 0);
3917 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3918 return XEXP (addr, 0);
3920 return NULL;
3923 /* A utility routine used here, in reload, and in try_split. The insns
3924 after PREV up to and including LAST are known to adjust the stack,
3925 with a final value of END_ARGS_SIZE. Iterate backward from LAST
3926 placing notes as appropriate. PREV may be NULL, indicating the
3927 entire insn sequence prior to LAST should be scanned.
3929 The set of allowed stack pointer modifications is small:
3930 (1) One or more auto-inc style memory references (aka pushes),
3931 (2) One or more addition/subtraction with the SP as destination,
3932 (3) A single move insn with the SP as destination,
3933 (4) A call_pop insn,
3934 (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3936 Insns in the sequence that do not modify the SP are ignored,
3937 except for noreturn calls.
3939 The return value is the amount of adjustment that can be trivially
3940 verified, via immediate operand or auto-inc. If the adjustment
3941 cannot be trivially extracted, the return value is INT_MIN. */
3943 HOST_WIDE_INT
3944 find_args_size_adjust (rtx_insn *insn)
3946 rtx dest, set, pat;
3947 int i;
3949 pat = PATTERN (insn);
3950 set = NULL;
3952 /* Look for a call_pop pattern. */
3953 if (CALL_P (insn))
3955 /* We have to allow non-call_pop patterns for the case
3956 of emit_single_push_insn of a TLS address. */
3957 if (GET_CODE (pat) != PARALLEL)
3958 return 0;
3960 /* All call_pop have a stack pointer adjust in the parallel.
3961 The call itself is always first, and the stack adjust is
3962 usually last, so search from the end. */
3963 for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3965 set = XVECEXP (pat, 0, i);
3966 if (GET_CODE (set) != SET)
3967 continue;
3968 dest = SET_DEST (set);
3969 if (dest == stack_pointer_rtx)
3970 break;
3972 /* We'd better have found the stack pointer adjust. */
3973 if (i == 0)
3974 return 0;
3975 /* Fall through to process the extracted SET and DEST
3976 as if it was a standalone insn. */
3978 else if (GET_CODE (pat) == SET)
3979 set = pat;
3980 else if ((set = single_set (insn)) != NULL)
3982 else if (GET_CODE (pat) == PARALLEL)
3984 /* ??? Some older ports use a parallel with a stack adjust
3985 and a store for a PUSH_ROUNDING pattern, rather than a
3986 PRE/POST_MODIFY rtx. Don't force them to update yet... */
3987 /* ??? See h8300 and m68k, pushqi1. */
3988 for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3990 set = XVECEXP (pat, 0, i);
3991 if (GET_CODE (set) != SET)
3992 continue;
3993 dest = SET_DEST (set);
3994 if (dest == stack_pointer_rtx)
3995 break;
3997 /* We do not expect an auto-inc of the sp in the parallel. */
3998 gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3999 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4000 != stack_pointer_rtx);
4002 if (i < 0)
4003 return 0;
4005 else
4006 return 0;
4008 dest = SET_DEST (set);
4010 /* Look for direct modifications of the stack pointer. */
4011 if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
4013 /* Look for a trivial adjustment, otherwise assume nothing. */
4014 /* Note that the SPU restore_stack_block pattern refers to
4015 the stack pointer in V4SImode. Consider that non-trivial. */
4016 if (SCALAR_INT_MODE_P (GET_MODE (dest))
4017 && GET_CODE (SET_SRC (set)) == PLUS
4018 && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
4019 && CONST_INT_P (XEXP (SET_SRC (set), 1)))
4020 return INTVAL (XEXP (SET_SRC (set), 1));
4021 /* ??? Reload can generate no-op moves, which will be cleaned
4022 up later. Recognize it and continue searching. */
4023 else if (rtx_equal_p (dest, SET_SRC (set)))
4024 return 0;
4025 else
4026 return HOST_WIDE_INT_MIN;
4028 else
4030 rtx mem, addr;
4032 /* Otherwise only think about autoinc patterns. */
4033 if (mem_autoinc_base (dest) == stack_pointer_rtx)
4035 mem = dest;
4036 gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
4037 != stack_pointer_rtx);
4039 else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
4040 mem = SET_SRC (set);
4041 else
4042 return 0;
4044 addr = XEXP (mem, 0);
4045 switch (GET_CODE (addr))
4047 case PRE_INC:
4048 case POST_INC:
4049 return GET_MODE_SIZE (GET_MODE (mem));
4050 case PRE_DEC:
4051 case POST_DEC:
4052 return -GET_MODE_SIZE (GET_MODE (mem));
4053 case PRE_MODIFY:
4054 case POST_MODIFY:
4055 addr = XEXP (addr, 1);
4056 gcc_assert (GET_CODE (addr) == PLUS);
4057 gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
4058 gcc_assert (CONST_INT_P (XEXP (addr, 1)));
4059 return INTVAL (XEXP (addr, 1));
4060 default:
4061 gcc_unreachable ();
4067 fixup_args_size_notes (rtx_insn *prev, rtx_insn *last, int end_args_size)
4069 int args_size = end_args_size;
4070 bool saw_unknown = false;
4071 rtx_insn *insn;
4073 for (insn = last; insn != prev; insn = PREV_INSN (insn))
4075 HOST_WIDE_INT this_delta;
4077 if (!NONDEBUG_INSN_P (insn))
4078 continue;
4080 this_delta = find_args_size_adjust (insn);
4081 if (this_delta == 0)
4083 if (!CALL_P (insn)
4084 || ACCUMULATE_OUTGOING_ARGS
4085 || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
4086 continue;
4089 gcc_assert (!saw_unknown);
4090 if (this_delta == HOST_WIDE_INT_MIN)
4091 saw_unknown = true;
4093 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
4094 if (STACK_GROWS_DOWNWARD)
4095 this_delta = -(unsigned HOST_WIDE_INT) this_delta;
4097 args_size -= this_delta;
4100 return saw_unknown ? INT_MIN : args_size;
4103 #ifdef PUSH_ROUNDING
4104 /* Emit single push insn. */
4106 static void
4107 emit_single_push_insn_1 (machine_mode mode, rtx x, tree type)
4109 rtx dest_addr;
4110 unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
4111 rtx dest;
4112 enum insn_code icode;
4114 stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
4115 /* If there is push pattern, use it. Otherwise try old way of throwing
4116 MEM representing push operation to move expander. */
4117 icode = optab_handler (push_optab, mode);
4118 if (icode != CODE_FOR_nothing)
4120 struct expand_operand ops[1];
4122 create_input_operand (&ops[0], x, mode);
4123 if (maybe_expand_insn (icode, 1, ops))
4124 return;
4126 if (GET_MODE_SIZE (mode) == rounded_size)
4127 dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
4128 /* If we are to pad downward, adjust the stack pointer first and
4129 then store X into the stack location using an offset. This is
4130 because emit_move_insn does not know how to pad; it does not have
4131 access to type. */
4132 else if (FUNCTION_ARG_PADDING (mode, type) == downward)
4134 unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
4135 HOST_WIDE_INT offset;
4137 emit_move_insn (stack_pointer_rtx,
4138 expand_binop (Pmode,
4139 STACK_GROWS_DOWNWARD ? sub_optab
4140 : add_optab,
4141 stack_pointer_rtx,
4142 gen_int_mode (rounded_size, Pmode),
4143 NULL_RTX, 0, OPTAB_LIB_WIDEN));
4145 offset = (HOST_WIDE_INT) padding_size;
4146 if (STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_DEC)
4147 /* We have already decremented the stack pointer, so get the
4148 previous value. */
4149 offset += (HOST_WIDE_INT) rounded_size;
4151 if (!STACK_GROWS_DOWNWARD && STACK_PUSH_CODE == POST_INC)
4152 /* We have already incremented the stack pointer, so get the
4153 previous value. */
4154 offset -= (HOST_WIDE_INT) rounded_size;
4156 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4157 gen_int_mode (offset, Pmode));
4159 else
4161 if (STACK_GROWS_DOWNWARD)
4162 /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC. */
4163 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4164 gen_int_mode (-(HOST_WIDE_INT) rounded_size,
4165 Pmode));
4166 else
4167 /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC. */
4168 dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4169 gen_int_mode (rounded_size, Pmode));
4171 dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
4174 dest = gen_rtx_MEM (mode, dest_addr);
4176 if (type != 0)
4178 set_mem_attributes (dest, type, 1);
4180 if (cfun->tail_call_marked)
4181 /* Function incoming arguments may overlap with sibling call
4182 outgoing arguments and we cannot allow reordering of reads
4183 from function arguments with stores to outgoing arguments
4184 of sibling calls. */
4185 set_mem_alias_set (dest, 0);
4187 emit_move_insn (dest, x);
4190 /* Emit and annotate a single push insn. */
4192 static void
4193 emit_single_push_insn (machine_mode mode, rtx x, tree type)
4195 int delta, old_delta = stack_pointer_delta;
4196 rtx_insn *prev = get_last_insn ();
4197 rtx_insn *last;
4199 emit_single_push_insn_1 (mode, x, type);
4201 last = get_last_insn ();
4203 /* Notice the common case where we emitted exactly one insn. */
4204 if (PREV_INSN (last) == prev)
4206 add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
4207 return;
4210 delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
4211 gcc_assert (delta == INT_MIN || delta == old_delta);
4213 #endif
4215 /* If reading SIZE bytes from X will end up reading from
4216 Y return the number of bytes that overlap. Return -1
4217 if there is no overlap or -2 if we can't determine
4218 (for example when X and Y have different base registers). */
4220 static int
4221 memory_load_overlap (rtx x, rtx y, HOST_WIDE_INT size)
4223 rtx tmp = plus_constant (Pmode, x, size);
4224 rtx sub = simplify_gen_binary (MINUS, Pmode, tmp, y);
4226 if (!CONST_INT_P (sub))
4227 return -2;
4229 HOST_WIDE_INT val = INTVAL (sub);
4231 return IN_RANGE (val, 1, size) ? val : -1;
4234 /* Generate code to push X onto the stack, assuming it has mode MODE and
4235 type TYPE.
4236 MODE is redundant except when X is a CONST_INT (since they don't
4237 carry mode info).
4238 SIZE is an rtx for the size of data to be copied (in bytes),
4239 needed only if X is BLKmode.
4240 Return true if successful. May return false if asked to push a
4241 partial argument during a sibcall optimization (as specified by
4242 SIBCALL_P) and the incoming and outgoing pointers cannot be shown
4243 to not overlap.
4245 ALIGN (in bits) is maximum alignment we can assume.
4247 If PARTIAL and REG are both nonzero, then copy that many of the first
4248 bytes of X into registers starting with REG, and push the rest of X.
4249 The amount of space pushed is decreased by PARTIAL bytes.
4250 REG must be a hard register in this case.
4251 If REG is zero but PARTIAL is not, take any all others actions for an
4252 argument partially in registers, but do not actually load any
4253 registers.
4255 EXTRA is the amount in bytes of extra space to leave next to this arg.
4256 This is ignored if an argument block has already been allocated.
4258 On a machine that lacks real push insns, ARGS_ADDR is the address of
4259 the bottom of the argument block for this call. We use indexing off there
4260 to store the arg. On machines with push insns, ARGS_ADDR is 0 when a
4261 argument block has not been preallocated.
4263 ARGS_SO_FAR is the size of args previously pushed for this call.
4265 REG_PARM_STACK_SPACE is nonzero if functions require stack space
4266 for arguments passed in registers. If nonzero, it will be the number
4267 of bytes required. */
4269 bool
4270 emit_push_insn (rtx x, machine_mode mode, tree type, rtx size,
4271 unsigned int align, int partial, rtx reg, int extra,
4272 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
4273 rtx alignment_pad, bool sibcall_p)
4275 rtx xinner;
4276 enum direction stack_direction = STACK_GROWS_DOWNWARD ? downward : upward;
4278 /* Decide where to pad the argument: `downward' for below,
4279 `upward' for above, or `none' for don't pad it.
4280 Default is below for small data on big-endian machines; else above. */
4281 enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
4283 /* Invert direction if stack is post-decrement.
4284 FIXME: why? */
4285 if (STACK_PUSH_CODE == POST_DEC)
4286 if (where_pad != none)
4287 where_pad = (where_pad == downward ? upward : downward);
4289 xinner = x;
4291 int nregs = partial / UNITS_PER_WORD;
4292 rtx *tmp_regs = NULL;
4293 int overlapping = 0;
4295 if (mode == BLKmode
4296 || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4298 /* Copy a block into the stack, entirely or partially. */
4300 rtx temp;
4301 int used;
4302 int offset;
4303 int skip;
4305 offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4306 used = partial - offset;
4308 if (mode != BLKmode)
4310 /* A value is to be stored in an insufficiently aligned
4311 stack slot; copy via a suitably aligned slot if
4312 necessary. */
4313 size = GEN_INT (GET_MODE_SIZE (mode));
4314 if (!MEM_P (xinner))
4316 temp = assign_temp (type, 1, 1);
4317 emit_move_insn (temp, xinner);
4318 xinner = temp;
4322 gcc_assert (size);
4324 /* USED is now the # of bytes we need not copy to the stack
4325 because registers will take care of them. */
4327 if (partial != 0)
4328 xinner = adjust_address (xinner, BLKmode, used);
4330 /* If the partial register-part of the arg counts in its stack size,
4331 skip the part of stack space corresponding to the registers.
4332 Otherwise, start copying to the beginning of the stack space,
4333 by setting SKIP to 0. */
4334 skip = (reg_parm_stack_space == 0) ? 0 : used;
4336 #ifdef PUSH_ROUNDING
4337 /* Do it with several push insns if that doesn't take lots of insns
4338 and if there is no difficulty with push insns that skip bytes
4339 on the stack for alignment purposes. */
4340 if (args_addr == 0
4341 && PUSH_ARGS
4342 && CONST_INT_P (size)
4343 && skip == 0
4344 && MEM_ALIGN (xinner) >= align
4345 && can_move_by_pieces ((unsigned) INTVAL (size) - used, align)
4346 /* Here we avoid the case of a structure whose weak alignment
4347 forces many pushes of a small amount of data,
4348 and such small pushes do rounding that causes trouble. */
4349 && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4350 || align >= BIGGEST_ALIGNMENT
4351 || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4352 == (align / BITS_PER_UNIT)))
4353 && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4355 /* Push padding now if padding above and stack grows down,
4356 or if padding below and stack grows up.
4357 But if space already allocated, this has already been done. */
4358 if (extra && args_addr == 0
4359 && where_pad != none && where_pad != stack_direction)
4360 anti_adjust_stack (GEN_INT (extra));
4362 move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4364 else
4365 #endif /* PUSH_ROUNDING */
4367 rtx target;
4369 /* Otherwise make space on the stack and copy the data
4370 to the address of that space. */
4372 /* Deduct words put into registers from the size we must copy. */
4373 if (partial != 0)
4375 if (CONST_INT_P (size))
4376 size = GEN_INT (INTVAL (size) - used);
4377 else
4378 size = expand_binop (GET_MODE (size), sub_optab, size,
4379 gen_int_mode (used, GET_MODE (size)),
4380 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4383 /* Get the address of the stack space.
4384 In this case, we do not deal with EXTRA separately.
4385 A single stack adjust will do. */
4386 if (! args_addr)
4388 temp = push_block (size, extra, where_pad == downward);
4389 extra = 0;
4391 else if (CONST_INT_P (args_so_far))
4392 temp = memory_address (BLKmode,
4393 plus_constant (Pmode, args_addr,
4394 skip + INTVAL (args_so_far)));
4395 else
4396 temp = memory_address (BLKmode,
4397 plus_constant (Pmode,
4398 gen_rtx_PLUS (Pmode,
4399 args_addr,
4400 args_so_far),
4401 skip));
4403 if (!ACCUMULATE_OUTGOING_ARGS)
4405 /* If the source is referenced relative to the stack pointer,
4406 copy it to another register to stabilize it. We do not need
4407 to do this if we know that we won't be changing sp. */
4409 if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4410 || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4411 temp = copy_to_reg (temp);
4414 target = gen_rtx_MEM (BLKmode, temp);
4416 /* We do *not* set_mem_attributes here, because incoming arguments
4417 may overlap with sibling call outgoing arguments and we cannot
4418 allow reordering of reads from function arguments with stores
4419 to outgoing arguments of sibling calls. We do, however, want
4420 to record the alignment of the stack slot. */
4421 /* ALIGN may well be better aligned than TYPE, e.g. due to
4422 PARM_BOUNDARY. Assume the caller isn't lying. */
4423 set_mem_align (target, align);
4425 /* If part should go in registers and pushing to that part would
4426 overwrite some of the values that need to go into regs, load the
4427 overlapping values into temporary pseudos to be moved into the hard
4428 regs at the end after the stack pushing has completed.
4429 We cannot load them directly into the hard regs here because
4430 they can be clobbered by the block move expansions.
4431 See PR 65358. */
4433 if (partial > 0 && reg != 0 && mode == BLKmode
4434 && GET_CODE (reg) != PARALLEL)
4436 overlapping = memory_load_overlap (XEXP (x, 0), temp, partial);
4437 if (overlapping > 0)
4439 gcc_assert (overlapping % UNITS_PER_WORD == 0);
4440 overlapping /= UNITS_PER_WORD;
4442 tmp_regs = XALLOCAVEC (rtx, overlapping);
4444 for (int i = 0; i < overlapping; i++)
4445 tmp_regs[i] = gen_reg_rtx (word_mode);
4447 for (int i = 0; i < overlapping; i++)
4448 emit_move_insn (tmp_regs[i],
4449 operand_subword_force (target, i, mode));
4451 else if (overlapping == -1)
4452 overlapping = 0;
4453 /* Could not determine whether there is overlap.
4454 Fail the sibcall. */
4455 else
4457 overlapping = 0;
4458 if (sibcall_p)
4459 return false;
4462 emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4465 else if (partial > 0)
4467 /* Scalar partly in registers. */
4469 int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4470 int i;
4471 int not_stack;
4472 /* # bytes of start of argument
4473 that we must make space for but need not store. */
4474 int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4475 int args_offset = INTVAL (args_so_far);
4476 int skip;
4478 /* Push padding now if padding above and stack grows down,
4479 or if padding below and stack grows up.
4480 But if space already allocated, this has already been done. */
4481 if (extra && args_addr == 0
4482 && where_pad != none && where_pad != stack_direction)
4483 anti_adjust_stack (GEN_INT (extra));
4485 /* If we make space by pushing it, we might as well push
4486 the real data. Otherwise, we can leave OFFSET nonzero
4487 and leave the space uninitialized. */
4488 if (args_addr == 0)
4489 offset = 0;
4491 /* Now NOT_STACK gets the number of words that we don't need to
4492 allocate on the stack. Convert OFFSET to words too. */
4493 not_stack = (partial - offset) / UNITS_PER_WORD;
4494 offset /= UNITS_PER_WORD;
4496 /* If the partial register-part of the arg counts in its stack size,
4497 skip the part of stack space corresponding to the registers.
4498 Otherwise, start copying to the beginning of the stack space,
4499 by setting SKIP to 0. */
4500 skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4502 if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4503 x = validize_mem (force_const_mem (mode, x));
4505 /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4506 SUBREGs of such registers are not allowed. */
4507 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4508 && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4509 x = copy_to_reg (x);
4511 /* Loop over all the words allocated on the stack for this arg. */
4512 /* We can do it by words, because any scalar bigger than a word
4513 has a size a multiple of a word. */
4514 for (i = size - 1; i >= not_stack; i--)
4515 if (i >= not_stack + offset)
4516 if (!emit_push_insn (operand_subword_force (x, i, mode),
4517 word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4518 0, args_addr,
4519 GEN_INT (args_offset + ((i - not_stack + skip)
4520 * UNITS_PER_WORD)),
4521 reg_parm_stack_space, alignment_pad, sibcall_p))
4522 return false;
4524 else
4526 rtx addr;
4527 rtx dest;
4529 /* Push padding now if padding above and stack grows down,
4530 or if padding below and stack grows up.
4531 But if space already allocated, this has already been done. */
4532 if (extra && args_addr == 0
4533 && where_pad != none && where_pad != stack_direction)
4534 anti_adjust_stack (GEN_INT (extra));
4536 #ifdef PUSH_ROUNDING
4537 if (args_addr == 0 && PUSH_ARGS)
4538 emit_single_push_insn (mode, x, type);
4539 else
4540 #endif
4542 if (CONST_INT_P (args_so_far))
4543 addr
4544 = memory_address (mode,
4545 plus_constant (Pmode, args_addr,
4546 INTVAL (args_so_far)));
4547 else
4548 addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4549 args_so_far));
4550 dest = gen_rtx_MEM (mode, addr);
4552 /* We do *not* set_mem_attributes here, because incoming arguments
4553 may overlap with sibling call outgoing arguments and we cannot
4554 allow reordering of reads from function arguments with stores
4555 to outgoing arguments of sibling calls. We do, however, want
4556 to record the alignment of the stack slot. */
4557 /* ALIGN may well be better aligned than TYPE, e.g. due to
4558 PARM_BOUNDARY. Assume the caller isn't lying. */
4559 set_mem_align (dest, align);
4561 emit_move_insn (dest, x);
4565 /* Move the partial arguments into the registers and any overlapping
4566 values that we moved into the pseudos in tmp_regs. */
4567 if (partial > 0 && reg != 0)
4569 /* Handle calls that pass values in multiple non-contiguous locations.
4570 The Irix 6 ABI has examples of this. */
4571 if (GET_CODE (reg) == PARALLEL)
4572 emit_group_load (reg, x, type, -1);
4573 else
4575 gcc_assert (partial % UNITS_PER_WORD == 0);
4576 move_block_to_reg (REGNO (reg), x, nregs - overlapping, mode);
4578 for (int i = 0; i < overlapping; i++)
4579 emit_move_insn (gen_rtx_REG (word_mode, REGNO (reg)
4580 + nregs - overlapping + i),
4581 tmp_regs[i]);
4586 if (extra && args_addr == 0 && where_pad == stack_direction)
4587 anti_adjust_stack (GEN_INT (extra));
4589 if (alignment_pad && args_addr == 0)
4590 anti_adjust_stack (alignment_pad);
4592 return true;
4595 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4596 operations. */
4598 static rtx
4599 get_subtarget (rtx x)
4601 return (optimize
4602 || x == 0
4603 /* Only registers can be subtargets. */
4604 || !REG_P (x)
4605 /* Don't use hard regs to avoid extending their life. */
4606 || REGNO (x) < FIRST_PSEUDO_REGISTER
4607 ? 0 : x);
4610 /* A subroutine of expand_assignment. Optimize FIELD op= VAL, where
4611 FIELD is a bitfield. Returns true if the optimization was successful,
4612 and there's nothing else to do. */
4614 static bool
4615 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4616 unsigned HOST_WIDE_INT bitpos,
4617 unsigned HOST_WIDE_INT bitregion_start,
4618 unsigned HOST_WIDE_INT bitregion_end,
4619 machine_mode mode1, rtx str_rtx,
4620 tree to, tree src, bool reverse)
4622 machine_mode str_mode = GET_MODE (str_rtx);
4623 unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4624 tree op0, op1;
4625 rtx value, result;
4626 optab binop;
4627 gimple *srcstmt;
4628 enum tree_code code;
4630 if (mode1 != VOIDmode
4631 || bitsize >= BITS_PER_WORD
4632 || str_bitsize > BITS_PER_WORD
4633 || TREE_SIDE_EFFECTS (to)
4634 || TREE_THIS_VOLATILE (to))
4635 return false;
4637 STRIP_NOPS (src);
4638 if (TREE_CODE (src) != SSA_NAME)
4639 return false;
4640 if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4641 return false;
4643 srcstmt = get_gimple_for_ssa_name (src);
4644 if (!srcstmt
4645 || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4646 return false;
4648 code = gimple_assign_rhs_code (srcstmt);
4650 op0 = gimple_assign_rhs1 (srcstmt);
4652 /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4653 to find its initialization. Hopefully the initialization will
4654 be from a bitfield load. */
4655 if (TREE_CODE (op0) == SSA_NAME)
4657 gimple *op0stmt = get_gimple_for_ssa_name (op0);
4659 /* We want to eventually have OP0 be the same as TO, which
4660 should be a bitfield. */
4661 if (!op0stmt
4662 || !is_gimple_assign (op0stmt)
4663 || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4664 return false;
4665 op0 = gimple_assign_rhs1 (op0stmt);
4668 op1 = gimple_assign_rhs2 (srcstmt);
4670 if (!operand_equal_p (to, op0, 0))
4671 return false;
4673 if (MEM_P (str_rtx))
4675 unsigned HOST_WIDE_INT offset1;
4677 if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4678 str_mode = word_mode;
4679 str_mode = get_best_mode (bitsize, bitpos,
4680 bitregion_start, bitregion_end,
4681 MEM_ALIGN (str_rtx), str_mode, 0);
4682 if (str_mode == VOIDmode)
4683 return false;
4684 str_bitsize = GET_MODE_BITSIZE (str_mode);
4686 offset1 = bitpos;
4687 bitpos %= str_bitsize;
4688 offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4689 str_rtx = adjust_address (str_rtx, str_mode, offset1);
4691 else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4692 return false;
4693 else
4694 gcc_assert (!reverse);
4696 /* If the bit field covers the whole REG/MEM, store_field
4697 will likely generate better code. */
4698 if (bitsize >= str_bitsize)
4699 return false;
4701 /* We can't handle fields split across multiple entities. */
4702 if (bitpos + bitsize > str_bitsize)
4703 return false;
4705 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4706 bitpos = str_bitsize - bitpos - bitsize;
4708 switch (code)
4710 case PLUS_EXPR:
4711 case MINUS_EXPR:
4712 /* For now, just optimize the case of the topmost bitfield
4713 where we don't need to do any masking and also
4714 1 bit bitfields where xor can be used.
4715 We might win by one instruction for the other bitfields
4716 too if insv/extv instructions aren't used, so that
4717 can be added later. */
4718 if ((reverse || bitpos + bitsize != str_bitsize)
4719 && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4720 break;
4722 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4723 value = convert_modes (str_mode,
4724 TYPE_MODE (TREE_TYPE (op1)), value,
4725 TYPE_UNSIGNED (TREE_TYPE (op1)));
4727 /* We may be accessing data outside the field, which means
4728 we can alias adjacent data. */
4729 if (MEM_P (str_rtx))
4731 str_rtx = shallow_copy_rtx (str_rtx);
4732 set_mem_alias_set (str_rtx, 0);
4733 set_mem_expr (str_rtx, 0);
4736 if (bitsize == 1 && (reverse || bitpos + bitsize != str_bitsize))
4738 value = expand_and (str_mode, value, const1_rtx, NULL);
4739 binop = xor_optab;
4741 else
4742 binop = code == PLUS_EXPR ? add_optab : sub_optab;
4744 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4745 if (reverse)
4746 value = flip_storage_order (str_mode, value);
4747 result = expand_binop (str_mode, binop, str_rtx,
4748 value, str_rtx, 1, OPTAB_WIDEN);
4749 if (result != str_rtx)
4750 emit_move_insn (str_rtx, result);
4751 return true;
4753 case BIT_IOR_EXPR:
4754 case BIT_XOR_EXPR:
4755 if (TREE_CODE (op1) != INTEGER_CST)
4756 break;
4757 value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4758 value = convert_modes (str_mode,
4759 TYPE_MODE (TREE_TYPE (op1)), value,
4760 TYPE_UNSIGNED (TREE_TYPE (op1)));
4762 /* We may be accessing data outside the field, which means
4763 we can alias adjacent data. */
4764 if (MEM_P (str_rtx))
4766 str_rtx = shallow_copy_rtx (str_rtx);
4767 set_mem_alias_set (str_rtx, 0);
4768 set_mem_expr (str_rtx, 0);
4771 binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4772 if (bitpos + bitsize != str_bitsize)
4774 rtx mask = gen_int_mode ((HOST_WIDE_INT_1U << bitsize) - 1,
4775 str_mode);
4776 value = expand_and (str_mode, value, mask, NULL_RTX);
4778 value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4779 if (reverse)
4780 value = flip_storage_order (str_mode, value);
4781 result = expand_binop (str_mode, binop, str_rtx,
4782 value, str_rtx, 1, OPTAB_WIDEN);
4783 if (result != str_rtx)
4784 emit_move_insn (str_rtx, result);
4785 return true;
4787 default:
4788 break;
4791 return false;
4794 /* In the C++ memory model, consecutive bit fields in a structure are
4795 considered one memory location.
4797 Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4798 returns the bit range of consecutive bits in which this COMPONENT_REF
4799 belongs. The values are returned in *BITSTART and *BITEND. *BITPOS
4800 and *OFFSET may be adjusted in the process.
4802 If the access does not need to be restricted, 0 is returned in both
4803 *BITSTART and *BITEND. */
4805 void
4806 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4807 unsigned HOST_WIDE_INT *bitend,
4808 tree exp,
4809 HOST_WIDE_INT *bitpos,
4810 tree *offset)
4812 HOST_WIDE_INT bitoffset;
4813 tree field, repr;
4815 gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4817 field = TREE_OPERAND (exp, 1);
4818 repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4819 /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4820 need to limit the range we can access. */
4821 if (!repr)
4823 *bitstart = *bitend = 0;
4824 return;
4827 /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4828 part of a larger bit field, then the representative does not serve any
4829 useful purpose. This can occur in Ada. */
4830 if (handled_component_p (TREE_OPERAND (exp, 0)))
4832 machine_mode rmode;
4833 HOST_WIDE_INT rbitsize, rbitpos;
4834 tree roffset;
4835 int unsignedp, reversep, volatilep = 0;
4836 get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4837 &roffset, &rmode, &unsignedp, &reversep,
4838 &volatilep);
4839 if ((rbitpos % BITS_PER_UNIT) != 0)
4841 *bitstart = *bitend = 0;
4842 return;
4846 /* Compute the adjustment to bitpos from the offset of the field
4847 relative to the representative. DECL_FIELD_OFFSET of field and
4848 repr are the same by construction if they are not constants,
4849 see finish_bitfield_layout. */
4850 if (tree_fits_uhwi_p (DECL_FIELD_OFFSET (field))
4851 && tree_fits_uhwi_p (DECL_FIELD_OFFSET (repr)))
4852 bitoffset = (tree_to_uhwi (DECL_FIELD_OFFSET (field))
4853 - tree_to_uhwi (DECL_FIELD_OFFSET (repr))) * BITS_PER_UNIT;
4854 else
4855 bitoffset = 0;
4856 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
4857 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
4859 /* If the adjustment is larger than bitpos, we would have a negative bit
4860 position for the lower bound and this may wreak havoc later. Adjust
4861 offset and bitpos to make the lower bound non-negative in that case. */
4862 if (bitoffset > *bitpos)
4864 HOST_WIDE_INT adjust = bitoffset - *bitpos;
4865 gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4867 *bitpos += adjust;
4868 if (*offset == NULL_TREE)
4869 *offset = size_int (-adjust / BITS_PER_UNIT);
4870 else
4871 *offset
4872 = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4873 *bitstart = 0;
4875 else
4876 *bitstart = *bitpos - bitoffset;
4878 *bitend = *bitstart + tree_to_uhwi (DECL_SIZE (repr)) - 1;
4881 /* Returns true if ADDR is an ADDR_EXPR of a DECL that does not reside
4882 in memory and has non-BLKmode. DECL_RTL must not be a MEM; if
4883 DECL_RTL was not set yet, return NORTL. */
4885 static inline bool
4886 addr_expr_of_non_mem_decl_p_1 (tree addr, bool nortl)
4888 if (TREE_CODE (addr) != ADDR_EXPR)
4889 return false;
4891 tree base = TREE_OPERAND (addr, 0);
4893 if (!DECL_P (base)
4894 || TREE_ADDRESSABLE (base)
4895 || DECL_MODE (base) == BLKmode)
4896 return false;
4898 if (!DECL_RTL_SET_P (base))
4899 return nortl;
4901 return (!MEM_P (DECL_RTL (base)));
4904 /* Returns true if the MEM_REF REF refers to an object that does not
4905 reside in memory and has non-BLKmode. */
4907 static inline bool
4908 mem_ref_refers_to_non_mem_p (tree ref)
4910 tree base = TREE_OPERAND (ref, 0);
4911 return addr_expr_of_non_mem_decl_p_1 (base, false);
4914 /* Expand an assignment that stores the value of FROM into TO. If NONTEMPORAL
4915 is true, try generating a nontemporal store. */
4917 void
4918 expand_assignment (tree to, tree from, bool nontemporal)
4920 rtx to_rtx = 0;
4921 rtx result;
4922 machine_mode mode;
4923 unsigned int align;
4924 enum insn_code icode;
4926 /* Don't crash if the lhs of the assignment was erroneous. */
4927 if (TREE_CODE (to) == ERROR_MARK)
4929 expand_normal (from);
4930 return;
4933 /* Optimize away no-op moves without side-effects. */
4934 if (operand_equal_p (to, from, 0))
4935 return;
4937 /* Handle misaligned stores. */
4938 mode = TYPE_MODE (TREE_TYPE (to));
4939 if ((TREE_CODE (to) == MEM_REF
4940 || TREE_CODE (to) == TARGET_MEM_REF)
4941 && mode != BLKmode
4942 && !mem_ref_refers_to_non_mem_p (to)
4943 && ((align = get_object_alignment (to))
4944 < GET_MODE_ALIGNMENT (mode))
4945 && (((icode = optab_handler (movmisalign_optab, mode))
4946 != CODE_FOR_nothing)
4947 || SLOW_UNALIGNED_ACCESS (mode, align)))
4949 rtx reg, mem;
4951 reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4952 reg = force_not_mem (reg);
4953 mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4954 if (TREE_CODE (to) == MEM_REF && REF_REVERSE_STORAGE_ORDER (to))
4955 reg = flip_storage_order (mode, reg);
4957 if (icode != CODE_FOR_nothing)
4959 struct expand_operand ops[2];
4961 create_fixed_operand (&ops[0], mem);
4962 create_input_operand (&ops[1], reg, mode);
4963 /* The movmisalign<mode> pattern cannot fail, else the assignment
4964 would silently be omitted. */
4965 expand_insn (icode, 2, ops);
4967 else
4968 store_bit_field (mem, GET_MODE_BITSIZE (mode), 0, 0, 0, mode, reg,
4969 false);
4970 return;
4973 /* Assignment of a structure component needs special treatment
4974 if the structure component's rtx is not simply a MEM.
4975 Assignment of an array element at a constant index, and assignment of
4976 an array element in an unaligned packed structure field, has the same
4977 problem. Same for (partially) storing into a non-memory object. */
4978 if (handled_component_p (to)
4979 || (TREE_CODE (to) == MEM_REF
4980 && (REF_REVERSE_STORAGE_ORDER (to)
4981 || mem_ref_refers_to_non_mem_p (to)))
4982 || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4984 machine_mode mode1;
4985 HOST_WIDE_INT bitsize, bitpos;
4986 unsigned HOST_WIDE_INT bitregion_start = 0;
4987 unsigned HOST_WIDE_INT bitregion_end = 0;
4988 tree offset;
4989 int unsignedp, reversep, volatilep = 0;
4990 tree tem;
4992 push_temp_slots ();
4993 tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4994 &unsignedp, &reversep, &volatilep);
4996 /* Make sure bitpos is not negative, it can wreak havoc later. */
4997 if (bitpos < 0)
4999 gcc_assert (offset == NULL_TREE);
5000 offset = size_int (bitpos >> LOG2_BITS_PER_UNIT);
5001 bitpos &= BITS_PER_UNIT - 1;
5004 if (TREE_CODE (to) == COMPONENT_REF
5005 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
5006 get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
5007 /* The C++ memory model naturally applies to byte-aligned fields.
5008 However, if we do not have a DECL_BIT_FIELD_TYPE but BITPOS or
5009 BITSIZE are not byte-aligned, there is no need to limit the range
5010 we can access. This can occur with packed structures in Ada. */
5011 else if (bitsize > 0
5012 && bitsize % BITS_PER_UNIT == 0
5013 && bitpos % BITS_PER_UNIT == 0)
5015 bitregion_start = bitpos;
5016 bitregion_end = bitpos + bitsize - 1;
5019 to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
5021 /* If the field has a mode, we want to access it in the
5022 field's mode, not the computed mode.
5023 If a MEM has VOIDmode (external with incomplete type),
5024 use BLKmode for it instead. */
5025 if (MEM_P (to_rtx))
5027 if (mode1 != VOIDmode)
5028 to_rtx = adjust_address (to_rtx, mode1, 0);
5029 else if (GET_MODE (to_rtx) == VOIDmode)
5030 to_rtx = adjust_address (to_rtx, BLKmode, 0);
5033 if (offset != 0)
5035 machine_mode address_mode;
5036 rtx offset_rtx;
5038 if (!MEM_P (to_rtx))
5040 /* We can get constant negative offsets into arrays with broken
5041 user code. Translate this to a trap instead of ICEing. */
5042 gcc_assert (TREE_CODE (offset) == INTEGER_CST);
5043 expand_builtin_trap ();
5044 to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
5047 offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
5048 address_mode = get_address_mode (to_rtx);
5049 if (GET_MODE (offset_rtx) != address_mode)
5051 /* We cannot be sure that the RTL in offset_rtx is valid outside
5052 of a memory address context, so force it into a register
5053 before attempting to convert it to the desired mode. */
5054 offset_rtx = force_operand (offset_rtx, NULL_RTX);
5055 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5058 /* If we have an expression in OFFSET_RTX and a non-zero
5059 byte offset in BITPOS, adding the byte offset before the
5060 OFFSET_RTX results in better intermediate code, which makes
5061 later rtl optimization passes perform better.
5063 We prefer intermediate code like this:
5065 r124:DI=r123:DI+0x18
5066 [r124:DI]=r121:DI
5068 ... instead of ...
5070 r124:DI=r123:DI+0x10
5071 [r124:DI+0x8]=r121:DI
5073 This is only done for aligned data values, as these can
5074 be expected to result in single move instructions. */
5075 if (mode1 != VOIDmode
5076 && bitpos != 0
5077 && bitsize > 0
5078 && (bitpos % bitsize) == 0
5079 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
5080 && MEM_ALIGN (to_rtx) >= GET_MODE_ALIGNMENT (mode1))
5082 to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
5083 bitregion_start = 0;
5084 if (bitregion_end >= (unsigned HOST_WIDE_INT) bitpos)
5085 bitregion_end -= bitpos;
5086 bitpos = 0;
5089 to_rtx = offset_address (to_rtx, offset_rtx,
5090 highest_pow2_factor_for_target (to,
5091 offset));
5094 /* No action is needed if the target is not a memory and the field
5095 lies completely outside that target. This can occur if the source
5096 code contains an out-of-bounds access to a small array. */
5097 if (!MEM_P (to_rtx)
5098 && GET_MODE (to_rtx) != BLKmode
5099 && (unsigned HOST_WIDE_INT) bitpos
5100 >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
5102 expand_normal (from);
5103 result = NULL;
5105 /* Handle expand_expr of a complex value returning a CONCAT. */
5106 else if (GET_CODE (to_rtx) == CONCAT)
5108 unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
5109 if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
5110 && bitpos == 0
5111 && bitsize == mode_bitsize)
5112 result = store_expr (from, to_rtx, false, nontemporal, reversep);
5113 else if (bitsize == mode_bitsize / 2
5114 && (bitpos == 0 || bitpos == mode_bitsize / 2))
5115 result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
5116 nontemporal, reversep);
5117 else if (bitpos + bitsize <= mode_bitsize / 2)
5118 result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
5119 bitregion_start, bitregion_end,
5120 mode1, from, get_alias_set (to),
5121 nontemporal, reversep);
5122 else if (bitpos >= mode_bitsize / 2)
5123 result = store_field (XEXP (to_rtx, 1), bitsize,
5124 bitpos - mode_bitsize / 2,
5125 bitregion_start, bitregion_end,
5126 mode1, from, get_alias_set (to),
5127 nontemporal, reversep);
5128 else if (bitpos == 0 && bitsize == mode_bitsize)
5130 rtx from_rtx;
5131 result = expand_normal (from);
5132 from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
5133 TYPE_MODE (TREE_TYPE (from)), 0);
5134 emit_move_insn (XEXP (to_rtx, 0),
5135 read_complex_part (from_rtx, false));
5136 emit_move_insn (XEXP (to_rtx, 1),
5137 read_complex_part (from_rtx, true));
5139 else
5141 rtx temp = assign_stack_temp (GET_MODE (to_rtx),
5142 GET_MODE_SIZE (GET_MODE (to_rtx)));
5143 write_complex_part (temp, XEXP (to_rtx, 0), false);
5144 write_complex_part (temp, XEXP (to_rtx, 1), true);
5145 result = store_field (temp, bitsize, bitpos,
5146 bitregion_start, bitregion_end,
5147 mode1, from, get_alias_set (to),
5148 nontemporal, reversep);
5149 emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
5150 emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
5153 else
5155 if (MEM_P (to_rtx))
5157 /* If the field is at offset zero, we could have been given the
5158 DECL_RTX of the parent struct. Don't munge it. */
5159 to_rtx = shallow_copy_rtx (to_rtx);
5160 set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
5161 if (volatilep)
5162 MEM_VOLATILE_P (to_rtx) = 1;
5165 if (optimize_bitfield_assignment_op (bitsize, bitpos,
5166 bitregion_start, bitregion_end,
5167 mode1, to_rtx, to, from,
5168 reversep))
5169 result = NULL;
5170 else
5171 result = store_field (to_rtx, bitsize, bitpos,
5172 bitregion_start, bitregion_end,
5173 mode1, from, get_alias_set (to),
5174 nontemporal, reversep);
5177 if (result)
5178 preserve_temp_slots (result);
5179 pop_temp_slots ();
5180 return;
5183 /* If the rhs is a function call and its value is not an aggregate,
5184 call the function before we start to compute the lhs.
5185 This is needed for correct code for cases such as
5186 val = setjmp (buf) on machines where reference to val
5187 requires loading up part of an address in a separate insn.
5189 Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
5190 since it might be a promoted variable where the zero- or sign- extension
5191 needs to be done. Handling this in the normal way is safe because no
5192 computation is done before the call. The same is true for SSA names. */
5193 if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
5194 && COMPLETE_TYPE_P (TREE_TYPE (from))
5195 && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
5196 && ! (((VAR_P (to)
5197 || TREE_CODE (to) == PARM_DECL
5198 || TREE_CODE (to) == RESULT_DECL)
5199 && REG_P (DECL_RTL (to)))
5200 || TREE_CODE (to) == SSA_NAME))
5202 rtx value;
5203 rtx bounds;
5205 push_temp_slots ();
5206 value = expand_normal (from);
5208 /* Split value and bounds to store them separately. */
5209 chkp_split_slot (value, &value, &bounds);
5211 if (to_rtx == 0)
5212 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5214 /* Handle calls that return values in multiple non-contiguous locations.
5215 The Irix 6 ABI has examples of this. */
5216 if (GET_CODE (to_rtx) == PARALLEL)
5218 if (GET_CODE (value) == PARALLEL)
5219 emit_group_move (to_rtx, value);
5220 else
5221 emit_group_load (to_rtx, value, TREE_TYPE (from),
5222 int_size_in_bytes (TREE_TYPE (from)));
5224 else if (GET_CODE (value) == PARALLEL)
5225 emit_group_store (to_rtx, value, TREE_TYPE (from),
5226 int_size_in_bytes (TREE_TYPE (from)));
5227 else if (GET_MODE (to_rtx) == BLKmode)
5229 /* Handle calls that return BLKmode values in registers. */
5230 if (REG_P (value))
5231 copy_blkmode_from_reg (to_rtx, value, TREE_TYPE (from));
5232 else
5233 emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
5235 else
5237 if (POINTER_TYPE_P (TREE_TYPE (to)))
5238 value = convert_memory_address_addr_space
5239 (GET_MODE (to_rtx), value,
5240 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
5242 emit_move_insn (to_rtx, value);
5245 /* Store bounds if required. */
5246 if (bounds
5247 && (BOUNDED_P (to) || chkp_type_has_pointer (TREE_TYPE (to))))
5249 gcc_assert (MEM_P (to_rtx));
5250 chkp_emit_bounds_store (bounds, value, to_rtx);
5253 preserve_temp_slots (to_rtx);
5254 pop_temp_slots ();
5255 return;
5258 /* Ordinary treatment. Expand TO to get a REG or MEM rtx. */
5259 to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
5261 /* Don't move directly into a return register. */
5262 if (TREE_CODE (to) == RESULT_DECL
5263 && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
5265 rtx temp;
5267 push_temp_slots ();
5269 /* If the source is itself a return value, it still is in a pseudo at
5270 this point so we can move it back to the return register directly. */
5271 if (REG_P (to_rtx)
5272 && TYPE_MODE (TREE_TYPE (from)) == BLKmode
5273 && TREE_CODE (from) != CALL_EXPR)
5274 temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
5275 else
5276 temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
5278 /* Handle calls that return values in multiple non-contiguous locations.
5279 The Irix 6 ABI has examples of this. */
5280 if (GET_CODE (to_rtx) == PARALLEL)
5282 if (GET_CODE (temp) == PARALLEL)
5283 emit_group_move (to_rtx, temp);
5284 else
5285 emit_group_load (to_rtx, temp, TREE_TYPE (from),
5286 int_size_in_bytes (TREE_TYPE (from)));
5288 else if (temp)
5289 emit_move_insn (to_rtx, temp);
5291 preserve_temp_slots (to_rtx);
5292 pop_temp_slots ();
5293 return;
5296 /* In case we are returning the contents of an object which overlaps
5297 the place the value is being stored, use a safe function when copying
5298 a value through a pointer into a structure value return block. */
5299 if (TREE_CODE (to) == RESULT_DECL
5300 && TREE_CODE (from) == INDIRECT_REF
5301 && ADDR_SPACE_GENERIC_P
5302 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
5303 && refs_may_alias_p (to, from)
5304 && cfun->returns_struct
5305 && !cfun->returns_pcc_struct)
5307 rtx from_rtx, size;
5309 push_temp_slots ();
5310 size = expr_size (from);
5311 from_rtx = expand_normal (from);
5313 emit_block_move_via_libcall (XEXP (to_rtx, 0), XEXP (from_rtx, 0), size);
5315 preserve_temp_slots (to_rtx);
5316 pop_temp_slots ();
5317 return;
5320 /* Compute FROM and store the value in the rtx we got. */
5322 push_temp_slots ();
5323 result = store_expr_with_bounds (from, to_rtx, 0, nontemporal, false, to);
5324 preserve_temp_slots (result);
5325 pop_temp_slots ();
5326 return;
5329 /* Emits nontemporal store insn that moves FROM to TO. Returns true if this
5330 succeeded, false otherwise. */
5332 bool
5333 emit_storent_insn (rtx to, rtx from)
5335 struct expand_operand ops[2];
5336 machine_mode mode = GET_MODE (to);
5337 enum insn_code code = optab_handler (storent_optab, mode);
5339 if (code == CODE_FOR_nothing)
5340 return false;
5342 create_fixed_operand (&ops[0], to);
5343 create_input_operand (&ops[1], from, mode);
5344 return maybe_expand_insn (code, 2, ops);
5347 /* Generate code for computing expression EXP,
5348 and storing the value into TARGET.
5350 If the mode is BLKmode then we may return TARGET itself.
5351 It turns out that in BLKmode it doesn't cause a problem.
5352 because C has no operators that could combine two different
5353 assignments into the same BLKmode object with different values
5354 with no sequence point. Will other languages need this to
5355 be more thorough?
5357 If CALL_PARAM_P is nonzero, this is a store into a call param on the
5358 stack, and block moves may need to be treated specially.
5360 If NONTEMPORAL is true, try using a nontemporal store instruction.
5362 If REVERSE is true, the store is to be done in reverse order.
5364 If BTARGET is not NULL then computed bounds of EXP are
5365 associated with BTARGET. */
5368 store_expr_with_bounds (tree exp, rtx target, int call_param_p,
5369 bool nontemporal, bool reverse, tree btarget)
5371 rtx temp;
5372 rtx alt_rtl = NULL_RTX;
5373 location_t loc = curr_insn_location ();
5375 if (VOID_TYPE_P (TREE_TYPE (exp)))
5377 /* C++ can generate ?: expressions with a throw expression in one
5378 branch and an rvalue in the other. Here, we resolve attempts to
5379 store the throw expression's nonexistent result. */
5380 gcc_assert (!call_param_p);
5381 expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5382 return NULL_RTX;
5384 if (TREE_CODE (exp) == COMPOUND_EXPR)
5386 /* Perform first part of compound expression, then assign from second
5387 part. */
5388 expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5389 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5390 return store_expr_with_bounds (TREE_OPERAND (exp, 1), target,
5391 call_param_p, nontemporal, reverse,
5392 btarget);
5394 else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5396 /* For conditional expression, get safe form of the target. Then
5397 test the condition, doing the appropriate assignment on either
5398 side. This avoids the creation of unnecessary temporaries.
5399 For non-BLKmode, it is more efficient not to do this. */
5401 rtx_code_label *lab1 = gen_label_rtx (), *lab2 = gen_label_rtx ();
5403 do_pending_stack_adjust ();
5404 NO_DEFER_POP;
5405 jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5406 store_expr_with_bounds (TREE_OPERAND (exp, 1), target, call_param_p,
5407 nontemporal, reverse, btarget);
5408 emit_jump_insn (targetm.gen_jump (lab2));
5409 emit_barrier ();
5410 emit_label (lab1);
5411 store_expr_with_bounds (TREE_OPERAND (exp, 2), target, call_param_p,
5412 nontemporal, reverse, btarget);
5413 emit_label (lab2);
5414 OK_DEFER_POP;
5416 return NULL_RTX;
5418 else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5419 /* If this is a scalar in a register that is stored in a wider mode
5420 than the declared mode, compute the result into its declared mode
5421 and then convert to the wider mode. Our value is the computed
5422 expression. */
5424 rtx inner_target = 0;
5426 /* We can do the conversion inside EXP, which will often result
5427 in some optimizations. Do the conversion in two steps: first
5428 change the signedness, if needed, then the extend. But don't
5429 do this if the type of EXP is a subtype of something else
5430 since then the conversion might involve more than just
5431 converting modes. */
5432 if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5433 && TREE_TYPE (TREE_TYPE (exp)) == 0
5434 && GET_MODE_PRECISION (GET_MODE (target))
5435 == TYPE_PRECISION (TREE_TYPE (exp)))
5437 if (!SUBREG_CHECK_PROMOTED_SIGN (target,
5438 TYPE_UNSIGNED (TREE_TYPE (exp))))
5440 /* Some types, e.g. Fortran's logical*4, won't have a signed
5441 version, so use the mode instead. */
5442 tree ntype
5443 = (signed_or_unsigned_type_for
5444 (SUBREG_PROMOTED_SIGN (target), TREE_TYPE (exp)));
5445 if (ntype == NULL)
5446 ntype = lang_hooks.types.type_for_mode
5447 (TYPE_MODE (TREE_TYPE (exp)),
5448 SUBREG_PROMOTED_SIGN (target));
5450 exp = fold_convert_loc (loc, ntype, exp);
5453 exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5454 (GET_MODE (SUBREG_REG (target)),
5455 SUBREG_PROMOTED_SIGN (target)),
5456 exp);
5458 inner_target = SUBREG_REG (target);
5461 temp = expand_expr (exp, inner_target, VOIDmode,
5462 call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5464 /* Handle bounds returned by call. */
5465 if (TREE_CODE (exp) == CALL_EXPR)
5467 rtx bounds;
5468 chkp_split_slot (temp, &temp, &bounds);
5469 if (bounds && btarget)
5471 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5472 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5473 chkp_set_rtl_bounds (btarget, tmp);
5477 /* If TEMP is a VOIDmode constant, use convert_modes to make
5478 sure that we properly convert it. */
5479 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5481 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5482 temp, SUBREG_PROMOTED_SIGN (target));
5483 temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5484 GET_MODE (target), temp,
5485 SUBREG_PROMOTED_SIGN (target));
5488 convert_move (SUBREG_REG (target), temp,
5489 SUBREG_PROMOTED_SIGN (target));
5491 return NULL_RTX;
5493 else if ((TREE_CODE (exp) == STRING_CST
5494 || (TREE_CODE (exp) == MEM_REF
5495 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5496 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5497 == STRING_CST
5498 && integer_zerop (TREE_OPERAND (exp, 1))))
5499 && !nontemporal && !call_param_p
5500 && MEM_P (target))
5502 /* Optimize initialization of an array with a STRING_CST. */
5503 HOST_WIDE_INT exp_len, str_copy_len;
5504 rtx dest_mem;
5505 tree str = TREE_CODE (exp) == STRING_CST
5506 ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5508 exp_len = int_expr_size (exp);
5509 if (exp_len <= 0)
5510 goto normal_expr;
5512 if (TREE_STRING_LENGTH (str) <= 0)
5513 goto normal_expr;
5515 str_copy_len = strlen (TREE_STRING_POINTER (str));
5516 if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5517 goto normal_expr;
5519 str_copy_len = TREE_STRING_LENGTH (str);
5520 if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5521 && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5523 str_copy_len += STORE_MAX_PIECES - 1;
5524 str_copy_len &= ~(STORE_MAX_PIECES - 1);
5526 str_copy_len = MIN (str_copy_len, exp_len);
5527 if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5528 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5529 MEM_ALIGN (target), false))
5530 goto normal_expr;
5532 dest_mem = target;
5534 dest_mem = store_by_pieces (dest_mem,
5535 str_copy_len, builtin_strncpy_read_str,
5536 CONST_CAST (char *,
5537 TREE_STRING_POINTER (str)),
5538 MEM_ALIGN (target), false,
5539 exp_len > str_copy_len ? 1 : 0);
5540 if (exp_len > str_copy_len)
5541 clear_storage (adjust_address (dest_mem, BLKmode, 0),
5542 GEN_INT (exp_len - str_copy_len),
5543 BLOCK_OP_NORMAL);
5544 return NULL_RTX;
5546 else
5548 rtx tmp_target;
5550 normal_expr:
5551 /* If we want to use a nontemporal or a reverse order store, force the
5552 value into a register first. */
5553 tmp_target = nontemporal || reverse ? NULL_RTX : target;
5554 temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5555 (call_param_p
5556 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5557 &alt_rtl, false);
5559 /* Handle bounds returned by call. */
5560 if (TREE_CODE (exp) == CALL_EXPR)
5562 rtx bounds;
5563 chkp_split_slot (temp, &temp, &bounds);
5564 if (bounds && btarget)
5566 gcc_assert (TREE_CODE (btarget) == SSA_NAME);
5567 rtx tmp = targetm.calls.load_returned_bounds (bounds);
5568 chkp_set_rtl_bounds (btarget, tmp);
5573 /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5574 the same as that of TARGET, adjust the constant. This is needed, for
5575 example, in case it is a CONST_DOUBLE or CONST_WIDE_INT and we want
5576 only a word-sized value. */
5577 if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5578 && TREE_CODE (exp) != ERROR_MARK
5579 && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5580 temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5581 temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5583 /* If value was not generated in the target, store it there.
5584 Convert the value to TARGET's type first if necessary and emit the
5585 pending incrementations that have been queued when expanding EXP.
5586 Note that we cannot emit the whole queue blindly because this will
5587 effectively disable the POST_INC optimization later.
5589 If TEMP and TARGET compare equal according to rtx_equal_p, but
5590 one or both of them are volatile memory refs, we have to distinguish
5591 two cases:
5592 - expand_expr has used TARGET. In this case, we must not generate
5593 another copy. This can be detected by TARGET being equal according
5594 to == .
5595 - expand_expr has not used TARGET - that means that the source just
5596 happens to have the same RTX form. Since temp will have been created
5597 by expand_expr, it will compare unequal according to == .
5598 We must generate a copy in this case, to reach the correct number
5599 of volatile memory references. */
5601 if ((! rtx_equal_p (temp, target)
5602 || (temp != target && (side_effects_p (temp)
5603 || side_effects_p (target))))
5604 && TREE_CODE (exp) != ERROR_MARK
5605 /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5606 but TARGET is not valid memory reference, TEMP will differ
5607 from TARGET although it is really the same location. */
5608 && !(alt_rtl
5609 && rtx_equal_p (alt_rtl, target)
5610 && !side_effects_p (alt_rtl)
5611 && !side_effects_p (target))
5612 /* If there's nothing to copy, don't bother. Don't call
5613 expr_size unless necessary, because some front-ends (C++)
5614 expr_size-hook must not be given objects that are not
5615 supposed to be bit-copied or bit-initialized. */
5616 && expr_size (exp) != const0_rtx)
5618 if (GET_MODE (temp) != GET_MODE (target) && GET_MODE (temp) != VOIDmode)
5620 if (GET_MODE (target) == BLKmode)
5622 /* Handle calls that return BLKmode values in registers. */
5623 if (REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
5624 copy_blkmode_from_reg (target, temp, TREE_TYPE (exp));
5625 else
5626 store_bit_field (target,
5627 INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5628 0, 0, 0, GET_MODE (temp), temp, reverse);
5630 else
5631 convert_move (target, temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5634 else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5636 /* Handle copying a string constant into an array. The string
5637 constant may be shorter than the array. So copy just the string's
5638 actual length, and clear the rest. First get the size of the data
5639 type of the string, which is actually the size of the target. */
5640 rtx size = expr_size (exp);
5642 if (CONST_INT_P (size)
5643 && INTVAL (size) < TREE_STRING_LENGTH (exp))
5644 emit_block_move (target, temp, size,
5645 (call_param_p
5646 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5647 else
5649 machine_mode pointer_mode
5650 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5651 machine_mode address_mode = get_address_mode (target);
5653 /* Compute the size of the data to copy from the string. */
5654 tree copy_size
5655 = size_binop_loc (loc, MIN_EXPR,
5656 make_tree (sizetype, size),
5657 size_int (TREE_STRING_LENGTH (exp)));
5658 rtx copy_size_rtx
5659 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5660 (call_param_p
5661 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5662 rtx_code_label *label = 0;
5664 /* Copy that much. */
5665 copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5666 TYPE_UNSIGNED (sizetype));
5667 emit_block_move (target, temp, copy_size_rtx,
5668 (call_param_p
5669 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5671 /* Figure out how much is left in TARGET that we have to clear.
5672 Do all calculations in pointer_mode. */
5673 if (CONST_INT_P (copy_size_rtx))
5675 size = plus_constant (address_mode, size,
5676 -INTVAL (copy_size_rtx));
5677 target = adjust_address (target, BLKmode,
5678 INTVAL (copy_size_rtx));
5680 else
5682 size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5683 copy_size_rtx, NULL_RTX, 0,
5684 OPTAB_LIB_WIDEN);
5686 if (GET_MODE (copy_size_rtx) != address_mode)
5687 copy_size_rtx = convert_to_mode (address_mode,
5688 copy_size_rtx,
5689 TYPE_UNSIGNED (sizetype));
5691 target = offset_address (target, copy_size_rtx,
5692 highest_pow2_factor (copy_size));
5693 label = gen_label_rtx ();
5694 emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5695 GET_MODE (size), 0, label);
5698 if (size != const0_rtx)
5699 clear_storage (target, size, BLOCK_OP_NORMAL);
5701 if (label)
5702 emit_label (label);
5705 /* Handle calls that return values in multiple non-contiguous locations.
5706 The Irix 6 ABI has examples of this. */
5707 else if (GET_CODE (target) == PARALLEL)
5709 if (GET_CODE (temp) == PARALLEL)
5710 emit_group_move (target, temp);
5711 else
5712 emit_group_load (target, temp, TREE_TYPE (exp),
5713 int_size_in_bytes (TREE_TYPE (exp)));
5715 else if (GET_CODE (temp) == PARALLEL)
5716 emit_group_store (target, temp, TREE_TYPE (exp),
5717 int_size_in_bytes (TREE_TYPE (exp)));
5718 else if (GET_MODE (temp) == BLKmode)
5719 emit_block_move (target, temp, expr_size (exp),
5720 (call_param_p
5721 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5722 /* If we emit a nontemporal store, there is nothing else to do. */
5723 else if (nontemporal && emit_storent_insn (target, temp))
5725 else
5727 if (reverse)
5728 temp = flip_storage_order (GET_MODE (target), temp);
5729 temp = force_operand (temp, target);
5730 if (temp != target)
5731 emit_move_insn (target, temp);
5735 return NULL_RTX;
5738 /* Same as store_expr_with_bounds but ignoring bounds of EXP. */
5740 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal,
5741 bool reverse)
5743 return store_expr_with_bounds (exp, target, call_param_p, nontemporal,
5744 reverse, NULL);
5747 /* Return true if field F of structure TYPE is a flexible array. */
5749 static bool
5750 flexible_array_member_p (const_tree f, const_tree type)
5752 const_tree tf;
5754 tf = TREE_TYPE (f);
5755 return (DECL_CHAIN (f) == NULL
5756 && TREE_CODE (tf) == ARRAY_TYPE
5757 && TYPE_DOMAIN (tf)
5758 && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5759 && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5760 && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5761 && int_size_in_bytes (type) >= 0);
5764 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5765 must have in order for it to completely initialize a value of type TYPE.
5766 Return -1 if the number isn't known.
5768 If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE. */
5770 static HOST_WIDE_INT
5771 count_type_elements (const_tree type, bool for_ctor_p)
5773 switch (TREE_CODE (type))
5775 case ARRAY_TYPE:
5777 tree nelts;
5779 nelts = array_type_nelts (type);
5780 if (nelts && tree_fits_uhwi_p (nelts))
5782 unsigned HOST_WIDE_INT n;
5784 n = tree_to_uhwi (nelts) + 1;
5785 if (n == 0 || for_ctor_p)
5786 return n;
5787 else
5788 return n * count_type_elements (TREE_TYPE (type), false);
5790 return for_ctor_p ? -1 : 1;
5793 case RECORD_TYPE:
5795 unsigned HOST_WIDE_INT n;
5796 tree f;
5798 n = 0;
5799 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5800 if (TREE_CODE (f) == FIELD_DECL)
5802 if (!for_ctor_p)
5803 n += count_type_elements (TREE_TYPE (f), false);
5804 else if (!flexible_array_member_p (f, type))
5805 /* Don't count flexible arrays, which are not supposed
5806 to be initialized. */
5807 n += 1;
5810 return n;
5813 case UNION_TYPE:
5814 case QUAL_UNION_TYPE:
5816 tree f;
5817 HOST_WIDE_INT n, m;
5819 gcc_assert (!for_ctor_p);
5820 /* Estimate the number of scalars in each field and pick the
5821 maximum. Other estimates would do instead; the idea is simply
5822 to make sure that the estimate is not sensitive to the ordering
5823 of the fields. */
5824 n = 1;
5825 for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5826 if (TREE_CODE (f) == FIELD_DECL)
5828 m = count_type_elements (TREE_TYPE (f), false);
5829 /* If the field doesn't span the whole union, add an extra
5830 scalar for the rest. */
5831 if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5832 TYPE_SIZE (type)) != 1)
5833 m++;
5834 if (n < m)
5835 n = m;
5837 return n;
5840 case COMPLEX_TYPE:
5841 return 2;
5843 case VECTOR_TYPE:
5844 return TYPE_VECTOR_SUBPARTS (type);
5846 case INTEGER_TYPE:
5847 case REAL_TYPE:
5848 case FIXED_POINT_TYPE:
5849 case ENUMERAL_TYPE:
5850 case BOOLEAN_TYPE:
5851 case POINTER_TYPE:
5852 case OFFSET_TYPE:
5853 case REFERENCE_TYPE:
5854 case NULLPTR_TYPE:
5855 return 1;
5857 case ERROR_MARK:
5858 return 0;
5860 case VOID_TYPE:
5861 case METHOD_TYPE:
5862 case FUNCTION_TYPE:
5863 case LANG_TYPE:
5864 default:
5865 gcc_unreachable ();
5869 /* Helper for categorize_ctor_elements. Identical interface. */
5871 static bool
5872 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5873 HOST_WIDE_INT *p_init_elts, bool *p_complete)
5875 unsigned HOST_WIDE_INT idx;
5876 HOST_WIDE_INT nz_elts, init_elts, num_fields;
5877 tree value, purpose, elt_type;
5879 /* Whether CTOR is a valid constant initializer, in accordance with what
5880 initializer_constant_valid_p does. If inferred from the constructor
5881 elements, true until proven otherwise. */
5882 bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5883 bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5885 nz_elts = 0;
5886 init_elts = 0;
5887 num_fields = 0;
5888 elt_type = NULL_TREE;
5890 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5892 HOST_WIDE_INT mult = 1;
5894 if (purpose && TREE_CODE (purpose) == RANGE_EXPR)
5896 tree lo_index = TREE_OPERAND (purpose, 0);
5897 tree hi_index = TREE_OPERAND (purpose, 1);
5899 if (tree_fits_uhwi_p (lo_index) && tree_fits_uhwi_p (hi_index))
5900 mult = (tree_to_uhwi (hi_index)
5901 - tree_to_uhwi (lo_index) + 1);
5903 num_fields += mult;
5904 elt_type = TREE_TYPE (value);
5906 switch (TREE_CODE (value))
5908 case CONSTRUCTOR:
5910 HOST_WIDE_INT nz = 0, ic = 0;
5912 bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5913 p_complete);
5915 nz_elts += mult * nz;
5916 init_elts += mult * ic;
5918 if (const_from_elts_p && const_p)
5919 const_p = const_elt_p;
5921 break;
5923 case INTEGER_CST:
5924 case REAL_CST:
5925 case FIXED_CST:
5926 if (!initializer_zerop (value))
5927 nz_elts += mult;
5928 init_elts += mult;
5929 break;
5931 case STRING_CST:
5932 nz_elts += mult * TREE_STRING_LENGTH (value);
5933 init_elts += mult * TREE_STRING_LENGTH (value);
5934 break;
5936 case COMPLEX_CST:
5937 if (!initializer_zerop (TREE_REALPART (value)))
5938 nz_elts += mult;
5939 if (!initializer_zerop (TREE_IMAGPART (value)))
5940 nz_elts += mult;
5941 init_elts += mult;
5942 break;
5944 case VECTOR_CST:
5946 unsigned i;
5947 for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5949 tree v = VECTOR_CST_ELT (value, i);
5950 if (!initializer_zerop (v))
5951 nz_elts += mult;
5952 init_elts += mult;
5955 break;
5957 default:
5959 HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5960 nz_elts += mult * tc;
5961 init_elts += mult * tc;
5963 if (const_from_elts_p && const_p)
5964 const_p
5965 = initializer_constant_valid_p (value,
5966 elt_type,
5967 TYPE_REVERSE_STORAGE_ORDER
5968 (TREE_TYPE (ctor)))
5969 != NULL_TREE;
5971 break;
5975 if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5976 num_fields, elt_type))
5977 *p_complete = false;
5979 *p_nz_elts += nz_elts;
5980 *p_init_elts += init_elts;
5982 return const_p;
5985 /* Examine CTOR to discover:
5986 * how many scalar fields are set to nonzero values,
5987 and place it in *P_NZ_ELTS;
5988 * how many scalar fields in total are in CTOR,
5989 and place it in *P_ELT_COUNT.
5990 * whether the constructor is complete -- in the sense that every
5991 meaningful byte is explicitly given a value --
5992 and place it in *P_COMPLETE.
5994 Return whether or not CTOR is a valid static constant initializer, the same
5995 as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0". */
5997 bool
5998 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5999 HOST_WIDE_INT *p_init_elts, bool *p_complete)
6001 *p_nz_elts = 0;
6002 *p_init_elts = 0;
6003 *p_complete = true;
6005 return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
6008 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
6009 of which had type LAST_TYPE. Each element was itself a complete
6010 initializer, in the sense that every meaningful byte was explicitly
6011 given a value. Return true if the same is true for the constructor
6012 as a whole. */
6014 bool
6015 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
6016 const_tree last_type)
6018 if (TREE_CODE (type) == UNION_TYPE
6019 || TREE_CODE (type) == QUAL_UNION_TYPE)
6021 if (num_elts == 0)
6022 return false;
6024 gcc_assert (num_elts == 1 && last_type);
6026 /* ??? We could look at each element of the union, and find the
6027 largest element. Which would avoid comparing the size of the
6028 initialized element against any tail padding in the union.
6029 Doesn't seem worth the effort... */
6030 return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
6033 return count_type_elements (type, true) == num_elts;
6036 /* Return 1 if EXP contains mostly (3/4) zeros. */
6038 static int
6039 mostly_zeros_p (const_tree exp)
6041 if (TREE_CODE (exp) == CONSTRUCTOR)
6043 HOST_WIDE_INT nz_elts, init_elts;
6044 bool complete_p;
6046 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6047 return !complete_p || nz_elts < init_elts / 4;
6050 return initializer_zerop (exp);
6053 /* Return 1 if EXP contains all zeros. */
6055 static int
6056 all_zeros_p (const_tree exp)
6058 if (TREE_CODE (exp) == CONSTRUCTOR)
6060 HOST_WIDE_INT nz_elts, init_elts;
6061 bool complete_p;
6063 categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
6064 return nz_elts == 0;
6067 return initializer_zerop (exp);
6070 /* Helper function for store_constructor.
6071 TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
6072 CLEARED is as for store_constructor.
6073 ALIAS_SET is the alias set to use for any stores.
6074 If REVERSE is true, the store is to be done in reverse order.
6076 This provides a recursive shortcut back to store_constructor when it isn't
6077 necessary to go through store_field. This is so that we can pass through
6078 the cleared field to let store_constructor know that we may not have to
6079 clear a substructure if the outer structure has already been cleared. */
6081 static void
6082 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
6083 HOST_WIDE_INT bitpos,
6084 unsigned HOST_WIDE_INT bitregion_start,
6085 unsigned HOST_WIDE_INT bitregion_end,
6086 machine_mode mode,
6087 tree exp, int cleared,
6088 alias_set_type alias_set, bool reverse)
6090 if (TREE_CODE (exp) == CONSTRUCTOR
6091 /* We can only call store_constructor recursively if the size and
6092 bit position are on a byte boundary. */
6093 && bitpos % BITS_PER_UNIT == 0
6094 && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
6095 /* If we have a nonzero bitpos for a register target, then we just
6096 let store_field do the bitfield handling. This is unlikely to
6097 generate unnecessary clear instructions anyways. */
6098 && (bitpos == 0 || MEM_P (target)))
6100 if (MEM_P (target))
6101 target
6102 = adjust_address (target,
6103 GET_MODE (target) == BLKmode
6104 || 0 != (bitpos
6105 % GET_MODE_ALIGNMENT (GET_MODE (target)))
6106 ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
6109 /* Update the alias set, if required. */
6110 if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
6111 && MEM_ALIAS_SET (target) != 0)
6113 target = copy_rtx (target);
6114 set_mem_alias_set (target, alias_set);
6117 store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT,
6118 reverse);
6120 else
6121 store_field (target, bitsize, bitpos, bitregion_start, bitregion_end, mode,
6122 exp, alias_set, false, reverse);
6126 /* Returns the number of FIELD_DECLs in TYPE. */
6128 static int
6129 fields_length (const_tree type)
6131 tree t = TYPE_FIELDS (type);
6132 int count = 0;
6134 for (; t; t = DECL_CHAIN (t))
6135 if (TREE_CODE (t) == FIELD_DECL)
6136 ++count;
6138 return count;
6142 /* Store the value of constructor EXP into the rtx TARGET.
6143 TARGET is either a REG or a MEM; we know it cannot conflict, since
6144 safe_from_p has been called.
6145 CLEARED is true if TARGET is known to have been zero'd.
6146 SIZE is the number of bytes of TARGET we are allowed to modify: this
6147 may not be the same as the size of EXP if we are assigning to a field
6148 which has been packed to exclude padding bits.
6149 If REVERSE is true, the store is to be done in reverse order. */
6151 static void
6152 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size,
6153 bool reverse)
6155 tree type = TREE_TYPE (exp);
6156 HOST_WIDE_INT exp_size = int_size_in_bytes (type);
6157 HOST_WIDE_INT bitregion_end = size > 0 ? size * BITS_PER_UNIT - 1 : 0;
6159 switch (TREE_CODE (type))
6161 case RECORD_TYPE:
6162 case UNION_TYPE:
6163 case QUAL_UNION_TYPE:
6165 unsigned HOST_WIDE_INT idx;
6166 tree field, value;
6168 /* The storage order is specified for every aggregate type. */
6169 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6171 /* If size is zero or the target is already cleared, do nothing. */
6172 if (size == 0 || cleared)
6173 cleared = 1;
6174 /* We either clear the aggregate or indicate the value is dead. */
6175 else if ((TREE_CODE (type) == UNION_TYPE
6176 || TREE_CODE (type) == QUAL_UNION_TYPE)
6177 && ! CONSTRUCTOR_ELTS (exp))
6178 /* If the constructor is empty, clear the union. */
6180 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
6181 cleared = 1;
6184 /* If we are building a static constructor into a register,
6185 set the initial value as zero so we can fold the value into
6186 a constant. But if more than one register is involved,
6187 this probably loses. */
6188 else if (REG_P (target) && TREE_STATIC (exp)
6189 && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
6191 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6192 cleared = 1;
6195 /* If the constructor has fewer fields than the structure or
6196 if we are initializing the structure to mostly zeros, clear
6197 the whole structure first. Don't do this if TARGET is a
6198 register whose mode size isn't equal to SIZE since
6199 clear_storage can't handle this case. */
6200 else if (size > 0
6201 && (((int) CONSTRUCTOR_NELTS (exp) != fields_length (type))
6202 || mostly_zeros_p (exp))
6203 && (!REG_P (target)
6204 || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
6205 == size)))
6207 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6208 cleared = 1;
6211 if (REG_P (target) && !cleared)
6212 emit_clobber (target);
6214 /* Store each element of the constructor into the
6215 corresponding field of TARGET. */
6216 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
6218 machine_mode mode;
6219 HOST_WIDE_INT bitsize;
6220 HOST_WIDE_INT bitpos = 0;
6221 tree offset;
6222 rtx to_rtx = target;
6224 /* Just ignore missing fields. We cleared the whole
6225 structure, above, if any fields are missing. */
6226 if (field == 0)
6227 continue;
6229 if (cleared && initializer_zerop (value))
6230 continue;
6232 if (tree_fits_uhwi_p (DECL_SIZE (field)))
6233 bitsize = tree_to_uhwi (DECL_SIZE (field));
6234 else
6235 gcc_unreachable ();
6237 mode = DECL_MODE (field);
6238 if (DECL_BIT_FIELD (field))
6239 mode = VOIDmode;
6241 offset = DECL_FIELD_OFFSET (field);
6242 if (tree_fits_shwi_p (offset)
6243 && tree_fits_shwi_p (bit_position (field)))
6245 bitpos = int_bit_position (field);
6246 offset = NULL_TREE;
6248 else
6249 gcc_unreachable ();
6251 /* If this initializes a field that is smaller than a
6252 word, at the start of a word, try to widen it to a full
6253 word. This special case allows us to output C++ member
6254 function initializations in a form that the optimizers
6255 can understand. */
6256 if (WORD_REGISTER_OPERATIONS
6257 && REG_P (target)
6258 && bitsize < BITS_PER_WORD
6259 && bitpos % BITS_PER_WORD == 0
6260 && GET_MODE_CLASS (mode) == MODE_INT
6261 && TREE_CODE (value) == INTEGER_CST
6262 && exp_size >= 0
6263 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
6265 tree type = TREE_TYPE (value);
6267 if (TYPE_PRECISION (type) < BITS_PER_WORD)
6269 type = lang_hooks.types.type_for_mode
6270 (word_mode, TYPE_UNSIGNED (type));
6271 value = fold_convert (type, value);
6272 /* Make sure the bits beyond the original bitsize are zero
6273 so that we can correctly avoid extra zeroing stores in
6274 later constructor elements. */
6275 tree bitsize_mask
6276 = wide_int_to_tree (type, wi::mask (bitsize, false,
6277 BITS_PER_WORD));
6278 value = fold_build2 (BIT_AND_EXPR, type, value, bitsize_mask);
6281 if (BYTES_BIG_ENDIAN)
6282 value
6283 = fold_build2 (LSHIFT_EXPR, type, value,
6284 build_int_cst (type,
6285 BITS_PER_WORD - bitsize));
6286 bitsize = BITS_PER_WORD;
6287 mode = word_mode;
6290 if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
6291 && DECL_NONADDRESSABLE_P (field))
6293 to_rtx = copy_rtx (to_rtx);
6294 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
6297 store_constructor_field (to_rtx, bitsize, bitpos,
6298 0, bitregion_end, mode,
6299 value, cleared,
6300 get_alias_set (TREE_TYPE (field)),
6301 reverse);
6303 break;
6305 case ARRAY_TYPE:
6307 tree value, index;
6308 unsigned HOST_WIDE_INT i;
6309 int need_to_clear;
6310 tree domain;
6311 tree elttype = TREE_TYPE (type);
6312 int const_bounds_p;
6313 HOST_WIDE_INT minelt = 0;
6314 HOST_WIDE_INT maxelt = 0;
6316 /* The storage order is specified for every aggregate type. */
6317 reverse = TYPE_REVERSE_STORAGE_ORDER (type);
6319 domain = TYPE_DOMAIN (type);
6320 const_bounds_p = (TYPE_MIN_VALUE (domain)
6321 && TYPE_MAX_VALUE (domain)
6322 && tree_fits_shwi_p (TYPE_MIN_VALUE (domain))
6323 && tree_fits_shwi_p (TYPE_MAX_VALUE (domain)));
6325 /* If we have constant bounds for the range of the type, get them. */
6326 if (const_bounds_p)
6328 minelt = tree_to_shwi (TYPE_MIN_VALUE (domain));
6329 maxelt = tree_to_shwi (TYPE_MAX_VALUE (domain));
6332 /* If the constructor has fewer elements than the array, clear
6333 the whole array first. Similarly if this is static
6334 constructor of a non-BLKmode object. */
6335 if (cleared)
6336 need_to_clear = 0;
6337 else if (REG_P (target) && TREE_STATIC (exp))
6338 need_to_clear = 1;
6339 else
6341 unsigned HOST_WIDE_INT idx;
6342 tree index, value;
6343 HOST_WIDE_INT count = 0, zero_count = 0;
6344 need_to_clear = ! const_bounds_p;
6346 /* This loop is a more accurate version of the loop in
6347 mostly_zeros_p (it handles RANGE_EXPR in an index). It
6348 is also needed to check for missing elements. */
6349 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
6351 HOST_WIDE_INT this_node_count;
6353 if (need_to_clear)
6354 break;
6356 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6358 tree lo_index = TREE_OPERAND (index, 0);
6359 tree hi_index = TREE_OPERAND (index, 1);
6361 if (! tree_fits_uhwi_p (lo_index)
6362 || ! tree_fits_uhwi_p (hi_index))
6364 need_to_clear = 1;
6365 break;
6368 this_node_count = (tree_to_uhwi (hi_index)
6369 - tree_to_uhwi (lo_index) + 1);
6371 else
6372 this_node_count = 1;
6374 count += this_node_count;
6375 if (mostly_zeros_p (value))
6376 zero_count += this_node_count;
6379 /* Clear the entire array first if there are any missing
6380 elements, or if the incidence of zero elements is >=
6381 75%. */
6382 if (! need_to_clear
6383 && (count < maxelt - minelt + 1
6384 || 4 * zero_count >= 3 * count))
6385 need_to_clear = 1;
6388 if (need_to_clear && size > 0)
6390 if (REG_P (target))
6391 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6392 else
6393 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6394 cleared = 1;
6397 if (!cleared && REG_P (target))
6398 /* Inform later passes that the old value is dead. */
6399 emit_clobber (target);
6401 /* Store each element of the constructor into the
6402 corresponding element of TARGET, determined by counting the
6403 elements. */
6404 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
6406 machine_mode mode;
6407 HOST_WIDE_INT bitsize;
6408 HOST_WIDE_INT bitpos;
6409 rtx xtarget = target;
6411 if (cleared && initializer_zerop (value))
6412 continue;
6414 mode = TYPE_MODE (elttype);
6415 if (mode == BLKmode)
6416 bitsize = (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6417 ? tree_to_uhwi (TYPE_SIZE (elttype))
6418 : -1);
6419 else
6420 bitsize = GET_MODE_BITSIZE (mode);
6422 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
6424 tree lo_index = TREE_OPERAND (index, 0);
6425 tree hi_index = TREE_OPERAND (index, 1);
6426 rtx index_r, pos_rtx;
6427 HOST_WIDE_INT lo, hi, count;
6428 tree position;
6430 /* If the range is constant and "small", unroll the loop. */
6431 if (const_bounds_p
6432 && tree_fits_shwi_p (lo_index)
6433 && tree_fits_shwi_p (hi_index)
6434 && (lo = tree_to_shwi (lo_index),
6435 hi = tree_to_shwi (hi_index),
6436 count = hi - lo + 1,
6437 (!MEM_P (target)
6438 || count <= 2
6439 || (tree_fits_uhwi_p (TYPE_SIZE (elttype))
6440 && (tree_to_uhwi (TYPE_SIZE (elttype)) * count
6441 <= 40 * 8)))))
6443 lo -= minelt; hi -= minelt;
6444 for (; lo <= hi; lo++)
6446 bitpos = lo * tree_to_shwi (TYPE_SIZE (elttype));
6448 if (MEM_P (target)
6449 && !MEM_KEEP_ALIAS_SET_P (target)
6450 && TREE_CODE (type) == ARRAY_TYPE
6451 && TYPE_NONALIASED_COMPONENT (type))
6453 target = copy_rtx (target);
6454 MEM_KEEP_ALIAS_SET_P (target) = 1;
6457 store_constructor_field
6458 (target, bitsize, bitpos, 0, bitregion_end,
6459 mode, value, cleared,
6460 get_alias_set (elttype), reverse);
6463 else
6465 rtx_code_label *loop_start = gen_label_rtx ();
6466 rtx_code_label *loop_end = gen_label_rtx ();
6467 tree exit_cond;
6469 expand_normal (hi_index);
6471 index = build_decl (EXPR_LOCATION (exp),
6472 VAR_DECL, NULL_TREE, domain);
6473 index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6474 SET_DECL_RTL (index, index_r);
6475 store_expr (lo_index, index_r, 0, false, reverse);
6477 /* Build the head of the loop. */
6478 do_pending_stack_adjust ();
6479 emit_label (loop_start);
6481 /* Assign value to element index. */
6482 position =
6483 fold_convert (ssizetype,
6484 fold_build2 (MINUS_EXPR,
6485 TREE_TYPE (index),
6486 index,
6487 TYPE_MIN_VALUE (domain)));
6489 position =
6490 size_binop (MULT_EXPR, position,
6491 fold_convert (ssizetype,
6492 TYPE_SIZE_UNIT (elttype)));
6494 pos_rtx = expand_normal (position);
6495 xtarget = offset_address (target, pos_rtx,
6496 highest_pow2_factor (position));
6497 xtarget = adjust_address (xtarget, mode, 0);
6498 if (TREE_CODE (value) == CONSTRUCTOR)
6499 store_constructor (value, xtarget, cleared,
6500 bitsize / BITS_PER_UNIT, reverse);
6501 else
6502 store_expr (value, xtarget, 0, false, reverse);
6504 /* Generate a conditional jump to exit the loop. */
6505 exit_cond = build2 (LT_EXPR, integer_type_node,
6506 index, hi_index);
6507 jumpif (exit_cond, loop_end, -1);
6509 /* Update the loop counter, and jump to the head of
6510 the loop. */
6511 expand_assignment (index,
6512 build2 (PLUS_EXPR, TREE_TYPE (index),
6513 index, integer_one_node),
6514 false);
6516 emit_jump (loop_start);
6518 /* Build the end of the loop. */
6519 emit_label (loop_end);
6522 else if ((index != 0 && ! tree_fits_shwi_p (index))
6523 || ! tree_fits_uhwi_p (TYPE_SIZE (elttype)))
6525 tree position;
6527 if (index == 0)
6528 index = ssize_int (1);
6530 if (minelt)
6531 index = fold_convert (ssizetype,
6532 fold_build2 (MINUS_EXPR,
6533 TREE_TYPE (index),
6534 index,
6535 TYPE_MIN_VALUE (domain)));
6537 position =
6538 size_binop (MULT_EXPR, index,
6539 fold_convert (ssizetype,
6540 TYPE_SIZE_UNIT (elttype)));
6541 xtarget = offset_address (target,
6542 expand_normal (position),
6543 highest_pow2_factor (position));
6544 xtarget = adjust_address (xtarget, mode, 0);
6545 store_expr (value, xtarget, 0, false, reverse);
6547 else
6549 if (index != 0)
6550 bitpos = ((tree_to_shwi (index) - minelt)
6551 * tree_to_uhwi (TYPE_SIZE (elttype)));
6552 else
6553 bitpos = (i * tree_to_uhwi (TYPE_SIZE (elttype)));
6555 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6556 && TREE_CODE (type) == ARRAY_TYPE
6557 && TYPE_NONALIASED_COMPONENT (type))
6559 target = copy_rtx (target);
6560 MEM_KEEP_ALIAS_SET_P (target) = 1;
6562 store_constructor_field (target, bitsize, bitpos, 0,
6563 bitregion_end, mode, value,
6564 cleared, get_alias_set (elttype),
6565 reverse);
6568 break;
6571 case VECTOR_TYPE:
6573 unsigned HOST_WIDE_INT idx;
6574 constructor_elt *ce;
6575 int i;
6576 int need_to_clear;
6577 int icode = CODE_FOR_nothing;
6578 tree elttype = TREE_TYPE (type);
6579 int elt_size = tree_to_uhwi (TYPE_SIZE (elttype));
6580 machine_mode eltmode = TYPE_MODE (elttype);
6581 HOST_WIDE_INT bitsize;
6582 HOST_WIDE_INT bitpos;
6583 rtvec vector = NULL;
6584 unsigned n_elts;
6585 alias_set_type alias;
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);
6594 icode = (int) optab_handler (vec_init_optab, mode);
6595 /* Don't use vec_init<mode> if some elements have VECTOR_TYPE. */
6596 if (icode != CODE_FOR_nothing)
6598 tree value;
6600 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6601 if (TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE)
6603 icode = CODE_FOR_nothing;
6604 break;
6607 if (icode != CODE_FOR_nothing)
6609 unsigned int i;
6611 vector = rtvec_alloc (n_elts);
6612 for (i = 0; i < n_elts; i++)
6613 RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6617 /* If the constructor has fewer elements than the vector,
6618 clear the whole array first. Similarly if this is static
6619 constructor of a non-BLKmode object. */
6620 if (cleared)
6621 need_to_clear = 0;
6622 else if (REG_P (target) && TREE_STATIC (exp))
6623 need_to_clear = 1;
6624 else
6626 unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6627 tree value;
6629 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6631 int n_elts_here = tree_to_uhwi
6632 (int_const_binop (TRUNC_DIV_EXPR,
6633 TYPE_SIZE (TREE_TYPE (value)),
6634 TYPE_SIZE (elttype)));
6636 count += n_elts_here;
6637 if (mostly_zeros_p (value))
6638 zero_count += n_elts_here;
6641 /* Clear the entire vector first if there are any missing elements,
6642 or if the incidence of zero elements is >= 75%. */
6643 need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6646 if (need_to_clear && size > 0 && !vector)
6648 if (REG_P (target))
6649 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6650 else
6651 clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6652 cleared = 1;
6655 /* Inform later passes that the old value is dead. */
6656 if (!cleared && !vector && REG_P (target))
6657 emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6659 if (MEM_P (target))
6660 alias = MEM_ALIAS_SET (target);
6661 else
6662 alias = get_alias_set (elttype);
6664 /* Store each element of the constructor into the corresponding
6665 element of TARGET, determined by counting the elements. */
6666 for (idx = 0, i = 0;
6667 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), idx, &ce);
6668 idx++, i += bitsize / elt_size)
6670 HOST_WIDE_INT eltpos;
6671 tree value = ce->value;
6673 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (value)));
6674 if (cleared && initializer_zerop (value))
6675 continue;
6677 if (ce->index)
6678 eltpos = tree_to_uhwi (ce->index);
6679 else
6680 eltpos = i;
6682 if (vector)
6684 /* vec_init<mode> should not be used if there are VECTOR_TYPE
6685 elements. */
6686 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6687 RTVEC_ELT (vector, eltpos)
6688 = expand_normal (value);
6690 else
6692 machine_mode value_mode =
6693 TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6694 ? TYPE_MODE (TREE_TYPE (value))
6695 : eltmode;
6696 bitpos = eltpos * elt_size;
6697 store_constructor_field (target, bitsize, bitpos, 0,
6698 bitregion_end, value_mode,
6699 value, cleared, alias, reverse);
6703 if (vector)
6704 emit_insn (GEN_FCN (icode)
6705 (target,
6706 gen_rtx_PARALLEL (GET_MODE (target), vector)));
6707 break;
6710 default:
6711 gcc_unreachable ();
6715 /* Store the value of EXP (an expression tree)
6716 into a subfield of TARGET which has mode MODE and occupies
6717 BITSIZE bits, starting BITPOS bits from the start of TARGET.
6718 If MODE is VOIDmode, it means that we are storing into a bit-field.
6720 BITREGION_START is bitpos of the first bitfield in this region.
6721 BITREGION_END is the bitpos of the ending bitfield in this region.
6722 These two fields are 0, if the C++ memory model does not apply,
6723 or we are not interested in keeping track of bitfield regions.
6725 Always return const0_rtx unless we have something particular to
6726 return.
6728 ALIAS_SET is the alias set for the destination. This value will
6729 (in general) be different from that for TARGET, since TARGET is a
6730 reference to the containing structure.
6732 If NONTEMPORAL is true, try generating a nontemporal store.
6734 If REVERSE is true, the store is to be done in reverse order. */
6736 static rtx
6737 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6738 unsigned HOST_WIDE_INT bitregion_start,
6739 unsigned HOST_WIDE_INT bitregion_end,
6740 machine_mode mode, tree exp,
6741 alias_set_type alias_set, bool nontemporal, bool reverse)
6743 if (TREE_CODE (exp) == ERROR_MARK)
6744 return const0_rtx;
6746 /* If we have nothing to store, do nothing unless the expression has
6747 side-effects. */
6748 if (bitsize == 0)
6749 return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6751 if (GET_CODE (target) == CONCAT)
6753 /* We're storing into a struct containing a single __complex. */
6755 gcc_assert (!bitpos);
6756 return store_expr (exp, target, 0, nontemporal, reverse);
6759 /* If the structure is in a register or if the component
6760 is a bit field, we cannot use addressing to access it.
6761 Use bit-field techniques or SUBREG to store in it. */
6763 if (mode == VOIDmode
6764 || (mode != BLKmode && ! direct_store[(int) mode]
6765 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6766 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6767 || REG_P (target)
6768 || GET_CODE (target) == SUBREG
6769 /* If the field isn't aligned enough to store as an ordinary memref,
6770 store it as a bit field. */
6771 || (mode != BLKmode
6772 && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6773 || bitpos % GET_MODE_ALIGNMENT (mode))
6774 && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6775 || (bitpos % BITS_PER_UNIT != 0)))
6776 || (bitsize >= 0 && mode != BLKmode
6777 && GET_MODE_BITSIZE (mode) > bitsize)
6778 /* If the RHS and field are a constant size and the size of the
6779 RHS isn't the same size as the bitfield, we must use bitfield
6780 operations. */
6781 || (bitsize >= 0
6782 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6783 && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0
6784 /* Except for initialization of full bytes from a CONSTRUCTOR, which
6785 we will handle specially below. */
6786 && !(TREE_CODE (exp) == CONSTRUCTOR
6787 && bitsize % BITS_PER_UNIT == 0)
6788 /* And except for bitwise copying of TREE_ADDRESSABLE types,
6789 where the FIELD_DECL has the right bitsize, but TREE_TYPE (exp)
6790 includes some extra padding. store_expr / expand_expr will in
6791 that case call get_inner_reference that will have the bitsize
6792 we check here and thus the block move will not clobber the
6793 padding that shouldn't be clobbered. In the future we could
6794 replace the TREE_ADDRESSABLE check with a check that
6795 get_base_address needs to live in memory. */
6796 && (!TREE_ADDRESSABLE (TREE_TYPE (exp))
6797 || TREE_CODE (exp) != COMPONENT_REF
6798 || TREE_CODE (DECL_SIZE (TREE_OPERAND (exp, 1))) != INTEGER_CST
6799 || (bitsize % BITS_PER_UNIT != 0)
6800 || (bitpos % BITS_PER_UNIT != 0)
6801 || (compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)), bitsize)
6802 != 0)))
6803 /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6804 decl we must use bitfield operations. */
6805 || (bitsize >= 0
6806 && TREE_CODE (exp) == MEM_REF
6807 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6808 && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6809 && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6810 && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6812 rtx temp;
6813 gimple *nop_def;
6815 /* If EXP is a NOP_EXPR of precision less than its mode, then that
6816 implies a mask operation. If the precision is the same size as
6817 the field we're storing into, that mask is redundant. This is
6818 particularly common with bit field assignments generated by the
6819 C front end. */
6820 nop_def = get_def_for_expr (exp, NOP_EXPR);
6821 if (nop_def)
6823 tree type = TREE_TYPE (exp);
6824 if (INTEGRAL_TYPE_P (type)
6825 && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6826 && bitsize == TYPE_PRECISION (type))
6828 tree op = gimple_assign_rhs1 (nop_def);
6829 type = TREE_TYPE (op);
6830 if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6831 exp = op;
6835 temp = expand_normal (exp);
6837 /* Handle calls that return values in multiple non-contiguous locations.
6838 The Irix 6 ABI has examples of this. */
6839 if (GET_CODE (temp) == PARALLEL)
6841 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
6842 machine_mode temp_mode
6843 = smallest_mode_for_size (size * BITS_PER_UNIT, MODE_INT);
6844 rtx temp_target = gen_reg_rtx (temp_mode);
6845 emit_group_store (temp_target, temp, TREE_TYPE (exp), size);
6846 temp = temp_target;
6849 /* Handle calls that return BLKmode values in registers. */
6850 else if (mode == BLKmode && REG_P (temp) && TREE_CODE (exp) == CALL_EXPR)
6852 rtx temp_target = gen_reg_rtx (GET_MODE (temp));
6853 copy_blkmode_from_reg (temp_target, temp, TREE_TYPE (exp));
6854 temp = temp_target;
6857 /* If the value has aggregate type and an integral mode then, if BITSIZE
6858 is narrower than this mode and this is for big-endian data, we first
6859 need to put the value into the low-order bits for store_bit_field,
6860 except when MODE is BLKmode and BITSIZE larger than the word size
6861 (see the handling of fields larger than a word in store_bit_field).
6862 Moreover, the field may be not aligned on a byte boundary; in this
6863 case, if it has reverse storage order, it needs to be accessed as a
6864 scalar field with reverse storage order and we must first put the
6865 value into target order. */
6866 if (AGGREGATE_TYPE_P (TREE_TYPE (exp))
6867 && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT)
6869 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (temp));
6871 reverse = TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (exp));
6873 if (reverse)
6874 temp = flip_storage_order (GET_MODE (temp), temp);
6876 if (bitsize < size
6877 && reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN
6878 && !(mode == BLKmode && bitsize > BITS_PER_WORD))
6879 temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6880 size - bitsize, NULL_RTX, 1);
6883 /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE. */
6884 if (mode != VOIDmode && mode != BLKmode
6885 && mode != TYPE_MODE (TREE_TYPE (exp)))
6886 temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6888 /* If the mode of TEMP and TARGET is BLKmode, both must be in memory
6889 and BITPOS must be aligned on a byte boundary. If so, we simply do
6890 a block copy. Likewise for a BLKmode-like TARGET. */
6891 if (GET_MODE (temp) == BLKmode
6892 && (GET_MODE (target) == BLKmode
6893 || (MEM_P (target)
6894 && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6895 && (bitpos % BITS_PER_UNIT) == 0
6896 && (bitsize % BITS_PER_UNIT) == 0)))
6898 gcc_assert (MEM_P (target) && MEM_P (temp)
6899 && (bitpos % BITS_PER_UNIT) == 0);
6901 target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6902 emit_block_move (target, temp,
6903 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6904 / BITS_PER_UNIT),
6905 BLOCK_OP_NORMAL);
6907 return const0_rtx;
6910 /* If the mode of TEMP is still BLKmode and BITSIZE not larger than the
6911 word size, we need to load the value (see again store_bit_field). */
6912 if (GET_MODE (temp) == BLKmode && bitsize <= BITS_PER_WORD)
6914 machine_mode temp_mode = smallest_mode_for_size (bitsize, MODE_INT);
6915 temp = extract_bit_field (temp, bitsize, 0, 1, NULL_RTX, temp_mode,
6916 temp_mode, false, NULL);
6919 /* Store the value in the bitfield. */
6920 store_bit_field (target, bitsize, bitpos,
6921 bitregion_start, bitregion_end,
6922 mode, temp, reverse);
6924 return const0_rtx;
6926 else
6928 /* Now build a reference to just the desired component. */
6929 rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6931 if (to_rtx == target)
6932 to_rtx = copy_rtx (to_rtx);
6934 if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6935 set_mem_alias_set (to_rtx, alias_set);
6937 /* Above we avoided using bitfield operations for storing a CONSTRUCTOR
6938 into a target smaller than its type; handle that case now. */
6939 if (TREE_CODE (exp) == CONSTRUCTOR && bitsize >= 0)
6941 gcc_assert (bitsize % BITS_PER_UNIT == 0);
6942 store_constructor (exp, to_rtx, 0, bitsize / BITS_PER_UNIT, reverse);
6943 return to_rtx;
6946 return store_expr (exp, to_rtx, 0, nontemporal, reverse);
6950 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6951 an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6952 codes and find the ultimate containing object, which we return.
6954 We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6955 bit position, *PUNSIGNEDP to the signedness and *PREVERSEP to the
6956 storage order of the field.
6957 If the position of the field is variable, we store a tree
6958 giving the variable offset (in units) in *POFFSET.
6959 This offset is in addition to the bit position.
6960 If the position is not variable, we store 0 in *POFFSET.
6962 If any of the extraction expressions is volatile,
6963 we store 1 in *PVOLATILEP. Otherwise we don't change that.
6965 If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6966 Otherwise, it is a mode that can be used to access the field.
6968 If the field describes a variable-sized object, *PMODE is set to
6969 BLKmode and *PBITSIZE is set to -1. An access cannot be made in
6970 this case, but the address of the object can be found. */
6972 tree
6973 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6974 HOST_WIDE_INT *pbitpos, tree *poffset,
6975 machine_mode *pmode, int *punsignedp,
6976 int *preversep, int *pvolatilep)
6978 tree size_tree = 0;
6979 machine_mode mode = VOIDmode;
6980 bool blkmode_bitfield = false;
6981 tree offset = size_zero_node;
6982 offset_int bit_offset = 0;
6984 /* First get the mode, signedness, storage order and size. We do this from
6985 just the outermost expression. */
6986 *pbitsize = -1;
6987 if (TREE_CODE (exp) == COMPONENT_REF)
6989 tree field = TREE_OPERAND (exp, 1);
6990 size_tree = DECL_SIZE (field);
6991 if (flag_strict_volatile_bitfields > 0
6992 && TREE_THIS_VOLATILE (exp)
6993 && DECL_BIT_FIELD_TYPE (field)
6994 && DECL_MODE (field) != BLKmode)
6995 /* Volatile bitfields should be accessed in the mode of the
6996 field's type, not the mode computed based on the bit
6997 size. */
6998 mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6999 else if (!DECL_BIT_FIELD (field))
7000 mode = DECL_MODE (field);
7001 else if (DECL_MODE (field) == BLKmode)
7002 blkmode_bitfield = true;
7004 *punsignedp = DECL_UNSIGNED (field);
7006 else if (TREE_CODE (exp) == BIT_FIELD_REF)
7008 size_tree = TREE_OPERAND (exp, 1);
7009 *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
7010 || TYPE_UNSIGNED (TREE_TYPE (exp)));
7012 /* For vector types, with the correct size of access, use the mode of
7013 inner type. */
7014 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
7015 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
7016 && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
7017 mode = TYPE_MODE (TREE_TYPE (exp));
7019 else
7021 mode = TYPE_MODE (TREE_TYPE (exp));
7022 *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
7024 if (mode == BLKmode)
7025 size_tree = TYPE_SIZE (TREE_TYPE (exp));
7026 else
7027 *pbitsize = GET_MODE_BITSIZE (mode);
7030 if (size_tree != 0)
7032 if (! tree_fits_uhwi_p (size_tree))
7033 mode = BLKmode, *pbitsize = -1;
7034 else
7035 *pbitsize = tree_to_uhwi (size_tree);
7038 *preversep = reverse_storage_order_for_component_p (exp);
7040 /* Compute cumulative bit-offset for nested component-refs and array-refs,
7041 and find the ultimate containing object. */
7042 while (1)
7044 switch (TREE_CODE (exp))
7046 case BIT_FIELD_REF:
7047 bit_offset += wi::to_offset (TREE_OPERAND (exp, 2));
7048 break;
7050 case COMPONENT_REF:
7052 tree field = TREE_OPERAND (exp, 1);
7053 tree this_offset = component_ref_field_offset (exp);
7055 /* If this field hasn't been filled in yet, don't go past it.
7056 This should only happen when folding expressions made during
7057 type construction. */
7058 if (this_offset == 0)
7059 break;
7061 offset = size_binop (PLUS_EXPR, offset, this_offset);
7062 bit_offset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
7064 /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN. */
7066 break;
7068 case ARRAY_REF:
7069 case ARRAY_RANGE_REF:
7071 tree index = TREE_OPERAND (exp, 1);
7072 tree low_bound = array_ref_low_bound (exp);
7073 tree unit_size = array_ref_element_size (exp);
7075 /* We assume all arrays have sizes that are a multiple of a byte.
7076 First subtract the lower bound, if any, in the type of the
7077 index, then convert to sizetype and multiply by the size of
7078 the array element. */
7079 if (! integer_zerop (low_bound))
7080 index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
7081 index, low_bound);
7083 offset = size_binop (PLUS_EXPR, offset,
7084 size_binop (MULT_EXPR,
7085 fold_convert (sizetype, index),
7086 unit_size));
7088 break;
7090 case REALPART_EXPR:
7091 break;
7093 case IMAGPART_EXPR:
7094 bit_offset += *pbitsize;
7095 break;
7097 case VIEW_CONVERT_EXPR:
7098 break;
7100 case MEM_REF:
7101 /* Hand back the decl for MEM[&decl, off]. */
7102 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
7104 tree off = TREE_OPERAND (exp, 1);
7105 if (!integer_zerop (off))
7107 offset_int boff, coff = mem_ref_offset (exp);
7108 boff = coff << LOG2_BITS_PER_UNIT;
7109 bit_offset += boff;
7111 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7113 goto done;
7115 default:
7116 goto done;
7119 /* If any reference in the chain is volatile, the effect is volatile. */
7120 if (TREE_THIS_VOLATILE (exp))
7121 *pvolatilep = 1;
7123 exp = TREE_OPERAND (exp, 0);
7125 done:
7127 /* If OFFSET is constant, see if we can return the whole thing as a
7128 constant bit position. Make sure to handle overflow during
7129 this conversion. */
7130 if (TREE_CODE (offset) == INTEGER_CST)
7132 offset_int tem = wi::sext (wi::to_offset (offset),
7133 TYPE_PRECISION (sizetype));
7134 tem <<= LOG2_BITS_PER_UNIT;
7135 tem += bit_offset;
7136 if (wi::fits_shwi_p (tem))
7138 *pbitpos = tem.to_shwi ();
7139 *poffset = offset = NULL_TREE;
7143 /* Otherwise, split it up. */
7144 if (offset)
7146 /* Avoid returning a negative bitpos as this may wreak havoc later. */
7147 if (wi::neg_p (bit_offset) || !wi::fits_shwi_p (bit_offset))
7149 offset_int mask = wi::mask <offset_int> (LOG2_BITS_PER_UNIT, false);
7150 offset_int tem = bit_offset.and_not (mask);
7151 /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
7152 Subtract it to BIT_OFFSET and add it (scaled) to OFFSET. */
7153 bit_offset -= tem;
7154 tem >>= LOG2_BITS_PER_UNIT;
7155 offset = size_binop (PLUS_EXPR, offset,
7156 wide_int_to_tree (sizetype, tem));
7159 *pbitpos = bit_offset.to_shwi ();
7160 *poffset = offset;
7163 /* We can use BLKmode for a byte-aligned BLKmode bitfield. */
7164 if (mode == VOIDmode
7165 && blkmode_bitfield
7166 && (*pbitpos % BITS_PER_UNIT) == 0
7167 && (*pbitsize % BITS_PER_UNIT) == 0)
7168 *pmode = BLKmode;
7169 else
7170 *pmode = mode;
7172 return exp;
7175 /* Alignment in bits the TARGET of an assignment may be assumed to have. */
7177 static unsigned HOST_WIDE_INT
7178 target_align (const_tree target)
7180 /* We might have a chain of nested references with intermediate misaligning
7181 bitfields components, so need to recurse to find out. */
7183 unsigned HOST_WIDE_INT this_align, outer_align;
7185 switch (TREE_CODE (target))
7187 case BIT_FIELD_REF:
7188 return 1;
7190 case COMPONENT_REF:
7191 this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
7192 outer_align = target_align (TREE_OPERAND (target, 0));
7193 return MIN (this_align, outer_align);
7195 case ARRAY_REF:
7196 case ARRAY_RANGE_REF:
7197 this_align = TYPE_ALIGN (TREE_TYPE (target));
7198 outer_align = target_align (TREE_OPERAND (target, 0));
7199 return MIN (this_align, outer_align);
7201 CASE_CONVERT:
7202 case NON_LVALUE_EXPR:
7203 case VIEW_CONVERT_EXPR:
7204 this_align = TYPE_ALIGN (TREE_TYPE (target));
7205 outer_align = target_align (TREE_OPERAND (target, 0));
7206 return MAX (this_align, outer_align);
7208 default:
7209 return TYPE_ALIGN (TREE_TYPE (target));
7214 /* Given an rtx VALUE that may contain additions and multiplications, return
7215 an equivalent value that just refers to a register, memory, or constant.
7216 This is done by generating instructions to perform the arithmetic and
7217 returning a pseudo-register containing the value.
7219 The returned value may be a REG, SUBREG, MEM or constant. */
7222 force_operand (rtx value, rtx target)
7224 rtx op1, op2;
7225 /* Use subtarget as the target for operand 0 of a binary operation. */
7226 rtx subtarget = get_subtarget (target);
7227 enum rtx_code code = GET_CODE (value);
7229 /* Check for subreg applied to an expression produced by loop optimizer. */
7230 if (code == SUBREG
7231 && !REG_P (SUBREG_REG (value))
7232 && !MEM_P (SUBREG_REG (value)))
7234 value
7235 = simplify_gen_subreg (GET_MODE (value),
7236 force_reg (GET_MODE (SUBREG_REG (value)),
7237 force_operand (SUBREG_REG (value),
7238 NULL_RTX)),
7239 GET_MODE (SUBREG_REG (value)),
7240 SUBREG_BYTE (value));
7241 code = GET_CODE (value);
7244 /* Check for a PIC address load. */
7245 if ((code == PLUS || code == MINUS)
7246 && XEXP (value, 0) == pic_offset_table_rtx
7247 && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
7248 || GET_CODE (XEXP (value, 1)) == LABEL_REF
7249 || GET_CODE (XEXP (value, 1)) == CONST))
7251 if (!subtarget)
7252 subtarget = gen_reg_rtx (GET_MODE (value));
7253 emit_move_insn (subtarget, value);
7254 return subtarget;
7257 if (ARITHMETIC_P (value))
7259 op2 = XEXP (value, 1);
7260 if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
7261 subtarget = 0;
7262 if (code == MINUS && CONST_INT_P (op2))
7264 code = PLUS;
7265 op2 = negate_rtx (GET_MODE (value), op2);
7268 /* Check for an addition with OP2 a constant integer and our first
7269 operand a PLUS of a virtual register and something else. In that
7270 case, we want to emit the sum of the virtual register and the
7271 constant first and then add the other value. This allows virtual
7272 register instantiation to simply modify the constant rather than
7273 creating another one around this addition. */
7274 if (code == PLUS && CONST_INT_P (op2)
7275 && GET_CODE (XEXP (value, 0)) == PLUS
7276 && REG_P (XEXP (XEXP (value, 0), 0))
7277 && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
7278 && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
7280 rtx temp = expand_simple_binop (GET_MODE (value), code,
7281 XEXP (XEXP (value, 0), 0), op2,
7282 subtarget, 0, OPTAB_LIB_WIDEN);
7283 return expand_simple_binop (GET_MODE (value), code, temp,
7284 force_operand (XEXP (XEXP (value,
7285 0), 1), 0),
7286 target, 0, OPTAB_LIB_WIDEN);
7289 op1 = force_operand (XEXP (value, 0), subtarget);
7290 op2 = force_operand (op2, NULL_RTX);
7291 switch (code)
7293 case MULT:
7294 return expand_mult (GET_MODE (value), op1, op2, target, 1);
7295 case DIV:
7296 if (!INTEGRAL_MODE_P (GET_MODE (value)))
7297 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7298 target, 1, OPTAB_LIB_WIDEN);
7299 else
7300 return expand_divmod (0,
7301 FLOAT_MODE_P (GET_MODE (value))
7302 ? RDIV_EXPR : TRUNC_DIV_EXPR,
7303 GET_MODE (value), op1, op2, target, 0);
7304 case MOD:
7305 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7306 target, 0);
7307 case UDIV:
7308 return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7309 target, 1);
7310 case UMOD:
7311 return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7312 target, 1);
7313 case ASHIFTRT:
7314 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7315 target, 0, OPTAB_LIB_WIDEN);
7316 default:
7317 return expand_simple_binop (GET_MODE (value), code, op1, op2,
7318 target, 1, OPTAB_LIB_WIDEN);
7321 if (UNARY_P (value))
7323 if (!target)
7324 target = gen_reg_rtx (GET_MODE (value));
7325 op1 = force_operand (XEXP (value, 0), NULL_RTX);
7326 switch (code)
7328 case ZERO_EXTEND:
7329 case SIGN_EXTEND:
7330 case TRUNCATE:
7331 case FLOAT_EXTEND:
7332 case FLOAT_TRUNCATE:
7333 convert_move (target, op1, code == ZERO_EXTEND);
7334 return target;
7336 case FIX:
7337 case UNSIGNED_FIX:
7338 expand_fix (target, op1, code == UNSIGNED_FIX);
7339 return target;
7341 case FLOAT:
7342 case UNSIGNED_FLOAT:
7343 expand_float (target, op1, code == UNSIGNED_FLOAT);
7344 return target;
7346 default:
7347 return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7351 #ifdef INSN_SCHEDULING
7352 /* On machines that have insn scheduling, we want all memory reference to be
7353 explicit, so we need to deal with such paradoxical SUBREGs. */
7354 if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7355 value
7356 = simplify_gen_subreg (GET_MODE (value),
7357 force_reg (GET_MODE (SUBREG_REG (value)),
7358 force_operand (SUBREG_REG (value),
7359 NULL_RTX)),
7360 GET_MODE (SUBREG_REG (value)),
7361 SUBREG_BYTE (value));
7362 #endif
7364 return value;
7367 /* Subroutine of expand_expr: return nonzero iff there is no way that
7368 EXP can reference X, which is being modified. TOP_P is nonzero if this
7369 call is going to be used to determine whether we need a temporary
7370 for EXP, as opposed to a recursive call to this function.
7372 It is always safe for this routine to return zero since it merely
7373 searches for optimization opportunities. */
7376 safe_from_p (const_rtx x, tree exp, int top_p)
7378 rtx exp_rtl = 0;
7379 int i, nops;
7381 if (x == 0
7382 /* If EXP has varying size, we MUST use a target since we currently
7383 have no way of allocating temporaries of variable size
7384 (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7385 So we assume here that something at a higher level has prevented a
7386 clash. This is somewhat bogus, but the best we can do. Only
7387 do this when X is BLKmode and when we are at the top level. */
7388 || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7389 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7390 && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7391 || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7392 || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7393 != INTEGER_CST)
7394 && GET_MODE (x) == BLKmode)
7395 /* If X is in the outgoing argument area, it is always safe. */
7396 || (MEM_P (x)
7397 && (XEXP (x, 0) == virtual_outgoing_args_rtx
7398 || (GET_CODE (XEXP (x, 0)) == PLUS
7399 && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7400 return 1;
7402 /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7403 find the underlying pseudo. */
7404 if (GET_CODE (x) == SUBREG)
7406 x = SUBREG_REG (x);
7407 if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7408 return 0;
7411 /* Now look at our tree code and possibly recurse. */
7412 switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7414 case tcc_declaration:
7415 exp_rtl = DECL_RTL_IF_SET (exp);
7416 break;
7418 case tcc_constant:
7419 return 1;
7421 case tcc_exceptional:
7422 if (TREE_CODE (exp) == TREE_LIST)
7424 while (1)
7426 if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7427 return 0;
7428 exp = TREE_CHAIN (exp);
7429 if (!exp)
7430 return 1;
7431 if (TREE_CODE (exp) != TREE_LIST)
7432 return safe_from_p (x, exp, 0);
7435 else if (TREE_CODE (exp) == CONSTRUCTOR)
7437 constructor_elt *ce;
7438 unsigned HOST_WIDE_INT idx;
7440 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (exp), idx, ce)
7441 if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7442 || !safe_from_p (x, ce->value, 0))
7443 return 0;
7444 return 1;
7446 else if (TREE_CODE (exp) == ERROR_MARK)
7447 return 1; /* An already-visited SAVE_EXPR? */
7448 else
7449 return 0;
7451 case tcc_statement:
7452 /* The only case we look at here is the DECL_INITIAL inside a
7453 DECL_EXPR. */
7454 return (TREE_CODE (exp) != DECL_EXPR
7455 || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7456 || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7457 || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7459 case tcc_binary:
7460 case tcc_comparison:
7461 if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7462 return 0;
7463 /* Fall through. */
7465 case tcc_unary:
7466 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7468 case tcc_expression:
7469 case tcc_reference:
7470 case tcc_vl_exp:
7471 /* Now do code-specific tests. EXP_RTL is set to any rtx we find in
7472 the expression. If it is set, we conflict iff we are that rtx or
7473 both are in memory. Otherwise, we check all operands of the
7474 expression recursively. */
7476 switch (TREE_CODE (exp))
7478 case ADDR_EXPR:
7479 /* If the operand is static or we are static, we can't conflict.
7480 Likewise if we don't conflict with the operand at all. */
7481 if (staticp (TREE_OPERAND (exp, 0))
7482 || TREE_STATIC (exp)
7483 || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7484 return 1;
7486 /* Otherwise, the only way this can conflict is if we are taking
7487 the address of a DECL a that address if part of X, which is
7488 very rare. */
7489 exp = TREE_OPERAND (exp, 0);
7490 if (DECL_P (exp))
7492 if (!DECL_RTL_SET_P (exp)
7493 || !MEM_P (DECL_RTL (exp)))
7494 return 0;
7495 else
7496 exp_rtl = XEXP (DECL_RTL (exp), 0);
7498 break;
7500 case MEM_REF:
7501 if (MEM_P (x)
7502 && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7503 get_alias_set (exp)))
7504 return 0;
7505 break;
7507 case CALL_EXPR:
7508 /* Assume that the call will clobber all hard registers and
7509 all of memory. */
7510 if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7511 || MEM_P (x))
7512 return 0;
7513 break;
7515 case WITH_CLEANUP_EXPR:
7516 case CLEANUP_POINT_EXPR:
7517 /* Lowered by gimplify.c. */
7518 gcc_unreachable ();
7520 case SAVE_EXPR:
7521 return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7523 default:
7524 break;
7527 /* If we have an rtx, we do not need to scan our operands. */
7528 if (exp_rtl)
7529 break;
7531 nops = TREE_OPERAND_LENGTH (exp);
7532 for (i = 0; i < nops; i++)
7533 if (TREE_OPERAND (exp, i) != 0
7534 && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7535 return 0;
7537 break;
7539 case tcc_type:
7540 /* Should never get a type here. */
7541 gcc_unreachable ();
7544 /* If we have an rtl, find any enclosed object. Then see if we conflict
7545 with it. */
7546 if (exp_rtl)
7548 if (GET_CODE (exp_rtl) == SUBREG)
7550 exp_rtl = SUBREG_REG (exp_rtl);
7551 if (REG_P (exp_rtl)
7552 && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7553 return 0;
7556 /* If the rtl is X, then it is not safe. Otherwise, it is unless both
7557 are memory and they conflict. */
7558 return ! (rtx_equal_p (x, exp_rtl)
7559 || (MEM_P (x) && MEM_P (exp_rtl)
7560 && true_dependence (exp_rtl, VOIDmode, x)));
7563 /* If we reach here, it is safe. */
7564 return 1;
7568 /* Return the highest power of two that EXP is known to be a multiple of.
7569 This is used in updating alignment of MEMs in array references. */
7571 unsigned HOST_WIDE_INT
7572 highest_pow2_factor (const_tree exp)
7574 unsigned HOST_WIDE_INT ret;
7575 int trailing_zeros = tree_ctz (exp);
7576 if (trailing_zeros >= HOST_BITS_PER_WIDE_INT)
7577 return BIGGEST_ALIGNMENT;
7578 ret = HOST_WIDE_INT_1U << trailing_zeros;
7579 if (ret > BIGGEST_ALIGNMENT)
7580 return BIGGEST_ALIGNMENT;
7581 return ret;
7584 /* Similar, except that the alignment requirements of TARGET are
7585 taken into account. Assume it is at least as aligned as its
7586 type, unless it is a COMPONENT_REF in which case the layout of
7587 the structure gives the alignment. */
7589 static unsigned HOST_WIDE_INT
7590 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7592 unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7593 unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7595 return MAX (factor, talign);
7598 /* Convert the tree comparison code TCODE to the rtl one where the
7599 signedness is UNSIGNEDP. */
7601 static enum rtx_code
7602 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7604 enum rtx_code code;
7605 switch (tcode)
7607 case EQ_EXPR:
7608 code = EQ;
7609 break;
7610 case NE_EXPR:
7611 code = NE;
7612 break;
7613 case LT_EXPR:
7614 code = unsignedp ? LTU : LT;
7615 break;
7616 case LE_EXPR:
7617 code = unsignedp ? LEU : LE;
7618 break;
7619 case GT_EXPR:
7620 code = unsignedp ? GTU : GT;
7621 break;
7622 case GE_EXPR:
7623 code = unsignedp ? GEU : GE;
7624 break;
7625 case UNORDERED_EXPR:
7626 code = UNORDERED;
7627 break;
7628 case ORDERED_EXPR:
7629 code = ORDERED;
7630 break;
7631 case UNLT_EXPR:
7632 code = UNLT;
7633 break;
7634 case UNLE_EXPR:
7635 code = UNLE;
7636 break;
7637 case UNGT_EXPR:
7638 code = UNGT;
7639 break;
7640 case UNGE_EXPR:
7641 code = UNGE;
7642 break;
7643 case UNEQ_EXPR:
7644 code = UNEQ;
7645 break;
7646 case LTGT_EXPR:
7647 code = LTGT;
7648 break;
7650 default:
7651 gcc_unreachable ();
7653 return code;
7656 /* Subroutine of expand_expr. Expand the two operands of a binary
7657 expression EXP0 and EXP1 placing the results in OP0 and OP1.
7658 The value may be stored in TARGET if TARGET is nonzero. The
7659 MODIFIER argument is as documented by expand_expr. */
7661 void
7662 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7663 enum expand_modifier modifier)
7665 if (! safe_from_p (target, exp1, 1))
7666 target = 0;
7667 if (operand_equal_p (exp0, exp1, 0))
7669 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7670 *op1 = copy_rtx (*op0);
7672 else
7674 *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7675 *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7680 /* Return a MEM that contains constant EXP. DEFER is as for
7681 output_constant_def and MODIFIER is as for expand_expr. */
7683 static rtx
7684 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7686 rtx mem;
7688 mem = output_constant_def (exp, defer);
7689 if (modifier != EXPAND_INITIALIZER)
7690 mem = use_anchored_address (mem);
7691 return mem;
7694 /* A subroutine of expand_expr_addr_expr. Evaluate the address of EXP.
7695 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7697 static rtx
7698 expand_expr_addr_expr_1 (tree exp, rtx target, machine_mode tmode,
7699 enum expand_modifier modifier, addr_space_t as)
7701 rtx result, subtarget;
7702 tree inner, offset;
7703 HOST_WIDE_INT bitsize, bitpos;
7704 int unsignedp, reversep, volatilep = 0;
7705 machine_mode mode1;
7707 /* If we are taking the address of a constant and are at the top level,
7708 we have to use output_constant_def since we can't call force_const_mem
7709 at top level. */
7710 /* ??? This should be considered a front-end bug. We should not be
7711 generating ADDR_EXPR of something that isn't an LVALUE. The only
7712 exception here is STRING_CST. */
7713 if (CONSTANT_CLASS_P (exp))
7715 result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7716 if (modifier < EXPAND_SUM)
7717 result = force_operand (result, target);
7718 return result;
7721 /* Everything must be something allowed by is_gimple_addressable. */
7722 switch (TREE_CODE (exp))
7724 case INDIRECT_REF:
7725 /* This case will happen via recursion for &a->b. */
7726 return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7728 case MEM_REF:
7730 tree tem = TREE_OPERAND (exp, 0);
7731 if (!integer_zerop (TREE_OPERAND (exp, 1)))
7732 tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7733 return expand_expr (tem, target, tmode, modifier);
7736 case CONST_DECL:
7737 /* Expand the initializer like constants above. */
7738 result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7739 0, modifier), 0);
7740 if (modifier < EXPAND_SUM)
7741 result = force_operand (result, target);
7742 return result;
7744 case REALPART_EXPR:
7745 /* The real part of the complex number is always first, therefore
7746 the address is the same as the address of the parent object. */
7747 offset = 0;
7748 bitpos = 0;
7749 inner = TREE_OPERAND (exp, 0);
7750 break;
7752 case IMAGPART_EXPR:
7753 /* The imaginary part of the complex number is always second.
7754 The expression is therefore always offset by the size of the
7755 scalar type. */
7756 offset = 0;
7757 bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7758 inner = TREE_OPERAND (exp, 0);
7759 break;
7761 case COMPOUND_LITERAL_EXPR:
7762 /* Allow COMPOUND_LITERAL_EXPR in initializers or coming from
7763 initializers, if e.g. rtl_for_decl_init is called on DECL_INITIAL
7764 with COMPOUND_LITERAL_EXPRs in it, or ARRAY_REF on a const static
7765 array with address of COMPOUND_LITERAL_EXPR in DECL_INITIAL;
7766 the initializers aren't gimplified. */
7767 if (COMPOUND_LITERAL_EXPR_DECL (exp)
7768 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
7769 return expand_expr_addr_expr_1 (COMPOUND_LITERAL_EXPR_DECL (exp),
7770 target, tmode, modifier, as);
7771 /* FALLTHRU */
7772 default:
7773 /* If the object is a DECL, then expand it for its rtl. Don't bypass
7774 expand_expr, as that can have various side effects; LABEL_DECLs for
7775 example, may not have their DECL_RTL set yet. Expand the rtl of
7776 CONSTRUCTORs too, which should yield a memory reference for the
7777 constructor's contents. Assume language specific tree nodes can
7778 be expanded in some interesting way. */
7779 gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7780 if (DECL_P (exp)
7781 || TREE_CODE (exp) == CONSTRUCTOR
7782 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7784 result = expand_expr (exp, target, tmode,
7785 modifier == EXPAND_INITIALIZER
7786 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7788 /* If the DECL isn't in memory, then the DECL wasn't properly
7789 marked TREE_ADDRESSABLE, which will be either a front-end
7790 or a tree optimizer bug. */
7792 gcc_assert (MEM_P (result));
7793 result = XEXP (result, 0);
7795 /* ??? Is this needed anymore? */
7796 if (DECL_P (exp))
7797 TREE_USED (exp) = 1;
7799 if (modifier != EXPAND_INITIALIZER
7800 && modifier != EXPAND_CONST_ADDRESS
7801 && modifier != EXPAND_SUM)
7802 result = force_operand (result, target);
7803 return result;
7806 /* Pass FALSE as the last argument to get_inner_reference although
7807 we are expanding to RTL. The rationale is that we know how to
7808 handle "aligning nodes" here: we can just bypass them because
7809 they won't change the final object whose address will be returned
7810 (they actually exist only for that purpose). */
7811 inner = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
7812 &unsignedp, &reversep, &volatilep);
7813 break;
7816 /* We must have made progress. */
7817 gcc_assert (inner != exp);
7819 subtarget = offset || bitpos ? NULL_RTX : target;
7820 /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7821 inner alignment, force the inner to be sufficiently aligned. */
7822 if (CONSTANT_CLASS_P (inner)
7823 && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7825 inner = copy_node (inner);
7826 TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7827 SET_TYPE_ALIGN (TREE_TYPE (inner), TYPE_ALIGN (TREE_TYPE (exp)));
7828 TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7830 result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7832 if (offset)
7834 rtx tmp;
7836 if (modifier != EXPAND_NORMAL)
7837 result = force_operand (result, NULL);
7838 tmp = expand_expr (offset, NULL_RTX, tmode,
7839 modifier == EXPAND_INITIALIZER
7840 ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7842 /* expand_expr is allowed to return an object in a mode other
7843 than TMODE. If it did, we need to convert. */
7844 if (GET_MODE (tmp) != VOIDmode && tmode != GET_MODE (tmp))
7845 tmp = convert_modes (tmode, GET_MODE (tmp),
7846 tmp, TYPE_UNSIGNED (TREE_TYPE (offset)));
7847 result = convert_memory_address_addr_space (tmode, result, as);
7848 tmp = convert_memory_address_addr_space (tmode, tmp, as);
7850 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7851 result = simplify_gen_binary (PLUS, tmode, result, tmp);
7852 else
7854 subtarget = bitpos ? NULL_RTX : target;
7855 result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7856 1, OPTAB_LIB_WIDEN);
7860 if (bitpos)
7862 /* Someone beforehand should have rejected taking the address
7863 of such an object. */
7864 gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7866 result = convert_memory_address_addr_space (tmode, result, as);
7867 result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7868 if (modifier < EXPAND_SUM)
7869 result = force_operand (result, target);
7872 return result;
7875 /* A subroutine of expand_expr. Evaluate EXP, which is an ADDR_EXPR.
7876 The TARGET, TMODE and MODIFIER arguments are as for expand_expr. */
7878 static rtx
7879 expand_expr_addr_expr (tree exp, rtx target, machine_mode tmode,
7880 enum expand_modifier modifier)
7882 addr_space_t as = ADDR_SPACE_GENERIC;
7883 machine_mode address_mode = Pmode;
7884 machine_mode pointer_mode = ptr_mode;
7885 machine_mode rmode;
7886 rtx result;
7888 /* Target mode of VOIDmode says "whatever's natural". */
7889 if (tmode == VOIDmode)
7890 tmode = TYPE_MODE (TREE_TYPE (exp));
7892 if (POINTER_TYPE_P (TREE_TYPE (exp)))
7894 as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7895 address_mode = targetm.addr_space.address_mode (as);
7896 pointer_mode = targetm.addr_space.pointer_mode (as);
7899 /* We can get called with some Weird Things if the user does silliness
7900 like "(short) &a". In that case, convert_memory_address won't do
7901 the right thing, so ignore the given target mode. */
7902 if (tmode != address_mode && tmode != pointer_mode)
7903 tmode = address_mode;
7905 result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7906 tmode, modifier, as);
7908 /* Despite expand_expr claims concerning ignoring TMODE when not
7909 strictly convenient, stuff breaks if we don't honor it. Note
7910 that combined with the above, we only do this for pointer modes. */
7911 rmode = GET_MODE (result);
7912 if (rmode == VOIDmode)
7913 rmode = tmode;
7914 if (rmode != tmode)
7915 result = convert_memory_address_addr_space (tmode, result, as);
7917 return result;
7920 /* Generate code for computing CONSTRUCTOR EXP.
7921 An rtx for the computed value is returned. If AVOID_TEMP_MEM
7922 is TRUE, instead of creating a temporary variable in memory
7923 NULL is returned and the caller needs to handle it differently. */
7925 static rtx
7926 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7927 bool avoid_temp_mem)
7929 tree type = TREE_TYPE (exp);
7930 machine_mode mode = TYPE_MODE (type);
7932 /* Try to avoid creating a temporary at all. This is possible
7933 if all of the initializer is zero.
7934 FIXME: try to handle all [0..255] initializers we can handle
7935 with memset. */
7936 if (TREE_STATIC (exp)
7937 && !TREE_ADDRESSABLE (exp)
7938 && target != 0 && mode == BLKmode
7939 && all_zeros_p (exp))
7941 clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7942 return target;
7945 /* All elts simple constants => refer to a constant in memory. But
7946 if this is a non-BLKmode mode, let it store a field at a time
7947 since that should make a CONST_INT, CONST_WIDE_INT or
7948 CONST_DOUBLE when we fold. Likewise, if we have a target we can
7949 use, it is best to store directly into the target unless the type
7950 is large enough that memcpy will be used. If we are making an
7951 initializer and all operands are constant, put it in memory as
7952 well.
7954 FIXME: Avoid trying to fill vector constructors piece-meal.
7955 Output them with output_constant_def below unless we're sure
7956 they're zeros. This should go away when vector initializers
7957 are treated like VECTOR_CST instead of arrays. */
7958 if ((TREE_STATIC (exp)
7959 && ((mode == BLKmode
7960 && ! (target != 0 && safe_from_p (target, exp, 1)))
7961 || TREE_ADDRESSABLE (exp)
7962 || (tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7963 && (! can_move_by_pieces
7964 (tree_to_uhwi (TYPE_SIZE_UNIT (type)),
7965 TYPE_ALIGN (type)))
7966 && ! mostly_zeros_p (exp))))
7967 || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7968 && TREE_CONSTANT (exp)))
7970 rtx constructor;
7972 if (avoid_temp_mem)
7973 return NULL_RTX;
7975 constructor = expand_expr_constant (exp, 1, modifier);
7977 if (modifier != EXPAND_CONST_ADDRESS
7978 && modifier != EXPAND_INITIALIZER
7979 && modifier != EXPAND_SUM)
7980 constructor = validize_mem (constructor);
7982 return constructor;
7985 /* Handle calls that pass values in multiple non-contiguous
7986 locations. The Irix 6 ABI has examples of this. */
7987 if (target == 0 || ! safe_from_p (target, exp, 1)
7988 || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7990 if (avoid_temp_mem)
7991 return NULL_RTX;
7993 target = assign_temp (type, TREE_ADDRESSABLE (exp), 1);
7996 store_constructor (exp, target, 0, int_expr_size (exp), false);
7997 return target;
8001 /* expand_expr: generate code for computing expression EXP.
8002 An rtx for the computed value is returned. The value is never null.
8003 In the case of a void EXP, const0_rtx is returned.
8005 The value may be stored in TARGET if TARGET is nonzero.
8006 TARGET is just a suggestion; callers must assume that
8007 the rtx returned may not be the same as TARGET.
8009 If TARGET is CONST0_RTX, it means that the value will be ignored.
8011 If TMODE is not VOIDmode, it suggests generating the
8012 result in mode TMODE. But this is done only when convenient.
8013 Otherwise, TMODE is ignored and the value generated in its natural mode.
8014 TMODE is just a suggestion; callers must assume that
8015 the rtx returned may not have mode TMODE.
8017 Note that TARGET may have neither TMODE nor MODE. In that case, it
8018 probably will not be used.
8020 If MODIFIER is EXPAND_SUM then when EXP is an addition
8021 we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
8022 or a nest of (PLUS ...) and (MINUS ...) where the terms are
8023 products as above, or REG or MEM, or constant.
8024 Ordinarily in such cases we would output mul or add instructions
8025 and then return a pseudo reg containing the sum.
8027 EXPAND_INITIALIZER is much like EXPAND_SUM except that
8028 it also marks a label as absolutely required (it can't be dead).
8029 It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
8030 This is used for outputting expressions used in initializers.
8032 EXPAND_CONST_ADDRESS says that it is okay to return a MEM
8033 with a constant address even if that address is not normally legitimate.
8034 EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
8036 EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
8037 a call parameter. Such targets require special care as we haven't yet
8038 marked TARGET so that it's safe from being trashed by libcalls. We
8039 don't want to use TARGET for anything but the final result;
8040 Intermediate values must go elsewhere. Additionally, calls to
8041 emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
8043 If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
8044 address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
8045 DECL_RTL of the VAR_DECL. *ALT_RTL is also set if EXP is a
8046 COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
8047 recursively.
8049 If INNER_REFERENCE_P is true, we are expanding an inner reference.
8050 In this case, we don't adjust a returned MEM rtx that wouldn't be
8051 sufficiently aligned for its mode; instead, it's up to the caller
8052 to deal with it afterwards. This is used to make sure that unaligned
8053 base objects for which out-of-bounds accesses are supported, for
8054 example record types with trailing arrays, aren't realigned behind
8055 the back of the caller.
8056 The normal operating mode is to pass FALSE for this parameter. */
8059 expand_expr_real (tree exp, rtx target, machine_mode tmode,
8060 enum expand_modifier modifier, rtx *alt_rtl,
8061 bool inner_reference_p)
8063 rtx ret;
8065 /* Handle ERROR_MARK before anybody tries to access its type. */
8066 if (TREE_CODE (exp) == ERROR_MARK
8067 || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
8069 ret = CONST0_RTX (tmode);
8070 return ret ? ret : const0_rtx;
8073 ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl,
8074 inner_reference_p);
8075 return ret;
8078 /* Try to expand the conditional expression which is represented by
8079 TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves. If it succeeds
8080 return the rtl reg which represents the result. Otherwise return
8081 NULL_RTX. */
8083 static rtx
8084 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
8085 tree treeop1 ATTRIBUTE_UNUSED,
8086 tree treeop2 ATTRIBUTE_UNUSED)
8088 rtx insn;
8089 rtx op00, op01, op1, op2;
8090 enum rtx_code comparison_code;
8091 machine_mode comparison_mode;
8092 gimple *srcstmt;
8093 rtx temp;
8094 tree type = TREE_TYPE (treeop1);
8095 int unsignedp = TYPE_UNSIGNED (type);
8096 machine_mode mode = TYPE_MODE (type);
8097 machine_mode orig_mode = mode;
8098 static bool expanding_cond_expr_using_cmove = false;
8100 /* Conditional move expansion can end up TERing two operands which,
8101 when recursively hitting conditional expressions can result in
8102 exponential behavior if the cmove expansion ultimatively fails.
8103 It's hardly profitable to TER a cmove into a cmove so avoid doing
8104 that by failing early if we end up recursing. */
8105 if (expanding_cond_expr_using_cmove)
8106 return NULL_RTX;
8108 /* If we cannot do a conditional move on the mode, try doing it
8109 with the promoted mode. */
8110 if (!can_conditionally_move_p (mode))
8112 mode = promote_mode (type, mode, &unsignedp);
8113 if (!can_conditionally_move_p (mode))
8114 return NULL_RTX;
8115 temp = assign_temp (type, 0, 0); /* Use promoted mode for temp. */
8117 else
8118 temp = assign_temp (type, 0, 1);
8120 expanding_cond_expr_using_cmove = true;
8121 start_sequence ();
8122 expand_operands (treeop1, treeop2,
8123 temp, &op1, &op2, EXPAND_NORMAL);
8125 if (TREE_CODE (treeop0) == SSA_NAME
8126 && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
8128 tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
8129 enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
8130 op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
8131 op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
8132 comparison_mode = TYPE_MODE (type);
8133 unsignedp = TYPE_UNSIGNED (type);
8134 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8136 else if (COMPARISON_CLASS_P (treeop0))
8138 tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
8139 enum tree_code cmpcode = TREE_CODE (treeop0);
8140 op00 = expand_normal (TREE_OPERAND (treeop0, 0));
8141 op01 = expand_normal (TREE_OPERAND (treeop0, 1));
8142 unsignedp = TYPE_UNSIGNED (type);
8143 comparison_mode = TYPE_MODE (type);
8144 comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
8146 else
8148 op00 = expand_normal (treeop0);
8149 op01 = const0_rtx;
8150 comparison_code = NE;
8151 comparison_mode = GET_MODE (op00);
8152 if (comparison_mode == VOIDmode)
8153 comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
8155 expanding_cond_expr_using_cmove = false;
8157 if (GET_MODE (op1) != mode)
8158 op1 = gen_lowpart (mode, op1);
8160 if (GET_MODE (op2) != mode)
8161 op2 = gen_lowpart (mode, op2);
8163 /* Try to emit the conditional move. */
8164 insn = emit_conditional_move (temp, comparison_code,
8165 op00, op01, comparison_mode,
8166 op1, op2, mode,
8167 unsignedp);
8169 /* If we could do the conditional move, emit the sequence,
8170 and return. */
8171 if (insn)
8173 rtx_insn *seq = get_insns ();
8174 end_sequence ();
8175 emit_insn (seq);
8176 return convert_modes (orig_mode, mode, temp, 0);
8179 /* Otherwise discard the sequence and fall back to code with
8180 branches. */
8181 end_sequence ();
8182 return NULL_RTX;
8186 expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode,
8187 enum expand_modifier modifier)
8189 rtx op0, op1, op2, temp;
8190 rtx_code_label *lab;
8191 tree type;
8192 int unsignedp;
8193 machine_mode mode;
8194 enum tree_code code = ops->code;
8195 optab this_optab;
8196 rtx subtarget, original_target;
8197 int ignore;
8198 bool reduce_bit_field;
8199 location_t loc = ops->location;
8200 tree treeop0, treeop1, treeop2;
8201 #define REDUCE_BIT_FIELD(expr) (reduce_bit_field \
8202 ? reduce_to_bit_field_precision ((expr), \
8203 target, \
8204 type) \
8205 : (expr))
8207 type = ops->type;
8208 mode = TYPE_MODE (type);
8209 unsignedp = TYPE_UNSIGNED (type);
8211 treeop0 = ops->op0;
8212 treeop1 = ops->op1;
8213 treeop2 = ops->op2;
8215 /* We should be called only on simple (binary or unary) expressions,
8216 exactly those that are valid in gimple expressions that aren't
8217 GIMPLE_SINGLE_RHS (or invalid). */
8218 gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
8219 || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
8220 || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
8222 ignore = (target == const0_rtx
8223 || ((CONVERT_EXPR_CODE_P (code)
8224 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
8225 && TREE_CODE (type) == VOID_TYPE));
8227 /* We should be called only if we need the result. */
8228 gcc_assert (!ignore);
8230 /* An operation in what may be a bit-field type needs the
8231 result to be reduced to the precision of the bit-field type,
8232 which is narrower than that of the type's mode. */
8233 reduce_bit_field = (INTEGRAL_TYPE_P (type)
8234 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8236 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8237 target = 0;
8239 /* Use subtarget as the target for operand 0 of a binary operation. */
8240 subtarget = get_subtarget (target);
8241 original_target = target;
8243 switch (code)
8245 case NON_LVALUE_EXPR:
8246 case PAREN_EXPR:
8247 CASE_CONVERT:
8248 if (treeop0 == error_mark_node)
8249 return const0_rtx;
8251 if (TREE_CODE (type) == UNION_TYPE)
8253 tree valtype = TREE_TYPE (treeop0);
8255 /* If both input and output are BLKmode, this conversion isn't doing
8256 anything except possibly changing memory attribute. */
8257 if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8259 rtx result = expand_expr (treeop0, target, tmode,
8260 modifier);
8262 result = copy_rtx (result);
8263 set_mem_attributes (result, type, 0);
8264 return result;
8267 if (target == 0)
8269 if (TYPE_MODE (type) != BLKmode)
8270 target = gen_reg_rtx (TYPE_MODE (type));
8271 else
8272 target = assign_temp (type, 1, 1);
8275 if (MEM_P (target))
8276 /* Store data into beginning of memory target. */
8277 store_expr (treeop0,
8278 adjust_address (target, TYPE_MODE (valtype), 0),
8279 modifier == EXPAND_STACK_PARM,
8280 false, TYPE_REVERSE_STORAGE_ORDER (type));
8282 else
8284 gcc_assert (REG_P (target)
8285 && !TYPE_REVERSE_STORAGE_ORDER (type));
8287 /* Store this field into a union of the proper type. */
8288 store_field (target,
8289 MIN ((int_size_in_bytes (TREE_TYPE
8290 (treeop0))
8291 * BITS_PER_UNIT),
8292 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8293 0, 0, 0, TYPE_MODE (valtype), treeop0, 0,
8294 false, false);
8297 /* Return the entire union. */
8298 return target;
8301 if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8303 op0 = expand_expr (treeop0, target, VOIDmode,
8304 modifier);
8306 /* If the signedness of the conversion differs and OP0 is
8307 a promoted SUBREG, clear that indication since we now
8308 have to do the proper extension. */
8309 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8310 && GET_CODE (op0) == SUBREG)
8311 SUBREG_PROMOTED_VAR_P (op0) = 0;
8313 return REDUCE_BIT_FIELD (op0);
8316 op0 = expand_expr (treeop0, NULL_RTX, mode,
8317 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8318 if (GET_MODE (op0) == mode)
8321 /* If OP0 is a constant, just convert it into the proper mode. */
8322 else if (CONSTANT_P (op0))
8324 tree inner_type = TREE_TYPE (treeop0);
8325 machine_mode inner_mode = GET_MODE (op0);
8327 if (inner_mode == VOIDmode)
8328 inner_mode = TYPE_MODE (inner_type);
8330 if (modifier == EXPAND_INITIALIZER)
8331 op0 = lowpart_subreg (mode, op0, inner_mode);
8332 else
8333 op0= convert_modes (mode, inner_mode, op0,
8334 TYPE_UNSIGNED (inner_type));
8337 else if (modifier == EXPAND_INITIALIZER)
8338 op0 = gen_rtx_fmt_e (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8339 ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8341 else if (target == 0)
8342 op0 = convert_to_mode (mode, op0,
8343 TYPE_UNSIGNED (TREE_TYPE
8344 (treeop0)));
8345 else
8347 convert_move (target, op0,
8348 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8349 op0 = target;
8352 return REDUCE_BIT_FIELD (op0);
8354 case ADDR_SPACE_CONVERT_EXPR:
8356 tree treeop0_type = TREE_TYPE (treeop0);
8358 gcc_assert (POINTER_TYPE_P (type));
8359 gcc_assert (POINTER_TYPE_P (treeop0_type));
8361 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8362 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8364 /* Conversions between pointers to the same address space should
8365 have been implemented via CONVERT_EXPR / NOP_EXPR. */
8366 gcc_assert (as_to != as_from);
8368 op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8370 /* Ask target code to handle conversion between pointers
8371 to overlapping address spaces. */
8372 if (targetm.addr_space.subset_p (as_to, as_from)
8373 || targetm.addr_space.subset_p (as_from, as_to))
8375 op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8377 else
8379 /* For disjoint address spaces, converting anything but a null
8380 pointer invokes undefined behavior. We truncate or extend the
8381 value as if we'd converted via integers, which handles 0 as
8382 required, and all others as the programmer likely expects. */
8383 #ifndef POINTERS_EXTEND_UNSIGNED
8384 const int POINTERS_EXTEND_UNSIGNED = 1;
8385 #endif
8386 op0 = convert_modes (mode, TYPE_MODE (treeop0_type),
8387 op0, POINTERS_EXTEND_UNSIGNED);
8389 gcc_assert (op0);
8390 return op0;
8393 case POINTER_PLUS_EXPR:
8394 /* Even though the sizetype mode and the pointer's mode can be different
8395 expand is able to handle this correctly and get the correct result out
8396 of the PLUS_EXPR code. */
8397 /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8398 if sizetype precision is smaller than pointer precision. */
8399 if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8400 treeop1 = fold_convert_loc (loc, type,
8401 fold_convert_loc (loc, ssizetype,
8402 treeop1));
8403 /* If sizetype precision is larger than pointer precision, truncate the
8404 offset to have matching modes. */
8405 else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8406 treeop1 = fold_convert_loc (loc, type, treeop1);
8407 /* FALLTHRU */
8409 case PLUS_EXPR:
8410 /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8411 something else, make sure we add the register to the constant and
8412 then to the other thing. This case can occur during strength
8413 reduction and doing it this way will produce better code if the
8414 frame pointer or argument pointer is eliminated.
8416 fold-const.c will ensure that the constant is always in the inner
8417 PLUS_EXPR, so the only case we need to do anything about is if
8418 sp, ap, or fp is our second argument, in which case we must swap
8419 the innermost first argument and our second argument. */
8421 if (TREE_CODE (treeop0) == PLUS_EXPR
8422 && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8423 && VAR_P (treeop1)
8424 && (DECL_RTL (treeop1) == frame_pointer_rtx
8425 || DECL_RTL (treeop1) == stack_pointer_rtx
8426 || DECL_RTL (treeop1) == arg_pointer_rtx))
8428 gcc_unreachable ();
8431 /* If the result is to be ptr_mode and we are adding an integer to
8432 something, we might be forming a constant. So try to use
8433 plus_constant. If it produces a sum and we can't accept it,
8434 use force_operand. This allows P = &ARR[const] to generate
8435 efficient code on machines where a SYMBOL_REF is not a valid
8436 address.
8438 If this is an EXPAND_SUM call, always return the sum. */
8439 if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8440 || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8442 if (modifier == EXPAND_STACK_PARM)
8443 target = 0;
8444 if (TREE_CODE (treeop0) == INTEGER_CST
8445 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8446 && TREE_CONSTANT (treeop1))
8448 rtx constant_part;
8449 HOST_WIDE_INT wc;
8450 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop1));
8452 op1 = expand_expr (treeop1, subtarget, VOIDmode,
8453 EXPAND_SUM);
8454 /* Use wi::shwi to ensure that the constant is
8455 truncated according to the mode of OP1, then sign extended
8456 to a HOST_WIDE_INT. Using the constant directly can result
8457 in non-canonical RTL in a 64x32 cross compile. */
8458 wc = TREE_INT_CST_LOW (treeop0);
8459 constant_part =
8460 immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8461 op1 = plus_constant (mode, op1, INTVAL (constant_part));
8462 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8463 op1 = force_operand (op1, target);
8464 return REDUCE_BIT_FIELD (op1);
8467 else if (TREE_CODE (treeop1) == INTEGER_CST
8468 && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8469 && TREE_CONSTANT (treeop0))
8471 rtx constant_part;
8472 HOST_WIDE_INT wc;
8473 machine_mode wmode = TYPE_MODE (TREE_TYPE (treeop0));
8475 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8476 (modifier == EXPAND_INITIALIZER
8477 ? EXPAND_INITIALIZER : EXPAND_SUM));
8478 if (! CONSTANT_P (op0))
8480 op1 = expand_expr (treeop1, NULL_RTX,
8481 VOIDmode, modifier);
8482 /* Return a PLUS if modifier says it's OK. */
8483 if (modifier == EXPAND_SUM
8484 || modifier == EXPAND_INITIALIZER)
8485 return simplify_gen_binary (PLUS, mode, op0, op1);
8486 goto binop2;
8488 /* Use wi::shwi to ensure that the constant is
8489 truncated according to the mode of OP1, then sign extended
8490 to a HOST_WIDE_INT. Using the constant directly can result
8491 in non-canonical RTL in a 64x32 cross compile. */
8492 wc = TREE_INT_CST_LOW (treeop1);
8493 constant_part
8494 = immed_wide_int_const (wi::shwi (wc, wmode), wmode);
8495 op0 = plus_constant (mode, op0, INTVAL (constant_part));
8496 if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8497 op0 = force_operand (op0, target);
8498 return REDUCE_BIT_FIELD (op0);
8502 /* Use TER to expand pointer addition of a negated value
8503 as pointer subtraction. */
8504 if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8505 || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8506 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8507 && TREE_CODE (treeop1) == SSA_NAME
8508 && TYPE_MODE (TREE_TYPE (treeop0))
8509 == TYPE_MODE (TREE_TYPE (treeop1)))
8511 gimple *def = get_def_for_expr (treeop1, NEGATE_EXPR);
8512 if (def)
8514 treeop1 = gimple_assign_rhs1 (def);
8515 code = MINUS_EXPR;
8516 goto do_minus;
8520 /* No sense saving up arithmetic to be done
8521 if it's all in the wrong mode to form part of an address.
8522 And force_operand won't know whether to sign-extend or
8523 zero-extend. */
8524 if (modifier != EXPAND_INITIALIZER
8525 && (modifier != EXPAND_SUM || mode != ptr_mode))
8527 expand_operands (treeop0, treeop1,
8528 subtarget, &op0, &op1, modifier);
8529 if (op0 == const0_rtx)
8530 return op1;
8531 if (op1 == const0_rtx)
8532 return op0;
8533 goto binop2;
8536 expand_operands (treeop0, treeop1,
8537 subtarget, &op0, &op1, modifier);
8538 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8540 case MINUS_EXPR:
8541 do_minus:
8542 /* For initializers, we are allowed to return a MINUS of two
8543 symbolic constants. Here we handle all cases when both operands
8544 are constant. */
8545 /* Handle difference of two symbolic constants,
8546 for the sake of an initializer. */
8547 if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8548 && really_constant_p (treeop0)
8549 && really_constant_p (treeop1))
8551 expand_operands (treeop0, treeop1,
8552 NULL_RTX, &op0, &op1, modifier);
8554 /* If the last operand is a CONST_INT, use plus_constant of
8555 the negated constant. Else make the MINUS. */
8556 if (CONST_INT_P (op1))
8557 return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8558 -INTVAL (op1)));
8559 else
8560 return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8563 /* No sense saving up arithmetic to be done
8564 if it's all in the wrong mode to form part of an address.
8565 And force_operand won't know whether to sign-extend or
8566 zero-extend. */
8567 if (modifier != EXPAND_INITIALIZER
8568 && (modifier != EXPAND_SUM || mode != ptr_mode))
8569 goto binop;
8571 expand_operands (treeop0, treeop1,
8572 subtarget, &op0, &op1, modifier);
8574 /* Convert A - const to A + (-const). */
8575 if (CONST_INT_P (op1))
8577 op1 = negate_rtx (mode, op1);
8578 return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8581 goto binop2;
8583 case WIDEN_MULT_PLUS_EXPR:
8584 case WIDEN_MULT_MINUS_EXPR:
8585 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8586 op2 = expand_normal (treeop2);
8587 target = expand_widen_pattern_expr (ops, op0, op1, op2,
8588 target, unsignedp);
8589 return target;
8591 case WIDEN_MULT_EXPR:
8592 /* If first operand is constant, swap them.
8593 Thus the following special case checks need only
8594 check the second operand. */
8595 if (TREE_CODE (treeop0) == INTEGER_CST)
8596 std::swap (treeop0, treeop1);
8598 /* First, check if we have a multiplication of one signed and one
8599 unsigned operand. */
8600 if (TREE_CODE (treeop1) != INTEGER_CST
8601 && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8602 != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8604 machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8605 this_optab = usmul_widen_optab;
8606 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8607 != CODE_FOR_nothing)
8609 if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8610 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8611 EXPAND_NORMAL);
8612 else
8613 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8614 EXPAND_NORMAL);
8615 /* op0 and op1 might still be constant, despite the above
8616 != INTEGER_CST check. Handle it. */
8617 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8619 op0 = convert_modes (innermode, mode, op0, true);
8620 op1 = convert_modes (innermode, mode, op1, false);
8621 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8622 target, unsignedp));
8624 goto binop3;
8627 /* Check for a multiplication with matching signedness. */
8628 else if ((TREE_CODE (treeop1) == INTEGER_CST
8629 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8630 || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8631 == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8633 tree op0type = TREE_TYPE (treeop0);
8634 machine_mode innermode = TYPE_MODE (op0type);
8635 bool zextend_p = TYPE_UNSIGNED (op0type);
8636 optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8637 this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8639 if (TREE_CODE (treeop0) != INTEGER_CST)
8641 if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8642 != CODE_FOR_nothing)
8644 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8645 EXPAND_NORMAL);
8646 /* op0 and op1 might still be constant, despite the above
8647 != INTEGER_CST check. Handle it. */
8648 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8650 widen_mult_const:
8651 op0 = convert_modes (innermode, mode, op0, zextend_p);
8653 = convert_modes (innermode, mode, op1,
8654 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8655 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1,
8656 target,
8657 unsignedp));
8659 temp = expand_widening_mult (mode, op0, op1, target,
8660 unsignedp, this_optab);
8661 return REDUCE_BIT_FIELD (temp);
8663 if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8664 != CODE_FOR_nothing
8665 && innermode == word_mode)
8667 rtx htem, hipart;
8668 op0 = expand_normal (treeop0);
8669 if (TREE_CODE (treeop1) == INTEGER_CST)
8670 op1 = convert_modes (innermode, mode,
8671 expand_normal (treeop1),
8672 TYPE_UNSIGNED (TREE_TYPE (treeop1)));
8673 else
8674 op1 = expand_normal (treeop1);
8675 /* op0 and op1 might still be constant, despite the above
8676 != INTEGER_CST check. Handle it. */
8677 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
8678 goto widen_mult_const;
8679 temp = expand_binop (mode, other_optab, op0, op1, target,
8680 unsignedp, OPTAB_LIB_WIDEN);
8681 hipart = gen_highpart (innermode, temp);
8682 htem = expand_mult_highpart_adjust (innermode, hipart,
8683 op0, op1, hipart,
8684 zextend_p);
8685 if (htem != hipart)
8686 emit_move_insn (hipart, htem);
8687 return REDUCE_BIT_FIELD (temp);
8691 treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8692 treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8693 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8694 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8696 case FMA_EXPR:
8698 optab opt = fma_optab;
8699 gimple *def0, *def2;
8701 /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8702 call. */
8703 if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8705 tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8706 tree call_expr;
8708 gcc_assert (fn != NULL_TREE);
8709 call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8710 return expand_builtin (call_expr, target, subtarget, mode, false);
8713 def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8714 /* The multiplication is commutative - look at its 2nd operand
8715 if the first isn't fed by a negate. */
8716 if (!def0)
8718 def0 = get_def_for_expr (treeop1, NEGATE_EXPR);
8719 /* Swap operands if the 2nd operand is fed by a negate. */
8720 if (def0)
8721 std::swap (treeop0, treeop1);
8723 def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8725 op0 = op2 = NULL;
8727 if (def0 && def2
8728 && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8730 opt = fnms_optab;
8731 op0 = expand_normal (gimple_assign_rhs1 (def0));
8732 op2 = expand_normal (gimple_assign_rhs1 (def2));
8734 else if (def0
8735 && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8737 opt = fnma_optab;
8738 op0 = expand_normal (gimple_assign_rhs1 (def0));
8740 else if (def2
8741 && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8743 opt = fms_optab;
8744 op2 = expand_normal (gimple_assign_rhs1 (def2));
8747 if (op0 == NULL)
8748 op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8749 if (op2 == NULL)
8750 op2 = expand_normal (treeop2);
8751 op1 = expand_normal (treeop1);
8753 return expand_ternary_op (TYPE_MODE (type), opt,
8754 op0, op1, op2, target, 0);
8757 case MULT_EXPR:
8758 /* If this is a fixed-point operation, then we cannot use the code
8759 below because "expand_mult" doesn't support sat/no-sat fixed-point
8760 multiplications. */
8761 if (ALL_FIXED_POINT_MODE_P (mode))
8762 goto binop;
8764 /* If first operand is constant, swap them.
8765 Thus the following special case checks need only
8766 check the second operand. */
8767 if (TREE_CODE (treeop0) == INTEGER_CST)
8768 std::swap (treeop0, treeop1);
8770 /* Attempt to return something suitable for generating an
8771 indexed address, for machines that support that. */
8773 if (modifier == EXPAND_SUM && mode == ptr_mode
8774 && tree_fits_shwi_p (treeop1))
8776 tree exp1 = treeop1;
8778 op0 = expand_expr (treeop0, subtarget, VOIDmode,
8779 EXPAND_SUM);
8781 if (!REG_P (op0))
8782 op0 = force_operand (op0, NULL_RTX);
8783 if (!REG_P (op0))
8784 op0 = copy_to_mode_reg (mode, op0);
8786 return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8787 gen_int_mode (tree_to_shwi (exp1),
8788 TYPE_MODE (TREE_TYPE (exp1)))));
8791 if (modifier == EXPAND_STACK_PARM)
8792 target = 0;
8794 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8795 return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8797 case TRUNC_MOD_EXPR:
8798 case FLOOR_MOD_EXPR:
8799 case CEIL_MOD_EXPR:
8800 case ROUND_MOD_EXPR:
8802 case TRUNC_DIV_EXPR:
8803 case FLOOR_DIV_EXPR:
8804 case CEIL_DIV_EXPR:
8805 case ROUND_DIV_EXPR:
8806 case EXACT_DIV_EXPR:
8808 /* If this is a fixed-point operation, then we cannot use the code
8809 below because "expand_divmod" doesn't support sat/no-sat fixed-point
8810 divisions. */
8811 if (ALL_FIXED_POINT_MODE_P (mode))
8812 goto binop;
8814 if (modifier == EXPAND_STACK_PARM)
8815 target = 0;
8816 /* Possible optimization: compute the dividend with EXPAND_SUM
8817 then if the divisor is constant can optimize the case
8818 where some terms of the dividend have coeffs divisible by it. */
8819 expand_operands (treeop0, treeop1,
8820 subtarget, &op0, &op1, EXPAND_NORMAL);
8821 bool mod_p = code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR
8822 || code == CEIL_MOD_EXPR || code == ROUND_MOD_EXPR;
8823 if (SCALAR_INT_MODE_P (mode)
8824 && optimize >= 2
8825 && get_range_pos_neg (treeop0) == 1
8826 && get_range_pos_neg (treeop1) == 1)
8828 /* If both arguments are known to be positive when interpreted
8829 as signed, we can expand it as both signed and unsigned
8830 division or modulo. Choose the cheaper sequence in that case. */
8831 bool speed_p = optimize_insn_for_speed_p ();
8832 do_pending_stack_adjust ();
8833 start_sequence ();
8834 rtx uns_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 1);
8835 rtx_insn *uns_insns = get_insns ();
8836 end_sequence ();
8837 start_sequence ();
8838 rtx sgn_ret = expand_divmod (mod_p, code, mode, op0, op1, target, 0);
8839 rtx_insn *sgn_insns = get_insns ();
8840 end_sequence ();
8841 unsigned uns_cost = seq_cost (uns_insns, speed_p);
8842 unsigned sgn_cost = seq_cost (sgn_insns, speed_p);
8844 /* If costs are the same then use as tie breaker the other
8845 other factor. */
8846 if (uns_cost == sgn_cost)
8848 uns_cost = seq_cost (uns_insns, !speed_p);
8849 sgn_cost = seq_cost (sgn_insns, !speed_p);
8852 if (uns_cost < sgn_cost || (uns_cost == sgn_cost && unsignedp))
8854 emit_insn (uns_insns);
8855 return uns_ret;
8857 emit_insn (sgn_insns);
8858 return sgn_ret;
8860 return expand_divmod (mod_p, code, mode, op0, op1, target, unsignedp);
8862 case RDIV_EXPR:
8863 goto binop;
8865 case MULT_HIGHPART_EXPR:
8866 expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8867 temp = expand_mult_highpart (mode, op0, op1, target, unsignedp);
8868 gcc_assert (temp);
8869 return temp;
8871 case FIXED_CONVERT_EXPR:
8872 op0 = expand_normal (treeop0);
8873 if (target == 0 || modifier == EXPAND_STACK_PARM)
8874 target = gen_reg_rtx (mode);
8876 if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8877 && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8878 || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8879 expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8880 else
8881 expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8882 return target;
8884 case FIX_TRUNC_EXPR:
8885 op0 = expand_normal (treeop0);
8886 if (target == 0 || modifier == EXPAND_STACK_PARM)
8887 target = gen_reg_rtx (mode);
8888 expand_fix (target, op0, unsignedp);
8889 return target;
8891 case FLOAT_EXPR:
8892 op0 = expand_normal (treeop0);
8893 if (target == 0 || modifier == EXPAND_STACK_PARM)
8894 target = gen_reg_rtx (mode);
8895 /* expand_float can't figure out what to do if FROM has VOIDmode.
8896 So give it the correct mode. With -O, cse will optimize this. */
8897 if (GET_MODE (op0) == VOIDmode)
8898 op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8899 op0);
8900 expand_float (target, op0,
8901 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8902 return target;
8904 case NEGATE_EXPR:
8905 op0 = expand_expr (treeop0, subtarget,
8906 VOIDmode, EXPAND_NORMAL);
8907 if (modifier == EXPAND_STACK_PARM)
8908 target = 0;
8909 temp = expand_unop (mode,
8910 optab_for_tree_code (NEGATE_EXPR, type,
8911 optab_default),
8912 op0, target, 0);
8913 gcc_assert (temp);
8914 return REDUCE_BIT_FIELD (temp);
8916 case ABS_EXPR:
8917 op0 = expand_expr (treeop0, subtarget,
8918 VOIDmode, EXPAND_NORMAL);
8919 if (modifier == EXPAND_STACK_PARM)
8920 target = 0;
8922 /* ABS_EXPR is not valid for complex arguments. */
8923 gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8924 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8926 /* Unsigned abs is simply the operand. Testing here means we don't
8927 risk generating incorrect code below. */
8928 if (TYPE_UNSIGNED (type))
8929 return op0;
8931 return expand_abs (mode, op0, target, unsignedp,
8932 safe_from_p (target, treeop0, 1));
8934 case MAX_EXPR:
8935 case MIN_EXPR:
8936 target = original_target;
8937 if (target == 0
8938 || modifier == EXPAND_STACK_PARM
8939 || (MEM_P (target) && MEM_VOLATILE_P (target))
8940 || GET_MODE (target) != mode
8941 || (REG_P (target)
8942 && REGNO (target) < FIRST_PSEUDO_REGISTER))
8943 target = gen_reg_rtx (mode);
8944 expand_operands (treeop0, treeop1,
8945 target, &op0, &op1, EXPAND_NORMAL);
8947 /* First try to do it with a special MIN or MAX instruction.
8948 If that does not win, use a conditional jump to select the proper
8949 value. */
8950 this_optab = optab_for_tree_code (code, type, optab_default);
8951 temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8952 OPTAB_WIDEN);
8953 if (temp != 0)
8954 return temp;
8956 /* For vector MIN <x, y>, expand it a VEC_COND_EXPR <x <= y, x, y>
8957 and similarly for MAX <x, y>. */
8958 if (VECTOR_TYPE_P (type))
8960 tree t0 = make_tree (type, op0);
8961 tree t1 = make_tree (type, op1);
8962 tree comparison = build2 (code == MIN_EXPR ? LE_EXPR : GE_EXPR,
8963 type, t0, t1);
8964 return expand_vec_cond_expr (type, comparison, t0, t1,
8965 original_target);
8968 /* At this point, a MEM target is no longer useful; we will get better
8969 code without it. */
8971 if (! REG_P (target))
8972 target = gen_reg_rtx (mode);
8974 /* If op1 was placed in target, swap op0 and op1. */
8975 if (target != op0 && target == op1)
8976 std::swap (op0, op1);
8978 /* We generate better code and avoid problems with op1 mentioning
8979 target by forcing op1 into a pseudo if it isn't a constant. */
8980 if (! CONSTANT_P (op1))
8981 op1 = force_reg (mode, op1);
8984 enum rtx_code comparison_code;
8985 rtx cmpop1 = op1;
8987 if (code == MAX_EXPR)
8988 comparison_code = unsignedp ? GEU : GE;
8989 else
8990 comparison_code = unsignedp ? LEU : LE;
8992 /* Canonicalize to comparisons against 0. */
8993 if (op1 == const1_rtx)
8995 /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8996 or (a != 0 ? a : 1) for unsigned.
8997 For MIN we are safe converting (a <= 1 ? a : 1)
8998 into (a <= 0 ? a : 1) */
8999 cmpop1 = const0_rtx;
9000 if (code == MAX_EXPR)
9001 comparison_code = unsignedp ? NE : GT;
9003 if (op1 == constm1_rtx && !unsignedp)
9005 /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
9006 and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
9007 cmpop1 = const0_rtx;
9008 if (code == MIN_EXPR)
9009 comparison_code = LT;
9012 /* Use a conditional move if possible. */
9013 if (can_conditionally_move_p (mode))
9015 rtx insn;
9017 start_sequence ();
9019 /* Try to emit the conditional move. */
9020 insn = emit_conditional_move (target, comparison_code,
9021 op0, cmpop1, mode,
9022 op0, op1, mode,
9023 unsignedp);
9025 /* If we could do the conditional move, emit the sequence,
9026 and return. */
9027 if (insn)
9029 rtx_insn *seq = get_insns ();
9030 end_sequence ();
9031 emit_insn (seq);
9032 return target;
9035 /* Otherwise discard the sequence and fall back to code with
9036 branches. */
9037 end_sequence ();
9040 if (target != op0)
9041 emit_move_insn (target, op0);
9043 lab = gen_label_rtx ();
9044 do_compare_rtx_and_jump (target, cmpop1, comparison_code,
9045 unsignedp, mode, NULL_RTX, NULL, lab,
9046 -1);
9048 emit_move_insn (target, op1);
9049 emit_label (lab);
9050 return target;
9052 case BIT_NOT_EXPR:
9053 op0 = expand_expr (treeop0, subtarget,
9054 VOIDmode, EXPAND_NORMAL);
9055 if (modifier == EXPAND_STACK_PARM)
9056 target = 0;
9057 /* In case we have to reduce the result to bitfield precision
9058 for unsigned bitfield expand this as XOR with a proper constant
9059 instead. */
9060 if (reduce_bit_field && TYPE_UNSIGNED (type))
9062 wide_int mask = wi::mask (TYPE_PRECISION (type),
9063 false, GET_MODE_PRECISION (mode));
9065 temp = expand_binop (mode, xor_optab, op0,
9066 immed_wide_int_const (mask, mode),
9067 target, 1, OPTAB_LIB_WIDEN);
9069 else
9070 temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
9071 gcc_assert (temp);
9072 return temp;
9074 /* ??? Can optimize bitwise operations with one arg constant.
9075 Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
9076 and (a bitwise1 b) bitwise2 b (etc)
9077 but that is probably not worth while. */
9079 case BIT_AND_EXPR:
9080 case BIT_IOR_EXPR:
9081 case BIT_XOR_EXPR:
9082 goto binop;
9084 case LROTATE_EXPR:
9085 case RROTATE_EXPR:
9086 gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
9087 || (GET_MODE_PRECISION (TYPE_MODE (type))
9088 == TYPE_PRECISION (type)));
9089 /* fall through */
9091 case LSHIFT_EXPR:
9092 case RSHIFT_EXPR:
9094 /* If this is a fixed-point operation, then we cannot use the code
9095 below because "expand_shift" doesn't support sat/no-sat fixed-point
9096 shifts. */
9097 if (ALL_FIXED_POINT_MODE_P (mode))
9098 goto binop;
9100 if (! safe_from_p (subtarget, treeop1, 1))
9101 subtarget = 0;
9102 if (modifier == EXPAND_STACK_PARM)
9103 target = 0;
9104 op0 = expand_expr (treeop0, subtarget,
9105 VOIDmode, EXPAND_NORMAL);
9107 /* Left shift optimization when shifting across word_size boundary.
9109 If mode == GET_MODE_WIDER_MODE (word_mode), then normally
9110 there isn't native instruction to support this wide mode
9111 left shift. Given below scenario:
9113 Type A = (Type) B << C
9115 |< T >|
9116 | dest_high | dest_low |
9118 | word_size |
9120 If the shift amount C caused we shift B to across the word
9121 size boundary, i.e part of B shifted into high half of
9122 destination register, and part of B remains in the low
9123 half, then GCC will use the following left shift expand
9124 logic:
9126 1. Initialize dest_low to B.
9127 2. Initialize every bit of dest_high to the sign bit of B.
9128 3. Logic left shift dest_low by C bit to finalize dest_low.
9129 The value of dest_low before this shift is kept in a temp D.
9130 4. Logic left shift dest_high by C.
9131 5. Logic right shift D by (word_size - C).
9132 6. Or the result of 4 and 5 to finalize dest_high.
9134 While, by checking gimple statements, if operand B is
9135 coming from signed extension, then we can simplify above
9136 expand logic into:
9138 1. dest_high = src_low >> (word_size - C).
9139 2. dest_low = src_low << C.
9141 We can use one arithmetic right shift to finish all the
9142 purpose of steps 2, 4, 5, 6, thus we reduce the steps
9143 needed from 6 into 2.
9145 The case is similar for zero extension, except that we
9146 initialize dest_high to zero rather than copies of the sign
9147 bit from B. Furthermore, we need to use a logical right shift
9148 in this case.
9150 The choice of sign-extension versus zero-extension is
9151 determined entirely by whether or not B is signed and is
9152 independent of the current setting of unsignedp. */
9154 temp = NULL_RTX;
9155 if (code == LSHIFT_EXPR
9156 && target
9157 && REG_P (target)
9158 && mode == GET_MODE_WIDER_MODE (word_mode)
9159 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode)
9160 && TREE_CONSTANT (treeop1)
9161 && TREE_CODE (treeop0) == SSA_NAME)
9163 gimple *def = SSA_NAME_DEF_STMT (treeop0);
9164 if (is_gimple_assign (def)
9165 && gimple_assign_rhs_code (def) == NOP_EXPR)
9167 machine_mode rmode = TYPE_MODE
9168 (TREE_TYPE (gimple_assign_rhs1 (def)));
9170 if (GET_MODE_SIZE (rmode) < GET_MODE_SIZE (mode)
9171 && TREE_INT_CST_LOW (treeop1) < GET_MODE_BITSIZE (word_mode)
9172 && ((TREE_INT_CST_LOW (treeop1) + GET_MODE_BITSIZE (rmode))
9173 >= GET_MODE_BITSIZE (word_mode)))
9175 rtx_insn *seq, *seq_old;
9176 unsigned int high_off = subreg_highpart_offset (word_mode,
9177 mode);
9178 bool extend_unsigned
9179 = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def)));
9180 rtx low = lowpart_subreg (word_mode, op0, mode);
9181 rtx dest_low = lowpart_subreg (word_mode, target, mode);
9182 rtx dest_high = simplify_gen_subreg (word_mode, target,
9183 mode, high_off);
9184 HOST_WIDE_INT ramount = (BITS_PER_WORD
9185 - TREE_INT_CST_LOW (treeop1));
9186 tree rshift = build_int_cst (TREE_TYPE (treeop1), ramount);
9188 start_sequence ();
9189 /* dest_high = src_low >> (word_size - C). */
9190 temp = expand_variable_shift (RSHIFT_EXPR, word_mode, low,
9191 rshift, dest_high,
9192 extend_unsigned);
9193 if (temp != dest_high)
9194 emit_move_insn (dest_high, temp);
9196 /* dest_low = src_low << C. */
9197 temp = expand_variable_shift (LSHIFT_EXPR, word_mode, low,
9198 treeop1, dest_low, unsignedp);
9199 if (temp != dest_low)
9200 emit_move_insn (dest_low, temp);
9202 seq = get_insns ();
9203 end_sequence ();
9204 temp = target ;
9206 if (have_insn_for (ASHIFT, mode))
9208 bool speed_p = optimize_insn_for_speed_p ();
9209 start_sequence ();
9210 rtx ret_old = expand_variable_shift (code, mode, op0,
9211 treeop1, target,
9212 unsignedp);
9214 seq_old = get_insns ();
9215 end_sequence ();
9216 if (seq_cost (seq, speed_p)
9217 >= seq_cost (seq_old, speed_p))
9219 seq = seq_old;
9220 temp = ret_old;
9223 emit_insn (seq);
9228 if (temp == NULL_RTX)
9229 temp = expand_variable_shift (code, mode, op0, treeop1, target,
9230 unsignedp);
9231 if (code == LSHIFT_EXPR)
9232 temp = REDUCE_BIT_FIELD (temp);
9233 return temp;
9236 /* Could determine the answer when only additive constants differ. Also,
9237 the addition of one can be handled by changing the condition. */
9238 case LT_EXPR:
9239 case LE_EXPR:
9240 case GT_EXPR:
9241 case GE_EXPR:
9242 case EQ_EXPR:
9243 case NE_EXPR:
9244 case UNORDERED_EXPR:
9245 case ORDERED_EXPR:
9246 case UNLT_EXPR:
9247 case UNLE_EXPR:
9248 case UNGT_EXPR:
9249 case UNGE_EXPR:
9250 case UNEQ_EXPR:
9251 case LTGT_EXPR:
9253 temp = do_store_flag (ops,
9254 modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
9255 tmode != VOIDmode ? tmode : mode);
9256 if (temp)
9257 return temp;
9259 /* Use a compare and a jump for BLKmode comparisons, or for function
9260 type comparisons is have_canonicalize_funcptr_for_compare. */
9262 if ((target == 0
9263 || modifier == EXPAND_STACK_PARM
9264 || ! safe_from_p (target, treeop0, 1)
9265 || ! safe_from_p (target, treeop1, 1)
9266 /* Make sure we don't have a hard reg (such as function's return
9267 value) live across basic blocks, if not optimizing. */
9268 || (!optimize && REG_P (target)
9269 && REGNO (target) < FIRST_PSEUDO_REGISTER)))
9270 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
9272 emit_move_insn (target, const0_rtx);
9274 rtx_code_label *lab1 = gen_label_rtx ();
9275 jumpifnot_1 (code, treeop0, treeop1, lab1, -1);
9277 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
9278 emit_move_insn (target, constm1_rtx);
9279 else
9280 emit_move_insn (target, const1_rtx);
9282 emit_label (lab1);
9283 return target;
9285 case COMPLEX_EXPR:
9286 /* Get the rtx code of the operands. */
9287 op0 = expand_normal (treeop0);
9288 op1 = expand_normal (treeop1);
9290 if (!target)
9291 target = gen_reg_rtx (TYPE_MODE (type));
9292 else
9293 /* If target overlaps with op1, then either we need to force
9294 op1 into a pseudo (if target also overlaps with op0),
9295 or write the complex parts in reverse order. */
9296 switch (GET_CODE (target))
9298 case CONCAT:
9299 if (reg_overlap_mentioned_p (XEXP (target, 0), op1))
9301 if (reg_overlap_mentioned_p (XEXP (target, 1), op0))
9303 complex_expr_force_op1:
9304 temp = gen_reg_rtx (GET_MODE_INNER (GET_MODE (target)));
9305 emit_move_insn (temp, op1);
9306 op1 = temp;
9307 break;
9309 complex_expr_swap_order:
9310 /* Move the imaginary (op1) and real (op0) parts to their
9311 location. */
9312 write_complex_part (target, op1, true);
9313 write_complex_part (target, op0, false);
9315 return target;
9317 break;
9318 case MEM:
9319 temp = adjust_address_nv (target,
9320 GET_MODE_INNER (GET_MODE (target)), 0);
9321 if (reg_overlap_mentioned_p (temp, op1))
9323 machine_mode imode = GET_MODE_INNER (GET_MODE (target));
9324 temp = adjust_address_nv (target, imode,
9325 GET_MODE_SIZE (imode));
9326 if (reg_overlap_mentioned_p (temp, op0))
9327 goto complex_expr_force_op1;
9328 goto complex_expr_swap_order;
9330 break;
9331 default:
9332 if (reg_overlap_mentioned_p (target, op1))
9334 if (reg_overlap_mentioned_p (target, op0))
9335 goto complex_expr_force_op1;
9336 goto complex_expr_swap_order;
9338 break;
9341 /* Move the real (op0) and imaginary (op1) parts to their location. */
9342 write_complex_part (target, op0, false);
9343 write_complex_part (target, op1, true);
9345 return target;
9347 case WIDEN_SUM_EXPR:
9349 tree oprnd0 = treeop0;
9350 tree oprnd1 = treeop1;
9352 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9353 target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
9354 target, unsignedp);
9355 return target;
9358 case REDUC_MAX_EXPR:
9359 case REDUC_MIN_EXPR:
9360 case REDUC_PLUS_EXPR:
9362 op0 = expand_normal (treeop0);
9363 this_optab = optab_for_tree_code (code, type, optab_default);
9364 machine_mode vec_mode = TYPE_MODE (TREE_TYPE (treeop0));
9366 struct expand_operand ops[2];
9367 enum insn_code icode = optab_handler (this_optab, vec_mode);
9369 create_output_operand (&ops[0], target, mode);
9370 create_input_operand (&ops[1], op0, vec_mode);
9371 expand_insn (icode, 2, ops);
9372 target = ops[0].value;
9373 if (GET_MODE (target) != mode)
9374 return gen_lowpart (tmode, target);
9375 return target;
9378 case VEC_UNPACK_HI_EXPR:
9379 case VEC_UNPACK_LO_EXPR:
9381 op0 = expand_normal (treeop0);
9382 temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
9383 target, unsignedp);
9384 gcc_assert (temp);
9385 return temp;
9388 case VEC_UNPACK_FLOAT_HI_EXPR:
9389 case VEC_UNPACK_FLOAT_LO_EXPR:
9391 op0 = expand_normal (treeop0);
9392 /* The signedness is determined from input operand. */
9393 temp = expand_widen_pattern_expr
9394 (ops, op0, NULL_RTX, NULL_RTX,
9395 target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
9397 gcc_assert (temp);
9398 return temp;
9401 case VEC_WIDEN_MULT_HI_EXPR:
9402 case VEC_WIDEN_MULT_LO_EXPR:
9403 case VEC_WIDEN_MULT_EVEN_EXPR:
9404 case VEC_WIDEN_MULT_ODD_EXPR:
9405 case VEC_WIDEN_LSHIFT_HI_EXPR:
9406 case VEC_WIDEN_LSHIFT_LO_EXPR:
9407 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9408 target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
9409 target, unsignedp);
9410 gcc_assert (target);
9411 return target;
9413 case VEC_PACK_TRUNC_EXPR:
9414 case VEC_PACK_SAT_EXPR:
9415 case VEC_PACK_FIX_TRUNC_EXPR:
9416 mode = TYPE_MODE (TREE_TYPE (treeop0));
9417 goto binop;
9419 case VEC_PERM_EXPR:
9420 expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
9421 op2 = expand_normal (treeop2);
9423 /* Careful here: if the target doesn't support integral vector modes,
9424 a constant selection vector could wind up smooshed into a normal
9425 integral constant. */
9426 if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
9428 tree sel_type = TREE_TYPE (treeop2);
9429 machine_mode vmode
9430 = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
9431 TYPE_VECTOR_SUBPARTS (sel_type));
9432 gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
9433 op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
9434 gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
9436 else
9437 gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
9439 temp = expand_vec_perm (mode, op0, op1, op2, target);
9440 gcc_assert (temp);
9441 return temp;
9443 case DOT_PROD_EXPR:
9445 tree oprnd0 = treeop0;
9446 tree oprnd1 = treeop1;
9447 tree oprnd2 = treeop2;
9448 rtx op2;
9450 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9451 op2 = expand_normal (oprnd2);
9452 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9453 target, unsignedp);
9454 return target;
9457 case SAD_EXPR:
9459 tree oprnd0 = treeop0;
9460 tree oprnd1 = treeop1;
9461 tree oprnd2 = treeop2;
9462 rtx op2;
9464 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9465 op2 = expand_normal (oprnd2);
9466 target = expand_widen_pattern_expr (ops, op0, op1, op2,
9467 target, unsignedp);
9468 return target;
9471 case REALIGN_LOAD_EXPR:
9473 tree oprnd0 = treeop0;
9474 tree oprnd1 = treeop1;
9475 tree oprnd2 = treeop2;
9476 rtx op2;
9478 this_optab = optab_for_tree_code (code, type, optab_default);
9479 expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
9480 op2 = expand_normal (oprnd2);
9481 temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
9482 target, unsignedp);
9483 gcc_assert (temp);
9484 return temp;
9487 case COND_EXPR:
9489 /* A COND_EXPR with its type being VOID_TYPE represents a
9490 conditional jump and is handled in
9491 expand_gimple_cond_expr. */
9492 gcc_assert (!VOID_TYPE_P (type));
9494 /* Note that COND_EXPRs whose type is a structure or union
9495 are required to be constructed to contain assignments of
9496 a temporary variable, so that we can evaluate them here
9497 for side effect only. If type is void, we must do likewise. */
9499 gcc_assert (!TREE_ADDRESSABLE (type)
9500 && !ignore
9501 && TREE_TYPE (treeop1) != void_type_node
9502 && TREE_TYPE (treeop2) != void_type_node);
9504 temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9505 if (temp)
9506 return temp;
9508 /* If we are not to produce a result, we have no target. Otherwise,
9509 if a target was specified use it; it will not be used as an
9510 intermediate target unless it is safe. If no target, use a
9511 temporary. */
9513 if (modifier != EXPAND_STACK_PARM
9514 && original_target
9515 && safe_from_p (original_target, treeop0, 1)
9516 && GET_MODE (original_target) == mode
9517 && !MEM_P (original_target))
9518 temp = original_target;
9519 else
9520 temp = assign_temp (type, 0, 1);
9522 do_pending_stack_adjust ();
9523 NO_DEFER_POP;
9524 rtx_code_label *lab0 = gen_label_rtx ();
9525 rtx_code_label *lab1 = gen_label_rtx ();
9526 jumpifnot (treeop0, lab0, -1);
9527 store_expr (treeop1, temp,
9528 modifier == EXPAND_STACK_PARM,
9529 false, false);
9531 emit_jump_insn (targetm.gen_jump (lab1));
9532 emit_barrier ();
9533 emit_label (lab0);
9534 store_expr (treeop2, temp,
9535 modifier == EXPAND_STACK_PARM,
9536 false, false);
9538 emit_label (lab1);
9539 OK_DEFER_POP;
9540 return temp;
9543 case VEC_COND_EXPR:
9544 target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9545 return target;
9547 case BIT_INSERT_EXPR:
9549 unsigned bitpos = tree_to_uhwi (treeop2);
9550 unsigned bitsize;
9551 if (INTEGRAL_TYPE_P (TREE_TYPE (treeop1)))
9552 bitsize = TYPE_PRECISION (TREE_TYPE (treeop1));
9553 else
9554 bitsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (treeop1)));
9555 rtx op0 = expand_normal (treeop0);
9556 rtx op1 = expand_normal (treeop1);
9557 rtx dst = gen_reg_rtx (mode);
9558 emit_move_insn (dst, op0);
9559 store_bit_field (dst, bitsize, bitpos, 0, 0,
9560 TYPE_MODE (TREE_TYPE (treeop1)), op1, false);
9561 return dst;
9564 default:
9565 gcc_unreachable ();
9568 /* Here to do an ordinary binary operator. */
9569 binop:
9570 expand_operands (treeop0, treeop1,
9571 subtarget, &op0, &op1, EXPAND_NORMAL);
9572 binop2:
9573 this_optab = optab_for_tree_code (code, type, optab_default);
9574 binop3:
9575 if (modifier == EXPAND_STACK_PARM)
9576 target = 0;
9577 temp = expand_binop (mode, this_optab, op0, op1, target,
9578 unsignedp, OPTAB_LIB_WIDEN);
9579 gcc_assert (temp);
9580 /* Bitwise operations do not need bitfield reduction as we expect their
9581 operands being properly truncated. */
9582 if (code == BIT_XOR_EXPR
9583 || code == BIT_AND_EXPR
9584 || code == BIT_IOR_EXPR)
9585 return temp;
9586 return REDUCE_BIT_FIELD (temp);
9588 #undef REDUCE_BIT_FIELD
9591 /* Return TRUE if expression STMT is suitable for replacement.
9592 Never consider memory loads as replaceable, because those don't ever lead
9593 into constant expressions. */
9595 static bool
9596 stmt_is_replaceable_p (gimple *stmt)
9598 if (ssa_is_replaceable_p (stmt))
9600 /* Don't move around loads. */
9601 if (!gimple_assign_single_p (stmt)
9602 || is_gimple_val (gimple_assign_rhs1 (stmt)))
9603 return true;
9605 return false;
9609 expand_expr_real_1 (tree exp, rtx target, machine_mode tmode,
9610 enum expand_modifier modifier, rtx *alt_rtl,
9611 bool inner_reference_p)
9613 rtx op0, op1, temp, decl_rtl;
9614 tree type;
9615 int unsignedp;
9616 machine_mode mode, dmode;
9617 enum tree_code code = TREE_CODE (exp);
9618 rtx subtarget, original_target;
9619 int ignore;
9620 tree context;
9621 bool reduce_bit_field;
9622 location_t loc = EXPR_LOCATION (exp);
9623 struct separate_ops ops;
9624 tree treeop0, treeop1, treeop2;
9625 tree ssa_name = NULL_TREE;
9626 gimple *g;
9628 type = TREE_TYPE (exp);
9629 mode = TYPE_MODE (type);
9630 unsignedp = TYPE_UNSIGNED (type);
9632 treeop0 = treeop1 = treeop2 = NULL_TREE;
9633 if (!VL_EXP_CLASS_P (exp))
9634 switch (TREE_CODE_LENGTH (code))
9636 default:
9637 case 3: treeop2 = TREE_OPERAND (exp, 2); /* FALLTHRU */
9638 case 2: treeop1 = TREE_OPERAND (exp, 1); /* FALLTHRU */
9639 case 1: treeop0 = TREE_OPERAND (exp, 0); /* FALLTHRU */
9640 case 0: break;
9642 ops.code = code;
9643 ops.type = type;
9644 ops.op0 = treeop0;
9645 ops.op1 = treeop1;
9646 ops.op2 = treeop2;
9647 ops.location = loc;
9649 ignore = (target == const0_rtx
9650 || ((CONVERT_EXPR_CODE_P (code)
9651 || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9652 && TREE_CODE (type) == VOID_TYPE));
9654 /* An operation in what may be a bit-field type needs the
9655 result to be reduced to the precision of the bit-field type,
9656 which is narrower than that of the type's mode. */
9657 reduce_bit_field = (!ignore
9658 && INTEGRAL_TYPE_P (type)
9659 && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9661 /* If we are going to ignore this result, we need only do something
9662 if there is a side-effect somewhere in the expression. If there
9663 is, short-circuit the most common cases here. Note that we must
9664 not call expand_expr with anything but const0_rtx in case this
9665 is an initial expansion of a size that contains a PLACEHOLDER_EXPR. */
9667 if (ignore)
9669 if (! TREE_SIDE_EFFECTS (exp))
9670 return const0_rtx;
9672 /* Ensure we reference a volatile object even if value is ignored, but
9673 don't do this if all we are doing is taking its address. */
9674 if (TREE_THIS_VOLATILE (exp)
9675 && TREE_CODE (exp) != FUNCTION_DECL
9676 && mode != VOIDmode && mode != BLKmode
9677 && modifier != EXPAND_CONST_ADDRESS)
9679 temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9680 if (MEM_P (temp))
9681 copy_to_reg (temp);
9682 return const0_rtx;
9685 if (TREE_CODE_CLASS (code) == tcc_unary
9686 || code == BIT_FIELD_REF
9687 || code == COMPONENT_REF
9688 || code == INDIRECT_REF)
9689 return expand_expr (treeop0, const0_rtx, VOIDmode,
9690 modifier);
9692 else if (TREE_CODE_CLASS (code) == tcc_binary
9693 || TREE_CODE_CLASS (code) == tcc_comparison
9694 || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9696 expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9697 expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9698 return const0_rtx;
9701 target = 0;
9704 if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9705 target = 0;
9707 /* Use subtarget as the target for operand 0 of a binary operation. */
9708 subtarget = get_subtarget (target);
9709 original_target = target;
9711 switch (code)
9713 case LABEL_DECL:
9715 tree function = decl_function_context (exp);
9717 temp = label_rtx (exp);
9718 temp = gen_rtx_LABEL_REF (Pmode, temp);
9720 if (function != current_function_decl
9721 && function != 0)
9722 LABEL_REF_NONLOCAL_P (temp) = 1;
9724 temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9725 return temp;
9728 case SSA_NAME:
9729 /* ??? ivopts calls expander, without any preparation from
9730 out-of-ssa. So fake instructions as if this was an access to the
9731 base variable. This unnecessarily allocates a pseudo, see how we can
9732 reuse it, if partition base vars have it set already. */
9733 if (!currently_expanding_to_rtl)
9735 tree var = SSA_NAME_VAR (exp);
9736 if (var && DECL_RTL_SET_P (var))
9737 return DECL_RTL (var);
9738 return gen_raw_REG (TYPE_MODE (TREE_TYPE (exp)),
9739 LAST_VIRTUAL_REGISTER + 1);
9742 g = get_gimple_for_ssa_name (exp);
9743 /* For EXPAND_INITIALIZER try harder to get something simpler. */
9744 if (g == NULL
9745 && modifier == EXPAND_INITIALIZER
9746 && !SSA_NAME_IS_DEFAULT_DEF (exp)
9747 && (optimize || !SSA_NAME_VAR (exp)
9748 || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9749 && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9750 g = SSA_NAME_DEF_STMT (exp);
9751 if (g)
9753 rtx r;
9754 location_t saved_loc = curr_insn_location ();
9755 location_t loc = gimple_location (g);
9756 if (loc != UNKNOWN_LOCATION)
9757 set_curr_insn_location (loc);
9758 ops.code = gimple_assign_rhs_code (g);
9759 switch (get_gimple_rhs_class (ops.code))
9761 case GIMPLE_TERNARY_RHS:
9762 ops.op2 = gimple_assign_rhs3 (g);
9763 /* Fallthru */
9764 case GIMPLE_BINARY_RHS:
9765 ops.op1 = gimple_assign_rhs2 (g);
9767 /* Try to expand conditonal compare. */
9768 if (targetm.gen_ccmp_first)
9770 gcc_checking_assert (targetm.gen_ccmp_next != NULL);
9771 r = expand_ccmp_expr (g);
9772 if (r)
9773 break;
9775 /* Fallthru */
9776 case GIMPLE_UNARY_RHS:
9777 ops.op0 = gimple_assign_rhs1 (g);
9778 ops.type = TREE_TYPE (gimple_assign_lhs (g));
9779 ops.location = loc;
9780 r = expand_expr_real_2 (&ops, target, tmode, modifier);
9781 break;
9782 case GIMPLE_SINGLE_RHS:
9784 r = expand_expr_real (gimple_assign_rhs1 (g), target,
9785 tmode, modifier, alt_rtl,
9786 inner_reference_p);
9787 break;
9789 default:
9790 gcc_unreachable ();
9792 set_curr_insn_location (saved_loc);
9793 if (REG_P (r) && !REG_EXPR (r))
9794 set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9795 return r;
9798 ssa_name = exp;
9799 decl_rtl = get_rtx_for_ssa_name (ssa_name);
9800 exp = SSA_NAME_VAR (ssa_name);
9801 goto expand_decl_rtl;
9803 case PARM_DECL:
9804 case VAR_DECL:
9805 /* If a static var's type was incomplete when the decl was written,
9806 but the type is complete now, lay out the decl now. */
9807 if (DECL_SIZE (exp) == 0
9808 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9809 && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9810 layout_decl (exp, 0);
9812 /* fall through */
9814 case FUNCTION_DECL:
9815 case RESULT_DECL:
9816 decl_rtl = DECL_RTL (exp);
9817 expand_decl_rtl:
9818 gcc_assert (decl_rtl);
9820 /* DECL_MODE might change when TYPE_MODE depends on attribute target
9821 settings for VECTOR_TYPE_P that might switch for the function. */
9822 if (currently_expanding_to_rtl
9823 && code == VAR_DECL && MEM_P (decl_rtl)
9824 && VECTOR_TYPE_P (type) && exp && DECL_MODE (exp) != mode)
9825 decl_rtl = change_address (decl_rtl, TYPE_MODE (type), 0);
9826 else
9827 decl_rtl = copy_rtx (decl_rtl);
9829 /* Record writes to register variables. */
9830 if (modifier == EXPAND_WRITE
9831 && REG_P (decl_rtl)
9832 && HARD_REGISTER_P (decl_rtl))
9833 add_to_hard_reg_set (&crtl->asm_clobbers,
9834 GET_MODE (decl_rtl), REGNO (decl_rtl));
9836 /* Ensure variable marked as used even if it doesn't go through
9837 a parser. If it hasn't be used yet, write out an external
9838 definition. */
9839 if (exp)
9840 TREE_USED (exp) = 1;
9842 /* Show we haven't gotten RTL for this yet. */
9843 temp = 0;
9845 /* Variables inherited from containing functions should have
9846 been lowered by this point. */
9847 if (exp)
9848 context = decl_function_context (exp);
9849 gcc_assert (!exp
9850 || SCOPE_FILE_SCOPE_P (context)
9851 || context == current_function_decl
9852 || TREE_STATIC (exp)
9853 || DECL_EXTERNAL (exp)
9854 /* ??? C++ creates functions that are not TREE_STATIC. */
9855 || TREE_CODE (exp) == FUNCTION_DECL);
9857 /* This is the case of an array whose size is to be determined
9858 from its initializer, while the initializer is still being parsed.
9859 ??? We aren't parsing while expanding anymore. */
9861 if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9862 temp = validize_mem (decl_rtl);
9864 /* If DECL_RTL is memory, we are in the normal case and the
9865 address is not valid, get the address into a register. */
9867 else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9869 if (alt_rtl)
9870 *alt_rtl = decl_rtl;
9871 decl_rtl = use_anchored_address (decl_rtl);
9872 if (modifier != EXPAND_CONST_ADDRESS
9873 && modifier != EXPAND_SUM
9874 && !memory_address_addr_space_p (exp ? DECL_MODE (exp)
9875 : GET_MODE (decl_rtl),
9876 XEXP (decl_rtl, 0),
9877 MEM_ADDR_SPACE (decl_rtl)))
9878 temp = replace_equiv_address (decl_rtl,
9879 copy_rtx (XEXP (decl_rtl, 0)));
9882 /* If we got something, return it. But first, set the alignment
9883 if the address is a register. */
9884 if (temp != 0)
9886 if (exp && MEM_P (temp) && REG_P (XEXP (temp, 0)))
9887 mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9889 return temp;
9892 if (exp)
9893 dmode = DECL_MODE (exp);
9894 else
9895 dmode = TYPE_MODE (TREE_TYPE (ssa_name));
9897 /* If the mode of DECL_RTL does not match that of the decl,
9898 there are two cases: we are dealing with a BLKmode value
9899 that is returned in a register, or we are dealing with
9900 a promoted value. In the latter case, return a SUBREG
9901 of the wanted mode, but mark it so that we know that it
9902 was already extended. */
9903 if (REG_P (decl_rtl)
9904 && dmode != BLKmode
9905 && GET_MODE (decl_rtl) != dmode)
9907 machine_mode pmode;
9909 /* Get the signedness to be used for this variable. Ensure we get
9910 the same mode we got when the variable was declared. */
9911 if (code != SSA_NAME)
9912 pmode = promote_decl_mode (exp, &unsignedp);
9913 else if ((g = SSA_NAME_DEF_STMT (ssa_name))
9914 && gimple_code (g) == GIMPLE_CALL
9915 && !gimple_call_internal_p (g))
9916 pmode = promote_function_mode (type, mode, &unsignedp,
9917 gimple_call_fntype (g),
9919 else
9920 pmode = promote_ssa_mode (ssa_name, &unsignedp);
9921 gcc_assert (GET_MODE (decl_rtl) == pmode);
9923 temp = gen_lowpart_SUBREG (mode, decl_rtl);
9924 SUBREG_PROMOTED_VAR_P (temp) = 1;
9925 SUBREG_PROMOTED_SET (temp, unsignedp);
9926 return temp;
9929 return decl_rtl;
9931 case INTEGER_CST:
9932 /* Given that TYPE_PRECISION (type) is not always equal to
9933 GET_MODE_PRECISION (TYPE_MODE (type)), we need to extend from
9934 the former to the latter according to the signedness of the
9935 type. */
9936 temp = immed_wide_int_const (wi::to_wide
9937 (exp,
9938 GET_MODE_PRECISION (TYPE_MODE (type))),
9939 TYPE_MODE (type));
9940 return temp;
9942 case VECTOR_CST:
9944 tree tmp = NULL_TREE;
9945 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9946 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9947 || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9948 || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9949 || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9950 || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9951 return const_vector_from_tree (exp);
9952 if (GET_MODE_CLASS (mode) == MODE_INT)
9954 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
9955 return const_scalar_mask_from_tree (exp);
9956 else
9958 tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9959 if (type_for_mode)
9960 tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR,
9961 type_for_mode, exp);
9964 if (!tmp)
9966 vec<constructor_elt, va_gc> *v;
9967 unsigned i;
9968 vec_alloc (v, VECTOR_CST_NELTS (exp));
9969 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9970 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9971 tmp = build_constructor (type, v);
9973 return expand_expr (tmp, ignore ? const0_rtx : target,
9974 tmode, modifier);
9977 case CONST_DECL:
9978 if (modifier == EXPAND_WRITE)
9980 /* Writing into CONST_DECL is always invalid, but handle it
9981 gracefully. */
9982 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (exp));
9983 machine_mode address_mode = targetm.addr_space.address_mode (as);
9984 op0 = expand_expr_addr_expr_1 (exp, NULL_RTX, address_mode,
9985 EXPAND_NORMAL, as);
9986 op0 = memory_address_addr_space (mode, op0, as);
9987 temp = gen_rtx_MEM (mode, op0);
9988 set_mem_addr_space (temp, as);
9989 return temp;
9991 return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9993 case REAL_CST:
9994 /* If optimized, generate immediate CONST_DOUBLE
9995 which will be turned into memory by reload if necessary.
9997 We used to force a register so that loop.c could see it. But
9998 this does not allow gen_* patterns to perform optimizations with
9999 the constants. It also produces two insns in cases like "x = 1.0;".
10000 On most machines, floating-point constants are not permitted in
10001 many insns, so we'd end up copying it to a register in any case.
10003 Now, we do the copying in expand_binop, if appropriate. */
10004 return const_double_from_real_value (TREE_REAL_CST (exp),
10005 TYPE_MODE (TREE_TYPE (exp)));
10007 case FIXED_CST:
10008 return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
10009 TYPE_MODE (TREE_TYPE (exp)));
10011 case COMPLEX_CST:
10012 /* Handle evaluating a complex constant in a CONCAT target. */
10013 if (original_target && GET_CODE (original_target) == CONCAT)
10015 machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
10016 rtx rtarg, itarg;
10018 rtarg = XEXP (original_target, 0);
10019 itarg = XEXP (original_target, 1);
10021 /* Move the real and imaginary parts separately. */
10022 op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
10023 op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
10025 if (op0 != rtarg)
10026 emit_move_insn (rtarg, op0);
10027 if (op1 != itarg)
10028 emit_move_insn (itarg, op1);
10030 return original_target;
10033 /* fall through */
10035 case STRING_CST:
10036 temp = expand_expr_constant (exp, 1, modifier);
10038 /* temp contains a constant address.
10039 On RISC machines where a constant address isn't valid,
10040 make some insns to get that address into a register. */
10041 if (modifier != EXPAND_CONST_ADDRESS
10042 && modifier != EXPAND_INITIALIZER
10043 && modifier != EXPAND_SUM
10044 && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
10045 MEM_ADDR_SPACE (temp)))
10046 return replace_equiv_address (temp,
10047 copy_rtx (XEXP (temp, 0)));
10048 return temp;
10050 case SAVE_EXPR:
10052 tree val = treeop0;
10053 rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl,
10054 inner_reference_p);
10056 if (!SAVE_EXPR_RESOLVED_P (exp))
10058 /* We can indeed still hit this case, typically via builtin
10059 expanders calling save_expr immediately before expanding
10060 something. Assume this means that we only have to deal
10061 with non-BLKmode values. */
10062 gcc_assert (GET_MODE (ret) != BLKmode);
10064 val = build_decl (curr_insn_location (),
10065 VAR_DECL, NULL, TREE_TYPE (exp));
10066 DECL_ARTIFICIAL (val) = 1;
10067 DECL_IGNORED_P (val) = 1;
10068 treeop0 = val;
10069 TREE_OPERAND (exp, 0) = treeop0;
10070 SAVE_EXPR_RESOLVED_P (exp) = 1;
10072 if (!CONSTANT_P (ret))
10073 ret = copy_to_reg (ret);
10074 SET_DECL_RTL (val, ret);
10077 return ret;
10081 case CONSTRUCTOR:
10082 /* If we don't need the result, just ensure we evaluate any
10083 subexpressions. */
10084 if (ignore)
10086 unsigned HOST_WIDE_INT idx;
10087 tree value;
10089 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
10090 expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
10092 return const0_rtx;
10095 return expand_constructor (exp, target, modifier, false);
10097 case TARGET_MEM_REF:
10099 addr_space_t as
10100 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10101 enum insn_code icode;
10102 unsigned int align;
10104 op0 = addr_for_mem_ref (exp, as, true);
10105 op0 = memory_address_addr_space (mode, op0, as);
10106 temp = gen_rtx_MEM (mode, op0);
10107 set_mem_attributes (temp, exp, 0);
10108 set_mem_addr_space (temp, as);
10109 align = get_object_alignment (exp);
10110 if (modifier != EXPAND_WRITE
10111 && modifier != EXPAND_MEMORY
10112 && mode != BLKmode
10113 && align < GET_MODE_ALIGNMENT (mode)
10114 /* If the target does not have special handling for unaligned
10115 loads of mode then it can use regular moves for them. */
10116 && ((icode = optab_handler (movmisalign_optab, mode))
10117 != CODE_FOR_nothing))
10119 struct expand_operand ops[2];
10121 /* We've already validated the memory, and we're creating a
10122 new pseudo destination. The predicates really can't fail,
10123 nor can the generator. */
10124 create_output_operand (&ops[0], NULL_RTX, mode);
10125 create_fixed_operand (&ops[1], temp);
10126 expand_insn (icode, 2, ops);
10127 temp = ops[0].value;
10129 return temp;
10132 case MEM_REF:
10134 const bool reverse = REF_REVERSE_STORAGE_ORDER (exp);
10135 addr_space_t as
10136 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
10137 machine_mode address_mode;
10138 tree base = TREE_OPERAND (exp, 0);
10139 gimple *def_stmt;
10140 enum insn_code icode;
10141 unsigned align;
10142 /* Handle expansion of non-aliased memory with non-BLKmode. That
10143 might end up in a register. */
10144 if (mem_ref_refers_to_non_mem_p (exp))
10146 HOST_WIDE_INT offset = mem_ref_offset (exp).to_short_addr ();
10147 base = TREE_OPERAND (base, 0);
10148 if (offset == 0
10149 && !reverse
10150 && tree_fits_uhwi_p (TYPE_SIZE (type))
10151 && (GET_MODE_BITSIZE (DECL_MODE (base))
10152 == tree_to_uhwi (TYPE_SIZE (type))))
10153 return expand_expr (build1 (VIEW_CONVERT_EXPR, type, base),
10154 target, tmode, modifier);
10155 if (TYPE_MODE (type) == BLKmode)
10157 temp = assign_stack_temp (DECL_MODE (base),
10158 GET_MODE_SIZE (DECL_MODE (base)));
10159 store_expr (base, temp, 0, false, false);
10160 temp = adjust_address (temp, BLKmode, offset);
10161 set_mem_size (temp, int_size_in_bytes (type));
10162 return temp;
10164 exp = build3 (BIT_FIELD_REF, type, base, TYPE_SIZE (type),
10165 bitsize_int (offset * BITS_PER_UNIT));
10166 REF_REVERSE_STORAGE_ORDER (exp) = reverse;
10167 return expand_expr (exp, target, tmode, modifier);
10169 address_mode = targetm.addr_space.address_mode (as);
10170 base = TREE_OPERAND (exp, 0);
10171 if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
10173 tree mask = gimple_assign_rhs2 (def_stmt);
10174 base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
10175 gimple_assign_rhs1 (def_stmt), mask);
10176 TREE_OPERAND (exp, 0) = base;
10178 align = get_object_alignment (exp);
10179 op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
10180 op0 = memory_address_addr_space (mode, op0, as);
10181 if (!integer_zerop (TREE_OPERAND (exp, 1)))
10183 rtx off = immed_wide_int_const (mem_ref_offset (exp), address_mode);
10184 op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
10185 op0 = memory_address_addr_space (mode, op0, as);
10187 temp = gen_rtx_MEM (mode, op0);
10188 set_mem_attributes (temp, exp, 0);
10189 set_mem_addr_space (temp, as);
10190 if (TREE_THIS_VOLATILE (exp))
10191 MEM_VOLATILE_P (temp) = 1;
10192 if (modifier != EXPAND_WRITE
10193 && modifier != EXPAND_MEMORY
10194 && !inner_reference_p
10195 && mode != BLKmode
10196 && align < GET_MODE_ALIGNMENT (mode))
10198 if ((icode = optab_handler (movmisalign_optab, mode))
10199 != CODE_FOR_nothing)
10201 struct expand_operand ops[2];
10203 /* We've already validated the memory, and we're creating a
10204 new pseudo destination. The predicates really can't fail,
10205 nor can the generator. */
10206 create_output_operand (&ops[0], NULL_RTX, mode);
10207 create_fixed_operand (&ops[1], temp);
10208 expand_insn (icode, 2, ops);
10209 temp = ops[0].value;
10211 else if (SLOW_UNALIGNED_ACCESS (mode, align))
10212 temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
10213 0, TYPE_UNSIGNED (TREE_TYPE (exp)),
10214 (modifier == EXPAND_STACK_PARM
10215 ? NULL_RTX : target),
10216 mode, mode, false, alt_rtl);
10218 if (reverse
10219 && modifier != EXPAND_MEMORY
10220 && modifier != EXPAND_WRITE)
10221 temp = flip_storage_order (mode, temp);
10222 return temp;
10225 case ARRAY_REF:
10228 tree array = treeop0;
10229 tree index = treeop1;
10230 tree init;
10232 /* Fold an expression like: "foo"[2].
10233 This is not done in fold so it won't happen inside &.
10234 Don't fold if this is for wide characters since it's too
10235 difficult to do correctly and this is a very rare case. */
10237 if (modifier != EXPAND_CONST_ADDRESS
10238 && modifier != EXPAND_INITIALIZER
10239 && modifier != EXPAND_MEMORY)
10241 tree t = fold_read_from_constant_string (exp);
10243 if (t)
10244 return expand_expr (t, target, tmode, modifier);
10247 /* If this is a constant index into a constant array,
10248 just get the value from the array. Handle both the cases when
10249 we have an explicit constructor and when our operand is a variable
10250 that was declared const. */
10252 if (modifier != EXPAND_CONST_ADDRESS
10253 && modifier != EXPAND_INITIALIZER
10254 && modifier != EXPAND_MEMORY
10255 && TREE_CODE (array) == CONSTRUCTOR
10256 && ! TREE_SIDE_EFFECTS (array)
10257 && TREE_CODE (index) == INTEGER_CST)
10259 unsigned HOST_WIDE_INT ix;
10260 tree field, value;
10262 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
10263 field, value)
10264 if (tree_int_cst_equal (field, index))
10266 if (!TREE_SIDE_EFFECTS (value))
10267 return expand_expr (fold (value), target, tmode, modifier);
10268 break;
10272 else if (optimize >= 1
10273 && modifier != EXPAND_CONST_ADDRESS
10274 && modifier != EXPAND_INITIALIZER
10275 && modifier != EXPAND_MEMORY
10276 && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
10277 && TREE_CODE (index) == INTEGER_CST
10278 && (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
10279 && (init = ctor_for_folding (array)) != error_mark_node)
10281 if (init == NULL_TREE)
10283 tree value = build_zero_cst (type);
10284 if (TREE_CODE (value) == CONSTRUCTOR)
10286 /* If VALUE is a CONSTRUCTOR, this optimization is only
10287 useful if this doesn't store the CONSTRUCTOR into
10288 memory. If it does, it is more efficient to just
10289 load the data from the array directly. */
10290 rtx ret = expand_constructor (value, target,
10291 modifier, true);
10292 if (ret == NULL_RTX)
10293 value = NULL_TREE;
10296 if (value)
10297 return expand_expr (value, target, tmode, modifier);
10299 else if (TREE_CODE (init) == CONSTRUCTOR)
10301 unsigned HOST_WIDE_INT ix;
10302 tree field, value;
10304 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
10305 field, value)
10306 if (tree_int_cst_equal (field, index))
10308 if (TREE_SIDE_EFFECTS (value))
10309 break;
10311 if (TREE_CODE (value) == CONSTRUCTOR)
10313 /* If VALUE is a CONSTRUCTOR, this
10314 optimization is only useful if
10315 this doesn't store the CONSTRUCTOR
10316 into memory. If it does, it is more
10317 efficient to just load the data from
10318 the array directly. */
10319 rtx ret = expand_constructor (value, target,
10320 modifier, true);
10321 if (ret == NULL_RTX)
10322 break;
10325 return
10326 expand_expr (fold (value), target, tmode, modifier);
10329 else if (TREE_CODE (init) == STRING_CST)
10331 tree low_bound = array_ref_low_bound (exp);
10332 tree index1 = fold_convert_loc (loc, sizetype, treeop1);
10334 /* Optimize the special case of a zero lower bound.
10336 We convert the lower bound to sizetype to avoid problems
10337 with constant folding. E.g. suppose the lower bound is
10338 1 and its mode is QI. Without the conversion
10339 (ARRAY + (INDEX - (unsigned char)1))
10340 becomes
10341 (ARRAY + (-(unsigned char)1) + INDEX)
10342 which becomes
10343 (ARRAY + 255 + INDEX). Oops! */
10344 if (!integer_zerop (low_bound))
10345 index1 = size_diffop_loc (loc, index1,
10346 fold_convert_loc (loc, sizetype,
10347 low_bound));
10349 if (tree_fits_uhwi_p (index1)
10350 && compare_tree_int (index1, TREE_STRING_LENGTH (init)) < 0)
10352 tree type = TREE_TYPE (TREE_TYPE (init));
10353 machine_mode mode = TYPE_MODE (type);
10355 if (GET_MODE_CLASS (mode) == MODE_INT
10356 && GET_MODE_SIZE (mode) == 1)
10357 return gen_int_mode (TREE_STRING_POINTER (init)
10358 [TREE_INT_CST_LOW (index1)],
10359 mode);
10364 goto normal_inner_ref;
10366 case COMPONENT_REF:
10367 /* If the operand is a CONSTRUCTOR, we can just extract the
10368 appropriate field if it is present. */
10369 if (TREE_CODE (treeop0) == CONSTRUCTOR)
10371 unsigned HOST_WIDE_INT idx;
10372 tree field, value;
10374 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
10375 idx, field, value)
10376 if (field == treeop1
10377 /* We can normally use the value of the field in the
10378 CONSTRUCTOR. However, if this is a bitfield in
10379 an integral mode that we can fit in a HOST_WIDE_INT,
10380 we must mask only the number of bits in the bitfield,
10381 since this is done implicitly by the constructor. If
10382 the bitfield does not meet either of those conditions,
10383 we can't do this optimization. */
10384 && (! DECL_BIT_FIELD (field)
10385 || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
10386 && (GET_MODE_PRECISION (DECL_MODE (field))
10387 <= HOST_BITS_PER_WIDE_INT))))
10389 if (DECL_BIT_FIELD (field)
10390 && modifier == EXPAND_STACK_PARM)
10391 target = 0;
10392 op0 = expand_expr (value, target, tmode, modifier);
10393 if (DECL_BIT_FIELD (field))
10395 HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
10396 machine_mode imode = TYPE_MODE (TREE_TYPE (field));
10398 if (TYPE_UNSIGNED (TREE_TYPE (field)))
10400 op1 = gen_int_mode ((HOST_WIDE_INT_1 << bitsize) - 1,
10401 imode);
10402 op0 = expand_and (imode, op0, op1, target);
10404 else
10406 int count = GET_MODE_PRECISION (imode) - bitsize;
10408 op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
10409 target, 0);
10410 op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
10411 target, 0);
10415 return op0;
10418 goto normal_inner_ref;
10420 case BIT_FIELD_REF:
10421 case ARRAY_RANGE_REF:
10422 normal_inner_ref:
10424 machine_mode mode1, mode2;
10425 HOST_WIDE_INT bitsize, bitpos;
10426 tree offset;
10427 int reversep, volatilep = 0, must_force_mem;
10428 tree tem
10429 = get_inner_reference (exp, &bitsize, &bitpos, &offset, &mode1,
10430 &unsignedp, &reversep, &volatilep);
10431 rtx orig_op0, memloc;
10432 bool clear_mem_expr = false;
10434 /* If we got back the original object, something is wrong. Perhaps
10435 we are evaluating an expression too early. In any event, don't
10436 infinitely recurse. */
10437 gcc_assert (tem != exp);
10439 /* If TEM's type is a union of variable size, pass TARGET to the inner
10440 computation, since it will need a temporary and TARGET is known
10441 to have to do. This occurs in unchecked conversion in Ada. */
10442 orig_op0 = op0
10443 = expand_expr_real (tem,
10444 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10445 && COMPLETE_TYPE_P (TREE_TYPE (tem))
10446 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10447 != INTEGER_CST)
10448 && modifier != EXPAND_STACK_PARM
10449 ? target : NULL_RTX),
10450 VOIDmode,
10451 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10452 NULL, true);
10454 /* If the field has a mode, we want to access it in the
10455 field's mode, not the computed mode.
10456 If a MEM has VOIDmode (external with incomplete type),
10457 use BLKmode for it instead. */
10458 if (MEM_P (op0))
10460 if (mode1 != VOIDmode)
10461 op0 = adjust_address (op0, mode1, 0);
10462 else if (GET_MODE (op0) == VOIDmode)
10463 op0 = adjust_address (op0, BLKmode, 0);
10466 mode2
10467 = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
10469 /* If we have either an offset, a BLKmode result, or a reference
10470 outside the underlying object, we must force it to memory.
10471 Such a case can occur in Ada if we have unchecked conversion
10472 of an expression from a scalar type to an aggregate type or
10473 for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
10474 passed a partially uninitialized object or a view-conversion
10475 to a larger size. */
10476 must_force_mem = (offset
10477 || mode1 == BLKmode
10478 || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
10480 /* Handle CONCAT first. */
10481 if (GET_CODE (op0) == CONCAT && !must_force_mem)
10483 if (bitpos == 0
10484 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0))
10485 && COMPLEX_MODE_P (mode1)
10486 && COMPLEX_MODE_P (GET_MODE (op0))
10487 && (GET_MODE_PRECISION (GET_MODE_INNER (mode1))
10488 == GET_MODE_PRECISION (GET_MODE_INNER (GET_MODE (op0)))))
10490 if (reversep)
10491 op0 = flip_storage_order (GET_MODE (op0), op0);
10492 if (mode1 != GET_MODE (op0))
10494 rtx parts[2];
10495 for (int i = 0; i < 2; i++)
10497 rtx op = read_complex_part (op0, i != 0);
10498 if (GET_CODE (op) == SUBREG)
10499 op = force_reg (GET_MODE (op), op);
10500 rtx temp = gen_lowpart_common (GET_MODE_INNER (mode1),
10501 op);
10502 if (temp)
10503 op = temp;
10504 else
10506 if (!REG_P (op) && !MEM_P (op))
10507 op = force_reg (GET_MODE (op), op);
10508 op = gen_lowpart (GET_MODE_INNER (mode1), op);
10510 parts[i] = op;
10512 op0 = gen_rtx_CONCAT (mode1, parts[0], parts[1]);
10514 return op0;
10516 if (bitpos == 0
10517 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10518 && bitsize)
10520 op0 = XEXP (op0, 0);
10521 mode2 = GET_MODE (op0);
10523 else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10524 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
10525 && bitpos
10526 && bitsize)
10528 op0 = XEXP (op0, 1);
10529 bitpos = 0;
10530 mode2 = GET_MODE (op0);
10532 else
10533 /* Otherwise force into memory. */
10534 must_force_mem = 1;
10537 /* If this is a constant, put it in a register if it is a legitimate
10538 constant and we don't need a memory reference. */
10539 if (CONSTANT_P (op0)
10540 && mode2 != BLKmode
10541 && targetm.legitimate_constant_p (mode2, op0)
10542 && !must_force_mem)
10543 op0 = force_reg (mode2, op0);
10545 /* Otherwise, if this is a constant, try to force it to the constant
10546 pool. Note that back-ends, e.g. MIPS, may refuse to do so if it
10547 is a legitimate constant. */
10548 else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
10549 op0 = validize_mem (memloc);
10551 /* Otherwise, if this is a constant or the object is not in memory
10552 and need be, put it there. */
10553 else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
10555 memloc = assign_temp (TREE_TYPE (tem), 1, 1);
10556 emit_move_insn (memloc, op0);
10557 op0 = memloc;
10558 clear_mem_expr = true;
10561 if (offset)
10563 machine_mode address_mode;
10564 rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
10565 EXPAND_SUM);
10567 gcc_assert (MEM_P (op0));
10569 address_mode = get_address_mode (op0);
10570 if (GET_MODE (offset_rtx) != address_mode)
10572 /* We cannot be sure that the RTL in offset_rtx is valid outside
10573 of a memory address context, so force it into a register
10574 before attempting to convert it to the desired mode. */
10575 offset_rtx = force_operand (offset_rtx, NULL_RTX);
10576 offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
10579 /* See the comment in expand_assignment for the rationale. */
10580 if (mode1 != VOIDmode
10581 && bitpos != 0
10582 && bitsize > 0
10583 && (bitpos % bitsize) == 0
10584 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
10585 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode1))
10587 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10588 bitpos = 0;
10591 op0 = offset_address (op0, offset_rtx,
10592 highest_pow2_factor (offset));
10595 /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
10596 record its alignment as BIGGEST_ALIGNMENT. */
10597 if (MEM_P (op0) && bitpos == 0 && offset != 0
10598 && is_aligning_offset (offset, tem))
10599 set_mem_align (op0, BIGGEST_ALIGNMENT);
10601 /* Don't forget about volatility even if this is a bitfield. */
10602 if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
10604 if (op0 == orig_op0)
10605 op0 = copy_rtx (op0);
10607 MEM_VOLATILE_P (op0) = 1;
10610 /* In cases where an aligned union has an unaligned object
10611 as a field, we might be extracting a BLKmode value from
10612 an integer-mode (e.g., SImode) object. Handle this case
10613 by doing the extract into an object as wide as the field
10614 (which we know to be the width of a basic mode), then
10615 storing into memory, and changing the mode to BLKmode. */
10616 if (mode1 == VOIDmode
10617 || REG_P (op0) || GET_CODE (op0) == SUBREG
10618 || (mode1 != BLKmode && ! direct_load[(int) mode1]
10619 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
10620 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
10621 && modifier != EXPAND_CONST_ADDRESS
10622 && modifier != EXPAND_INITIALIZER
10623 && modifier != EXPAND_MEMORY)
10624 /* If the bitfield is volatile and the bitsize
10625 is narrower than the access size of the bitfield,
10626 we need to extract bitfields from the access. */
10627 || (volatilep && TREE_CODE (exp) == COMPONENT_REF
10628 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (exp, 1))
10629 && mode1 != BLKmode
10630 && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)
10631 /* If the field isn't aligned enough to fetch as a memref,
10632 fetch it as a bit field. */
10633 || (mode1 != BLKmode
10634 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
10635 || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
10636 || (MEM_P (op0)
10637 && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10638 || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10639 && modifier != EXPAND_MEMORY
10640 && ((modifier == EXPAND_CONST_ADDRESS
10641 || modifier == EXPAND_INITIALIZER)
10642 ? STRICT_ALIGNMENT
10643 : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10644 || (bitpos % BITS_PER_UNIT != 0)))
10645 /* If the type and the field are a constant size and the
10646 size of the type isn't the same size as the bitfield,
10647 we must use bitfield operations. */
10648 || (bitsize >= 0
10649 && TYPE_SIZE (TREE_TYPE (exp))
10650 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10651 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10652 bitsize)))
10654 machine_mode ext_mode = mode;
10656 if (ext_mode == BLKmode
10657 && ! (target != 0 && MEM_P (op0)
10658 && MEM_P (target)
10659 && bitpos % BITS_PER_UNIT == 0))
10660 ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10662 if (ext_mode == BLKmode)
10664 if (target == 0)
10665 target = assign_temp (type, 1, 1);
10667 /* ??? Unlike the similar test a few lines below, this one is
10668 very likely obsolete. */
10669 if (bitsize == 0)
10670 return target;
10672 /* In this case, BITPOS must start at a byte boundary and
10673 TARGET, if specified, must be a MEM. */
10674 gcc_assert (MEM_P (op0)
10675 && (!target || MEM_P (target))
10676 && !(bitpos % BITS_PER_UNIT));
10678 emit_block_move (target,
10679 adjust_address (op0, VOIDmode,
10680 bitpos / BITS_PER_UNIT),
10681 GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10682 / BITS_PER_UNIT),
10683 (modifier == EXPAND_STACK_PARM
10684 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10686 return target;
10689 /* If we have nothing to extract, the result will be 0 for targets
10690 with SHIFT_COUNT_TRUNCATED == 0 and garbage otherwise. Always
10691 return 0 for the sake of consistency, as reading a zero-sized
10692 bitfield is valid in Ada and the value is fully specified. */
10693 if (bitsize == 0)
10694 return const0_rtx;
10696 op0 = validize_mem (op0);
10698 if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10699 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10701 /* If the result has a record type and the extraction is done in
10702 an integral mode, then the field may be not aligned on a byte
10703 boundary; in this case, if it has reverse storage order, it
10704 needs to be extracted as a scalar field with reverse storage
10705 order and put back into memory order afterwards. */
10706 if (TREE_CODE (type) == RECORD_TYPE
10707 && GET_MODE_CLASS (ext_mode) == MODE_INT)
10708 reversep = TYPE_REVERSE_STORAGE_ORDER (type);
10710 op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp,
10711 (modifier == EXPAND_STACK_PARM
10712 ? NULL_RTX : target),
10713 ext_mode, ext_mode, reversep, alt_rtl);
10715 /* If the result has a record type and the mode of OP0 is an
10716 integral mode then, if BITSIZE is narrower than this mode
10717 and this is for big-endian data, we must put the field
10718 into the high-order bits. And we must also put it back
10719 into memory order if it has been previously reversed. */
10720 if (TREE_CODE (type) == RECORD_TYPE
10721 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT)
10723 HOST_WIDE_INT size = GET_MODE_BITSIZE (GET_MODE (op0));
10725 if (bitsize < size
10726 && reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
10727 op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10728 size - bitsize, op0, 1);
10730 if (reversep)
10731 op0 = flip_storage_order (GET_MODE (op0), op0);
10734 /* If the result type is BLKmode, store the data into a temporary
10735 of the appropriate type, but with the mode corresponding to the
10736 mode for the data we have (op0's mode). */
10737 if (mode == BLKmode)
10739 rtx new_rtx
10740 = assign_stack_temp_for_type (ext_mode,
10741 GET_MODE_BITSIZE (ext_mode),
10742 type);
10743 emit_move_insn (new_rtx, op0);
10744 op0 = copy_rtx (new_rtx);
10745 PUT_MODE (op0, BLKmode);
10748 return op0;
10751 /* If the result is BLKmode, use that to access the object
10752 now as well. */
10753 if (mode == BLKmode)
10754 mode1 = BLKmode;
10756 /* Get a reference to just this component. */
10757 if (modifier == EXPAND_CONST_ADDRESS
10758 || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10759 op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10760 else
10761 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10763 if (op0 == orig_op0)
10764 op0 = copy_rtx (op0);
10766 /* Don't set memory attributes if the base expression is
10767 SSA_NAME that got expanded as a MEM. In that case, we should
10768 just honor its original memory attributes. */
10769 if (TREE_CODE (tem) != SSA_NAME || !MEM_P (orig_op0))
10770 set_mem_attributes (op0, exp, 0);
10772 if (REG_P (XEXP (op0, 0)))
10773 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10775 /* If op0 is a temporary because the original expressions was forced
10776 to memory, clear MEM_EXPR so that the original expression cannot
10777 be marked as addressable through MEM_EXPR of the temporary. */
10778 if (clear_mem_expr)
10779 set_mem_expr (op0, NULL_TREE);
10781 MEM_VOLATILE_P (op0) |= volatilep;
10783 if (reversep
10784 && modifier != EXPAND_MEMORY
10785 && modifier != EXPAND_WRITE)
10786 op0 = flip_storage_order (mode1, op0);
10788 if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10789 || modifier == EXPAND_CONST_ADDRESS
10790 || modifier == EXPAND_INITIALIZER)
10791 return op0;
10793 if (target == 0)
10794 target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10796 convert_move (target, op0, unsignedp);
10797 return target;
10800 case OBJ_TYPE_REF:
10801 return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10803 case CALL_EXPR:
10804 /* All valid uses of __builtin_va_arg_pack () are removed during
10805 inlining. */
10806 if (CALL_EXPR_VA_ARG_PACK (exp))
10807 error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10809 tree fndecl = get_callee_fndecl (exp), attr;
10811 if (fndecl
10812 && (attr = lookup_attribute ("error",
10813 DECL_ATTRIBUTES (fndecl))) != NULL)
10814 error ("%Kcall to %qs declared with attribute error: %s",
10815 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10816 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10817 if (fndecl
10818 && (attr = lookup_attribute ("warning",
10819 DECL_ATTRIBUTES (fndecl))) != NULL)
10820 warning_at (tree_nonartificial_location (exp),
10821 0, "%Kcall to %qs declared with attribute warning: %s",
10822 exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10823 TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10825 /* Check for a built-in function. */
10826 if (fndecl && DECL_BUILT_IN (fndecl))
10828 gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10829 if (CALL_WITH_BOUNDS_P (exp))
10830 return expand_builtin_with_bounds (exp, target, subtarget,
10831 tmode, ignore);
10832 else
10833 return expand_builtin (exp, target, subtarget, tmode, ignore);
10836 return expand_call (exp, target, ignore);
10838 case VIEW_CONVERT_EXPR:
10839 op0 = NULL_RTX;
10841 /* If we are converting to BLKmode, try to avoid an intermediate
10842 temporary by fetching an inner memory reference. */
10843 if (mode == BLKmode
10844 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
10845 && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10846 && handled_component_p (treeop0))
10848 machine_mode mode1;
10849 HOST_WIDE_INT bitsize, bitpos;
10850 tree offset;
10851 int unsignedp, reversep, volatilep = 0;
10852 tree tem
10853 = get_inner_reference (treeop0, &bitsize, &bitpos, &offset, &mode1,
10854 &unsignedp, &reversep, &volatilep);
10855 rtx orig_op0;
10857 /* ??? We should work harder and deal with non-zero offsets. */
10858 if (!offset
10859 && (bitpos % BITS_PER_UNIT) == 0
10860 && !reversep
10861 && bitsize >= 0
10862 && compare_tree_int (TYPE_SIZE (type), bitsize) == 0)
10864 /* See the normal_inner_ref case for the rationale. */
10865 orig_op0
10866 = expand_expr_real (tem,
10867 (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10868 && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10869 != INTEGER_CST)
10870 && modifier != EXPAND_STACK_PARM
10871 ? target : NULL_RTX),
10872 VOIDmode,
10873 modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier,
10874 NULL, true);
10876 if (MEM_P (orig_op0))
10878 op0 = orig_op0;
10880 /* Get a reference to just this component. */
10881 if (modifier == EXPAND_CONST_ADDRESS
10882 || modifier == EXPAND_SUM
10883 || modifier == EXPAND_INITIALIZER)
10884 op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10885 else
10886 op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10888 if (op0 == orig_op0)
10889 op0 = copy_rtx (op0);
10891 set_mem_attributes (op0, treeop0, 0);
10892 if (REG_P (XEXP (op0, 0)))
10893 mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10895 MEM_VOLATILE_P (op0) |= volatilep;
10900 if (!op0)
10901 op0 = expand_expr_real (treeop0, NULL_RTX, VOIDmode, modifier,
10902 NULL, inner_reference_p);
10904 /* If the input and output modes are both the same, we are done. */
10905 if (mode == GET_MODE (op0))
10907 /* If neither mode is BLKmode, and both modes are the same size
10908 then we can use gen_lowpart. */
10909 else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10910 && (GET_MODE_PRECISION (mode)
10911 == GET_MODE_PRECISION (GET_MODE (op0)))
10912 && !COMPLEX_MODE_P (GET_MODE (op0)))
10914 if (GET_CODE (op0) == SUBREG)
10915 op0 = force_reg (GET_MODE (op0), op0);
10916 temp = gen_lowpart_common (mode, op0);
10917 if (temp)
10918 op0 = temp;
10919 else
10921 if (!REG_P (op0) && !MEM_P (op0))
10922 op0 = force_reg (GET_MODE (op0), op0);
10923 op0 = gen_lowpart (mode, op0);
10926 /* If both types are integral, convert from one mode to the other. */
10927 else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10928 op0 = convert_modes (mode, GET_MODE (op0), op0,
10929 TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10930 /* If the output type is a bit-field type, do an extraction. */
10931 else if (reduce_bit_field)
10932 return extract_bit_field (op0, TYPE_PRECISION (type), 0,
10933 TYPE_UNSIGNED (type), NULL_RTX,
10934 mode, mode, false, NULL);
10935 /* As a last resort, spill op0 to memory, and reload it in a
10936 different mode. */
10937 else if (!MEM_P (op0))
10939 /* If the operand is not a MEM, force it into memory. Since we
10940 are going to be changing the mode of the MEM, don't call
10941 force_const_mem for constants because we don't allow pool
10942 constants to change mode. */
10943 tree inner_type = TREE_TYPE (treeop0);
10945 gcc_assert (!TREE_ADDRESSABLE (exp));
10947 if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10948 target
10949 = assign_stack_temp_for_type
10950 (TYPE_MODE (inner_type),
10951 GET_MODE_SIZE (TYPE_MODE (inner_type)), inner_type);
10953 emit_move_insn (target, op0);
10954 op0 = target;
10957 /* If OP0 is (now) a MEM, we need to deal with alignment issues. If the
10958 output type is such that the operand is known to be aligned, indicate
10959 that it is. Otherwise, we need only be concerned about alignment for
10960 non-BLKmode results. */
10961 if (MEM_P (op0))
10963 enum insn_code icode;
10965 if (modifier != EXPAND_WRITE
10966 && modifier != EXPAND_MEMORY
10967 && !inner_reference_p
10968 && mode != BLKmode
10969 && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10971 /* If the target does have special handling for unaligned
10972 loads of mode then use them. */
10973 if ((icode = optab_handler (movmisalign_optab, mode))
10974 != CODE_FOR_nothing)
10976 rtx reg;
10978 op0 = adjust_address (op0, mode, 0);
10979 /* We've already validated the memory, and we're creating a
10980 new pseudo destination. The predicates really can't
10981 fail. */
10982 reg = gen_reg_rtx (mode);
10984 /* Nor can the insn generator. */
10985 rtx_insn *insn = GEN_FCN (icode) (reg, op0);
10986 emit_insn (insn);
10987 return reg;
10989 else if (STRICT_ALIGNMENT)
10991 tree inner_type = TREE_TYPE (treeop0);
10992 HOST_WIDE_INT temp_size
10993 = MAX (int_size_in_bytes (inner_type),
10994 (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10995 rtx new_rtx
10996 = assign_stack_temp_for_type (mode, temp_size, type);
10997 rtx new_with_op0_mode
10998 = adjust_address (new_rtx, GET_MODE (op0), 0);
11000 gcc_assert (!TREE_ADDRESSABLE (exp));
11002 if (GET_MODE (op0) == BLKmode)
11003 emit_block_move (new_with_op0_mode, op0,
11004 GEN_INT (GET_MODE_SIZE (mode)),
11005 (modifier == EXPAND_STACK_PARM
11006 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
11007 else
11008 emit_move_insn (new_with_op0_mode, op0);
11010 op0 = new_rtx;
11014 op0 = adjust_address (op0, mode, 0);
11017 return op0;
11019 case MODIFY_EXPR:
11021 tree lhs = treeop0;
11022 tree rhs = treeop1;
11023 gcc_assert (ignore);
11025 /* Check for |= or &= of a bitfield of size one into another bitfield
11026 of size 1. In this case, (unless we need the result of the
11027 assignment) we can do this more efficiently with a
11028 test followed by an assignment, if necessary.
11030 ??? At this point, we can't get a BIT_FIELD_REF here. But if
11031 things change so we do, this code should be enhanced to
11032 support it. */
11033 if (TREE_CODE (lhs) == COMPONENT_REF
11034 && (TREE_CODE (rhs) == BIT_IOR_EXPR
11035 || TREE_CODE (rhs) == BIT_AND_EXPR)
11036 && TREE_OPERAND (rhs, 0) == lhs
11037 && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
11038 && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
11039 && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
11041 rtx_code_label *label = gen_label_rtx ();
11042 int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
11043 do_jump (TREE_OPERAND (rhs, 1),
11044 value ? label : 0,
11045 value ? 0 : label, -1);
11046 expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
11047 false);
11048 do_pending_stack_adjust ();
11049 emit_label (label);
11050 return const0_rtx;
11053 expand_assignment (lhs, rhs, false);
11054 return const0_rtx;
11057 case ADDR_EXPR:
11058 return expand_expr_addr_expr (exp, target, tmode, modifier);
11060 case REALPART_EXPR:
11061 op0 = expand_normal (treeop0);
11062 return read_complex_part (op0, false);
11064 case IMAGPART_EXPR:
11065 op0 = expand_normal (treeop0);
11066 return read_complex_part (op0, true);
11068 case RETURN_EXPR:
11069 case LABEL_EXPR:
11070 case GOTO_EXPR:
11071 case SWITCH_EXPR:
11072 case ASM_EXPR:
11073 /* Expanded in cfgexpand.c. */
11074 gcc_unreachable ();
11076 case TRY_CATCH_EXPR:
11077 case CATCH_EXPR:
11078 case EH_FILTER_EXPR:
11079 case TRY_FINALLY_EXPR:
11080 /* Lowered by tree-eh.c. */
11081 gcc_unreachable ();
11083 case WITH_CLEANUP_EXPR:
11084 case CLEANUP_POINT_EXPR:
11085 case TARGET_EXPR:
11086 case CASE_LABEL_EXPR:
11087 case VA_ARG_EXPR:
11088 case BIND_EXPR:
11089 case INIT_EXPR:
11090 case CONJ_EXPR:
11091 case COMPOUND_EXPR:
11092 case PREINCREMENT_EXPR:
11093 case PREDECREMENT_EXPR:
11094 case POSTINCREMENT_EXPR:
11095 case POSTDECREMENT_EXPR:
11096 case LOOP_EXPR:
11097 case EXIT_EXPR:
11098 case COMPOUND_LITERAL_EXPR:
11099 /* Lowered by gimplify.c. */
11100 gcc_unreachable ();
11102 case FDESC_EXPR:
11103 /* Function descriptors are not valid except for as
11104 initialization constants, and should not be expanded. */
11105 gcc_unreachable ();
11107 case WITH_SIZE_EXPR:
11108 /* WITH_SIZE_EXPR expands to its first argument. The caller should
11109 have pulled out the size to use in whatever context it needed. */
11110 return expand_expr_real (treeop0, original_target, tmode,
11111 modifier, alt_rtl, inner_reference_p);
11113 default:
11114 return expand_expr_real_2 (&ops, target, tmode, modifier);
11118 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
11119 signedness of TYPE), possibly returning the result in TARGET. */
11120 static rtx
11121 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
11123 HOST_WIDE_INT prec = TYPE_PRECISION (type);
11124 if (target && GET_MODE (target) != GET_MODE (exp))
11125 target = 0;
11126 /* For constant values, reduce using build_int_cst_type. */
11127 if (CONST_INT_P (exp))
11129 HOST_WIDE_INT value = INTVAL (exp);
11130 tree t = build_int_cst_type (type, value);
11131 return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
11133 else if (TYPE_UNSIGNED (type))
11135 machine_mode mode = GET_MODE (exp);
11136 rtx mask = immed_wide_int_const
11137 (wi::mask (prec, false, GET_MODE_PRECISION (mode)), mode);
11138 return expand_and (mode, exp, mask, target);
11140 else
11142 int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
11143 exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
11144 exp, count, target, 0);
11145 return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
11146 exp, count, target, 0);
11150 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
11151 when applied to the address of EXP produces an address known to be
11152 aligned more than BIGGEST_ALIGNMENT. */
11154 static int
11155 is_aligning_offset (const_tree offset, const_tree exp)
11157 /* Strip off any conversions. */
11158 while (CONVERT_EXPR_P (offset))
11159 offset = TREE_OPERAND (offset, 0);
11161 /* We must now have a BIT_AND_EXPR with a constant that is one less than
11162 power of 2 and which is larger than BIGGEST_ALIGNMENT. */
11163 if (TREE_CODE (offset) != BIT_AND_EXPR
11164 || !tree_fits_uhwi_p (TREE_OPERAND (offset, 1))
11165 || compare_tree_int (TREE_OPERAND (offset, 1),
11166 BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
11167 || !pow2p_hwi (tree_to_uhwi (TREE_OPERAND (offset, 1)) + 1))
11168 return 0;
11170 /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
11171 It must be NEGATE_EXPR. Then strip any more conversions. */
11172 offset = TREE_OPERAND (offset, 0);
11173 while (CONVERT_EXPR_P (offset))
11174 offset = TREE_OPERAND (offset, 0);
11176 if (TREE_CODE (offset) != NEGATE_EXPR)
11177 return 0;
11179 offset = TREE_OPERAND (offset, 0);
11180 while (CONVERT_EXPR_P (offset))
11181 offset = TREE_OPERAND (offset, 0);
11183 /* This must now be the address of EXP. */
11184 return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
11187 /* Return the tree node if an ARG corresponds to a string constant or zero
11188 if it doesn't. If we return nonzero, set *PTR_OFFSET to the offset
11189 in bytes within the string that ARG is accessing. The type of the
11190 offset will be `sizetype'. */
11192 tree
11193 string_constant (tree arg, tree *ptr_offset)
11195 tree array, offset, lower_bound;
11196 STRIP_NOPS (arg);
11198 if (TREE_CODE (arg) == ADDR_EXPR)
11200 if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
11202 *ptr_offset = size_zero_node;
11203 return TREE_OPERAND (arg, 0);
11205 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
11207 array = TREE_OPERAND (arg, 0);
11208 offset = size_zero_node;
11210 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
11212 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11213 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11214 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11215 return 0;
11217 /* Check if the array has a nonzero lower bound. */
11218 lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
11219 if (!integer_zerop (lower_bound))
11221 /* If the offset and base aren't both constants, return 0. */
11222 if (TREE_CODE (lower_bound) != INTEGER_CST)
11223 return 0;
11224 if (TREE_CODE (offset) != INTEGER_CST)
11225 return 0;
11226 /* Adjust offset by the lower bound. */
11227 offset = size_diffop (fold_convert (sizetype, offset),
11228 fold_convert (sizetype, lower_bound));
11231 else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
11233 array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
11234 offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
11235 if (TREE_CODE (array) != ADDR_EXPR)
11236 return 0;
11237 array = TREE_OPERAND (array, 0);
11238 if (TREE_CODE (array) != STRING_CST && !VAR_P (array))
11239 return 0;
11241 else
11242 return 0;
11244 else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
11246 tree arg0 = TREE_OPERAND (arg, 0);
11247 tree arg1 = TREE_OPERAND (arg, 1);
11249 STRIP_NOPS (arg0);
11250 STRIP_NOPS (arg1);
11252 if (TREE_CODE (arg0) == ADDR_EXPR
11253 && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
11254 || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
11256 array = TREE_OPERAND (arg0, 0);
11257 offset = arg1;
11259 else if (TREE_CODE (arg1) == ADDR_EXPR
11260 && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
11261 || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
11263 array = TREE_OPERAND (arg1, 0);
11264 offset = arg0;
11266 else
11267 return 0;
11269 else
11270 return 0;
11272 if (TREE_CODE (array) == STRING_CST)
11274 *ptr_offset = fold_convert (sizetype, offset);
11275 return array;
11277 else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
11279 int length;
11280 tree init = ctor_for_folding (array);
11282 /* Variables initialized to string literals can be handled too. */
11283 if (init == error_mark_node
11284 || !init
11285 || TREE_CODE (init) != STRING_CST)
11286 return 0;
11288 /* Avoid const char foo[4] = "abcde"; */
11289 if (DECL_SIZE_UNIT (array) == NULL_TREE
11290 || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
11291 || (length = TREE_STRING_LENGTH (init)) <= 0
11292 || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
11293 return 0;
11295 /* If variable is bigger than the string literal, OFFSET must be constant
11296 and inside of the bounds of the string literal. */
11297 offset = fold_convert (sizetype, offset);
11298 if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
11299 && (! tree_fits_uhwi_p (offset)
11300 || compare_tree_int (offset, length) >= 0))
11301 return 0;
11303 *ptr_offset = offset;
11304 return init;
11307 return 0;
11310 /* Generate code to calculate OPS, and exploded expression
11311 using a store-flag instruction and return an rtx for the result.
11312 OPS reflects a comparison.
11314 If TARGET is nonzero, store the result there if convenient.
11316 Return zero if there is no suitable set-flag instruction
11317 available on this machine.
11319 Once expand_expr has been called on the arguments of the comparison,
11320 we are committed to doing the store flag, since it is not safe to
11321 re-evaluate the expression. We emit the store-flag insn by calling
11322 emit_store_flag, but only expand the arguments if we have a reason
11323 to believe that emit_store_flag will be successful. If we think that
11324 it will, but it isn't, we have to simulate the store-flag with a
11325 set/jump/set sequence. */
11327 static rtx
11328 do_store_flag (sepops ops, rtx target, machine_mode mode)
11330 enum rtx_code code;
11331 tree arg0, arg1, type;
11332 machine_mode operand_mode;
11333 int unsignedp;
11334 rtx op0, op1;
11335 rtx subtarget = target;
11336 location_t loc = ops->location;
11338 arg0 = ops->op0;
11339 arg1 = ops->op1;
11341 /* Don't crash if the comparison was erroneous. */
11342 if (arg0 == error_mark_node || arg1 == error_mark_node)
11343 return const0_rtx;
11345 type = TREE_TYPE (arg0);
11346 operand_mode = TYPE_MODE (type);
11347 unsignedp = TYPE_UNSIGNED (type);
11349 /* We won't bother with BLKmode store-flag operations because it would mean
11350 passing a lot of information to emit_store_flag. */
11351 if (operand_mode == BLKmode)
11352 return 0;
11354 /* We won't bother with store-flag operations involving function pointers
11355 when function pointers must be canonicalized before comparisons. */
11356 if (targetm.have_canonicalize_funcptr_for_compare ()
11357 && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
11358 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
11359 == FUNCTION_TYPE))
11360 || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
11361 && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
11362 == FUNCTION_TYPE))))
11363 return 0;
11365 STRIP_NOPS (arg0);
11366 STRIP_NOPS (arg1);
11368 /* For vector typed comparisons emit code to generate the desired
11369 all-ones or all-zeros mask. Conveniently use the VEC_COND_EXPR
11370 expander for this. */
11371 if (TREE_CODE (ops->type) == VECTOR_TYPE)
11373 tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
11374 if (VECTOR_BOOLEAN_TYPE_P (ops->type)
11375 && expand_vec_cmp_expr_p (TREE_TYPE (arg0), ops->type, ops->code))
11376 return expand_vec_cmp_expr (ops->type, ifexp, target);
11377 else
11379 tree if_true = constant_boolean_node (true, ops->type);
11380 tree if_false = constant_boolean_node (false, ops->type);
11381 return expand_vec_cond_expr (ops->type, ifexp, if_true,
11382 if_false, target);
11386 /* Get the rtx comparison code to use. We know that EXP is a comparison
11387 operation of some type. Some comparisons against 1 and -1 can be
11388 converted to comparisons with zero. Do so here so that the tests
11389 below will be aware that we have a comparison with zero. These
11390 tests will not catch constants in the first operand, but constants
11391 are rarely passed as the first operand. */
11393 switch (ops->code)
11395 case EQ_EXPR:
11396 code = EQ;
11397 break;
11398 case NE_EXPR:
11399 code = NE;
11400 break;
11401 case LT_EXPR:
11402 if (integer_onep (arg1))
11403 arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
11404 else
11405 code = unsignedp ? LTU : LT;
11406 break;
11407 case LE_EXPR:
11408 if (! unsignedp && integer_all_onesp (arg1))
11409 arg1 = integer_zero_node, code = LT;
11410 else
11411 code = unsignedp ? LEU : LE;
11412 break;
11413 case GT_EXPR:
11414 if (! unsignedp && integer_all_onesp (arg1))
11415 arg1 = integer_zero_node, code = GE;
11416 else
11417 code = unsignedp ? GTU : GT;
11418 break;
11419 case GE_EXPR:
11420 if (integer_onep (arg1))
11421 arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
11422 else
11423 code = unsignedp ? GEU : GE;
11424 break;
11426 case UNORDERED_EXPR:
11427 code = UNORDERED;
11428 break;
11429 case ORDERED_EXPR:
11430 code = ORDERED;
11431 break;
11432 case UNLT_EXPR:
11433 code = UNLT;
11434 break;
11435 case UNLE_EXPR:
11436 code = UNLE;
11437 break;
11438 case UNGT_EXPR:
11439 code = UNGT;
11440 break;
11441 case UNGE_EXPR:
11442 code = UNGE;
11443 break;
11444 case UNEQ_EXPR:
11445 code = UNEQ;
11446 break;
11447 case LTGT_EXPR:
11448 code = LTGT;
11449 break;
11451 default:
11452 gcc_unreachable ();
11455 /* Put a constant second. */
11456 if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
11457 || TREE_CODE (arg0) == FIXED_CST)
11459 std::swap (arg0, arg1);
11460 code = swap_condition (code);
11463 /* If this is an equality or inequality test of a single bit, we can
11464 do this by shifting the bit being tested to the low-order bit and
11465 masking the result with the constant 1. If the condition was EQ,
11466 we xor it with 1. This does not require an scc insn and is faster
11467 than an scc insn even if we have it.
11469 The code to make this transformation was moved into fold_single_bit_test,
11470 so we just call into the folder and expand its result. */
11472 if ((code == NE || code == EQ)
11473 && integer_zerop (arg1)
11474 && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
11476 gimple *srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
11477 if (srcstmt
11478 && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
11480 enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
11481 tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11482 tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
11483 gimple_assign_rhs1 (srcstmt),
11484 gimple_assign_rhs2 (srcstmt));
11485 temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
11486 if (temp)
11487 return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
11491 if (! get_subtarget (target)
11492 || GET_MODE (subtarget) != operand_mode)
11493 subtarget = 0;
11495 expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
11497 if (target == 0)
11498 target = gen_reg_rtx (mode);
11500 /* Try a cstore if possible. */
11501 return emit_store_flag_force (target, code, op0, op1,
11502 operand_mode, unsignedp,
11503 (TYPE_PRECISION (ops->type) == 1
11504 && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
11507 /* Attempt to generate a casesi instruction. Returns 1 if successful,
11508 0 otherwise (i.e. if there is no casesi instruction).
11510 DEFAULT_PROBABILITY is the probability of jumping to the default
11511 label. */
11513 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
11514 rtx table_label, rtx default_label, rtx fallback_label,
11515 int default_probability)
11517 struct expand_operand ops[5];
11518 machine_mode index_mode = SImode;
11519 rtx op1, op2, index;
11521 if (! targetm.have_casesi ())
11522 return 0;
11524 /* Convert the index to SImode. */
11525 if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
11527 machine_mode omode = TYPE_MODE (index_type);
11528 rtx rangertx = expand_normal (range);
11530 /* We must handle the endpoints in the original mode. */
11531 index_expr = build2 (MINUS_EXPR, index_type,
11532 index_expr, minval);
11533 minval = integer_zero_node;
11534 index = expand_normal (index_expr);
11535 if (default_label)
11536 emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
11537 omode, 1, default_label,
11538 default_probability);
11539 /* Now we can safely truncate. */
11540 index = convert_to_mode (index_mode, index, 0);
11542 else
11544 if (TYPE_MODE (index_type) != index_mode)
11546 index_type = lang_hooks.types.type_for_mode (index_mode, 0);
11547 index_expr = fold_convert (index_type, index_expr);
11550 index = expand_normal (index_expr);
11553 do_pending_stack_adjust ();
11555 op1 = expand_normal (minval);
11556 op2 = expand_normal (range);
11558 create_input_operand (&ops[0], index, index_mode);
11559 create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
11560 create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
11561 create_fixed_operand (&ops[3], table_label);
11562 create_fixed_operand (&ops[4], (default_label
11563 ? default_label
11564 : fallback_label));
11565 expand_jump_insn (targetm.code_for_casesi, 5, ops);
11566 return 1;
11569 /* Attempt to generate a tablejump instruction; same concept. */
11570 /* Subroutine of the next function.
11572 INDEX is the value being switched on, with the lowest value
11573 in the table already subtracted.
11574 MODE is its expected mode (needed if INDEX is constant).
11575 RANGE is the length of the jump table.
11576 TABLE_LABEL is a CODE_LABEL rtx for the table itself.
11578 DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
11579 index value is out of range.
11580 DEFAULT_PROBABILITY is the probability of jumping to
11581 the default label. */
11583 static void
11584 do_tablejump (rtx index, machine_mode mode, rtx range, rtx table_label,
11585 rtx default_label, int default_probability)
11587 rtx temp, vector;
11589 if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
11590 cfun->cfg->max_jumptable_ents = INTVAL (range);
11592 /* Do an unsigned comparison (in the proper mode) between the index
11593 expression and the value which represents the length of the range.
11594 Since we just finished subtracting the lower bound of the range
11595 from the index expression, this comparison allows us to simultaneously
11596 check that the original index expression value is both greater than
11597 or equal to the minimum value of the range and less than or equal to
11598 the maximum value of the range. */
11600 if (default_label)
11601 emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
11602 default_label, default_probability);
11605 /* If index is in range, it must fit in Pmode.
11606 Convert to Pmode so we can index with it. */
11607 if (mode != Pmode)
11608 index = convert_to_mode (Pmode, index, 1);
11610 /* Don't let a MEM slip through, because then INDEX that comes
11611 out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
11612 and break_out_memory_refs will go to work on it and mess it up. */
11613 #ifdef PIC_CASE_VECTOR_ADDRESS
11614 if (flag_pic && !REG_P (index))
11615 index = copy_to_mode_reg (Pmode, index);
11616 #endif
11618 /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
11619 GET_MODE_SIZE, because this indicates how large insns are. The other
11620 uses should all be Pmode, because they are addresses. This code
11621 could fail if addresses and insns are not the same size. */
11622 index = simplify_gen_binary (MULT, Pmode, index,
11623 gen_int_mode (GET_MODE_SIZE (CASE_VECTOR_MODE),
11624 Pmode));
11625 index = simplify_gen_binary (PLUS, Pmode, index,
11626 gen_rtx_LABEL_REF (Pmode, table_label));
11628 #ifdef PIC_CASE_VECTOR_ADDRESS
11629 if (flag_pic)
11630 index = PIC_CASE_VECTOR_ADDRESS (index);
11631 else
11632 #endif
11633 index = memory_address (CASE_VECTOR_MODE, index);
11634 temp = gen_reg_rtx (CASE_VECTOR_MODE);
11635 vector = gen_const_mem (CASE_VECTOR_MODE, index);
11636 convert_move (temp, vector, 0);
11638 emit_jump_insn (targetm.gen_tablejump (temp, table_label));
11640 /* If we are generating PIC code or if the table is PC-relative, the
11641 table and JUMP_INSN must be adjacent, so don't output a BARRIER. */
11642 if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
11643 emit_barrier ();
11647 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11648 rtx table_label, rtx default_label, int default_probability)
11650 rtx index;
11652 if (! targetm.have_tablejump ())
11653 return 0;
11655 index_expr = fold_build2 (MINUS_EXPR, index_type,
11656 fold_convert (index_type, index_expr),
11657 fold_convert (index_type, minval));
11658 index = expand_normal (index_expr);
11659 do_pending_stack_adjust ();
11661 do_tablejump (index, TYPE_MODE (index_type),
11662 convert_modes (TYPE_MODE (index_type),
11663 TYPE_MODE (TREE_TYPE (range)),
11664 expand_normal (range),
11665 TYPE_UNSIGNED (TREE_TYPE (range))),
11666 table_label, default_label, default_probability);
11667 return 1;
11670 /* Return a CONST_VECTOR rtx representing vector mask for
11671 a VECTOR_CST of booleans. */
11672 static rtx
11673 const_vector_mask_from_tree (tree exp)
11675 rtvec v;
11676 unsigned i;
11677 int units;
11678 tree elt;
11679 machine_mode inner, mode;
11681 mode = TYPE_MODE (TREE_TYPE (exp));
11682 units = GET_MODE_NUNITS (mode);
11683 inner = GET_MODE_INNER (mode);
11685 v = rtvec_alloc (units);
11687 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11689 elt = VECTOR_CST_ELT (exp, i);
11691 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11692 if (integer_zerop (elt))
11693 RTVEC_ELT (v, i) = CONST0_RTX (inner);
11694 else if (integer_onep (elt)
11695 || integer_minus_onep (elt))
11696 RTVEC_ELT (v, i) = CONSTM1_RTX (inner);
11697 else
11698 gcc_unreachable ();
11701 return gen_rtx_CONST_VECTOR (mode, v);
11704 /* Return a CONST_INT rtx representing vector mask for
11705 a VECTOR_CST of booleans. */
11706 static rtx
11707 const_scalar_mask_from_tree (tree exp)
11709 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
11710 wide_int res = wi::zero (GET_MODE_PRECISION (mode));
11711 tree elt;
11712 unsigned i;
11714 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11716 elt = VECTOR_CST_ELT (exp, i);
11717 gcc_assert (TREE_CODE (elt) == INTEGER_CST);
11718 if (integer_all_onesp (elt))
11719 res = wi::set_bit (res, i);
11720 else
11721 gcc_assert (integer_zerop (elt));
11724 return immed_wide_int_const (res, mode);
11727 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree. */
11728 static rtx
11729 const_vector_from_tree (tree exp)
11731 rtvec v;
11732 unsigned i;
11733 int units;
11734 tree elt;
11735 machine_mode inner, mode;
11737 mode = TYPE_MODE (TREE_TYPE (exp));
11739 if (initializer_zerop (exp))
11740 return CONST0_RTX (mode);
11742 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (exp)))
11743 return const_vector_mask_from_tree (exp);
11745 units = GET_MODE_NUNITS (mode);
11746 inner = GET_MODE_INNER (mode);
11748 v = rtvec_alloc (units);
11750 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11752 elt = VECTOR_CST_ELT (exp, i);
11754 if (TREE_CODE (elt) == REAL_CST)
11755 RTVEC_ELT (v, i) = const_double_from_real_value (TREE_REAL_CST (elt),
11756 inner);
11757 else if (TREE_CODE (elt) == FIXED_CST)
11758 RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11759 inner);
11760 else
11761 RTVEC_ELT (v, i) = immed_wide_int_const (elt, inner);
11764 return gen_rtx_CONST_VECTOR (mode, v);
11767 /* Build a decl for a personality function given a language prefix. */
11769 tree
11770 build_personality_function (const char *lang)
11772 const char *unwind_and_version;
11773 tree decl, type;
11774 char *name;
11776 switch (targetm_common.except_unwind_info (&global_options))
11778 case UI_NONE:
11779 return NULL;
11780 case UI_SJLJ:
11781 unwind_and_version = "_sj0";
11782 break;
11783 case UI_DWARF2:
11784 case UI_TARGET:
11785 unwind_and_version = "_v0";
11786 break;
11787 case UI_SEH:
11788 unwind_and_version = "_seh0";
11789 break;
11790 default:
11791 gcc_unreachable ();
11794 name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11796 type = build_function_type_list (integer_type_node, integer_type_node,
11797 long_long_unsigned_type_node,
11798 ptr_type_node, ptr_type_node, NULL_TREE);
11799 decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11800 get_identifier (name), type);
11801 DECL_ARTIFICIAL (decl) = 1;
11802 DECL_EXTERNAL (decl) = 1;
11803 TREE_PUBLIC (decl) = 1;
11805 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
11806 are the flags assigned by targetm.encode_section_info. */
11807 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11809 return decl;
11812 /* Extracts the personality function of DECL and returns the corresponding
11813 libfunc. */
11816 get_personality_function (tree decl)
11818 tree personality = DECL_FUNCTION_PERSONALITY (decl);
11819 enum eh_personality_kind pk;
11821 pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11822 if (pk == eh_personality_none)
11823 return NULL;
11825 if (!personality
11826 && pk == eh_personality_any)
11827 personality = lang_hooks.eh_personality ();
11829 if (pk == eh_personality_lang)
11830 gcc_assert (personality != NULL_TREE);
11832 return XEXP (DECL_RTL (personality), 0);
11835 /* Returns a tree for the size of EXP in bytes. */
11837 static tree
11838 tree_expr_size (const_tree exp)
11840 if (DECL_P (exp)
11841 && DECL_SIZE_UNIT (exp) != 0)
11842 return DECL_SIZE_UNIT (exp);
11843 else
11844 return size_in_bytes (TREE_TYPE (exp));
11847 /* Return an rtx for the size in bytes of the value of EXP. */
11850 expr_size (tree exp)
11852 tree size;
11854 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11855 size = TREE_OPERAND (exp, 1);
11856 else
11858 size = tree_expr_size (exp);
11859 gcc_assert (size);
11860 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
11863 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
11866 /* Return a wide integer for the size in bytes of the value of EXP, or -1
11867 if the size can vary or is larger than an integer. */
11869 static HOST_WIDE_INT
11870 int_expr_size (tree exp)
11872 tree size;
11874 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
11875 size = TREE_OPERAND (exp, 1);
11876 else
11878 size = tree_expr_size (exp);
11879 gcc_assert (size);
11882 if (size == 0 || !tree_fits_shwi_p (size))
11883 return -1;
11885 return tree_to_shwi (size);